コード例 #1
0
    def SetTraffic(self, service_ref, config_changes, tracker, asyn,
                   is_managed):
        """Set traffic splits for service."""
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(False),
                interruptable=True,
                abortde_message='aborted')
        serv = self.GetService(service_ref)
        if not serv:
            raise serverless_exceptions.ServiceNotFoundError(
                'Service [{}] could not be found.'.format(
                    service_ref.servicesId))

        if not serv.spec.template:
            if is_managed:
                raise serverless_exceptions.UnsupportedOperationError(
                    'Your provider does not support setting traffic for this service.'
                )
            else:
                raise serverless_exceptions.UnsupportedOperationError(
                    'You must upgrade your cluster to version 0.61 or greater '
                    'to set traffic.')
        self._UpdateOrCreateService(service_ref, config_changes, False, serv)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
コード例 #2
0
    def Run(self, args):
        """Update the service resource.

       Different from `deploy` in that it can only update the service spec but
       no IAM or Cloud build changes.

    Args:
      args: Args!
    Returns:
      googlecloudsdk.api_lib.run.Service, the updated service
    """
        changes = flags.GetServiceConfigurationChanges(args)
        if not changes or (len(changes) == 1 and isinstance(
                changes[0],
                config_changes.SetClientNameAndVersionAnnotationChange)):
            raise exceptions.NoConfigurationChangeError(
                'No configuration change requested. '
                'Did you mean to include the flags `--update-env-vars`, '
                '`--memory`, `--concurrency`, `--timeout`, `--connectivity`, '
                '`--image`?')
        changes.insert(
            0,
            config_changes.DeleteAnnotationChange(
                k8s_object.BINAUTHZ_BREAKGLASS_ANNOTATION))
        changes.append(
            config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack()))

        conn_context = connection_context.GetConnectionContext(
            args, flags.Product.RUN, self.ReleaseTrack())
        service_ref = args.CONCEPTS.service.Parse()
        flags.ValidateResource(service_ref)

        with serverless_operations.Connect(conn_context) as client:
            service = client.GetService(service_ref)
            resource_change_validators.ValidateClearVpcConnector(service, args)
            has_latest = (service is None or traffic.LATEST_REVISION_KEY
                          in service.spec_traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=False, include_route=has_latest)
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                service = client.ReleaseService(service_ref,
                                                changes,
                                                tracker,
                                                asyn=args.async_,
                                                prefetch=service)

            if args.async_:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service.name))
            else:
                service = client.GetService(service_ref)
                pretty_print.Success(
                    messages_util.GetSuccessMessageForSynchronousDeploy(
                        service))
            return service
コード例 #3
0
  def ReleaseService(self, service_ref, config_changes, tracker=None,
                     asyn=False, private_endpoint=None,
                     allow_unauthenticated=False):
    """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      private_endpoint: bool, True if creating a new Service for
        Cloud Run on GKE that should only be addressable from within the
        cluster. False if it should be publicly addressable. None if
        its existing visibility should remain unchanged.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access.
    """
    if tracker is None:
      tracker = progress_tracker.NoOpStagedProgressTracker(
          stages.ServiceStages(), interruptable=True, aborted_message='aborted')
    with_code = any(
        isinstance(c, deployable_pkg.Deployable) for c in config_changes)
    self._UpdateOrCreateService(
        service_ref, config_changes, with_code, private_endpoint)
    if allow_unauthenticated:
      self.AddIamPolicyBinding(service_ref, ['allUsers'], 'roles/run.invoker')
    if not asyn:
      getter = functools.partial(self.GetService, service_ref)
      self.WaitForCondition(ServiceConditionPoller(getter, tracker))
コード例 #4
0
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       private_endpoint=None):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      private_endpoint:
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(),
                interruptable=True,
                aborted_message='aborted')
        with_code = any(
            isinstance(c, deployable_pkg.Deployable) for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_code,
                                    private_endpoint)
        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
コード例 #5
0
  def Run(self, args):
    """Create or Update service from YAML."""
    conn_context = connection_context.GetConnectionContext(
        args, self.ReleaseTrack())

    with serverless_operations.Connect(conn_context) as client:
      new_service = service.Service(
          messages_util.DictToMessageWithErrorCheck(
              args.FILE, client.messages_module.Service),
          client.messages_module)

      # If managed, namespace must match project (or will default to project if
      # not specified).
      # If not managed, namespace simply must not conflict if specified in
      # multiple places (or will default to "default" if not specified).
      namespace = args.CONCEPTS.namespace.Parse().Name()  # From flag or default
      if new_service.metadata.namespace is not None:
        if (args.IsSpecified('namespace') and
            namespace != new_service.metadata.namespace):
          raise exceptions.ConfigurationError(
              'Namespace specified in file does not match passed flag.')
        namespace = new_service.metadata.namespace
        project = properties.VALUES.core.project.Get()
        if flags.IsManaged(args) and namespace != project:
          raise exceptions.ConfigurationError(
              'Namespace must be [{}] for Cloud Run (fully managed).'.format(
                  project))
      new_service.metadata.namespace = namespace

      changes = [config_changes.ReplaceServiceChange(new_service)]
      service_ref = resources.REGISTRY.Parse(
          new_service.metadata.name,
          params={'namespacesId': namespace},
          collection='run.namespaces.services')
      original_service = client.GetService(service_ref)

      pretty_print.Info(deploy.GetStartDeployMessage(conn_context, service_ref))

      deployment_stages = stages.ServiceStages()
      header = (
          'Deploying...' if original_service else 'Deploying new service...')
      with progress_tracker.StagedProgressTracker(
          header,
          deployment_stages,
          failure_message='Deployment failed',
          suppress_output=args.async_) as tracker:
        client.ReleaseService(
            service_ref,
            changes,
            tracker,
            asyn=args.async_,
            allow_unauthenticated=None,
            for_replace=True)
      if args.async_:
        pretty_print.Success(
            'Service [{{bold}}{serv}{{reset}}] is deploying '
            'asynchronously.'.format(serv=service_ref.servicesId))
      else:
        pretty_print.Success(deploy.GetSuccessMessageForSynchronousDeploy(
            client, service_ref))
