Ejemplo n.º 1
0
def init_api():
    config.load_config()
    logging.config.dictConfig(config.config["logging"])

    CORS(app)  # TODO: Holdover from node server, this may not be needed.
    blueprint = Blueprint("api", __name__)
    firebase_app = firebase_admin.initialize_app(credentials.Certificate(
        config.config["auth"]["key-file"]),
                                                 options=config.config["auth"])

    # Set up API and register namespaces
    api = Api(
        blueprint,
        title="Beecology API",
        description=
        "The Beecology API, for uploading/downloading Beecology data or performing data analyses",
        contact_email="*****@*****.**",
        version="1.1.0",
        default_mediatype="application/json",
        authorizations=bee_authorizations,
        validate=True)
    api.add_namespace(bee_data_api.api)
    api.add_namespace(analysis_api.api, "/analysis")

    app.register_blueprint(blueprint)
Ejemplo n.º 2
0
def load_config():

    logging.config.fileConfig(os.path.dirname(path.abspath(__file__)) + '/proof.ini')

    logger.info('Loading config options.')
    config = Configuration(os.path.dirname(path.abspath(__file__)) + '/proof.ini')
    config.load_config()
    logger.info('Config is loaded.')

    return config
Ejemplo n.º 3
0
def set_config(cfg):
    """
    Defines the configuration object
    """
    global config
    config=cfg
    config.load_config()
    # Defines the log configuration
    logging.config.fileConfig(os.sep.join(
        [os.path.dirname(config.CONFIG_FILE),'log.cfg']))
    # Set the log level for sqlalchemy orm
    logging.getLogger('sqlalchemy').setLevel(
            eval('logging.'+config.get('database.log.level'))
        )
Ejemplo n.º 4
0
def main():
    if len(sys.argv) < 2:
        log.info("Please specify the config file. Use default")
        path = 'tweetstack.conf'
    else:
        path = sys.argv[1]

    conf = config.load_config(path)
    serv = service.Service(conf)
    serv.run()
Ejemplo n.º 5
0
def print_versions(args):
    """Print versions and exit."""
    versions = _("EH Forwarder Bot\n"
                 "Version: {version}\n"
                 "Python version:\n"
                 "{py_version}\n"
                 "Running on profile \"{profile}\".").format(
                     version=__version__,
                     py_version=sys.version,
                     profile=args.profile)
    try:
        if args.profile:
            coordinator.profile = str(args.profile)

        conf = config.load_config()
        # Master channel
        master_channel: MasterChannel = utils.locate_module(
            conf['master_channel'], 'master')
        instance_id = conf['master_channel'].split('#', 1)[1:]
        instance_id = (instance_id and instance_id[0]) or _("Default instance")
        versions += "\n\n" + _("Master channel:") + "\n    " + _("{name} ({id}) {version} # {instance_id}") \
            .format(name=master_channel.channel_name,
                    id=master_channel.channel_id,
                    version=master_channel.__version__,
                    instance_id=instance_id)
        versions += "\n\n" + ngettext("Slave channel:", "Slave channels:",
                                      len(conf['slave_channels']))
        for i in conf['slave_channels']:
            instance_id = i.split('#', 1)[1:]
            instance_id = (instance_id
                           and instance_id[0]) or _("Default instance")
            slave_channel: SlaveChannel = utils.locate_module(i, 'slave')
            versions += "\n    " + _("{name} ({id}) {version} # {instance_id}") \
                .format(name=slave_channel.channel_name,
                        id=slave_channel.channel_id,
                        version=slave_channel.__version__,
                        instance_id=instance_id)
        versions += "\n\n" + ngettext("Middleware:", "Middlewares:",
                                      len(conf['middlewares']))
        if conf['middlewares']:
            for i in conf['middlewares']:
                instance_id = i.split('#', 1)[1:]
                instance_id = (instance_id
                               and instance_id[0]) or _("Default instance")
                middleware: Middleware = utils.locate_module(i, 'middleware')
                versions += "\n    " + _("{name} ({id}) {version} # {instance_id}") \
                    .format(name=middleware.middleware_name,
                            id=middleware.middleware_id,
                            version=middleware.__version__,
                            instance_id=instance_id)
        else:
            versions += "\n    " + _("No middleware is enabled.")
    finally:
        print(versions)
