Пример #1
0
def client_devices_edit(client_id, did):
    with app.db.session_scope():
        device = app.db.get_device_sql(did)
        MyForm = model_form(Device, \
                        base_class=Form, \
                        db_session=app.db.get_session(),
                        exclude=['params', 'commands', 'sensors', 'address', 'xpl_commands', 'xpl_stats', 'device_type_id', 'client_id', 'client_version'])
        form = MyForm(request.form, device)

        if request.method == 'POST' and form.validate():
            # save it
            app.db.update_device(did, \
           d_name=request.form['name'], \
           d_description=request.form['description'], \
           d_reference=request.form['reference'])
            # message the suer
            flash(gettext("Device saved"), 'success')
            # reload stats
            req = MQSyncReq(app.zmq_context)
            msg = MQMessage()
            msg.set_action('reload')
            resp = req.request('xplgw', msg.get(), 100)
            # redirect
            return redirect("/client/{0}/devices/known".format(client_id))
        else:
            return render_template('client_device_edit.html',
                                   form=form,
                                   clientid=client_id,
                                   mactve="clients",
                                   active='devices')
Пример #2
0
    def test_XplCmd(self, parameters = {}, statParams = None):
        """Send an Xpl cmd with statics parameters by request, if necessary start testcase listener for ack message and 
             check if insert in database is ok.
        """
        if self.get_return_confirmation() :
            schema,  data,  statResult = self.get_XplStat_fromAck(statParams)
            th = threading.Thread(None, self.assert_Xpl_Stat_Ack_Wait, "th_test_0110_xpl-ack_from_{0}".format(self.command_name), (schema,  data, statResult))
            th.start()
            time.sleep(1)
        else : 
            print (u"No ack required for {0}".format(self.command_name))
        if self._device and self.command_id :
            cli = MQSyncReq(zmq.Context())
            msg = MQMessage()
            msg.set_action('cmd.send')
            msg.add_data('cmdid', self.command_id)
            msg.add_data('cmdparams', parameters)
            print (u"Send xpl_cmnd {0}".format(self.command_name))
            cli.request('xplgw', msg.get(), timeout=10)
#            if self.get_return_confirmation() :
#                self.assert_get_last_command_in_db(statResult)
#            else : 
#                print (u"No ack required for {0}".format(self.command_name))
            return True
        else : return False
Пример #3
0
    def query(self, plugin, key, element='', nb_test=QUERY_CONFIG_NUM_TRY):
        '''
        Ask the config system for the value. Calling this function will make
        your program wait until it got an answer

        @param plugin : the plugin of the item requesting the value,
        must exists in the config database
        @param element : the name of the element which requests config, None if
        it's a technolgy global parameter
        @param key : the key to fetch corresponding value, if it's an empty string,
        all the config items for this technology will be fetched
        '''
        msg = MQMessage()
        msg._action = 'config.get'
        msg._data = {
            'plugin': plugin,
            'key': key,
            'element': element,
            'hostname': get_sanitized_hostname()
        }
        ret = self.cli.request('dbmgr', msg.get(), timeout=QUERY_CONFIG_WAIT)
        if ret is None:
            return None
        else:
            if 'value' in ret._data.keys():
                return ret._data['value']
            else:
                return None
Пример #4
0
 def request_stop(self):
     """ Request the plugin to stop
     """
     print(u"Request plugin to stop : '{0}' on '{1}'".format(
         self.name, self.host))
     cli = MQSyncReq(zmq.Context())
     msg = MQMessage()
     msg.set_action('plugin.stop.do')
     msg.add_data('name', self.name)
     msg.add_data('host', self.host)
     result = cli.request("plugin-{0}.{1}".format(self.name, self.host),
                          msg.get(),
                          timeout=30)
     if result:
         msgid, content = result.get()
         content = json.loads(content)
         print(u"Response : {0}".format(content))
         if content['status']:
             print(u"Plugin stopped")
             return True
         else:
             print(u"Error : plugin not stopped")
             return False
     else:
         raise RuntimeError(
             "MQ Timeout when requesting to stop the plugin (the plugin didn't stop itself)"
         )
