Revision b7ca7496 snfOCCI/compute.py
b/snfOCCI/compute.py | ||
---|---|---|
35 | 35 |
from snfOCCI.config import SERVER_CONFIG |
36 | 36 |
|
37 | 37 |
from occi.backend import ActionBackend, KindBackend |
38 |
from occi.extensions.infrastructure import START, STOP, SUSPEND, RESTART
|
|
38 |
from occi.extensions import infrastructure
|
|
39 | 39 |
from occi.exceptions import HTTPError |
40 | 40 |
|
41 | 41 |
|
... | ... | |
75 | 75 |
vm_name = entity.attributes['occi.core.title'] |
76 | 76 |
info = snf.create_server(vm_name, flavor_id, image_id) |
77 | 77 |
|
78 |
entity.actions = [START] |
|
79 | 78 |
entity.attributes['occi.compute.state'] = 'inactive' |
80 | 79 |
entity.attributes['occi.core.id'] = str(info['id']) |
81 | 80 |
entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch'] |
82 | 81 |
entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores'] |
83 | 82 |
entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory'] |
84 | 83 |
|
84 |
entity.actions = [infrastructure.STOP, |
|
85 |
infrastructure.SUSPEND, |
|
86 |
infrastructure.RESTART] |
|
87 |
|
|
85 | 88 |
# entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']} |
86 | 89 |
info['adminPass']= "" |
87 | 90 |
print info |
... | ... | |
108 | 111 |
|
109 | 112 |
status_dict = {'ACTIVE' : 'active', |
110 | 113 |
'STOPPED' : 'inactive', |
114 |
'REBOOT' : 'inactive', |
|
111 | 115 |
'ERROR' : 'inactive', |
112 | 116 |
'BUILD' : 'inactive', |
113 | 117 |
'DELETED' : 'inactive', |
118 |
'UNKNOWN' : 'inactive' |
|
114 | 119 |
} |
115 | 120 |
|
116 | 121 |
entity.attributes['occi.compute.state'] = status_dict[vm_state] |
... | ... | |
120 | 125 |
|
121 | 126 |
else: |
122 | 127 |
if entity.attributes['occi.compute.state'] == 'inactive': |
123 |
entity.actions = [START] |
|
128 |
entity.actions = [infrastructure.START]
|
|
124 | 129 |
if entity.attributes['occi.compute.state'] == 'active': |
125 |
entity.actions = [STOP, SUSPEND, RESTART]
|
|
130 |
entity.actions = [infrastructure.STOP, infrastructure.SUSPEND, infrastructure.RESTART]
|
|
126 | 131 |
|
127 | 132 |
|
128 | 133 |
def delete(self, entity, extras): |
129 | 134 |
|
130 | 135 |
#Deleting compute instance |
131 |
|
|
136 |
print "Deleting VM" + str(vm_id) |
|
132 | 137 |
snf = extras['snf'] |
133 | 138 |
vm_id = int(entity.attributes['occi.core.id']) |
134 | 139 |
snf.delete_server(vm_id) |
135 | 140 |
|
136 | 141 |
|
137 |
def action(self, entity, action, extras): |
|
142 |
def get_vm_actions(self, entity ,vm_state): |
|
143 |
|
|
144 |
actions = [] |
|
145 |
|
|
146 |
status_dict = {'ACTIVE' : 'active', |
|
147 |
'STOPPED' : 'inactive', |
|
148 |
'REBOOT' : 'inactive', |
|
149 |
'ERROR' : 'inactive', |
|
150 |
'BUILD' : 'inactive', |
|
151 |
'DELETED' : 'inactive', |
|
152 |
'UNKNOWN' : 'inactive' |
|
153 |
} |
|
154 |
|
|
155 |
if vm_state in status_dict: |
|
156 |
|
|
157 |
entity.attributes['occi.compute.state'] = status_dict[vm_state] |
|
158 |
if vm_state == 'ACTIVE': |
|
159 |
actions.append(infrastructure.STOP) |
|
160 |
actions.append(infrastructure.RESTART) |
|
161 |
elif vm_state in ('STOPPED'): |
|
162 |
actions.append(infrastructure.START) |
|
163 |
|
|
164 |
return actions |
|
165 |
else: |
|
166 |
raise HTTPError(500, 'Undefined status of the VM') |
|
167 |
|
|
168 |
def action(self, entity, action, attributes, extras): |
|
138 | 169 |
|
139 | 170 |
#Triggering action to compute instances |
140 | 171 |
|
... | ... | |
144 | 175 |
vm_id = int(entity.attributes['occi.core.id']) |
145 | 176 |
vm_info = snf.get_server_details(vm_id) |
146 | 177 |
vm_state = vm_info['status'] |
147 |
|
|
178 |
|
|
179 |
# Define the allowed actions depending on the state of the VM |
|
180 |
entity.actions = self.get_vm_actions(entity,vm_state) |
|
181 |
|
|
148 | 182 |
|
149 | 183 |
if vm_state == 'ERROR': |
150 | 184 |
raise HTTPError(500, 'ERROR building the compute instance') |
151 | 185 |
|
152 | 186 |
else: |
153 | 187 |
if action not in entity.actions: |
154 |
raise AttributeError("This action is currently no applicable.") |
|
188 |
raise AttributeError("This action is currently no applicable in the current status of the VM (CURRENT_STATE = " + str(vm_state)+ ").")
|
|
155 | 189 |
|
156 |
elif action == START: |
|
157 |
print "Starting VM" |
|
190 |
elif action == infrastructure.START:
|
|
191 |
print "Starting VM" + str(vm_id)
|
|
158 | 192 |
client.start_server(vm_id) |
159 | 193 |
|
160 |
elif action == STOP: |
|
161 |
print "Stopping VM" |
|
194 |
elif action == infrastructure.STOP:
|
|
195 |
print "Stopping VM" + str(vm_id)
|
|
162 | 196 |
client.shutdown_server(vm_id) |
163 | 197 |
|
164 |
elif action == RESTART: |
|
165 |
print "Restarting VM" |
|
198 |
elif action == infrastructure.RESTART:
|
|
199 |
print "Restarting VM" + str(vm_id)
|
|
166 | 200 |
snf.reboot_server(vm_id) |
167 | 201 |
|
168 |
elif action == SUSPEND: |
|
202 |
elif action == infrastructure.SUSPEND:
|
|
169 | 203 |
raise HTTPError(501, "This actions is currently no applicable") |
204 |
|
|
205 |
|
|
206 |
|
Also available in: Unified diff