Premier jet de rsurrection
darcs-hash:20081006215727-61eff-b956729b6a66e7806baaaf43496fbc598551a9cf.gz
This commit is contained in:
parent
067948c565
commit
af558fc447
1 changed files with 98 additions and 4 deletions
|
@ -20,15 +20,16 @@
|
|||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
|
||||
|
||||
u"""Outil pour ressusciter des machines du cimetière."""
|
||||
u"""Outil pour ressusciter des objets du cimetière."""
|
||||
|
||||
import os, sys
|
||||
import cPickle
|
||||
import dialog
|
||||
import time, pwd, ldap, ldap.modlist, random
|
||||
from time import mktime, strptime
|
||||
|
||||
sys.path.append('/usr/scripts/gestion')
|
||||
from ldap_crans import crans_ldap, MachineFixe, MachineWifi
|
||||
from ldap_crans import crans_ldap, MachineFixe, MachineWifi, Adherent, date_format
|
||||
from gest_crans import select, set_machine
|
||||
from whos import aff
|
||||
import affich_tools
|
||||
|
@ -36,6 +37,7 @@ import user_tests
|
|||
|
||||
db = crans_ldap()
|
||||
isadm = user_tests.isadm()
|
||||
cur_user = os.getenv('SUDO_USER') or pwd.getpwuid(os.getuid())[0]
|
||||
|
||||
dlg = dialog.Dialog()
|
||||
dlg.setBackgroundTitle('Ressusciter une machine')
|
||||
|
@ -47,7 +49,8 @@ def menu_principal():
|
|||
annul, choix = dlg.menu(u"Que souhaitez vous faire ?",
|
||||
choices=[("A",u"Ressusciter à partir d'un adhérent",u"Rechercher les anciennes machines d'un adhérent"),
|
||||
("D",u"Ressusciter à partir de la date",u"Rechercher à partir de la date de suppression de la machine"),
|
||||
("F",u"Ressusciter à partir d'un fichier",u"Récupérer depuis un fichier du cimetière")],
|
||||
("F",u"Ressusciter à partir d'un fichier",u"Récupérer depuis un fichier du cimetière"),
|
||||
("C",u"Ressusciter un adhérent",u"Ressusciter un adhérent depuis un fichier du cimetière")],
|
||||
item_help=1,title=u"Ressusciter")
|
||||
if annul: break
|
||||
|
||||
|
@ -132,6 +135,26 @@ def menu_principal():
|
|||
if not adh: continue
|
||||
ressuscite(adh, machine)
|
||||
|
||||
elif choix == 'C':
|
||||
# Ressusciter à partir d'un fichier
|
||||
fichier = None
|
||||
while 1:
|
||||
annul, result = dlg.inputbox(width=54, height=12,
|
||||
text=u"Vous pourrez peut-être aussi exécuter le script sur le fichier\nressuscite [le fichier]\n\nFichier : ",
|
||||
init=u"/home/cimetiere/Adherent",
|
||||
title=u"Sélection du fichier")
|
||||
if annul: break
|
||||
try:
|
||||
fichier = open(result, 'r')
|
||||
break
|
||||
except:
|
||||
dlg.msgbox(text=u"Fichier invalide",title=u"Sélection du fichier")
|
||||
continue
|
||||
if fichier != None:
|
||||
adh = cPickle.load(fichier)
|
||||
if not adh: continue
|
||||
ressuscite_adherent(adh)
|
||||
|
||||
continue
|
||||
|
||||
|
||||
|
@ -330,6 +353,77 @@ def ressuscite(adh, oldmachine):
|
|||
return True
|
||||
|
||||
|
||||
def ressuscite_adherent(old):
|
||||
"""Ressuscite une instance d'adhérent"""
|
||||
|
||||
if not isinstance(old, Adherent):
|
||||
dlg.msgbox(text=u"Ceci n'est pas un adhérent !",
|
||||
title=u"Mauvais type d'objet")
|
||||
return
|
||||
if not isadm:
|
||||
dlg.msgbox(text=u"Vous devez être nounou !",
|
||||
title=u"Droits insuffisants")
|
||||
return
|
||||
|
||||
# On contourne ldap_crans
|
||||
data = old._data
|
||||
|
||||
# Ajout d'une entrée dans l'historique
|
||||
timestamp = time.localtime()
|
||||
hist = "%s, %s : " % (time.strftime(date_format, timestamp), cur_user)
|
||||
hist += "resurrection"
|
||||
data['historique'].append(hist)
|
||||
|
||||
# Entrées à vérifier :
|
||||
# aid, canonicalAlias, homeDirectory, mail, mailAlias, uid, uidNumber
|
||||
|
||||
# On recherche les aid/uidNumber pris
|
||||
used_aid = [0]
|
||||
used_uidNumber = [0]
|
||||
for r in db.conn.search_s(Adherent.base_dn, 1, Adherent.filtre_idn):
|
||||
# r = ( dn, {} )
|
||||
d = r[1]
|
||||
if d.has_key('aid'):
|
||||
used_aid.append(int(d['aid'][0]))
|
||||
if d.has_key('uidNumber'):
|
||||
used_uidNumber.append(int(d['uidNumber'][0]))
|
||||
|
||||
# Nouvelle valeur pour aid (peut mieux faire)
|
||||
aid = max(used_aid) + random.randint(1, 10)
|
||||
data['aid'] = [str(aid)]
|
||||
dn = 'aid=%d,ou=data,dc=crans,dc=org' % aid
|
||||
|
||||
if 'posixAccount' in data['objectClass']:
|
||||
# L'adhérent avait un compte, il faut changer/vérifier ses attributs
|
||||
uidNumber = max(used_uidNumber) + random.randint(1, 10)
|
||||
data['uidNumber'] = [str(uidNumber)]
|
||||
|
||||
# Disponibilité du login (vérification sommaire)
|
||||
login = data['uid'][0]
|
||||
if os.path.exists("/home/" + login):
|
||||
no = dlg.yesno(text=u"Le compte %s existe déjà, continuer ?" % login,
|
||||
title=u"Compte existant")
|
||||
if no:
|
||||
return
|
||||
if True:
|
||||
# On croise les doigts
|
||||
try:
|
||||
modlist = ldap.modlist.addModlist(data)
|
||||
db.conn.add_s(dn, modlist)
|
||||
dlg.msgbox(text=u"Résurrection effectée ! Veuillez maintenant restaurer le home et les mails",
|
||||
title=u"Fin")
|
||||
# Au cas où l'adhérent avait des droits
|
||||
db.services_to_restart('droits')
|
||||
# On notifie après une résurrection
|
||||
db.services_to_restart('mail_modif', ['uid=' + login])
|
||||
except Exception, e:
|
||||
dlg.msgbox(text=unicode(e), title=u"Erreur")
|
||||
|
||||
else:
|
||||
print data
|
||||
raise ValueError("debug")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
menu_principal()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue