root / snf-cyclades-app / synnefo / logic / backend.py @ 1a894bfe
History | View | Annotate | Download (16.2 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 |
import json |
35 |
|
36 |
from logging import getLogger |
37 |
from django.conf import settings |
38 |
from django.db import transaction |
39 |
from datetime import datetime |
40 |
|
41 |
from synnefo.db.models import (Backend, VirtualMachine, Network, NetworkLink) |
42 |
from synnefo.logic import utils |
43 |
from synnefo.util.rapi import GanetiRapiClient |
44 |
|
45 |
|
46 |
|
47 |
log = getLogger('synnefo.logic')
|
48 |
|
49 |
|
50 |
_firewall_tags = { |
51 |
'ENABLED': settings.GANETI_FIREWALL_ENABLED_TAG,
|
52 |
'DISABLED': settings.GANETI_FIREWALL_DISABLED_TAG,
|
53 |
'PROTECTED': settings.GANETI_FIREWALL_PROTECTED_TAG}
|
54 |
|
55 |
_reverse_tags = dict((v.split(':')[3], k) for k, v in _firewall_tags.items()) |
56 |
|
57 |
|
58 |
def create_client(hostname, port=5080, username=None, password=None): |
59 |
return GanetiRapiClient(hostname, port, username, password)
|
60 |
|
61 |
@transaction.commit_on_success
|
62 |
def process_op_status(vm, etime, jobid, opcode, status, logmsg): |
63 |
"""Process a job progress notification from the backend
|
64 |
|
65 |
Process an incoming message from the backend (currently Ganeti).
|
66 |
Job notifications with a terminating status (sucess, error, or canceled),
|
67 |
also update the operating state of the VM.
|
68 |
|
69 |
"""
|
70 |
# See #1492, #1031, #1111 why this line has been removed
|
71 |
#if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
|
72 |
if status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]: |
73 |
raise VirtualMachine.InvalidBackendMsgError(opcode, status)
|
74 |
|
75 |
vm.backendjobid = jobid |
76 |
vm.backendjobstatus = status |
77 |
vm.backendopcode = opcode |
78 |
vm.backendlogmsg = logmsg |
79 |
|
80 |
# Notifications of success change the operating state
|
81 |
state_for_success = VirtualMachine.OPER_STATE_FROM_OPCODE.get(opcode, None)
|
82 |
if status == 'success' and state_for_success is not None: |
83 |
utils.update_state(vm, state_for_success) |
84 |
# Set the deleted flag explicitly, cater for admin-initiated removals
|
85 |
if opcode == 'OP_INSTANCE_REMOVE': |
86 |
vm.deleted = True
|
87 |
vm.nics.all().delete() |
88 |
|
89 |
# Special case: if OP_INSTANCE_CREATE fails --> ERROR
|
90 |
if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE': |
91 |
utils.update_state(vm, 'ERROR')
|
92 |
|
93 |
# Special case: OP_INSTANCE_REMOVE fails for machines in ERROR,
|
94 |
# when no instance exists at the Ganeti backend.
|
95 |
# See ticket #799 for all the details.
|
96 |
#
|
97 |
if (status == 'error' and opcode == 'OP_INSTANCE_REMOVE' and |
98 |
vm.operstate == 'ERROR'):
|
99 |
vm.deleted = True
|
100 |
vm.nics.all().delete() |
101 |
|
102 |
vm.backendtime = etime |
103 |
# Any other notification of failure leaves the operating state unchanged
|
104 |
|
105 |
vm.save() |
106 |
|
107 |
|
108 |
@transaction.commit_on_success
|
109 |
def process_net_status(vm, etime, nics): |
110 |
"""Process a net status notification from the backend
|
111 |
|
112 |
Process an incoming message from the Ganeti backend,
|
113 |
detailing the NIC configuration of a VM instance.
|
114 |
|
115 |
Update the state of the VM in the DB accordingly.
|
116 |
"""
|
117 |
|
118 |
vm.nics.all().delete() |
119 |
for i, nic in enumerate(nics): |
120 |
if i == 0: |
121 |
net = Network.objects.get(public=True)
|
122 |
else:
|
123 |
try:
|
124 |
link = NetworkLink.objects.get(name=nic['link'])
|
125 |
except NetworkLink.DoesNotExist:
|
126 |
# Cannot find an instance of NetworkLink for
|
127 |
# the link attribute specified in the notification
|
128 |
raise NetworkLink.DoesNotExist("Cannot find a NetworkLink " |
129 |
"object for link='%s'" % nic['link']) |
130 |
net = link.network |
131 |
if net is None: |
132 |
raise Network.DoesNotExist("NetworkLink for link='%s' not " |
133 |
"associated with an existing Network instance." %
|
134 |
nic['link'])
|
135 |
|
136 |
firewall = nic.get('firewall', '') |
137 |
firewall_profile = _reverse_tags.get(firewall, '')
|
138 |
if not firewall_profile and net.public: |
139 |
firewall_profile = settings.DEFAULT_FIREWALL_PROFILE |
140 |
|
141 |
vm.nics.create( |
142 |
network=net, |
143 |
index=i, |
144 |
mac=nic.get('mac', ''), |
145 |
ipv4=nic.get('ip', ''), |
146 |
ipv6=nic.get('ipv6', ''), |
147 |
firewall_profile=firewall_profile) |
148 |
|
149 |
# network nics modified, update network object
|
150 |
net.save() |
151 |
|
152 |
vm.backendtime = etime |
153 |
vm.save() |
154 |
|
155 |
|
156 |
@transaction.commit_on_success
|
157 |
def process_create_progress(vm, etime, rprogress, wprogress): |
158 |
|
159 |
# XXX: This only uses the read progress for now.
|
160 |
# Explore whether it would make sense to use the value of wprogress
|
161 |
# somewhere.
|
162 |
percentage = int(rprogress)
|
163 |
|
164 |
# The percentage may exceed 100%, due to the way
|
165 |
# snf-progress-monitor tracks bytes read by image handling processes
|
166 |
percentage = 100 if percentage > 100 else percentage |
167 |
if percentage < 0: |
168 |
raise ValueError("Percentage cannot be negative") |
169 |
|
170 |
# FIXME: log a warning here, see #1033
|
171 |
# if last_update > percentage:
|
172 |
# raise ValueError("Build percentage should increase monotonically " \
|
173 |
# "(old = %d, new = %d)" % (last_update, percentage))
|
174 |
|
175 |
# This assumes that no message of type 'ganeti-create-progress' is going to
|
176 |
# arrive once OP_INSTANCE_CREATE has succeeded for a Ganeti instance and
|
177 |
# the instance is STARTED. What if the two messages are processed by two
|
178 |
# separate dispatcher threads, and the 'ganeti-op-status' message for
|
179 |
# successful creation gets processed before the 'ganeti-create-progress'
|
180 |
# message? [vkoukis]
|
181 |
#
|
182 |
#if not vm.operstate == 'BUILD':
|
183 |
# raise VirtualMachine.IllegalState("VM is not in building state")
|
184 |
|
185 |
vm.buildpercentage = percentage |
186 |
vm.backendtime = etime |
187 |
vm.save() |
188 |
|
189 |
|
190 |
def start_action(vm, action): |
191 |
"""Update the state of a VM when a new action is initiated."""
|
192 |
if not action in [x[0] for x in VirtualMachine.ACTIONS]: |
193 |
raise VirtualMachine.InvalidActionError(action)
|
194 |
|
195 |
# No actions to deleted and no actions beside destroy to suspended VMs
|
196 |
if vm.deleted:
|
197 |
raise VirtualMachine.DeletedError
|
198 |
|
199 |
# No actions to machines being built. They may be destroyed, however.
|
200 |
if vm.operstate == 'BUILD' and action != 'DESTROY': |
201 |
raise VirtualMachine.BuildingError
|
202 |
|
203 |
vm.action = action |
204 |
vm.backendjobid = None
|
205 |
vm.backendopcode = None
|
206 |
vm.backendjobstatus = None
|
207 |
vm.backendlogmsg = None
|
208 |
|
209 |
# Update the relevant flags if the VM is being suspended or destroyed.
|
210 |
# Do not set the deleted flag here, see ticket #721.
|
211 |
#
|
212 |
# The deleted flag is set asynchronously, when an OP_INSTANCE_REMOVE
|
213 |
# completes successfully. Hence, a server may be visible for some time
|
214 |
# after a DELETE /servers/id returns HTTP 204.
|
215 |
#
|
216 |
if action == "DESTROY": |
217 |
# vm.deleted = True
|
218 |
pass
|
219 |
elif action == "SUSPEND": |
220 |
vm.suspended = True
|
221 |
elif action == "START": |
222 |
vm.suspended = False
|
223 |
vm.save() |
224 |
|
225 |
|
226 |
def create_instance(vm, flavor, image, password, personality): |
227 |
"""`image` is a dictionary which should contain the keys:
|
228 |
'backend_id', 'format' and 'metadata'
|
229 |
|
230 |
metadata value should be a dictionary.
|
231 |
"""
|
232 |
nic = {'ip': 'pool', 'network': settings.GANETI_PUBLIC_NETWORK} |
233 |
|
234 |
if settings.IGNORE_FLAVOR_DISK_SIZES:
|
235 |
if image['backend_id'].find("windows") >= 0: |
236 |
sz = 14000
|
237 |
else:
|
238 |
sz = 4000
|
239 |
else:
|
240 |
sz = flavor.disk * 1024
|
241 |
|
242 |
# Handle arguments to CreateInstance() as a dictionary,
|
243 |
# initialize it based on a deployment-specific value.
|
244 |
# This enables the administrator to override deployment-specific
|
245 |
# arguments, such as the disk template to use, name of os provider
|
246 |
# and hypervisor-specific parameters at will (see Synnefo #785, #835).
|
247 |
#
|
248 |
kw = settings.GANETI_CREATEINSTANCE_KWARGS |
249 |
kw['mode'] = 'create' |
250 |
kw['name'] = vm.backend_vm_id
|
251 |
# Defined in settings.GANETI_CREATEINSTANCE_KWARGS
|
252 |
kw['disk_template'] = flavor.disk_template
|
253 |
kw['disks'] = [{"size": sz}] |
254 |
kw['nics'] = [nic]
|
255 |
# Defined in settings.GANETI_CREATEINSTANCE_KWARGS
|
256 |
# kw['os'] = settings.GANETI_OS_PROVIDER
|
257 |
kw['ip_check'] = False |
258 |
kw['name_check'] = False |
259 |
# Do not specific a node explicitly, have
|
260 |
# Ganeti use an iallocator instead
|
261 |
#
|
262 |
# kw['pnode']=rapi.GetNodes()[0]
|
263 |
kw['dry_run'] = settings.TEST
|
264 |
|
265 |
kw['beparams'] = {
|
266 |
'auto_balance': True, |
267 |
'vcpus': flavor.cpu,
|
268 |
'memory': flavor.ram}
|
269 |
|
270 |
kw['osparams'] = {
|
271 |
'img_id': image['backend_id'], |
272 |
'img_passwd': password,
|
273 |
'img_format': image['format']} |
274 |
if personality:
|
275 |
kw['osparams']['img_personality'] = json.dumps(personality) |
276 |
|
277 |
kw['osparams']['img_properties'] = json.dumps(image['metadata']) |
278 |
|
279 |
# Defined in settings.GANETI_CREATEINSTANCE_KWARGS
|
280 |
# kw['hvparams'] = dict(serial_console=False)
|
281 |
|
282 |
return vm.client.CreateInstance(**kw)
|
283 |
|
284 |
|
285 |
def delete_instance(vm): |
286 |
start_action(vm, 'DESTROY')
|
287 |
vm.client.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST) |
288 |
|
289 |
|
290 |
def reboot_instance(vm, reboot_type): |
291 |
assert reboot_type in ('soft', 'hard') |
292 |
vm.client.RebootInstance(vm.backend_vm_id, reboot_type, dry_run=settings.TEST) |
293 |
log.info('Rebooting instance %s', vm.backend_vm_id)
|
294 |
|
295 |
|
296 |
def startup_instance(vm): |
297 |
start_action(vm, 'START')
|
298 |
vm.client.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST) |
299 |
|
300 |
|
301 |
def shutdown_instance(vm): |
302 |
start_action(vm, 'STOP')
|
303 |
vm.client.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST) |
304 |
|
305 |
|
306 |
def get_instance_console(vm): |
307 |
# RAPI GetInstanceConsole() returns endpoints to the vnc_bind_address,
|
308 |
# which is a cluster-wide setting, either 0.0.0.0 or 127.0.0.1, and pretty
|
309 |
# useless (see #783).
|
310 |
#
|
311 |
# Until this is fixed on the Ganeti side, construct a console info reply
|
312 |
# directly.
|
313 |
#
|
314 |
# WARNING: This assumes that VNC runs on port network_port on
|
315 |
# the instance's primary node, and is probably
|
316 |
# hypervisor-specific.
|
317 |
#
|
318 |
console = {} |
319 |
console['kind'] = 'vnc' |
320 |
i = vm.client.GetInstance(vm.backend_vm_id) |
321 |
if i['hvparams']['serial_console']: |
322 |
raise Exception("hv parameter serial_console cannot be true") |
323 |
console['host'] = i['pnode'] |
324 |
console['port'] = i['network_port'] |
325 |
|
326 |
return console
|
327 |
# return rapi.GetInstanceConsole(vm.backend_vm_id)
|
328 |
|
329 |
|
330 |
def request_status_update(vm): |
331 |
return vm.client.GetInstanceInfo(vm.backend_vm_id)
|
332 |
|
333 |
|
334 |
def update_status(vm, status): |
335 |
utils.update_state(vm, status) |
336 |
|
337 |
|
338 |
def create_network_link(): |
339 |
try:
|
340 |
last = NetworkLink.objects.order_by('-index')[0] |
341 |
index = last.index + 1
|
342 |
except IndexError: |
343 |
index = 1
|
344 |
|
345 |
if index <= settings.GANETI_MAX_LINK_NUMBER:
|
346 |
name = '%s%d' % (settings.GANETI_LINK_PREFIX, index)
|
347 |
return NetworkLink.objects.create(index=index, name=name,
|
348 |
available=True)
|
349 |
return None # All link slots are filled |
350 |
|
351 |
|
352 |
@transaction.commit_on_success
|
353 |
def create_network(name, user_id): |
354 |
try:
|
355 |
link = NetworkLink.objects.filter(available=True)[0] |
356 |
except IndexError: |
357 |
link = create_network_link() |
358 |
if not link: |
359 |
raise NetworkLink.NotAvailable
|
360 |
|
361 |
network = Network.objects.create( |
362 |
name=name, |
363 |
userid=user_id, |
364 |
state='ACTIVE',
|
365 |
link=link) |
366 |
|
367 |
link.network = network |
368 |
link.available = False
|
369 |
link.save() |
370 |
|
371 |
return network
|
372 |
|
373 |
|
374 |
@transaction.commit_on_success
|
375 |
def delete_network(net): |
376 |
link = net.link |
377 |
if link.name != settings.GANETI_NULL_LINK:
|
378 |
link.available = True
|
379 |
link.network = None
|
380 |
link.save() |
381 |
|
382 |
for vm in net.machines.all(): |
383 |
disconnect_from_network(vm, net) |
384 |
vm.save() |
385 |
net.state = 'DELETED'
|
386 |
net.save() |
387 |
|
388 |
|
389 |
def connect_to_network(vm, net): |
390 |
nic = {'mode': 'bridged', 'link': net.link.name} |
391 |
vm.client.ModifyInstance(vm.backend_vm_id, nics=[('add', -1, nic)], |
392 |
hotplug=True, dry_run=settings.TEST)
|
393 |
|
394 |
|
395 |
def disconnect_from_network(vm, net): |
396 |
nics = vm.nics.filter(network__public=False).order_by('index') |
397 |
ops = [('remove', nic.index, {}) for nic in nics if nic.network == net] |
398 |
if not ops: # Vm not connected to network |
399 |
return
|
400 |
vm.client.ModifyInstance(vm.backend_vm_id, nics=ops[::-1],
|
401 |
hotplug=True, dry_run=settings.TEST)
|
402 |
|
403 |
|
404 |
def set_firewall_profile(vm, profile): |
405 |
try:
|
406 |
tag = _firewall_tags[profile] |
407 |
except KeyError: |
408 |
raise ValueError("Unsopported Firewall Profile: %s" % profile) |
409 |
|
410 |
client = vm.client |
411 |
# Delete all firewall tags
|
412 |
for t in _firewall_tags.values(): |
413 |
client.DeleteInstanceTags(vm.backend_vm_id, [t], dry_run=settings.TEST) |
414 |
|
415 |
client.AddInstanceTags(vm.backend_vm_id, [tag], dry_run=settings.TEST) |
416 |
|
417 |
# XXX NOP ModifyInstance call to force process_net_status to run
|
418 |
# on the dispatcher
|
419 |
vm.client.ModifyInstance(vm.backend_vm_id, |
420 |
os_name=settings.GANETI_CREATEINSTANCE_KWARGS['os'])
|
421 |
|
422 |
|
423 |
def get_ganeti_instances(backend=None, bulk=False): |
424 |
Instances = [c.client.GetInstances(bulk=bulk)\ |
425 |
for c in get_backends(backend)] |
426 |
return reduce(list.__add__, Instances, []) |
427 |
|
428 |
|
429 |
def get_ganeti_nodes(backend=None, bulk=False): |
430 |
Nodes = [c.client.GetNodes(bulk=bulk) for c in get_backends(backend)] |
431 |
return reduce(list.__add__, Nodes, []) |
432 |
|
433 |
|
434 |
def get_ganeti_jobs(backend=None, bulk=False): |
435 |
Jobs = [c.client.GetJobs(bulk=bulk) for c in get_backends(backend)] |
436 |
return reduce(list.__add__, Jobs, []) |
437 |
|
438 |
##
|
439 |
##
|
440 |
##
|
441 |
|
442 |
|
443 |
def get_backends(backend=None): |
444 |
if backend:
|
445 |
return [backend]
|
446 |
return Backend.objects.all()
|
447 |
|
448 |
|
449 |
def get_physical_resources(backend): |
450 |
""" Get the physical resources of a backend.
|
451 |
|
452 |
Get the resources of a backend as reported by the backend (not the db).
|
453 |
|
454 |
"""
|
455 |
nodes = get_ganeti_nodes(backend, bulk=True)
|
456 |
attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] |
457 |
res = {} |
458 |
for a in attr: |
459 |
res[a] = 0
|
460 |
for n in nodes: |
461 |
# Filter out drained, offline and not vm_capable nodes since they will
|
462 |
# not take part in the vm allocation process
|
463 |
if n['vm_capable'] and not n['drained'] and not n['offline']\ |
464 |
and n['cnodes']: |
465 |
for a in attr: |
466 |
res[a] += int(n[a])
|
467 |
return res
|
468 |
|
469 |
|
470 |
def update_resources(backend, resources=None): |
471 |
""" Update the state of the backend resources in db.
|
472 |
|
473 |
"""
|
474 |
|
475 |
if not resources: |
476 |
resources = get_physical_resources(backend) |
477 |
|
478 |
backend.mfree = resources['mfree']
|
479 |
backend.mtotal = resources['mtotal']
|
480 |
backend.dfree = resources['dfree']
|
481 |
backend.dtotal = resources['dtotal']
|
482 |
backend.pinst_cnt = resources['pinst_cnt']
|
483 |
backend.ctotal = resources['ctotal']
|
484 |
backend.updated = datetime.now() |
485 |
backend.save() |
486 |
|
487 |
|
488 |
def get_memory_from_instances(backend): |
489 |
""" Get the memory that is used from instances.
|
490 |
|
491 |
Get the used memory of a backend. Note: This is different for
|
492 |
the real memory used, due to kvm's memory de-duplication.
|
493 |
|
494 |
"""
|
495 |
instances = backend.client.GetInstances(bulk=True)
|
496 |
mem = 0
|
497 |
for i in instances: |
498 |
mem += i['oper_ram']
|
499 |
return mem
|