1 from snfOCCI.config import SERVER_CONFIG
3 from kamaki.clients.compute import ComputeClient
4 from kamaki.clients.cyclades import CycladesClient
5 from kamaki.config import Config
7 from occi.backend import ActionBackend, KindBackend
8 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART
11 #Compute Backend for snf-occi-server
13 class MyBackend(KindBackend, ActionBackend):
15 An very simple abstract backend which handles update and replace for
16 attributes. Support for links and mixins would need to added.
19 def update(self, old, new, extras):
20 raise AttributeError("This action is currently no applicable.")
22 def replace(self, old, new, extras):
23 raise AttributeError("This action is currently no applicable.")
26 class ComputeBackend(MyBackend):
28 Backend for Cyclades/Openstack compute instances
31 def create(self, entity, extras):
33 for mixin in entity.mixins:
34 if mixin.related[0].term == 'os_tpl':
36 image_id = mixin.attributes['occi.core.id']
37 if mixin.related[0].term == 'resource_tpl':
39 flavor_id = mixin.attributes['occi.core.id']
41 entity.attributes['occi.compute.state'] = 'inactive'
42 entity.actions = [START]
44 #Registry identifier is the uuid key occi.handler assigns
45 #attribute 'occi.core.id' will be the snf-server id
48 conf.set('token',extras['token'])
49 snf = ComputeClient(conf)
51 vm_name = entity.attributes['occi.compute.hostname']
52 info = snf.create_server(vm_name, flavor_id, image_id)
53 entity.attributes['occi.core.id'] = str(info['id'])
54 entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch']
55 entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
56 entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
57 entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
59 def retrieve(self, entity, extras):
61 # triggering cyclades to retrieve up to date information
63 conf.set('token',extras['token'])
64 snf = ComputeClient(conf)
66 vm_id = int(entity.attributes['occi.core.id'])
67 vm_info = snf.get_server_details(vm_id)
68 vm_state = vm_info['status']
70 status_dict = {'ACTIVE' : 'active',
71 'STOPPED' : 'inactive',
74 'DELETED' : 'inactive',
77 entity.attributes['occi.compute.state'] = status_dict[vm_state]
79 if entity.attributes['occi.compute.state'] == 'inactive':
80 entity.actions = [START]
81 if entity.attributes['occi.compute.state'] == 'active':
82 entity.actions = [STOP, SUSPEND, RESTART]
83 if entity.attributes['occi.compute.state'] == 'suspended':
84 entity.actions = [START]
87 def delete(self, entity, extras):
89 # delete vm with vm_id = entity.attributes['occi.core.id']
91 conf.set('token',extras['token'])
92 snf = ComputeClient(conf)
94 vm_id = int(entity.attributes['occi.core.id'])
95 snf.delete_server(vm_id)
98 def action(self, entity, action, extras):
101 conf.set('token',extras['token'])
102 client = CycladesClient(conf)
104 vm_id = int(entity.attributes['occi.core.id'])
106 if action not in entity.actions:
107 raise AttributeError("This action is currently no applicable.")
109 elif action == START:
111 client.start_server(vm_id)
116 client.shutdown_server(vm_id)
118 elif action == RESTART:
119 print "Restarting VM"
120 snf.reboot_server(vm_id)
123 elif action == SUSPEND:
124 raise AttributeError("This actions is currently no applicable")