Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (18 kB)

1
import logging
2

    
3
from socket import getfqdn
4
from functools import wraps
5
from django import dispatch
6
from django.db import transaction
7
from django.utils import simplejson as json
8

    
9
from snf_django.lib.api import faults
10
from django.conf import settings
11
from synnefo import quotas
12
from synnefo.api import util
13
from synnefo.logic import backend
14
from synnefo.logic.backend_allocator import BackendAllocator
15
from synnefo.db.models import (NetworkInterface, VirtualMachine,
16
                               VirtualMachineMetadata, IPAddress)
17
from synnefo.db import query as db_query, pools
18

    
19
from vncauthproxy.client import request_forwarding as request_vnc_forwarding
20

    
21
log = logging.getLogger(__name__)
22

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

    
26

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

    
31
    # Destroyin a server should always be permitted
32
    if action == "DESTROY":
33
        return
34

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

    
43
    # Check if action can be performed to VM's operstate
44
    operstate = vm.operstate
45
    if operstate == "BUILD" and action != "BUILD":
46
        raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
47
    elif (action == "START" and operstate != "STOPPED") or\
48
         (action == "STOP" and operstate != "STARTED") or\
49
         (action == "RESIZE" and operstate != "STOPPED") or\
50
         (action in ["CONNECT", "DISCONNECT"] and operstate != "STOPPED"
51
          and not settings.GANETI_USE_HOTPLUG):
52
        raise faults.BadRequest("Can not perform '%s' action while server is"
53
                                " in '%s' state." % (action, operstate))
54
    return
55

    
56

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

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

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

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

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

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

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

    
120
            log.info("user: %s, vm: %s, action: %s, job_id: %s, serial: %s",
121
                     user_id, vm.id, action, job_id, vm.serial)
122

    
123
            # store the new task in the VM
124
            if job_id is not None:
125
                vm.task = action
126
                vm.task_job_id = job_id
127
            vm.save()
128

    
129
            return vm
130
        return wrapper
131
    return decorator
132

    
133

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

    
142
    if private_networks is None:
143
        private_networks = []
144
    if floating_ips is None:
145
        floating_ips = []
146

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

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

    
169
    nics = create_instance_nics(vm, userid, private_networks, floating_ips)
170

    
171
    for key, val in metadata.items():
172
        VirtualMachineMetadata.objects.create(
173
            meta_key=key,
174
            meta_value=val,
175
            vm=vm)
176

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

    
180
    return vm
181

    
182

    
183
@transaction.commit_on_success
184
def allocate_new_server(userid, flavor):
185
    """Allocate a new server to a Ganeti backend.
186

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

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

194
    """
195
    backend_allocator = BackendAllocator()
196
    use_backend = backend_allocator.allocate(userid, flavor)
197
    if use_backend is None:
198
        log.error("No available backend for VM with flavor %s", flavor)
199
        raise faults.ServiceUnavailable("No available backends")
200
    return use_backend
201

    
202

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

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

    
232
    return jobID
233

    
234

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

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

243
    """
244
    nics = []
245
    for network_id in settings.DEFAULT_INSTANCE_NETWORKS:
246
        if network_id == "SNF:ANY_PUBLIC":
247
            ipaddress = util.allocate_public_ip(userid=userid,
248
                                                backend=vm.backend)
249
            nic, ipaddress = create_nic(vm, ipaddress=ipaddress)
250
        else:
251
            try:
252
                network = util.get_network(network_id, userid,
253
                                           non_deleted=True)
254
            except faults.ItemNotFound:
255
                msg = "Invalid configuration. Setting"\
256
                      " 'DEFAULT_INSTANCE_NETWORKS' contains invalid"\
257
                      " network '%s'" % network_id
258
                log.error(msg)
259
                raise faults.InternalServerError(msg)
260
            nic, ipaddress = create_nic(vm, network=network)
261
        nics.append(nic)
262
    for address in floating_ips:
263
        floating_ip = util.get_floating_ip_by_address(vm.userid, address,
264
                                                      for_update=True)
265
        nic, ipaddress = create_nic(vm, ipaddress=floating_ip)
266
        nics.append(nic)
267
    for network_id in private_networks:
268
        network = util.get_network(network_id, userid, non_deleted=True)
269
        if network.public:
270
            raise faults.Forbidden("Can not connect to public network")
271
        nic, ipaddress = create_nic(vm, network=network)
272
        nics.append(nic)
273
    for index, nic in enumerate(nics):
274
        nic.index = index
275
        nic.save()
276
    return nics
277

    
278

    
279
def create_nic(vm, network=None, ipaddress=None, address=None):
280
    """Helper functions for create NIC objects.
281

282
    Create a NetworkInterface connecting a VirtualMachine to a network with the
283
    IPAddress specified. If no 'ipaddress' is passed and the network has an
284
    IPv4 subnet, then an IPv4 address will be automatically be allocated.
285

