Révision complète du script de listage des serveurs, version 16.03.10.
authorProgfou <jean-christophe.andre@auf.org>
Thu, 10 Mar 2016 06:01:17 +0000 (01:01 -0500)
committerProgfou <jean-christophe.andre@auf.org>
Thu, 10 Mar 2016 06:01:17 +0000 (01:01 -0500)
scripts/ansible-debian-versions

index 80fd503..8c4a42a 100755 (executable)
@@ -20,11 +20,31 @@ subset = sys.argv[1]
 
 URL = "http://git.auf.org/?p=ansible.git;a=blob;f=scripts/ansible-debian-versions"
 
-release_color = {
+VERSION = "16.03.10"
+
+RELEASE_COLOR = {
   "8": "00ae00", "7": "e6ff00", "6": "ff950e", "5": "ff420e",
 }
-default_release_color = "ff0000"
-unknown_release_color = "f0f0f0"
+DEFAULT_RELEASE_COLOR = "ff0000"
+UNKNOWN_RELEASE_COLOR = "f0f0f0"
+
+VIRTUAL_INDEX = {
+  "inconnu": 0,
+  "physique": 5,
+  "openvz / hôte": 10,
+  "openvz / invité": 15,
+  "kvm / hôte": 20,
+  "QEMU / invité": 23,
+  "kvm / invité": 25,
+}
+
+VIRTUAL_COLOR = {
+  "kvm / hôte": "00ae00", "kvm / invité": "00ae00",
+  "openvz / invité": "e6ff00",
+  "openvz / hôte": "ff950e",
+  "physique": "ff420e",
+}
+UNKNOWN_VIRTUAL_COLOR = "f0f0f0"
 
 # collecte des données
 
@@ -43,6 +63,12 @@ server_list = []
 for server,data in out['contacted'].items():
     data['name'] = server
     facts = data['ansible_facts']
+    # famille
+    if 'ansible_os_family' in facts:
+        data['os_family'] = facts['ansible_os_family']
+    else:
+        data['os_family'] = 'inconnue'
+    # distribution
     if 'ansible_lsb' in facts:
         lsb_data = facts['ansible_lsb']
         data['description'] = lsb_data['description']
@@ -56,28 +82,34 @@ for server,data in out['contacted'].items():
     else:
         data['description'] = 'inconnue (information non disponible)'
         data['major_release'] = '0'
+    # virtualisation
     if 'ansible_virtualization_type' in facts and \
        'ansible_virtualization_role' in facts:
-        virtual_info = "%s / %s" % ( facts['ansible_virtualization_type'],
-             facts['ansible_virtualization_role'] )
-        if virtual_info == 'NA / NA':
-            virtual_info = "physique"
-        if facts['ansible_virtualization_role'] != 'guest':
-            virtual_info = "'''%s'''" % virtual_info
-        virtual_info = virtual_info.replace('host', 'hôte')
-        virtual_info = virtual_info.replace('guest', 'invité')
+        if facts['ansible_virtualization_role'] == 'guest':
+            virtual_info = '%s / invité' % facts['ansible_virtualization_type']
+        elif facts['ansible_virtualization_role'] == 'host':
+            virtual_info = '%s / hôte' % facts['ansible_virtualization_type']
+        elif facts['ansible_virtualization_role'] == 'NA':
+            virtual_info = 'physique'
+        else:
+            virtual_info = "inconnu (%s / %s)" % (
+                facts['ansible_virtualization_type'],
+                facts['ansible_virtualization_role'] )
         data['virtual_info'] = virtual_info
     elif 'ansible_processor' in facts and facts['ansible_processor'][0].startswith('QEMU'):
-        data['virtual_info'] = "'''QEMU'''"
+        data['virtual_info'] = 'QEMU / invité'
     else:
         data['virtual_info'] = 'inconnu'
+    # architecture
     if 'ansible_machine' in facts and 'ansible_userspace_bits' in facts:
         data['architecture'] = "%s bits / %s" % (
             facts['ansible_userspace_bits'], facts['ansible_machine'] )
     else:
         data['architecture'] = 'inconnue'
+    # ajout à la liste
     server_list.append({
         'name': data['name'],
+        'os_family': data['os_family'],
         'description': data['description'],
         'major_release': data['major_release'],
         'architecture': data['architecture'],
@@ -85,8 +117,10 @@ for server,data in out['contacted'].items():
     })
 
 for server,data in out['dark'].items():
+    # ajout à la liste
     server_list.append({
         'name': server,
+        'os_family': 'inconnue',
         'description': 'inconnue (échec de connexion)',
         'major_release': '0',
         'architecture': 'inconnu',
@@ -95,19 +129,35 @@ for server,data in out['dark'].items():
 
 # tri de la liste des serveurs par version de distribution croissante
 
-server_list.sort(key=lambda x: '%03d-%s' % (int(x['major_release']),x['name']))
+def sort_key(x):
+    os_family = '0' if x['os_family'] == 'inconnue' else x['os_family']
+    virtual_index = VIRTUAL_INDEX.get(x['virtual_info'], 1)
+    reversed_name = '.'.join(reversed(x['name'].split('.')))
+    return '%s-%03d-%02d-%s' % ( os_family, int(x['major_release']),
+        virtual_index, reversed_name )
+
+server_list.sort(key=sort_key)
 
 # affichage du code wiki MoinMoin correspondant
 
-print "||<tablestyle=\"background-color: #cccccc;\">'''Serveur'''||'''Version du système au %s'''||'''Architecture'''||'''Virtualisation'''||" % strftime('%Y-%m-%d')
+print "||<rowstyle=\"background-color: #cccccc;\">'''Serveur'''||'''Version du système au %s'''||'''Architecture'''||'''Virtualisation'''||" % strftime('%Y-%m-%d')
 
 for server in server_list:
+    # couleur pour la version
     if server['major_release'] == '0':
-        server['color'] = unknown_release_color
-    elif server['major_release'] in release_color:
-        server['color'] = release_color[server['major_release']]
+        server['release_color'] = UNKNOWN_RELEASE_COLOR
+    elif server['major_release'] in RELEASE_COLOR:
+        server['release_color'] = RELEASE_COLOR[server['major_release']]
+    else:
+        server['release_color'] = DEFAULT_RELEASE_COLOR
+    # couleur pour la virtualisation
+    if server['virtual_info'] in VIRTUAL_COLOR:
+        server['virtual_color'] = VIRTUAL_COLOR[server['virtual_info']]
     else:
-        server['color'] = default_release_color
-    print "||<rowstyle=\"background-color: #%(color)s;\">%(name)s||%(description)s||%(architecture)s||%(virtual_info)s||" % server
+        server['virtual_color'] = UNKNOWN_VIRTUAL_COLOR
+    if 'invité' not in server['virtual_info']:
+        server['virtual_info'] = "'''%s'''" % server['virtual_info']
+    # affichage
+    print "||<)>%(name)s||<#%(release_color)s>%(description)s||%(architecture)s||<#%(virtual_color)s>%(virtual_info)s||" % server
 
-print "(tableau généré via le script [[%s|ansible-debian-versions]])" % URL
+print "(tableau généré via le script [[%s|ansible-debian-versions]] version %s)" % (URL, VERSION)