6 # Copyright Matthieu Patou <mat@matws.net> 2011
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from samba import sites
26 from samba.samdb import SamDB
27 from samba.auth import system_session
28 from samba.netcmd import (
35 class cmd_sites_create(Command):
36 """Create a new site"""
38 synopsis = "%prog <site> [options]"
40 takes_args = ["sitename"]
42 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
43 lp = sambaopts.get_loadparm()
44 creds = credopts.get_credentials(lp, fallback_machine=True)
45 url = lp.private_path("sam.ldb")
47 if not os.path.exists(url):
48 raise CommandError("secret database not found at %s " % url)
49 samdb = SamDB(url=url, session_info=system_session(),
50 credentials=creds, lp=lp)
52 samdb.transaction_start()
54 ok = sites.create_site(samdb, samdb.get_config_basedn(), sitename)
55 samdb.transaction_commit()
56 except sites.SiteAlreadyExistsException, e:
57 samdb.transaction_cancel()
58 raise CommandError("Error while creating site %s, error: %s" % (sitename, str(e)))
60 self.outf.write("Site %s created !\n" % sitename)
62 class cmd_sites_delete(Command):
63 """Delete a new site"""
65 synopsis = "%prog <site> [options]"
67 takes_args = ["sitename"]
69 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
70 lp = sambaopts.get_loadparm()
71 creds = credopts.get_credentials(lp, fallback_machine=True)
72 url = lp.private_path("sam.ldb")
74 if not os.path.exists(url):
75 raise CommandError("secret database not found at %s " % url)
76 samdb = SamDB(url=url, session_info=system_session(),
77 credentials=creds, lp=lp)
79 samdb.transaction_start()
81 ok = sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
82 samdb.transaction_commit()
83 except sites.SiteException, e:
84 samdb.transaction_cancel()
85 raise CommandError("Error while removing site %s, error: %s" % (sitename, str(e)))
87 self.outf.write("Site %s removed!\n" % sitename)
91 class cmd_sites(SuperCommand):
92 """Sites management"""
95 subcommands["create"] = cmd_sites_create()
96 subcommands["remove"] = cmd_sites_delete()