root / snf-occi-server.py @ 22167a8c
History | View | Annotate | Download (2.4 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
from registry import snfRegistry |
4 |
from kamaki.clients.compute import ComputeClient |
5 |
from kamaki.clients.cyclades import CycladesClient |
6 |
from kamaki.config import Config |
7 |
|
8 |
from occi.core_model import Mixin |
9 |
from occi.backend import MixinBackend |
10 |
from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE |
11 |
from occi.wsgi import Application |
12 |
|
13 |
from wsgiref.simple_server import make_server |
14 |
from wsgiref.validate import validator |
15 |
|
16 |
from compute import ComputeBackend |
17 |
|
18 |
|
19 |
class MyAPP(Application): |
20 |
'''
|
21 |
An OCCI WSGI application.
|
22 |
'''
|
23 |
|
24 |
def __call__(self, environ, response): |
25 |
|
26 |
|
27 |
#TODO up-to-date compute instances
|
28 |
|
29 |
|
30 |
# token will be represented in self.extras
|
31 |
return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN']) |
32 |
|
33 |
|
34 |
if __name__ == '__main__': |
35 |
|
36 |
APP = MyAPP(registry = snfRegistry()) |
37 |
|
38 |
COMPUTE_BACKEND = ComputeBackend() |
39 |
|
40 |
APP.register_backend(COMPUTE, COMPUTE_BACKEND) |
41 |
APP.register_backend(START, COMPUTE_BACKEND) |
42 |
APP.register_backend(STOP, COMPUTE_BACKEND) |
43 |
APP.register_backend(RESTART, COMPUTE_BACKEND) |
44 |
APP.register_backend(SUSPEND, COMPUTE_BACKEND) |
45 |
APP.register_backend(RESOURCE_TEMPLATE, MixinBackend()) |
46 |
APP.register_backend(OS_TEMPLATE, MixinBackend()) |
47 |
|
48 |
snf = ComputeClient(Config()) |
49 |
|
50 |
images = snf.list_images() |
51 |
for image in images: |
52 |
IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])} |
53 |
IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES) |
54 |
APP.register_backend(IMAGE, MixinBackend()) |
55 |
|
56 |
flavors = snf.list_flavors() |
57 |
for flavor in flavors: |
58 |
FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'], |
59 |
'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'], |
60 |
'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'], |
61 |
'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'], |
62 |
} |
63 |
FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES) |
64 |
APP.register_backend(FLAVOR, MixinBackend()) |
65 |
|
66 |
|
67 |
VALIDATOR_APP = validator(APP) |
68 |
HTTPD = make_server('', 8888, VALIDATOR_APP) |
69 |
HTTPD.serve_forever() |
70 |
|