Пример #1
0
def main():
    """
    Serve the LocalstackGateway with the default configuration directly through hypercorn. This is mostly for
    development purposes and documentation on how to serve the Gateway.
    """
    from .serving.hypercorn import serve

    use_ssl = True
    port = 4566

    # serve the LocalStackAwsGateway in a dev app
    from localstack.utils.bootstrap import setup_logging

    setup_logging()

    if use_ssl:
        from localstack.services.generic_proxy import (
            GenericProxy,
            install_predefined_cert_if_available,
        )

        install_predefined_cert_if_available()
        _, cert_file_name, key_file_name = GenericProxy.create_ssl_cert(
            serial_number=port)
        ssl_creds = (cert_file_name, key_file_name)
    else:
        ssl_creds = None

    gw = LocalstackAwsGateway(SERVICE_PLUGINS)

    serve(gw, use_reloader=True, port=port, ssl_creds=ssl_creds)
Пример #2
0
def start_infra(asynchronous=False, apis=None):
    try:
        # load plugins
        load_plugins()

        event_publisher.fire_event(event_publisher.EVENT_START_INFRA,
            {'d': in_docker() and 1 or 0, 'c': in_ci() and 1 or 0})

        # set up logging
        setup_logging()

        # prepare APIs
        apis = canonicalize_api_names(apis)
        # set environment
        os.environ['AWS_REGION'] = config.DEFAULT_REGION
        os.environ['ENV'] = ENV_DEV
        # register signal handlers
        if not os.environ.get(ENV_INTERNAL_TEST_RUN):
            register_signal_handlers()
        # make sure AWS credentials are configured, otherwise boto3 bails on us
        check_aws_credentials()
        # install libs if not present
        install.install_components(apis)
        # Some services take a bit to come up
        sleep_time = 5
        # start services
        thread = None

        if 'elasticsearch' in apis or 'es' in apis:
            sleep_time = max(sleep_time, 10)

        # loop through plugins and start each service
        for name, plugin in SERVICE_PLUGINS.items():
            if name in apis:
                t1 = plugin.start(asynchronous=True)
                thread = thread or t1

        time.sleep(sleep_time)
        # ensure that all infra components are up and running
        check_infra(apis=apis)
        # restore persisted data
        restore_persisted_data(apis=apis)
        print('Ready.')
        sys.stdout.flush()
        if not asynchronous and thread:
            # this is a bit of an ugly hack, but we need to make sure that we
            # stay in the execution context of the main thread, otherwise our
            # signal handlers don't work
            while True:
                time.sleep(1)
        return thread
    except KeyboardInterrupt:
        print('Shutdown')
    except Exception as e:
        print('Error starting infrastructure: %s %s' % (e, traceback.format_exc()))
        sys.stdout.flush()
        raise e
    finally:
        if not asynchronous:
            stop_infra()
Пример #3
0
def do_start_infra(asynchronous, apis, is_in_docker):
    # import to avoid cyclic dependency
    from localstack.services.edge import BOOTSTRAP_LOCK

    event_publisher.fire_event(event_publisher.EVENT_START_INFRA,
        {'d': is_in_docker and 1 or 0, 'c': in_ci() and 1 or 0})

    # set up logging
    setup_logging()

    # prepare APIs
    apis = canonicalize_api_names(apis)

    @log_duration()
    def prepare_environment():
        # set environment
        os.environ['AWS_REGION'] = config.DEFAULT_REGION
        os.environ['ENV'] = ENV_DEV
        # register signal handlers
        if not is_local_test_mode():
            register_signal_handlers()
        # make sure AWS credentials are configured, otherwise boto3 bails on us
        check_aws_credentials()

    @log_duration()
    def prepare_installation():
        # install libs if not present
        install.install_components(apis)

    @log_duration()
    def start_api_services():

        # Some services take a bit to come up
        sleep_time = 5
        # start services
        thread = None

        # loop through plugins and start each service
        for name, plugin in SERVICE_PLUGINS.items():
            if plugin.is_enabled(api_names=apis):
                record_service_health(name, 'starting')
                t1 = plugin.start(asynchronous=True)
                thread = thread or t1

        time.sleep(sleep_time)
        # ensure that all infra components are up and running
        check_infra(apis=apis)
        # restore persisted data
        persistence.restore_persisted_data(apis=apis)
        return thread

    prepare_environment()
    prepare_installation()
    with BOOTSTRAP_LOCK:
        thread = start_api_services()
    print(READY_MARKER_OUTPUT)
    sys.stdout.flush()

    return thread
