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 from __future__ import print_function
20 from samba import sites, subnets
21 from samba.samdb import SamDB
22 import samba.getopt as options
23 from samba.auth import system_session
24 from samba.netcmd import (
32 class cmd_sites_create(Command):
33 """Create a new site."""
35 synopsis = "%prog <site> [options]"
37 takes_args = ["sitename"]
39 takes_optiongroups = {
40 "sambaopts": options.SambaOptions,
41 "versionopts": options.VersionOptions,
42 "credopts": options.CredentialsOptions,
46 Option("-H", "--URL", help="LDB URL for database or target server",
47 type=str, metavar="URL", dest="H"),
50 def run(self, sitename, H=None, sambaopts=None, credopts=None,
52 lp = sambaopts.get_loadparm()
53 creds = credopts.get_credentials(lp, fallback_machine=True)
54 samdb = SamDB(url=H, session_info=system_session(),
55 credentials=creds, lp=lp)
57 samdb.transaction_start()
59 sites.create_site(samdb, samdb.get_config_basedn(), sitename)
60 samdb.transaction_commit()
61 except sites.SiteAlreadyExistsException as e:
62 samdb.transaction_cancel()
63 raise CommandError("Error while creating site %s, error: %s" %
66 self.outf.write("Site %s created !\n" % sitename)
69 class cmd_sites_delete(Command):
70 """Delete an existing 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,
83 Option("-H", "--URL", help="LDB URL for database or target server",
84 type=str, metavar="URL", dest="H"),
87 def run(self, sitename, H=None, sambaopts=None, credopts=None,
89 lp = sambaopts.get_loadparm()
90 creds = credopts.get_credentials(lp, fallback_machine=True)
91 samdb = SamDB(url=H, session_info=system_session(),
92 credentials=creds, lp=lp)
94 samdb.transaction_start()
96 sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
97 samdb.transaction_commit()
98 except sites.SiteException as e:
99 samdb.transaction_cancel()
101 "Error while removing site %s, error: %s" % (sitename, str(e)))
103 self.outf.write("Site %s removed!\n" % sitename)
106 class cmd_sites_subnet_create(Command):
107 """Create a new subnet."""
108 synopsis = "%prog <subnet> <site-of-subnet> [options]"
109 takes_args = ["subnetname", "site_of_subnet"]
111 takes_optiongroups = {
112 "sambaopts": options.SambaOptions,
113 "versionopts": options.VersionOptions,
114 "credopts": options.CredentialsOptions,
118 Option("-H", "--URL", help="LDB URL for database or target server",
119 type=str, metavar="URL", dest="H"),
122 def run(self, subnetname, site_of_subnet, H=None, sambaopts=None,
123 credopts=None, versionopts=None):
124 lp = sambaopts.get_loadparm()
125 creds = credopts.get_credentials(lp)
126 samdb = SamDB(url=H, session_info=system_session(),
127 credentials=creds, lp=lp)
129 samdb.transaction_start()
131 subnets.create_subnet(samdb, samdb.get_config_basedn(), subnetname,
133 samdb.transaction_commit()
134 except subnets.SubnetException as e:
135 samdb.transaction_cancel()
136 raise CommandError("Error while creating subnet %s: %s" %
139 self.outf.write("Subnet %s created !\n" % subnetname)
142 class cmd_sites_subnet_delete(Command):
143 """Delete an existing subnet."""
145 synopsis = "%prog <subnet> [options]"
147 takes_args = ["subnetname"]
149 takes_optiongroups = {
150 "sambaopts": options.SambaOptions,
151 "versionopts": options.VersionOptions,
152 "credopts": options.CredentialsOptions,
156 Option("-H", "--URL", help="LDB URL for database or target server",
157 type=str, metavar="URL", dest="H"),
160 def run(self, subnetname, H=None, sambaopts=None, credopts=None,
162 lp = sambaopts.get_loadparm()
163 creds = credopts.get_credentials(lp)
164 samdb = SamDB(url=H, session_info=system_session(),
165 credentials=creds, lp=lp)
167 samdb.transaction_start()
169 subnets.delete_subnet(samdb, samdb.get_config_basedn(), subnetname)
170 samdb.transaction_commit()
171 except subnets.SubnetException as e:
172 samdb.transaction_cancel()
173 raise CommandError("Error while removing subnet %s, error: %s" %
176 self.outf.write("Subnet %s removed!\n" % subnetname)
179 class cmd_sites_subnet_set_site(Command):
180 """Assign a subnet to a site."""
181 synopsis = "%prog <subnet> <site-of-subnet> [options]"
182 takes_args = ["subnetname", "site_of_subnet"]
184 takes_optiongroups = {
185 "sambaopts": options.SambaOptions,
186 "versionopts": options.VersionOptions,
187 "credopts": options.CredentialsOptions,
191 Option("-H", "--URL", help="LDB URL for database or target server",
192 type=str, metavar="URL", dest="H"),
195 def run(self, subnetname, site_of_subnet, H=None, sambaopts=None,
196 credopts=None, versionopts=None):
197 lp = sambaopts.get_loadparm()
198 creds = credopts.get_credentials(lp)
199 samdb = SamDB(url=H, session_info=system_session(),
200 credentials=creds, lp=lp)
202 samdb.transaction_start()
204 subnets.set_subnet_site(samdb, samdb.get_config_basedn(),
205 subnetname, site_of_subnet)
206 samdb.transaction_commit()
207 except subnets.SubnetException as e:
208 samdb.transaction_cancel()
209 raise CommandError("Error assigning subnet %s to site %s: %s" %
210 (subnetname, site_of_subnet, e))
212 print(("Subnet %s shifted to site %s" %
213 (subnetname, site_of_subnet)), file=self.outf)
216 class cmd_sites_subnet(SuperCommand):
217 """Subnet management subcommands."""
219 "create": cmd_sites_subnet_create(),
220 "remove": cmd_sites_subnet_delete(),
221 "set-site": cmd_sites_subnet_set_site(),
224 class cmd_sites(SuperCommand):
225 """Sites management."""
227 subcommands["create"] = cmd_sites_create()
228 subcommands["remove"] = cmd_sites_delete()
229 subcommands["subnet"] = cmd_sites_subnet()