def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = PARSER.parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id_prefix + options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()
    util.setup_environ(app_id)

    # pylint: disable=protected-access
    if options.java_app_base_url:
        # If the user specified a java_app_base_url, then the actual app is running
        # via the classic Java SDK, so we use the appropriate dispatcher that will
        # send requests to the Java app rather than forward them internally to a
        # devappserver2 module.
        dispatcher = _LocalJavaAppDispatcher(
            java_app_base_url=options.java_app_base_url)
    else:
        # TODO: Rename LocalFakeDispatcher or re-implement for
        # api_server.py.
        dispatcher = request_info_lib._LocalFakeDispatcher()
    request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
    # pylint: enable=protected-access

    metrics_logger = metrics.GetMetricsLogger()
    metrics_logger.Start(
        options.google_analytics_client_id,
        user_agent=options.google_analytics_user_agent,
        support_datastore_emulator=options.support_datastore_emulator,
        category=metrics.API_SERVER_CATEGORY)

    # When Cloud Datastore Emulator is invoked from api_server, it should be in
    # test mode, which stores in memory.
    options.datastore_emulator_is_test_mode = True
    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        metrics.GetMetricsLogger().Stop()
        server.quit()
Example #2
0
def main():
  """Parses command line options and launches the API server."""
  shutdown.install_signal_handlers()

  options = PARSER.parse_args()
  logging.getLogger().setLevel(
      constants.LOG_LEVEL_TO_PYTHON_CONSTANT[options.dev_appserver_log_level])

  # Parse the application configuration if config_paths are provided, else
  # provide sensible defaults.
  if options.config_paths:
    app_config = application_configuration.ApplicationConfiguration(
        options.config_paths, options.app_id)
    app_id = app_config.app_id
    app_root = app_config.modules[0].application_root
  else:
    app_id = (options.app_id_prefix + options.app_id if
              options.app_id else DEFAULT_API_SERVER_APP_ID)
    app_root = tempfile.mkdtemp()
  util.setup_environ(app_id)

  # pylint: disable=protected-access
  if options.java_app_base_url:
    # If the user specified a java_app_base_url, then the actual app is running
    # via the classic Java SDK, so we use the appropriate dispatcher that will
    # send requests to the Java app rather than forward them internally to a
    # devappserver2 module.
    dispatcher = _LocalJavaAppDispatcher(
        java_app_base_url=options.java_app_base_url)
  else:
    # TODO: Rename LocalFakeDispatcher or re-implement for
    # api_server.py.
    dispatcher = request_info_lib._LocalFakeDispatcher()
  request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
  # pylint: enable=protected-access

  metrics_logger = metrics.GetMetricsLogger()
  metrics_logger.Start(
      options.google_analytics_client_id,
      user_agent=options.google_analytics_user_agent,
      support_datastore_emulator=options.support_datastore_emulator,
      category=metrics.API_SERVER_CATEGORY)

  # When Cloud Datastore Emulator is invoked from api_server, it should be in
  # test mode, which stores in memory.
  options.datastore_emulator_is_test_mode = True
  server = create_api_server(
      request_info=request_info,
      storage_path=get_storage_path(options.storage_path, app_id),
      options=options, app_id=app_id, app_root=app_root)

  try:
    server.start()
    shutdown.wait_until_shutdown()
  finally:
    metrics.GetMetricsLogger().Stop()
    server.quit()
Example #3
0
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = PARSER.parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id_prefix + options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()
    util.setup_environ(app_id)

    # pylint: disable=protected-access
    if options.java_app_base_url:
        # If the user specified a java_app_base_url, then the actual app is running
        # via the classic Java SDK, so we use the appropriate dispatcher that will
        # send requests to the Java app rather than forward them internally to a
        # devappserver2 module.
        dispatcher = _LocalJavaAppDispatcher(
            java_app_base_url=options.java_app_base_url)
    else:
        # TODO: Rename LocalFakeDispatcher or re-implement for
        # api_server.py.
        dispatcher = request_info_lib._LocalFakeDispatcher()
    request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
    # pylint: enable=protected-access

    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        server.quit()
Example #4
0
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = cli_parser.create_command_line_parser(
        cli_parser.API_SERVER_CONFIGURATION).parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id_prefix + options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()
    util.setup_environ(app_id)

    # pylint: disable=protected-access
    # TODO: Rename LocalFakeDispatcher or re-implement for api_server.py.
    request_info = wsgi_request_info.WSGIRequestInfo(
        request_info_lib._LocalFakeDispatcher())
    # pylint: enable=protected-access

    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        server.quit()