Пример #5
0
    def _on_mpd_message(self, msg):
        """Helper method called on message receive.

        msg is a list w/ the message parts
        """
        # 1st part is empty
        msg.pop(0)
        # 2nd part is protocol version
        # TODO: version check
        proto = msg.pop(0)
        # 3rd part is message type
        msg_type = msg.pop(0)
        # XXX: hardcoded message types!
        # any message resets the liveness counter
        self.need_handshake = False
        self.curr_liveness = self.HB_LIVENESS
        if msg_type == b'\x05':  # disconnect
            self.curr_liveness = 0  # reconnect will be triggered by hb timer
        elif msg_type == b'\x02':  # request
            # remaining parts are the user message
            envelope, msg = split_address(msg)
            envelope.append(b'')
            envelope = [b'', self._proto_version, b'\x03'] + envelope  # REPLY
            self.envelope = envelope
            mes = MQMessage()
            mes.set(msg)
            self.on_mdp_request(mes)
        else:
            # invalid message
            # ignored
            pass
        return
Пример #6
0
 def request_startup(self):
     """ Request the plugin to start over the manager
     """
     print(
         u"Request plugin startup to the manager for '{0}' on '{1}'".format(
             self.name, self.host))
     cli = MQSyncReq(zmq.Context())
     msg = MQMessage()
     msg.set_action('plugin.start.do')
     msg.add_data('name', self.name)
     msg.add_data('host', self.host)
     result = cli.request('manager', msg.get(), timeout=10)
     if result:
         msgid, content = result.get()
         content = json.loads(content)
         print(u"Response from the manager : {0}".format(content))
         if content['status']:
             print(u"Plugin started")
             return True
         else:
             print(u"Error : plugin not started")
             return False
     else:
         raise RuntimeError(
             "MQ Timeout when requesting manager to start the plugin")
Пример #7
0
def client_devices_new(client_id):
    cli = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('client.detail.get')
    res = cli.request('manager', msg.get(), timeout=10)
    if res is not None:
        detaila = res.get_data()
        data = detaila[client_id]['data']
    else:
        data = {}
    if type(data["device_types"]) is not dict:
        dtypes = {}
    else:
        dtypes = list(data["device_types"].keys())
    products = {}
    if "products" in data:
        for prod in data["products"]:
            products[prod["name"]] = prod["type"]

    return render_template('client_device_new.html',
                           device_types=dtypes,
                           products=products,
                           clientid=client_id,
                           mactve="clients",
                           active='devices')
Пример #8
0
    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()
Пример #9
0
def configure(type, name, host, key, value):
    cli = MQSyncReq(zmq.Context())
    msg = MQMessage()
    msg.set_action('config.set')
    msg.add_data('type', type)
    msg.add_data('host', host)
    msg.add_data('name', name)
    msg.add_data('data', {key: value})
    result = cli.request('dbmgr', msg.get(), timeout=10)
    if result:
        data = result.get_data()
        if 'status' in data:
            if not data['status']:
                print(result.get())
                raise RuntimeError(
                    "DbMgr did not return status true on a config.set for {0}-{1}.{2} : {3} = {4}"
                    .format(type, name, host, key, value))
            else:
                return True
        else:
            print(result.get())
            raise RuntimeError(
                "DbMgr did not return a status on a config.set for {0}-{1}.{2} : {3} = {4}"
                .format(type, name, host, key, value))
    else:
        raise RuntimeError(
            "Error while setting configuration for {0}-{1}.{2} : {3} = {4}".
            format(type, name, host, key, value))
Пример #10
0
    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
Пример #11
0
 def reload_stats(self):
     self.log.debug(u"=============== reload stats")
     req = MQSyncReq(self.zmq)
     msg = MQMessage()
     msg.set_action( 'reload' )
     resp = req.request('xplgw', msg.get(), 100)
     self.log.debug(u"Reply from xplgw: {0}".format(resp))
     self.log.debug(u"=============== reload stats END")
Пример #12
0
 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())
Пример #13
0
    def _mdp_reply_config_set(self, data):
        """ Reply to config.set MQ req
            @param data : MQ req message
        """
        print "#################"
        msg = MQMessage()
        msg.set_action('config.result')
        status = True
        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Config set : missing 'type' field : {0}".format(data)

        if msg_data['type'] != "plugin":
            status = False
            reason = "Config set not available for type={0}".format(
                msg_data['type'])

        if 'name' not in msg_data:
            status = False
            reason = "Config set : missing 'name' field : {0}".format(data)

        if 'host' not in msg_data:
            status = False
            reason = "Config set : missing 'host' field : {0}".format(data)

        if 'data' not in msg_data:
            status = False
            reason = "Config set : missing 'data' field : {0}".format(data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            name = msg_data['name']
            host = msg_data['host']
            data = msg_data['data']
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            try:
                # we add a configured key set to true to tell the UIs and plugins that there are some configuration elements
                self._db.set_plugin_config(name, host, "configured", True)
                for key in msg_data['data']:
                    self._db.set_plugin_config(name, host, key, data[key])
                self.publish_config_updated(type, name, host)
            except:
                reason = "Error while setting configuration for '{0} {1} on {2}' : {3}".format(
                    type, name, host, traceback.format_exc())
                status = False
                self.log.error(reason)

        msg.add_data('status', status)
        msg.add_data('reason', reason)

        self.log.debug(msg.get())
        self.reply(msg.get())
Пример #14
0
 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())
