Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / backend.py @ 3a6be177

History | View | Annotate | Download (36.5 kB)

1
# Copyright 2011-2013 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
from django.conf import settings
34
from django.db import transaction
35
from datetime import datetime, timedelta
36

    
37
from synnefo.db.models import (Backend, VirtualMachine, Network,
38
                               BackendNetwork, BACKEND_STATUSES,
39
                               pooled_rapi_client, VirtualMachineDiagnostic,
40
                               Flavor)
41
from synnefo.logic import utils
42
from synnefo import quotas
43
from synnefo.api.util import release_resource, allocate_ip
44
from synnefo.util.mac2eui64 import mac2eui64
45
from synnefo.logic.rapi import GanetiApiError
46

    
47
from logging import getLogger
48
log = getLogger(__name__)
49

    
50

    
51
_firewall_tags = {
52
    'ENABLED': settings.GANETI_FIREWALL_ENABLED_TAG,
53
    'DISABLED': settings.GANETI_FIREWALL_DISABLED_TAG,
54
    'PROTECTED': settings.GANETI_FIREWALL_PROTECTED_TAG}
55

    
56
_reverse_tags = dict((v.split(':')[3], k) for k, v in _firewall_tags.items())
57

    
58
# Timeout in seconds for building NICs. After this period the NICs considered
59
# stale and removed from DB.
60
BUILDING_NIC_TIMEOUT = timedelta(seconds=180)
61

    
62
SIMPLE_NIC_FIELDS = ["state", "mac", "network", "firewall_profile", "index"]
63
COMPLEX_NIC_FIELDS = ["ipv4_address", "ipv6_address"]
64
NIC_FIELDS = SIMPLE_NIC_FIELDS + COMPLEX_NIC_FIELDS
65
UNKNOWN_NIC_PREFIX = "unknown-"
66

    
67

    
68
def handle_vm_quotas(vm, job_id, job_opcode, job_status, job_fields):
69
    """Handle quotas for updated VirtualMachine.
70

71
    Update quotas for the updated VirtualMachine based on the job that run on
72
    the Ganeti backend. If a commission has been already issued for this job,
73
    then this commission is just accepted or rejected based on the job status.
74
    Otherwise, a new commission for the given change is issued, that is also in
75
    force and auto-accept mode. In this case, previous commissions are
76
    rejected, since they reflect a previous state of the VM.
77

78
    """
79
    if job_status not in ["success", "error", "canceled"]:
80
        return vm
81

    
82
    # Check successful completion of a job will trigger any quotable change in
83
    # the VM state.
84
    action = utils.get_action_from_opcode(job_opcode, job_fields)
85
    if action == "BUILD":
86
        # Quotas for new VMs are automatically accepted by the API
87
        return vm
88
    commission_info = quotas.get_commission_info(vm, action=action,
89
                                                 action_fields=job_fields)
90

    
91
    if vm.task_job_id == job_id and vm.serial is not None:
92
        # Commission for this change has already been issued. So just
93
        # accept/reject it. Special case is OP_INSTANCE_CREATE, which even
94
        # if fails, must be accepted, as the user must manually remove the
95
        # failed server
96
        serial = vm.serial
97
        if job_status == "success":
98
            quotas.accept_serial(serial)
99
        elif job_status in ["error", "canceled"]:
100
            log.debug("Job %s failed. Rejecting related serial %s", job_id,
101
                      serial)
102
            quotas.reject_serial(serial)
103
        vm.serial = None
104
    elif job_status == "success" and commission_info is not None:
105
        log.debug("Expected job was %s. Processing job %s. Commission for"
106
                  " this job: %s", vm.task_job_id, job_id, commission_info)
107
        # Commission for this change has not been issued, or the issued
108
        # commission was unaware of the current change. Reject all previous
109
        # commissions and create a new one in forced mode!
110
        commission_name = ("client: dispatcher, resource: %s, ganeti_job: %s"
111
                           % (vm, job_id))
112
        quotas.handle_resource_commission(vm, action,
113
                                          commission_info=commission_info,
114
                                          commission_name=commission_name,
115
                                          force=True,
116
                                          auto_accept=True)
117
        log.debug("Issued new commission: %s", vm.serial)
118

    
119
    return vm
120

    
121

    
122
@transaction.commit_on_success
123
def process_op_status(vm, etime, jobid, opcode, status, logmsg, nics=None,
124
                      job_fields=None):
125
    """Process a job progress notification from the backend
126

127
    Process an incoming message from the backend (currently Ganeti).
128
    Job notifications with a terminating status (sucess, error, or canceled),
129
    also update the operating state of the VM.
130

131
    """
132
    # See #1492, #1031, #1111 why this line has been removed
133
    #if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
134
    if status not in [x[0] for x in BACKEND_STATUSES]:
135
        raise VirtualMachine.InvalidBackendMsgError(opcode, status)
