servers.py 18.7 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
15
from synnefo import quotas
from synnefo.api import util
from synnefo.logic import backend
from synnefo.logic.backend_allocator import BackendAllocator
from synnefo.logic.rapi import GanetiApiError
16
from synnefo.db.models import (NetworkInterface, VirtualMachine, Network,
17
                               VirtualMachineMetadata, FloatingIP)
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
44
45
46

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
    if operstate == "BUILD":
        raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
47
48
49
50
51
    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):
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
81
82
83
84
85
86
87
88
89
90
91
92
93
        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)

            # Resolve(reject) previous serial if it is still pending!!
            previous_serial = vm.serial
            if previous_serial and not previous_serial.resolved:
                quotas.resolve_vm_commission(serial=previous_serial)

            # Check if action is quotable and issue the corresponding
            # commission
            serial = None
            commission_info = quotas.get_commission_info(vm, action=action)
            if commission_info is not None:
                # Issue new commission, associate it with the VM
94
                commission_name = "client: api, resource %s" % vm
95
96
97
                serial = quotas.issue_commission(user=user_id,
                                                 source=quotas.DEFAULT_SOURCE,
                                                 provisions=commission_info,
98
                                                 name=commission_name,
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                                                 force=False,
                                                 auto_accept=False)
            vm.serial = serial

            # 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

            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


@transaction.commit_manually
def create(userid, name, password, flavor, image, metadata={},
132
133
           personality=[], private_networks=None, floating_ips=None,
           use_backend=None):
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    if use_backend is None:
        # Allocate backend to host the server. Commit after allocation to
        # release the locks hold by the backend allocator.
        try:
            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")
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

149
150
151
152
153
    if private_networks is None:
        private_networks = []
    if floating_ips is None:
        floating_ips = []

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    # 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

    try:
        # 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,
            action="CREATE")

        log.info("Created entry in DB for VM '%s'", vm)

        # dispatch server created signal
        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']),
        })

188
        nics = create_instance_nics(vm, userid, private_networks, floating_ips)
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        # Also we must create the VM metadata in the same transaction.
        for key, val in metadata.items():
            VirtualMachineMetadata.objects.create(
                meta_key=key,
                meta_value=val,
                vm=vm)
        # Issue commission to Quotaholder and accept it since at the end of
        # this transaction the VirtualMachine object will be created in the DB.
        # Note: the following call does a commit!
        quotas.issue_and_accept_commission(vm)
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    try:
207
        jobID = backend.create_instance(vm, nics, flavor, image)
208
209
210
211
212
213
        # At this point the job is enqueued in the Ganeti backend
        vm.backendjobid = jobID
        vm.task = "BUILD"
        vm.task_job_id = jobID
        vm.save()
        transaction.commit()
214
        log.info("User %s created VM %s, NICs %s, Backend %s, JobID %s",
215
                 userid, vm, nics, backend, str(jobID))
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    except GanetiApiError as e:
        log.exception("Can not communicate to backend %s: %s.",
                      backend, e)
        # Failed while enqueuing OP_INSTANCE_CREATE to backend. Restore
        # already reserved quotas by issuing a negative commission
        vm.operstate = "ERROR"
        vm.backendlogmsg = "Can not communicate to backend."
        vm.deleted = True
        vm.save()
        quotas.issue_and_accept_commission(vm, delete=True)
        raise
    except:
        transaction.rollback()
        raise

    return vm


234
def create_instance_nics(vm, userid, private_networks=[], floating_ips=[]):
235
236
237
238
239
240
241
242
243
244
245
    """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
    network policy (defined by administration settings) and the private
    networks defined by the user.

    """
    attachments = []
    for network_id in settings.DEFAULT_INSTANCE_NETWORKS:
        network, address = None, None
246
        if network_id == "SNF:ANY_PUBLIC":
247
            network, address = util.allocate_public_address(backend=vm.backend)
248
249
250
251
252
253
254
255
256
        else:
            try:
                network = Network.objects.get(id=network_id, deleted=False)
            except Network.DoesNotExist:
                msg = "Invalid configuration. Setting"\
                      " 'DEFAULT_INSTANCE_NETWORKS' contains invalid"\
                      " network '%s'" % network_id
                log.error(msg)
                raise Exception(msg)
257
            if network.subnet is not None and network.dhcp:
258
259
                address = util.get_network_free_address(network)
        attachments.append((network, address))
260
    for address in floating_ips:
261
        floating_ip = add_floating_ip_to_vm(vm=vm, address=address)
262
263
        network = floating_ip.network
        attachments.append((network, address))
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    for network_id in private_networks:
        network, address = None, None
        network = util.get_network(network_id, userid, non_deleted=True)
        if network.public:
            raise faults.Forbidden("Can not connect to public network")
        if network.dhcp:
            address = util.get_network_free_address(network)
        attachments.append((network, address))

    nics = []
    for index, (network, address) in enumerate(attachments):
        # Create VM's public NIC. Do not wait notification form ganeti
        # hooks to create this NIC, because if the hooks never run (e.g.
        # building error) the VM's public IP address will never be
        # released!
        nic = NetworkInterface.objects.create(machine=vm, network=network,
                                              index=index, ipv4=address,
                                              state="BUILDING")
        nics.append(nic)
    return nics


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
@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,
330
                       "cyclades.ram": 1048576 * (flavor.ram - old_flavor.ram)}
