r26522: Fix warnings on SamDB connect from Python, simplify the setup code for the...
authorJelmer Vernooij <jelmer@samba.org>
Tue, 18 Dec 2007 01:21:28 +0000 (02:21 +0100)
committerStefan Metzmacher <metze@samba.org>
Fri, 21 Dec 2007 04:51:42 +0000 (05:51 +0100)
source/scripting/python/samba/__init__.py
source/scripting/python/samba/provision.py
source/scripting/python/samba/samdb.py

index a46b148bbb6ccec1253482f25e7f45a22cd27b78..fd294952b8db1c1e6e16253d26a1ef70565dfad2 100644 (file)
@@ -75,13 +75,14 @@ class Ldb(ldb.Ldb):
         if lp is not None:
             self.set_loadparm(self, lp)
 
-        if url:
-            self.connect(url)
-
         def msg(l,text):
             print text
         self.set_debug(msg)
 
+        if url is not None:
+            self.connect(url)
+
+
     set_credentials = misc.ldb_set_credentials
     set_session_info = misc.ldb_set_session_info
     set_loadparm = misc.ldb_set_loadparm
index 0a3c183fcc9a95323b2b65b418f628e33f52d04b..2c6e50219a07777ffff9ee9a5e438fde7070a48b 100644 (file)
@@ -230,15 +230,10 @@ def setup_modify_ldif(setup_dir, ldif, subobj, ldb):
         ldb.modify(msg)
 
 
-def setup_ldb(setup_dir, ldif, session_info, credentials, subobj, lp, dbname, 
-              erase=True):
-    assert dbname is not None
-    ldb = open_ldb(session_info, credentials, lp, dbname)
+def setup_ldb(ldb, setup_dir, ldif, subobj):
     assert ldb is not None
     ldb.transaction_start()
     try:
-        if erase:
-            ldb.erase();    
         setup_add_ldif(setup_dir, ldif, subobj, ldb)
     except:
         ldb.transaction_cancel()
@@ -281,8 +276,8 @@ def provision_default_paths(lp, subobj):
     paths = ProvisionPaths()
     private_dir = lp.get("private dir")
     paths.shareconf = os.path.join(private_dir, "share.ldb")
-    paths.samdb = lp.get("sam database") or os.path.join(private_dir, "samdb.ldb")
-    paths.secrets = lp.get("secrets database") or os.path.join(private_dir, "secrets.ldb")
+    paths.samdb = os.path.join(private_dir, lp.get("sam database") or "samdb.ldb")
+    paths.secrets = os.path.join(private_dir, lp.get("secrets database") or "secrets.ldb")
     paths.templates = os.path.join(private_dir, "templates.ldb")
     paths.keytab = os.path.join(private_dir, "secrets.keytab")
     paths.dns = os.path.join(private_dir, subobj.dnsdomain + ".zone")
