Esempio n. 1
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
Esempio n. 2
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)

    @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()
    print(READY_MARKER_OUTPUT)
    sys.stdout.flush()

    return thread