def get_parents(class_name, id_): InternalError.check( class_name in _translations, code=InternalError.ASSERTION, message="tried to access a not-managed class in hierarchy", data={"class_name": class_name}) return _translations[class_name]['parents'](id_)
def get(class_name, id_): InternalError.check(class_name in _translations, code=InternalError.ASSERTION, message="tried to access a not-managed class in hierarchy", data={"class_name": class_name}) obj = _translations[class_name]['get'](id_) UserError.check(obj is not None, UserError.ENTITY_DOES_NOT_EXIST, message="entity doesn't exist.", data={"class_name": class_name, "id_": id_}) return obj
def get_tasks_settings(self): """ get the tasks settings of the current module :return: dict containing Config.TASKS_MAX_WORKERS :rtype: dict """ InternalError.check('tasks' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="tasks configuration missing") return self.original_settings[self.tomato_module]['tasks']
def get_account_info_update_interval(self): """ get the interval in which to update user account info :return: interval in seconds :rtype: int """ InternalError.check('account-info-update-interval' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="account-info-update-interval configuration missing") return self.original_settings[self.tomato_module]['account-info-update-interval']
def get_duration_log_settings(self): """ get duration log settings :return: dict containing 'enabled', 'location', 'size' :rtype: dict """ InternalError.check('duration-log' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="duration log configuration missing") return {k: v for k, v in self.original_settings[self.tomato_module]['duration-log'].iteritems()}
def get_host_connections_settings(self): """ get host connections settings :return: dict containing 'update-interval', 'availability-halftime', 'resource-sync-interval', 'component-timeout' :rtype: dict """ InternalError.check('host-connections' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="host connection configuration missing") return {k: v for k, v in self.original_settings[self.tomato_module]['host-connections'].iteritems()}
def get_github_settings(self): """ get the github config :return: the github config, a dict containing 'access-token', 'repository-owner', 'repository-name' :rtype: dict(str) """ InternalError.check('github' in self.original_settings, code=InternalError.CONFIGURATION_ERROR, message="github not configured") return {k: v for k, v in self.original_settings['github'].iteritems()}
def get_template_dir(self): """ get the directory where templates will be stored :return: directory path :rtype: str """ InternalError.check('templates' in self.original_settings[self.tomato_module]['paths'], code=InternalError.CONFIGURATION_ERROR, message="template directory missing") return self.original_settings[self.tomato_module]['paths']['templates']
def get_bittorrent_settings(self): """ bittorrent settings :return: dict containing 'tracker-port' and 'bittorrent-restart' :rtype: dict """ InternalError.check('bittorrent' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="bittorrent configuration missing") return {k: v for k, v in self.original_settings[self.tomato_module]['bittorrent'].iteritems()}
def get_user_quota(self, config_name): """ get quota parameters for the configuration configured in settings under this name :param str config_name: name of the configuration in settings (/user-quota/[config_name]) :return: dict containing 'cputime', 'memory', 'diskspace', 'traffic', 'continous-factor' :rtype: dict """ InternalError.check(config_name in self.original_settings['user-quota'], code=InternalError.INVALID_PARAMETER, message="No such quota config", data={"config_name": config_name}) return {k: v for k, v in self.original_settings['user-quota'][config_name].iteritems()}
def get_web_resource_location(self, resource_type): """ get the url to the specified web resource (Config.WEB_RESOURCE_*) :return: url to the resource list :rtype: str """ InternalError.check('web-resources' in self.original_settings[self.tomato_module], code=InternalError.CONFIGURATION_ERROR, message="web resource configuration missing") InternalError.check(resource_type in self.original_settings[self.tomato_module]['web-resources'], code=InternalError.INVALID_PARAMETER, message="No such web resource type", data={"resource_type": resource_type}) return self.original_settings[self.tomato_module]['web-resources'][resource_type]
def get_external_url(self, external_url): """ get the url for the given external link. :param external_url: link name (Config.EXTERNAL_URL_*) :return: the url :rtype: str """ InternalError.check(external_url in self.original_settings['external-urls'], code=InternalError.INVALID_PARAMETER, message="External URL does not exist", data={"external-url": external_url, 'existing': self.original_settings['external-urls'].keys()}) return self.original_settings['external-urls'][external_url]
def get_dumpmanager_enabled(self, tomato_module): """ get whether dumps on this module are enabled :param str tomato_module: tomato module as in Config :return: whether dumps on this module are enabled :rtype: bool """ InternalError.check(tomato_module in Config.TOMATO_MODULES, code=InternalError.INVALID_PARAMETER, message="invalid tomato module", todump=False, data={'tomato_module': tomato_module}) return self.original_settings[tomato_module]['dumps']['enabled']
def __init__(self, filename, tomato_module): """ Load settings from settings file :param str filename: path to settings file :param str tomato_module: tomato module (e.g., "web" or "backend_core") :return: None """ InternalError.check(tomato_module in Config.TOMATO_MODULES, code=InternalError.INVALID_PARAMETER, message="invalid tomato module %s" % tomato_module, todump=False, data={'tomato_module': tomato_module}) self.tomato_module = tomato_module self.filename = filename self.reload()
def reload(self): InternalError.check(os.path.exists(self.filename), code=InternalError.CONFIGURATION_ERROR, message="configuration missing", todump=False, data={'filename': self.filename}) with open(self.filename, "r") as f: print "reading settings file '%s'." % self.filename settings_content = f.read() self.original_settings = yaml.load(settings_content % OsFormatter()) self.secret_key = os.getenv('SECRET_KEY', str(random.random())) for path in filter(os.path.exists, ["/etc/tomato/backend.conf", os.path.expanduser("~/.tomato/backend.conf"), "backend.conf"]): print >> sys.stderr, "Found old-style config at %s - This is no longer supported." % (path) for path in filter(os.path.exists, ["/etc/tomato/web.conf", os.path.expanduser("~/.tomato/web.conf"), "web.conf"]): print >> sys.stderr, "Found old-style config at %s - This is no longer supported." % (path) print "debugging is %s" % ("ENABLED" if self.debugging_enabled() else "disabled")
def get_email_settings(self, message_type): """ get email parameters for a certain message type. :param str message_type: message type from Config.EMAIL_* :return: configuration including 'smtp-server', 'from', 'subject', 'body' :trype: dict(str) """ settings = self.original_settings['email'] InternalError.check(message_type in settings['messages'], code=InternalError.INVALID_PARAMETER, message="No such message template", data={"message_type": message_type}) return { 'smtp-server': settings['smtp-server'], 'from': settings['from'], 'subject': settings['messages'][message_type]['subject'], 'body': settings['messages'][message_type]['body'] }
def camera_by_id(self, cameraID): """ A method to get a camera object by using camera's ID Parameters ---------- cameraID : str Id of the camera in the database. Returns ------- :obj:`Camera` A camera object. """ if self.token is None: self._request_token() url = Client.base_URL + "cameras/" + cameraID header = self.header_builder() response = self._check_token(response=requests.get(url, headers=header), flag='GET', url=url) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) elif response.status_code == 404: raise ResourceNotFoundError(response.json()['message']) elif response.status_code == 403: raise AuthorizationError(response.json()['message']) elif response.status_code == 422: raise FormatError(response.json()['message']) else: raise InternalError() return Camera.process_json(**response.json())
def usage_by_client(self, clientID, owner): """ Parameters ---------- clientID : str Client's ID of the application. owner : str Username of the owner of the client application. Returns ------- int The number of requests made by the client. """ url = Client.base_URL + "apps/" + clientID + "/usage" param = {'owner': owner} if self.token is None: self._request_token() header = self.header_builder() response = self._check_token(response=requests.get(url, headers=header, params=param), flag='GET', url=url, params=param) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) elif response.status_code == 403: raise AuthorizationError(response.json()['message']) elif response.status_code == 404: raise ResourceNotFoundError(response.json()['message']) else: raise InternalError() return response.json()['api_usage']
def client_ids_by_owner(self, owner): """ Parameters ---------- owner : str Username of the owner of the client application. Returns ------- list of str A list of client's ID owned by the user. """ url = Client.base_URL + 'apps/by-owner' param = {'owner': owner} if self.token is None: self._request_token() header = self.header_builder() response = self._check_token(response=requests.get(url, headers=header, params=param), flag='GET', url=url, params=param) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) else: raise InternalError() clientObject = response.json() clientIDs = [] for ct in clientObject: clientIDs.append(ct['clientID']) return clientIDs
def reset_secret(self, clientID): """ Parameters ---------- clientID: str Client Id of the application. Returns -------- str New clientSecret """ url = Client.base_URL + 'apps/' + clientID + '/secret' if self.token is None: self._request_token() header = self.header_builder() response = self._check_token(response=requests.put(url, headers=header, data=None), flag='PUT', url=url, data=None) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) elif response.status_code == 404: raise ResourceNotFoundError(response.json()['message']) else: raise InternalError() return response.json()['clientSecret']
def update_permission(self, clientID, permissionLevel): """ Parameters ---------- clientID : str Client Id of the application. permissionLevel : str, optional Permission level of client. Returns ------- str Success message. """ url = Client.base_URL + 'apps/' + clientID if self.token is None: self._request_token() header = self.header_builder() data = {'permissionLevel': permissionLevel} response = self._check_token(response=requests.put(url, headers=header, data=data), flag='PUT', url=url, data=data) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) elif response.status_code == 404: raise ResourceNotFoundError(response.json()['message']) else: raise InternalError() return response.json()['message']
def register(self, owner, permissionLevel='user'): """Client initialization method. Parameters ---------- owner : str Username of the owner of the client application. permissionLevel : str, optional Permission level of the owner of the client application. Default permission level is 'user'. Returns ------- str Client id of the newly registered client application. str Client secret of the newly registered client application. """ url = Client.base_URL + 'apps/register' if self.token is None: self._request_token() header = self.header_builder() data = {'owner': owner, 'permissionLevel': permissionLevel} response = self._check_token(response=requests.post(url, headers=header, data=data), flag='POST', url=url, data=data) if response.status_code != 200: if response.status_code == 401: raise AuthenticationError(response.json()['message']) elif response.status_code == 422: raise FormatError(response.json()['message']) else: raise InternalError() return response.json()['clientID'], response.json()['clientSecret']
def _check_exists(self): """ check whether this object exists remotely. Do not modify any fields here! :return: whether it exists :rtype: bool """ raise InternalError(code=InternalError.UNKNOWN, message="this function should have been overridden", data={'function': '%s.exists' % repr(self.__class__)})
def _remove(self): """ remove the object from the server. Do not modify any fields here! You should invalidate info of other objects if needed. :return: None """ raise InternalError(code=InternalError.UNKNOWN, message="this function should have been overridden", data={'function': '%s._remove' % repr(self.__class__)})
def _fetch_info(self, fetch=False): """ fetch info from server. Do not modify any fields here! :param fetch: if true, force the server to update remote info. :return: server info as fetched from server :rtype: dict """ raise InternalError(code=InternalError.UNKNOWN, message="this function should have been overridden", data={'function': '%s._fetch_info' % repr(self.__class__)})
def __init__(self, bit_stream): (self._magic, self._n_segs, self._elem_size, self._data_size, self._var_offs, self._is_boolean) = bit_stream.readlist(self._fmt) self._is_boolean = bool(self._is_boolean) if self._magic != RLE_PAYLOAD_MAGIC: raise InternalError('Chunk payload is not RLE format')
def _modify(self, attrs): """ modify data on server. return object info. Do not modify any fields here! You should invalidate info of other objects if needed. :param dict attrs: params for modification. :return: server info as fetched from server (usually returned by ____modify) :rtype: dict """ raise InternalError(code=InternalError.UNKNOWN, message="this function should have been overridden", data={'function': '%s._modify' % repr(self.__class__)})
def _action(self, action, params): """ run an action. Do not modify any fields here! Usually, afterwards, every knowledge about this object will be invalidated. If you do not wish this, override _after_action. :param str action: action name :param str params: action params :return: action return value """ raise InternalError(code=InternalError.UNKNOWN, message="this function should have been overridden", data={'function': '%s._action' % repr(self.__class__)})
def send_close(self, portal=None, stmt=None): logger.debug('FE-send_close portal: %s', portal) if portal is not None: msg = protocol.build_close_portal(portal) elif stmt is not None: msg = protocol.build_close_stmt(stmt) else: raise InternalError( 'must special the name of portal or stmt to close') self._c._write(msg)
def get_interfaces(self, target_module): """ :param str target_module: tomato module to retrieve this information for. :return: a list of interface configs. each config is a dict, containing host, port, protocol, and optionally ssl :rtype: list(dict) """ InternalError.check(target_module in Config.TOMATO_MODULES, code=InternalError.INVALID_PARAMETER, message="invalid tomato module", todump=False, data={'tomato_module': target_module}) conf = self.original_settings['services'][target_module] if 'interfaces' in conf: res = [] for interface in conf['interfaces']: res.append({ 'host': conf['host'], 'port': interface['port'], 'protocol': interface['protocol'], 'ssl': interface['ssl'] }) return res else: return [{ 'host': conf['host'], 'port': conf['port'], 'protocol': conf['protocol'] }]
def _check_settings(self): """ Check settings for completeness. When an error is found, either fill it in, or raise an exception. :return: None """ error_found = False # set to true if tomato cannot be started bad_paths = [] # services if 'services' not in self.original_settings: print "Configuration ERROR at /services: section is missing." print " this is fatal." error_found = True bad_paths.append('/services') else: for service in default_settings['services']: service_settings = self.original_settings['services'].get(service, None) if service_settings is None: print "Configuration ERROR at /services/%s: is missing." % service print " this is fatal." error_found = True bad_paths.append('/services/%s' % service) else: if not self.original_settings['services'][service].get('host', None): print "Configuration ERROR at /services/%s/host: %s hostname not found" % (service, service) print " this is fatal." error_found = True bad_paths.append('/services/%s/host') if 'interfaces' in service_settings: interface_config_error_found = False for interface in service_settings['interfaces']: for s in ("port", "ssl", "protocol"): if interface.get(s, None) is None: print "Configuration ERROR at /services/%s/interfaces: %s not specified" % (service, s) interface_config_error_found = True if interface_config_error_found: print " Using default interface config for %s" % service if 'interface' in default_settings['services'][service]: service_settings['interface'] = default_settings['services'][service]['interface'] else: del service_settings['interface'] service_settings['port'] = default_settings['services'][service]['port'] service_settings['protocol'] = default_settings['services'][service]['protocol'] else: for s in ('port', 'protocol'): if not service_settings.get(s, None): print "Configuration ERROR at /services/%s/%s: %s not specified" % (service, s, s) if s in default_settings['services'][service]: print " using default %s." % s service_settings[s] = default_settings['services'][service][s] else: print " this is fatal." error_found = True bad_paths.append('/services/%s/%s' % (service, s)) # external urls if 'external-urls' not in self.original_settings: print "Configuration WARNING at /external-urls: section is missing." print " using defeault external urls." self.original_settings['external-urls'] = default_settings['external-urls'] else: for k, v in default_settings['external-urls'].iteritems(): if k not in self.original_settings['external-urls']: print "Configuration ERROR at /external-urls: %s missing" % k print " using default URL" self.original_settings['external-urls'][k] = v else: if not self.original_settings['external-urls'][k]: print "Configuration WARNING at /extrnal-urls/%s: is empty" % k # github if self.is_web(): if not self.original_settings['github'].get('access-token', None): print "Configuration WARNING at /github/access-token: GitHub access token is not set." print " access tokens can be created on the GitHub page in settings -> My Personal Access Tokens" print " The token needs the scope 'repo' or 'public_repo'" self.original_settings['github']['access-token'] = "" else: for s in ("repository-owner", "repository-name"): if not self.original_settings['github'].get(s, None): print "Configuration WARNING at /github/%s: %s not configured" % (s,s) print " disabling GitHub." del self.original_settings['github'] # rpc-timeout if not self.original_settings.get('rpc-timeout', None): print "Configuration WARNING at /rpc-timeout: not set" print " using default RPC timeout." self.original_settings['rpc-timeout'] = default_settings['rpc-timeout'] # e-mail if not self.original_settings.get('email', None): print "Configuration ERROR at /email: is missing." print " this is fatal." error_found = True bad_paths.append('/email') else: if not self.original_settings['email'].get('smtp-server', None): print "Configuration ERROR at /email/smtp-server: is missing." print " this is fatal." error_found = True bad_paths.append('/email/smtp-server') if not self.original_settings['email'].get('from', None): print "Configuration ERROR at /email/smtp-server/from: is missing." print " using default E-Mail sender" self.original_settings['email']['from'] = default_settings['email']['from'] if not self.original_settings['email'].get('messages', None): print "Configuration ERROR at /email/messages: is missing." print " using default messages" self.original_settings['email']['messages'] = default_settings['email']['messages'] for m, message in default_settings['email']['messages'].iteritems(): if not self.original_settings['email']['messages'].get(m, None): print "Configuration ERROR at /email/messages/%s: is missing." % m print " using default message" self.original_settings['email']['messages'][m] = message else: for k in ('subject', 'body'): if not self.original_settings['email']['messages'][m].get(k, None): print "Configuration ERROR at /email/messages/%s/%s: is missing." % (m,k) print " using default %s" % k self.original_settings['email']['messages'][m][k] = message[k] # topologies if not self.original_settings.get('topologies', None): print "Configuration ERROR at /topologies: is missing." print " using default topology settings." self.original_settings['topologies'] = default_settings['topologies'] else: for k, v in default_settings['topologies'].iteritems(): if not self.original_settings['topologies'].get(k, None): print "Configuration ERROR at /topologies/%s: not set." % k print " using default." self.original_settings['topologies'][k] = v # user-quota if not self.original_settings.get('user-quota', None): print "Configuration ERROR at /user-quota: is missing." print " using default user quota settings." self.original_settings['user-quota'] = default_settings['user-quota'] else: for q, quota in default_settings['user-quota'].iteritems(): if not self.original_settings['user-quota'].get(q, None): print "Configuration ERROR at /user-quota/%s: is missing." % q print " using default settings for %s" % q self.original_settings['user-quota'][q] = quota else: for k, v in quota.iteritems(): if not self.original_settings['user-quota'][q].get(k, None): print "Configuration ERROR at /user-quota/%s/%s: is missing." % (q, k) print " using default %s for %s" % (k, q) self.original_settings['user-quota'][q][k] = v # dumpmanager if not self.original_settings.get('dumpmanager', None): print "Configuration ERROR at /dumpmanager: is missing." print " using default dumpmanager settings." self.original_settings['dumpmanager'] = default_settings['dumpmanager'] else: for k, v in default_settings['dumpmanager'].iteritems(): if not self.original_settings['dumpmanager'].get(k, None): print "Configuration ERROR at /dumpmanager/%s: is missing." % k print " using default setting for %s" % k self.original_settings['dumpmanager'][k] = v # dumpmanager if not self.original_settings.get('debugging', None): print "Configuration ERROR at /debugging: is missing." print " using default debugging settings." self.original_settings['debugging'] = default_settings['debugging'] else: for k, v in default_settings['debugging'].iteritems(): if not self.original_settings['debugging'].get(k, None): print "Configuration ERROR at /debugging/%s: is missing." % k print " using default setting for %s" % k self.original_settings['debugging'][k] = v # tomato modules' settings for tomato_module in Config.TOMATO_MODULES: module_settings = self.original_settings.get(tomato_module, None) this_mosule_defaults = default_settings[tomato_module] if module_settings is None: print "Configuration ERROR at /%s: is missing." % tomato_module print " this is fatal." error_found = True bad_paths.append('/%s' % tomato_module) for sec, default_sec_val in this_mosule_defaults.iteritems(): if not module_settings.get(sec, None): print "Configuration ERROR at /%s/%s: is missing." % (tomato_module, sec) if sec in ('paths', 'ssl', 'database'): print " this is fatal." error_found = True bad_paths.append('/%s/%s' % (tomato_module, sec)) else: print " using default %s" % sec module_settings[sec] = default_sec_val else: if isinstance(default_sec_val, dict): for k, v in default_sec_val.iteritems(): if module_settings[sec].get(k, None) is None: print "Configuration ERROR at /%s/%s/%s: is missing." % (tomato_module, sec, k) if (sec == "database" and k == "server") or (sec in ('paths', 'ssl')) or (sec == 'duration-log' and k == 'location'): print " this is fatal." error_found = True bad_paths.append('/%s/%s/%s' % (tomato_module, sec, k)) else: print " using default." module_settings[sec][k] = v else: if sec == "database" and k == 'server': if not module_settings[sec][k].get('host', None): print "Configuration ERROR at %s/%s/%s/host" % (tomato_module, sec, k) print " this is fatal." error_found = True bad_paths.append('%s/%s/%s/host' % (tomato_module, sec, k)) if not module_settings[sec][k].get('port', None): print "Configuration ERROR at %s/%s/%s/port" % (tomato_module, sec, k) print " using default port." module_settings[sec][k]['port'] = v['port'] InternalError.check(not error_found, code=InternalError.CONFIGURATION_ERROR, message="fatal configuration error", todump=False, data={'bad_paths': bad_paths})
def get_parents(class_name, id_): InternalError.check(class_name in _translations, code=InternalError.ASSERTION, message="tried to access a not-managed class in hierarchy", data={"class_name": class_name}) return _translations[class_name]['parents'](id_)