Beispiel #1
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client
    resources = holder.resources

    minimal_action = (client.messages.InstanceGroupManagerUpdatePolicy.
                      MinimalActionValueValuesEnum.REPLACE)
    max_surge = update_instances_utils.ParseFixedOrPercent(
        '--max-surge', 'max-surge', args.max_surge, client.messages)
    return client.MakeRequests([
        rolling_action.CreateRequest(args, client, resources,
                                     minimal_action, max_surge)
    ])
Beispiel #2
0
def CreateRequest(args,
                  cleared_fields,
                  client,
                  resources,
                  minimal_action,
                  max_surge=None):
    """Create request helper for compute instance-groups managed rolling-action.

  Args:
    args: argparse namespace
    cleared_fields: Fields which are left cleared, but should be send in request
    client: The compute client
    resources: The compute resources
    minimal_action: MinimalActionValueValuesEnum value
    max_surge: InstanceGroupManagerUpdatePolicy.maxSurge value

  Returns:
    ComputeInstanceGroupManagersPatchRequest or
    ComputeRegionInstanceGroupManagersPatchRequest instance
  """
    resource_arg = instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG
    default_scope = compute_scope.ScopeEnum.ZONE
    scope_lister = flags.GetDefaultScopeLister(client)
    igm_ref = resource_arg.ResolveAsResource(args,
                                             resources,
                                             default_scope=default_scope,
                                             scope_lister=scope_lister)

    update_policy_type = (client.messages.InstanceGroupManagerUpdatePolicy.
                          TypeValueValuesEnum.PROACTIVE)
    max_unavailable = update_instances_utils.ParseFixedOrPercent(
        '--max-unavailable', 'max-unavailable', args.max_unavailable,
        client.messages)

    igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
        igm_ref, client)

    versions = (igm_info.versions or [
        client.messages.InstanceGroupManagerVersion(
            instanceTemplate=igm_info.instanceTemplate)
    ])
    current_time_str = str(times.Now(times.UTC))
    for i, version in enumerate(versions):
        version.name = '%d/%s' % (i, current_time_str)

    update_policy = client.messages.InstanceGroupManagerUpdatePolicy(
        maxSurge=max_surge,
        maxUnavailable=max_unavailable,
        minReadySec=args.min_ready,
        minimalAction=minimal_action,
        type=update_policy_type)
    igm_resource = client.messages.InstanceGroupManager(
        instanceTemplate=None, updatePolicy=update_policy, versions=versions)
    if igm_ref.Collection() == 'compute.instanceGroupManagers':
        service = client.apitools_client.instanceGroupManagers
        request = client.messages.ComputeInstanceGroupManagersPatchRequest(
            instanceGroupManager=igm_ref.Name(),
            instanceGroupManagerResource=igm_resource,
            project=igm_ref.project,
            zone=igm_ref.zone)
    elif igm_ref.Collection() == 'compute.regionInstanceGroupManagers':
        service = client.apitools_client.regionInstanceGroupManagers
        request = client.messages.ComputeRegionInstanceGroupManagersPatchRequest(
            instanceGroupManager=igm_ref.Name(),
            instanceGroupManagerResource=igm_resource,
            project=igm_ref.project,
            region=igm_ref.region)
    # Due to 'Patch' semantics, we have to clear either 'fixed' or 'percent'.
    # Otherwise, we'll get an error that both 'fixed' and 'percent' are set.
    if max_surge is not None:
        cleared_fields.append('updatePolicy.maxSurge.fixed' if max_surge.fixed
                              is None else 'updatePolicy.maxSurge.percent')
    if max_unavailable is not None:
        cleared_fields.append(
            'updatePolicy.maxUnavailable.fixed' if max_unavailable.
            fixed is None else 'updatePolicy.maxUnavailable.percent')
    return (service, 'Patch', request)
Beispiel #3
0
    def CreateRequest(self, args, cleared_fields):
        resource_arg = instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG
        default_scope = compute_scope.ScopeEnum.ZONE
        scope_lister = flags.GetDefaultScopeLister(self.compute_client,
                                                   self.project)
        igm_ref = resource_arg.ResolveAsResource(args,
                                                 self.resources,
                                                 default_scope=default_scope,
                                                 scope_lister=scope_lister)

        update_instances_utils.ValidateUpdateInstancesArgs(args)
        update_policy_type = update_instances_utils.ParseUpdatePolicyType(
            '--type', args.type, self.messages)
        max_surge = update_instances_utils.ParseFixedOrPercent(
            '--max-surge', 'max-surge', args.max_surge, self.messages)
        max_unavailable = update_instances_utils.ParseFixedOrPercent(
            '--max-unavailable', 'max-unavailable', args.max_unavailable,
            self.messages)

        igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, self.compute_client)
        if args.action == 'replace':
            versions = []
            if args.version_original:
                versions.append(
                    update_instances_utils.ParseVersion(
                        '--version-original', args.version_original,
                        self.resources, self.messages))
            versions.append(
                update_instances_utils.ParseVersion('--version-new',
                                                    args.version_new,
                                                    self.resources,
                                                    self.messages))
            managed_instance_groups_utils.ValidateVersions(
                igm_info, versions, args.force)

            # TODO(b/36056457): Decide what we should do when two versions have the
            # same instance template (this can happen with canary restart performed
            # using tags).
            igm_version_names = {
                version.instanceTemplate: version.name
                for version in igm_info.versions
            }
            for version in versions:
                version.name = igm_version_names.get(version.instanceTemplate)
                version.tag = version.name
            minimal_action = (self.messages.InstanceGroupManagerUpdatePolicy.
                              MinimalActionValueValuesEnum.REPLACE)
        elif args.action == 'restart' and igm_info.versions is not None:
            versions = (igm_info.versions or [
                self.messages.InstanceGroupManagerVersion(
                    instanceTemplate=igm_info.instanceTemplate)
            ])
            current_time_str = str(times.Now(times.UTC))
            for i, version in enumerate(versions):
                version.name = '%d/%s' % (i, current_time_str)
                version.tag = version.name
            minimal_action = (self.messages.InstanceGroupManagerUpdatePolicy.
                              MinimalActionValueValuesEnum.RESTART)
        else:
            raise exceptions.InvalidArgumentException('--action',
                                                      'unknown action type.')

        update_policy = self.messages.InstanceGroupManagerUpdatePolicy(
            maxSurge=max_surge,
            maxUnavailable=max_unavailable,
            minReadySec=args.min_ready,
            minimalAction=minimal_action,
            type=update_policy_type)
        igm_resource = self.messages.InstanceGroupManager(
            instanceTemplate=None,
            updatePolicy=update_policy,
            versions=versions)
        if hasattr(igm_ref, 'zone'):
            service = self.compute.instanceGroupManagers
            request = (self.messages.ComputeInstanceGroupManagersPatchRequest(
                instanceGroupManager=igm_ref.Name(),
                instanceGroupManagerResource=igm_resource,
                project=self.project,
                zone=igm_ref.zone))
        elif hasattr(igm_ref, 'region'):
            service = self.compute.regionInstanceGroupManagers
            request = (
                self.messages.ComputeRegionInstanceGroupManagersPatchRequest(
                    instanceGroupManager=igm_ref.Name(),
                    instanceGroupManagerResource=igm_resource,
                    project=self.project,
                    region=igm_ref.region))
        # Due to 'Patch' semantics, we have to clear either 'fixed' or 'percent'.
        # Otherwise, we'll get an error that both 'fixed' and 'percent' are set.
        if max_surge is not None:
            cleared_fields.append(
                'updatePolicy.maxSurge.fixed' if max_surge.fixed is None else
                'updatePolicy.maxSurge.percent')
        if max_unavailable is not None:
            cleared_fields.append(
                'updatePolicy.maxUnavailable.fixed' if max_unavailable.
                fixed is None else 'updatePolicy.maxUnavailable.percent')
        return (service, 'Patch', request)
