Example #1
0
  def _CreateVersionResource(self, service_config, manifest, version_id, build,
                             extra_config_settings):
    """Constructs a Version resource for deployment.

    Args:
      service_config: ServiceYamlInfo, Service info parsed from a service yaml
        file.
      manifest: Dictionary mapping source files to Google Cloud Storage
        locations.
      version_id: str, The version of the service.
      build: BuildArtifact, The build ID or image path. Build ID only supported
        in beta.
      extra_config_settings: dict, client config settings to pass to the server
        as beta settings.

    Returns:
      A Version resource whose Deployment includes either a container pointing
        to a completed image, or a build pointing to an in-progress build.
    """

    parsed_yaml = service_config.parsed.ToYAML()
    config_dict = yaml.load(parsed_yaml)
    try:
      # pylint: disable=protected-access
      schema_parser = convert_yaml.GetSchemaParser(self.client._VERSION)
      json_version_resource = schema_parser.ConvertValue(config_dict)
    except ValueError, e:
      raise exceptions.ConfigError(
          '[{f}] could not be converted to the App Engine configuration '
          'format for the following reason: {msg}'.format(
              f=service_config.file, msg=e.message))
Example #2
0
    def _CreateVersionResource(self, service_config, manifest, version_id,
                               image, endpoints_info):
        """Constructs a Version resource for deployment."""
        appinfo = service_config.parsed

        # TODO(b/29453752): Remove when we want to stop supporting module
        if appinfo.module:
            appinfo.service = appinfo.module
            appinfo.module = None

        parsed_yaml = service_config.parsed.ToYAML()
        config_dict = yaml.safe_load(parsed_yaml)
        try:
            json_version_resource = yaml_schema_v1beta.SCHEMA.ConvertValue(
                config_dict)
        except ValueError, e:
            raise exceptions.ConfigError(
                '[{f}] could not be converted to the App Engine configuration '
                'format for the following reason: {msg}'.format(
                    f=service_config.file, msg=e.message))
  def _CreateVersionResource(
      self, service_config, manifest, version_id, build, endpoints_info):
    """Constructs a Version resource for deployment."""
    appinfo = service_config.parsed

    # TODO(b/29453752): Remove when we want to stop supporting module
    if appinfo.module:
      appinfo.service = appinfo.module
      appinfo.module = None

    parsed_yaml = service_config.parsed.ToYAML()
    config_dict = yaml.safe_load(parsed_yaml)
    try:
      # pylint: disable=protected-access
      schema_parser = convert_yaml.GetSchemaParser(self.client._VERSION)
      json_version_resource = schema_parser.ConvertValue(config_dict)
    except ValueError, e:
      raise exceptions.ConfigError(
          '[{f}] could not be converted to the App Engine configuration '
          'format for the following reason: {msg}'.format(
              f=service_config.file, msg=e.message))
Example #4
0
    def _CreateVersionResource(self, service_config, manifest, version_id,
                               build, endpoints_info, extra_config_settings):
        """Constructs a Version resource for deployment.

    Args:
      service_config: ServiceYamlInfo, Service info parsed from a service yaml
        file.
      manifest: Dictionary mapping source files to Google Cloud Storage
        locations.
      version_id: str, The version of the service.
      build: BuildArtifact, The build ID or image path. Build ID only supported
        in beta.
      endpoints_info: EndpointsServiceInfo, Endpoints service info to be added
        to the AppInfoExternal configuration. Only provided when Endpoints API
        Management feature is enabled.
      extra_config_settings: dict, client config settings to pass to the server
        as beta settings.

    Returns:
      A Version resource whose Deployment includes either a container pointing
        to a completed image, or a build pointing to an in-progress build.
    """
        appinfo = service_config.parsed

        # TODO(b/29453752): Remove when we want to stop supporting module
        if appinfo.module:
            appinfo.service = appinfo.module
            appinfo.module = None

        parsed_yaml = service_config.parsed.ToYAML()
        config_dict = yaml.safe_load(parsed_yaml)
        try:
            # pylint: disable=protected-access
            schema_parser = convert_yaml.GetSchemaParser(self.client._VERSION)
            json_version_resource = schema_parser.ConvertValue(config_dict)
        except ValueError, e:
            raise exceptions.ConfigError(
                '[{f}] could not be converted to the App Engine configuration '
                'format for the following reason: {msg}'.format(
                    f=service_config.file, msg=e.message))
