s4-python: Remove execute flag from netcmd scripts.
[samba.git] / source4 / scripting / python / samba / netcmd / sites.py
1
2 #!/usr/bin/env python
3 #
4 # sites management
5 #
6 # Copyright Matthieu Patou <mat@matws.net> 2011
7 #
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.
12 #
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.
17 #
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/>.
20 #
21
22
23
24 import os
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 (
30     Command,
31     CommandError,
32     SuperCommand
33     )
34
35
36 class cmd_sites_create(Command):
37     """Create a new site"""
38
39     synopsis = "%prog <site> [options]"
40
41     takes_args = ["sitename"]
42
43     takes_optiongroups = {
44         "sambaopts": options.SambaOptions,
45         "versionopts": options.VersionOptions,
46         "credopts": options.CredentialsOptions,
47     }
48
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")
53
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)
58
59         samdb.transaction_start()
60         try:
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)))
66
67         self.outf.write("Site %s created !\n" % sitename)
68
69 class cmd_sites_delete(Command):
70     """Delete a new site"""
71
72     synopsis = "%prog <site> [options]"
73
74     takes_args = ["sitename"]
75
76     takes_optiongroups = {
77         "sambaopts": options.SambaOptions,
78         "versionopts": options.VersionOptions,
79         "credopts": options.CredentialsOptions,
80     }
81
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")
86
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)
91
92         samdb.transaction_start()
93         try:
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)))
99
100         self.outf.write("Site %s removed!\n" % sitename)
101
102
103
104 class cmd_sites(SuperCommand):
105     """Sites management"""
106
107     subcommands = {}
108     subcommands["create"] = cmd_sites_create()
109     subcommands["remove"] = cmd_sites_delete()