Verified Commit 4756336f authored by Štěpán Henek's avatar Štěpán Henek 🌩

sentry-sdk integration

env variable SENTRY_DSN needs to be set and sentry-sdk installed
to work properly
parent 4a673e9a
import argparse import argparse
import bottle import bottle
import logging import logging
import os
import re import re
import typing import typing
...@@ -16,47 +17,55 @@ def get_arg_parser(): ...@@ -16,47 +17,55 @@ def get_arg_parser():
:return: instance of ArgumentParser :return: instance of ArgumentParser
""" """
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version=__version__) parser.add_argument("--version", action="version", version=__version__)
group = parser.add_argument_group("run server") group = parser.add_argument_group("run server")
group.add_argument("-H", "--host", default="0.0.0.0") group.add_argument("-H", "--host", default="0.0.0.0")
group.add_argument("-p", "--port", type=int, default=8080) group.add_argument("-p", "--port", type=int, default=8080)
group.add_argument("--session-timeout", type=int, default=900,
help="session timeout (in seconds)")
group.add_argument("-s", "--server", choices=["wsgiref", "flup", "cgi"], default="wsgiref")
group.add_argument("-d", "--debug", action="store_true")
group.add_argument("--noauth", action="store_true",
help="disable authentication (available only in debug mode)")
parser.add_argument("-R", "--routes", action="store_true", help="print routes and exit")
group.add_argument( group.add_argument(
"-S", "--static", action="store_true", "--session-timeout", type=int, default=900, help="session timeout (in seconds)"
help="serve static files directly through foris app (should be used for debug only)"
) )
group.add_argument("-s", "--server", choices=["wsgiref", "flup", "cgi"], default="wsgiref")
group.add_argument("-d", "--debug", action="store_true")
group.add_argument( group.add_argument(
"-a", "--app", choices=["config"], default="config", "--noauth",
help="sets which app should be started (config/...)", action="store_true",
help="disable authentication (available only in debug mode)",
) )
parser.add_argument("-R", "--routes", action="store_true", help="print routes and exit")
group.add_argument( group.add_argument(
"-b", "--message-bus", choices=["ubus", "unix-socket", "mqtt"], default="ubus", "-S",
help="message bus type" "--static",
action="store_true",
help="serve static files directly through foris app (should be used for debug only)",
) )
group.add_argument( group.add_argument(
"--mqtt-port", default=1883, type=int, help="mqtt port (default 1883)" "-a",
"--app",
choices=["config"],
default="config",
help="sets which app should be started (config/...)",
) )
group.add_argument( group.add_argument(
"--mqtt-host", default="localhost", help="mqtt host (default 'localhost')" "-b",
"--message-bus",
choices=["ubus", "unix-socket", "mqtt"],
default="ubus",
help="message bus type",
) )
group.add_argument("--mqtt-port", default=1883, type=int, help="mqtt port (default 1883)")
group.add_argument("--mqtt-host", default="localhost", help="mqtt host (default 'localhost')")
group.add_argument( group.add_argument(
"--mqtt-passwd-file", type=lambda x: read_passwd_file(x), "--mqtt-passwd-file",
type=lambda x: read_passwd_file(x),
help="path to passwd file (first record will be used to authenticate)", help="path to passwd file (first record will be used to authenticate)",
default=None, default=None,
) )
group.add_argument( group.add_argument(
"--mqtt-controller-id", type=lambda x: re.match(r"[0-9a-zA-Z]{16}", x).group().upper(), "--mqtt-controller-id",
type=lambda x: re.match(r"[0-9a-zA-Z]{16}", x).group().upper(),
help="sets which controller on the messages bus should be configured (8 bytes in hex)", help="sets which controller on the messages bus should be configured (8 bytes in hex)",
) )
group.add_argument( group.add_argument("--bus-socket", default="/var/run/ubus.sock", help="message bus socket path")
"--bus-socket", default="/var/run/ubus.sock", help="message bus socket path"
)
group.add_argument( group.add_argument(
"--ws-port", default=0, help="websocket server port - insecure (0=autodetect)", type=int "--ws-port", default=0, help="websocket server port - insecure (0=autodetect)", type=int
) )
...@@ -64,14 +73,16 @@ def get_arg_parser(): ...@@ -64,14 +73,16 @@ def get_arg_parser():
"--wss-port", default=0, help="websocket server port - secure (0=autodetect)", type=int "--wss-port", default=0, help="websocket server port - secure (0=autodetect)", type=int
) )
group.add_argument( group.add_argument(
"--ws-path", default="/foris-ws", help="websocket server url path - insecure", type=str, "--ws-path", default="/foris-ws", help="websocket server url path - insecure", type=str
) )
group.add_argument( group.add_argument(
"--wss-path", default="/foris-ws", help="websocket server url path - secure", type=str, "--wss-path", default="/foris-ws", help="websocket server url path - secure", type=str
) )
group.add_argument( group.add_argument(
"-A", "--assets", default="/tmp/.foris_workdir/dynamic_assets", "-A",
help="Path where dynamic foris assets will be generated." "--assets",
default="/tmp/.foris_workdir/dynamic_assets",
help="Path where dynamic foris assets will be generated.",
) )
return parser return parser
...@@ -86,14 +97,27 @@ def main(): ...@@ -86,14 +97,27 @@ def main():
logger = logging.getLogger("foris") logger = logging.getLogger("foris")
logger.debug("Version %s" % __version__) logger.debug("Version %s" % __version__)
# try to include sentry (if installed)
try:
import sentry_sdk
from sentry_sdk.integrations.bottle import BottleIntegration
dsn = os.environ["SENTRY_DSN"]
sentry_sdk.init(dsn=dsn, integrations=[BottleIntegration()])
except (ImportError, KeyError):
pass
# set backend # set backend
if args.message_bus in ["ubus", "unix-socket"]: if args.message_bus in ["ubus", "unix-socket"]:
current_state.set_backend(Backend(args.message_bus, path=args.bus_socket)) current_state.set_backend(Backend(args.message_bus, path=args.bus_socket))
elif args.message_bus == "mqtt": elif args.message_bus == "mqtt":
current_state.set_backend( current_state.set_backend(
Backend( Backend(
args.message_bus, host=args.mqtt_host, port=args.mqtt_port, args.message_bus,
credentials=args.mqtt_passwd_file, controller_id=args.mqtt_controller_id, host=args.mqtt_host,
port=args.mqtt_port,
credentials=args.mqtt_passwd_file,
controller_id=args.mqtt_controller_id,
) )
) )
...@@ -104,6 +128,7 @@ def main(): ...@@ -104,6 +128,7 @@ def main():
if args.app == "config": if args.app == "config":
from foris.config_app import prepare_config_app from foris.config_app import prepare_config_app
main_app = prepare_config_app(args) main_app = prepare_config_app(args)
if args.routes: if args.routes:
......
...@@ -16,6 +16,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) ...@@ -16,6 +16,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def merge_po_files(): def merge_po_files():
from babel.messages.pofile import read_po, write_po from babel.messages.pofile import read_po, write_po
trans_dirs = glob.glob(os.path.join(BASE_DIR, "foris/locale/*/LC_MESSAGES/")) trans_dirs = glob.glob(os.path.join(BASE_DIR, "foris/locale/*/LC_MESSAGES/"))
# iterate through translations # iterate through translations
...@@ -53,6 +54,7 @@ class BuildCmd(build_py): ...@@ -53,6 +54,7 @@ class BuildCmd(build_py):
# compile translation # compile translation
from babel.messages import frontend as babel from babel.messages import frontend as babel
distribution = copy.copy(self.distribution) distribution = copy.copy(self.distribution)
cmd = babel.compile_catalog(distribution) cmd = babel.compile_catalog(distribution)
cmd.directory = os.path.join(os.path.dirname(__file__), "foris", "locale") cmd.directory = os.path.join(os.path.dirname(__file__), "foris", "locale")
...@@ -75,18 +77,14 @@ setup( ...@@ -75,18 +77,14 @@ setup(
install_requires=[ install_requires=[
"bottle", "bottle",
"bottle_i18n", "bottle_i18n",
'pbkdf2', "pbkdf2",
'flup', "flup",
"ubus @ git+https://gitlab.labs.nic.cz/turris/python-ubus.git", "ubus @ git+https://gitlab.labs.nic.cz/turris/python-ubus.git",
"paho-mqtt", "paho-mqtt",
], ],
setup_requires=[ setup_requires=["babel", "jinja2"],
'babel', provides=["foris"],
'jinja2', extras_require={"sentry": ["sentry-sdk>=0.7.9"]},
],
provides=[
"foris"
],
packages=[ packages=[
"foris", "foris",
"foris.config_handlers", "foris.config_handlers",
...@@ -101,7 +99,7 @@ setup( ...@@ -101,7 +99,7 @@ setup(
"foris_plugins", "foris_plugins",
], ],
package_data={ package_data={
'': [ "": [
"LICENSE", "LICENSE",
"locale/**/LC_MESSAGES/*.mo", "locale/**/LC_MESSAGES/*.mo",
"templates/**", "templates/**",
...@@ -114,15 +112,7 @@ setup( ...@@ -114,15 +112,7 @@ setup(
"utils/*.pickle2", "utils/*.pickle2",
] ]
}, },
namespace_packages=[ namespace_packages=["foris_plugins"],
'foris_plugins', cmdclass={"build_py": BuildCmd},
], entry_points={"console_scripts": ["foris = foris.__main__:main"]},
cmdclass={
"build_py": BuildCmd,
},
entry_points={
"console_scripts": [
"foris = foris.__main__:main",
]
},
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment