Actions now working/using older pyssf version
[snf-occi] / snf-occi-server.py
1 #!/usr/bin/env python
2
3 from kamaki.clients.compute import ComputeClient
4 from kamaki.clients.cyclades import CycladesClient
5 from kamaki.config  import Config
6
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
10
11 from occi.wsgi import Application
12
13 from wsgiref.simple_server import make_server
14 from wsgiref.validate import validator
15
16
17 class MyBackend(KindBackend, ActionBackend):
18     '''
19     An very simple abstract backend which handles update and replace for
20     attributes. Support for links and mixins would need to added.
21     '''
22
23     def update(self, old, new, extras):
24         # here you can check what information from new_entity you wanna bring
25         # into old_entity
26
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]
31
32     def replace(self, old, new, extras):
33         print('Replacing a resource with id: ' + old.identifier)
34         old.attributes = {}
35         for item in new.attributes.keys():
36             old.attributes[item] = new.attributes[item]
37         old.attributes['occi.compute.state'] = 'inactive'
38
39
40 class ComputeBackend(MyBackend):
41     '''
42     Backend for Cyclades/Openstack compute instances
43     '''
44
45     def create(self, entity, extras):
46     
47         for mixin in entity.mixins:
48             if mixin.related[0].term == 'os_tpl':
49                 image = mixin
50                 image_id = mixin.attributes['occi.core.id']
51             if mixin.related[0].term == 'resource_tpl':
52                 flavor = mixin
53                 flavor_id = mixin.attributes['occi.core.id']
54                 
55         entity.attributes['occi.compute.state'] = 'active'
56         entity.actions = [STOP, SUSPEND, RESTART]
57
58         #Registry identifier is the uuid key occi.handler assigns
59         #attribute 'occi.core.id' will be the snf-server id
60
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']
67
68     def retrieve(self, entity, extras):
69         
70         # triggering cyclades to retrieve up to date information
71
72         snf = ComputeClient(Config())
73
74         vm_id = int(entity.attributes['occi.core.id'])
75         vm_info = snf.get_server_details(vm_id)
76         vm_state = vm_info['status']
77         
78         status_dict = {'ACTIVE' : 'active',
79                        'STOPPED' : 'inactive',
80                        'ERROR' : 'inactive',
81                        'BUILD' : 'inactive',
82                        'DELETED' : 'inactive',
83                        }
84         
85         entity.attributes['occi.compute.state'] = status_dict[vm_state]
86
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]
93
94
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)
100
101
102     def action(self, entity, action, extras):
103         
104         client = CycladesClient(Config())
105         vm_id = int(entity.attributes['occi.core.id'])
106
107         if action not in entity.actions:
108             raise AttributeError("This action is currently no applicable.")
109
110         elif action == START:
111
112             print "Starting VM"
113             client.start_server(vm_id)
114
115
116         elif action == STOP:
117
118             print "Stopping VM"
119             client.shutdown_server(vm_id)
120             
121         elif action == RESTART:
122             snf.reboot_server(vm_id)
123
124
125         elif action == SUSPEND:
126
127             #TODO VM suspending
128
129             entity.attributes['occi.compute.state'] = 'suspended'
130             entity.actions = [START]
131
132
133 class MyAPP(Application):
134     '''
135     An OCCI WSGI application.
136     '''
137
138     def __call__(self, environ, response):
139
140         return self._call_occi(environ, response)
141
142 if __name__ == '__main__':
143
144     APP = MyAPP()
145
146     COMPUTE_BACKEND = ComputeBackend()
147
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())
155     
156     snf = ComputeClient(Config())
157     
158     images = snf.list_images()
159     for image in 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())
163
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'],
170                              }
171         FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
172         APP.register_backend(FLAVOR, MixinBackend())
173
174  
175     VALIDATOR_APP = validator(APP)
176     HTTPD = make_server('', 8888, VALIDATOR_APP)
177     HTTPD.serve_forever()
178