Example #5
0
    def start(self, options):
        """Start devappserver2 servers based on the provided command line arguments.

    Args:
      options: An argparse.Namespace containing the command line arguments.

    Raises:
      PhpPathError: php executable path is not specified for php72.
      MissingDatastoreEmulatorError: dev_appserver.py is not invoked from the right
        directory.
    """
        self._options = options

        self._options.datastore_emulator_cmd = self._correct_datastore_emulator_cmd(
            self._options.datastore_emulator_cmd)
        self._check_datastore_emulator_support()

        logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
            options.dev_appserver_log_level])

        parsed_env_variables = dict(options.env_variables or [])
        configuration = application_configuration.ApplicationConfiguration(
            config_paths=options.config_paths,
            app_id=options.app_id,
            runtime=options.runtime,
            env_variables=parsed_env_variables)
        all_module_runtimes = {
            module.runtime
            for module in configuration.modules
        }
        self._check_platform_support(all_module_runtimes)

        storage_path = api_server.get_storage_path(options.storage_path,
                                                   configuration.app_id)
        datastore_path = api_server.get_datastore_path(storage_path,
                                                       options.datastore_path)
        datastore_data_type = (
            datastore_converter.get_stub_type(datastore_path)
            if os.path.isfile(datastore_path) else None)

        if options.skip_sdk_update_check:
            logging.info('Skipping SDK update check.')
        else:
            update_checker.check_for_updates(configuration)

        # There is no good way to set the default encoding from application code
        # (it needs to be done during interpreter initialization in site.py or
        # sitecustomize.py) so just warn developers if they have a different
        # encoding than production.
        if sys.getdefaultencoding() != constants.PROD_DEFAULT_ENCODING:
            logging.warning(
                'The default encoding of your local Python interpreter is set to %r '
                'while App Engine\'s production environment uses %r; as a result '
                'your code may behave differently when deployed.',
                sys.getdefaultencoding(), constants.PROD_DEFAULT_ENCODING)

        if options.port == 0:
            logging.warn(
                'DEFAULT_VERSION_HOSTNAME will not be set correctly with '
                '--port=0')

        util.setup_environ(configuration.app_id)

        php_version = self._get_php_runtime(configuration)
        if not options.php_executable_path and php_version == 'php72':
            raise PhpPathError(
                'For php72, --php_executable_path must be specified.')

        if options.ssl_certificate_path and options.ssl_certificate_key_path:
            ssl_certificate_paths = self._create_ssl_certificate_paths_if_valid(
                options.ssl_certificate_path, options.ssl_certificate_key_path)
        else:
            if options.ssl_certificate_path or options.ssl_certificate_key_path:
                logging.warn('Must provide both --ssl_certificate_path and '
                             '--ssl_certificate_key_path to enable SSL. Since '
                             'only one flag was provided, not using SSL.')
            ssl_certificate_paths = None

        if options.google_analytics_client_id:
            metrics_logger = metrics.GetMetricsLogger()
            metrics_logger.Start(
                options.google_analytics_client_id,
                options.google_analytics_user_agent,
                all_module_runtimes,
                {module.env or 'standard'
                 for module in configuration.modules},
                options.support_datastore_emulator,
                datastore_data_type,
                bool(ssl_certificate_paths),
                options,
                multi_module=len(configuration.modules) > 1,
                dispatch_config=configuration.dispatch is not None,
            )

        self._dispatcher = dispatcher.Dispatcher(
            configuration, options.host, options.port, options.auth_domain,
            constants.LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
            self._create_php_config(options, php_version),
            self._create_python_config(options),
            self._create_java_config(options), self._create_go_config(options),
            self._create_custom_config(options),
            self._create_cloud_sql_config(options),
            self._create_vm_config(options),
            self._create_module_to_setting(options.max_module_instances,
                                           configuration,
                                           '--max_module_instances'),
            options.use_mtime_file_watcher, options.watcher_ignore_re,
            options.automatic_restart, options.allow_skipped_files,
            self._create_module_to_setting(options.threadsafe_override,
                                           configuration,
                                           '--threadsafe_override'),
            options.external_port, options.specified_service_ports,
            options.enable_host_checking, ssl_certificate_paths)

        wsgi_request_info_ = wsgi_request_info.WSGIRequestInfo(
            self._dispatcher)

        apiserver = api_server.create_api_server(
            wsgi_request_info_, storage_path, options, configuration.app_id,
            configuration.modules[0].application_root)
        apiserver.start()
        self._running_modules.append(apiserver)

        self._dispatcher.start(options.api_host, apiserver.port,
                               wsgi_request_info_)

        xsrf_path = os.path.join(storage_path, 'xsrf')
        admin = admin_server.AdminServer(options.admin_host,
                                         options.admin_port, self._dispatcher,
                                         configuration, xsrf_path,
                                         options.enable_host_checking,
                                         options.enable_console)
        admin.start()
        self._running_modules.append(admin)
        try:
            default = self._dispatcher.get_module_by_name('default')
            apiserver.set_balanced_address(default.balanced_address)
        except request_info.ModuleDoesNotExistError:
            logging.warning('No default module found. Ignoring.')
