Exemple #1
0
    def Run(self, args):
        """Run 'operations wait'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    Raises:
      DeploymentManagerError: Operation finished with error(s) or timed out.
    """
        project = properties.VALUES.core.project.Get(required=True)
        failed_ops = []
        for operation_name in args.operation_name:
            try:
                dm_v2_util.WaitForOperation(operation_name, project,
                                            self.context, '',
                                            OPERATION_TIMEOUT)
            except DeploymentManagerError:
                failed_ops.append(operation_name)
        if failed_ops:
            if len(failed_ops) == 1:
                raise DeploymentManagerError(
                    'Operation %s failed to complete or has errors.' %
                    failed_ops[0])
            else:
                raise DeploymentManagerError(
                    'Some operations failed to complete without errors:\n' +
                    '\n'.join(failed_ops))
        else:
            log.status.Print('All operations completed successfully.')
Exemple #2
0
  def Run(self, args):
    """Run 'deployments stop'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether stop operation succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: The stop operation encountered an error.
    """
    client = self.context['deploymentmanager-client']
    messages = self.context['deploymentmanager-messages']
    project = properties.VALUES.core.project.Get(required=True)

    # Get the fingerprint from the deployment to stop.
    try:
      current_deployment = client.deployments.Get(
          messages.DeploymentmanagerDeploymentsGetRequest(
              project=project,
              deployment=args.deployment_name
          )
      )
      # If no fingerprint is present, default to an empty fingerprint.
      # This empty default can be removed once the fingerprint change is
      # fully implemented and all deployments have fingerprints.
      fingerprint = current_deployment.fingerprint or ''
    except apitools_base.HttpError as error:
      raise exceptions.HttpException(dm_v2_util.GetError(error))

    try:
      operation = client.deployments.Stop(
          messages.DeploymentmanagerDeploymentsStopRequest(
              project=project,
              deployment=args.deployment_name,
              deploymentsStopRequest=messages.DeploymentsStopRequest(
                  fingerprint=fingerprint,
              ),
          )
      )
    except apitools_base.HttpError as error:
      raise exceptions.HttpException(dm_v2_util.GetError(error))
    if args.async:
      return operation
    else:
      op_name = operation.name
      try:
        dm_v2_util.WaitForOperation(op_name, project, self.context, 'stop',
                                    OPERATION_TIMEOUT)
        log.status.Print('Stop operation ' + op_name
                         + ' completed successfully.')
      except exceptions.ToolException:
        # Operation timed out or had errors. Print this warning, then still
        # show whatever operation can be gotten.
        log.error('Stop operation ' + op_name
                  + ' has errors or failed to complete within '
                  + str(OPERATION_TIMEOUT) + ' seconds.')
      except apitools_base.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))
      try:
        # Fetch a list of the stopped resources.
        response = client.resources.List(
            messages.DeploymentmanagerResourcesListRequest(
                project=project,
                deployment=args.deployment_name,
            )
        )
        # TODO(user): Pagination
        return response.resources if response.resources else []
      except apitools_base.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))
  def Run(self, args):
    """Run 'deployments create'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether create operation succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: Config file could not be read or parsed, or the deployment
          creation operation encountered an error.
    """
    client = self.context['deploymentmanager-v2beta2']
    messages = self.context['deploymentmanager-v2beta2-messages']
    project = properties.VALUES.core.project.Get(required=True)

    deployment = messages.Deployment(
        name=args.deployment_name,
        target=dm_v2_util.BuildTargetConfig(messages, args.config),
    )
    if args.description:
      deployment.description = args.description
    if args.preview:
      deployment.intent = 'PREVIEW'

    try:
      operation = client.deployments.Insert(
          messages.DeploymentmanagerDeploymentsInsertRequest(
              project=project,
              deployment=deployment,
          )
      )
    except apitools_base.HttpError as error:
      raise exceptions.HttpException(dm_v2_util.GetError(error))
    if args.async:
      return operation
    else:
      op_name = operation.name
      try:
        dm_v2_util.WaitForOperation(op_name, project, self.context, 'create',
                                    OPERATION_TIMEOUT)
        log.status.Print('Create operation ' + op_name
                         + ' completed successfully.')
      except exceptions.ToolException:
        # Operation timed out or had errors. Print this warning, then still
        # show whatever operation can be gotten.
        log.error('Create operation ' + op_name
                  + ' has errors or failed to complete within '
                  + str(OPERATION_TIMEOUT) + ' seconds.')
      except apitools_base.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))
      try:
        # Fetch a list of the previewed or updated resources.
        response = client.resources.List(
            messages.DeploymentmanagerResourcesListRequest(
                project=project,
                deployment=args.deployment_name,
            )
        )
        # TODO(user): Pagination
        return response.resources if response.resources else []
      except apitools_base.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))