136

    
137
    vm.backendjobid = jobid
138
    vm.backendjobstatus = status
139
    vm.backendopcode = opcode
140
    vm.backendlogmsg = logmsg
141

    
142
    if status in ["queued", "waiting", "running"]:
143
        vm.save()
144
        return
145

    
146
    if job_fields is None:
147
        job_fields = {}
148
    state_for_success = VirtualMachine.OPER_STATE_FROM_OPCODE.get(opcode)
149

    
150
    # Notifications of success change the operating state
151
    if status == "success":
152
        if state_for_success is not None:
153
            vm.operstate = state_for_success
154
        beparams = job_fields.get("beparams", None)
155
        if beparams:
156
            # Change the flavor of the VM
157
            _process_resize(vm, beparams)
158
        # Update backendtime only for jobs that have been successfully
159
        # completed, since only these jobs update the state of the VM. Else a
160
        # "race condition" may occur when a successful job (e.g.
161
        # OP_INSTANCE_REMOVE) completes before an error job and messages arrive
162
        # in reversed order.
163
        vm.backendtime = etime
164

    
165
    if status in ["success", "error", "canceled"] and nics is not None:
166
        # Update the NICs of the VM
167
        _process_net_status(vm, etime, nics)
168

    
169
    # Special case: if OP_INSTANCE_CREATE fails --> ERROR
170
    if opcode == 'OP_INSTANCE_CREATE' and status in ('canceled', 'error'):
171
        vm.operstate = 'ERROR'
172
        vm.backendtime = etime
173
    elif opcode == 'OP_INSTANCE_REMOVE':
174
        # Special case: OP_INSTANCE_REMOVE fails for machines in ERROR,
175
        # when no instance exists at the Ganeti backend.
176
        # See ticket #799 for all the details.
177
        if status == 'success' or (status == 'error' and
178
                                   not vm_exists_in_backend(vm)):
179
            # VM has been deleted
180
            for nic in vm.nics.all():
181
                # Release the IP
182
                remove_nic_ips(nic)
183
                # And delete the NIC.
184
                nic.delete()
185
            vm.deleted = True
186
            vm.operstate = state_for_success
187
            vm.backendtime = etime
188
            status = "success"
189

    
190
    if status in ["success", "error", "canceled"]:
191
        # Job is finalized: Handle quotas/commissioning
192
        vm = handle_vm_quotas(vm, job_id=jobid, job_opcode=opcode,
193
                              job_status=status, job_fields=job_fields)
194
        # and clear task fields
195
        if vm.task_job_id == jobid:
196
            vm.task = None
197
            vm.task_job_id = None
198

    
199
    vm.save()
200

    
201

    
202
def _process_resize(vm, beparams):
203
    """Change flavor of a VirtualMachine based on new beparams."""
204
    old_flavor = vm.flavor
205
    vcpus = beparams.get("vcpus", old_flavor.cpu)
206
    ram = beparams.get("maxmem", old_flavor.ram)
207
    if vcpus == old_flavor.cpu and ram == old_flavor.ram:
208
        return
209
    try:
210
        new_flavor = Flavor.objects.get(cpu=vcpus, ram=ram,
211
                                        disk=old_flavor.disk,
212
                                        disk_template=old_flavor.disk_template)
213
    except Flavor.DoesNotExist:
214
        raise Exception("Can not find flavor for VM")
215
    vm.flavor = new_flavor
216
    vm.save()
217

    
218

    
219
@transaction.commit_on_success
220
def process_net_status(vm, etime, nics):
221
    """Wrap _process_net_status inside transaction."""
222
    _process_net_status(vm, etime, nics)
223

    
224

    
225
def _process_net_status(vm, etime, nics):
226
    """Process a net status notification from the backend
227

228
    Process an incoming message from the Ganeti backend,
229
    detailing the NIC configuration of a VM instance.
230

231
    Update the state of the VM in the DB accordingly.
232

233
    """
234
    ganeti_nics = process_ganeti_nics(nics)
235
    db_nics = dict([(nic.id, nic)
236
                    for nic in vm.nics.prefetch_related("ips__subnet")])
237

    
238
    # Get X-Lock on backend before getting X-Lock on network IP pools, to
239
    # guarantee that no deadlock will occur with Backend allocator.
240
    Backend.objects.select_for_update().get(id=vm.backend_id)
241

    
242
    for nic_name in set(db_nics.keys()) | set(ganeti_nics.keys()):
243
        db_nic = db_nics.get(nic_name)
244
        ganeti_nic = ganeti_nics.get(nic_name)
245
        if ganeti_nic is None:
246
            # NIC exists in DB but not in Ganeti. If the NIC is in 'building'
247
            # state for more than 5 minutes, then we remove the NIC.
248
            # TODO: This is dangerous as the job may be stack in the queue, and
249
            # releasing the IP may lead to duplicate IP use.
250
            if db_nic.state != "BUILD" or\
