Beispiel #1
0
def run_dash(loop, tasks):
    # noinspection PyBroadException
    try:
        conf.dashboard_obj = dashboard.Dashboard(
            conf.config_dir,
            conf.dash,
            dash_compile_on_start=conf.dash_compile_on_start,
            dash_force_compile=conf.dash_force_compile,
            profile_dashboard=conf.profile_dashboard,
            dashboard_dir=conf.dashboard_dir,
        )
        setup_routes(conf.dashboard_obj)

        if conf.dash_ssl_certificate is not None and conf.dash_ssl_key is not None:
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            context.load_cert_chain(conf.dash_ssl_certificate,
                                    conf.dash_ssl_key)
        else:
            context = None

        handler = app.make_handler()

        f = loop.create_server(handler,
                               "0.0.0.0",
                               int(conf.dash_port),
                               ssl=context)

        tasks.append(asyncio. async (f))
        tasks.append(asyncio. async (update_rss()))
        return f
    except:
        utils.log(conf.dash, "WARNING", '-' * 60)
        utils.log(conf.dash, "WARNING", "Unexpected error in dashboard thread")
        utils.log(conf.dash, "WARNING", '-' * 60)
        utils.log(conf.dash, "WARNING", traceback.format_exc())
        utils.log(conf.dash, "WARNING", '-' * 60)