Beispiel #4
0
    def CreateRequests(self, args):
        resource_arg = instance_groups_flags.ZONAL_INSTANCE_GROUP_MANAGER_ARG
        default_scope = flags.ScopeEnum.ZONE
        scope_lister = flags.GetDefaultScopeLister(self.compute_client,
                                                   self.project)
        igm_ref = resource_arg.ResolveAsResource(args,
                                                 self.resources,
                                                 default_scope=default_scope,
                                                 scope_lister=scope_lister)

        update_instances_utils.ValidateUpdateInstancesArgs(args)
        update_policy_type = update_instances_utils.ParseUpdatePolicyType(
            '--type', args.type, self.messages)
        max_surge = update_instances_utils.ParseFixedOrPercent(
            '--max-surge', 'max-surge', args.max_surge, self.messages)
        max_unavailable = update_instances_utils.ParseFixedOrPercent(
            '--max-unavailable', 'max-unavailable', args.max_unavailable,
            self.messages)

        igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, self.project, self.compute, self.http, self.batch_url)
        if args.action == 'replace':
            versions = []
            if args.version_original:
                versions.append(
                    update_instances_utils.ParseVersion(
                        '--version-original', args.version_original,
                        self.resources, self.messages))
            versions.append(
                update_instances_utils.ParseVersion('--version-new',
                                                    args.version_new,
                                                    self.resources,
                                                    self.messages))
            managed_instance_groups_utils.ValidateVersions(
                igm_info, versions, args.force)

            igm_tags = dict((version.instanceTemplate, version.tag)
                            for version in igm_info.versions)
            for version in versions:
                version.tag = igm_tags.get(version.instanceTemplate)
            minimal_action = (self.messages.InstanceGroupManagerUpdatePolicy.
                              MinimalActionValueValuesEnum.REPLACE)
        elif args.action == 'restart' and igm_info.versions is not None:
            versions = (igm_info.versions or [
                self.messages.InstanceGroupManagerVersion(
                    instanceTemplate=igm_info.instanceTemplate)
            ])
            for version in versions:
                version.tag = str(times.Now(times.UTC))
            minimal_action = (self.messages.InstanceGroupManagerUpdatePolicy.
                              MinimalActionValueValuesEnum.RESTART)
        else:
            raise exceptions.InvalidArgumentException('--action',
                                                      'unknown action type.')

        update_policy = self.messages.InstanceGroupManagerUpdatePolicy(
            maxSurge=max_surge,
            maxUnavailable=max_unavailable,
            minReadySec=args.min_ready,
            minimalAction=minimal_action,
            type=update_policy_type,
        )
        service = self.compute.instanceGroupManagers
        request = (self.messages.ComputeInstanceGroupManagersPatchRequest(
            instanceGroupManager=igm_ref.Name(),
            instanceGroupManagerResource=(self.messages.InstanceGroupManager(
                instanceTemplate=None,
                updatePolicy=update_policy,
                versions=versions)),
            project=self.project,
            zone=igm_ref.zone))

        return [(service, self.method, request)]
Beispiel #5
0
def CreateRequest(args,
                  client,
                  resources,
                  minimal_action,
                  max_surge=None):
  """Create request helper for compute instance-groups managed rolling-action.

  Args:
    args: argparse namespace
    client: The compute client
    resources: The compute resources
    minimal_action: MinimalActionValueValuesEnum value
    max_surge: InstanceGroupManagerUpdatePolicy.maxSurge value

  Returns:
    ComputeInstanceGroupManagersPatchRequest or
    ComputeRegionInstanceGroupManagersPatchRequest instance

  Raises:
    ValueError: if instance group manager collection path is unknown
  """
  resource_arg = instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG
  default_scope = compute_scope.ScopeEnum.ZONE
  scope_lister = flags.GetDefaultScopeLister(client)
  igm_ref = resource_arg.ResolveAsResource(
      args, resources, default_scope=default_scope, scope_lister=scope_lister)

  if igm_ref.Collection() not in [
      'compute.instanceGroupManagers', 'compute.regionInstanceGroupManagers'
  ]:
    raise ValueError('Unknown reference type {0}'.format(igm_ref.Collection()))

  update_policy_type = (client.messages.InstanceGroupManagerUpdatePolicy.
                        TypeValueValuesEnum.PROACTIVE)
  max_unavailable = update_instances_utils.ParseFixedOrPercent(
      '--max-unavailable', 'max-unavailable', args.max_unavailable,
      client.messages)

  igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
      igm_ref, client)

  versions = (igm_info.versions or [
      client.messages.InstanceGroupManagerVersion(
          instanceTemplate=igm_info.instanceTemplate)
  ])
  current_time_str = str(times.Now(times.UTC))
  for i, version in enumerate(versions):
    version.name = '%d/%s' % (i, current_time_str)

  update_policy = client.messages.InstanceGroupManagerUpdatePolicy(
      maxSurge=max_surge,
      maxUnavailable=max_unavailable,
      minimalAction=minimal_action,
      type=update_policy_type)
  # min_ready is available in alpha and beta APIs only
  if hasattr(args, 'min_ready'):
    update_policy.minReadySec = args.min_ready
  # replacement_method is available in alpha API only
  if hasattr(args, 'replacement_method'):
    replacement_method = update_instances_utils.ParseReplacementMethod(
        args.replacement_method, client.messages)
    update_policy.replacementMethod = replacement_method

  igm_resource = client.messages.InstanceGroupManager(
      instanceTemplate=None, updatePolicy=update_policy, versions=versions)
  if igm_ref.Collection() == 'compute.instanceGroupManagers':
    service = client.apitools_client.instanceGroupManagers
    request = client.messages.ComputeInstanceGroupManagersPatchRequest(
        instanceGroupManager=igm_ref.Name(),
        instanceGroupManagerResource=igm_resource,
        project=igm_ref.project,
        zone=igm_ref.zone)
  else:
    service = client.apitools_client.regionInstanceGroupManagers
    request = client.messages.ComputeRegionInstanceGroupManagersPatchRequest(
        instanceGroupManager=igm_ref.Name(),
        instanceGroupManagerResource=igm_resource,
        project=igm_ref.project,
        region=igm_ref.region)
  return (service, 'Patch', request)