251
                (db_nic.state == "BUILD" and
252
                 etime > db_nic.created + BUILDING_NIC_TIMEOUT):
253
                remove_nic_ips(db_nic)
254
                db_nic.delete()
255
            else:
256
                log.warning("Ignoring recent building NIC: %s", db_nic)
257
        elif db_nic is None:
258
            msg = ("NIC/%s of VM %s does not exist in DB! Cannot automatically"
259
                   " fix this issue!" % (nic_name, vm))
260
            log.error(msg)
261
            continue
262
        elif not nics_are_equal(db_nic, ganeti_nic):
263
            for f in SIMPLE_NIC_FIELDS:
264
                # Update the NIC in DB with the values from Ganeti NIC
265
                setattr(db_nic, f, ganeti_nic[f])
266
                db_nic.save()
267
            # Special case where the IPv4 address has changed, because you
268
            # need to release the old IPv4 address and reserve the new one
269
            ipv4_address = ganeti_nic["ipv4_address"]
270
            if db_nic.ipv4_address != ipv4_address:
271
                remove_nic_ips(db_nic)
272
                if ipv4_address:
273
                    network = ganeti_nic["network"]
274
                    ipaddress = allocate_ip(network, vm.userid,
275
                                            address=ipv4_address)
276
                    ipaddress.nic = nic
277
                    ipaddress.save()
278

    
279
    vm.backendtime = etime
280
    vm.save()
281

    
282

    
283
def nics_are_equal(db_nic, gnt_nic):
284
    for field in NIC_FIELDS:
285
        if getattr(db_nic, field) != gnt_nic[field]:
286
            return False
287
    return True
288

    
289

    
290
def process_ganeti_nics(ganeti_nics):
291
    """Process NIC dict from ganeti"""
292
    new_nics = []
293
    for index, gnic in enumerate(ganeti_nics):
294
        nic_name = gnic.get("name", None)
295
        if nic_name is not None:
296
            nic_id = utils.id_from_nic_name(nic_name)
297
        else:
298
            # Put as default value the index. If it is an unknown NIC to
299
            # synnefo it will be created automaticaly.
300
            nic_id = UNKNOWN_NIC_PREFIX + str(index)
301
        network_name = gnic.get('network', '')
302
        network_id = utils.id_from_network_name(network_name)
303
        network = Network.objects.get(id=network_id)
304

    
305
        # Get the new nic info
306
        mac = gnic.get('mac')
307
        ipv4 = gnic.get('ip')
308
        subnet6 = network.subnet6
309
        ipv6 = mac2eui64(mac, subnet6) if subnet6 else None
310

    
311
        firewall = gnic.get('firewall')
312
        firewall_profile = _reverse_tags.get(firewall)
313
        if not firewall_profile and network.public:
314
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE
315

    
316
        nic_info = {
317
            'index': index,
318
            'network': network,
319
            'mac': mac,
320
            'ipv4_address': ipv4,
321
            'ipv6_address': ipv6,
322
            'firewall_profile': firewall_profile,
323
            'state': 'ACTIVE'}
324

    
325
        new_nics.append((nic_id, nic_info))
326
    return dict(new_nics)
327

    
328

    
329
def remove_nic_ips(nic):
330
    """Remove IP addresses associated with a NetworkInterface.
331

332
    Remove all IP addresses that are associated with the NetworkInterface
333
    object, by returning them to the pool and deleting the IPAddress object. If
334
    the IP is a floating IP, then it is just disassociated from the NIC.
335

336
    """
337

    
338
    for ip in nic.ips.all():
339
        if ip.ipversion == 4:
340
            if ip.floating_ip:
341
                ip.nic = None
342
                ip.save()
343
            else:
344
                ip.release_address()
345
        if not ip.floating_ip:
346
            ip.delete()
347

    
348

    
349
@transaction.commit_on_success
350
def process_network_status(back_network, etime, jobid, opcode, status, logmsg):
351
    if status not in [x[0] for x in BACKEND_STATUSES]:
352
        raise Network.InvalidBackendMsgError(opcode, status)
353

    
354
    back_network.backendjobid = jobid
355
    back_network.backendjobstatus = status
356
    back_network.backendopcode = opcode
357
    back_network.backendlogmsg = logmsg
358

    
359
    network = back_network.network
360

    
361
    # Notifications of success change the operating state
362
    state_for_success = BackendNetwork.OPER_STATE_FROM_OPCODE.get(opcode, None)
363
    if status == 'success' and state_for_success is not None:
364
        back_network.operstate = state_for_success
365

    
366
    if status in ('canceled', 'error') and opcode == 'OP_NETWORK_ADD':
367
        back_network.operstate = 'ERROR'
368
        back_network.backendtime = etime
369

    
370
    if opcode == 'OP_NETWORK_REMOVE':
371
        network_is_deleted = (status == "success")
