Revision f5b4f2a3 snf-cyclades-app/synnefo/logic/backend.py

b/snf-cyclades-app/synnefo/logic/backend.py
34 34
import json
35 35

  
36 36
from logging import getLogger
37

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

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

  
45 44

  
45

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

  
48
rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO)
49 48

  
50 49
_firewall_tags = {
51 50
    'ENABLED': settings.GANETI_FIREWALL_ENABLED_TAG,
......
282 281
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
283 282
    # kw['hvparams'] = dict(serial_console=False)
284 283

  
285
    return rapi.CreateInstance(**kw)
284
    return vm.client.CreateInstance(**kw)
286 285

  
287 286

  
288 287
def delete_instance(vm):
289 288
    start_action(vm, 'DESTROY')
290
    rapi.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)
289
    vm.client.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)
291 290

  
292 291

  
293 292
def reboot_instance(vm, reboot_type):
294 293
    assert reboot_type in ('soft', 'hard')
295
    rapi.RebootInstance(vm.backend_vm_id, reboot_type, dry_run=settings.TEST)
294
    vm.client.RebootInstance(vm.backend_vm_id, reboot_type, dry_run=settings.TEST)
296 295
    log.info('Rebooting instance %s', vm.backend_vm_id)
297 296

  
298 297

  
299 298
def startup_instance(vm):
300 299
    start_action(vm, 'START')
301
    rapi.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST)
300
    vm.client.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST)
302 301

  
303 302

  
304 303
def shutdown_instance(vm):
305 304
    start_action(vm, 'STOP')
306
    rapi.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST)
305
    vm.client.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST)
307 306

  
308 307

  
309 308
def get_instance_console(vm):
......
320 319
    #
321 320
    console = {}
322 321
    console['kind'] = 'vnc'
323
    i = rapi.GetInstance(vm.backend_vm_id)
322
    i = vm.client.GetInstance(vm.backend_vm_id)
324 323
    if i['hvparams']['serial_console']:
325 324
        raise Exception("hv parameter serial_console cannot be true")
326 325
    console['host'] = i['pnode']
......
331 330

  
332 331

  
333 332
def request_status_update(vm):
334
    return rapi.GetInstanceInfo(vm.backend_vm_id)
335

  
336

  
337
def get_job_status(jobid):
338
    return rapi.GetJobStatus(jobid)
333
    return vm.client.GetInstanceInfo(vm.backend_vm_id)
339 334

  
340 335

  
341 336
def update_status(vm, status):
......
395 390

  
396 391
def connect_to_network(vm, net):
397 392
    nic = {'mode': 'bridged', 'link': net.link.name}
398
    rapi.ModifyInstance(vm.backend_vm_id, nics=[('add', -1, nic)],
393
    vm.client.ModifyInstance(vm.backend_vm_id, nics=[('add', -1, nic)],
399 394
                        hotplug=True, dry_run=settings.TEST)
400 395

  
401 396

  
......
404 399
    ops = [('remove', nic.index, {}) for nic in nics if nic.network == net]
405 400
    if not ops:  # Vm not connected to network
406 401
        return
407
    rapi.ModifyInstance(vm.backend_vm_id, nics=ops[::-1],
402
    vm.client.ModifyInstance(vm.backend_vm_id, nics=ops[::-1],
408 403
                        hotplug=True, dry_run=settings.TEST)
409 404

  
410 405

  
......
414 409
    except KeyError:
415 410
        raise ValueError("Unsopported Firewall Profile: %s" % profile)
416 411

  
412
    client = vm.client
417 413
    # Delete all firewall tags
418 414
    for t in _firewall_tags.values():
419
        rapi.DeleteInstanceTags(vm.backend_vm_id, [t], dry_run=settings.TEST)
415
        client.DeleteInstanceTags(vm.backend_vm_id, [t], dry_run=settings.TEST)
420 416

  
421
    rapi.AddInstanceTags(vm.backend_vm_id, [tag], dry_run=settings.TEST)
417
    client.AddInstanceTags(vm.backend_vm_id, [tag], dry_run=settings.TEST)
422 418

  
423 419
    # XXX NOP ModifyInstance call to force process_net_status to run
424 420
    # on the dispatcher
425
    rapi.ModifyInstance(vm.backend_vm_id,
421
    vm.client.ModifyInstance(vm.backend_vm_id,
426 422
                        os_name=settings.GANETI_CREATEINSTANCE_KWARGS['os'])
427 423

  
428 424

  
429
def get_ganeti_instances():
430
    return rapi.GetInstances()
425
def get_ganeti_instances(backend=None, bulk=False):
426
    Instances = [c.client.GetInstances(bulk=bulk) for c in get_backends(backend)]
427
    return reduce(list.__add__, Instances, [])
428

  
429

  
430
def get_ganeti_nodes(backend=None, bulk=False):
431
    Nodes = [c.client.GetNodes(bulk=bulk) for c in get_backends(backend)]
432
    return reduce(list.__add__, Nodes, [])
433

  
434

  
435
def get_ganeti_jobs(backend=None, bulk=False):
436
    Jobs = [c.client.GetJobs(bulk=bulk) for c in get_backends(backend)]
437
    return reduce(list.__add__, Jobs, [])
438

  
439
##
440
##
441
##
442
def get_backends(backend=None):
443
    if backend:
444
        return [backend]
445
    return Backend.objects.all()
446

  
431 447

  
432 448

  
433
def get_ganeti_nodes():
434
    return rapi.GetNodes()
435 449

  
436 450

  
437
def get_ganeti_jobs():
438
    return rapi.GetJobs()

Also available in: Unified diff