def create(self, entity, extras): """ Creates a new volume. """ context = extras['nova_ctx'] if 'occi.storage.size' not in entity.attributes: raise AttributeError('size attribute not found!') new_volume = storage.create_storage( entity.attributes['occi.storage' '.size'], context) vol_id = new_volume['id'] # Work around problem that instance is lazy-loaded... new_volume = storage.get_storage(vol_id, context) if new_volume['status'] == 'error': raise exceptions.HTTPError( 500, 'There was an error creating the ' 'volume') entity.attributes['occi.core.id'] = str(vol_id) entity.identifier = infrastructure.STORAGE.location + vol_id if new_volume['status'] == 'available': entity.attributes['occi.storage.state'] = 'active' entity.actions = [ infrastructure.OFFLINE, infrastructure.BACKUP, infrastructure.SNAPSHOT, infrastructure.RESIZE ]
def restart_vm(uid, method, context): """ Restarts a VM. OS types == SOFT, HARD OCCI -> graceful, warm and cold mapping: - SOFT -> graceful, warm - HARD -> cold uid -- id of the instance method -- how the machine should be restarted. context -- the os context """ instance = get_vm(uid, context) if method in ('graceful', 'warm'): reboot_type = 'SOFT' elif method is 'cold': reboot_type = 'HARD' else: raise AttributeError('Unknown method.') try: COMPUTE_API.reboot(context, instance, reboot_type) except exception.InstanceInvalidState: raise exceptions.HTTPError(406, 'VM is in an invalid state.')
def start_vm(uid, context): """ Starts a vm that is in the stopped state. Note, currently we do not use the nova start and stop, rather the resume/suspend methods. The start action also unpauses a paused VM. uid -- id of the instance state -- the state the VM is in (str) context -- the os context """ instance = get_vm(uid, context) try: if instance['vm_state'] in [vm_states.PAUSED]: COMPUTE_API.unpause(context, instance) elif instance['vm_state'] in [vm_states.SUSPENDED]: COMPUTE_API.resume(context, instance) # the following will probably not happen, as COMPUTE_API.stop() # is never called. elif instance['vm_state'] in [vm_states.STOPPED]: COMPUTE_API.start(context, instance) else: raise exceptions.HTTPError(500, "Unable to map start to appropriate OS action.") except exceptions.HTTPError as e: raise e except Exception as e: raise AttributeError(e.message)
def get_vm(uid, context): """ Retrieve an VM instance from nova. uid -- id of the instance context -- the os context """ try: instance = COMPUTE_API.get(context, uid) except Exception as e: raise exceptions.HTTPError(404, 'VM not found!') return instance
def delete_vm(uid, context): """ Destroy a VM. uid -- id of the instance context -- the os context """ try: instance = get_vm(uid, context) COMPUTE_API.delete(context, instance) except Exception as error: raise exceptions.HTTPError(500, str(error))
def delete(self, entity, extras): """ Deletes the security rule. """ try: context = extras['nova_ctx'] rule = security.retrieve_rule(entity.attributes['occi.core.id'], context) security.remove_rule(rule, context) except Exception as error: raise exceptions.HTTPError(500, str(error))
def retrieve_rule(uid, context): """ Retrieve a rule. uid -- Id of the rule (entity.attributes['occi.core.id']) context -- The os context. """ try: return db.security_group_rule_get(context, int(uid)) except Exception: raise exceptions.HTTPError(404, 'Rule not found!')
def get_storage(uid, context): """ Retrieve an Volume instance from nova. uid -- id of the instance context -- the os context """ try: instance = VOLUME_API.get(context, uid) except Exception: raise exceptions.HTTPError(404, 'Volume not found!') return instance
def suspend_vm(uid, context): """ Suspends a VM. Use the start action to unsuspend a VM. uid -- id of the instance context -- the os context """ instance = get_vm(uid, context) try: COMPUTE_API.pause(context, instance) except Exception as error: raise exceptions.HTTPError(500, str(error))
def start_vm(uid, context): """ Starts a vm that is in the stopped state. Note, currently we do not use the nova start and stop, rather the resume/suspend methods. The start action also unpauses a paused VM. uid -- id of the instance state -- the state the VM is in (str) context -- the os context """ instance = get_vm(uid, context) try: COMPUTE_API.resume(context, instance) except Exception as error: raise exceptions.HTTPError(500, 'Error while starting VM: ' + str(error))
def attach_volume(instance_id, volume_id, mount_point, context): """ Attaches a storage volume. instance_id -- Id of the VM. volume_id -- Id of the storage volume. mount_point -- Where to mount. context -- The os security context. """ instance = get_vm(instance_id, context) try: vol_instance = VOLUME_API.get(context, volume_id) volume_id = vol_instance['id'] COMPUTE_API.attach_volume(context, instance, volume_id, mount_point) except exception.NotFound: raise exceptions.HTTPError(404, 'Volume not found!') except exception.InvalidDevicePath: raise AttributeError('Invalid device path!')
def stop_vm(uid, context): """ Stops a VM. Rather than use stop, suspend is used. OCCI -> graceful, acpioff, poweroff OS -> unclear uid -- id of the instance context -- the os context """ instance = get_vm(uid, context) try: # There are issues with the stop and start methods of OS. For now # we'll use suspend. # self.compute_api.stop(context, instance) COMPUTE_API.suspend(context, instance) except Exception as error: raise exceptions.HTTPError(500, 'Error while stopping VM: ' + str(error))