372
        if network_is_deleted or (status == "error" and not
373
                                  network_exists_in_backend(back_network)):
374
            back_network.operstate = state_for_success
375
            back_network.deleted = True
376
            back_network.backendtime = etime
377

    
378
    if status == 'success':
379
        back_network.backendtime = etime
380
    back_network.save()
381
    # Also you must update the state of the Network!!
382
    update_network_state(network)
383

    
384

    
385
def update_network_state(network):
386
    """Update the state of a Network based on BackendNetwork states.
387

388
    Update the state of a Network based on the operstate of the networks in the
389
    backends that network exists.
390

391
    The state of the network is:
392
    * ACTIVE: If it is 'ACTIVE' in at least one backend.
393
    * DELETED: If it is is 'DELETED' in all backends that have been created.
394

395
    This function also releases the resources (MAC prefix or Bridge) and the
396
    quotas for the network.
397

398
    """
399
    if network.deleted:
400
        # Network has already been deleted. Just assert that state is also
401
        # DELETED
402
        if not network.state == "DELETED":
403
            network.state = "DELETED"
404
            network.save()
405
        return
406

    
407
    backend_states = [s.operstate for s in network.backend_networks.all()]
408
    if not backend_states and network.action != "DESTROY":
409
        if network.state != "ACTIVE":
410
            network.state = "ACTIVE"
411
            network.save()
412
            return
413

    
414
    # Network is deleted when all BackendNetworks go to "DELETED" operstate
415
    deleted = reduce(lambda x, y: x == y and "DELETED", backend_states,
416
                     "DELETED")
417

    
418
    # Release the resources on the deletion of the Network
419
    if deleted:
420
        log.info("Network %r deleted. Releasing link %r mac_prefix %r",
421
                 network.id, network.mac_prefix, network.link)
422
        network.deleted = True
423
        network.state = "DELETED"
424
        if network.mac_prefix:
425
            if network.FLAVORS[network.flavor]["mac_prefix"] == "pool":
426
                release_resource(res_type="mac_prefix",
427
                                 value=network.mac_prefix)
428
        if network.link:
429
            if network.FLAVORS[network.flavor]["link"] == "pool":
430
                release_resource(res_type="bridge", value=network.link)
431

    
432
        # Issue commission
433
        if network.userid:
434
            quotas.issue_and_accept_commission(network, delete=True)
435
            # the above has already saved the object and committed;
436
            # a second save would override others' changes, since the
437
            # object is now unlocked
438
            return
439
        elif not network.public:
440
            log.warning("Network %s does not have an owner!", network.id)
441

    
442
        # TODO!!!!!
443
        # Set all subnets as deleted
444
        network.subnets.update(deleted=True)
445
        # And delete the IP pools
446
        network.subnets.ip_pools.all().delete()
447
    network.save()
448

    
449

    
450
@transaction.commit_on_success
451
def process_network_modify(back_network, etime, jobid, opcode, status,
452
                           job_fields):
453
    assert (opcode == "OP_NETWORK_SET_PARAMS")
454
    if status not in [x[0] for x in BACKEND_STATUSES]:
455
        raise Network.InvalidBackendMsgError(opcode, status)
456

    
457
    back_network.backendjobid = jobid
458
    back_network.backendjobstatus = status
459
    back_network.opcode = opcode
460

    
461
    add_reserved_ips = job_fields.get("add_reserved_ips")
462
    if add_reserved_ips:
463
        network = back_network.network
464
        for ip in add_reserved_ips:
465
            network.reserve_address(ip, external=True)
466

    
467
    if status == 'success':
468
        back_network.backendtime = etime
469
    back_network.save()
470

    
471

    
472
@transaction.commit_on_success
473
def process_create_progress(vm, etime, progress):
474

    
475
    percentage = int(progress)
476

    
477
    # The percentage may exceed 100%, due to the way
478
    # snf-image:copy-progress tracks bytes read by image handling processes
479
    percentage = 100 if percentage > 100 else percentage
480
    if percentage < 0:
481
        raise ValueError("Percentage cannot be negative")
482

    
483
    # FIXME: log a warning here, see #1033
484
#   if last_update > percentage:
485
#       raise ValueError("Build percentage should increase monotonically " \
486
#                        "(old = %d, new = %d)" % (last_update, percentage))
487

    
488
    # This assumes that no message of type 'ganeti-create-progress' is going to
489
    # arrive once OP_INSTANCE_CREATE has succeeded for a Ganeti instance and
490
    # the instance is STARTED.  What if the two messages are processed by two
491
    # separate dispatcher threads, and the 'ganeti-op-status' message for
492
    # successful creation gets processed before the 'ganeti-create-progress'
493
    # message? [vkoukis]
494
    #
495
    #if not vm.operstate == 'BUILD':
496
    #    raise VirtualMachine.IllegalState("VM is not in building state")
497

    
498
    vm.buildpercentage = percentage