Beispiel #2
0
    def __init__(self, ad, loop, logger, access, **config):

        self.AD = ad
        self.logger = logger
        self.acc = access

        self.dashboard_dir = None
        self._process_arg("dashboard_dir", config)

        self.dash_password = None
        self._process_arg("dash_password", config)

        self.dash_url = None
        self._process_arg("dash_url", config)

        self.config_dir = None
        self._process_arg("config_dir", config)

        self.dash_compile_on_start = False
        self._process_arg("dash_compile_on_start", config)

        self.dash_force_compile = False
        self._process_arg("dash_force_compile", config)

        self.work_factor = 8
        self._process_arg("work_factor", config)

        self.profile_dashboard = False
        self._process_arg("profile_dashboard", config)

        self.dash_ssl_certificate = None
        self._process_arg("dash_ssl_certificate", config)

        self.dash_ssl_key = None
        self._process_arg("dash_ssl_key", config)

        self.rss_feeds = None
        self._process_arg("rss_feeds", config)

        self.fa4compatibility = False
        self._process_arg("fa4compatibility", config)

        if "rss_feeds" in config:
            self.rss_feeds = []
            for feed in config["rss_feeds"]:
                if feed["target"].count('.') != 1:
                    self.log(
                        "WARNING",
                        "Invalid RSS feed target: {}".format(feed["target"]))
                else:
                    self.rss_feeds.append(feed)

        self.rss_update = None
        self._process_arg("rss_update", config)

        self.rss_last_update = None

        self.stopping = False

        url = urlparse(self.dash_url)

        dash_net = url.netloc.split(":")
        self.dash_host = dash_net[0]
        try:
            self.dash_port = dash_net[1]
        except IndexError:
            self.dash_port = 80

        if self.dash_host == "":
            raise ValueError("Invalid host for 'dash_url'")

        # find dashboard dir

        if self.dashboard_dir is None:
            if self.config_dir is None:
                self.dashboard_dir = utils.find_path("dashboards")
            else:
                self.dashboard_dir = os.path.join(self.config_dir,
                                                  "dashboards")

            #
            # Setup compile directories
            #
            if self.config_dir is None:
                self.compile_dir = utils.find_path("compiled")
            else:
                self.compile_dir = os.path.join(self.config_dir, "compiled")

        # Setup WS handler

        self.app = web.Application()
        self.app['websockets'] = {}

        self.loop = loop
        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)

        try:
            self.dashboard_obj = dashboard.Dashboard(
                self.config_dir,
                access,
                dash_compile_on_start=self.dash_compile_on_start,
                dash_force_compile=self.dash_force_compile,
                profile_dashboard=self.profile_dashboard,
                dashboard_dir=self.dashboard_dir,
                fa4compatibility=self.fa4compatibility)
            self.setup_routes()

            if self.dash_ssl_certificate is not None and self.dash_ssl_key is not None:
                context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                context.load_cert_chain(self.dash_ssl_certificate,
                                        self.dash_ssl_key)
            else:
                context = None

            handler = self.app.make_handler()

            f = loop.create_server(handler,
                                   "0.0.0.0",
                                   int(self.dash_port),
                                   ssl=context)

            #print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [
            #   [(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in
            #    [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])

            loop.create_task(f)
            loop.create_task(self.update_rss())
        except:
            self.log("WARNING", '-' * 60)
            self.log("WARNING", "Unexpected error in dashboard thread")
            self.log("WARNING", '-' * 60)
            self.log("WARNING", traceback.format_exc())
            self.log("WARNING", '-' * 60)
    def __init__(self, ad: AppDaemon, loop, logging, appdaemon, dashboard,
                 admin, api, http):

        self.AD = ad
        self.logging = logging
        self.logger = ad.logging.get_child("_http")
        self.access = ad.logging.get_access()

        self.appdaemon = appdaemon
        self.dashboard = dashboard
        self.dashboard_dir = None
        self.admin = admin
        self.http = http
        self.api = api
        self.runner = None

        self.template_dir = os.path.join(os.path.dirname(__file__), "assets",
                                         "templates")

        self.password = None
        self._process_arg("password", http)

        self.valid_tokens = []
        self._process_arg("tokens", http)

        self.url = None
        self._process_arg("url", http)

        self.work_factor = 12
        self._process_arg("work_factor", http)

        self.ssl_certificate = None
        self._process_arg("ssl_certificate", http)

        self.ssl_key = None
        self._process_arg("ssl_key", http)

        self.transport = "ws"
        self._process_arg("transport", http)
        self.logger.info("Using '%s' for event stream", self.transport)

        self.config_dir = None
        self._process_arg("config_dir", dashboard)

        self.static_dirs = {}
        self._process_arg("static_dirs", http)

        self.stopping = False

        self.endpoints = {}
        self.app_routes = {}

        self.dashboard_obj = None
        self.admin_obj = None

        self.install_dir = os.path.dirname(__file__)

        self.javascript_dir = os.path.join(self.install_dir, "assets",
                                           "javascript")
        self.template_dir = os.path.join(self.install_dir, "assets",
                                         "templates")
        self.css_dir = os.path.join(self.install_dir, "assets", "css")
        self.fonts_dir = os.path.join(self.install_dir, "assets", "fonts")
        self.webfonts_dir = os.path.join(self.install_dir, "assets",
                                         "webfonts")
        self.images_dir = os.path.join(self.install_dir, "assets", "images")

        try:
            url = urlparse(self.url)

            net = url.netloc.split(":")
            self.host = net[0]
            try:
                self.port = net[1]
            except IndexError:
                self.port = 80

            if self.host == "":
                raise ValueError("Invalid host for 'url'")

            self.app = web.Application()

            if "headers" in self.http:
                self.app.on_response_prepare.append(self.add_response_headers)

            # Setup event stream

            self.stream = stream.ADStream(self.AD, self.app, self.transport)

            self.loop = loop
            self.executor = concurrent.futures.ThreadPoolExecutor(
                max_workers=5)

            if self.ssl_certificate is not None and self.ssl_key is not None:
                self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                self.context.load_cert_chain(self.ssl_certificate,
                                             self.ssl_key)
            else:
                self.context = None

            self.setup_http_routes()

            #
            # API
            #

            if api is not None:
                self.logger.info("Starting API")
                self.setup_api_routes()
            else:
                self.logger.info("API is disabled")

            #
            # Admin
            #

            if admin is not None:
                self.logger.info("Starting Admin Interface")

                self.stats_update = "realtime"
                self._process_arg("stats_update", admin)

                self.admin_obj = adadmin.Admin(
                    self.config_dir,
                    logging,
                    self.AD,
                    javascript_dir=self.javascript_dir,
                    template_dir=self.template_dir,
                    css_dir=self.css_dir,
                    fonts_dir=self.fonts_dir,
                    webfonts_dir=self.webfonts_dir,
                    images_dir=self.images_dir,
                    transport=self.transport,
                    **admin)

            else:
                self.logger.info("Admin Interface is disabled")
            #
            # Dashboards
            #

            if dashboard is not None:
                self.logger.info("Starting Dashboards")

                self._process_arg("dashboard_dir", dashboard)

                self.compile_on_start = True
                self._process_arg("compile_on_start", dashboard)

                self.force_compile = False
                self._process_arg("force_compile", dashboard)

                self.profile_dashboard = False
                self._process_arg("profile_dashboard", dashboard)

                self.rss_feeds = None
                self._process_arg("rss_feeds", dashboard)

                self.fa4compatibility = False
                self._process_arg("fa4compatibility", dashboard)

                if "rss_feeds" in dashboard:
                    self.rss_feeds = []
                    for feed in dashboard["rss_feeds"]:
                        if feed["target"].count(".") != 1:
                            self.logger.warning("Invalid RSS feed target: %s",
                                                feed["target"])
                        else:
                            self.rss_feeds.append(feed)

                self.rss_update = None
                self._process_arg("rss_update", dashboard)

                self.rss_last_update = None

                # find dashboard dir

                if self.dashboard_dir is None:
                    if self.config_dir is None:
                        self.dashboard_dir = utils.find_path("dashboards")
                    else:
                        self.dashboard_dir = os.path.join(
                            self.config_dir, "dashboards")

                self.javascript_dir = os.path.join(self.install_dir, "assets",
                                                   "javascript")
                self.template_dir = os.path.join(self.install_dir, "assets",
                                                 "templates")
                self.css_dir = os.path.join(self.install_dir, "assets", "css")
                self.fonts_dir = os.path.join(self.install_dir, "assets",
                                              "fonts")
                self.webfonts_dir = os.path.join(self.install_dir, "assets",
                                                 "webfonts")
                self.images_dir = os.path.join(self.install_dir, "assets",
                                               "images")

                #
                # Setup compile directories
                #
                if self.config_dir is None:
                    self.compile_dir = utils.find_path("compiled")
                else:
                    self.compile_dir = os.path.join(self.config_dir,
                                                    "compiled")

                self.dashboard_obj = addashboard.Dashboard(
                    self.config_dir,
                    self.logging,
                    dash_compile_on_start=self.compile_on_start,
                    dash_force_compile=self.force_compile,
                    profile_dashboard=self.profile_dashboard,
                    dashboard_dir=self.dashboard_dir,
                    fa4compatibility=self.fa4compatibility,
                    transport=self.transport,
                    javascript_dir=self.javascript_dir,
                    template_dir=self.template_dir,
                    css_dir=self.css_dir,
                    fonts_dir=self.fonts_dir,
                    webfonts_dir=self.webfonts_dir,
                    images_dir=self.images_dir,
                )
                self.setup_dashboard_routes()

            else:
                self.logger.info("Dashboards Disabled")

            #
            # Finish up and start the server
            #

            # handler = self.app.make_handler()

            # f = loop.create_server(handler, "0.0.0.0", int(self.port), ssl=context)
            # loop.create_task(f)

            if self.dashboard_obj is not None:
                loop.create_task(self.update_rss())

        except Exception:
            self.logger.warning("-" * 60)
            self.logger.warning("Unexpected error in HTTP module")
            self.logger.warning("-" * 60)
            self.logger.warning(traceback.format_exc())
            self.logger.warning("-" * 60)
