def __init__(self): """ Initiate DbHelper, Logs and config """ XplPlugin.__init__(self, 'xplgw') MQAsyncSub.__init__(self, self.zmq, 'xplgw', ['client.conversion', 'client.list']) self.log.info(u"XPL manager initialisation...") self._db = DbHelper() self.pub = MQPub(zmq.Context(), 'xplgw') self.stats = None self.client_xpl_map = {} self.client_conversion_map = {} self._load_client_to_xpl_target() self._load_conversions() self.load() self.ready()
def __init__(self, stop): """ prepare an empty package list """ ### init vars self._stop = stop self._clients = {} self._clients_with_details = {} ### init logger log = logger.Logger('manager') self.log = log.get_logger('manager') self.log.info(u"Clients initialisation") self._pub = MQPub(zmq.Context(), 'manager') ### Check for dead clients thr_check_dead_clients = Thread(None, self._check_dead_clients, "check_dead_clients", (), {}) thr_check_dead_clients.start()
def __init__(self, name, stop_cb = None, is_manager = False, parser = None, daemonize = True, test = False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info(u"The plugin is starting in TEST mode. Test option is {0}".format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format(self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format(self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format(self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format(self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error(u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part")
class Plugin(BasePlugin, MQRep): ''' Global plugin class, manage signal handlers. This class shouldn't be used as-it but should be extended by no xPL plugin or by the class xPL plugin which will be used by the xPL plugins This class is a Singleton ''' def __init__(self, name, stop_cb = None, is_manager = False, parser = None, daemonize = True, test = False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info(u"The plugin is starting in TEST mode. Test option is {0}".format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format(self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format(self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format(self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format(self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error(u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part") def check_configured(self): """ For a plugin only To be call in the plugin __init__() Check in database (over queryconfig) if the key 'configured' is set to True for the plugin if not, stop the plugin and log this """ self._config = Query(self.zmq, self.log) configured = self._config.query(self._name, 'configured') if configured == '1': configured = True if configured != True: self.log.error(u"The plugin is not configured (configured = '{0}'. Stopping the plugin...".format(configured)) self.force_leave(status = STATUS_NOT_CONFIGURED) return False self.log.info(u"The plugin is configured. Continuing (hoping that the user applied the appropriate configuration ;)") return True def _load_json(self): """ Load the plugin json file """ try: self.log.info(u"Read the json file and validate id".format(self._name)) pkg_json = PackageJson(pkg_type = "plugin", name = self._name) # check if json is valid if pkg_json.validate() == False: # TODO : how to get the reason ? self.log.error(u"Invalid json file") self.force_leave(status = STATUS_INVALID) else: # if valid, store the data so that it can be used later self.log.info(u"The json file is valid") self.json_data = pkg_json.get_json() except: self.log.error(u"Error while trying to read the json file : {1}".format(self._name, traceback.format_exc())) self.force_leave(status = STATUS_INVALID) def get_config(self, key): """ Try to get the config over the MQ. If value is None, get the default value """ value = self._config.query(self._name, key) if value == None or value == 'None': self.log.info(u"Value for '{0}' is None or 'None' : trying to get the default value instead...".format(key)) value = self.get_config_default_value(key) self.log.info(u"Value for '{0}' is : {1}".format(key, value)) return self.cast_config_value(key, value) def get_config_default_value(self, key): """ Get the default value for a config key from the json file @param key : configuration key """ for idx in range(len(self.json_data['configuration'])): if self.json_data['configuration'][idx]['key'] == key: default = self.json_data['configuration'][idx]['default'] self.log.info(u"Default value required for key '{0}' = {1}".format(key, default)) return default def cast_config_value(self, key, value): """ Cast the config value as the given type in the json file @param key : configuration key @param value : configuration value to cast and return @return : the casted value """ for idx in range(len(self.json_data['configuration'])): if self.json_data['configuration'][idx]['key'] == key: type = self.json_data['configuration'][idx]['default'] self.log.info(u"Casting value for key '{0}' in type '{1}'...".format(key, type)) return self.cast(value, type) # no cast operation : return the value if value == "None": return None return value def cast(self, value, type): """ Cast a value for a type @param value : value to cast @param type : type in which you want to cast the value """ try: if type == "boolean": # just in case, the "True"/"False" are not already converted in True/False # this is (currently) done on queryconfig side if value == "True": return True elif value == "False": return False # type == choice : nothing to do if type == "date": self.log.error(u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it") if type == "datetime": self.log.error(u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it") # type == email : nothing to do if type == "float": return float(value) if type == "integer": return float(value) # type == ipv4 : nothing to do # type == multiple choice : nothing to do # type == string : nothing to do if type == "time": self.log.error(u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it") # type == url : nothing to do except: # if an error occurs : return the default value and log a warning self.log.warning(u"Error while casting value '{0}' to type '{1}'. The plugin may not work!! Error : {2}".format(value, type, traceback.format_exc())) return value return value def get_device_list(self, quit_if_no_device = False): """ Request the dbmgr component over MQ to get the devices list for this client @param quit_if_no_device: if True, exit the plugin if there is no devices """ self.log.info(u"Retrieve the devices list for this client...") mq_client = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('device.get') msg.add_data('type', 'plugin') msg.add_data('name', self._name) msg.add_data('host', self.get_sanitized_hostname()) result = mq_client.request('dbmgr', msg.get(), timeout=10) if not result: self.log.error(u"Unable to retrieve the device list") self.force_leave() return [] else: device_list = result.get_data()['devices'] if device_list == []: self.log.warn(u"There is no device created for this client") if quit_if_no_device: self.log.warn(u"The developper requested to stop the client if there is no device created") self.force_leave() return [] for a_device in device_list: self.log.info(u"- id : {0} / name : {1} / device type id : {2}".format(a_device['id'], \ a_device['name'], \ a_device['device_type_id'])) # log some informations about the device # notice that even if we are not in the XplPlugin class we will display xpl related informations : # for some no xpl plugins, there will just be nothing to display. # first : the stats self.log.info(u" xpl_stats features :") for a_xpl_stat in a_device['xpl_stats']: self.log.info(u" - {0}".format(a_xpl_stat)) self.log.info(u" Static Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat]['parameters']['static']: self.log.info(u" - {0} = {1}".format(a_feature['key'], a_feature['value'])) self.log.info(u" Dynamic Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat]['parameters']['dynamic']: self.log.info(u" - {0}".format(a_feature['key'])) # then, the commands self.log.info(u" xpl_commands features :") for a_xpl_cmd in a_device['xpl_commands']: self.log.info(u" - {0}".format(a_xpl_cmd)) self.log.info(u" + Parameters :") for a_feature in a_device['xpl_commands'][a_xpl_cmd]['parameters']: self.log.info(u" - {0} = {1}".format(a_feature['key'], a_feature['value'])) self.devices = device_list return device_list def device_detected(self, device_type, type, feature, data): """ The plugin developpers can call this function when a device is detected This function will check if a corresponding device exists and : - if so, do nothing - if not, add the device in a 'new devices' list - if the device is already in the 'new devices list', does nothing - if not : add it into the list and send a MQ message : an event for the UI to say a new device is detected ### TODO : implement a req/rep MQ message to allow UI to get the new devices list @param device_type : device_type of the detected device @param data : data about the device (address or any other configuration element of a device for this plugin) @param type : xpl_stats, xpl_commands @param feature : a xpl_stat or xpl_command feature """ self.log.debug(u"Device detected : device_type = {0}, data = {1}".format(device_type, data)) #self.log.debug(u"Already existing devices : {0}".format(self.devices)) # browse all devices to find if the device exists found = False for a_device in self.devices: # first, search for device type if a_device['device_type_id'] == device_type: params = a_device[type][feature]['parameters']['static'] found = True for key in data: for a_param in params: if key == a_param['key'] and data[key] != a_param['value']: found = False break if found: break if found: self.log.debug(u"The device already exists : id={0}.".format(a_device['id'])) else: self.log.debug(u"The device doesn't exists in database") # add the device feature in the new devices list : self.new_devices[device_type][type][feature] = data self.log.debug(u"Check if the device has already be marked as new...") found = False for a_device in self.new_devices: if a_device['device_type_id'] == device_type and \ a_device['type'] == type and \ a_device['feature'] == feature: if data == a_device['data']: found = True if found == False: new_device ={'device_type_id' : device_type, 'type' : type, 'feature' : feature, 'data' : data} self.log.info(u"New device feature detected and added in the new devices list : {0}".format(new_device)) self.new_devices.append(new_device) # publish new devices update self._pub.send_event('device.new', {"type" : "plugin", "name" : self._name, "host" : self.get_sanitized_hostname(), "client_id" : "plugin-{0}.{1}".format(self._name, self.get_sanitized_hostname()), "devices" : self.new_devices}) # TODO : later (0.4.0+), publish one "new device" notification with only the new device detected else: self.log.debug(u"The device has already been detected since the plugin startup") def get_parameter(self, a_device, key): """ For a device feature, return the required parameter value @param a_device: the device informations @param key: the parameter key """ try: self.log.debug(u"Get parameter '{0}'".format(key)) for a_param in a_device['parameters']: if a_param == key: value = self.cast(a_device['parameters'][a_param]['value'], a_device['parameters'][a_param]['type']) self.log.debug(u"Parameter value found: {0}".format(value)) return value self.log.warning(u"Parameter not found : return None") return None except: self.log.error(u"Error while looking for a device parameter. Return None. Error: {0}".format(traceback.format_exc())) return None def get_parameter_for_feature(self, a_device, type, feature, key): """ For a device feature, return the required parameter value @param a_device: the device informations @param type: the parameter type (xpl_stats, ...) @param feature: the parameter feature @param key: the parameter key """ try: self.log.debug(u"Get parameter '{0}' for '{1}', feature '{2}'".format(key, type, feature)) for a_param in a_device[type][feature]['parameters']['static']: if a_param['key'] == key: value = self.cast(a_param['value'], a_param['type']) self.log.debug(u"Parameter value found: {0}".format(value)) return value self.log.warning(u"Parameter not found : return None") return None except: self.log.error(u"Error while looking for a device feature parameter. Return None. Error: {0}".format(traceback.format_exc())) return None def check_for_pictures(self): """ if some products are defined, check if the corresponding pictures are present in the products/ folder """ self.log.info(u"Check if there are pictures for the defined products") ok = True ok_product = None if self.json_data.has_key('products'): for product in self.json_data['products']: ok_product = False for ext in PRODUCTS_PICTURES_EXTENSIONS: file = "{0}.{1}".format(product['id'], ext) if os.path.isfile("{0}/{1}".format(self.get_products_directory(), file)): ok_product = True break if ok_product: self.log.debug(u"- OK : {0} ({1})".format(product['name'], file)) else: ok = False self.log.warning(u"- Missing : {0} ({1}.{2})".format(product['name'], product['id'], PRODUCTS_PICTURES_EXTENSIONS)) if ok == False: self.log.warning(u"Some pictures are missing!") else: if ok_product == None: self.log.info(u"There is no products defined for this plugin") def ready(self, ioloopstart=1): """ to call at the end of the __init__ of classes that inherits of this one In the XplPLugin class, this function will be completed to also activate the xpl hbeat """ if self.dont_run_ready == True: return ### send plugin status : STATUS_ALIVE # TODO : why the dbmgr has no self._name defined ??????? # temporary set as unknown to avoir blocking bugs if not hasattr(self, '_name'): self._name = "unknown" self._set_status(STATUS_ALIVE) ### Instantiate the MQ # nothing can be launched after this line (blocking call!!!!) self.log.info(u"Start IOLoop for MQ : nothing else can be executed in the __init__ after this! Make sure that the self.ready() call is the last line of your init!!!!") if ioloopstart == 1: IOLoop.instance().start() def on_mdp_request(self, msg): """ Handle Requests over MQ @param msg : MQ req message """ self.log.debug(u"MQ Request received : {0}" . format(str(msg))) ### stop the plugin if msg.get_action() == "plugin.stop.do": self.log.info(u"Plugin stop request : {0}".format(msg)) self._mdp_reply_plugin_stop(msg) elif msg.get_action() == "helper.list.get": self.log.info(u"Plugin helper list request : {0}".format(msg)) self._mdp_reply_helper_list(msg) elif msg.get_action() == "helper.help.get": self.log.info(u"Plugin helper help request : {0}".format(msg)) self._mdp_reply_helper_help(msg) elif msg.get_action() == "helper.do": self.log.info(u"Plugin helper action request : {0}".format(msg)) self._mdp_reply_helper_do(msg) elif msg.get_action() == "device.new.get": self.log.info(u"Plugin new devices request : {0}".format(msg)) self._mdp_reply_device_new_get(msg) def _mdp_reply_helper_do(self, msg): contens = msg.get_data() if 'command' in contens.keys(): if contens['command'] in self.helpers.keys(): if 'parameters' not in contens.keys(): contens['parameters'] = {} params = [] else: params = [] for key, value in contens['parameters'].items(): params.append( "{0}='{1}'".format(key, value) ) command = "self.{0}(".format(self.helpers[contens['command']]['call']) command += ", ".join(params) command += ")" result = eval(command) # run the command with all params msg = MQMessage() msg.set_action('helper.do.result') msg.add_data('command', contens['command']) msg.add_data('parameters', contens['parameters']) msg.add_data('result', result) self.reply(msg.get()) def _mdp_reply_helper_help(self, data): content = data.get_data() if 'command' in contens.keys(): if content['command'] in self.helpers.keys(): msg = MQMessage() msg.set_action('helper.help.result') msg.add_data('help', self.helpers[content['command']]['help']) self.reply(msg.get()) def _mdp_reply_plugin_stop(self, data): """ Stop the plugin @param data : MQ req message First, send the MQ Rep to 'ack' the request Then, change the plugin status to STATUS_STOP_REQUEST Then, quit the plugin by calling force_leave(). This should make the plugin send a STATUS_STOPPED if all is ok Notice that no check is done on the MQ req content : we need nothing in it as it is directly addressed to a plugin """ # check if the message is for us content = data.get_data() if content['name'] != self._name or content['host'] != self.get_sanitized_hostname(): return ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('plugin.stop.result') status = True reason = "" msg.add_data('status', status) msg.add_data('reason', reason) self.reply(msg.get()) ### Change the plugin status self._set_status(STATUS_STOP_REQUEST) ### Try to stop the plugin # if it fails, the manager should try to kill the plugin self.force_leave() def _mdp_reply_helper_list(self, data): """ Return a list of supported helpers @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('helper.list.result') msg.add_data('actions', self.helpers.keys()) self.reply(msg.get()) def _mdp_reply_device_new_get(self, data): """ Return a list of new devices detected @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('device.new.result') msg.add_data('devices', self.new_devices) self.reply(msg.get()) def _set_status(self, status): """ Set the plugin status and send it """ # when ctrl-c is done, there is no more self._name at this point... # why ? because the force_leave method is called twice as show in the logs : # # ^CKeyBoardInterrupt # 2013-12-20 22:48:41,040 domogik-manager INFO Keyoard Interrupt detected, leave now. # Traceback (most recent call last): # File "./manager.py", line 1176, in <module> # main() # File "./manager.py", line 1173, in main # 2013-12-20 22:48:41,041 domogik-manager DEBUG force_leave called # 2013-12-20 22:48:41,044 domogik-manager DEBUG __del__ Single xpl plugin # 2013-12-20 22:48:41,045 domogik-manager DEBUG force_leave called if hasattr(self, '_name'): if self._name not in CORE_COMPONENTS: self._status = status self._send_status() def _send_status_loop(self): """ send the status each STATUS_HBEAT seconds """ # TODO : we could optimize by resetting the timer each time the status is sent # but as this is used only to check for dead plugins by the manager, it is not very important ;) while not self._stop.isSet(): self._send_status() self._stop.wait(STATUS_HBEAT) def _send_status(self): """ Send the plugin status over the MQ """ if hasattr(self, "_pub"): if self._name in CORE_COMPONENTS: #type = "core" return else: type = "plugin" self.log.debug("Send plugin status : {0}".format(self._status)) self._pub.send_event('plugin.status', {"type" : type, "name" : self._name, "host" : self.get_sanitized_hostname(), "event" : self._status}) def get_config_files(self): """ Return list of config files """ return self._config_files def get_products_directory(self): """ getter """ return self.products_directory def get_libraries_directory(self): """ getter """ return self.libraries_directory def get_packages_directory(self): """ getter """ return self.packages_directory def get_resources_directory(self): """ getter """ return self.resources_directory def get_data_files_directory(self): """ Return the directory where a plugin developper can store data files. If the directory doesn't exist, try to create it. After that, try to create a file inside it. If something goes wrong, generate an explicit exception. """ path = "{0}/{1}/{2}_{3}/data/" % (self.librairies_directory, PACKAGES_DIR, "plugin", self._name) if os.path.exists(path): if not os.access(path, os.W_OK & os.X_OK): raise OSError("Can't write in directory %s" % path) else: try: os.mkdir(path, '0770') self.log.info(u"Create directory %s." % path) except: raise OSError("Can't create directory %s." % path) try: tmp_prefix = "write_test"; count = 0 filename = os.path.join(path, tmp_prefix) while(os.path.exists(filename)): filename = "{}.{}".format(os.path.join(path, tmp_prefix),count) count = count + 1 f = open(filename,"w") f.close() os.remove(filename) except : raise IOError("Can't create a file in directory %s." % path) return path def register_helper(self, action, help_string, callback): if action not in self.helpers: self.helpers[action] = {'call': callback, 'help': help_string} def publish_helper(self, key, data): if hasattr(self, "_pub"): if self._name in CORE_COMPONENTS: type = "core" else: type = "plugin" self._pub.send_event('helper.publish', {"origin" : self._mq_name, "key": key, "data": data}) def _get_pid(self): """ Get current pid and write it to a file """ pid = os.getpid() pid_file = os.path.join(self._pid_dir_path, self._name + ".pid") self.log.debug(u"Write pid file for pid '%s' in file '%s'" % (str(pid), pid_file)) fil = open(pid_file, "w") fil.write(str(pid)) fil.close() def __del__(self): if hasattr(self, "log"): self.log.debug(u"__del__ Single plugin") # we guess that if no "log" is defined, the plugin has not really started, so there is no need to call force leave (and _stop, .... won't be created) self.force_leave() def force_leave(self, status = False, return_code = None): """ Leave threads & timers In the XplPLugin class, this function will be completed to also activate the xpl hbeat """ if return_code != None: self.set_return_code(return_code) self.log.info("Return code set to {0} when calling force_leave()".format(return_code)) # avoid ready() to be launched self.dont_run_ready = True # stop IOLoop #try: # IOLoop.instance().start() #except: # pass if hasattr(self, "log"): self.log.debug(u"force_leave called") # send stopped status over the MQ if status: self._set_status(status) else: self._set_status(STATUS_STOPPED) # try to stop the thread try: self.get_stop().set() except AttributeError: pass if hasattr(self, "_timers"): for t in self._timers: if hasattr(self, "log"): self.log.debug(u"Try to stop timer %s" % t) t.stop() if hasattr(self, "log"): self.log.debug(u"Timer stopped %s" % t) if hasattr(self, "_stop_cb"): for cb in self._stop_cb: if hasattr(self, "log"): self.log.debug(u"Calling stop additionnal method : %s " % cb.__name__) cb() if hasattr(self, "_threads"): for t in self._threads: if hasattr(self, "log"): self.log.debug(u"Try to stop thread %s" % t) try: t.join() except RuntimeError: pass if hasattr(self, "log"): self.log.debug(u"Thread stopped %s" % t) #t._Thread__stop() #Finally, we try to delete all remaining threads for t in threading.enumerate(): if t != threading.current_thread() and t.__class__ != threading._MainThread: if hasattr(self, "log"): self.log.info(u"The thread %s was not registered, killing it" % t.name) t.join() if hasattr(self, "log"): self.log.info(u"Thread %s stopped." % t.name) if threading.activeCount() > 1: if hasattr(self, "log"): self.log.warn(u"There are more than 1 thread remaining : %s" % threading.enumerate())
class Clients(): """ The clients list client_id : for a domogik plugin : plugin-<name>.<hostname> for an external member : <vendor id>-<device id>.<instance> { client_id = { xpl_source : vendorid-deviceid.instance host : hostname or ip type : plugin, ... name : package name (onewire, ipx800, ...) package_id : [type]+[name] status : alive, stopped, dead, unknown configured : True/False (plugins) or None (other types) data : { .... } }, ... = {} } The data part is related to the type WARNING : the 'primary key' is the client_id as you may have several clients with the same {type,name} So, all updates will be done on a client_id """ def __init__(self, stop): """ prepare an empty package list """ ### init vars self._stop = stop self._clients = {} self._clients_with_details = {} ### init logger log = logger.Logger('manager') self.log = log.get_logger('manager') self.log.info(u"Clients initialisation") self._pub = MQPub(zmq.Context(), 'manager') ### Check for dead clients thr_check_dead_clients = Thread(None, self._check_dead_clients, "check_dead_clients", (), {}) thr_check_dead_clients.start() def _check_dead_clients(self): """ Check if some clients are dead If the last time a client n a alive state has been seen is greater than twice STATUS_HBEAT seconds, set the client as dead """ while not self._stop.isSet(): now = time.time() for a_client in self._clients: # check if the client is dead only when the client is alive (or partially alive) if self._clients[a_client]['status'] in (STATUS_STARTING, STATUS_ALIVE, STATUS_STOP_REQUEST): delta = now - self._clients[a_client]['last_seen'] if delta > 2*STATUS_HBEAT: # client is dead! self.set_status(a_client, STATUS_DEAD) self._stop.wait(STATUS_HBEAT) #def add(self, host, type, name, client_id, xpl_source, data): def add(self, host, type, name, client_id, xpl_source, data, configured = None): """ Add a client to the list of clients @param host : client hostname or ip or dns @param type : client type @param name : client name @param client_id : client id @param data : client data : only for clients details @param configured : True/False : for a plugin : True if the plugin is configured, else False None : for type != 'plugin' """ self.log.info(u"Add new client : host={0}, type={1}, name={2}, client_id={3}, data={4}".format(host, type, name, client_id, str(data))) client = { "host" : host, "type" : type, "name" : name, "xpl_source" : xpl_source, "package_id" : "{0}-{1}".format(type, name), "pid" : 0, "last_seen" : time.time(), "status" : STATUS_UNKNOWN, "configured" : configured} client_with_details = { "host" : host, "type" : type, "name" : name, "xpl_source" : xpl_source, "package_id" : "{0}-{1}".format(type, name), "pid" : 0, "last_seen" : time.time(), "status" : STATUS_UNKNOWN, "configured" : configured, "data" : data} self._clients[client_id] = client self._clients_with_details[client_id] = client_with_details self.publish_update() def set_configured(self, client_id, new_status): """ Set a new status to a client """ # the first time this function is called, the client is not already registered (on client startup) # so we need to handle this case to avoid a KeyError exception if client_id not in self._clients: return old_status = self._clients[client_id]['configured'] if old_status == new_status: return self._clients[client_id]['configured'] = new_status self.log.info("The client 'configured' flag is now set to : {0}".format(new_status)) self.publish_update() def set_status(self, client_id, new_status): """ Set a new status to a client """ self.log.debug(u"Try to set a new status : {0} => {1}".format(client_id, new_status)) if new_status not in (STATUS_UNKNOWN, STATUS_STARTING, STATUS_ALIVE, STATUS_STOPPED, STATUS_DEAD, STATUS_INVALID, STATUS_STOP_REQUEST, STATUS_NOT_CONFIGURED): self.log.error(u"Invalid status : {0}".format(new_status)) return old_status = self._clients[client_id]['status'] # in all cases, set the 'last seen' time for the clients which are not dead if new_status == STATUS_DEAD: self._clients[client_id]['last_seen'] = time.time() if old_status == new_status: self.log.debug(u"The status was already {0} : nothing to do".format(old_status)) return self._clients[client_id]['status'] = new_status self._clients_with_details[client_id]['status'] = new_status self.log.info(u"Status set : {0} => {1}".format(client_id, new_status)) self.publish_update() def set_pid(self, client_id, pid): """ Set a pid to a client """ self.log.debug(u"Try to set the pid : {0} => {1}".format(client_id, pid)) self._clients[client_id]['pid'] = pid self._clients_with_details[client_id]['pid'] = pid self.log.info(u"Pid set : {0} => {1}".format(client_id, pid)) self.publish_update() def get_list(self): """ Return the clients list """ return self._clients def get_detail(self): """ Return the clients details """ return self._clients_with_details def publish_update(self): """ Publish the clients list update over the MQ """ # MQ publisher self._pub.send_event('client.list', self._clients) self._pub.send_event('client.detail', self._clients_with_details)
def __init__(self, name, stop_cb=None, is_manager=False, parser=None, daemonize=True, test=False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info( u"The plugin is starting in TEST mode. Test option is {0}". format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format( self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format( self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format( self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format( self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error( u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part")
class Plugin(BasePlugin, MQRep): ''' Global plugin class, manage signal handlers. This class shouldn't be used as-it but should be extended by no xPL plugin or by the class xPL plugin which will be used by the xPL plugins This class is a Singleton ''' def __init__(self, name, stop_cb=None, is_manager=False, parser=None, daemonize=True, test=False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info( u"The plugin is starting in TEST mode. Test option is {0}". format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format( self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format( self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format( self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format( self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error( u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part") def check_configured(self): """ For a plugin only To be call in the plugin __init__() Check in database (over queryconfig) if the key 'configured' is set to True for the plugin if not, stop the plugin and log this """ self._config = Query(self.zmq, self.log) configured = self._config.query(self._name, 'configured') if configured == '1': configured = True if configured != True: self.log.error( u"The plugin is not configured (configured = '{0}'. Stopping the plugin..." .format(configured)) self.force_leave(status=STATUS_NOT_CONFIGURED) return False self.log.info( u"The plugin is configured. Continuing (hoping that the user applied the appropriate configuration ;)" ) return True def _load_json(self): """ Load the plugin json file """ try: self.log.info(u"Read the json file and validate id".format( self._name)) pkg_json = PackageJson(pkg_type="plugin", name=self._name) # check if json is valid if pkg_json.validate() == False: # TODO : how to get the reason ? self.log.error(u"Invalid json file") self.force_leave(status=STATUS_INVALID) else: # if valid, store the data so that it can be used later self.log.info(u"The json file is valid") self.json_data = pkg_json.get_json() except: self.log.error( u"Error while trying to read the json file : {1}".format( self._name, traceback.format_exc())) self.force_leave(status=STATUS_INVALID) def get_config(self, key): """ Try to get the config over the MQ. If value is None, get the default value """ value = self._config.query(self._name, key) if value == None or value == 'None': self.log.info( u"Value for '{0}' is None or 'None' : trying to get the default value instead..." .format(key)) value = self.get_config_default_value(key) self.log.info(u"Value for '{0}' is : {1}".format(key, value)) return self.cast_config_value(key, value) def get_config_default_value(self, key): """ Get the default value for a config key from the json file @param key : configuration key """ for idx in range(len(self.json_data['configuration'])): if self.json_data['configuration'][idx]['key'] == key: default = self.json_data['configuration'][idx]['default'] self.log.info( u"Default value required for key '{0}' = {1}".format( key, default)) return default def cast_config_value(self, key, value): """ Cast the config value as the given type in the json file @param key : configuration key @param value : configuration value to cast and return @return : the casted value """ for idx in range(len(self.json_data['configuration'])): if self.json_data['configuration'][idx]['key'] == key: type = self.json_data['configuration'][idx]['default'] self.log.info( u"Casting value for key '{0}' in type '{1}'...".format( key, type)) return self.cast(value, type) # no cast operation : return the value if value == "None": return None return value def cast(self, value, type): """ Cast a value for a type @param value : value to cast @param type : type in which you want to cast the value """ try: if type == "boolean": # just in case, the "True"/"False" are not already converted in True/False # this is (currently) done on queryconfig side if value == "True": return True elif value == "False": return False # type == choice : nothing to do if type == "date": self.log.error( u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it" ) if type == "datetime": self.log.error( u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it" ) # type == email : nothing to do if type == "float": return float(value) if type == "integer": return float(value) # type == ipv4 : nothing to do # type == multiple choice : nothing to do # type == string : nothing to do if type == "time": self.log.error( u"TODO : the cast in date format is not yet developped. Please request fritz_smh to do it" ) # type == url : nothing to do except: # if an error occurs : return the default value and log a warning self.log.warning( u"Error while casting value '{0}' to type '{1}'. The plugin may not work!! Error : {2}" .format(value, type, traceback.format_exc())) return value return value def get_device_list(self, quit_if_no_device=False): """ Request the dbmgr component over MQ to get the devices list for this client @param quit_if_no_device: if True, exit the plugin if there is no devices """ self.log.info(u"Retrieve the devices list for this client...") mq_client = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('device.get') msg.add_data('type', 'plugin') msg.add_data('name', self._name) msg.add_data('host', self.get_sanitized_hostname()) result = mq_client.request('dbmgr', msg.get(), timeout=10) if not result: self.log.error(u"Unable to retrieve the device list") self.force_leave() return [] else: device_list = result.get_data()['devices'] if device_list == []: self.log.warn(u"There is no device created for this client") if quit_if_no_device: self.log.warn( u"The developper requested to stop the client if there is no device created" ) self.force_leave() return [] for a_device in device_list: self.log.info(u"- id : {0} / name : {1} / device type id : {2}".format(a_device['id'], \ a_device['name'], \ a_device['device_type_id'])) # log some informations about the device # notice that even if we are not in the XplPlugin class we will display xpl related informations : # for some no xpl plugins, there will just be nothing to display. # first : the stats self.log.info(u" xpl_stats features :") for a_xpl_stat in a_device['xpl_stats']: self.log.info(u" - {0}".format(a_xpl_stat)) self.log.info(u" Static Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat][ 'parameters']['static']: self.log.info(u" - {0} = {1}".format( a_feature['key'], a_feature['value'])) self.log.info(u" Dynamic Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat][ 'parameters']['dynamic']: self.log.info(u" - {0}".format(a_feature['key'])) # then, the commands self.log.info(u" xpl_commands features :") for a_xpl_cmd in a_device['xpl_commands']: self.log.info(u" - {0}".format(a_xpl_cmd)) self.log.info(u" + Parameters :") for a_feature in a_device['xpl_commands'][a_xpl_cmd][ 'parameters']: self.log.info(u" - {0} = {1}".format( a_feature['key'], a_feature['value'])) self.devices = device_list return device_list def device_detected(self, device_type, type, feature, data): """ The plugin developpers can call this function when a device is detected This function will check if a corresponding device exists and : - if so, do nothing - if not, add the device in a 'new devices' list - if the device is already in the 'new devices list', does nothing - if not : add it into the list and send a MQ message : an event for the UI to say a new device is detected ### TODO : implement a req/rep MQ message to allow UI to get the new devices list @param device_type : device_type of the detected device @param data : data about the device (address or any other configuration element of a device for this plugin) @param type : xpl_stats, xpl_commands @param feature : a xpl_stat or xpl_command feature """ self.log.debug( u"Device detected : device_type = {0}, data = {1}".format( device_type, data)) #self.log.debug(u"Already existing devices : {0}".format(self.devices)) # browse all devices to find if the device exists found = False for a_device in self.devices: # first, search for device type if a_device['device_type_id'] == device_type: params = a_device[type][feature]['parameters']['static'] found = True for key in data: for a_param in params: if key == a_param[ 'key'] and data[key] != a_param['value']: found = False break if found: break if found: self.log.debug(u"The device already exists : id={0}.".format( a_device['id'])) else: self.log.debug(u"The device doesn't exists in database") # add the device feature in the new devices list : self.new_devices[device_type][type][feature] = data self.log.debug( u"Check if the device has already be marked as new...") found = False for a_device in self.new_devices: if a_device['device_type_id'] == device_type and \ a_device['type'] == type and \ a_device['feature'] == feature: if data == a_device['data']: found = True if found == False: new_device = { 'device_type_id': device_type, 'type': type, 'feature': feature, 'data': data } self.log.info( u"New device feature detected and added in the new devices list : {0}" .format(new_device)) self.new_devices.append(new_device) # publish new devices update self._pub.send_event( 'device.new', { "type": "plugin", "name": self._name, "host": self.get_sanitized_hostname(), "client_id": "plugin-{0}.{1}".format(self._name, self.get_sanitized_hostname()), "devices": self.new_devices }) # TODO : later (0.4.0+), publish one "new device" notification with only the new device detected else: self.log.debug( u"The device has already been detected since the plugin startup" ) def get_parameter(self, a_device, key): """ For a device feature, return the required parameter value @param a_device: the device informations @param key: the parameter key """ try: self.log.debug(u"Get parameter '{0}'".format(key)) for a_param in a_device['parameters']: if a_param == key: value = self.cast(a_device['parameters'][a_param]['value'], a_device['parameters'][a_param]['type']) self.log.debug(u"Parameter value found: {0}".format(value)) return value self.log.warning(u"Parameter not found : return None") return None except: self.log.error( u"Error while looking for a device parameter. Return None. Error: {0}" .format(traceback.format_exc())) return None def get_parameter_for_feature(self, a_device, type, feature, key): """ For a device feature, return the required parameter value @param a_device: the device informations @param type: the parameter type (xpl_stats, ...) @param feature: the parameter feature @param key: the parameter key """ try: self.log.debug( u"Get parameter '{0}' for '{1}', feature '{2}'".format( key, type, feature)) for a_param in a_device[type][feature]['parameters']['static']: if a_param['key'] == key: value = self.cast(a_param['value'], a_param['type']) self.log.debug(u"Parameter value found: {0}".format(value)) return value self.log.warning(u"Parameter not found : return None") return None except: self.log.error( u"Error while looking for a device feature parameter. Return None. Error: {0}" .format(traceback.format_exc())) return None def check_for_pictures(self): """ if some products are defined, check if the corresponding pictures are present in the products/ folder """ self.log.info(u"Check if there are pictures for the defined products") ok = True ok_product = None if self.json_data.has_key('products'): for product in self.json_data['products']: ok_product = False for ext in PRODUCTS_PICTURES_EXTENSIONS: file = "{0}.{1}".format(product['id'], ext) if os.path.isfile("{0}/{1}".format( self.get_products_directory(), file)): ok_product = True break if ok_product: self.log.debug(u"- OK : {0} ({1})".format( product['name'], file)) else: ok = False self.log.warning(u"- Missing : {0} ({1}.{2})".format( product['name'], product['id'], PRODUCTS_PICTURES_EXTENSIONS)) if ok == False: self.log.warning(u"Some pictures are missing!") else: if ok_product == None: self.log.info(u"There is no products defined for this plugin") def ready(self, ioloopstart=1): """ to call at the end of the __init__ of classes that inherits of this one In the XplPLugin class, this function will be completed to also activate the xpl hbeat """ if self.dont_run_ready == True: return ### send plugin status : STATUS_ALIVE # TODO : why the dbmgr has no self._name defined ??????? # temporary set as unknown to avoir blocking bugs if not hasattr(self, '_name'): self._name = "unknown" self._set_status(STATUS_ALIVE) ### Instantiate the MQ # nothing can be launched after this line (blocking call!!!!) self.log.info( u"Start IOLoop for MQ : nothing else can be executed in the __init__ after this! Make sure that the self.ready() call is the last line of your init!!!!" ) if ioloopstart == 1: IOLoop.instance().start() def on_mdp_request(self, msg): """ Handle Requests over MQ @param msg : MQ req message """ self.log.debug(u"MQ Request received : {0}".format(str(msg))) ### stop the plugin if msg.get_action() == "plugin.stop.do": self.log.info(u"Plugin stop request : {0}".format(msg)) self._mdp_reply_plugin_stop(msg) elif msg.get_action() == "helper.list.get": self.log.info(u"Plugin helper list request : {0}".format(msg)) self._mdp_reply_helper_list(msg) elif msg.get_action() == "helper.help.get": self.log.info(u"Plugin helper help request : {0}".format(msg)) self._mdp_reply_helper_help(msg) elif msg.get_action() == "helper.do": self.log.info(u"Plugin helper action request : {0}".format(msg)) self._mdp_reply_helper_do(msg) elif msg.get_action() == "device.new.get": self.log.info(u"Plugin new devices request : {0}".format(msg)) self._mdp_reply_device_new_get(msg) def _mdp_reply_helper_do(self, msg): contens = msg.get_data() if 'command' in contens.keys(): if contens['command'] in self.helpers.keys(): if 'parameters' not in contens.keys(): contens['parameters'] = {} params = [] else: params = [] for key, value in contens['parameters'].items(): params.append("{0}='{1}'".format(key, value)) command = "self.{0}(".format( self.helpers[contens['command']]['call']) command += ", ".join(params) command += ")" result = eval(command) # run the command with all params msg = MQMessage() msg.set_action('helper.do.result') msg.add_data('command', contens['command']) msg.add_data('parameters', contens['parameters']) msg.add_data('result', result) self.reply(msg.get()) def _mdp_reply_helper_help(self, data): content = data.get_data() if 'command' in contens.keys(): if content['command'] in self.helpers.keys(): msg = MQMessage() msg.set_action('helper.help.result') msg.add_data('help', self.helpers[content['command']]['help']) self.reply(msg.get()) def _mdp_reply_plugin_stop(self, data): """ Stop the plugin @param data : MQ req message First, send the MQ Rep to 'ack' the request Then, change the plugin status to STATUS_STOP_REQUEST Then, quit the plugin by calling force_leave(). This should make the plugin send a STATUS_STOPPED if all is ok Notice that no check is done on the MQ req content : we need nothing in it as it is directly addressed to a plugin """ # check if the message is for us content = data.get_data() if content['name'] != self._name or content[ 'host'] != self.get_sanitized_hostname(): return ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('plugin.stop.result') status = True reason = "" msg.add_data('status', status) msg.add_data('reason', reason) self.reply(msg.get()) ### Change the plugin status self._set_status(STATUS_STOP_REQUEST) ### Try to stop the plugin # if it fails, the manager should try to kill the plugin self.force_leave() def _mdp_reply_helper_list(self, data): """ Return a list of supported helpers @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('helper.list.result') msg.add_data('actions', self.helpers.keys()) self.reply(msg.get()) def _mdp_reply_device_new_get(self, data): """ Return a list of new devices detected @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('device.new.result') msg.add_data('devices', self.new_devices) self.reply(msg.get()) def _set_status(self, status): """ Set the plugin status and send it """ # when ctrl-c is done, there is no more self._name at this point... # why ? because the force_leave method is called twice as show in the logs : # # ^CKeyBoardInterrupt # 2013-12-20 22:48:41,040 domogik-manager INFO Keyoard Interrupt detected, leave now. # Traceback (most recent call last): # File "./manager.py", line 1176, in <module> # main() # File "./manager.py", line 1173, in main # 2013-12-20 22:48:41,041 domogik-manager DEBUG force_leave called # 2013-12-20 22:48:41,044 domogik-manager DEBUG __del__ Single xpl plugin # 2013-12-20 22:48:41,045 domogik-manager DEBUG force_leave called if hasattr(self, '_name'): if self._name not in CORE_COMPONENTS: self._status = status self._send_status() def _send_status_loop(self): """ send the status each STATUS_HBEAT seconds """ # TODO : we could optimize by resetting the timer each time the status is sent # but as this is used only to check for dead plugins by the manager, it is not very important ;) while not self._stop.isSet(): self._send_status() self._stop.wait(STATUS_HBEAT) def _send_status(self): """ Send the plugin status over the MQ """ if hasattr(self, "_pub"): if self._name in CORE_COMPONENTS: #type = "core" return else: type = "plugin" self.log.debug("Send plugin status : {0}".format(self._status)) self._pub.send_event( 'plugin.status', { "type": type, "name": self._name, "host": self.get_sanitized_hostname(), "event": self._status }) def get_config_files(self): """ Return list of config files """ return self._config_files def get_products_directory(self): """ getter """ return self.products_directory def get_libraries_directory(self): """ getter """ return self.libraries_directory def get_packages_directory(self): """ getter """ return self.packages_directory def get_resources_directory(self): """ getter """ return self.resources_directory def get_data_files_directory(self): """ Return the directory where a plugin developper can store data files. If the directory doesn't exist, try to create it. After that, try to create a file inside it. If something goes wrong, generate an explicit exception. """ path = "{0}/{1}/{2}_{3}/data/" % (self.librairies_directory, PACKAGES_DIR, "plugin", self._name) if os.path.exists(path): if not os.access(path, os.W_OK & os.X_OK): raise OSError("Can't write in directory %s" % path) else: try: os.mkdir(path, '0770') self.log.info(u"Create directory %s." % path) except: raise OSError("Can't create directory %s." % path) try: tmp_prefix = "write_test" count = 0 filename = os.path.join(path, tmp_prefix) while (os.path.exists(filename)): filename = "{}.{}".format(os.path.join(path, tmp_prefix), count) count = count + 1 f = open(filename, "w") f.close() os.remove(filename) except: raise IOError("Can't create a file in directory %s." % path) return path def register_helper(self, action, help_string, callback): if action not in self.helpers: self.helpers[action] = {'call': callback, 'help': help_string} def publish_helper(self, key, data): if hasattr(self, "_pub"): if self._name in CORE_COMPONENTS: type = "core" else: type = "plugin" self._pub.send_event('helper.publish', { "origin": self._mq_name, "key": key, "data": data }) def _get_pid(self): """ Get current pid and write it to a file """ pid = os.getpid() pid_file = os.path.join(self._pid_dir_path, self._name + ".pid") self.log.debug(u"Write pid file for pid '%s' in file '%s'" % (str(pid), pid_file)) fil = open(pid_file, "w") fil.write(str(pid)) fil.close() def __del__(self): if hasattr(self, "log"): self.log.debug(u"__del__ Single plugin") # we guess that if no "log" is defined, the plugin has not really started, so there is no need to call force leave (and _stop, .... won't be created) self.force_leave() def force_leave(self, status=False, return_code=None): """ Leave threads & timers In the XplPLugin class, this function will be completed to also activate the xpl hbeat """ if return_code != None: self.set_return_code(return_code) self.log.info( "Return code set to {0} when calling force_leave()".format( return_code)) # avoid ready() to be launched self.dont_run_ready = True # stop IOLoop #try: # IOLoop.instance().start() #except: # pass if hasattr(self, "log"): self.log.debug(u"force_leave called") # send stopped status over the MQ if status: self._set_status(status) else: self._set_status(STATUS_STOPPED) # try to stop the thread try: self.get_stop().set() except AttributeError: pass if hasattr(self, "_timers"): for t in self._timers: if hasattr(self, "log"): self.log.debug(u"Try to stop timer %s" % t) t.stop() if hasattr(self, "log"): self.log.debug(u"Timer stopped %s" % t) if hasattr(self, "_stop_cb"): for cb in self._stop_cb: if hasattr(self, "log"): self.log.debug(u"Calling stop additionnal method : %s " % cb.__name__) cb() if hasattr(self, "_threads"): for t in self._threads: if hasattr(self, "log"): self.log.debug(u"Try to stop thread %s" % t) try: t.join() except RuntimeError: pass if hasattr(self, "log"): self.log.debug(u"Thread stopped %s" % t) #t._Thread__stop() #Finally, we try to delete all remaining threads for t in threading.enumerate(): if t != threading.current_thread( ) and t.__class__ != threading._MainThread: if hasattr(self, "log"): self.log.info( u"The thread %s was not registered, killing it" % t.name) t.join() if hasattr(self, "log"): self.log.info(u"Thread %s stopped." % t.name) if threading.activeCount() > 1: if hasattr(self, "log"): self.log.warn(u"There are more than 1 thread remaining : %s" % threading.enumerate())