Пример #15
0
 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())
Пример #16
0
def client_devices_delete(client_id, did):
    with app.db.session_scope():
        app.db.del_device(did)
    # reload stats
    req = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('reload')
    resp = req.request('xplgw', msg.get(), 100)
    return redirect("/client/{0}/devices/known".format(client_id))
Пример #17
0
    def __init__(self, server_interfaces, server_port):
        """ Initiate DbHelper, Logs and config
            Then, start HTTP server and give it initialized data
            @param server_interfaces :  interfaces of HTTP server
            @param server_port :  port of HTTP server
        """

        Plugin.__init__(self, name='admin')
        # logging initialization
        self.log.info(u"Admin Server initialisation...")
        self.log.debug(u"locale : %s %s" % locale.getdefaultlocale())

        try:
            try:
                cfg_rest = Loader('admin')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.interfaces = conf_rest['interfaces']
                self.port = conf_rest['port']
                # if rest_use_ssl = True, set here path for ssl certificate/key
                self.use_ssl = conf_rest['use_ssl']
                self.key_file = conf_rest['ssl_certificate']
                self.cert_file = conf_rest['ssl_key']
            except KeyError:
                # default parameters
                self.interfaces = server_interfaces
                self.port = server_port
                self.use_ssl = False
                self.key_file = ""
                self.cert_file = ""
                self.clean_json = False
            self.log.info(u"Configuration : interfaces:port = %s:%s" %
                          (self.interfaces, self.port))

            # get all datatypes
            cli = MQSyncReq(self.zmq)
            msg = MQMessage()
            msg.set_action('datatype.get')
            res = cli.request('manager', msg.get(), timeout=10)
            if res is not None:
                self.datatypes = res.get_data()['datatypes']
            else:
                self.datatypes = {}

