fix auto-approve & debug print

This commit is contained in:
jem 2020-03-25 15:08:11 +01:00
parent 89824db10b
commit b2265a1fe7
4 changed files with 66 additions and 34 deletions

View file

@ -28,7 +28,7 @@ use_plugin("python.distutils")
default_task = "publish" default_task = "publish"
name = "ddadevops" name = "ddadevops"
version = "0.4.2.dev11" version = "0.4.2.dev27"
summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud"
description = __doc__ description = __doc__
authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")]

View file

@ -6,7 +6,7 @@ terraform, dda-pallet, aws & hetzner-cloud.
from .credential import gopass_credential_from_env_path, gopass_credential_from_path from .credential import gopass_credential_from_env_path, gopass_credential_from_path
from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build
from .devops_terraform_build import DevopsTerraformBuild, create_devops_terraform_build_config from .devops_terraform_build import WorkaroundTerraform, DevopsTerraformBuild, create_devops_terraform_build_config
from .hetzner_mixin import HetznerMixin, add_hetzner_mixin_config from .hetzner_mixin import HetznerMixin, add_hetzner_mixin_config
from .aws_backend_properties_mixin import AwsBackendPropertiesMixin, add_aws_backend_properties_mixin_config from .aws_backend_properties_mixin import AwsBackendPropertiesMixin, add_aws_backend_properties_mixin_config
from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config

View file

@ -1,5 +1,5 @@
from python_terraform import * from python_terraform import *
from .devops_terraform_build import DevopsTerraformBuild from .devops_terraform_build import DevopsTerraformBuild, WorkaroundTerraform
def add_aws_backend_properties_mixin_config(config, account_name): def add_aws_backend_properties_mixin_config(config, account_name):
@ -14,7 +14,7 @@ class AwsBackendPropertiesMixin(DevopsTerraformBuild):
super().__init__(project, config) super().__init__(project, config)
aws_mixin_config = config['AwsBackendPropertiesMixin'] aws_mixin_config = config['AwsBackendPropertiesMixin']
self.account_name = aws_mixin_config['account_name'] self.account_name = aws_mixin_config['account_name']
def backend_config(self): def backend_config(self):
return "backend." + self.account_name + "." + self.stage + ".properties" return "backend." + self.account_name + "." + self.stage + ".properties"
@ -26,35 +26,48 @@ class AwsBackendPropertiesMixin(DevopsTerraformBuild):
def copy_build_resources_from_package(self): def copy_build_resources_from_package(self):
super().copy_build_resources_from_package() super().copy_build_resources_from_package()
self.copy_build_resource_file_from_package('aws_provider.tf') self.copy_build_resource_file_from_package('aws_provider.tf')
self.copy_build_resource_file_from_package('aws_backend_properties_vars.tf') self.copy_build_resource_file_from_package(
self.copy_build_resource_file_from_package('aws_backend_with_properties.tf') 'aws_backend_properties_vars.tf')
self.copy_build_resource_file_from_package(
'aws_backend_with_properties.tf')
def init_client(self): def init_client(self):
tf = Terraform(working_dir=self.build_path()) tf = WorkaroundTerraform(working_dir=self.build_path())
self.print_terraform_command('init --backend-config=' + self.backend_config())
tf.init(backend_config=self.backend_config()) tf.init(backend_config=self.backend_config())
self.print_terraform_command(tf)
if self.use_workspace: if self.use_workspace:
try: try:
tf.workspace('select', slef.stage) tf.workspace('select', slef.stage)
self.print_terraform_command(tf)
except: except:
tf.workspace('new', self.stage) tf.workspace('new', self.stage)
self.print_terraform_command(tf)
return tf return tf
def plan(self): def plan(self):
tf = self.init_client() tf = self.init_client()
self.print_terraform_command('plan --var-file=' + self.backend_config()) tf.plan(capture_output=False, raise_on_error=True,
tf.plan(capture_output=False, var=self.project_vars(), var=self.project_vars(),
var_file=self.backend_config()) var_file=self.backend_config())
self.print_terraform_command(tf)
def apply(self, p_auto_approve=False): def apply(self, auto_approve=False):
tf = self.init_client() tf = self.init_client()
self.print_terraform_command('apply --var-file=' + self.backend_config()) tf.apply(capture_output=False, raise_on_error=True,
tf.apply(capture_output=False, auto_approve=p_auto_approve, skip_plan=auto_approve,
var=self.project_vars(), var_file=self.backend_config()) var=self.project_vars(),
var_file=self.backend_config())
self.print_terraform_command(tf)
self.write_output(tf) self.write_output(tf)
def destroy(self, p_auto_approve=False): def destroy(self, auto_approve=False):
tf = self.init_client() tf = self.init_client()
self.print_terraform_command('destroy --var-file=' + self.backend_config()) if auto_approve:
tf.destroy(capture_output=False, auto_approve=p_auto_approve, force = IsFlagged
var=self.project_vars(), var_file=self.backend_config()) else:
force = None
tf.destroy(capture_output=False, raise_on_error=True,
force=force,
var=self.project_vars(),
var_file=self.backend_config())
self.print_terraform_command(tf)