@@ -341,13 +336,17 @@ def provision_become_dc(setup_dir, subobj, message, paths, lp, session_info,
     subobj.fix(paths)
 
     message("Setting up templates into %s" % paths.templates)
-    setup_ldb(setup_dir, "provision_templates.ldif", session_info,
-              credentials, subobj, lp, paths.templates)
+    templates_ldb = Ldb(paths.templates, session_info=session_info,
+                        credentials=credentials, lp=lp)
+    templates_ldb.erase()
+    setup_ldb(templates_ldb, setup_dir, "provision_templates.ldif", subobj)
 
     # Also wipes the database
     message("Setting up %s partitions" % paths.samdb)
-    setup_ldb(setup_dir, "provision_partitions.ldif", session_info, 
-              credentials, subobj, lp, paths.samdb)
+    samdb = SamDB(paths.samdb, credentials=credentials, 
+                  session_info=session_info, lp=lp)
+    samdb.erase()
+    setup_ldb(samdb, setup_dir, "provision_partitions.ldif", subobj)
 
     samdb = SamDB(paths.samdb, session_info=session_info, 
                   credentials=credentials, lp=lp)
@@ -371,11 +370,12 @@ def provision_become_dc(setup_dir, subobj, message, paths, lp, session_info,
     samdb.transaction_commit()
 
     message("Setting up %s" % paths.secrets)
-    setup_ldb(setup_dir, "secrets_init.ldif", session_info, credentials, 
-              subobj, lp, paths.secrets)
-
-    setup_ldb(setup_dir, "secrets.ldif", session_info, credentials, subobj, 
-              lp, paths.secrets, False)
+    secrets_ldb = Ldb(paths.secrets, session_info=session_info,
+                      credentials=credentials, lp=lp)
+    secrets_ldb.clear()
+    setup_ldb(secrets_ldb, setup_dir, "secrets_init.ldif", subobj)
+    setup_ldb(secrets_ldb, setup_dir, "secrets.ldif", subobj)
+    setup_ldb(secrets_ldb, setup_dir, "secrets_dc.ldif", subobj)
 
 
 def provision(lp, setup_dir, subobj, message, blank, paths, session_info, 
@@ -408,14 +408,16 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info,
     # only install a new shares config db if there is none
     if not os.path.exists(paths.shareconf):
         message("Setting up share.ldb")
-        setup_ldb(setup_dir, "share.ldif", session_info, credentials, subobj, 
-                  lp, paths.shareconf)
+        share_ldb = Ldb(paths.shareconf, session_info=session_info, 
+                        credentials=credentials, lp=lp)
+        setup_ldb(share_ldb, setup_dir, "share.ldif", subobj)
 
     message("Setting up %s" % paths.secrets)
-    setup_ldb(setup_dir, "secrets_init.ldif", session_info, credentials, 
-              subobj, lp, paths.secrets)
-    setup_ldb(setup_dir, "secrets.ldif", session_info, credentials, subobj, 
-              lp, paths.secrets, False)
+    secrets_ldb = Ldb(paths.secrets, session_info=session_info, 
+                      credentials=credentials, lp=lp)
+    secrets_ldb.erase()
+    setup_ldb(secrets_ldb, setup_dir, "secrets_init.ldif", subobj)
+    setup_ldb(secrets_ldb, setup_dir, "secrets.ldif", subobj)
 
     message("Setting up registry")
     reg = registry.Registry()
@@ -427,12 +429,16 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info,
     #reg.apply_patchfile(provision_reg)
 
     message("Setting up templates into %s" % paths.templates)
-    setup_ldb(setup_dir, "provision_templates.ldif", session_info, 
-              credentials, subobj, lp, paths.templates)
+    templates_ldb = Ldb(paths.templates, session_info=session_info,
+                        credentials=credentials, lp=lp)
+    templates_ldb.erase()
+    setup_ldb(templates_ldb, setup_dir, "provision_templates.ldif", subobj)
 
     message("Setting up sam.ldb partitions")
-    setup_ldb(setup_dir, "provision_partitions.ldif", session_info, 
-              credentials, subobj, lp, paths.samdb)
+    samdb = SamDB(paths.samdb, session_info=session_info, 
+                  credentials=credentials, lp=lp)
+    samdb.erase()
+    setup_ldb(samdb, setup_dir, "provision_partitions.ldif", subobj)
 
     samdb = SamDB(paths.samdb, session_info=session_info, 
                   credentials=credentials, lp=lp)
@@ -453,7 +459,6 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info,
     samdb.transaction_commit()
 
     message("Pre-loading the Samba 4 and AD schema")
-
     samdb = SamDB(paths.samdb, session_info=session_info, 
                   credentials=credentials, lp=lp)
     samdb.set_domain_sid(subobj.domainsid)
index ce06efa3de4db6a1f0331fa64c291e1a86a0b261..e3f001deb1c9bb8e086fad5928ccfe5ae26c0f37 100644 (file)
@@ -25,10 +25,14 @@ import misc
 import ldb
 
 class SamDB(samba.Ldb):
-    def __init__(self, *args, **kwargs):
-        super(SamDB, self).__init__(*args, **kwargs)
+    def __init__(self, url=None, session_info=None, credentials=None, 
+                 modules_dir=None, lp=None):
+        super(SamDB, self).__init__(session_info=session_info, credentials=credentials,
+                                    modules_dir=modules_dir, lp=lp)
         misc.dsdb_set_global_schema(self)
         misc.ldb_register_samba_handlers(self)
+        if url:
+            self.connect(url)
 
     def add_foreign(self, domaindn, sid, desc):
         """Add a foreign security principle."""