def main():
    setup_logging()

    # make sure all API names and ports are mapped properly
    canonicalize_api_names()

    # start API
    sys.exit(start_up())
Пример #5
0
def _setup_cli_debug():
    from localstack import config
    from localstack.utils.bootstrap import setup_logging

    config.DEBUG = True
    os.environ["DEBUG"] = "1"

    setup_logging()
Пример #6
0
def start_infra(asynchronous=False, apis=None):
    try:
        os.environ[LOCALSTACK_INFRA_PROCESS] = "1"

        is_in_docker = in_docker()
        # print a warning if we're not running in Docker but using Docker based LAMBDA_EXECUTOR
        if not is_in_docker and "docker" in config.LAMBDA_EXECUTOR and not is_linux():
            print(
                (
                    "!WARNING! - Running outside of Docker with $LAMBDA_EXECUTOR=%s can lead to "
                    "problems on your OS. The environment variable $LOCALSTACK_HOSTNAME may not "
                    "be properly set in your Lambdas."
                )
                % config.LAMBDA_EXECUTOR
            )

        if is_in_docker and not config.LAMBDA_REMOTE_DOCKER and not config.dirs.functions:
            print(
                "!WARNING! - Looks like you have configured $LAMBDA_REMOTE_DOCKER=0 - "
                "please make sure to configure $HOST_TMP_FOLDER to point to your host's $TMPDIR"
            )

        print_runtime_information(is_in_docker)

        # apply patches
        patch_urllib3_connection_pool(maxsize=128)
        patch_instance_tracker_meta()

        # set up logging
        setup_logging()

        # run hooks, to allow them to apply patches and changes
        hooks.on_infra_start.run()
        # with changes that hooks have made, now start the infrastructure
        thread = do_start_infra(asynchronous, apis, is_in_docker)

        if not asynchronous and thread:
            # We're making sure that we stay in the execution context of the
            # main thread, otherwise our signal handlers don't work
            SHUTDOWN_INFRA.wait()

        return thread

    except KeyboardInterrupt:
        print("Shutdown")
    except Exception as e:
        print("Error starting infrastructure: %s %s" % (e, traceback.format_exc()))
        sys.stdout.flush()
        raise e
    finally:
        if not asynchronous:
            stop_infra()
Пример #7
0
def main():
    setup_logging()

    # patch moto implementation
    apply_patches()

    # add memory profiling endpoint
    inject_stats_endpoint()

    # make sure all API names and ports are mapped properly
    canonicalize_api_names()

    # start API
    sys.exit(moto_main())
Пример #8
0
def do_start_infra(asynchronous, apis, is_in_docker):
    event_publisher.fire_event(event_publisher.EVENT_START_INFRA, {
        'd': is_in_docker and 1 or 0,
        'c': in_ci() and 1 or 0
    })

    # set up logging
    setup_logging()

    # prepare APIs
    apis = canonicalize_api_names(apis)
    # set environment
    os.environ['AWS_REGION'] = config.DEFAULT_REGION
    os.environ['ENV'] = ENV_DEV
    # register signal handlers
    if not is_local_test_mode():
        register_signal_handlers()
    # make sure AWS credentials are configured, otherwise boto3 bails on us
    check_aws_credentials()
    # install libs if not present
    install.install_components(apis)
    # Some services take a bit to come up
    sleep_time = 5
    # start services
    thread = None

    # loop through plugins and start each service
    for name, plugin in SERVICE_PLUGINS.items():
        if plugin.is_enabled(api_names=apis):
            record_service_health(name, 'starting')
            t1 = plugin.start(asynchronous=True)
            thread = thread or t1

    time.sleep(sleep_time)
    # ensure that all infra components are up and running
    check_infra(apis=apis)
    # restore persisted data
    persistence.restore_persisted_data(apis=apis)
    print('Ready.')
    sys.stdout.flush()
    if not asynchronous and thread:
        # this is a bit of an ugly hack, but we need to make sure that we
        # stay in the execution context of the main thread, otherwise our
        # signal handlers don't work
        sleep_forever()
    return thread
