Ejemplo n.º 1
0
def _get_api_server(app_id):
  """Return a configured and started api_server.APIServer."""
  tmp_dir = tempfile.mkdtemp()
  os.environ['APPLICATION_ID'] = app_id
  stub_util.setup_stubs(
      request_data=APIRequestInfo(),
      app_id=app_id,
      application_root=os.getcwd(),
      trusted=False,
      appidentity_email_address=None,
      appidentity_private_key_path=None,
      blobstore_path=tmp_dir,
      datastore_consistency=
      datastore_stub_util.PseudoRandomHRConsistencyPolicy(),
      datastore_path=':memory:',
      datastore_require_indexes=False,
      datastore_auto_id_policy=datastore_stub_util.SCATTERED,
      images_host_prefix='http://localhost:8080',
      logs_path=':memory:',
      mail_smtp_host='',
      mail_smtp_port=25,
      mail_smtp_user='',
      mail_smtp_password='',
      mail_enable_sendmail=False,
      mail_show_mail_body=False,
      mail_allow_tls=False,
      search_index_path=None,
      taskqueue_auto_run_tasks=False,
      taskqueue_default_http_server='http://localhost:8080',
      user_login_url='/_ah/login?continue=%s',
      user_logout_url='/_ah/login?continue=%s',
      default_gcs_bucket_name=None)

  server = api_server.APIServer('localhost', 0, app_id)
  server.start()
  return server
Ejemplo n.º 2
0
def _get_api_server(app_id):
    """Return a configured and started api_server.APIServer."""
    tmp_dir = tempfile.mkdtemp()
    os.environ['APPLICATION_ID'] = app_id
    stub_util.setup_stubs(
        request_data=APIRequestInfo(),
        app_id=app_id,
        application_root=os.getcwd(),
        trusted=False,
        appidentity_email_address=None,
        appidentity_private_key_path=None,
        blobstore_path=tmp_dir,
        datastore_consistency=datastore_stub_util.
        PseudoRandomHRConsistencyPolicy(),
        datastore_path=':memory:',
        datastore_require_indexes=False,
        datastore_auto_id_policy=datastore_stub_util.SCATTERED,
        images_host_prefix='http://localhost:8080',
        logs_path=':memory:',
        mail_smtp_host='',
        mail_smtp_port=25,
        mail_smtp_user='',
        mail_smtp_password='',
        mail_enable_sendmail=False,
        mail_show_mail_body=False,
        mail_allow_tls=False,
        search_index_path=None,
        taskqueue_auto_run_tasks=False,
        taskqueue_default_http_server='http://localhost:8080',
        user_login_url='/_ah/login?continue=%s',
        user_logout_url='/_ah/login?continue=%s',
        default_gcs_bucket_name=None)

    server = api_server.APIServer('localhost', 0, app_id)
    server.start()
    return server
