samba-tool: Fix typo in usage.
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / netcmd / sites.py
blobae63dea10b74d9c6656483b39d2fb2d30b6619ba
1 # sites management
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 import os
20 from samba import sites
21 from samba.samdb import SamDB
22 import samba.getopt as options
23 from samba.auth import system_session
24 from samba.netcmd import (
25 Command,
26 CommandError,
27 SuperCommand
31 class cmd_sites_create(Command):
32 """Create a new site"""
34 synopsis = "%prog <site> [options]"
36 takes_args = ["sitename"]
38 takes_optiongroups = {
39 "sambaopts": options.SambaOptions,
40 "versionopts": options.VersionOptions,
41 "credopts": options.CredentialsOptions,
44 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
45 lp = sambaopts.get_loadparm()
46 creds = credopts.get_credentials(lp, fallback_machine=True)
47 url = lp.private_path("sam.ldb")
49 if not os.path.exists(url):
50 raise CommandError("secret database not found at %s " % url)
51 samdb = SamDB(url=url, session_info=system_session(),
52 credentials=creds, lp=lp)
54 samdb.transaction_start()
55 try:
56 ok = sites.create_site(samdb, samdb.get_config_basedn(), sitename)
57 samdb.transaction_commit()
58 except sites.SiteAlreadyExistsException, e:
59 samdb.transaction_cancel()
60 raise CommandError("Error while creating site %s, error: %s" % (sitename, str(e)))
62 self.outf.write("Site %s created !\n" % sitename)
64 class cmd_sites_delete(Command):
65 """Delete an existing site"""
67 synopsis = "%prog <site> [options]"
69 takes_args = ["sitename"]
71 takes_optiongroups = {
72 "sambaopts": options.SambaOptions,
73 "versionopts": options.VersionOptions,
74 "credopts": options.CredentialsOptions,
77 def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
78 lp = sambaopts.get_loadparm()
79 creds = credopts.get_credentials(lp, fallback_machine=True)
80 url = lp.private_path("sam.ldb")
82 if not os.path.exists(url):
83 raise CommandError("secret database not found at %s " % url)
84 samdb = SamDB(url=url, session_info=system_session(),
85 credentials=creds, lp=lp)
87 samdb.transaction_start()
88 try:
89 ok = sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
90 samdb.transaction_commit()
91 except sites.SiteException, e:
92 samdb.transaction_cancel()
93 raise CommandError(
94 "Error while removing site %s, error: %s" % (sitename, str(e)))
96 self.outf.write("Site %s removed!\n" % sitename)
100 class cmd_sites(SuperCommand):
101 """Sites management"""
103 subcommands = {}
104 subcommands["create"] = cmd_sites_create()
105 subcommands["remove"] = cmd_sites_delete()