Example #5
0
    def _CreateVersionResource(self,
                               service_config,
                               manifest,
                               version_id,
                               build,
                               extra_config_settings=None,
                               service_account_email=None):
        """Constructs a Version resource for deployment.

    Args:
      service_config: ServiceYamlInfo, Service info parsed from a service yaml
        file.
      manifest: Dictionary mapping source files to Google Cloud Storage
        locations.
      version_id: str, The version of the service.
      build: BuildArtifact, The build ID, image path, or build options.
      extra_config_settings: dict, client config settings to pass to the server
        as beta settings.
      service_account_email: identity of this deployed version. If not set,
        Admin API will fallback to use the App Engine default appspot SA.

    Returns:
      A Version resource whose Deployment includes either a container pointing
        to a completed image, or a build pointing to an in-progress build.
    """
        config_dict = copy.deepcopy(service_config.parsed.ToDict())

        # We always want to set a value for entrypoint when sending the request
        # to Zeus, even if one wasn't specified in the yaml file
        if 'entrypoint' not in config_dict:
            config_dict['entrypoint'] = ''

        try:
            # pylint: disable=protected-access
            schema_parser = convert_yaml.GetSchemaParser(self.client._VERSION)
            json_version_resource = schema_parser.ConvertValue(config_dict)
        except ValueError as e:
            raise exceptions.ConfigError(
                '[{f}] could not be converted to the App Engine configuration '
                'format for the following reason: {msg}'.format(
                    f=service_config.file, msg=six.text_type(e)))
        log.debug('Converted YAML to JSON: "{0}"'.format(
            json.dumps(json_version_resource, indent=2, sort_keys=True)))

        # Override the 'service_account' in app.yaml if CLI provided this param.
        if service_account_email is not None:
            json_version_resource['serviceAccount'] = service_account_email

        json_version_resource['deployment'] = {}
        # Add the deployment manifest information.
        json_version_resource['deployment']['files'] = manifest
        if build:
            if build.IsImage():
                json_version_resource['deployment']['container'] = {
                    'image': build.identifier
                }
            elif build.IsBuildId():
                json_version_resource['deployment']['build'] = {
                    'cloudBuildId': build.identifier
                }
            elif build.IsBuildOptions():
                json_version_resource['deployment']['cloudBuildOptions'] = (
                    build.identifier)
        version_resource = encoding.PyValueToMessage(self.messages.Version,
                                                     json_version_resource)
        # For consistency in the tests:
        if version_resource.envVariables:
            version_resource.envVariables.additionalProperties.sort(
                key=lambda x: x.key)

        # We need to pipe some settings to the server as beta settings.
        if extra_config_settings:
            if 'betaSettings' not in json_version_resource:
                json_version_resource['betaSettings'] = {}
            json_version_resource['betaSettings'].update(extra_config_settings)

        # In the JSON representation, BetaSettings are a dict of key-value pairs.
        # In the Message representation, BetaSettings are an ordered array of
        # key-value pairs. Sort the key-value pairs here, so that unit testing is
        # possible.
        if 'betaSettings' in json_version_resource:
            json_dict = json_version_resource.get('betaSettings')
            attributes = []
            for key, value in sorted(json_dict.items()):
                attributes.append(
                    self.messages.Version.BetaSettingsValue.AdditionalProperty(
                        key=key, value=value))
            version_resource.betaSettings = self.messages.Version.BetaSettingsValue(
                additionalProperties=attributes)

        # The files in the deployment manifest also need to be sorted for unit
        # testing purposes.
        try:
            version_resource.deployment.files.additionalProperties.sort(
                key=operator.attrgetter('key'))
        except AttributeError:  # manifest not present, or no files in manifest
            pass

        # Add an ID for the version which is to be created.
        version_resource.id = version_id
        return version_resource