Beispiel #6
0
  def CreateRequest(self, args, cleared_fields, client, resources):
    resource_arg = instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG
    default_scope = compute_scope.ScopeEnum.ZONE
    scope_lister = flags.GetDefaultScopeLister(client)
    igm_ref = resource_arg.ResolveAsResource(
        args, resources, default_scope=default_scope, scope_lister=scope_lister)

    update_policy_type = update_instances_utils.ParseUpdatePolicyType(
        '--type', args.type, client.messages)
    max_surge = update_instances_utils.ParseFixedOrPercent(
        '--max-surge', 'max-surge', args.max_surge, client.messages)
    max_unavailable = update_instances_utils.ParseFixedOrPercent(
        '--max-unavailable', 'max-unavailable', args.max_unavailable,
        client.messages)

    igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
        igm_ref, client)

    versions = []
    versions.append(
        update_instances_utils.ParseVersion(igm_ref.project, '--version',
                                            args.version, resources,
                                            client.messages))
    if args.canary_version:
      versions.append(
          update_instances_utils.ParseVersion(
              igm_ref.project, '--canary-version', args.canary_version,
              resources, client.messages))
    managed_instance_groups_utils.ValidateVersions(igm_info, versions,
                                                   args.force)

    # TODO(b/36049787): Decide what we should do when two versions have the same
    #              instance template (this can happen with canary restart
    #              performed using tags).
    igm_version_names = {
        version.instanceTemplate: version.name
        for version in igm_info.versions
    }
    for version in versions:
      if not version.name:
        version.name = igm_version_names.get(version.instanceTemplate)
    minimal_action = (client.messages.InstanceGroupManagerUpdatePolicy.
                      MinimalActionValueValuesEnum.REPLACE)

    update_policy = client.messages.InstanceGroupManagerUpdatePolicy(
        maxSurge=max_surge,
        maxUnavailable=max_unavailable,
        minReadySec=args.min_ready,
        minimalAction=minimal_action,
        type=update_policy_type)
    igm_resource = client.messages.InstanceGroupManager(
        instanceTemplate=None, updatePolicy=update_policy, versions=versions)
    if hasattr(igm_ref, 'zone'):
      service = client.apitools_client.instanceGroupManagers
      request = (client.messages.ComputeInstanceGroupManagersPatchRequest(
          instanceGroupManager=igm_ref.Name(),
          instanceGroupManagerResource=igm_resource,
          project=igm_ref.project,
          zone=igm_ref.zone))
    elif hasattr(igm_ref, 'region'):
      service = client.apitools_client.regionInstanceGroupManagers
      request = (client.messages.ComputeRegionInstanceGroupManagersPatchRequest(
          instanceGroupManager=igm_ref.Name(),
          instanceGroupManagerResource=igm_resource,
          project=igm_ref.project,
          region=igm_ref.region))
    # Due to 'Patch' semantics, we have to clear either 'fixed' or 'percent'.
    # Otherwise, we'll get an error that both 'fixed' and 'percent' are set.
    if max_surge is not None:
      cleared_fields.append('updatePolicy.maxSurge.fixed' if max_surge.fixed is
                            None else 'updatePolicy.maxSurge.percent')
    if max_unavailable is not None:
      cleared_fields.append('updatePolicy.maxUnavailable.fixed'
                            if max_unavailable.fixed is None else
                            'updatePolicy.maxUnavailable.percent')
    return (service, 'Patch', request)
