snf-occi-server.py 2.4 KB
Newer Older
John Giannelos's avatar
John Giannelos committed
1
2
#!/usr/bin/env python

John Giannelos's avatar
John Giannelos committed
3
4
5
from snfOCCI.registry import snfRegistry
from snfOCCI.compute import ComputeBackend

John Giannelos's avatar
John Giannelos committed
6
from kamaki.clients.compute import ComputeClient
7
from kamaki.clients.cyclades import CycladesClient
John Giannelos's avatar
John Giannelos committed
8
9
from kamaki.config  import Config

10
11
from occi.core_model import Mixin
from occi.backend import MixinBackend
John Giannelos's avatar
John Giannelos committed
12
13
14
15
16
17
from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
from occi.wsgi import Application

from wsgiref.simple_server import make_server
from wsgiref.validate import validator

John Giannelos's avatar
John Giannelos committed
18

John Giannelos's avatar
John Giannelos committed
19
20
21
22
23
24
25
26


class MyAPP(Application):
    '''
    An OCCI WSGI application.
    '''

    def __call__(self, environ, response):
27

28

29
        #TODO up-to-date compute instances                
30

31

32
        # token will be represented in self.extras
John Giannelos's avatar
John Giannelos committed
33
        return self._call_occi(environ, response, security = None, token = environ['HTTP_AUTH_TOKEN'])
John Giannelos's avatar
John Giannelos committed
34

35

John Giannelos's avatar
John Giannelos committed
36
37
if __name__ == '__main__':

38
39
    APP = MyAPP(registry = snfRegistry())

John Giannelos's avatar
John Giannelos committed
40
41
42
43
44
45
46
47
48
49
50
    COMPUTE_BACKEND = ComputeBackend()

    APP.register_backend(COMPUTE, COMPUTE_BACKEND)
    APP.register_backend(START, COMPUTE_BACKEND)
    APP.register_backend(STOP, COMPUTE_BACKEND)
    APP.register_backend(RESTART, COMPUTE_BACKEND)
    APP.register_backend(SUSPEND, COMPUTE_BACKEND)
    APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
    APP.register_backend(OS_TEMPLATE, MixinBackend())
    
    snf = ComputeClient(Config())
John Giannelos's avatar
John Giannelos committed
51

John Giannelos's avatar
John Giannelos committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    images = snf.list_images()
    for image in images:
        IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])}
        IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
        APP.register_backend(IMAGE, MixinBackend())

    flavors = snf.list_flavors()
    for flavor in flavors:
        FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'],
                             'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
                             'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
                             'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
                             }
        FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
        APP.register_backend(FLAVOR, MixinBackend())

 
    VALIDATOR_APP = validator(APP)
    HTTPD = make_server('', 8888, VALIDATOR_APP)
    HTTPD.serve_forever()