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