<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> import web <EOL> from nailgun . api . v1 . handlers . base import BaseHandler <EOL> from nailgun . api . v1 . handlers . base import CollectionHandler <EOL> from nailgun . api . v1 . handlers . base import content <EOL> from nailgun . api . v1 . handlers . base import SingleHandler <EOL> from nailgun . api . v1 . validators import node as node_validators <EOL> from nailgun import errors <EOL> from nailgun import objects <EOL> from nailgun . db import db <EOL> from nailgun . db . sqlalchemy . models import Node <EOL> from nailgun . task . manager import NodeDeletionTaskManager <EOL> from nailgun . logger import logger <EOL> from nailgun import notifier <EOL> class NodeHandler ( SingleHandler ) : <EOL> single = objects . Node <EOL> validator = node_validators . NodeValidator <EOL> @ content <EOL> def PUT ( self , obj_id ) : <EOL> """<STR_LIT>""" <EOL> obj = self . get_object_or_404 ( self . single , obj_id ) <EOL> data = self . checked_data ( <EOL> self . validator . validate_update , <EOL> instance = obj <EOL> ) <EOL> try : <EOL> self . single . update ( obj , data ) <EOL> except errors . NetworkTemplateCannotBeApplied as exc : <EOL> raise self . http ( <NUM_LIT> , exc . message ) <EOL> return self . single . to_json ( obj ) <EOL> @ content <EOL> def DELETE ( self , obj_id ) : <EOL> """<STR_LIT>""" <EOL> node = self . get_object_or_404 ( self . single , obj_id ) <EOL> task_manager = NodeDeletionTaskManager ( cluster_id = node . cluster_id ) <EOL> try : <EOL> task = task_manager . execute ( [ node ] , mclient_remove = False ) <EOL> except errors . ControllerInErrorState as e : <EOL> raise self . http ( <NUM_LIT> , e . message ) <EOL> self . raise_task ( task ) <EOL> class NodeCollectionHandler ( CollectionHandler ) : <EOL> """<STR_LIT>""" <EOL> validator = node_validators . NodeValidator <EOL> collection = objects . NodeCollection <EOL> @ content <EOL> def GET ( self ) : <EOL> """<STR_LIT>""" <EOL> cluster_id = web . input ( cluster_id = None ) . cluster_id <EOL> nodes = self . collection . eager_nodes_handlers ( None ) <EOL> if cluster_id == '<STR_LIT>' : <EOL> nodes = nodes . filter_by ( cluster_id = None ) <EOL> elif cluster_id : <EOL> nodes = nodes . filter_by ( cluster_id = cluster_id ) <EOL> return self . collection . to_json ( nodes ) <EOL> @ content <EOL> def PUT ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . checked_data ( <EOL> self . validator . validate_collection_update <EOL> ) <EOL> nodes_updated = [ ] <EOL> for nd in data : <EOL> node = self . collection . single . get_by_meta ( nd ) <EOL> if not node : <EOL> raise self . http ( <NUM_LIT> , "<STR_LIT>" . format ( nd ) ) <EOL> try : <EOL> self . collection . single . update ( node , nd ) <EOL> except errors . NetworkTemplateCannotBeApplied as exc : <EOL> raise self . http ( <NUM_LIT> , exc . message ) <EOL> nodes_updated . append ( node . id ) <EOL> nodes = self . collection . filter_by_id_list ( <EOL> self . collection . eager_nodes_handlers ( None ) , <EOL> nodes_updated <EOL> ) <EOL> return self . collection . to_json ( nodes ) <EOL> @ content <EOL> def DELETE ( self ) : <EOL> """<STR_LIT>""" <EOL> node_ids = self . checked_data ( <EOL> validate_method = self . validator . validate_collection_delete , <EOL> data = web . input ( ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> nodes = self . get_objects_list_or_404 ( self . collection , node_ids ) <EOL> task_manager = NodeDeletionTaskManager ( cluster_id = nodes [ <NUM_LIT:0> ] . cluster_id ) <EOL> try : <EOL> task = task_manager . execute ( nodes , mclient_remove = False ) <EOL> except errors . ControllerInErrorState as e : <EOL> raise self . http ( <NUM_LIT> , e . message ) <EOL> self . raise_task ( task ) <EOL> class NodeAgentHandler ( BaseHandler ) : <EOL> collection = objects . NodeCollection <EOL> validator = node_validators . NodeValidator <EOL> @ content <EOL> def PUT ( self ) : <EOL> """<STR_LIT>""" <EOL> nd = self . checked_data ( <EOL> self . validator . validate_update , <EOL> data = web . data ( ) ) <EOL> node = self . collection . single . get_by_meta ( nd ) <EOL> if not node : <EOL> raise self . http ( <NUM_LIT> , "<STR_LIT>" . format ( nd ) ) <EOL> node . timestamp = datetime . now ( ) <EOL> if not node . online : <EOL> node . online = True <EOL> msg = u"<STR_LIT>" . format ( node . human_readable_name ) <EOL> logger . info ( msg ) <EOL> notifier . notify ( "<STR_LIT>" , msg , node_id = node . id ) <EOL> db ( ) . flush ( ) <EOL> if '<STR_LIT>' in nd and ( <EOL> node . agent_checksum == nd [ '<STR_LIT>' ] <EOL> ) : <EOL> return { '<STR_LIT:id>' : node . id , '<STR_LIT>' : True } <EOL> self . collection . single . update_by_agent ( node , nd ) <EOL> return { "<STR_LIT:id>" : node . id } <EOL> class NodesAllocationStatsHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> @ content <EOL> def GET ( self ) : <EOL> """<STR_LIT>""" <EOL> unallocated_nodes = db ( ) . query ( Node ) . filter_by ( cluster_id = None ) . count ( ) <EOL> total_nodes = db ( ) . query ( Node ) . count ( ) <EOL> return { '<STR_LIT>' : total_nodes , <EOL> '<STR_LIT>' : unallocated_nodes } <EOL> class NodeAttributesHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> validator = node_validators . NodeAttributesValidator <EOL> @ content <EOL> def GET ( self , node_id ) : <EOL> """<STR_LIT>""" <EOL> node = self . get_object_or_404 ( objects . Node , node_id ) <EOL> return objects . Node . get_attributes ( node ) <EOL> @ content <EOL> def PUT ( self , node_id ) : <EOL> """<STR_LIT>""" <EOL> node = self . get_object_or_404 ( objects . Node , node_id ) <EOL> if not node . cluster : <EOL> raise self . http ( <NUM_LIT> , "<STR_LIT>" <EOL> . format ( node . id ) ) <EOL> data = self . checked_data ( node = node , cluster = node . cluster ) <EOL> objects . Node . update_attributes ( node , data ) <EOL> return objects . Node . get_attributes ( node ) </s>
