Revision 1a894bfe snf-cyclades-app/synnefo/logic/backend.py

b/snf-cyclades-app/synnefo/logic/backend.py
36 36
from logging import getLogger
37 37
from django.conf import settings
38 38
from django.db import transaction
39
from datetime import datetime
39 40

  
40 41
from synnefo.db.models import (Backend, VirtualMachine, Network, NetworkLink)
41 42
from synnefo.logic import utils
......
55 56

  
56 57

  
57 58
def create_client(hostname, port=5080, username=None, password=None):
58
    return GanetiRapiClient(hostname=hostname,
59
                            port=port,
60
                            username=username,
61
                            password=password)
59
    return GanetiRapiClient(hostname, port, username, password)
62 60

  
63 61
@transaction.commit_on_success
64 62
def process_op_status(vm, etime, jobid, opcode, status, logmsg):
......
423 421

  
424 422

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

  
429 428

  
......
439 438
##
440 439
##
441 440
##
441

  
442

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

  
447 448

  
449
def get_physical_resources(backend):
450
    """ Get the physical resources of a backend.
451

  
452
    Get the resources of a backend as reported by the backend (not the db).
448 453

  
454
    """
455
    nodes = get_ganeti_nodes(backend, bulk=True)
456
    attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
457
    res = {}
458
    for a in attr:
459
        res[a] = 0
460
    for n in nodes:
461
        # Filter out drained, offline and not vm_capable nodes since they will
462
        # not take part in the vm allocation process
463
        if n['vm_capable'] and not n['drained'] and not n['offline']\
464
           and n['cnodes']:
465
            for a in attr:
466
                res[a] += int(n[a])
467
    return res
468

  
469

  
470
def update_resources(backend, resources=None):
471
    """ Update the state of the backend resources in db.
472

  
473
    """
449 474

  
475
    if not resources:
476
        resources = get_physical_resources(backend)
450 477

  
478
    backend.mfree = resources['mfree']
479
    backend.mtotal = resources['mtotal']
480
    backend.dfree = resources['dfree']
481
    backend.dtotal = resources['dtotal']
482
    backend.pinst_cnt = resources['pinst_cnt']
483
    backend.ctotal = resources['ctotal']
484
    backend.updated = datetime.now()
485
    backend.save()
486

  
487

  
488
def get_memory_from_instances(backend):
489
    """ Get the memory that is used from instances.
490

  
491
    Get the used memory of a backend. Note: This is different for
492
    the real memory used, due to kvm's memory de-duplication.
493

  
494
    """
495
    instances = backend.client.GetInstances(bulk=True)
496
    mem = 0
497
    for i in instances:
498
        mem += i['oper_ram']
499
    return mem

Also available in: Unified diff