Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.9 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import json
35

    
36
from logging import getLogger
37

    
38
from django.conf import settings
39
from django.db import transaction
40

    
41
from synnefo.db.models import (VirtualMachine, Network, NetworkLink)
42
from synnefo.logic import utils
43
from synnefo.util.rapi import GanetiRapiClient
44

    
45

    
46
log = getLogger('synnefo.logic')
47

    
48
rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO)
49

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

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

    
57

    
58
@transaction.commit_on_success
59
def process_op_status(vm, jobid, opcode, status, logmsg):
60
    """Process a job progress notification from the backend
61

62
    Process an incoming message from the backend (currently Ganeti).
63
    Job notifications with a terminating status (sucess, error, or canceled),
64
    also update the operating state of the VM.
65

66
    """
67
    # See #1492, #1031, #1111 why this line has been removed
68
    #if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
69
    if status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]:
70
        raise VirtualMachine.InvalidBackendMsgError(opcode, status)
71

    
72
    vm.backendjobid = jobid
73
    vm.backendjobstatus = status
74
    vm.backendopcode = opcode
75
    vm.backendlogmsg = logmsg
76

    
77
    # Notifications of success change the operating state
78
    state_for_success = VirtualMachine.OPER_STATE_FROM_OPCODE.get(opcode, None)
79
    if status == 'success' and state_for_success is not None:
80
        utils.update_state(vm, state_for_success)
81
        # Set the deleted flag explicitly, cater for admin-initiated removals
82
        if opcode == 'OP_INSTANCE_REMOVE':
83
            vm.deleted = True
84

    
85
    # Special case: if OP_INSTANCE_CREATE fails --> ERROR
86
    if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
87
        utils.update_state(vm, 'ERROR')
88

    
89
    # Special case: OP_INSTANCE_REMOVE fails for machines in ERROR,
90
    # when no instance exists at the Ganeti backend.
91
    # See ticket #799 for all the details.
92
    #
93
    if (status == 'error' and opcode == 'OP_INSTANCE_REMOVE' and
94
        vm.operstate == 'ERROR'):
95
        vm.deleted = True
96

    
97
    # Any other notification of failure leaves the operating state unchanged
98

    
99
    vm.save()
100

    
101

    
102
@transaction.commit_on_success
103
def process_net_status(vm, nics):
104
    """Process a net status notification from the backend
105

106
    Process an incoming message from the Ganeti backend,
107
    detailing the NIC configuration of a VM instance.
108

109
    Update the state of the VM in the DB accordingly.
110
    """
111

    
112
    vm.nics.all().delete()
113
    for i, nic in enumerate(nics):
114
        if i == 0:
115
            net = Network.objects.get(public=True)
116
        else:
117
            try:
118
                link = NetworkLink.objects.get(name=nic['link'])
119
            except NetworkLink.DoesNotExist:
120
                # Cannot find an instance of NetworkLink for
121
                # the link attribute specified in the notification
122
                raise NetworkLink.DoesNotExist("Cannot find a NetworkLink "
123
                    "object for link='%s'" % nic['link'])
124
            net = link.network
125
            if net is None:
126
                raise Network.DoesNotExist("NetworkLink for link='%s' not "
127
                    "associated with an existing Network instance." %
128
                    nic['link'])
129

    
130
        firewall = nic.get('firewall', '')
131
        firewall_profile = _reverse_tags.get(firewall, '')
132
        if not firewall_profile and net.public:
133
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE
134

    
135
        vm.nics.create(
136
            network=net,
137
            index=i,
138
            mac=nic.get('mac', ''),
139
            ipv4=nic.get('ip', ''),
140
            ipv6=nic.get('ipv6', ''),
141
            firewall_profile=firewall_profile)
142

    
143
        # network nics modified, update network object
144
        net.save()
145

    
146
    vm.save()
147

    
148

    
149
@transaction.commit_on_success
150
def process_create_progress(vm, rprogress, wprogress):
151

    
152
    # XXX: This only uses the read progress for now.
153
    #      Explore whether it would make sense to use the value of wprogress
154
    #      somewhere.
155
    percentage = int(rprogress)
156

    
157
    # The percentage may exceed 100%, due to the way
158
    # snf-progress-monitor tracks bytes read by image handling processes
159
    percentage = 100 if percentage > 100 else percentage
160
    if percentage < 0:
161
        raise ValueError("Percentage cannot be negative")
162

    
163
    last_update = vm.buildpercentage
164

    
165
    # FIXME: log a warning here, see #1033
166
#   if last_update > percentage:
167
#       raise ValueError("Build percentage should increase monotonically " \
168
#                        "(old = %d, new = %d)" % (last_update, percentage))
169

    
170
    # This assumes that no message of type 'ganeti-create-progress' is going to
