Esempio n. 1
0
    def __init__(self, ad: AppDaemon, loop, logging, appdaemon, dashboard,
                 admin, aui, 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.aui = aui
        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.valid_tokens = []
        self.url = None
        self.work_factor = 12
        self.ssl_certificate = None
        self.ssl_key = None
        self.transport = "ws"

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

        self.static_dirs = {}

        self._process_http(http)

        self.stopping = False

        self.app_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")

        # AUI
        self.aui_dir = os.path.join(self.install_dir, "assets", "aui")
        self.aui_css_dir = os.path.join(self.install_dir, "assets", "aui/css")
        self.aui_js_dir = os.path.join(self.install_dir, "assets", "aui/js")

        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 aui is not None:
                self.logger.info("Starting Admin Interface")

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

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

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

            if admin is not None or aui is not None:

                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,
                )

            if admin is None and aui is None:
                self.logger.info("Admin Interface is disabled")
            #
            # Dashboards
            #

            if dashboard is not None:
                self._process_dashboard(dashboard)

            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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    def __init__(self, ad, loop, logger, access, **config):

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

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

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

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

        self.admin_port = 5002
        self._process_arg("admin_port", 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.stopping = False

        # Setup WS handler

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

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

        try:

            self.admin_obj = admin.Admin(self.config_dir, access, self.AD)

            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.admin_port),
                                   ssl=context)

            loop.create_task(f)
        except:
            self.log("WARNING", '-' * 60)
            self.log("WARNING", "Unexpected error in admin thread")
            self.log("WARNING", '-' * 60)
            self.log("WARNING", traceback.format_exc())
            self.log("WARNING", '-' * 60)