Example #6
0
    def start(self, options):
        """Start devappserver2 servers based on the provided command line arguments.

    Args:
      options: An argparse.Namespace containing the command line arguments.
    """
        self._options = options

        logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
            options.dev_appserver_log_level])

        parsed_env_variables = dict(options.env_variables or [])
        configuration = application_configuration.ApplicationConfiguration(
            config_paths=options.config_paths,
            app_id=options.app_id,
            runtime=options.runtime,
            env_variables=parsed_env_variables)

        if options.google_analytics_client_id:
            metrics_logger = metrics.GetMetricsLogger()
            metrics_logger.Start(
                options.google_analytics_client_id,
                options.google_analytics_user_agent,
                {module.runtime
                 for module in configuration.modules},
                {module.env or 'standard'
                 for module in configuration.modules})

        if options.skip_sdk_update_check:
            logging.info('Skipping SDK update check.')
        else:
            update_checker.check_for_updates(configuration)

        # There is no good way to set the default encoding from application code
        # (it needs to be done during interpreter initialization in site.py or
        # sitecustomize.py) so just warn developers if they have a different
        # encoding than production.
        if sys.getdefaultencoding() != constants.PROD_DEFAULT_ENCODING:
            logging.warning(
                'The default encoding of your local Python interpreter is set to %r '
                'while App Engine\'s production environment uses %r; as a result '
                'your code may behave differently when deployed.',
                sys.getdefaultencoding(), constants.PROD_DEFAULT_ENCODING)

        if options.port == 0:
            logging.warn(
                'DEFAULT_VERSION_HOSTNAME will not be set correctly with '
                '--port=0')

        util.setup_environ(configuration.app_id)

        self._dispatcher = dispatcher.Dispatcher(
            configuration, options.host, options.port, options.auth_domain,
            constants.LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
            self._create_php_config(options),
            self._create_python_config(options),
            self._create_java_config(options), self._create_go_config(options),
            self._create_custom_config(options),
            self._create_cloud_sql_config(options),
            self._create_vm_config(options),
            self._create_module_to_setting(options.max_module_instances,
                                           configuration,
                                           '--max_module_instances'),
            options.use_mtime_file_watcher, options.watcher_ignore_re,
            options.automatic_restart, options.allow_skipped_files,
            self._create_module_to_setting(options.threadsafe_override,
                                           configuration,
                                           '--threadsafe_override'),
            options.external_port, options.specified_service_ports)

        wsgi_request_info_ = wsgi_request_info.WSGIRequestInfo(
            self._dispatcher)
        storage_path = api_server.get_storage_path(options.storage_path,
                                                   configuration.app_id)

        apiserver = api_server.create_api_server(
            wsgi_request_info_, storage_path, options, configuration.app_id,
            configuration.modules[0].application_root)
        apiserver.start()
        self._running_modules.append(apiserver)

        self._dispatcher.start(options.api_host, apiserver.port,
                               wsgi_request_info_)

        xsrf_path = os.path.join(storage_path, 'xsrf')
        admin = admin_server.AdminServer(options.admin_host,
                                         options.admin_port, self._dispatcher,
                                         configuration, xsrf_path)
        admin.start()
        self._running_modules.append(admin)
        try:
            default = self._dispatcher.get_module_by_name('default')
            apiserver.set_balanced_address(default.balanced_address)
        except request_info.ModuleDoesNotExistError:
            logging.warning('No default module found. Ignoring.')
