servers.py 21 KB
Newer Older
1
2
3
4
5
6
7
8
9
import logging

from socket import getfqdn
from functools import wraps
from django import dispatch
from django.db import transaction
from django.utils import simplejson as json

from snf_django.lib.api import faults
10
from django.conf import settings
11
12
13
14
from synnefo import quotas
from synnefo.api import util
from synnefo.logic import backend
from synnefo.logic.backend_allocator import BackendAllocator
15
from synnefo.db import pools
16
from synnefo.db.models import (NetworkInterface, VirtualMachine,
17
                               VirtualMachineMetadata, IPAddressLog)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from vncauthproxy.client import request_forwarding as request_vnc_forwarding

log = logging.getLogger(__name__)

# server creation signal
server_created = dispatch.Signal(providing_args=["created_vm_params"])


def validate_server_action(vm, action):
    if vm.deleted:
        raise faults.BadRequest("Server '%s' has been deleted." % vm.id)

    # Destroyin a server should always be permitted
    if action == "DESTROY":
        return

    # Check that there is no pending action
    pending_action = vm.task
    if pending_action:
        if pending_action == "BUILD":
            raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
        raise faults.BadRequest("Can not perform '%s' action while there is a"
                                " pending '%s'." % (action, pending_action))

    # Check if action can be performed to VM's operstate
    operstate = vm.operstate
44
    if operstate == "BUILD" and action != "BUILD":
45
        raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
46
47
48
49
50
    elif (action == "START" and operstate != "STOPPED") or\
         (action == "STOP" and operstate != "STARTED") or\
         (action == "RESIZE" and operstate != "STOPPED") or\
         (action in ["CONNECT", "DISCONNECT"] and operstate != "STOPPED"
          and not settings.GANETI_USE_HOTPLUG):
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        raise faults.BadRequest("Can not perform '%s' action while server is"
                                " in '%s' state." % (action, operstate))
    return


def server_command(action):
    """Handle execution of a server action.

    Helper function to validate and execute a server action, handle quota
    commission and update the 'task' of the VM in the DB.

    1) Check if action can be performed. If it can, then there must be no
       pending task (with the exception of DESTROY).
    2) Handle previous commission if unresolved:
       * If it is not pending and it to accept, then accept
       * If it is not pending and to reject or is pending then reject it. Since
       the action can be performed only if there is no pending task, then there
       can be no pending commission. The exception is DESTROY, but in this case
       the commission can safely be rejected, and the dispatcher will generate
       the correct ones!
    3) Issue new commission and associate it with the VM. Also clear the task.
    4) Send job to ganeti
    5) Update task and commit
    """
    def decorator(func):
        @wraps(func)
        @transaction.commit_on_success
        def wrapper(vm, *args, **kwargs):
            user_id = vm.userid
            validate_server_action(vm, action)
81
            vm.action = action
82

83
84
85
86
            commission_name = "client: api, resource: %s" % vm
            quotas.handle_resource_commission(vm, action=action,
                                              commission_name=commission_name)
            vm.save()
87

88
89
90
91
92
93
94
95
96
97
98
            # XXX: Special case for server creation!
            if action == "BUILD":
                # Perform a commit, because the VirtualMachine must be saved to
                # DB before the OP_INSTANCE_CREATE job in enqueued in Ganeti.
                # Otherwise, messages will arrive from snf-dispatcher about
                # this instance, before the VM is stored in DB.
                transaction.commit()
                # After committing the locks are released. Refetch the instance
                # to guarantee x-lock.
                vm = VirtualMachine.objects.select_for_update().get(id=vm.id)

99
100
101
102
103
104
105
106
107
108
109
110
111
            # Send the job to Ganeti and get the associated jobID
            try:
                job_id = func(vm, *args, **kwargs)
            except Exception as e:
                if vm.serial is not None:
                    # Since the job never reached Ganeti, reject the commission
                    log.debug("Rejecting commission: '%s', could not perform"
                              " action '%s': %s" % (vm.serial,  action, e))
                    transaction.rollback()
                    quotas.reject_serial(vm.serial)
                    transaction.commit()
                raise

