refonte presque totale pour intgration aux scripts

darcs-hash:20041022011028-4ec08-98bce9c5c7f4e5fae5f2ccd4a68bbc5633c88e2e.gz
This commit is contained in:
chove 2004-10-22 03:10:28 +02:00
parent 8cd8a93fad
commit 701db500c1

View file

@ -1,77 +1,41 @@
#! /usr/bin/env python
# -*- coding: iso-8859-15 -*-
"""
Classe de génération du fichier de l'autostatus
Utilisé par generate.py
"""
# importation des fonctions et classes
import sys, os
sys.path.append('/usr/scripts/gestion')
from ldap_crans import crans, crans_ldap, strip_accents
from ldap_crans import crans
from gen_confs import gen_config
def autostatus (db) :
# définition de la classe
class autostatus(gen_config) :
# CONFIGURATION #
#################
# fichier de l'autostatus
#file = open('/home/chove/hosts','w')
file = open('/etc/autostatus/hosts','w')
CONFFILE = '/home/chove/hosts'
# machines à ne pas mettre dans l'autostatus
exclude = ["non-configure.wifi.crans.org"]
# machines crans
machines = crans().machines()
# tri des machines par type
bornes = {}
switchs = {}
serveurs = {}
# tri des machines
for m in machines :
# machine exclue
if m.nom() in exclude :
print "Exclusion de %s" % m.nom()
continue
# on ajoute au bon tableau les infos
if m.canal() :
# C'est une borne (format : index#~#nom#~#commentaire)
if m.info() :
# on regarde si c'est une borne de batiment
if ( m.info()[0][0:3] == "Au " ) and ( len(m.info()[0]) == 5 ) :
bornes[ "1-" + m.info()[0][4]+m.info()[0][3] ] = [ m.nom() , m.info()[0] ]
else :
bornes[ "2-" +m.info()[0] ] = [ m.nom() , m.info()[0] ]
else :
# on met les bornes non définies à la fin (ordre alphabétique)
bornes[ "3-" + m.nom() ] = [ m.nom() , "Inutilisee" ]
elif 'bat' in m.nom() or 'backbone' in m.nom() :
# C'est un switch
# on convertit le bat# en bat#-0
if not "-" in m.nom().split(".")[0] :
index = m.nom().split(".")[0] + "-0"
else :
index = m.nom().split(".")[0]
if m.info() :
switchs[ index ] = [ m.nom() , m.info()[0] ]
else :
switchs[ index ] = [ m.nom() , "Infos non disponiles" ]
else :
# C'est un serveur
if m.info() :
serveurs[ m.nom() ] = [ m.nom() , m.info()[0] ]
else :
serveurs[ m.nom() ] = [ m.nom() , "Infos non disponiles" ]
# début du fichier
file.write( """# NE PAS MODIFIER : fichié généré par /home/chove/autostatus.py
# matrice du fichier d'autostatus
matrice = """
# Format : name address depend,list contact description
# routeurs vers l'extérieur #
#############################
%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Routeurs, dans l'ordre où ils sont entre le crans et l'extérieur :</font></b></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Routeurs, dans l'ordre où ils sont entre le crans et l'extérieur :</font></b></TR>
komaz 138.231.136.4 none alerts@crans.org Notre routeur
komaz-ext 138.231.135.6 komaz alerts@crans.org Patte externe de notre routeur.
@ -88,79 +52,137 @@ OrsayCSSI1 193.51.180.122 komaz,komaz-ext,irts,irts-ext,gateway.zr
OrsayCSSI2 193.51.180.121 komaz,komaz-ext,irts,irts-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan,CachanCSSI1,CachanCSSI2,OrsayCSSI1 alerts@crans.org Routeur RENATER
nriCSSI 193.51.179.41 komaz,komaz-ext,irts,irts-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan,CachanCSSI1,CachanCSSI2,OrsayCSSI1,OrsayCSSI2 alerts@crans.org Routeur RENATER
""" )
# serveurs du crans
file.write( """
# serveurs du crans #
#####################
%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Serveurs :</font></b></TR>
%%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Serveurs :</font></b></TR>
""" )
liste = serveurs.keys()
liste.sort()
for i in liste :
data = serveurs[i]
if ".wifi.crans.org" in i :
file.write ( data[0].split(".")[0] + ".wifi " + data[0] + " none nobody " + strip_accents ( data[1] + "\n" ).replace('_',' ') )
else :
file.write ( data[0].split(".")[0] + " " + data[0] + " none nobody " + strip_accents ( data[1] + "\n" ).replace('_',' ') )
%(serveurs)s
# switchs
file.write ( """
# switchs #
###########
%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Switchs :</font></b></TR>
""" )
liste = switchs.keys()
liste.sort()
for i in liste :
data = switchs[i]
file.write ( data[0].split(".")[0] + " " + data[0] + " none nobody " + strip_accents ( data[1] + "\n" ).replace('_',' ') )
%%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Switchs :</font></b></TR>
%(switchs)s
# bornes wifi
file.write ( """
# bornes wifi #
###############
%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Bornes wifi :</font></b></TR>
%%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Bornes wifi :</font></b></TR>
""" )
liste = bornes.keys()
liste.sort()
for i in liste :
data = bornes[i]
file.write ( data[0].split(".")[0] + " " + data[0] + " none nobody " + strip_accents ( data[1] + "\n" ).replace('_',' ') )
%(bornes)s
# services extérieurs
file.write ( """
# Sites web et Services #
#########################
%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Sites web et Services HORS de l'ENS:</font></b></TR>
%HTML: <TR><td colspan=3 class="table"><font size="2">(21:FTP ; 80:HTTP ; 119:news ; 5190:port du protocole ICQ)</TR>
%%HTML: <TR> <TD class=table colSpan=3><p></TD></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="+1"><B>Sites web et Services HORS de l'ENS:</font></b></TR>
%%HTML: <TR><td colspan=3 class="table"><font size="2">(21:FTP ; 80:HTTP ; 119:news ; 5190:port du protocole ICQ)</TR>
Jussieu:21 195.83.118.1 irts,irts-ext,komaz,komaz-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan nobody Le serveur <a href="ftp://ftp.lip6.fr">FTP</a> de Jussieu. (France)
Free:21 213.228.0.141 irts,irts-ext,komaz,komaz-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan nobody Le serveur <a href="ftp://ftp.free.fr">FTP</a> de free. (France)
Voila:80 195.101.94.80 irts,irts-ext,komaz,komaz-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan nobody Est-ce que <a href="http://www.voila.fr">Voila</a> fonctionne ? (France)
Yahoo!:80 66.94.230.39 irts,irts-ext,komaz,komaz-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan nobody Est-ce que <a href="http://www.yahoo.com">Yahoo!</a> fonctionne ? (USA)
Altavista:80 216.155.200.155 irts,irts-ext,komaz,komaz-ext,gateway.zrt,kwai,pioneer,pioneer-ext,RenaterCachan nobody Est-ce que <a href="http://www.altavista.com">Altavista</a> fonctionne ? (USA)
""" )
file.close()
"""
# service à redémarer
restart_cmd = ""
# FIN DE LA CONFIGURATION
def __str__ (self) :
return "autostatus"
# on commit
os.popen( "cvs -Q -d '/usr/cvs-rep' commit -l -m 'Changement dans l autotstatus' /etc/autostatus/hosts" )
def make_config (self, dico ) :
"""
Transforme le dico en suite lignes de configuration
"""
liste = dico.keys()
liste.sort()
append = ""
for i in liste :
append = append + " ".join( dico[i] ) + "\n"
return append.encode('iso-8859-15')
def _gen (self) :
# machines crans
machines = crans().machines()
# tri des machines par type
bornes = {}
switchs = {}
serveurs = {}
# tri des machines
for m in machines :
# machine exclue
if m.nom() in self.exclude :
continue
# les bornes
if m.canal() :
# C'est une borne
if m.info() :
# on regarde si c'est une borne de batiment
if ( m.info()[0][0:3] == "Au " ) and ( len(m.info()[0]) == 5 ) :
bornes[ "1-" + m.info()[0][4]+m.info()[0][3] ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , m.info()[0] ]
else :
bornes[ "2-" +m.info()[0] ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , m.info()[0] ]
else :
# on met les bornes non définies à la fin (ordre alphabétique)
bornes[ "3-" + m.nom() ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , 'Inutilise' ]
# les switchs
elif 'bat' == m.nom()[0:3] or 'backbone.crans.org' == m.nom() :
# on convertit le bat# en bat#-0
if not "-" in m.nom().split(".")[0] :
index = m.nom().split(".")[0] + "-0"
else :
index = m.nom().split(".")[0]
# on ajoute au dictionnaire
if m.info() :
switchs[ index ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , m.info()[0] ]
else :
switchs[ index ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , 'Infos non disponibles' ]
# les serveurs
else :
if m.info() :
serveurs[ m.nom() ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , m.info()[0] ]
else :
serveurs[ m.nom() ] = [ m.nom().split(".")[0] , m.nom() , 'none' , 'nobody' , 'Infos non disponibles' ]
# début du fichier
self.file = self._open_conf(self.CONFFILE)
# génère le dictionnaire pour les modifications
dico = {}
dico['switchs'] = self.make_config(switchs)
dico['bornes'] = self.make_config(bornes)
dico['serveurs'] = self.make_config(serveurs)
# on écrit dans le fichier de configuration
self.file.write( self.matrice % dico )
# on ferme le fichier
self.file.close()
# on commit
#os.popen( "cvs -Q -d '/usr/cvs-rep' commit -l -m 'Changement dans l autotstatus' /etc/autostatus/hosts" )
if __name__ == '__main__' :
# base de données
base = crans_ldap()
tmp = autostatus()
# lancement du script
autostatus ( base )
tmp.reconfigure()