Python google.appengine.api.app_identity.get_default_version_hostname() Examples

The following are 28 code examples of google.appengine.api.app_identity.get_default_version_hostname(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.api.app_identity , or try the search function .
Example #1
Source File: resultdb.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def create_invocation_async(task_run_id, realm):
  """This is wrapper for CreateInvocation API.

  Returns:
    update-token for created invocation.
  """
  hostname = app_identity.get_default_version_hostname()
  response_headers = {}

  yield _call_resultdb_recorder_api_async(
      'CreateInvocation', {
          'requestId': str(uuid.uuid4()),
          'invocationId': _get_invocation_id(task_run_id),
          'invocation': {
              'producerResource': '//%s/tasks/%s' % (hostname, task_run_id),
              'realm': realm,
          }
      },
      project_id=realm.split(':')[0],
      response_headers=response_headers)
  update_token = response_headers.get('update-token')
  assert update_token, ("response_headers should have valid update-token: %s" %
                        response_headers)
  raise ndb.Return(update_token) 
Example #2
Source File: main.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def bootstrap_templates():
  TEMPLATES_DIR = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'templates')
  template.bootstrap(
      {'templates': TEMPLATES_DIR},
      global_env={
        'hostname': app_identity.get_default_version_hostname()
      }) 
Example #3
Source File: env_utils.py    From upvote with Apache License 2.0 5 votes vote down vote up
def CurrentEnvironment():
  """Returns the current environment the app is running in.

  Returns:
    The DefaultEnv subclass associated with the current environment.

  Raises:
    UnknownEnvironmentError: if the environment cannot be determined.
  """
  logging.info('Attempting to determine current environment')

  # Check the DEFAULT_VERSION_HOSTNAME first.
  logging.info('Checking DEFAULT_VERSION_HOSTNAME')
  if 'DEFAULT_VERSION_HOSTNAME' in os.environ:
    hostname = app_identity.get_default_version_hostname()
    logging.info('DEFAULT_VERSION_HOSTNAME is %s', hostname)
    for env in _ALL_ENVS:
      if env.HOSTNAME == hostname:
        return env
  else:
    logging.info('DEFAULT_VERSION_HOSTNAME not present')

  # Fall back to APPLICATION_ID.
  logging.info('Checking APPLICATION_ID')
  if 'APPLICATION_ID' in os.environ:
    app_id = app_identity.get_application_id()
    logging.info('APPLICATION_ID is %s', app_id)
    for env in _ALL_ENVS:
      if env.PROJECT_ID == app_id:
        return env
  else:
    logging.info('APPLICATION_ID not present')

  # Well shit...
  logging.warning('Unable to determine the current environment')
  raise UnknownEnvironmentError 
Example #4
Source File: taskqueue.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def __host_from_target(target):
    """Calculate the value of the host header from a target.

    Args:
      target: A string representing the target hostname or the constant
          `DEFAULT_APP_VERSION`.

    Returns:
      The string to be used as the host header, or None if it can not be
      determined.
    """
    default_hostname = app_identity.get_default_version_hostname()
    if default_hostname is None:



      return None

    server_software = os.environ.get('SERVER_SOFTWARE', '')
    if target is DEFAULT_APP_VERSION:
      return default_hostname
    elif server_software.startswith(
        'Dev') and server_software != 'Development/1.0 (testbed)':

      target_components = target.rsplit('.', 3)
      module = target_components[-1]
      version = len(target_components) > 1 and target_components[-2] or None
      instance = len(target_components) > 2 and target_components[-3] or None
      try:
        return modules.get_hostname(module=module, version=version,
                                    instance=instance)
      except modules.InvalidModuleError, e:


        if not version:
          return modules.get_hostname(module='default', version=module,
                                      instance=instance)
        else:
          raise e 
