Ejemplo n.º 1
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        igm_ref = instance_groups_flags.CreateGroupReference(
            client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        mig_utils.GetInstanceGroupManagerOrThrow(igm_ref, client)

        old_autoscaler = mig_utils.AutoscalerForMigByRef(
            client, holder.resources, igm_ref)
        if mig_utils.IsAutoscalerNew(old_autoscaler):
            raise NoMatchingAutoscalerFoundError(
                'Instance group manager [{}] has no existing autoscaler; '
                'cannot update.'.format(igm_ref.Name()))

        autoscalers_client = autoscalers_api.GetClient(client, igm_ref)
        new_autoscaler = autoscalers_client.message_type(
            name=old_autoscaler.name,  # PATCH needs this
            autoscalingPolicy=client.messages.AutoscalingPolicy())
        if args.IsSpecified('mode'):
            mode = mig_utils.ParseModeString(args.mode, client.messages)
            new_autoscaler.autoscalingPolicy.mode = mode

        if self.scale_in:
            if args.IsSpecified('clear_scale_in_control'):
                new_autoscaler.autoscalingPolicy.scaleDownControl = None
            else:
                new_autoscaler.autoscalingPolicy.scaleDownControl = \
                  mig_utils.BuildScaleDown(args, client.messages)

        return self._SendPatchRequest(args, client, autoscalers_client,
                                      igm_ref, new_autoscaler)
Ejemplo n.º 2
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = instance_groups_flags.CreateGroupReference(
            client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        # Require confirmation if autoscaling a GKE node group.
        self._PromptToAutoscaleGKENodeGroup(args)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client, holder.resources, igm_ref, args)

        autoscalers_client = autoscalers_api.GetClient(client, igm_ref)
        if is_new:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource, igm_ref)
            return autoscalers_client.Insert(igm_ref, autoscaler_resource)
        return autoscalers_client.Update(igm_ref, autoscaler_resource)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        igm_ref = instance_groups_flags.CreateGroupReference(
            client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        mig_utils.GetInstanceGroupManagerOrThrow(igm_ref, client)

        old_autoscaler = mig_utils.AutoscalerForMigByRef(
            client, holder.resources, igm_ref)
        if mig_utils.IsAutoscalerNew(old_autoscaler):
            raise NoMatchingAutoscalerFoundError(
                'Instance group manager [{}] has no existing autoscaler; '
                'cannot update.'.format(igm_ref.Name()))

        autoscalers_client = autoscalers_api.GetClient(client, igm_ref)
        new_autoscaler = autoscalers_client.message_type(
            name=old_autoscaler.name,  # PATCH needs this
            autoscalingPolicy=client.messages.AutoscalingPolicy())
        if args.IsSpecified('mode'):
            mode = mig_utils.ParseModeString(args.mode, client.messages)
            new_autoscaler.autoscalingPolicy.mode = mode

        if args.IsSpecified('clear_scale_in_control'):
            new_autoscaler.autoscalingPolicy.scaleInControl = None
        else:
            new_autoscaler.autoscalingPolicy.scaleInControl = \
              mig_utils.BuildScaleIn(args, client.messages)

        if self.clear_scale_down and args.IsSpecified(
                'clear_scale_down_control'):
            new_autoscaler.autoscalingPolicy.scaleDownControl = None

        if args.IsSpecified('cpu_utilization_predictive_method'):
            cpu_predictive_enum = client.messages.AutoscalingPolicyCpuUtilization.PredictiveMethodValueValuesEnum
            new_autoscaler.autoscalingPolicy.cpuUtilization = client.messages.AutoscalingPolicyCpuUtilization(
            )
            new_autoscaler.autoscalingPolicy.cpuUtilization.predictiveMethod = arg_utils.ChoiceToEnum(
                args.cpu_utilization_predictive_method, cpu_predictive_enum)

        scheduled = mig_utils.BuildSchedules(args, client.messages)
        if scheduled:
            new_autoscaler.autoscalingPolicy.scalingSchedules = scheduled

        if args.IsSpecified('min_num_replicas'):
            new_autoscaler.autoscalingPolicy.minNumReplicas = args.min_num_replicas
        if args.IsSpecified('max_num_replicas'):
            new_autoscaler.autoscalingPolicy.maxNumReplicas = args.max_num_replicas

        return self._SendPatchRequest(args, client, autoscalers_client,
                                      igm_ref, new_autoscaler)
Ejemplo n.º 4
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    igm_ref = instance_groups_flags.CreateGroupReference(
        client, holder.resources, args)

    autoscaler = managed_instance_groups_utils.AutoscalerForMigByRef(
        client, holder.resources, igm_ref)
    if autoscaler:
      autoscaler_dict = encoding.MessageToDict(autoscaler)
      for f in _IGNORED_FIELDS:
        if f in autoscaler_dict:
          del autoscaler_dict[f]
    else:
      autoscaler_dict = None
    with open(args.autoscaling_file, 'w') as f:
      json.dump(autoscaler_dict, f)
Ejemplo n.º 5
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        managed_instance_groups_utils.ValidateAutoscalerArgs(args)
        managed_instance_groups_utils.ValidateStackdriverMetricsFlags(args)
        managed_instance_groups_utils.ValidateConflictsWithAutoscalingFile(
            args, (managed_instance_groups_utils.
                   ARGS_CONFLICTING_WITH_AUTOSCALING_FILE_ALPHA))
        igm_ref = instance_groups_flags.CreateGroupReference(
            client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client,
            holder.resources,
            igm_ref,
            args,
            scale_in=True,
            predictive=True)

        managed_instance_groups_utils.ValidateGeneratedAutoscalerIsValid(
            args, autoscaler_resource)

        autoscalers_client = autoscalers_api.GetClient(client, igm_ref)
        if args.IsSpecified('autoscaling_file'):
            if is_new:
                existing_autoscaler_name = None
            else:
                existing_autoscaler_name = autoscaler_resource.name
            return self._SetAutoscalerFromFile(args.autoscaling_file,
                                               autoscalers_client, igm_ref,
                                               existing_autoscaler_name)

        if is_new:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource, igm_ref)
            return autoscalers_client.Insert(igm_ref, autoscaler_resource)
        return autoscalers_client.Update(igm_ref, autoscaler_resource)