示例#1
0
def start(argv=None, db_kwargs={}):
    if argv is None:
        argv = sys.argv[1:]
    utils.setup_dirs()
    args = parser.parse_args(argv)
    utils.parse_options(args)

    if not args.only_web:
        db.init(**db_kwargs)
        command.init_commands()
        hlogger.Logger.init_listener(args)
        monkey.patch_all(thread=False)

    hlogger.Logger.setup_logger(args)

    if args.generate_config:
        constants.config.save()
        log.i("Generated configuration file at '{}'".format(
            io_cmd.CoreFS(constants.settings_file).path),
              stdout=True)
        return

    log.i("HPX SERVER START")

    if not args.only_web:
        constants.available_commands = command.get_available_commands()

        services.init_generic_services()

        if not args.safe:
            plugins.plugin_loader(constants.dir_plugin)
        else:
            plugins.registered.init_plugins()

    log.i("Starting webserver... ({}:{})".format(constants.host_web,
                                                 constants.port_web),
          stdout=True)
    web_args = (constants.host_web, constants.port_web,
                constants.dev if args.only_web else False)
    if args.only_web:
        server.WebServer().run(*web_args)
    else:
        Process(target=server.WebServer().run,
                args=web_args,
                kwargs={
                    'logging_queue': hlogger.Logger._queue,
                    'logging_args': args
                },
                daemon=True).start()
        server.HPServer().run(interactive=args.interact)

    if not args.only_web:
        constants.config.save()
        hlogger.Logger.shutdown_listener()
    log.i("HPX SERVER END")
示例#2
0
def main():
    utils.setup_dirs()
    log.setup_logger(dev=constants.dev, debug=config.debug.value)
    try:
        update_info = clsutils.internaldb.update_info.get({})
        if not update_info:
            log.e("No update info registered")
            sys.exit(1)

        if not os.path.exists(update_info.get('from', '')):
            log.e("New release does not exist")
            sys.exit(1)

        if not os.path.exists(update_info.get('to', '')):
            log.e("Application location does not exist")
            sys.exit(1)

        state = constants.UpdateState.Success
        log.i("Installing new release..")
        try:
            move_replace(update_info['from'], update_info['to'])
            log.i("Removing leftovers..")
            shutil.rmtree(update_info['from'])
        except BaseException:
            log.exception("Failed to install new release")
            state = constants.UpdateState.Failed

        if update_info.get('restart', False):
            app = update_info.get('app', '')
            if app:
                args = update_info.get('args', [])
                log.i('Launching application', app, "with args", args)
                atexit.register(launch_app, app,
                                [subprocess.list2cmdline([app, *args])])
            else:
                log.e('No launch application provided')

        update_info['state'] = state.value
        clsutils.internaldb.update_info.set(update_info)
        log.i('Finished with state:', state)
    except BaseException:
        log.exception("Updater failed")
示例#3
0
def start():
    utils.setup_dirs()
    args = parser.parse_args()
    utils.parse_options(args)
    utils.setup_logger(args)

    if args.generate_config:
        constants.config.save()
        print("Generated configuration file at '{}'".format(
            io_cmd.CoreFS(constants.settings_file).path))
        return

    log.i("HPX SERVER START")

    if not args.only_web:
        constants.available_commands = command.get_available_commands()
        constants.core_plugin = plugins._plugin_load(
            "happypanda.core.coreplugin", "core", _logger=log)

        if not args.safe:
            plugins.plugin_loader(constants.dir_plugin)
        else:
            plugins.registered.init_plugins()

    log.i("Starting webserver... ({}:{})".format(constants.host_web,
                                                 constants.port_web),
          stdout=True)
    web_args = (constants.host_web, constants.port_web,
                constants.dev if args.only_web else False)
    if args.only_web:
        server.WebServer().run(*web_args)
    else:
        Process(target=server.WebServer().run, args=web_args,
                daemon=True).start()
        server.HPServer().run(interactive=args.interact)

    if not args.only_web:
        constants.config.save()
    log.i("HPX SERVER END")
