3 from kamaki.clients.compute import ComputeClient
4 from kamaki.clients.cyclades import CycladesClient
5 from kamaki.config import Config
7 from occi.core_model import Mixin
8 from occi.backend import ActionBackend, KindBackend, MixinBackend
9 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
11 from occi.wsgi import Application
13 from wsgiref.simple_server import make_server
14 from wsgiref.validate import validator
17 class MyBackend(KindBackend, ActionBackend):
19 An very simple abstract backend which handles update and replace for
20 attributes. Support for links and mixins would need to added.
23 def update(self, old, new, extras):
24 # here you can check what information from new_entity you wanna bring
27 # trigger your hypervisor and push most recent information
28 print('Updating a resource with id: ' + old.identifier)
29 for item in new.attributes.keys():
30 old.attributes[item] = new.attributes[item]
32 def replace(self, old, new, extras):
33 print('Replacing a resource with id: ' + old.identifier)
35 for item in new.attributes.keys():
36 old.attributes[item] = new.attributes[item]
37 old.attributes['occi.compute.state'] = 'inactive'
40 class ComputeBackend(MyBackend):
42 Backend for Cyclades/Openstack compute instances
45 def create(self, entity, extras):
47 for mixin in entity.mixins:
48 if mixin.related[0].term == 'os_tpl':
50 image_id = mixin.attributes['occi.core.id']
51 if mixin.related[0].term == 'resource_tpl':
53 flavor_id = mixin.attributes['occi.core.id']
55 entity.attributes['occi.compute.state'] = 'active'
56 entity.actions = [STOP, SUSPEND, RESTART]
58 #Registry identifier is the uuid key occi.handler assigns
59 #attribute 'occi.core.id' will be the snf-server id
61 snf = ComputeClient(Config())
62 vm_name = entity.attributes['occi.compute.hostname']
63 info = snf.create_server(vm_name, flavor_id, image_id)
64 entity.attributes['occi.core.id'] = str(info['id'])
65 entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
66 entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
68 def retrieve(self, entity, extras):
70 # triggering cyclades to retrieve up to date information
72 snf = ComputeClient(Config())
74 vm_id = int(entity.attributes['occi.core.id'])
75 vm_info = snf.get_server_details(vm_id)
76 vm_state = vm_info['status']
78 status_dict = {'ACTIVE' : 'active',
79 'STOPPED' : 'inactive',
82 'DELETED' : 'inactive',
85 entity.attributes['occi.compute.state'] = status_dict[vm_state]
87 if entity.attributes['occi.compute.state'] == 'inactive':
88 entity.actions = [START]
89 if entity.attributes['occi.compute.state'] == 'active':
90 entity.actions = [STOP, SUSPEND, RESTART]
91 if entity.attributes['occi.compute.state'] == 'suspended':
92 entity.actions = [START]
95 def delete(self, entity, extras):
96 # delete vm with vm_id = entity.attributes['occi.core.id']
97 snf = ComputeClient(Config())
98 vm_id = int(entity.attributes['occi.core.id'])
99 snf.delete_server(vm_id)
102 def action(self, entity, action, extras):
104 client = CycladesClient(Config())
105 vm_id = int(entity.attributes['occi.core.id'])
107 if action not in entity.actions:
108 raise AttributeError("This action is currently no applicable.")
110 elif action == START:
113 client.start_server(vm_id)
119 client.shutdown_server(vm_id)
121 elif action == RESTART:
122 snf.reboot_server(vm_id)
125 elif action == SUSPEND:
129 entity.attributes['occi.compute.state'] = 'suspended'
130 entity.actions = [START]
133 class MyAPP(Application):
135 An OCCI WSGI application.
138 def __call__(self, environ, response):
140 return self._call_occi(environ, response)
142 if __name__ == '__main__':
146 COMPUTE_BACKEND = ComputeBackend()
148 APP.register_backend(COMPUTE, COMPUTE_BACKEND)
149 APP.register_backend(START, COMPUTE_BACKEND)
150 APP.register_backend(STOP, COMPUTE_BACKEND)
151 APP.register_backend(RESTART, COMPUTE_BACKEND)
152 APP.register_backend(SUSPEND, COMPUTE_BACKEND)
153 APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
154 APP.register_backend(OS_TEMPLATE, MixinBackend())
156 snf = ComputeClient(Config())
158 images = snf.list_images()
160 IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
161 IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
162 APP.register_backend(IMAGE, MixinBackend())
164 flavors = snf.list_flavors()
165 for flavor in flavors:
166 FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'],
167 'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
168 'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
169 'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
171 FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
172 APP.register_backend(FLAVOR, MixinBackend())
175 VALIDATOR_APP = validator(APP)
176 HTTPD = make_server('', 8888, VALIDATOR_APP)
177 HTTPD.serve_forever()