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/>.
19 from samba import sites
20 from samba.samdb import SamDB
21 import samba.getopt as options
22 from samba.auth import system_session
23 from samba.netcmd import (
30 class cmd_sites_create(Command):
31 """Create a new site."""
33 synopsis = "%prog <site> [options]"
35 takes_args = ["sitename"]
37 takes_optiongroups = {
38 "sambaopts": options.SambaOptions,
39 "versionopts": options.VersionOptions,
40 "credopts": options.CredentialsOptions,
43 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
44 lp = sambaopts.get_loadparm()
45 creds = credopts.get_credentials(lp, fallback_machine=True)
46 url = lp.private_path("sam.ldb")
48 if not os.path.exists(url):
49 raise CommandError("secret database not found at %s " % url)
50 samdb = SamDB(url=url, session_info=system_session(),
51 credentials=creds, lp=lp)
53 samdb.transaction_start()
55 sites.create_site(samdb, samdb.get_config_basedn(), sitename)
56 samdb.transaction_commit()
57 except sites.SiteAlreadyExistsException, e:
58 samdb.transaction_cancel()
59 raise CommandError("Error while creating site %s, error: %s" %
62 self.outf.write("Site %s created !\n" % sitename)
65 class cmd_sites_delete(Command):
66 """Delete an existing site."""
68 synopsis = "%prog <site> [options]"
70 takes_args = ["sitename"]
72 takes_optiongroups = {
73 "sambaopts": options.SambaOptions,
74 "versionopts": options.VersionOptions,
75 "credopts": options.CredentialsOptions,
78 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
79 lp = sambaopts.get_loadparm()
80 creds = credopts.get_credentials(lp, fallback_machine=True)
81 url = lp.private_path("sam.ldb")
83 if not os.path.exists(url):
84 raise CommandError("secret database not found at %s " % url)
85 samdb = SamDB(url=url, session_info=system_session(),
86 credentials=creds, lp=lp)
88 samdb.transaction_start()
90 sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
91 samdb.transaction_commit()
92 except sites.SiteException, e:
93 samdb.transaction_cancel()
95 "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()