fix auto-approve & debug print
This commit is contained in:
parent
89824db10b
commit
b2265a1fe7
4 changed files with 66 additions and 34 deletions
2
build.py
2
build.py
|
@ -28,7 +28,7 @@ use_plugin("python.distutils")
|
|||
default_task = "publish"
|
||||
|
||||
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"
|
||||
description = __doc__
|
||||
authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")]
|
||||
|
|
|
@ -6,7 +6,7 @@ terraform, dda-pallet, aws & hetzner-cloud.
|
|||
|
||||
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_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 .aws_backend_properties_mixin import AwsBackendPropertiesMixin, add_aws_backend_properties_mixin_config
|
||||
from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
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):
|
||||
|
@ -26,35 +26,48 @@ class AwsBackendPropertiesMixin(DevopsTerraformBuild):
|
|||
def copy_build_resources_from_package(self):
|
||||
super().copy_build_resources_from_package()
|
||||
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('aws_backend_with_properties.tf')
|
||||
self.copy_build_resource_file_from_package(
|
||||
'aws_backend_properties_vars.tf')
|
||||
self.copy_build_resource_file_from_package(
|
||||
'aws_backend_with_properties.tf')
|
||||
|
||||
def init_client(self):
|
||||
tf = Terraform(working_dir=self.build_path())
|
||||
self.print_terraform_command('init --backend-config=' + self.backend_config())
|
||||
tf = WorkaroundTerraform(working_dir=self.build_path())
|
||||
tf.init(backend_config=self.backend_config())
|
||||
self.print_terraform_command(tf)
|
||||
if self.use_workspace:
|
||||
try:
|
||||
tf.workspace('select', slef.stage)
|
||||
self.print_terraform_command(tf)
|
||||
except:
|
||||
tf.workspace('new', self.stage)
|
||||
self.print_terraform_command(tf)
|
||||
return tf
|
||||
|
||||
def plan(self):
|
||||
tf = self.init_client()
|
||||
self.print_terraform_command('plan --var-file=' + self.backend_config())
|
||||
tf.plan(capture_output=False, var=self.project_vars(),
|
||||
tf.plan(capture_output=False, raise_on_error=True,
|
||||
var=self.project_vars(),
|
||||
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()
|
||||
self.print_terraform_command('apply --var-file=' + self.backend_config())
|
||||
tf.apply(capture_output=False, auto_approve=p_auto_approve,
|
||||
var=self.project_vars(), var_file=self.backend_config())
|
||||
tf.apply(capture_output=False, raise_on_error=True,
|
||||
skip_plan=auto_approve,
|
||||
var=self.project_vars(),
|
||||
var_file=self.backend_config())
|
||||
self.print_terraform_command(tf)
|
||||
self.write_output(tf)
|
||||
|
||||
def destroy(self, p_auto_approve=False):
|
||||
def destroy(self, auto_approve=False):
|
||||
tf = self.init_client()
|
||||
self.print_terraform_command('destroy --var-file=' + self.backend_config())
|
||||
tf.destroy(capture_output=False, auto_approve=p_auto_approve,
|
||||
var=self.project_vars(), var_file=self.backend_config())
|
||||
if auto_approve:
|
||||
force = IsFlagged
|
||||
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)
|
||||
|
|
|
@ -25,7 +25,21 @@ def create_devops_terraform_build_config(stage, project_root_path, build_commons
|
|||
'debug_print_terraform_command': debug_print_terraform_command})
|
||||
return ret
|
||||
|
||||
|
||||
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,
|
||||
**kwargs):
|
||||
"""
|
||||
|
@ -41,17 +55,18 @@ class WorkaroundTerraform(Terraform):
|
|||
default = kwargs
|
||||
default['input'] = input
|
||||
default['no_color'] = no_color
|
||||
if skip_plan:
|
||||
default['auto-approve'] = IsFlagged
|
||||
else:
|
||||
default['auto-approve'] = None
|
||||
option_dict = self._generate_default_options(default)
|
||||
default['auto-approve'] = (skip_plan == True)
|
||||
args = self._generate_default_args(dir_or_plan)
|
||||
return self.cmd('apply', *args, **option_dict)
|
||||
|
||||
def generate_cmd_string(self, cmd, *args, **kwargs):
|
||||
self.latest_cmd = super().generate_cmd_string(self, cmd, *args, **kwargs)
|
||||
return self.latest_cmd
|
||||
|
||||
def latest_cmd(self):
|
||||
return self.latest_cmd
|
||||
result = super().generate_cmd_string(cmd, *args, **kwargs)
|
||||
self.latest_cmd = ' '.join(result)
|
||||
return result
|
||||
|
||||
|
||||
class DevopsTerraformBuild(DevopsBuild):
|
||||
|
@ -80,7 +95,8 @@ class DevopsTerraformBuild(DevopsBuild):
|
|||
return ret
|
||||
|
||||
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:
|
||||
output_file.write(my_data.decode(sys.stdout.encoding))
|
||||
|
||||
|
@ -127,12 +143,14 @@ class DevopsTerraformBuild(DevopsBuild):
|
|||
|
||||
def plan(self):
|
||||
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)
|
||||
|
||||
def apply(self, auto_approve=False):
|
||||
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,
|
||||
skip_plan=auto_approve,
|
||||
var=self.project_vars())
|
||||
self.print_terraform_command(tf)
|
||||
self.write_output(tf)
|
||||
|
@ -140,10 +158,11 @@ class DevopsTerraformBuild(DevopsBuild):
|
|||
def destroy(self, auto_approve=False):
|
||||
tf = self.init_client()
|
||||
if auto_approve:
|
||||
force=IsFlagged
|
||||
force = IsFlagged
|
||||
else:
|
||||
force=None
|
||||
tf.destroy(capture_output=False, raise_on_error=True, force=force,
|
||||
force = None
|
||||
tf.destroy(capture_output=False, raise_on_error=True,
|
||||
force=force,
|
||||
var=self.project_vars())
|
||||
self.print_terraform_command(tf)
|
||||
|
||||
|
@ -155,5 +174,5 @@ class DevopsTerraformBuild(DevopsBuild):
|
|||
|
||||
def print_terraform_command(self, tf):
|
||||
if self.debug_print_terraform_command:
|
||||
output = 'cd ' + self.build_path() + ' ' + tf.latest_cmd()
|
||||
output = 'cd ' + self.build_path() + ' && ' + tf.latest_cmd
|
||||
print(output)
|
||||
|
|
Loading…
Reference in a new issue