Ajustements Unicode et couleurs, version 16.03.11.
[ansible.git] / scripts / ansible-debian-versions
CommitLineData
28212351
P
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
c3937e94
P
3"""Outil d'extraction d'informations système sur les serveurs Debian.
4
5Copyright ©2016 Agence universitaire de la Francophonie -- www.auf.org
6Licence : GPL version 3
7Auteur : Progfou <jean-christophe.andre@auf.org>
8"""
fea17b3a 9import sys
28212351 10import ansible.runner
fb962525 11from os import getenv
28212351 12from time import strftime
11ee5702 13import re
28212351 14
fea17b3a 15if len(sys.argv) != 2:
fb962525 16 print (u"Usage : %s <nom-d-ensemble-de-machines>" % sys.argv[0]).encode('utf-8')
fea17b3a
P
17 sys.exit(1)
18subset = sys.argv[1]
19
263a400d
P
20# constantes
21
22URL = "http://git.auf.org/?p=ansible.git;a=blob;f=scripts/ansible-debian-versions"
23
fb962525
P
24VERSION = "16.03.11"
25
26RELEASE_NAME = {
27 u'Debian': {
28 u'8': 'jessie', u'7': 'wheezy', u'6': 'squeeze', u'5': 'lenny', u'4': 'etch', u'3': 'sarge',
29 },
30}
133a6b5d
P
31
32RELEASE_COLOR = {
fb962525
P
33 u'Debian': {
34 u"8": "00ae00", u"7": "e6ff00", u"6": "ff950e", u"5": "ff420e",
35 },
36 u'FreeBSD': {
37 u"10": "00ae00", u"9": "e6ff00", u"8": "ff950e", u"7": "ff420e",
38 },
28212351 39}
133a6b5d
P
40DEFAULT_RELEASE_COLOR = "ff0000"
41UNKNOWN_RELEASE_COLOR = "f0f0f0"
42
43VIRTUAL_INDEX = {
fb962525
P
44 u"inconnu": 0,
45 u"physique": 5,
46 u"openvz / hôte": 10,
47 u"openvz / invité": 15,
48 u"kvm / hôte": 20,
49 u"QEMU / invité": 23,
50 u"kvm / invité": 25,
133a6b5d
P
51}
52
53VIRTUAL_COLOR = {
fb962525
P
54 u"kvm / hôte": "e0ffe0",
55 u"kvm / invité": "e0ffe0",
56 u"openvz / invité": "ffe0a0", #"e6ff00",
57 u"openvz / hôte": "ffc0c0", #"ff950e",
58 u"physique": "ffffa0", #"ff420e",
133a6b5d
P
59}
60UNKNOWN_VIRTUAL_COLOR = "f0f0f0"
28212351 61
263a400d
P
62# collecte des données
63
fea17b3a 64pm = ansible.runner.Runner(module_name='setup', timeout=5, subset=subset)
28212351
P
65out = pm.run()
66
263a400d
P
67# sauvegarde temporaire pour débogage
68
69import json
fb962525 70file('/tmp/out-%s.json' % getenv('USER'), 'w').write(json.dumps(out, sort_keys=True, indent=4, separators=(',', ': ')))
263a400d
P
71
72# traitement des données
28212351
P
73
74server_list = []
263a400d 75
28212351 76for server,data in out['contacted'].items():
263a400d
P
77 data['name'] = server
78 facts = data['ansible_facts']
133a6b5d
P
79 # famille
80 if 'ansible_os_family' in facts:
81 data['os_family'] = facts['ansible_os_family']
82 else:
fb962525 83 data['os_family'] = u'inconnue'
133a6b5d 84 # distribution
fb962525
P
85 if 'ansible_distribution' in facts:
86 major = re.findall(r'[0-9]+', facts['ansible_distribution_version'])
87 if len(major) > 0:
88 data['major_release'] = major[0]
89 else:
90 data['major_release'] = u'0'
91 version = re.findall(r'[0-9\.]+', facts['ansible_distribution_version'])
92 if len(version) > 0:
93 data['version'] = version[0]
94 else:
95 data['version'] = u'0'
96 data['description'] = u'%s %s' % (facts['ansible_distribution'],
97 data['version'])
98 release = facts.get('ansible_distribution_release', u'NA')
99 if release == u'NA' and data['os_family'] in RELEASE_NAME:
100 os_release_name = RELEASE_NAME[data['os_family']]
101 if data['major_release'] in os_release_name:
102 release = os_release_name[data['major_release']]
103 if release != u'NA':
104 data['description'] += u' (%s)' % release
263a400d 105 else:
fb962525
P
106 data['major_release'] = u'0'
107 data['description'] = u'inconnue (information non disponible)'
133a6b5d 108 # virtualisation
263a400d
P
109 if 'ansible_virtualization_type' in facts and \
110 'ansible_virtualization_role' in facts:
fb962525
P
111 if facts['ansible_virtualization_role'] == u'guest':
112 virtual_info = u'%s / invité' % facts['ansible_virtualization_type']
113 elif facts['ansible_virtualization_role'] == u'host':
114 virtual_info = u'%s / hôte' % facts['ansible_virtualization_type']
115 elif facts['ansible_virtualization_role'] == u'NA':
116 virtual_info = u'physique'
133a6b5d 117 else:
fb962525 118 virtual_info = u'inconnu (%s / %s)' % (
133a6b5d
P
119 facts['ansible_virtualization_type'],
120 facts['ansible_virtualization_role'] )
263a400d 121 data['virtual_info'] = virtual_info
11ee5702 122 elif 'ansible_processor' in facts and facts['ansible_processor'][0].startswith('QEMU'):
fb962525 123 data['virtual_info'] = u'QEMU / invité'
28212351 124 else:
fb962525 125 data['virtual_info'] = u'inconnu'
133a6b5d 126 # architecture
263a400d 127 if 'ansible_machine' in facts and 'ansible_userspace_bits' in facts:
fb962525 128 data['architecture'] = u'%s bits / %s' % (
c3937e94 129 facts['ansible_userspace_bits'], facts['ansible_machine'] )
263a400d 130 else:
fb962525 131 data['architecture'] = u'inconnue'
133a6b5d 132 # ajout à la liste
263a400d
P
133 server_list.append({
134 'name': data['name'],
133a6b5d 135 'os_family': data['os_family'],
263a400d
P
136 'description': data['description'],
137 'major_release': data['major_release'],
138 'architecture': data['architecture'],
139 'virtual_info': data['virtual_info'],
140 })
141
28212351 142for server,data in out['dark'].items():
133a6b5d 143 # ajout à la liste
263a400d
P
144 server_list.append({
145 'name': server,
fb962525
P
146 'os_family': u'inconnue',
147 'description': u'inconnue (échec de connexion)',
148 'major_release': u'0',
149 'architecture': u'inconnu',
150 'virtual_info': u'inconnu',
263a400d
P
151 })
152
153# tri de la liste des serveurs par version de distribution croissante
28212351 154
133a6b5d 155def sort_key(x):
fb962525 156 os_family = u'0' if x['os_family'] == u'inconnue' else x['os_family']
133a6b5d
P
157 virtual_index = VIRTUAL_INDEX.get(x['virtual_info'], 1)
158 reversed_name = '.'.join(reversed(x['name'].split('.')))
fb962525 159 return u'%s-%03d-%02d-%s' % ( os_family, int(x['major_release']),
133a6b5d
P
160 virtual_index, reversed_name )
161
162server_list.sort(key=sort_key)
28212351 163
263a400d
P
164# affichage du code wiki MoinMoin correspondant
165
fb962525 166print u"||<rowstyle=\"background-color: #cccccc;\">'''Serveur'''||'''Version du système'''||'''Architecture'''||'''Virtualisation'''||".encode('utf-8')
263a400d 167
28212351 168for server in server_list:
133a6b5d 169 # couleur pour la version
fb962525 170 if server['major_release'] == u'0':
133a6b5d 171 server['release_color'] = UNKNOWN_RELEASE_COLOR
fb962525
P
172 elif server['os_family'] in RELEASE_COLOR:
173 os_release_color = RELEASE_COLOR[server['os_family']]
174 if server['major_release'] in os_release_color:
175 server['release_color'] = os_release_color[server['major_release']]
176 else:
177 server['release_color'] = DEFAULT_RELEASE_COLOR
133a6b5d
P
178 else:
179 server['release_color'] = DEFAULT_RELEASE_COLOR
180 # couleur pour la virtualisation
181 if server['virtual_info'] in VIRTUAL_COLOR:
182 server['virtual_color'] = VIRTUAL_COLOR[server['virtual_info']]
28212351 183 else:
133a6b5d 184 server['virtual_color'] = UNKNOWN_VIRTUAL_COLOR
fb962525
P
185 if u'invité' not in server['virtual_info']:
186 server['virtual_info'] = u"'''%s'''" % server['virtual_info']
133a6b5d 187 # affichage
fb962525
P
188 print ( (u"||<)>%(name)s||<#%(release_color)s>%(description)s||" +
189 u"%(architecture)s||<#%(virtual_color)s>%(virtual_info)s||") % \
190 server ).encode('utf-8')
263a400d 191
fb962525
P
192print ( u"(tableau généré le %s à %s avec [[%s|ansible-debian-versions]] %s)" % \
193 (strftime('%Y-%m-%d'), strftime('%H:%M:%S UTC'), URL, VERSION) ).encode('utf-8')