Ejemplo n.º 3
0
def create_api_server(request_info, storage_path, options, app_id, app_root):
    """Creates an API server.

  Args:
    request_info: An apiproxy_stub.RequestInfo instance used by the stubs to
      lookup information about the request associated with an API call.
    storage_path: A string directory for storing API stub data.
    options: An instance of argparse.Namespace containing command line flags.
    app_id: String representing an application ID, used for configuring paths
      and string constants in API stubs.
    app_root: The path to the directory containing the user's
      application e.g. "/home/joe/myapp", used for locating application yaml
      files, eg index.yaml for the datastore stub.

  Returns:
    An instance of APIServer.

  Raises:
    DatastoreFileError: Cloud Datastore emulator is used while stub_type is
      datastore_converter.StubTypes.PYTHON_FILE_STUB.
  """
    datastore_path = get_datastore_path(storage_path, options.datastore_path)

    logs_path = options.logs_path or os.path.join(storage_path, 'logs.db')
    search_index_path = options.search_indexes_path or os.path.join(
        storage_path, 'search_indexes')
    blobstore_path = options.blobstore_path or os.path.join(
        storage_path, 'blobs')

    if options.clear_datastore:
        _clear_datastore_storage(datastore_path)
    if options.clear_search_indexes:
        _clear_search_indexes_storage(search_index_path)
    if options.auto_id_policy == datastore_stub_util.SEQUENTIAL:
        logging.warn(
            "--auto_id_policy='sequential' is deprecated. This option "
            "will be removed in a future release.")

    application_address = '%s' % options.host
    if options.port and options.port != 80:
        application_address += ':' + str(options.port)

    user_login_url = '/%s?%s=%%s' % (login.LOGIN_URL_RELATIVE,
                                     login.CONTINUE_PARAM)
    user_logout_url = '/%s?%s=%%s' % (login.LOGOUT_URL_RELATIVE,
                                      login.CONTINUE_PARAM)

    if options.datastore_consistency_policy == 'time':
        consistency = datastore_stub_util.TimeBasedHRConsistencyPolicy()
    elif options.datastore_consistency_policy == 'random':
        consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy()
    elif options.datastore_consistency_policy == 'consistent':
        consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy(1.0)
    else:
        assert 0, ('unknown consistency policy: %r' %
                   options.datastore_consistency_policy)

    stub_type = (datastore_converter.get_stub_type(datastore_path)
                 if os.path.exists(datastore_path) else None)
    gcd_emulator_launching_thread = None
    if options.support_datastore_emulator:
        if stub_type == datastore_converter.StubTypes.PYTHON_FILE_STUB:
            raise DatastoreFileError(
                'The datastore file %s cannot be recognized by dev_appserver. Please '
                'restart dev_appserver with --clear_datastore=1' %
                datastore_path)
        env_emulator_host = os.environ.get('DATASTORE_EMULATOR_HOST')
        if env_emulator_host:  # emulator already running, reuse it.
            logging.warning(
                'Detected environment variable DATASTORE_EMULATOR_HOST=%s, '
                'dev_appserver will speak to the Cloud Datastore emulator running on '
                'this address. The datastore_path %s will be neglected.\nIf you '
                'want datastore to store on %s, remove DATASTORE_EMULATOR_HOST '
                'from environment variables and restart dev_appserver',
                env_emulator_host, datastore_path, datastore_path)
        else:
            gcd_emulator_launching_thread = _launch_gcd_emulator(
                app_id=app_id,
                emulator_port=(options.datastore_emulator_port
                               if options.datastore_emulator_port else
                               portpicker.PickUnusedPort()),
                silent=options.dev_appserver_log_level != 'debug',
                index_file=os.path.join(app_root, 'index.yaml'),
                require_indexes=options.require_indexes,
                datastore_path=datastore_path,
                stub_type=stub_type,
                cmd=options.datastore_emulator_cmd,
                is_test=options.datastore_emulator_is_test_mode)
    else:
        # Use SQLite stub.
        # For historic reason we are still supporting conversion from file stub to
        # SQLite stub data. But this conversion will go away.

        if stub_type == datastore_converter.StubTypes.PYTHON_FILE_STUB:
            datastore_converter.convert_datastore_file_stub_data_to_sqlite(
                app_id, datastore_path)

    stub_util.setup_stubs(
        request_data=request_info,
        app_id=app_id,
        application_root=app_root,
        # The "trusted" flag is only relevant for Google administrative
        # applications.
        trusted=getattr(options, 'trusted', False),
        appidentity_email_address=options.appidentity_email_address,
        appidentity_private_key_path=os.path.abspath(
            options.appidentity_private_key_path)
        if options.appidentity_private_key_path else None,
        blobstore_path=blobstore_path,
        datastore_path=datastore_path,
        datastore_consistency=consistency,
        datastore_require_indexes=options.require_indexes,
        datastore_auto_id_policy=options.auto_id_policy,
        images_host_prefix='http://%s' % application_address,
        logs_path=logs_path,
        mail_smtp_host=options.smtp_host,
        mail_smtp_port=options.smtp_port,
        mail_smtp_user=options.smtp_user,
        mail_smtp_password=options.smtp_password,
        mail_enable_sendmail=options.enable_sendmail,
        mail_show_mail_body=options.show_mail_body,
        mail_allow_tls=options.smtp_allow_tls,
        search_index_path=search_index_path,
        taskqueue_auto_run_tasks=options.enable_task_running,
        taskqueue_default_http_server=application_address,
        user_login_url=user_login_url,
        user_logout_url=user_logout_url,
        default_gcs_bucket_name=options.default_gcs_bucket_name,
        appidentity_oauth_url=options.appidentity_oauth_url,
        datastore_grpc_stub_class=(datastore_grpc_stub.DatastoreGrpcStub
                                   if options.support_datastore_emulator else
                                   None))
    return APIServer(
        options.api_host, options.api_port, app_id,
        options.api_server_supports_grpc or options.support_datastore_emulator,
        options.grpc_api_port, options.enable_host_checking,
        gcd_emulator_launching_thread)
