def discover_hosts(self): # this function is called when no node-discovery driver is specified; # discover the default host set in config for this driver if not self.address: raise error.OSFError('Cloud has no nodes. Specify address in ' 'cloud management driver or add node ' 'discovery driver') if not self.cached_hosts: LOG.info('Discovering host name and MAC address for %s', self.address) host = node_collection.Host(ip=self.address) mac = None if self.iface: cmd = 'cat /sys/class/net/{}/address'.format(self.iface) res = self.execute_on_cloud([host], {'command': cmd}) mac = res[0].payload['stdout'] res = self.execute_on_cloud([host], {'command': 'hostname'}) hostname = res[0].payload['stdout'] # update my hosts self.cached_hosts = [ node_collection.Host(ip=self.address, mac=mac, fqdn=hostname) ] return self.cached_hosts
def add_module_paths(paths): global MODULE_PATHS for path in paths: if not os.path.exists(path): raise error.OSFError('{!r} does not exist'.format(path)) # find all subfolders dirs = [x[0] for x in os.walk(path)] MODULE_PATHS.update(dirs)
def _get_connection(self): if self._cached_conn is None: libvirt_module = importutils.try_import('libvirt') if not libvirt_module: raise error.OSFError('libvirt-python is required ' 'to use LibvirtDriver') self._cached_conn = libvirt_module.open(self.connection_uri) return self._cached_conn
def get_default_config_file(): if 'OS_FAULTS_CONFIG' in os.environ: return os.environ['OS_FAULTS_CONFIG'] for config_file in CONFIG_FILES: if os.path.exists(config_file): return config_file msg = 'Config file is not found on any of paths: {}'.format(CONFIG_FILES) raise error.OSFError(msg)
def _get_connection(self): if self._cached_conn is None: try: import libvirt except ImportError: raise error.OSFError('libvirt-python is required ' 'to use LibvirtDriver') self._cached_conn = libvirt.open(self.connection_uri) return self._cached_conn
def verify(self): """Verify connection to the cloud.""" nodes = self.get_nodes() if not nodes: raise error.OSFError('Cloud has no nodes') task = {'command': 'hostname'} task_result = self.execute_on_cloud(nodes.hosts, task) LOG.debug('Host names of cloud nodes: %s', ', '.join(r.payload['stdout'] for r in task_result)) LOG.info('Connected to cloud successfully!')
def _read_config(config_filename): os_faults_config = config_filename or os.environ.get('OS_FAULTS_CONFIG') if os_faults_config: CONFIG_FILES.insert(0, os_faults_config) for config_file in CONFIG_FILES: if os.path.exists(config_file): with open(config_file) as fd: return yaml.safe_load(fd.read()) msg = 'Config file is not found on any of paths: {}'.format(CONFIG_FILES) raise error.OSFError(msg)
def connect(cloud_config=None, config_filename=None): """Connects to the cloud :param cloud_config: dict with cloud and power management params :param config_filename: name of the file where to read config from :returns: CloudManagement object """ if cloud_config is None: config_filename = config_filename or get_default_config_file() with open(config_filename) as fd: cloud_config = yaml.safe_load(fd.read()) jsonschema.validate(cloud_config, CONFIG_SCHEMA) cloud_management_conf = cloud_config['cloud_management'] cloud_management = _init_driver(cloud_management_conf) services = cloud_config.get('services') if services: cloud_management.update_services(services) cloud_management.validate_services() containers = cloud_config.get('containers') if containers: cloud_management.update_containers(containers) cloud_management.validate_containers() node_discover_conf = cloud_config.get('node_discover') if node_discover_conf: node_discover = _init_driver(node_discover_conf) cloud_management.set_node_discover(node_discover) power_managements_conf = cloud_config.get('power_managements') if power_managements_conf: for pm_conf in power_managements_conf: pm = _init_driver(pm_conf) cloud_management.add_power_management(pm) power_management_conf = cloud_config.get('power_management') if power_management_conf: if power_managements_conf: raise error.OSFError('Please use only power_managements') else: LOG.warning('power_management is deprecated, use ' 'power_managements instead.') power_management = _init_driver(power_management_conf) cloud_management.add_power_management(power_management) return cloud_management
def get_nodes(self, fqdns=None): """Get nodes in the cloud This function returns NodesCollection representing all nodes in the cloud or only those that has specified FQDNs. :param fqdns list of FQDNs or None to retrieve all nodes :return: NodesCollection """ if self.node_discover is None: raise error.OSFError('node_discover is not specified and "{}" ' 'driver does not support discovering'.format( self.NAME)) hosts = self.node_discover.discover_hosts() nodes = self.NODE_CLS(cloud_management=self, hosts=hosts) if fqdns: LOG.debug('Trying to find nodes with FQDNs: %s', fqdns) nodes = nodes.filter(lambda node: node.fqdn in fqdns) LOG.debug('The following nodes were found: %s', nodes.hosts) return nodes