def _ValidateInstancesFlags(
    args,
    support_kms=False,
    support_max_run_duration=False
):
  """Validate flags for instance template that affects instance creation.

  Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.
      support_kms: If KMS is supported.
      support_max_run_duration: max-run-durrations is supported in instance
        scheduling.
  """
  instances_flags.ValidateDiskCommonFlags(args)
  instances_flags.ValidateDiskBootFlags(args, enable_kms=support_kms)
  instances_flags.ValidateCreateDiskFlags(args)
  instances_flags.ValidateLocalSsdFlags(args)
  instances_flags.ValidateNicFlags(args)
  instances_flags.ValidateServiceAccountAndScopeArgs(args)
  instances_flags.ValidateAcceleratorArgs(args)
  instances_flags.ValidateReservationAffinityGroup(args)
  instances_flags.ValidateNetworkPerformanceConfigsArgs(args)
  instances_flags.ValidateInstanceScheduling(
      args, support_max_run_duration=support_max_run_duration)
 def _ValidateArgs(self, args):
   self._ValidateTrackSpecificArgs(args)
   instances_flags.ValidateAcceleratorArgs(args)
   instances_flags.ValidateNicFlags(args)
   instances_flags.ValidateNetworkTierArgs(args)
   instances_flags.ValidateKonletArgs(args)
   instances_flags.ValidateDiskCommonFlags(args)
   instances_flags.ValidateServiceAccountAndScopeArgs(args)
   instances_flags.ValidatePublicPtrFlags(args)
   instances_flags.ValidateNetworkPerformanceConfigsArgs(args)
   instances_flags.ValidateInstanceScheduling(args)
   if instance_utils.UseExistingBootDisk(args.disk or []):
     raise exceptions.InvalidArgumentException(
         '--disk', 'Boot disk specified for containerized VM.')
    def Run(self, args):
        """Runs bulk create command.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.

    Returns:
      A resource object dispatched by display.Displayer().
    """

        instances_flags.ValidateBulkCreateArgs(args)
        if self._support_enable_target_shape:
            instances_flags.ValidateBulkTargetShapeArgs(args)
        instances_flags.ValidateLocationPolicyArgs(args)
        instances_flags.ValidateBulkDiskFlags(
            args,
            enable_source_snapshot_csek=self._support_source_snapshot_csek,
            enable_image_csek=self._support_image_csek)
        instances_flags.ValidateImageFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateNicFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        instances_flags.ValidateAcceleratorArgs(args)
        instances_flags.ValidateNetworkTierArgs(args)
        instances_flags.ValidateReservationAffinityGroup(args)
        instances_flags.ValidateNetworkPerformanceConfigsArgs(args)
        instances_flags.ValidateInstanceScheduling(
            args, support_max_run_duration=self._support_max_run_duration)

        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        compute_client = holder.client
        resource_parser = holder.resources

        project = properties.VALUES.core.project.GetOrFail()
        location = None
        scope = None

        if args.IsSpecified('zone'):
            location = args.zone
            scope = compute_scopes.ScopeEnum.ZONE
        elif args.IsSpecified('region'):
            location = args.region
            scope = compute_scopes.ScopeEnum.REGION

        instances_service, request = self._CreateRequests(
            args, holder, compute_client, resource_parser, project, location,
            scope)

        self._errors = []
        self._log_async = False
        self._status_message = None

        if args.async_:
            self._log_async = True
            try:
                response = instances_service.BulkInsert(request)
                self._operation_selflink = response.selfLink
                return {'operationGroupId': response.operationGroupId}
            except exceptions.HttpException as error:
                raise error

        errors_to_collect = []
        response = compute_client.MakeRequests(
            [(instances_service, 'BulkInsert', request)],
            errors_to_collect=errors_to_collect,
            log_result=False,
            always_return_operation=True,
            no_followup=True)

        self._errors = errors_to_collect
        if response:
            operation_group_id = response[0].operationGroupId
            result = _GetResult(compute_client, request, operation_group_id)
            if result['createdInstanceCount'] is not None and result[
                    'failedInstanceCount'] is not None:
                self._status_message = 'VM instances created: {}, failed: {}.'.format(
                    result['createdInstanceCount'],
                    result['failedInstanceCount'])
            return result
        return
