def _CreateDiskMessages(self, args, boot_disk_size_gb, image_uri,
                         instance_ref):
     """Creates API messages with disks attached to VM instance."""
     persistent_disks, _ = (
         instance_utils.CreatePersistentAttachedDiskMessages(
             self.resources, self.compute_client, None, args.disk or [],
             instance_ref))
     persistent_create_disks = (
         instance_utils.CreatePersistentCreateDiskMessages(
             self, self.compute_client, self.resources, None,
             getattr(args, 'create_disk', []), instance_ref))
     local_ssds = []
     for x in args.local_ssd or []:
         local_ssd = instance_utils.CreateLocalSsdMessage(
             self.resources, self.messages, x.get('device-name'),
             x.get('interface'), instance_ref.zone)
         local_ssds.append(local_ssd)
     boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
         self.compute_client,
         self.resources,
         disk_type=args.boot_disk_type,
         disk_device_name=args.boot_disk_device_name,
         disk_auto_delete=args.boot_disk_auto_delete,
         disk_size_gb=boot_disk_size_gb,
         require_csek_key_create=None,
         image_uri=image_uri,
         instance_ref=instance_ref,
         csek_keys=None)
     return ([boot_disk] + persistent_disks + persistent_create_disks +
             local_ssds)
Beispiel #2
0
    def _CreateDiskMessage(self, args, skip_defaults, instance_name, project,
                           zone, compute_client, resource_parser,
                           create_boot_disk, boot_disk_size_gb, image_uri,
                           csek_keys):
        persistent_disks = (
            instance_utils.CreatePersistentAttachedDiskMessages(
                resource_parser, compute_client, csek_keys, args.disk or [],
                project, zone))
        persistent_create_disks = (
            instance_utils.CreatePersistentCreateDiskMessages(
                compute_client,
                resource_parser,
                csek_keys,
                getattr(args, 'create_disk', []),
                project,
                zone,
                enable_kms=self._support_kms,
                enable_snapshots=True,
                resource_policy=self._support_disk_resource_policy,
                enable_source_snapshot_csek=self._support_source_snapshot_csek,
                enable_image_csek=self._support_image_csek))
        local_nvdimms = []
        if self._support_nvdimm:
            local_nvdimms = instance_utils.CreateLocalNvdimmMessages(
                args, resource_parser, compute_client.messages, zone, project)
        local_ssds = instance_utils.CreateLocalSsdMessages(
            args, resource_parser, compute_client.messages, zone, project)

        if create_boot_disk:
            boot_snapshot_uri = instance_utils.ResolveSnapshotURI(
                user_project=project,
                snapshot=args.source_snapshot,
                resource_parser=resource_parser)

            boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
                compute_client,
                resource_parser,
                disk_type=args.boot_disk_type,
                disk_device_name=args.boot_disk_device_name,
                disk_auto_delete=args.boot_disk_auto_delete,
                disk_size_gb=boot_disk_size_gb,
                require_csek_key_create=(args.require_csek_key_create
                                         if csek_keys else None),
                image_uri=image_uri,
                instance_name=instance_name,
                project=project,
                zone=zone,
                csek_keys=csek_keys,
                kms_args=args,
                snapshot_uri=boot_snapshot_uri,
                enable_kms=self._support_kms)
            persistent_disks = [boot_disk] + persistent_disks
        return persistent_disks + persistent_create_disks + local_nvdimms + local_ssds
 def _CreateDiskMessages(self, holder, args, boot_disk_size_gb, image_uri,
                         instance_ref, skip_defaults):
   """Creates API messages with disks attached to VM instance."""
   if (skip_defaults and not args.IsSpecified('disk') and
       not args.IsSpecified('create_disk') and
       not args.IsSpecified('local_ssd') and
       not args.IsSpecified('boot_disk_type') and
       not args.IsSpecified('boot_disk_device_name') and
       not args.IsSpecified('boot_disk_auto_delete')):
     return []
   else:
     persistent_disks, _ = (
         instance_utils.CreatePersistentAttachedDiskMessages(
             holder.resources, holder.client, None, args.disk or [],
             instance_ref))
     persistent_create_disks = (
         instance_utils.CreatePersistentCreateDiskMessages(
             holder.client, holder.resources, None,
             getattr(args, 'create_disk', []), instance_ref))
     local_ssds = []
     for x in args.local_ssd or []:
       local_ssd = instance_utils.CreateLocalSsdMessage(
           holder.resources,
           holder.client.messages,
           x.get('device-name'),
           x.get('interface'),
           x.get('size'),
           instance_ref.zone,
           instance_ref.project)
       local_ssds.append(local_ssd)
     boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
         holder.client, holder.resources,
         disk_type=args.boot_disk_type,
         disk_device_name=args.boot_disk_device_name,
         disk_auto_delete=args.boot_disk_auto_delete,
         disk_size_gb=boot_disk_size_gb,
         require_csek_key_create=None,
         image_uri=image_uri,
         instance_ref=instance_ref,
         csek_keys=None)
     return (
         [boot_disk] + persistent_disks + persistent_create_disks + local_ssds)
