Ejemplo n.º 1
0
class _KerviModuleLoader(process._KerviProcess):
    """ Private class that starts a separate process that loads a module in the Kervi application """
    def init_process(self, **kwargs):
        self.spine.log.verbose("load: %s", self.name)

        try:
            import kervi.hal as hal
            hal._load()
            __import__(self.name, fromlist=[''])

        except ImportError:
            self.spine.log.exception("module not found:{0}", self.name)
        except:
            self.spine.log.exception("error load module:{0}", self.name)

        self.spine.send_command("startThreads", local_only=True)
        self.spine.trigger_event("moduleLoaded", self.name)

    def terminate_process(self):
        pass

    def load_spine(self, process_id, spine_port, root_address=None, ip=None):
        from kervi.plugin.message_bus.bus_manager import BusManager
        self._bus_manager = BusManager()
        self._bus_manager.load(process_id, spine_port, root_address, ip)
        return self._bus_manager.bus
Ejemplo n.º 2
0
class _KerviPluginProcess(process._KerviProcess):
    """ Private class that starts a separate process for plugins """
    def init_process(self, **kwargs):
        self.spine.log.debug("init process login: %s", self.name)

        plugin_module = kwargs.pop("plugin_module", None)
        plugin_config = kwargs.pop("plugin_config", {})
        self._load_silent = kwargs.pop("load_silent", False)
        self.plugin = _PluginInfo(plugin_module,
                                  plugin_config,
                                  None,
                                  None,
                                  None,
                                  load_silent=self._load_silent)
        self.plugin.load()
        self.spine.trigger_event(
            "moduleLoaded",
            self.name,
        )

    def load_spine(self, process_id, spine_port, root_address=None, ip=None):
        from kervi.plugin.message_bus.bus_manager import BusManager
        self._bus_manager = BusManager()
        self._bus_manager.load(process_id, spine_port, root_address, ip)
        return self._bus_manager.bus

    def process_step(self):
        self.plugin.process_step()
        time.sleep(0.01)

    def terminate_process(self):
        self.plugin.terminate_process()