Example #5
Source File: taskqueue.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def __target_from_host(host):
    """Calculate the value of the target parameter from a host header.

    Args:
      host: A string representing the hostname for this task.

    Returns:
      A string containing the target of this task, or the constant
      `DEFAULT_APP_VERSION` if it is the default version.

      If this code is running in a unit-test where the environment variable
      `DEFAULT_VERSION_HOSTNAME` is not set then the constant
      `_UNKNOWN_APP_VERSION` is returned.
    """
    default_hostname = app_identity.get_default_version_hostname()
    if default_hostname is None:



      return _UNKNOWN_APP_VERSION

    if host.endswith(default_hostname):

      version_name = host[:-(len(default_hostname) + 1)]
      if version_name:
        return version_name





    return DEFAULT_APP_VERSION 
Example #6
Source File: app_identity_test.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def test_get_default_gcs_bucket_name():
    assert app_identity.get_default_version_hostname() 
Example #7
Source File: app_identity_test.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def test_get_default_version_hostname():
    app_id = app_identity.get_application_id()
    hostname = app_identity.get_default_version_hostname()
    assert hostname
    assert app_id in hostname 
Example #8
Source File: rest_gae.py    From rest_gae with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, ndb.Model):
            obj_dict = obj.to_dict()

            # Each BlobKeyProperty is represented as a dict of upload_url/download_url
            for (name, prop) in obj._properties.iteritems():
                if isinstance(prop, ndb.BlobKeyProperty):
                    server_host = app_identity.get_default_version_hostname()
                    blob_property_url = 'http://%s%s/%s/%s' % (server_host, obj.RESTMeta.base_url, self._decode_key(obj.key), name) # e.g. /api/my_model/<SOME_KEY>/blob_prop
                    obj_dict[name] = {
                            'upload_url': blob_property_url,
                            'download_url': blob_property_url if getattr(obj, name) else None # Display as null if the blob property is not set
                            }



            # Filter the properties that will be returned to user
            included_properties = get_included_properties(obj, 'output')
            obj_dict = dict((k,v) for k,v in obj_dict.iteritems() if k in included_properties)
            # Translate the property names
            obj_dict = translate_property_names(obj_dict, obj, 'output')
            obj_dict['id'] = self._decode_key(obj.key)

            return obj_dict

        elif isinstance(obj, datetime) or isinstance(obj, date) or isinstance(obj, time):
            return obj.isoformat()

        elif isinstance(obj, ndb.Key):
            return self._decode_key(obj)

        elif isinstance(obj, ndb.GeoPt):
            return str(obj)

        else:
            return json.JSONEncoder.default(self, obj) 
Example #9
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_urlfetch_service_id():
  """Returns a value for X-URLFetch-Service-Id header for GAE <-> GAE calls.

  Usually it can be omitted. It is required in certain environments.
  """
  if is_local_dev_server():
    return 'LOCAL'
  hostname = app_identity.get_default_version_hostname().split('.')
  return hostname[-2].upper() if len(hostname) >= 3 else 'APPSPOT' 
Example #10
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_hosturl():
  """Returns the url hostname of this instance locked to the currently running
  version.

  This function hides the fact that app_identity.get_default_version_hostname()
  returns None on the dev server and modules.get_hostname() returns incorrectly
  qualified hostname for HTTPS usage on the prod server. <3
  """
  if is_local_dev_server():
    # TODO(maruel): It'd be nice if it were easier to use a ephemeral SSL
    # certificate here and not assume unsecured connection.
    return 'http://' + modules.get_hostname()

  return 'https://%s-dot-%s' % (
      get_app_version(), app_identity.get_default_version_hostname()) 
Example #11
Source File: gce_vm_auth.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _allowed_audience_re():
  """Returns a regular expression for allowed 'aud' field."""
  return _audience_re(app_identity.get_default_version_hostname())


# Extracted into a separate function for simpler testing. 
Example #12
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_urlfetch_service_id():
  """Returns a value for X-URLFetch-Service-Id header for GAE <-> GAE calls.

  Usually it can be omitted. It is required in certain environments.
  """
  if is_local_dev_server():
    return 'LOCAL'
  hostname = app_identity.get_default_version_hostname().split('.')
  return hostname[-2].upper() if len(hostname) >= 3 else 'APPSPOT' 
