def __execute_cleanup(self, report_status, keg_name, keg_status,
                       strategy_execution):
     cleanup_errors = []
     delta_capture = KegDeltaCapture(existing_delta=report_status.delta)
     if strategy_execution.run_cleanup:
         cleanup_errors = self.__cleanout_keg(report_status, keg_name,
                                              keg_status, delta_capture)
         if len(cleanup_errors) == 0:
             try:
                 self.keg_persister.delete(keg_name)
             except PersistenceError as e:
                 msg = f'Failed to remove Keg \'{keg_name}\' on request \'{report_status.uid}\''
                 logger.exception(msg)
                 cleanup_errors.append(f'{msg}: {e}')
             else:
                 record_ref = self.keg_persister.build_record_reference(
                     keg_status.uid, keg_name)
                 delta_capture.removed_object(
                     V1alpha1ObjectStatus(
                         group=record_ref.get('apiVersion'),
                         kind=record_ref.get('kind'),
                         namespace=record_ref['metadata'].get('namespace'),
                         name=record_ref['metadata'].get('name'),
                         uid=record_ref['metadata'].get('uid')))
     report_status.delta = delta_capture.delta
     return PhaseResult(errors=cleanup_errors)
 def __dict_to_obj_status(self, obj_dict):
     group = obj_dict.get('apiVersion')
     kind = obj_dict.get('kind')
     metadata = obj_dict.get('metadata')
     name = metadata.get('name')
     namespace = metadata.get('namespace')
     uid = metadata.get('uid')
     return V1alpha1ObjectStatus(group=group, kind=kind, name=name, namespace=namespace, uid=uid)
Esempio n. 3
0
 def __do_decorate(self, obj_status, action, parent_task_settings,
                   script_name, keg_name, keg_status):
     if obj_status == None:
         obj_status = V1alpha1ObjectStatus(group=action.group,
                                           kind=action.kind,
                                           namespace=action.namespace,
                                           name=action.name)
         keg_status.composition.objects.append(obj_status)
     obj_status.state = EntityStates.DELETE_PENDING
     obj_status.error = None
Esempio n. 4
0
 def __do_decorate(self, obj_status, action, parent_task_settings, script_name, keg_name, keg_status):
     if obj_status == None:
         obj_status = V1alpha1ObjectStatus(group=action.group, kind=action.kind, namespace=action.namespace, 
                                             name=action.name)
         keg_status.composition.objects.append(obj_status)
         obj_status.state = EntityStates.CREATE_PENDING
     if obj_status.state not in [EntityStates.CREATE_FAILED, EntityStates.CREATE_PENDING]:
         obj_status.state = EntityStates.UPDATE_PENDING
     else:
         obj_status.state = EntityStates.CREATE_PENDING
     obj_status.error = None
     self.__add_tags(obj_status, action.tags, script_name)
     return obj_status
    def __process_next_phases(self, report_status, keg_name,
                              strategy_execution, resource_context_properties):
        keg_status, new_keg = self.__get_or_init_keg(keg_name)
        if new_keg is True:
            uid = self.keg_persister.get_record_uid(keg_name)
            record_ref = self.keg_persister.build_record_reference(
                uid, keg_name)
            delta_capture = KegDeltaCapture(report_status.delta)
            delta_capture.deployed_object(
                V1alpha1ObjectStatus(
                    group=record_ref.get('apiVersion'),
                    kind=record_ref.get('kind'),
                    namespace=record_ref['metadata'].get('namespace'),
                    name=record_ref['metadata'].get('name'),
                    uid=record_ref['metadata'].get('uid')))
            report_status.delta = delta_capture.delta
            self.kegd_persister.update(report_status.uid, report_status)
        all_errors = []
        try:
            # This point forward we must make best efforts to update the request if there is an error (and to run Immediate cleanup on error)
            while report_status.phase != None and report_status.phase != StrategyExecutionPhases.END:
                phase_result = None
                try:
                    if report_status.phase == StrategyExecutionPhases.TASKS:
                        phase_result = self.__execute_task_groups(
                            report_status, keg_name, keg_status,
                            strategy_execution)
                    elif report_status.phase == StrategyExecutionPhases.READY_CHECK:
                        phase_result = self.__execute_ready_check_task(
                            report_status, keg_name, keg_status,
                            strategy_execution, resource_context_properties)
                    elif report_status.phase == StrategyExecutionPhases.OUTPUTS:
                        phase_result = self.__execute_output_extraction_task(
                            report_status, keg_name, keg_status,
                            strategy_execution, resource_context_properties)
                    elif report_status.phase == StrategyExecutionPhases.IMMEDIATE_CLEANUP or report_status.phase == StrategyExecutionPhases.IMMEDIATE_CLEANUP_ON_FAILURE:
                        phase_result = self.__process_immediate_cleanup(
                            report_status, keg_name, keg_status,
                            strategy_execution, all_errors)
                    elif report_status.phase == StrategyExecutionPhases.CLEANUP:
                        phase_result = self.__execute_cleanup(
                            report_status, keg_name, keg_status,
                            strategy_execution)
                except Exception as e:
                    logger.exception(
                        f'An error occurred whilst processing phase \'{report_status.phase}\' of deployment strategy \'{report_status.uid}\' on group \'{keg_name}\''
                    )
                    phase_result = PhaseResult(errors=[f'{e}'])

                if phase_result.requeue is not None:
                    return phase_result
                else:
                    all_errors.extend(phase_result.errors)
                    # Even if there are errors, we may move to the Immediate cleanup stage on failure
                    self.__move_to_next_phase(report_status,
                                              strategy_execution, all_errors)
        except Exception as e:
            # Fail safe catch all, that makes sure we don't lose any of the original errors
            if len(all_errors) > 0:
                logger.exception(
                    f'An error occurred whilst processing phases of the deployment strategy \'{report_status.uid}\' on group \'{keg_name}\''
                )
                all_errors.append(f'{e}')
                raise MultiErrorStrategyProcessingError(f'{e}',
                                                        all_errors) from e
            else:
                raise

        return PhaseResult(errors=all_errors)