Esempio n. 4
0
    def Run(self, args):
        instances_flags.ValidateDiskFlags(
            args,
            enable_kms=self._support_kms,
            enable_snapshots=True,
            enable_source_snapshot_csek=self._support_source_snapshot_csek,
            enable_image_csek=self._support_image_csek)
        instances_flags.ValidateImageFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateNicFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        instances_flags.ValidateAcceleratorArgs(args)
        instances_flags.ValidateNetworkTierArgs(args)
        instances_flags.ValidateReservationAffinityGroup(args)
        instances_flags.ValidateNetworkPerformanceConfigsArgs(args)
        instances_flags.ValidateInstanceScheduling(
            args, support_max_run_duration=self._support_max_run_duration)

        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        compute_client = holder.client
        resource_parser = holder.resources

        instance_refs = instance_utils.GetInstanceRefs(args, compute_client,
                                                       holder)
        if len(instance_refs) > 1 and args.IsSpecified('address'):
            raise exceptions.BadArgumentException(
                '--address',
                'Multiple instances were specified for creation. --address flag can '
                'be used when creating a single instance.')

        requests = self._CreateRequests(args, instance_refs,
                                        instance_refs[0].project,
                                        instance_refs[0].zone, compute_client,
                                        resource_parser, holder)
        if not args.async_:
            # TODO(b/63664449): Replace this with poller + progress tracker.
            try:
                # Using legacy MakeRequests (which also does polling) here until
                # replaced by api_lib.utils.waiter.
                return compute_client.MakeRequests(requests)
            except exceptions.ToolException as e:
                invalid_machine_type_message_regex = (
                    r'Invalid value for field \'resource.machineType\': .+. '
                    r'Machine type with name \'.+\' does not exist in zone \'.+\'\.'
                )
                if re.search(invalid_machine_type_message_regex,
                             six.text_type(e)):
                    raise compute_exceptions.ArgumentError(
                        six.text_type(e) +
                        '\nUse `gcloud compute machine-types list --zones` to see the '
                        'available machine  types.')
                raise

        errors_to_collect = []
        responses = compute_client.BatchRequests(requests, errors_to_collect)
        for r in responses:
            err = getattr(r, 'error', None)
            if err:
                errors_to_collect.append(poller.OperationErrors(err.errors))
        if errors_to_collect:
            raise core_exceptions.MultiError(errors_to_collect)

        operation_refs = [
            holder.resources.Parse(r.selfLink) for r in responses
        ]

        log.status.Print(
            'NOTE: The users will be charged for public IPs when VMs '
            'are created.')

        for instance_ref, operation_ref in zip(instance_refs, operation_refs):
            log.status.Print(
                'Instance creation in progress for [{}]: {}'.format(
                    instance_ref.instance, operation_ref.SelfLink()))
        log.status.Print(
            'Use [gcloud compute operations describe URI] command '
            'to check the status of the operation(s).')
        if not args.IsSpecified('format'):
            # For async output we need a separate format. Since we already printed in
            # the status messages information about operations there is nothing else
            # needs to be printed.
            args.format = 'disable'
        return responses
    def _Run(self, args):
        """Issues request necessary for setting scheduling options."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        instance_ref = flags.INSTANCE_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=flags.GetInstanceZoneScopeLister(client))

        scheduling_options = client.messages.Scheduling()

        scheduling_options.automaticRestart = args.restart_on_failure

        if args.IsSpecified('preemptible'):
            scheduling_options.preemptible = args.preemptible

        if self._support_host_error_timeout_seconds and hasattr(
                args, 'host_error_timeout_seconds'):
            scheduling_options.hostErrorTimeoutSeconds = args.host_error_timeout_seconds

        if (hasattr(args, 'provisioning_model')
                and args.IsSpecified('provisioning_model')):
            scheduling_options.provisioningModel = (
                client.messages.Scheduling.ProvisioningModelValueValuesEnum(
                    args.provisioning_model))

        cleared_fields = []

        if (hasattr(args, 'instance_termination_action')
                and args.IsSpecified('instance_termination_action')):
            flags.ValidateInstanceScheduling(args)
            scheduling_options.instanceTerminationAction = (
                client.messages.Scheduling.
                InstanceTerminationActionValueValuesEnum(
                    args.instance_termination_action))
        elif args.IsSpecified('clear_instance_termination_action'):
            scheduling_options.instanceTerminationAction = None
            cleared_fields.append('instanceTerminationAction')

        if args.IsSpecified('min_node_cpu'):
            scheduling_options.minNodeCpus = int(args.min_node_cpu)
        elif args.IsSpecified('clear_min_node_cpu'):
            scheduling_options.minNodeCpus = None
            cleared_fields.append('minNodeCpus')

        if args.IsSpecified('maintenance_policy'):
            scheduling_options.onHostMaintenance = (
                client.messages.Scheduling.OnHostMaintenanceValueValuesEnum(
                    args.maintenance_policy))

        if instance_utils.IsAnySpecified(args, 'node', 'node_affinity_file',
                                         'node_group'):
            affinities = sole_tenancy_util.GetSchedulingNodeAffinityListFromArgs(
                args, client.messages)
            scheduling_options.nodeAffinities = affinities
        elif args.IsSpecified('clear_node_affinities'):
            scheduling_options.nodeAffinities = []
            cleared_fields.append('nodeAffinities')

        with holder.client.apitools_client.IncludeFields(cleared_fields):
            request = client.messages.ComputeInstancesSetSchedulingRequest(
                instance=instance_ref.Name(),
                project=instance_ref.project,
                scheduling=scheduling_options,
                zone=instance_ref.zone)

            return client.MakeRequests([(client.apitools_client.instances,
                                         'SetScheduling', request)])