Ejemplo n.º 4
0
def create_api_server(
    request_info, storage_path, options, app_id, app_root):
  """Creates an API server.

  Args:
    request_info: An apiproxy_stub.RequestInfo instance used by the stubs to
      lookup information about the request associated with an API call.
    storage_path: A string directory for storing API stub data.
    options: An instance of argparse.Namespace containing command line flags.
    app_id: String representing an application ID, used for configuring paths
      and string constants in API stubs.
    app_root: The path to the directory containing the user's
      application e.g. "/home/joe/myapp", used for locating application yaml
      files, eg index.yaml for the datastore stub.

  Returns:
    An instance of APIServer.

  Raises:
    DatastoreFileError: Cloud Datastore emulator is used while stub_type is
      datastore_converter.StubTypes.PYTHON_FILE_STUB.
  """
  datastore_path = get_datastore_path(storage_path, options.datastore_path)

  logs_path = options.logs_path or os.path.join(storage_path, 'logs.db')
  search_index_path = options.search_indexes_path or os.path.join(
      storage_path, 'search_indexes')
  blobstore_path = options.blobstore_path or os.path.join(
      storage_path, 'blobs')

  if options.clear_datastore:
    _clear_datastore_storage(datastore_path)
  if options.clear_search_indexes:
    _clear_search_indexes_storage(search_index_path)
  if options.auto_id_policy == datastore_stub_util.SEQUENTIAL:
    logging.warn("--auto_id_policy='sequential' is deprecated. This option "
                 "will be removed in a future release.")

  application_address = '%s' % options.host
  if options.port and options.port != 80:
    application_address += ':' + str(options.port)

  user_login_url = '/%s?%s=%%s' % (
      login.LOGIN_URL_RELATIVE, login.CONTINUE_PARAM)
  user_logout_url = '/%s?%s=%%s' % (
      login.LOGOUT_URL_RELATIVE, login.CONTINUE_PARAM)

  if options.datastore_consistency_policy == 'time':
    consistency = datastore_stub_util.TimeBasedHRConsistencyPolicy()
  elif options.datastore_consistency_policy == 'random':
    consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy()
  elif options.datastore_consistency_policy == 'consistent':
    consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy(1.0)
  else:
    assert 0, ('unknown consistency policy: %r' %
               options.datastore_consistency_policy)

  stub_type = (datastore_converter.get_stub_type(datastore_path)
               if os.path.exists(datastore_path) else None)
  gcd_emulator_launching_thread = None
  if options.support_datastore_emulator:
    if stub_type == datastore_converter.StubTypes.PYTHON_FILE_STUB:
      raise DatastoreFileError(
          'The datastore file %s cannot be recognized by dev_appserver. Please '
          'restart dev_appserver with --clear_datastore=1' % datastore_path)
    # The flag should override environment variable regarding emulator host.
    if options.running_datastore_emulator_host:
      os.environ['DATASTORE_EMULATOR_HOST'] = (
          options.running_datastore_emulator_host)
    env_emulator_host = os.environ.get('DATASTORE_EMULATOR_HOST')
    if env_emulator_host:  # emulator already running, reuse it.
      logging.warning(
          'Detected environment variable DATASTORE_EMULATOR_HOST=%s, '
          'dev_appserver will speak to the Cloud Datastore emulator running on '
          'this address. The datastore_path %s will be neglected.\nIf you '
          'want datastore to store on %s, remove DATASTORE_EMULATOR_HOST '
          'from environment variables and restart dev_appserver',
          env_emulator_host, datastore_path, datastore_path)
    else:
      gcd_emulator_launching_thread = _launch_gcd_emulator(
          app_id=app_id,
          emulator_port=(options.datastore_emulator_port
                         if options.datastore_emulator_port else
                         portpicker.pick_unused_port()),
          silent=options.dev_appserver_log_level != 'debug',
          index_file=os.path.join(app_root, 'index.yaml'),
          require_indexes=options.require_indexes,
          datastore_path=datastore_path,
          stub_type=stub_type,
          cmd=options.datastore_emulator_cmd,
          is_test=options.datastore_emulator_is_test_mode,
          auto_id_policy=options.auto_id_policy)
  else:
    # Use SQLite stub.
    # For historic reason we are still supporting conversion from file stub to
    # SQLite stub data. But this conversion will go away.



    if stub_type == datastore_converter.StubTypes.PYTHON_FILE_STUB:
      datastore_converter.convert_datastore_file_stub_data_to_sqlite(
          app_id, datastore_path)

  stub_util.setup_stubs(
      request_data=request_info,
      app_id=app_id,
      application_root=app_root,
      # The "trusted" flag is only relevant for Google administrative
      # applications.
      trusted=getattr(options, 'trusted', False),
      appidentity_email_address=options.appidentity_email_address,
      appidentity_private_key_path=os.path.abspath(
          options.appidentity_private_key_path)
      if options.appidentity_private_key_path else None,
      blobstore_path=blobstore_path,
      datastore_path=datastore_path,
      datastore_consistency=consistency,
      datastore_require_indexes=options.require_indexes,
      datastore_auto_id_policy=options.auto_id_policy,
      images_host_prefix='http://%s' % application_address,
      logs_path=logs_path,
      mail_smtp_host=options.smtp_host,
      mail_smtp_port=options.smtp_port,
      mail_smtp_user=options.smtp_user,
      mail_smtp_password=options.smtp_password,
      mail_enable_sendmail=options.enable_sendmail,
      mail_show_mail_body=options.show_mail_body,
      mail_allow_tls=options.smtp_allow_tls,
      search_index_path=search_index_path,
      taskqueue_auto_run_tasks=options.enable_task_running,
      taskqueue_default_http_server=application_address,
      user_login_url=user_login_url,
      user_logout_url=user_logout_url,
      default_gcs_bucket_name=options.default_gcs_bucket_name,
      appidentity_oauth_url=options.appidentity_oauth_url,
      datastore_grpc_stub_class=(
          datastore_grpc_stub.DatastoreGrpcStub
          if options.support_datastore_emulator else None)
  )
  return APIServer(
      options.api_host, options.api_port, app_id,
      options.api_server_supports_grpc or options.support_datastore_emulator,
      options.grpc_api_port, options.enable_host_checking,
      gcd_emulator_launching_thread)
Ejemplo n.º 5
0
def create_api_server(request_info, storage_path, options, app_id, app_root):
    """Creates an API server.

  Args:
    request_info: An apiproxy_stub.RequestInfo instance used by the stubs to
      lookup information about the request associated with an API call.
    storage_path: A string directory for storing API stub data.
    options: An instance of argparse.Namespace containing command line flags.
    app_id: String representing an application ID, used for configuring paths
      and string constants in API stubs.
    app_root: The path to the directory containing the user's
      application e.g. "/home/joe/myapp", used for locating application yaml
      files, eg index.yaml for the datastore stub.

  Returns:
    An instance of APIServer.
  """
    emulator_launching_thread = None
    if options.support_datastore_emulator and not os.environ.get(
            'DATASTORE_EMULATOR_HOST'):
        gcd_emulator_port = portpicker.PickUnusedPort()
        emulator_launching_thread = threading.Thread(
            target=GCD_EMULATOR_MANAGER.launch,
            args=[
                gcd_emulator_port, options.dev_appserver_log_level != 'debug',
                os.path.join(app_root, 'index.yaml'), options.require_indexes
            ])
        emulator_launching_thread.start()
        os.environ[
            'DATASTORE_EMULATOR_HOST'] = 'localhost:%d' % gcd_emulator_port

    datastore_path = options.datastore_path or os.path.join(
        storage_path, 'datastore.db')
    logs_path = options.logs_path or os.path.join(storage_path, 'logs.db')
    search_index_path = options.search_indexes_path or os.path.join(
        storage_path, 'search_indexes')
    blobstore_path = options.blobstore_path or os.path.join(
        storage_path, 'blobs')

    if options.clear_datastore:
        _clear_datastore_storage(datastore_path)
    if options.clear_search_indexes:
        _clear_search_indexes_storage(search_index_path)
    if options.auto_id_policy == datastore_stub_util.SEQUENTIAL:
        logging.warn(
            "--auto_id_policy='sequential' is deprecated. This option "
            "will be removed in a future release.")

    application_address = '%s' % options.host
    if options.port and options.port != 80:
        application_address += ':' + str(options.port)

    user_login_url = '/%s?%s=%%s' % (login.LOGIN_URL_RELATIVE,
                                     login.CONTINUE_PARAM)
    user_logout_url = '/%s?%s=%%s' % (login.LOGOUT_URL_RELATIVE,
                                      login.CONTINUE_PARAM)

    if options.datastore_consistency_policy == 'time':
        consistency = datastore_stub_util.TimeBasedHRConsistencyPolicy()
    elif options.datastore_consistency_policy == 'random':
        consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy()
    elif options.datastore_consistency_policy == 'consistent':
        consistency = datastore_stub_util.PseudoRandomHRConsistencyPolicy(1.0)
    else:
        assert 0, ('unknown consistency policy: %r' %
                   options.datastore_consistency_policy)

    # Check if local datastore data should be converted.
    # Using GCD Emulator this could convert python file stub or sqlite stub data
    # to Emulator data format; Without GCD Emulator this converts python file stub
    # to sqlite stub data.
    if os.path.exists(datastore_path):
        data_type = datastore_converter.get_data_type(datastore_path)
        if options.support_datastore_emulator:
            if data_type in [
                    datastore_converter.StubTypes.PYTHON_FILE_STUB,
                    datastore_converter.StubTypes.PYTHON_SQLITE_STUB
            ]:
                if emulator_launching_thread:
                    emulator_launching_thread.join()
                gcd_emulator_host = os.environ.get('DATASTORE_EMULATOR_HOST')
                datastore_converter.convert_python_data_to_emulator(
                    app_id, data_type, datastore_path, gcd_emulator_host)
        else:
            if data_type != datastore_converter.StubTypes.PYTHON_SQLITE_STUB:
                datastore_converter.convert_datastore_file_stub_data_to_sqlite(
                    app_id, datastore_path)

    stub_util.setup_stubs(
        request_data=request_info,
        app_id=app_id,
        application_root=app_root,
        # The "trusted" flag is only relevant for Google administrative
        # applications.
        trusted=getattr(options, 'trusted', False),
        appidentity_email_address=options.appidentity_email_address,
        appidentity_private_key_path=os.path.abspath(
            options.appidentity_private_key_path)
        if options.appidentity_private_key_path else None,
        blobstore_path=blobstore_path,
        datastore_path=datastore_path,
        datastore_consistency=consistency,
        datastore_require_indexes=options.require_indexes,
        datastore_auto_id_policy=options.auto_id_policy,
        images_host_prefix='http://%s' % application_address,
        logs_path=logs_path,
        mail_smtp_host=options.smtp_host,
        mail_smtp_port=options.smtp_port,
        mail_smtp_user=options.smtp_user,
        mail_smtp_password=options.smtp_password,
        mail_enable_sendmail=options.enable_sendmail,
        mail_show_mail_body=options.show_mail_body,
        mail_allow_tls=options.smtp_allow_tls,
        search_index_path=search_index_path,
        taskqueue_auto_run_tasks=options.enable_task_running,
        taskqueue_default_http_server=application_address,
        user_login_url=user_login_url,
        user_logout_url=user_logout_url,
        default_gcs_bucket_name=options.default_gcs_bucket_name,
        appidentity_oauth_url=options.appidentity_oauth_url,
        datastore_grpc_stub_class=(datastore_grpc_stub.DatastoreGrpcStub
                                   if options.support_datastore_emulator else
                                   None))

    if emulator_launching_thread:
        emulator_launching_thread.join()
    return APIServer(
        options.api_host, options.api_port, app_id,
        options.api_server_supports_grpc or options.support_datastore_emulator,
        options.grpc_api_port, options.enable_host_checking)