Example #13
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_hosturl():
  """Returns the url hostname of this instance locked to the currently running
  version.

  This function hides the fact that app_identity.get_default_version_hostname()
  returns None on the dev server and modules.get_hostname() returns incorrectly
  qualified hostname for HTTPS usage on the prod server. <3
  """
  if is_local_dev_server():
    # TODO(maruel): It'd be nice if it were easier to use a ephemeral SSL
    # certificate here and not assume unsecured connection.
    return 'http://' + modules.get_hostname()

  return 'https://%s-dot-%s' % (
      get_app_version(), app_identity.get_default_version_hostname()) 
Example #14
Source File: gce_vm_auth.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _allowed_audience_re():
  """Returns a regular expression for allowed 'aud' field."""
  return _audience_re(app_identity.get_default_version_hostname())


# Extracted into a separate function for simpler testing. 
Example #15
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_urlfetch_service_id():
  """Returns a value for X-URLFetch-Service-Id header for GAE <-> GAE calls.

  Usually it can be omitted. It is required in certain environments.
  """
  if is_local_dev_server():
    return 'LOCAL'
  hostname = app_identity.get_default_version_hostname().split('.')
  return hostname[-2].upper() if len(hostname) >= 3 else 'APPSPOT' 
Example #16
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_hosturl():
  """Returns the url hostname of this instance locked to the currently running
  version.

  This function hides the fact that app_identity.get_default_version_hostname()
  returns None on the dev server and modules.get_hostname() returns incorrectly
  qualified hostname for HTTPS usage on the prod server. <3
  """
  if is_local_dev_server():
    # TODO(maruel): It'd be nice if it were easier to use a ephemeral SSL
    # certificate here and not assume unsecured connection.
    return 'http://' + modules.get_hostname()

  return 'https://%s-dot-%s' % (
      get_app_version(), app_identity.get_default_version_hostname()) 
Example #17
Source File: gce_vm_auth.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _allowed_audience_re():
  """Returns a regular expression for allowed 'aud' field."""
  return _audience_re(app_identity.get_default_version_hostname())


# Extracted into a separate function for simpler testing. 
Example #18
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_urlfetch_service_id():
  """Returns a value for X-URLFetch-Service-Id header for GAE <-> GAE calls.

  Usually it can be omitted. It is required in certain environments.
  """
  if is_local_dev_server():
    return 'LOCAL'
  hostname = app_identity.get_default_version_hostname().split('.')
  return hostname[-2].upper() if len(hostname) >= 3 else 'APPSPOT' 
Example #19
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_hosturl():
  """Returns the url hostname of this instance locked to the currently running
  version.

  This function hides the fact that app_identity.get_default_version_hostname()
  returns None on the dev server and modules.get_hostname() returns incorrectly
  qualified hostname for HTTPS usage on the prod server. <3
  """
  if is_local_dev_server():
    # TODO(maruel): It'd be nice if it were easier to use a ephemeral SSL
    # certificate here and not assume unsecured connection.
    return 'http://' + modules.get_hostname()

  return 'https://%s-dot-%s' % (
      get_app_version(), app_identity.get_default_version_hostname()) 
Example #20
Source File: handlers_bot.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def post(self):
    res = self._process()
    event = res.request.get('event')
    if event not in self.ALLOWED_EVENTS:
      logging.error('Unexpected event type')
      self.abort_with_error(400, error='Unsupported event type')
    message = res.request.get('message')
    # Record the event in a BotEvent entity so it can be listed on the bot's
    # page. The dimensions won't be applied to BotInfo since they may not be
    # valid, but will be applied to BotEvent for analysis purpose.
    bot_management.bot_event(
        event_type=event,
        bot_id=res.bot_id,
        external_ip=self.request.remote_addr,
        authenticated_as=auth.get_peer_identity().to_bytes(),
        dimensions=res.dimensions,
        state=res.state,
        version=res.version,
        quarantined=bool(res.quarantined_msg),
        maintenance_msg=res.maintenance_msg,
        task_id=None,
        task_name=None,
        message=message,
        register_dimensions=False)

    if event == 'bot_error':
      # Also logs this to ereporter2, so it will be listed in the server's
      # hourly ereporter2 report. THIS IS NOISY so it should only be done with
      # issues requiring action. In this case, include again the bot's URL since
      # there's no context in the report. Redundantly include the bot id so
      # messages are bucketted by bot.
      line = ('%s\n'
              '\nhttps://%s/restricted/bot/%s') % (
                  message, app_identity.get_default_version_hostname(),
                  res.bot_id)
      ereporter2.log_request(self.request, source='bot', message=line)
    self.send_response({})