コード例 #6
0
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       allow_unauthenticated=None):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement Adjust().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access. False if removing the IAM policy to allow
        unauthenticated access from a service.
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(allow_unauthenticated is not None),
                interruptable=True,
                aborted_message='aborted')
        with_image = any(
            isinstance(c, config_changes_mod.ImageChange)
            for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_image)

        if allow_unauthenticated is not None:
            try:
                tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
                tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
                self.AddOrRemoveIamPolicyBinding(
                    service_ref, allow_unauthenticated,
                    ALLOW_UNAUTH_POLICY_BINDING_MEMBERS,
                    ALLOW_UNAUTH_POLICY_BINDING_ROLE)
                tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
            except api_exceptions.HttpError:
                warning_message = (
                    'Setting IAM policy failed, try "gcloud beta run services '
                    '{}-iam-policy-binding --region={region} --member=allUsers '
                    '--role=roles/run.invoker {service}"'.format(
                        'add' if allow_unauthenticated else 'remove',
                        region=self._region,
                        service=service_ref.servicesId))
                tracker.CompleteStageWithWarning(
                    stages.SERVICE_IAM_POLICY_SET,
                    warning_message=warning_message)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            poller = ServiceConditionPoller(getter, tracker)
            self.WaitForCondition(poller)
            for msg in run_condition.GetNonTerminalMessages(
                    poller.GetConditions()):
                tracker.AddWarning(msg)
コード例 #7
0
ファイル: update.py プロジェクト: superina5/first_app
    def Run(self, args):
        """Update configuration information about the service.

    Does not change the running code.

    Args:
      args: Args!
    """
        conn_context = connection_context.GetConnectionContext(args)
        service_ref = flags.GetService(args)

        if conn_context.supports_one_platform:
            flags.VerifyOnePlatformFlags(args)
        else:
            flags.VerifyGKEFlags(args)

        with serverless_operations.Connect(conn_context) as client:
            changes = flags.GetConfigurationChanges(args)
            endpoint_visibility = flags.GetEndpointVisibility(args)
            allow_unauth = None
            if conn_context.supports_one_platform:
                allow_unauth = flags.GetAllowUnauthenticated(
                    args, client, service_ref)
            if not changes and endpoint_visibility is None and allow_unauth is None:
                raise exceptions.NoConfigurationChangeError(
                    'No configuration change requested. '
                    'Did you mean to include the flags `--update-env-vars`, '
                    '`--memory`, `--concurrency`, `--timeout`, `--connectivity`, '
                    'or `--allow-unauthenticated`?')
            deployment_stages = stages.ServiceStages(allow_unauth is not None)
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                client.ReleaseService(service_ref,
                                      changes,
                                      tracker,
                                      asyn=args. async,
                                      private_endpoint=endpoint_visibility,
                                      allow_unauthenticated=allow_unauth)
            if args. async:
                pretty_print.Success('Deploying asynchronously.')
            else:
                url = client.GetServiceUrl(service_ref)
                active_revs = client.GetActiveRevisions(service_ref)

                msg = (
                    '{{bold}}Service [{serv}] revision{plural} {rev_msg} is active'
                    ' and serving traffic at{{reset}} {url}')

                rev_msg = ' '.join(['[{}]'.format(rev) for rev in active_revs])

                msg = msg.format(serv=service_ref.servicesId,
                                 plural='s' if len(active_revs) > 1 else '',
                                 rev_msg=rev_msg,
                                 url=url)

                pretty_print.Success(msg)
コード例 #8
0
  def Run(self, args):
    """Deploy a container to Cloud Run."""
    service_ref = flags.GetService(args)
    image = args.image
    # Build an image from source if source specified.
    if flags.FlagIsExplicitlySet(args, 'source'):
      # Create a tag for the image creation
      if image is None and not args.IsSpecified('config'):
        image = 'gcr.io/{projectID}/cloud-run-source-deploy/{service}:{tag}'.format(
            projectID=properties.VALUES.core.project.Get(required=True),
            service=service_ref.servicesId,
            tag=uuid.uuid4().hex)
      messages = cloudbuild_util.GetMessagesModule()
      build_config = submit_util.CreateBuildConfig(
          image, args.no_cache, messages, args.substitutions, args.config,
          args.IsSpecified('source'), False, args.source,
          args.gcs_source_staging_dir, args.ignore_file, args.gcs_log_dir,
          args.machine_type, args.disk_size)
      submit_util.Build(messages, args.async_, build_config)

    # Deploy a container with an image
    conn_context = connection_context.GetConnectionContext(
        args, flags.Product.RUN, self.ReleaseTrack())
    config_changes = flags.GetConfigurationChanges(args)

    with serverless_operations.Connect(conn_context) as operations:
      image_change = config_changes_mod.ImageChange(image)
      changes = [image_change]
      if config_changes:
        changes.extend(config_changes)
      service = operations.GetService(service_ref)
      allow_unauth = GetAllowUnauth(args, operations, service_ref, service)

      pretty_print.Info(GetStartDeployMessage(conn_context, service_ref))
      has_latest = (service is None or
                    traffic.LATEST_REVISION_KEY in service.spec_traffic)
      deployment_stages = stages.ServiceStages(
          include_iam_policy_set=allow_unauth is not None,
          include_route=has_latest)
      header = 'Deploying...' if service else 'Deploying new service...'
      with progress_tracker.StagedProgressTracker(
          header,
          deployment_stages,
          failure_message='Deployment failed',
          suppress_output=args.async_) as tracker:
        operations.ReleaseService(
            service_ref,
            changes,
            tracker,
            asyn=args.async_,
            allow_unauthenticated=allow_unauth,
            prefetch=service)
      if args.async_:
        pretty_print.Success(
            'Service [{{bold}}{serv}{{reset}}] is deploying '
            'asynchronously.'.format(serv=service_ref.servicesId))
      else:
        pretty_print.Success(GetSuccessMessageForSynchronousDeploy(
            operations, service_ref))
コード例 #9
0
    def Run(self, args):
        """Update configuration information about the service.

    Does not change the running code.

    Args:
      args: Args!
    """
        changes = flags.GetConfigurationChanges(args)
        if not changes:
            raise exceptions.NoConfigurationChangeError(
                'No configuration change requested. '
                'Did you mean to include the flags `--update-env-vars`, '
                '`--memory`, `--concurrency`, `--timeout`, `--connectivity`?')

        conn_context = connection_context.GetConnectionContext(
            args, product=connection_context.Product.RUN)
        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as client:
            service = client.GetService(service_ref)
            has_latest = (service is None
                          or traffic.LATEST_REVISION_KEY in service.traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=False, include_route=has_latest)
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                client.ReleaseService(service_ref,
                                      changes,
                                      tracker,
                                      asyn=args.async_,
                                      prefetch=service)
            if args.async_:
                pretty_print.Success('Deploying asynchronously.')
            else:
                service = client.GetService(service_ref)
                latest_ready = service.status.latestReadyRevisionName
                latest_percent_traffic = sum(
                    target.percent for target in service.status.traffic
                    if target.latestRevision or (
                        latest_ready and target.revisionName == latest_ready))
                msg = (
                    'Service [{{bold}}{serv}{{reset}}] '
                    'revision [{{bold}}{rev}{{reset}}] '
                    'has been deployed and is serving '
                    '{{bold}}{latest_percent_traffic}{{reset}} percent of traffic'
                )
                if latest_percent_traffic:
                    msg += (' at {{bold}}{url}{{reset}}')
                msg = msg.format(serv=service_ref.servicesId,
                                 rev=latest_ready,
                                 url=service.domain
                                 if 'domain' in dir(service) else service.url,
                                 latest_percent_traffic=latest_percent_traffic)
                pretty_print.Success(msg)
