Esempio n. 1
0
def local(ctx,
          host='127.0.0.1',
          port=8000,
          stage=DEFAULT_STAGE_NAME,
          autoreload=True):
    # type: (click.Context, str, int, str, bool) -> None
    factory = ctx.obj['factory']  # type: CliFactory
    from chalice.cli import reloader
    # We don't create the server here because that will bind the
    # socket and we only want to do this in the worker process.
    server_factory = functools.partial(create_local_server, factory, host,
                                       port, stage)
    # When running `chalice local`, a stdout logger is configured
    # so you'll see the same stdout logging as you would when
    # running in lambda.  This is configuring the root logger.
    # The app-specific logger (app.log) will still continue
    # to work.
    logging.basicConfig(stream=sys.stdout,
                        level=logging.INFO,
                        format='{message}',
                        style='{')
    config = factory.create_config_obj(chalice_stage_name=stage)
    if autoreload:
        project_dir = config.project_dir
        rc = reloader.run_with_reloader(server_factory, os.environ,
                                        project_dir)
        # Click doesn't sys.exit() with the RC this function.  The
        # recommended way to do this is to use sys.exit() directly,
        # see: https://github.com/pallets/click/issues/747
        sys.exit(rc)
    run_local_server(factory, host, port, stage)
Esempio n. 2
0
def local(ctx, host='127.0.0.1', port=8000, stage=DEFAULT_STAGE_NAME,
          autoreload=True):
    # type: (click.Context, str, int, str, bool) -> None
    factory = ctx.obj['factory']  # type: CLIFactory
    from chalice.cli import reloader
    # We don't create the server here because that will bind the
    # socket and we only want to do this in the worker process.
    server_factory = functools.partial(
        create_local_server, factory, host, port, stage)
    # When running `chalice local`, a stdout logger is configured
    # so you'll see the same stdout logging as you would when
    # running in lambda.  This is configuring the root logger.
    # The app-specific logger (app.log) will still continue
    # to work.
    logging.basicConfig(
        stream=sys.stdout, level=logging.INFO, format='%(message)s')
    if autoreload:
        project_dir = factory.create_config_obj(
            chalice_stage_name=stage).project_dir
        rc = reloader.run_with_reloader(
            server_factory, os.environ, project_dir)
        # Click doesn't sys.exit() with the RC this function.  The
        # recommended way to do this is to use sys.exit() directly,
        # see: https://github.com/pallets/click/issues/747
        sys.exit(rc)
    run_local_server(factory, host, port, stage)
Esempio n. 3
0
    def multi_execute(cls, project_dir, workspace, execution_list):
        """ Runs the microservice in a local Chalice emulator.
        """

        # chalice.cli package is not defined in deployment
        from .factory import CwsFactory

        for command, options in execution_list:
            os.environ['WORKSPACE'] = workspace
            command.app.config.load_environment_variables(project_dir)
            if command.app.entries is None:
                command.app.deferred_init(workspace)

            debug = options['debug']
            autoreload = options['autoreload']
            if debug:
                logging.basicConfig(stream=sys.stdout,
                                    level=logging.INFO,
                                    format='%(message)s')

            if autoreload:
                if not options['_from_cws']:
                    sys.argv = [sys.executable, sys.argv[0]]

                class _ThreadedLocalServer(ThreadedLocalServer):
                    def __init__(self):
                        super().__init__()
                        self._app_object = self
                        self._config = Config()
                        self._host = options['host']
                        self._port = options['port']
                        self._server = LocalDevServer(command.app,
                                                      self._config, self._host,
                                                      self._port)

                rc = reloader.run_with_reloader(_ThreadedLocalServer,
                                                os.environ, project_dir)
                sys.exit(rc)
            else:
                factory = CwsFactory(command.app, project_dir, debug=debug)
                run_local_server(factory, options['host'], options['port'],
                                 workspace)