View file

@ -25,7 +25,21 @@ def create_devops_terraform_build_config(stage, project_root_path, build_commons
'debug_print_terraform_command': debug_print_terraform_command}) 'debug_print_terraform_command': debug_print_terraform_command})
return ret return ret
class WorkaroundTerraform(Terraform): class WorkaroundTerraform(Terraform):
def __init__(self, working_dir=None,
targets=None,
state=None,
variables=None,
parallelism=None,
var_file=None,
terraform_bin_path=None,
is_env_vars_included=True,
):
super().__init__(working_dir, targets, state, variables, parallelism,
var_file, terraform_bin_path, is_env_vars_included)
self.latest_cmd = ''
def apply(self, dir_or_plan=None, input=False, skip_plan=False, no_color=IsFlagged, def apply(self, dir_or_plan=None, input=False, skip_plan=False, no_color=IsFlagged,
**kwargs): **kwargs):
""" """
@ -41,17 +55,18 @@ class WorkaroundTerraform(Terraform):
default = kwargs default = kwargs
default['input'] = input default['input'] = input
default['no_color'] = no_color default['no_color'] = no_color
if skip_plan:
default['auto-approve'] = IsFlagged
else:
default['auto-approve'] = None
option_dict = self._generate_default_options(default) option_dict = self._generate_default_options(default)
default['auto-approve'] = (skip_plan == True)
args = self._generate_default_args(dir_or_plan) args = self._generate_default_args(dir_or_plan)
return self.cmd('apply', *args, **option_dict) return self.cmd('apply', *args, **option_dict)
def generate_cmd_string(self, cmd, *args, **kwargs): def generate_cmd_string(self, cmd, *args, **kwargs):
self.latest_cmd = super().generate_cmd_string(self, cmd, *args, **kwargs) result = super().generate_cmd_string(cmd, *args, **kwargs)
return self.latest_cmd self.latest_cmd = ' '.join(result)
return result
def latest_cmd(self):
return self.latest_cmd
class DevopsTerraformBuild(DevopsBuild): class DevopsTerraformBuild(DevopsBuild):
@ -78,9 +93,10 @@ class DevopsTerraformBuild(DevopsBuild):
if self.additional_vars: if self.additional_vars:
ret.update(self.additional_vars) ret.update(self.additional_vars)
return ret return ret
def copy_build_resource_file_from_package(self, name): def copy_build_resource_file_from_package(self, name):
my_data = resource_string(__name__, "src/main/resources/terraform/" + name) my_data = resource_string(
__name__, "src/main/resources/terraform/" + name)
with open(self.build_path() + '/' + name, "w") as output_file: with open(self.build_path() + '/' + name, "w") as output_file:
output_file.write(my_data.decode(sys.stdout.encoding)) output_file.write(my_data.decode(sys.stdout.encoding))
@ -127,24 +143,27 @@ class DevopsTerraformBuild(DevopsBuild):
def plan(self): def plan(self):
tf = self.init_client() tf = self.init_client()
tf.plan(capture_output=False, raise_on_error=True, var=self.project_vars()) tf.plan(capture_output=False, raise_on_error=True,
var=self.project_vars())
self.print_terraform_command(tf) self.print_terraform_command(tf)
def apply(self, auto_approve=False): def apply(self, auto_approve=False):
tf = self.init_client() tf = self.init_client()
tf.apply(capture_output=False, raise_on_error=True, skip_plan=auto_approve, tf.apply(capture_output=False, raise_on_error=True,
var=self.project_vars()) skip_plan=auto_approve,
var=self.project_vars())
self.print_terraform_command(tf) self.print_terraform_command(tf)
self.write_output(tf) self.write_output(tf)
def destroy(self, auto_approve=False): def destroy(self, auto_approve=False):
tf = self.init_client() tf = self.init_client()
if auto_approve: if auto_approve:
force=IsFlagged force = IsFlagged
else: else:
force=None force = None
tf.destroy(capture_output=False, raise_on_error=True, force=force, tf.destroy(capture_output=False, raise_on_error=True,
var=self.project_vars()) force=force,
var=self.project_vars())
self.print_terraform_command(tf) self.print_terraform_command(tf)
def tf_import(self, tf_import_name, tf_import_resource,): def tf_import(self, tf_import_name, tf_import_resource,):
@ -155,5 +174,5 @@ class DevopsTerraformBuild(DevopsBuild):
def print_terraform_command(self, tf): def print_terraform_command(self, tf):
if self.debug_print_terraform_command: if self.debug_print_terraform_command:
output = 'cd ' + self.build_path() + ' ' + tf.latest_cmd() output = 'cd ' + self.build_path() + ' && ' + tf.latest_cmd
print(output) print(output)