root / lib / cmdlib / network.py @ 402df383
History | View | Annotate | Download (21.3 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 |
|
22 |
"""Logical units dealing with networks."""
|
23 |
|
24 |
from ganeti import constants |
25 |
from ganeti import errors |
26 |
from ganeti import locking |
27 |
from ganeti import network |
28 |
from ganeti import objects |
29 |
from ganeti import qlang |
30 |
from ganeti import query |
31 |
from ganeti import utils |
32 |
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU, QueryBase |
33 |
from ganeti.cmdlib.common import ShareAll, CheckNodeGroupInstances |
34 |
|
35 |
|
36 |
def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6, |
37 |
mac_prefix, tags): |
38 |
"""Builds network related env variables for hooks
|
39 |
|
40 |
This builds the hook environment from individual variables.
|
41 |
|
42 |
@type name: string
|
43 |
@param name: the name of the network
|
44 |
@type subnet: string
|
45 |
@param subnet: the ipv4 subnet
|
46 |
@type gateway: string
|
47 |
@param gateway: the ipv4 gateway
|
48 |
@type network6: string
|
49 |
@param network6: the ipv6 subnet
|
50 |
@type gateway6: string
|
51 |
@param gateway6: the ipv6 gateway
|
52 |
@type mac_prefix: string
|
53 |
@param mac_prefix: the mac_prefix
|
54 |
@type tags: list
|
55 |
@param tags: the tags of the network
|
56 |
|
57 |
"""
|
58 |
env = {} |
59 |
if name:
|
60 |
env["NETWORK_NAME"] = name
|
61 |
if subnet:
|
62 |
env["NETWORK_SUBNET"] = subnet
|
63 |
if gateway:
|
64 |
env["NETWORK_GATEWAY"] = gateway
|
65 |
if network6:
|
66 |
env["NETWORK_SUBNET6"] = network6
|
67 |
if gateway6:
|
68 |
env["NETWORK_GATEWAY6"] = gateway6
|
69 |
if mac_prefix:
|
70 |
env["NETWORK_MAC_PREFIX"] = mac_prefix
|
71 |
if tags:
|
72 |
env["NETWORK_TAGS"] = " ".join(tags) |
73 |
|
74 |
return env
|
75 |
|
76 |
|
77 |
class LUNetworkAdd(LogicalUnit): |
78 |
"""Logical unit for creating networks.
|
79 |
|
80 |
"""
|
81 |
HPATH = "network-add"
|
82 |
HTYPE = constants.HTYPE_NETWORK |
83 |
REQ_BGL = False
|
84 |
|
85 |
def BuildHooksNodes(self): |
86 |
"""Build hooks nodes.
|
87 |
|
88 |
"""
|
89 |
mn = self.cfg.GetMasterNode()
|
90 |
return ([mn], [mn])
|
91 |
|
92 |
def CheckArguments(self): |
93 |
if self.op.mac_prefix: |
94 |
self.op.mac_prefix = \
|
95 |
utils.NormalizeAndValidateThreeOctetMacPrefix(self.op.mac_prefix)
|
96 |
|
97 |
def ExpandNames(self): |
98 |
self.network_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId()) |
99 |
|
100 |
if self.op.conflicts_check: |
101 |
self.share_locks[locking.LEVEL_NODE] = 1 |
102 |
self.share_locks[locking.LEVEL_NODE_ALLOC] = 1 |
103 |
self.needed_locks = {
|
104 |
locking.LEVEL_NODE: locking.ALL_SET, |
105 |
locking.LEVEL_NODE_ALLOC: locking.ALL_SET, |
106 |
} |
107 |
else:
|
108 |
self.needed_locks = {}
|
109 |
|
110 |
self.add_locks[locking.LEVEL_NETWORK] = self.network_uuid |
111 |
|
112 |
def CheckPrereq(self): |
113 |
if self.op.network is None: |
114 |
raise errors.OpPrereqError("Network must be given", |
115 |
errors.ECODE_INVAL) |
116 |
|
117 |
try:
|
118 |
existing_uuid = self.cfg.LookupNetwork(self.op.network_name) |
119 |
except errors.OpPrereqError:
|
120 |
pass
|
121 |
else:
|
122 |
raise errors.OpPrereqError("Desired network name '%s' already exists as a" |
123 |
" network (UUID: %s)" %
|
124 |
(self.op.network_name, existing_uuid),
|
125 |
errors.ECODE_EXISTS) |
126 |
|
127 |
# Check tag validity
|
128 |
for tag in self.op.tags: |
129 |
objects.TaggableObject.ValidateTag(tag) |
130 |
|
131 |
self.nobj = objects.Network(name=self.op.network_name, |
132 |
network=self.op.network,
|
133 |
gateway=self.op.gateway,
|
134 |
network6=self.op.network6,
|
135 |
gateway6=self.op.gateway6,
|
136 |
mac_prefix=self.op.mac_prefix,
|
137 |
uuid=self.network_uuid)
|
138 |
|
139 |
# Initialize the associated address pool
|
140 |
try:
|
141 |
self.pool = network.AddressPool.InitializeNetwork(self.nobj) |
142 |
except errors.AddressPoolError, err:
|
143 |
raise errors.OpPrereqError("Cannot create IP address pool for network" |
144 |
" '%s': %s" % (self.op.network_name, err)) |
145 |
|
146 |
def BuildHooksEnv(self): |
147 |
"""Build hooks env.
|
148 |
|
149 |
"""
|
150 |
args = { |
151 |
"name": self.op.network_name, |
152 |
"subnet": self.op.network, |
153 |
"gateway": self.op.gateway, |
154 |
"network6": self.op.network6, |
155 |
"gateway6": self.op.gateway6, |
156 |
"mac_prefix": self.op.mac_prefix, |
157 |
"tags": self.op.tags, |
158 |
} |
159 |
return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 |
160 |
|
161 |
def Exec(self, feedback_fn): |
162 |
"""Add the ip pool to the cluster.
|
163 |
|
164 |
"""
|
165 |
# Check if we need to reserve the nodes and the cluster master IP
|
166 |
# These may not be allocated to any instances in routed mode, as
|
167 |
# they wouldn't function anyway.
|
168 |
if self.op.conflicts_check: |
169 |
for node in self.cfg.GetAllNodesInfo().values(): |
170 |
for ip in [node.primary_ip, node.secondary_ip]: |
171 |
if self.pool.Contains(ip): |
172 |
self.pool.Reserve(ip, True) |
173 |
self.LogInfo("Reserved IP address of node '%s' (%s)", node.name, ip) |
174 |
|
175 |
master_ip = self.cfg.GetClusterInfo().master_ip
|
176 |
if self.pool.Contains(master_ip): |
177 |
self.pool.Reserve(master_ip, True) |
178 |
self.LogInfo("Reserved cluster master IP address (%s)", master_ip) |
179 |
|
180 |
if self.op.add_reserved_ips: |
181 |
for ip in self.op.add_reserved_ips: |
182 |
try:
|
183 |
self.pool.Reserve(ip, external=True) |
184 |
except errors.AddressPoolError, err:
|
185 |
raise errors.OpExecError("Cannot reserve IP address '%s': %s" % |
186 |
(ip, err)) |
187 |
|
188 |
if self.op.tags: |
189 |
for tag in self.op.tags: |
190 |
self.nobj.AddTag(tag)
|
191 |
|
192 |
self.cfg.AddNetwork(self.nobj, self.proc.GetECId(), check_uuid=False) |
193 |
del self.remove_locks[locking.LEVEL_NETWORK] |
194 |
|
195 |
|
196 |
class LUNetworkRemove(LogicalUnit): |
197 |
HPATH = "network-remove"
|
198 |
HTYPE = constants.HTYPE_NETWORK |
199 |
REQ_BGL = False
|
200 |
|
201 |
def ExpandNames(self): |
202 |
self.network_uuid = self.cfg.LookupNetwork(self.op.network_name) |
203 |
|
204 |
self.share_locks[locking.LEVEL_NODEGROUP] = 1 |
205 |
self.needed_locks = {
|
206 |
locking.LEVEL_NETWORK: [self.network_uuid],
|
207 |
locking.LEVEL_NODEGROUP: locking.ALL_SET, |
208 |
} |
209 |
|
210 |
def CheckPrereq(self): |
211 |
"""Check prerequisites.
|
212 |
|
213 |
This checks that the given network name exists as a network, that is
|
214 |
empty (i.e., contains no nodes), and that is not the last group of the
|
215 |
cluster.
|
216 |
|
217 |
"""
|
218 |
# Verify that the network is not conncted.
|
219 |
node_groups = [group.name |
220 |
for group in self.cfg.GetAllNodeGroupsInfo().values() |
221 |
if self.network_uuid in group.networks] |
222 |
|
223 |
if node_groups:
|
224 |
self.LogWarning("Network '%s' is connected to the following" |
225 |
" node groups: %s" %
|
226 |
(self.op.network_name,
|
227 |
utils.CommaJoin(utils.NiceSort(node_groups)))) |
228 |
raise errors.OpPrereqError("Network still connected", errors.ECODE_STATE) |
229 |
|
230 |
def BuildHooksEnv(self): |
231 |
"""Build hooks env.
|
232 |
|
233 |
"""
|
234 |
return {
|
235 |
"NETWORK_NAME": self.op.network_name, |
236 |
} |
237 |
|
238 |
def BuildHooksNodes(self): |
239 |
"""Build hooks nodes.
|
240 |
|
241 |
"""
|
242 |
mn = self.cfg.GetMasterNode()
|
243 |
return ([mn], [mn])
|
244 |
|
245 |
def Exec(self, feedback_fn): |
246 |
"""Remove the network.
|
247 |
|
248 |
"""
|
249 |
try:
|
250 |
self.cfg.RemoveNetwork(self.network_uuid) |
251 |
except errors.ConfigurationError:
|
252 |
raise errors.OpExecError("Network '%s' with UUID %s disappeared" % |
253 |
(self.op.network_name, self.network_uuid)) |
254 |
|
255 |
|
256 |
class LUNetworkSetParams(LogicalUnit): |
257 |
"""Modifies the parameters of a network.
|
258 |
|
259 |
"""
|
260 |
HPATH = "network-modify"
|
261 |
HTYPE = constants.HTYPE_NETWORK |
262 |
REQ_BGL = False
|
263 |
|
264 |
def CheckArguments(self): |
265 |
if (self.op.gateway and |
266 |
(self.op.add_reserved_ips or self.op.remove_reserved_ips)): |
267 |
raise errors.OpPrereqError("Cannot modify gateway and reserved ips" |
268 |
" at once", errors.ECODE_INVAL)
|
269 |
|
270 |
def ExpandNames(self): |
271 |
self.network_uuid = self.cfg.LookupNetwork(self.op.network_name) |
272 |
|
273 |
self.needed_locks = {
|
274 |
locking.LEVEL_NETWORK: [self.network_uuid],
|
275 |
} |
276 |
|
277 |
def CheckPrereq(self): |
278 |
"""Check prerequisites.
|
279 |
|
280 |
"""
|
281 |
self.network = self.cfg.GetNetwork(self.network_uuid) |
282 |
self.gateway = self.network.gateway |
283 |
self.mac_prefix = self.network.mac_prefix |
284 |
self.network6 = self.network.network6 |
285 |
self.gateway6 = self.network.gateway6 |
286 |
self.tags = self.network.tags |
287 |
|
288 |
self.pool = network.AddressPool(self.network) |
289 |
|
290 |
if self.op.gateway: |
291 |
if self.op.gateway == constants.VALUE_NONE: |
292 |
self.gateway = None |
293 |
else:
|
294 |
self.gateway = self.op.gateway |
295 |
|
296 |
if self.op.mac_prefix: |
297 |
if self.op.mac_prefix == constants.VALUE_NONE: |
298 |
self.mac_prefix = None |
299 |
else:
|
300 |
self.mac_prefix = \
|
301 |
utils.NormalizeAndValidateThreeOctetMacPrefix(self.op.mac_prefix)
|
302 |
|
303 |
if self.op.gateway6: |
304 |
if self.op.gateway6 == constants.VALUE_NONE: |
305 |
self.gateway6 = None |
306 |
else:
|
307 |
self.gateway6 = self.op.gateway6 |
308 |
|
309 |
if self.op.network6: |
310 |
if self.op.network6 == constants.VALUE_NONE: |
311 |
self.network6 = None |
312 |
else:
|
313 |
self.network6 = self.op.network6 |
314 |
|
315 |
def BuildHooksEnv(self): |
316 |
"""Build hooks env.
|
317 |
|
318 |
"""
|
319 |
args = { |
320 |
"name": self.op.network_name, |
321 |
"subnet": self.network.network, |
322 |
"gateway": self.gateway, |
323 |
"network6": self.network6, |
324 |
"gateway6": self.gateway6, |
325 |
"mac_prefix": self.mac_prefix, |
326 |
"tags": self.tags, |
327 |
} |
328 |
return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 |
329 |
|
330 |
def BuildHooksNodes(self): |
331 |
"""Build hooks nodes.
|
332 |
|
333 |
"""
|
334 |
mn = self.cfg.GetMasterNode()
|
335 |
return ([mn], [mn])
|
336 |
|
337 |
def Exec(self, feedback_fn): |
338 |
"""Modifies the network.
|
339 |
|
340 |
"""
|
341 |
#TODO: reserve/release via temporary reservation manager
|
342 |
# extend cfg.ReserveIp/ReleaseIp with the external flag
|
343 |
ec_id = self.proc.GetECId()
|
344 |
if self.op.gateway: |
345 |
if self.gateway == self.network.gateway: |
346 |
self.LogWarning("Gateway is already %s", self.gateway) |
347 |
else:
|
348 |
if self.gateway: |
349 |
self.cfg.ReserveIp(self.network_uuid, self.gateway, True, ec_id) |
350 |
if self.network.gateway: |
351 |
self.cfg.ReleaseIp(self.network_uuid, self.network.gateway, True, ec_id) |
352 |
self.network.gateway = self.gateway |
353 |
|
354 |
if self.op.add_reserved_ips: |
355 |
for ip in self.op.add_reserved_ips: |
356 |
self.cfg.ReserveIp(self.network_uuid, ip, True, ec_id) |
357 |
|
358 |
if self.op.remove_reserved_ips: |
359 |
for ip in self.op.remove_reserved_ips: |
360 |
if ip == self.network.gateway: |
361 |
self.LogWarning("Cannot unreserve Gateway's IP") |
362 |
continue
|
363 |
self.cfg.ReleaseIp(self.network_uuid, ip, True, ec_id) |
364 |
|
365 |
if self.op.mac_prefix: |
366 |
self.network.mac_prefix = self.mac_prefix |
367 |
|
368 |
if self.op.network6: |
369 |
self.network.network6 = self.network6 |
370 |
|
371 |
if self.op.gateway6: |
372 |
self.network.gateway6 = self.gateway6 |
373 |
|
374 |
self.pool.Validate()
|
375 |
|
376 |
self.cfg.Update(self.network, feedback_fn) |
377 |
|
378 |
|
379 |
class NetworkQuery(QueryBase): |
380 |
FIELDS = query.NETWORK_FIELDS |
381 |
|
382 |
def ExpandNames(self, lu): |
383 |
lu.needed_locks = {} |
384 |
lu.share_locks = ShareAll() |
385 |
|
386 |
self.do_locking = self.use_locking |
387 |
|
388 |
all_networks = lu.cfg.GetAllNetworksInfo() |
389 |
name_to_uuid = dict((n.name, n.uuid) for n in all_networks.values()) |
390 |
|
391 |
if self.names: |
392 |
missing = [] |
393 |
self.wanted = []
|
394 |
|
395 |
for name in self.names: |
396 |
if name in name_to_uuid: |
397 |
self.wanted.append(name_to_uuid[name])
|
398 |
else:
|
399 |
missing.append(name) |
400 |
|
401 |
if missing:
|
402 |
raise errors.OpPrereqError("Some networks do not exist: %s" % missing, |
403 |
errors.ECODE_NOENT) |
404 |
else:
|
405 |
self.wanted = locking.ALL_SET
|
406 |
|
407 |
if self.do_locking: |
408 |
lu.needed_locks[locking.LEVEL_NETWORK] = self.wanted
|
409 |
if query.NETQ_INST in self.requested_data: |
410 |
lu.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET |
411 |
if query.NETQ_GROUP in self.requested_data: |
412 |
lu.needed_locks[locking.LEVEL_NODEGROUP] = locking.ALL_SET |
413 |
|
414 |
def DeclareLocks(self, lu, level): |
415 |
pass
|
416 |
|
417 |
def _GetQueryData(self, lu): |
418 |
"""Computes the list of networks and their attributes.
|
419 |
|
420 |
"""
|
421 |
all_networks = lu.cfg.GetAllNetworksInfo() |
422 |
|
423 |
network_uuids = self._GetNames(lu, all_networks.keys(),
|
424 |
locking.LEVEL_NETWORK) |
425 |
|
426 |
do_instances = query.NETQ_INST in self.requested_data |
427 |
do_groups = query.NETQ_GROUP in self.requested_data |
428 |
|
429 |
network_to_instances = None
|
430 |
network_to_groups = None
|
431 |
|
432 |
# For NETQ_GROUP, we need to map network->[groups]
|
433 |
if do_groups:
|
434 |
all_groups = lu.cfg.GetAllNodeGroupsInfo() |
435 |
network_to_groups = dict((uuid, []) for uuid in network_uuids) |
436 |
for _, group in all_groups.iteritems(): |
437 |
for net_uuid in network_uuids: |
438 |
netparams = group.networks.get(net_uuid, None)
|
439 |
if netparams:
|
440 |
info = (group.name, netparams[constants.NIC_MODE], |
441 |
netparams[constants.NIC_LINK]) |
442 |
|
443 |
network_to_groups[net_uuid].append(info) |
444 |
|
445 |
if do_instances:
|
446 |
all_instances = lu.cfg.GetAllInstancesInfo() |
447 |
network_to_instances = dict((uuid, []) for uuid in network_uuids) |
448 |
for instance in all_instances.values(): |
449 |
for nic in instance.nics: |
450 |
if nic.network in network_uuids: |
451 |
network_to_instances[nic.network].append(instance.name) |
452 |
break
|
453 |
|
454 |
if query.NETQ_STATS in self.requested_data: |
455 |
stats = \ |
456 |
dict((uuid,
|
457 |
self._GetStats(network.AddressPool(all_networks[uuid])))
|
458 |
for uuid in network_uuids) |
459 |
else:
|
460 |
stats = None
|
461 |
|
462 |
return query.NetworkQueryData([all_networks[uuid]
|
463 |
for uuid in network_uuids], |
464 |
network_to_groups, |
465 |
network_to_instances, |
466 |
stats) |
467 |
|
468 |
@staticmethod
|
469 |
def _GetStats(pool): |
470 |
"""Returns statistics for a network address pool.
|
471 |
|
472 |
"""
|
473 |
return {
|
474 |
"free_count": pool.GetFreeCount(),
|
475 |
"reserved_count": pool.GetReservedCount(),
|
476 |
"map": pool.GetMap(),
|
477 |
"external_reservations":
|
478 |
utils.CommaJoin(pool.GetExternalReservations()), |
479 |
} |
480 |
|
481 |
|
482 |
class LUNetworkQuery(NoHooksLU): |
483 |
"""Logical unit for querying networks.
|
484 |
|
485 |
"""
|
486 |
REQ_BGL = False
|
487 |
|
488 |
def CheckArguments(self): |
489 |
self.nq = NetworkQuery(qlang.MakeSimpleFilter("name", self.op.names), |
490 |
self.op.output_fields, self.op.use_locking) |
491 |
|
492 |
def ExpandNames(self): |
493 |
self.nq.ExpandNames(self) |
494 |
|
495 |
def Exec(self, feedback_fn): |
496 |
return self.nq.OldStyleQuery(self) |
497 |
|
498 |
|
499 |
def _FmtNetworkConflict(details): |
500 |
"""Utility for L{_NetworkConflictCheck}.
|
501 |
|
502 |
"""
|
503 |
return utils.CommaJoin("nic%s/%s" % (idx, ipaddr) |
504 |
for (idx, ipaddr) in details) |
505 |
|
506 |
|
507 |
def _NetworkConflictCheck(lu, check_fn, action, instances): |
508 |
"""Checks for network interface conflicts with a network.
|
509 |
|
510 |
@type lu: L{LogicalUnit}
|
511 |
@type check_fn: callable receiving one parameter (L{objects.NIC}) and
|
512 |
returning boolean
|
513 |
@param check_fn: Function checking for conflict
|
514 |
@type action: string
|
515 |
@param action: Part of error message (see code)
|
516 |
@raise errors.OpPrereqError: If conflicting IP addresses are found.
|
517 |
|
518 |
"""
|
519 |
conflicts = [] |
520 |
|
521 |
for (_, instance) in lu.cfg.GetMultiInstanceInfo(instances): |
522 |
instconflicts = [(idx, nic.ip) |
523 |
for (idx, nic) in enumerate(instance.nics) |
524 |
if check_fn(nic)]
|
525 |
|
526 |
if instconflicts:
|
527 |
conflicts.append((instance.name, instconflicts)) |
528 |
|
529 |
if conflicts:
|
530 |
lu.LogWarning("IP addresses from network '%s', which is about to %s"
|
531 |
" node group '%s', are in use: %s" %
|
532 |
(lu.network_name, action, lu.group.name, |
533 |
utils.CommaJoin(("%s: %s" %
|
534 |
(name, _FmtNetworkConflict(details))) |
535 |
for (name, details) in conflicts))) |
536 |
|
537 |
raise errors.OpPrereqError("Conflicting IP addresses found; " |
538 |
" remove/modify the corresponding network"
|
539 |
" interfaces", errors.ECODE_STATE)
|
540 |
|
541 |
|
542 |
class LUNetworkConnect(LogicalUnit): |
543 |
"""Connect a network to a nodegroup
|
544 |
|
545 |
"""
|
546 |
HPATH = "network-connect"
|
547 |
HTYPE = constants.HTYPE_NETWORK |
548 |
REQ_BGL = False
|
549 |
|
550 |
def ExpandNames(self): |
551 |
self.network_name = self.op.network_name |
552 |
self.group_name = self.op.group_name |
553 |
self.network_mode = self.op.network_mode |
554 |
self.network_link = self.op.network_link |
555 |
|
556 |
self.network_uuid = self.cfg.LookupNetwork(self.network_name) |
557 |
self.group_uuid = self.cfg.LookupNodeGroup(self.group_name) |
558 |
|
559 |
self.needed_locks = {
|
560 |
locking.LEVEL_INSTANCE: [], |
561 |
locking.LEVEL_NODEGROUP: [self.group_uuid],
|
562 |
} |
563 |
self.share_locks[locking.LEVEL_INSTANCE] = 1 |
564 |
|
565 |
if self.op.conflicts_check: |
566 |
self.needed_locks[locking.LEVEL_NETWORK] = [self.network_uuid] |
567 |
self.share_locks[locking.LEVEL_NETWORK] = 1 |
568 |
|
569 |
def DeclareLocks(self, level): |
570 |
if level == locking.LEVEL_INSTANCE:
|
571 |
assert not self.needed_locks[locking.LEVEL_INSTANCE] |
572 |
|
573 |
# Lock instances optimistically, needs verification once group lock has
|
574 |
# been acquired
|
575 |
if self.op.conflicts_check: |
576 |
self.needed_locks[locking.LEVEL_INSTANCE] = \
|
577 |
self.cfg.GetNodeGroupInstances(self.group_uuid) |
578 |
|
579 |
def BuildHooksEnv(self): |
580 |
ret = { |
581 |
"GROUP_NAME": self.group_name, |
582 |
"GROUP_NETWORK_MODE": self.network_mode, |
583 |
"GROUP_NETWORK_LINK": self.network_link, |
584 |
} |
585 |
return ret
|
586 |
|
587 |
def BuildHooksNodes(self): |
588 |
nodes = self.cfg.GetNodeGroup(self.group_uuid).members |
589 |
return (nodes, nodes)
|
590 |
|
591 |
def CheckPrereq(self): |
592 |
owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP)) |
593 |
|
594 |
assert self.group_uuid in owned_groups |
595 |
|
596 |
# Check if locked instances are still correct
|
597 |
owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE)) |
598 |
if self.op.conflicts_check: |
599 |
CheckNodeGroupInstances(self.cfg, self.group_uuid, owned_instances) |
600 |
|
601 |
self.netparams = {
|
602 |
constants.NIC_MODE: self.network_mode,
|
603 |
constants.NIC_LINK: self.network_link,
|
604 |
} |
605 |
objects.NIC.CheckParameterSyntax(self.netparams)
|
606 |
|
607 |
self.group = self.cfg.GetNodeGroup(self.group_uuid) |
608 |
#if self.network_mode == constants.NIC_MODE_BRIDGED:
|
609 |
# _CheckNodeGroupBridgesExist(self, self.network_link, self.group_uuid)
|
610 |
self.connected = False |
611 |
if self.network_uuid in self.group.networks: |
612 |
self.LogWarning("Network '%s' is already mapped to group '%s'" % |
613 |
(self.network_name, self.group.name)) |
614 |
self.connected = True |
615 |
|
616 |
# check only if not already connected
|
617 |
elif self.op.conflicts_check: |
618 |
pool = network.AddressPool(self.cfg.GetNetwork(self.network_uuid)) |
619 |
|
620 |
_NetworkConflictCheck(self, lambda nic: pool.Contains(nic.ip), |
621 |
"connect to", owned_instances)
|
622 |
|
623 |
def Exec(self, feedback_fn): |
624 |
# Connect the network and update the group only if not already connected
|
625 |
if not self.connected: |
626 |
self.group.networks[self.network_uuid] = self.netparams |
627 |
self.cfg.Update(self.group, feedback_fn) |
628 |
|
629 |
|
630 |
class LUNetworkDisconnect(LogicalUnit): |
631 |
"""Disconnect a network to a nodegroup
|
632 |
|
633 |
"""
|
634 |
HPATH = "network-disconnect"
|
635 |
HTYPE = constants.HTYPE_NETWORK |
636 |
REQ_BGL = False
|
637 |
|
638 |
def ExpandNames(self): |
639 |
self.network_name = self.op.network_name |
640 |
self.group_name = self.op.group_name |
641 |
|
642 |
self.network_uuid = self.cfg.LookupNetwork(self.network_name) |
643 |
self.group_uuid = self.cfg.LookupNodeGroup(self.group_name) |
644 |
|
645 |
self.needed_locks = {
|
646 |
locking.LEVEL_INSTANCE: [], |
647 |
locking.LEVEL_NODEGROUP: [self.group_uuid],
|
648 |
} |
649 |
self.share_locks[locking.LEVEL_INSTANCE] = 1 |
650 |
|
651 |
def DeclareLocks(self, level): |
652 |
if level == locking.LEVEL_INSTANCE:
|
653 |
assert not self.needed_locks[locking.LEVEL_INSTANCE] |
654 |
|
655 |
# Lock instances optimistically, needs verification once group lock has
|
656 |
# been acquired
|
657 |
self.needed_locks[locking.LEVEL_INSTANCE] = \
|
658 |
self.cfg.GetNodeGroupInstances(self.group_uuid) |
659 |
|
660 |
def BuildHooksEnv(self): |
661 |
ret = { |
662 |
"GROUP_NAME": self.group_name, |
663 |
} |
664 |
return ret
|
665 |
|
666 |
def BuildHooksNodes(self): |
667 |
nodes = self.cfg.GetNodeGroup(self.group_uuid).members |
668 |
return (nodes, nodes)
|
669 |
|
670 |
def CheckPrereq(self): |
671 |
owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP)) |
672 |
|
673 |
assert self.group_uuid in owned_groups |
674 |
|
675 |
# Check if locked instances are still correct
|
676 |
owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE)) |
677 |
CheckNodeGroupInstances(self.cfg, self.group_uuid, owned_instances) |
678 |
|
679 |
self.group = self.cfg.GetNodeGroup(self.group_uuid) |
680 |
self.connected = True |
681 |
if self.network_uuid not in self.group.networks: |
682 |
self.LogWarning("Network '%s' is not mapped to group '%s'", |
683 |
self.network_name, self.group.name) |
684 |
self.connected = False |
685 |
|
686 |
# We need this check only if network is not already connected
|
687 |
else:
|
688 |
_NetworkConflictCheck(self, lambda nic: nic.network == self.network_uuid, |
689 |
"disconnect from", owned_instances)
|
690 |
|
691 |
def Exec(self, feedback_fn): |
692 |
# Disconnect the network and update the group only if network is connected
|
693 |
if self.connected: |
694 |
del self.group.networks[self.network_uuid] |
695 |
self.cfg.Update(self.group, feedback_fn) |