def _stop(self): """ This is an auto-generated method for the PySwitchLib. """ if self._daemon_id: pyro_proxy_name = 'PySwitchLib.' + self._daemon_id uri = None try: with Pyro4.locateNS(host='localhost', port=self._pyro_ns_port) as ns: try: uri = ns.lookup(pyro_proxy_name) except: pass if uri: ns.remove(pyro_proxy_name) except: pass finally: ns_daemon_dict = ConfigFileUtil().read( filename=pyswitchlib_ns_daemon_file) if self._daemon_id in ns_daemon_dict: uri = ns_daemon_dict[self._daemon_id] del ns_daemon_dict[self._daemon_id] if len(ns_daemon_dict): ConfigFileUtil().write( filename=pyswitchlib_ns_daemon_file, conf_dict=ns_daemon_dict, do_merge=False) else: try: os.unlink(pyswitchlib_ns_daemon_file) except: pass if uri: try: with Pyro4.Proxy(uri) as pyro_proxy: pyro_proxy.shutdown() pyro_proxy._pyroRelease() except: pass super(PySwitchLibApiDaemonRunner, self)._stop()
def _restart(self): """ This is an auto-generated method for the PySwitchLib. """ daemon_prefix = ConfigUtil().get_prefix_for_daemon_id( daemon_id=self._daemon_id, conf_dict=self._pyswitchlib_conf) if daemon_prefix: if self._daemon_id in self._pyswitchlib_conf: daemon_prefixes = self._pyswitchlib_conf[ self._daemon_id].split(':') if len(daemon_prefixes) > 1: daemon_prefixes.remove(daemon_prefix) daemon_prefixes.insert(0, daemon_prefix) self._pyswitchlib_conf[self._daemon_id] = ':'.join( daemon_prefixes) ConfigFileUtil().write(filename=pyswitchlib_conf_file, conf_dict=self._pyswitchlib_conf) super(PySwitchLibApiDaemonRunner, self)._restart()
def __init__(self, ip_addr='', auth=('admin', 'password'), rest_proto=None, cacert=None, fw_ver='', timeout='', api_port=None): def on_deletion(killed_ref): self._cleanup_timer_handle() self._session.close() self._response.close() atexit.register(self._cleanup_timer_handle) self._weakref = weakref.ref(self, on_deletion) self._ip_addr = ip_addr self._auth = auth self._rest_proto_input = '' self._rest_protocol = 'http' self._attempted_rest_protocols = [] self._enabled_rest_protocols = [] self._cacert_input = '' self._os_type = 'unknown' self._os_ver = fw_ver self._os_full_ver = fw_ver self._default_connection_timeout = 60 self._default_response_timeout = 1800 self._default_session_verify = False self._session_timeout = (self._default_connection_timeout, self._default_response_timeout) self._session = requests.Session() self._response = requests.Response() self._overall_success = True self._overall_status = [] self._rest_session_auth_max_retries = 1 self._rest_session_auth_token_expiration = 160 self._rest_session_auth_token_expired = '_EXPIRED_' self._rest_session_auth_token = self._rest_session_auth_token_expired self._rest_session_timer_handle = None self._rest_config_path = '/rest/config/running' self._rest_operational_path = '/rest/operational-state' self._rest_rpc_path = '/rest/operational-state' self._rest_discover_path = '/rest' self._yang_list = None self._module_obj = None self._pyro_ns_port = None self._pyro_proxy_name = '' self._pyro_daemon_id = 'default' self._pyro_bind_max_retries = 30 self._ns_pid_file = os.path.join(os.sep, 'etc', 'pyswitchlib', '.pyswitchlib_ns.pid') self._pyswitchlib_conf_filename = os.path.join(os.sep, 'etc', 'pyswitchlib', 'pyswitchlib.conf') self._pyswitchlib_ns_daemon_filename = os.path.join( os.sep, 'etc', 'pyswitchlib', '.pyswitchlib_ns_daemon.uri') self._pyswitchlib_conf = ConfigFileUtil().read( filename=self._pyswitchlib_conf_filename) self._pyswitchlib_ns_daemon = ConfigFileUtil().read( filename=self._pyswitchlib_ns_daemon_filename) for key in self._pyswitchlib_conf: if 'ns_port' == key: self._pyro_ns_port = int(self._pyswitchlib_conf[key]) elif 'api_daemon_' in key: if sys.prefix in self._pyswitchlib_conf[key]: self._pyro_daemon_id = key elif 'cacert' == key: if cacert is None: cacert = self._pyswitchlib_conf[key] if api_port: self._pyro_ns_port = api_port if os.path.exists(self._ns_pid_file): self._pyro_proxy_name = 'PYRONAME:PySwitchLib.' + self._pyro_daemon_id if self._pyro_ns_port: self._pyro_proxy_name += '@localhost:' + str( self._pyro_ns_port) else: if self._pyswitchlib_ns_daemon: if self._pyro_daemon_id in self._pyswitchlib_ns_daemon: self._pyro_proxy_name = self._pyswitchlib_ns_daemon[ self._pyro_daemon_id] if rest_proto is not None: if rest_proto.lower() == 'http' or rest_proto.lower( ) == 'https' or rest_proto.lower() == 'auto': self._rest_proto_input = rest_proto.lower() if self._rest_proto_input == 'http' or self._rest_proto_input == 'https': self._rest_protocol = self._rest_proto_input else: raise RestProtocolTypeError( "Rest protocol type must be 'http', 'https', or 'auto'. '" + rest_proto + "' was specified.") if cacert is not None: self._cacert_input = cacert if cacert: if self._rest_protocol == 'https' or self._rest_proto_input == 'auto': if os.path.isfile(cacert): self._default_session_verify = cacert else: raise CACertificateNotFoundError( "The CA certificate file '" + cacert + "' could not be found.") else: self._default_session_verify = False elif cacert is False: self._default_session_verify = False else: raise CACertificateNotSpecifiedError( "The path to the CA certificate file is not specified.") else: self._default_session_verify = False if timeout != '': self._session_timeout = timeout self._create_timer_handle() self._discover_rest_protocol_and_paths() self._update_fw_version() self._supported_module_name = self._get_supported_module() with Pyro4.Proxy(self._pyro_proxy_name) as pyro_proxy: for n in range(self._pyro_bind_max_retries): try: pyro_proxy._pyroBind() except (Pyro4.errors.NamingError, Pyro4.errors.CommunicationError) as e: if n == 0: if self._pyswitchlib_conf and 'ns_port' in self._pyswitchlib_conf: bound_api_port = int( self._pyswitchlib_conf['ns_port']) if bound_api_port and self._pyro_ns_port and bound_api_port != self._pyro_ns_port: raise ExistingApiPortBound( "API port: " + str(bound_api_port) + " is already bound.") pyswitchlib_api_daemon = os.path.join( get_python_lib(), 'pyswitchlib', 'pyswitchlib_api_daemon.py') pyswitchlib_api_start_string = 'python ' + pyswitchlib_api_daemon + ' start' if self._pyro_ns_port: pyswitchlib_api_start_string += ' ' + str( self._pyro_ns_port) os.system(pyswitchlib_api_start_string) else: break time.sleep(1) else: raise ApiDaemonConnectionError( "Cannot connect to pyswitchlib_api_daemon.py.") self._proxied = pyro_proxy
def _get_configured_daemon(self, daemon_id='', daemon_prefix=''): """ This is an auto-generated method for the PySwitchLib. """ daemon_uri_dict = {} pyro_daemon = Pyro4.Daemon() Pyro4.config.THREADPOOL_SIZE_MIN = 10 Pyro4.config.THREADPOOL_SIZE = 200 daemon_lib_path = ConfigUtil().get_prefix_lib_path( prefix=daemon_prefix, package='pyswitchlib') if daemon_lib_path: sys.prefix = daemon_prefix sys.exec_prefix = daemon_prefix sys.path.insert(0, daemon_lib_path) pyswitchlib_api_create = __import__('pyswitchlib.api.create', fromlist=['*']) pyswitchlib_api_update = __import__('pyswitchlib.api.update', fromlist=['*']) pyswitchlib_api_delete = __import__('pyswitchlib.api.delete', fromlist=['*']) pyswitchlib_api_get = __import__('pyswitchlib.api.get', fromlist=['*']) pyswitchlib_api_rpc = __import__('pyswitchlib.api.rpc', fromlist=['*']) map( lambda filtered_api: setattr(PySwitchLibApiDaemon, filtered_api[0], filtered_api[1]), filter(lambda api: '__' not in api[0], pyswitchlib_api_create.__dict__.items())) map( lambda filtered_api: setattr(PySwitchLibApiDaemon, filtered_api[0], filtered_api[1]), filter(lambda api: '__' not in api[0], pyswitchlib_api_update.__dict__.items())) map( lambda filtered_api: setattr(PySwitchLibApiDaemon, filtered_api[0], filtered_api[1]), filter(lambda api: '__' not in api[0], pyswitchlib_api_delete.__dict__.items())) map( lambda filtered_api: setattr(PySwitchLibApiDaemon, filtered_api[0], filtered_api[1]), filter(lambda api: '__' not in api[0], pyswitchlib_api_get.__dict__.items())) map( lambda filtered_api: setattr(PySwitchLibApiDaemon, filtered_api[0], filtered_api[1]), filter(lambda api: '__' not in api[0], pyswitchlib_api_rpc.__dict__.items())) api_exposed_class = Pyro4.expose(PySwitchLibApiDaemon) daemon_obj = api_exposed_class(pyro_daemon=pyro_daemon) uri = pyro_daemon.register(daemon_obj, force=True) daemon_uri_dict[daemon_id] = uri ConfigFileUtil().write(filename=pyswitchlib_ns_daemon_file, conf_dict=daemon_uri_dict) return pyro_daemon, uri
'restart': _restart, } def run(self): """ This is an auto-generated method for the PySwitchLib. """ self._daemon_thread.start() while True: time.sleep(5) if __name__ == "__main__": pyswitchlib_conf = ConfigFileUtil().read(filename=pyswitchlib_conf_file) daemon_id = None if len(sys.argv) == 3: if sys.argv[2] in pyswitchlib_conf: daemon_id = sys.argv[2] else: daemon_id = ConfigUtil().get_daemon_id_for_prefix( prefix=sys.prefix, conf_dict=pyswitchlib_conf) if not daemon_id: daemon_id = 'default' pid_file = ConfigUtil().get_pidfilename_for_daemon_id( daemon_id=daemon_id, conf_dict=pyswitchlib_conf)
def __init__(self, sysobj, **kwargs): """ """ self.base = kwargs.pop('base') self._conn = kwargs.pop('conn') self.host = self._conn[0] auth_snmp = kwargs.pop('auth_snmp', (None, None, None, None)) self._auth = (auth_snmp[0], auth_snmp[1]) self._test = kwargs.pop('test', False) self._callback = kwargs.pop('callback', None) self._enablepass = auth_snmp[2] snmpconfig = auth_snmp[3] self._snmpversion = snmpconfig['version'] self._snmpport = snmpconfig['snmpport'] self._snmpv2c = snmpconfig['snmpv2c'] self._v3user = snmpconfig['v3user'] self._v3auth = snmpconfig['v3auth'] self._v3priv = snmpconfig['v3priv'] self._authpass = snmpconfig['authpass'] self._privpass = snmpconfig['privpass'] self._sysobj = sysobj self._proxied = None ns_daemon_dict = ConfigFileUtil().read( filename=pyswitchlib_ns_daemon_file) if pyswitchlib_daemon in ns_daemon_dict: uri = ns_daemon_dict[pyswitchlib_daemon] with Pyro4.Proxy(uri) as pyro_proxy: pyro_proxy._pyroBind() self._proxied = pyro_proxy if self._callback is None: self._callback = self._callback_main self._mgr = {} self.reconnect() # self._os_type = version_list[0][2] devicemap = SNMPUtils.SNMP_DEVICE_MAP[sysobj] self.platform_type_val = devicemap[0] self._os_type = devicemap[1] self.fullver = self.firmware_version # self.fullver = version_list[0][1] thismodule = sys.modules[__name__] os_table = getattr(thismodule, '%s_VERSIONS' % str(self.os_type).upper()) if self.fullver in os_table: ver = self.fullver else: ver = util.get_two_tuple_version(self.fullver) for router_attr in ROUTER_ATTRS: if router_attr in os_table[ver]: setattr(self.base, router_attr, os_table[ver][router_attr](self._callback)) # setattr(self.base, 'snmp', NI_VERSIONS['6.1.0T163']['snmp'](self._callback)) setattr(self, 'asset', self._mgr)
Pyro4.locateNS(host='localhost') except Pyro4.errors.NamingError: Pyro4.naming.startNSloop(host='localhost', enableBroadcast=False) def run(self): """ This is an auto-generated method for the PySwitchLib. """ self._nameserver_thread.start() while True: time.sleep(5) if __name__ == "__main__": pyswitchlib_conf = ConfigFileUtil().read(filename=pyswitchlib_conf_file) if len(sys.argv) >= 2: if sys.argv[1] == 'start': if os.path.exists(pid_file): with open(pid_file, 'r') as pid: if os.path.isdir(os.path.join(os.sep, 'proc', pid.readline().rstrip())): print(sys.argv[0].split('/')[-1] + ' is already started.') sys.exit(0) elif sys.argv[1] == 'status': if os.path.exists(pid_file): with open(pid_file, 'r') as pid: proc_pid = pid.readline().rstrip() if os.path.isdir(os.path.join(os.sep, 'proc', proc_pid)): print(sys.argv[0].split('/')[-1] + ' (pid ' + proc_pid + ') is running...')