def _inner(): if initial_delay: greenthread.sleep(initial_delay) try: while self._running: start = timeutils.utcnow() self.f(*self.args, **self.kw) end = timeutils.utcnow() if not self._running: break delay = interval - timeutils.delta_seconds(start, end) if delay <= 0: LOG.warn(_('task run outlasted interval by %s sec') % -delay) greenthread.sleep(delay if delay > 0 else 0) except LoopingCallDone as e: self.stop() done.send(e.retvalue) except Exception: LOG.exception(_('in fixed duration looping call')) done.send_exception(*sys.exc_info()) return else: done.send(True)
def _invoke_driver_thread(self, vdu, instance, vdu_name, mgmt_ip, method, arguments, event): global launchd_threads_info LOG.debug(_("Configuration of the remote VNF %s being intiated"), instance) _thr_id = str(uuid.generate_uuid()).split('-')[1] try: driver_thread = ImplThread(self._configure_service, self._condition, vdu, instance, mgmt_ip, method, arguments, event, _id=_thr_id) self._condition.acquire() launchd_threads_info[_thr_id] = { 'vm_name': instance, 'thread_obj': driver_thread, 'vnfd': vdu['_vnfd'], 'vdu': vdu_name } self._condition.release() driver_thread.start() except RuntimeError: LOG.warning(_("Configuration by the Driver Failed!"))
def monitor_thread_pool(self, thread_info=None, condition=None): global launchd_threads_info if thread_info is None: thread_info = launchd_threads_info if condition is None: condition = self._condition while True: condition.acquire() for each_thread in iter(thread_info.keys()): if 'result' in thread_info[each_thread].keys(): LOG.debug(_("Worker Thread # for VNF configuration Ending"), thread_info[each_thread]) LOG.debug(_("%s"), thread_info) #print "Result: "+str(thread_info[each_thread]['result'])+'\n' ## TODO(padmaja, anirudh): The return value to be standardized, # to enable support crosss language drivers. status = 'ERROR' if 'Error' in str(thread_info[each_thread]['result']) else 'COMPLETE' self.vplugin_rpc.send_ack(self.ctx, thread_info[each_thread]['vnfd'], thread_info[each_thread]['vdu'], thread_info[each_thread]['vm_name'], status, self.nsd_id) if thread_info[each_thread]['thread_obj'].isAlive(): thread_info[each_thread]['thread_obj'].kill() del(thread_info[each_thread]) condition.wait() condition.release()
def monitor_thread_pool(self, thread_info=None, condition=None): global launchd_threads_info if thread_info is None: thread_info = launchd_threads_info if condition is None: condition = self._condition while True: condition.acquire() for each_thread in iter(thread_info.keys()): if 'result' in thread_info[each_thread].keys(): LOG.debug(_("Worker Thread # for VNF configuration Ending"), thread_info[each_thread]) LOG.debug(_("%s"), thread_info) status = 'ERROR' if 'ERROR' in str(thread_info[each_thread]['result']) else 'COMPLETE' self.vplugin_rpc.send_ack(self.ctx, thread_info[each_thread]['vnfd'], thread_info[each_thread]['vdu'], thread_info[each_thread]['vm_name'], status, self.nsd_id, self.ns_config) if status == "COMPLETE": self.drv_conf[thread_info[each_thread]['vdu']]['COMPLETE'].append(thread_info[each_thread]['vm_name']) if thread_info[each_thread]['thread_obj'].isAlive(): thread_info[each_thread]['thread_obj'].kill() del(thread_info[each_thread]) condition.wait() condition.release()
def _inner(): if initial_delay: greenthread.sleep(initial_delay) try: while self._running: idle = self.f(*self.args, **self.kw) if not self._running: break if periodic_interval_max is not None: idle = min(idle, periodic_interval_max) LOG.debug( _('Dynamic looping call sleeping for %.02f ' 'seconds'), idle) greenthread.sleep(idle) except LoopingCallDone as e: self.stop() done.send(e.retvalue) except Exception: LOG.exception(_('in dynamic looping call')) done.send_exception(*sys.exc_info()) return else: done.send(True)
def _inner(): if initial_delay: greenthread.sleep(initial_delay) try: while self._running: start = timeutils.utcnow() self.f(*self.args, **self.kw) end = timeutils.utcnow() if not self._running: break delay = interval - timeutils.delta_seconds(start, end) if delay <= 0: LOG.warn( _('task run outlasted interval by %s sec') % -delay) greenthread.sleep(delay if delay > 0 else 0) except LoopingCallDone as e: self.stop() done.send(e.retvalue) except Exception: LOG.exception(_('in fixed duration looping call')) done.send_exception(*sys.exc_info()) return else: done.send(True)
def inner(*args, **kwargs): try: with lock(name, lock_file_prefix, external, lock_path): LOG.debug(_('Got semaphore / lock "%(function)s"'), {'function': f.__name__}) return f(*args, **kwargs) finally: LOG.debug(_('Semaphore / lock released "%(function)s"'), {'function': f.__name__})
def _configure_service(self, vdu, instance, mgmt_ip, configuration_event): status = "ERROR" try: #vdu['_drv_obj'].configure_service(instance) LOG.debug(_("Configuration of VNF being carried out on VDU:%s with IP:%s"), vdu, mgmt_ip) status = vdu['_drv_obj'].push_configuration(instance, mgmt_ip, configuration_event) except exceptions.DriverException or Exception: LOG.exception(_("Configuration of VNF Failed!!")) finally: return status
def configure_service(self, *args, **kwargs): """configure the service """ self._check_connection() conf_dict = {"private_ip": self.localip , "public_ip": self.localip , "chronos_ip": self.localip, "homer_ip": self.localip , "service_name": "homer"} confstr = templates.homer_settings.format(**conf_dict) conf_dict1 = {"service_homer": "cassandra"} confstr1 = templates.homer_service.format(**conf_dict1) configuration_done = False while not configuration_done: try: with manager.connect(host=self.mgmtip, username=self.username, password=self.password, port=830, hostkey_verify=False) as m: print "VM is UP" LOG.debug(_("VM is UP")) configuration_done = True c = m.get_config(source='running').data_xml LOG.debug(_("config %s"), c) #c1 = m.edit_config(target='candidate', config=confstr1) #m.commit() c2 = m.edit_config(target='candidate', config=confstr) m.commit(confirmed=False, timeout=300) except Exception as e: LOG.debug(_("VM is DOWN %s"), e) LOG.debug(_("VM is DOWN")) print e print "VM is down" time.sleep(5) """ with manager.connect(host=self.mgmtip, username=self.username, password=self.password, port=830, hostkey_verify=False) as m: c = m.get_config(source='running').data_xml #c1 = m.edit_config(target='candidate', config=confstr1) #m.commit() c2 = m.edit_config(target='candidate', config=confstr) m.commit() """ """ with manager.connect(host=self.mgmtip, port=830, username=self.username, password=self.password, hostkey_verify=False) as m: #m = manager.connect(self.mgmtip, port=830, username=self.username, password=self.password,hostkey_verify=False) try: print(m) print("in try") c = m.get_config(source='running').data_xml print c c1 = m.edit_config(target='candidate', config=confstr) print c1 finally: print("in final") m.commit() #m.close_session() """ """
def _invoke_driver_thread(self, vdu, instance, vdu_name): global launchd_threads_info LOG.debug(_("Configuration of the remote VNF %s being intiated"), instance) _thr_id = str(uuid.generate_uuid()).split('-')[1] try: driver_thread = ImplThread(self._configure_service, self._condition, vdu, instance, _id = _thr_id) self._condition.acquire() launchd_threads_info[_thr_id] = {'vm_name': instance, 'thread_obj': driver_thread, 'vnfd': vdu['_vnfd'], 'vdu':vdu_name} self._condition.release() driver_thread.start() except RuntimeError: LOG.warning(_("Configuration by the Driver Failed!"))
def _configure_service(self, vdu, instance, mgmt_ip, method, arguments, event): status = "ERROR" try: LOG.debug( _("Configuration of VNF being carried out on VDU:%s with IP:%s" ), vdu, mgmt_ip) configurator = getattr(vdu[event]['_drv_obj'], event) drv_args = dict({'conf': arguments, 'mgmt-ip': mgmt_ip}) status = configurator(**drv_args) except exceptions.DriverException or Exception: LOG.exception(_("Configuration of VNF Failed!!")) finally: return status
def _report_state(self): try: self.agent_state.get('configurations').update( self.cache.get_state()) ctx = context.get_admin_context_without_session() self.state_rpc.report_state(ctx, self.agent_state, self.use_call) except AttributeError: # This means the server does not support report_state LOG.warn(_("VNF server does not support state report." " State report for this agent will be disabled.")) self.heartbeat.stop() return except Exception: LOG.exception(_("Failed reporting state!")) return
def __init__(self, conf): LOG.debug(_("Bono __init__")) LOG.debug(_(conf)) self.__ncport = "830" self.retries = 10 self.homerip = "0.0.0.0" self.hsip = "0.0.0.0" self.sproutip = "0.0.0.0" self.parse(conf) try: self.__uname = "user="******"password="+self.password except KeyError: raise
def _configure_vdu(self, vdu_name, method, arguments, event, update=False): status = "" for instance in range(0, len(self.drv_conf[vdu_name]['_instances'])): instance_name = self.drv_conf[vdu_name]['_instances'][instance] if instance_name not in self.drv_conf[vdu_name][ 'COMPLETE'] or update: try: if not self.drv_conf[vdu_name][event]['_driver']: status = 'COMPLETE' self.vplugin_rpc.send_ack( self.ctx, self.drv_conf[vdu_name]['_vnfd'], vdu_name, instance_name, status, self.nsd_id, self.ns_config) self.drv_conf[vdu_name][status].append(instance_name) else: self._invoke_driver_thread( self.drv_conf[vdu_name], instance_name, vdu_name, self.drv_conf[vdu_name]['_mgmt_ips'] [instance_name], method, arguments, event) status = 'COMPLETE' except Exception: status = 'ERROR' LOG.warn(_("Configuration Failed for VNF %s"), instance_name) self.vplugin_rpc.send_ack(self.ctx, self.drv_conf[vdu_name]['_vnfd'], vdu_name, instance_name, status, self.nsd_id, self.ns_config, event=event) return status
def main(manager='vnfmanager.vnf_manager.VNFMgrWithStateReport'): # placebo func to replace the server/__init__ with project startup. # pool of threads needed to spawn worker threads for RPC. # Default action for project's startup, explictly maintainly a pool # for manager as it cannot inherit the vnfsvc's thread pool. pool = eventlet.GreenPool() pool.waitall() conf_params = read_sys_args(sys.argv) _register_opts(cfg.CONF) common_config.init(sys.argv[1:]) uuid = conf_params['uuid'] config.setup_logging(cfg.CONF) LOG.warn(_("UUID: %s"), uuid) vnfm_conf_dir = conf_params['vnfm_conf_dir'].split("/") vnfm_conf_dir[-2] = uuid vnfm_conf_dir = "/".join(vnfm_conf_dir) vnfm_conf_path = vnfm_conf_dir + uuid + '.yaml' load_vnfm_conf(vnfm_conf_path) server = vnfsvc_service.Service.create( binary='vnf-manager', topic=topics.set_topic_name(uuid, prefix=topics.VNF_MANAGER), report_interval=60, manager=manager) service.launch(server).wait()
def _report_state(self): try: self.agent_state.get('configurations').update( self.cache.get_state()) ctx = context.get_admin_context_without_session() self.state_rpc.report_state(ctx, self.agent_state, self.use_call) except AttributeError: # This means the server does not support report_state LOG.warn( _("VNF server does not support state report." " State report for this agent will be disabled.")) self.heartbeat.stop() return except Exception: LOG.exception(_("Failed reporting state!")) return
def execute(cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): try: obj, cmd = create_process(cmd, root_helper=root_helper, addl_env=addl_env) _stdout, _stderr = (process_input and obj.communicate(process_input) or obj.communicate()) obj.stdin.close() m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n" "Stderr: %(stderr)r") % { 'cmd': cmd, 'code': obj.returncode, 'stdout': _stdout, 'stderr': _stderr } if obj.returncode: LOG.error(m) if check_exit_code: raise RuntimeError(m) else: LOG.debug(m) finally: # NOTE(termie): this appears to be necessary to let the subprocess # call clean something up in between calls, without # it two execute calls in a row hangs the second one greenthread.sleep(0) return return_stderr and (_stdout, _stderr) or _stdout
def __exit__(self, exc_type, exc_val, exc_tb): try: self.unlock() self.lockfile.close() except IOError: LOG.exception(_("Could not release the acquired lock `%s`"), self.fname)
def initialize_if_enabled(): backdoor_locals = { 'exit': _dont_use_this, # So we don't exit the entire process 'quit': _dont_use_this, # So we don't exit the entire process 'fo': _find_objects, 'pgt': _print_greenthreads, 'pnt': _print_nativethreads, } if CONF.backdoor_port is None: return None start_port, end_port = _parse_port_range(str(CONF.backdoor_port)) # NOTE(johannes): The standard sys.displayhook will print the value of # the last expression and set it to __builtin__._, which overwrites # the __builtin__._ that gettext sets. Let's switch to using pprint # since it won't interact poorly with gettext, and it's easier to # read the output too. def displayhook(val): if val is not None: pprint.pprint(val) sys.displayhook = displayhook sock = _listen('localhost', start_port, end_port, eventlet.listen) # In the case of backdoor port being zero, a port number is assigned by # listen(). In any case, pull the port number out here. port = sock.getsockname()[1] LOG.info(_('Eventlet backdoor listening on %(port)s for process %(pid)d') % {'port': port, 'pid': os.getpid()}) eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock, locals=backdoor_locals) return port
class VNFMgrException(Exception): """Base VnfManager Exception. To correctly use this class, inherit from it and define a 'message' property. That message will get printf'd with the keyword arguments provided to the constructor. """ message = _("An unknown exception occurred.") def __init__(self, **kwargs): try: super(VNFMgrException, self).__init__(self.message % kwargs) self.msg = self.message % kwargs except Exception: with excutils.save_and_reraise_exception() as ctxt: if not self.use_fatal_exceptions(): ctxt.reraise = False # at least get the core message out if something happened super(VNFMgrException, self).__init__(self.message) def __unicode__(self): return unicode(self.msg) def use_fatal_exceptions(self): return False
def bool_from_string(subject, strict=False, default=False): """Interpret a string as a boolean. A case-insensitive match is performed such that strings matching 't', 'true', 'on', 'y', 'yes', or '1' are considered True and, when `strict=False`, anything else returns the value specified by 'default'. Useful for JSON-decoded stuff and config file parsing. If `strict=True`, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """ if not isinstance(subject, six.string_types): subject = six.text_type(subject) lowered = subject.strip().lower() if lowered in TRUE_STRINGS: return True elif lowered in FALSE_STRINGS: return False elif strict: acceptable = ', '.join( "'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS)) msg = _("Unrecognized value '%(val)s', acceptable values are:" " %(acceptable)s") % {'val': subject, 'acceptable': acceptable} raise ValueError(msg) else: return default
def main(manager='vnfmanager.vnf_manager.VNFMgrWithStateReport'): # placebo func to replace the server/__init__ with project startup. # pool of threads needed to spawn worker threads for RPC. # Default action for project's startup, explictly maintainly a pool # for manager as it cannot inherit the vnfsvc's thread pool. pool = eventlet.GreenPool() pool.waitall() conf_params = read_sys_args(sys.argv) _register_opts(cfg.CONF) common_config.init(sys.argv[1:]) uuid = conf_params['uuid'] config.setup_logging(cfg.CONF) LOG.warn(_("UUID: %s"), uuid) vnfm_conf_dir = conf_params['vnfm_conf_dir'].split("/") vnfm_conf_dir[-2] = uuid vnfm_conf_dir = "/".join(vnfm_conf_dir) vnfm_conf_path = vnfm_conf_dir+uuid+'.yaml' load_vnfm_conf(vnfm_conf_path) server = vnfsvc_service.Service.create( binary='vnf-manager', topic=topics.set_topic_name(uuid, prefix=topics.VNF_MANAGER), report_interval=60, manager=manager) service.launch(server).wait()
def _configure_vdu(self, vdu_name, method, arguments ,scale=False, update=False): ## ARGS ADDED BY ANIRUDH status = "" for instance in range(0,len(self.drv_conf[vdu_name]['_instances'])): instance_name = self.drv_conf[vdu_name]['_instances'][instance] if instance_name not in self.drv_conf[vdu_name]['COMPLETE'] or update: try: if not self.drv_conf[vdu_name]['_driver']: status = 'COMPLETE' self.vplugin_rpc.send_ack(self.ctx, self.drv_conf[vdu_name]['_vnfd'], vdu_name, instance_name, #self.drv_conf[vdu_name]['_mgmt_ips'][instance_name], status, self.nsd_id, self.ns_config) self.drv_conf[vdu_name][status].append(instance_name) else: self._invoke_driver_thread(self.drv_conf[vdu_name], instance_name, vdu_name, self.drv_conf[vdu_name]['_mgmt_ips'][instance_name], method, arguments, scale) status = 'COMPLETE' except Exception: status = 'ERROR' LOG.warn(_("Configuration Failed for VNF %s"), instance_name) self.vplugin_rpc.send_ack(self.ctx, self.drv_conf[vdu_name]['_vnfd'], vdu_name, instance_name, status, self.nsd_id, self.ns_config) return status
def bool_from_string(subject, strict=False, default=False): """Interpret a string as a boolean. A case-insensitive match is performed such that strings matching 't', 'true', 'on', 'y', 'yes', or '1' are considered True and, when `strict=False`, anything else returns the value specified by 'default'. Useful for JSON-decoded stuff and config file parsing. If `strict=True`, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """ if not isinstance(subject, six.string_types): subject = six.text_type(subject) lowered = subject.strip().lower() if lowered in TRUE_STRINGS: return True elif lowered in FALSE_STRINGS: return False elif strict: acceptable = ', '.join("'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS)) msg = _("Unrecognized value '%(val)s', acceptable values are:" " %(acceptable)s") % { 'val': subject, 'acceptable': acceptable } raise ValueError(msg) else: return default
def deprecated(self, msg, *args, **kwargs): """Call this method when a deprecated feature is used. If the system is configured for fatal deprecations then the message is logged at the 'critical' level and :class:`DeprecatedConfig` will be raised. Otherwise, the message will be logged (once) at the 'warn' level. :raises: :class:`DeprecatedConfig` if the system is configured for fatal deprecations. """ stdmsg = _("Deprecated: %s") % msg if CONF.fatal_deprecations: self.critical(stdmsg, *args, **kwargs) raise DeprecatedConfig(msg=stdmsg) # Using a list because a tuple with dict can't be stored in a set. sent_args = self._deprecated_messages_sent.setdefault(msg, list()) if args in sent_args: # Already logged this message, so don't log it again. return sent_args.append(args) self.warn(stdmsg, *args, **kwargs)
def parse(self,cfg): for i in range(0, len(cfg['Ims'])): regex = re.compile('^(vBono)\_\d*$') #if(cfg['Ims'][i]['name']=="vBono"): if re.search(regex, cfg['Ims'][i]['name']): LOG.debug(_("CONDITION*********************************SATISFIED")) self.username=cfg['Ims'][i]['vm_details']['image_details']['username'] self.password=cfg['Ims'][i]['vm_details']['image_details']['password'] #self.pkt_in_ips_key = cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'].keys()[0] self.localip=cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips']['ims-'+cfg['Ims'][i]['name']] self.publicip=cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips']['ims-'+cfg['Ims'][i]['name']] self.mgmtip=cfg['Ims'][i]['vm_details']['network_interfaces']['management-interface']['ips']['ims-'+cfg['Ims'][i]['name']] if(cfg['Ims'][i]['name']=="vSprout"): self.pkt_in_ips_key = cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'].keys()[0] self.sproutip=cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'][self.pkt_in_ips_key] if(cfg['Ims'][i]['name']=="vHomer"): self.pkt_in_ips_key = cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'].keys()[0] self.homerip=cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'][self.pkt_in_ips_key] if(cfg['Ims'][i]['name']=="vHS"): self.pkt_in_ips_key = cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'].keys()[0] self.hsip=cfg['Ims'][i]['vm_details']['network_interfaces']['pkt-in']['ips'][self.pkt_in_ips_key]
def execute(cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): try: obj, cmd = create_process(cmd, root_helper=root_helper, addl_env=addl_env) _stdout, _stderr = (process_input and obj.communicate(process_input) or obj.communicate()) obj.stdin.close() m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n" "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode, 'stdout': _stdout, 'stderr': _stderr} if obj.returncode: LOG.error(m) if check_exit_code: raise RuntimeError(m) else: LOG.debug(m) finally: # NOTE(termie): this appears to be necessary to let the subprocess # call clean something up in between calls, without # it two execute calls in a row hangs the second one greenthread.sleep(0) return return_stderr and (_stdout, _stderr) or _stdout
def _configure_service(self, vdu, instance, mgmt_ip, method, arguments): status = "ERROR" try: #return vdu['_drv_obj'].configure_service(instance) #vdu['_drv_obj'].configure_service(instance) LOG.debug(_("Configuration of VNF being carried out on VDU:%s with IP:%s"), vdu, mgmt_ip) configurator = getattr(vdu['_drv_obj'], method) #status = vdu['_drv_obj'].push_configuration(instance, mgmt_ip, method, arguments) drv_args = dict({'conf': arguments, 'mgmt-ip': mgmt_ip}) #arguments.fromkeys(('conf', 'mgmt-ip'), [arguments, mgmt_ip]) #arguments.update({'mgmt-ip': mgmt_ip}) status = configurator(**drv_args) except exceptions.DriverException or Exception: LOG.exception(_("Configuration of VNF Failed!!")) finally: return status
def wait(self): super(Service, self).wait() for x in self.timers: try: x.wait() except Exception: LOG.exception(_("Exception occurs when waiting for timer")) pass
def setup_logging(conf): """Sets up the logging options for a log with supplied name. :param conf: a cfg.ConfOpts object """ product_name = "vnfsvc" logging.setup(product_name) LOG.info(_("Logging enabled!"))
def _populate_vnfd_drivers(self, drv_conf): vnfd_drv_cls_path = drv_conf['_driver'] try: drv_conf['_drv_obj'] = importutils.import_object(vnfd_drv_cls_path, self.ns_config) except Exception: LOG.warn(_("%s driver not Loaded"), vnfd_drv_cls_path) raise
def configure_service(self, *args, **kwargs): """configure the service """ #self._check_connection() LOG.debug(_("Ellis configure service")) LOG.debug(_(self.hsip)) LOG.debug(_(self.homerip)) conf_dict = {"private_ip": self.localip , "public_ip": self.publicip , "chronos_ip": self.localip,"homer_ip": self.homerip, "homestead_ip": self.hsip , "service_name": "ellis"} confstr = templates.ellis_settings.format(**conf_dict) configuration_done = False while not configuration_done: try: with manager.connect(host=self.mgmtip, username=self.username, password=self.password, port=830, hostkey_verify=False) as m: print "VM is UP" LOG.debug(_("VM is UP")) configuration_done = True c = m.get_config(source='running').data_xml LOG.debug(_("config %s"), c) #c1 = m.edit_config(target='candidate', config=confstr1) #m.commit() c2 = m.edit_config(target='candidate', config=confstr) m.commit(confirmed=False, timeout=300) except Exception as e: LOG.debug(_("VM is DOWN %s"), e) LOG.debug(_("VM is DOWN")) print e print "VM is down" time.sleep(5)
def stop(self): super(Service, self).stop() for x in self.timers: try: x.stop() except Exception: LOG.exception(_("Exception occurs when timer stops")) pass self.timers = []
def string_to_bytes(text, unit_system='IEC', return_int=False): """Converts a string into an float representation of bytes. The units supported for IEC :: Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it) KB, KiB, MB, MiB, GB, GiB, TB, TiB The units supported for SI :: kb(it), Mb(it), Gb(it), Tb(it) kB, MB, GB, TB Note that the SI unit system does not support capital letter 'K' :param text: String input for bytes size conversion. :param unit_system: Unit system for byte size conversion. :param return_int: If True, returns integer representation of text in bytes. (default: decimal) :returns: Numerical representation of text in bytes. :raises ValueError: If text has an invalid value. """ try: base, reg_ex = UNIT_SYSTEM_INFO[unit_system] except KeyError: msg = _('Invalid unit system: "%s"') % unit_system raise ValueError(msg) match = reg_ex.match(text) if match: magnitude = float(match.group(1)) unit_prefix = match.group(2) if match.group(3) in ['b', 'bit']: magnitude /= 8 else: msg = _('Invalid string format: %s') % text raise ValueError(msg) if not unit_prefix: res = magnitude else: res = magnitude * pow(base, UNIT_PREFIX_EXPONENT[unit_prefix]) if return_int: return int(math.ceil(res)) return res
def monitor_thread_pool(self, thread_info=None, condition=None): global launchd_threads_info if thread_info is None: thread_info = launchd_threads_info if condition is None: condition = self._condition while True: condition.acquire() for each_thread in iter(thread_info.keys()): if 'policy' in thread_info[each_thread].keys(): pass elif 'result' in thread_info[each_thread].keys(): LOG.debug( _("Worker Thread # for VNF configuration Ending"), thread_info[each_thread]) LOG.debug(_("%s"), thread_info) status = 'ERROR' if 'ERROR' in str( thread_info[each_thread]['result']) else 'COMPLETE' if ('event' in thread_info[each_thread] ) and thread_info[each_thread]['event']: self.vplugin_rpc.send_ack( self.ctx, thread_info[each_thread]['vnfd'], thread_info[each_thread]['vdu'], thread_info[each_thread]['vm_name'], status, self.nsd_id, thread_info[each_thread]['conf'], event=thread_info[each_thread]['event']) else: self.vplugin_rpc.send_ack( self.ctx, thread_info[each_thread]['vnfd'], thread_info[each_thread]['vdu'], thread_info[each_thread]['vm_name'], status, self.nsd_id, self.ns_config) if status == "COMPLETE": self.drv_conf[thread_info[each_thread] ['vdu']]['COMPLETE'].append( thread_info[each_thread]['vm_name']) if thread_info[each_thread]['thread_obj'].isAlive(): thread_info[each_thread]['thread_obj'].kill() del (thread_info[each_thread]) condition.wait() condition.release()
def __init__(self, conf): LOG.debug(_(conf)) self.__ncport = "830" self.retries = 10 self.parse(conf) try: self.__uname = "user="******"password="+self.password except KeyError: raise
class LogConfigError(Exception): message = _('Error loading logging config %(log_config)s: %(err_msg)s') def __init__(self, log_config, err_msg): self.log_config = log_config self.err_msg = err_msg def __str__(self): return self.message % dict(log_config=self.log_config, err_msg=self.err_msg)
def _configure_service(self, vdu, instance, mgmt_ip, method, arguments): status = "ERROR" import pdb;pdb.set_trace() try: #return vdu['_drv_obj'].configure_service(instance) #vdu['_drv_obj'].configure_service(instance) LOG.debug(_("Configuration of VNF being carried out on VDU:%s with IP:%s"), vdu, mgmt_ip) configurator = getattr(vdu['_drv_obj'], method) if (type(arguments) == type({}) and 'config' in arguments.keys()): drv_args = dict({'conf': arguments['config'], 'mgmt-ip': mgmt_ip}) else: drv_args = dict({'conf': arguments, 'mgmt-ip': mgmt_ip}) #arguments.fromkeys(('conf', 'mgmt-ip'), [arguments, mgmt_ip]) #arguments.update({'mgmt-ip': mgmt_ip}) status = configurator(**drv_args) except exceptions.DriverException or Exception: LOG.exception(_("Configuration of VNF Failed!!")) finally: return status
def configure_service(self, *args, **kwargs): """configure the service """ #self._check_connection() conf_dict = {"private_ip": self.localip , "public_ip": self.localip , "chronos_ip": self.localip, "homestead_ip": self.localip , "service_name": "homestead"} confstr = templates.homestead_settings.format(**conf_dict) conf_dict1 = {"service_homestead": "cassandra"} confstr1 = templates.homestead_service.format(**conf_dict1) conf_dict2 = {"service_homestead_prov": "homestead-prov"} confstr2 = templates.homestead_prov_service.format(**conf_dict2) configuration_done = False while not configuration_done: try: with manager.connect(host=self.mgmtip, username=self.username, password=self.password, port=830, hostkey_verify=False) as m: print "VM is UP" LOG.debug(_("VM is UP")) configuration_done = True c = m.get_config(source='running').data_xml #c1 = m.edit_config(target='candidate', config=confstr1) #m.commit() c2 = m.edit_config(target='candidate', config=confstr) m.commit(confirmed=False,timeout=300) except Exception as e: LOG.debug(_("exception %s"), e) LOG.debug(_("VM is down")) print e print "VM is down" time.sleep(5) """ with manager.connect(host=self.mgmtip, username=self.username, password=self.password, port=830, hostkey_verify=False) as m: print("ncclient Successfully Connected") c = m.get_config(source='running').data_xml #c1 = m.edit_config(target='candidate', config=confstr1) #m.commit() c2 = m.edit_config(target='candidate', config=confstr) m.commit() #c3 = m.edit_config(target='candidate', config=confstr2) #m.commit() """ """
def update_vdu(self, context, scaled_vdu, config_vdu): if scaled_vdu: if self.drv_conf[scaled_vdu]: for instance in self.drv_conf[scaled_vdu]['_instances']: if instance not in self.drv_conf[scaled_vdu]['COMPLETE']: LOG.warn(_("Configuration not completed for instance %s , belongs to %s "), instance_name, scaled_vdu) raise Exception else: raise Exception LOG.warn(_("Scaled VDU information is empty!!")) if config_vdu: if self.drv_conf[config_vdu]: del self.drv_conf[config_vdu]['COMPLETE'][:] self._configure_vdu(config_vdu) else: raise Exception LOG.warn(_("To Update VDU information is empty!!"))
def push_configuration(self, instance, mgmt_ip, configuration_event): try: LOG.debug(_("***** CONFIGURATION EVENT *****:%s"),configuration_event) LOG.debug(_("***** LIFECYCLE EVENTS *****:%s"),self.lifecycle_events) config_xml = '<config>'+self.lifecycle_events[configuration_event]+'</config>' LOG.debug(_("DRIVER CODE RECEIVED IP**:%s"),mgmt_ip) self._check_connection(mgmt_ip) import pdb;pdb.set_trace() time.sleep(5) mgr = manager.connect(host=mgmt_ip, port=self.port, username=self.username, password=self.password, hostkey_verify=False) LOG.debug(_("Driver nc client manager instantiated: %s"), mgr) mgr.lock() LOG.debug(_("Configuration XML is: %s"), config_xml) mgr.edit_config(target='candidate', config=config_xml) mgr.commit() mgr.unlock() mgr.close_session() status = "COMPLETE" except KeyError: LOG.debug(_("Configuration Event not in Lifecycle Events")) status = "ERROR" except Exception: status = "ERROR" finally: return status
def _populate_vnfd_drivers(self, drv_conf): vnfd_drv_cls_path = drv_conf['_driver'] username = drv_conf['_username'] password = drv_conf['_password'] lf_events = drv_conf['_lc_events'] try: drv_conf['_drv_obj'] = importutils.import_object(vnfd_drv_cls_path, self.ns_config, username = username, password = password) except Exception: LOG.warn(_("%s driver not Loaded"), vnfd_drv_cls_path) raise
def __init__(self, host=None): super(VNFManager, self).__init__(host=host) self.vplugin_rpc = VNFPluginCallbacks(topics.PLUGIN, cfg.CONF.host) self.needs_resync_reasons = [] self.drv_conf = dict() self.conf = cfg.CONF self.ctx = context.get_admin_context_without_session() self.ns_config = dict() self.nsd_id = self.conf.vnfm_conf_d['service']['nsd_id'] self._condition = threading.Condition() monitor_daemon = threading.Thread(target=self.monitor_thread_pool) monitor_daemon.setDaemon(True) LOG.warn(_("Waiter Daemon Starting")) self.configure_vdus(self.ctx, self.conf.vnfm_conf_d) monitor_daemon.start()
def _populate_vnfd_drivers(self, drv_conf): vnfd_drv_cls_path = {} for event in drv_conf['_lc_events']: vnfd_drv_cls_path[event] = drv_conf[event]['_driver'] username = drv_conf['_username'] password = drv_conf['_password'] lf_events = drv_conf['_lc_events'] try: for event in drv_conf['_lc_events']: kwargs = { 'conf': self.ns_config, 'username': username, 'password': password } drv_conf[event]['_drv_obj'] = importutils.import_object( vnfd_drv_cls_path[event], **kwargs) except Exception: LOG.warn(_("%s driver not Loaded"), vnfd_drv_cls_path[event]) raise
def postConfigure(self, context, conf): LOG.debug(_(" In post configure")) for instance in conf['instances_list']: mgmt_ip = conf['mgmt-ip'][instance] _thr_id = str(uuid.generate_uuid()).split('-')[1] vdu = {} vdu['name'] = conf['vdu'] kwargs = { 'conf': self.ns_config, 'username': self.drv_conf[vdu['name']]['_username'], 'password': self.drv_conf[vdu['name']]['_password'] } drv_obj = importutils.import_object(conf['driver'], **kwargs) self.drv_conf[vdu['name']]['config'] = {} self.drv_conf[vdu['name']]['config']['_driver'] = conf['driver'] vdu['config'] = {} vdu['config']['_drv_obj'] = drv_obj method = 'postconfigure' arguments = conf['data'] configThread = ImplThread(self._configure_service, self._condition, vdu, instance, mgmt_ip, method, arguments, conf['event'], _id=_thr_id) self._condition.acquire() launchd_threads_info[_thr_id] = { 'vm_name': instance, 'thread_obj': configThread, 'vnfd': conf['vnf'], 'vdu': conf['vdu'], 'conf': conf, 'event': "postconfigure" } self._condition.release() configThread.start()
def initialize_if_enabled(): backdoor_locals = { 'exit': _dont_use_this, # So we don't exit the entire process 'quit': _dont_use_this, # So we don't exit the entire process 'fo': _find_objects, 'pgt': _print_greenthreads, 'pnt': _print_nativethreads, } if CONF.backdoor_port is None: return None start_port, end_port = _parse_port_range(str(CONF.backdoor_port)) # NOTE(johannes): The standard sys.displayhook will print the value of # the last expression and set it to __builtin__._, which overwrites # the __builtin__._ that gettext sets. Let's switch to using pprint # since it won't interact poorly with gettext, and it's easier to # read the output too. def displayhook(val): if val is not None: pprint.pprint(val) sys.displayhook = displayhook sock = _listen('localhost', start_port, end_port, eventlet.listen) # In the case of backdoor port being zero, a port number is assigned by # listen(). In any case, pull the port number out here. port = sock.getsockname()[1] LOG.info( _('Eventlet backdoor listening on %(port)s for process %(pid)d') % { 'port': port, 'pid': os.getpid() }) eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock, locals=backdoor_locals) return port
def _find_facility_from_conf(): facility_names = logging.handlers.SysLogHandler.facility_names facility = getattr(logging.handlers.SysLogHandler, CONF.syslog_log_facility, None) if facility is None and CONF.syslog_log_facility in facility_names: facility = facility_names.get(CONF.syslog_log_facility) if facility is None: valid_facilities = facility_names.keys() consts = ['LOG_AUTH', 'LOG_AUTHPRIV', 'LOG_CRON', 'LOG_DAEMON', 'LOG_FTP', 'LOG_KERN', 'LOG_LPR', 'LOG_MAIL', 'LOG_NEWS', 'LOG_AUTH', 'LOG_SYSLOG', 'LOG_USER', 'LOG_UUCP', 'LOG_LOCAL0', 'LOG_LOCAL1', 'LOG_LOCAL2', 'LOG_LOCAL3', 'LOG_LOCAL4', 'LOG_LOCAL5', 'LOG_LOCAL6', 'LOG_LOCAL7'] valid_facilities.extend(consts) raise TypeError(_('syslog facility must be one of: %s') % ', '.join("'%s'" % fac for fac in valid_facilities)) return facility
def read_cached_file(filename, force_reload=False): """Read from a file if it has been modified. :param force_reload: Whether to reload the file. :returns: A tuple with a boolean specifying if the data is fresh or not. """ global _FILE_CACHE if force_reload and filename in _FILE_CACHE: del _FILE_CACHE[filename] reloaded = False mtime = os.path.getmtime(filename) cache_info = _FILE_CACHE.setdefault(filename, {}) if not cache_info or mtime > cache_info.get('mtime', 0): LOG.debug(_("Reloading cached file %s") % filename) with open(filename) as fap: cache_info['data'] = fap.read() cache_info['mtime'] = mtime reloaded = True return (reloaded, cache_info['data'])
def create_process(cmd, root_helper=None, addl_env=None): """Create a process object for the given command. The return value will be a tuple of the process object and the list of command arguments used to create it. """ if root_helper: cmd = shlex.split(root_helper) + cmd cmd = map(str, cmd) LOG.debug(_("Running command: %s"), cmd) env = os.environ.copy() if addl_env: env.update(addl_env) obj = subprocess_popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) return obj, cmd
from oslo_db import options as db_options import oslo_messaging as messaging from paste import deploy from vnfmanager.common import utils from vnfmanager.openstack.common import log as logging from vnfmanager import version from vnfmanager.common import rpc as n_rpc from vnfmanager.openstack.common.gettextutils import _ LOG = logging.getLogger(__name__) core_opts = [ cfg.StrOpt('bind_host', default='0.0.0.0', help=_("The host IP to bind to")), cfg.StrOpt('host', default=utils.get_hostname(), help=_("The hostname Vnfsvc is running on")), ] """ cfg.IntOpt('bind_port', default=9010, help=_("The port to bind to")), cfg.StrOpt('api_paste_config', default="api-paste.ini", help=_("The API paste config file to use")), cfg.StrOpt('api_extensions_path', default="", help=_("The path for API extensions")), cfg.StrOpt('policy_file', default="policy.json", help=_("The policy file to use")), cfg.StrOpt('auth_strategy', default='keystone', help=_("The type of authentication to use")), cfg.StrOpt('core_plugin',
class DeprecatedConfig(Exception): message = _("Fatal call to deprecated config: %(msg)s") def __init__(self, msg): super(Exception, self).__init__(self.message % dict(msg=msg))
class InvalidPeriodicTaskArg(Exception): message = _("Unexpected argument for periodic task creation: %(arg)s.")
def lock(name, lock_file_prefix=None, external=False, lock_path=None): """Context based lock This function yields a `threading.Semaphore` instance (if we don't use eventlet.monkey_patch(), else `semaphore.Semaphore`) unless external is True, in which case, it'll yield an InterProcessLock instance. :param lock_file_prefix: The lock_file_prefix argument is used to provide lock files on disk with a meaningful prefix. :param external: The external keyword argument denotes whether this lock should work across multiple processes. This means that if two different workers both run a method decorated with @synchronized('mylock', external=True), only one of them will execute at a time. :param lock_path: The lock_path keyword argument is used to specify a special location for external lock files to live. If nothing is set, then CONF.lock_path is used as a default. """ with _semaphores_lock: try: sem = _semaphores[name] except KeyError: sem = threading.Semaphore() _semaphores[name] = sem with sem: LOG.debug(_('Got semaphore "%(lock)s"'), {'lock': name}) # NOTE(mikal): I know this looks odd if not hasattr(local.strong_store, 'locks_held'): local.strong_store.locks_held = [] local.strong_store.locks_held.append(name) try: if external and not CONF.disable_process_locking: LOG.debug(_('Attempting to grab file lock "%(lock)s"'), {'lock': name}) # We need a copy of lock_path because it is non-local local_lock_path = lock_path or CONF.lock_path if not local_lock_path: raise cfg.RequiredOptError('lock_path') if not os.path.exists(local_lock_path): fileutils.ensure_tree(local_lock_path) LOG.info(_('Created lock path: %s'), local_lock_path) def add_prefix(name, prefix): if not prefix: return name sep = '' if prefix.endswith('-') else '-' return '%s%s%s' % (prefix, sep, name) # NOTE(mikal): the lock name cannot contain directory # separators lock_file_name = add_prefix(name.replace(os.sep, '_'), lock_file_prefix) lock_file_path = os.path.join(local_lock_path, lock_file_name) try: lock = InterProcessLock(lock_file_path) with lock as lock: LOG.debug(_('Got file lock "%(lock)s" at %(path)s'), { 'lock': name, 'path': lock_file_path }) yield lock finally: LOG.debug(_('Released file lock "%(lock)s" at %(path)s'), { 'lock': name, 'path': lock_file_path }) else: yield sem finally: local.strong_store.locks_held.remove(name)