<s> import traceback <EOL> import six <EOL> import web <EOL> from nailgun . api . v1 . handlers . base import BaseHandler <EOL> from nailgun . api . v1 . handlers . base import content <EOL> from nailgun . api . v1 . validators . cluster import ProvisionSelectedNodesValidator <EOL> from nailgun . api . v1 . validators . node import DeploySelectedNodesValidator <EOL> from nailgun . api . v1 . validators . node import NodeDeploymentValidator <EOL> from nailgun . api . v1 . validators . node import NodesFilterValidator <EOL> from nailgun . api . v1 . validators . orchestrator_graph import GraphSolverVisualizationValidator <EOL> from nailgun . logger import logger <EOL> from nailgun import errors <EOL> from nailgun import objects <EOL> from nailgun . orchestrator import deployment_serializers <EOL> from nailgun . orchestrator import graph_visualization <EOL> from nailgun . orchestrator import orchestrator_graph <EOL> from nailgun . orchestrator import provisioning_serializers <EOL> from nailgun . orchestrator . stages import post_deployment_serialize <EOL> from nailgun . orchestrator . stages import pre_deployment_serialize <EOL> from nailgun . orchestrator import task_based_deployment <EOL> from nailgun . task . helpers import TaskHelper <EOL> from nailgun . task import manager <EOL> class NodesFilterMixin ( object ) : <EOL> validator = NodesFilterValidator <EOL> def get_default_nodes ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_nodes ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> nodes = web . input ( nodes = None ) . nodes <EOL> if nodes : <EOL> node_ids = self . checked_data ( data = nodes ) <EOL> return self . get_objects_list_or_404 ( <EOL> objects . NodeCollection , <EOL> node_ids <EOL> ) <EOL> return self . get_default_nodes ( cluster ) or [ ] <EOL> class DefaultOrchestratorInfo ( NodesFilterMixin , BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> @ content <EOL> def GET ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> nodes = self . get_nodes ( cluster ) <EOL> return self . _serialize ( cluster , nodes ) <EOL> def _serialize ( self , cluster , nodes ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_default_nodes ( self , cluster ) : <EOL> return objects . Cluster . get_nodes_not_for_deletion ( cluster ) <EOL> class OrchestratorInfo ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def get_orchestrator_info ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def update_orchestrator_info ( self , cluster , data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ content <EOL> def GET ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> return self . get_orchestrator_info ( cluster ) <EOL> @ content <EOL> def PUT ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> data = self . checked_data ( ) <EOL> self . update_orchestrator_info ( cluster , data ) <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( cluster_id ) ) <EOL> return data <EOL> @ content <EOL> def DELETE ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> self . update_orchestrator_info ( cluster , { } ) <EOL> raise self . http ( <NUM_LIT> , '<STR_LIT:{}>' ) <EOL> class DefaultProvisioningInfo ( DefaultOrchestratorInfo ) : <EOL> def _serialize ( self , cluster , nodes ) : <EOL> return provisioning_serializers . serialize ( <EOL> cluster , nodes , ignore_customized = True ) <EOL> class DefaultDeploymentInfo ( DefaultOrchestratorInfo ) : <EOL> def _serialize ( self , cluster , nodes ) : <EOL> if objects . Release . is_lcm_supported ( cluster . release ) : <EOL> return deployment_serializers . serialize_for_lcm ( <EOL> cluster , nodes , ignore_customized = True <EOL> ) <EOL> graph = orchestrator_graph . AstuteGraph ( cluster ) <EOL> return deployment_serializers . serialize ( <EOL> graph , cluster , nodes , ignore_customized = True ) <EOL> class DefaultPrePluginsHooksInfo ( DefaultOrchestratorInfo ) : <EOL> def _serialize ( self , cluster , nodes ) : <EOL> if objects . Release . is_lcm_supported ( cluster . release ) : <EOL> raise self . http ( <EOL> <NUM_LIT> , msg = "<STR_LIT>" <EOL> ) <EOL> graph = orchestrator_graph . AstuteGraph ( cluster ) <EOL> return pre_deployment_serialize ( graph , cluster , nodes ) <EOL> class DefaultPostPluginsHooksInfo ( DefaultOrchestratorInfo ) : <EOL> def _serialize ( self , cluster , nodes ) : <EOL> if objects . Release . is_lcm_supported ( cluster . release ) : <EOL> raise self . http ( <EOL> <NUM_LIT> , msg = "<STR_LIT>" <EOL> ) <EOL> graph = orchestrator_graph . AstuteGraph ( cluster ) <EOL> return post_deployment_serialize ( graph , cluster , nodes ) <EOL> class ProvisioningInfo ( OrchestratorInfo ) : <EOL> def get_orchestrator_info ( self , cluster ) : <EOL> return objects . Cluster . get_provisioning_info ( cluster ) <EOL> def update_orchestrator_info ( self , cluster , data ) : <EOL> return objects . Cluster . replace_provisioning_info ( cluster , data ) <EOL> class DeploymentInfo ( OrchestratorInfo ) : <EOL> def get_orchestrator_info ( self , cluster ) : <EOL> return objects . Cluster . get_deployment_info ( cluster ) <EOL> def update_orchestrator_info ( self , cluster , data ) : <EOL> return objects . Cluster . replace_deployment_info ( cluster , data ) <EOL> class SelectedNodesBase ( NodesFilterMixin , BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle_task ( self , cluster , ** kwargs ) : <EOL> nodes = self . get_nodes ( cluster ) <EOL> try : <EOL> task_manager = self . task_manager ( cluster_id = cluster . id ) <EOL> task = task_manager . execute ( nodes , ** kwargs ) <EOL> except Exception as exc : <EOL> logger . warn ( <EOL> u'<STR_LIT>' , <EOL> task_manager . __class__ . __name__ , traceback . format_exc ( ) ) <EOL> raise self . http ( <NUM_LIT> , message = six . text_type ( exc ) ) <EOL> self . raise_task ( task ) <EOL> @ content <EOL> def PUT ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> return self . handle_task ( cluster ) <EOL> class ProvisionSelectedNodes ( SelectedNodesBase ) : <EOL> """<STR_LIT>""" <EOL> validator = ProvisionSelectedNodesValidator <EOL> task_manager = manager . ProvisioningTaskManager <EOL> def get_default_nodes ( self , cluster ) : <EOL> return TaskHelper . nodes_to_provision ( cluster ) <EOL> @ content <EOL> def PUT ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> self . checked_data ( self . validator . validate_provision , cluster = cluster ) <EOL> return self . handle_task ( cluster ) <EOL> class BaseDeploySelectedNodes ( SelectedNodesBase ) : <EOL> validator = DeploySelectedNodesValidator <EOL> task_manager = manager . DeploymentTaskManager <EOL> def get_default_nodes ( self , cluster ) : <EOL> return TaskHelper . nodes_to_deploy ( cluster ) <EOL> def get_graph_type ( self ) : <EOL> return web . input ( graph_type = None ) . graph_type <EOL> def get_nodes ( self , cluster ) : <EOL> nodes_to_deploy = super ( <EOL> BaseDeploySelectedNodes , self ) . get_nodes ( cluster ) <EOL> self . validate ( cluster , nodes_to_deploy , self . get_graph_type ( ) ) <EOL> return nodes_to_deploy <EOL> def validate ( self , cluster , nodes_to_deploy , graph_type = None ) : <EOL> self . checked_data ( self . validator . validate_nodes_to_deploy , <EOL> nodes = nodes_to_deploy , cluster_id = cluster . id ) <EOL> self . checked_data ( self . validator . validate_release , cluster = cluster , <EOL> graph_type = graph_type ) <EOL> class DeploySelectedNodes ( BaseDeploySelectedNodes ) : <EOL> """<STR_LIT>""" <EOL> @ content <EOL> def PUT ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> return self . handle_task ( cluster , graph_type = self . get_graph_type ( ) ) <EOL> class DeploySelectedNodesWithTasks ( BaseDeploySelectedNodes ) : <EOL> validator = NodeDeploymentValidator <EOL> @ content <EOL> def PUT ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> data = self . checked_data ( <EOL> self . validator . validate_deployment , <EOL> cluster = cluster , <EOL> graph_type = self . get_graph_type ( ) ) <EOL> return self . handle_task ( <EOL> cluster , <EOL> deployment_tasks = data , <EOL> graph_type = self . get_graph_type ( ) ) <EOL> class TaskDeployGraph ( BaseHandler ) : <EOL> validator = GraphSolverVisualizationValidator <EOL> def GET ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> web . header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' , unique = True ) <EOL> graph_type = web . input ( graph_type = None ) . graph_type <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> tasks = objects . Cluster . get_deployment_tasks ( cluster , graph_type ) <EOL> graph = orchestrator_graph . GraphSolver ( tasks ) <EOL> tasks = web . input ( tasks = None ) . tasks <EOL> parents_for = web . input ( parents_for = None ) . parents_for <EOL> remove = web . input ( remove = None ) . remove <EOL> if tasks : <EOL> tasks = self . checked_data ( <EOL> self . validator . validate , <EOL> data = tasks , <EOL> cluster = cluster , <EOL> graph_type = graph_type ) <EOL> logger . debug ( '<STR_LIT>' , tasks ) <EOL> if parents_for : <EOL> parents_for = self . checked_data ( <EOL> self . validator . validate_task_presence , <EOL> data = parents_for , <EOL> graph = graph ) <EOL> logger . debug ( '<STR_LIT>' , parents_for ) <EOL> if remove : <EOL> remove = list ( set ( remove . split ( '<STR_LIT:U+002C>' ) ) ) <EOL> remove = self . checked_data ( <EOL> self . validator . validate_tasks_types , <EOL> data = remove ) <EOL> logger . debug ( '<STR_LIT>' , remove ) <EOL> visualization = graph_visualization . GraphVisualization ( graph ) <EOL> dotgraph = visualization . get_dotgraph ( tasks = tasks , <EOL> parents_for = parents_for , <EOL> remove = remove ) <EOL> return dotgraph . to_string ( ) <EOL> class SerializedTasksHandler ( NodesFilterMixin , BaseHandler ) : <EOL> def get_default_nodes ( self , cluster ) : <EOL> return TaskHelper . nodes_to_deploy ( cluster ) <EOL> @ content <EOL> def GET ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> cluster = self . get_object_or_404 ( objects . Cluster , cluster_id ) <EOL> nodes = self . get_nodes ( cluster ) <EOL> self . checked_data ( self . validator . validate_placement , <EOL> data = nodes , cluster = cluster ) <EOL> tasks = web . input ( tasks = None ) . tasks <EOL> graph_type = web . input ( graph_type = None ) . graph_type <EOL> task_ids = [ t . strip ( ) for t in tasks . split ( '<STR_LIT:U+002C>' ) ] if tasks else None <EOL> try : <EOL> serialized_tasks = task_based_deployment . TasksSerializer . serialize ( <EOL> cluster , <EOL> nodes , <EOL> objects . Cluster . get_deployment_tasks ( cluster , graph_type ) , <EOL> task_ids = task_ids <EOL> ) <EOL> return { '<STR_LIT>' : serialized_tasks [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : serialized_tasks [ <NUM_LIT:1> ] } <EOL> except errors . TaskBaseDeploymentNotAllowed as exc : <EOL> raise self . http ( <NUM_LIT> , msg = six . text_type ( exc ) ) </s>
