bernhard: utils/testing create_ldap_users.py,1.4,1.5

cvs at kolab.org cvs at kolab.org
Mon Feb 16 16:50:18 CET 2009


Author: bernhard

Update of /kolabrepository/utils/testing
In directory doto:/tmp/cvs-serv5592

Modified Files:
	create_ldap_users.py 
Log Message:
* Updated the docstring with more usage information that was missing
for parameters like -t --member. 
* Added parameter -o for offset
* Added optional passwd on as parameter



Index: create_ldap_users.py
===================================================================
RCS file: /kolabrepository/utils/testing/create_ldap_users.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- create_ldap_users.py	22 May 2008 17:12:03 -0000	1.4
+++ create_ldap_users.py	16 Feb 2009 15:50:16 -0000	1.5
@@ -3,7 +3,7 @@
 
 Usage:
 
-create_ldap_users.py [-h hostname] [-p port] [-u admin_dn] [-n num] basedn cmd
+create_ldap_users.py [-h hostname] [-p port] [-u admin_dn] [-n num] [-o offset] [-t type] [--member dnmember] basedn cmd [passwd]
 
 cmd may be one of
 
@@ -14,10 +14,18 @@
 
 Host and port default to localhost and the standard ldap port.
 
+The type can be "user" (default) or "group".
+
 The admin_dn should be the initial part of the dn of the admin account
 to use, that ist admin + ',cn=internal,' + base_dn will be used as the
 dn for the ldap bind.  The default is 'cn=manager'.
 
+Offset will be the start number for additions and deletion (default = 0).
+of users only.
+
+dn_member is the dn of the initial member for groups.
+
+
 Security considerations:
     The connection is not encrypted by default and thus the password
     can be sniffed.
@@ -27,6 +35,8 @@
 # http://python-ldap.sourceforge.net/
 # on debian woody it's packaged as python-ldap
 
+#import random
+import re
 import sys
 import getopt
 import ldap
@@ -34,9 +44,10 @@
 import getpass
 import time
 
-def open_ldap(ldapuri, admin_dn_part):
+def open_ldap(ldapuri, admin_dn_part, pwd = None):
     conn = ldap.initialize(ldapuri)
-    pwd = getpass.getpass("ldap bind password:")
+    if not pwd:
+        pwd = getpass.getpass("ldap bind password:")
     conn.simple_bind_s(admin_dn_part + "," + "cn=internal" + "," + base_dn,
                        pwd)
     return conn
@@ -45,15 +56,17 @@
     return conn.search_s("k=kolab," + base_dn, ldap.SCOPE_BASE,
                          filterstr='(objectClass=*)')[0][1]
 
-def set_delete_flag(conn, filterstr):
+def set_delete_flag(conn, filterstr, offset):
     hosts = fetch_kolab_info(conn)["kolabHost"]
     result = conn.search_s(base_dn, ldap.SCOPE_ONELEVEL, filterstr=filterstr)
     for dn, attrs in result:
-        print dn, hosts
-        conn.modify_s(dn, [(ldap.MOD_ADD, "kolabDeleteFlag", hosts)])
+        n = re.match(r'cn=[^,0-9]+(\d+),',dn).group(1)
+        if int(n) >= offset:
+            conn.modify_s(dn, [(ldap.MOD_ADD, "kolabDeleteFlag", hosts)])
+            print dn, hosts
 
 
-def add_user(conn, num_users):
+def add_user(conn, num_users, offset):
     kolab_info = fetch_kolab_info(conn)
 
     mail_domain = kolab_info["postfix-mydomain"][0]
@@ -67,8 +80,9 @@
         }
 
     users =  [("test%d" % n, "auto", "autotest%d" % n)
-              for n in range(num_users)]
+              for n in range(offset, num_users + offset)]
     for sn, givenName, mailuid in users:
+        #time.sleep(random.randrange(0,4))
         descr = common_attrs.copy()
         cn = givenName + " " + sn
         descr["cn"] = [cn]
@@ -80,11 +94,12 @@
         print conn.add_s(dn, ldap.modlist.addModlist(descr))
 
 
-def delete_auto_users(conn):
-    set_delete_flag(conn, "(&(objectClass=kolabInetOrgPerson)(cn=auto*))")
+def delete_auto_users(conn, offset):
+    set_delete_flag(conn, "(&(objectClass=kolabInetOrgPerson)(cn=auto*))", 
+        offset)
 
 
-def add_groups(conn, num_entries, member):
+def add_groups(conn, num_entries, member, offset=0):
     kolab_info = fetch_kolab_info(conn)
 
     mail_domain = kolab_info["postfix-mydomain"][0]
@@ -96,7 +111,7 @@
         }
 
     users =  [("testgrp%d@%s" % (n, mail_domain), "auto", "autotest%d" % n)
-              for n in range(num_entries)]
+              for n in range(offset, offset + num_entries)]
     for n in range(num_entries):
         mail = "testgrp%d@%s" % (n, mail_domain)
         descr = common_attrs.copy()
@@ -108,8 +123,8 @@
         print conn.add_s(dn, ldap.modlist.addModlist(descr))
 
 
-def delete_auto_groups(conn):
-    set_delete_flag(conn, "(&(objectClass=kolabGroupOfNames)(cn=testgrp*))")
+def delete_auto_groups(conn, offset=0):
+    set_delete_flag(conn, "(&(objectClass=kolabGroupOfNames)(cn=testgrp*))", offset)
 
 
 def main():
@@ -122,8 +137,9 @@
     num_entries = 10
     entry_type = "user"
     group_member = None
+    offset = 0
 
-    opts, args = getopt.getopt(sys.argv[1:], 'h:p:u:n:t:',
+    opts, args = getopt.getopt(sys.argv[1:], 'h:p:u:n:o:t:',
                                ["host=", "port=", "user=", "num=",
                                 "type=", "member="])
     for optchar, value in opts:
@@ -133,6 +149,8 @@
             port = int(value)
         elif optchar in ("-u", "--user"):
             admin_dn_part = value
+        elif optchar in ("-o", "--offset"):
+            offset = int(value)
         elif optchar in ("-t", "--type"):
             entry_type = value
         elif optchar in "--member":
@@ -147,11 +165,11 @@
     if len(args) < 2:
         print >>sys.stderr, "missing parameters"
         sys.exit(1)
-    elif len(args) > 2:
+    elif len(args) > 3:
         print >>sys.stderr, "too many parameters"
         sys.exit(1)
 
-    base_dn, cmd = args
+    base_dn, cmd, pwd = args
 
     if cmd not in ("add", "delete"):
         print >>sys.stderr, "unknown command", repr(cmd)
@@ -164,17 +182,17 @@
     uri = "ldap://" + hostname
 
 
-    conn = open_ldap(uri, admin_dn_part)
+    conn = open_ldap(uri, admin_dn_part, pwd)
     if entry_type == "user":
         if cmd == "add":
-            add_user(conn, num_entries)
+            add_user(conn, num_entries, offset)
         elif cmd == "delete":
-            delete_auto_users(conn)
+            delete_auto_users(conn, offset)
     elif entry_type == "group":
         if cmd == "add":
-            add_groups(conn, num_entries, group_member)
+            add_groups(conn, num_entries, group_member, offset)
         elif cmd == "delete":
-            delete_auto_groups(conn)
+            delete_auto_groups(conn, offset)
 
 
 





More information about the commits mailing list