root / snf-cyclades-app / synnefo / api / actions.py @ 87920bc3
History | View | Annotate | Download (13.1 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from socket import getfqdn |
35 |
from vncauthproxy.client import request_forwarding as request_vnc_forwarding |
36 |
|
37 |
from django.db import transaction |
38 |
from django.conf import settings |
39 |
from django.http import HttpResponse |
40 |
from django.template.loader import render_to_string |
41 |
from django.utils import simplejson as json |
42 |
|
43 |
from synnefo.api.faults import (BadRequest, ServiceUnavailable, |
44 |
ItemNotFound, BuildInProgress) |
45 |
from synnefo.api.util import (random_password, get_vm, get_nic_from_index, |
46 |
get_network_free_address) |
47 |
from synnefo.db.models import NetworkInterface, Network |
48 |
from synnefo.db.pools import EmptyPool |
49 |
from synnefo.logic import backend |
50 |
from synnefo.logic.utils import get_rsapi_state |
51 |
|
52 |
from logging import getLogger |
53 |
log = getLogger(__name__) |
54 |
|
55 |
|
56 |
server_actions = {} |
57 |
network_actions = {} |
58 |
|
59 |
|
60 |
def server_action(name): |
61 |
'''Decorator for functions implementing server actions.
|
62 |
`name` is the key in the dict passed by the client.
|
63 |
'''
|
64 |
|
65 |
def decorator(func): |
66 |
server_actions[name] = func |
67 |
return func
|
68 |
return decorator
|
69 |
|
70 |
|
71 |
def network_action(name): |
72 |
'''Decorator for functions implementing network actions.
|
73 |
`name` is the key in the dict passed by the client.
|
74 |
'''
|
75 |
|
76 |
def decorator(func): |
77 |
network_actions[name] = func |
78 |
return func
|
79 |
return decorator
|
80 |
|
81 |
|
82 |
@server_action('changePassword') |
83 |
def change_password(request, vm, args): |
84 |
# Normal Response Code: 202
|
85 |
# Error Response Codes: computeFault (400, 500),
|
86 |
# serviceUnavailable (503),
|
87 |
# unauthorized (401),
|
88 |
# badRequest (400),
|
89 |
# badMediaType(415),
|
90 |
# itemNotFound (404),
|
91 |
# buildInProgress (409),
|
92 |
# overLimit (413)
|
93 |
|
94 |
raise ServiceUnavailable('Changing password is not supported.') |
95 |
|
96 |
|
97 |
@server_action('reboot') |
98 |
def reboot(request, vm, args): |
99 |
# Normal Response Code: 202
|
100 |
# Error Response Codes: computeFault (400, 500),
|
101 |
# serviceUnavailable (503),
|
102 |
# unauthorized (401),
|
103 |
# badRequest (400),
|
104 |
# badMediaType(415),
|
105 |
# itemNotFound (404),
|
106 |
# buildInProgress (409),
|
107 |
# overLimit (413)
|
108 |
|
109 |
log.info("Reboot VM %s", vm)
|
110 |
reboot_type = args.get('type', '') |
111 |
if reboot_type not in ('SOFT', 'HARD'): |
112 |
raise BadRequest('Malformed Request.') |
113 |
backend.reboot_instance(vm, reboot_type.lower()) |
114 |
return HttpResponse(status=202) |
115 |
|
116 |
|
117 |
@server_action('start') |
118 |
def start(request, vm, args): |
119 |
# Normal Response Code: 202
|
120 |
# Error Response Codes: serviceUnavailable (503),
|
121 |
# itemNotFound (404)
|
122 |
|
123 |
log.info("Start VM %s", vm)
|
124 |
if args:
|
125 |
raise BadRequest('Malformed Request.') |
126 |
backend.startup_instance(vm) |
127 |
return HttpResponse(status=202) |
128 |
|
129 |
|
130 |
@server_action('shutdown') |
131 |
def shutdown(request, vm, args): |
132 |
# Normal Response Code: 202
|
133 |
# Error Response Codes: serviceUnavailable (503),
|
134 |
# itemNotFound (404)
|
135 |
|
136 |
log.info("Shutdown VM %s", vm)
|
137 |
if args:
|
138 |
raise BadRequest('Malformed Request.') |
139 |
backend.shutdown_instance(vm) |
140 |
return HttpResponse(status=202) |
141 |
|
142 |
|
143 |
@server_action('rebuild') |
144 |
def rebuild(request, vm, args): |
145 |
# Normal Response Code: 202
|
146 |
# Error Response Codes: computeFault (400, 500),
|
147 |
# serviceUnavailable (503),
|
148 |
# unauthorized (401),
|
149 |
# badRequest (400),
|
150 |
# badMediaType(415),
|
151 |
# itemNotFound (404),
|
152 |
# buildInProgress (409),
|
153 |
# serverCapacityUnavailable (503),
|
154 |
# overLimit (413)
|
155 |
|
156 |
raise ServiceUnavailable('Rebuild not supported.') |
157 |
|
158 |
|
159 |
@server_action('resize') |
160 |
def resize(request, vm, args): |
161 |
# Normal Response Code: 202
|
162 |
# Error Response Codes: computeFault (400, 500),
|
163 |
# serviceUnavailable (503),
|
164 |
# unauthorized (401),
|
165 |
# badRequest (400),
|
166 |
# badMediaType(415),
|
167 |
# itemNotFound (404),
|
168 |
# buildInProgress (409),
|
169 |
# serverCapacityUnavailable (503),
|
170 |
# overLimit (413),
|
171 |
# resizeNotAllowed (403)
|
172 |
|
173 |
raise ServiceUnavailable('Resize not supported.') |
174 |
|
175 |
|
176 |
@server_action('confirmResize') |
177 |
def confirm_resize(request, vm, args): |
178 |
# Normal Response Code: 204
|
179 |
# Error Response Codes: computeFault (400, 500),
|
180 |
# serviceUnavailable (503),
|
181 |
# unauthorized (401),
|
182 |
# badRequest (400),
|
183 |
# badMediaType(415),
|
184 |
# itemNotFound (404),
|
185 |
# buildInProgress (409),
|
186 |
# serverCapacityUnavailable (503),
|
187 |
# overLimit (413),
|
188 |
# resizeNotAllowed (403)
|
189 |
|
190 |
raise ServiceUnavailable('Resize not supported.') |
191 |
|
192 |
|
193 |
@server_action('revertResize') |
194 |
def revert_resize(request, vm, args): |
195 |
# Normal Response Code: 202
|
196 |
# Error Response Codes: computeFault (400, 500),
|
197 |
# serviceUnavailable (503),
|
198 |
# unauthorized (401),
|
199 |
# badRequest (400),
|
200 |
# badMediaType(415),
|
201 |
# itemNotFound (404),
|
202 |
# buildInProgress (409),
|
203 |
# serverCapacityUnavailable (503),
|
204 |
# overLimit (413),
|
205 |
# resizeNotAllowed (403)
|
206 |
|
207 |
raise ServiceUnavailable('Resize not supported.') |
208 |
|
209 |
|
210 |
@server_action('console') |
211 |
def get_console(request, vm, args): |
212 |
"""Arrange for an OOB console of the specified type
|
213 |
|
214 |
This method arranges for an OOB console of the specified type.
|
215 |
Only consoles of type "vnc" are supported for now.
|
216 |
|
217 |
It uses a running instance of vncauthproxy to setup proper
|
218 |
VNC forwarding with a random password, then returns the necessary
|
219 |
VNC connection info to the caller.
|
220 |
|
221 |
"""
|
222 |
# Normal Response Code: 200
|
223 |
# Error Response Codes: computeFault (400, 500),
|
224 |
# serviceUnavailable (503),
|
225 |
# unauthorized (401),
|
226 |
# badRequest (400),
|
227 |
# badMediaType(415),
|
228 |
# itemNotFound (404),
|
229 |
# buildInProgress (409),
|
230 |
# overLimit (413)
|
231 |
|
232 |
log.info("Get console VM %s", vm)
|
233 |
|
234 |
console_type = args.get('type', '') |
235 |
if console_type != 'vnc': |
236 |
raise BadRequest('Type can only be "vnc".') |
237 |
|
238 |
# Use RAPI to get VNC console information for this instance
|
239 |
if get_rsapi_state(vm) != 'ACTIVE': |
240 |
raise BadRequest('Server not in ACTIVE state.') |
241 |
|
242 |
if settings.TEST:
|
243 |
console_data = {'kind': 'vnc', 'host': 'ganeti_node', 'port': 1000} |
244 |
else:
|
245 |
console_data = backend.get_instance_console(vm) |
246 |
|
247 |
if console_data['kind'] != 'vnc': |
248 |
message = 'got console of kind %s, not "vnc"' % console_data['kind'] |
249 |
raise ServiceUnavailable(message)
|
250 |
|
251 |
# Let vncauthproxy decide on the source port.
|
252 |
# The alternative: static allocation, e.g.
|
253 |
# sport = console_data['port'] - 1000
|
254 |
sport = 0
|
255 |
daddr = console_data['host']
|
256 |
dport = console_data['port']
|
257 |
password = random_password() |
258 |
|
259 |
if settings.TEST:
|
260 |
fwd = {'source_port': 1234, 'status': 'OK'} |
261 |
else:
|
262 |
fwd = request_vnc_forwarding(sport, daddr, dport, password) |
263 |
|
264 |
if fwd['status'] != "OK": |
265 |
raise ServiceUnavailable('vncauthproxy returned error status') |
266 |
|
267 |
# Verify that the VNC server settings haven't changed
|
268 |
if not settings.TEST: |
269 |
if console_data != backend.get_instance_console(vm):
|
270 |
raise ServiceUnavailable('VNC Server settings changed.') |
271 |
|
272 |
console = { |
273 |
'type': 'vnc', |
274 |
'host': getfqdn(),
|
275 |
'port': fwd['source_port'], |
276 |
'password': password}
|
277 |
|
278 |
if request.serialization == 'xml': |
279 |
mimetype = 'application/xml'
|
280 |
data = render_to_string('console.xml', {'console': console}) |
281 |
else:
|
282 |
mimetype = 'application/json'
|
283 |
data = json.dumps({'console': console})
|
284 |
|
285 |
return HttpResponse(data, mimetype=mimetype, status=200) |
286 |
|
287 |
|
288 |
@server_action('firewallProfile') |
289 |
@transaction.commit_on_success
|
290 |
def set_firewall_profile(request, vm, args): |
291 |
# Normal Response Code: 200
|
292 |
# Error Response Codes: computeFault (400, 500),
|
293 |
# serviceUnavailable (503),
|
294 |
# unauthorized (401),
|
295 |
# badRequest (400),
|
296 |
# badMediaType(415),
|
297 |
# itemNotFound (404),
|
298 |
# buildInProgress (409),
|
299 |
# overLimit (413)
|
300 |
|
301 |
profile = args.get('profile', '') |
302 |
log.info("Set VM %s firewall %s", vm, profile)
|
303 |
if profile not in [x[0] for x in NetworkInterface.FIREWALL_PROFILES]: |
304 |
raise BadRequest("Unsupported firewall profile") |
305 |
backend.set_firewall_profile(vm, profile) |
306 |
return HttpResponse(status=202) |
307 |
|
308 |
|
309 |
@network_action('add') |
310 |
@transaction.commit_on_success
|
311 |
def add(request, net, args): |
312 |
# Normal Response Code: 202
|
313 |
# Error Response Codes: computeFault (400, 500),
|
314 |
# serviceUnavailable (503),
|
315 |
# unauthorized (401),
|
316 |
# badRequest (400),
|
317 |
# badMediaType(415),
|
318 |
# itemNotFound (404),
|
319 |
# overLimit (413)
|
320 |
|
321 |
server_id = args.get('serverRef', None) |
322 |
if not server_id: |
323 |
raise BadRequest('Malformed Request.') |
324 |
vm = get_vm(server_id, request.user_uniq) |
325 |
|
326 |
log.info("Connect VM %s to Network %s", vm, net)
|
327 |
|
328 |
net = Network.objects.get(id=net.id) |
329 |
|
330 |
if net.state != 'ACTIVE': |
331 |
raise ServiceUnavailable('Network not active yet') |
332 |
|
333 |
address = None
|
334 |
if net.dhcp:
|
335 |
# Get a free IP from the address pool.
|
336 |
try:
|
337 |
address = get_network_free_address(net) |
338 |
except EmptyPool:
|
339 |
raise ServiceUnavailable('Network is full') |
340 |
|
341 |
log.info("Connecting VM %s to Network %s(%s)", vm, net, address)
|
342 |
|
343 |
backend.connect_to_network(vm, net, address) |
344 |
return HttpResponse(status=202) |
345 |
|
346 |
|
347 |
@network_action('remove') |
348 |
@transaction.commit_on_success
|
349 |
def remove(request, net, args): |
350 |
# Normal Response Code: 202
|
351 |
# Error Response Codes: computeFault (400, 500),
|
352 |
# serviceUnavailable (503),
|
353 |
# unauthorized (401),
|
354 |
# badRequest (400),
|
355 |
# badMediaType(415),
|
356 |
# itemNotFound (404),
|
357 |
# overLimit (413)
|
358 |
|
359 |
try: # attachment string: nic-<vm-id>-<nic-index> |
360 |
server_id = args.get('attachment', None).split('-')[1] |
361 |
nic_index = args.get('attachment', None).split('-')[2] |
362 |
except AttributeError: |
363 |
raise BadRequest("Malformed Request") |
364 |
except IndexError: |
365 |
raise BadRequest('Malformed Network Interface Id') |
366 |
|
367 |
if not server_id or not nic_index: |
368 |
raise BadRequest('Malformed Request.') |
369 |
vm = get_vm(server_id, request.user_uniq) |
370 |
nic = get_nic_from_index(vm, nic_index) |
371 |
|
372 |
log.info("Disconnect VM %s NIC %s", vm, str(nic.index)) |
373 |
|
374 |
if nic.dirty:
|
375 |
raise BuildInProgress('Machine is busy.') |
376 |
else:
|
377 |
vm.nics.all().update(dirty=True)
|
378 |
|
379 |
backend.disconnect_from_network(vm, nic) |
380 |
return HttpResponse(status=202) |