Example #1
0
def serve(port):
    os.environ[LOCALSTACK_WEB_PROCESS] = '1'
    ensure_webapp_installed()
    load_plugins()
    canonicalize_api_names()

    backend_url = 'http://localhost:%s' % port
    config.USE_SSL = True
    services_infra.start_proxy(config.PORT_WEB_UI_SSL, backend_url)

    generic_proxy.serve_flask_app(app=app, port=port, quiet=True)
Example #2
0
def check_infra(retries=10,
                expect_shutdown=False,
                apis=None,
                additional_checks=[]):
    try:
        apis = apis or canonicalize_api_names()
        print_error = retries <= 0

        # loop through plugins and check service status
        for name, plugin in SERVICE_PLUGINS.items():
            if name in apis:
                check_service_health(api=name,
                                     print_error=print_error,
                                     expect_shutdown=expect_shutdown)

        for additional in additional_checks:
            additional(expect_shutdown=expect_shutdown)
    except Exception as e:
        if retries <= 0:
            LOG.error(
                'Error checking state of local environment (after some retries): %s'
                % traceback.format_exc())
            raise e
        time.sleep(3)
        check_infra(retries - 1,
                    expect_shutdown=expect_shutdown,
                    apis=apis,
                    additional_checks=additional_checks)
Example #3
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()
Example #4
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
Example #5
0
def wait_for_infra_shutdown(apis=None):
    apis = apis or canonicalize_api_names()

    names = [name for name, plugin in SERVICE_PLUGINS.items() if name in apis]

    def check(name):
        check_service_health(api=name, expect_shutdown=True)
        LOG.debug('[shutdown] api %s has shut down', name)

    # no special significance to 10 workers, seems like a reasonable number given the number of services we have
    with ThreadPoolExecutor(max_workers=10) as executor:
        executor.map(check, names)
Example #6
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
Example #7
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()
Example #8
0
 def is_enabled(self, api_names=None):
     if self.default_active:
         return True
     if api_names is None:
         api_names = canonicalize_api_names()
     return self.name() in api_names
Example #9
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
Example #10
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