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