49a9f9d189010643537177b8027621098d711b41
[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
20
21 import os
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 (
27     Command,
28     CommandError,
29     SuperCommand
30     )
31
32
33 class cmd_sites_create(Command):
34     """Create a new site"""
35
36     synopsis = "%prog <site> [options]"
37
38     takes_args = ["sitename"]
39
40     takes_optiongroups = {
41         "sambaopts": options.SambaOptions,
42         "versionopts": options.VersionOptions,
43         "credopts": options.CredentialsOptions,
44     }
45
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")
50
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)
55
56         samdb.transaction_start()
57         try:
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)))
63
64         self.outf.write("Site %s created !\n" % sitename)
65
66 class cmd_sites_delete(Command):
67     """Delete a new site"""
68
69     synopsis = "%prog <site> [options]"
70
71     takes_args = ["sitename"]
72
73     takes_optiongroups = {
74         "sambaopts": options.SambaOptions,
75         "versionopts": options.VersionOptions,
76         "credopts": options.CredentialsOptions,
77     }
78
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")
83
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)
88
89         samdb.transaction_start()
90         try:
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)))
96
97         self.outf.write("Site %s removed!\n" % sitename)
98
99
100
101 class cmd_sites(SuperCommand):
102     """Sites management"""
103
104     subcommands = {}
105     subcommands["create"] = cmd_sites_create()
106     subcommands["remove"] = cmd_sites_delete()