<s> import uuid <EOL> from datetime import datetime <EOL> from flask import current_app <EOL> from manager_rest import models , storage_manager <EOL> from manager_rest . blueprints_manager import tasks , BlueprintsManager <EOL> import manager_rest . manager_exceptions <EOL> import manager_rest . workflow_client as wf_client <EOL> from dsl_parser import constants <EOL> from handlers import ( DeploymentUpdateNodeHandler , <EOL> DeploymentUpdateNodeInstanceHandler ) <EOL> from validator import StepValidator <EOL> from utils import extract_ids <EOL> from constants import STATE , CHANGE_TYPE <EOL> class DeploymentUpdateManager ( object ) : <EOL> def __init__ ( self ) : <EOL> self . sm = storage_manager . get_storage_manager ( ) <EOL> self . workflow_client = wf_client . get_workflow_client ( ) <EOL> self . _node_handler = DeploymentUpdateNodeHandler ( ) <EOL> self . _node_instance_handler = DeploymentUpdateNodeInstanceHandler ( ) <EOL> self . _step_validator = StepValidator ( ) <EOL> def get_deployment_update ( self , deployment_update_id ) : <EOL> """<STR_LIT>""" <EOL> return self . sm . get_deployment_update ( deployment_update_id ) <EOL> def deployment_updates_list ( self , include = None , filters = None , <EOL> pagination = None , sort = None ) : <EOL> """<STR_LIT>""" <EOL> return self . sm . deployment_updates_list ( include = include , <EOL> filters = filters , <EOL> pagination = pagination , <EOL> sort = sort ) <EOL> def stage_deployment_update ( self , deployment_id , staged_blueprint ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_no_active_updates_per_deployment ( deployment_id ) <EOL> deployment_update = models . DeploymentUpdate ( deployment_id , <EOL> staged_blueprint ) <EOL> self . sm . put_deployment_update ( deployment_update ) <EOL> return deployment_update <EOL> def create_deployment_update_step ( self , deployment_update_id , <EOL> operation , entity_type , entity_id ) : <EOL> """<STR_LIT>""" <EOL> step = models . DeploymentUpdateStep ( operation , <EOL> entity_type , <EOL> entity_id ) <EOL> dep_update = self . get_deployment_update ( deployment_update_id ) <EOL> self . _step_validator . validate ( dep_update , step ) <EOL> self . sm . put_deployment_update_step ( deployment_update_id , step ) <EOL> return step <EOL> def commit_deployment_update ( self , deployment_update_id ) : <EOL> """<STR_LIT>""" <EOL> dep_update = self . get_deployment_update ( deployment_update_id ) <EOL> dep_update . state = STATE . COMMITTING <EOL> self . sm . update_deployment_update ( dep_update ) <EOL> modified_entity_ids , depup_nodes = self . _node_handler . handle ( dep_update ) <EOL> node_instance_changes = self . _extract_changes ( dep_update , <EOL> depup_nodes ) <EOL> depup_node_instances = self . _node_instance_handler . handle ( dep_update , <EOL> node_instance_changes ) <EOL> dep_update . deployment_update_nodes = depup_nodes <EOL> dep_update . deployment_update_node_instances = depup_node_instances <EOL> dep_update . modified_entity_ids = modified_entity_ids . to_dict ( ) <EOL> self . sm . update_deployment_update ( dep_update ) <EOL> self . _execute_update_workflow ( dep_update , <EOL> depup_node_instances , <EOL> modified_entity_ids . to_dict ( ) ) <EOL> return models . DeploymentUpdate ( deployment_update_id , <EOL> dep_update . blueprint ) <EOL> def _validate_no_active_updates_per_deployment ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> existing_updates = self . deployment_updates_list ( filters = { <EOL> '<STR_LIT>' : deployment_id <EOL> } ) . items <EOL> active_update = next ( iter ( <EOL> [ u for u in existing_updates <EOL> if u . state != STATE . COMMITTED ] ) , None ) <EOL> if active_update : <EOL> raise manager_rest . manager_exceptions . ConflictError ( <EOL> '<STR_LIT>' <EOL> . format ( active_update . id ) <EOL> ) <EOL> def _extract_changes ( self , dep_update , raw_nodes ) : <EOL> """<STR_LIT>""" <EOL> deployment_id_filter = { '<STR_LIT>' : dep_update . deployment_id } <EOL> raw_node_instances = [ instance . to_dict ( ) for instance in <EOL> self . sm . get_node_instances ( filters = deployment_id_filter ) . items ] <EOL> return tasks . modify_deployment ( <EOL> nodes = raw_nodes , <EOL> previous_node_instances = raw_node_instances , <EOL> modified_nodes = ( ) <EOL> ) <EOL> def _execute_update_workflow ( self , <EOL> dep_update , <EOL> node_instances , <EOL> modified_entity_ids ) : <EOL> """<STR_LIT>""" <EOL> added_instances = node_instances [ CHANGE_TYPE . ADDED_AND_RELATED ] <EOL> extended_instances = node_instances [ CHANGE_TYPE . EXTENDED_AND_RELATED ] <EOL> reduced_instances = node_instances [ CHANGE_TYPE . REDUCED_AND_RELATED ] <EOL> removed_instances = node_instances [ CHANGE_TYPE . REMOVED_AND_RELATED ] <EOL> instance_ids = { <EOL> '<STR_LIT>' : dep_update . id , <EOL> '<STR_LIT>' : <EOL> extract_ids ( added_instances . get ( CHANGE_TYPE . AFFECTED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( added_instances . get ( CHANGE_TYPE . RELATED ) ) , <EOL> '<STR_LIT>' : modified_entity_ids , <EOL> '<STR_LIT>' : <EOL> extract_ids ( extended_instances . get ( CHANGE_TYPE . AFFECTED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( extended_instances . get ( CHANGE_TYPE . RELATED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( reduced_instances . get ( CHANGE_TYPE . AFFECTED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( reduced_instances . get ( CHANGE_TYPE . RELATED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( removed_instances . get ( CHANGE_TYPE . AFFECTED ) ) , <EOL> '<STR_LIT>' : <EOL> extract_ids ( removed_instances . get ( CHANGE_TYPE . RELATED ) ) <EOL> } <EOL> return self . execute_workflow ( deployment_id = dep_update . deployment_id , <EOL> workflow_id = '<STR_LIT>' , <EOL> parameters = instance_ids ) <EOL> def finalize_commit ( self , deployment_update_id ) : <EOL> """<STR_LIT>""" <EOL> dep_update = self . get_deployment_update ( deployment_update_id ) <EOL> self . _node_instance_handler . finalize ( dep_update ) <EOL> self . _node_handler . finalize ( dep_update ) <EOL> dep_update . state = STATE . COMMITTED <EOL> self . sm . update_deployment_update ( dep_update ) <EOL> return models . DeploymentUpdate ( deployment_update_id , <EOL> dep_update . blueprint ) <EOL> def execute_workflow ( self , <EOL> deployment_id , <EOL> workflow_id , <EOL> parameters = None , <EOL> allow_custom_parameters = False , <EOL> force = False ) : <EOL> """<STR_LIT>""" <EOL> deployment = self . sm . get_deployment ( deployment_id ) <EOL> blueprint = self . sm . get_blueprint ( deployment . blueprint_id ) <EOL> if workflow_id not in deployment . workflows : <EOL> raise manager_rest . manager_exceptions . NonexistentWorkflowError ( <EOL> '<STR_LIT>' . format ( <EOL> workflow_id , deployment_id ) ) <EOL> workflow = deployment . workflows [ workflow_id ] <EOL> execution_parameters = BlueprintsManager . _merge_and_validate_execution_parameters ( <EOL> workflow , workflow_id , parameters , allow_custom_parameters ) <EOL> execution_id = str ( uuid . uuid4 ( ) ) <EOL> new_execution = models . Execution ( <EOL> id = execution_id , <EOL> status = models . Execution . PENDING , <EOL> created_at = str ( datetime . now ( ) ) , <EOL> blueprint_id = deployment . blueprint_id , <EOL> workflow_id = workflow_id , <EOL> deployment_id = deployment_id , <EOL> error = '<STR_LIT>' , <EOL> parameters = BlueprintsManager . _get_only_user_execution_parameters ( <EOL> execution_parameters ) , <EOL> is_system_workflow = False ) <EOL> self . sm . put_execution ( new_execution . id , new_execution ) <EOL> workflow_plugins = blueprint . plan [ <EOL> constants . WORKFLOW_PLUGINS_TO_INSTALL ] <EOL> self . workflow_client . execute_workflow ( <EOL> workflow_id , <EOL> workflow , <EOL> workflow_plugins = workflow_plugins , <EOL> blueprint_id = deployment . blueprint_id , <EOL> deployment_id = deployment_id , <EOL> execution_id = execution_id , <EOL> execution_parameters = execution_parameters ) <EOL> return new_execution <EOL> def get_deployment_updates_manager ( ) : <EOL> """<STR_LIT>""" <EOL> manager = current_app . config . get ( '<STR_LIT>' ) <EOL> if not manager : <EOL> current_app . config [ '<STR_LIT>' ] = DeploymentUpdateManager ( ) <EOL> manager = current_app . config . get ( '<STR_LIT>' ) <EOL> return manager </s>