# Launch server, stats
            self.log.info(u"Admin Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
            self.start_http()
            # calls the tornado.ioloop.instance().start()

            ### Component is ready
            self.ready(0)
            IOLoop.instance().start()
        except:
            self.log.error(u"%s" % self.get_exception())
Пример #18
0
def orphans_delete(did):
    with app.db.session_scope():
        app.db.del_device(did)
    # reload stats
    req = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('reload')
    resp = req.request('xplgw', msg.get(), 100)
    flash(gettext("Device deleted"), "success")
    return redirect("/orphans")
Пример #19
0
 def _mdp_reply(self, plugin, hostname, key, value, element=None):
     msg = MQMessage()
     msg.setaction('config.result')
     msg.add_data('plugin', plugin)
     msg.add_data('hostname', hostname)
     msg.add_data('key', key)
     msg.add_data('value', value)
     if element:
         msg.add_data('element', element)
     print msg.get()
     self.reply(msg.get())
Пример #20
0
    def set(self, plugin, key, value):
        '''
        Send a xpl message to set value for a param

        @param technology : the technology of the item
        @param key : the key to set corresponding value,
        @param value : the value to set
        '''
        msg = MQMessage()
        msg._action = 'config.set'
        self.cli.request('dbmgr', msg.get(), timeout=QUERY_CONFIG_WAIT)
Пример #21
0
 def _load_client_to_xpl_target(self):
     cli = MQSyncReq(self.zmq)
     msg = MQMessage()
     msg.set_action('client.list.get')
     response = cli.request('manager', msg.get(), timeout=10)
     if response:
         self._parse_xpl_target(response.get_data())
     else:
         self.log.error(
             u"Updating client list was not successfull, no response from manager"
         )
Пример #22
0
    def on_mdp_request(self, msg):
        # XplPlugin handles MQ Req/rep also
        XplPlugin.on_mdp_request(self, msg)

        if msg.get_action() == "reload":
            self.load()
            msg = MQMessage()
            msg.set_action('reload.result')
            self.reply(msg.get())
        elif msg.get_action() == "cmd.send":
            self._send_xpl_command(msg)
Пример #23
0
 def _load_conversions(self):
     print "============================ conversion"
     cli = MQSyncReq(self.zmq)
     msg = MQMessage()
     msg.set_action('client.conversion.get')
     response = cli.request('manager', msg.get(), timeout=10)
     if response:
         self._parse_conversions(response.get_data())
     else:
         self.log.error(
             u"Updating client conversion list was not successfull, no response from manager"
         )
Пример #24
0
    def _mdp_reply_config_delete(self, data):
        """ Reply to config.delete MQ req
            Delete all the config items for the given type, name and host
            @param data : MQ req message
        """
        msg = MQMessage()
        msg.set_action('config.result')
        status = True
        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Config request : missing 'type' field : {0}".format(data)

        if msg_data['type'] != "plugin":
            status = False
            reason = "Config request not available for type={0}".format(
                msg_data['type'])

        if 'name' not in msg_data:
            status = False
            reason = "Config request : missing 'name' field : {0}".format(data)

        if 'host' not in msg_data:
            status = False
            reason = "Config request : missing 'host' field : {0}".format(data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            name = msg_data['name']
            host = msg_data['host']
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            try:
                self._db.del_plugin_config(name, host)
                self.log.info(u"Delete config for {0} {1}".format(type, name))
                self.publish_config_updated(type, name, host)
            except:
                status = False
                reason = "Error while deleting configuration for '{0} {1} on {2} : {3}".format(
                    type, name, host, traceback.format_exc())
                self.log.error(reason)

        msg.add_data('reason', reason)
        msg.add_data('status', status)

        self.log.debug(msg.get())
        self.reply(msg.get())
Пример #25
0
def clients():
    cli = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('client.list.get')
    res = cli.request('manager', msg.get(), timeout=10)
    if res is not None:
        client_list = res.get_data()
    else:
        client_list = {}

    return render_template('clients.html',
                           mactve="clients",
                           overview_state="collapse",
                           clients=client_list)
Пример #26
0
 def do_action(self, condition, tests):
     cli = MQSyncReq(zmq.Context())
     msg = MQMessage()
     msg.set_action('cmd.send')
     msg.add_data('cmdid', self._params['cmdid'])
     msg.add_data('cmdparams', self._params['cmdparams'])
     # do the request
     res = cli.request('xplgw', msg.get(), timeout=10)
     if res:
         data = res.get_data()
         if not data['status']:
             self._log.error(
                 "Command sending to XPL gw failed: {0}".format(res))
     else:
         self._log.error("XPL gw did not respond")
Пример #27
0
def client_devices_detected(client_id):
    cli = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('device.new.get')
    res = cli.request(str(client_id), msg.get(), timeout=10)
    if res is not None:
        data = res.get_data()
        devices = data['devices']
    else:
        devices = {}
    return render_template('client_detected.html',
                           devices=devices,
                           clientid=client_id,
                           mactve="clients",
                           active='devices')
Пример #28
0
    def request(self, service, msg, timeout=None):
        """Send the given message.

        :param msg:     message parts to send.
        :type msg:      list of str
        :param timeout: time to wait in milliseconds.
        :type timeout:  int
        
        :rtype : message parts
        """
	ret = self.rawrequest( service, msg, timeout)
	msg = None
	if ret:
	    msg = MQMessage()
            msg.set(ret)
        return msg
Пример #29
0
    def _mdp_reply_devices_result(self, data):
        """ Reply to device.get MQ req
            @param data : MQ req message
        """
        msg = MQMessage()
        msg.set_action('device.result')
        status = True

        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Devices request : missing 'type' field : {0}".format(
                data)

        if 'name' not in msg_data:
            status = False
            reason = "Devices request : missing 'name' field : {0}".format(
                data)

        if 'host' not in msg_data:
            status = False
            reason = "Devices request : missing 'host' field : {0}".format(
                data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            #if type == "plugin":
            #    type = DMG_VENDOR_ID
            name = msg_data['name']
            host = msg_data['host']
            dev_list = self._db.list_devices_by_plugin("{0}-{1}.{2}".format(
                type, name, host))
            #dev_json = json.dumps(dev_list, cls=domogik_encoder(), check_circular=False),
            dev_json = dev_list
            print(dev_json)
            msg.add_data('status', status)
            msg.add_data('reason', reason)
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            msg.add_data('devices', dev_json)

        self.reply(msg.get())
Пример #30
0
def client_detail(client_id):
    cli = MQSyncReq(app.zmq_context)
    msg = MQMessage()
    msg.set_action('client.detail.get')
    res = cli.request('manager', msg.get(), timeout=10)
    if res is not None:
        detaila = res.get_data()
        detail = detaila[client_id]
    else:
        detail = {}

    return render_template('client.html',
                           loop={'index': 1},
                           clientid=client_id,
                           data=detail,
                           mactve="clients",
                           active='home')