scripts/gestion/gen_confs/generate.py
bernat 31ef749810 Evite d'ouvrir 1000 connexions la base LDAP
darcs-hash:20040831153442-d1718-cb93c1c5da074b56a61db18e225e8ee9148bffd6.gz
2004-08-31 17:34:42 +02:00

161 lines
4.5 KiB
Python
Executable file

#! /usr/bin/env python
# -*- coding: iso-8859-15 -*-
import sys, signal, os, commands, getopt
sys.path.append('/usr/scripts/gestion')
from ldap_crans import crans_ldap, crans, ann_scol
from lock import *
from affich_tools import anim, cprint, OK, ERREUR, WARNING
from time import localtime
import config
signal.signal(signal.SIGINT,signal.SIG_IGN) # Pas de Ctrl-C
db = crans_ldap()
make_lock('auto_generate')
##### Options fournies ?
try :
if len(sys.argv) > 1 :
options, arg = getopt.getopt(sys.argv[1:], '', ['quiet', 'home=', 'ML-ENS=', 'droits', 'switch=' , 'dhcp', 'dns', 'firewall' ])
else :
options, arg = ( [],'')
except getopt.error, msg :
sys.stderr.write('%s\n' % msg)
sys.exit(255)
debug = 1 # défaut
to_do = {}
for opt, val in options :
if opt == '--quiet' :
debug = 0
elif len(opt)>2 and opt[:2]=='--' :
to_do[opt[2:]] = [ val ]
##### Lecture de la base LDAP si besion ce qu'il y a a faire et préparation
if not to_do :
if debug : print 'Lecture services à redémarrer dans la base LDAP'
to_do = db.services_to_restart()
auto = 1
else :
auto = 0
if debug : print 'Services à redémarrer imposés (non lecture de la base LDAP)'
inst = []
if 'home' in to_do.keys() :
if auto : db.services_to_restart('-home')
cprint('Création home','gras')
for args in to_do['home'] :
anim('\t' + args)
try :
home, uid, login = args.split(',')
os.mkdir(home, 0755)
os.chown(home, int(uid) ,config.gid)
os.mkdir(home + '/Mail', 0700)
os.chown(home + '/Mail', int(uid) ,config.gid)
status, output = commands.getstatusoutput('/usr/sbin/edquota -p pauget %s' % login )
if status :
print WARNING
if debug :
sys.stderr.write(output+'\n')
else :
print OK
except :
print ERREUR
if debug :
import traceback
traceback.print_exc()
if 'ML-ENS' in to_do.keys() :
db.services_to_restart('-ML-ENS')
cprint('Inscription ML-ENS','gras')
for mail in to_do['ML-ENS'] :
anim('\t'+mail)
status, output = commands.getstatusoutput("echo '%s' | /usr/sbin/add_members -r - com-ens >/dev/null 2>&1" % mail)
if status :
# Il y a eu une erreur
print ERREUR
if debug :
sys.stderr.write(output+'\n')
else :
print OK
if 'droits' in to_do.keys() :
db.services_to_restart('-droits')
from gen_confs.droits import droits
a = droits()
a.debug = debug
a.reconfigure()
if 'switch' in to_do.keys() :
if auto : db.services_to_restart('-switch')
from gen_confs.switchs import switch
a = switch(to_do['switch'])
a.debug = debug
a.reconfigure()
# Les services suivants ont besoin de la liste des machines
# On va donc la lire une seule fois pour leur passer ensuite
if 'firewall' in to_do.keys() :
# Quand sila et komaz liront la base LDAP
# db.services_to_restart('firewall-komaz')
# db.services_to_restart('firewall-sila')
db.services_to_restart('-firewall')
from gen_confs.firewall import firewall
inst.append(firewall())
if 'dns' in to_do.keys() :
db.services_to_restart('-dns')
from gen_confs.bind import dns
inst.append(dns())
if 'dhcp' in to_do.keys() :
from gen_confs.dhcpd import dhcp
db.services_to_restart('-dhcp')
inst.append(dhcp())
##### On fait ce qu'il reste à faire
if inst :
##### Récolte des données
cprint('Lecture base LDAP','gras')
# Machines de l'assoce
machines = crans(db.conn).machines()
# Machines des adhérents et clubs de l'année en cours
if localtime()[1] == 9:
# On est en septembre, on autorise ceux qui ont payé l'an dernier et cette année
base = db.search('(paiement=%d|paiement=%d)' % (int(ann_scol),
int(ann_scol) - 1))
else:
base = db.search('paiement=%s' % ann_scol)
base = base['adherent'] + base['club']
a = anim('\ttri machines',len(base))
for adh in base :
a.cycle()
# Adhérent ayant payé l'année en cours
if 'bloq' in adh.blacklist_actif() :
# Adhérent ignoré
continue
machines += adh.machines()
a.reinit()
print OK
#### Reconfiguration des services
for i in inst :
i.debug = debug
i.machines = machines
try :
i.reconfigure()
except :
sys.stderr.write('Erreur dans le service %s\n' % i)
if debug :
print 'Non traité ici mais signalé dans la base LDAP : \n\t', db.services_to_restart()
signal.signal(signal.SIGINT,signal.SIG_DFL) # Comportement normal de Ctrl-C
remove_lock('auto_generate')