Beispiel #4
0
  def _GetDiskMessagess(
      self, args, skip_defaults, instance_refs, compute_client,
      resource_parser, create_boot_disk, boot_disk_size_gb, image_uri,
      csek_keys):
    flags_to_check = [
        'disk', 'local_ssd', 'boot_disk_type', 'boot_disk_device_name',
        'boot_disk_auto_delete', 'require_csek_key_create',
    ]
    if self._support_kms:
      flags_to_check.extend([
          'create_disk', 'boot_disk_kms_key', 'boot_disk_kms_project',
          'boot_disk_kms_location', 'boot_disk_kms_keyring',
      ])
    if (skip_defaults and
        not instance_utils.IsAnySpecified(args, *flags_to_check)):
      return [[] for _ in instance_refs]

    # A list of lists where the element at index i contains a list of
    # disk messages that should be set for the instance at index i.
    disks_messages = []

    # A mapping of zone to boot disk references for all existing boot
    # disks that are being attached.
    # TODO(b/36050875): Simplify since resources.Resource is now hashable.
    existing_boot_disks = {}
    for instance_ref in instance_refs:
      persistent_disks, boot_disk_ref = (
          instance_utils.CreatePersistentAttachedDiskMessages(
              resource_parser, compute_client, csek_keys,
              args.disk or [], instance_ref))
      persistent_create_disks = (
          instance_utils.CreatePersistentCreateDiskMessages(
              compute_client,
              resource_parser,
              csek_keys,
              getattr(args, 'create_disk', []),
              instance_ref))
      local_ssds = []
      for x in args.local_ssd or []:
        local_ssds.append(
            instance_utils.CreateLocalSsdMessage(
                resource_parser,
                compute_client.messages,
                x.get('device-name'),
                x.get('interface'),
                x.get('size'),
                instance_ref.zone,
                instance_ref.project)
        )

      if create_boot_disk:
        boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
            compute_client, resource_parser,
            disk_type=args.boot_disk_type,
            disk_device_name=args.boot_disk_device_name,
            disk_auto_delete=args.boot_disk_auto_delete,
            disk_size_gb=boot_disk_size_gb,
            require_csek_key_create=(
                args.require_csek_key_create if csek_keys else None),
            image_uri=image_uri,
            instance_ref=instance_ref,
            csek_keys=csek_keys,
            kms_args=args)
        persistent_disks = [boot_disk] + persistent_disks
      else:
        existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
      disks_messages.append(persistent_disks + persistent_create_disks +
                            local_ssds)
    return disks_messages
