1 from kamaki.clients.compute import ComputeClient
2 from kamaki.clients.cyclades import CycladesClient
3 from kamaki.config import Config
5 from occi.backend import ActionBackend, KindBackend
6 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART
9 #Compute Backend for snf-occi-server
11 class MyBackend(KindBackend, ActionBackend):
13 An very simple abstract backend which handles update and replace for
14 attributes. Support for links and mixins would need to added.
17 def update(self, old, new, extras):
18 # here you can check what information from new_entity you wanna bring
21 # trigger your hypervisor and push most recent information
22 print('Updating a resource with id: ' + old.identifier)
23 for item in new.attributes.keys():
24 old.attributes[item] = new.attributes[item]
26 def replace(self, old, new, extras):
27 print('Replacing a resource with id: ' + old.identifier)
29 for item in new.attributes.keys():
30 old.attributes[item] = new.attributes[item]
31 old.attributes['occi.compute.state'] = 'inactive'
34 class ComputeBackend(MyBackend):
36 Backend for Cyclades/Openstack compute instances
39 def create(self, entity, extras):
41 for mixin in entity.mixins:
42 if mixin.related[0].term == 'os_tpl':
44 image_id = mixin.attributes['occi.core.id']
45 if mixin.related[0].term == 'resource_tpl':
47 flavor_id = mixin.attributes['occi.core.id']
49 entity.attributes['occi.compute.state'] = 'active'
50 entity.actions = [STOP, SUSPEND, RESTART]
52 #Registry identifier is the uuid key occi.handler assigns
53 #attribute 'occi.core.id' will be the snf-server id
56 conf.set('token',extras['token'])
57 snf = ComputeClient(conf)
59 vm_name = entity.attributes['occi.compute.hostname']
60 info = snf.create_server(vm_name, flavor_id, image_id)
61 entity.attributes['occi.core.id'] = str(info['id'])
62 entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
63 entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
65 def retrieve(self, entity, extras):
67 # triggering cyclades to retrieve up to date information
69 conf.set('token',extras['token'])
70 snf = ComputeClient(conf)
72 vm_id = int(entity.attributes['occi.core.id'])
73 vm_info = snf.get_server_details(vm_id)
74 vm_state = vm_info['status']
76 status_dict = {'ACTIVE' : 'active',
77 'STOPPED' : 'inactive',
80 'DELETED' : 'inactive',
83 entity.attributes['occi.compute.state'] = status_dict[vm_state]
85 if entity.attributes['occi.compute.state'] == 'inactive':
86 entity.actions = [START]
87 if entity.attributes['occi.compute.state'] == 'active':
88 entity.actions = [STOP, SUSPEND, RESTART]
89 if entity.attributes['occi.compute.state'] == 'suspended':
90 entity.actions = [START]
93 def delete(self, entity, extras):
95 # delete vm with vm_id = entity.attributes['occi.core.id']
97 conf.set('token',extras['token'])
98 snf = ComputeClient(conf)
100 vm_id = int(entity.attributes['occi.core.id'])
101 snf.delete_server(vm_id)
104 def action(self, entity, action, extras):
107 conf.set('token',extras['token'])
108 client = CycladesClient(conf)
110 vm_id = int(entity.attributes['occi.core.id'])
112 if action not in entity.actions:
113 raise AttributeError("This action is currently no applicable.")
115 elif action == START:
117 client.start_server(vm_id)
122 client.shutdown_server(vm_id)
124 elif action == RESTART:
125 print "Restarting VM"
126 snf.reboot_server(vm_id)
129 elif action == SUSPEND:
131 print "Suspending VM"