def _Delete(self): if self.id is None: return cmd = util.RackCLICommand(self, 'block-storage', 'volume', 'delete') cmd.flags['id'] = self.id cmd.Issue() self._WaitForRemoteDiskDeletion()
def testCommonFlagsWithOptionalFlags(self): rack_resource = RackspaceResource(profile='US', region='DFW') cmd = util.RackCLICommand(rack_resource, 'servers', 'keypair', 'list') cmd.flags['all-pages'] = True self.assertEqual(cmd._GetCommand(), [ 'path/rack', 'servers', 'keypair', 'list', '--all-pages', '--output', 'json', '--profile', 'US', '--region', 'DFW'])
def _Delete(self): if self.id is None: return cmd = util.RackCLICommand(self, 'networks', 'security-group-rule', 'delete') cmd.flags['id'] = self.id cmd.Issue()
def _GetCreateCommand(self, tf): """Generates RackCLI command for creating a Rackspace VM. Args: tf: file object containing cloud-config script. Returns: RackCLICommand containing RackCLI arguments to build a Rackspace VM. """ create_cmd = util.RackCLICommand(self, 'servers', 'instance', 'create') create_cmd.flags['name'] = self.name create_cmd.flags['keypair'] = self.name create_cmd.flags['flavor-id'] = self.machine_type if FLAGS.rackspace_boot_from_cbs_volume: blk_flag = RenderBlockDeviceTemplate(self.image, REMOTE_BOOT_DISK_SIZE_GB) create_cmd.flags['block-device'] = blk_flag else: create_cmd.flags['image-id'] = self.image if FLAGS.rackspace_network_id is not None: create_cmd.flags['networks'] = ','.join([ rackspace_network.PUBLIC_NET_ID, rackspace_network.SERVICE_NET_ID, FLAGS.rackspace_network_id ]) create_cmd.flags['user-data'] = tf.name metadata = ['owner=%s' % FLAGS.owner] for key, value in six.iteritems(self.boot_metadata): metadata.append('%s=%s' % (key, value)) create_cmd.flags['metadata'] = ','.join(metadata) return create_cmd
def _Exists(self): if self.id is None: return False cmd = util.RackCLICommand(self, 'networks', 'security-group', 'get') cmd.flags['id'] = self.id stdout, stderr, _ = cmd.Issue() return not stderr
def _Create(self): cmd = util.RackCLICommand(self, 'block-storage', 'volume', 'create') cmd.flags['size'] = self.disk_size cmd.flags['name'] = self.name cmd.flags['volume-type'] = REMOTE_TYPES_TRANSLATION[self.media] stdout, stderr, _ = cmd.Issue() resp = json.loads(stdout) self.id = resp['ID']
def _PostCreate(self): """Gets the VM's information.""" get_cmd = util.RackCLICommand(self, 'servers', 'instance', 'get') get_cmd.flags['id'] = self.id stdout, _, _ = get_cmd.Issue() resp = json.loads(stdout) self.internal_ip = resp['PrivateIPv4'] self.ip_address = resp['PublicIPv4']
def _DeleteInstance(self): """Executes delete command for removing a Rackspace VM.""" cmd = util.RackCLICommand(self, 'servers', 'instance', 'delete') cmd.flags['id'] = self.id stdout, _, _ = cmd.Issue(suppress_warning=True) resp = json.loads(stdout) if 'result' not in resp or 'Deleting' not in resp['result']: raise errors.Resource.RetryableDeletionError()
def _Create(self): cmd = util.RackCLICommand(self, 'networks', 'network', 'create') cmd.flags['name'] = self.name if self.tenant_id: cmd.flags['tenant-id'] = self.tenant_id stdout, _, _ = cmd.Issue() resp = json.loads(stdout) if resp['ID']: self.id = resp['ID']
def _Exists(self): if self.id is None: return False cmd = util.RackCLICommand(self, 'block-storage', 'volume', 'get') cmd.flags['id'] = self.id stdout, stderr, _ = cmd.Issue(suppress_warning=True) if stdout and stdout.strip(): return stdout return not stderr
def AddMetadata(self, **kwargs): """Adds metadata to the VM via RackCLI update-metadata command.""" if not kwargs: return cmd = util.RackCLICommand(self, 'servers', 'instance', 'update-metadata') cmd.flags['id'] = self.id cmd.flags['metadata'] = ','.join('{0}={1}'.format(key, value) for key, value in kwargs.iteritems()) cmd.Issue()
def _WaitForRemoteDiskDeletion(self): cmd = util.RackCLICommand(self, 'block-storage', 'volume', 'get') cmd.flags['id'] = self.id stdout, stderr, _ = cmd.Issue() if stderr: logging.info('Disk: %s has been successfully deleted.' % self.name) return raise errors.Resource.RetryableDeletionError( 'Disk: %s has not been deleted. Retrying to check status.' % self.name)
def _Create(self): cmd = util.RackCLICommand(self, 'networks', 'subnet', 'create') cmd.flags['network-id'] = self.network_id cmd.flags['cidr'] = self.cidr cmd.flags['ip-version'] = self.ip_ver if self.name: cmd.flags['name'] = self.name if self.tenant_id: cmd.flags['tenant-id'] = self.tenant_id stdout, stderr, _ = cmd.Issue() resp = json.loads(stdout) self.id = resp['ID']
def _Exists(self): """Returns true if the VM exists otherwise returns false.""" if self.id is None: return False get_cmd = util.RackCLICommand(self, 'servers', 'instance', 'get') get_cmd.flags['id'] = self.id stdout, _, _ = get_cmd.Issue(suppress_warning=True) try: resp = json.loads(stdout) except ValueError: return False status = resp['Status'] return status in INSTANCE_EXISTS_STATUSES
def _Create(self): cmd = util.RackCLICommand(self, 'networks', 'security-group-rule', 'create') cmd.flags['security-group-id'] = self.sec_group_id cmd.flags['direction'] = self.direction cmd.flags['ether-type'] = self.ip_ver cmd.flags['protocol'] = self.protocol cmd.flags['port-range-min'] = self.port_range_min cmd.flags['port-range-max'] = self.port_range_max if self.source_cidr: cmd.flags['remote-ip-prefix'] = self.source_cidr stdout, stderr, _ = cmd.Issue() resp = json.loads(stdout) self.id = resp['ID']
def _WaitForRemoteDiskAttached(self, vm): cmd = util.RackCLICommand(self, 'block-storage', 'volume', 'get') cmd.flags['id'] = self.id stdout, stderr, _ = cmd.Issue() if stdout: resp = json.loads(stdout) attachments = resp['Attachments'] if attachments: logging.info('Disk: %s has been attached to %s.' % (self.name, vm.id)) return raise errors.Resource.RetryableCreationError( 'Disk: %s is not yet attached. Retrying to check status.' % self.name)
def _WaitForInstanceUntilActive(self): """Waits until instance achieves non-transient state.""" get_cmd = util.RackCLICommand(self, 'servers', 'instance', 'get') get_cmd.flags['id'] = self.id stdout, stderr, _ = get_cmd.Issue() if stdout: instance = json.loads(stdout) if instance['Status'] == 'ACTIVE': logging.info('VM: %s is up and running.' % self.name) return elif instance['Status'] == 'ERROR': logging.error('VM: %s failed to boot.' % self.name) raise errors.VirtualMachine.VmStateError() raise errors.Resource.RetryableCreationError( 'VM: %s is not running. Retrying to check status.' % self.name)
def _DetachRemoteDisk(self): if self.id is None: raise errors.Error('Cannot detach remote disk %s' % self.name) if self.attached_vm_id is None: raise errors.VirtualMachine.VmStateError( 'Cannot detach remote disk %s from a non-existing VM' % self.name) cmd = util.RackCLICommand(self, 'servers', 'volume-attachment', 'delete') cmd.flags['id'] = self.id cmd.flags['server-id'] = self.attached_vm_id stdout, stderr, _ = cmd.Issue() if stdout: resp = json.loads(stdout) if 'Successfully deleted' in resp['result']: return raise errors.Resource.RetryableDeletionError(stderr)
def _AttachRemoteDisk(self, vm): if self.id is None: raise errors.Error('Cannot attach remote disk %s' % self.name) if vm.id is None: raise errors.VirtualMachine.VmStateError( 'Cannot attach remote disk %s to non-existing %s VM' % (self.name, vm.name)) cmd = util.RackCLICommand(self, 'servers', 'volume-attachment', 'create') cmd.flags['volume-id'] = self.id cmd.flags['server-id'] = vm.id stdout, stderr, _ = cmd.Issue() if stderr: raise errors.Error('Failed to attach remote disk %s to %s' % (self.name, vm.name)) resp = json.loads(stdout) self.device_path = resp['Device']
def _WaitForInstanceUntilDeleted(self): """Waits until instance has been fully removed, or deleted.""" get_cmd = util.RackCLICommand(self, 'servers', 'instance', 'get') get_cmd.flags['id'] = self.id stdout, stderr, _ = get_cmd.Issue() if stderr: resp = json.loads(stderr) if 'error' in resp and "couldn't find" in resp['error']: logging.info('VM: %s has been successfully deleted.' % self.name) return instance = json.loads(stdout) if instance['Status'] == 'ERROR': logging.error('VM: %s failed to delete.' % self.name) raise errors.VirtualMachine.VmStateError() if instance['Status'] == 'DELETED': logging.info('VM: %s has been successfully deleted.' % self.name) else: raise errors.Resource.RetryableDeletionError( 'VM: %s has not been deleted. Retrying to check status.' % self.name)
def testCommonFlagsWithoutOptionalFlags(self): rack_resource = RackspaceResource(profile=None) cmd = util.RackCLICommand(rack_resource, 'servers', 'image', 'list') self.assertEqual( cmd._GetCommand(), ['path/rack', 'servers', 'image', 'list', '--output', 'json'])
def _DeleteSSHPublicKey(self): """Deletes SSH public key used for a VM.""" cmd = util.RackCLICommand(self, 'servers', 'keypair', 'delete') cmd.flags['name'] = self.name cmd.Issue()
def _UploadSSHPublicKey(self): """Uploads SSH public key to the VM's region. 1 key per VM per Region.""" cmd = util.RackCLICommand(self, 'servers', 'keypair', 'upload') cmd.flags = OrderedDict([('name', self.name), ('file', self.ssh_public_key)]) cmd.Issue()
def _Create(self): cmd = util.RackCLICommand(self, 'networks', 'security-group', 'create') cmd.flags['name'] = self.name stdout, stderr, _ = cmd.Issue() resp = json.loads(stdout) self.id = resp['ID']