コード例 #10
0
    def Run(self, args):
        """Update configuration information about the service.

    Does not change the running code.

    Args:
      args: Args!
    """
        changes = flags.GetConfigurationChanges(args)
        if not changes:
            raise exceptions.NoConfigurationChangeError(
                'No configuration change requested. '
                'Did you mean to include the flags `--update-env-vars`, '
                '`--memory`, `--concurrency`, `--timeout`, `--connectivity`?')

        conn_context = connection_context.GetConnectionContext(
            args, product=connection_context.Product.RUN)
        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as client:
            service = client.GetService(service_ref)
            has_latest = (service is None
                          or traffic.LATEST_REVISION_KEY in service.traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=False, include_route=has_latest)
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                client.ReleaseService(service_ref,
                                      changes,
                                      tracker,
                                      asyn=args.async_,
                                      prefetch=service)
            if args.async_:
                pretty_print.Success('Deploying asynchronously.')
            else:
                service = client.GetService(service_ref)
                active_revs = client.GetActiveRevisions(service_ref)

                msg = (
                    'Service [{{bold}}{serv}{{reset}}] revision{plural} {rev_msg} '
                    'is active and serving traffic at {{bold}}{url}{{reset}}')

                rev_msg = ' '.join([
                    '[{{bold}}{}{{reset}}]'.format(rev) for rev in active_revs
                ])

                msg = msg.format(serv=service_ref.servicesId,
                                 plural='s' if len(active_revs) > 1 else '',
                                 rev_msg=rev_msg,
                                 url=service.domain
                                 if 'domain' in dir(service) else service.url)

                pretty_print.Success(msg)
コード例 #11
0
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       private_endpoint=None,
                       allow_unauthenticated=False):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      private_endpoint: bool, True if creating a new Service for
        Cloud Run on GKE that should only be addressable from within the
        cluster. False if it should be publicly addressable. None if
        its existing visibility should remain unchanged.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access.
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(allow_unauthenticated),
                interruptable=True,
                aborted_message='aborted')
        with_image = any(
            isinstance(c, config_changes_mod.ImageChange)
            for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_image,
                                    private_endpoint)

        if allow_unauthenticated:
            try:
                tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
                tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
                self.AddIamPolicyBinding(service_ref, ['allUsers'],
                                         'roles/run.invoker')
                tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
            except api_exceptions.HttpError:
                warning_message = (
                    'Setting IAM policy failed, try "gcloud beta run services '
                    'add-iam-policy-binding --region=%s --member=allUsers '
                    '--role=roles/run.invoker %s"') % (self._region,
                                                       service_ref.servicesId)
                tracker.CompleteStageWithWarning(
                    stages.SERVICE_IAM_POLICY_SET,
                    warning_message=warning_message)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
コード例 #12
0
    def Run(self, args):
        """Create or Update service from YAML."""
        conn_context = connection_context.GetConnectionContext(args)
        if conn_context.supports_one_platform:
            flags.VerifyOnePlatformFlags(args)
        else:
            flags.VerifyGKEFlags(args)

        with serverless_operations.Connect(conn_context) as client:
            message_dict = yaml.load_path(args.FILE)
            new_service = service.Service(
                messages_util.DictToMessageWithErrorCheck(
                    message_dict, client.messages_module.Service),
                client.messages_module)

            changes = [config_changes.ReplaceServiceChange(new_service)]
            service_ref = resources.REGISTRY.Parse(
                new_service.metadata.name,
                params={'namespacesId': new_service.metadata.namespace},
                collection='run.namespaces.services')
            original_service = client.GetService(service_ref)

            pretty_print.Info(
                deploy.GetStartDeployMessage(conn_context, service_ref))

            deployment_stages = stages.ServiceStages()
            header = ('Deploying...'
                      if original_service else 'Deploying new service...')
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                client.ReleaseService(service_ref,
                                      changes,
                                      tracker,
                                      asyn=args. async,
                                      allow_unauthenticated=None,
                                      for_replace=True)
            if args. async:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                pretty_print.Success(
                    deploy.GetSuccessMessageForSynchronousDeploy(
                        client, service_ref))
コード例 #13
0
ファイル: deploy.py プロジェクト: OzLustig/MyDay
    def Run(self, args):
        """Deploy a container to Cloud Run."""
        image = args.image

        conn_context = connection_context.GetConnectionContext(
            args, product=connection_context.Product.RUN)
        config_changes = flags.GetConfigurationChanges(args)

        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as operations:
            image_change = config_changes_mod.ImageChange(image)
            changes = [image_change]
            if config_changes:
                changes.extend(config_changes)
            service = operations.GetService(service_ref)
            allow_unauth = GetAllowUnauth(args, operations, service_ref,
                                          service)

            pretty_print.Info(GetStartDeployMessage(conn_context, service_ref))
            has_latest = (service is None
                          or traffic.LATEST_REVISION_KEY in service.traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=allow_unauth is not None,
                include_route=has_latest)
            header = 'Deploying...' if service else 'Deploying new service...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                operations.ReleaseService(service_ref,
                                          changes,
                                          tracker,
                                          asyn=args.async_,
                                          allow_unauthenticated=allow_unauth,
                                          prefetch=service)
            if args.async_:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                pretty_print.Success(
                    GetSuccessMessageForSynchronousDeploy(
                        operations, service_ref))