示例#4
0
def start(argv=None, db_kwargs={}):
    assert sys.version_info >= (3, 6), "Python 3.6 and up is required"
    e_code = None
    e_num = 0
    try:
        utils.setup_online_reporter()
        log.i("HPX START")
        log.i("Version:", constants.version_str)
        log.i("DB Version:", constants.version_db_str)
        log.i("Web Version:", constants.version_web_str)
        if argv is None:
            argv = sys.argv[1:]
        utils.setup_dirs()
        args = parser.parse_args(argv)
        utils.parse_options(args)
        # setup logger without multiprocessing
        hlogger.Logger.setup_logger(args, main=True, dev=constants.dev, debug=config.debug.value)
        utils.enable_loggers(config.enabled_loggers.value)
        db_inited = False
        if constants.dev:
            log.i("DEVELOPER MODE ENABLED", stdout=True)
        else:
            pdb.set_trace = lambda: None  # disable pdb
        if config.debug.value:
            log.i("DEBUG MODE ENABLED", stdout=True)
        log.i(utils.os_info())

        if not args.only_web:
            db_inited = db.init(**db_kwargs)
            command.setup_commands()
        else:
            db_inited = True

        if cmd_commands(args):
            return

        if not args.only_web:  # can't init earlier because of cmd_commands
            hlogger.Logger.init_listener(args=args, debug=config.debug.value, dev=constants.dev)

        # setup logger with multiprocessing
        hlogger.Logger.setup_logger(
            args,
            main=True,
            dev=constants.dev,
            debug=config.debug.value,
            logging_queue=hlogger.Logger._queue)

        # invalidate all cache
        if constants.dev or config.debug.value:
            for n, c in constants.cache_regions.items():
                c.invalidate()

        update_state = check_update() if not (not constants.is_frozen and constants.dev) else None

        if not update_state == constants.UpdateState.Installing.value and db_inited:

            utils.setup_i18n()

            if not args.only_web:
                constants.available_commands = command.get_available_commands()

                services.setup_generic_services()

                constants.plugin_manager = plugins.PluginManager()

                if not args.safe:
                    plugins.plugin_loader(constants.plugin_manager, constants.dir_plugin)
                    if config.plugin_dir.value:
                        plugins.plugin_loader(constants.plugin_manager, config.plugin_dir.value)

            constants.notification = server.ClientNotifications()

            # starting stuff
            services.Scheduler.generic.start()
            init_commands(args)

            log.i("Starting webserver... ({}:{})".format(config.host_web.value, config.port_web.value), stdout=True)
            web_args = (config.host_web.value, config.port_web.value)
            web_kwargs = {
                'dev': constants.dev,
                'debug': config.debug.value,
            }
            if args.only_web:
                server.WebServer().run(*web_args, **web_kwargs)
            else:
                web_kwargs.update({'logging_queue': hlogger.Logger._queue,
                                   'cmd_args': args, })
                constants.web_proc = Process(target=server.WebServer().run,
                                             args=web_args,
                                             kwargs=web_kwargs,
                                             daemon=True,
                                             name="gevent")
                constants.web_proc.start()
                hp_server = server.HPServer()
                meta_cmd.ShutdownApplication.shutdown.subscribe(hp_server.shutdown)
                meta_cmd.RestartApplication.restart.subscribe(hp_server.restart)
                meta_cmd.UpdateApplication.update.subscribe(hp_server.update)
                e_code = hp_server.run(interactive=args.interact)

        else:
            if db_inited:
                e_code = constants.ExitCode.Update
            else:
                e_code = constants.ExitCode.Exit

        io_cmd.CoreFS(constants.dir_temp).delete(ignore_errors=True)
        log.i("HPX END")

        if e_code == constants.ExitCode.Exit:
            log.i("Shutting down...", stdout=True)
        elif e_code == constants.ExitCode.Restart:
            log.i("Restarting...", stdout=True)
        if not args.only_web:
            config.config.save()
            services.Scheduler.shutdown_all()
            hlogger.Logger.shutdown_listener()

        hlogger.shutdown()

        # the gui will handle the restart
        if e_code == constants.ExitCode.Restart and not constants.from_gui:
            utils.restart_process()
        elif e_code == constants.ExitCode.Update and not constants.from_gui:
            utils.launch_updater()

    except Exception as e:
        if constants.web_proc:
            constants.web_proc.terminate()
        print(e)  # intentional
        e_num = 1
        if not isinstance(e, exceptions.CoreError):
            if config.report_critical_errors.value and not constants.dev and constants.is_frozen:
                rollbar.report_exc_info()
            raise
    return e_code.value if e_code else e_num