171
    # arrive once OP_INSTANCE_CREATE has succeeded for a Ganeti instance and
172
    # the instance is STARTED.  What if the two messages are processed by two
173
    # separate dispatcher threads, and the 'ganeti-op-status' message for
174
    # successful creation gets processed before the 'ganeti-create-progress'
175
    # message? [vkoukis]
176
    #
177
    #if not vm.operstate == 'BUILD':
178
    #    raise VirtualMachine.IllegalState("VM is not in building state")
179

    
180
    vm.buildpercentage = percentage
181
    vm.save()
182

    
183

    
184
def start_action(vm, action):
185
    """Update the state of a VM when a new action is initiated."""
186
    if not action in [x[0] for x in VirtualMachine.ACTIONS]:
187
        raise VirtualMachine.InvalidActionError(action)
188

    
189
    # No actions to deleted and no actions beside destroy to suspended VMs
190
    if vm.deleted:
191
        raise VirtualMachine.DeletedError
192

    
193
    # No actions to machines being built. They may be destroyed, however.
194
    if vm.operstate == 'BUILD' and action != 'DESTROY':
195
        raise VirtualMachine.BuildingError
196

    
197
    vm.action = action
198
    vm.backendjobid = None
199
    vm.backendopcode = None
200
    vm.backendjobstatus = None
201
    vm.backendlogmsg = None
202

    
203
    # Update the relevant flags if the VM is being suspended or destroyed.
204
    # Do not set the deleted flag here, see ticket #721.
205
    #
206
    # The deleted flag is set asynchronously, when an OP_INSTANCE_REMOVE
207
    # completes successfully. Hence, a server may be visible for some time
208
    # after a DELETE /servers/id returns HTTP 204.
209
    #
210
    if action == "DESTROY":
211
        # vm.deleted = True
212
        pass
213
    elif action == "SUSPEND":
214
        vm.suspended = True
215
    elif action == "START":
216
        vm.suspended = False
217
    vm.save()
218

    
219

    
220
def create_instance(vm, flavor, image, password, personality):
221
    """`image` is a dictionary which should contain the keys:
222
            'backend_id', 'format' and 'metadata'
223

224
        metadata value should be a dictionary.
225
    """
226
    nic = {'ip': 'pool', 'network': settings.GANETI_PUBLIC_NETWORK}
227

    
228
    if settings.IGNORE_FLAVOR_DISK_SIZES:
229
        if image['backend_id'].find("windows") >= 0:
230
            sz = 14000
231
        else:
232
            sz = 4000
233
    else:
234
        sz = flavor.disk * 1024
235

    
236
    # Handle arguments to CreateInstance() as a dictionary,
237
    # initialize it based on a deployment-specific value.
238
    # This enables the administrator to override deployment-specific
239
    # arguments, such as the disk template to use, name of os provider
240
    # and hypervisor-specific parameters at will (see Synnefo #785, #835).
241
    #
242
    kw = settings.GANETI_CREATEINSTANCE_KWARGS
243
    kw['mode'] = 'create'
244
    kw['name'] = vm.backend_id
245
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
246
    kw['disk_template'] = flavor.disk_template
247
    kw['disks'] = [{"size": sz}]
248
    kw['nics'] = [nic]
249
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
250
    # kw['os'] = settings.GANETI_OS_PROVIDER
251
    kw['ip_check'] = False
252
    kw['name_check'] = False
253
    # Do not specific a node explicitly, have
254
    # Ganeti use an iallocator instead
255
    #
256
    # kw['pnode']=rapi.GetNodes()[0]
257
    kw['dry_run'] = settings.TEST
258

    
259
    kw['beparams'] = {
260
        'auto_balance': True,
261
        'vcpus': flavor.cpu,
262
        'memory': flavor.ram}
263

    
264
    kw['osparams'] = {
265
        'img_id': image['backend_id'],
266
        'img_passwd': password,
267
        'img_format': image['format']}
268
    if personality:
269
        kw['osparams']['img_personality'] = json.dumps(personality)
270

    
271
    kw['osparams']['img_properties'] = json.dumps(image['metadata'])
272

    
273
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
274
    # kw['hvparams'] = dict(serial_console=False)
275

    
276
    return rapi.CreateInstance(**kw)
277

    
278

    
279
def delete_instance(vm):
280
    start_action(vm, 'DESTROY')
281
    rapi.DeleteInstance(vm.backend_id, dry_run=settings.TEST)
282
    vm.nics.all().delete()
283

    
284

    
285
def reboot_instance(vm, reboot_type):
286
    assert reboot_type in ('soft', 'hard')
287
    rapi.RebootInstance(vm.backend_id, reboot_type, dry_run=settings.TEST)
288
    log.info('Rebooting instance %s', vm.backend_id)
