root / lib / client / gnt_network.py @ 503574ec
History | View | Annotate | Download (11.7 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2011, 2012 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
"""IP pool related commands"""
|
22 |
|
23 |
# pylint: disable=W0401,W0614
|
24 |
# W0401: Wildcard import ganeti.cli
|
25 |
# W0614: Unused import %s from wildcard import (since we need cli)
|
26 |
|
27 |
import textwrap |
28 |
|
29 |
from ganeti.cli import * |
30 |
from ganeti import constants |
31 |
from ganeti import opcodes |
32 |
from ganeti import utils |
33 |
from ganeti import errors |
34 |
|
35 |
|
36 |
#: default list of fields for L{ListNetworks}
|
37 |
_LIST_DEF_FIELDS = ["name", "network", "gateway", |
38 |
"network_type", "mac_prefix", "group_list", "tags"] |
39 |
|
40 |
|
41 |
def _HandleReservedIPs(ips): |
42 |
if ips is None: |
43 |
return None |
44 |
elif not ips: |
45 |
return []
|
46 |
else:
|
47 |
return utils.UnescapeAndSplit(ips, sep=",") |
48 |
|
49 |
|
50 |
def AddNetwork(opts, args): |
51 |
"""Add a network to the cluster.
|
52 |
|
53 |
@param opts: the command line options selected by the user
|
54 |
@type args: list
|
55 |
@param args: a list of length 1 with the network name to create
|
56 |
@rtype: int
|
57 |
@return: the desired exit code
|
58 |
|
59 |
"""
|
60 |
(network_name, ) = args |
61 |
|
62 |
if opts.network is None: |
63 |
raise errors.OpPrereqError("The --network option must be given", |
64 |
errors.ECODE_INVAL) |
65 |
|
66 |
if opts.tags is not None: |
67 |
tags = opts.tags.split(",")
|
68 |
else:
|
69 |
tags = [] |
70 |
|
71 |
reserved_ips = _HandleReservedIPs(opts.add_reserved_ips) |
72 |
|
73 |
op = opcodes.OpNetworkAdd(network_name=network_name, |
74 |
gateway=opts.gateway, |
75 |
network=opts.network, |
76 |
gateway6=opts.gateway6, |
77 |
network6=opts.network6, |
78 |
mac_prefix=opts.mac_prefix, |
79 |
network_type=opts.network_type, |
80 |
add_reserved_ips=reserved_ips, |
81 |
conflicts_check=opts.conflicts_check, |
82 |
tags=tags) |
83 |
SubmitOpCode(op, opts=opts) |
84 |
|
85 |
|
86 |
def MapNetwork(opts, args): |
87 |
"""Map a network to a node group.
|
88 |
|
89 |
@param opts: the command line options selected by the user
|
90 |
@type args: list
|
91 |
@param args: a list of length 3 with network, nodegroup, mode, physlink
|
92 |
@rtype: int
|
93 |
@return: the desired exit code
|
94 |
|
95 |
"""
|
96 |
network = args[0]
|
97 |
groups = args[1]
|
98 |
mode = args[2]
|
99 |
link = args[3]
|
100 |
|
101 |
# TODO: allow comma separated group names
|
102 |
if groups == "all": |
103 |
cl = GetClient() |
104 |
(groups, ) = cl.QueryGroups([], ["name"], False) |
105 |
else:
|
106 |
groups = [groups] |
107 |
|
108 |
for group in groups: |
109 |
op = opcodes.OpNetworkConnect(group_name=group, |
110 |
network_name=network, |
111 |
network_mode=mode, |
112 |
network_link=link, |
113 |
conflicts_check=opts.conflicts_check) |
114 |
SubmitOpCode(op, opts=opts) |
115 |
|
116 |
|
117 |
def UnmapNetwork(opts, args): |
118 |
"""Unmap a network from a node group.
|
119 |
|
120 |
@param opts: the command line options selected by the user
|
121 |
@type args: list
|
122 |
@param args: a list of length 3 with network, nodegorup
|
123 |
@rtype: int
|
124 |
@return: the desired exit code
|
125 |
|
126 |
"""
|
127 |
network = args[0]
|
128 |
groups = args[1]
|
129 |
|
130 |
#TODO: allow comma separated group names
|
131 |
if groups == "all": |
132 |
cl = GetClient() |
133 |
(groups, ) = cl.QueryGroups([], ["name"], False) |
134 |
else:
|
135 |
groups = [groups] |
136 |
|
137 |
for group in groups: |
138 |
op = opcodes.OpNetworkDisconnect(group_name=group, |
139 |
network_name=network, |
140 |
conflicts_check=opts.conflicts_check) |
141 |
SubmitOpCode(op, opts=opts) |
142 |
|
143 |
|
144 |
def ListNetworks(opts, args): |
145 |
"""List Ip pools and their properties.
|
146 |
|
147 |
@param opts: the command line options selected by the user
|
148 |
@type args: list
|
149 |
@param args: networks to list, or empty for all
|
150 |
@rtype: int
|
151 |
@return: the desired exit code
|
152 |
|
153 |
"""
|
154 |
desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) |
155 |
fmtoverride = { |
156 |
"group_list":
|
157 |
(lambda data: utils.CommaJoin("%s (%s, %s)" % (name, mode, link) |
158 |
for (name, mode, link) in data), |
159 |
False),
|
160 |
"inst_list": (",".join, False), |
161 |
"tags": (",".join, False), |
162 |
} |
163 |
|
164 |
return GenericList(constants.QR_NETWORK, desired_fields, args, None, |
165 |
opts.separator, not opts.no_headers,
|
166 |
verbose=opts.verbose, format_override=fmtoverride) |
167 |
|
168 |
|
169 |
def ListNetworkFields(opts, args): |
170 |
"""List network fields.
|
171 |
|
172 |
@param opts: the command line options selected by the user
|
173 |
@type args: list
|
174 |
@param args: fields to list, or empty for all
|
175 |
@rtype: int
|
176 |
@return: the desired exit code
|
177 |
|
178 |
"""
|
179 |
return GenericListFields(constants.QR_NETWORK, args, opts.separator,
|
180 |
not opts.no_headers)
|
181 |
|
182 |
|
183 |
def ShowNetworkConfig(_, args): |
184 |
"""Show network information.
|
185 |
|
186 |
@type args: list
|
187 |
@param args: should either be an empty list, in which case
|
188 |
we show information about all nodes, or should contain
|
189 |
a list of networks (names or UUIDs) to be queried for information
|
190 |
@rtype: int
|
191 |
@return: the desired exit code
|
192 |
|
193 |
"""
|
194 |
cl = GetClient() |
195 |
result = cl.QueryNetworks(fields=["name", "network", "gateway", |
196 |
"network6", "gateway6", |
197 |
"mac_prefix", "network_type", |
198 |
"free_count", "reserved_count", |
199 |
"map", "group_list", "inst_list", |
200 |
"external_reservations",
|
201 |
"serial_no", "uuid"], |
202 |
names=args, use_locking=False)
|
203 |
|
204 |
for (name, network, gateway, network6, gateway6,
|
205 |
mac_prefix, network_type, free_count, reserved_count, |
206 |
mapping, group_list, instances, ext_res, serial, uuid) in result:
|
207 |
size = free_count + reserved_count |
208 |
ToStdout("Network name: %s", name)
|
209 |
ToStdout("UUID: %s", uuid)
|
210 |
ToStdout("Serial number: %d", serial)
|
211 |
ToStdout(" Subnet: %s", network)
|
212 |
ToStdout(" Gateway: %s", gateway)
|
213 |
ToStdout(" IPv6 Subnet: %s", network6)
|
214 |
ToStdout(" IPv6 Gateway: %s", gateway6)
|
215 |
ToStdout(" Mac Prefix: %s", mac_prefix)
|
216 |
ToStdout(" Type: %s", network_type)
|
217 |
ToStdout(" Size: %d", size)
|
218 |
ToStdout(" Free: %d (%.2f%%)", free_count,
|
219 |
100 * float(free_count) / float(size)) |
220 |
ToStdout(" Usage map:")
|
221 |
idx = 0
|
222 |
for line in textwrap.wrap(mapping, width=64): |
223 |
ToStdout(" %s %s %d", str(idx).rjust(3), line.ljust(64), idx + 63) |
224 |
idx += 64
|
225 |
ToStdout(" (X) used (.) free")
|
226 |
|
227 |
if ext_res:
|
228 |
ToStdout(" externally reserved IPs:")
|
229 |
for line in textwrap.wrap(ext_res, width=64): |
230 |
ToStdout(" %s" % line)
|
231 |
|
232 |
if group_list:
|
233 |
ToStdout(" connected to node groups:")
|
234 |
for group in group_list: |
235 |
ToStdout(" %s", group)
|
236 |
else:
|
237 |
ToStdout(" not connected to any node group")
|
238 |
|
239 |
if instances:
|
240 |
ToStdout(" used by %d instances:", len(instances)) |
241 |
for inst in instances: |
242 |
((ips, networks), ) = cl.QueryInstances([inst], |
243 |
["nic.ips", "nic.networks"], |
244 |
use_locking=False)
|
245 |
|
246 |
l = lambda value: ", ".join(str(idx) + ":" + str(ip) |
247 |
for idx, (ip, net) in enumerate(value) |
248 |
if net == name)
|
249 |
|
250 |
ToStdout(" %s : %s", inst, l(zip(ips, networks))) |
251 |
else:
|
252 |
ToStdout(" not used by any instances")
|
253 |
|
254 |
|
255 |
def SetNetworkParams(opts, args): |
256 |
"""Modifies an IP address pool's parameters.
|
257 |
|
258 |
@param opts: the command line options selected by the user
|
259 |
@type args: list
|
260 |
@param args: should contain only one element, the node group name
|
261 |
|
262 |
@rtype: int
|
263 |
@return: the desired exit code
|
264 |
|
265 |
"""
|
266 |
|
267 |
# TODO: add "network": opts.network,
|
268 |
all_changes = { |
269 |
"gateway": opts.gateway,
|
270 |
"add_reserved_ips": _HandleReservedIPs(opts.add_reserved_ips),
|
271 |
"remove_reserved_ips": _HandleReservedIPs(opts.remove_reserved_ips),
|
272 |
"mac_prefix": opts.mac_prefix,
|
273 |
"network_type": opts.network_type,
|
274 |
"gateway6": opts.gateway6,
|
275 |
"network6": opts.network6,
|
276 |
} |
277 |
|
278 |
if all_changes.values().count(None) == len(all_changes): |
279 |
ToStderr("Please give at least one of the parameters.")
|
280 |
return 1 |
281 |
|
282 |
# pylint: disable=W0142
|
283 |
op = opcodes.OpNetworkSetParams(network_name=args[0], **all_changes)
|
284 |
|
285 |
# TODO: add feedback to user, e.g. list the modifications
|
286 |
SubmitOrSend(op, opts) |
287 |
|
288 |
|
289 |
def RemoveNetwork(opts, args): |
290 |
"""Remove an IP address pool from the cluster.
|
291 |
|
292 |
@param opts: the command line options selected by the user
|
293 |
@type args: list
|
294 |
@param args: a list of length 1 with the id of the IP address pool to remove
|
295 |
@rtype: int
|
296 |
@return: the desired exit code
|
297 |
|
298 |
"""
|
299 |
(network_name,) = args |
300 |
op = opcodes.OpNetworkRemove(network_name=network_name, force=opts.force) |
301 |
SubmitOpCode(op, opts=opts) |
302 |
|
303 |
|
304 |
commands = { |
305 |
"add": (
|
306 |
AddNetwork, ARGS_ONE_NETWORK, |
307 |
[DRY_RUN_OPT, NETWORK_OPT, GATEWAY_OPT, ADD_RESERVED_IPS_OPT, |
308 |
MAC_PREFIX_OPT, NETWORK_TYPE_OPT, NETWORK6_OPT, GATEWAY6_OPT, |
309 |
NOCONFLICTSCHECK_OPT, TAG_ADD_OPT], |
310 |
"<network_name>", "Add a new IP network to the cluster"), |
311 |
"list": (
|
312 |
ListNetworks, ARGS_MANY_NETWORKS, |
313 |
[NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT], |
314 |
"[<network_id>...]",
|
315 |
"Lists the IP networks in the cluster. The available fields can be shown"
|
316 |
" using the \"list-fields\" command (see the man page for details)."
|
317 |
" The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
|
318 |
"list-fields": (
|
319 |
ListNetworkFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
|
320 |
"Lists all available fields for networks"),
|
321 |
"info": (
|
322 |
ShowNetworkConfig, ARGS_MANY_NETWORKS, [], |
323 |
"[<network_name>...]", "Show information about the network(s)"), |
324 |
"modify": (
|
325 |
SetNetworkParams, ARGS_ONE_NETWORK, |
326 |
[DRY_RUN_OPT, SUBMIT_OPT, ADD_RESERVED_IPS_OPT, REMOVE_RESERVED_IPS_OPT, |
327 |
GATEWAY_OPT, MAC_PREFIX_OPT, NETWORK_TYPE_OPT, NETWORK6_OPT, GATEWAY6_OPT], |
328 |
"<network_name>", "Alters the parameters of a network"), |
329 |
"connect": (
|
330 |
MapNetwork, |
331 |
[ArgNetwork(min=1, max=1), ArgGroup(min=1, max=1), |
332 |
ArgChoice(min=1, max=1, choices=constants.NIC_VALID_MODES), |
333 |
ArgUnknown(min=1, max=1)], |
334 |
[NOCONFLICTSCHECK_OPT], |
335 |
"<network_name> <node_group> <mode> <link>",
|
336 |
"Map a given network to the specified node group"
|
337 |
" with given mode and link (netparams)"),
|
338 |
"disconnect": (
|
339 |
UnmapNetwork, |
340 |
[ArgNetwork(min=1, max=1), ArgGroup(min=1, max=1)], |
341 |
[NOCONFLICTSCHECK_OPT], |
342 |
"<network_name> <node_group>",
|
343 |
"Unmap a given network from a specified node group"),
|
344 |
"remove": (
|
345 |
RemoveNetwork, ARGS_ONE_NETWORK, [FORCE_OPT, DRY_RUN_OPT], |
346 |
"[--dry-run] <network_id>",
|
347 |
"Remove an (empty) network from the cluster"),
|
348 |
"list-tags": (
|
349 |
ListTags, ARGS_ONE_NETWORK, [], |
350 |
"<network_name>", "List the tags of the given network"), |
351 |
"add-tags": (
|
352 |
AddTags, [ArgNetwork(min=1, max=1), ArgUnknown()], |
353 |
[TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], |
354 |
"<network_name> tag...", "Add tags to the given network"), |
355 |
"remove-tags": (
|
356 |
RemoveTags, [ArgNetwork(min=1, max=1), ArgUnknown()], |
357 |
[TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], |
358 |
"<network_name> tag...", "Remove tags from given network"), |
359 |
} |
360 |
|
361 |
|
362 |
def Main(): |
363 |
return GenericMain(commands, override={"tag_type": constants.TAG_NETWORK}) |