def GetIPAddress(self): stdout, _ = vm_util.IssueRetryableCommand( [azure.AZURE_PATH, 'network', 'public-ip', 'show', '--output', 'json', '--name', self.name] + self.resource_group.args) response = json.loads(stdout) return response['ipAddress']
def ImportKeyfile(self): """Imports the public keyfile to AWS.""" with self._lock: if self.region in self.imported_keyfile_set: return cat_cmd = ['cat', vm_util.GetPublicKeyPath()] keyfile, _ = vm_util.IssueRetryableCommand(cat_cmd) import_cmd = util.AWS_PREFIX + [ 'ec2', '--region=%s' % self.region, 'import-key-pair', '--key-name=%s' % 'perfkit-key-%s' % FLAGS.run_uri, '--public-key-material=%s' % keyfile ] vm_util.IssueRetryableCommand(import_cmd) self.imported_keyfile_set.add(self.region) if self.region in self.deleted_keyfile_set: self.deleted_keyfile_set.remove(self.region)
def _Delete(self): """Delete a VM instance.""" delete_cmd = util.AWS_PREFIX + [ 'ec2', 'terminate-instances', '--region=%s' % self.region, '--instance-ids=%s' % self.id ] vm_util.IssueRetryableCommand(delete_cmd)
def GetAzureStorageConnectionString(storage_account_name, resource_group_args): """Get connection string.""" stdout, _ = vm_util.IssueRetryableCommand([ AZURE_PATH, 'storage', 'account', 'show-connection-string', '--name', storage_account_name ] + resource_group_args + AZURE_SUFFIX) response = json.loads(stdout) return response['connectionString']
def _Delete(self): """Deletes the disk.""" delete_cmd = util.AWS_PREFIX + [ 'ec2', 'delete-volume', '--region=%s' % self.region, '--volume-id=%s' % self.id ] vm_util.IssueRetryableCommand(delete_cmd)
def _PostCreate(self): """Get cluster info.""" if not FLAGS.kubeconfig: FLAGS.kubeconfig = vm_util.PrependTempDir('kubeconfig') vm_util.IssueRetryableCommand([ azure.AZURE_PATH, 'acs', 'kubernetes', 'get-credentials', '--name', self.name, '--file', FLAGS.kubeconfig, '--ssh-key-file', vm_util.GetPrivateKeyPath(), ] + self.resource_group.args)
def Detach(self): """Detaches the disk from a VM.""" nova_env = os.environ.copy() nova_env.update(util.GetDefaultRackspaceNovaEnv(self.zone)) detach_cmd = [ FLAGS.nova_path, 'volume-detach', self.attached_vm_name, self.id ] vm_util.IssueRetryableCommand(detach_cmd, env=nova_env) 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 _Delete(self): """Deletes the security group.""" delete_cmd = util.ALI_PREFIX + [ 'ecs', 'DeleteSecurityGroup', '--RegionId %s' % self.region, '--SecurityGroupId %s' % self.group_id ] delete_cmd = util.GetEncodedCmd(delete_cmd) vm_util.IssueRetryableCommand(delete_cmd)
def GetInternalIP(self): """Grab some data.""" stdout, _ = vm_util.IssueRetryableCommand( [azure.AZURE_PATH, 'network', 'nic', 'show', '--json', self.name] + self.resource_group.args) response = json.loads(stdout) return response['ipConfigurations'][0]['privateIPAddress']
def _PostCreate(self): """Allow the service principle to read from the repository.""" create_role_assignment_cmd = [ azure.AZURE_PATH, 'role', 'assignment', 'create', '--assignee', self.service_principal.app_id, '--role', 'Reader', '--scope', self.acr_id, ] vm_util.IssueRetryableCommand(create_role_assignment_cmd)
def ImportKeyfile(self): if not (self.client.keypairs.findall(name=self.key_name)): cat_cmd = ['cat', vm_util.GetPublicKeyPath()] key_file, _ = vm_util.IssueRetryableCommand(cat_cmd) pk = self.client.keypairs.create(self.key_name, public_key=key_file) else: pk = self.client.keypairs.findall(name=self.key_name)[0] self.pk = pk
def GetAllRegions() -> Set[str]: """Returns all valid regions.""" stdout, _ = vm_util.IssueRetryableCommand( [AZURE_PATH, 'account', 'list-locations', '--output', 'json']) # Filter out staging regions from the output. return set([ item['name'] for item in json.loads(stdout) if _IsRecommendedRegion(item) ])
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 AddMetadata(self, **tags): tag_string = ';'.join( ['%s=%s' % (key, value) for key, value in tags.iteritems()]) vm_util.IssueRetryableCommand( [azure.AZURE_PATH, 'vm', 'set', self.name, '--tags', tag_string] + self.resource_group.args)
def _Create(self): """Creates the Placement Group.""" create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-placement-group', '--region=%s' % self.region, '--group-name=%s' % self.name, '--strategy=cluster'] vm_util.IssueRetryableCommand(create_cmd)
def _PostCreate(self): """Get VM data.""" stdout, _ = vm_util.IssueRetryableCommand( [azure.AZURE_PATH, 'vm', 'show', '--json', self.name] + self.resource_group.args) response = json.loads(stdout) self.os_disk.name = response['storageProfile']['osDisk']['name'] self.os_disk.created = True self.internal_ip = self.nic.GetInternalIP() self.ip_address = self.public_ip.GetIPAddress()
def _PostCreate(self): """Gets data about the route table.""" describe_cmd = util.AWS_PREFIX + [ 'ec2', 'describe-route-tables', '--region=%s' % self.region, '--filters=Name=vpc-id,Values=%s' % self.vpc_id] stdout, _ = vm_util.IssueRetryableCommand(describe_cmd) response = json.loads(stdout) self.id = response['RouteTables'][0]['RouteTableId']
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 _Exists(self): """Returns true if the security group exists.""" show_cmd = util.ALI_PREFIX + [ 'ecs', 'DescribeSecurityGroupAttribute', '--RegionId %s' % self.region, '--SecurityGroupId %s' % self.group_id ] show_cmd = util.GetEncodedCmd(show_cmd) stdout, _ = vm_util.IssueRetryableCommand(show_cmd) return 'SecurityGroupId' in json.loads(stdout)
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 or port == SSH_PORT: return create_cmd = [AZURE_PATH, 'vm', 'endpoint', 'create', vm.name, str(port)] vm_util.IssueRetryableCommand( create_cmd + ['--protocol=tcp']) vm_util.IssueRetryableCommand( create_cmd + ['--protocol=udp'])
def CreateRoute(self, internet_gateway_id): """Adds a route to the internet gateway.""" create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-route', '--region=%s' % self.region, '--route-table-id=%s' % self.id, '--gateway-id=%s' % internet_gateway_id, '--destination-cidr-block=0.0.0.0/0'] vm_util.IssueRetryableCommand(create_cmd)
def _PostCreate(self): super(BaseWindowsAzureVirtualMachine, self)._PostCreate() config_dict = {'commandToExecute': windows_virtual_machine.STARTUP_SCRIPT} config = json.dumps(config_dict) vm_util.IssueRetryableCommand([ azure.AZURE_PATH, 'vm', 'extension', 'set', '--vm-name', self.name, '--name', 'CustomScriptExtension', '--publisher', 'Microsoft.Compute', '--version', '1.4', '--protected-settings=%s' % config ] + self.resource_group.args)
def GetRegionsInGeo(geo: str) -> Set[str]: """Gets valid regions in the geo.""" stdout, _ = vm_util.IssueRetryableCommand([ AZURE_PATH, 'account', 'list-locations', '--output', 'json', '--query', f"[?metadata.geographyGroup == '{geo}']" ]) return set([ item['name'] for item in json.loads(stdout) if _IsRecommendedRegion(item) ])
def _Delete(self): """Deletes the disk.""" delete_cmd = util.ALI_PREFIX + [ 'ecs', 'DeleteDisk', '--DiskId %s' % self.id ] logging.info( 'Deleting AliCloud disk %s. This may fail if the disk is not ' 'yet detached, but will be retried.', self.id) delete_cmd = util.GetEncodedCmd(delete_cmd) vm_util.IssueRetryableCommand(delete_cmd)
def _Create(self): """Creates the internet gateway.""" create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-internet-gateway', '--region=%s' % self.region] stdout, _ = vm_util.IssueRetryableCommand(create_cmd) response = json.loads(stdout) self.id = response['InternetGateway']['InternetGatewayId'] util.AddDefaultTags(self.id, self.region)
def _Create(self): """Creates the peering object. Documentation on creating a vpc object: https://docs.aws.amazon.com/vpc/latest/peering/vpc-pg.pdf """ # Creates Peering Connection create_cmd = util.AWS_PREFIX + [ 'ec2', 'create-vpc-peering-connection', '--region=%s' % self.network_a.region, '--peer-region=%s' % self.network_b.region, '--vpc-id=%s' % self.network_a.vpc.id, '--peer-vpc-id=%s' % self.network_b.vpc.id] stdout, _ = vm_util.IssueRetryableCommand(create_cmd) response = json.loads(stdout) self.id = response['VpcPeeringConnection'][ 'VpcPeeringConnectionId'] # Accepts Peering Connection accept_cmd = util.AWS_PREFIX + [ 'ec2', 'accept-vpc-peering-connection', '--region=%s' % self.network_b.region, '--vpc-peering-connection-id=%s' % self.id] vm_util.IssueRetryableCommand(accept_cmd) util.AddDefaultTags(self.id, self.network_a.region) logging.info('Creating VPC peering between %s and %s', self.network_a.vpc.cidr, self.network_b.vpc.cidr) # Adds VPC peering to both networks' route tables self.network_a.route_table.CreateVpcPeeringRoute(self.id, self.network_b.vpc.cidr) self.network_b.route_table.CreateVpcPeeringRoute(self.id, self.network_a.vpc.cidr) # Updates security group to allow inbound traffic from peering networks self.network_a.vpc.AllowVpcPeerInBound(self.network_b.vpc) self.network_b.vpc.AllowVpcPeerInBound(self.network_a.vpc)
def IssueRetryable(self, **kwargs): """Tries running the openstack cli command until it succeeds or times out. Args: **kwargs: Keyword arguments to forward to vm_util.IssueRetryableCommand when issuing the openstack cli command. Returns: (stdout, stderr) pair of strings from running the openstack command. """ return vm_util.IssueRetryableCommand(self._GetCommand(), **kwargs)
def IssueRetryableCommandFunction(cmd, **kwargs): """Use vm_util to issue the given retryable command. Args: cmd: the gcloud command to run **kwargs: additional arguments for the gcloud command Returns: stdout, stderr, tuple from running the command """ return vm_util.IssueRetryableCommand(cmd.GetCommand(), **kwargs)
def GetAzureStorageAccountKey(storage_account_name, resource_group_args): """Get storage account key.""" stdout, _ = vm_util.IssueRetryableCommand( [AZURE_PATH, 'storage', 'account', 'keys', 'list', '--account-name', storage_account_name] + resource_group_args + AZURE_SUFFIX) response = json.loads(stdout) # A new storage account comes with two keys, but we only need one. assert response[0]['permissions'] == 'Full' return response[0]['value']