Пример #9
0
def cli():
    """
    The LocalStack Package Manager (lpm) CLI is a set of commands to install third-party packages used by localstack
    service providers.

    Here are some handy commands:

    List all packages

        python -m localstack.cli.lpm list

    Install DynamoDB Local:

        python -m localstack.cli.install dynamodb-local

    Install all community packages, four in parallel:

        python -m localstack.cli.lpm list | grep "/community" | cut -d'/' -f1 | xargs python -m localstack.cli.lpm install --parallel 4
    """
    setup_logging()
Пример #10
0
def start_infra(asynchronous=False, apis=None):
    events.infra_starting.set()

    try:
        os.environ[LOCALSTACK_INFRA_PROCESS] = "1"

        is_in_docker = in_docker()

        print_runtime_information(is_in_docker)

        # apply patches
        patch_urllib3_connection_pool(maxsize=128)
        patch_instance_tracker_meta()

        # set up logging
        setup_logging()

        # run hooks, to allow them to apply patches and changes
        hooks.on_infra_start.run()
        # with changes that hooks have made, now start the infrastructure
        thread = do_start_infra(asynchronous, apis, is_in_docker)

        if not asynchronous and thread:
            # We're making sure that we stay in the execution context of the
            # main thread, otherwise our signal handlers don't work
            SHUTDOWN_INFRA.wait()

        return thread

    except KeyboardInterrupt:
        print("Shutdown")
    except Exception as e:
        print("Error starting infrastructure: %s %s" %
              (e, traceback.format_exc()))
        sys.stdout.flush()
        raise e
    finally:
        if not asynchronous:
            stop_infra()
Пример #11
0
def main():
    setup_logging()
    port = int(sys.argv[1]) if len(sys.argv) > 0 else MULTI_SERVER_PORT
    start_server(port)
Пример #12
0
def main():
    LOG.info("LocalStack version: %s" % constants.VERSION)
    # set basic CLI commands
    config.CLI_COMMANDS["infra"] = {
        "description": "Commands to manage the infrastructure",
        "function": cmd_infra,
    }
    config.CLI_COMMANDS["start"] = {
        "description": "Shorthand to start the infrastructure",
        "function": cmd_infra,
    }
    config.CLI_COMMANDS["ssh"] = {
        "description": "Shorthand to obtain a shell in the running container",
        "function": cmd_ssh,
    }
    config.CLI_COMMANDS["status"] = {
        "description": "Obtain status details about the installation",
        "function": cmd_status,
    }
    config.CLI_COMMANDS["config"] = {
        "description": "Validate docker configurations",
        "function": cmd_config,
    }

    # load CLI plugins
    bootstrap.load_plugins(scope=bootstrap.PLUGIN_SCOPE_COMMANDS)

    # create final usage string
    additional_params = []
    additional_commands = ""
    for cmd in sorted(config.CLI_COMMANDS.keys()):
        cmd_details = config.CLI_COMMANDS[cmd]
        additional_commands += "\n  %s%s%s" % (
            cmd,
            (20 - len(cmd)) * " ",
            cmd_details["description"],
        )
        for param in cmd_details.get("parameters", []):
            additional_params.append(param)
    additional_params = "\n".join(additional_params)
    doc_string = __doc__ % (additional_commands, additional_params)

    args = docopt(doc_string, options_first=True)

    if args["--version"]:
        print(constants.VERSION)
        sys.exit(0)

    if args["--debug"]:
        config.DEBUG = True
        os.environ["DEBUG"] = "1"

    # set up logging (after DEBUG has been configured)
    setup_logging()

    # invoke subcommand
    argv = [args["<command>"]] + args["<args>"]
    subcommand = config.CLI_COMMANDS.get(args["<command>"])
    if subcommand:
        try:
            subcommand["function"](argv, args)
        except Exception as e:
            if os.environ.get("DEBUG") in ["1", "true"]:
                print(traceback.format_exc())
            print("ERROR: %s" % e)
            sys.exit(1)
    else:
        print('ERROR: Invalid command "%s"' % args["<command>"])
        sys.exit(1)