112
113
114
115
116
117
118
            if action == "BUILD" and vm.serial is not None:
                # XXX: Special case for server creation: we must accept the
                # commission because the VM has been stored in DB. Also, if
                # communication with Ganeti fails, the job will never reach
                # Ganeti, and the commission will never be resolved.
                quotas.accept_serial(vm.serial)

119
120
121
122
123
124
125
126
127
128
129
130
131
132
            log.info("user: %s, vm: %s, action: %s, job_id: %s, serial: %s",
                     user_id, vm.id, action, job_id, vm.serial)

            # store the new task in the VM
            if job_id is not None:
                vm.task = action
                vm.task_job_id = job_id
            vm.save()

            return vm
        return wrapper
    return decorator


133
@transaction.commit_on_success
134
def create(userid, name, password, flavor, image, metadata={},
135
           personality=[], networks=None, floating_ips=None,
136
           use_backend=None):
137
    if use_backend is None:
138
139
        # Allocate server to a Ganeti backend
        use_backend = allocate_new_server(userid, flavor)
140

141
142
    if networks is None:
        networks = []
143
144
145
    if floating_ips is None:
        floating_ips = []

146
147
148
149
150
151
152
153
154
155
156
157
    # Fix flavor for archipelago
    disk_template, provider = util.get_flavor_provider(flavor)
    if provider:
        flavor.disk_template = disk_template
        flavor.disk_provider = provider
        flavor.disk_origin = None
        if provider == 'vlmc':
            flavor.disk_origin = image['checksum']
            image['backend_id'] = 'null'
    else:
        flavor.disk_provider = None

158
159
160
161
162
163
164
165
166
167
    # We must save the VM instance now, so that it gets a valid
    # vm.backend_vm_id.
    vm = VirtualMachine.objects.create(name=name,
                                       backend=use_backend,
                                       userid=userid,
                                       imageid=image["id"],
                                       flavor=flavor,
                                       operstate="BUILD")
    log.info("Created entry in DB for VM '%s'", vm)

168
    nics = create_instance_nics(vm, userid, networks, floating_ips)
169
170
171
172
173
174

    for key, val in metadata.items():
        VirtualMachineMetadata.objects.create(
            meta_key=key,
            meta_value=val,
            vm=vm)
175

176
    # Create the server in Ganeti.
177
    vm = create_server(vm, nics, flavor, image, personality, password)
178
179
180
181

    return vm


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@transaction.commit_on_success
def allocate_new_server(userid, flavor):
    """Allocate a new server to a Ganeti backend.

    Allocation is performed based on the owner of the server and the specified
    flavor. Also, backends that do not have a public IPv4 address are excluded
    from server allocation.

    This function runs inside a transaction, because after allocating the
    instance a commit must be performed in order to release all locks.

    """
    backend_allocator = BackendAllocator()
    use_backend = backend_allocator.allocate(userid, flavor)
    if use_backend is None:
        log.error("No available backend for VM with flavor %s", flavor)
        raise faults.ServiceUnavailable("No available backends")
    return use_backend


@server_command("BUILD")
def create_server(vm, nics, flavor, image, personality, password):
    # dispatch server created signal needed to trigger the 'vmapi', which
    # enriches the vm object with the 'config_url' attribute which must be
    # passed to the Ganeti job.
    server_created.send(sender=vm, created_vm_params={
        'img_id': image['backend_id'],
        'img_passwd': password,
        'img_format': str(image['format']),
        'img_personality': json.dumps(personality),
        'img_properties': json.dumps(image['metadata']),
    })
    # send job to Ganeti
215
216
217
218
219
220
221
222
223
224
    try:
        jobID = backend.create_instance(vm, nics, flavor, image)
    except:
        log.exception("Failed create instance '%s'", vm)
        jobID = None
        vm.operstate = "ERROR"
        vm.backendlogmsg = "Failed to send job to Ganeti."
        vm.save()
        vm.nics.all().update(state="ERROR")

