def _reconcile_host(context, host_data, dom_factory=dom.DOM_Factory(), db_session=None): """ Performs the actual reconciliation at the host level :param context: The database context. :param host_data: A dictionary of data that represents the latest inventory information on the server. The data should be in the network DOM format. :param dom_factory: Optional factory used to create the DOM objects. Not required to be set. :param db_session: The database session. Should be started and finalized outside this class. """ if not db_session: db_session = session.get_session() # Parse the inventory data into a DOM object. Use the no_db DOM factory # as we want to parse into non-DB backed elements to start... non_db_fact = dom.No_DB_DOM_Factory() server_dom = dom.parse_to_host(host_data, non_db_fact) msg = (ras.vif_get_msg('info', 'RECONCILE_HOST_START') % {'host': server_dom.host_name}) ras.trace(LOG, __name__, ras.TRACE_DEBUG, msg) # Get the inventory data from the database. db_vio_servers = db.vio_server_find_all(context, server_dom.host_name, db_session) # If there are no VIO Servers, the system may be turned off. It is very # unlikely that they are actually removed (all of them at least). # Therefore, we flip the SEAs in each VioServer to a state of unavailable # and they do not show up in the UI...but are not deleted. if len(server_dom.vio_servers) == 0: LOG.info(_("Flipping host %s to unavailable due to lack of VioServers" % server_dom.host_name)) _make_system_unavailable(db_vio_servers, context, db_session) return # The first step is to find VIO Servers do add/remove/modify. Those are # the three passes that need to be made. # # We start with the idea that all of the data base items should be removed. # From there, we parse down which are still on the system (therefore need # to be modified) and then the new adds. db_vios_to_del = dom.shallow_copy_as_ordinary_list(db_vio_servers) srv_vios_to_add = [] srv_vios_to_modify = [] for vio_server in server_dom.vio_servers: db_vios = _find_vios(db_vio_servers, vio_server.lpar_id) if db_vios: srv_vios_to_modify.append(vio_server) db_vios_to_del.remove(db_vios) else: srv_vios_to_add.append(vio_server) # Now that we know what to modify/create/delete...loop through each and # execute the commands to reconcile db_host_dom = dom.Host(server_dom.host_name, db_vio_servers) # Save off the network associations first so we can recreate any that # need to be later on. net_assns = _build_net_assn_dict( db.network_association_find_all(context, db_host_dom.host_name, db_session)) for db_vios in db_vios_to_del: _remove_vios(db_vios, db_host_dom, context, db_session) for server_vios in srv_vios_to_modify: _reconcile_vios(_find_vios(db_vio_servers, server_vios.lpar_id), server_vios, context, db_session, dom_factory) for server_vios in srv_vios_to_add: _add_vios(server_vios, db_host_dom, context, db_session, dom_factory) msg = (ras.vif_get_msg('info', 'RECONCILE_HOST_END') % {'host': server_dom.host_name}) ras.trace(LOG, __name__, ras.TRACE_DEBUG, msg) # Cleanup NetworkAssociations in case any VIOSes went away or came back. _cleanup_network_associations(db_host_dom, net_assns, context, db_session)
def _get_viable_hosts(self, context, instance_uuid, hosts_from_db, network_uuids, source_host=None): """ Find the viable hosts that match the network requirements. :param context: A context object used to authorize the DB access. :param instance_uuid: The instance UUID that will be used to get the network requirements of an instance. :param hosts_from_db: A list of compute nodes :param network_uuids: An list of requested networks for the deployment. :param source_host: When this API is invoked for LPM, the host that is being migrated from. This is needed to check vswitch config on the source host. :returns: A dictionary of hosts with the hostid and the hostname """ candidate_hosts = copy.copy(hosts_from_db) # Determine which vswitches are needed for each network on source_host required_vswitches = {} if source_host: netasc_list = dom_api.network_association_find_all(context, source_host) for netasc in netasc_list: for net_uuid in network_uuids: if netasc.neutron_net_id == net_uuid and\ netasc.sea is not None: required_vswitches[net_uuid] = \ netasc.sea.get_primary_vea().vswitch_name dom_factory = dom_model.DOM_Factory() # Check if each host contains a valid network for host in hosts_from_db: # Keep track of the networks that remaining to be processed. networks_to_process = copy.copy(network_uuids) if not networks_to_process: continue # Get the DOM so that we can verify the state of the chains. host_dom = dom_model.Host(host, dom_api.vio_server_find_all(context, host)) # Walk through each of the network associations that exist for # the host and see if it was set to be used. A 'use this host' # indication is if the network associations SEA is not None netasc_list = dom_api.network_association_find_all(context, host) for net_assn in netasc_list: if net_assn.neutron_net_id in network_uuids: # No longer a network to process networks_to_process.remove(net_assn.neutron_net_id) # Check the SEA chain. At least one SEA in the chain # should be available. If there are no SEAs, then remove # it as a candidate at_least_one_available = False if net_assn.sea: s_nm = net_assn.sea.name s_lp = net_assn.sea.vio_server.lpar_id sea_chain = host_dom.find_sea_chain_for_sea_info(s_nm, s_lp) for sea in sea_chain: if sea and sea.is_available(): at_least_one_available = True break # Check the vswitches. The same vswitch name must be used # on the source and target host. vswitch_matches = True if net_assn.neutron_net_id in required_vswitches and\ net_assn.sea is not None: if(net_assn.sea.get_primary_vea().vswitch_name != required_vswitches[net_assn.neutron_net_id]): vswitch_matches = False # Check the conditions. If there isn't one available, # yet the host is still here...remove that host as a # candidate. if((not vswitch_matches or not at_least_one_available) and host in candidate_hosts): candidate_hosts.remove(host) # Skip the next, computationally expensive, step if there are no # more networks to process. if len(networks_to_process) == 0: continue # There may be some networks that haven't been processed yet. # This is typically for when a host was added after the network # was created. In this scenario, we should call down into # the adapter mapping task and find the default. The default # may be do not use. # # We don't do this by default because it's far more expensive # than a database operation, and we want the standard flow to be # as fast as possible. temp_context = context.elevated() vios_list = dom_api.vio_server_find_all(temp_context, host) host_dom = dom_factory.create_host(host, vios_list) for net_id in networks_to_process: net_info = self.get(temp_context, net_id) if not net_info or\ net_info.get('provider:segmentation_id') is None: continue vlanid = int(net_info.get('provider:segmentation_id')) sea = self.build_default_network_assn(temp_context, host_dom, vlanid, net_id) # If the SEA that came back from this is None, we need to # remove it as a candidate. We can be confident at this # point though that the network association was created, # thus speeding up our flow through next time. if sea is None or not sea.is_available(): candidate_hosts.remove(host) return candidate_hosts