Example #6
0
    def _CreateVersionResource(self,
                               service_config,
                               manifest,
                               version_id,
                               build,
                               extra_config_settings=None):
        """Constructs a Version resource for deployment.

    Args:
      service_config: ServiceYamlInfo, Service info parsed from a service yaml
        file.
      manifest: Dictionary mapping source files to Google Cloud Storage
        locations.
      version_id: str, The version of the service.
      build: BuildArtifact, The build ID, image path, or build options.
      extra_config_settings: dict, client config settings to pass to the server
        as beta settings.

    Returns:
      A Version resource whose Deployment includes either a container pointing
        to a completed image, or a build pointing to an in-progress build.
    """
        # TODO(b/79871515): Move this import back to the top.
        # pylint: disable=g-import-not-at-top
        from googlecloudsdk.third_party.appengine.admin.tools.conversion import convert_yaml

        parsed_yaml = service_config.parsed.ToYAML()
        config_dict = yaml.load(parsed_yaml)
        try:
            # pylint: disable=protected-access
            schema_parser = convert_yaml.GetSchemaParser(self.client._VERSION)
            json_version_resource = schema_parser.ConvertValue(config_dict)
        except ValueError as e:
            raise exceptions.ConfigError(
                '[{f}] could not be converted to the App Engine configuration '
                'format for the following reason: {msg}'.format(
                    f=service_config.file, msg=e.message))
        log.debug('Converted YAML to JSON: "{0}"'.format(
            json.dumps(json_version_resource, indent=2, sort_keys=True)))

        json_version_resource['deployment'] = {}
        # Add the deployment manifest information.
        json_version_resource['deployment']['files'] = manifest
        if build:
            if build.IsImage():
                json_version_resource['deployment']['container'] = {
                    'image': build.identifier
                }
            elif build.IsBuildId():
                json_version_resource['deployment']['build'] = {
                    'cloudBuildId': build.identifier
                }
            elif build.IsBuildOptions():
                json_version_resource['deployment']['cloudBuildOptions'] = (
                    build.identifier)
        version_resource = encoding.PyValueToMessage(self.messages.Version,
                                                     json_version_resource)

        # We need to pipe some settings to the server as beta settings.
        if extra_config_settings:
            if 'betaSettings' not in json_version_resource:
                json_version_resource['betaSettings'] = {}
            json_version_resource['betaSettings'].update(extra_config_settings)

        # In the JSON representation, BetaSettings are a dict of key-value pairs.
        # In the Message representation, BetaSettings are an ordered array of
        # key-value pairs. Sort the key-value pairs here, so that unit testing is
        # possible.
        if 'betaSettings' in json_version_resource:
            json_dict = json_version_resource.get('betaSettings')
            attributes = []
            for key, value in sorted(json_dict.items()):
                attributes.append(
                    self.messages.Version.BetaSettingsValue.AdditionalProperty(
                        key=key, value=value))
            version_resource.betaSettings = self.messages.Version.BetaSettingsValue(
                additionalProperties=attributes)

        # The files in the deployment manifest also need to be sorted for unit
        # testing purposes.
        try:
            version_resource.deployment.files.additionalProperties.sort(
                key=operator.attrgetter('key'))
        except AttributeError:  # manifest not present, or no files in manifest
            pass

        # Add an ID for the version which is to be created.
        version_resource.id = version_id
        return version_resource
Example #7
0
def DisplayProposedDeployment(app,
                              project,
                              services,
                              configs,
                              version,
                              promote,
                              service_account,
                              api_version='v1'):
  """Prints the details of the proposed deployment.

  Args:
    app: Application resource for the current application (required if any
      services are deployed, otherwise ignored).
    project: The name of the current project.
    services: [deployables.Service], The services being deployed.
    configs: [yaml_parsing.ConfigYamlInfo], The configurations being updated.
    version: The version identifier of the application to be deployed.
    promote: Whether the newly deployed version will receive all traffic
      (this affects deployed URLs).
    service_account: The service account that the deployed version will run as.
    api_version: Version of the yaml file parser to use. Use 'v1' by default.

  Returns:
    dict (str->str), a mapping of service names to deployed service URLs

  This includes information on to-be-deployed services (including service name,
  version number, and deployed URLs) as well as configurations.
  """
  deployed_urls = {}
  if services:
    if app is None:
      raise TypeError('If services are deployed, must provide `app` parameter.')
    log.status.Print('Services to deploy:\n')
    for service in services:
      use_ssl = deploy_command_util.UseSsl(service.service_info)
      url = deploy_command_util.GetAppHostname(
          app=app, service=service.service_id,
          version=None if promote else version, use_ssl=use_ssl)
      deployed_urls[service.service_id] = url
      schema_parser = convert_yaml.GetSchemaParser(api_version)
      service_account_from_yaml = ''
      try:
        service_account_from_yaml = schema_parser.ConvertValue(
            service.service_info.parsed.ToDict()).get('serviceAccount', None)
      except ValueError as e:
        raise exceptions.ConfigError(
            '[{f}] could not be converted to the App Engine configuration '
            'format for the following reason: {msg}'.format(
                f=service.service_info, msg=six.text_type(e)))
      display_service_account = 'App Engine default service account'
      if service_account:
        display_service_account = service_account
      elif service_account_from_yaml:
        display_service_account = service_account_from_yaml
      log.status.Print(
          DEPLOY_SERVICE_MESSAGE_TEMPLATE.format(
              project=project,
              service=service.service_id,
              version=version,
              descriptor=service.descriptor,
              source=service.source,
              url=url,
              service_account=display_service_account))
      if not promote:
        default_url = deploy_command_util.GetAppHostname(
            app=app, service=service.service_id, use_ssl=use_ssl)
        log.status.Print(PROMOTE_MESSAGE_TEMPLATE.format(
            default_url=default_url))

  if configs:
    DisplayProposedConfigDeployments(project, configs)

  return deployed_urls