Пример #13
0
def main():
    setup_logging()
    port = int(sys.argv[1]) if len(sys.argv) > 1 else get_multi_server_port()
    start_server(port)
Пример #14
0
def do_start_infra(asynchronous, apis, is_in_docker):
    event_publisher.fire_event(
        event_publisher.EVENT_START_INFRA,
        {
            "d": is_in_docker and 1 or 0,
            "c": in_ci() and 1 or 0
        },
    )

    # set up logging
    setup_logging()

    if config.DEVELOP:
        install.install_debugpy_and_dependencies()
        import debugpy

        LOG.info("Starting debug server at: %s:%s" %
                 (constants.BIND_HOST, config.DEVELOP_PORT))
        debugpy.listen((constants.BIND_HOST, config.DEVELOP_PORT))

        if config.WAIT_FOR_DEBUGGER:
            debugpy.wait_for_client()

    # prepare APIs
    apis = canonicalize_api_names(apis)
    analytics.log.event("infra_start", apis=apis)

    @log_duration()
    def prepare_environment():
        # set environment
        os.environ["AWS_REGION"] = config.DEFAULT_REGION
        os.environ["ENV"] = ENV_DEV
        # register signal handlers
        if not is_local_test_mode():
            register_signal_handlers()
        # make sure AWS credentials are configured, otherwise boto3 bails on us
        check_aws_credentials()
        patch_moto_request_handling()

    @log_duration()
    def prepare_installation():
        # install libs if not present
        install.install_components(apis)

    @log_duration()
    def preload_services():
        """
        Preload services if EAGER_SERVICE_LOADING is true.
        """
        # TODO: lazy loading should become the default beginning 0.13.0
        if not config.EAGER_SERVICE_LOADING:
            # listing the available service plugins will cause resolution of the entry points
            SERVICE_PLUGINS.list_available()
            return

        apis = list()
        for api in SERVICE_PLUGINS.list_available():
            try:
                SERVICE_PLUGINS.require(api)
                apis.append(api)
            except ServiceDisabled as e:
                LOG.debug("%s", e)
            except Exception:
                LOG.exception("could not load service plugin %s", api)

        if persistence.is_persistence_enabled():
            if not config.is_env_true(constants.ENV_PRO_ACTIVATED):
                LOG.warning(
                    "Persistence mechanism for community services (based on API calls record&replay) will be "
                    "deprecated in 0.13.0 ")

            persistence.restore_persisted_data(apis)

    @log_duration()
    def start_runtime_components():
        from localstack.services.edge import start_edge
        from localstack.services.internal import LocalstackResourceHandler, get_internal_apis

        # serve internal APIs through the generic proxy
        ProxyListener.DEFAULT_LISTENERS.append(
            LocalstackResourceHandler(get_internal_apis()))

        # TODO: we want a composable LocalStack runtime (edge proxy, service manager, dns, ...)
        t = start_thread(start_edge, quiet=False)

        # TODO: properly encapsulate starting/stopping of edge server in a class
        if not poll_condition(
                lambda: is_port_open(config.get_edge_port_http()),
                timeout=5,
                interval=0.1):
            raise TimeoutError(
                f"gave up waiting for edge server on {config.EDGE_BIND_HOST}:{config.EDGE_PORT}"
            )

        return t

    prepare_environment()
    prepare_installation()
    thread = start_runtime_components()
    preload_services()

    if config.DATA_DIR:
        persistence.save_startup_info()

    print(READY_MARKER_OUTPUT)
    sys.stdout.flush()

    INFRA_READY.set()
    analytics.log.event("infra_ready")

    return thread
