<s> """<STR_LIT>""" <EOL> import collections <EOL> import datetime <EOL> import threading <EOL> from oslo_config import cfg <EOL> from oslo_db import exception as db_exc <EOL> from oslo_db . sqlalchemy import enginefacade <EOL> from oslo_db . sqlalchemy import utils as db_utils <EOL> from oslo_log import log <EOL> from oslo_utils import strutils <EOL> from oslo_utils import timeutils <EOL> from oslo_utils import uuidutils <EOL> from sqlalchemy . orm . exc import NoResultFound <EOL> from sqlalchemy import sql <EOL> from ironic . common import exception <EOL> from ironic . common . i18n import _ <EOL> from ironic . common . i18n import _LW <EOL> from ironic . common import states <EOL> from ironic . common import utils <EOL> from ironic . db import api <EOL> from ironic . db . sqlalchemy import models <EOL> CONF = cfg . CONF <EOL> CONF . import_opt ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> LOG = log . getLogger ( __name__ ) <EOL> _CONTEXT = threading . local ( ) <EOL> def get_backend ( ) : <EOL> """<STR_LIT>""" <EOL> return Connection ( ) <EOL> def _session_for_read ( ) : <EOL> return enginefacade . reader . using ( _CONTEXT ) <EOL> def _session_for_write ( ) : <EOL> return enginefacade . writer . using ( _CONTEXT ) <EOL> def model_query ( model , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> with _session_for_read ( ) as session : <EOL> query = session . query ( model , * args ) <EOL> return query <EOL> def add_identity_filter ( query , value ) : <EOL> """<STR_LIT>""" <EOL> if strutils . is_int_like ( value ) : <EOL> return query . filter_by ( id = value ) <EOL> elif uuidutils . is_uuid_like ( value ) : <EOL> return query . filter_by ( uuid = value ) <EOL> else : <EOL> raise exception . InvalidIdentity ( identity = value ) <EOL> def add_port_filter ( query , value ) : <EOL> """<STR_LIT>""" <EOL> if utils . is_valid_mac ( value ) : <EOL> return query . filter_by ( address = value ) <EOL> else : <EOL> return add_identity_filter ( query , value ) <EOL> def add_port_filter_by_node ( query , value ) : <EOL> if strutils . is_int_like ( value ) : <EOL> return query . filter_by ( node_id = value ) <EOL> else : <EOL> query = query . join ( models . Node , <EOL> models . Port . node_id == models . Node . id ) <EOL> return query . filter ( models . Node . uuid == value ) <EOL> def add_portgroup_filter ( query , value ) : <EOL> """<STR_LIT>""" <EOL> if utils . is_valid_mac ( value ) : <EOL> return query . filter_by ( address = value ) <EOL> else : <EOL> return add_identity_filter ( query , value ) <EOL> def add_portgroup_filter_by_node ( query , value ) : <EOL> if strutils . is_int_like ( value ) : <EOL> return query . filter_by ( node_id = value ) <EOL> else : <EOL> query = query . join ( models . Node , <EOL> models . Portgroup . node_id == models . Node . id ) <EOL> return query . filter ( models . Node . uuid == value ) <EOL> def add_port_filter_by_portgroup ( query , value ) : <EOL> if strutils . is_int_like ( value ) : <EOL> return query . filter_by ( portgroup_id = value ) <EOL> else : <EOL> query = query . join ( models . Portgroup , <EOL> models . Port . portgroup_id == models . Portgroup . id ) <EOL> return query . filter ( models . Portgroup . uuid == value ) <EOL> def add_node_filter_by_chassis ( query , value ) : <EOL> if strutils . is_int_like ( value ) : <EOL> return query . filter_by ( chassis_id = value ) <EOL> else : <EOL> query = query . join ( models . Chassis , <EOL> models . Node . chassis_id == models . Chassis . id ) <EOL> return query . filter ( models . Chassis . uuid == value ) <EOL> def _paginate_query ( model , limit = None , marker = None , sort_key = None , <EOL> sort_dir = None , query = None ) : <EOL> if not query : <EOL> query = model_query ( model ) <EOL> sort_keys = [ '<STR_LIT:id>' ] <EOL> if sort_key and sort_key not in sort_keys : <EOL> sort_keys . insert ( <NUM_LIT:0> , sort_key ) <EOL> try : <EOL> query = db_utils . paginate_query ( query , model , limit , sort_keys , <EOL> marker = marker , sort_dir = sort_dir ) <EOL> except db_exc . InvalidSortKey : <EOL> raise exception . InvalidParameterValue ( <EOL> _ ( '<STR_LIT>' ) <EOL> % { '<STR_LIT:key>' : sort_key } ) <EOL> return query . all ( ) <EOL> class Connection ( api . Connection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> def _add_nodes_filters ( self , query , filters ) : <EOL> if filters is None : <EOL> filters = [ ] <EOL> if '<STR_LIT>' in filters : <EOL> chassis_obj = self . get_chassis_by_uuid ( filters [ '<STR_LIT>' ] ) <EOL> query = query . filter_by ( chassis_id = chassis_obj . id ) <EOL> if '<STR_LIT>' in filters : <EOL> if filters [ '<STR_LIT>' ] : <EOL> query = query . filter ( models . Node . instance_uuid != sql . null ( ) ) <EOL> else : <EOL> query = query . filter ( models . Node . instance_uuid == sql . null ( ) ) <EOL> if '<STR_LIT>' in filters : <EOL> if filters [ '<STR_LIT>' ] : <EOL> query = query . filter ( models . Node . reservation != sql . null ( ) ) <EOL> else : <EOL> query = query . filter ( models . Node . reservation == sql . null ( ) ) <EOL> if '<STR_LIT>' in filters : <EOL> query = query . filter ( models . Node . reservation . in_ ( <EOL> filters [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in filters : <EOL> query = query . filter_by ( maintenance = filters [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in filters : <EOL> query = query . filter_by ( driver = filters [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in filters : <EOL> query = query . filter_by ( provision_state = filters [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in filters : <EOL> limit = ( timeutils . utcnow ( ) - <EOL> datetime . timedelta ( seconds = filters [ '<STR_LIT>' ] ) ) <EOL> query = query . filter ( models . Node . provision_updated_at < limit ) <EOL> if '<STR_LIT>' in filters : <EOL> limit = ( ( timeutils . utcnow ( ) ) - <EOL> ( datetime . timedelta ( <EOL> seconds = filters [ '<STR_LIT>' ] ) ) ) <EOL> query = query . filter ( models . Node . inspection_started_at < limit ) <EOL> if '<STR_LIT>' in filters : <EOL> query = query . filter_by ( console_enabled = filters [ '<STR_LIT>' ] ) <EOL> return query <EOL> def get_nodeinfo_list ( self , columns = None , filters = None , limit = None , <EOL> marker = None , sort_key = None , sort_dir = None ) : <EOL> if columns is None : <EOL> columns = [ models . Node . id ] <EOL> else : <EOL> columns = [ getattr ( models . Node , c ) for c in columns ] <EOL> query = model_query ( * columns , base_model = models . Node ) <EOL> query = self . _add_nodes_filters ( query , filters ) <EOL> return _paginate_query ( models . Node , limit , marker , <EOL> sort_key , sort_dir , query ) <EOL> def get_node_list ( self , filters = None , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> query = model_query ( models . Node ) <EOL> query = self . _add_nodes_filters ( query , filters ) <EOL> return _paginate_query ( models . Node , limit , marker , <EOL> sort_key , sort_dir , query ) <EOL> def reserve_node ( self , tag , node_id ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Node ) <EOL> query = add_identity_filter ( query , node_id ) <EOL> count = query . filter_by ( reservation = None ) . update ( <EOL> { '<STR_LIT>' : tag } , synchronize_session = False ) <EOL> try : <EOL> node = query . one ( ) <EOL> if count != <NUM_LIT:1> : <EOL> raise exception . NodeLocked ( node = node . uuid , <EOL> host = node [ '<STR_LIT>' ] ) <EOL> return node <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node_id ) <EOL> def release_node ( self , tag , node_id ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Node ) <EOL> query = add_identity_filter ( query , node_id ) <EOL> count = query . filter_by ( reservation = tag ) . update ( <EOL> { '<STR_LIT>' : None } , synchronize_session = False ) <EOL> try : <EOL> if count != <NUM_LIT:1> : <EOL> node = query . one ( ) <EOL> if node [ '<STR_LIT>' ] is None : <EOL> raise exception . NodeNotLocked ( node = node . uuid ) <EOL> else : <EOL> raise exception . NodeLocked ( node = node . uuid , <EOL> host = node [ '<STR_LIT>' ] ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node_id ) <EOL> def create_node ( self , values ) : <EOL> if '<STR_LIT>' not in values : <EOL> values [ '<STR_LIT>' ] = uuidutils . generate_uuid ( ) <EOL> if '<STR_LIT>' not in values : <EOL> values [ '<STR_LIT>' ] = states . NOSTATE <EOL> if '<STR_LIT>' not in values : <EOL> values [ '<STR_LIT>' ] = states . ENROLL <EOL> if '<STR_LIT>' in values : <EOL> LOG . warning ( <EOL> _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , { '<STR_LIT>' : values [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : values [ '<STR_LIT>' ] } ) <EOL> del values [ '<STR_LIT>' ] <EOL> node = models . Node ( ) <EOL> node . update ( values ) <EOL> with _session_for_write ( ) as session : <EOL> try : <EOL> session . add ( node ) <EOL> session . flush ( ) <EOL> except db_exc . DBDuplicateEntry as exc : <EOL> if '<STR_LIT:name>' in exc . columns : <EOL> raise exception . DuplicateName ( name = values [ '<STR_LIT:name>' ] ) <EOL> elif '<STR_LIT>' in exc . columns : <EOL> raise exception . InstanceAssociated ( <EOL> instance_uuid = values [ '<STR_LIT>' ] , <EOL> node = values [ '<STR_LIT>' ] ) <EOL> raise exception . NodeAlreadyExists ( uuid = values [ '<STR_LIT>' ] ) <EOL> return node <EOL> def get_node_by_id ( self , node_id ) : <EOL> query = model_query ( models . Node ) . filter_by ( id = node_id ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node = node_id ) <EOL> def get_node_by_uuid ( self , node_uuid ) : <EOL> query = model_query ( models . Node ) . filter_by ( uuid = node_uuid ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node = node_uuid ) <EOL> def get_node_by_name ( self , node_name ) : <EOL> query = model_query ( models . Node ) . filter_by ( name = node_name ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node = node_name ) <EOL> def get_node_by_instance ( self , instance ) : <EOL> if not uuidutils . is_uuid_like ( instance ) : <EOL> raise exception . InvalidUUID ( uuid = instance ) <EOL> query = ( model_query ( models . Node ) <EOL> . filter_by ( instance_uuid = instance ) ) <EOL> try : <EOL> result = query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . InstanceNotFound ( instance = instance ) <EOL> return result <EOL> def destroy_node ( self , node_id ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Node ) <EOL> query = add_identity_filter ( query , node_id ) <EOL> try : <EOL> node_ref = query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node = node_id ) <EOL> if uuidutils . is_uuid_like ( node_id ) : <EOL> node_id = node_ref [ '<STR_LIT:id>' ] <EOL> port_query = model_query ( models . Port ) <EOL> port_query = add_port_filter_by_node ( port_query , node_id ) <EOL> port_query . delete ( ) <EOL> portgroup_query = model_query ( models . Portgroup ) <EOL> portgroup_query = add_portgroup_filter_by_node ( portgroup_query , <EOL> node_id ) <EOL> portgroup_query . delete ( ) <EOL> tag_query = model_query ( models . NodeTag ) . filter_by ( node_id = node_id ) <EOL> tag_query . delete ( ) <EOL> query . delete ( ) <EOL> def update_node ( self , node_id , values ) : <EOL> if '<STR_LIT>' in values : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidParameterValue ( err = msg ) <EOL> try : <EOL> return self . _do_update_node ( node_id , values ) <EOL> except db_exc . DBDuplicateEntry as e : <EOL> if '<STR_LIT:name>' in e . columns : <EOL> raise exception . DuplicateName ( name = values [ '<STR_LIT:name>' ] ) <EOL> elif '<STR_LIT>' in e . columns : <EOL> raise exception . NodeAlreadyExists ( uuid = values [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT>' in e . columns : <EOL> raise exception . InstanceAssociated ( <EOL> instance_uuid = values [ '<STR_LIT>' ] , <EOL> node = node_id ) <EOL> else : <EOL> raise e <EOL> def _do_update_node ( self , node_id , values ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Node ) <EOL> query = add_identity_filter ( query , node_id ) <EOL> try : <EOL> ref = query . with_lockmode ( '<STR_LIT>' ) . one ( ) <EOL> except NoResultFound : <EOL> raise exception . NodeNotFound ( node = node_id ) <EOL> if values . get ( "<STR_LIT>" ) and ref . instance_uuid : <EOL> raise exception . NodeAssociated ( <EOL> node = ref . uuid , instance = ref . instance_uuid ) <EOL> if '<STR_LIT>' in values : <EOL> values [ '<STR_LIT>' ] = timeutils . utcnow ( ) <EOL> if values [ '<STR_LIT>' ] == states . INSPECTING : <EOL> values [ '<STR_LIT>' ] = timeutils . utcnow ( ) <EOL> values [ '<STR_LIT>' ] = None <EOL> elif ( ref . provision_state == states . INSPECTING and <EOL> values [ '<STR_LIT>' ] == states . MANAGEABLE ) : <EOL> values [ '<STR_LIT>' ] = timeutils . utcnow ( ) <EOL> values [ '<STR_LIT>' ] = None <EOL> elif ( ref . provision_state == states . INSPECTING and <EOL> values [ '<STR_LIT>' ] == states . INSPECTFAIL ) : <EOL> values [ '<STR_LIT>' ] = None <EOL> ref . update ( values ) <EOL> return ref <EOL> def get_port_by_id ( self , port_id ) : <EOL> query = model_query ( models . Port ) . filter_by ( id = port_id ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortNotFound ( port = port_id ) <EOL> def get_port_by_uuid ( self , port_uuid ) : <EOL> query = model_query ( models . Port ) . filter_by ( uuid = port_uuid ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortNotFound ( port = port_uuid ) <EOL> def get_port_by_address ( self , address ) : <EOL> query = model_query ( models . Port ) . filter_by ( address = address ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortNotFound ( port = address ) <EOL> def get_port_list ( self , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> return _paginate_query ( models . Port , limit , marker , <EOL> sort_key , sort_dir ) <EOL> def get_ports_by_node_id ( self , node_id , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> query = model_query ( models . Port ) <EOL> query = query . filter_by ( node_id = node_id ) <EOL> return _paginate_query ( models . Port , limit , marker , <EOL> sort_key , sort_dir , query ) <EOL> def get_ports_by_portgroup_id ( self , portgroup_id , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> query = model_query ( models . Port ) <EOL> query = query . filter_by ( portgroup_id = portgroup_id ) <EOL> return _paginate_query ( models . Port , limit , marker , <EOL> sort_key , sort_dir , query ) <EOL> def create_port ( self , values ) : <EOL> if not values . get ( '<STR_LIT>' ) : <EOL> values [ '<STR_LIT>' ] = uuidutils . generate_uuid ( ) <EOL> port = models . Port ( ) <EOL> port . update ( values ) <EOL> with _session_for_write ( ) as session : <EOL> try : <EOL> session . add ( port ) <EOL> session . flush ( ) <EOL> except db_exc . DBDuplicateEntry as exc : <EOL> if '<STR_LIT:address>' in exc . columns : <EOL> raise exception . MACAlreadyExists ( mac = values [ '<STR_LIT:address>' ] ) <EOL> raise exception . PortAlreadyExists ( uuid = values [ '<STR_LIT>' ] ) <EOL> return port <EOL> def update_port ( self , port_id , values ) : <EOL> if '<STR_LIT>' in values : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidParameterValue ( err = msg ) <EOL> try : <EOL> with _session_for_write ( ) as session : <EOL> query = model_query ( models . Port ) <EOL> query = add_port_filter ( query , port_id ) <EOL> ref = query . one ( ) <EOL> ref . update ( values ) <EOL> session . flush ( ) <EOL> except NoResultFound : <EOL> raise exception . PortNotFound ( port = port_id ) <EOL> except db_exc . DBDuplicateEntry : <EOL> raise exception . MACAlreadyExists ( mac = values [ '<STR_LIT:address>' ] ) <EOL> return ref <EOL> def destroy_port ( self , port_id ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Port ) <EOL> query = add_port_filter ( query , port_id ) <EOL> count = query . delete ( ) <EOL> if count == <NUM_LIT:0> : <EOL> raise exception . PortNotFound ( port = port_id ) <EOL> def get_portgroup_by_id ( self , portgroup_id ) : <EOL> query = model_query ( models . Portgroup ) . filter_by ( id = portgroup_id ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortgroupNotFound ( portgroup = portgroup_id ) <EOL> def get_portgroup_by_uuid ( self , portgroup_uuid ) : <EOL> query = model_query ( models . Portgroup ) . filter_by ( uuid = portgroup_uuid ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortgroupNotFound ( portgroup = portgroup_uuid ) <EOL> def get_portgroup_by_address ( self , address ) : <EOL> query = model_query ( models . Portgroup ) . filter_by ( address = address ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortgroupNotFound ( portgroup = address ) <EOL> def get_portgroup_by_name ( self , name ) : <EOL> query = model_query ( models . Portgroup ) . filter_by ( name = name ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . PortgroupNotFound ( portgroup = name ) <EOL> def get_portgroup_list ( self , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> return _paginate_query ( models . Portgroup , limit , marker , <EOL> sort_key , sort_dir ) <EOL> def get_portgroups_by_node_id ( self , node_id , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> query = model_query ( models . Portgroup ) <EOL> query = query . filter_by ( node_id = node_id ) <EOL> return _paginate_query ( models . Portgroup , limit , marker , <EOL> sort_key , sort_dir , query ) <EOL> def create_portgroup ( self , values ) : <EOL> if not values . get ( '<STR_LIT>' ) : <EOL> values [ '<STR_LIT>' ] = uuidutils . generate_uuid ( ) <EOL> portgroup = models . Portgroup ( ) <EOL> portgroup . update ( values ) <EOL> with _session_for_write ( ) as session : <EOL> try : <EOL> session . add ( portgroup ) <EOL> session . flush ( ) <EOL> except db_exc . DBDuplicateEntry as exc : <EOL> if '<STR_LIT:name>' in exc . columns : <EOL> raise exception . PortgroupDuplicateName ( name = values [ '<STR_LIT:name>' ] ) <EOL> elif '<STR_LIT:address>' in exc . columns : <EOL> raise exception . PortgroupMACAlreadyExists ( <EOL> mac = values [ '<STR_LIT:address>' ] ) <EOL> raise exception . PortgroupAlreadyExists ( uuid = values [ '<STR_LIT>' ] ) <EOL> return portgroup <EOL> def update_portgroup ( self , portgroup_id , values ) : <EOL> if '<STR_LIT>' in values : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidParameterValue ( err = msg ) <EOL> with _session_for_write ( ) as session : <EOL> try : <EOL> query = model_query ( models . Portgroup ) <EOL> query = add_portgroup_filter ( query , portgroup_id ) <EOL> ref = query . one ( ) <EOL> ref . update ( values ) <EOL> session . flush ( ) <EOL> except NoResultFound : <EOL> raise exception . PortgroupNotFound ( portgroup = portgroup_id ) <EOL> except db_exc . DBDuplicateEntry as exc : <EOL> if '<STR_LIT:name>' in exc . columns : <EOL> raise exception . PortgroupDuplicateName ( name = values [ '<STR_LIT:name>' ] ) <EOL> elif '<STR_LIT:address>' in exc . columns : <EOL> raise exception . PortgroupMACAlreadyExists ( <EOL> mac = values [ '<STR_LIT:address>' ] ) <EOL> else : <EOL> raise exc <EOL> return ref <EOL> def destroy_portgroup ( self , portgroup_id ) : <EOL> def portgroup_not_empty ( session ) : <EOL> """<STR_LIT>""" <EOL> query = model_query ( models . Port ) <EOL> query = add_port_filter_by_portgroup ( query , portgroup_id ) <EOL> return query . count ( ) != <NUM_LIT:0> <EOL> with _session_for_write ( ) as session : <EOL> if portgroup_not_empty ( session ) : <EOL> raise exception . PortgroupNotEmpty ( portgroup = portgroup_id ) <EOL> query = model_query ( models . Portgroup , session = session ) <EOL> query = add_identity_filter ( query , portgroup_id ) <EOL> count = query . delete ( ) <EOL> if count == <NUM_LIT:0> : <EOL> raise exception . PortgroupNotFound ( portgroup = portgroup_id ) <EOL> def get_chassis_by_id ( self , chassis_id ) : <EOL> query = model_query ( models . Chassis ) . filter_by ( id = chassis_id ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . ChassisNotFound ( chassis = chassis_id ) <EOL> def get_chassis_by_uuid ( self , chassis_uuid ) : <EOL> query = model_query ( models . Chassis ) . filter_by ( uuid = chassis_uuid ) <EOL> try : <EOL> return query . one ( ) <EOL> except NoResultFound : <EOL> raise exception . ChassisNotFound ( chassis = chassis_uuid ) <EOL> def get_chassis_list ( self , limit = None , marker = None , <EOL> sort_key = None , sort_dir = None ) : <EOL> return _paginate_query ( models . Chassis , limit , marker , <EOL> sort_key , sort_dir ) <EOL> def create_chassis ( self , values ) : <EOL> if not values . get ( '<STR_LIT>' ) : <EOL> values [ '<STR_LIT>' ] = uuidutils . generate_uuid ( ) <EOL> chassis = models . Chassis ( ) <EOL> chassis . update ( values ) <EOL> with _session_for_write ( ) as session : <EOL> try : <EOL> session . add ( chassis ) <EOL> session . flush ( ) <EOL> except db_exc . DBDuplicateEntry : <EOL> raise exception . ChassisAlreadyExists ( uuid = values [ '<STR_LIT>' ] ) <EOL> return chassis <EOL> def update_chassis ( self , chassis_id , values ) : <EOL> if '<STR_LIT>' in values : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . InvalidParameterValue ( err = msg ) <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Chassis ) <EOL> query = add_identity_filter ( query , chassis_id ) <EOL> count = query . update ( values ) <EOL> if count != <NUM_LIT:1> : <EOL> raise exception . ChassisNotFound ( chassis = chassis_id ) <EOL> ref = query . one ( ) <EOL> return ref <EOL> def destroy_chassis ( self , chassis_id ) : <EOL> def chassis_not_empty ( ) : <EOL> """<STR_LIT>""" <EOL> query = model_query ( models . Node ) <EOL> query = add_node_filter_by_chassis ( query , chassis_id ) <EOL> return query . count ( ) != <NUM_LIT:0> <EOL> with _session_for_write ( ) : <EOL> if chassis_not_empty ( ) : <EOL> raise exception . ChassisNotEmpty ( chassis = chassis_id ) <EOL> query = model_query ( models . Chassis ) <EOL> query = add_identity_filter ( query , chassis_id ) <EOL> count = query . delete ( ) <EOL> if count != <NUM_LIT:1> : <EOL> raise exception . ChassisNotFound ( chassis = chassis_id ) <EOL> def register_conductor ( self , values , update_existing = False ) : <EOL> with _session_for_write ( ) as session : <EOL> query = ( model_query ( models . Conductor ) <EOL> . filter_by ( hostname = values [ '<STR_LIT>' ] ) ) <EOL> try : <EOL> ref = query . one ( ) <EOL> if ref . online is True and not update_existing : <EOL> raise exception . ConductorAlreadyRegistered ( <EOL> conductor = values [ '<STR_LIT>' ] ) <EOL> except NoResultFound : <EOL> ref = models . Conductor ( ) <EOL> session . add ( ref ) <EOL> ref . update ( values ) <EOL> ref . update ( { '<STR_LIT>' : timeutils . utcnow ( ) , <EOL> '<STR_LIT>' : True } ) <EOL> return ref <EOL> def get_conductor ( self , hostname ) : <EOL> try : <EOL> return ( model_query ( models . Conductor ) <EOL> . filter_by ( hostname = hostname , online = True ) <EOL> . one ( ) ) <EOL> except NoResultFound : <EOL> raise exception . ConductorNotFound ( conductor = hostname ) <EOL> def unregister_conductor ( self , hostname ) : <EOL> with _session_for_write ( ) : <EOL> query = ( model_query ( models . Conductor ) <EOL> . filter_by ( hostname = hostname , online = True ) ) <EOL> count = query . update ( { '<STR_LIT>' : False } ) <EOL> if count == <NUM_LIT:0> : <EOL> raise exception . ConductorNotFound ( conductor = hostname ) <EOL> def touch_conductor ( self , hostname ) : <EOL> with _session_for_write ( ) : <EOL> query = ( model_query ( models . Conductor ) <EOL> . filter_by ( hostname = hostname ) ) <EOL> count = query . update ( { '<STR_LIT>' : timeutils . utcnow ( ) , <EOL> '<STR_LIT>' : True } ) <EOL> if count == <NUM_LIT:0> : <EOL> raise exception . ConductorNotFound ( conductor = hostname ) <EOL> def clear_node_reservations_for_conductor ( self , hostname ) : <EOL> nodes = [ ] <EOL> with _session_for_write ( ) : <EOL> query = ( model_query ( models . Node ) <EOL> . filter_by ( reservation = hostname ) ) <EOL> nodes = [ node [ '<STR_LIT>' ] for node in query ] <EOL> query . update ( { '<STR_LIT>' : None } ) <EOL> if nodes : <EOL> nodes = '<STR_LIT:U+002CU+0020>' . join ( nodes ) <EOL> LOG . warning ( <EOL> _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , { '<STR_LIT>' : hostname , '<STR_LIT>' : nodes } ) <EOL> def get_active_driver_dict ( self , interval = None ) : <EOL> if interval is None : <EOL> interval = CONF . conductor . heartbeat_timeout <EOL> limit = timeutils . utcnow ( ) - datetime . timedelta ( seconds = interval ) <EOL> result = ( model_query ( models . Conductor ) <EOL> . filter_by ( online = True ) <EOL> . filter ( models . Conductor . updated_at >= limit ) <EOL> . all ( ) ) <EOL> d2c = collections . defaultdict ( set ) <EOL> for row in result : <EOL> for driver in row [ '<STR_LIT>' ] : <EOL> d2c [ driver ] . add ( row [ '<STR_LIT>' ] ) <EOL> return d2c <EOL> def get_offline_conductors ( self ) : <EOL> interval = CONF . conductor . heartbeat_timeout <EOL> limit = timeutils . utcnow ( ) - datetime . timedelta ( seconds = interval ) <EOL> result = ( model_query ( models . Conductor ) . filter_by ( ) <EOL> . filter ( models . Conductor . updated_at < limit ) <EOL> . all ( ) ) <EOL> return [ row [ '<STR_LIT>' ] for row in result ] <EOL> def touch_node_provisioning ( self , node_id ) : <EOL> with _session_for_write ( ) : <EOL> query = model_query ( models . Node ) <EOL> query = add_identity_filter ( query , node_id ) <EOL> count = query . update ( { '<STR_LIT>' : timeutils . utcnow ( ) } ) <EOL> if count == <NUM_LIT:0> : <EOL> raise exception . NodeNotFound ( node_id ) <EOL> def _check_node_exists ( self , node_id ) : <EOL> if not model_query ( models . Node ) . filter_by ( id = node_id ) . scalar ( ) : <EOL> raise exception . NodeNotFound ( node = node_id ) <EOL> def set_node_tags ( self , node_id , tags ) : <EOL> tags = set ( tags ) <EOL> with _session_for_write ( ) as session : <EOL> self . unset_node_tags ( node_id ) <EOL> node_tags = [ ] <EOL> for tag in tags : <EOL> node_tag = models . NodeTag ( tag = tag , node_id = node_id ) <EOL> session . add ( node_tag ) <EOL> node_tags . append ( node_tag ) <EOL> return node_tags <EOL> def unset_node_tags ( self , node_id ) : <EOL> self . _check_node_exists ( node_id ) <EOL> with _session_for_write ( ) : <EOL> model_query ( models . NodeTag ) . filter_by ( node_id = node_id ) . delete ( ) <EOL> def get_node_tags_by_node_id ( self , node_id ) : <EOL> self . _check_node_exists ( node_id ) <EOL> result = ( model_query ( models . NodeTag ) <EOL> . filter_by ( node_id = node_id ) <EOL> . all ( ) ) <EOL> return result <EOL> def add_node_tag ( self , node_id , tag ) : <EOL> node_tag = models . NodeTag ( tag = tag , node_id = node_id ) <EOL> self . _check_node_exists ( node_id ) <EOL> try : <EOL> with _session_for_write ( ) as session : <EOL> session . add ( node_tag ) <EOL> session . flush ( ) <EOL> except db_exc . DBDuplicateEntry : <EOL> pass <EOL> return node_tag <EOL> def delete_node_tag ( self , node_id , tag ) : <EOL> self . _check_node_exists ( node_id ) <EOL> with _session_for_write ( ) : <EOL> result = model_query ( models . NodeTag ) . filter_by ( <EOL> node_id = node_id , tag = tag ) . delete ( ) <EOL> if not result : <EOL> raise exception . NodeTagNotFound ( node_id = node_id , tag = tag ) <EOL> def node_tag_exists ( self , node_id , tag ) : <EOL> q = model_query ( models . NodeTag ) . filter_by ( node_id = node_id , tag = tag ) <EOL> return model_query ( q . exists ( ) ) . scalar ( ) </s>