コード例 #14
0
ファイル: update.py プロジェクト: NiyonikaS/HackXX2019
    def Run(self, args):
        """Update configuration information about the service.

    Does not change the running code.

    Args:
      args: Args!
    """
        conn_context = connection_context.GetConnectionContext(args)
        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as client:
            changes = flags.GetConfigurationChanges(args)
            if not changes:
                raise exceptions.NoConfigurationChangeError(
                    'No configuration change requested. '
                    'Did you mean to include the flags `--update-env-vars`, '
                    '`--memory`, `--concurrency`, or `--timeout`?')
            deployment_stages = stages.ServiceStages()
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                client.ReleaseService(service_ref, changes, tracker,
                                      args. async)
            if args. async:
                pretty_print.Success('Deploying asynchronously.')
            else:
                url = client.GetServiceUrl(service_ref)
                active_revs = client.GetActiveRevisions(service_ref)

                msg = (
                    '{{bold}}Service [{serv}] revision{plural} {rev_msg} is active'
                    ' and serving traffic at{{reset}} {url}')

                rev_msg = ' '.join(['[{}]'.format(rev) for rev in active_revs])

                msg = msg.format(serv=service_ref.servicesId,
                                 plural='s' if len(active_revs) > 1 else '',
                                 rev_msg=rev_msg,
                                 url=url)

                pretty_print.Success(msg)
コード例 #15
0
ファイル: update.py プロジェクト: iofh/QA-System
    def Run(self, args):
        """Update configuration information about the service.

    Does not change the running code.

    Args:
      args: Args!
    """
        changes = flags.GetConfigurationChanges(args)
        if not changes:
            raise exceptions.NoConfigurationChangeError(
                'No configuration change requested. '
                'Did you mean to include the flags `--update-env-vars`, '
                '`--memory`, `--concurrency`, `--timeout`, `--connectivity`?')

        conn_context = connection_context.GetConnectionContext(
            args, flags.Product.RUN, self.ReleaseTrack())
        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as client:
            service = client.GetService(service_ref)
            has_latest = (service is None or traffic.LATEST_REVISION_KEY
                          in service.spec_traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=False, include_route=has_latest)
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                client.ReleaseService(service_ref,
                                      changes,
                                      tracker,
                                      asyn=args.async_,
                                      prefetch=service)
            if args.async_:
                pretty_print.Success('Deploying asynchronously.')
            else:
                pretty_print.Success(
                    messages_util.GetSuccessMessageForSynchronousDeploy(
                        client, service_ref))
コード例 #16
0
    def Run(self, args):
        """Deploy a container to Cloud Run."""
        image = args.image

        conn_context = connection_context.GetConnectionContext(
            args, self.ReleaseTrack())
        config_changes = flags.GetConfigurationChanges(args)

        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as operations:
            image_change = config_changes_mod.ImageChange(image)
            changes = [image_change]
            if config_changes:
                changes.extend(config_changes)
            exists = operations.GetService(service_ref)
            allow_unauth = GetAllowUnauth(args, operations, service_ref,
                                          exists)

            pretty_print.Info(GetStartDeployMessage(conn_context, service_ref))

            deployment_stages = stages.ServiceStages(allow_unauth is not None)
            header = 'Deploying...' if exists else 'Deploying new service...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                operations.ReleaseService(service_ref,
                                          changes,
                                          tracker,
                                          asyn=args.async_,
                                          allow_unauthenticated=allow_unauth)
            if args.async_:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                pretty_print.Success(
                    GetSuccessMessageForSynchronousDeploy(
                        operations, service_ref))
コード例 #17
0
    def Run(self, args):
        """Create or Update service from YAML."""
        conn_context = connection_context.GetConnectionContext(
            args, flags.Product.RUN, self.ReleaseTrack())

        with serverless_operations.Connect(conn_context) as client:
            try:
                new_service = service.Service(
                    messages_util.DictToMessageWithErrorCheck(
                        args.FILE, client.messages_module.Service),
                    client.messages_module)
            except messages_util.ScalarTypeMismatchError as e:
                exceptions.MaybeRaiseCustomFieldMismatch(e)

            # If managed, namespace must match project (or will default to project if
            # not specified).
            # If not managed, namespace simply must not conflict if specified in
            # multiple places (or will default to "default" if not specified).
            namespace = args.CONCEPTS.namespace.Parse().Name(
            )  # From flag or default
            if new_service.metadata.namespace is not None:
                if (args.IsSpecified('namespace')
                        and namespace != new_service.metadata.namespace):
                    raise exceptions.ConfigurationError(
                        'Namespace specified in file does not match passed flag.'
                    )
                namespace = new_service.metadata.namespace
                if flags.GetPlatform() == flags.PLATFORM_MANAGED:
                    project = properties.VALUES.core.project.Get()
                    project_number = projects_util.GetProjectNumber(project)
                    if namespace != project and namespace != str(
                            project_number):
                        raise exceptions.ConfigurationError(
                            'Namespace must be project ID [{}] or quoted number [{}] for '
                            'Cloud Run (fully managed).'.format(
                                project, project_number))
            new_service.metadata.namespace = namespace

            changes = [
                config_changes.ReplaceServiceChange(new_service),
                config_changes.SetLaunchStageAnnotationChange(
                    self.ReleaseTrack())
            ]
            service_ref = resources.REGISTRY.Parse(
                new_service.metadata.name,
                params={'namespacesId': new_service.metadata.namespace},
                collection='run.namespaces.services')
            service_obj = client.GetService(service_ref)

            pretty_print.Info(
                run_messages_util.GetStartDeployMessage(
                    conn_context,
                    service_ref,
                    operation='Applying new configuration'))

            deployment_stages = stages.ServiceStages()
            header = ('Deploying...'
                      if service_obj else 'Deploying new service...')
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                service_obj = client.ReleaseService(service_ref,
                                                    changes,
                                                    tracker,
                                                    asyn=args.async_,
                                                    allow_unauthenticated=None,
                                                    for_replace=True)
            if args.async_:
                pretty_print.Success(
                    'New configuration for [{{bold}}{serv}{{reset}}] is being applied '
                    'asynchronously.'.format(serv=service_obj.name))
            else:
                service_obj = client.GetService(service_ref)
                pretty_print.Success(
                    'New configuration has been applied to service '
                    '[{{bold}}{serv}{{reset}}].\n'
                    'URL: {{bold}}{url}{{reset}}'.format(
                        serv=service_obj.name, url=service_obj.domain))
            return service_obj
