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 import samba.getopt as options
28 from samba.auth import system_session
29 from samba.netcmd import (
36 class cmd_sites_create(Command):
37 """Create a new site"""
39 synopsis = "%prog <site> [options]"
41 takes_args = ["sitename"]
43 takes_optiongroups = {
44 "sambaopts": options.SambaOptions,
45 "versionopts": options.VersionOptions,
46 "credopts": options.CredentialsOptions,
49 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
50 lp = sambaopts.get_loadparm()
51 creds = credopts.get_credentials(lp, fallback_machine=True)
52 url = lp.private_path("sam.ldb")
54 if not os.path.exists(url):
55 raise CommandError("secret database not found at %s " % url)
56 samdb = SamDB(url=url, session_info=system_session(),
57 credentials=creds, lp=lp)
59 samdb.transaction_start()
61 ok = sites.create_site(samdb, samdb.get_config_basedn(), sitename)
62 samdb.transaction_commit()
63 except sites.SiteAlreadyExistsException, e:
64 samdb.transaction_cancel()
65 raise CommandError("Error while creating site %s, error: %s" % (sitename, str(e)))
67 self.outf.write("Site %s created !\n" % sitename)
69 class cmd_sites_delete(Command):
70 """Delete a new site"""
72 synopsis = "%prog <site> [options]"
74 takes_args = ["sitename"]
76 takes_optiongroups = {
77 "sambaopts": options.SambaOptions,
78 "versionopts": options.VersionOptions,
79 "credopts": options.CredentialsOptions,
82 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
83 lp = sambaopts.get_loadparm()
84 creds = credopts.get_credentials(lp, fallback_machine=True)
85 url = lp.private_path("sam.ldb")
87 if not os.path.exists(url):
88 raise CommandError("secret database not found at %s " % url)
89 samdb = SamDB(url=url, session_info=system_session(),
90 credentials=creds, lp=lp)
92 samdb.transaction_start()
94 ok = sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
95 samdb.transaction_commit()
96 except sites.SiteException, e:
97 samdb.transaction_cancel()
98 raise CommandError("Error while removing site %s, error: %s" % (sitename, str(e)))
100 self.outf.write("Site %s removed!\n" % sitename)
104 class cmd_sites(SuperCommand):
105 """Sites management"""
108 subcommands["create"] = cmd_sites_create()
109 subcommands["remove"] = cmd_sites_delete()