X-Git-Url: https://code.grnet.gr/git/snf-occi/blobdiff_plain/3f0eb9af99edeb3e321f265dfc6a218d6998ce34..b8e8a8f919edf623942b915f01c29750c216a140:/snfOCCI/compute.py diff --git a/snfOCCI/compute.py b/snfOCCI/compute.py index 28621f7..50d4223 100644 --- a/snfOCCI/compute.py +++ b/snfOCCI/compute.py @@ -1,34 +1,21 @@ -from kamaki.clients.compute import ComputeClient -from kamaki.clients.cyclades import CycladesClient -from kamaki.config import Config +from snfOCCI.config import SERVER_CONFIG from occi.backend import ActionBackend, KindBackend -from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART +from occi.extensions.infrastructure import START, STOP, SUSPEND, RESTART +from occi.exceptions import HTTPError #Compute Backend for snf-occi-server class MyBackend(KindBackend, ActionBackend): - ''' - An very simple abstract backend which handles update and replace for - attributes. Support for links and mixins would need to added. - ''' - def update(self, old, new, extras): - # here you can check what information from new_entity you wanna bring - # into old_entity + # Updating and Replacing compute instances not supported by Cyclades - # trigger your hypervisor and push most recent information - print('Updating a resource with id: ' + old.identifier) - for item in new.attributes.keys(): - old.attributes[item] = new.attributes[item] + def update(self, old, new, extras): + raise HTTPError(501, "Update is currently no applicable") def replace(self, old, new, extras): - print('Replacing a resource with id: ' + old.identifier) - old.attributes = {} - for item in new.attributes.keys(): - old.attributes[item] = new.attributes[item] - old.attributes['occi.compute.state'] = 'inactive' + raise HTTPError(501, "Replace is currently no applicable") class ComputeBackend(MyBackend): @@ -37,37 +24,40 @@ class ComputeBackend(MyBackend): ''' def create(self, entity, extras): - - for mixin in entity.mixins: - if mixin.related[0].term == 'os_tpl': - image = mixin - image_id = mixin.attributes['occi.core.id'] - if mixin.related[0].term == 'resource_tpl': - flavor = mixin - flavor_id = mixin.attributes['occi.core.id'] - - entity.attributes['occi.compute.state'] = 'active' - entity.actions = [STOP, SUSPEND, RESTART] - #Registry identifier is the uuid key occi.handler assigns - #attribute 'occi.core.id' will be the snf-server id + #Creating new compute instance + + try: + + snf = extras['snf'] + + for mixin in entity.mixins: + if mixin.related[0].term == 'os_tpl': + image_id = mixin.attributes['occi.core.id'] + if mixin.related[0].term == 'resource_tpl': + flavor = mixin + flavor_id = mixin.attributes['occi.core.id'] - conf = Config() - conf.set('token',extras['token']) - snf = ComputeClient(conf) + vm_name = entity.attributes['occi.core.title'] + info = snf.create_server(vm_name, flavor_id, image_id) - vm_name = entity.attributes['occi.compute.hostname'] - info = snf.create_server(vm_name, flavor_id, image_id) - entity.attributes['occi.core.id'] = str(info['id']) - entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores'] - entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory'] + entity.actions = [START] + entity.attributes['occi.compute.state'] = 'inactive' + entity.attributes['occi.core.id'] = str(info['id']) + entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch'] + entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores'] + entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory'] + entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']} + + except (UnboundLocalError, KeyError) as e: + raise HTTPError(406, 'Missing details about compute instance') + def retrieve(self, entity, extras): - # triggering cyclades to retrieve up to date information - conf = Config() - conf.set('token',extras['token']) - snf = ComputeClient(conf) + #Triggering cyclades to retrieve up to date information + + snf = extras['snf'] vm_id = int(entity.attributes['occi.core.id']) vm_info = snf.get_server_details(vm_id) @@ -81,51 +71,56 @@ class ComputeBackend(MyBackend): } entity.attributes['occi.compute.state'] = status_dict[vm_state] + + if vm_state == 'ERROR': + raise HTTPError(500, 'ERROR building the compute instance') - if entity.attributes['occi.compute.state'] == 'inactive': - entity.actions = [START] - if entity.attributes['occi.compute.state'] == 'active': - entity.actions = [STOP, SUSPEND, RESTART] - if entity.attributes['occi.compute.state'] == 'suspended': - entity.actions = [START] + else: + if entity.attributes['occi.compute.state'] == 'inactive': + entity.actions = [START] + if entity.attributes['occi.compute.state'] == 'active': + entity.actions = [STOP, SUSPEND, RESTART] def delete(self, entity, extras): - # delete vm with vm_id = entity.attributes['occi.core.id'] - conf = Config() - conf.set('token',extras['token']) - snf = ComputeClient(conf) + #Deleting compute instance + snf = extras['snf'] vm_id = int(entity.attributes['occi.core.id']) snf.delete_server(vm_id) def action(self, entity, action, extras): - conf = Config() - conf.set('token',extras['token']) - client = CycladesClient(conf) + #Triggering action to compute instances - vm_id = int(entity.attributes['occi.core.id']) + client = extras['client'] + snf = extras['snf'] - if action not in entity.actions: - raise AttributeError("This action is currently no applicable.") + vm_id = int(entity.attributes['occi.core.id']) + vm_info = snf.get_server_details(vm_id) + vm_state = vm_info['status'] - elif action == START: - print "Starting VM" - client.start_server(vm_id) + if vm_state == 'ERROR': + raise HTTPError(500, 'ERROR building the compute instance') - elif action == STOP: - print "Stopping VM" - client.shutdown_server(vm_id) + else: + if action not in entity.actions: + raise AttributeError("This action is currently no applicable.") - elif action == RESTART: - print "Restarting VM" - snf.reboot_server(vm_id) - + elif action == START: + print "Starting VM" + client.start_server(vm_id) + + elif action == STOP: + print "Stopping VM" + client.shutdown_server(vm_id) + + elif action == RESTART: + print "Restarting VM" + snf.reboot_server(vm_id) - elif action == SUSPEND: - #TODO VM suspending - print "Suspending VM" + elif action == SUSPEND: + raise HTTPError(501, "This actions is currently no applicable")