Ejemplo n.º 3
0
class Module(object):
    """ Kervi application class that starts a kervi application and loads all needed modules.
    """
    def __init__(self, user_config=None, **kwargs):
        """ Settings is a dictionary with the following content
        """

        from kervi.config import load
        import inspect
        import sys
        import os

        script_path = os.path.basename(os.path.abspath(inspect.stack()[1][1]))
        script_name, script_ext = os.path.splitext(script_path)

        config_files = []

        import getopt
        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)

        config_files += [script_name + ".config.json"]
        config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break
        if selected_config_file:
            print("using config file:", selected_config_file)
        #else:
        #    print("no config file found, revert to defaults")

        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        service_commands = []
        detect_devices = None
        self._as_service = False
        for opt, arg in opts:
            if opt in ("--as-service"):
                self._as_service = True

            if opt in ("--detect-devices"):
                detect_devices = True

            if opt in ("--install-service"):
                service_commands += ["install"]

            if opt in ("--uninstall-service"):
                service_commands += ["uninstall"]

            if opt in ("--start-service"):
                service_commands += ["start"]

            if opt in ("--stop-service"):
                service_commands = ["stop"]

            if opt in ("--restart-service"):
                service_commands = ["restart"]
            if opt in ("--service-status"):
                service_commands = ["status"]

        if service_commands:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                hal.service_commands(service_commands,
                                     self.config.application.name,
                                     self.config.application.id, script_path)
            exit()

        if detect_devices:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                devices = hal.detect_devices()
                print("devices:")
                _pretty_print(devices)
            exit()

        self._log_handler = KerviLogHandler(self.config)
        self._log_queue = self._log_handler._log_queue
        self._logger = KerviLog("module")

        self._logger.info("Starting kervi module, please wait")

        try:
            from kervi.version import VERSION
        except:
            VERSION = "0.0.0"

        self._logger.verbose("kervi version: %s", VERSION)

        self.started = False
        if self.config.module.app_connection_local and not self.config.network.ipc_root_address:
            self._logger.verbose("Locating kervi application with id {0}...",
                                 self.config.application.id)
            from kervi.utility.discovery import find_kervi_app
            address, port = find_kervi_app(self.config.application.id)
            if address:
                self.config.network.ipc_root_address = address
                self.config.network.ipc_root_port = port
            else:
                self._logger.error("Locate kervi application failed")
                exit()

        self._root_address = "tcp://" + self.config.network.ipc_root_address + ":" + str(
            self.config.network.ipc_root_port)

        from kervi.plugin.message_bus.bus_manager import BusManager
        self.bus_manager = BusManager()
        #self.config.network.ipc_root_port = nethelper.get_free_port([self.config.network.ipc_root_port])
        self._logger.verbose("ip: {0}", self.config.network.ip)
        if self.config.module.app_connection_local:
            self.bus_manager.load(
                "kervi-module", self.config.network.ipc_module_port,
                "tcp://" + self.config.network.ipc_root_address + ":" +
                str(self.config.network.ipc_root_port), self.config.network.ip)
        else:
            self.bus_manager.load("kervi-main",
                                  self.config.network.ipc_root_port, None,
                                  self.config.network.ipc_root_address)

        from kervi import spine
        self.spine = spine.Spine()

        self.spine.register_event_handler("processReady",
                                          self._process_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine = spine.Spine()

        self._module_processes = []
        self._process_info = []
        self._process_info_lock = threading.Lock()

        import kervi.hal as hal
        hal_driver = hal._load()
        if hal_driver:
            self._logger.verbose("Using HAL driver: %s", hal_driver)

        self._actions = _ModuleActions(self)

    @property
    def actions(self):
        return self._actions

    def _validateSettings(self):
        pass

    def get_process_info(self):
        return {"id": "application"}

    def _process_ready(self, scope, process_id, pid):
        self._process_info_lock.acquire()
        try:
            for process in self._process_info:
                if process["id"] == process_id:
                    process["ready"] = True
                    process["pid"] = pid
        finally:
            self._process_info_lock.release()

    def _is_ready(self):
        result = True
        self._process_info_lock.acquire()
        try:
            for process in self._process_info:
                if not process["ready"]:
                    result = False
        finally:
            self._process_info_lock.release()

        return result

    def _start(self):
        self.started = True

        if self._as_service:
            import signal
            signal.signal(signal.SIGINT, handler_stop_signals)
            signal.signal(signal.SIGTERM, handler_stop_signals)

        try:
            import dashboards
        except ImportError:
            pass

        #self.spine.run()
        self.spine.send_command("startThreads", local_only=True)
        time.sleep(.5)

        module_port = self.config.network.ipc_module_port
        pluginManager = PluginManager(self.config)
        self._process_info_lock.acquire()
        #self._process_info = [{"id":"IPC", "ready":False}]
        plugin_modules = pluginManager.prepare_load()
        for plugin_module in plugin_modules:
            self._process_info.append({
                "id": plugin_module,
                "ready": False,
                "pid": None
            })
        self._process_info_lock.release()

        module_port = self.config.network.ipc_module_port
        module_port = pluginManager.load_plugins(module_port + 1)

        for module in self.config.modules:
            module_port += 1
            self._process_info_lock.acquire()
            self._process_info += [{"id": module, "ready": False, "pid": None}]
            self._process_info_lock.release()

            self._module_processes += [
                process._start_process("module-" + self.config.module.ip,
                                       module,
                                       self.config,
                                       nethelper.get_free_port([module_port]),
                                       app_helpers._KerviModuleLoader,
                                       process_id=self.config.module.id + "-" +
                                       module)
            ]

        while not self._is_ready():
            time.sleep(1)

        self._logger.info("module connected to application at: %s",
                          self._root_address)
        self._logger.info("Press ctrl + c to stop your module")
        self.spine.trigger_event("moduleReady", self.config.module.id)
        self.spine.send_command("kervi_action_module_main")

    def _input_thread(self, list):
        try:
            raw_input()
            list.append(None)
        except KeyboardInterrupt:
            pass
        except:
            pass

    def run(self):
        """Run the application loop. The application will continue until termination with ctrl-c"""

        if not self.started:
            self._start()
        try:
            char_list = []
            thread.start_new_thread(self._input_thread, (char_list, ))
            while not char_list and _app_running:
                self.spine.trigger_event("modulePing", self.config.module.id)
                time.sleep(1)

        except KeyboardInterrupt:
            pass

        self.stop()

    def _xrun(self):
        """Used in tests"""

        if not self.started:
            self._start()

    def stop(self, force_exit=True, restart=False):
        self.spine.send_command("kervi_action_module_exit")
        self.spine.trigger_event("moduleStopped", self.config.module.id)
        self._logger.warn("stopping processes")
        process._stop_processes("module-" + self.config.module.id)
        self.spine.trigger_event("processTerminating", None, local_only=True)
        time.sleep(1)
        spine.Spine().stop()
        #for thread in threading.enumerate():
        #    print("running thread",thread.name)
        self._logger.info("module stopped")
        if force_exit:
            import os
            os._exit(0)

    def _stop_action(self):
        self._logger.verbose("module action: stop")
        self.stop()

    def _restart_action(self):
        self._logger.verbose("module action: restart")
        self.stop(restart=True)

    def _reboot_action(self):
        self._logger.verbose("module action: reboot")
        import kervi.hal as hal
        hal.device_reboot()

    def _shutdown_action(self):
        self._logger.verbose("module action: shutdown")
        import kervi.hal as hal
        hal.device_shutdown()
Ejemplo n.º 4
0
class Application(object):
    """ Kervi application class that starts a kervi application and loads all needed modules.
        This class should be used by it self like:

    """
    def __init__(self, user_config=None):
        """ Settings is a dictionary with the following content
        """
        global _console_log

        print("\033[92mStarting kervi application \033[0m")
        import inspect
        import getopt
        self.char_list = []
        self._websocket_event = threading.Event()
        self._in_stop = False
        self._discovery_thread = None
        config_files = []
        self._webserver_info = None
        opts, args = getopt.getopt(sys.argv[1:], "c", [
            "config_file=", "as-service", "install-service",
            "uninstall-service", "start-service", "stop-service",
            "restart-service", "status-service", "detect-devices"
        ])
        for opt, arg in opts:
            if opt in ("-c", "--config_file"):
                if os.path.isfile(arg):
                    config_files += [arg]
                else:
                    print("Specified config file not found:", arg)
        script_path = os.path.abspath(inspect.stack()[1][1])
        script_name = os.path.basename(script_path)
        script_name, script_ext = os.path.splitext(script_name)

        config_files += [script_name + ".config.json"]
        config_files += ["kervi.config.json"]

        selected_config_file = None
        for config_file in config_files:
            if os.path.isfile(config_file):
                selected_config_file = config_file
                break

        from kervi.config import load
        self.config = load(config_file=selected_config_file,
                           config_user=user_config,
                           config_base=get_default_config())

        service_commands = []
        detect_devices = None
        self._as_service = False
        for opt, arg in opts:
            if opt in ("--as-service"):
                self._as_service = True

            if opt in ("--detect-devices"):
                detect_devices = True

            if opt in ("--install-service"):
                service_commands += ["install"]

            if opt in ("--uninstall-service"):
                service_commands += ["uninstall"]

            if opt in ("--start-service"):
                service_commands += ["start"]

            if opt in ("--stop-service"):
                service_commands = ["stop"]

            if opt in ("--restart-service"):
                service_commands = ["restart"]
            if opt in ("--service-status"):
                service_commands = ["status"]

        if service_commands:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                hal.service_commands(service_commands,
                                     self.config.application.name,
                                     self.config.application.id, script_path)
            exit()

        if detect_devices:
            import kervi.hal as hal
            hal_driver = hal._load(self.config.platform.driver)
            if hal_driver:
                devices = hal.detect_devices()
                print("devices:")
                _pretty_print(devices)
            exit()
        self._log_handler = KerviLogHandler(self.config)
        self._log_queue = self._log_handler._log_queue
        self._logger = KerviLog("application")
        #self._validateSettings()
        self.started = False
        self._webserver_port = None

        try:
            from kervi.version import VERSION
        except:
            VERSION = "0.0.0"

        self._logger.verbose("kervi version: %s", VERSION)

        import kervi.hal as hal
        hal_driver = hal._load(self.config.platform.driver)
        if hal_driver:
            self._logger.verbose("platform driver: %s", hal_driver)
            self._logger.verbose("board: %s", hal.get_board_name())

        from kervi.plugin.message_bus.bus_manager import BusManager
        self.bus_manager = BusManager(self._log_queue)
        self.config.network.ipc_root_port = nethelper.get_free_port(
            [self.config.network.ipc_root_port])
        self.bus_manager.load("kervi-main", self.config.network.ipc_root_port,
                              None, self.config.network.ipc_root_address)

        from kervi import spine
        self.spine = spine.Spine()
        self.spine.register_query_handler("GetApplicationInfo",
                                          self._get_application_info)
        self.spine.register_query_handler("getProcessInfo",
                                          self.get_process_info)
        self.spine.register_event_handler("modulePing", self._module_ping)
        self.spine.register_event_handler("processReady",
                                          self._process_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine.register_event_handler("WebAppReady",
                                          self._webapp_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self.spine.register_event_handler("websocketReady",
                                          self._websocket_ready,
                                          scope="app-" +
                                          self.config.application.id)
        self._module_processes = []
        self._process_info = []
        self._process_info_lock = threading.Lock()

        self._kervi_modules = []
        self._kervi_modules_lock = threading.Lock()

        from kervi.storage.storage_manager import StorageManager
        self._storage_manager = StorageManager(self._log_queue)

        from kervi.utility.authorization_manager import AuthorizationManager
        self._authorization_manager = AuthorizationManager(self._log_queue)

        from kervi.messaging.message_manager import MessageManager
        self._message_manager = MessageManager(self._log_queue)

        self._app_actions = _AppActions(self)

    @property
    def actions(self):
        return self._app_actions

    def _validateSettings(self):
        pass

    def _get_application_info(self):
        unit_system = self.config.unit_system
        display = {}
        display["unit_system"] = self.config.display.unit_systems.systems[
            unit_system]
        display["texts"] = self.config.texts.ui

        return {
            "name": self.config.application.name,
            "id": self.config.application.id,
            "display": display,
            "unit_system": unit_system
        }

    def get_process_info(self):
        return {"id": "application"}

    def _module_ping(self, module_id):
        with self._kervi_modules_lock:
            for module in self._kervi_modules:
                if module.module_id == module_id:

                    if not module.is_alive:
                        self._logger.verbose("module started: %s", module_id)
                        self.spine.trigger_event("moduleStarted", module_id)
                    module.ping()
                    break
            else:
                self._logger.verbose("module started: %s", module_id)
                self._kervi_modules += [KerviModule(module_id)]
                self.spine.trigger_event("moduleStarted", module_id)

    def _check_kervi_modules(self):
        with self._kervi_modules_lock:
            for module in self._kervi_modules:
                if not module.is_alive and module.connected:
                    self._logger.verbose("module stopped: %s",
                                         module.module_id)
                    module.connected = False
                    self.spine.trigger_event("moduleStopped", module.module_id)

    def _process_ready(self, scope, process_id, pid):
        self._process_info_lock.acquire()
        try:
            for process in self._process_info:
                if process["id"] == process_id:
                    process["ready"] = True
                    process["pid"] = pid
        finally:
            self._process_info_lock.release()

    def _webapp_ready(self, scope, webserver_info):
        self._webserver_info = webserver_info
        ready_message = "Reach your application at http://" + self._webserver_info[
            "ip"] + ":" + str(self._webserver_info["port"])
        self.spine.log.info(ready_message)
        self._websocket_event.wait()
        self.spine.send_command("startWebSocket")

    def _websocket_ready(self, scope, info):
        self._websocket_event.set()

    def _is_ready(self):
        result = True
        self._process_info_lock.acquire()
        try:
            for process in self._process_info:
                if not process["ready"]:
                    result = False
        finally:
            self._process_info_lock.release()

        return result

    def _start(self):
        self.started = True

        try:
            import dashboards
        except ImportError:
            pass

        import kervi.core.utility.process as process
        import kervi.utility.application_helpers as app_helpers

        #if self._as_service:
        signal.signal(signal.SIGINT, handler_stop_signals)
        signal.signal(signal.SIGTERM, handler_stop_signals)

        self.spine.send_command("startThreads", local_only=True)
        time.sleep(.5)
        module_port = self.config.network.ipc_root_port
        pluginManager = PluginManager(self.config, log_queue=self._log_queue)
        self._process_info_lock.acquire()
        plugin_modules = pluginManager.prepare_load()
        for plugin_module in plugin_modules:
            self._process_info.append({
                "id": plugin_module,
                "ready": False,
                "pid": None
            })
        self._process_info_lock.release()

        module_port = pluginManager.load_plugins(module_port + 1)

        for module in self.config.modules:
            self._process_info_lock.acquire()
            self._process_info += [{"id": module, "ready": False, "pid": None}]
            self._process_info_lock.release()

            module_port += 1
            self._module_processes += [
                process._start_process("app-" + self.config.application.id,
                                       module,
                                       self.config,
                                       nethelper.get_free_port([module_port]),
                                       app_helpers._KerviModuleLoader,
                                       log_queue=self._log_queue)
            ]

        #print("wait for ready")
        try:
            while not self.char_list and not self._is_ready():
                #print(self.char_list)
                time.sleep(1)
        except KeyboardInterrupt:
            pass

        #print("pi", self._process_info)

        if not self._in_stop:
            from kervi.dashboards import Dashboard
            Dashboard._add_default()

            self.spine.send_command("kervi_action_app_main")
            ready_message = "Your Kervi application is running"
            self._logger.info(ready_message)

            self._logger.info("Press ctrl + c to stop your application")
            self.spine.trigger_event("appReady", self.config.application.id)

            if self.config.discovery.enabled:
                self._discovery_thread = KerviAppDiscovery(
                    self.config.network.ip,
                    self.config.network.ipc_root_port,
                    self.config.discovery.port,
                    self.config.application.id,
                    self.config.discovery.challenge,
                    self.config.application.name,
                    "http://" + self.config.network.
                    ip  # + ":" + str(self.config.network.http_port)
                )
                self._discovery_thread.start()
            else:
                self._discovery_thread = None

    def _input_thread(self, list):
        try:
            raw_input()
            list.append(None)
        except KeyboardInterrupt:
            pass
        except:
            pass

    def run(self):
        """Run the application loop. The application will continue until termination with ctrl-c"""
        thread.start_new_thread(self._input_thread, (self.char_list, ))
        if not self.started:
            self._start()
        try:
            while not self.char_list and _app_running:
                self._check_kervi_modules()
                self.spine.trigger_event("appPing", self.config.application.id,
                                         {"ts": int(time.time())})
                time.sleep(1)

        except KeyboardInterrupt:
            pass

        self.stop()

    def _xrun(self):
        """Used in tests"""

        if not self.started:
            self._start()

    def stop(self, force_exit=True, restart=False):
        self._logger.warn("stopping processes")
        self._in_stop = True
        self.spine.send_command("kervi_action_app_exit")

        if self._discovery_thread:
            self._discovery_thread.terminate()

        import kervi.core.utility.process as process
        process._stop_processes("app-" + self.config.application.id)
        self.spine.trigger_event("processTerminating", None, local_only=True)
        time.sleep(1)
        self.bus_manager.stop()
        self._log_handler.stop()

        if self.config.development.debug_threads:
            time.sleep(1)
            for thread in threading.enumerate():
                print("running thread", thread.name)
        self._logger.info("application stopped")

        import psutil
        current_process = psutil.Process()
        children = current_process.children(recursive=True)
        for child in children:
            child.kill()

        if force_exit:
            import os
            os._exit(0)

    def _stop_action(self):
        self._logger.verbose("stop action", "")
        self.stop()

    def _restart_action(self):
        self._logger.verbose("restart action", "")
        self.stop(restart=True)

    def _reboot_action(self):
        self._logger.verbose("reboot action", "")
        import kervi.hal as hal
        hal.device_reboot()

    def _shutdown_action(self):
        self._logger.verbose("shutdown action", "")
        import kervi.hal as hal
        hal.device_shutdown()