def _Create(self): """Create a GCE VM instance.""" super(GceVirtualMachine, self)._Create() with open(self.ssh_public_key) as f: public_key = f.read().rstrip('\n') with tempfile.NamedTemporaryFile(dir=vm_util.GetTempDir(), prefix='key-metadata') as tf: tf.write('%s:%s\n' % (self.user_name, public_key)) tf.flush() create_cmd = [FLAGS.gcloud_path, 'compute', 'instances', 'create', self.name, '--disk', 'name=%s' % self.boot_disk.name, 'boot=yes', 'mode=rw', '--machine-type', self.machine_type, '--tags=perfkitbenchmarker', '--maintenance-policy', 'TERMINATE', '--metadata-from-file', 'sshKeys=%s' % tf.name, '--metadata', 'owner=%s' % FLAGS.owner] ssd_interface_option = NVME if NVME in self.image else SCSI for _ in range(self.num_ssds): create_cmd.append('--local-ssd') create_cmd.append('interface=%s' % ssd_interface_option) if FLAGS.gcloud_scopes: create_cmd.extend(['--scopes'] + re.split(r'[,; ]', FLAGS.gcloud_scopes)) create_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(create_cmd)
def _Delete(self): """Deletes the Firewall Rule.""" delete_cmd = [ FLAGS.gcloud_path, 'compute', 'firewall-rules', 'delete', self.name ] delete_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(delete_cmd)
def _Delete(self): """Delete a GCE VM instance.""" delete_cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'delete', self.name ] delete_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(delete_cmd)
def _Create(self): """Create a GCE VM instance.""" with open(self.ssh_public_key) as f: public_key = f.read().rstrip('\n') with vm_util.NamedTemporaryFile(dir=vm_util.GetTempDir(), prefix='key-metadata') as tf: tf.write('%s:%s\n' % (self.user_name, public_key)) tf.close() create_cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'create', self.name, '--disk', 'name=%s' % self.boot_disk.name, 'boot=yes', 'mode=rw', '--machine-type', self.machine_type, '--tags=perfkitbenchmarker', '--no-restart-on-failure', '--metadata-from-file', 'sshKeys=%s' % tf.name, '--metadata', 'owner=%s' % FLAGS.owner ] for key, value in self.boot_metadata.iteritems(): create_cmd.append('%s=%s' % (key, value)) if not FLAGS.gce_migrate_on_maintenance: create_cmd.extend(['--maintenance-policy', 'TERMINATE']) ssd_interface_option = NVME if NVME in self.image else SCSI for _ in range(self.max_local_disks): create_cmd.append('--local-ssd') create_cmd.append('interface=%s' % ssd_interface_option) if FLAGS.gcloud_scopes: create_cmd.extend(['--scopes'] + re.split(r'[,; ]', FLAGS.gcloud_scopes)) create_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(create_cmd)
def Detach(self): """Detaches the disk from a VM.""" detach_cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'detach-disk', self.attached_vm_name, '--device-name', self.name ] detach_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueRetryableCommand(detach_cmd) self.attached_vm_name = None
def DisallowAllPorts(self): """Closes all ports on the firewall.""" for firewall in self.firewall_names: firewall_cmd = [ FLAGS.gcloud_path, 'compute', 'firewall-rules', 'delete', firewall ] firewall_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueRetryableCommand(firewall_cmd)
def _Create(self): """Creates the Firewall Rule.""" create_cmd = [ FLAGS.gcloud_path, 'compute', 'firewall-rules', 'create', self.name, '--allow', 'tcp:%d' % self.port, 'udp:%d' % self.port ] create_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(create_cmd)
def _PostCreate(self): super(WindowsGceVirtualMachine, self)._PostCreate() reset_password_cmd = [ FLAGS.gcloud_path, 'compute', 'reset-windows-password', '--user', self.user_name, self.name ] reset_password_cmd.extend(util.GetDefaultGcloudFlags(self)) stdout, _ = vm_util.IssueRetryableCommand(reset_password_cmd) response = json.loads(stdout) self.password = response['password']
def _PostCreate(self): """Get the instance's data.""" getinstance_cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'describe', self.name ] getinstance_cmd.extend(util.GetDefaultGcloudFlags(self)) stdout, _, _ = vm_util.IssueCommand(getinstance_cmd) response = json.loads(stdout) network_interface = response['networkInterfaces'][0] self.internal_ip = network_interface['networkIP'] self.ip_address = network_interface['accessConfigs'][0]['natIP']
def _Exists(self): """Returns True if the Firewall Rule exists.""" describe_cmd = [ FLAGS.gcloud_path, 'compute', 'firewall-rules', 'describe', self.name ] describe_cmd.extend(util.GetDefaultGcloudFlags(self)) _, _, retcode = vm_util.IssueCommand(describe_cmd, suppress_warning=True) if retcode: return False return True
def AddMetadata(self, **kwargs): """Adds metadata to the VM via 'gcloud compute instances add-metadata'.""" if not kwargs: return cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'add-metadata', self.name, '--metadata' ] for key, value in kwargs.iteritems(): cmd.append('{0}={1}'.format(key, value)) cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueCommand(cmd)
def _Exists(self): """Returns true if the disk exists.""" getdisk_cmd = [ FLAGS.gcloud_path, 'compute', 'disks', 'describe', self.name ] getdisk_cmd.extend(util.GetDefaultGcloudFlags(self)) stdout, _, _ = vm_util.IssueCommand(getdisk_cmd, suppress_warning=True) try: json.loads(stdout) except ValueError: return False return True
def _Create(self): """Creates the disk.""" create_cmd = [ FLAGS.gcloud_path, 'compute', 'disks', 'create', self.name, '--size', str(self.disk_size), '--type', DISK_TYPE[self.disk_type] ] create_cmd.extend(util.GetDefaultGcloudFlags(self)) if self.image: create_cmd.extend(['--image', self.image]) if FLAGS.image_project: create_cmd.extend(['--image-project', FLAGS.image_project]) vm_util.IssueCommand(create_cmd)
def _Exists(self): """Returns true if the VM exists.""" getinstance_cmd = [FLAGS.gcloud_path, 'compute', 'instances', 'describe', self.name] getinstance_cmd.extend(util.GetDefaultGcloudFlags(self)) stdout, _, _ = vm_util.IssueCommand(getinstance_cmd) try: json.loads(stdout) except ValueError: return False return True
def Attach(self, vm): """Attaches the disk to a VM. Args: vm: The GceVirtualMachine instance to which the disk will be attached. """ self.attached_vm_name = vm.name attach_cmd = [ FLAGS.gcloud_path, 'compute', 'instances', 'attach-disk', self.attached_vm_name, '--device-name', self.name, '--disk', self.name ] attach_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueRetryableCommand(attach_cmd)
def AllowPort(self, vm, port): """Opens a port on the firewall. Args: vm: The BaseVirtualMachine object to open the port for. port: The local port to open. """ if vm.is_static: return with self._lock: firewall_name = ('perfkit-firewall-%s-%d' % (FLAGS.run_uri, port)) if firewall_name in self.firewall_names: return firewall_cmd = [ FLAGS.gcloud_path, 'compute', 'firewall-rules', 'create', firewall_name, '--allow', 'tcp:%d' % port, 'udp:%d' % port ] firewall_cmd.extend(util.GetDefaultGcloudFlags(self)) vm_util.IssueRetryableCommand(firewall_cmd) self.firewall_names.append(firewall_name)