コード例 #18
0
    def Run(self, args):
        """Deploy a container to Cloud Run."""
        flags.GetAndValidatePlatform(args, self.ReleaseTrack(),
                                     flags.Product.RUN)
        service_ref = args.CONCEPTS.service.Parse()
        flags.ValidateResource(service_ref)
        build_type = None
        image = None
        pack = None
        source = None
        include_build = flags.FlagIsExplicitlySet(args, 'source')
        operation_message = 'Deploying container'
        # Build an image from source if source specified
        if include_build:
            # Create a tag for the image creation
            source = args.source
            if not args.IsSpecified('image'):
                args.image = 'gcr.io/{projectID}/cloud-run-source-deploy/{service}:{tag}'.format(
                    projectID=properties.VALUES.core.project.Get(
                        required=True),
                    service=service_ref.servicesId,
                    tag=uuid.uuid4().hex)
            # Use GCP Buildpacks if Dockerfile doesn't exist
            docker_file = args.source + '/Dockerfile'
            if os.path.exists(docker_file):
                build_type = BuildType.DOCKERFILE
            else:
                pack = [{'image': args.image}]
                build_type = BuildType.BUILDPACKS
            image = None if pack else args.image
            operation_message = 'Building using {build_type} and deploying container'.format(
                build_type=build_type.value)
        elif not args.IsSpecified('image'):
            raise c_exceptions.RequiredArgumentException(
                '--image', 'Requires a container image to deploy (e.g. '
                '`gcr.io/cloudrun/hello:latest`) if no build source is provided.'
            )
        # Deploy a container with an image
        conn_context = connection_context.GetConnectionContext(
            args, flags.Product.RUN, self.ReleaseTrack())
        changes = flags.GetConfigurationChanges(args)
        changes.insert(
            0,
            config_changes.DeleteAnnotationChange(
                k8s_object.BINAUTHZ_BREAKGLASS_ANNOTATION))
        changes.append(
            config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack()))

        with serverless_operations.Connect(conn_context) as operations:
            service = operations.GetService(service_ref)
            allow_unauth = GetAllowUnauth(args, operations, service_ref,
                                          service)
            resource_change_validators.ValidateClearVpcConnector(service, args)

            pretty_print.Info(
                messages_util.GetStartDeployMessage(conn_context, service_ref,
                                                    operation_message))
            has_latest = (service is None or traffic.LATEST_REVISION_KEY
                          in service.spec_traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=allow_unauth is not None,
                include_route=has_latest,
                include_build=include_build)
            header = None
            if include_build:
                header = 'Building and deploying'
            else:
                header = 'Deploying'
            if service is None:
                header += ' new service'
            header += '...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                service = operations.ReleaseService(
                    service_ref,
                    changes,
                    tracker,
                    asyn=args.async_,
                    allow_unauthenticated=allow_unauth,
                    prefetch=service,
                    build_image=image,
                    build_pack=pack,
                    build_source=source)

            if args.async_:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service.name))
            else:
                service = operations.GetService(service_ref)
                pretty_print.Success(
                    messages_util.GetSuccessMessageForSynchronousDeploy(
                        service))
            return service
コード例 #19
0
ファイル: deploy.py プロジェクト: bopopescu/FamiBookWebApp
    def Run(self, args):
        """Deploy an app, function or container to Cloud Run."""
        source_ref = flags.GetSourceRef(args.source, args.image)
        config_changes = flags.GetConfigurationChanges(args)

        conn_context = connection_context.GetConnectionContext(args)

        if (conn_context.supports_one_platform
                and getattr(args, 'connectivity', None)):
            raise exceptions.ConfigurationError(
                'The `--endpoint=[internal|external]` flag '
                'is only supported with Cloud Run on GKE.')

        if (not conn_context.supports_one_platform
                and getattr(args, 'allow_unauthenticated', None)):
            raise exceptions.ConfigurationError(
                'The `--allow-unauthenticated` flag '
                'is not supported with Cloud Run on GKE.')

        service_ref = flags.GetService(args)
        function_entrypoint = flags.GetFunction(args.function)
        msg = ('Deploying {dep_type} to {operator} '
               'service [{{bold}}{service}{{reset}}]'
               ' in {ns_label} [{{bold}}{ns}{{reset}}]')

        msg += conn_context.location_label

        if function_entrypoint:
            dep_type = 'function [{{bold}}{}{{reset}}]'.format(
                function_entrypoint)
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type=dep_type,
                           function=function_entrypoint,
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))
        elif source_ref.source_type is source_ref.SourceType.IMAGE:
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type='container',
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))
        else:
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type='app',
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))

        with serverless_operations.Connect(conn_context) as operations:
            if not (source_ref.source_type is source_ref.SourceType.IMAGE
                    or operations.IsSourceBranch()):
                raise exceptions.SourceNotSupportedError()
            new_deployable = operations.Detect(service_ref.Parent(),
                                               source_ref, function_entrypoint)
            operations.Upload(new_deployable)
            changes = [new_deployable]
            if config_changes:
                changes.extend(config_changes)
            if args.connectivity == 'internal':
                private_endpoint = True
            elif args.connectivity == 'external':
                private_endpoint = False
            else:
                private_endpoint = None
            deployment_stages = stages.ServiceStages()
            exists = operations.GetService(service_ref)

            if (not exists and not args.allow_unauthenticated
                    and conn_context.supports_one_platform):

                if operations.CanAddIamPolicyBinding(service_ref):
                    allow_unauth = console_io.PromptContinue(
                        prompt_string=('Allow unauthenticated invocations '
                                       'to new service [{}]?'.format(
                                           service_ref.servicesId)),
                        default=False)
                else:
                    allow_unauth = False
                    pretty_print.Info(
                        'This new service will require authentication to be invoked.'
                    )
            else:
                allow_unauth = False

            header = 'Deploying...' if exists else 'Deploying new service...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                operations.ReleaseService(service_ref,
                                          changes,
                                          tracker,
                                          asyn=args. async,
                                          private_endpoint=private_endpoint,
                                          allow_unauthenticated=allow_unauth
                                          or args.allow_unauthenticated)
            if args. async:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                url = operations.GetServiceUrl(service_ref)
                conf = operations.GetConfiguration(service_ref)
                msg = ('Service [{{bold}}{serv}{{reset}}] '
                       'revision [{{bold}}{rev}{{reset}}] '
                       'has been deployed and is serving traffic at '
                       '{{bold}}{url}{{reset}}')
                msg = msg.format(serv=service_ref.servicesId,
                                 rev=conf.status.latestReadyRevisionName,
                                 url=url)
                pretty_print.Success(msg)