Ejemplo n.º 6
0
    def __init__(self):
        # Logging.
        self.__setup_log()
        self.logger.info('Starting game')

        try:
            # Data.
            self.data = data.Data()
            self.data.config.config_dir = self.CONFIG_DIR

            # Load configuration.
            config.load_config(self.data, self.CONFIG_DIR, self.CONFIG_LOAD_FILE)

            # Draw decision trees.
            if self.data.config.render_decision_trees:
                renderer = dtr.DecisionTreeRenderer(self.DECISION_GRAPH_DIR, self.DECISION_GRAPH_FILE)
                renderer.render(self.data.game.decision_tree.start_nodes, self.data.game.decision_tree.nodes)

            # Demo mode.
            demo_loader = demo.Demo(self.data)
            demo_loader.load()

            # State modules.
            self.state_modules = [
                demo_loader,
                eventhandler.PygameEventHandler(self.data),
                director.Director()
            ]

            # Render modules.
            self.render_modules = [
                graphics.PygameGraphics(self.data),
                cleaner.RenderCleaner()
            ]

            # Initialize main loop.
            super().__init__(1.0 / self.data.config.fps, self.data.config.max_frame_time)
        except:
            self.logger.exception(self.LOG_TEXT_EXCEPTION)
            raise
Ejemplo n.º 7
0
def main():
    args = parser.parse_args()

    if getattr(args, "version", None):
        return print_versions(args)

    if args.profile:
        coordinator.profile = str(args.profile)

    conf = config.load_config()

    setup_logging(args, conf)
    setup_telemetry(conf['telemetry'])

    atexit.register(stop_gracefully)
    signal.signal(signal.SIGTERM, stop_gracefully)
    signal.signal(signal.SIGINT, stop_gracefully)

    init(conf)
    poll()
Ejemplo n.º 8
0
def main():
    args = parser.parse_args()

    if getattr(args, "version", None):
        return print_versions(args)

    if args.trace_threads:
        try:
            import hanging_threads
            global trace_threads
            trace_threads = True
        except ModuleNotFoundError:
            print(
                _("Required dependencies are not found. Please install them with "
                  "the following command:"))
            print()
            print(
                f"    ${sys.executable} -m pip install 'ehforwarderbot[trace]'"
            )
            print()
            exit(1)

    if args.profile:
        coordinator.profile = str(args.profile)

    conf = config.load_config()

    setup_logging(args, conf)
    setup_telemetry(conf['telemetry'])

    init(conf)

    # Only register graceful stop signals when we are ready to start
    # polling threads.
    atexit.register(stop_gracefully)
    signal.signal(signal.SIGTERM, stop_gracefully)
    signal.signal(signal.SIGINT, stop_gracefully)

    poll()
Ejemplo n.º 9
0
def main():
    args = parser.parse_args()

    if getattr(args, "version", None):
        versions = _("EH Forwarder Bot\n"
                     "Version: {version}\n"
                     "Python version:\n"
                     "{py_version}").format(version=__version__,
                                            py_version=sys.version)
        try:
            conf = config.load_config()
            # Master channel
            master_channel: EFBChannel = utils.locate_module(
                conf['master_channel'], 'master')
            instance_id = conf['master_channel'].split('#', 1)[1:]
            instance_id = (instance_id
                           and instance_id[0]) or _("Default instance")
            versions += "\n\n" + _("Master channel:") + "\n    " + _("{name} ({id}) {version} # {instance_id}") \
                .format(name=master_channel.channel_name,
                        id=master_channel.channel_id,
                        version=master_channel.__version__,
                        instance_id=instance_id)
            versions += "\n\n" + ngettext("Slave channel:", "Slave channels:",
                                          len(conf['slave_channels']))
            for i in conf['slave_channels']:
                instance_id = i.split('#', 1)[1:]
                instance_id = (instance_id
                               and instance_id[0]) or _("Default instance")
                slave_channel: EFBChannel = utils.locate_module(i, 'slave')
                versions += "\n    " + _("{name} ({id}) {version} # {instance_id}") \
                            .format(name=slave_channel.channel_name,
                                    id=slave_channel.channel_id,
                                    version=slave_channel.__version__,
                                    instance_id=instance_id)
            versions += "\n\n" + ngettext("Middleware:", "Middlewares:",
                                          len(conf['middlewares']))
            if conf['middlewares']:
                for i in conf['middlewares']:
                    instance_id = i.split('#', 1)[1:]
                    instance_id = (instance_id
                                   and instance_id[0]) or _("Default instance")
                    middleware: EFBMiddleware = utils.locate_module(
                        i, 'middleware')
                    versions += "\n    " + _("{name} ({id}) {version} # {instance_id}") \
                                .format(name=middleware.middleware_name,
                                        id=middleware.middleware_id,
                                        version=middleware.__version__,
                                        instance_id=instance_id)
            else:
                versions += "\n    " + _("No middleware is enabled.")
        finally:
            print(versions)
    else:
        conf = config.load_config()

        setup_logging(args, conf)
        setup_telemetry(conf['telemetry'])

        atexit.register(stop_gracefully)

        if args.profile:
            coordinator.profile = str(args.profile)

        init(conf)
        poll()