Beispiel #4
0
    def __init__(self, ad, loop, logger, access, **config):

        self.AD = ad
        self.logger = logger
        self.acc = access

        self.dashboard_dir = None
        self._process_arg("dashboard_dir", config)

        self.dash_password = None
        self._process_arg("dash_password", config)

        self.dash_url = None
        self._process_arg("dash_url", config)

        self.config_dir = None
        self._process_arg("config_dir", config)

        self.dash_compile_on_start = False
        self._process_arg("dash_compile_on_start", config)

        self.dash_force_compile = False
        self._process_arg("dash_force_compile", config)

        self.work_factor = 8
        self._process_arg("work_factor", config)

        self.profile_dashboard = False
        self._process_arg("profile_dashboard", config)

        self.dash_ssl_certificate = None
        self._process_arg("dash_ssl_certificate", config)

        self.dash_ssl_key = None
        self._process_arg("dash_ssl_key", config)

        self.rss_feeds = None
        self._process_arg("rss_feeds", config)

        self.rss_update = None
        self._process_arg("rss_update", config)

        self.rss_last_update = None

        self.stopping = False

        url = urlparse(self.dash_url)

        dash_net = url.netloc.split(":")
        self.dash_host = dash_net[0]
        try:
            self.dash_port = dash_net[1]
        except IndexError:
            self.dash_port = 80

        if self.dash_host == "":
            raise ValueError("Invalid host for 'dash_url'")

        # find dashboard dir

        if self.dashboard_dir is None:
            if self.config_dir is None:
                self.dashboard_dir = utils.find_path("dashboards")
            else:
                self.dashboard_dir = os.path.join(self.config_dir, "dashboards")


            #
            # Setup compile directories
            #
            if self.config_dir is None:
                self.compile_dir = utils.find_path("compiled")
            else:
                self.compile_dir = os.path.join(self.config_dir, "compiled")


        # Setup WS handler

        self.app = web.Application()
        self.app['websockets'] = {}

        self.loop = loop
        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)

        try:
            self.dashboard_obj = dashboard.Dashboard(self.config_dir, access,
                                                 dash_compile_on_start=self.dash_compile_on_start,
                                                 dash_force_compile=self.dash_force_compile,
                                                 profile_dashboard=self.profile_dashboard,
                                                 dashboard_dir = self.dashboard_dir,
                                                     )
            self.setup_routes()

            if self.dash_ssl_certificate is not None and self.dash_ssl_key is not None:
                context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                context.load_cert_chain(self.dash_ssl_certificate, self.dash_ssl_key)
            else:
                context = None

            handler = self.app.make_handler()

            f = loop.create_server(handler, "0.0.0.0", int(self.dash_port), ssl=context)

            loop.create_task(f)
            loop.create_task(self.update_rss())
        except:
            self.log("WARNING", '-' * 60)
            self.log("WARNING", "Unexpected error in dashboard thread")
            self.log("WARNING", '-' * 60)
            self.log("WARNING", traceback.format_exc())
            self.log("WARNING", '-' * 60)
