Fixed setup scripts/module renaming
[snf-occi] / snfOCCI / compute.py
1 from kamaki.clients.compute import ComputeClient
2 from kamaki.clients.cyclades import CycladesClient
3 from kamaki.config  import Config
4
5 from occi.backend import ActionBackend, KindBackend
6 from occi.extensions.infrastructure import COMPUTE, START, STOP, SUSPEND, RESTART
7
8
9 #Compute Backend for snf-occi-server
10
11 class MyBackend(KindBackend, ActionBackend):
12     '''
13     An very simple abstract backend which handles update and replace for
14     attributes. Support for links and mixins would need to added.
15     '''
16
17     def update(self, old, new, extras):
18         # here you can check what information from new_entity you wanna bring
19         # into old_entity
20
21         # trigger your hypervisor and push most recent information
22         print('Updating a resource with id: ' + old.identifier)
23         for item in new.attributes.keys():
24             old.attributes[item] = new.attributes[item]
25
26     def replace(self, old, new, extras):
27         print('Replacing a resource with id: ' + old.identifier)
28         old.attributes = {}
29         for item in new.attributes.keys():
30             old.attributes[item] = new.attributes[item]
31         old.attributes['occi.compute.state'] = 'inactive'
32
33
34 class ComputeBackend(MyBackend):
35     '''
36     Backend for Cyclades/Openstack compute instances
37     '''
38
39     def create(self, entity, extras):
40     
41         for mixin in entity.mixins:
42             if mixin.related[0].term == 'os_tpl':
43                 image = mixin
44                 image_id = mixin.attributes['occi.core.id']
45             if mixin.related[0].term == 'resource_tpl':
46                 flavor = mixin
47                 flavor_id = mixin.attributes['occi.core.id']
48                 
49         entity.attributes['occi.compute.state'] = 'active'
50         entity.actions = [STOP, SUSPEND, RESTART]
51
52         #Registry identifier is the uuid key occi.handler assigns
53         #attribute 'occi.core.id' will be the snf-server id
54
55         conf = Config()
56         conf.set('token',extras['token'])
57         snf = ComputeClient(conf)
58
59         vm_name = entity.attributes['occi.compute.hostname']
60         info = snf.create_server(vm_name, flavor_id, image_id)
61         entity.attributes['occi.core.id'] = str(info['id'])
62         entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
63         entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
64
65     def retrieve(self, entity, extras):
66         
67         # triggering cyclades to retrieve up to date information
68         conf = Config()
69         conf.set('token',extras['token'])
70         snf = ComputeClient(conf)
71
72         vm_id = int(entity.attributes['occi.core.id'])
73         vm_info = snf.get_server_details(vm_id)
74         vm_state = vm_info['status']
75         
76         status_dict = {'ACTIVE' : 'active',
77                        'STOPPED' : 'inactive',
78                        'ERROR' : 'inactive',
79                        'BUILD' : 'inactive',
80                        'DELETED' : 'inactive',
81                        }
82         
83         entity.attributes['occi.compute.state'] = status_dict[vm_state]
84
85         if entity.attributes['occi.compute.state'] == 'inactive':
86             entity.actions = [START]
87         if entity.attributes['occi.compute.state'] == 'active': 
88             entity.actions = [STOP, SUSPEND, RESTART]
89         if entity.attributes['occi.compute.state'] == 'suspended':
90             entity.actions = [START]
91
92
93     def delete(self, entity, extras):
94
95         # delete vm with vm_id = entity.attributes['occi.core.id']
96         conf = Config()
97         conf.set('token',extras['token'])
98         snf = ComputeClient(conf)
99
100         vm_id = int(entity.attributes['occi.core.id'])
101         snf.delete_server(vm_id)
102
103
104     def action(self, entity, action, extras):
105
106         conf = Config()
107         conf.set('token',extras['token'])
108         client = CycladesClient(conf)
109
110         vm_id = int(entity.attributes['occi.core.id'])
111
112         if action not in entity.actions:
113             raise AttributeError("This action is currently no applicable.")
114
115         elif action == START:
116             print "Starting VM"
117             client.start_server(vm_id)
118
119
120         elif action == STOP:
121             print "Stopping VM"
122             client.shutdown_server(vm_id)
123             
124         elif action == RESTART:
125             print "Restarting VM"
126             snf.reboot_server(vm_id)
127
128
129         elif action == SUSPEND:
130             #TODO VM suspending
131             print "Suspending VM"