Mise en forme.

darcs-hash:20060321215226-68412-e038f2d8d906de971d698059064ee6b91816ad01.gz
This commit is contained in:
glondu 2006-03-21 22:52:26 +01:00
parent 4b21e8ccbd
commit c158276928

View file

@ -4,12 +4,12 @@
# Copyright (C) Frédéric Pauget # Copyright (C) Frédéric Pauget
# Licence : GPLv2 # Licence : GPLv2
"""Ce script permet de lancer la reconfiguration des divers services """Ce script permet de lancer la reconfiguration des divers services
Usage: %(prog)s options Usage: %(prog)s options
Les options possibles sont : Les options possibles sont :
\t%(options)s \t%(options)s
Les options avec = doivent être suivies d'un argument. Plusieurs Les options avec = doivent être suivies d'un argument. Plusieurs
arguments peuvent être founis pour une même option, les séparer par & arguments peuvent être founis pour une même option, les séparer par &
""" """
@ -29,308 +29,308 @@ if os.getuid() != 0:
sys.stderr.write("Il faut être root\n") sys.stderr.write("Il faut être root\n")
sys.exit(1) sys.exit(1)
signal.signal(signal.SIGINT,signal.SIG_IGN) # Pas de Ctrl-C signal.signal(signal.SIGINT, signal.SIG_IGN) # Pas de Ctrl-C
db = crans_ldap() db = crans_ldap()
make_lock('auto_generate','Big lock',nowait=1) make_lock('auto_generate', 'Big lock', nowait=1)
class base_reconfigure : class base_reconfigure:
__service_develop = { __service_develop = {
'macip' : [ 'rouge-macip', 'zamok-macip', 'sila-macip' , 'komaz-macip' ] , 'macip': [ 'rouge-macip', 'zamok-macip', 'sila-macip', 'komaz-macip' ],
'droits': [ 'rouge-droits', 'ragnarok-droits' ] , 'droits': [ 'rouge-droits', 'ragnarok-droits' ],
'blacklist_upload' : [ 'sila-blacklist_upload', 'komaz-blacklist' ] , 'blacklist_upload': [ 'sila-blacklist_upload', 'komaz-blacklist' ],
'blacklist_p2p' : [ 'sila-blacklist_p2p', 'komaz-blacklist' ] , 'blacklist_p2p': [ 'sila-blacklist_p2p', 'komaz-blacklist' ],
'blacklist_autodisc_upload' : [ 'sila-blacklist_autodisc_upload', 'komaz-blacklist'] , 'blacklist_autodisc_upload': [ 'sila-blacklist_autodisc_upload', 'komaz-blacklist'],
'blacklist_autodisc_p2p' : [ 'sila-blacklist_autodisc_p2p' , 'komaz-blacklist'] 'blacklist_autodisc_p2p': [ 'sila-blacklist_autodisc_p2p', 'komaz-blacklist']
} }
def __init__(self,to_do=[]) : def __init__(self, to_do=[]):
if not to_do : if not to_do:
if debug : print 'Lecture services à redémarrer dans la base LDAP' if debug: print 'Lecture services à redémarrer dans la base LDAP'
auto = 1 auto = 1
to_do = {} to_do = {}
# Création de la liste de ce qu'il y a à faire # Création de la liste de ce qu'il y a à faire
for serv in db.services_to_restart() : for serv in db.services_to_restart():
# Services spéciaux portant sur plusieurs machines # Services spéciaux portant sur plusieurs machines
to_add = self.__service_develop.get(serv.nom,[]) to_add = self.__service_develop.get(serv.nom, [])
if to_add : if to_add:
for nom in to_add : for nom in to_add:
for t in serv.start : for t in serv.start:
db.services_to_restart(nom,serv.args,t) db.services_to_restart(nom, serv.args, t)
if time()-timezone > t : if time() - timezone > t:
to_do[nom] = serv.args to_do[nom] = serv.args
db.services_to_restart('-' + serv.nom) db.services_to_restart('-' + serv.nom)
else : else:
for t in serv.start : for t in serv.start:
if time()-timezone > t : if time() - timezone > t:
to_do[serv.nom] = serv.args to_do[serv.nom] = serv.args
break break
else : else:
auto = 0 auto = 0
if debug : print 'Services à redémarrer imposés (non lecture de la base LDAP)' if debug: print 'Services à redémarrer imposés (non lecture de la base LDAP)'
for serv,args in to_do.items() : for serv, args in to_do.items():
# Au cas ou le service porte sur plusieurs machines # Au cas ou le service porte sur plusieurs machines
service = serv.replace('%s-'%hostname,'') service = serv.replace('%s-'%hostname, '')
if hasattr(self,service) : if hasattr(self, service):
# Le service est à reconfigurer sur cette machine # Le service est à reconfigurer sur cette machine
db.services_to_restart('-%s' % serv) db.services_to_restart('-%s' % serv)
try: try:
m = getattr(self,service) m = getattr(self, service)
if len(getargspec(m)[0])>1 : if len(getargspec(m)[0])>1:
m(args) m(args)
else : else:
m() m()
except: except:
if auto : db.services_to_restart(serv,args) if auto: db.services_to_restart(serv, args)
sys.stderr.write('%s :'%serv) sys.stderr.write('%s :'%serv)
import traceback import traceback
traceback.print_exc() traceback.print_exc()
else : else:
# Le service n'existe pas # Le service n'existe pas
if debug : print "Le service %s n'est pas disponible sur %s" % (service, hostname) if debug: print "Le service %s n'est pas disponible sur %s" % (service, hostname)
if debug : if debug:
reste = db.services_to_restart() reste = db.services_to_restart()
if reste : if reste:
print "Reste à faire :" print "Reste à faire :"
for s in reste : for s in reste:
print '\t%s' % s print '\t%s' % s
else : else:
print "Plus rien à faire" print "Plus rien à faire"
def _machines(self) : def _machines(self):
""" Retourne les machines de la base étant 'à jour' """ """ Retourne les machines de la base étant 'à jour' """
return db.all_machines(graphic=True) return db.all_machines(graphic=True)
def _do(self,service,machines=None) : def _do(self, service, machines=None):
# Reconfiguration des services # Reconfiguration des services
service.debug = debug service.debug = debug
if machines : if machines:
service.machines = machines service.machines = machines
service.reconfigure() service.reconfigure()
def macip(self,ips) : def macip(self, ips):
import firewall import firewall
cprint("Mise à jour correspondance MAC-IP",'gras') cprint("Mise à jour correspondance MAC-IP", 'gras')
eval("firewall.firewall_%s()" % hostname).mac_ip_maj(ips) eval("firewall.firewall_%s()" % hostname).mac_ip_maj(ips)
class rouge(base_reconfigure) : class rouge(base_reconfigure):
def autostatus(self) : def autostatus(self):
from autostatus import autostatus from autostatus import autostatus
self._do(autostatus()) self._do(autostatus())
def dns(self) : def dns(self):
from gen_confs.bind import dns from gen_confs.bind import dns
self._do(dns(),self._machines()) self._do(dns(), self._machines())
def dhcp(self) : def dhcp(self):
from gen_confs.dhcpd import dhcp from gen_confs.dhcpd import dhcp
self._do(dhcp(),self._machines()) self._do(dhcp(), self._machines())
def droits(self) : def droits(self):
from gen_confs.droits import droits_ldap from gen_confs.droits import droits_ldap
self._do(droits_ldap()) self._do(droits_ldap())
def ML_ens(self,mails) : def ML_ens(self, mails):
from adherents import ML_ens from adherents import ML_ens
self._do(ML_ens(mails)) self._do(ML_ens(mails))
def mail_modif(self,trucs) : def mail_modif(self, trucs):
""" trucs est une liste de recherches à effectuer dans la base """ trucs est une liste de recherches à effectuer dans la base
l'affichage des résultats formera le corps du mail """ l'affichage des résultats formera le corps du mail """
from supervison import mail from supervison import mail
self._do(mail(trucs)) self._do(mail(trucs))
def switch(self,chambres) : def switch(self, chambres):
from gen_confs.switchs import switch from gen_confs.switchs import switch
self._do(switch(chambres)) self._do(switch(chambres))
def surveillance_exemptions(self) : def surveillance_exemptions(self):
from gen_confs.surveillance import exemptions from gen_confs.surveillance import exemptions
self._do(exemptions()) self._do(exemptions())
def surveillance_machines(self) : def surveillance_machines(self):
from gen_confs.surveillance import machines from gen_confs.surveillance import machines
self._do(machines(),self._machines()) self._do(machines(), self._machines())
class zamok(base_reconfigure) : class zamok(base_reconfigure):
# Tout est dans le parent # Tout est dans le parent
pass pass
class vert(base_reconfigure) : class vert(base_reconfigure):
def home(self,args) : def home(self, args):
from adherents import home from adherents import home
self._do(home(args)) self._do(home(args))
def del_user(self,args) : def del_user(self, args):
from adherents import del_user from adherents import del_user
self._do(del_user(args)) self._do(del_user(args))
class komaz(base_reconfigure) : class komaz(base_reconfigure):
def __fw(self) : def __fw(self):
if not hasattr(self,'__real_fw') : if not hasattr(self, '__real_fw'):
from firewall import firewall_komaz from firewall import firewall_komaz
self.__real_fw = firewall_komaz() self.__real_fw = firewall_komaz()
return self.__real_fw return self.__real_fw
def macip(self,ips) : def macip(self, ips):
cprint("Mise à jour correspondance MAC-IP",'gras') cprint("Mise à jour correspondance MAC-IP", 'gras')
self.__fw().mac_ip_maj(ips) self.__fw().mac_ip_maj(ips)
def ports(self,ips) : def ports(self, ips):
self.__fw().port_maj(ips) self.__fw().port_maj(ips)
def blacklist(self) : def blacklist(self):
self.__fw().blacklist() self.__fw().blacklist()
class sila(base_reconfigure) : class sila(base_reconfigure):
def bl_carte_etudiant(self) : def bl_carte_etudiant(self):
from gen_confs.squid import squid_carte from gen_confs.squid import squid_carte
self._do(squid_carte()) self._do(squid_carte())
def bl_chbre_invalide(self) : def bl_chbre_invalide(self):
from gen_confs.squid import squid_chbre from gen_confs.squid import squid_chbre
self._do(squid_chbre()) self._do(squid_chbre())
def blacklist_virus(self) : def blacklist_virus(self):
from gen_confs.squid import squid_virus from gen_confs.squid import squid_virus
self._do(squid_virus()) self._do(squid_virus())
def blacklist_warez(self) : def blacklist_warez(self):
from gen_confs.squid import squid_warez from gen_confs.squid import squid_warez
self._do(squid_warez()) self._do(squid_warez())
def blacklist_upload(self) : def blacklist_upload(self):
from gen_confs.squid import squid_upload from gen_confs.squid import squid_upload
self._do(squid_upload()) self._do(squid_upload())
def blacklist_p2p(self) : def blacklist_p2p(self):
from gen_confs.squid import squid_p2p from gen_confs.squid import squid_p2p
self._do(squid_p2p()) self._do(squid_p2p())
def blacklist_autodisc_upload(self) : def blacklist_autodisc_upload(self):
from gen_confs.squid import squid_autodisc_upload from gen_confs.squid import squid_autodisc_upload
self._do(squid_autodisc_upload()) self._do(squid_autodisc_upload())
def blacklist_autodisc_p2p(self) : def blacklist_autodisc_p2p(self):
from gen_confs.squid import squid_autodisc_p2p from gen_confs.squid import squid_autodisc_p2p
self._do(squid_autodisc_p2p()) self._do(squid_autodisc_p2p())
class ragnarok(base_reconfigure) : class ragnarok(base_reconfigure):
__restart_wifi_update = False __restart_wifi_update = False
def macip(self) : def macip(self):
# Surcharge car rien à faire ici # Surcharge car rien à faire ici
pass pass
def conf_wifi_ng(self) : def conf_wifi_ng(self):
from gen_confs.wifi_ng import conf_wifi_ng from gen_confs.wifi_ng import conf_wifi_ng
bornes = None bornes = None
if os.getenv("BORNE"): if os.getenv("BORNE"):
bornes = os.getenv("BORNE").split(",") bornes = os.getenv("BORNE").split(",")
self._do(conf_wifi_ng(bornes)) self._do(conf_wifi_ng(bornes))
def dhcp(self) : def dhcp(self):
from gen_confs.dhcpd import dhcp from gen_confs.dhcpd import dhcp
self._do(dhcp(),self._machines()) self._do(dhcp(), self._machines())
def droits(self) : def droits(self):
from gen_confs.droits import droits_openbsd from gen_confs.droits import droits_openbsd
self._do(droits_openbsd()) self._do(droits_openbsd())
class vert(base_reconfigure) : class vert(base_reconfigure):
def home(self,args) : def home(self, args):
from adherents import home from adherents import home
self._do(home(args)) self._do(home(args))
def del_user(self,args) : def del_user(self, args):
from adherents import del_user from adherents import del_user
self._do(del_user(args)) self._do(del_user(args))
def mail_bienvenue(self,mails) : def mail_bienvenue(self, mails):
from adherents import mail_bienvenue from adherents import mail_bienvenue
self._do(mail_bienvenue(mails)) self._do(mail_bienvenue(mails))
signal.signal(signal.SIGINT,signal.SIG_DFL) # Comportement normal de Ctrl-C signal.signal(signal.SIGINT, signal.SIG_DFL) # Comportement normal de Ctrl-C
remove_lock('auto_generate') remove_lock('auto_generate')
if __name__ == '__main__' : if __name__ == '__main__':
classe = eval(hostname) classe = eval(hostname)
args_autorises = ['quiet', 'remove=', 'add=', 'list' ,'help', 'reconnect'] args_autorises = ['quiet', 'remove=', 'add=', 'list', 'help', 'reconnect']
# Ajout aussi des arguments spécifiques à la machine # Ajout aussi des arguments spécifiques à la machine
for nom in dir(classe) : for nom in dir(classe):
if nom[0]!='_' : if nom[0] != '_':
if len(getargspec(getattr(classe,nom))[0])>1 : if len(getargspec(getattr(classe,nom))[0])>1:
nom += '=' nom += '='
args_autorises.append(nom) args_autorises.append(nom)
##### Options fournies ? ##### Options fournies ?
try: try:
if len(sys.argv) > 1 : if len(sys.argv) > 1:
options, arg = getopt.getopt(sys.argv[1:], 'h', args_autorises) options, arg = getopt.getopt(sys.argv[1:], 'h', args_autorises)
else : else:
options, arg = ( [],'') options, arg = ([], '')
except getopt.error, msg : except getopt.error, msg:
sys.stderr.write('%s\n' % msg) sys.stderr.write('%s\n' % msg)
sys.exit(255) sys.exit(255)
debug = 1 # défaut debug = 1 # défaut
to_do = {} to_do = {}
for opt, val in options : for opt, val in options:
if opt == '--quiet' : if opt == '--quiet':
debug = 0 debug = 0
elif opt == '--remove' : elif opt == '--remove':
for serv in val.split('&') : for serv in val.split('&'):
print 'Supression de %s' % serv print 'Supression de %s' % serv
db.services_to_restart('--%s' % serv) db.services_to_restart('--%s' % serv)
sys.exit(0) sys.exit(0)
elif opt == '--list' : elif opt == '--list':
print 'Services à redémarrer :' print 'Services à redémarrer :'
for s in db.services_to_restart() : for s in db.services_to_restart():
print '\t%s' % s print '\t%s' % s
sys.exit(0) sys.exit(0)
elif opt == '--reconnect' : elif opt == '--reconnect':
# Personnes à reconnecter # Personnes à reconnecter
print 'Recheche personnes en fin de sanction.' print 'Recheche personnes en fin de sanction.'
hier = strftime('%d/%m/%Y %H:%M'.split()[0],localtime(time()-60*60*24)) hier = strftime('%d/%m/%Y %H:%M'.split()[0], localtime(time() - 60*60*24))
c = db.search('blacklist=*,%s*' % hier) c = db.search('blacklist=*,%s*' % hier)
services = [] services = []
for a_reco in c['adherent'] + c['machine'] + c['club'] : for a_reco in c['adherent'] + c['machine'] + c['club']:
for bl in a_reco.blacklist() : for bl in a_reco.blacklist():
fin, sanction = bl.split(',')[1:3] fin, sanction = bl.split(',')[1:3]
if fin.split()[0] == hier and sanction not in services : if fin.split()[0] == hier and sanction not in services:
services.append(sanction) services.append(sanction)
for s in services : for s in services:
print "Ajout de blacklist_%s pour reconfiguration" % s print "Ajout de blacklist_%s pour reconfiguration" % s
db.services_to_restart('blacklist_%s' % s.encode()) db.services_to_restart('blacklist_%s' % s.encode())
sys.exit(0) sys.exit(0)
elif opt=='--add' : elif opt == '--add':
# Ajout d'un item dans les services à redémarrer # Ajout d'un item dans les services à redémarrer
for serv in val.split('&') : for serv in val.split('&'):
if serv.find(',')!=-1 : if serv.find(',') != -1:
serv, arg = serv.split(',',1) serv, arg = serv.split(',', 1)
arg = arg.split(',') arg = arg.split(',')
else : else:
arg = [] arg = []
print 'Ajout de %s (%s)' % ( serv, arg ) print 'Ajout de %s (%s)' % (serv, arg)
db.services_to_restart(serv, arg ) db.services_to_restart(serv, arg)
sys.exit(0) sys.exit(0)
elif opt =='-h' or opt == '--help' : elif opt == '-h' or opt == '--help':
print __doc__ % { 'prog' : sys.argv[0].split('/')[-1] , 'options' : '--'+'\n\t--'.join(args_autorises) } print __doc__ % { 'prog': sys.argv[0].split('/')[-1], 'options': '--'+'\n\t--'.join(args_autorises) }
sys.exit(0) sys.exit(0)
elif len(opt)>2 and opt[:2]=='--' : elif len(opt)>2 and opt[:2] == '--':
to_do[opt[2:]] = val.split('&') to_do[opt[2:]] = val.split('&')