Beispiel #5
0
    def _process_dashboard(self, dashboard):
        self.logger.info("Starting Dashboards")

        self._process_arg("dashboard_dir", dashboard)

        self.compile_on_start = True
        self._process_arg("compile_on_start", dashboard)

        self.force_compile = False
        self._process_arg("force_compile", dashboard)

        self.profile_dashboard = False
        self._process_arg("profile_dashboard", dashboard)

        self.rss_feeds = None
        self._process_arg("rss_feeds", dashboard)

        self.fa4compatibility = False
        self._process_arg("fa4compatibility", dashboard)

        if "rss_feeds" in dashboard:
            self.rss_feeds = []
            for feed in dashboard["rss_feeds"]:
                if feed["target"].count(".") != 1:
                    self.logger.warning("Invalid RSS feed target: %s",
                                        feed["target"])
                else:
                    self.rss_feeds.append(feed)

        self.rss_update = None
        self._process_arg("rss_update", dashboard)

        self.rss_last_update = None

        # find dashboard dir

        if self.dashboard_dir is None:
            if self.config_dir is None:
                self.dashboard_dir = utils.find_path("dashboards")
            else:
                self.dashboard_dir = os.path.join(self.config_dir,
                                                  "dashboards")

        self.javascript_dir = os.path.join(self.install_dir, "assets",
                                           "javascript")
        self.template_dir = os.path.join(self.install_dir, "assets",
                                         "templates")
        self.css_dir = os.path.join(self.install_dir, "assets", "css")
        self.fonts_dir = os.path.join(self.install_dir, "assets", "fonts")
        self.webfonts_dir = os.path.join(self.install_dir, "assets",
                                         "webfonts")
        self.images_dir = os.path.join(self.install_dir, "assets", "images")

        #
        # Setup compile directories
        #
        if self.config_dir is None:
            self.compile_dir = utils.find_path("compiled")
        else:
            self.compile_dir = os.path.join(self.config_dir, "compiled")

        self.dashboard_obj = addashboard.Dashboard(
            self.config_dir,
            self.logging,
            dash_compile_on_start=self.compile_on_start,
            dash_force_compile=self.force_compile,
            profile_dashboard=self.profile_dashboard,
            dashboard_dir=self.dashboard_dir,
            fa4compatibility=self.fa4compatibility,
            transport=self.transport,
            javascript_dir=self.javascript_dir,
            template_dir=self.template_dir,
            css_dir=self.css_dir,
            fonts_dir=self.fonts_dir,
            webfonts_dir=self.webfonts_dir,
            images_dir=self.images_dir,
        )
        self.setup_dashboard_routes()