samba.netcmd: Formatting fixes, break lines.
[metze/samba/wip.git] / source4 / scripting / python / samba / netcmd / sites.py
1 # sites management
2 #
3 # Copyright Matthieu Patou <mat@matws.net> 2011
4 #
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.
9 #
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.
14 #
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/>.
17 #
18
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
28     )
29
30
31 class cmd_sites_create(Command):
32     """Create a new site"""
33
34     synopsis = "%prog <site> [options]"
35
36     takes_args = ["sitename"]
37
38     takes_optiongroups = {
39         "sambaopts": options.SambaOptions,
40         "versionopts": options.VersionOptions,
41         "credopts": options.CredentialsOptions,
42     }
43
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")
48
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)
53
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)))
61
62         self.outf.write("Site %s created !\n" % sitename)
63
64 class cmd_sites_delete(Command):
65     """Delete a new site"""
66
67     synopsis = "%prog <site> [options]"
68
69     takes_args = ["sitename"]
70
71     takes_optiongroups = {
72         "sambaopts": options.SambaOptions,
73         "versionopts": options.VersionOptions,
74         "credopts": options.CredentialsOptions,
75     }
76
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")
81
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)
86
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)))
95
96         self.outf.write("Site %s removed!\n" % sitename)
97
98
99
100 class cmd_sites(SuperCommand):
101     """Sites management"""
102
103     subcommands = {}
104     subcommands["create"] = cmd_sites_create()
105     subcommands["remove"] = cmd_sites_delete()