Пример #15
0
def do_start_infra(asynchronous, apis, is_in_docker):
    # import to avoid cyclic dependency
    from localstack.services.edge import BOOTSTRAP_LOCK

    event_publisher.fire_event(
        event_publisher.EVENT_START_INFRA,
        {
            "d": is_in_docker and 1 or 0,
            "c": in_ci() and 1 or 0
        },
    )

    # set up logging
    setup_logging()

    if config.DEVELOP:
        install.install_debugpy_and_dependencies()
        import debugpy

        LOG.info("Starting debug server at: %s:%s" %
                 (constants.BIND_HOST, config.DEVELOP_PORT))
        debugpy.listen((constants.BIND_HOST, config.DEVELOP_PORT))

        if config.WAIT_FOR_DEBUGGER:
            debugpy.wait_for_client()

    # prepare APIs
    apis = canonicalize_api_names(apis)
    analytics.log.event("infra_start", apis=apis)

    @log_duration()
    def prepare_environment():
        # set environment
        os.environ["AWS_REGION"] = config.DEFAULT_REGION
        os.environ["ENV"] = ENV_DEV
        # register signal handlers
        if not is_local_test_mode():
            register_signal_handlers()
        # make sure AWS credentials are configured, otherwise boto3 bails on us
        check_aws_credentials()

    @log_duration()
    def prepare_installation():
        # install libs if not present
        install.install_components(apis)

    @log_duration()
    def start_api_services():

        # Some services take a bit to come up
        sleep_time = 5
        # start services
        thread = None

        # loop through plugins and start each service
        for name, plugin in SERVICE_PLUGINS.items():
            if plugin.is_enabled(api_names=apis):
                record_service_health(name, "starting")
                t1 = plugin.start(asynchronous=True)
                thread = thread or t1

        time.sleep(sleep_time)
        # ensure that all infra components are up and running
        check_infra(apis=apis)
        # restore persisted data
        record_service_health(
            "features:persistence",
            "initializing" if config.DATA_DIR else "disabled")
        persistence.restore_persisted_data(apis=apis)
        if config.DATA_DIR:
            record_service_health("features:persistence", "initialized")
        return thread

    prepare_environment()
    prepare_installation()
    with BOOTSTRAP_LOCK:
        thread = start_api_services()

        if config.DATA_DIR:
            persistence.save_startup_info()

    print(READY_MARKER_OUTPUT)
    sys.stdout.flush()

    INFRA_READY.set()
    analytics.log.event("infra_ready")

    return thread
Пример #16
0
def main():
    LOG.info('LocalStack version: %s' % constants.VERSION)
    # set basic CLI commands
    config.CLI_COMMANDS['infra'] = {
        'description': 'Commands to manage the infrastructure',
        'function': cmd_infra
    }
    config.CLI_COMMANDS['start'] = {
        'description': 'Shorthand to start the infrastructure',
        'function': cmd_infra
    }
    config.CLI_COMMANDS['web'] = {
        'description': 'Commands to manage the Web dashboard (deprecated)',
        'function': cmd_web
    }
    config.CLI_COMMANDS['ssh'] = {
        'description': 'Shorthand to obtain a shell in the running container',
        'function': cmd_ssh
    }
    config.CLI_COMMANDS['status'] = {
        'description': 'Obtain status details about the installation',
        'function': cmd_status
    }
    config.CLI_COMMANDS['config'] = {
        'description': 'Validate docker configurations',
        'function': cmd_config
    }

    # load CLI plugins
    bootstrap.load_plugins(scope=bootstrap.PLUGIN_SCOPE_COMMANDS)

    # create final usage string
    additional_params = []
    additional_commands = ''
    for cmd in sorted(config.CLI_COMMANDS.keys()):
        cmd_details = config.CLI_COMMANDS[cmd]
        additional_commands += '\n  %s%s%s' % (cmd, (20 - len(cmd)) * ' ', cmd_details['description'])
        for param in cmd_details.get('parameters', []):
            additional_params.append(param)
    additional_params = '\n'.join(additional_params)
    doc_string = __doc__ % (additional_commands, additional_params)

    args = docopt(doc_string, options_first=True)

    if args['--version']:
        print(constants.VERSION)
        sys.exit(0)

    if args['--debug']:
        config.DEBUG = True
        os.environ['DEBUG'] = '1'

    # set up logging (after DEBUG has been configured)
    setup_logging()

    # invoke subcommand
    argv = [args['<command>']] + args['<args>']
    subcommand = config.CLI_COMMANDS.get(args['<command>'])
    if subcommand:
        try:
            subcommand['function'](argv, args)
        except Exception as e:
            if os.environ.get('DEBUG') in ['1', 'true']:
                print(traceback.format_exc())
            print('ERROR: %s' % e)
            sys.exit(1)
    else:
        print('ERROR: Invalid command "%s"' % args['<command>'])
        sys.exit(1)
