Revision 1da50fe3

b/snf-cyclades-app/synnefo/logic/backend.py
894 894
    return res
895 895

  
896 896

  
897
def update_resources(backend, resources=None):
897
def update_backend_resources(backend, resources=None):
898 898
    """ Update the state of the backend resources in db.
899 899

  
900 900
    """
......
926 926
        mem += i['oper_ram']
927 927
    return mem
928 928

  
929

  
930
def get_available_disk_templates(backend):
931
    """Get the list of available disk templates of a Ganeti backend.
932

  
933
    The list contains the disk templates that are enabled in the Ganeti backend
934
    and also included in ipolicy-disk-templates.
935

  
936
    """
937
    with pooled_rapi_client(backend) as c:
938
        info = c.GetInfo()
939
    enabled_disk_templates = info["enabled_disk_templates"]
940
    ipolicy_disk_templates = info["ipolicy"]["disk-templates"]
941
    return [dp for dp in enabled_disk_templates
942
            if dp in ipolicy_disk_templates]
943

  
944

  
945
def update_backend_disk_templates(backend):
946
    disk_templates = get_available_disk_templates(backend)
947
    backend.disk_templates = disk_templates
948
    backend.save()
949

  
950

  
929 951
##
930 952
## Synchronized operations for reconciliation
931 953
##
b/snf-cyclades-app/synnefo/logic/backend_allocator.py
35 35
                              BACKEND_PER_USER, ARCHIPELAGO_BACKENDS,
36 36
                              DEFAULT_INSTANCE_NETWORKS)
37 37
from synnefo.db.models import Backend
38
from synnefo.logic.backend import update_resources
38
from synnefo.logic.backend import update_backend_resources
39 39
from synnefo.api.util import backend_public_networks
40 40

  
41 41
log = logging.getLogger(__name__)
......
167 167
        if now > b.updated + delta:
168 168
            log.debug("Updating resources of backend %r. Last Updated %r",
169 169
                      b, b.updated)
170
            update_resources(b)
170
            update_backend_resources(b)
171 171

  
172 172

  
173 173
def get_backend_for_user(userid):
b/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
33 33

  
34 34
from synnefo.db.models import Backend, Network
35 35
from django.db.utils import IntegrityError
36
from synnefo.logic.backend import (get_physical_resources,
37
                                   update_resources,
38
                                   create_network_synced,
39
                                   connect_network_synced)
36
from synnefo.logic import backend as backend_mod
40 37
from synnefo.management.common import check_backend_credentials
41 38
from snf_django.management.utils import pprint_table
42 39

  
......
116 113
            return
117 114

  
118 115
        stream.write("Retrieving backend resources:\n")
119
        resources = get_physical_resources(backend)
116
        resources = backend_mod.get_physical_resources(backend)
120 117
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
121 118

  
122 119
        table = [[str(resources[x]) for x in attr]]
123 120
        pprint_table(stream, table, attr)
124 121

  
125
        update_resources(backend, resources)
122
        backend_mod.update_backend_resources(backend, resources)
123
        backend_mod.update_backend_disk_templates(backend)
126 124

  
127 125
        networks = Network.objects.filter(deleted=False, floating_ip_pool=True)
128 126
        if not networks:
......
139 137

  
140 138
        for net in networks:
141 139
            net.create_backend_network(backend)
142
            result = create_network_synced(net, backend)
140
            result = backend_mod.create_network_synced(net, backend)
143 141
            if result[0] != "success":
144 142
                stream.write('\nError Creating Network %s: %s\n' %
145 143
                             (net.backend_id, result[1]))
146 144
            else:
147 145
                stream.write('Successfully created Network: %s\n' %
148 146
                             net.backend_id)
149
            result = connect_network_synced(network=net, backend=backend)
147
            result = backend_mod.connect_network_synced(network=net,
148
                                                        backend=backend)
150 149
            if result[0] != "success":
151 150
                stream.write('\nError Connecting Network %s: %s\n' %
152 151
                             (net.backend_id, result[1]))
b/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or without
4 4
# modification, are permitted provided that the following conditions
......
27 27
# those of the authors and should not be interpreted as representing official
28 28
# policies, either expressed or implied, of GRNET S.A.
29 29
#
30

  
31
from optparse import make_option
32 30
from django.core.management.base import BaseCommand
33
from synnefo.management.common import get_backend
34

  
35
from django.conf import settings
36
import datetime
37 31

  
38 32
from synnefo.db.models import Backend
39
from synnefo.logic.backend import update_resources
40

  
41

  
42
class Command(BaseCommand):
43
    can_import_settings = True
33
from synnefo.logic import backend as backend_mod
44 34

  
45
    help = "Update backend statistics, which are used for instance allocation."
46
    output_transaction = True  # The management command runs inside
47
                               # an SQL transaction
48
    option_list = BaseCommand.option_list + (
49
        make_option('--backend-id', dest='backend_id',
50
                    help="Update statistics of only this backend"),
51
        make_option('--older-than', dest='older_than', metavar="MINUTES",
52
                    help="Update only backends that have not been updated for\
53
                    MINUTES. Set to 0 to force update."),
54
        make_option('--include-drained', dest='drained',
55
                    default=False,
56
                    action='store_true',
57
                    help="Also update statistics of drained backends")
58
    )
59 35

  
60
    def handle(self, **options):
36
HELP_MSG = """Query Ganeti backends and update the status of backend in DB.
61 37

  
62
        if options['backend_id']:
63
            backends = [get_backend(options['backend_id'])]
64
        else:
65
            backends = Backend.objects.filter(offline=False)
66
            if not options['drained']:
67
                backends = backends.filter(drained=False)
38
This command updates:
39
    * the list of the enabled disk-templates
40
    * the available resources (disk, memory, CPUs)
41
"""
68 42

  
69
        now = datetime.datetime.now()
70
        if options['older_than'] is not None:
71
            minutes = int(options['older_than'])
72
        else:
73
            minutes = settings.BACKEND_REFRESH_MIN
74 43

  
75
        delta = datetime.timedelta(minutes=minutes)
44
class Command(BaseCommand):
45
    help = HELP_MSG
76 46

  
77
        for b in backends:
78
            if now > b.updated + delta:
79
                update_resources(b)
80
                print 'Successfully updated backend with id: %d' % b.id
81
            else:
82
                print 'Backend %d does not need update' % b.id
47
    def handle(self, **options):
48
        for backend in Backend.objects.filter(offline=False):
49
            backend_mod.update_backend_disk_templates(backend)
50
            backend_mod.update_backend_resources(backend)
51
            self.stdout.write("Successfully updated backend '%s'\n" % backend)

Also available in: Unified diff