Пример #1
0
    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)
Пример #2
0
    def preload_services():
        """
        Preload services - restore persistence, and initialize services if EAGER_SERVICE_LOADING=1.
        """

        # listing the available service plugins will cause resolution of the entry points
        available_services = SERVICE_PLUGINS.list_available()

        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 versions 0.13.0 and above"
                )

            persistence.restore_persisted_data(available_services)

        # lazy is the default beginning with version 0.13.0
        if not config.EAGER_SERVICE_LOADING:
            return

        for api in available_services:
            try:
                SERVICE_PLUGINS.require(api)
            except ServiceDisabled as e:
                LOG.debug("%s", e)
            except Exception:
                LOG.exception("could not load service plugin %s", api)
Пример #3
0
def stop_infra():
    if common.INFRA_STOPPED:
        return
    common.INFRA_STOPPED = True

    event_publisher.fire_event(event_publisher.EVENT_STOP_INFRA)
    analytics.log.event("infra_stop")

    try:
        generic_proxy.QUIET = True
        LOG.debug("[shutdown] Cleaning up services ...")
        SERVICE_PLUGINS.stop_all_services()
        LOG.debug("[shutdown] Cleaning up files ...")
        common.cleanup(files=True, quiet=True)
        LOG.debug("[shutdown] Cleaning up resources ...")
        common.cleanup_resources()

        if config.FORCE_SHUTDOWN:
            LOG.debug(
                "[shutdown] Force shutdown, not waiting for infrastructure to shut down"
            )
            return

        LOG.debug("[shutdown] Waiting for infrastructure to shut down ...")
        wait_for_infra_shutdown()
        LOG.debug("[shutdown] Infrastructure is shut down")
    finally:
        SHUTDOWN_INFRA.set()
Пример #4
0
def stop_infra():
    if common.INFRA_STOPPED:
        return
    # also used to signal shutdown for edge proxy so that any further requests will be rejected
    common.INFRA_STOPPED = True  # TODO: should probably be STOPPING since it isn't stopped yet

    event_publisher.fire_event(event_publisher.EVENT_STOP_INFRA)
    analytics.log.event("infra_stop")

    try:
        generic_proxy.QUIET = True  # TODO: this doesn't seem to be doing anything
        LOG.debug("[shutdown] Cleaning up services ...")
        SERVICE_PLUGINS.stop_all_services()
        LOG.debug("[shutdown] Cleaning up files ...")
        common.cleanup(files=True, quiet=True)
        LOG.debug("[shutdown] Cleaning up resources ...")
        common.cleanup_resources()

        if config.FORCE_SHUTDOWN:
            LOG.debug("[shutdown] Force shutdown, not waiting for infrastructure to shut down")
            return

        LOG.debug("[shutdown] Waiting for infrastructure to shut down ...")
        wait_for_infra_shutdown()
        LOG.debug("[shutdown] Infrastructure is shut down")
    finally:
        SHUTDOWN_INFRA.set()
Пример #5
0
def stop_infra():
    if events.infra_stopping.is_set():
        return
    # also used to signal shutdown for edge proxy so that any further requests will be rejected
    events.infra_stopping.set()

    event_publisher.fire_event(event_publisher.EVENT_STOP_INFRA)
    analytics.log.event("infra_stop")

    try:
        generic_proxy.QUIET = True  # TODO: this doesn't seem to be doing anything
        LOG.debug("[shutdown] Cleaning up services ...")
        SERVICE_PLUGINS.stop_all_services()
        LOG.debug("[shutdown] Cleaning up resources ...")
        cleanup_resources()

        if config.FORCE_SHUTDOWN:
            LOG.debug(
                "[shutdown] Force shutdown, not waiting for infrastructure to shut down"
            )
            return

        LOG.debug("[shutdown] Waiting for infrastructure to shut down ...")
        wait_for_infra_shutdown()
        LOG.debug("[shutdown] Infrastructure is shut down")
    finally:
        events.infra_stopped.set()
Пример #6
0
    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
Пример #7
0
def get_service_stats() -> Dict[str, str]:
    from localstack.services.plugins import SERVICE_PLUGINS

    return {
        service: state.value
        for service, state in SERVICE_PLUGINS.get_states().items()
    }
Пример #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 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()
Пример #10
0
 def restart_alarms(*args):
     poll_condition(lambda: SERVICE_PLUGINS.is_running("cloudwatch"))
     self.alarm_scheduler.restart_existing_alarms()