[SCM] Samba Shared Repository - branch master updated
Matthieu Patou
mat at samba.org
Wed Nov 2 08:15:02 MDT 2011
The branch, master has been updated
via dfd341a samba-tool: add subcommand for sites manipulation
via 3770389 s4-selftest: add unit tests for sites's function in python
via 66a34d8 s4-python: add function to manipulate sites in python
from 251209b s4-resolver: make it work back with ipv4 only DNS records
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master
- Log -----------------------------------------------------------------
commit dfd341a2fdd017a0e3248327a129a2a5c41e2636
Author: Matthieu Patou <mat at matws.net>
Date: Wed Nov 2 13:32:33 2011 +0100
samba-tool: add subcommand for sites manipulation
Autobuild-User: Matthieu Patou <mat at samba.org>
Autobuild-Date: Wed Nov 2 15:14:32 CET 2011 on sn-devel-104
commit 37703892bd59637db129bf1342e34be83c77cebe
Author: Matthieu Patou <mat at matws.net>
Date: Wed Nov 2 13:31:40 2011 +0100
s4-selftest: add unit tests for sites's function in python
commit 66a34d86de6237272c7bc99176dbcfad856345f3
Author: Matthieu Patou <mat at matws.net>
Date: Wed Nov 2 13:30:59 2011 +0100
s4-python: add function to manipulate sites in python
-----------------------------------------------------------------------
Summary of changes:
source4/dsdb/tests/python/sites.py | 125 ++++++++++++++++++++++++
source4/scripting/python/samba/netcmd/main.py | 2 +
source4/scripting/python/samba/netcmd/sites.py | 96 ++++++++++++++++++
source4/scripting/python/samba/sites.py | 63 ++++++++++++
source4/selftest/tests.py | 1 +
5 files changed, 287 insertions(+), 0 deletions(-)
create mode 100644 source4/dsdb/tests/python/sites.py
create mode 100644 source4/scripting/python/samba/netcmd/sites.py
create mode 100644 source4/scripting/python/samba/sites.py
Changeset truncated at 500 lines:
diff --git a/source4/dsdb/tests/python/sites.py b/source4/dsdb/tests/python/sites.py
new file mode 100644
index 0000000..d3f5c57
--- /dev/null
+++ b/source4/dsdb/tests/python/sites.py
@@ -0,0 +1,125 @@
+#!/usr/bin/env python
+#
+# Unit tests for sites manipulation in samba
+# Copyright (C) Matthieu Patou <mat at matws.net> 2011
+#
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import optparse
+import sys
+sys.path.insert(0, "bin/python")
+import samba
+samba.ensure_external_module("testtools", "testtools")
+samba.ensure_external_module("subunit", "subunit/python")
+
+import samba.getopt as options
+from samba import sites
+from samba.auth import system_session
+from samba.samdb import SamDB
+import samba.tests
+from samba.dcerpc import security
+from subunit.run import SubunitTestRunner
+import unittest
+
+parser = optparse.OptionParser("dirsync.py [options] <host>")
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
+parser.add_option_group(options.VersionOptions(parser))
+
+# use command line creds if available
+credopts = options.CredentialsOptions(parser)
+parser.add_option_group(credopts)
+opts, args = parser.parse_args()
+
+if len(args) < 1:
+ parser.print_usage()
+ sys.exit(1)
+
+host = args[0]
+if not "://" in host:
+ ldaphost = "ldap://%s" % host
+ ldapshost = "ldaps://%s" % host
+else:
+ ldaphost = host
+ start = host.rindex("://")
+ host = host.lstrip(start+3)
+
+lp = sambaopts.get_loadparm()
+creds = credopts.get_credentials(lp)
+
+#
+# Tests start here
+#
+
+class SitesBaseTests(samba.tests.TestCase):
+
+ def setUp(self):
+ super(SitesBaseTests, self).setUp()
+ self.ldb_admin = ldb
+ self.base_dn = ldb.domain_dn()
+ self.domain_sid = security.dom_sid(ldb.get_domain_sid())
+ self.configuration_dn = self.ldb_admin.get_config_basedn().get_linearized()
+
+ def get_user_dn(self, name):
+ return "CN=%s,CN=Users,%s" % (name, self.base_dn)
+
+
+#tests on sites
+class SimpleSitesTests(SitesBaseTests):
+
+
+ def test_create(self):
+ """test creation of 1 site"""
+
+ self.ldb_admin.transaction_start()
+ ok = sites.create_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
+ "testsamba")
+ self.ldb_admin.transaction_commit()
+ self.assertTrue(ok)
+ ok = False
+ try:
+ ok = sites.create_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
+ "testsamba")
+ self.assertFalse(ok)
+ except:
+ self.assertFalse(ok)
+
+ def test_delete(self):
+ """test creation of 1 site"""
+
+ self.ldb_admin.transaction_start()
+ ok = sites.delete_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
+ "testsamba")
+ self.ldb_admin.transaction_commit()
+ self.assertTrue(ok)
+ ok = False
+ try:
+ ok = sites.delete_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
+ "testsamba")
+ self.assertFalse(ok)
+ except:
+ self.assertFalse(ok)
+
+
+ldb = SamDB(ldapshost, credentials=creds, session_info=system_session(lp), lp=lp)
+
+runner = SubunitTestRunner()
+rc = 0
+
+if not runner.run(unittest.makeSuite(SimpleSitesTests)).wasSuccessful():
+ rc = 1
+
+sys.exit(rc)
diff --git a/source4/scripting/python/samba/netcmd/main.py b/source4/scripting/python/samba/netcmd/main.py
index 61bbcf1..3bdbced 100644
--- a/source4/scripting/python/samba/netcmd/main.py
+++ b/source4/scripting/python/samba/netcmd/main.py
@@ -32,6 +32,7 @@ from samba.netcmd.group import cmd_group
from samba.netcmd.ldapcmp import cmd_ldapcmp
from samba.netcmd.ntacl import cmd_ntacl
from samba.netcmd.rodc import cmd_rodc
+from samba.netcmd.sites import cmd_sites
from samba.netcmd.spn import cmd_spn
from samba.netcmd.testparm import cmd_testparm
from samba.netcmd.time import cmd_time
@@ -55,6 +56,7 @@ class cmd_sambatool(SuperCommand):
subcommands["ldapcmp"] = cmd_ldapcmp()
subcommands["ntacl"] = cmd_ntacl()
subcommands["rodc"] = cmd_rodc()
+ subcommands["sites"] = cmd_sites()
subcommands["spn"] = cmd_spn()
subcommands["testparm"] = cmd_testparm()
subcommands["time"] = cmd_time()
diff --git a/source4/scripting/python/samba/netcmd/sites.py b/source4/scripting/python/samba/netcmd/sites.py
new file mode 100644
index 0000000..a63b524
--- /dev/null
+++ b/source4/scripting/python/samba/netcmd/sites.py
@@ -0,0 +1,96 @@
+
+#!/usr/bin/env python
+#
+# sites management
+#
+# Copyright Matthieu Patou <mat at matws.net> 2011
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+
+
+import os
+from samba import sites
+from samba import Ldb
+from samba.auth import system_session
+from samba.netcmd import (
+ Command,
+ CommandError,
+ SuperCommand
+ )
+
+
+class cmd_sites_create(Command):
+ """Create a new site"""
+
+ synopsis = "%prog <site> [options]"
+
+ takes_args = ["sitename"]
+
+ def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
+ lp = sambaopts.get_loadparm()
+ creds = credopts.get_credentials(lp, fallback_machine=True)
+ name = "sam.ldb"
+ path = lp.get("private dir")
+ url = os.path.join(path, name)
+ if not os.path.exists(url):
+ raise CommandError("secret database not found at %s " % url)
+ samdb = Ldb(url=url, session_info=system_session(),
+ credentials=creds, lp=lp)
+
+ samdb.transaction_start()
+ ok = sites.create_site(samdb, samdb.get_config_basedn(), sitename)
+ samdb.transaction_commit()
+
+ if not ok:
+ raise CommandError("Error while creating site %s" % sitename)
+
+ self.outf.write("Site %s created !\n" % sitename)
+
+class cmd_sites_delete(Command):
+ """Delete a new site"""
+
+ synopsis = "%prog <site> [options]"
+
+ takes_args = ["sitename"]
+
+ def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
+ lp = sambaopts.get_loadparm()
+ creds = credopts.get_credentials(lp, fallback_machine=True)
+ name = "sam.ldb"
+ path = lp.get("private dir")
+ url = os.path.join(path, name)
+ if not os.path.exists(url):
+ raise CommandError("secret database not found at %s " % url)
+ samdb = Ldb(url=url, session_info=system_session(),
+ credentials=creds, lp=lp)
+
+ samdb.transaction_start()
+ ok = sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
+ samdb.transaction_commit()
+
+ if not ok:
+ raise CommandError("Error while creating site %s" % sitename)
+
+ self.outf.write("Site %s removed!\n" % sitename)
+
+
+
+class cmd_sites(SuperCommand):
+ """Sites management"""
+
+ subcommands = {}
+ subcommands["create"] = cmd_sites_create()
+ subcommands["remove"] = cmd_sites_delete()
diff --git a/source4/scripting/python/samba/sites.py b/source4/scripting/python/samba/sites.py
new file mode 100644
index 0000000..d1d0e75
--- /dev/null
+++ b/source4/scripting/python/samba/sites.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+#
+# python site manipulation code
+# Copyright Matthieu Patou <mat at matws.net> 2011
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+"""Manipulating sites."""
+
+import ldb
+from ldb import FLAG_MOD_ADD
+
+def create_site(samdb, configDn, siteName):
+ ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
+ expression='(&(objectclass=Site)(cn=%s))' % siteName)
+ if len(ret) != 0:
+ raise Exception('A site with the name %s already exists' % siteName)
+
+ m = ldb.Message()
+ m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
+ m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
+
+ samdb.add(m)
+
+ m2 = ldb.Message()
+ m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
+ m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
+
+ samdb.add(m2)
+
+ m3 = ldb.Message()
+ m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
+ m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
+
+ samdb.add(m3)
+
+ return True
+
+def delete_site(samdb, configDn, siteName):
+
+ dnsite = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
+ dnserver = ldb.Dn(samdb, "Cn=Servers,%s" % str(dnsite))
+
+ ret = samdb.search(base=dnserver, scope=ldb.SCOPE_ONELEVEL,
+ expression='(objectclass=server)')
+ if len(ret) != 0:
+ raise Exception('Site %s still has servers in it, move them before removal' % siteName)
+
+ samdb.delete(dnsite, ["tree_delete:0"])
+
+ return True
diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py
index 6e61b73..ba19020 100755
--- a/source4/selftest/tests.py
+++ b/source4/selftest/tests.py
@@ -431,6 +431,7 @@ plantestsuite("samba4.sam.python(dc)", "dc", [python, os.path.join(samba4srcdir,
plansambapythontestsuite("samba4.schemaInfo.python(dc)", "dc", os.path.join(samba4srcdir, 'dsdb/tests/python'), 'dsdb_schema_info', extra_args=['-U"$DOMAIN/$DC_USERNAME%$DC_PASSWORD"'])
plantestsuite("samba4.urgent_replication.python(dc)", "dc", [python, os.path.join(samba4srcdir, "dsdb/tests/python/urgent_replication.py"), '$PREFIX_ABS/dc/private/sam.ldb'], allow_empty_output=True)
plantestsuite("samba4.ldap.dirsync.python(dc)", "dc", [python, os.path.join(samba4srcdir, "dsdb/tests/python/dirsync.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '-W', '$DOMAIN'])
+plantestsuite("samba4.ldap.sites.python(dc)", "dc", [python, os.path.join(samba4srcdir, "dsdb/tests/python/sites.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '-W', '$DOMAIN'])
for env in ["dc", "fl2000dc", "fl2003dc", "fl2008r2dc"]:
plantestsuite("samba4.ldap_schema.python(%s)" % env, env, [python, os.path.join(samba4srcdir, "dsdb/tests/python/ldap_schema.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '-W', '$DOMAIN'])
plantestsuite("samba4.ldap.possibleInferiors.python(%s)" % env, env, [python, os.path.join(samba4srcdir, "dsdb/samdb/ldb_modules/tests/possibleinferiors.py"), "ldap://$SERVER", '-U"$USERNAME%$PASSWORD"', "-W", "$DOMAIN"])
--
Samba Shared Repository
More information about the samba-cvs
mailing list