289

    
290

    
291
def startup_instance(vm):
292
    start_action(vm, 'START')
293
    rapi.StartupInstance(vm.backend_id, dry_run=settings.TEST)
294

    
295

    
296
def shutdown_instance(vm):
297
    start_action(vm, 'STOP')
298
    rapi.ShutdownInstance(vm.backend_id, dry_run=settings.TEST)
299

    
300

    
301
def get_instance_console(vm):
302
    # RAPI GetInstanceConsole() returns endpoints to the vnc_bind_address,
303
    # which is a cluster-wide setting, either 0.0.0.0 or 127.0.0.1, and pretty
304
    # useless (see #783).
305
    #
306
    # Until this is fixed on the Ganeti side, construct a console info reply
307
    # directly.
308
    #
309
    # WARNING: This assumes that VNC runs on port network_port on
310
    #          the instance's primary node, and is probably
311
    #          hypervisor-specific.
312
    #
313
    console = {}
314
    console['kind'] = 'vnc'
315
    i = rapi.GetInstance(vm.backend_id)
316
    if i['hvparams']['serial_console']:
317
        raise Exception("hv parameter serial_console cannot be true")
318
    console['host'] = i['pnode']
319
    console['port'] = i['network_port']
320

    
321
    return console
322
    # return rapi.GetInstanceConsole(vm.backend_id)
323

    
324

    
325
def request_status_update(vm):
326
    return rapi.GetInstanceInfo(vm.backend_id)
327

    
328

    
329
def get_job_status(jobid):
330
    return rapi.GetJobStatus(jobid)
331

    
332

    
333
def update_status(vm, status):
334
    utils.update_state(vm, status)
335

    
336

    
337
def create_network_link():
338
    try:
339
        last = NetworkLink.objects.order_by('-index')[0]
340
        index = last.index + 1
341
    except IndexError:
342
        index = 1
343

    
344
    if index <= settings.GANETI_MAX_LINK_NUMBER:
345
        name = '%s%d' % (settings.GANETI_LINK_PREFIX, index)
346
        return NetworkLink.objects.create(index=index, name=name,
347
                                            available=True)
348
    return None     # All link slots are filled
349

    
350

    
351
@transaction.commit_on_success
352
def create_network(name, user_id):
353
    try:
354
        link = NetworkLink.objects.filter(available=True)[0]
355
    except IndexError:
356
        link = create_network_link()
357
        if not link:
358
            return None
359

    
360
    network = Network.objects.create(
361
        name=name,
362
        userid=user_id,
363
        state='ACTIVE',
364
        link=link)
365

    
366
    link.network = network
367
    link.available = False
368
    link.save()
369

    
370
    return network
371

    
372

    
373
@transaction.commit_on_success
374
def delete_network(net):
375
    link = net.link
376
    if link.name != settings.GANETI_NULL_LINK:
377
        link.available = True
378
        link.network = None
379
        link.save()
380

    
381
    for vm in net.machines.all():
382
        disconnect_from_network(vm, net)
383
        vm.save()
384
    net.state = 'DELETED'
385
    net.save()
386

    
387

    
388
def connect_to_network(vm, net):
389
    nic = {'mode': 'bridged', 'link': net.link.name}
390
    rapi.ModifyInstance(vm.backend_id, nics=[('add', nic)],
391
                        dry_run=settings.TEST)
392

    
393

    
394
def disconnect_from_network(vm, net):
395
    nics = vm.nics.filter(network__public=False).order_by('index')
396
    ops = [('remove', nic.index, {}) for nic in nics if nic.network == net]
397
    if not ops: # Vm not connected to network
398
        return
399
    rapi.ModifyInstance(vm.backend_id, nics=ops[::-1], dry_run=settings.TEST)
400

    
401

    
402
def set_firewall_profile(vm, profile):
403
    try:
404
        tag = _firewall_tags[profile]
405
    except KeyError:
406
        raise ValueError("Unsopported Firewall Profile: %s" % profile)
407

    
408
    # Delete all firewall tags
409
    for t in _firewall_tags.values():
410
        rapi.DeleteInstanceTags(vm.backend_id, [t], dry_run=settings.TEST)
411

    
412
    rapi.AddInstanceTags(vm.backend_id, [tag], dry_run=settings.TEST)
413

    
414
    # XXX NOP ModifyInstance call to force process_net_status to run
415
    # on the dispatcher
416
    rapi.ModifyInstance(vm.backend_id,
417
                        os_name=settings.GANETI_CREATEINSTANCE_KWARGS['os'])
418

    
419

    
420
def get_ganeti_instances():
421
    return rapi.GetInstances()
422

    
423

    
424
def get_ganeti_nodes():
425
    return rapi.GetNodes()
426

    
427

    
428
def get_ganeti_jobs():
429
    return rapi.GetJobs()