331
332
333
    # Save serial to VM, since it is needed by server_command decorator
    vm.serial = quotas.issue_commission(user=vm.userid,
                                        source=quotas.DEFAULT_SOURCE,
334
335
                                        provisions=commission_info,
                                        name="resource: %s. resize" % vm)
336
337
338
339
    return backend.resize_instance(vm, vcpus=flavor.cpu, memory=flavor.ram)


@server_command("SET_FIREWALL_PROFILE")
340
341
def set_firewall_profile(vm, profile, index=0):
    log.info("Setting VM %s, NIC index %s, firewall %s", vm, index, profile)
342
343
344

    if profile not in [x[0] for x in NetworkInterface.FIREWALL_PROFILES]:
        raise faults.BadRequest("Unsupported firewall profile")
345
    backend.set_firewall_profile(vm, profile=profile, index=index)
346
347
348
349
350
351
352
353
354
    return None


@server_command("CONNECT")
def connect(vm, network):
    if network.state != 'ACTIVE':
        raise faults.BuildInProgress('Network not active yet')

    address = None
355
    if network.subnet is not None and network.dhcp:
356
        # Get a free IP from the address pool.
357
        address = util.get_network_free_address(network)
358
359
360

    log.info("Connecting VM %s to Network %s(%s)", vm, network, address)

361
362
363
364
365
366
    nic = NetworkInterface.objects.create(machine=vm,
                                          network=network,
                                          ipv4=address,
                                          state="BUILDING")

    return backend.connect_to_network(vm, nic)
367
368
369
370
371
372
373
374
375
376
377
378
379
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


@server_command("DISCONNECT")
def disconnect(vm, nic_index):
    nic = util.get_nic_from_index(vm, nic_index)

    log.info("Removing NIC %s from VM %s", str(nic.index), vm)

    if nic.dirty:
        raise faults.BuildInProgress('Machine is busy.')
    else:
        vm.nics.all().update(dirty=True)

    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
@server_command("CONNECT")
440
def add_floating_ip(vm, address):
441
    floating_ip = add_floating_ip_to_vm(vm, address)
442
443
444
445
446
447
448
    nic = NetworkInterface.objects.create(machine=vm,
                                          network=floating_ip.network,
                                          ipv4=floating_ip.ipv4,
                                          state="BUILDING")
    log.info("Connecting VM %s to floating IP %s. NIC: %s", vm, floating_ip,
             nic)
    return backend.connect_to_network(vm, nic)
449
450
451
452
453
454
455
456
457
458
459


def add_floating_ip_to_vm(vm, address):
    """Get a floating IP by it's address and add it to VirtualMachine.

    Helper function for looking up a FloatingIP by it's address and associating
    it with a VirtualMachine object (without adding the NIC in the Ganeti
    backend!). This function also checks if the floating IP is currently used
    by any instance and if it is available in the Backend that hosts the VM.

    """
460
461
    user_id = vm.userid
    try:
462
463
        # Get lock in VM, to guarantee that floating IP will only by assigned
        # once
464
465
466
467
468
469
470
471
472
473
        floating_ip = FloatingIP.objects.select_for_update()\
                                        .get(userid=user_id, ipv4=address,
                                             deleted=False)
    except FloatingIP.DoesNotExist:
        raise faults.ItemNotFound("Floating IP '%s' does not exist" % address)

    if floating_ip.in_use():
        raise faults.Conflict("Floating IP '%s' already in use" %
                              floating_ip.id)

474
475
476
477
478
479
    bnet = floating_ip.network.backend_networks.filter(backend=vm.backend_id)
    if not bnet.exists():
        msg = "Network '%s' is a floating IP pool, but it not connected"\
              " to backend '%s'" % (floating_ip.network, vm.backend)
        raise faults.ServiceUnavailable(msg)

480
481
    floating_ip.machine = vm
    floating_ip.save()
482
    return floating_ip
483
484


485
@server_command("DISCONNECT")
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
def remove_floating_ip(vm, address):
    user_id = vm.userid
    try:
        floating_ip = FloatingIP.objects.select_for_update()\
                                        .get(userid=user_id, ipv4=address,
                                             deleted=False, machine=vm)
    except FloatingIP.DoesNotExist:
        raise faults.ItemNotFound("Floating IP '%s' does not exist" % address)

    try:
        nic = NetworkInterface.objects.get(machine=vm, ipv4=address)
    except NetworkInterface.DoesNotExist:
        raise faults.ItemNotFound("Floating IP '%s' is not attached to"
                                  "VM '%s'" % (floating_ip, vm))

    log.info("Removing NIC %s from VM %s. Floating IP '%s'", str(nic.index),
             vm, floating_ip)

    return backend.disconnect_from_network(vm, nic)