class APIService(threading.Thread): @trace def __init__(self, pid, passive, active, background, debug, port, hostname): super(APIService, self).__init__() self._stop = threading.Event() self.pid = pid self.abort = False self.aborted = False self.port = port self.hostname = hostname self.api = API(pid, passive, active, background) cbdebug("Initializing API Service on port " + str(self.port)) if debug is None: self.server = AsyncDocXMLRPCServer((self.hostname, int(self.port)), allow_none=True) else: self.server = DocXMLRPCServer((self.hostname, int(self.port)), allow_none=True) self.server.abort = False self.server.aborted = False self.server.set_server_title("API Service (xmlrpc)") self.server.set_server_name("API Service (xmlrpc)") #self.server.register_introspection_functions() self.api.signatures = {} for methodtuple in inspect.getmembers(self.api, predicate=inspect.ismethod): name = methodtuple[0] if name in ["__init__", "success", "error"]: continue func = getattr(self.api, name) argspec = inspect.getargspec(func) spec = argspec[0] defaults = [] if argspec[3] is None else argspec[3] num_spec = len(spec) num_defaults = len(defaults) diff = num_spec - num_defaults named = diff - 1 doc = "Usage: " for x in range(1, diff): doc += spec[x] + ", " for x in range(diff, num_spec): doc += spec[x] + " = " + str(defaults[x - diff]) + ", " doc = doc[:-2] self.api.signatures[name] = {"args": spec[1:], "named": named} self.server.register_function(unwrap_kwargs(func, doc), name) # self.server.register_instance(self.api) cbdebug("API Service started") @trace def run(self): cbdebug("API Service waiting for requests...") self.server.serve_forever() cbdebug("API Service shutting down...") @trace def stop(self): cbdebug("Calling API Service shutdown....") self._stop.set() self.server.shutdown()
class APIService ( threading.Thread ): @trace def __init__(self, pid, passive, active, background, debug, port, hostname) : super(APIService, self).__init__() self._stop = threading.Event() self.pid = pid self.abort = False self.aborted = False self.port = port self.hostname = hostname self.api = API(pid, passive, active, background, port, debug) cbdebug("Initializing API Service on " + hostname + ":" + str(port)) if debug is None : self.server = AsyncDocXMLRPCServer((self.hostname, int(self.port)), allow_none = True) else : self.server = DocXMLRPCServer((self.hostname, int(self.port)), allow_none = True) self.server.abort = False self.server.aborted = False self.server.set_server_title("API Service (xmlrpc)") self.server.set_server_name("API Service (xmlrpc)") #self.server.register_introspection_functions() self.api.signatures = {} for methodtuple in inspect.getmembers(self.api, predicate=inspect.ismethod) : name = methodtuple[0] if name in ["__init__", "success", "error", "migrate" ] : continue func = getattr(self.api, name) argspec = inspect.getargspec(func) spec = argspec[0] defaults = [] if argspec[3] is None else argspec[3] num_spec = len(spec) num_defaults = len(defaults) diff = num_spec - num_defaults named = diff - 1 doc = "Usage: " for x in range(1, diff) : doc += spec[x] + ", " for x in range(diff, num_spec) : doc += spec[x] + " = " + str(defaults[x - diff]) + ", " doc = doc[:-2] self.api.signatures[name] = {"args" : spec[1:], "named" : named } self.server.register_function(unwrap_kwargs(func, doc), name) # self.server.register_instance(self.api) cbdebug("API Service started") @trace def run(self): cbdebug("API Service waiting for requests...") self.server.serve_forever() cbdebug("API Service shutting down...") @trace def stop (self) : cbdebug("Calling API Service shutdown....") self._stop.set() self.server.shutdown()
class XmlrpcListener(Listener): logger = logging.get_logger(name='xmlrpc') def __init__(self, communicator, name, params, client_request_class=None): Listener.__init__(self, communicator, name, params, client_request_class=client_request_class) if not params.has_key('service_name'): params['service_name'] = self.get_name() host = params.get('host') port = int(params.get('port')) self._server = DocXMLRPCServer(addr=(host, port), allow_none=1, logRequests=0) self.add_service(XmlrpcDispatcher(self), name=params['service_name']) def __get_instance_methods__(self, instance, prefix=None): methods = {} for o in dir(instance): if not o.startswith('_'): method = getattr(instance, o) if method.__class__.__name__ == 'instancemethod': if prefix: methods["%s.%s" % (prefix, o)] = method else: methods[o] = method return methods def add_service(self, service, **kwargs): ''' Add a service to listener. @param service: service object ''' name = kwargs.get('name', None) methods = self.__get_instance_methods__(service, name) for method_name, method in methods.items(): self._server.register_function(method, method_name) def start(self): self._server.serve_forever() def stop(self, force): try: self._server.shutdown() except Exception, error: XmlrpcListener.logger.warning(error)
class APIService ( threading.Thread ): @trace def __init__(self, pid, passive, active, background, debug, port, hostname) : super(APIService, self).__init__() self._stop = threading.Event() self.pid = pid self.abort = False self.aborted = False self.port = port self.hostname = hostname self.api = API(pid, passive, active, background) cbdebug("Initializing API Service on port " + str(self.port)) if debug is None : self.server = AsyncDocXMLRPCServer((self.hostname, int(self.port)), allow_none = True) else : self.server = DocXMLRPCServer((self.hostname, int(self.port)), allow_none = True) self.server.abort = False self.server.aborted = False self.server.set_server_title("API Service (xmlrpc)") self.server.set_server_name("API Service (xmlrpc)") #self.server.register_introspection_functions() self.server.register_instance(self.api) cbdebug("API Service started") @trace def run(self): cbdebug("API Service waiting for requests...") self.server.serve_forever() cbdebug("API Service shutting down...") @trace def stop (self) : cbdebug("Calling API Service shutdown....") self._stop.set() self.server.shutdown()
class Server(Mode): def _initialise(self, args): logging.debug('Starting server mode checks on config file') config = get_config(args.config_file) self._clients = {} self._backup_location = '' self._port = 9001 if config.has_option('server', 'backup_location'): self._backup_location = config.get('server', 'backup_location') if not os.path.isdir(self._backup_location): logging.warn( "Backup location '%s' does not exist, attempting to create it" % self._backup_location) try: os.makedirs(self._backup_location) except: raise RuntimeError( 'Could not create the requested backup location') else: raise RuntimeError('Backup location not specified in config file') if not config.has_option('server', 'port'): logging.warn('No port specified, using 9001') else: try: self._port = int(config.get('server', 'port')) except: raise RuntimeError('Server port must be an integer') for section in config.sections(): if not section == 'server': logging.debug('Found a client: %s' % section) if not config.has_option(section, 'artifacts'): raise RuntimeError( 'Client sections require an artifacts option') artifacts_string = config.get(section, 'artifacts') artifacts = {} if artifacts_string == '': raise RuntimeError('Artifacts list cannot be empty') for artifact in artifacts_string.split(','): logging.debug('Found an artifact: %s' % artifact) file_based = True filename = '' backup_command = '' restore_command = '' cleanup = False versions = 1 interval = '1h' if config.has_option(section, artifact + '_filename'): filename = config.get(section, artifact + '_filename') else: raise RuntimeError( "Artifacts must have at least a file specified. Error in client '%s'" % section) if config.has_option(section, artifact + '_backup_command'): file_based = False backup_command = config.get( section, artifact + '_backup_command') if config.has_option(section, artifact + '_restore_command'): restore_command = config.get( section, artifact + '_restore_command') else: raise RuntimeError( "A backup command was specified without a restore command. A restore command is required in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_cleanup'): tmp = config.get(section, artifact + '_cleanup') if tmp.lower() == 'true': cleanup = True elif tmp.lower() == 'false': cleanup = False else: raise RuntimeError( "Invalid option for cleanup in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_versions'): try: versions = int( config.get(section, artifact + '_versions')) except: raise RuntimeError( "Version option must be an integer in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_interval'): interval = config.get(section, artifact + '_interval') regex = "^(\d+w ?)?(\d+d ?)?(\d+h ?)?(\d+m ?)?(\d+s ?)?$" if not re.search(regex, interval): raise RuntimeError( "Interval option must in valid timedelta format. e.g. 1w2d3h4m. In client '%s', artifact '%s'" % (section, artifact)) artifacts[artifact] = { 'file_based': file_based, 'filename': filename, 'backup_command': backup_command, 'restore_command': restore_command, 'cleanup': cleanup, 'versions': versions, 'interval': interval } self._clients[section] = artifacts if not len(self._clients) > 0: raise RuntimeError('No clients specified') self._server = None def _add_arguments(self): self._parser.add_argument('config_file', metavar='CONFIGFILE') def run(self): logging.debug('Starting XMLRPC server') self._server = DocXMLRPCServer(('0.0.0.0', self._port), logRequests=False) self._server.register_instance( _XMLRPCServer(self._clients, self._backup_location)) self._server.serve_forever() def stop(self): logging.debug('Stopping XMLRPC Server') if self._server != None: self._server.shutdown()
class Server(Mode): def _initialise(self, args): logging.debug('Starting server mode checks on config file') config = get_config(args.config_file) self._clients = {} self._backup_location = '' self._port = 9001 if config.has_option('server', 'backup_location'): self._backup_location = config.get('server', 'backup_location') if not os.path.isdir(self._backup_location): logging.warn("Backup location '%s' does not exist, attempting to create it" % self._backup_location) try: os.makedirs(self._backup_location) except: raise RuntimeError('Could not create the requested backup location') else: raise RuntimeError('Backup location not specified in config file') if not config.has_option('server', 'port'): logging.warn('No port specified, using 9001') else: try: self._port = int(config.get('server', 'port')) except: raise RuntimeError('Server port must be an integer') for section in config.sections(): if not section == 'server': logging.debug('Found a client: %s' % section) if not config.has_option(section, 'artifacts'): raise RuntimeError('Client sections require an artifacts option') artifacts_string = config.get(section, 'artifacts') artifacts = {} if artifacts_string == '': raise RuntimeError('Artifacts list cannot be empty') for artifact in artifacts_string.split(','): logging.debug('Found an artifact: %s' % artifact) file_based = True filename = '' backup_command = '' restore_command = '' cleanup = False versions = 1 interval = '1h' if config.has_option(section, artifact + '_filename'): filename = config.get(section, artifact + '_filename') else: raise RuntimeError("Artifacts must have at least a file specified. Error in client '%s'" % section) if config.has_option(section, artifact + '_backup_command'): file_based = False backup_command = config.get(section, artifact + '_backup_command') if config.has_option(section, artifact + '_restore_command'): restore_command = config.get(section, artifact + '_restore_command') else: raise RuntimeError("A backup command was specified without a restore command. A restore command is required in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_cleanup'): tmp = config.get(section, artifact + '_cleanup') if tmp.lower() == 'true': cleanup = True elif tmp.lower() == 'false': cleanup = False else: raise RuntimeError("Invalid option for cleanup in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_versions'): try: versions = int(config.get(section, artifact + '_versions')) except: raise RuntimeError("Version option must be an integer in client '%s', artifact '%s'" % (section, artifact)) if config.has_option(section, artifact + '_interval'): interval = config.get(section, artifact + '_interval') regex = "^(\d+w ?)?(\d+d ?)?(\d+h ?)?(\d+m ?)?(\d+s ?)?$" if not re.search(regex, interval): raise RuntimeError("Interval option must in valid timedelta format. e.g. 1w2d3h4m. In client '%s', artifact '%s'" % (section, artifact)) artifacts[artifact] = { 'file_based': file_based, 'filename': filename, 'backup_command': backup_command, 'restore_command': restore_command, 'cleanup': cleanup, 'versions': versions, 'interval': interval } self._clients[section] = artifacts if not len(self._clients) > 0: raise RuntimeError('No clients specified') self._server = None def _add_arguments(self): self._parser.add_argument('config_file', metavar='CONFIGFILE') def run(self): logging.debug('Starting XMLRPC server') self._server = DocXMLRPCServer(('0.0.0.0', self._port), logRequests=False) self._server.register_instance(_XMLRPCServer(self._clients, self._backup_location)) self._server.serve_forever() def stop(self): logging.debug('Stopping XMLRPC Server') if self._server != None: self._server.shutdown()