225
226
227
228
229
230
231
232
233
    # At this point the job is enqueued in the Ganeti backend
    vm.backendjobid = jobID
    vm.save()
    log.info("User %s created VM %s, NICs %s, Backend %s, JobID %s",
             vm.userid, vm, nics, backend, str(jobID))

    return jobID


234
def create_instance_nics(vm, userid, networks=[], floating_ips=[]):
235
236
237
238
    """Create NICs for VirtualMachine.

    Helper function for allocating IP addresses and creating NICs in the DB
    for a VirtualMachine. Created NICs are the combination of the default
239
240
    network policy (defined by administration settings) and the networks
    defined by the user.
241
242

    """
243
    ports = []
244
    for network_id in settings.DEFAULT_INSTANCE_NETWORKS:
245
        if network_id == "SNF:ANY_PUBLIC":
246
247
            ipaddress = util.allocate_public_ip(userid=userid,
                                                backend=vm.backend)
248
249
            port = _create_port(userid, network=ipaddress.network,
                                use_ipaddress=ipaddress)
250
251
        else:
            try:
252
253
254
                network = util.get_network(network_id, userid,
                                           non_deleted=True)
            except faults.ItemNotFound:
255
256
257
258
                msg = "Invalid configuration. Setting"\
                      " 'DEFAULT_INSTANCE_NETWORKS' contains invalid"\
                      " network '%s'" % network_id
                log.error(msg)
259
                raise faults.InternalServerError(msg)
260
261
262
263
            port = _create_port(userid, network)
        ports.append(port)

    for floating_ip_id in floating_ips:
264
        floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id,
265
266
267
268
269
270
271
272
273
274
275
276
                                                 for_update=True)
        port = _create_port(userid, network=floating_ip.network,
                            use_ipaddress=floating_ip)
        ports.append(port)

    for net in networks:
        port_id = net.get("port")
        net_id = net.get("uuid")
        if port_id is not None:
            port = util.get_port(port_id, userid, for_update=True)
            ports.append(port)
        elif net_id is not None:
277
            address = net.get("fixed_ip")
278
279
            network = util.get_network(net_id, userid, non_deleted=True)
            if network.public:
280
281
282
283
284
285
286
287
288
289
290
                if address is None:
                    msg = ("Can not connect to public network %s. Specify"
                           " 'fixed_ip'" " attribute to connect to a public"
                           " network")
                    raise faults.BadRequest(msg % network.id)
                floating_ip = util.get_floating_ip_by_address(userid,
                                                              address,
                                                              for_update=True)
                port = _create_port(userid, network, use_ipaddress=floating_ip)
            else:
                port = _create_port(userid, network, address=address)
291
292
            ports.append(port)
        else:
293
294
            raise faults.BadRequest("Network 'uuid' or 'port' attribute"
                                    " is required.")
295
296
297
298
299
300

    for index, port in enumerate(ports):
        associate_port_with_machine(port, vm)
        port.index = index
        port.save()
    return ports
301
302


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
@server_command("DESTROY")
def destroy(vm):
    log.info("Deleting VM %s", vm)
    return backend.delete_instance(vm)


@server_command("START")
def start(vm):
    log.info("Starting VM %s", vm)
    return backend.startup_instance(vm)


@server_command("STOP")
def stop(vm):
    log.info("Stopping VM %s", vm)
    return backend.shutdown_instance(vm)


@server_command("REBOOT")
def reboot(vm, reboot_type):
    if reboot_type not in ("SOFT", "HARD"):
        raise faults.BadRequest("Malformed request. Invalid reboot"
                                " type %s" % reboot_type)
    log.info("Rebooting VM %s. Type %s", vm, reboot_type)

    return backend.reboot_instance(vm, reboot_type.lower())