Пример #17
0
def start_infra(asynchronous=False, apis=None):
    try:
        os.environ[LOCALSTACK_INFRA_PROCESS] = '1'

        is_in_docker = in_docker()
        # print a warning if we're not running in Docker but using Docker based LAMBDA_EXECUTOR
        if not is_in_docker and 'docker' in config.LAMBDA_EXECUTOR and not is_linux(
        ):
            print((
                '!WARNING! - Running outside of Docker with $LAMBDA_EXECUTOR=%s can lead to '
                'problems on your OS. The environment variable $LOCALSTACK_HOSTNAME may not '
                'be properly set in your Lambdas.') % config.LAMBDA_EXECUTOR)

        if is_in_docker and config.LAMBDA_REMOTE_DOCKER and not os.environ.get(
                'HOST_TMP_FOLDER'):
            print(
                '!WARNING! - Looks like you have configured $LAMBDA_REMOTE_DOCKER=1 - '
                "please make sure to configure $HOST_TMP_FOLDER to point to your host's $TMPDIR"
            )

        # apply patches
        patch_urllib3_connection_pool(maxsize=128)

        # load plugins
        load_plugins()

        event_publisher.fire_event(event_publisher.EVENT_START_INFRA, {
            'd': is_in_docker and 1 or 0,
            'c': in_ci() and 1 or 0
        })

        # set up logging
        setup_logging()

        # prepare APIs
        apis = canonicalize_api_names(apis)
        # set environment
        os.environ['AWS_REGION'] = config.DEFAULT_REGION
        os.environ['ENV'] = ENV_DEV
        # register signal handlers
        if not os.environ.get(ENV_INTERNAL_TEST_RUN):
            register_signal_handlers()
        # make sure AWS credentials are configured, otherwise boto3 bails on us
        check_aws_credentials()
        # install libs if not present
        install.install_components(apis)
        # Some services take a bit to come up
        sleep_time = 5
        # start services
        thread = None

        # loop through plugins and start each service
        for name, plugin in SERVICE_PLUGINS.items():
            if plugin.is_enabled(api_names=apis):
                record_service_health(name, 'starting')
                t1 = plugin.start(asynchronous=True)
                thread = thread or t1

        time.sleep(sleep_time)
        # ensure that all infra components are up and running
        check_infra(apis=apis)
        # restore persisted data
        persistence.restore_persisted_data(apis=apis)
        print('Ready.')
        sys.stdout.flush()
        if not asynchronous and thread:
            # this is a bit of an ugly hack, but we need to make sure that we
            # stay in the execution context of the main thread, otherwise our
            # signal handlers don't work
            while True:
                time.sleep(1)
        return thread
    except KeyboardInterrupt:
        print('Shutdown')
    except Exception as e:
        print('Error starting infrastructure: %s %s' %
              (e, traceback.format_exc()))
        sys.stdout.flush()
        raise e
    finally:
        if not asynchronous:
            stop_infra()
Пример #18
0
def main():
    setup_logging()
    # patch moto implementation
    apply_patches()
    # start API
    sys.exit(moto_main())