### Bot Security API RPC handlers 
Example #21
Source File: gce_vm_auth.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _allowed_audience_re():
  """Returns a regular expression for allowed 'aud' field."""
  return _audience_re(app_identity.get_default_version_hostname())


# Extracted into a separate function for simpler testing. 
Example #22
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_urlfetch_service_id():
  """Returns a value for X-URLFetch-Service-Id header for GAE <-> GAE calls.

  Usually it can be omitted. It is required in certain environments.
  """
  if is_local_dev_server():
    return 'LOCAL'
  hostname = app_identity.get_default_version_hostname().split('.')
  return hostname[-2].upper() if len(hostname) >= 3 else 'APPSPOT' 
Example #23
Source File: on_error.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def log(**kwargs):
  """Adds an error. This will indirectly notify the admins.

  Returns the entity id for the report.
  """
  identity = None
  if not auth.get_current_identity().is_anonymous:
    identity = auth.get_current_identity().to_bytes()
  try:
    # Trim all the messages to 4kb to reduce spam.
    LIMIT = 4096
    for key, value in kwargs.items():
      if key not in VALID_ERROR_KEYS:
        logging.error('Dropping unknown detail %s: %s', key, value)
        kwargs.pop(key)
      elif isinstance(value, basestring) and len(value) > LIMIT:
        value = value[:LIMIT-1] + u'\u2026'
        kwargs[key] = value

    if kwargs.get('source') == 'server':
      # Automatically use the version of the server code.
      kwargs.setdefault('version', utils.get_app_version())
      kwargs.setdefault('python_version', platform.python_version())

    error = models.Error(identity=identity, **kwargs)
    error.put()
    key_id = error.key.integer_id()
    # The format of the message is important here. The first line is used to
    # generate a signature, so it must be unique for each category of errors.
    logging.error(
        '%s\n%s\n\nSource: %s\nhttps://%s/restricted/ereporter2/errors/%s',
        error.message, error.stack, error.source,
        app_identity.get_default_version_hostname(), key_id)
    return key_id
  except (datastore_errors.BadValueError, TypeError) as e:
    stack = formatter._reformat_stack(traceback.format_exc())
    # That's the error about the error.
    error = models.Error(
        source='server',
        category='exception',
        message='log(%s) caused: %s' % (kwargs, str(e)),
        exception_type=str(type(e)),
        stack=stack)
    error.put()
    key_id = error.key.integer_id()
    logging.error(
        'Failed to log a %s error\n%s\n%s', error.source, key_id, error.message)
    return key_id 