Beispiel #5
0
  def CreateRequests(self, args):
    instances_flags.ValidateDiskFlags(args)
    instances_flags.ValidateLocalSsdFlags(args)
    instances_flags.ValidateNicFlags(args)

    # This feature is only exposed in alpha/beta
    allow_rsa_encrypted = self.ReleaseTrack() in [base.ReleaseTrack.ALPHA,
                                                  base.ReleaseTrack.BETA]
    self.csek_keys = csek_utils.CsekKeyStore.FromArgs(args, allow_rsa_encrypted)

    scheduling = instance_utils.CreateSchedulingMessage(
        messages=self.messages,
        maintenance_policy=args.maintenance_policy,
        preemptible=args.preemptible,
        restart_on_failure=args.restart_on_failure)

    service_accounts = instance_utils.CreateServiceAccountMessages(
        messages=self.messages,
        scopes=([] if args.no_scopes else args.scopes))

    if args.tags:
      tags = self.messages.Tags(items=args.tags)
    else:
      tags = None

    metadata = metadata_utils.ConstructMetadataMessage(
        self.messages,
        metadata=args.metadata,
        metadata_from_file=args.metadata_from_file)

    # If the user already provided an initial Windows password and
    # username through metadata, then there is no need to check
    # whether the image or the boot disk is Windows.

    boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
    utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

    instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
        args, self.resources, scope_lister=flags.GetDefaultScopeLister(
            self.compute_client, self.project))

    # Check if the zone is deprecated or has maintenance coming.
    self.WarnForZonalCreation(instance_refs)

    if hasattr(args, 'network_interface') and args.network_interface:
      network_interfaces = instance_utils.CreateNetworkInterfaceMessages(
          resources=self.resources,
          compute_client=self.compute_client,
          network_interface_arg=args.network_interface,
          instance_refs=instance_refs)
    else:
      network_interfaces = [
          instance_utils.CreateNetworkInterfaceMessage(
              resources=self.resources,
              compute_client=self.compute_client,
              network=args.network,
              subnet=args.subnet,
              private_network_ip=args.private_network_ip,
              no_address=args.no_address,
              address=args.address,
              instance_refs=instance_refs)
      ]

    machine_type_uris = instance_utils.CreateMachineTypeUris(
        resources=self.resources,
        compute_client=self.compute_client,
        project=self.project,
        machine_type=args.machine_type,
        custom_cpu=args.custom_cpu,
        custom_memory=args.custom_memory,
        instance_refs=instance_refs)

    create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk or [])
    if create_boot_disk:
      image_uri, _ = self.ExpandImageFlag(
          image=args.image,
          image_family=args.image_family,
          image_project=args.image_project,
          return_image_resource=False)
    else:
      image_uri = None

    # A list of lists where the element at index i contains a list of
    # disk messages that should be set for the instance at index i.
    disks_messages = []

    # A mapping of zone to boot disk references for all existing boot
    # disks that are being attached.
    # TODO(user): Simplify this once resources.Resource becomes
    # hashable.
    existing_boot_disks = {}

    for instance_ref in instance_refs:
      persistent_disks, boot_disk_ref = (
          instance_utils.CreatePersistentAttachedDiskMessages(
              self.resources, self.compute_client, self.csek_keys,
              args.disk or [], instance_ref))
      persistent_create_disks = (
          instance_utils.CreatePersistentCreateDiskMessages(
              self,
              self.compute_client,
              self.resources,
              self.csek_keys,
              getattr(args, 'create_disk', []),
              instance_ref))
      local_ssds = []
      for x in args.local_ssd or []:
        local_ssds.append(
            instance_utils.CreateLocalSsdMessage(
                self.resources,
                self.messages,
                x.get('device-name'),
                x.get('interface'),
                instance_ref.zone)
        )

      if create_boot_disk:
        boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
            self.compute_client, self.resources,
            disk_type=args.boot_disk_type,
            disk_device_name=args.boot_disk_device_name,
            disk_auto_delete=args.boot_disk_auto_delete,
            disk_size_gb=boot_disk_size_gb,
            require_csek_key_create=(
                args.require_csek_key_create if self.csek_keys else None),
            image_uri=image_uri,
            instance_ref=instance_ref,
            csek_keys=self.csek_keys)
        persistent_disks = [boot_disk] + persistent_disks
      else:
        existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
      disks_messages.append(persistent_disks + persistent_create_disks +
                            local_ssds)

    requests = []
    for instance_ref, machine_type_uri, disks in zip(
        instance_refs, machine_type_uris, disks_messages):
      requests.append(self.messages.ComputeInstancesInsertRequest(
          instance=self.messages.Instance(
              canIpForward=args.can_ip_forward,
              disks=disks,
              description=args.description,
              machineType=machine_type_uri,
              metadata=metadata,
              name=instance_ref.Name(),
              networkInterfaces=network_interfaces,
              serviceAccounts=service_accounts,
              scheduling=scheduling,
              tags=tags,
          ),
          project=self.project,
          zone=instance_ref.zone))

    return requests
