def destroy_resources(self, resources, terraform_with_targets): """ Start destroying the esources by calling PyTerraform class destroy Args: resources (list): Resources to be destroyed terraform_with_targets (boolean): If partial destroy is to be done (if --tags is supplied) """ destroy_resources = resources if terraform_with_targets else None self.run_pre_destoy(resources) # May be timeout causes first destroy to be a failure hence attempt as many times as the value in the setting for attempt in range(Settings.DESTROY_NUM_ATTEMPTS): self.executed_with_error = False self.exception = None try: PyTerraform().terraform_destroy(destroy_resources) self.run_post_destoy(resources) break except Exception as e: self.executed_with_error = True self.exception = e PyTerraform.save_terraform_output() self.current_destroy_status = self.destroy_statuses.get('execution_finished')
def _cleanup_execute_terraform(self, dry_run): py_terraform = PyTerraform() if not dry_run: self.terraform_outputs = py_terraform.save_terraform_output() self._delete_terraform_provider_file() self.current_install_status = self.install_statuses.get( 'execution_finished')
def terraform_apply(self, resources, terraform_with_targets, dry_run): apply_resources = resources if terraform_with_targets else None py_terraform = PyTerraform() self.current_install_status = self.install_statuses.get( 'tf_init_start') py_terraform.terraform_init() self.current_install_status = self.install_statuses.get( 'tf_init_complete') self.current_install_status = self.install_statuses.get( 'tf_plan_start') py_terraform.terraform_plan(apply_resources) self.current_install_status = self.install_statuses.get( 'tf_plan_complete') for resource in resources: resource.pre_terraform_apply() if not dry_run: self.current_install_status = self.install_statuses.get( 'tf_apply_start') py_terraform.terraform_apply(apply_resources) self.current_install_status = self.install_statuses.get( 'tf_apply_complete') self.terraform_outputs = py_terraform.save_terraform_output( ) # Save Output if there is no exception for resource in resources: resource.post_terraform_apply()
def _cleanup_installation_process(self, dry_run): """ Cleanup the process of installation once it is completed Args: dry_run (boolean): Decides whether original install should be done """ py_terraform = PyTerraform() self.terraform_outputs = py_terraform.save_terraform_output() self._delete_terraform_provider_file() self.current_install_status = self.install_statuses.get( 'execution_finished')
def re_deploy_pacbot(self, input_instance): """ Start method for redeploy Args: input_instance (Input object): User input values """ resources_to_process = self.get_resources_to_process(input_instance) try: resources_to_taint = self.get_resources_with_given_tags( input_instance, ["deploy"]) response = PyTerraform().terraform_taint( resources_to_taint ) # If tainted or destroyed already then skip it except Exception as e: pass terraform_with_targets = False if self.need_complete_install else True resources_to_process = self.get_complete_resources( input_instance ) if self.need_complete_install else resources_to_process self.run_pre_deployment_process(resources_to_process) self.run_real_deployment(input_instance, resources_to_process, terraform_with_targets)
def execute(self, provider): """ Execution method which read inputs, initialises, validate and execute status command Args: provider (str): Provider name based on which the corresponding classes are retrieved """ py_terraform = PyTerraform() self.initialize_classes(provider) input_instance = self.read_input() need_instance = False display_op_list = [] resources = self.get_resources_to_process(input_instance) terraform_outputs = py_terraform.save_terraform_output() status = py_terraform.get_current_status() if not status and not terraform_outputs: print(K.NO_STATUS_OUTPUT) else: print(self.BCYAN_ANSI + "\n%s" % K.CURRENT_STATUS_MSG + self.RESET_ANSI) if terraform_outputs: print("\t%s" % K.CURRENTLY_INSTALLED_RESOURCES) for item in terraform_outputs: print(self.CYAN_ANSI + "\t\t Resource: %s" % (item.replace("_", " ").title()) + self.RESET_ANSI) if status: cmd = self.BGREEN_ANSI + "Last Executed Command: " + self.RESET_ANSI + status[ 'last_exec_command'] self.show_inner_inline_message(cmd) cmd_status = self.BGREEN_ANSI + "Last Command Status: " + self.RESET_ANSI + status[ 'status_code'].replace("_STATUS_", " ") self.show_inner_inline_message(cmd_status) cmd_details = self.BGREEN_ANSI + "Last Command Details: " + self.RESET_ANSI + status[ 'description'] self.show_inner_inline_message(cmd_details) cmd_time = self.BGREEN_ANSI + "Command Executed Time: " + self.RESET_ANSI + status[ 'executed_time'] self.show_inner_inline_message(cmd_time) for resource in resources: output = resource.render_output(terraform_outputs) if output: display_op_list.append(output) self.display_op_msg(display_op_list)
def re_deploy_pacbot(self, input_instance): resources_to_process = self.get_resources_to_process(input_instance) response = PyTerraform().terraform_taint(resources_to_process) terraform_with_targets = True # THis should be set as false otherwise dependent resources will not be built # self.stop_ecs_tasks() self.install_class(self.args, input_instance, check_dependent_resources=False).execute( resources_to_process, terraform_with_targets, self.dry_run)
def destroy_resources(self, resources, terraform_with_targets): destroy_resources = resources if terraform_with_targets else None exception = None # May be timeout causes first destroy to be a failure hence attempt as many times as the value in the setting for attempt in range(Settings.DESTROY_NUM_ATTEMPTS): try: PyTerraform().terraform_destroy(destroy_resources) return except Exception as e: exception = e self._cleanup_destroy() self.executed_with_error = True raise Exception(exception)
def _get_terraform_output_count(self, prev_count): """ Get current terraform resources count by calling the output command Args: prev_count (int): Previous count obtained before the current instant Returns: count (int): Current resources count """ try: output = PyTerraform.load_terraform_output() return len(output) except: return prev_count
def destroy_resources(self, resources, terraform_with_targets): destroy_resources = resources if terraform_with_targets else None self.run_pre_destoy(resources) # May be timeout causes first destroy to be a failure hence attempt as many times as the value in the setting for attempt in range(Settings.DESTROY_NUM_ATTEMPTS): try: PyTerraform().terraform_destroy(destroy_resources) self.run_post_destoy(resources) break except Exception as e: self.executed_with_error = True self.exception = e self.current_destroy_status = self.destroy_statuses.get('execution_finished')
def terraform_apply(self, resources, terraform_with_targets, dry_run): """ Call terraform apply command through PyTerraform class Args: resources (list): Resources to be created terraform_with_targets (boolean): If partial install is to be done (if --tags is supplied) dry_run (boolean): Decides whether original install should be done """ apply_resources = resources if terraform_with_targets else None py_terraform = PyTerraform() self.current_install_status = self.install_statuses.get( 'tf_init_start') py_terraform.terraform_init() self.current_install_status = self.install_statuses.get( 'tf_init_complete') self.current_install_status = self.install_statuses.get( 'tf_plan_start') py_terraform.terraform_plan(apply_resources) self.current_install_status = self.install_statuses.get( 'tf_plan_complete') for resource in resources: resource.pre_terraform_apply() if not dry_run: self.current_install_status = self.install_statuses.get( 'tf_apply_start') py_terraform.terraform_apply(apply_resources) self.current_install_status = self.install_statuses.get( 'tf_apply_complete') self.terraform_outputs = py_terraform.save_terraform_output( ) # Save Output if there is no exception for resource in resources: resource.post_terraform_apply()
def render_terraform_apply_progress(self, resources, terraform_with_targets): """ Show the status of actualy terraform apply Args: resources (list): Resources to be created terraform_with_targets (boolean): If partial install is to be done (if --tags is supplied) """ counter = False # If Plan doesn't end up in error if not self.executed_with_error and self.terraform_thread.isAlive(): start_time = datetime.now() self.show_step_heading(K.TERRAFORM_APPLY_STARTED, write_log=False) py_terraform = PyTerraform() output_count = prev_output_count = 0 while self.install_statuses.get( 'execution_finished' ) > self.current_install_status and self.terraform_thread.isAlive( ): counter = False if counter else True duration = self.CYAN_ANSI + self.get_duration( datetime.now() - start_time) + self.END_ANSI if counter: try: # output_count = len(py_terraform.load_terraform_output()) # This uses terraform output command output_count = self.files_count_in_output_status_dir( ) - 1 prev_output_count = output_count except: output_count = prev_output_count else: output_count = prev_output_count duration_msg = ", Time elapsed: %s" % duration count_msg = self.GREEN_ANSI + str(output_count) + "/" + str( self.total_resources_count) + self.END_ANSI message = "Resources created: " + count_msg + duration_msg self.show_progress_message(message, 1.5) self.clear_status_dir_files() self._render_step_trail_message(K.TERRAFORM_APPLY_COMPLETED, K.EXECUTED_WITH_ERROR, start_time)
def re_create_resources(self, resources, terraform_with_targets, dry_run): """ Start installing the resources by calling PyTerraform class destroy Args: resources (list): Resources to be created terraform_with_targets (boolean): If partial install is to be done (if --tags is supplied) dry_run (boolean): Decides whether original install should be done """ try: if not dry_run: PyTerraform().terraform_destroy(resources) self.destroy = True self.terraform_apply(resources, terraform_with_targets, dry_run) except Exception as e: self.executed_with_error = True self.exception = e self._cleanup_installation_process(dry_run)
def __init__(self, input=None): self.input = input self.tf_outputs = PyTerraform.load_terraform_output_from_json_file() self.clear_status_dir_files()
def pre_generate_terraform(self): # To support latest terraform version PyTerraform.change_tf_extension_to_tf_json()
def _get_terraform_output_count(self, prev_count): try: output = PyTerraform.load_terraform_output() return len(output) except: return prev_count
def generate_terraform_files_and_upgrade_state(self, input_instance): all_resources = self.get_complete_resources(input_instance) for resource in all_resources: resource.generate_terraform() PyTerraform.terrafomr12_upgrade( ) # This is required only when terraform version 12 is used
def __init__(self, input=None): self.input = input self.tf_outputs = PyTerraform.load_terraform_output_from_json_file()