Example #24
Source File: on_error.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def log(**kwargs):
  """Adds an error. This will indirectly notify the admins.

  Returns the entity id for the report.
  """
  identity = None
  if not auth.get_current_identity().is_anonymous:
    identity = auth.get_current_identity().to_bytes()
  try:
    # Trim all the messages to 4kb to reduce spam.
    LIMIT = 4096
    for key, value in kwargs.items():
      if key not in VALID_ERROR_KEYS:
        logging.error('Dropping unknown detail %s: %s', key, value)
        kwargs.pop(key)
      elif isinstance(value, basestring) and len(value) > LIMIT:
        value = value[:LIMIT-1] + u'\u2026'
        kwargs[key] = value

    if kwargs.get('source') == 'server':
      # Automatically use the version of the server code.
      kwargs.setdefault('version', utils.get_app_version())
      kwargs.setdefault('python_version', platform.python_version())

    error = models.Error(identity=identity, **kwargs)
    error.put()
    key_id = error.key.integer_id()
    # The format of the message is important here. The first line is used to
    # generate a signature, so it must be unique for each category of errors.
    logging.error(
        '%s\n%s\n\nSource: %s\nhttps://%s/restricted/ereporter2/errors/%s',
        error.message, error.stack, error.source,
        app_identity.get_default_version_hostname(), key_id)
    return key_id
  except (datastore_errors.BadValueError, TypeError) as e:
    stack = formatter._reformat_stack(traceback.format_exc())
    # That's the error about the error.
    error = models.Error(
        source='server',
        category='exception',
        message='log(%s) caused: %s' % (kwargs, str(e)),
        exception_type=str(type(e)),
        stack=stack)
    error.put()
    key_id = error.key.integer_id()
    logging.error(
        'Failed to log a %s error\n%s\n%s', error.source, key_id, error.message)
    return key_id 
Example #25
Source File: on_error.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def log(**kwargs):
  """Adds an error. This will indirectly notify the admins.

  Returns the entity id for the report.
  """
  identity = None
  if not auth.get_current_identity().is_anonymous:
    identity = auth.get_current_identity().to_bytes()
  try:
    # Trim all the messages to 4kb to reduce spam.
    LIMIT = 4096
    for key, value in kwargs.items():
      if key not in VALID_ERROR_KEYS:
        logging.error('Dropping unknown detail %s: %s', key, value)
        kwargs.pop(key)
      elif isinstance(value, basestring) and len(value) > LIMIT:
        value = value[:LIMIT-1] + u'\u2026'
        kwargs[key] = value

    if kwargs.get('source') == 'server':
      # Automatically use the version of the server code.
      kwargs.setdefault('version', utils.get_app_version())
      kwargs.setdefault('python_version', platform.python_version())

    error = models.Error(identity=identity, **kwargs)
    error.put()
    key_id = error.key.integer_id()
    # The format of the message is important here. The first line is used to
    # generate a signature, so it must be unique for each category of errors.
    logging.error(
        '%s\n%s\n\nSource: %s\nhttps://%s/restricted/ereporter2/errors/%s',
        error.message, error.stack, error.source,
        app_identity.get_default_version_hostname(), key_id)
    return key_id
  except (datastore_errors.BadValueError, TypeError) as e:
    stack = formatter._reformat_stack(traceback.format_exc())
    # That's the error about the error.
    error = models.Error(
        source='server',
        category='exception',
        message='log(%s) caused: %s' % (kwargs, str(e)),
        exception_type=str(type(e)),
        stack=stack)
    error.put()
    key_id = error.key.integer_id()
    logging.error(
        'Failed to log a %s error\n%s\n%s', error.source, key_id, error.message)
    return key_id 
Example #26
Source File: handlers_bot.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def post(self, task_id=None):
    request = self.parse_body()
    # TODO(crbug.com/1015701): take from X-Luci-Swarming-Bot-ID header.
    bot_id = request.get('id')
    task_id = request.get('task_id', '')
    message = request.get('message', 'unknown')

    # Make sure bot self-reported ID matches the authentication token. Raises
    # auth.AuthorizationError if not.
    bot_auth.validate_bot_id_and_fetch_config(bot_id)

    bot_management.bot_event(
        event_type='task_error',
        bot_id=bot_id,
        external_ip=self.request.remote_addr,
        authenticated_as=auth.get_peer_identity().to_bytes(),
        dimensions=None,
        state=None,
        version=None,
        quarantined=None,
        maintenance_msg=None,
        task_id=task_id,
        task_name=None,
        message=message,
        register_dimensions=False)
    line = ('Bot: https://%s/restricted/bot/%s\n'
            'Task failed: https://%s/user/task/%s\n'
            '%s') % (app_identity.get_default_version_hostname(), bot_id,
                     app_identity.get_default_version_hostname(), task_id,
                     message)
    ereporter2.log_request(self.request, source='bot', message=line)

    msg = log_unexpected_keys(self.EXPECTED_KEYS, request, self.request, 'bot',
                              'keys')
    if msg:
      self.abort_with_error(400, error=msg)

    msg = task_scheduler.bot_terminate_task(
        task_pack.unpack_run_result_key(task_id), bot_id)
    if msg:
      logging.error(msg)
      self.abort_with_error(400, error=msg)
    self.send_response({}) 
