示例#1
0
  def Run(self, args):
    instances_flags.ValidateDiskFlags(args, enable_kms=self._support_kms,
                                      enable_snapshots=self._support_snapshots)
    instances_flags.ValidateLocalSsdFlags(args)
    instances_flags.ValidateNicFlags(args)
    instances_flags.ValidateServiceAccountAndScopeArgs(args)
    instances_flags.ValidateAcceleratorArgs(args)
    instances_flags.ValidateNetworkTierArgs(args)
    instances_flags.ValidateAllocationAffinityGroup(args)

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

    instance_refs = instance_utils.GetInstanceRefs(args, compute_client, holder)

    requests = self._CreateRequests(
        args, instance_refs, 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 exceptions.ToolException(
              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]

    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
示例#2
0
    def Run(self, args):
        self._ValidateArgs(args)

        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        source_instance_template = instance_utils.GetSourceInstanceTemplate(
            args, holder.resources, self.SOURCE_INSTANCE_TEMPLATE)
        skip_defaults = instance_utils.GetSkipDefaults(
            source_instance_template)
        scheduling = instance_utils.GetScheduling(args, client, skip_defaults)
        service_accounts = instance_utils.GetServiceAccounts(
            args, client, skip_defaults)
        user_metadata = instance_utils.GetValidatedMetadata(args, client)
        boot_disk_size_gb = instance_utils.GetBootDiskSizeGb(args)
        instance_refs = instance_utils.GetInstanceRefs(args, client, holder)
        network_interfaces = self.GetNetworkInterfaces(args, holder.resources,
                                                       client, holder,
                                                       instance_refs,
                                                       skip_defaults)
        machine_type_uris = instance_utils.GetMachineTypeUris(
            args, client, holder, instance_refs, skip_defaults)
        image_uri = self.GetImageUri(args, client, holder, instance_refs)
        labels = containers_utils.GetLabelsMessageWithCosVersion(
            args.labels, image_uri, holder.resources, client.messages.Instance)
        can_ip_forward = instance_utils.GetCanIpForward(args, skip_defaults)
        tags = containers_utils.CreateTagsMessage(client.messages, args.tags)

        requests = []
        for instance_ref, machine_type_uri in zip(instance_refs,
                                                  machine_type_uris):
            metadata = containers_utils.CreateKonletMetadataMessage(
                client.messages, args, instance_ref.Name(), user_metadata)
            disks = instance_utils.CreateDiskMessages(holder, args,
                                                      boot_disk_size_gb,
                                                      image_uri, instance_ref,
                                                      skip_defaults)
            request = client.messages.ComputeInstancesInsertRequest(
                instance=client.messages.Instance(
                    canIpForward=can_ip_forward,
                    disks=disks,
                    description=args.description,
                    labels=labels,
                    machineType=machine_type_uri,
                    metadata=metadata,
                    minCpuPlatform=args.min_cpu_platform,
                    name=instance_ref.Name(),
                    networkInterfaces=network_interfaces,
                    serviceAccounts=service_accounts,
                    scheduling=scheduling,
                    tags=tags),
                sourceInstanceTemplate=source_instance_template,
                project=instance_ref.project,
                zone=instance_ref.zone)

            requests.append(
                (client.apitools_client.instances, 'Insert', request))

        return client.MakeRequests(requests)
示例#3
0
    def Run(self, args):
        self._ValidateArgs(args)

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

        container_mount_disk = instances_flags.GetValidatedContainerMountDisk(
            holder, args.container_mount_disk, args.disk, args.create_disk)
        source_instance_template = instance_utils.GetSourceInstanceTemplate(
            args, resource_parser, self.SOURCE_INSTANCE_TEMPLATE)
        skip_defaults = instance_utils.GetSkipDefaults(
            source_instance_template)
        scheduling = instance_utils.GetScheduling(args,
                                                  compute_client,
                                                  skip_defaults,
                                                  support_min_node_cpu=False)
        service_accounts = instance_utils.GetServiceAccounts(
            args, compute_client, skip_defaults)
        user_metadata = instance_utils.GetValidatedMetadata(
            args, compute_client)
        boot_disk_size_gb = instance_utils.GetBootDiskSizeGb(args)
        instance_refs = instance_utils.GetInstanceRefs(args, compute_client,
                                                       holder)
        network_interfaces = self.GetNetworkInterfaces(
            args, resource_parser, compute_client, holder,
            instance_refs[0].project, instance_refs[0].zone,
            compute_scopes.ScopeEnum.ZONE, skip_defaults)
        image_uri = self.GetImageUri(args, compute_client, resource_parser,
                                     instance_refs)
        labels = containers_utils.GetLabelsMessageWithCosVersion(
            args.labels, image_uri, resource_parser,
            compute_client.messages.Instance)
        can_ip_forward = instance_utils.GetCanIpForward(args, skip_defaults)
        tags = containers_utils.CreateTagsMessage(compute_client.messages,
                                                  args.tags)
        confidential_vm = (args.IsSpecified('confidential_compute')
                           and args.confidential_compute)

        requests = []
        for instance_ref in instance_refs:
            metadata = containers_utils.CreateKonletMetadataMessage(
                compute_client.messages,
                args,
                instance_ref.Name(),
                user_metadata,
                container_mount_disk_enabled=self.
                _container_mount_disk_enabled,
                container_mount_disk=container_mount_disk)

            disks = []
            if self.CheckDiskMessageArgs(args, skip_defaults):
                disks = create_utils.CreateDiskMessages(
                    args=args,
                    instance_name=instance_ref.Name(),
                    project=instance_ref.project,
                    location=instance_ref.zone,
                    scope=compute_scopes.ScopeEnum.ZONE,
                    compute_client=compute_client,
                    resource_parser=resource_parser,
                    boot_disk_size_gb=boot_disk_size_gb,
                    image_uri=image_uri,
                    create_boot_disk=self._support_create_boot_disk,
                    support_nvdimm=self._support_nvdimm,
                    support_match_container_mount_disks=self.
                    _support_match_container_mount_disks)

            machine_type_uri = None
            if instance_utils.CheckSpecifiedMachineTypeArgs(
                    args, skip_defaults):
                machine_type_uri = create_utils.CreateMachineTypeUri(
                    args=args,
                    compute_client=compute_client,
                    resource_parser=resource_parser,
                    project=instance_ref.project,
                    location=instance_ref.zone,
                    scope=compute_scopes.ScopeEnum.ZONE,
                    confidential_vm=confidential_vm)

            guest_accelerators = create_utils.GetAccelerators(
                args=args,
                compute_client=compute_client,
                resource_parser=resource_parser,
                project=instance_ref.project,
                location=instance_ref.zone,
                scope=compute_scopes.ScopeEnum.ZONE)

            instance = compute_client.messages.Instance(
                canIpForward=can_ip_forward,
                disks=disks,
                guestAccelerators=guest_accelerators,
                description=args.description,
                labels=labels,
                machineType=machine_type_uri,
                metadata=metadata,
                minCpuPlatform=args.min_cpu_platform,
                name=instance_ref.Name(),
                networkInterfaces=network_interfaces,
                serviceAccounts=service_accounts,
                scheduling=scheduling,
                tags=tags)
            if args.private_ipv6_google_access_type is not None:
                instance.privateIpv6GoogleAccess = (
                    instances_flags.GetPrivateIpv6GoogleAccessTypeFlagMapper(
                        compute_client.messages).GetEnumForChoice(
                            args.private_ipv6_google_access_type))

            confidential_instance_config = (
                create_utils.BuildConfidentialInstanceConfigMessage(
                    messages=compute_client.messages, args=args))
            if confidential_instance_config:
                instance.confidentialInstanceConfig = confidential_instance_config

            has_threads_per_core = (self._support_threads_per_core
                                    and args.threads_per_core is not None)
            if (args.enable_nested_virtualization is not None
                    or has_threads_per_core):
                threads_per_core = args.threads_per_core if has_threads_per_core else None
                instance.advancedMachineFeatures = (
                    instance_utils.CreateAdvancedMachineFeaturesMessage(
                        compute_client.messages,
                        args.enable_nested_virtualization, threads_per_core))

            shielded_instance_config = create_utils.BuildShieldedInstanceConfigMessage(
                messages=compute_client.messages, args=args)
            if shielded_instance_config:
                instance.shieldedInstanceConfig = shielded_instance_config

            if self._support_network_performance_configs and \
                args.IsSpecified('network_performance_configs'):
                instance.networkPerformanceConfig = \
                    instance_utils.GetNetworkPerformanceConfig(args, compute_client)

            request = compute_client.messages.ComputeInstancesInsertRequest(
                instance=instance,
                sourceInstanceTemplate=source_instance_template,
                project=instance_ref.project,
                zone=instance_ref.zone)

            requests.append(
                (compute_client.apitools_client.instances, 'Insert', request))

        return compute_client.MakeRequests(requests)