Ejemplo n.º 10
0
Importing this file causes the standard settings to be loaded
and a standard service manager to be created. This allows services
to be properly initialized before the webserver process has forked.
"""

import logging.config

from vonx.common import config
from vonx.indy.manager import IndyManager
from vonx.web import init_web

# Load application settings (environment)
ENV = config.load_settings()

# Load and apply logging config
LOG_CONFIG = config.load_config(ENV.get('LOG_CONFIG_PATH'))
logging.config.dictConfig(LOG_CONFIG)

MANAGER = IndyManager(ENV)


def pre_init():
    MANAGER.start_process()


async def init_app():
    return await init_web(MANAGER)


def shutdown():
    MANAGER.stop()
Ejemplo n.º 11
0
                        required=True,
                        dest="target_template",
                        type=str,
                        help='HTML Login template for the Captive Portal.')
    parser.add_argument('-d',
                        '--debug',
                        required=False,
                        dest="debug",
                        action='store_true',
                        help='Turn DEBUG output ON')

    options = parser.parse_args()

    # Init configuration manager
    config = ConfigurationManager()
    config.load_config('config.ini')

    # Check root invokation
    if os.geteuid():
        print '[-] Non root user detected.'
        print '[!] Please run again as root.'
        sys.exit(-1)

    # Logs configuration
    fileLogFormatter = logging.Formatter(
        "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
    consoleLogFormatter = logging.Formatter("%(message)s")
    rootLogger = logging.getLogger()

    fileHandler = logging.FileHandler(config.log_file)
    fileHandler.setFormatter(fileLogFormatter)
Ejemplo n.º 12
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c',
        '--config',
        type=pathlib.Path,
        help='Load this configuration file instead of the default files.')
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='Show configuration before starting, '
                        'and asyncio task status at shutdown.')
    parser.add_argument('-V',
                        '--version',
                        action='store_true',
                        help='Show the version of Flamenco Worker and stops.')
    parser.add_argument(
        '-r',
        '--reregister',
        action='store_true',
        help="Erases authentication information and re-registers this worker "
        "at the Manager. WARNING: this can cause duplicate worker information "
        "in the Manager's database.")
    parser.add_argument(
        '-d',
        '--debug',
        action='store_true',
        help="Enables debug logging for Flamenco Worker's own log entries. "
        "Edit the logging config in flamenco-worker.cfg "
        "for more powerful options.")
    parser.add_argument(
        '-t',
        '--test',
        action='store_true',
        help="Starts up in testing mode, in which only a handful of "
        "test-specific task types are accepted. This overrides the task_types "
        "in the configuration file.")
    parser.add_argument('-1',
                        '--single',
                        action='store_true',
                        help="Runs a single tasks, then exits.")
    args = parser.parse_args()

    if args.version:
        from . import __version__
        print(__version__)
        raise SystemExit()

    # Load configuration
    from . import config
    confparser = config.load_config(args.config, args.verbose, args.test)
    config.configure_logging(confparser, enable_debug=args.debug)

    log = logging.getLogger(__name__)
    log.debug('Starting, pid=%d', os.getpid())

    log_startup()

    if args.test:
        log.warning('Test mode enabled, overriding task_types=%r',
                    confparser.value('task_types'))

    if args.reregister:
        log.warning(
            'Erasing worker_id and worker_secret so we can attempt re-registration.'
        )
        confparser.erase('worker_id')
        confparser.erase('worker_secret')

    if args.single:
        log.info(
            'Running in single-task mode, will stop after performing one task.'
        )

    # Find the Manager using UPnP/SSDP if we have no manager_url.
    if not confparser.value('manager_url'):
        from . import ssdp_discover

        try:
            manager_url = ssdp_discover.find_flamenco_manager()
        except ssdp_discover.DiscoveryFailed:
            log.fatal('Unable to find Flamenco Manager via UPnP/SSDP.')
            raise SystemExit(1)

        log.info('Found Flamenco Manager at %s', manager_url)
        confparser.setvalue('manager_url', manager_url)

    # Patch AsyncIO
    from . import patch_asyncio
    patch_asyncio.patch_asyncio()

    # Construct the AsyncIO loop
    loop = construct_asyncio_loop()
    if args.verbose:
        log.debug('Enabling AsyncIO debugging')
        loop.set_debug(True)
    shutdown_future = loop.create_future()

    # Piece all the components together.
    from . import runner, worker, upstream, upstream_update_queue, may_i_run, __version__

    fmanager = upstream.FlamencoManager(
        manager_url=confparser.value('manager_url'),
        flamenco_worker_version=__version__,
    )

    tuqueue = upstream_update_queue.TaskUpdateQueue(
        db_fname=confparser.value('task_update_queue_db'),
        manager=fmanager,
        shutdown_future=shutdown_future,
    )
    trunner = runner.TaskRunner(
        shutdown_future=shutdown_future,
        subprocess_pid_file=confparser.value('subprocess_pid_file'),
    )

    pretask_check_params = parse_pretask_check_config(confparser, log)

    fworker = worker.FlamencoWorker(
        manager=fmanager,
        trunner=trunner,
        tuqueue=tuqueue,
        task_types=confparser.value('task_types').split(),
        worker_id=confparser.value('worker_id'),
        worker_secret=confparser.value('worker_secret'),
        worker_registration_secret=confparser.value(
            'worker_registration_secret'),
        loop=loop,
        shutdown_future=shutdown_future,
        push_log_max_interval=confparser.interval_secs(
            'push_log_max_interval_seconds'),
        push_log_max_entries=confparser.value('push_log_max_entries', int),
        push_act_max_interval=confparser.interval_secs(
            'push_act_max_interval_seconds'),
        initial_state='testing' if args.test else 'awake',
        run_single_task=args.single,
        pretask_check_params=pretask_check_params,
    )

    mir = may_i_run.MayIRun(
        manager=fmanager,
        worker=fworker,
        poll_interval=confparser.interval_secs('may_i_run_interval_seconds'),
        loop=loop,
    )

    def shutdown(signum, stackframe):
        """Perform a clean shutdown."""

        # Raise an exception, so that the exception is bubbled upwards, until
        # the asyncio loop stops executing the current task. Only then can we
        # run things like loop.run_until_complete(mir_work_task).
        log.warning('Shutting down due to signal %i', signum)
        raise KeyboardInterrupt()

    def sleep(signum, stackframe):
        log.warning('Going asleep due to signal %i', signum)
        fworker.go_to_state_asleep()

    def wakeup(signum, stackframe):
        log.warning('Waking up due to signal %i', signum)
        fworker.go_to_state_awake()

    # Shut down cleanly upon TERM signal
    import signal
    signal.signal(signal.SIGTERM, shutdown)
    signal.signal(signal.SIGINT, shutdown)

    if hasattr(signal, 'SIGUSR1'):
        # Windows doesn't have USR1/2 signals.
        signal.signal(signal.SIGUSR1, sleep)
        signal.signal(signal.SIGUSR2, wakeup)

    if hasattr(signal, 'SIGPOLL'):
        # Not sure how cross-platform SIGPOLL is.
        signal.signal(signal.SIGPOLL, asyncio_report_tasks)

    # Start asynchronous tasks.
    asyncio.ensure_future(tuqueue.work(loop=loop))
    mir_work_task = asyncio.ensure_future(mir.work())

    def do_clean_shutdown():
        shutdown_future.cancel()
        mir_work_task.cancel()
        try:
            loop.run_until_complete(asyncio.wait_for(mir_work_task, 5))
        except requests.exceptions.ConnectionError:
            log.warning(
                "Unable to connect to HTTP server, but that's fine as we're shutting down."
            )
        except asyncio.TimeoutError:
            log.debug("Timeout waiting for may-I-run task, "
                      "but that's fine as we're shutting down.")
        except KeyboardInterrupt:
            log.info(
                'Keyboard interrupt while shutting down, ignoring as we are shutting down.'
            )

        fworker.shutdown()

        async def stop_loop():
            log.info('Waiting to give tasks the time to stop gracefully')
            await asyncio.sleep(1)
            loop.stop()

        loop.run_until_complete(stop_loop())

    try:
        loop.run_until_complete(fworker.startup())
        fworker.mainloop()
    except worker.UnableToRegisterError:
        # The worker will have logged something, we'll just shut down cleanly.
        pass
    except KeyboardInterrupt:
        do_clean_shutdown()
    except:
        log.exception('Uncaught exception!')
    else:
        do_clean_shutdown()

    # Report on the asyncio task status
    if args.verbose:
        asyncio_report_tasks()

    log.warning('Closing asyncio loop')
    loop.close()
    log.warning('Flamenco Worker is shut down')
Ejemplo n.º 13
0
            "help - gives this, don't act surprised, what did you expect?\n" +
            "add_twitch_notification (post to channel) (twitch user) " +
            "(message to send)\n")

        if message.content.startswith("!{}".format(config.APP["name"])):
            await message.channel.send(list_of_commands)

    async def logout(self):
        # Whatever is needed to close the feedwithout breaking the whole damn
        # thing
        pass


DEBUG = os.sys.argv[-1] == "--debug"
if DEBUG:
    LOGGER.setLevel(logging.DEBUG)
else:
    LOGGER.setLevel(logging.INFO)

client = Client()
config.load_config()
logging.config.dictConfig(config.LOGGING)

if DEBUG:
    LOGGER.debug("In debug mode")
else:
    try:
        client.run(config.APP["token"])
    except KeyboardInterrupt:
        client.logout()