コード例 #20
0
  def Run(self, args):
    """Deploy a container to Cloud Run."""
    image = args.image

    conn_context = connection_context.GetConnectionContext(args)
    config_changes = flags.GetConfigurationChanges(args)

    if conn_context.supports_one_platform:
      flags.VerifyOnePlatformFlags(args)
    else:
      flags.VerifyGKEFlags(args)

    service_ref = flags.GetService(args)

    with serverless_operations.Connect(conn_context) as operations:
      image_change = config_changes_mod.ImageChange(image)
      changes = [image_change]
      if config_changes:
        changes.extend(config_changes)
      if args.connectivity == 'internal':
        private_endpoint = True
      elif args.connectivity == 'external':
        private_endpoint = False
      else:
        private_endpoint = None
      exists = operations.GetService(service_ref)

      if (not exists and not args.allow_unauthenticated and
          conn_context.supports_one_platform):

        if operations.CanAddIamPolicyBinding(service_ref):
          allow_unauth = console_io.PromptContinue(
              prompt_string=(
                  'Allow unauthenticated invocations '
                  'to new service [{}]?'.format(
                      service_ref.servicesId)),
              default=False)
        else:
          allow_unauth = False
          pretty_print.Info(
              'This new service will require authentication to be invoked.')
      else:
        allow_unauth = False

      msg = ('Deploying {dep_type} to {operator} '
             'service [{{bold}}{service}{{reset}}]'
             ' in {ns_label} [{{bold}}{ns}{{reset}}]')
      msg += conn_context.location_label

      pretty_print.Info(msg.format(
          operator=conn_context.operator,
          ns_label=conn_context.ns_label,
          dep_type='container',
          service=service_ref.servicesId,
          ns=service_ref.namespacesId))

      deployment_stages = stages.ServiceStages(
          allow_unauth or args.allow_unauthenticated)
      header = 'Deploying...' if exists else 'Deploying new service...'
      with progress_tracker.StagedProgressTracker(
          header,
          deployment_stages,
          failure_message='Deployment failed',
          suppress_output=args.async) as tracker:
        operations.ReleaseService(
            service_ref,
            changes,
            tracker,
            asyn=args.async,
            private_endpoint=private_endpoint,
            allow_unauthenticated=allow_unauth or args.allow_unauthenticated)
      if args.async:
        pretty_print.Success(
            'Service [{{bold}}{serv}{{reset}}] is deploying '
            'asynchronously.'.format(serv=service_ref.servicesId))
      else:
        url = operations.GetServiceUrl(service_ref)
        conf = operations.GetConfiguration(service_ref)
        msg = (
            'Service [{{bold}}{serv}{{reset}}] '
            'revision [{{bold}}{rev}{{reset}}] '
            'has been deployed and is serving traffic at '
            '{{bold}}{url}{{reset}}')
        msg = msg.format(
            serv=service_ref.servicesId,
            rev=conf.status.latestReadyRevisionName,
            url=url)
        pretty_print.Success(msg)
コード例 #21
0
ファイル: deploy.py プロジェクト: superina5/first_app
    def Run(self, args):
        """Deploy a container to Cloud Run."""
        image = args.image

        conn_context = connection_context.GetConnectionContext(args)
        config_changes = flags.GetConfigurationChanges(args)

        if conn_context.supports_one_platform:
            flags.VerifyOnePlatformFlags(args)
        else:
            flags.VerifyGKEFlags(args)

        service_ref = flags.GetService(args)

        with serverless_operations.Connect(conn_context) as operations:
            image_change = config_changes_mod.ImageChange(image)
            changes = [image_change]
            if config_changes:
                changes.extend(config_changes)
            endpoint_visibility = flags.GetEndpointVisibility(args)
            exists = operations.GetService(service_ref)
            allow_unauth = None
            if conn_context.supports_one_platform:
                allow_unauth = flags.GetAllowUnauthenticated(
                    args, operations, service_ref, not exists)
                # Don't try to remove a policy binding from a service that doesn't exist
                if not exists and not allow_unauth:
                    allow_unauth = None

            msg = ('Deploying {dep_type} to {operator} '
                   'service [{{bold}}{service}{{reset}}]'
                   ' in {ns_label} [{{bold}}{ns}{{reset}}]')
            msg += conn_context.location_label

            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type='container',
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))

            deployment_stages = stages.ServiceStages(allow_unauth is not None)
            header = 'Deploying...' if exists else 'Deploying new service...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                operations.ReleaseService(service_ref,
                                          changes,
                                          tracker,
                                          asyn=args. async,
                                          private_endpoint=endpoint_visibility,
                                          allow_unauthenticated=allow_unauth)
            if args. async:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                url = operations.GetServiceUrl(service_ref)
                conf = operations.GetConfiguration(service_ref)
                msg = ('Service [{{bold}}{serv}{{reset}}] '
                       'revision [{{bold}}{rev}{{reset}}] '
                       'has been deployed and is serving traffic at '
                       '{{bold}}{url}{{reset}}')
                msg = msg.format(serv=service_ref.servicesId,
                                 rev=conf.status.latestReadyRevisionName,
                                 url=url)
                pretty_print.Success(msg)
コード例 #22
0
ファイル: deploy.py プロジェクト: gabrielbessler/HackTech2019
    def Run(self, args):
        """Deploy an app, function or container to Cloud Run."""
        source_ref = flags.GetSourceRef(args.source, args.image)
        config_changes = flags.GetConfigurationChanges(args)

        conn_context = connection_context.GetConnectionContext(args)

        # pylint: disable=protected-access
        if (not isinstance(conn_context,
                           connection_context._GKEConnectionContext)
                and getattr(args, 'connectivity', None)):
            raise exceptions.ConfigurationError(
                'The `--endpoint=[internal|external]` flag '
                'is only supported with Cloud Run on GKE.')
        # pylint: enable=protected-access

        service_ref = flags.GetService(args)
        function_entrypoint = flags.GetFunction(args.function)
        msg = ('Deploying {dep_type} to {operator} '
               'service [{{bold}}{service}{{reset}}]'
               ' in {ns_label} [{{bold}}{ns}{{reset}}]')

        msg += conn_context.location_label

        if function_entrypoint:
            dep_type = 'function [{{bold}}{}{{reset}}]'.format(
                function_entrypoint)
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type=dep_type,
                           function=function_entrypoint,
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))
        elif source_ref.source_type is source_ref.SourceType.IMAGE:
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type='container',
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))
        else:
            pretty_print.Info(
                msg.format(operator=conn_context.operator,
                           ns_label=conn_context.ns_label,
                           dep_type='app',
                           service=service_ref.servicesId,
                           ns=service_ref.namespacesId))

        with serverless_operations.Connect(conn_context) as operations:
            if not (source_ref.source_type is source_ref.SourceType.IMAGE
                    or operations.IsSourceBranch()):
                raise exceptions.SourceNotSupportedError()
            new_deployable = operations.Detect(service_ref.Parent(),
                                               source_ref, function_entrypoint)
            operations.Upload(new_deployable)
            changes = [new_deployable]
            if config_changes:
                changes.extend(config_changes)
            if args.connectivity == 'internal':
                private_endpoint = True
            elif args.connectivity == 'external':
                private_endpoint = False
            else:
                private_endpoint = None
            deployment_stages = stages.ServiceStages()
            with progress_tracker.StagedProgressTracker(
                    'Deploying...',
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args. async) as tracker:
                operations.ReleaseService(service_ref,
                                          changes,
                                          tracker,
                                          asyn=args. async,
                                          private_endpoint=private_endpoint)
            if args. async:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service_ref.servicesId))
            else:
                url = operations.GetServiceUrl(service_ref)
                conf = operations.GetConfiguration(service_ref)
                msg = ('Service [{{bold}}{serv}{{reset}}] '
                       'revision [{{bold}}{rev}{{reset}}] '
                       'has been deployed and is serving traffic at '
                       '{{bold}}{url}{{reset}}')
                msg = msg.format(serv=service_ref.servicesId,
                                 rev=conf.status.latestReadyRevisionName,
                                 url=url)
                pretty_print.Success(msg)