@server_command("RESIZE")
def resize(vm, flavor):
    old_flavor = vm.flavor
    # User requested the same flavor
    if old_flavor.id == flavor.id:
        raise faults.BadRequest("Server '%s' flavor is already '%s'."
                                % (vm, flavor))
        return None
    # Check that resize can be performed
    if old_flavor.disk != flavor.disk:
        raise faults.BadRequest("Can not resize instance disk.")
    if old_flavor.disk_template != flavor.disk_template:
        raise faults.BadRequest("Can not change instance disk template.")

    log.info("Resizing VM from flavor '%s' to '%s", old_flavor, flavor)
    commission_info = {"cyclades.cpu": flavor.cpu - old_flavor.cpu,
347
                       "cyclades.ram": 1048576 * (flavor.ram - old_flavor.ram)}
348
349
350
    # Save serial to VM, since it is needed by server_command decorator
    vm.serial = quotas.issue_commission(user=vm.userid,
                                        source=quotas.DEFAULT_SOURCE,
351
352
                                        provisions=commission_info,
                                        name="resource: %s. resize" % vm)
353
354
355
356
    return backend.resize_instance(vm, vcpus=flavor.cpu, memory=flavor.ram)


@server_command("SET_FIREWALL_PROFILE")
357
358
def set_firewall_profile(vm, profile, nic):
    log.info("Setting VM %s, NIC %s, firewall %s", vm, nic, profile)
359
360
361

    if profile not in [x[0] for x in NetworkInterface.FIREWALL_PROFILES]:
        raise faults.BadRequest("Unsupported firewall profile")
362
    backend.set_firewall_profile(vm, profile=profile, nic=nic)
363
364
365
366
    return None


@server_command("CONNECT")
367
368
def connect(vm, network, port=None):
    if port is None:
369
370
        port = _create_port(vm.userid, network)
    associate_port_with_machine(port, vm)
371

372
    log.info("Creating NIC %s with IPv4 Address %s", port, port.ipv4_address)
373

374
    return backend.connect_to_network(vm, port)
375
376
377


@server_command("DISCONNECT")
378
379
def disconnect(vm, nic):
    log.info("Removing NIC %s from VM %s", nic, vm)
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    return backend.disconnect_from_network(vm, nic)


def console(vm, console_type):
    """Arrange for an OOB console of the specified type

    This method arranges for an OOB console of the specified type.
    Only consoles of type "vnc" are supported for now.

    It uses a running instance of vncauthproxy to setup proper
    VNC forwarding with a random password, then returns the necessary
    VNC connection info to the caller.

    """
    log.info("Get console  VM %s, type %s", vm, console_type)

    # Use RAPI to get VNC console information for this instance
    if vm.operstate != "STARTED":
        raise faults.BadRequest('Server not in ACTIVE state.')

    if settings.TEST:
        console_data = {'kind': 'vnc', 'host': 'ganeti_node', 'port': 1000}
    else:
        console_data = backend.get_instance_console(vm)

    if console_data['kind'] != 'vnc':
        message = 'got console of kind %s, not "vnc"' % console_data['kind']
        raise faults.ServiceUnavailable(message)

    # Let vncauthproxy decide on the source port.
    # The alternative: static allocation, e.g.
    # sport = console_data['port'] - 1000
    sport = 0
    daddr = console_data['host']
    dport = console_data['port']
    password = util.random_password()

    if settings.TEST:
        fwd = {'source_port': 1234, 'status': 'OK'}
    else:
        fwd = request_vnc_forwarding(sport, daddr, dport, password)

    if fwd['status'] != "OK":
        raise faults.ServiceUnavailable('vncauthproxy returned error status')

    # Verify that the VNC server settings haven't changed
    if not settings.TEST:
        if console_data != backend.get_instance_console(vm):
            raise faults.ServiceUnavailable('VNC Server settings changed.')

    console = {
        'type': 'vnc',
        'host': getfqdn(),
        'port': fwd['source_port'],
        'password': password}

    return console
437
438