Beispiel #6
0
    def _CreateRequests(self, args, compute_client, resource_parser):
        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        self.csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, allow_rsa_encrypted)

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=compute_client.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        if args.tags:
            tags = compute_client.messages.Tags(items=args.tags)
        else:
            tags = None

        metadata = metadata_utils.ConstructMetadataMessage(
            compute_client.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        # If the user already provided an initial Windows password and
        # username through metadata, then there is no need to check
        # whether the image or the boot disk is Windows.

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
            args,
            resource_parser,
            scope_lister=flags.GetDefaultScopeLister(compute_client))

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(compute_client)
        zone_resource_fetcher.WarnForZonalCreation(instance_refs)

        network_interface_arg = getattr(args, 'network_interface', None)
        if network_interface_arg:
            network_interfaces = instance_utils.CreateNetworkInterfaceMessages(
                resources=resource_parser,
                compute_client=compute_client,
                network_interface_arg=network_interface_arg,
                instance_refs=instance_refs,
                support_network_tier=self._support_network_tier)
        else:
            if self._support_public_dns is True:
                instances_flags.ValidatePublicDnsFlags(args)

            network_tier = getattr(args, 'network_tier', None)

            network_interfaces = [
                instance_utils.CreateNetworkInterfaceMessage(
                    resources=resource_parser,
                    compute_client=compute_client,
                    network=args.network,
                    subnet=args.subnet,
                    private_network_ip=args.private_network_ip,
                    no_address=args.no_address,
                    address=args.address,
                    instance_refs=instance_refs,
                    network_tier=network_tier,
                    no_public_dns=getattr(args, 'no_public_dns', None),
                    public_dns=getattr(args, 'public_dns', None),
                    no_public_ptr=getattr(args, 'no_public_ptr', None),
                    public_ptr=getattr(args, 'public_ptr', None),
                    no_public_ptr_domain=getattr(args, 'no_public_ptr_domain',
                                                 None),
                    public_ptr_domain=getattr(args, 'public_ptr_domain', None))
            ]

        machine_type_uris = instance_utils.CreateMachineTypeUris(
            resources=resource_parser,
            compute_client=compute_client,
            project=instance_refs[0].project,
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            ext=getattr(args, 'custom_extensions', None),
            instance_refs=instance_refs)

        create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk
                                                                  or [])
        if create_boot_disk:
            image_expander = image_utils.ImageExpander(compute_client,
                                                       resource_parser)
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_refs[0].project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            image_uri = None

        # A list of lists where the element at index i contains a list of
        # disk messages that should be set for the instance at index i.
        disks_messages = []

        # A mapping of zone to boot disk references for all existing boot
        # disks that are being attached.
        # TODO(b/36050875): Simplify since resources.Resource is now hashable.
        existing_boot_disks = {}

        for instance_ref in instance_refs:
            persistent_disks, boot_disk_ref = (
                instance_utils.CreatePersistentAttachedDiskMessages(
                    resource_parser, compute_client, self.csek_keys, args.disk
                    or [], instance_ref))
            persistent_create_disks = (
                instance_utils.CreatePersistentCreateDiskMessages(
                    compute_client, resource_parser, self.csek_keys,
                    getattr(args, 'create_disk', []), instance_ref))
            local_ssds = []
            for x in args.local_ssd or []:
                local_ssds.append(
                    instance_utils.CreateLocalSsdMessage(
                        resource_parser, compute_client.messages,
                        x.get('device-name'), x.get('interface'),
                        x.get('size'), instance_ref.zone))

            if create_boot_disk:
                boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
                    compute_client,
                    resource_parser,
                    disk_type=args.boot_disk_type,
                    disk_device_name=args.boot_disk_device_name,
                    disk_auto_delete=args.boot_disk_auto_delete,
                    disk_size_gb=boot_disk_size_gb,
                    require_csek_key_create=(args.require_csek_key_create
                                             if self.csek_keys else None),
                    image_uri=image_uri,
                    instance_ref=instance_ref,
                    csek_keys=self.csek_keys)
                persistent_disks = [boot_disk] + persistent_disks
            else:
                existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
            disks_messages.append(persistent_disks + persistent_create_disks +
                                  local_ssds)

        accelerator_args = getattr(args, 'accelerator', None)

        project_to_sa = {}
        requests = []
        for instance_ref, machine_type_uri, disks in zip(
                instance_refs, machine_type_uris, disks_messages):
            if instance_ref.project not in project_to_sa:
                scopes = None
                if not args.no_scopes and not args.scopes:
                    # User didn't provide any input on scopes. If project has no default
                    # service account then we want to create a VM with no scopes
                    request = (
                        compute_client.apitools_client.projects, 'Get',
                        compute_client.messages.ComputeProjectsGetRequest(
                            project=instance_ref.project))
                    errors = []
                    result = compute_client.MakeRequests([request], errors)
                    if not errors:
                        if not result[0].defaultServiceAccount:
                            scopes = []
                            log.status.Print(
                                'There is no default service account for project {}. '
                                'Instance {} will not have scopes.'.format(
                                    instance_ref.project, instance_ref.Name))
                if scopes is None:
                    scopes = [] if args.no_scopes else args.scopes

                if args.no_service_account:
                    service_account = None
                else:
                    service_account = args.service_account
                service_accounts = instance_utils.CreateServiceAccountMessages(
                    messages=compute_client.messages,
                    scopes=scopes,
                    service_account=service_account)
                project_to_sa[instance_ref.project] = service_accounts

            instance = compute_client.messages.Instance(
                canIpForward=args.can_ip_forward,
                disks=disks,
                description=args.description,
                machineType=machine_type_uri,
                metadata=metadata,
                name=instance_ref.Name(),
                networkInterfaces=network_interfaces,
                serviceAccounts=project_to_sa[instance_ref.project],
                scheduling=scheduling,
                tags=tags)
            if getattr(args, 'min_cpu_platform', None):
                instance.minCpuPlatform = args.min_cpu_platform
            if accelerator_args:
                accelerator_type_name = accelerator_args['type']
                accelerator_type_ref = resource_parser.Parse(
                    accelerator_type_name,
                    collection='compute.acceleratorTypes',
                    params={
                        'project': instance_ref.project,
                        'zone': instance_ref.zone
                    })
                # Accelerator count is default to 1.
                accelerator_count = int(accelerator_args.get('count', 1))
                accelerators = instance_utils.CreateAcceleratorConfigMessages(
                    compute_client.messages, accelerator_type_ref,
                    accelerator_count)
                instance.guestAccelerators = accelerators

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

            sole_tenancy_host_arg = getattr(args, 'sole_tenancy_host', None)
            if sole_tenancy_host_arg:
                sole_tenancy_host_ref = resource_parser.Parse(
                    sole_tenancy_host_arg,
                    collection='compute.hosts',
                    params={
                        'project': instance_ref.project,
                        'zone': instance_ref.zone
                    })
                request.instance.host = sole_tenancy_host_ref.SelfLink()
            requests.append(
                (compute_client.apitools_client.instances, 'Insert', request))
        return requests