root / lib / cmdlib / common.py @ 1d870e0d
History | View | Annotate | Download (3.8 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 |
"""Common functions used by multiple logical units."""
|
23 |
|
24 |
from ganeti import errors |
25 |
from ganeti import locking |
26 |
from ganeti import utils |
27 |
|
28 |
|
29 |
def _ExpandItemName(fn, name, kind): |
30 |
"""Expand an item name.
|
31 |
|
32 |
@param fn: the function to use for expansion
|
33 |
@param name: requested item name
|
34 |
@param kind: text description ('Node' or 'Instance')
|
35 |
@return: the resolved (full) name
|
36 |
@raise errors.OpPrereqError: if the item is not found
|
37 |
|
38 |
"""
|
39 |
full_name = fn(name) |
40 |
if full_name is None: |
41 |
raise errors.OpPrereqError("%s '%s' not known" % (kind, name), |
42 |
errors.ECODE_NOENT) |
43 |
return full_name
|
44 |
|
45 |
|
46 |
def _ExpandInstanceName(cfg, name): |
47 |
"""Wrapper over L{_ExpandItemName} for instance."""
|
48 |
return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance") |
49 |
|
50 |
|
51 |
def _ExpandNodeName(cfg, name): |
52 |
"""Wrapper over L{_ExpandItemName} for nodes."""
|
53 |
return _ExpandItemName(cfg.ExpandNodeName, name, "Node") |
54 |
|
55 |
|
56 |
def _ShareAll(): |
57 |
"""Returns a dict declaring all lock levels shared.
|
58 |
|
59 |
"""
|
60 |
return dict.fromkeys(locking.LEVELS, 1) |
61 |
|
62 |
|
63 |
def _CheckNodeGroupInstances(cfg, group_uuid, owned_instances): |
64 |
"""Checks if the instances in a node group are still correct.
|
65 |
|
66 |
@type cfg: L{config.ConfigWriter}
|
67 |
@param cfg: The cluster configuration
|
68 |
@type group_uuid: string
|
69 |
@param group_uuid: Node group UUID
|
70 |
@type owned_instances: set or frozenset
|
71 |
@param owned_instances: List of currently owned instances
|
72 |
|
73 |
"""
|
74 |
wanted_instances = cfg.GetNodeGroupInstances(group_uuid) |
75 |
if owned_instances != wanted_instances:
|
76 |
raise errors.OpPrereqError("Instances in node group '%s' changed since" |
77 |
" locks were acquired, wanted '%s', have '%s';"
|
78 |
" retry the operation" %
|
79 |
(group_uuid, |
80 |
utils.CommaJoin(wanted_instances), |
81 |
utils.CommaJoin(owned_instances)), |
82 |
errors.ECODE_STATE) |
83 |
|
84 |
return wanted_instances
|
85 |
|
86 |
|
87 |
def _GetWantedNodes(lu, nodes): |
88 |
"""Returns list of checked and expanded node names.
|
89 |
|
90 |
@type lu: L{LogicalUnit}
|
91 |
@param lu: the logical unit on whose behalf we execute
|
92 |
@type nodes: list
|
93 |
@param nodes: list of node names or None for all nodes
|
94 |
@rtype: list
|
95 |
@return: the list of nodes, sorted
|
96 |
@raise errors.ProgrammerError: if the nodes parameter is wrong type
|
97 |
|
98 |
"""
|
99 |
if nodes:
|
100 |
return [_ExpandNodeName(lu.cfg, name) for name in nodes] |
101 |
|
102 |
return utils.NiceSort(lu.cfg.GetNodeList())
|
103 |
|
104 |
|
105 |
def _GetWantedInstances(lu, instances): |
106 |
"""Returns list of checked and expanded instance names.
|
107 |
|
108 |
@type lu: L{LogicalUnit}
|
109 |
@param lu: the logical unit on whose behalf we execute
|
110 |
@type instances: list
|
111 |
@param instances: list of instance names or None for all instances
|
112 |
@rtype: list
|
113 |
@return: the list of instances, sorted
|
114 |
@raise errors.OpPrereqError: if the instances parameter is wrong type
|
115 |
@raise errors.OpPrereqError: if any of the passed instances is not found
|
116 |
|
117 |
"""
|
118 |
if instances:
|
119 |
wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances] |
120 |
else:
|
121 |
wanted = utils.NiceSort(lu.cfg.GetInstanceList()) |
122 |
return wanted
|