commit 1da50fe378f4cb0b183282fdc53e01dc100c1e25
Author: Christos Stavrakakis <cstavr@grnet.gr>
Date:   Mon Sep 30 16:01:21 2013 +0300

    cyclades: Refactor backend-update-status cmd
    
    The command has been changed to no longer take any arguments or options.
    Instead, this command always updates the resources and the enabled disk
    templates of all online Ganeti backends.

diff --git a/snf-cyclades-app/synnefo/logic/backend.py b/snf-cyclades-app/synnefo/logic/backend.py
index 16c1824..d1ef151 100644
--- a/snf-cyclades-app/synnefo/logic/backend.py
+++ b/snf-cyclades-app/synnefo/logic/backend.py
@@ -894,7 +894,7 @@ def get_physical_resources(backend):
     return res
 
 
-def update_resources(backend, resources=None):
+def update_backend_resources(backend, resources=None):
     """ Update the state of the backend resources in db.
 
     """
@@ -926,6 +926,28 @@ def get_memory_from_instances(backend):
         mem += i['oper_ram']
     return mem
 
+
+def get_available_disk_templates(backend):
+    """Get the list of available disk templates of a Ganeti backend.
+
+    The list contains the disk templates that are enabled in the Ganeti backend
+    and also included in ipolicy-disk-templates.
+
+    """
+    with pooled_rapi_client(backend) as c:
+        info = c.GetInfo()
+    enabled_disk_templates = info["enabled_disk_templates"]
+    ipolicy_disk_templates = info["ipolicy"]["disk-templates"]
+    return [dp for dp in enabled_disk_templates
+            if dp in ipolicy_disk_templates]
+
+
+def update_backend_disk_templates(backend):
+    disk_templates = get_available_disk_templates(backend)
+    backend.disk_templates = disk_templates
+    backend.save()
+
+
 ##
 ## Synchronized operations for reconciliation
 ##
diff --git a/snf-cyclades-app/synnefo/logic/backend_allocator.py b/snf-cyclades-app/synnefo/logic/backend_allocator.py
index 85410cd..eea0384 100644
--- a/snf-cyclades-app/synnefo/logic/backend_allocator.py
+++ b/snf-cyclades-app/synnefo/logic/backend_allocator.py
@@ -35,7 +35,7 @@ from synnefo.settings import (BACKEND_ALLOCATOR_MODULE, BACKEND_REFRESH_MIN,
                               BACKEND_PER_USER, ARCHIPELAGO_BACKENDS,
                               DEFAULT_INSTANCE_NETWORKS)
 from synnefo.db.models import Backend
-from synnefo.logic.backend import update_resources
+from synnefo.logic.backend import update_backend_resources
 from synnefo.api.util import backend_public_networks
 
 log = logging.getLogger(__name__)
@@ -167,7 +167,7 @@ def refresh_backends_stats(backends):
         if now > b.updated + delta:
             log.debug("Updating resources of backend %r. Last Updated %r",
                       b, b.updated)
-            update_resources(b)
+            update_backend_resources(b)
 
 
 def get_backend_for_user(userid):
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py b/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
index b17e10b..941ba90 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
@@ -33,10 +33,7 @@ from django.core.management.base import BaseCommand, CommandError
 
 from synnefo.db.models import Backend, Network
 from django.db.utils import IntegrityError
-from synnefo.logic.backend import (get_physical_resources,
-                                   update_resources,
-                                   create_network_synced,
-                                   connect_network_synced)
+from synnefo.logic import backend as backend_mod
 from synnefo.management.common import check_backend_credentials
 from snf_django.management.utils import pprint_table
 
@@ -116,13 +113,14 @@ def create_backend(clustername, port, username, password, hypervisor=None,
             return
 
         stream.write("Retrieving backend resources:\n")
-        resources = get_physical_resources(backend)
+        resources = backend_mod.get_physical_resources(backend)
         attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
 
         table = [[str(resources[x]) for x in attr]]
         pprint_table(stream, table, attr)
 
-        update_resources(backend, resources)
+        backend_mod.update_backend_resources(backend, resources)
+        backend_mod.update_backend_disk_templates(backend)
 
         networks = Network.objects.filter(deleted=False, floating_ip_pool=True)
         if not networks:
@@ -139,14 +137,15 @@ def create_backend(clustername, port, username, password, hypervisor=None,
 
         for net in networks:
             net.create_backend_network(backend)
-            result = create_network_synced(net, backend)
+            result = backend_mod.create_network_synced(net, backend)
             if result[0] != "success":
                 stream.write('\nError Creating Network %s: %s\n' %
                              (net.backend_id, result[1]))
             else:
                 stream.write('Successfully created Network: %s\n' %
                              net.backend_id)
-            result = connect_network_synced(network=net, backend=backend)
+            result = backend_mod.connect_network_synced(network=net,
+                                                        backend=backend)
             if result[0] != "success":
                 stream.write('\nError Connecting Network %s: %s\n' %
                              (net.backend_id, result[1]))
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py b/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py
index a471fec..998c53d 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py
@@ -1,4 +1,4 @@
-# Copyright 2011-2012 GRNET S.A. All rights reserved.
+# Copyright 2011-2013 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -27,56 +27,25 @@
 # those of the authors and should not be interpreted as representing official
 # policies, either expressed or implied, of GRNET S.A.
 #
-
-from optparse import make_option
 from django.core.management.base import BaseCommand
-from synnefo.management.common import get_backend
-
-from django.conf import settings
-import datetime
 
 from synnefo.db.models import Backend
-from synnefo.logic.backend import update_resources
-
-
-class Command(BaseCommand):
-    can_import_settings = True
+from synnefo.logic import backend as backend_mod
 
-    help = "Update backend statistics, which are used for instance allocation."
-    output_transaction = True  # The management command runs inside
-                               # an SQL transaction
-    option_list = BaseCommand.option_list + (
-        make_option('--backend-id', dest='backend_id',
-                    help="Update statistics of only this backend"),
-        make_option('--older-than', dest='older_than', metavar="MINUTES",
-                    help="Update only backends that have not been updated for\
-                    MINUTES. Set to 0 to force update."),
-        make_option('--include-drained', dest='drained',
-                    default=False,
-                    action='store_true',
-                    help="Also update statistics of drained backends")
-    )
 
-    def handle(self, **options):
+HELP_MSG = """Query Ganeti backends and update the status of backend in DB.
 
-        if options['backend_id']:
-            backends = [get_backend(options['backend_id'])]
-        else:
-            backends = Backend.objects.filter(offline=False)
-            if not options['drained']:
-                backends = backends.filter(drained=False)
+This command updates:
+    * the list of the enabled disk-templates
+    * the available resources (disk, memory, CPUs)
+"""
 
-        now = datetime.datetime.now()
-        if options['older_than'] is not None:
-            minutes = int(options['older_than'])
-        else:
-            minutes = settings.BACKEND_REFRESH_MIN
 
-        delta = datetime.timedelta(minutes=minutes)
+class Command(BaseCommand):
+    help = HELP_MSG
 
-        for b in backends:
-            if now > b.updated + delta:
-                update_resources(b)
-                print 'Successfully updated backend with id: %d' % b.id
-            else:
-                print 'Backend %d does not need update' % b.id
+    def handle(self, **options):
+        for backend in Backend.objects.filter(offline=False):
+            backend_mod.update_backend_disk_templates(backend)
+            backend_mod.update_backend_resources(backend)
+            self.stdout.write("Successfully updated backend '%s'\n" % backend)
