active_replication.py
1 |
#! /usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
###########################################################################
|
4 |
#
|
5 |
# Eole NG
|
6 |
# Copyright Pole de Competence Eole (Ministere Education - Academie Dijon)
|
7 |
# Licence CeCill http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
|
8 |
# eole@ac-dijon.fr
|
9 |
#
|
10 |
###########################################################################
|
11 |
import sys |
12 |
from os import system |
13 |
from time import sleep |
14 |
from os.path import isfile |
15 |
from subprocess import Popen, PIPE |
16 |
from creole import parsedico |
17 |
from creole.eosfunc import gen_random |
18 |
from creole.utils import print_red, print_green, print_orange |
19 |
from scribe.eoleldap import Ldap |
20 |
from scribe.eoletools import format_current_date |
21 |
from zephir import lib_zephir |
22 |
|
23 |
def err_msg(message): |
24 |
"""
|
25 |
Sortie avec message d'erreur
|
26 |
"""
|
27 |
print_red("Erreur : ", newline=False) |
28 |
print message
|
29 |
print
|
30 |
sys.exit(1)
|
31 |
|
32 |
def readdefault(message, valeur): |
33 |
"""
|
34 |
Question avec valeur proposée
|
35 |
"""
|
36 |
print message
|
37 |
res = raw_input('[%s] : ' % valeur) |
38 |
if not res: |
39 |
return valeur
|
40 |
else:
|
41 |
return res
|
42 |
|
43 |
def send_zephir(conf_data): |
44 |
"""
|
45 |
Envoi de la configuration à Zephir pour prise en compte par le serveur de réplication
|
46 |
"""
|
47 |
from zephir.zephir_conf.zephir_conf import id_serveur, adresse_zephir |
48 |
print_green("\nEnvoi de la configuration sur Zephir")
|
49 |
import sys, xmlrpclib, getpass, base64 |
50 |
try:
|
51 |
login = ""
|
52 |
con_ok = False
|
53 |
zephir_proxy = None
|
54 |
while not con_ok: |
55 |
# saisie des informations de connexion
|
56 |
login = raw_input("\nVeuillez saisir votre identifiant Zéphir (rien pour annuler l'envoi) :") |
57 |
if login == "": |
58 |
return False |
59 |
passwd = getpass.getpass('Mot de passe pour %s :' % login)
|
60 |
zephir_proxy = xmlrpclib.ServerProxy("https://%s:%s@%s:7080" % (login,passwd,adresse_zephir), transport=lib_zephir.TransportEole())
|
61 |
# test des identifiants
|
62 |
try:
|
63 |
res = lib_zephir.convert(zephir_proxy.get_permissions(login)) |
64 |
assert res[0] == 1 |
65 |
con_ok = True
|
66 |
except:
|
67 |
print_red('Erreur de connexion au serveur ou indentifiants incorrects')
|
68 |
# saisie du serveur de réplication
|
69 |
num_serv = ""
|
70 |
serv_ok = False
|
71 |
while not serv_ok: |
72 |
num_serv = raw_input("\nIdentifiant Zéphir du serveur de réplication (rien pour annuler l'envoi) :") |
73 |
if num_serv == "": |
74 |
return False |
75 |
# vérification des données
|
76 |
try:
|
77 |
res = lib_zephir.convert(zephir_proxy.uucp.add_replication(int(num_serv), id_serveur, base64.encodestring(conf_data)))
|
78 |
# Envoi des données de configuration
|
79 |
if res[0] != 1: |
80 |
print_red("Erreur lors de l'envoi à zephir : %s" % str(res[1])) |
81 |
else:
|
82 |
serv_ok = True
|
83 |
print_green("""Cette configuration sera prise en compte par le serveur
|
84 |
de réplication lors de sa prochaine connexion à Zéphir""")
|
85 |
|
86 |
except xmlrpclib.ProtocolError:
|
87 |
print_red('Serveur %s non retrouvé ou permissions insuffisantes' % num_serv)
|
88 |
except Exception, e: |
89 |
print_red("Erreur lors de l'envoi de la configuration à Zéphir : %s" % str(e)) |
90 |
except:
|
91 |
# erreur de récupération
|
92 |
sys.exit("Erreur d'envoi de la configuration sur zephir")
|
93 |
|
94 |
dico = parsedico.parse_dico() |
95 |
ldif = "/root/annuaire-%s.ldif" % format_current_date()
|
96 |
conf = "/root/replication-%(numero_etab)s.conf" % dico
|
97 |
replicator = "/root/.reader"
|
98 |
print
|
99 |
## test de la vestion d'openldap
|
100 |
#cmd = "dpkg-query -W -f='${Version}' 'slapd'"
|
101 |
#slapd = Popen(cmd, shell=True, stdout=PIPE).stdout.read()
|
102 |
#if 'eole' not in slapd:
|
103 |
# err_msg("Mise à jour du paquet slapd nécessaire")
|
104 |
|
105 |
# test du dictionnaire
|
106 |
if dico.get('ldap_replication', '') != 'oui': |
107 |
err_msg("""Vous devez d'abord activer la réplication ldap
|
108 |
dans l'interface de configuration du module (mode expert)
|
109 |
et reconfigurer votre serveur""")
|
110 |
|
111 |
# test de la configuration réelle
|
112 |
if file('/etc/ldap/slapd.conf').read().find('syncprov') == -1: |
113 |
err_msg("""Votre serveur est mal paramétré, lancez la commande reconfigure""")
|
114 |
|
115 |
# présence du réplicator
|
116 |
#if file(ldif).read().find('cn=replicator,o=gouv,c=fr') != -1:
|
117 |
# print_orange("Le compte de réplication existe déjà")
|
118 |
# print
|
119 |
if True: # FIXME XXX |
120 |
if not isfile(replicator): |
121 |
replicator_pwd = ''
|
122 |
else:
|
123 |
replicator_pwd = file(replicator).read().strip()
|
124 |
if replicator_pwd != '': |
125 |
print_green("Utilisation du compte de réplication existant")
|
126 |
print
|
127 |
else:
|
128 |
err_msg("impossible de récupérer le mot de passe du compte de réplication")
|
129 |
#else:
|
130 |
# print_green("Création du compte de réplication")
|
131 |
# print
|
132 |
# sleep(2)
|
133 |
# replicator_pwd = gen_random()
|
134 |
# rdn = "cn=replicator,o=gouv,c=fr"
|
135 |
# datas = [('objectClass', 'person'),
|
136 |
# ('cn', 'replicator'),
|
137 |
# ('sn', 'Compte de replication'),
|
138 |
# ('userPassword', sshaDigest(replicator_pwd))]
|
139 |
# conn = Ldap()
|
140 |
# conn.connect()
|
141 |
# conn._add(rdn, datas)
|
142 |
# conn.close()
|
143 |
# fic = file(replicator, 'w')
|
144 |
# fic.write(replicator_pwd)
|
145 |
# fic.close()
|
146 |
print_green("Génération de la configuration client")
|
147 |
adresse_scribe = readdefault("Adresse utilisée pour accéder au Scribe depuis le client", dico['adresse_ip_eth0']) |
148 |
libelle = dico['libelle_etab'].replace('%', '%%') |
149 |
numero = dico['numero_etab'].replace('%', '%%') |
150 |
acad = dico['nom_academie'].replace('%', '%%') |
151 |
print
|
152 |
print_green("Ecriture du fichier %s" % conf)
|
153 |
tmpl = """# %s (%s)
|
154 |
syncrepl rid=%%i
|
155 |
provider=ldap://%s:389
|
156 |
type=refreshAndPersist
|
157 |
interval=00:01:00:00
|
158 |
searchbase="ou=%s,ou=%s,ou=education,o=gouv,c=fr"
|
159 |
filter="(&(objectClass=ENTPerson)(!(uid=admin)))"
|
160 |
scope=sub
|
161 |
schemachecking=off
|
162 |
bindmethod=simple
|
163 |
binddn="cn=reader,o=gouv,c=fr"
|
164 |
credentials=%s
|
165 |
|
166 |
""" % (libelle, numero, adresse_scribe, numero, acad, replicator_pwd)
|
167 |
fic = file(conf, 'w') |
168 |
fic.write(tmpl) |
169 |
fic.close() |
170 |
# La configuration est générée localement, on l'envoie via zephir si possible
|
171 |
if lib_zephir.registered == 1: |
172 |
send_zephir(tmpl) |
173 |
print
|
174 |
print "fin" |