499
    vm.backendtime = etime
500
    vm.save()
501

    
502

    
503
@transaction.commit_on_success
504
def create_instance_diagnostic(vm, message, source, level="DEBUG", etime=None,
505
                               details=None):
506
    """
507
    Create virtual machine instance diagnostic entry.
508

509
    :param vm: VirtualMachine instance to create diagnostic for.
510
    :param message: Diagnostic message.
511
    :param source: Diagnostic source identifier (e.g. image-helper).
512
    :param level: Diagnostic level (`DEBUG`, `INFO`, `WARNING`, `ERROR`).
513
    :param etime: The time the message occured (if available).
514
    :param details: Additional details or debug information.
515
    """
516
    VirtualMachineDiagnostic.objects.create_for_vm(vm, level, source=source,
517
                                                   source_date=etime,
518
                                                   message=message,
519
                                                   details=details)
520

    
521

    
522
def create_instance(vm, nics, flavor, image):
523
    """`image` is a dictionary which should contain the keys:
524
            'backend_id', 'format' and 'metadata'
525

526
        metadata value should be a dictionary.
527
    """
528

    
529
    # Handle arguments to CreateInstance() as a dictionary,
530
    # initialize it based on a deployment-specific value.
531
    # This enables the administrator to override deployment-specific
532
    # arguments, such as the disk template to use, name of os provider
533
    # and hypervisor-specific parameters at will (see Synnefo #785, #835).
534
    #
535
    kw = vm.backend.get_create_params()
536
    kw['mode'] = 'create'
537
    kw['name'] = vm.backend_vm_id
538
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
539

    
540
    kw['disk_template'] = flavor.disk_template
541
    kw['disks'] = [{"size": flavor.disk * 1024}]
542
    provider = flavor.disk_provider
543
    if provider:
544
        kw['disks'][0]['provider'] = provider
545
        kw['disks'][0]['origin'] = flavor.disk_origin
546

    
547
    kw['nics'] = [{"name": nic.backend_uuid,
548
                   "network": nic.network.backend_id,
549
                   "ip": nic.ipv4_address}
550
                  for nic in nics]
551
    backend = vm.backend
552
    depend_jobs = []
553
    for nic in nics:
554
        network = Network.objects.select_for_update().get(id=nic.network_id)
555
        bnet, created = BackendNetwork.objects.get_or_create(backend=backend,
556
                                                             network=network)
557
        if bnet.operstate != "ACTIVE":
558
            depend_jobs = create_network(network, backend, connect=True)
559
    kw["depends"] = [[job, ["success", "error", "canceled"]]
560
                     for job in depend_jobs]
561

    
562
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
563
    # kw['os'] = settings.GANETI_OS_PROVIDER
564
    kw['ip_check'] = False
565
    kw['name_check'] = False
566

    
567
    # Do not specific a node explicitly, have
568
    # Ganeti use an iallocator instead
569
    #kw['pnode'] = rapi.GetNodes()[0]
570

    
571
    kw['dry_run'] = settings.TEST
572

    
573
    kw['beparams'] = {
574
        'auto_balance': True,
575
        'vcpus': flavor.cpu,
576
        'memory': flavor.ram}
577

    
578
    kw['osparams'] = {
579
        'config_url': vm.config_url,
580
        # Store image id and format to Ganeti
581
        'img_id': image['backend_id'],
582
        'img_format': image['format']}
583

    
584
    # Use opportunistic locking
585
    kw['opportunistic_locking'] = True
586

    
587
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
588
    # kw['hvparams'] = dict(serial_console=False)
589

    
590
    log.debug("Creating instance %s", utils.hide_pass(kw))
591
    with pooled_rapi_client(vm) as client:
592
        return client.CreateInstance(**kw)
593

    
594

    
595
def delete_instance(vm):
596
    with pooled_rapi_client(vm) as client:
597
        return client.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)
598

    
599

    
600
def reboot_instance(vm, reboot_type):
601
    assert reboot_type in ('soft', 'hard')
602
    kwargs = {"instance": vm.backend_vm_id,
603
              "reboot_type": "hard"}
604
    # XXX: Currently shutdown_timeout parameter is not supported from the
605
    # Ganeti RAPI. Until supported, we will fallback for both reboot types
606
    # to the default shutdown timeout of Ganeti (120s). Note that reboot
607
    # type of Ganeti job must be always hard. The 'soft' and 'hard' type
608
    # of OS API is different from the one in Ganeti, and maps to
609
    # 'shutdown_timeout'.
610
    #if reboot_type == "hard":
611
    #    kwargs["shutdown_timeout"] = 0
612
    if settings.TEST:
613
        kwargs["dry_run"] = True
614
    with pooled_rapi_client(vm) as client:
615
        return client.RebootInstance(**kwargs)
616

    
617

    
618
def startup_instance(vm):
619
    with pooled_rapi_client(vm) as client:
620
        return client.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST)
621

    
622

    
623
def shutdown_instance(vm):
624
    with pooled_rapi_client(vm) as client:
625
        return client.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST)
626

    
627

    
628
def resize_instance(vm, vcpus, memory):
629
    beparams = {"vcpus": int(vcpus),
630
                "minmem": int(memory),
631
                "maxmem": int(memory)}
632
    with pooled_rapi_client(vm) as client:
633
        return client.ModifyInstance(vm.backend_vm_id, beparams=beparams)
634

    
635

    
636
def get_instance_console(vm):
637
    # RAPI GetInstanceConsole() returns endpoints to the vnc_bind_address,
638
    # which is a cluster-wide setting, either 0.0.0.0 or 127.0.0.1, and pretty
639
    # useless (see #783).
640
    #
641
    # Until this is fixed on the Ganeti side, construct a console info reply
642
    # directly.
643
    #
644
    # WARNING: This assumes that VNC runs on port network_port on
645
    #          the instance's primary node, and is probably
646
    #          hypervisor-specific.
647
    #
648
    log.debug("Getting console for vm %s", vm)
649

    
650
    console = {}
651
    console['kind'] = 'vnc'
652

    
653
    with pooled_rapi_client(vm) as client:
654
        i = client.GetInstance(vm.backend_vm_id)
655

    
656
    if vm.backend.hypervisor == "kvm" and i['hvparams']['serial_console']:
657
        raise Exception("hv parameter serial_console cannot be true")
658
    console['host'] = i['pnode']
659
    console['port'] = i['network_port']
660

    
661
    return console
662

    
663

    
664
def get_instance_info(vm):
665
    with pooled_rapi_client(vm) as client:
666
        return client.GetInstance(vm.backend_vm_id)
667

    
668

    
669
def vm_exists_in_backend(vm):
670
    try:
671
        get_instance_info(vm)
672
        return True
673
    except GanetiApiError as e:
674
        if e.code == 404:
675
            return False
676
        raise e
677

    
678

    
679
def get_network_info(backend_network):
680
    with pooled_rapi_client(backend_network) as client:
681
        return client.GetNetwork(backend_network.network.backend_id)
682

    
683

    
684
def network_exists_in_backend(backend_network):
685
    try:
686
        get_network_info(backend_network)
687
        return True
688
    except GanetiApiError as e:
689
        if e.code == 404:
690
            return False
691

    
692

    
693
def create_network(network, backend, connect=True):
694
    """Create a network in a Ganeti backend"""
695
    log.debug("Creating network %s in backend %s", network, backend)
696

    
697
    job_id = _create_network(network, backend)
698

    
699
    if connect:
700
        job_ids = connect_network(network, backend, depends=[job_id])
701
        return job_ids
702
    else:
703
        return [job_id]
704

    
705

    
706
def _create_network(network, backend):
707
    """Create a network."""
708

    
709
    tags = network.backend_tag
710
    subnet = None
711
    subnet6 = None
712
    gateway = None
713
    gateway6 = None
714
    for _subnet in network.subnets.all():
715
        if _subnet.ipversion == 4:
716
            if _subnet.dhcp:
717
                tags.append('nfdhcpd')
718
            subnet = _subnet.cidr
719
            gateway = _subnet.gateway
720
        elif _subnet.ipversion == 6:
721
            subnet6 = _subnet.cidr
722
            gateway6 = _subnet.gateway
723

    
724
    if network.public:
725
        conflicts_check = True
726
        tags.append('public')
727
    else:
728
        conflicts_check = False
729
        tags.append('private')
730

    
731
    # Use a dummy network subnet for IPv6 only networks. Currently Ganeti does
732
    # not support IPv6 only networks. To bypass this limitation, we create the
733
    # network with a dummy network subnet, and make Cyclades connect instances
734
    # to such networks, with address=None.
735
    if subnet is None:
736
        subnet = "10.0.0.0/24"
737

    
738
    try:
739
        bn = BackendNetwork.objects.get(network=network, backend=backend)
740
        mac_prefix = bn.mac_prefix
741
    except BackendNetwork.DoesNotExist:
742
        raise Exception("BackendNetwork for network '%s' in backend '%s'"
743
                        " does not exist" % (network.id, backend.id))
744

    
745
    with pooled_rapi_client(backend) as client:
746
        return client.CreateNetwork(network_name=network.backend_id,
747
                                    network=subnet,
748
                                    network6=subnet6,
749
                                    gateway=gateway,
750
                                    gateway6=gateway6,
751
                                    mac_prefix=mac_prefix,
752
                                    conflicts_check=conflicts_check,
753
                                    tags=tags)
754

    
755

    
756
def connect_network(network, backend, depends=[], group=None):
757
    """Connect a network to nodegroups."""
758
    log.debug("Connecting network %s to backend %s", network, backend)
759

    
760
    if network.public:
761
        conflicts_check = True
762
    else:
763
        conflicts_check = False
764

    
765
    depends = [[job, ["success", "error", "canceled"]] for job in depends]
766
    with pooled_rapi_client(backend) as client:
767
        groups = [group] if group is not None else client.GetGroups()
768
        job_ids = []
769
        for group in groups:
770
            job_id = client.ConnectNetwork(network.backend_id, group,
771
                                           network.mode, network.link,
772
                                           conflicts_check,
773
                                           depends=depends)
774
            job_ids.append(job_id)
775
    return job_ids
776

    
777

    
778
def delete_network(network, backend, disconnect=True):
779
    log.debug("Deleting network %s from backend %s", network, backend)
780

    
781
    depends = []
782
    if disconnect:
783
        depends = disconnect_network(network, backend)
784
    _delete_network(network, backend, depends=depends)
785

    
786

    
787
def _delete_network(network, backend, depends=[]):
788
    depends = [[job, ["success", "error", "canceled"]] for job in depends]
789
    with pooled_rapi_client(backend) as client:
790
        return client.DeleteNetwork(network.backend_id, depends)
791

    
792

    
793
def disconnect_network(network, backend, group=None):
794
    log.debug("Disconnecting network %s to backend %s", network, backend)
795

    
796
    with pooled_rapi_client(backend) as client:
797
        groups = [group] if group is not None else client.GetGroups()
798
        job_ids = []
799
        for group in groups:
800
            job_id = client.DisconnectNetwork(network.backend_id, group)
801
            job_ids.append(job_id)
802
    return job_ids
803

    
804

    
805
def connect_to_network(vm, nic):
806
    network = nic.network
807
    backend = vm.backend
808
    network = Network.objects.select_for_update().get(id=network.id)
809
    bnet, created = BackendNetwork.objects.get_or_create(backend=backend,
810
                                                         network=network)
811
    depend_jobs = []
812
    if bnet.operstate != "ACTIVE":
813
        depend_jobs = create_network(network, backend, connect=True)
814

    
815
    depends = [[job, ["success", "error", "canceled"]] for job in depend_jobs]
816

    
817
    nic = {'name': nic.backend_uuid,
818
           'network': network.backend_id,
819
           'ip': nic.ipv4_address}
820

    
821
    log.debug("Adding NIC %s to VM %s", nic, vm)
822

    
823
    kwargs = {
824
        "instance": vm.backend_vm_id,
825
        "nics": [("add", "-1", nic)],
826
        "depends": depends,
827
    }
828
    if vm.backend.use_hotplug():
829
        kwargs["hotplug"] = True
830
    if settings.TEST:
831
        kwargs["dry_run"] = True
832

    
833
    with pooled_rapi_client(vm) as client:
834
        return client.ModifyInstance(**kwargs)
835

    
836

    
837
def disconnect_from_network(vm, nic):
838
    log.debug("Removing NIC %s of VM %s", nic, vm)
839

    
840
    kwargs = {
841
        "instance": vm.backend_vm_id,
842
        "nics": [("remove", nic.backend_uuid, {})],
843
    }
844
    if vm.backend.use_hotplug():
845
        kwargs["hotplug"] = True
846
    if settings.TEST:
847
        kwargs["dry_run"] = True
848

    
849
    with pooled_rapi_client(vm) as client:
850
        jobID = client.ModifyInstance(**kwargs)
851
        firewall_profile = nic.firewall_profile
852
        if firewall_profile and firewall_profile != "DISABLED":
853
            tag = _firewall_tags[firewall_profile] % nic.backend_uuid
854
            client.DeleteInstanceTags(vm.backend_vm_id, [tag],
855
                                      dry_run=settings.TEST)
856

    
857
        return jobID
858

    
859

    
860
def set_firewall_profile(vm, profile, nic):
861
    uuid = nic.backend_uuid
862
    try:
863
        tag = _firewall_tags[profile] % uuid
864
    except KeyError:
865
        raise ValueError("Unsopported Firewall Profile: %s" % profile)
866

    
867
    log.debug("Setting tag of VM %s, NIC %s, to %s", vm, nic, profile)
868

    
869
    with pooled_rapi_client(vm) as client:
870
        # Delete previous firewall tags
871
        old_tags = client.GetInstanceTags(vm.backend_vm_id)
872
        delete_tags = [(t % uuid) for t in _firewall_tags.values()
873
                       if (t % uuid) in old_tags]
874
        if delete_tags:
875
            client.DeleteInstanceTags(vm.backend_vm_id, delete_tags,
876
                                      dry_run=settings.TEST)
877

    
878
        if profile != "DISABLED":
879
            client.AddInstanceTags(vm.backend_vm_id, [tag],
880
                                   dry_run=settings.TEST)
881

    
882
        # XXX NOP ModifyInstance call to force process_net_status to run
883
        # on the dispatcher
