in perfkitbenchmarker/providers/gcp/gce_virtual_machine.py [0:0]
def _GenerateCreateCommand(self, ssh_keys_path):
"""Generates a command to create the VM instance.
Args:
ssh_keys_path: string. Path to a file containing the sshKeys metadata.
Returns:
GcloudCommand. gcloud command to issue in order to create the VM instance.
"""
args = ['compute', 'instances', 'create', self.name]
cmd = util.GcloudCommand(self, *args)
cmd.flags['async'] = True
if gcp_flags.GCE_CREATE_LOG_HTTP.value:
cmd.flags['log-http'] = True
if gcp_flags.GCE_NODE_GROUP.value:
cmd.flags['node-group'] = gcp_flags.GCE_NODE_GROUP.value
# Compute all flags requiring alpha first. Then if any flags are different
# between alpha and GA, we can set the appropriate ones.
if self.gce_egress_bandwidth_tier:
network_performance_configs = (
f'total-egress-bandwidth-tier={self.gce_egress_bandwidth_tier}'
)
cmd.flags['network-performance-configs'] = network_performance_configs
self.metadata['gce_egress_bandwidth_tier'] = (
self.gce_egress_bandwidth_tier
)
if gcp_flags.GCE_CONFIDENTIAL_COMPUTE.value:
# TODO(pclay): remove when on-host-maintenance gets promoted to GA
cmd.use_alpha_gcloud = True
if gcp_flags.GCE_CONFIDENTIAL_COMPUTE_TYPE.value == 'sev':
cmd.flags.update({'confidential-compute-type': 'SEV'})
cmd.flags.update({'on-host-maintenance': 'TERMINATE'})
elif self.on_host_maintenance:
# TODO(pclay): remove when on-host-maintenance gets promoted to GA
maintenance_flag = 'maintenance-policy'
if cmd.use_alpha_gcloud:
maintenance_flag = 'on-host-maintenance'
cmd.flags[maintenance_flag] = self.on_host_maintenance
if self.network.subnet_resources:
net_resources = self.network.subnet_resources
ni_arg_name = 'subnet'
else:
net_resources = self.network.network_resources
ni_arg_name = 'network'
# Bundle network-related arguments with --network-interface
# This flag is mutually exclusive with any of these flags:
# --address, --network, --network-tier, --subnet, --private-network-ip.
# gcloud compute instances create ... --network-interface=
for idx, net_resource in enumerate(net_resources):
gce_nic_type = self.gce_nic_types[idx].upper()
gce_nic_queue_count_arg = []
if gcp_flags.GCE_NIC_QUEUE_COUNTS.value[idx] != 'default':
gce_nic_queue_count_arg = [
f'queue-count={gcp_flags.GCE_NIC_QUEUE_COUNTS.value[idx]}'
]
no_address_arg = []
if not self.assign_external_ip or idx > 0:
no_address_arg = ['no-address']
cmd.additional_flags += [
'--network-interface',
','.join(
[
f'{ni_arg_name}={net_resource.name}',
f'nic-type={gce_nic_type}',
f'network-tier={self.gce_network_tier.upper()}',
]
+ gce_nic_queue_count_arg
+ no_address_arg
),
]
if self.image:
cmd.flags['image'] = self.image
elif self.image_family:
cmd.flags['image-family'] = self.image_family
self.metadata['image_family'] = self.image_family
if self.image_project is not None:
cmd.flags['image-project'] = self.image_project
self.metadata['image_project'] = self.image_project
for disk_ in self.disks:
cmd.flags.update(disk_.GetCreationCommand())
if self.machine_type is None:
cmd.flags['custom-cpu'] = self.cpus
cmd.flags['custom-memory'] = '{}MiB'.format(self.memory_mib)
else:
cmd.flags['machine-type'] = self.machine_type
if self.min_cpu_platform:
cmd.flags['min-cpu-platform'] = self.min_cpu_platform
# metal instances do not support disable SMT.
if self.threads_per_core and 'metal' not in self.machine_type:
cmd.flags['threads-per-core'] = self.threads_per_core
self.metadata['threads_per_core'] = self.threads_per_core
if self.visible_core_count:
cmd.flags['visible-core-count'] = self.visible_core_count
self.metadata['visible_core_count'] = self.visible_core_count
if self.gpu_count and (
self.cpus
or (
self.machine_type
and self.machine_type not in _FIXED_GPU_MACHINE_TYPES
)
):
cmd.flags['accelerator'] = GenerateAcceleratorSpecString(
self.gpu_type, self.gpu_count
)
cmd.flags['tags'] = ','.join(['perfkitbenchmarker'] + (self.gce_tags or []))
if not self.automatic_restart:
cmd.flags['no-restart-on-failure'] = True
self.metadata['automatic_restart'] = self.automatic_restart
if self.host:
cmd.flags['node-group'] = self.host.name
if self.gce_shielded_secure_boot:
cmd.flags['shielded-secure-boot'] = True
if self.network.placement_group:
self.metadata.update(self.network.placement_group.GetResourceMetadata())
cmd.flags['resource-policies'] = self.network.placement_group.name
else:
self.metadata['placement_group_style'] = (
placement_group.PLACEMENT_GROUP_NONE
)
metadata_from_file = {'sshKeys': ssh_keys_path}
if self.boot_metadata_from_file:
metadata_from_file.update(self.boot_metadata_from_file)
parsed_metadata_from_file = flag_util.ParseKeyValuePairs(
FLAGS.gcp_instance_metadata_from_file
)
for key, value in parsed_metadata_from_file.items():
if key in metadata_from_file:
logging.warning(
(
'Metadata "%s" is set internally. Cannot be overridden '
'from command line.'
),
key,
)
continue
metadata_from_file[key] = value
cmd.flags['metadata-from-file'] = ','.join(
['%s=%s' % (k, v) for k, v in metadata_from_file.items()]
)
# passing sshKeys does not work with OS Login
metadata = {'enable-oslogin': 'FALSE'}
metadata.update(self.boot_metadata)
metadata.update(util.GetDefaultTags())
# Signal (along with timeout_utc) that VM is short lived.
metadata['vm_nature'] = 'ephemeral'
additional_metadata = {}
additional_metadata.update(self.vm_metadata)
additional_metadata.update(
flag_util.ParseKeyValuePairs(FLAGS.gcp_instance_metadata)
)
for key, value in additional_metadata.items():
if key in metadata:
logging.warning(
(
'Metadata "%s" is set internally. Cannot be overridden '
'from command line.'
),
key,
)
continue
metadata[key] = value
if self.preemptible:
cmd.flags['preemptible'] = True
metadata.update([self._PreemptibleMetadataKeyValue()])
cmd.flags['metadata'] = util.FormatTags(metadata)
if (
self.machine_type is None
or self.machine_type not in gce_disk.FIXED_SSD_MACHINE_TYPES
):
# Append the `--local-ssd` args only when it's a customized or old-gen VM.
cmd.flags['local-ssd'] = [
'interface={}'.format(self.ssd_interface)
] * self.max_local_disks
cmd.flags.update(self.create_disk_strategy.GetCreationCommand())
if gcp_flags.GCE_VM_SERVICE_ACCOUNT.value:
cmd.flags['service-account'] = gcp_flags.GCE_VM_SERVICE_ACCOUNT.value
if gcp_flags.GCLOUD_SCOPES.value:
cmd.flags['scopes'] = ','.join(
re.split(r'[,; ]', gcp_flags.GCLOUD_SCOPES.value)
)
if gcp_flags.GCE_PERFORMANCE_MONITORING_UNIT.value:
cmd.flags['performance-monitoring-unit'] = (
gcp_flags.GCE_PERFORMANCE_MONITORING_UNIT.value
)
cmd.flags['labels'] = util.MakeFormattedDefaultTags()
return cmd