Exemple #4
0
  def Run(self, args):
    """Run 'deployments delete'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether insert operation succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: The deployment deletion operation encountered an error.
    """
    client = self.context['deploymentmanager-client']
    messages = self.context['deploymentmanager-messages']
    project = properties.VALUES.core.project.Get(required=True)

    prompt_message = ('The following deployments will be deleted:\n- '
                      + '\n- '.join(args.deployment_name))
    if not args.quiet:
      if not console_io.PromptContinue(message=prompt_message, default=False):
        raise exceptions.ToolException('Deletion aborted by user.')

    operations = []
    for deployment_name in args.deployment_name:
      try:
        operation = client.deployments.Delete(
            messages.DeploymentmanagerDeploymentsDeleteRequest(
                project=project,
                deployment=deployment_name,
            )
        )
      except apitools_base.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))
      if args.async:
        operations.append(operation)
      else:
        op_name = operation.name
        try:
          dm_v2_util.WaitForOperation(op_name, project, self.context, 'delete',
                                      OPERATION_TIMEOUT)
          log.status.Print('Delete operation ' + op_name
                           + ' completed successfully.')
        except (exceptions.ToolException, DeploymentManagerError):
          log.error('Delete operation ' + op_name
                    + ' has errors or failed to complete within in '
                    + str(OPERATION_TIMEOUT) + ' seconds.')
        except apitools_base.HttpError as error:
          raise exceptions.HttpException(dm_v2_util.GetError(error))
        try:
          completed_operation = client.operations.Get(
              messages.DeploymentmanagerOperationsGetRequest(
                  project=project,
                  operation=op_name,
              )
          )
        except apitools_base.HttpError as error:
          raise exceptions.HttpException(dm_v2_util.GetError(error))
        operations.append(completed_operation)

    return operations
Exemple #5
0
    def Run(self, args):
        """Run 'deployments update'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether update operation succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: Config file could not be read or parsed, or the deployment
          creation operation encountered an error.
    """
        client = self.context['deploymentmanager-v2beta2']
        messages = self.context['deploymentmanager-v2beta2-messages']
        project = properties.VALUES.core.project.Get(required=True)

        deployment = messages.Deployment(
            name=args.deployment_name,
            intent='PREVIEW' if args.preview else 'UPDATE',
        )
        if args.config:
            deployment.target = dm_v2_util.BuildTargetConfig(
                messages, args.config)
        # Get the fingerprint from the deployment to update.
        try:
            current_deployment = client.deployments.Get(
                messages.DeploymentmanagerDeploymentsGetRequest(
                    project=project, deployment=args.deployment_name))
            # If no fingerprint is present, default to an empty fingerprint.
            # This empty default can be removed once the fingerprint change is
            # fully implemented and all deployments have fingerprints.
            deployment.fingerprint = current_deployment.fingerprint or ''
        except apitools_base.HttpError as error:
            raise exceptions.HttpException(dm_v2_util.GetError(error))

        try:
            operation = client.deployments.Update(
                messages.DeploymentmanagerDeploymentsUpdateRequest(
                    deploymentResource=deployment,
                    project=project,
                    deployment=args.deployment_name,
                    createPolicy=(
                        messages.DeploymentmanagerDeploymentsUpdateRequest.
                        CreatePolicyValueValuesEnum(args.create_policy)),
                    deletePolicy=(
                        messages.DeploymentmanagerDeploymentsUpdateRequest.
                        DeletePolicyValueValuesEnum(args.delete_policy)),
                    updatePolicy=(
                        messages.DeploymentmanagerDeploymentsUpdateRequest.
                        UpdatePolicyValueValuesEnum(args.update_policy)),
                ))
        except apitools_base.HttpError as error:
            raise exceptions.HttpException(dm_v2_util.GetError(error))
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_v2_util.WaitForOperation(op_name, project, self.context,
                                            'update', OPERATION_TIMEOUT)
                log.status.Print('Update operation ' + op_name +
                                 ' completed successfully.')
            except exceptions.ToolException:
                # Operation timed out or had errors. Print this warning, then still
                # show whatever operation can be gotten.
                log.error('Update operation ' + op_name +
                          ' has errors or failed to complete within ' +
                          str(OPERATION_TIMEOUT) + ' seconds.')
            except apitools_base.HttpError as error:
                raise exceptions.HttpException(dm_v2_util.GetError(error))
            try:
                # Fetch a list of the previewed or updated resources.
                response = client.resources.List(
                    messages.DeploymentmanagerResourcesListRequest(
                        project=project,
                        deployment=args.deployment_name,
                    ))
                # TODO(user): Pagination
                return response.resources if response.resources else []
            except apitools_base.HttpError as error:
                raise exceptions.HttpException(dm_v2_util.GetError(error))