439
440
441
442
443
444
445
446
def rename(server, new_name):
    """Rename a VirtualMachine."""
    old_name = server.name
    server.name = new_name
    server.save()
    log.info("Renamed server '%s' from '%s' to '%s'", server, old_name,
             new_name)
    return server
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468


@transaction.commit_on_success
def create_port(*args, **kwargs):
    return _create_port(*args, **kwargs)


def _create_port(userid, network, machine=None, use_ipaddress=None,
                 address=None, name="", security_groups=None,
                 device_owner=None):
    """Create a new port on the specified network.

    Create a new Port(NetworkInterface model) on the specified Network. If
    'machine' is specified, the machine will be connected to the network using
    this port. If 'use_ipaddress' argument is specified, the port will be
    assigned this IPAddress. Otherwise, an IPv4 address from the IPv4 subnet
    will be allocated.

    """
    if network.state != "ACTIVE":
        raise faults.BuildInProgress("Can not create port while network is in"
                                     " state %s" % network.state)
469
470
471
    if network.action == "DESTROY":
        msg = "Can not create port. Network %s is being deleted."
        raise faults.Conflict(msg % network.id)
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    ipaddress = None
    if use_ipaddress is not None:
        # Use an existing IPAddress object.
        ipaddress = use_ipaddress
        if ipaddress and (ipaddress.network_id != network.id):
            msg = "IP Address %s does not belong to network %s"
            raise faults.Conflict(msg % (ipaddress.address, network.id))
    else:
        # If network has IPv4 subnets, try to allocate the address that the
        # the user specified or a random one.
        if network.subnets.filter(ipversion=4).exists():
            try:
                ipaddress = util.allocate_ip(network, userid=userid,
                                             address=address)
            except pools.ValueNotAvailable:
                msg = "Address %s is already in use." % address
                raise faults.Conflict(msg)
        elif address is not None:
            raise faults.BadRequest("Address %s is not a valid IP for the"
                                    " defined network subnets" % address)

    if ipaddress is not None and ipaddress.nic is not None:
        raise faults.Conflict("IP address '%s' is already in use" %
                              ipaddress.address)

    port = NetworkInterface.objects.create(network=network,
                                           state="DOWN",
                                           userid=userid,
                                           device_owner=None,
                                           name=name)

    # add the security groups if any
    if security_groups:
        port.security_groups.add(*security_groups)

    if ipaddress is not None:
        # Associate IPAddress with the Port
        ipaddress.nic = port
        ipaddress.save()

    if machine is not None:
513
        # Connect port to the instance.
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
        machine = connect(machine, network, port)
        jobID = machine.task_job_id
        log.info("Created Port %s with IP %s. Ganeti Job: %s",
                 port, ipaddress, jobID)
    else:
        log.info("Created Port %s with IP %s not attached to any instance",
                 port, ipaddress)

    return port


def associate_port_with_machine(port, machine):
    """Associate a Port with a VirtualMachine.

    Associate the port with the VirtualMachine and add an entry to the
    IPAddressLog if the port has a public IPv4 address from a public network.

    """
532
533
    if port.machine is not None:
        raise faults.Conflict("Port %s is already in use." % port.id)
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
    if port.network.public:
        ipv4_address = port.ipv4_address
        if ipv4_address is not None:
            ip_log = IPAddressLog.objects.create(server_id=machine.id,
                                                 network_id=port.network_id,
                                                 address=ipv4_address,
                                                 active=True)
            log.debug("Created IP log entry %s", ip_log)
    port.machine = machine
    port.state = "BUILD"
    port.device_owner = "vm"
    port.save()
    return port


@transaction.commit_on_success
def delete_port(port):
    """Delete a port by removing the NIC card from the instance.

    Send a Job to remove the NIC card from the instance. The port
    will be deleted and the associated IPv4 addressess will be released
    when the job completes successfully.

    """

    if port.machine is not None:
        vm = disconnect(port.machine, port)
        log.info("Removing port %s, Job: %s", port, vm.task_job_id)
    else:
        backend.remove_nic_ips(port)
        port.delete()
        log.info("Removed port %s", port)

    return port