def doStartService(self, *args, **kwargs): """ Action method. """ if not self.installed(): self.automat('service-not-installed') return depends_results = [] for depend_name in self.dependent_on(): depend_service = services().get(depend_name, None) if depend_service is None: depends_results.append((depend_name, 'not found')) continue if depend_service.state != 'ON': depends_results.append((depend_name, 'not started')) continue if len(depends_results) > 0: self.automat('service-depend-off', depends_results) return lg.out(2, '[%s] STARTING' % self.service_name) try: result = self.start() except: lg.exc() self.automat('service-failed', 'exception when starting') return if isinstance(result, Deferred): result.addCallback(lambda x: self.automat('service-started')) result.addErrback(lambda x: self.automat('service-failed', x)) return if result: self.automat('service-started') else: self.automat('service-failed', 'result is %r' % result)
def isAllDependsStopped(self, *args, **kwargs): """ Condition method. """ for svc in services().values(): if self.service_name in svc.dependent_on(): if svc.state != 'OFF' and \ svc.state != 'DEPENDS_OFF' and \ svc.state != 'NOT_INSTALLED': return False return True
def doStopDependentServices(self, *args, **kwargs): """ Action method. """ count = 0 for svc in services().values(): if self.service_name in svc.dependent_on(): lg.out(6, '%r sends "stop" to %r' % (self, svc)) svc.automat('stop') count += 1 if count == 0: self.automat('depend-service-stopped')
def process(json_request): lg.out(20, 'filemanager_api.process %s' % json_request) if not driver.is_started('service_backups'): return {'result': { "success": False, "error": "network [service_backups] is not started: %s" % ( driver.services().get('service_backups', '!!! not found !!!'))}} mode = '' result = {} try: if isinstance(json_request, str) or isinstance(json_request, unicode): import json json_request = json.loads(json_request) mode = json_request['params']['mode'] if mode == 'config': result = _config(json_request['params']) elif mode == 'stats': result = _stats(json_request['params']) elif mode == 'list': result = _list(json_request['params']) elif mode == 'listlocal': result = _list_local(json_request['params']) elif mode == 'listall': result = _list_all(json_request['params']) elif mode == 'upload': result = _upload(json_request['params']) elif mode == 'delete': result = _delete(json_request['params']) elif mode == 'deleteversion': result = _delete_version(json_request['params']) elif mode == 'download': result = _download(json_request['params']) elif mode == 'tasks': result = _list_active_tasks(json_request['params']) elif mode == 'packets': result = _list_in_out_packets(json_request['params']) elif mode == 'connections': result = _list_active_connections(json_request['params']) elif mode == 'streams': result = _list_active_streams(json_request['params']) elif mode == 'debuginfo': result = _debuginfo(json_request['params']) else: result = {"result": {"success": False, "error": 'filemanager method %s not found' % mode}} except Exception as exc: lg.exc() descr = str(sys.exc_info()[0].__name__) + ': ' + str(sys.exc_info()[1]) result = {"result": {"success": False, "error": descr}} # lg.out(4, ' ERROR unknown mode: %s' % mode) lg.out(20, ' %s' % pprint.pformat(result)) return result
def process(json_request): lg.out(12, 'filemanager_api.process %s' % json_request) if not driver.is_on('service_my_data'): return {'result': { "success": False, "error": "network [service_my_data] is not started: %s" % ( driver.services().get('service_my_data', '!!! not found !!!'))}} mode = '' result = {} try: if strng.is_string(json_request): import json json_request = json.loads(json_request) mode = json_request['params']['mode'] if mode == 'config': result = _config(json_request['params']) elif mode == 'stats': result = _stats(json_request['params']) elif mode == 'list': result = _list(json_request['params']) elif mode == 'listlocal': result = _list_local(json_request['params']) elif mode == 'listall': result = _list_all(json_request['params']) elif mode == 'upload': result = _upload(json_request['params']) elif mode == 'delete': result = _delete(json_request['params']) elif mode == 'deleteversion': result = _delete_version(json_request['params']) elif mode == 'download': result = _download(json_request['params']) elif mode == 'tasks': result = _list_active_tasks(json_request['params']) elif mode == 'packets': result = _list_in_out_packets(json_request['params']) elif mode == 'connections': result = _list_active_connections(json_request['params']) elif mode == 'streams': result = _list_active_streams(json_request['params']) elif mode == 'debuginfo': result = _debuginfo(json_request['params']) else: result = {"result": {"success": False, "error": 'filemanager method %s not found' % mode}} except Exception as exc: lg.exc() descr = str(sys.exc_info()[0].__name__) + ': ' + str(sys.exc_info()[1]) result = {"result": {"success": False, "error": descr}} # lg.out(4, ' ERROR unknown mode: %s' % mode) lg.out(20, ' %s' % pprint.pformat(result)) return result
def __init__(self): if not self.service_name: raise RequireSubclass() if self.service_name in list(services().keys()): raise ServiceAlreadyExist(self.service_name) self.result_deferred = None automat.Automat.__init__( self, name=self.service_name, state='OFF', debug_level=_DebugLevel, log_events=_Debug, log_transitions=_Debug, )
def isAllDependsStopped(self, *args, **kwargs): """ Condition method. """ for svc in driver.services().values(): if self.service_name in svc.dependent_on(): if svc.state != 'OFF' and svc.state != 'DEPENDS_OFF' and svc.state != 'NOT_INSTALLED': if _Debug: lg.out( _DebugLevel, ' dependent %r not stopped yet, %r will have to wait' % ( svc, self, )) return False return True
def __init__(self): if not self.service_name: raise driver.RequireSubclass() if self.service_name in list(driver.services().keys()): raise driver.ServiceAlreadyExist(self.service_name) self.result_deferred = None if self.data_dir_required: my_data_dir_path = self.data_dir_path() if not os.path.isdir(my_data_dir_path): os.makedirs(my_data_dir_path) automat.Automat.__init__( self, name=self.service_name, state='OFF', debug_level=_DebugLevel, log_events=_Debug, log_transitions=_Debug, )
def doStartService(self, *args, **kwargs): """ Action method. """ if not self.installed(): self.automat('service-not-installed') return depends_results = [] for depend_name in self.dependent_on(): depend_service = driver.services().get(depend_name, None) if depend_service is None: depends_results.append((depend_name, 'not found')) continue if depend_service.state != 'ON': depends_results.append((depend_name, 'not started')) continue if len(depends_results) > 0: self.automat('service-depend-off', depends_results) return if _Debug: lg.out(_DebugLevel, '[%s] STARTING' % self.service_name) self.suspended = bool(self.start_suspended) try: result = self.start() except Exception as exc: lg.exc() self.automat('service-failed', exc) return if isinstance(result, Deferred): result.addCallback(lambda x: self.automat('service-started')) result.addErrback(lambda err: self.automat('service-failed', err)) return if result: self.automat('service-started') else: lg.warn('failed to start %r, result from .start() method is %r' % ( self, result, )) self.automat('service-failed', Exception('service %r failed to start' % self))
def _check_to_use_best_proto(self): # if no incoming traffic - do nothing if len(active_protos()) == 0: return True lid = my_id.getLocalIdentity() order = lid.getProtoOrder() # if no protocols in local identity - do nothing if len(order) == 0: return True # when transport proxy is working we do not need to check our contacts at all if settings.transportIsEnabled('proxy'): if driver.is_on('service_proxy_transport'): if settings.transportReceivingIsEnabled('proxy'): try: # TODO: change here to receive the value directly from service_proxy_transport object router_idurl = driver.services( )['service_proxy_transport'].transport.options[ 'router_idurl'] except: router_idurl = None if router_idurl: router_identity = identitycache.FromCache(router_idurl) contacts_is_ok = True router_protos = router_identity.getContactsByProto() if lid.getContactsNumber() != len(router_protos): contacts_is_ok = False if contacts_is_ok: for proto, contact in router_protos.items(): if lid.getProtoContact(proto) != contact: contacts_is_ok = False if contacts_is_ok: if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto returning True : proxy_transport is OK' ) return True first = order[0] # if first contact in local identity is not working yet # but there is another working methods - switch first method if first not in active_protos(): if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto first contact (%s) is not working! active_protos()=%s' % (first, str(active_protos()))) return False # #small hack to make udp as first method if all is fine # if first != 'udp' and ('udp' in active_protos() and 'tcp' in active_protos()): # lg.out(2, 'p2p_connector._check_to_use_best_proto first contact (%s) but UDP also works! active_protos()=%s' % (first, str(active_protos()))) # return False # if tcp contact is on first place and it is working - we are VERY HAPPY! - no need to change anything - return False if first == 'tcp' and 'tcp' in active_protos(): return True # but if tcp method is not the first and it works - we want to TURN IT ON! - return True if first != 'tcp' and 'tcp' in active_protos(): if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto tcp is not first but it works active_protos()=%s' % str(active_protos())) return False # if we are using udp and it is working - this is fantastic! if first == 'udp' and 'udp' in active_protos(): # but let's check if TCP is also working # in that case we want to switch to TCP if 'tcp' in active_protos(): return False return True # udp seems to be working and first contact is not working - so switch to udp if first != 'udp' and 'udp' in active_protos(): if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto udp is not first but it works active_protos()=%s' % str(active_protos())) return False # http seems to work and it is first - cool! if first == 'http' and 'http' in active_protos(): return True # but if http method is not the first and it works - we want to TURN IT ON! - return True if first != 'http' and 'http' in active_protos(): if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto http is not first but it works active_protos()=%s' % str(active_protos())) return False # if we are using proxy and it is working - that is fine - it must work always! if first == 'proxy' and 'proxy' in active_protos(): return True # proxy seems to be working and first contact is not working - so switch to proxy if first != 'proxy' and 'proxy' in active_protos(): if _Debug: lg.out( _DebugLevel - 6, 'p2p_connector._check_to_use_best_proto proxy is not first but it works active_protos()=%s' % str(active_protos())) return False # in other cases - do nothing return True