286
    """
287
    userid = vm.userid
288

    
289
    if ipaddress is None:
290
        if network.subnets.filter(ipversion=4).exists():
291
            try:
292
                ipaddress = util.allocate_ip(network, userid=userid,
293
                                             address=address)
294
            except pools.ValueNotAvailable:
295
                raise faults.badRequest("Address '%s' is not available." %
296
                                        address)
297

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

    
302
    if network is None:
303
        network = ipaddress.network
304
    elif network.state != 'ACTIVE':
305
        # TODO: What if is in settings ?
306
        raise faults.BuildInProgress('Network not active yet')
307

    
308
    #device_owner = "router" if vm.router else "vm"
309
    device_owner = "vm"
310
    nic = NetworkInterface.objects.create(machine=vm, network=network,
311
                                          state="BUILD",
312
                                          device_owner=device_owner)
313
    if ipaddress is not None:
314
        ipaddress.nic = nic
315
        ipaddress.save()
316

    
317
    return nic, ipaddress
318

    
319

    
320
@server_command("DESTROY")
321
def destroy(vm):
322
    log.info("Deleting VM %s", vm)
323
    return backend.delete_instance(vm)
324

    
325

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

    
331

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

    
337

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

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

    
347

    
348
@server_command("RESIZE")
349
def resize(vm, flavor):
350
    old_flavor = vm.flavor
351
    # User requested the same flavor
352
    if old_flavor.id == flavor.id:
353
        raise faults.BadRequest("Server '%s' flavor is already '%s'."
354
                                % (vm, flavor))
355
        return None
356
    # Check that resize can be performed
357
    if old_flavor.disk != flavor.disk:
358
        raise faults.BadRequest("Can not resize instance disk.")
359
    if old_flavor.disk_template != flavor.disk_template:
360
        raise faults.BadRequest("Can not change instance disk template.")
361

    
362
    log.info("Resizing VM from flavor '%s' to '%s", old_flavor, flavor)
363
    commission_info = {"cyclades.cpu": flavor.cpu - old_flavor.cpu,
364
                       "cyclades.ram": 1048576 * (flavor.ram - old_flavor.ram)}
365
    # Save serial to VM, since it is needed by server_command decorator
366
    vm.serial = quotas.issue_commission(user=vm.userid,
367
                                        source=quotas.DEFAULT_SOURCE,
368
                                        provisions=commission_info,
369
                                        name="resource: %s. resize" % vm)
370
    return backend.resize_instance(vm, vcpus=flavor.cpu, memory=flavor.ram)
371

    
372

    
373
@server_command("SET_FIREWALL_PROFILE")
374
def set_firewall_profile(vm, profile, nic):
375
    log.info("Setting VM %s, NIC %s, firewall %s", vm, nic, profile)
376

    
377
    if profile not in [x[0] for x in NetworkInterface.FIREWALL_PROFILES]:
378
        raise faults.BadRequest("Unsupported firewall profile")
379
    backend.set_firewall_profile(vm, profile=profile, nic=nic)
380
    return None
381

    
382

    
383
@server_command("CONNECT")
384
def connect(vm, network):
385
    nic, ipaddress = create_nic(vm, network)
386

    
387
    log.info("Creating NIC %s with IPAddress %s", nic, ipaddress)
388

    
389
    return backend.connect_to_network(vm, nic)
390

    
391

    
392
@server_command("DISCONNECT")
393
def disconnect(vm, nic):
394
    log.info("Removing NIC %s from VM %s", nic, vm)
395
    return backend.disconnect_from_network(vm, nic)
396

    
397

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

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

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

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

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

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

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

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

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

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

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

    
445
    console = {
446
        'type': 'vnc',
447
        'host': getfqdn(),
448
        'port': fwd['source_port'],
449
        'password': password}
450

    
451
    return console
452

    
453

    
454
@server_command("CONNECT")
455
def add_floating_ip(vm, address):
456
    # Use for_update, to guarantee that floating IP will only by assigned once
457
    # and that it can not be released will it is being attached!
458
    floating_ip = util.get_floating_ip_by_address(vm.userid, address,
459
                                                  for_update=True)
460
    nic, floating_ip = create_nic(vm, ipaddress=floating_ip)
461
    log.info("Created NIC %s with floating IP %s", nic, floating_ip)
462
    return backend.connect_to_network(vm, nic)
463

    
464

    
465
@server_command("DISCONNECT")
466
def remove_floating_ip(vm, address):
467
    try:
468
        floating_ip = db_query.get_server_floating_ip(server=vm,
469
                                                      address=address,
470
                                                      for_update=True)
471
    except IPAddress.DoesNotExist:
472
        raise faults.BadRequest("Server '%s' has no floating ip with"
473
                                " address '%s'" % (vm, address))
474

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

    
479
    return backend.disconnect_from_network(vm, nic)
480

    
481

    
482
def rename(server, new_name):
483
    """Rename a VirtualMachine."""
484
    old_name = server.name
485
    server.name = new_name
486
    server.save()
487
    log.info("Renamed server '%s' from '%s' to '%s'", server, old_name,
488
             new_name)
489
    return server