From 02d6fb43ad6925f9039ef04e3af4c70abc1407fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Elliott=20B=C3=A9cue?= Date: Sun, 27 Jan 2013 00:29:04 +0100 Subject: [PATCH] =?UTF-8?q?Pour=20supprimer=20les=20trous=20au=20niveau=20?= =?UTF-8?q?des=20mid.=20=C3=80=20n'utiliser=20qu'en=20cas=20de=20n=C3=A9ce?= =?UTF-8?q?ssit=C3=A9...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ignore-this: 3128815debd47d6346cc3f15c856d700 darcs-hash:20130126232904-afe24-89e87521e474cdd26b6b8e512eee69553cd660c4.gz --- gestion/tools/move_mid.py | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 gestion/tools/move_mid.py diff --git a/gestion/tools/move_mid.py b/gestion/tools/move_mid.py new file mode 100755 index 00000000..2af2c801 --- /dev/null +++ b/gestion/tools/move_mid.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf8 -*- + +import sys +import ldap +sys.path.append('/usr/scripts/gestion') +import ldap_crans + +def move(machine, mid, conn): + """ + Déplace l'objet ldap machine vers le mid considéré + """ + + conn.conn.rename_s(machine.dn, "mid=%s" % (mid)) + machine.dn = "mid=%s,%s" % (mid, conn.base_dn) + machine._data["mid"] = [ "%s" % (mid) ] + machine.save() + +def construct_mids(from_mid, conn): + """ + Cherche un mid libre dans la base LDAP en partant de 256 + (autant ne pas mettre de garbage au milieu des serveurs + """ + + if from_mid == None or from_mid == '': + from_mid = 256 + + filtre_idn = ldap_crans.Machine.filtre_idn + liste = conn.conn.search_s(conn.base_dn, ldap.SCOPE_SUBTREE, filtre_idn) + liste_mid = [0] + + for element in liste: + r = element[0].split(',')[0] + if r[:3] != "mid": continue + liste_mid.append(int(r[4:])) + + liste_mid.sort() + return list(set(range(from_mid, 65536)).intersection(liste_mid)) + +if __name__ == "__main__": + conn = ldap_crans.crans_ldap() + liste_mid = construct_mids(256, conn) + + while True: + last = liste_mid[-1] + libre = 256 + + while libre in liste_mid: + libre = libre + 1 + + print libre, last + if libre >= last: + break + + else: + machine = conn.search('mid=%s' % (last))['machine'][0] + prev_mid = machine.id() + move(machine, libre, conn) + print "Machine mid=%s déplacée au mid=%s" % (prev_mid, libre) + + liste_mid.remove(last) + liste_mid.append(libre) + liste_mid.sort() + + print liste_mid