Statistics
| Branch: | Revision:

root / snf-occi-server.py @ 4efa9a11

History | View | Annotate | Download (5 kB)

1
#!/usr/bin/env python
2

    
3
from kamaki.clients.compute import ComputeClient
4
from kamaki.config  import Config
5

    
6
from occi.core_model import Mixin
7
from occi.backend import ActionBackend, KindBackend, MixinBackend
8
from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART, RESOURCE_TEMPLATE, OS_TEMPLATE
9

    
10
from occi.wsgi import Application
11

    
12
from wsgiref.simple_server import make_server
13
from wsgiref.validate import validator
14

    
15

    
16
class MyBackend(KindBackend, ActionBackend):
17
    '''
18
    An very simple abstract backend which handles update and replace for
19
    attributes. Support for links and mixins would need to added.
20
    '''
21

    
22
    def update(self, old, new, extras):
23
        # here you can check what information from new_entity you wanna bring
24
        # into old_entity
25

    
26
        # trigger your hypervisor and push most recent information
27
        print('Updating a resource with id: ' + old.identifier)
28
        for item in new.attributes.keys():
29
            old.attributes[item] = new.attributes[item]
30

    
31
    def replace(self, old, new, extras):
32
        print('Replacing a resource with id: ' + old.identifier)
33
        old.attributes = {}
34
        for item in new.attributes.keys():
35
            old.attributes[item] = new.attributes[item]
36
        old.attributes['occi.compute.state'] = 'inactive'
37

    
38

    
39
class ComputeBackend(MyBackend):
40
    '''
41
    A Backend for compute instances.
42
    '''
43

    
44
    def create(self, entity, extras):
45
        entity.attributes['occi.compute.state'] = 'active'
46
        entity.actions = [STOP, SUSPEND, RESTART]
47
        
48
        for mixin in entity.mixins:
49
            print mixin.term
50
            print mixin.attributes
51

    
52
        print('Creating the virtual machine with id: ' + entity.identifier)
53
        
54

    
55
    def retrieve(self, entity, extras):
56
        # triggering cyclades to retrieve up to date information
57

    
58
        if entity.attributes['occi.compute.state'] == 'inactive':
59
            entity.actions = [START]
60
        if entity.attributes['occi.compute.state'] == 'active': 
61
            entity.actions = [STOP, SUSPEND, RESTART]
62
        if entity.attributes['occi.compute.state'] == 'suspended':
63
            entity.actions = [START]
64

    
65
    def delete(self, entity, extras):
66
        # call the management framework to delete this compute instance...
67
        print('Removing representation of virtual machine with id: '
68
              + entity.identifier)
69

    
70
    def action(self, entity, action, extras):
71
        if action not in entity.actions:
72
            raise AttributeError("This action is currently no applicable.")
73
        elif action == START:
74
            entity.attributes['occi.compute.state'] = 'active'
75
            # read attributes from action and do something with it :-)
76
            print('Starting virtual machine with id' + entity.identifier)
77
        elif action == STOP:
78
            entity.attributes['occi.compute.state'] = 'inactive'
79
            # read attributes from action and do something with it :-)
80
            print('Stopping virtual machine with id' + entity.identifier)
81
        elif action == RESTART:
82
            entity.attributes['occi.compute.state'] = 'active'
83
            # read attributes from action and do something with it :-)
84
            print('Restarting virtual machine with id' + entity.identifier)
85
        elif action == SUSPEND:
86
            entity.attributes['occi.compute.state'] = 'suspended'
87
            # read attributes from action and do something with it :-)
88
            print('Suspending virtual machine with id' + entity.identifier)
89

    
90

    
91
class MyAPP(Application):
92
    '''
93
    An OCCI WSGI application.
94
    '''
95

    
96
    def __call__(self, environ, response):
97
        sec_obj = {'username': 'password'}
98
        return self._call_occi(environ, response, security=sec_obj, foo=None)
99

    
100
if __name__ == '__main__':
101

    
102
    APP = MyAPP()
103

    
104
    COMPUTE_BACKEND = ComputeBackend()
105

    
106
    APP.register_backend(COMPUTE, COMPUTE_BACKEND)
107
    APP.register_backend(START, COMPUTE_BACKEND)
108
    APP.register_backend(STOP, COMPUTE_BACKEND)
109
    APP.register_backend(RESTART, COMPUTE_BACKEND)
110
    APP.register_backend(SUSPEND, COMPUTE_BACKEND)
111
    APP.register_backend(RESOURCE_TEMPLATE, MixinBackend())
112
    APP.register_backend(OS_TEMPLATE, MixinBackend())
113
    
114

    
115
    snf = ComputeClient(Config())
116
    images = snf.list_images()
117
    for image in images:
118
        IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE])
119
        APP.register_backend(IMAGE, MixinBackend())
120

    
121
    flavors = snf.list_flavors()
122
    for flavor in flavors:
123
        FLAVOR_ATTRIBUTES = {'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'],
124
                             'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'],
125
                             'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'],
126
                             }
127
        FLAVOR =Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
128
        APP.register_backend(FLAVOR, MixinBackend())
129
        
130
    VALIDATOR_APP = validator(APP)
131

    
132
    HTTPD = make_server('', 8888, VALIDATOR_APP)
133
    HTTPD.serve_forever()
134