Example #7
0
  def start(self, options):
    """Start devappserver2 servers based on the provided command line arguments.

    Args:
      options: An argparse.Namespace containing the command line arguments.

    Raises:
      PhpPathError: php executable path is not specified for php72.
      MissingDatastoreEmulatorError: dev_appserver.py is not invoked from the right
        directory.
    """
    self._options = options

    self._correct_datastore_emulator_cmd()
    self._fail_for_using_datastore_emulator_from_legacy_sdk()
    self._decide_use_datastore_emulator()

    logging.getLogger().setLevel(
        constants.LOG_LEVEL_TO_PYTHON_CONSTANT[options.dev_appserver_log_level])

    parsed_env_variables = dict(options.env_variables or [])

    if options.dev_appserver_log_setup_script:
      try:
        execfile(options.dev_appserver_log_setup_script, {}, {})
      except Exception as e:
        logging.exception("Error executing log setup script at %r.",
                          options.dev_appserver_log_setup_script)

    configuration = application_configuration.ApplicationConfiguration(
        config_paths=options.config_paths,
        app_id=options.app_id,
        runtime=options.runtime,
        env_variables=parsed_env_variables)
    all_module_runtimes = {module.runtime for module in configuration.modules}
    self._check_platform_support(all_module_runtimes)

    storage_path = api_server.get_storage_path(
        options.storage_path, configuration.app_id)
    datastore_path = api_server.get_datastore_path(
        storage_path, options.datastore_path)
    datastore_data_type = (datastore_converter.get_stub_type(datastore_path)
                           if os.path.isfile(datastore_path) else None)

    if options.skip_sdk_update_check:
      logging.info('Skipping SDK update check.')
    else:
      update_checker.check_for_updates(configuration)

    # There is no good way to set the default encoding from application code
    # (it needs to be done during interpreter initialization in site.py or
    # sitecustomize.py) so just warn developers if they have a different
    # encoding than production.
    if sys.getdefaultencoding() != constants.PROD_DEFAULT_ENCODING:
      logging.warning(
          'The default encoding of your local Python interpreter is set to %r '
          'while App Engine\'s production environment uses %r; as a result '
          'your code may behave differently when deployed.',
          sys.getdefaultencoding(), constants.PROD_DEFAULT_ENCODING)

    if options.port == 0:
      logging.warn('DEFAULT_VERSION_HOSTNAME will not be set correctly with '
                   '--port=0')

    util.setup_environ(configuration.app_id)

    php_version = self._get_php_runtime(configuration)
    if not options.php_executable_path and php_version == 'php72':
      raise PhpPathError('For php72, --php_executable_path must be specified.')

    if options.ssl_certificate_path and options.ssl_certificate_key_path:
      ssl_certificate_paths = self._create_ssl_certificate_paths_if_valid(
          options.ssl_certificate_path, options.ssl_certificate_key_path)
    else:
      if options.ssl_certificate_path or options.ssl_certificate_key_path:
        logging.warn('Must provide both --ssl_certificate_path and '
                     '--ssl_certificate_key_path to enable SSL. Since '
                     'only one flag was provided, not using SSL.')
      ssl_certificate_paths = None

    if options.google_analytics_client_id:
      metrics_logger = metrics.GetMetricsLogger()
      metrics_logger.Start(
          options.google_analytics_client_id,
          options.google_analytics_user_agent,
          all_module_runtimes,
          {module.env or 'standard' for module in configuration.modules},
          options.support_datastore_emulator, datastore_data_type,
          bool(ssl_certificate_paths), options,
          multi_module=len(configuration.modules) > 1,
          dispatch_config=configuration.dispatch is not None,
          grpc_import_report=self.grpc_import_report,
          java_major_version=self.java_major_version
      )

    self._dispatcher = dispatcher.Dispatcher(
        configuration, options.host, options.port, options.auth_domain,
        constants.LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
        self._create_php_config(options, php_version),
        self._create_python_config(options),
        self._create_java_config(options),
        self._create_go_config(options),
        self._create_custom_config(options),
        self._create_cloud_sql_config(options),
        self._create_vm_config(options),
        self._create_module_to_setting(options.max_module_instances,
                                       configuration, '--max_module_instances'),
        options.use_mtime_file_watcher, options.watcher_ignore_re,
        options.automatic_restart, options.allow_skipped_files,
        self._create_module_to_setting(
            options.threadsafe_override,
            configuration,
            '--threadsafe_override'),
        options.external_port,
        options.specified_service_ports,
        options.enable_host_checking,
        ssl_certificate_paths)

    wsgi_request_info_ = wsgi_request_info.WSGIRequestInfo(self._dispatcher)

    apiserver = api_server.create_api_server(
        wsgi_request_info_, storage_path, options, configuration.app_id,
        configuration.modules[0].application_root)
    apiserver.start()
    self._running_modules.append(apiserver)

    self._dispatcher.start(
        options.api_host, apiserver.port, wsgi_request_info_)

    xsrf_path = os.path.join(storage_path, 'xsrf')
    admin = admin_server.AdminServer(options.admin_host, options.admin_port,
                                     self._dispatcher, configuration, xsrf_path,
                                     options.enable_host_checking,
                                     options.enable_console)
    admin.start()
    self._running_modules.append(admin)

    if options.enable_datastore_translator:
      # We do a late import just in case of any problems with importing
      # protobuf (although in theory python/wrapper_util.py's path-munging has
      # handled them all).
      from google.appengine.tools.devappserver2.datastore_translator import (
        datastore_translator_server)
      translator = datastore_translator_server.DatastoreTranslatorServer(
        options.datastore_translator_host, options.datastore_translator_port,
        options.enable_host_checking)
      translator.start()
      self._running_modules.append(translator)

    try:
      default = self._dispatcher.get_module_by_name('default')
      apiserver.set_balanced_address(default.balanced_address)
    except request_info.ModuleDoesNotExistError:
      logging.warning('No default module found. Ignoring.')