コード例 #23
0
    def Run(self, args):
        """Deploy a container to Cloud Run."""
        platform = flags.GetAndValidatePlatform(args, self.ReleaseTrack(),
                                                flags.Product.RUN)

        include_build = flags.FlagIsExplicitlySet(args, 'source')
        if not include_build and not args.IsSpecified('image'):
            if console_io.CanPrompt():
                args.source = flags.PromptForDefaultSource()
                include_build = True
            else:
                raise c_exceptions.RequiredArgumentException(
                    '--image', 'Requires a container image to deploy (e.g. '
                    '`gcr.io/cloudrun/hello:latest`) if no build source is provided.'
                )

        service_ref = args.CONCEPTS.service.Parse()
        flags.ValidateResource(service_ref)

        # Obtaining the connection context prompts the user to select a region if
        # one hasn't been provided. We want to do this prior to preparing a source
        # deploy so that we can use that region for the Artifact Registry repo.
        conn_context = connection_context.GetConnectionContext(
            args, flags.Product.RUN, self.ReleaseTrack())

        build_type = None
        image = None
        pack = None
        source = None
        operation_message = 'Deploying container to'
        repo_to_create = None
        # Build an image from source if source specified
        if include_build:
            source = args.source

            ar_repo = docker_util.DockerRepo(
                project_id=properties.VALUES.core.project.Get(required=True),
                location_id=artifact_registry.RepoRegion(
                    args,
                    cluster_location=(conn_context.cluster_location if platform
                                      == platforms.PLATFORM_GKE else None)),
                repo_id='cloud-run-source-deploy')
            if artifact_registry.ShouldCreateRepository(ar_repo):
                repo_to_create = ar_repo
            # The image is built with latest tag. After build, the image digest
            # from the build result will be added to the image of the service spec.
            args.image = '{repo}/{service}'.format(
                repo=ar_repo.GetDockerString(), service=service_ref.servicesId)
            # Use GCP Buildpacks if Dockerfile doesn't exist
            docker_file = source + '/Dockerfile'
            if os.path.exists(docker_file):
                build_type = BuildType.DOCKERFILE
            else:
                pack = [{'image': args.image}]
                build_type = BuildType.BUILDPACKS
            image = None if pack else args.image
            operation_message = (
                'Building using {build_type} and deploying container'
                ' to').format(build_type=build_type.value)
            pretty_print.Info(
                messages_util.GetBuildEquivalentForSourceRunMessage(
                    service_ref.servicesId, pack, source))

        # Deploy a container with an image
        changes = flags.GetServiceConfigurationChanges(args)
        changes.insert(
            0,
            config_changes.DeleteAnnotationChange(
                k8s_object.BINAUTHZ_BREAKGLASS_ANNOTATION))
        changes.append(
            config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack()))

        with serverless_operations.Connect(conn_context) as operations:
            service = operations.GetService(service_ref)
            allow_unauth = GetAllowUnauth(args, operations, service_ref,
                                          service)
            resource_change_validators.ValidateClearVpcConnector(service, args)

            pretty_print.Info(
                messages_util.GetStartDeployMessage(conn_context, service_ref,
                                                    operation_message))
            has_latest = (service is None or traffic.LATEST_REVISION_KEY
                          in service.spec_traffic)
            deployment_stages = stages.ServiceStages(
                include_iam_policy_set=allow_unauth is not None,
                include_route=has_latest,
                include_build=include_build,
                include_create_repo=repo_to_create is not None,
            )
            header = None
            if include_build:
                header = 'Building and deploying'
            else:
                header = 'Deploying'
            if service is None:
                header += ' new service'
            header += '...'
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                service = operations.ReleaseService(
                    service_ref,
                    changes,
                    tracker,
                    asyn=args.async_,
                    allow_unauthenticated=allow_unauth,
                    prefetch=service,
                    build_image=image,
                    build_pack=pack,
                    build_source=source,
                    repo_to_create=repo_to_create)

            if args.async_:
                pretty_print.Success(
                    'Service [{{bold}}{serv}{{reset}}] is deploying '
                    'asynchronously.'.format(serv=service.name))
            else:
                service = operations.GetService(service_ref)
                pretty_print.Success(
                    messages_util.GetSuccessMessageForSynchronousDeploy(
                        service))
            return service