884
        os_name = settings.GANETI_CREATEINSTANCE_KWARGS['os']
885
        client.ModifyInstance(vm.backend_vm_id,
886
                              os_name=os_name)
887
    return None
888

    
889

    
890
def get_instances(backend, bulk=True):
891
    with pooled_rapi_client(backend) as c:
892
        return c.GetInstances(bulk=bulk)
893

    
894

    
895
def get_nodes(backend, bulk=True):
896
    with pooled_rapi_client(backend) as c:
897
        return c.GetNodes(bulk=bulk)
898

    
899

    
900
def get_jobs(backend, bulk=True):
901
    with pooled_rapi_client(backend) as c:
902
        return c.GetJobs(bulk=bulk)
903

    
904

    
905
def get_physical_resources(backend):
906
    """ Get the physical resources of a backend.
907

908
    Get the resources of a backend as reported by the backend (not the db).
909

910
    """
911
    nodes = get_nodes(backend, bulk=True)
912
    attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
913
    res = {}
914
    for a in attr:
915
        res[a] = 0
916
    for n in nodes:
917
        # Filter out drained, offline and not vm_capable nodes since they will
918
        # not take part in the vm allocation process
919
        can_host_vms = n['vm_capable'] and not (n['drained'] or n['offline'])
920
        if can_host_vms and n['cnodes']:
921
            for a in attr:
922
                res[a] += int(n[a] or 0)
923
    return res
924

    
925

    
926
def update_backend_resources(backend, resources=None):
927
    """ Update the state of the backend resources in db.
928

929
    """
930

    
931
    if not resources:
932
        resources = get_physical_resources(backend)
933

    
934
    backend.mfree = resources['mfree']
935
    backend.mtotal = resources['mtotal']
936
    backend.dfree = resources['dfree']
937
    backend.dtotal = resources['dtotal']
938
    backend.pinst_cnt = resources['pinst_cnt']
939
    backend.ctotal = resources['ctotal']
940
    backend.updated = datetime.now()
941
    backend.save()
942

    
943

    
944
def get_memory_from_instances(backend):
945
    """ Get the memory that is used from instances.
946

947
    Get the used memory of a backend. Note: This is different for
948
    the real memory used, due to kvm's memory de-duplication.
949

950
    """
951
    with pooled_rapi_client(backend) as client:
952
        instances = client.GetInstances(bulk=True)
953
    mem = 0
954
    for i in instances:
955
        mem += i['oper_ram']
956
    return mem
957

    
958

    
959
def get_available_disk_templates(backend):
960
    """Get the list of available disk templates of a Ganeti backend.
961

962
    The list contains the disk templates that are enabled in the Ganeti backend
963
    and also included in ipolicy-disk-templates.
964

965
    """
966
    with pooled_rapi_client(backend) as c:
967
        info = c.GetInfo()
968
    ipolicy_disk_templates = info["ipolicy"]["disk-templates"]
969
    try:
970
        enabled_disk_templates = info["enabled_disk_templates"]
971
        return [dp for dp in enabled_disk_templates
972
                if dp in ipolicy_disk_templates]
973
    except KeyError:
974
        # Ganeti < 2.8 does not have 'enabled_disk_templates'
975
        return ipolicy_disk_templates
976

    
977

    
978
def update_backend_disk_templates(backend):
979
    disk_templates = get_available_disk_templates(backend)
980
    backend.disk_templates = disk_templates
981
    backend.save()
982

    
983

    
984
##
985
## Synchronized operations for reconciliation
986
##
987

    
988

    
989
def create_network_synced(network, backend):
990
    result = _create_network_synced(network, backend)
991
    if result[0] != 'success':
992
        return result
993
    result = connect_network_synced(network, backend)
994
    return result
995

    
996

    
997
def _create_network_synced(network, backend):
998
    with pooled_rapi_client(backend) as client:
999
        job = _create_network(network, backend)
1000
        result = wait_for_job(client, job)
1001
    return result
1002

    
1003

    
1004
def connect_network_synced(network, backend):
1005
    with pooled_rapi_client(backend) as client:
1006
        for group in client.GetGroups():
1007
            job = client.ConnectNetwork(network.backend_id, group,
1008
                                        network.mode, network.link)
1009
            result = wait_for_job(client, job)
1010
            if result[0] != 'success':
1011
                return result
1012

    
1013
    return result
1014

    
1015

    
1016
def wait_for_job(client, jobid):
1017
    result = client.WaitForJobChange(jobid, ['status', 'opresult'], None, None)
1018
    status = result['job_info'][0]
1019
    while status not in ['success', 'error', 'cancel']:
1020
        result = client.WaitForJobChange(jobid, ['status', 'opresult'],
1021
                                         [result], None)
1022
        status = result['job_info'][0]
1023

    
1024
    if status == 'success':
1025
        return (status, None)
1026
    else:
1027
        error = result['job_info'][1]
1028
        return (status, error)