Beispiel #7
0
  def CreateRequest(self, args, client, resources):
    resource_arg = instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG
    default_scope = compute_scope.ScopeEnum.ZONE
    scope_lister = flags.GetDefaultScopeLister(client)
    igm_ref = resource_arg.ResolveAsResource(
        args, resources, default_scope=default_scope, scope_lister=scope_lister)

    if igm_ref.Collection() not in [
        'compute.instanceGroupManagers', 'compute.regionInstanceGroupManagers'
    ]:
      raise ValueError('Unknown reference type {0}'.format(
          igm_ref.Collection()))

    update_policy_type = update_instances_utils.ParseUpdatePolicyType(
        '--type', args.type, client.messages)
    max_surge = update_instances_utils.ParseFixedOrPercent(
        '--max-surge', 'max-surge', args.max_surge, client.messages)
    max_unavailable = update_instances_utils.ParseFixedOrPercent(
        '--max-unavailable', 'max-unavailable', args.max_unavailable,
        client.messages)

    igm_info = managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
        igm_ref, client)

    versions = []
    versions.append(
        update_instances_utils.ParseVersion(igm_ref.project, '--version',
                                            args.version, resources,
                                            client.messages))
    if args.canary_version:
      versions.append(
          update_instances_utils.ParseVersion(igm_ref.project,
                                              '--canary-version',
                                              args.canary_version, resources,
                                              client.messages))
    managed_instance_groups_utils.ValidateVersions(igm_info, versions,
                                                   resources, args.force)

    # TODO(b/36049787): Decide what we should do when two versions have the same
    #              instance template (this can happen with canary restart
    #              performed using tags).
    igm_version_names = {
        version.instanceTemplate: version.name
        for version in igm_info.versions
    }
    for version in versions:
      if not version.name:
        version.name = igm_version_names.get(version.instanceTemplate)
    minimal_action = (client.messages.InstanceGroupManagerUpdatePolicy.
                      MinimalActionValueValuesEnum.REPLACE)

    update_policy = client.messages.InstanceGroupManagerUpdatePolicy(
        maxSurge=max_surge,
        maxUnavailable=max_unavailable,
        minimalAction=minimal_action,
        type=update_policy_type)
    # min_ready is available in alpha and beta APIs only
    if hasattr(args, 'min_ready'):
      update_policy.minReadySec = args.min_ready
    # replacement_method is available in alpha API only
    if hasattr(args, 'replacement_method'):
      replacement_method = update_instances_utils.ParseReplacementMethod(
          args.replacement_method, client.messages)
      update_policy.replacementMethod = replacement_method

    rolling_action.ValidateAndFixUpdaterAgainstStateful(update_policy, igm_ref,
                                                        igm_info, client, args)

    igm_resource = client.messages.InstanceGroupManager(
        instanceTemplate=None, updatePolicy=update_policy, versions=versions)
    if hasattr(igm_ref, 'zone'):
      service = client.apitools_client.instanceGroupManagers
      request = (client.messages.ComputeInstanceGroupManagersPatchRequest(
          instanceGroupManager=igm_ref.Name(),
          instanceGroupManagerResource=igm_resource,
          project=igm_ref.project,
          zone=igm_ref.zone))
    elif hasattr(igm_ref, 'region'):
      service = client.apitools_client.regionInstanceGroupManagers
      request = (client.messages.ComputeRegionInstanceGroupManagersPatchRequest(
          instanceGroupManager=igm_ref.Name(),
          instanceGroupManagerResource=igm_resource,
          project=igm_ref.project,
          region=igm_ref.region))
    return service, 'Patch', request