コード例 #24
0
  def ReleaseService(self,
                     service_ref,
                     config_changes,
                     tracker=None,
                     asyn=False,
                     allow_unauthenticated=None,
                     for_replace=False,
                     prefetch=False,
                     build_op_ref=None,
                     build_log_url=None):
    """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release.
      config_changes: list, objects that implement Adjust().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, return without waiting for the service to be updated.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access. False if removing the IAM policy to allow
        unauthenticated access from a service.
      for_replace: bool, If the change is for a replacing the service from a
        YAML specification.
      prefetch: the service, pre-fetched for ReleaseService. `False` indicates
        the caller did not perform a prefetch; `None` indicates a nonexistant
        service.
      build_op_ref: The reference to the build.
      build_log_url: The log url of the build result.
    """
    if tracker is None:
      tracker = progress_tracker.NoOpStagedProgressTracker(
          stages.ServiceStages(allow_unauthenticated is not None),
          interruptable=True, aborted_message='aborted')
    if build_op_ref is not None:
      tracker.StartStage(stages.BUILD_READY)
      tracker.UpdateHeaderMessage('Building Container.')
      tracker.UpdateStage(
          stages.BUILD_READY, 'Logs are available at [{build_log_url}].'.format(
              build_log_url=build_log_url))
      client = cloudbuild_util.GetClientInstance()
      poller = waiter.CloudOperationPoller(client.projects_builds,
                                           client.operations)
      operation = waiter.PollUntilDone(poller, build_op_ref)
      response_dict = encoding.MessageToPyValue(operation.response)
      if response_dict and response_dict['status'] != 'SUCCESS':
        tracker.FailStage(
            stages.BUILD_READY, None,
            message='Container build failed and '
            'logs are available at [{build_log_url}].'.format(
                build_log_url=build_log_url))
        return
      else:
        tracker.CompleteStage(stages.BUILD_READY)
    if prefetch is None:
      serv = None
    else:
      serv = prefetch or self.GetService(service_ref)
    if for_replace:
      with_image = True
    else:
      with_image = any(
          isinstance(c, config_changes_mod.ImageChange) for c in config_changes)
      self._AddRevisionForcingChange(serv, config_changes)
      if serv and not with_image:
        # Avoid changing the running code by making the new revision by digest
        self._EnsureImageDigest(serv, config_changes)
    config_changes = [_SetClientNameAndVersion()] + config_changes

    self._UpdateOrCreateService(
        service_ref, config_changes, with_image, serv)

    if allow_unauthenticated is not None:
      try:
        tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
        tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
        self.AddOrRemoveIamPolicyBinding(service_ref, allow_unauthenticated,
                                         ALLOW_UNAUTH_POLICY_BINDING_MEMBER,
                                         ALLOW_UNAUTH_POLICY_BINDING_ROLE)
        tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
      except api_exceptions.HttpError:
        warning_message = (
            'Setting IAM policy failed, try "gcloud beta run services '
            '{}-iam-policy-binding --region={region} --member=allUsers '
            '--role=roles/run.invoker {service}"'.format(
                'add' if allow_unauthenticated else 'remove',
                region=self._region,
                service=service_ref.servicesId))
        tracker.CompleteStageWithWarning(
            stages.SERVICE_IAM_POLICY_SET, warning_message=warning_message)

    if not asyn:
      getter = functools.partial(self.GetService, service_ref)
      poller = ServiceConditionPoller(
          getter,
          tracker,
          dependencies=stages.ServiceDependencies(),
          serv=serv)
      self.WaitForCondition(poller)
      for msg in run_condition.GetNonTerminalMessages(poller.GetConditions()):
        tracker.AddWarning(msg)
コード例 #25
0
    def Run(self, args):
        """Create or Update service from YAML."""
        run_messages = apis.GetMessagesModule(
            global_methods.SERVERLESS_API_NAME,
            global_methods.SERVERLESS_API_VERSION)
        service_dict = dict(args.FILE)
        # Clear the status to make migration from k8s deployments easier.
        # Since a Deployment status will have several fields that Cloud Run doesn't
        # support, trying to convert it to a message as-is will fail even though
        # status is ignored by the server.
        if 'status' in service_dict:
            del service_dict['status']

        # For cases where YAML contains the project number as metadata.namespace,
        # preemptively convert them to a string to avoid validation failures.
        namespace = service_dict.get('metadata', {}).get('namespace', None)
        if namespace is not None and not isinstance(namespace, str):
            service_dict['metadata']['namespace'] = str(namespace)

        try:
            raw_service = messages_util.DictToMessageWithErrorCheck(
                service_dict, run_messages.Service)
            new_service = service.Service(raw_service, run_messages)
        except messages_util.ScalarTypeMismatchError as e:
            exceptions.MaybeRaiseCustomFieldMismatch(
                e,
                help_text=
                'Please make sure that the YAML file matches the Knative '
                'service definition spec in https://kubernetes.io/docs/'
                'reference/kubernetes-api/services-resources/service-v1/'
                '#Service.')

        # If managed, namespace must match project (or will default to project if
        # not specified).
        # If not managed, namespace simply must not conflict if specified in
        # multiple places (or will default to "default" if not specified).
        namespace = args.CONCEPTS.namespace.Parse().Name(
        )  # From flag or default
        if new_service.metadata.namespace is not None:
            if (args.IsSpecified('namespace')
                    and namespace != new_service.metadata.namespace):
                raise exceptions.ConfigurationError(
                    'Namespace specified in file does not match passed flag.')
            namespace = new_service.metadata.namespace
            if platforms.GetPlatform() == platforms.PLATFORM_MANAGED:
                project = properties.VALUES.core.project.Get()
                project_number = projects_util.GetProjectNumber(project)
                if namespace != project and namespace != str(project_number):
                    raise exceptions.ConfigurationError(
                        'Namespace must be project ID [{}] or quoted number [{}] for '
                        'Cloud Run (fully managed).'.format(
                            project, project_number))
        new_service.metadata.namespace = namespace

        changes = [
            config_changes.ReplaceServiceChange(new_service),
            config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack())
        ]
        service_ref = resources.REGISTRY.Parse(
            new_service.metadata.name,
            params={'namespacesId': new_service.metadata.namespace},
            collection='run.namespaces.services')

        region_label = new_service.region if new_service.is_managed else None

        conn_context = connection_context.GetConnectionContext(
            args,
            flags.Product.RUN,
            self.ReleaseTrack(),
            region_label=region_label)

        with serverless_operations.Connect(conn_context) as client:
            service_obj = client.GetService(service_ref)

            pretty_print.Info(
                run_messages_util.GetStartDeployMessage(
                    conn_context,
                    service_ref,
                    operation='Applying new configuration to'))

            deployment_stages = stages.ServiceStages()
            header = ('Deploying...'
                      if service_obj else 'Deploying new service...')
            with progress_tracker.StagedProgressTracker(
                    header,
                    deployment_stages,
                    failure_message='Deployment failed',
                    suppress_output=args.async_) as tracker:
                service_obj = client.ReleaseService(service_ref,
                                                    changes,
                                                    tracker,
                                                    asyn=args.async_,
                                                    allow_unauthenticated=None,
                                                    for_replace=True)
            if args.async_:
                pretty_print.Success(
                    'New configuration for [{{bold}}{serv}{{reset}}] is being applied '
                    'asynchronously.'.format(serv=service_obj.name))
            else:
                service_obj = client.GetService(service_ref)
                pretty_print.Success(
                    'New configuration has been applied to service '
                    '[{{bold}}{serv}{{reset}}].\n'
                    'URL: {{bold}}{url}{{reset}}'.format(
                        serv=service_obj.name, url=service_obj.domain))
            return service_obj