Example #27
Source File: backends.py    From python-compat-runtime with Apache License 2.0 4 votes vote down vote up
def get_hostname(backend=None, instance=None):
  """DEPRECATED: Returns the hostname for a backend or backend instance.

  Warning: This API is deprecated and will be removed in a future
  release. Please migrate to the Modules API as soon as possible.

  Args:
    backend: The name of the backend. If None, the current backend will be used.

    instance: An optoinal instance number. If provided, the hostname will
      represent the specific instance. If absent, the hostname will represent
      the backend as a whole.

  Raises:
    InvalidBackendError
    InvalidInstanceError

  Returns:
    The hostname of the backend or backend instance.
  """
  if backend is None:
    backend = get_backend()


  if not isinstance(backend, (str, unicode)):
    raise InvalidBackendError('Invalid backend: %s' % backend)

  if not re.match('^[a-zA-Z0-9\-]+$', backend):
    raise InvalidBackendError('Invalid backend: %s' % backend)


  if instance is not None:
    try:
      instance = int(instance)
    except ValueError:
      raise InvalidInstanceError('instance must be an integer.')


  if _is_dev2_environment():
    return _get_dev2_hostname(backend, instance)
  elif _is_dev_environment():
    return _get_dev_hostname(backend, instance)

  hostname = app_identity.get_default_version_hostname()
  if hostname is None:
    raise DefaultHostnameError

  hostname = '%s.%s' % (backend, hostname)
  if instance is not None:
    hostname = '%d.%s' % (instance, hostname)
  return hostname 
Example #28
Source File: on_error.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def log(**kwargs):
  """Adds an error. This will indirectly notify the admins.

  Returns the entity id for the report.
  """
  identity = None
  if not auth.get_current_identity().is_anonymous:
    identity = auth.get_current_identity().to_bytes()
  try:
    # Trim all the messages to 4kb to reduce spam.
    LIMIT = 4096
    for key, value in kwargs.items():
      if key not in VALID_ERROR_KEYS:
        logging.error('Dropping unknown detail %s: %s', key, value)
        kwargs.pop(key)
      elif isinstance(value, basestring) and len(value) > LIMIT:
        value = value[:LIMIT-1] + u'\u2026'
        kwargs[key] = value

    if kwargs.get('source') == 'server':
      # Automatically use the version of the server code.
      kwargs.setdefault('version', utils.get_app_version())
      kwargs.setdefault('python_version', platform.python_version())

    error = models.Error(identity=identity, **kwargs)
    error.put()
    key_id = error.key.integer_id()
    # The format of the message is important here. The first line is used to
    # generate a signature, so it must be unique for each category of errors.
    logging.error(
        '%s\n%s\n\nSource: %s\nhttps://%s/restricted/ereporter2/errors/%s',
        error.message, error.stack, error.source,
        app_identity.get_default_version_hostname(), key_id)
    return key_id
  except (datastore_errors.BadValueError, TypeError) as e:
    stack = formatter._reformat_stack(traceback.format_exc())
    # That's the error about the error.
    error = models.Error(
        source='server',
        category='exception',
        message='log(%s) caused: %s' % (kwargs, str(e)),
        exception_type=str(type(e)),
        stack=stack)
    error.put()
    key_id = error.key.integer_id()
    logging.error(
        'Failed to log a %s error\n%s\n%s', error.source, key_id, error.message)
    return key_id