4 # Copyright (C) 2011 Google Inc.
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.
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.
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
21 """IP pool related commands"""
23 # pylint: disable-msg=W0401,W0614
24 # W0401: Wildcard import ganeti.cli
25 # W0614: Unused import %s from wildcard import (since we need cli)
27 from ganeti.cli import *
28 from ganeti import constants
29 from ganeti import opcodes
30 from ganeti import utils
31 from textwrap import wrap
34 #: default list of fields for L{ListNetworks}
35 _LIST_DEF_FIELDS = ["name", "network", "gateway",
36 "network_type", "mac_prefix", "group_list", "tags"]
39 def _HandleReservedIPs(ips):
44 return utils.UnescapeAndSplit(ips, sep=",")
47 def AddNetwork(opts, args):
48 """Add a network to the cluster.
50 @param opts: the command line options selected by the user
52 @param args: a list of length 1 with the network name to create
54 @return: the desired exit code
57 (network_name, ) = args
59 if opts.tags is not None:
60 tags = opts.tags.split(",")
64 op = opcodes.OpNetworkAdd(network_name=network_name,
67 gateway6=opts.gateway6,
68 network6=opts.network6,
69 mac_prefix=opts.mac_prefix,
70 network_type=opts.network_type,
71 add_reserved_ips=_HandleReservedIPs(opts.add_reserved_ips),
73 SubmitOpCode(op, opts=opts)
76 def MapNetwork(opts, args):
77 """Map a network to a node group.
79 @param opts: the command line options selected by the user
81 @param args: a list of length 3 with network, nodegroup, mode, physlink
83 @return: the desired exit code
91 #TODO: allow comma separated group names
94 (groups, ) = cl.QueryGroups([], ['name'], False)
99 op = opcodes.OpNetworkConnect(group_name=group,
100 network_name=network,
103 conflicts_check=opts.conflicts_check)
104 SubmitOpCode(op, opts=opts)
107 def UnmapNetwork(opts, args):
108 """Unmap a network from a node group.
110 @param opts: the command line options selected by the user
112 @param args: a list of length 3 with network, nodegorup
114 @return: the desired exit code
120 #TODO: allow comma separated group names
123 (groups, ) = cl.QueryGroups([], ['name'], False)
128 op = opcodes.OpNetworkDisconnect(group_name=group,
129 network_name=network,
130 conflicts_check=opts.conflicts_check)
131 SubmitOpCode(op, opts=opts)
134 def ListNetworks(opts, args):
135 """List Ip pools and their properties.
137 @param opts: the command line options selected by the user
139 @param args: networks to list, or empty for all
141 @return: the desired exit code
144 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
146 "group_list": (",".join, False),
147 "inst_list": (",".join, False),
148 "tags": (",".join, False),
151 return GenericList(constants.QR_NETWORK, desired_fields, args, None,
152 opts.separator, not opts.no_headers,
153 verbose=opts.verbose, format_override=fmtoverride)
156 def ListNetworkFields(opts, args):
157 """List network fields.
159 @param opts: the command line options selected by the user
161 @param args: fields to list, or empty for all
163 @return: the desired exit code
166 return GenericListFields(constants.QR_NETWORK, args, opts.separator,
170 def ShowNetworkConfig(opts, args):
171 """Show network information.
173 @param opts: the command line options selected by the user
175 @param args: should either be an empty list, in which case
176 we show information about all nodes, or should contain
177 a list of networks (names or UUIDs) to be queried for information
179 @return: the desired exit code
183 result = cl.QueryNetworks(fields=["name", "network", "gateway",
184 "network6", "gateway6",
185 "mac_prefix", "network_type",
186 "free_count", "reserved_count",
187 "map", "group_list", "inst_list",
188 "external_reservations"],
189 names=args, use_locking=False)
191 for (name, network, gateway, network6, gateway6,
192 mac_prefix, network_type, free_count, reserved_count,
193 map, group_list, instances, ext_res) in result:
194 size = free_count + reserved_count
195 ToStdout("Network name: %s", name)
196 ToStdout(" subnet: %s", network)
197 ToStdout(" gateway: %s", gateway)
198 ToStdout(" subnet6: %s", network6)
199 ToStdout(" gateway6: %s", gateway6)
200 ToStdout(" mac prefix: %s", mac_prefix)
201 ToStdout(" type: %s", network_type)
202 ToStdout(" size: %d", size)
203 ToStdout(" free: %d (%.2f%%)", free_count,
204 100 * float(free_count)/float(size))
205 ToStdout(" usage map:")
207 for line in wrap(map, width=64):
208 ToStdout(" %s %s %d", str(idx).rjust(3), line.ljust(64), idx + 63)
210 ToStdout(" (X) used (.) free")
213 ToStdout(" externally reserved IPs:")
214 for line in wrap(ext_res, width=64):
215 ToStdout(" %s" % line)
218 ToStdout(" connected to node groups:")
219 for group in group_list:
220 ToStdout(" %s", group)
222 ToStdout(" not connected to any node group")
225 ToStdout(" used by %d instances:", len(instances))
226 for inst in instances:
227 ((ips, networks), ) = cl.QueryInstances([inst],
228 ["nic.ips", "nic.networks"],
231 l = lambda value: ", ".join(`idx`+":"+str(ip)
232 for idx, (ip, net) in enumerate(value)
235 ToStdout(" %s : %s", inst, l(zip(ips,networks)))
237 ToStdout(" not used by any instances")
240 def SetNetworkParams(opts, args):
241 """Modifies an IP address pool's parameters.
243 @param opts: the command line options selected by the user
245 @param args: should contain only one element, the node group name
248 @return: the desired exit code
252 # TODO: add "network": opts.network,
254 "gateway": opts.gateway,
255 "add_reserved_ips": _HandleReservedIPs(opts.add_reserved_ips),
256 "remove_reserved_ips": _HandleReservedIPs(opts.remove_reserved_ips),
257 "mac_prefix": opts.mac_prefix,
258 "network_type": opts.network_type,
259 "gateway6": opts.gateway6,
260 "network6": opts.network6,
263 if all_changes.values().count(None) == len(all_changes):
264 ToStderr("Please give at least one of the parameters.")
267 op = opcodes.OpNetworkSetParams(network_name=args[0],
268 # pylint: disable-msg=W0142
271 # TODO: add feedback to user, e.g. list the modifications
272 SubmitOrSend(op, opts)
275 def RemoveNetwork(opts, args):
276 """Remove an IP address pool from the cluster.
278 @param opts: the command line options selected by the user
280 @param args: a list of length 1 with the id of the IP address pool to remove
282 @return: the desired exit code
285 (network_name,) = args
286 op = opcodes.OpNetworkRemove(network_name=network_name, force=opts.force)
287 SubmitOpCode(op, opts=opts)
292 AddNetwork, ARGS_ONE_NETWORK,
293 [DRY_RUN_OPT, NETWORK_OPT, GATEWAY_OPT, ADD_RESERVED_IPS_OPT,
294 MAC_PREFIX_OPT, NETWORK_TYPE_OPT, NETWORK6_OPT, GATEWAY6_OPT,
295 TAG_ADD_OPT, NOCONFLICTSCHECK_OPT],
296 "<network_name>", "Add a new IP network to the cluster"),
298 ListNetworks, ARGS_MANY_NETWORKS,
299 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT],
301 "Lists the IP networks in the cluster. The available fields can be shown"
302 " using the \"list-fields\" command (see the man page for details)."
303 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
305 ListNetworkFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
306 "Lists all available fields for networks"),
308 ShowNetworkConfig, ARGS_MANY_NETWORKS, [],
309 "[<network_name>...]", "Show information about the network(s)"),
311 SetNetworkParams, ARGS_ONE_NETWORK,
312 [DRY_RUN_OPT, SUBMIT_OPT, ADD_RESERVED_IPS_OPT, REMOVE_RESERVED_IPS_OPT,
313 GATEWAY_OPT, MAC_PREFIX_OPT, NETWORK_TYPE_OPT, NETWORK6_OPT, GATEWAY6_OPT],
314 "<network_name>", "Alters the parameters of a network"),
317 [ArgNetwork(min=1, max=1), ArgGroup(min=1, max=1),
318 ArgUnknown(min=1, max=1), ArgUnknown(min=1, max=1)],
319 [NOCONFLICTSCHECK_OPT],
320 "<network_name> <node_group> <mode> <link>",
321 "Map a given network to the specified node group"
322 " with given mode and link (netparams)"),
325 [ArgNetwork(min=1, max=1), ArgGroup(min=1, max=1)],
326 [NOCONFLICTSCHECK_OPT],
327 "<network_name> <node_group>",
328 "Unmap a given network from a specified node group"),
330 RemoveNetwork, ARGS_ONE_NETWORK, [FORCE_OPT, DRY_RUN_OPT],
331 "[--dry-run] <network_id>",
332 "Remove an (empty) network from the cluster"),
334 ListTags, ARGS_ONE_NETWORK, [],
335 "<network_name>", "List the tags of the given network"),
337 AddTags, [ArgNetwork(min=1, max=1), ArgUnknown()],
338 [TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT],
339 "<network_name> tag...", "Add tags to the given network"),
341 RemoveTags, [ArgNetwork(min=1, max=1), ArgUnknown()],
342 [TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT],
343 "<network_name> tag...", "Remove tags from given network"),
348 return GenericMain(commands, override={"tag_type": constants.TAG_NETWORK})