root / lib / cmdlib.py @ 1338f2b4
History | View | Annotate | Download (337.4 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | e7c6e02b | Michael Hanselmann | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | 880478f8 | Iustin Pop | """Module implementing the master-side code."""
|
23 | a8083063 | Iustin Pop | |
24 | c70d2d9b | Iustin Pop | # pylint: disable-msg=W0201
|
25 | c70d2d9b | Iustin Pop | |
26 | c70d2d9b | Iustin Pop | # W0201 since most LU attributes are defined in CheckPrereq or similar
|
27 | c70d2d9b | Iustin Pop | # functions
|
28 | a8083063 | Iustin Pop | |
29 | a8083063 | Iustin Pop | import os |
30 | a8083063 | Iustin Pop | import os.path |
31 | a8083063 | Iustin Pop | import time |
32 | a8083063 | Iustin Pop | import re |
33 | a8083063 | Iustin Pop | import platform |
34 | ffa1c0dc | Iustin Pop | import logging |
35 | 74409b12 | Iustin Pop | import copy |
36 | b98bf262 | Michael Hanselmann | import OpenSSL |
37 | a8083063 | Iustin Pop | |
38 | a8083063 | Iustin Pop | from ganeti import ssh |
39 | a8083063 | Iustin Pop | from ganeti import utils |
40 | a8083063 | Iustin Pop | from ganeti import errors |
41 | a8083063 | Iustin Pop | from ganeti import hypervisor |
42 | 6048c986 | Guido Trotter | from ganeti import locking |
43 | a8083063 | Iustin Pop | from ganeti import constants |
44 | a8083063 | Iustin Pop | from ganeti import objects |
45 | 8d14b30d | Iustin Pop | from ganeti import serializer |
46 | 112f18a5 | Iustin Pop | from ganeti import ssconf |
47 | 1338f2b4 | Balazs Lecz | from ganeti import uidpool |
48 | d61df03e | Iustin Pop | |
49 | d61df03e | Iustin Pop | |
50 | a8083063 | Iustin Pop | class LogicalUnit(object): |
51 | 396e1b78 | Michael Hanselmann | """Logical Unit base class.
|
52 | a8083063 | Iustin Pop |
|
53 | a8083063 | Iustin Pop | Subclasses must follow these rules:
|
54 | d465bdc8 | Guido Trotter | - implement ExpandNames
|
55 | 6fd35c4d | Michael Hanselmann | - implement CheckPrereq (except when tasklets are used)
|
56 | 6fd35c4d | Michael Hanselmann | - implement Exec (except when tasklets are used)
|
57 | a8083063 | Iustin Pop | - implement BuildHooksEnv
|
58 | a8083063 | Iustin Pop | - redefine HPATH and HTYPE
|
59 | 05f86716 | Guido Trotter | - optionally redefine their run requirements:
|
60 | 7e55040e | Guido Trotter | REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
|
61 | 05f86716 | Guido Trotter |
|
62 | 05f86716 | Guido Trotter | Note that all commands require root permissions.
|
63 | a8083063 | Iustin Pop |
|
64 | 20777413 | Iustin Pop | @ivar dry_run_result: the value (if any) that will be returned to the caller
|
65 | 20777413 | Iustin Pop | in dry-run mode (signalled by opcode dry_run parameter)
|
66 | 20777413 | Iustin Pop |
|
67 | a8083063 | Iustin Pop | """
|
68 | a8083063 | Iustin Pop | HPATH = None
|
69 | a8083063 | Iustin Pop | HTYPE = None
|
70 | a8083063 | Iustin Pop | _OP_REQP = [] |
71 | 7e55040e | Guido Trotter | REQ_BGL = True
|
72 | a8083063 | Iustin Pop | |
73 | 72737a7f | Iustin Pop | def __init__(self, processor, op, context, rpc): |
74 | a8083063 | Iustin Pop | """Constructor for LogicalUnit.
|
75 | a8083063 | Iustin Pop |
|
76 | 5bbd3f7f | Michael Hanselmann | This needs to be overridden in derived classes in order to check op
|
77 | a8083063 | Iustin Pop | validity.
|
78 | a8083063 | Iustin Pop |
|
79 | a8083063 | Iustin Pop | """
|
80 | 5bfac263 | Iustin Pop | self.proc = processor
|
81 | a8083063 | Iustin Pop | self.op = op
|
82 | 77b657a3 | Guido Trotter | self.cfg = context.cfg
|
83 | 77b657a3 | Guido Trotter | self.context = context
|
84 | 72737a7f | Iustin Pop | self.rpc = rpc
|
85 | ca2a79e1 | Guido Trotter | # Dicts used to declare locking needs to mcpu
|
86 | d465bdc8 | Guido Trotter | self.needed_locks = None |
87 | 6683bba2 | Guido Trotter | self.acquired_locks = {}
|
88 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 0) |
89 | ca2a79e1 | Guido Trotter | self.add_locks = {}
|
90 | ca2a79e1 | Guido Trotter | self.remove_locks = {}
|
91 | c4a2fee1 | Guido Trotter | # Used to force good behavior when calling helper functions
|
92 | c4a2fee1 | Guido Trotter | self.recalculate_locks = {}
|
93 | c92b310a | Michael Hanselmann | self.__ssh = None |
94 | 86d9d3bb | Iustin Pop | # logging
|
95 | fe267188 | Iustin Pop | self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103 |
96 | fe267188 | Iustin Pop | self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103 |
97 | d984846d | Iustin Pop | self.LogStep = processor.LogStep # pylint: disable-msg=C0103 |
98 | 20777413 | Iustin Pop | # support for dry-run
|
99 | 20777413 | Iustin Pop | self.dry_run_result = None |
100 | ee844e20 | Iustin Pop | # support for generic debug attribute
|
101 | ee844e20 | Iustin Pop | if (not hasattr(self.op, "debug_level") or |
102 | ee844e20 | Iustin Pop | not isinstance(self.op.debug_level, int)): |
103 | ee844e20 | Iustin Pop | self.op.debug_level = 0 |
104 | c92b310a | Michael Hanselmann | |
105 | 6fd35c4d | Michael Hanselmann | # Tasklets
|
106 | 3a012b41 | Michael Hanselmann | self.tasklets = None |
107 | 6fd35c4d | Michael Hanselmann | |
108 | a8083063 | Iustin Pop | for attr_name in self._OP_REQP: |
109 | a8083063 | Iustin Pop | attr_val = getattr(op, attr_name, None) |
110 | a8083063 | Iustin Pop | if attr_val is None: |
111 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Required parameter '%s' missing" % |
112 | 5c983ee5 | Iustin Pop | attr_name, errors.ECODE_INVAL) |
113 | 6fd35c4d | Michael Hanselmann | |
114 | 4be4691d | Iustin Pop | self.CheckArguments()
|
115 | a8083063 | Iustin Pop | |
116 | c92b310a | Michael Hanselmann | def __GetSSH(self): |
117 | c92b310a | Michael Hanselmann | """Returns the SshRunner object
|
118 | c92b310a | Michael Hanselmann |
|
119 | c92b310a | Michael Hanselmann | """
|
120 | c92b310a | Michael Hanselmann | if not self.__ssh: |
121 | 6b0469d2 | Iustin Pop | self.__ssh = ssh.SshRunner(self.cfg.GetClusterName()) |
122 | c92b310a | Michael Hanselmann | return self.__ssh |
123 | c92b310a | Michael Hanselmann | |
124 | c92b310a | Michael Hanselmann | ssh = property(fget=__GetSSH)
|
125 | c92b310a | Michael Hanselmann | |
126 | 4be4691d | Iustin Pop | def CheckArguments(self): |
127 | 4be4691d | Iustin Pop | """Check syntactic validity for the opcode arguments.
|
128 | 4be4691d | Iustin Pop |
|
129 | 4be4691d | Iustin Pop | This method is for doing a simple syntactic check and ensure
|
130 | 4be4691d | Iustin Pop | validity of opcode parameters, without any cluster-related
|
131 | 4be4691d | Iustin Pop | checks. While the same can be accomplished in ExpandNames and/or
|
132 | 4be4691d | Iustin Pop | CheckPrereq, doing these separate is better because:
|
133 | 4be4691d | Iustin Pop |
|
134 | 4be4691d | Iustin Pop | - ExpandNames is left as as purely a lock-related function
|
135 | 5bbd3f7f | Michael Hanselmann | - CheckPrereq is run after we have acquired locks (and possible
|
136 | 4be4691d | Iustin Pop | waited for them)
|
137 | 4be4691d | Iustin Pop |
|
138 | 4be4691d | Iustin Pop | The function is allowed to change the self.op attribute so that
|
139 | 4be4691d | Iustin Pop | later methods can no longer worry about missing parameters.
|
140 | 4be4691d | Iustin Pop |
|
141 | 4be4691d | Iustin Pop | """
|
142 | 4be4691d | Iustin Pop | pass
|
143 | 4be4691d | Iustin Pop | |
144 | d465bdc8 | Guido Trotter | def ExpandNames(self): |
145 | d465bdc8 | Guido Trotter | """Expand names for this LU.
|
146 | d465bdc8 | Guido Trotter |
|
147 | d465bdc8 | Guido Trotter | This method is called before starting to execute the opcode, and it should
|
148 | d465bdc8 | Guido Trotter | update all the parameters of the opcode to their canonical form (e.g. a
|
149 | d465bdc8 | Guido Trotter | short node name must be fully expanded after this method has successfully
|
150 | d465bdc8 | Guido Trotter | completed). This way locking, hooks, logging, ecc. can work correctly.
|
151 | d465bdc8 | Guido Trotter |
|
152 | d465bdc8 | Guido Trotter | LUs which implement this method must also populate the self.needed_locks
|
153 | d465bdc8 | Guido Trotter | member, as a dict with lock levels as keys, and a list of needed lock names
|
154 | d465bdc8 | Guido Trotter | as values. Rules:
|
155 | e4376078 | Iustin Pop |
|
156 | e4376078 | Iustin Pop | - use an empty dict if you don't need any lock
|
157 | e4376078 | Iustin Pop | - if you don't need any lock at a particular level omit that level
|
158 | e4376078 | Iustin Pop | - don't put anything for the BGL level
|
159 | e4376078 | Iustin Pop | - if you want all locks at a level use locking.ALL_SET as a value
|
160 | d465bdc8 | Guido Trotter |
|
161 | 3977a4c1 | Guido Trotter | If you need to share locks (rather than acquire them exclusively) at one
|
162 | 3977a4c1 | Guido Trotter | level you can modify self.share_locks, setting a true value (usually 1) for
|
163 | 3977a4c1 | Guido Trotter | that level. By default locks are not shared.
|
164 | 3977a4c1 | Guido Trotter |
|
165 | 6fd35c4d | Michael Hanselmann | This function can also define a list of tasklets, which then will be
|
166 | 6fd35c4d | Michael Hanselmann | executed in order instead of the usual LU-level CheckPrereq and Exec
|
167 | 6fd35c4d | Michael Hanselmann | functions, if those are not defined by the LU.
|
168 | 6fd35c4d | Michael Hanselmann |
|
169 | e4376078 | Iustin Pop | Examples::
|
170 | e4376078 | Iustin Pop |
|
171 | e4376078 | Iustin Pop | # Acquire all nodes and one instance
|
172 | e4376078 | Iustin Pop | self.needed_locks = {
|
173 | e4376078 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET,
|
174 | e4376078 | Iustin Pop | locking.LEVEL_INSTANCE: ['instance1.example.tld'],
|
175 | e4376078 | Iustin Pop | }
|
176 | e4376078 | Iustin Pop | # Acquire just two nodes
|
177 | e4376078 | Iustin Pop | self.needed_locks = {
|
178 | e4376078 | Iustin Pop | locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
|
179 | e4376078 | Iustin Pop | }
|
180 | e4376078 | Iustin Pop | # Acquire no locks
|
181 | e4376078 | Iustin Pop | self.needed_locks = {} # No, you can't leave it to the default value None
|
182 | d465bdc8 | Guido Trotter |
|
183 | d465bdc8 | Guido Trotter | """
|
184 | d465bdc8 | Guido Trotter | # The implementation of this method is mandatory only if the new LU is
|
185 | d465bdc8 | Guido Trotter | # concurrent, so that old LUs don't need to be changed all at the same
|
186 | d465bdc8 | Guido Trotter | # time.
|
187 | d465bdc8 | Guido Trotter | if self.REQ_BGL: |
188 | d465bdc8 | Guido Trotter | self.needed_locks = {} # Exclusive LUs don't need locks. |
189 | d465bdc8 | Guido Trotter | else:
|
190 | d465bdc8 | Guido Trotter | raise NotImplementedError |
191 | d465bdc8 | Guido Trotter | |
192 | fb8dcb62 | Guido Trotter | def DeclareLocks(self, level): |
193 | fb8dcb62 | Guido Trotter | """Declare LU locking needs for a level
|
194 | fb8dcb62 | Guido Trotter |
|
195 | fb8dcb62 | Guido Trotter | While most LUs can just declare their locking needs at ExpandNames time,
|
196 | fb8dcb62 | Guido Trotter | sometimes there's the need to calculate some locks after having acquired
|
197 | fb8dcb62 | Guido Trotter | the ones before. This function is called just before acquiring locks at a
|
198 | fb8dcb62 | Guido Trotter | particular level, but after acquiring the ones at lower levels, and permits
|
199 | fb8dcb62 | Guido Trotter | such calculations. It can be used to modify self.needed_locks, and by
|
200 | fb8dcb62 | Guido Trotter | default it does nothing.
|
201 | fb8dcb62 | Guido Trotter |
|
202 | fb8dcb62 | Guido Trotter | This function is only called if you have something already set in
|
203 | fb8dcb62 | Guido Trotter | self.needed_locks for the level.
|
204 | fb8dcb62 | Guido Trotter |
|
205 | fb8dcb62 | Guido Trotter | @param level: Locking level which is going to be locked
|
206 | fb8dcb62 | Guido Trotter | @type level: member of ganeti.locking.LEVELS
|
207 | fb8dcb62 | Guido Trotter |
|
208 | fb8dcb62 | Guido Trotter | """
|
209 | fb8dcb62 | Guido Trotter | |
210 | a8083063 | Iustin Pop | def CheckPrereq(self): |
211 | a8083063 | Iustin Pop | """Check prerequisites for this LU.
|
212 | a8083063 | Iustin Pop |
|
213 | a8083063 | Iustin Pop | This method should check that the prerequisites for the execution
|
214 | a8083063 | Iustin Pop | of this LU are fulfilled. It can do internode communication, but
|
215 | a8083063 | Iustin Pop | it should be idempotent - no cluster or system changes are
|
216 | a8083063 | Iustin Pop | allowed.
|
217 | a8083063 | Iustin Pop |
|
218 | a8083063 | Iustin Pop | The method should raise errors.OpPrereqError in case something is
|
219 | a8083063 | Iustin Pop | not fulfilled. Its return value is ignored.
|
220 | a8083063 | Iustin Pop |
|
221 | a8083063 | Iustin Pop | This method should also update all the parameters of the opcode to
|
222 | d465bdc8 | Guido Trotter | their canonical form if it hasn't been done by ExpandNames before.
|
223 | a8083063 | Iustin Pop |
|
224 | a8083063 | Iustin Pop | """
|
225 | 3a012b41 | Michael Hanselmann | if self.tasklets is not None: |
226 | b4a9eb66 | Michael Hanselmann | for (idx, tl) in enumerate(self.tasklets): |
227 | abae1b2b | Michael Hanselmann | logging.debug("Checking prerequisites for tasklet %s/%s",
|
228 | abae1b2b | Michael Hanselmann | idx + 1, len(self.tasklets)) |
229 | 6fd35c4d | Michael Hanselmann | tl.CheckPrereq() |
230 | 6fd35c4d | Michael Hanselmann | else:
|
231 | 6fd35c4d | Michael Hanselmann | raise NotImplementedError |
232 | a8083063 | Iustin Pop | |
233 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
234 | a8083063 | Iustin Pop | """Execute the LU.
|
235 | a8083063 | Iustin Pop |
|
236 | a8083063 | Iustin Pop | This method should implement the actual work. It should raise
|
237 | a8083063 | Iustin Pop | errors.OpExecError for failures that are somewhat dealt with in
|
238 | a8083063 | Iustin Pop | code, or expected.
|
239 | a8083063 | Iustin Pop |
|
240 | a8083063 | Iustin Pop | """
|
241 | 3a012b41 | Michael Hanselmann | if self.tasklets is not None: |
242 | b4a9eb66 | Michael Hanselmann | for (idx, tl) in enumerate(self.tasklets): |
243 | abae1b2b | Michael Hanselmann | logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets)) |
244 | 6fd35c4d | Michael Hanselmann | tl.Exec(feedback_fn) |
245 | 6fd35c4d | Michael Hanselmann | else:
|
246 | 6fd35c4d | Michael Hanselmann | raise NotImplementedError |
247 | a8083063 | Iustin Pop | |
248 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
249 | a8083063 | Iustin Pop | """Build hooks environment for this LU.
|
250 | a8083063 | Iustin Pop |
|
251 | a8083063 | Iustin Pop | This method should return a three-node tuple consisting of: a dict
|
252 | a8083063 | Iustin Pop | containing the environment that will be used for running the
|
253 | a8083063 | Iustin Pop | specific hook for this LU, a list of node names on which the hook
|
254 | a8083063 | Iustin Pop | should run before the execution, and a list of node names on which
|
255 | a8083063 | Iustin Pop | the hook should run after the execution.
|
256 | a8083063 | Iustin Pop |
|
257 | a8083063 | Iustin Pop | The keys of the dict must not have 'GANETI_' prefixed as this will
|
258 | a8083063 | Iustin Pop | be handled in the hooks runner. Also note additional keys will be
|
259 | a8083063 | Iustin Pop | added by the hooks runner. If the LU doesn't define any
|
260 | a8083063 | Iustin Pop | environment, an empty dict (and not None) should be returned.
|
261 | a8083063 | Iustin Pop |
|
262 | 8a3fe350 | Guido Trotter | No nodes should be returned as an empty list (and not None).
|
263 | a8083063 | Iustin Pop |
|
264 | a8083063 | Iustin Pop | Note that if the HPATH for a LU class is None, this function will
|
265 | a8083063 | Iustin Pop | not be called.
|
266 | a8083063 | Iustin Pop |
|
267 | a8083063 | Iustin Pop | """
|
268 | a8083063 | Iustin Pop | raise NotImplementedError |
269 | a8083063 | Iustin Pop | |
270 | 1fce5219 | Guido Trotter | def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result): |
271 | 1fce5219 | Guido Trotter | """Notify the LU about the results of its hooks.
|
272 | 1fce5219 | Guido Trotter |
|
273 | 1fce5219 | Guido Trotter | This method is called every time a hooks phase is executed, and notifies
|
274 | 1fce5219 | Guido Trotter | the Logical Unit about the hooks' result. The LU can then use it to alter
|
275 | 1fce5219 | Guido Trotter | its result based on the hooks. By default the method does nothing and the
|
276 | 1fce5219 | Guido Trotter | previous result is passed back unchanged but any LU can define it if it
|
277 | 1fce5219 | Guido Trotter | wants to use the local cluster hook-scripts somehow.
|
278 | 1fce5219 | Guido Trotter |
|
279 | e4376078 | Iustin Pop | @param phase: one of L{constants.HOOKS_PHASE_POST} or
|
280 | e4376078 | Iustin Pop | L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
|
281 | e4376078 | Iustin Pop | @param hook_results: the results of the multi-node hooks rpc call
|
282 | e4376078 | Iustin Pop | @param feedback_fn: function used send feedback back to the caller
|
283 | e4376078 | Iustin Pop | @param lu_result: the previous Exec result this LU had, or None
|
284 | e4376078 | Iustin Pop | in the PRE phase
|
285 | e4376078 | Iustin Pop | @return: the new Exec result, based on the previous result
|
286 | e4376078 | Iustin Pop | and hook results
|
287 | 1fce5219 | Guido Trotter |
|
288 | 1fce5219 | Guido Trotter | """
|
289 | 2d54e29c | Iustin Pop | # API must be kept, thus we ignore the unused argument and could
|
290 | 2d54e29c | Iustin Pop | # be a function warnings
|
291 | 2d54e29c | Iustin Pop | # pylint: disable-msg=W0613,R0201
|
292 | 1fce5219 | Guido Trotter | return lu_result
|
293 | 1fce5219 | Guido Trotter | |
294 | 43905206 | Guido Trotter | def _ExpandAndLockInstance(self): |
295 | 43905206 | Guido Trotter | """Helper function to expand and lock an instance.
|
296 | 43905206 | Guido Trotter |
|
297 | 43905206 | Guido Trotter | Many LUs that work on an instance take its name in self.op.instance_name
|
298 | 43905206 | Guido Trotter | and need to expand it and then declare the expanded name for locking. This
|
299 | 43905206 | Guido Trotter | function does it, and then updates self.op.instance_name to the expanded
|
300 | 43905206 | Guido Trotter | name. It also initializes needed_locks as a dict, if this hasn't been done
|
301 | 43905206 | Guido Trotter | before.
|
302 | 43905206 | Guido Trotter |
|
303 | 43905206 | Guido Trotter | """
|
304 | 43905206 | Guido Trotter | if self.needed_locks is None: |
305 | 43905206 | Guido Trotter | self.needed_locks = {}
|
306 | 43905206 | Guido Trotter | else:
|
307 | 43905206 | Guido Trotter | assert locking.LEVEL_INSTANCE not in self.needed_locks, \ |
308 | 43905206 | Guido Trotter | "_ExpandAndLockInstance called with instance-level locks set"
|
309 | cf26a87a | Iustin Pop | self.op.instance_name = _ExpandInstanceName(self.cfg, |
310 | cf26a87a | Iustin Pop | self.op.instance_name)
|
311 | cf26a87a | Iustin Pop | self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name |
312 | 43905206 | Guido Trotter | |
313 | a82ce292 | Guido Trotter | def _LockInstancesNodes(self, primary_only=False): |
314 | c4a2fee1 | Guido Trotter | """Helper function to declare instances' nodes for locking.
|
315 | c4a2fee1 | Guido Trotter |
|
316 | c4a2fee1 | Guido Trotter | This function should be called after locking one or more instances to lock
|
317 | c4a2fee1 | Guido Trotter | their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
|
318 | c4a2fee1 | Guido Trotter | with all primary or secondary nodes for instances already locked and
|
319 | c4a2fee1 | Guido Trotter | present in self.needed_locks[locking.LEVEL_INSTANCE].
|
320 | c4a2fee1 | Guido Trotter |
|
321 | c4a2fee1 | Guido Trotter | It should be called from DeclareLocks, and for safety only works if
|
322 | c4a2fee1 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] is set.
|
323 | c4a2fee1 | Guido Trotter |
|
324 | c4a2fee1 | Guido Trotter | In the future it may grow parameters to just lock some instance's nodes, or
|
325 | c4a2fee1 | Guido Trotter | to just lock primaries or secondary nodes, if needed.
|
326 | c4a2fee1 | Guido Trotter |
|
327 | e4376078 | Iustin Pop | If should be called in DeclareLocks in a way similar to::
|
328 | c4a2fee1 | Guido Trotter |
|
329 | e4376078 | Iustin Pop | if level == locking.LEVEL_NODE:
|
330 | e4376078 | Iustin Pop | self._LockInstancesNodes()
|
331 | c4a2fee1 | Guido Trotter |
|
332 | a82ce292 | Guido Trotter | @type primary_only: boolean
|
333 | a82ce292 | Guido Trotter | @param primary_only: only lock primary nodes of locked instances
|
334 | a82ce292 | Guido Trotter |
|
335 | c4a2fee1 | Guido Trotter | """
|
336 | c4a2fee1 | Guido Trotter | assert locking.LEVEL_NODE in self.recalculate_locks, \ |
337 | c4a2fee1 | Guido Trotter | "_LockInstancesNodes helper function called with no nodes to recalculate"
|
338 | c4a2fee1 | Guido Trotter | |
339 | c4a2fee1 | Guido Trotter | # TODO: check if we're really been called with the instance locks held
|
340 | c4a2fee1 | Guido Trotter | |
341 | c4a2fee1 | Guido Trotter | # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
|
342 | c4a2fee1 | Guido Trotter | # future we might want to have different behaviors depending on the value
|
343 | c4a2fee1 | Guido Trotter | # of self.recalculate_locks[locking.LEVEL_NODE]
|
344 | c4a2fee1 | Guido Trotter | wanted_nodes = [] |
345 | 6683bba2 | Guido Trotter | for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]: |
346 | c4a2fee1 | Guido Trotter | instance = self.context.cfg.GetInstanceInfo(instance_name)
|
347 | c4a2fee1 | Guido Trotter | wanted_nodes.append(instance.primary_node) |
348 | a82ce292 | Guido Trotter | if not primary_only: |
349 | a82ce292 | Guido Trotter | wanted_nodes.extend(instance.secondary_nodes) |
350 | 9513b6ab | Guido Trotter | |
351 | 9513b6ab | Guido Trotter | if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE: |
352 | 9513b6ab | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
|
353 | 9513b6ab | Guido Trotter | elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND: |
354 | 9513b6ab | Guido Trotter | self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
|
355 | c4a2fee1 | Guido Trotter | |
356 | c4a2fee1 | Guido Trotter | del self.recalculate_locks[locking.LEVEL_NODE] |
357 | c4a2fee1 | Guido Trotter | |
358 | a8083063 | Iustin Pop | |
359 | fe267188 | Iustin Pop | class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223 |
360 | a8083063 | Iustin Pop | """Simple LU which runs no hooks.
|
361 | a8083063 | Iustin Pop |
|
362 | a8083063 | Iustin Pop | This LU is intended as a parent for other LogicalUnits which will
|
363 | a8083063 | Iustin Pop | run no hooks, in order to reduce duplicate code.
|
364 | a8083063 | Iustin Pop |
|
365 | a8083063 | Iustin Pop | """
|
366 | a8083063 | Iustin Pop | HPATH = None
|
367 | a8083063 | Iustin Pop | HTYPE = None
|
368 | a8083063 | Iustin Pop | |
369 | fc8a6b8f | Iustin Pop | def BuildHooksEnv(self): |
370 | fc8a6b8f | Iustin Pop | """Empty BuildHooksEnv for NoHooksLu.
|
371 | fc8a6b8f | Iustin Pop |
|
372 | fc8a6b8f | Iustin Pop | This just raises an error.
|
373 | fc8a6b8f | Iustin Pop |
|
374 | fc8a6b8f | Iustin Pop | """
|
375 | fc8a6b8f | Iustin Pop | assert False, "BuildHooksEnv called for NoHooksLUs" |
376 | fc8a6b8f | Iustin Pop | |
377 | a8083063 | Iustin Pop | |
378 | 9a6800e1 | Michael Hanselmann | class Tasklet: |
379 | 9a6800e1 | Michael Hanselmann | """Tasklet base class.
|
380 | 9a6800e1 | Michael Hanselmann |
|
381 | 9a6800e1 | Michael Hanselmann | Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
|
382 | 9a6800e1 | Michael Hanselmann | they can mix legacy code with tasklets. Locking needs to be done in the LU,
|
383 | 9a6800e1 | Michael Hanselmann | tasklets know nothing about locks.
|
384 | 9a6800e1 | Michael Hanselmann |
|
385 | 9a6800e1 | Michael Hanselmann | Subclasses must follow these rules:
|
386 | 9a6800e1 | Michael Hanselmann | - Implement CheckPrereq
|
387 | 9a6800e1 | Michael Hanselmann | - Implement Exec
|
388 | 9a6800e1 | Michael Hanselmann |
|
389 | 9a6800e1 | Michael Hanselmann | """
|
390 | 464243a7 | Michael Hanselmann | def __init__(self, lu): |
391 | 464243a7 | Michael Hanselmann | self.lu = lu
|
392 | 464243a7 | Michael Hanselmann | |
393 | 464243a7 | Michael Hanselmann | # Shortcuts
|
394 | 464243a7 | Michael Hanselmann | self.cfg = lu.cfg
|
395 | 464243a7 | Michael Hanselmann | self.rpc = lu.rpc
|
396 | 464243a7 | Michael Hanselmann | |
397 | 9a6800e1 | Michael Hanselmann | def CheckPrereq(self): |
398 | 9a6800e1 | Michael Hanselmann | """Check prerequisites for this tasklets.
|
399 | 9a6800e1 | Michael Hanselmann |
|
400 | 9a6800e1 | Michael Hanselmann | This method should check whether the prerequisites for the execution of
|
401 | 9a6800e1 | Michael Hanselmann | this tasklet are fulfilled. It can do internode communication, but it
|
402 | 9a6800e1 | Michael Hanselmann | should be idempotent - no cluster or system changes are allowed.
|
403 | 9a6800e1 | Michael Hanselmann |
|
404 | 9a6800e1 | Michael Hanselmann | The method should raise errors.OpPrereqError in case something is not
|
405 | 9a6800e1 | Michael Hanselmann | fulfilled. Its return value is ignored.
|
406 | 9a6800e1 | Michael Hanselmann |
|
407 | 9a6800e1 | Michael Hanselmann | This method should also update all parameters to their canonical form if it
|
408 | 9a6800e1 | Michael Hanselmann | hasn't been done before.
|
409 | 9a6800e1 | Michael Hanselmann |
|
410 | 9a6800e1 | Michael Hanselmann | """
|
411 | 9a6800e1 | Michael Hanselmann | raise NotImplementedError |
412 | 9a6800e1 | Michael Hanselmann | |
413 | 9a6800e1 | Michael Hanselmann | def Exec(self, feedback_fn): |
414 | 9a6800e1 | Michael Hanselmann | """Execute the tasklet.
|
415 | 9a6800e1 | Michael Hanselmann |
|
416 | 9a6800e1 | Michael Hanselmann | This method should implement the actual work. It should raise
|
417 | 9a6800e1 | Michael Hanselmann | errors.OpExecError for failures that are somewhat dealt with in code, or
|
418 | 9a6800e1 | Michael Hanselmann | expected.
|
419 | 9a6800e1 | Michael Hanselmann |
|
420 | 9a6800e1 | Michael Hanselmann | """
|
421 | 9a6800e1 | Michael Hanselmann | raise NotImplementedError |
422 | 9a6800e1 | Michael Hanselmann | |
423 | 9a6800e1 | Michael Hanselmann | |
424 | dcb93971 | Michael Hanselmann | def _GetWantedNodes(lu, nodes): |
425 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded node names.
|
426 | 83120a01 | Michael Hanselmann |
|
427 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
428 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
429 | e4376078 | Iustin Pop | @type nodes: list
|
430 | e4376078 | Iustin Pop | @param nodes: list of node names or None for all nodes
|
431 | e4376078 | Iustin Pop | @rtype: list
|
432 | e4376078 | Iustin Pop | @return: the list of nodes, sorted
|
433 | 083a91c9 | Iustin Pop | @raise errors.ProgrammerError: if the nodes parameter is wrong type
|
434 | 83120a01 | Michael Hanselmann |
|
435 | 83120a01 | Michael Hanselmann | """
|
436 | 3312b702 | Iustin Pop | if not isinstance(nodes, list): |
437 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'nodes'", |
438 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
439 | dcb93971 | Michael Hanselmann | |
440 | ea47808a | Guido Trotter | if not nodes: |
441 | ea47808a | Guido Trotter | raise errors.ProgrammerError("_GetWantedNodes should only be called with a" |
442 | ea47808a | Guido Trotter | " non-empty list of nodes whose name is to be expanded.")
|
443 | dcb93971 | Michael Hanselmann | |
444 | 61dabca4 | Iustin Pop | wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes] |
445 | a7ba5e53 | Iustin Pop | return utils.NiceSort(wanted)
|
446 | 3312b702 | Iustin Pop | |
447 | 3312b702 | Iustin Pop | |
448 | 3312b702 | Iustin Pop | def _GetWantedInstances(lu, instances): |
449 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded instance names.
|
450 | 3312b702 | Iustin Pop |
|
451 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
452 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
453 | e4376078 | Iustin Pop | @type instances: list
|
454 | e4376078 | Iustin Pop | @param instances: list of instance names or None for all instances
|
455 | e4376078 | Iustin Pop | @rtype: list
|
456 | e4376078 | Iustin Pop | @return: the list of instances, sorted
|
457 | e4376078 | Iustin Pop | @raise errors.OpPrereqError: if the instances parameter is wrong type
|
458 | e4376078 | Iustin Pop | @raise errors.OpPrereqError: if any of the passed instances is not found
|
459 | 3312b702 | Iustin Pop |
|
460 | 3312b702 | Iustin Pop | """
|
461 | 3312b702 | Iustin Pop | if not isinstance(instances, list): |
462 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'", |
463 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
464 | 3312b702 | Iustin Pop | |
465 | 3312b702 | Iustin Pop | if instances:
|
466 | cf26a87a | Iustin Pop | wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances] |
467 | 3312b702 | Iustin Pop | else:
|
468 | a7f5dc98 | Iustin Pop | wanted = utils.NiceSort(lu.cfg.GetInstanceList()) |
469 | a7f5dc98 | Iustin Pop | return wanted
|
470 | dcb93971 | Michael Hanselmann | |
471 | dcb93971 | Michael Hanselmann | |
472 | dcb93971 | Michael Hanselmann | def _CheckOutputFields(static, dynamic, selected): |
473 | 83120a01 | Michael Hanselmann | """Checks whether all selected fields are valid.
|
474 | 83120a01 | Michael Hanselmann |
|
475 | a2d2e1a7 | Iustin Pop | @type static: L{utils.FieldSet}
|
476 | 31bf511f | Iustin Pop | @param static: static fields set
|
477 | a2d2e1a7 | Iustin Pop | @type dynamic: L{utils.FieldSet}
|
478 | 31bf511f | Iustin Pop | @param dynamic: dynamic fields set
|
479 | 83120a01 | Michael Hanselmann |
|
480 | 83120a01 | Michael Hanselmann | """
|
481 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet() |
482 | 31bf511f | Iustin Pop | f.Extend(static) |
483 | 31bf511f | Iustin Pop | f.Extend(dynamic) |
484 | dcb93971 | Michael Hanselmann | |
485 | 31bf511f | Iustin Pop | delta = f.NonMatching(selected) |
486 | 31bf511f | Iustin Pop | if delta:
|
487 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Unknown output fields selected: %s" |
488 | 5c983ee5 | Iustin Pop | % ",".join(delta), errors.ECODE_INVAL)
|
489 | dcb93971 | Michael Hanselmann | |
490 | dcb93971 | Michael Hanselmann | |
491 | a5961235 | Iustin Pop | def _CheckBooleanOpField(op, name): |
492 | a5961235 | Iustin Pop | """Validates boolean opcode parameters.
|
493 | a5961235 | Iustin Pop |
|
494 | a5961235 | Iustin Pop | This will ensure that an opcode parameter is either a boolean value,
|
495 | a5961235 | Iustin Pop | or None (but that it always exists).
|
496 | a5961235 | Iustin Pop |
|
497 | a5961235 | Iustin Pop | """
|
498 | a5961235 | Iustin Pop | val = getattr(op, name, None) |
499 | a5961235 | Iustin Pop | if not (val is None or isinstance(val, bool)): |
500 | a5961235 | Iustin Pop | raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" % |
501 | 5c983ee5 | Iustin Pop | (name, str(val)), errors.ECODE_INVAL)
|
502 | a5961235 | Iustin Pop | setattr(op, name, val)
|
503 | a5961235 | Iustin Pop | |
504 | a5961235 | Iustin Pop | |
505 | 7736a5f2 | Iustin Pop | def _CheckGlobalHvParams(params): |
506 | 7736a5f2 | Iustin Pop | """Validates that given hypervisor params are not global ones.
|
507 | 7736a5f2 | Iustin Pop |
|
508 | 7736a5f2 | Iustin Pop | This will ensure that instances don't get customised versions of
|
509 | 7736a5f2 | Iustin Pop | global params.
|
510 | 7736a5f2 | Iustin Pop |
|
511 | 7736a5f2 | Iustin Pop | """
|
512 | 7736a5f2 | Iustin Pop | used_globals = constants.HVC_GLOBALS.intersection(params) |
513 | 7736a5f2 | Iustin Pop | if used_globals:
|
514 | 7736a5f2 | Iustin Pop | msg = ("The following hypervisor parameters are global and cannot"
|
515 | 7736a5f2 | Iustin Pop | " be customized at instance level, please modify them at"
|
516 | 1f864b60 | Iustin Pop | " cluster level: %s" % utils.CommaJoin(used_globals))
|
517 | 7736a5f2 | Iustin Pop | raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
518 | 7736a5f2 | Iustin Pop | |
519 | 7736a5f2 | Iustin Pop | |
520 | a5961235 | Iustin Pop | def _CheckNodeOnline(lu, node): |
521 | a5961235 | Iustin Pop | """Ensure that a given node is online.
|
522 | a5961235 | Iustin Pop |
|
523 | a5961235 | Iustin Pop | @param lu: the LU on behalf of which we make the check
|
524 | a5961235 | Iustin Pop | @param node: the node to check
|
525 | 733a2b6a | Iustin Pop | @raise errors.OpPrereqError: if the node is offline
|
526 | a5961235 | Iustin Pop |
|
527 | a5961235 | Iustin Pop | """
|
528 | a5961235 | Iustin Pop | if lu.cfg.GetNodeInfo(node).offline:
|
529 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Can't use offline node %s" % node, |
530 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
531 | a5961235 | Iustin Pop | |
532 | a5961235 | Iustin Pop | |
533 | 733a2b6a | Iustin Pop | def _CheckNodeNotDrained(lu, node): |
534 | 733a2b6a | Iustin Pop | """Ensure that a given node is not drained.
|
535 | 733a2b6a | Iustin Pop |
|
536 | 733a2b6a | Iustin Pop | @param lu: the LU on behalf of which we make the check
|
537 | 733a2b6a | Iustin Pop | @param node: the node to check
|
538 | 733a2b6a | Iustin Pop | @raise errors.OpPrereqError: if the node is drained
|
539 | 733a2b6a | Iustin Pop |
|
540 | 733a2b6a | Iustin Pop | """
|
541 | 733a2b6a | Iustin Pop | if lu.cfg.GetNodeInfo(node).drained:
|
542 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Can't use drained node %s" % node, |
543 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
544 | 733a2b6a | Iustin Pop | |
545 | 733a2b6a | Iustin Pop | |
546 | 231cd901 | Iustin Pop | def _CheckNodeHasOS(lu, node, os_name, force_variant): |
547 | 231cd901 | Iustin Pop | """Ensure that a node supports a given OS.
|
548 | 231cd901 | Iustin Pop |
|
549 | 231cd901 | Iustin Pop | @param lu: the LU on behalf of which we make the check
|
550 | 231cd901 | Iustin Pop | @param node: the node to check
|
551 | 231cd901 | Iustin Pop | @param os_name: the OS to query about
|
552 | 231cd901 | Iustin Pop | @param force_variant: whether to ignore variant errors
|
553 | 231cd901 | Iustin Pop | @raise errors.OpPrereqError: if the node is not supporting the OS
|
554 | 231cd901 | Iustin Pop |
|
555 | 231cd901 | Iustin Pop | """
|
556 | 231cd901 | Iustin Pop | result = lu.rpc.call_os_get(node, os_name) |
557 | 231cd901 | Iustin Pop | result.Raise("OS '%s' not in supported OS list for node %s" %
|
558 | 231cd901 | Iustin Pop | (os_name, node), |
559 | 231cd901 | Iustin Pop | prereq=True, ecode=errors.ECODE_INVAL)
|
560 | 231cd901 | Iustin Pop | if not force_variant: |
561 | 231cd901 | Iustin Pop | _CheckOSVariant(result.payload, os_name) |
562 | 231cd901 | Iustin Pop | |
563 | 231cd901 | Iustin Pop | |
564 | 0e3baaf3 | Iustin Pop | def _RequireFileStorage(): |
565 | 0e3baaf3 | Iustin Pop | """Checks that file storage is enabled.
|
566 | 0e3baaf3 | Iustin Pop |
|
567 | 0e3baaf3 | Iustin Pop | @raise errors.OpPrereqError: when file storage is disabled
|
568 | 0e3baaf3 | Iustin Pop |
|
569 | 0e3baaf3 | Iustin Pop | """
|
570 | 0e3baaf3 | Iustin Pop | if not constants.ENABLE_FILE_STORAGE: |
571 | 0e3baaf3 | Iustin Pop | raise errors.OpPrereqError("File storage disabled at configure time", |
572 | 0e3baaf3 | Iustin Pop | errors.ECODE_INVAL) |
573 | 0e3baaf3 | Iustin Pop | |
574 | 0e3baaf3 | Iustin Pop | |
575 | 5d55819e | Iustin Pop | def _CheckDiskTemplate(template): |
576 | 5d55819e | Iustin Pop | """Ensure a given disk template is valid.
|
577 | 5d55819e | Iustin Pop |
|
578 | 5d55819e | Iustin Pop | """
|
579 | 5d55819e | Iustin Pop | if template not in constants.DISK_TEMPLATES: |
580 | 5d55819e | Iustin Pop | msg = ("Invalid disk template name '%s', valid templates are: %s" %
|
581 | 5d55819e | Iustin Pop | (template, utils.CommaJoin(constants.DISK_TEMPLATES))) |
582 | 5d55819e | Iustin Pop | raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
583 | 0e3baaf3 | Iustin Pop | if template == constants.DT_FILE:
|
584 | 0e3baaf3 | Iustin Pop | _RequireFileStorage() |
585 | 0e3baaf3 | Iustin Pop | |
586 | 0e3baaf3 | Iustin Pop | |
587 | 0e3baaf3 | Iustin Pop | def _CheckStorageType(storage_type): |
588 | 0e3baaf3 | Iustin Pop | """Ensure a given storage type is valid.
|
589 | 0e3baaf3 | Iustin Pop |
|
590 | 0e3baaf3 | Iustin Pop | """
|
591 | 0e3baaf3 | Iustin Pop | if storage_type not in constants.VALID_STORAGE_TYPES: |
592 | 0e3baaf3 | Iustin Pop | raise errors.OpPrereqError("Unknown storage type: %s" % storage_type, |
593 | f276c4b5 | Iustin Pop | errors.ECODE_INVAL) |
594 | 0e3baaf3 | Iustin Pop | if storage_type == constants.ST_FILE:
|
595 | 0e3baaf3 | Iustin Pop | _RequireFileStorage() |
596 | 0e3baaf3 | Iustin Pop | |
597 | 5d55819e | Iustin Pop | |
598 | 5d55819e | Iustin Pop | |
599 | 31624382 | Iustin Pop | def _CheckInstanceDown(lu, instance, reason): |
600 | 31624382 | Iustin Pop | """Ensure that an instance is not running."""
|
601 | 31624382 | Iustin Pop | if instance.admin_up:
|
602 | 31624382 | Iustin Pop | raise errors.OpPrereqError("Instance %s is marked to be up, %s" % |
603 | 31624382 | Iustin Pop | (instance.name, reason), errors.ECODE_STATE) |
604 | 31624382 | Iustin Pop | |
605 | 31624382 | Iustin Pop | pnode = instance.primary_node |
606 | 31624382 | Iustin Pop | ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode] |
607 | 31624382 | Iustin Pop | ins_l.Raise("Can't contact node %s for instance information" % pnode,
|
608 | 31624382 | Iustin Pop | prereq=True, ecode=errors.ECODE_ENVIRON)
|
609 | 31624382 | Iustin Pop | |
610 | 31624382 | Iustin Pop | if instance.name in ins_l.payload: |
611 | 31624382 | Iustin Pop | raise errors.OpPrereqError("Instance %s is running, %s" % |
612 | 31624382 | Iustin Pop | (instance.name, reason), errors.ECODE_STATE) |
613 | 31624382 | Iustin Pop | |
614 | 31624382 | Iustin Pop | |
615 | cf26a87a | Iustin Pop | def _ExpandItemName(fn, name, kind): |
616 | cf26a87a | Iustin Pop | """Expand an item name.
|
617 | cf26a87a | Iustin Pop |
|
618 | cf26a87a | Iustin Pop | @param fn: the function to use for expansion
|
619 | cf26a87a | Iustin Pop | @param name: requested item name
|
620 | cf26a87a | Iustin Pop | @param kind: text description ('Node' or 'Instance')
|
621 | cf26a87a | Iustin Pop | @return: the resolved (full) name
|
622 | cf26a87a | Iustin Pop | @raise errors.OpPrereqError: if the item is not found
|
623 | cf26a87a | Iustin Pop |
|
624 | cf26a87a | Iustin Pop | """
|
625 | cf26a87a | Iustin Pop | full_name = fn(name) |
626 | cf26a87a | Iustin Pop | if full_name is None: |
627 | cf26a87a | Iustin Pop | raise errors.OpPrereqError("%s '%s' not known" % (kind, name), |
628 | cf26a87a | Iustin Pop | errors.ECODE_NOENT) |
629 | cf26a87a | Iustin Pop | return full_name
|
630 | cf26a87a | Iustin Pop | |
631 | cf26a87a | Iustin Pop | |
632 | cf26a87a | Iustin Pop | def _ExpandNodeName(cfg, name): |
633 | cf26a87a | Iustin Pop | """Wrapper over L{_ExpandItemName} for nodes."""
|
634 | cf26a87a | Iustin Pop | return _ExpandItemName(cfg.ExpandNodeName, name, "Node") |
635 | cf26a87a | Iustin Pop | |
636 | cf26a87a | Iustin Pop | |
637 | cf26a87a | Iustin Pop | def _ExpandInstanceName(cfg, name): |
638 | cf26a87a | Iustin Pop | """Wrapper over L{_ExpandItemName} for instance."""
|
639 | cf26a87a | Iustin Pop | return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance") |
640 | cf26a87a | Iustin Pop | |
641 | cf26a87a | Iustin Pop | |
642 | ecb215b5 | Michael Hanselmann | def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, |
643 | 67fc3042 | Iustin Pop | memory, vcpus, nics, disk_template, disks, |
644 | 7c4d6c7b | Michael Hanselmann | bep, hvp, hypervisor_name): |
645 | e4376078 | Iustin Pop | """Builds instance related env variables for hooks
|
646 | e4376078 | Iustin Pop |
|
647 | e4376078 | Iustin Pop | This builds the hook environment from individual variables.
|
648 | e4376078 | Iustin Pop |
|
649 | e4376078 | Iustin Pop | @type name: string
|
650 | e4376078 | Iustin Pop | @param name: the name of the instance
|
651 | e4376078 | Iustin Pop | @type primary_node: string
|
652 | e4376078 | Iustin Pop | @param primary_node: the name of the instance's primary node
|
653 | e4376078 | Iustin Pop | @type secondary_nodes: list
|
654 | e4376078 | Iustin Pop | @param secondary_nodes: list of secondary nodes as strings
|
655 | e4376078 | Iustin Pop | @type os_type: string
|
656 | e4376078 | Iustin Pop | @param os_type: the name of the instance's OS
|
657 | 0d68c45d | Iustin Pop | @type status: boolean
|
658 | 0d68c45d | Iustin Pop | @param status: the should_run status of the instance
|
659 | e4376078 | Iustin Pop | @type memory: string
|
660 | e4376078 | Iustin Pop | @param memory: the memory size of the instance
|
661 | e4376078 | Iustin Pop | @type vcpus: string
|
662 | e4376078 | Iustin Pop | @param vcpus: the count of VCPUs the instance has
|
663 | e4376078 | Iustin Pop | @type nics: list
|
664 | 5e3d3eb3 | Guido Trotter | @param nics: list of tuples (ip, mac, mode, link) representing
|
665 | 5e3d3eb3 | Guido Trotter | the NICs the instance has
|
666 | 2c2690c9 | Iustin Pop | @type disk_template: string
|
667 | 5bbd3f7f | Michael Hanselmann | @param disk_template: the disk template of the instance
|
668 | 2c2690c9 | Iustin Pop | @type disks: list
|
669 | 2c2690c9 | Iustin Pop | @param disks: the list of (size, mode) pairs
|
670 | 67fc3042 | Iustin Pop | @type bep: dict
|
671 | 67fc3042 | Iustin Pop | @param bep: the backend parameters for the instance
|
672 | 67fc3042 | Iustin Pop | @type hvp: dict
|
673 | 67fc3042 | Iustin Pop | @param hvp: the hypervisor parameters for the instance
|
674 | 7c4d6c7b | Michael Hanselmann | @type hypervisor_name: string
|
675 | 7c4d6c7b | Michael Hanselmann | @param hypervisor_name: the hypervisor for the instance
|
676 | e4376078 | Iustin Pop | @rtype: dict
|
677 | e4376078 | Iustin Pop | @return: the hook environment for this instance
|
678 | ecb215b5 | Michael Hanselmann |
|
679 | 396e1b78 | Michael Hanselmann | """
|
680 | 0d68c45d | Iustin Pop | if status:
|
681 | 0d68c45d | Iustin Pop | str_status = "up"
|
682 | 0d68c45d | Iustin Pop | else:
|
683 | 0d68c45d | Iustin Pop | str_status = "down"
|
684 | 396e1b78 | Michael Hanselmann | env = { |
685 | 0e137c28 | Iustin Pop | "OP_TARGET": name,
|
686 | 396e1b78 | Michael Hanselmann | "INSTANCE_NAME": name,
|
687 | 396e1b78 | Michael Hanselmann | "INSTANCE_PRIMARY": primary_node,
|
688 | 396e1b78 | Michael Hanselmann | "INSTANCE_SECONDARIES": " ".join(secondary_nodes), |
689 | ecb215b5 | Michael Hanselmann | "INSTANCE_OS_TYPE": os_type,
|
690 | 0d68c45d | Iustin Pop | "INSTANCE_STATUS": str_status,
|
691 | 396e1b78 | Michael Hanselmann | "INSTANCE_MEMORY": memory,
|
692 | 396e1b78 | Michael Hanselmann | "INSTANCE_VCPUS": vcpus,
|
693 | 2c2690c9 | Iustin Pop | "INSTANCE_DISK_TEMPLATE": disk_template,
|
694 | 7c4d6c7b | Michael Hanselmann | "INSTANCE_HYPERVISOR": hypervisor_name,
|
695 | 396e1b78 | Michael Hanselmann | } |
696 | 396e1b78 | Michael Hanselmann | |
697 | 396e1b78 | Michael Hanselmann | if nics:
|
698 | 396e1b78 | Michael Hanselmann | nic_count = len(nics)
|
699 | 62f0dd02 | Guido Trotter | for idx, (ip, mac, mode, link) in enumerate(nics): |
700 | 396e1b78 | Michael Hanselmann | if ip is None: |
701 | 396e1b78 | Michael Hanselmann | ip = ""
|
702 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC%d_IP" % idx] = ip
|
703 | 2c2690c9 | Iustin Pop | env["INSTANCE_NIC%d_MAC" % idx] = mac
|
704 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_MODE" % idx] = mode
|
705 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_LINK" % idx] = link
|
706 | 62f0dd02 | Guido Trotter | if mode == constants.NIC_MODE_BRIDGED:
|
707 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_BRIDGE" % idx] = link
|
708 | 396e1b78 | Michael Hanselmann | else:
|
709 | 396e1b78 | Michael Hanselmann | nic_count = 0
|
710 | 396e1b78 | Michael Hanselmann | |
711 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC_COUNT"] = nic_count
|
712 | 396e1b78 | Michael Hanselmann | |
713 | 2c2690c9 | Iustin Pop | if disks:
|
714 | 2c2690c9 | Iustin Pop | disk_count = len(disks)
|
715 | 2c2690c9 | Iustin Pop | for idx, (size, mode) in enumerate(disks): |
716 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK%d_SIZE" % idx] = size
|
717 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK%d_MODE" % idx] = mode
|
718 | 2c2690c9 | Iustin Pop | else:
|
719 | 2c2690c9 | Iustin Pop | disk_count = 0
|
720 | 2c2690c9 | Iustin Pop | |
721 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK_COUNT"] = disk_count
|
722 | 2c2690c9 | Iustin Pop | |
723 | 67fc3042 | Iustin Pop | for source, kind in [(bep, "BE"), (hvp, "HV")]: |
724 | 67fc3042 | Iustin Pop | for key, value in source.items(): |
725 | 67fc3042 | Iustin Pop | env["INSTANCE_%s_%s" % (kind, key)] = value
|
726 | 67fc3042 | Iustin Pop | |
727 | 396e1b78 | Michael Hanselmann | return env
|
728 | 396e1b78 | Michael Hanselmann | |
729 | 96acbc09 | Michael Hanselmann | |
730 | f9b10246 | Guido Trotter | def _NICListToTuple(lu, nics): |
731 | 62f0dd02 | Guido Trotter | """Build a list of nic information tuples.
|
732 | 62f0dd02 | Guido Trotter |
|
733 | f9b10246 | Guido Trotter | This list is suitable to be passed to _BuildInstanceHookEnv or as a return
|
734 | f9b10246 | Guido Trotter | value in LUQueryInstanceData.
|
735 | 62f0dd02 | Guido Trotter |
|
736 | 62f0dd02 | Guido Trotter | @type lu: L{LogicalUnit}
|
737 | 62f0dd02 | Guido Trotter | @param lu: the logical unit on whose behalf we execute
|
738 | 62f0dd02 | Guido Trotter | @type nics: list of L{objects.NIC}
|
739 | 62f0dd02 | Guido Trotter | @param nics: list of nics to convert to hooks tuples
|
740 | 62f0dd02 | Guido Trotter |
|
741 | 62f0dd02 | Guido Trotter | """
|
742 | 62f0dd02 | Guido Trotter | hooks_nics = [] |
743 | 62f0dd02 | Guido Trotter | c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT] |
744 | 62f0dd02 | Guido Trotter | for nic in nics: |
745 | 62f0dd02 | Guido Trotter | ip = nic.ip |
746 | 62f0dd02 | Guido Trotter | mac = nic.mac |
747 | 62f0dd02 | Guido Trotter | filled_params = objects.FillDict(c_nicparams, nic.nicparams) |
748 | 62f0dd02 | Guido Trotter | mode = filled_params[constants.NIC_MODE] |
749 | 62f0dd02 | Guido Trotter | link = filled_params[constants.NIC_LINK] |
750 | 62f0dd02 | Guido Trotter | hooks_nics.append((ip, mac, mode, link)) |
751 | 62f0dd02 | Guido Trotter | return hooks_nics
|
752 | 396e1b78 | Michael Hanselmann | |
753 | 96acbc09 | Michael Hanselmann | |
754 | 338e51e8 | Iustin Pop | def _BuildInstanceHookEnvByObject(lu, instance, override=None): |
755 | ecb215b5 | Michael Hanselmann | """Builds instance related env variables for hooks from an object.
|
756 | ecb215b5 | Michael Hanselmann |
|
757 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
758 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
759 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
760 | e4376078 | Iustin Pop | @param instance: the instance for which we should build the
|
761 | e4376078 | Iustin Pop | environment
|
762 | e4376078 | Iustin Pop | @type override: dict
|
763 | e4376078 | Iustin Pop | @param override: dictionary with key/values that will override
|
764 | e4376078 | Iustin Pop | our values
|
765 | e4376078 | Iustin Pop | @rtype: dict
|
766 | e4376078 | Iustin Pop | @return: the hook environment dictionary
|
767 | e4376078 | Iustin Pop |
|
768 | ecb215b5 | Michael Hanselmann | """
|
769 | 67fc3042 | Iustin Pop | cluster = lu.cfg.GetClusterInfo() |
770 | 67fc3042 | Iustin Pop | bep = cluster.FillBE(instance) |
771 | 67fc3042 | Iustin Pop | hvp = cluster.FillHV(instance) |
772 | 396e1b78 | Michael Hanselmann | args = { |
773 | 396e1b78 | Michael Hanselmann | 'name': instance.name,
|
774 | 396e1b78 | Michael Hanselmann | 'primary_node': instance.primary_node,
|
775 | 396e1b78 | Michael Hanselmann | 'secondary_nodes': instance.secondary_nodes,
|
776 | ecb215b5 | Michael Hanselmann | 'os_type': instance.os,
|
777 | 0d68c45d | Iustin Pop | 'status': instance.admin_up,
|
778 | 338e51e8 | Iustin Pop | 'memory': bep[constants.BE_MEMORY],
|
779 | 338e51e8 | Iustin Pop | 'vcpus': bep[constants.BE_VCPUS],
|
780 | f9b10246 | Guido Trotter | 'nics': _NICListToTuple(lu, instance.nics),
|
781 | 2c2690c9 | Iustin Pop | 'disk_template': instance.disk_template,
|
782 | 2c2690c9 | Iustin Pop | 'disks': [(disk.size, disk.mode) for disk in instance.disks], |
783 | 67fc3042 | Iustin Pop | 'bep': bep,
|
784 | 67fc3042 | Iustin Pop | 'hvp': hvp,
|
785 | b0c63e2b | Iustin Pop | 'hypervisor_name': instance.hypervisor,
|
786 | 396e1b78 | Michael Hanselmann | } |
787 | 396e1b78 | Michael Hanselmann | if override:
|
788 | 396e1b78 | Michael Hanselmann | args.update(override) |
789 | 7260cfbe | Iustin Pop | return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142 |
790 | 396e1b78 | Michael Hanselmann | |
791 | 396e1b78 | Michael Hanselmann | |
792 | 44485f49 | Guido Trotter | def _AdjustCandidatePool(lu, exceptions): |
793 | ec0292f1 | Iustin Pop | """Adjust the candidate pool after node operations.
|
794 | ec0292f1 | Iustin Pop |
|
795 | ec0292f1 | Iustin Pop | """
|
796 | 44485f49 | Guido Trotter | mod_list = lu.cfg.MaintainCandidatePool(exceptions) |
797 | ec0292f1 | Iustin Pop | if mod_list:
|
798 | ec0292f1 | Iustin Pop | lu.LogInfo("Promoted nodes to master candidate role: %s",
|
799 | 1f864b60 | Iustin Pop | utils.CommaJoin(node.name for node in mod_list)) |
800 | ec0292f1 | Iustin Pop | for name in mod_list: |
801 | ec0292f1 | Iustin Pop | lu.context.ReaddNode(name) |
802 | 44485f49 | Guido Trotter | mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions) |
803 | ec0292f1 | Iustin Pop | if mc_now > mc_max:
|
804 | ec0292f1 | Iustin Pop | lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
|
805 | ec0292f1 | Iustin Pop | (mc_now, mc_max)) |
806 | ec0292f1 | Iustin Pop | |
807 | ec0292f1 | Iustin Pop | |
808 | 6d7e1f20 | Guido Trotter | def _DecideSelfPromotion(lu, exceptions=None): |
809 | 6d7e1f20 | Guido Trotter | """Decide whether I should promote myself as a master candidate.
|
810 | 6d7e1f20 | Guido Trotter |
|
811 | 6d7e1f20 | Guido Trotter | """
|
812 | 6d7e1f20 | Guido Trotter | cp_size = lu.cfg.GetClusterInfo().candidate_pool_size |
813 | 6d7e1f20 | Guido Trotter | mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions) |
814 | 6d7e1f20 | Guido Trotter | # the new node will increase mc_max with one, so:
|
815 | 6d7e1f20 | Guido Trotter | mc_should = min(mc_should + 1, cp_size) |
816 | 6d7e1f20 | Guido Trotter | return mc_now < mc_should
|
817 | 6d7e1f20 | Guido Trotter | |
818 | 6d7e1f20 | Guido Trotter | |
819 | b165e77e | Guido Trotter | def _CheckNicsBridgesExist(lu, target_nics, target_node, |
820 | b165e77e | Guido Trotter | profile=constants.PP_DEFAULT): |
821 | b165e77e | Guido Trotter | """Check that the brigdes needed by a list of nics exist.
|
822 | b165e77e | Guido Trotter |
|
823 | b165e77e | Guido Trotter | """
|
824 | b165e77e | Guido Trotter | c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile] |
825 | b165e77e | Guido Trotter | paramslist = [objects.FillDict(c_nicparams, nic.nicparams) |
826 | b165e77e | Guido Trotter | for nic in target_nics] |
827 | b165e77e | Guido Trotter | brlist = [params[constants.NIC_LINK] for params in paramslist |
828 | b165e77e | Guido Trotter | if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
|
829 | b165e77e | Guido Trotter | if brlist:
|
830 | b165e77e | Guido Trotter | result = lu.rpc.call_bridges_exist(target_node, brlist) |
831 | 4c4e4e1e | Iustin Pop | result.Raise("Error checking bridges on destination node '%s'" %
|
832 | 045dd6d9 | Iustin Pop | target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
|
833 | b165e77e | Guido Trotter | |
834 | b165e77e | Guido Trotter | |
835 | b165e77e | Guido Trotter | def _CheckInstanceBridgesExist(lu, instance, node=None): |
836 | bf6929a2 | Alexander Schreiber | """Check that the brigdes needed by an instance exist.
|
837 | bf6929a2 | Alexander Schreiber |
|
838 | bf6929a2 | Alexander Schreiber | """
|
839 | b165e77e | Guido Trotter | if node is None: |
840 | 29921401 | Iustin Pop | node = instance.primary_node |
841 | b165e77e | Guido Trotter | _CheckNicsBridgesExist(lu, instance.nics, node) |
842 | bf6929a2 | Alexander Schreiber | |
843 | bf6929a2 | Alexander Schreiber | |
844 | c6f1af07 | Iustin Pop | def _CheckOSVariant(os_obj, name): |
845 | f2c05717 | Guido Trotter | """Check whether an OS name conforms to the os variants specification.
|
846 | f2c05717 | Guido Trotter |
|
847 | c6f1af07 | Iustin Pop | @type os_obj: L{objects.OS}
|
848 | c6f1af07 | Iustin Pop | @param os_obj: OS object to check
|
849 | f2c05717 | Guido Trotter | @type name: string
|
850 | f2c05717 | Guido Trotter | @param name: OS name passed by the user, to check for validity
|
851 | f2c05717 | Guido Trotter |
|
852 | f2c05717 | Guido Trotter | """
|
853 | c6f1af07 | Iustin Pop | if not os_obj.supported_variants: |
854 | f2c05717 | Guido Trotter | return
|
855 | f2c05717 | Guido Trotter | try:
|
856 | f2c05717 | Guido Trotter | variant = name.split("+", 1)[1] |
857 | f2c05717 | Guido Trotter | except IndexError: |
858 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("OS name must include a variant", |
859 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
860 | f2c05717 | Guido Trotter | |
861 | c6f1af07 | Iustin Pop | if variant not in os_obj.supported_variants: |
862 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL) |
863 | f2c05717 | Guido Trotter | |
864 | f2c05717 | Guido Trotter | |
865 | 5ba9701d | Michael Hanselmann | def _GetNodeInstancesInner(cfg, fn): |
866 | 5ba9701d | Michael Hanselmann | return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)] |
867 | 5ba9701d | Michael Hanselmann | |
868 | 5ba9701d | Michael Hanselmann | |
869 | e9721add | Michael Hanselmann | def _GetNodeInstances(cfg, node_name): |
870 | e9721add | Michael Hanselmann | """Returns a list of all primary and secondary instances on a node.
|
871 | e9721add | Michael Hanselmann |
|
872 | e9721add | Michael Hanselmann | """
|
873 | e9721add | Michael Hanselmann | |
874 | e9721add | Michael Hanselmann | return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes) |
875 | e9721add | Michael Hanselmann | |
876 | e9721add | Michael Hanselmann | |
877 | 80cb875c | Michael Hanselmann | def _GetNodePrimaryInstances(cfg, node_name): |
878 | 80cb875c | Michael Hanselmann | """Returns primary instances on a node.
|
879 | 80cb875c | Michael Hanselmann |
|
880 | 80cb875c | Michael Hanselmann | """
|
881 | 5ba9701d | Michael Hanselmann | return _GetNodeInstancesInner(cfg,
|
882 | 5ba9701d | Michael Hanselmann | lambda inst: node_name == inst.primary_node)
|
883 | 80cb875c | Michael Hanselmann | |
884 | 80cb875c | Michael Hanselmann | |
885 | 692738fc | Michael Hanselmann | def _GetNodeSecondaryInstances(cfg, node_name): |
886 | 692738fc | Michael Hanselmann | """Returns secondary instances on a node.
|
887 | 692738fc | Michael Hanselmann |
|
888 | 692738fc | Michael Hanselmann | """
|
889 | 5ba9701d | Michael Hanselmann | return _GetNodeInstancesInner(cfg,
|
890 | 5ba9701d | Michael Hanselmann | lambda inst: node_name in inst.secondary_nodes) |
891 | 692738fc | Michael Hanselmann | |
892 | 692738fc | Michael Hanselmann | |
893 | efb8da02 | Michael Hanselmann | def _GetStorageTypeArgs(cfg, storage_type): |
894 | efb8da02 | Michael Hanselmann | """Returns the arguments for a storage type.
|
895 | efb8da02 | Michael Hanselmann |
|
896 | efb8da02 | Michael Hanselmann | """
|
897 | efb8da02 | Michael Hanselmann | # Special case for file storage
|
898 | efb8da02 | Michael Hanselmann | if storage_type == constants.ST_FILE:
|
899 | a4d138b7 | Michael Hanselmann | # storage.FileStorage wants a list of storage directories
|
900 | a4d138b7 | Michael Hanselmann | return [[cfg.GetFileStorageDir()]]
|
901 | efb8da02 | Michael Hanselmann | |
902 | efb8da02 | Michael Hanselmann | return []
|
903 | efb8da02 | Michael Hanselmann | |
904 | efb8da02 | Michael Hanselmann | |
905 | 2d9005d8 | Michael Hanselmann | def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq): |
906 | 2d9005d8 | Michael Hanselmann | faulty = [] |
907 | 2d9005d8 | Michael Hanselmann | |
908 | 2d9005d8 | Michael Hanselmann | for dev in instance.disks: |
909 | 2d9005d8 | Michael Hanselmann | cfg.SetDiskID(dev, node_name) |
910 | 2d9005d8 | Michael Hanselmann | |
911 | 2d9005d8 | Michael Hanselmann | result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks) |
912 | 2d9005d8 | Michael Hanselmann | result.Raise("Failed to get disk status from node %s" % node_name,
|
913 | 045dd6d9 | Iustin Pop | prereq=prereq, ecode=errors.ECODE_ENVIRON) |
914 | 2d9005d8 | Michael Hanselmann | |
915 | 2d9005d8 | Michael Hanselmann | for idx, bdev_status in enumerate(result.payload): |
916 | 2d9005d8 | Michael Hanselmann | if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY: |
917 | 2d9005d8 | Michael Hanselmann | faulty.append(idx) |
918 | 2d9005d8 | Michael Hanselmann | |
919 | 2d9005d8 | Michael Hanselmann | return faulty
|
920 | 2d9005d8 | Michael Hanselmann | |
921 | 2d9005d8 | Michael Hanselmann | |
922 | b98bf262 | Michael Hanselmann | def _FormatTimestamp(secs): |
923 | b98bf262 | Michael Hanselmann | """Formats a Unix timestamp with the local timezone.
|
924 | b98bf262 | Michael Hanselmann |
|
925 | b98bf262 | Michael Hanselmann | """
|
926 | b98bf262 | Michael Hanselmann | return time.strftime("%F %T %Z", time.gmtime(secs)) |
927 | b98bf262 | Michael Hanselmann | |
928 | b98bf262 | Michael Hanselmann | |
929 | b5f5fae9 | Luca Bigliardi | class LUPostInitCluster(LogicalUnit): |
930 | b5f5fae9 | Luca Bigliardi | """Logical unit for running hooks after cluster initialization.
|
931 | b5f5fae9 | Luca Bigliardi |
|
932 | b5f5fae9 | Luca Bigliardi | """
|
933 | b5f5fae9 | Luca Bigliardi | HPATH = "cluster-init"
|
934 | b5f5fae9 | Luca Bigliardi | HTYPE = constants.HTYPE_CLUSTER |
935 | b5f5fae9 | Luca Bigliardi | _OP_REQP = [] |
936 | b5f5fae9 | Luca Bigliardi | |
937 | b5f5fae9 | Luca Bigliardi | def BuildHooksEnv(self): |
938 | b5f5fae9 | Luca Bigliardi | """Build hooks env.
|
939 | b5f5fae9 | Luca Bigliardi |
|
940 | b5f5fae9 | Luca Bigliardi | """
|
941 | b5f5fae9 | Luca Bigliardi | env = {"OP_TARGET": self.cfg.GetClusterName()} |
942 | b5f5fae9 | Luca Bigliardi | mn = self.cfg.GetMasterNode()
|
943 | b5f5fae9 | Luca Bigliardi | return env, [], [mn]
|
944 | b5f5fae9 | Luca Bigliardi | |
945 | b5f5fae9 | Luca Bigliardi | def CheckPrereq(self): |
946 | b5f5fae9 | Luca Bigliardi | """No prerequisites to check.
|
947 | b5f5fae9 | Luca Bigliardi |
|
948 | b5f5fae9 | Luca Bigliardi | """
|
949 | b5f5fae9 | Luca Bigliardi | return True |
950 | b5f5fae9 | Luca Bigliardi | |
951 | b5f5fae9 | Luca Bigliardi | def Exec(self, feedback_fn): |
952 | b5f5fae9 | Luca Bigliardi | """Nothing to do.
|
953 | b5f5fae9 | Luca Bigliardi |
|
954 | b5f5fae9 | Luca Bigliardi | """
|
955 | b5f5fae9 | Luca Bigliardi | return True |
956 | b5f5fae9 | Luca Bigliardi | |
957 | b5f5fae9 | Luca Bigliardi | |
958 | b2c750a4 | Luca Bigliardi | class LUDestroyCluster(LogicalUnit): |
959 | a8083063 | Iustin Pop | """Logical unit for destroying the cluster.
|
960 | a8083063 | Iustin Pop |
|
961 | a8083063 | Iustin Pop | """
|
962 | b2c750a4 | Luca Bigliardi | HPATH = "cluster-destroy"
|
963 | b2c750a4 | Luca Bigliardi | HTYPE = constants.HTYPE_CLUSTER |
964 | a8083063 | Iustin Pop | _OP_REQP = [] |
965 | a8083063 | Iustin Pop | |
966 | b2c750a4 | Luca Bigliardi | def BuildHooksEnv(self): |
967 | b2c750a4 | Luca Bigliardi | """Build hooks env.
|
968 | b2c750a4 | Luca Bigliardi |
|
969 | b2c750a4 | Luca Bigliardi | """
|
970 | b2c750a4 | Luca Bigliardi | env = {"OP_TARGET": self.cfg.GetClusterName()} |
971 | b2c750a4 | Luca Bigliardi | return env, [], []
|
972 | b2c750a4 | Luca Bigliardi | |
973 | a8083063 | Iustin Pop | def CheckPrereq(self): |
974 | a8083063 | Iustin Pop | """Check prerequisites.
|
975 | a8083063 | Iustin Pop |
|
976 | a8083063 | Iustin Pop | This checks whether the cluster is empty.
|
977 | a8083063 | Iustin Pop |
|
978 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
979 | a8083063 | Iustin Pop |
|
980 | a8083063 | Iustin Pop | """
|
981 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
982 | a8083063 | Iustin Pop | |
983 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
984 | db915bd1 | Michael Hanselmann | if len(nodelist) != 1 or nodelist[0] != master: |
985 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d node(s) in" |
986 | 5c983ee5 | Iustin Pop | " this cluster." % (len(nodelist) - 1), |
987 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
988 | db915bd1 | Michael Hanselmann | instancelist = self.cfg.GetInstanceList()
|
989 | db915bd1 | Michael Hanselmann | if instancelist:
|
990 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d instance(s) in" |
991 | 5c983ee5 | Iustin Pop | " this cluster." % len(instancelist), |
992 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
993 | a8083063 | Iustin Pop | |
994 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
995 | a8083063 | Iustin Pop | """Destroys the cluster.
|
996 | a8083063 | Iustin Pop |
|
997 | a8083063 | Iustin Pop | """
|
998 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
999 | b989b9d9 | Ken Wehr | modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
|
1000 | 3141ad3b | Luca Bigliardi | |
1001 | 3141ad3b | Luca Bigliardi | # Run post hooks on master node before it's removed
|
1002 | 3141ad3b | Luca Bigliardi | hm = self.proc.hmclass(self.rpc.call_hooks_runner, self) |
1003 | 3141ad3b | Luca Bigliardi | try:
|
1004 | 3141ad3b | Luca Bigliardi | hm.RunPhase(constants.HOOKS_PHASE_POST, [master]) |
1005 | 3141ad3b | Luca Bigliardi | except:
|
1006 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0702
|
1007 | 3141ad3b | Luca Bigliardi | self.LogWarning("Errors occurred running hooks on %s" % master) |
1008 | 3141ad3b | Luca Bigliardi | |
1009 | 781de953 | Iustin Pop | result = self.rpc.call_node_stop_master(master, False) |
1010 | 4c4e4e1e | Iustin Pop | result.Raise("Could not disable the master role")
|
1011 | b989b9d9 | Ken Wehr | |
1012 | b989b9d9 | Ken Wehr | if modify_ssh_setup:
|
1013 | b989b9d9 | Ken Wehr | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
1014 | b989b9d9 | Ken Wehr | utils.CreateBackup(priv_key) |
1015 | b989b9d9 | Ken Wehr | utils.CreateBackup(pub_key) |
1016 | b989b9d9 | Ken Wehr | |
1017 | 140aa4a8 | Iustin Pop | return master
|
1018 | a8083063 | Iustin Pop | |
1019 | a8083063 | Iustin Pop | |
1020 | b98bf262 | Michael Hanselmann | def _VerifyCertificateInner(filename, expired, not_before, not_after, now, |
1021 | b98bf262 | Michael Hanselmann | warn_days=constants.SSL_CERT_EXPIRATION_WARN, |
1022 | b98bf262 | Michael Hanselmann | error_days=constants.SSL_CERT_EXPIRATION_ERROR): |
1023 | b98bf262 | Michael Hanselmann | """Verifies certificate details for LUVerifyCluster.
|
1024 | b98bf262 | Michael Hanselmann |
|
1025 | b98bf262 | Michael Hanselmann | """
|
1026 | b98bf262 | Michael Hanselmann | if expired:
|
1027 | b98bf262 | Michael Hanselmann | msg = "Certificate %s is expired" % filename
|
1028 | b98bf262 | Michael Hanselmann | |
1029 | b98bf262 | Michael Hanselmann | if not_before is not None and not_after is not None: |
1030 | b98bf262 | Michael Hanselmann | msg += (" (valid from %s to %s)" %
|
1031 | b98bf262 | Michael Hanselmann | (_FormatTimestamp(not_before), |
1032 | b98bf262 | Michael Hanselmann | _FormatTimestamp(not_after))) |
1033 | b98bf262 | Michael Hanselmann | elif not_before is not None: |
1034 | b98bf262 | Michael Hanselmann | msg += " (valid from %s)" % _FormatTimestamp(not_before)
|
1035 | b98bf262 | Michael Hanselmann | elif not_after is not None: |
1036 | b98bf262 | Michael Hanselmann | msg += " (valid until %s)" % _FormatTimestamp(not_after)
|
1037 | b98bf262 | Michael Hanselmann | |
1038 | b98bf262 | Michael Hanselmann | return (LUVerifyCluster.ETYPE_ERROR, msg)
|
1039 | b98bf262 | Michael Hanselmann | |
1040 | b98bf262 | Michael Hanselmann | elif not_before is not None and not_before > now: |
1041 | b98bf262 | Michael Hanselmann | return (LUVerifyCluster.ETYPE_WARNING,
|
1042 | b98bf262 | Michael Hanselmann | "Certificate %s not yet valid (valid from %s)" %
|
1043 | b98bf262 | Michael Hanselmann | (filename, _FormatTimestamp(not_before))) |
1044 | b98bf262 | Michael Hanselmann | |
1045 | b98bf262 | Michael Hanselmann | elif not_after is not None: |
1046 | b98bf262 | Michael Hanselmann | remaining_days = int((not_after - now) / (24 * 3600)) |
1047 | b98bf262 | Michael Hanselmann | |
1048 | b98bf262 | Michael Hanselmann | msg = ("Certificate %s expires in %d days" % (filename, remaining_days))
|
1049 | b98bf262 | Michael Hanselmann | |
1050 | b98bf262 | Michael Hanselmann | if remaining_days <= error_days:
|
1051 | b98bf262 | Michael Hanselmann | return (LUVerifyCluster.ETYPE_ERROR, msg)
|
1052 | b98bf262 | Michael Hanselmann | |
1053 | b98bf262 | Michael Hanselmann | if remaining_days <= warn_days:
|
1054 | b98bf262 | Michael Hanselmann | return (LUVerifyCluster.ETYPE_WARNING, msg)
|
1055 | b98bf262 | Michael Hanselmann | |
1056 | b98bf262 | Michael Hanselmann | return (None, None) |
1057 | b98bf262 | Michael Hanselmann | |
1058 | b98bf262 | Michael Hanselmann | |
1059 | b98bf262 | Michael Hanselmann | def _VerifyCertificate(filename): |
1060 | b98bf262 | Michael Hanselmann | """Verifies a certificate for LUVerifyCluster.
|
1061 | b98bf262 | Michael Hanselmann |
|
1062 | b98bf262 | Michael Hanselmann | @type filename: string
|
1063 | b98bf262 | Michael Hanselmann | @param filename: Path to PEM file
|
1064 | b98bf262 | Michael Hanselmann |
|
1065 | b98bf262 | Michael Hanselmann | """
|
1066 | b98bf262 | Michael Hanselmann | try:
|
1067 | b98bf262 | Michael Hanselmann | cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, |
1068 | b98bf262 | Michael Hanselmann | utils.ReadFile(filename)) |
1069 | b98bf262 | Michael Hanselmann | except Exception, err: # pylint: disable-msg=W0703 |
1070 | b98bf262 | Michael Hanselmann | return (LUVerifyCluster.ETYPE_ERROR,
|
1071 | b98bf262 | Michael Hanselmann | "Failed to load X509 certificate %s: %s" % (filename, err))
|
1072 | b98bf262 | Michael Hanselmann | |
1073 | b98bf262 | Michael Hanselmann | # Depending on the pyOpenSSL version, this can just return (None, None)
|
1074 | b98bf262 | Michael Hanselmann | (not_before, not_after) = utils.GetX509CertValidity(cert) |
1075 | b98bf262 | Michael Hanselmann | |
1076 | b98bf262 | Michael Hanselmann | return _VerifyCertificateInner(filename, cert.has_expired(),
|
1077 | b98bf262 | Michael Hanselmann | not_before, not_after, time.time()) |
1078 | b98bf262 | Michael Hanselmann | |
1079 | b98bf262 | Michael Hanselmann | |
1080 | d8fff41c | Guido Trotter | class LUVerifyCluster(LogicalUnit): |
1081 | a8083063 | Iustin Pop | """Verifies the cluster status.
|
1082 | a8083063 | Iustin Pop |
|
1083 | a8083063 | Iustin Pop | """
|
1084 | d8fff41c | Guido Trotter | HPATH = "cluster-verify"
|
1085 | d8fff41c | Guido Trotter | HTYPE = constants.HTYPE_CLUSTER |
1086 | a0c9776a | Iustin Pop | _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"] |
1087 | d4b9d97f | Guido Trotter | REQ_BGL = False
|
1088 | d4b9d97f | Guido Trotter | |
1089 | 7c874ee1 | Iustin Pop | TCLUSTER = "cluster"
|
1090 | 7c874ee1 | Iustin Pop | TNODE = "node"
|
1091 | 7c874ee1 | Iustin Pop | TINSTANCE = "instance"
|
1092 | 7c874ee1 | Iustin Pop | |
1093 | 7c874ee1 | Iustin Pop | ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
|
1094 | b98bf262 | Michael Hanselmann | ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
|
1095 | 7c874ee1 | Iustin Pop | EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
|
1096 | 7c874ee1 | Iustin Pop | EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
|
1097 | 7c874ee1 | Iustin Pop | EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
|
1098 | 7c874ee1 | Iustin Pop | EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
|
1099 | 7c874ee1 | Iustin Pop | EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
|
1100 | 7c874ee1 | Iustin Pop | EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
|
1101 | 7c874ee1 | Iustin Pop | ENODEDRBD = (TNODE, "ENODEDRBD")
|
1102 | 7c874ee1 | Iustin Pop | ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
|
1103 | 7c874ee1 | Iustin Pop | ENODEHOOKS = (TNODE, "ENODEHOOKS")
|
1104 | 7c874ee1 | Iustin Pop | ENODEHV = (TNODE, "ENODEHV")
|
1105 | 7c874ee1 | Iustin Pop | ENODELVM = (TNODE, "ENODELVM")
|
1106 | 7c874ee1 | Iustin Pop | ENODEN1 = (TNODE, "ENODEN1")
|
1107 | 7c874ee1 | Iustin Pop | ENODENET = (TNODE, "ENODENET")
|
1108 | 7c874ee1 | Iustin Pop | ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
|
1109 | 7c874ee1 | Iustin Pop | ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
|
1110 | 7c874ee1 | Iustin Pop | ENODERPC = (TNODE, "ENODERPC")
|
1111 | 7c874ee1 | Iustin Pop | ENODESSH = (TNODE, "ENODESSH")
|
1112 | 7c874ee1 | Iustin Pop | ENODEVERSION = (TNODE, "ENODEVERSION")
|
1113 | 7c0aa8e9 | Iustin Pop | ENODESETUP = (TNODE, "ENODESETUP")
|
1114 | 313b2dd4 | Michael Hanselmann | ENODETIME = (TNODE, "ENODETIME")
|
1115 | 7c874ee1 | Iustin Pop | |
1116 | a0c9776a | Iustin Pop | ETYPE_FIELD = "code"
|
1117 | a0c9776a | Iustin Pop | ETYPE_ERROR = "ERROR"
|
1118 | a0c9776a | Iustin Pop | ETYPE_WARNING = "WARNING"
|
1119 | a0c9776a | Iustin Pop | |
1120 | 02c521e4 | Iustin Pop | class NodeImage(object): |
1121 | 02c521e4 | Iustin Pop | """A class representing the logical and physical status of a node.
|
1122 | 02c521e4 | Iustin Pop |
|
1123 | 02c521e4 | Iustin Pop | @ivar volumes: a structure as returned from
|
1124 | 3a488770 | Iustin Pop | L{ganeti.backend.GetVolumeList} (runtime)
|
1125 | 02c521e4 | Iustin Pop | @ivar instances: a list of running instances (runtime)
|
1126 | 02c521e4 | Iustin Pop | @ivar pinst: list of configured primary instances (config)
|
1127 | 02c521e4 | Iustin Pop | @ivar sinst: list of configured secondary instances (config)
|
1128 | 02c521e4 | Iustin Pop | @ivar sbp: diction of {secondary-node: list of instances} of all peers
|
1129 | 02c521e4 | Iustin Pop | of this node (config)
|
1130 | 02c521e4 | Iustin Pop | @ivar mfree: free memory, as reported by hypervisor (runtime)
|
1131 | 02c521e4 | Iustin Pop | @ivar dfree: free disk, as reported by the node (runtime)
|
1132 | 02c521e4 | Iustin Pop | @ivar offline: the offline status (config)
|
1133 | 02c521e4 | Iustin Pop | @type rpc_fail: boolean
|
1134 | 02c521e4 | Iustin Pop | @ivar rpc_fail: whether the RPC verify call was successfull (overall,
|
1135 | 02c521e4 | Iustin Pop | not whether the individual keys were correct) (runtime)
|
1136 | 02c521e4 | Iustin Pop | @type lvm_fail: boolean
|
1137 | 02c521e4 | Iustin Pop | @ivar lvm_fail: whether the RPC call didn't return valid LVM data
|
1138 | 02c521e4 | Iustin Pop | @type hyp_fail: boolean
|
1139 | 02c521e4 | Iustin Pop | @ivar hyp_fail: whether the RPC call didn't return the instance list
|
1140 | 02c521e4 | Iustin Pop | @type ghost: boolean
|
1141 | 02c521e4 | Iustin Pop | @ivar ghost: whether this is a known node or not (config)
|
1142 | 02c521e4 | Iustin Pop |
|
1143 | 02c521e4 | Iustin Pop | """
|
1144 | 02c521e4 | Iustin Pop | def __init__(self, offline=False): |
1145 | 02c521e4 | Iustin Pop | self.volumes = {}
|
1146 | 02c521e4 | Iustin Pop | self.instances = []
|
1147 | 02c521e4 | Iustin Pop | self.pinst = []
|
1148 | 02c521e4 | Iustin Pop | self.sinst = []
|
1149 | 02c521e4 | Iustin Pop | self.sbp = {}
|
1150 | 02c521e4 | Iustin Pop | self.mfree = 0 |
1151 | 02c521e4 | Iustin Pop | self.dfree = 0 |
1152 | 02c521e4 | Iustin Pop | self.offline = offline
|
1153 | 02c521e4 | Iustin Pop | self.rpc_fail = False |
1154 | 02c521e4 | Iustin Pop | self.lvm_fail = False |
1155 | 02c521e4 | Iustin Pop | self.hyp_fail = False |
1156 | 02c521e4 | Iustin Pop | self.ghost = False |
1157 | 02c521e4 | Iustin Pop | |
1158 | d4b9d97f | Guido Trotter | def ExpandNames(self): |
1159 | d4b9d97f | Guido Trotter | self.needed_locks = {
|
1160 | d4b9d97f | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
1161 | d4b9d97f | Guido Trotter | locking.LEVEL_INSTANCE: locking.ALL_SET, |
1162 | d4b9d97f | Guido Trotter | } |
1163 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
1164 | a8083063 | Iustin Pop | |
1165 | 7c874ee1 | Iustin Pop | def _Error(self, ecode, item, msg, *args, **kwargs): |
1166 | 7c874ee1 | Iustin Pop | """Format an error message.
|
1167 | 7c874ee1 | Iustin Pop |
|
1168 | 7c874ee1 | Iustin Pop | Based on the opcode's error_codes parameter, either format a
|
1169 | 7c874ee1 | Iustin Pop | parseable error code, or a simpler error string.
|
1170 | 7c874ee1 | Iustin Pop |
|
1171 | 7c874ee1 | Iustin Pop | This must be called only from Exec and functions called from Exec.
|
1172 | 7c874ee1 | Iustin Pop |
|
1173 | 7c874ee1 | Iustin Pop | """
|
1174 | a0c9776a | Iustin Pop | ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) |
1175 | 7c874ee1 | Iustin Pop | itype, etxt = ecode |
1176 | 7c874ee1 | Iustin Pop | # first complete the msg
|
1177 | 7c874ee1 | Iustin Pop | if args:
|
1178 | 7c874ee1 | Iustin Pop | msg = msg % args |
1179 | 7c874ee1 | Iustin Pop | # then format the whole message
|
1180 | 7c874ee1 | Iustin Pop | if self.op.error_codes: |
1181 | 7c874ee1 | Iustin Pop | msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
|
1182 | 7c874ee1 | Iustin Pop | else:
|
1183 | 7c874ee1 | Iustin Pop | if item:
|
1184 | 7c874ee1 | Iustin Pop | item = " " + item
|
1185 | 7c874ee1 | Iustin Pop | else:
|
1186 | 7c874ee1 | Iustin Pop | item = ""
|
1187 | 7c874ee1 | Iustin Pop | msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
|
1188 | 7c874ee1 | Iustin Pop | # and finally report it via the feedback_fn
|
1189 | 7c874ee1 | Iustin Pop | self._feedback_fn(" - %s" % msg) |
1190 | 7c874ee1 | Iustin Pop | |
1191 | a0c9776a | Iustin Pop | def _ErrorIf(self, cond, *args, **kwargs): |
1192 | a0c9776a | Iustin Pop | """Log an error message if the passed condition is True.
|
1193 | a0c9776a | Iustin Pop |
|
1194 | a0c9776a | Iustin Pop | """
|
1195 | a0c9776a | Iustin Pop | cond = bool(cond) or self.op.debug_simulate_errors |
1196 | a0c9776a | Iustin Pop | if cond:
|
1197 | a0c9776a | Iustin Pop | self._Error(*args, **kwargs)
|
1198 | a0c9776a | Iustin Pop | # do not mark the operation as failed for WARN cases only
|
1199 | a0c9776a | Iustin Pop | if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR: |
1200 | a0c9776a | Iustin Pop | self.bad = self.bad or cond |
1201 | a0c9776a | Iustin Pop | |
1202 | 02c521e4 | Iustin Pop | def _VerifyNode(self, ninfo, nresult): |
1203 | a8083063 | Iustin Pop | """Run multiple tests against a node.
|
1204 | a8083063 | Iustin Pop |
|
1205 | 112f18a5 | Iustin Pop | Test list:
|
1206 | e4376078 | Iustin Pop |
|
1207 | a8083063 | Iustin Pop | - compares ganeti version
|
1208 | 5bbd3f7f | Michael Hanselmann | - checks vg existence and size > 20G
|
1209 | a8083063 | Iustin Pop | - checks config file checksum
|
1210 | a8083063 | Iustin Pop | - checks ssh to other nodes
|
1211 | a8083063 | Iustin Pop |
|
1212 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1213 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1214 | 02c521e4 | Iustin Pop | @param nresult: the results from the node
|
1215 | 02c521e4 | Iustin Pop | @rtype: boolean
|
1216 | 02c521e4 | Iustin Pop | @return: whether overall this call was successful (and we can expect
|
1217 | 02c521e4 | Iustin Pop | reasonable values in the respose)
|
1218 | 098c0958 | Michael Hanselmann |
|
1219 | a8083063 | Iustin Pop | """
|
1220 | 02c521e4 | Iustin Pop | node = ninfo.name |
1221 | 7260cfbe | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1222 | 25361b9a | Iustin Pop | |
1223 | 02c521e4 | Iustin Pop | # main result, nresult should be a non-empty dict
|
1224 | 02c521e4 | Iustin Pop | test = not nresult or not isinstance(nresult, dict) |
1225 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODERPC, node,
|
1226 | 7c874ee1 | Iustin Pop | "unable to verify node: no data returned")
|
1227 | a0c9776a | Iustin Pop | if test:
|
1228 | 02c521e4 | Iustin Pop | return False |
1229 | 25361b9a | Iustin Pop | |
1230 | a8083063 | Iustin Pop | # compares ganeti version
|
1231 | a8083063 | Iustin Pop | local_version = constants.PROTOCOL_VERSION |
1232 | 02c521e4 | Iustin Pop | remote_version = nresult.get("version", None) |
1233 | a0c9776a | Iustin Pop | test = not (remote_version and |
1234 | a0c9776a | Iustin Pop | isinstance(remote_version, (list, tuple)) and |
1235 | a0c9776a | Iustin Pop | len(remote_version) == 2) |
1236 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODERPC, node,
|
1237 | a0c9776a | Iustin Pop | "connection to node returned invalid data")
|
1238 | a0c9776a | Iustin Pop | if test:
|
1239 | 02c521e4 | Iustin Pop | return False |
1240 | a0c9776a | Iustin Pop | |
1241 | a0c9776a | Iustin Pop | test = local_version != remote_version[0]
|
1242 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEVERSION, node,
|
1243 | a0c9776a | Iustin Pop | "incompatible protocol versions: master %s,"
|
1244 | a0c9776a | Iustin Pop | " node %s", local_version, remote_version[0]) |
1245 | a0c9776a | Iustin Pop | if test:
|
1246 | 02c521e4 | Iustin Pop | return False |
1247 | a8083063 | Iustin Pop | |
1248 | e9ce0a64 | Iustin Pop | # node seems compatible, we can actually try to look into its results
|
1249 | a8083063 | Iustin Pop | |
1250 | e9ce0a64 | Iustin Pop | # full package version
|
1251 | a0c9776a | Iustin Pop | self._ErrorIf(constants.RELEASE_VERSION != remote_version[1], |
1252 | a0c9776a | Iustin Pop | self.ENODEVERSION, node,
|
1253 | 7c874ee1 | Iustin Pop | "software version mismatch: master %s, node %s",
|
1254 | 7c874ee1 | Iustin Pop | constants.RELEASE_VERSION, remote_version[1],
|
1255 | a0c9776a | Iustin Pop | code=self.ETYPE_WARNING)
|
1256 | e9ce0a64 | Iustin Pop | |
1257 | 02c521e4 | Iustin Pop | hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
|
1258 | 02c521e4 | Iustin Pop | if isinstance(hyp_result, dict): |
1259 | 02c521e4 | Iustin Pop | for hv_name, hv_result in hyp_result.iteritems(): |
1260 | 02c521e4 | Iustin Pop | test = hv_result is not None |
1261 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEHV, node,
|
1262 | 02c521e4 | Iustin Pop | "hypervisor %s verify failure: '%s'", hv_name, hv_result)
|
1263 | a8083063 | Iustin Pop | |
1264 | a8083063 | Iustin Pop | |
1265 | 02c521e4 | Iustin Pop | test = nresult.get(constants.NV_NODESETUP, |
1266 | 02c521e4 | Iustin Pop | ["Missing NODESETUP results"])
|
1267 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s", |
1268 | 02c521e4 | Iustin Pop | "; ".join(test))
|
1269 | 02c521e4 | Iustin Pop | |
1270 | 02c521e4 | Iustin Pop | return True |
1271 | 02c521e4 | Iustin Pop | |
1272 | 02c521e4 | Iustin Pop | def _VerifyNodeTime(self, ninfo, nresult, |
1273 | 02c521e4 | Iustin Pop | nvinfo_starttime, nvinfo_endtime): |
1274 | 02c521e4 | Iustin Pop | """Check the node time.
|
1275 | 02c521e4 | Iustin Pop |
|
1276 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1277 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1278 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1279 | 02c521e4 | Iustin Pop | @param nvinfo_starttime: the start time of the RPC call
|
1280 | 02c521e4 | Iustin Pop | @param nvinfo_endtime: the end time of the RPC call
|
1281 | 02c521e4 | Iustin Pop |
|
1282 | 02c521e4 | Iustin Pop | """
|
1283 | 02c521e4 | Iustin Pop | node = ninfo.name |
1284 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1285 | 02c521e4 | Iustin Pop | |
1286 | 02c521e4 | Iustin Pop | ntime = nresult.get(constants.NV_TIME, None)
|
1287 | 02c521e4 | Iustin Pop | try:
|
1288 | 02c521e4 | Iustin Pop | ntime_merged = utils.MergeTime(ntime) |
1289 | 02c521e4 | Iustin Pop | except (ValueError, TypeError): |
1290 | 02c521e4 | Iustin Pop | _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time") |
1291 | 02c521e4 | Iustin Pop | return
|
1292 | 02c521e4 | Iustin Pop | |
1293 | 02c521e4 | Iustin Pop | if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
|
1294 | 02c521e4 | Iustin Pop | ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged) |
1295 | 02c521e4 | Iustin Pop | elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
|
1296 | 02c521e4 | Iustin Pop | ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime) |
1297 | 02c521e4 | Iustin Pop | else:
|
1298 | 02c521e4 | Iustin Pop | ntime_diff = None
|
1299 | 02c521e4 | Iustin Pop | |
1300 | 02c521e4 | Iustin Pop | _ErrorIf(ntime_diff is not None, self.ENODETIME, node, |
1301 | 02c521e4 | Iustin Pop | "Node time diverges by at least %s from master node time",
|
1302 | 02c521e4 | Iustin Pop | ntime_diff) |
1303 | 02c521e4 | Iustin Pop | |
1304 | 02c521e4 | Iustin Pop | def _VerifyNodeLVM(self, ninfo, nresult, vg_name): |
1305 | 02c521e4 | Iustin Pop | """Check the node time.
|
1306 | 02c521e4 | Iustin Pop |
|
1307 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1308 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1309 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1310 | 02c521e4 | Iustin Pop | @param vg_name: the configured VG name
|
1311 | 02c521e4 | Iustin Pop |
|
1312 | 02c521e4 | Iustin Pop | """
|
1313 | 02c521e4 | Iustin Pop | if vg_name is None: |
1314 | 02c521e4 | Iustin Pop | return
|
1315 | 02c521e4 | Iustin Pop | |
1316 | 02c521e4 | Iustin Pop | node = ninfo.name |
1317 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1318 | 02c521e4 | Iustin Pop | |
1319 | 02c521e4 | Iustin Pop | # checks vg existence and size > 20G
|
1320 | 02c521e4 | Iustin Pop | vglist = nresult.get(constants.NV_VGLIST, None)
|
1321 | 02c521e4 | Iustin Pop | test = not vglist
|
1322 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups") |
1323 | 02c521e4 | Iustin Pop | if not test: |
1324 | 02c521e4 | Iustin Pop | vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name, |
1325 | 02c521e4 | Iustin Pop | constants.MIN_VG_SIZE) |
1326 | 02c521e4 | Iustin Pop | _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
|
1327 | 02c521e4 | Iustin Pop | |
1328 | 02c521e4 | Iustin Pop | # check pv names
|
1329 | 02c521e4 | Iustin Pop | pvlist = nresult.get(constants.NV_PVLIST, None)
|
1330 | 02c521e4 | Iustin Pop | test = pvlist is None |
1331 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node") |
1332 | a0c9776a | Iustin Pop | if not test: |
1333 | 02c521e4 | Iustin Pop | # check that ':' is not present in PV names, since it's a
|
1334 | 02c521e4 | Iustin Pop | # special character for lvcreate (denotes the range of PEs to
|
1335 | 02c521e4 | Iustin Pop | # use on the PV)
|
1336 | 02c521e4 | Iustin Pop | for _, pvname, owner_vg in pvlist: |
1337 | 02c521e4 | Iustin Pop | test = ":" in pvname |
1338 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV" |
1339 | 02c521e4 | Iustin Pop | " '%s' of VG '%s'", pvname, owner_vg)
|
1340 | 02c521e4 | Iustin Pop | |
1341 | 02c521e4 | Iustin Pop | def _VerifyNodeNetwork(self, ninfo, nresult): |
1342 | 02c521e4 | Iustin Pop | """Check the node time.
|
1343 | 02c521e4 | Iustin Pop |
|
1344 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1345 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1346 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1347 | 02c521e4 | Iustin Pop |
|
1348 | 02c521e4 | Iustin Pop | """
|
1349 | 02c521e4 | Iustin Pop | node = ninfo.name |
1350 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1351 | 02c521e4 | Iustin Pop | |
1352 | 02c521e4 | Iustin Pop | test = constants.NV_NODELIST not in nresult |
1353 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODESSH, node,
|
1354 | a0c9776a | Iustin Pop | "node hasn't returned node ssh connectivity data")
|
1355 | a0c9776a | Iustin Pop | if not test: |
1356 | 02c521e4 | Iustin Pop | if nresult[constants.NV_NODELIST]:
|
1357 | 02c521e4 | Iustin Pop | for a_node, a_msg in nresult[constants.NV_NODELIST].items(): |
1358 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODESSH, node, |
1359 | a0c9776a | Iustin Pop | "ssh communication with node '%s': %s", a_node, a_msg)
|
1360 | 25361b9a | Iustin Pop | |
1361 | 02c521e4 | Iustin Pop | test = constants.NV_NODENETTEST not in nresult |
1362 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODENET, node,
|
1363 | a0c9776a | Iustin Pop | "node hasn't returned node tcp connectivity data")
|
1364 | a0c9776a | Iustin Pop | if not test: |
1365 | 02c521e4 | Iustin Pop | if nresult[constants.NV_NODENETTEST]:
|
1366 | 02c521e4 | Iustin Pop | nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys()) |
1367 | 7c874ee1 | Iustin Pop | for anode in nlist: |
1368 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODENET, node, |
1369 | a0c9776a | Iustin Pop | "tcp communication with node '%s': %s",
|
1370 | 02c521e4 | Iustin Pop | anode, nresult[constants.NV_NODENETTEST][anode]) |
1371 | a8083063 | Iustin Pop | |
1372 | 02c521e4 | Iustin Pop | def _VerifyInstance(self, instance, instanceconfig, node_image): |
1373 | a8083063 | Iustin Pop | """Verify an instance.
|
1374 | a8083063 | Iustin Pop |
|
1375 | a8083063 | Iustin Pop | This function checks to see if the required block devices are
|
1376 | a8083063 | Iustin Pop | available on the instance's node.
|
1377 | a8083063 | Iustin Pop |
|
1378 | a8083063 | Iustin Pop | """
|
1379 | 7260cfbe | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1380 | a8083063 | Iustin Pop | node_current = instanceconfig.primary_node |
1381 | a8083063 | Iustin Pop | |
1382 | a8083063 | Iustin Pop | node_vol_should = {} |
1383 | a8083063 | Iustin Pop | instanceconfig.MapLVsByNode(node_vol_should) |
1384 | a8083063 | Iustin Pop | |
1385 | a8083063 | Iustin Pop | for node in node_vol_should: |
1386 | 02c521e4 | Iustin Pop | n_img = node_image[node] |
1387 | 02c521e4 | Iustin Pop | if n_img.offline or n_img.rpc_fail or n_img.lvm_fail: |
1388 | 02c521e4 | Iustin Pop | # ignore missing volumes on offline or broken nodes
|
1389 | 0a66c968 | Iustin Pop | continue
|
1390 | a8083063 | Iustin Pop | for volume in node_vol_should[node]: |
1391 | 02c521e4 | Iustin Pop | test = volume not in n_img.volumes |
1392 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
|
1393 | a0c9776a | Iustin Pop | "volume %s missing on node %s", volume, node)
|
1394 | a8083063 | Iustin Pop | |
1395 | 0d68c45d | Iustin Pop | if instanceconfig.admin_up:
|
1396 | 02c521e4 | Iustin Pop | pri_img = node_image[node_current] |
1397 | 02c521e4 | Iustin Pop | test = instance not in pri_img.instances and not pri_img.offline |
1398 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEDOWN, instance,
|
1399 | a0c9776a | Iustin Pop | "instance not running on its primary node %s",
|
1400 | a0c9776a | Iustin Pop | node_current) |
1401 | a8083063 | Iustin Pop | |
1402 | 02c521e4 | Iustin Pop | for node, n_img in node_image.items(): |
1403 | a8083063 | Iustin Pop | if (not node == node_current): |
1404 | 02c521e4 | Iustin Pop | test = instance in n_img.instances
|
1405 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
|
1406 | a0c9776a | Iustin Pop | "instance should not run on node %s", node)
|
1407 | a8083063 | Iustin Pop | |
1408 | 02c521e4 | Iustin Pop | def _VerifyOrphanVolumes(self, node_vol_should, node_image): |
1409 | a8083063 | Iustin Pop | """Verify if there are any unknown volumes in the cluster.
|
1410 | a8083063 | Iustin Pop |
|
1411 | a8083063 | Iustin Pop | The .os, .swap and backup volumes are ignored. All other volumes are
|
1412 | a8083063 | Iustin Pop | reported as unknown.
|
1413 | a8083063 | Iustin Pop |
|
1414 | a8083063 | Iustin Pop | """
|
1415 | 02c521e4 | Iustin Pop | for node, n_img in node_image.items(): |
1416 | 02c521e4 | Iustin Pop | if n_img.offline or n_img.rpc_fail or n_img.lvm_fail: |
1417 | 02c521e4 | Iustin Pop | # skip non-healthy nodes
|
1418 | 02c521e4 | Iustin Pop | continue
|
1419 | 02c521e4 | Iustin Pop | for volume in n_img.volumes: |
1420 | a0c9776a | Iustin Pop | test = (node not in node_vol_should or |
1421 | a0c9776a | Iustin Pop | volume not in node_vol_should[node]) |
1422 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEORPHANLV, node, |
1423 | 7c874ee1 | Iustin Pop | "volume %s is unknown", volume)
|
1424 | a8083063 | Iustin Pop | |
1425 | 02c521e4 | Iustin Pop | def _VerifyOrphanInstances(self, instancelist, node_image): |
1426 | a8083063 | Iustin Pop | """Verify the list of running instances.
|
1427 | a8083063 | Iustin Pop |
|
1428 | a8083063 | Iustin Pop | This checks what instances are running but unknown to the cluster.
|
1429 | a8083063 | Iustin Pop |
|
1430 | a8083063 | Iustin Pop | """
|
1431 | 02c521e4 | Iustin Pop | for node, n_img in node_image.items(): |
1432 | 02c521e4 | Iustin Pop | for o_inst in n_img.instances: |
1433 | a0c9776a | Iustin Pop | test = o_inst not in instancelist |
1434 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEORPHANINSTANCE, node, |
1435 | 7c874ee1 | Iustin Pop | "instance %s on node %s should not exist", o_inst, node)
|
1436 | a8083063 | Iustin Pop | |
1437 | 02c521e4 | Iustin Pop | def _VerifyNPlusOneMemory(self, node_image, instance_cfg): |
1438 | 2b3b6ddd | Guido Trotter | """Verify N+1 Memory Resilience.
|
1439 | 2b3b6ddd | Guido Trotter |
|
1440 | 02c521e4 | Iustin Pop | Check that if one single node dies we can still start all the
|
1441 | 02c521e4 | Iustin Pop | instances it was primary for.
|
1442 | 2b3b6ddd | Guido Trotter |
|
1443 | 2b3b6ddd | Guido Trotter | """
|
1444 | 02c521e4 | Iustin Pop | for node, n_img in node_image.items(): |
1445 | 02c521e4 | Iustin Pop | # This code checks that every node which is now listed as
|
1446 | 02c521e4 | Iustin Pop | # secondary has enough memory to host all instances it is
|
1447 | 02c521e4 | Iustin Pop | # supposed to should a single other node in the cluster fail.
|
1448 | 2b3b6ddd | Guido Trotter | # FIXME: not ready for failover to an arbitrary node
|
1449 | 2b3b6ddd | Guido Trotter | # FIXME: does not support file-backed instances
|
1450 | 02c521e4 | Iustin Pop | # WARNING: we currently take into account down instances as well
|
1451 | 02c521e4 | Iustin Pop | # as up ones, considering that even if they're down someone
|
1452 | 02c521e4 | Iustin Pop | # might want to start them even in the event of a node failure.
|
1453 | 02c521e4 | Iustin Pop | for prinode, instances in n_img.sbp.items(): |
1454 | 2b3b6ddd | Guido Trotter | needed_mem = 0
|
1455 | 2b3b6ddd | Guido Trotter | for instance in instances: |
1456 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
|
1457 | c0f2b229 | Iustin Pop | if bep[constants.BE_AUTO_BALANCE]:
|
1458 | 3924700f | Iustin Pop | needed_mem += bep[constants.BE_MEMORY] |
1459 | 02c521e4 | Iustin Pop | test = n_img.mfree < needed_mem |
1460 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEN1, node, |
1461 | 7c874ee1 | Iustin Pop | "not enough memory on to accommodate"
|
1462 | 7c874ee1 | Iustin Pop | " failovers should peer node %s fail", prinode)
|
1463 | 2b3b6ddd | Guido Trotter | |
1464 | 02c521e4 | Iustin Pop | def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum, |
1465 | 02c521e4 | Iustin Pop | master_files): |
1466 | 02c521e4 | Iustin Pop | """Verifies and computes the node required file checksums.
|
1467 | 02c521e4 | Iustin Pop |
|
1468 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1469 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1470 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1471 | 02c521e4 | Iustin Pop | @param file_list: required list of files
|
1472 | 02c521e4 | Iustin Pop | @param local_cksum: dictionary of local files and their checksums
|
1473 | 02c521e4 | Iustin Pop | @param master_files: list of files that only masters should have
|
1474 | 02c521e4 | Iustin Pop |
|
1475 | 02c521e4 | Iustin Pop | """
|
1476 | 02c521e4 | Iustin Pop | node = ninfo.name |
1477 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1478 | 02c521e4 | Iustin Pop | |
1479 | 02c521e4 | Iustin Pop | remote_cksum = nresult.get(constants.NV_FILELIST, None)
|
1480 | 02c521e4 | Iustin Pop | test = not isinstance(remote_cksum, dict) |
1481 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEFILECHECK, node,
|
1482 | 02c521e4 | Iustin Pop | "node hasn't returned file checksum data")
|
1483 | 02c521e4 | Iustin Pop | if test:
|
1484 | 02c521e4 | Iustin Pop | return
|
1485 | 02c521e4 | Iustin Pop | |
1486 | 02c521e4 | Iustin Pop | for file_name in file_list: |
1487 | 02c521e4 | Iustin Pop | node_is_mc = ninfo.master_candidate |
1488 | 02c521e4 | Iustin Pop | must_have = (file_name not in master_files) or node_is_mc |
1489 | 02c521e4 | Iustin Pop | # missing
|
1490 | 02c521e4 | Iustin Pop | test1 = file_name not in remote_cksum |
1491 | 02c521e4 | Iustin Pop | # invalid checksum
|
1492 | 02c521e4 | Iustin Pop | test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name] |
1493 | 02c521e4 | Iustin Pop | # existing and good
|
1494 | 02c521e4 | Iustin Pop | test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name] |
1495 | 02c521e4 | Iustin Pop | _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node, |
1496 | 02c521e4 | Iustin Pop | "file '%s' missing", file_name)
|
1497 | 02c521e4 | Iustin Pop | _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node, |
1498 | 02c521e4 | Iustin Pop | "file '%s' has wrong checksum", file_name)
|
1499 | 02c521e4 | Iustin Pop | # not candidate and this is not a must-have file
|
1500 | 02c521e4 | Iustin Pop | _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node, |
1501 | 02c521e4 | Iustin Pop | "file '%s' should not exist on non master"
|
1502 | 02c521e4 | Iustin Pop | " candidates (and the file is outdated)", file_name)
|
1503 | 02c521e4 | Iustin Pop | # all good, except non-master/non-must have combination
|
1504 | 02c521e4 | Iustin Pop | _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node, |
1505 | 02c521e4 | Iustin Pop | "file '%s' should not exist"
|
1506 | 02c521e4 | Iustin Pop | " on non master candidates", file_name)
|
1507 | 02c521e4 | Iustin Pop | |
1508 | 02c521e4 | Iustin Pop | def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_map): |
1509 | 02c521e4 | Iustin Pop | """Verifies and the node DRBD status.
|
1510 | 02c521e4 | Iustin Pop |
|
1511 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1512 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1513 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1514 | 02c521e4 | Iustin Pop | @param instanceinfo: the dict of instances
|
1515 | 02c521e4 | Iustin Pop | @param drbd_map: the DRBD map as returned by
|
1516 | 02c521e4 | Iustin Pop | L{ganeti.config.ConfigWriter.ComputeDRBDMap}
|
1517 | 02c521e4 | Iustin Pop |
|
1518 | 02c521e4 | Iustin Pop | """
|
1519 | 02c521e4 | Iustin Pop | node = ninfo.name |
1520 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1521 | 02c521e4 | Iustin Pop | |
1522 | 02c521e4 | Iustin Pop | # compute the DRBD minors
|
1523 | 02c521e4 | Iustin Pop | node_drbd = {} |
1524 | 02c521e4 | Iustin Pop | for minor, instance in drbd_map[node].items(): |
1525 | 02c521e4 | Iustin Pop | test = instance not in instanceinfo |
1526 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ECLUSTERCFG, None, |
1527 | 02c521e4 | Iustin Pop | "ghost instance '%s' in temporary DRBD map", instance)
|
1528 | 02c521e4 | Iustin Pop | # ghost instance should not be running, but otherwise we
|
1529 | 02c521e4 | Iustin Pop | # don't give double warnings (both ghost instance and
|
1530 | 02c521e4 | Iustin Pop | # unallocated minor in use)
|
1531 | 02c521e4 | Iustin Pop | if test:
|
1532 | 02c521e4 | Iustin Pop | node_drbd[minor] = (instance, False)
|
1533 | 02c521e4 | Iustin Pop | else:
|
1534 | 02c521e4 | Iustin Pop | instance = instanceinfo[instance] |
1535 | 02c521e4 | Iustin Pop | node_drbd[minor] = (instance.name, instance.admin_up) |
1536 | 02c521e4 | Iustin Pop | |
1537 | 02c521e4 | Iustin Pop | # and now check them
|
1538 | 02c521e4 | Iustin Pop | used_minors = nresult.get(constants.NV_DRBDLIST, []) |
1539 | 02c521e4 | Iustin Pop | test = not isinstance(used_minors, (tuple, list)) |
1540 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1541 | 02c521e4 | Iustin Pop | "cannot parse drbd status file: %s", str(used_minors)) |
1542 | 02c521e4 | Iustin Pop | if test:
|
1543 | 02c521e4 | Iustin Pop | # we cannot check drbd status
|
1544 | 02c521e4 | Iustin Pop | return
|
1545 | 02c521e4 | Iustin Pop | |
1546 | 02c521e4 | Iustin Pop | for minor, (iname, must_exist) in node_drbd.items(): |
1547 | 02c521e4 | Iustin Pop | test = minor not in used_minors and must_exist |
1548 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1549 | 02c521e4 | Iustin Pop | "drbd minor %d of instance %s is not active", minor, iname)
|
1550 | 02c521e4 | Iustin Pop | for minor in used_minors: |
1551 | 02c521e4 | Iustin Pop | test = minor not in node_drbd |
1552 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1553 | 02c521e4 | Iustin Pop | "unallocated drbd minor %d is in use", minor)
|
1554 | 02c521e4 | Iustin Pop | |
1555 | 02c521e4 | Iustin Pop | def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name): |
1556 | 02c521e4 | Iustin Pop | """Verifies and updates the node volume data.
|
1557 | 02c521e4 | Iustin Pop |
|
1558 | 02c521e4 | Iustin Pop | This function will update a L{NodeImage}'s internal structures
|
1559 | 02c521e4 | Iustin Pop | with data from the remote call.
|
1560 | 02c521e4 | Iustin Pop |
|
1561 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1562 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1563 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1564 | 02c521e4 | Iustin Pop | @param nimg: the node image object
|
1565 | 02c521e4 | Iustin Pop | @param vg_name: the configured VG name
|
1566 | 02c521e4 | Iustin Pop |
|
1567 | 02c521e4 | Iustin Pop | """
|
1568 | 02c521e4 | Iustin Pop | node = ninfo.name |
1569 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1570 | 02c521e4 | Iustin Pop | |
1571 | 02c521e4 | Iustin Pop | nimg.lvm_fail = True
|
1572 | 02c521e4 | Iustin Pop | lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
|
1573 | 02c521e4 | Iustin Pop | if vg_name is None: |
1574 | 02c521e4 | Iustin Pop | pass
|
1575 | 02c521e4 | Iustin Pop | elif isinstance(lvdata, basestring): |
1576 | 02c521e4 | Iustin Pop | _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s", |
1577 | 02c521e4 | Iustin Pop | utils.SafeEncode(lvdata)) |
1578 | 02c521e4 | Iustin Pop | elif not isinstance(lvdata, dict): |
1579 | 02c521e4 | Iustin Pop | _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)") |
1580 | 02c521e4 | Iustin Pop | else:
|
1581 | 02c521e4 | Iustin Pop | nimg.volumes = lvdata |
1582 | 02c521e4 | Iustin Pop | nimg.lvm_fail = False
|
1583 | 02c521e4 | Iustin Pop | |
1584 | 02c521e4 | Iustin Pop | def _UpdateNodeInstances(self, ninfo, nresult, nimg): |
1585 | 02c521e4 | Iustin Pop | """Verifies and updates the node instance list.
|
1586 | 02c521e4 | Iustin Pop |
|
1587 | 02c521e4 | Iustin Pop | If the listing was successful, then updates this node's instance
|
1588 | 02c521e4 | Iustin Pop | list. Otherwise, it marks the RPC call as failed for the instance
|
1589 | 02c521e4 | Iustin Pop | list key.
|
1590 | 02c521e4 | Iustin Pop |
|
1591 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1592 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1593 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1594 | 02c521e4 | Iustin Pop | @param nimg: the node image object
|
1595 | 02c521e4 | Iustin Pop |
|
1596 | 02c521e4 | Iustin Pop | """
|
1597 | 02c521e4 | Iustin Pop | idata = nresult.get(constants.NV_INSTANCELIST, None)
|
1598 | 02c521e4 | Iustin Pop | test = not isinstance(idata, list) |
1599 | 02c521e4 | Iustin Pop | self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed" |
1600 | 02c521e4 | Iustin Pop | " (instancelist): %s", utils.SafeEncode(str(idata))) |
1601 | 02c521e4 | Iustin Pop | if test:
|
1602 | 02c521e4 | Iustin Pop | nimg.hyp_fail = True
|
1603 | 02c521e4 | Iustin Pop | else:
|
1604 | 02c521e4 | Iustin Pop | nimg.instances = idata |
1605 | 02c521e4 | Iustin Pop | |
1606 | 02c521e4 | Iustin Pop | def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name): |
1607 | 02c521e4 | Iustin Pop | """Verifies and computes a node information map
|
1608 | 02c521e4 | Iustin Pop |
|
1609 | 02c521e4 | Iustin Pop | @type ninfo: L{objects.Node}
|
1610 | 02c521e4 | Iustin Pop | @param ninfo: the node to check
|
1611 | 02c521e4 | Iustin Pop | @param nresult: the remote results for the node
|
1612 | 02c521e4 | Iustin Pop | @param nimg: the node image object
|
1613 | 02c521e4 | Iustin Pop | @param vg_name: the configured VG name
|
1614 | 02c521e4 | Iustin Pop |
|
1615 | 02c521e4 | Iustin Pop | """
|
1616 | 02c521e4 | Iustin Pop | node = ninfo.name |
1617 | 02c521e4 | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1618 | 02c521e4 | Iustin Pop | |
1619 | 02c521e4 | Iustin Pop | # try to read free memory (from the hypervisor)
|
1620 | 02c521e4 | Iustin Pop | hv_info = nresult.get(constants.NV_HVINFO, None)
|
1621 | 02c521e4 | Iustin Pop | test = not isinstance(hv_info, dict) or "memory_free" not in hv_info |
1622 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)") |
1623 | 02c521e4 | Iustin Pop | if not test: |
1624 | 02c521e4 | Iustin Pop | try:
|
1625 | 02c521e4 | Iustin Pop | nimg.mfree = int(hv_info["memory_free"]) |
1626 | 02c521e4 | Iustin Pop | except (ValueError, TypeError): |
1627 | 02c521e4 | Iustin Pop | _ErrorIf(True, self.ENODERPC, node, |
1628 | 02c521e4 | Iustin Pop | "node returned invalid nodeinfo, check hypervisor")
|
1629 | 02c521e4 | Iustin Pop | |
1630 | 02c521e4 | Iustin Pop | # FIXME: devise a free space model for file based instances as well
|
1631 | 02c521e4 | Iustin Pop | if vg_name is not None: |
1632 | 02c521e4 | Iustin Pop | test = (constants.NV_VGLIST not in nresult or |
1633 | 02c521e4 | Iustin Pop | vg_name not in nresult[constants.NV_VGLIST]) |
1634 | 02c521e4 | Iustin Pop | _ErrorIf(test, self.ENODELVM, node,
|
1635 | 02c521e4 | Iustin Pop | "node didn't return data for the volume group '%s'"
|
1636 | 02c521e4 | Iustin Pop | " - it is either missing or broken", vg_name)
|
1637 | 02c521e4 | Iustin Pop | if not test: |
1638 | 02c521e4 | Iustin Pop | try:
|
1639 | 02c521e4 | Iustin Pop | nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
|
1640 | 02c521e4 | Iustin Pop | except (ValueError, TypeError): |
1641 | 02c521e4 | Iustin Pop | _ErrorIf(True, self.ENODERPC, node, |
1642 | 02c521e4 | Iustin Pop | "node returned invalid LVM info, check LVM status")
|
1643 | 02c521e4 | Iustin Pop | |
1644 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1645 | a8083063 | Iustin Pop | """Check prerequisites.
|
1646 | a8083063 | Iustin Pop |
|
1647 | e54c4c5e | Guido Trotter | Transform the list of checks we're going to skip into a set and check that
|
1648 | e54c4c5e | Guido Trotter | all its members are valid.
|
1649 | a8083063 | Iustin Pop |
|
1650 | a8083063 | Iustin Pop | """
|
1651 | e54c4c5e | Guido Trotter | self.skip_set = frozenset(self.op.skip_checks) |
1652 | e54c4c5e | Guido Trotter | if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set): |
1653 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid checks to be skipped specified", |
1654 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
1655 | a8083063 | Iustin Pop | |
1656 | d8fff41c | Guido Trotter | def BuildHooksEnv(self): |
1657 | d8fff41c | Guido Trotter | """Build hooks env.
|
1658 | d8fff41c | Guido Trotter |
|
1659 | 5bbd3f7f | Michael Hanselmann | Cluster-Verify hooks just ran in the post phase and their failure makes
|
1660 | d8fff41c | Guido Trotter | the output be logged in the verify output and the verification to fail.
|
1661 | d8fff41c | Guido Trotter |
|
1662 | d8fff41c | Guido Trotter | """
|
1663 | d8fff41c | Guido Trotter | all_nodes = self.cfg.GetNodeList()
|
1664 | 35e994e9 | Iustin Pop | env = { |
1665 | 35e994e9 | Iustin Pop | "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags()) |
1666 | 35e994e9 | Iustin Pop | } |
1667 | 35e994e9 | Iustin Pop | for node in self.cfg.GetAllNodesInfo().values(): |
1668 | 35e994e9 | Iustin Pop | env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags()) |
1669 | 35e994e9 | Iustin Pop | |
1670 | d8fff41c | Guido Trotter | return env, [], all_nodes
|
1671 | d8fff41c | Guido Trotter | |
1672 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1673 | a8083063 | Iustin Pop | """Verify integrity of cluster, performing various test on nodes.
|
1674 | a8083063 | Iustin Pop |
|
1675 | a8083063 | Iustin Pop | """
|
1676 | a0c9776a | Iustin Pop | self.bad = False |
1677 | 7260cfbe | Iustin Pop | _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103 |
1678 | 7c874ee1 | Iustin Pop | verbose = self.op.verbose
|
1679 | 7c874ee1 | Iustin Pop | self._feedback_fn = feedback_fn
|
1680 | a8083063 | Iustin Pop | feedback_fn("* Verifying global settings")
|
1681 | 8522ceeb | Iustin Pop | for msg in self.cfg.VerifyConfig(): |
1682 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ECLUSTERCFG, None, msg) |
1683 | a8083063 | Iustin Pop | |
1684 | b98bf262 | Michael Hanselmann | # Check the cluster certificates
|
1685 | b98bf262 | Michael Hanselmann | for cert_filename in constants.ALL_CERT_FILES: |
1686 | b98bf262 | Michael Hanselmann | (errcode, msg) = _VerifyCertificate(cert_filename) |
1687 | b98bf262 | Michael Hanselmann | _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode) |
1688 | b98bf262 | Michael Hanselmann | |
1689 | a8083063 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
1690 | e69d05fd | Iustin Pop | hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
|
1691 | a8083063 | Iustin Pop | nodelist = utils.NiceSort(self.cfg.GetNodeList())
|
1692 | 9d4bfc96 | Iustin Pop | nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist] |
1693 | a8083063 | Iustin Pop | instancelist = utils.NiceSort(self.cfg.GetInstanceList())
|
1694 | 6d2e83d5 | Iustin Pop | instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname)) |
1695 | 6d2e83d5 | Iustin Pop | for iname in instancelist) |
1696 | 93e4c50b | Guido Trotter | i_non_redundant = [] # Non redundant instances
|
1697 | 3924700f | Iustin Pop | i_non_a_balanced = [] # Non auto-balanced instances
|
1698 | 02c521e4 | Iustin Pop | n_offline = 0 # Count of offline nodes |
1699 | 02c521e4 | Iustin Pop | n_drained = 0 # Count of nodes being drained |
1700 | 02c521e4 | Iustin Pop | node_vol_should = {} |
1701 | a8083063 | Iustin Pop | |
1702 | a8083063 | Iustin Pop | # FIXME: verify OS list
|
1703 | a8083063 | Iustin Pop | # do local checksums
|
1704 | 112f18a5 | Iustin Pop | master_files = [constants.CLUSTER_CONF_FILE] |
1705 | 112f18a5 | Iustin Pop | |
1706 | 112f18a5 | Iustin Pop | file_names = ssconf.SimpleStore().GetFileList() |
1707 | d3100055 | Michael Hanselmann | file_names.extend(constants.ALL_CERT_FILES) |
1708 | 112f18a5 | Iustin Pop | file_names.extend(master_files) |
1709 | 112f18a5 | Iustin Pop | |
1710 | a8083063 | Iustin Pop | local_checksums = utils.FingerprintFiles(file_names) |
1711 | a8083063 | Iustin Pop | |
1712 | a8083063 | Iustin Pop | feedback_fn("* Gathering data (%d nodes)" % len(nodelist)) |
1713 | a8083063 | Iustin Pop | node_verify_param = { |
1714 | 25361b9a | Iustin Pop | constants.NV_FILELIST: file_names, |
1715 | 82e37788 | Iustin Pop | constants.NV_NODELIST: [node.name for node in nodeinfo |
1716 | 82e37788 | Iustin Pop | if not node.offline], |
1717 | 25361b9a | Iustin Pop | constants.NV_HYPERVISOR: hypervisors, |
1718 | 25361b9a | Iustin Pop | constants.NV_NODENETTEST: [(node.name, node.primary_ip, |
1719 | 82e37788 | Iustin Pop | node.secondary_ip) for node in nodeinfo |
1720 | 82e37788 | Iustin Pop | if not node.offline], |
1721 | 25361b9a | Iustin Pop | constants.NV_INSTANCELIST: hypervisors, |
1722 | 25361b9a | Iustin Pop | constants.NV_VERSION: None,
|
1723 | 25361b9a | Iustin Pop | constants.NV_HVINFO: self.cfg.GetHypervisorType(),
|
1724 | 7c0aa8e9 | Iustin Pop | constants.NV_NODESETUP: None,
|
1725 | 313b2dd4 | Michael Hanselmann | constants.NV_TIME: None,
|
1726 | a8083063 | Iustin Pop | } |
1727 | 313b2dd4 | Michael Hanselmann | |
1728 | cc9e1230 | Guido Trotter | if vg_name is not None: |
1729 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_VGLIST] = None
|
1730 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_LVLIST] = vg_name |
1731 | d091393e | Iustin Pop | node_verify_param[constants.NV_PVLIST] = [vg_name] |
1732 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_DRBDLIST] = None
|
1733 | 313b2dd4 | Michael Hanselmann | |
1734 | 02c521e4 | Iustin Pop | # Build our expected cluster state
|
1735 | 02c521e4 | Iustin Pop | node_image = dict((node.name, self.NodeImage(offline=node.offline)) |
1736 | 02c521e4 | Iustin Pop | for node in nodeinfo) |
1737 | 02c521e4 | Iustin Pop | |
1738 | 02c521e4 | Iustin Pop | for instance in instancelist: |
1739 | 02c521e4 | Iustin Pop | inst_config = instanceinfo[instance] |
1740 | 02c521e4 | Iustin Pop | |
1741 | 02c521e4 | Iustin Pop | for nname in inst_config.all_nodes: |
1742 | 02c521e4 | Iustin Pop | if nname not in node_image: |
1743 | 02c521e4 | Iustin Pop | # ghost node
|
1744 | 02c521e4 | Iustin Pop | gnode = self.NodeImage()
|
1745 | 02c521e4 | Iustin Pop | gnode.ghost = True
|
1746 | 02c521e4 | Iustin Pop | node_image[nname] = gnode |
1747 | 02c521e4 | Iustin Pop | |
1748 | 02c521e4 | Iustin Pop | inst_config.MapLVsByNode(node_vol_should) |
1749 | 02c521e4 | Iustin Pop | |
1750 | 02c521e4 | Iustin Pop | pnode = inst_config.primary_node |
1751 | 02c521e4 | Iustin Pop | node_image[pnode].pinst.append(instance) |
1752 | 02c521e4 | Iustin Pop | |
1753 | 02c521e4 | Iustin Pop | for snode in inst_config.secondary_nodes: |
1754 | 02c521e4 | Iustin Pop | nimg = node_image[snode] |
1755 | 02c521e4 | Iustin Pop | nimg.sinst.append(instance) |
1756 | 02c521e4 | Iustin Pop | if pnode not in nimg.sbp: |
1757 | 02c521e4 | Iustin Pop | nimg.sbp[pnode] = [] |
1758 | 02c521e4 | Iustin Pop | nimg.sbp[pnode].append(instance) |
1759 | 02c521e4 | Iustin Pop | |
1760 | 02c521e4 | Iustin Pop | # At this point, we have the in-memory data structures complete,
|
1761 | 02c521e4 | Iustin Pop | # except for the runtime information, which we'll gather next
|
1762 | 02c521e4 | Iustin Pop | |
1763 | 313b2dd4 | Michael Hanselmann | # Due to the way our RPC system works, exact response times cannot be
|
1764 | 313b2dd4 | Michael Hanselmann | # guaranteed (e.g. a broken node could run into a timeout). By keeping the
|
1765 | 313b2dd4 | Michael Hanselmann | # time before and after executing the request, we can at least have a time
|
1766 | 313b2dd4 | Michael Hanselmann | # window.
|
1767 | 313b2dd4 | Michael Hanselmann | nvinfo_starttime = time.time() |
1768 | 72737a7f | Iustin Pop | all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
|
1769 | 72737a7f | Iustin Pop | self.cfg.GetClusterName())
|
1770 | 313b2dd4 | Michael Hanselmann | nvinfo_endtime = time.time() |
1771 | a8083063 | Iustin Pop | |
1772 | 3924700f | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
1773 | 112f18a5 | Iustin Pop | master_node = self.cfg.GetMasterNode()
|
1774 | 6d2e83d5 | Iustin Pop | all_drbd_map = self.cfg.ComputeDRBDMap()
|
1775 | 6d2e83d5 | Iustin Pop | |
1776 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying node status")
|
1777 | 112f18a5 | Iustin Pop | for node_i in nodeinfo: |
1778 | 112f18a5 | Iustin Pop | node = node_i.name |
1779 | 02c521e4 | Iustin Pop | nimg = node_image[node] |
1780 | 25361b9a | Iustin Pop | |
1781 | 0a66c968 | Iustin Pop | if node_i.offline:
|
1782 | 7c874ee1 | Iustin Pop | if verbose:
|
1783 | 7c874ee1 | Iustin Pop | feedback_fn("* Skipping offline node %s" % (node,))
|
1784 | 02c521e4 | Iustin Pop | n_offline += 1
|
1785 | 0a66c968 | Iustin Pop | continue
|
1786 | 0a66c968 | Iustin Pop | |
1787 | 112f18a5 | Iustin Pop | if node == master_node:
|
1788 | 25361b9a | Iustin Pop | ntype = "master"
|
1789 | 112f18a5 | Iustin Pop | elif node_i.master_candidate:
|
1790 | 25361b9a | Iustin Pop | ntype = "master candidate"
|
1791 | 22f0f71d | Iustin Pop | elif node_i.drained:
|
1792 | 22f0f71d | Iustin Pop | ntype = "drained"
|
1793 | 02c521e4 | Iustin Pop | n_drained += 1
|
1794 | 112f18a5 | Iustin Pop | else:
|
1795 | 25361b9a | Iustin Pop | ntype = "regular"
|
1796 | 7c874ee1 | Iustin Pop | if verbose:
|
1797 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying node %s (%s)" % (node, ntype))
|
1798 | 25361b9a | Iustin Pop | |
1799 | 4c4e4e1e | Iustin Pop | msg = all_nvinfo[node].fail_msg |
1800 | a0c9776a | Iustin Pop | _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg) |
1801 | 6f68a739 | Iustin Pop | if msg:
|
1802 | 02c521e4 | Iustin Pop | nimg.rpc_fail = True
|
1803 | 25361b9a | Iustin Pop | continue
|
1804 | 25361b9a | Iustin Pop | |
1805 | 6f68a739 | Iustin Pop | nresult = all_nvinfo[node].payload |
1806 | a8083063 | Iustin Pop | |
1807 | 02c521e4 | Iustin Pop | nimg.call_ok = self._VerifyNode(node_i, nresult)
|
1808 | 02c521e4 | Iustin Pop | self._VerifyNodeNetwork(node_i, nresult)
|
1809 | 02c521e4 | Iustin Pop | self._VerifyNodeLVM(node_i, nresult, vg_name)
|
1810 | 02c521e4 | Iustin Pop | self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
|
1811 | 02c521e4 | Iustin Pop | master_files) |
1812 | 02c521e4 | Iustin Pop | self._VerifyNodeDrbd(node_i, nresult, instanceinfo, all_drbd_map)
|
1813 | 02c521e4 | Iustin Pop | self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
|
1814 | a8083063 | Iustin Pop | |
1815 | 02c521e4 | Iustin Pop | self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
|
1816 | 02c521e4 | Iustin Pop | self._UpdateNodeInstances(node_i, nresult, nimg)
|
1817 | 02c521e4 | Iustin Pop | self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
|
1818 | a8083063 | Iustin Pop | |
1819 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying instance status")
|
1820 | a8083063 | Iustin Pop | for instance in instancelist: |
1821 | 7c874ee1 | Iustin Pop | if verbose:
|
1822 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying instance %s" % instance)
|
1823 | 6d2e83d5 | Iustin Pop | inst_config = instanceinfo[instance] |
1824 | 02c521e4 | Iustin Pop | self._VerifyInstance(instance, inst_config, node_image)
|
1825 | 832261fd | Iustin Pop | inst_nodes_offline = [] |
1826 | a8083063 | Iustin Pop | |
1827 | 93e4c50b | Guido Trotter | pnode = inst_config.primary_node |
1828 | 02c521e4 | Iustin Pop | pnode_img = node_image[pnode] |
1829 | 02c521e4 | Iustin Pop | _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline, |
1830 | a0c9776a | Iustin Pop | self.ENODERPC, pnode, "instance %s, connection to" |
1831 | a0c9776a | Iustin Pop | " primary node failed", instance)
|
1832 | 93e4c50b | Guido Trotter | |
1833 | 02c521e4 | Iustin Pop | if pnode_img.offline:
|
1834 | 832261fd | Iustin Pop | inst_nodes_offline.append(pnode) |
1835 | 832261fd | Iustin Pop | |
1836 | 93e4c50b | Guido Trotter | # If the instance is non-redundant we cannot survive losing its primary
|
1837 | 93e4c50b | Guido Trotter | # node, so we are not N+1 compliant. On the other hand we have no disk
|
1838 | 93e4c50b | Guido Trotter | # templates with more than one secondary so that situation is not well
|
1839 | 93e4c50b | Guido Trotter | # supported either.
|
1840 | 93e4c50b | Guido Trotter | # FIXME: does not support file-backed instances
|
1841 | 02c521e4 | Iustin Pop | if not inst_config.secondary_nodes: |
1842 | 93e4c50b | Guido Trotter | i_non_redundant.append(instance) |
1843 | 02c521e4 | Iustin Pop | _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT, |
1844 | 02c521e4 | Iustin Pop | instance, "instance has multiple secondary nodes: %s",
|
1845 | 02c521e4 | Iustin Pop | utils.CommaJoin(inst_config.secondary_nodes), |
1846 | 02c521e4 | Iustin Pop | code=self.ETYPE_WARNING)
|
1847 | 93e4c50b | Guido Trotter | |
1848 | c0f2b229 | Iustin Pop | if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]: |
1849 | 3924700f | Iustin Pop | i_non_a_balanced.append(instance) |
1850 | 3924700f | Iustin Pop | |
1851 | 93e4c50b | Guido Trotter | for snode in inst_config.secondary_nodes: |
1852 | 02c521e4 | Iustin Pop | s_img = node_image[snode] |
1853 | 02c521e4 | Iustin Pop | _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode, |
1854 | 02c521e4 | Iustin Pop | "instance %s, connection to secondary node failed", instance)
|
1855 | 02c521e4 | Iustin Pop | |
1856 | 02c521e4 | Iustin Pop | if s_img.offline:
|
1857 | 832261fd | Iustin Pop | inst_nodes_offline.append(snode) |
1858 | 832261fd | Iustin Pop | |
1859 | a0c9776a | Iustin Pop | # warn that the instance lives on offline nodes
|
1860 | a0c9776a | Iustin Pop | _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
|
1861 | a0c9776a | Iustin Pop | "instance lives on offline node(s) %s",
|
1862 | 1f864b60 | Iustin Pop | utils.CommaJoin(inst_nodes_offline)) |
1863 | 02c521e4 | Iustin Pop | # ... or ghost nodes
|
1864 | 02c521e4 | Iustin Pop | for node in inst_config.all_nodes: |
1865 | 02c521e4 | Iustin Pop | _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
|
1866 | 02c521e4 | Iustin Pop | "instance lives on ghost node %s", node)
|
1867 | 93e4c50b | Guido Trotter | |
1868 | a8083063 | Iustin Pop | feedback_fn("* Verifying orphan volumes")
|
1869 | 02c521e4 | Iustin Pop | self._VerifyOrphanVolumes(node_vol_should, node_image)
|
1870 | a8083063 | Iustin Pop | |
1871 | 02c521e4 | Iustin Pop | feedback_fn("* Verifying oprhan instances")
|
1872 | 02c521e4 | Iustin Pop | self._VerifyOrphanInstances(instancelist, node_image)
|
1873 | a8083063 | Iustin Pop | |
1874 | e54c4c5e | Guido Trotter | if constants.VERIFY_NPLUSONE_MEM not in self.skip_set: |
1875 | e54c4c5e | Guido Trotter | feedback_fn("* Verifying N+1 Memory redundancy")
|
1876 | 02c521e4 | Iustin Pop | self._VerifyNPlusOneMemory(node_image, instanceinfo)
|
1877 | 2b3b6ddd | Guido Trotter | |
1878 | 2b3b6ddd | Guido Trotter | feedback_fn("* Other Notes")
|
1879 | 2b3b6ddd | Guido Trotter | if i_non_redundant:
|
1880 | 2b3b6ddd | Guido Trotter | feedback_fn(" - NOTICE: %d non-redundant instance(s) found."
|
1881 | 2b3b6ddd | Guido Trotter | % len(i_non_redundant))
|
1882 | 2b3b6ddd | Guido Trotter | |
1883 | 3924700f | Iustin Pop | if i_non_a_balanced:
|
1884 | 3924700f | Iustin Pop | feedback_fn(" - NOTICE: %d non-auto-balanced instance(s) found."
|
1885 | 3924700f | Iustin Pop | % len(i_non_a_balanced))
|
1886 | 3924700f | Iustin Pop | |
1887 | 0a66c968 | Iustin Pop | if n_offline:
|
1888 | 02c521e4 | Iustin Pop | feedback_fn(" - NOTICE: %d offline node(s) found." % n_offline)
|
1889 | 0a66c968 | Iustin Pop | |
1890 | 22f0f71d | Iustin Pop | if n_drained:
|
1891 | 02c521e4 | Iustin Pop | feedback_fn(" - NOTICE: %d drained node(s) found." % n_drained)
|
1892 | 22f0f71d | Iustin Pop | |
1893 | a0c9776a | Iustin Pop | return not self.bad |
1894 | a8083063 | Iustin Pop | |
1895 | d8fff41c | Guido Trotter | def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result): |
1896 | 5bbd3f7f | Michael Hanselmann | """Analyze the post-hooks' result
|
1897 | e4376078 | Iustin Pop |
|
1898 | e4376078 | Iustin Pop | This method analyses the hook result, handles it, and sends some
|
1899 | d8fff41c | Guido Trotter | nicely-formatted feedback back to the user.
|
1900 | d8fff41c | Guido Trotter |
|
1901 | e4376078 | Iustin Pop | @param phase: one of L{constants.HOOKS_PHASE_POST} or
|
1902 | e4376078 | Iustin Pop | L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
|
1903 | e4376078 | Iustin Pop | @param hooks_results: the results of the multi-node hooks rpc call
|
1904 | e4376078 | Iustin Pop | @param feedback_fn: function used send feedback back to the caller
|
1905 | e4376078 | Iustin Pop | @param lu_result: previous Exec result
|
1906 | e4376078 | Iustin Pop | @return: the new Exec result, based on the previous result
|
1907 | e4376078 | Iustin Pop | and hook results
|
1908 | d8fff41c | Guido Trotter |
|
1909 | d8fff41c | Guido Trotter | """
|
1910 | 38206f3c | Iustin Pop | # We only really run POST phase hooks, and are only interested in
|
1911 | 38206f3c | Iustin Pop | # their results
|
1912 | d8fff41c | Guido Trotter | if phase == constants.HOOKS_PHASE_POST:
|
1913 | d8fff41c | Guido Trotter | # Used to change hooks' output to proper indentation
|
1914 | d8fff41c | Guido Trotter | indent_re = re.compile('^', re.M)
|
1915 | d8fff41c | Guido Trotter | feedback_fn("* Hooks Results")
|
1916 | 7c874ee1 | Iustin Pop | assert hooks_results, "invalid result from hooks" |
1917 | 7c874ee1 | Iustin Pop | |
1918 | 7c874ee1 | Iustin Pop | for node_name in hooks_results: |
1919 | 7c874ee1 | Iustin Pop | res = hooks_results[node_name] |
1920 | 7c874ee1 | Iustin Pop | msg = res.fail_msg |
1921 | a0c9776a | Iustin Pop | test = msg and not res.offline |
1922 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEHOOKS, node_name, |
1923 | 7c874ee1 | Iustin Pop | "Communication failure in hooks execution: %s", msg)
|
1924 | dd9e9f9c | Michael Hanselmann | if res.offline or msg: |
1925 | dd9e9f9c | Michael Hanselmann | # No need to investigate payload if node is offline or gave an error.
|
1926 | a0c9776a | Iustin Pop | # override manually lu_result here as _ErrorIf only
|
1927 | a0c9776a | Iustin Pop | # overrides self.bad
|
1928 | 7c874ee1 | Iustin Pop | lu_result = 1
|
1929 | 7c874ee1 | Iustin Pop | continue
|
1930 | 7c874ee1 | Iustin Pop | for script, hkr, output in res.payload: |
1931 | a0c9776a | Iustin Pop | test = hkr == constants.HKR_FAIL |
1932 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEHOOKS, node_name, |
1933 | 7c874ee1 | Iustin Pop | "Script %s failed, output:", script)
|
1934 | a0c9776a | Iustin Pop | if test:
|
1935 | 7c874ee1 | Iustin Pop | output = indent_re.sub(' ', output)
|
1936 | 7c874ee1 | Iustin Pop | feedback_fn("%s" % output)
|
1937 | 6d7b472a | Iustin Pop | lu_result = 0
|
1938 | d8fff41c | Guido Trotter | |
1939 | d8fff41c | Guido Trotter | return lu_result
|
1940 | d8fff41c | Guido Trotter | |
1941 | a8083063 | Iustin Pop | |
1942 | 2c95a8d4 | Iustin Pop | class LUVerifyDisks(NoHooksLU): |
1943 | 2c95a8d4 | Iustin Pop | """Verifies the cluster disks status.
|
1944 | 2c95a8d4 | Iustin Pop |
|
1945 | 2c95a8d4 | Iustin Pop | """
|
1946 | 2c95a8d4 | Iustin Pop | _OP_REQP = [] |
1947 | d4b9d97f | Guido Trotter | REQ_BGL = False
|
1948 | d4b9d97f | Guido Trotter | |
1949 | d4b9d97f | Guido Trotter | def ExpandNames(self): |
1950 | d4b9d97f | Guido Trotter | self.needed_locks = {
|
1951 | d4b9d97f | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
1952 | d4b9d97f | Guido Trotter | locking.LEVEL_INSTANCE: locking.ALL_SET, |
1953 | d4b9d97f | Guido Trotter | } |
1954 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
1955 | 2c95a8d4 | Iustin Pop | |
1956 | 2c95a8d4 | Iustin Pop | def CheckPrereq(self): |
1957 | 2c95a8d4 | Iustin Pop | """Check prerequisites.
|
1958 | 2c95a8d4 | Iustin Pop |
|
1959 | 2c95a8d4 | Iustin Pop | This has no prerequisites.
|
1960 | 2c95a8d4 | Iustin Pop |
|
1961 | 2c95a8d4 | Iustin Pop | """
|
1962 | 2c95a8d4 | Iustin Pop | pass
|
1963 | 2c95a8d4 | Iustin Pop | |
1964 | 2c95a8d4 | Iustin Pop | def Exec(self, feedback_fn): |
1965 | 2c95a8d4 | Iustin Pop | """Verify integrity of cluster disks.
|
1966 | 2c95a8d4 | Iustin Pop |
|
1967 | 29d376ec | Iustin Pop | @rtype: tuple of three items
|
1968 | 29d376ec | Iustin Pop | @return: a tuple of (dict of node-to-node_error, list of instances
|
1969 | 29d376ec | Iustin Pop | which need activate-disks, dict of instance: (node, volume) for
|
1970 | 29d376ec | Iustin Pop | missing volumes
|
1971 | 29d376ec | Iustin Pop |
|
1972 | 2c95a8d4 | Iustin Pop | """
|
1973 | 29d376ec | Iustin Pop | result = res_nodes, res_instances, res_missing = {}, [], {} |
1974 | 2c95a8d4 | Iustin Pop | |
1975 | 2c95a8d4 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
1976 | 2c95a8d4 | Iustin Pop | nodes = utils.NiceSort(self.cfg.GetNodeList())
|
1977 | 2c95a8d4 | Iustin Pop | instances = [self.cfg.GetInstanceInfo(name)
|
1978 | 2c95a8d4 | Iustin Pop | for name in self.cfg.GetInstanceList()] |
1979 | 2c95a8d4 | Iustin Pop | |
1980 | 2c95a8d4 | Iustin Pop | nv_dict = {} |
1981 | 2c95a8d4 | Iustin Pop | for inst in instances: |
1982 | 2c95a8d4 | Iustin Pop | inst_lvs = {} |
1983 | 0d68c45d | Iustin Pop | if (not inst.admin_up or |
1984 | 2c95a8d4 | Iustin Pop | inst.disk_template not in constants.DTS_NET_MIRROR): |
1985 | 2c95a8d4 | Iustin Pop | continue
|
1986 | 2c95a8d4 | Iustin Pop | inst.MapLVsByNode(inst_lvs) |
1987 | 2c95a8d4 | Iustin Pop | # transform { iname: {node: [vol,],},} to {(node, vol): iname}
|
1988 | 2c95a8d4 | Iustin Pop | for node, vol_list in inst_lvs.iteritems(): |
1989 | 2c95a8d4 | Iustin Pop | for vol in vol_list: |
1990 | 2c95a8d4 | Iustin Pop | nv_dict[(node, vol)] = inst |
1991 | 2c95a8d4 | Iustin Pop | |
1992 | 2c95a8d4 | Iustin Pop | if not nv_dict: |
1993 | 2c95a8d4 | Iustin Pop | return result
|
1994 | 2c95a8d4 | Iustin Pop | |
1995 | b2a6ccd4 | Iustin Pop | node_lvs = self.rpc.call_lv_list(nodes, vg_name)
|
1996 | 2c95a8d4 | Iustin Pop | |
1997 | 2c95a8d4 | Iustin Pop | for node in nodes: |
1998 | 2c95a8d4 | Iustin Pop | # node_volume
|
1999 | 29d376ec | Iustin Pop | node_res = node_lvs[node] |
2000 | 29d376ec | Iustin Pop | if node_res.offline:
|
2001 | ea9ddc07 | Iustin Pop | continue
|
2002 | 4c4e4e1e | Iustin Pop | msg = node_res.fail_msg |
2003 | 29d376ec | Iustin Pop | if msg:
|
2004 | 29d376ec | Iustin Pop | logging.warning("Error enumerating LVs on node %s: %s", node, msg)
|
2005 | 29d376ec | Iustin Pop | res_nodes[node] = msg |
2006 | 2c95a8d4 | Iustin Pop | continue
|
2007 | 2c95a8d4 | Iustin Pop | |
2008 | 29d376ec | Iustin Pop | lvs = node_res.payload |
2009 | 1122eb25 | Iustin Pop | for lv_name, (_, _, lv_online) in lvs.items(): |
2010 | b63ed789 | Iustin Pop | inst = nv_dict.pop((node, lv_name), None)
|
2011 | b63ed789 | Iustin Pop | if (not lv_online and inst is not None |
2012 | b63ed789 | Iustin Pop | and inst.name not in res_instances): |
2013 | b08d5a87 | Iustin Pop | res_instances.append(inst.name) |
2014 | 2c95a8d4 | Iustin Pop | |
2015 | b63ed789 | Iustin Pop | # any leftover items in nv_dict are missing LVs, let's arrange the
|
2016 | b63ed789 | Iustin Pop | # data better
|
2017 | b63ed789 | Iustin Pop | for key, inst in nv_dict.iteritems(): |
2018 | b63ed789 | Iustin Pop | if inst.name not in res_missing: |
2019 | b63ed789 | Iustin Pop | res_missing[inst.name] = [] |
2020 | b63ed789 | Iustin Pop | res_missing[inst.name].append(key) |
2021 | b63ed789 | Iustin Pop | |
2022 | 2c95a8d4 | Iustin Pop | return result
|
2023 | 2c95a8d4 | Iustin Pop | |
2024 | 2c95a8d4 | Iustin Pop | |
2025 | 60975797 | Iustin Pop | class LURepairDiskSizes(NoHooksLU): |
2026 | 60975797 | Iustin Pop | """Verifies the cluster disks sizes.
|
2027 | 60975797 | Iustin Pop |
|
2028 | 60975797 | Iustin Pop | """
|
2029 | 60975797 | Iustin Pop | _OP_REQP = ["instances"]
|
2030 | 60975797 | Iustin Pop | REQ_BGL = False
|
2031 | 60975797 | Iustin Pop | |
2032 | 60975797 | Iustin Pop | def ExpandNames(self): |
2033 | 60975797 | Iustin Pop | if not isinstance(self.op.instances, list): |
2034 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'", |
2035 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2036 | 60975797 | Iustin Pop | |
2037 | 60975797 | Iustin Pop | if self.op.instances: |
2038 | 60975797 | Iustin Pop | self.wanted_names = []
|
2039 | 60975797 | Iustin Pop | for name in self.op.instances: |
2040 | cf26a87a | Iustin Pop | full_name = _ExpandInstanceName(self.cfg, name)
|
2041 | 60975797 | Iustin Pop | self.wanted_names.append(full_name)
|
2042 | 60975797 | Iustin Pop | self.needed_locks = {
|
2043 | 60975797 | Iustin Pop | locking.LEVEL_NODE: [], |
2044 | 60975797 | Iustin Pop | locking.LEVEL_INSTANCE: self.wanted_names,
|
2045 | 60975797 | Iustin Pop | } |
2046 | 60975797 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
2047 | 60975797 | Iustin Pop | else:
|
2048 | 60975797 | Iustin Pop | self.wanted_names = None |
2049 | 60975797 | Iustin Pop | self.needed_locks = {
|
2050 | 60975797 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET, |
2051 | 60975797 | Iustin Pop | locking.LEVEL_INSTANCE: locking.ALL_SET, |
2052 | 60975797 | Iustin Pop | } |
2053 | 60975797 | Iustin Pop | self.share_locks = dict(((i, 1) for i in locking.LEVELS)) |
2054 | 60975797 | Iustin Pop | |
2055 | 60975797 | Iustin Pop | def DeclareLocks(self, level): |
2056 | 60975797 | Iustin Pop | if level == locking.LEVEL_NODE and self.wanted_names is not None: |
2057 | 60975797 | Iustin Pop | self._LockInstancesNodes(primary_only=True) |
2058 | 60975797 | Iustin Pop | |
2059 | 60975797 | Iustin Pop | def CheckPrereq(self): |
2060 | 60975797 | Iustin Pop | """Check prerequisites.
|
2061 | 60975797 | Iustin Pop |
|
2062 | 60975797 | Iustin Pop | This only checks the optional instance list against the existing names.
|
2063 | 60975797 | Iustin Pop |
|
2064 | 60975797 | Iustin Pop | """
|
2065 | 60975797 | Iustin Pop | if self.wanted_names is None: |
2066 | 60975797 | Iustin Pop | self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE] |
2067 | 60975797 | Iustin Pop | |
2068 | 60975797 | Iustin Pop | self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name |
2069 | 60975797 | Iustin Pop | in self.wanted_names] |
2070 | 60975797 | Iustin Pop | |
2071 | b775c337 | Iustin Pop | def _EnsureChildSizes(self, disk): |
2072 | b775c337 | Iustin Pop | """Ensure children of the disk have the needed disk size.
|
2073 | b775c337 | Iustin Pop |
|
2074 | b775c337 | Iustin Pop | This is valid mainly for DRBD8 and fixes an issue where the
|
2075 | b775c337 | Iustin Pop | children have smaller disk size.
|
2076 | b775c337 | Iustin Pop |
|
2077 | b775c337 | Iustin Pop | @param disk: an L{ganeti.objects.Disk} object
|
2078 | b775c337 | Iustin Pop |
|
2079 | b775c337 | Iustin Pop | """
|
2080 | b775c337 | Iustin Pop | if disk.dev_type == constants.LD_DRBD8:
|
2081 | b775c337 | Iustin Pop | assert disk.children, "Empty children for DRBD8?" |
2082 | b775c337 | Iustin Pop | fchild = disk.children[0]
|
2083 | b775c337 | Iustin Pop | mismatch = fchild.size < disk.size |
2084 | b775c337 | Iustin Pop | if mismatch:
|
2085 | b775c337 | Iustin Pop | self.LogInfo("Child disk has size %d, parent %d, fixing", |
2086 | b775c337 | Iustin Pop | fchild.size, disk.size) |
2087 | b775c337 | Iustin Pop | fchild.size = disk.size |
2088 | b775c337 | Iustin Pop | |
2089 | b775c337 | Iustin Pop | # and we recurse on this child only, not on the metadev
|
2090 | b775c337 | Iustin Pop | return self._EnsureChildSizes(fchild) or mismatch |
2091 | b775c337 | Iustin Pop | else:
|
2092 | b775c337 | Iustin Pop | return False |
2093 | b775c337 | Iustin Pop | |
2094 | 60975797 | Iustin Pop | def Exec(self, feedback_fn): |
2095 | 60975797 | Iustin Pop | """Verify the size of cluster disks.
|
2096 | 60975797 | Iustin Pop |
|
2097 | 60975797 | Iustin Pop | """
|
2098 | 60975797 | Iustin Pop | # TODO: check child disks too
|
2099 | 60975797 | Iustin Pop | # TODO: check differences in size between primary/secondary nodes
|
2100 | 60975797 | Iustin Pop | per_node_disks = {} |
2101 | 60975797 | Iustin Pop | for instance in self.wanted_instances: |
2102 | 60975797 | Iustin Pop | pnode = instance.primary_node |
2103 | 60975797 | Iustin Pop | if pnode not in per_node_disks: |
2104 | 60975797 | Iustin Pop | per_node_disks[pnode] = [] |
2105 | 60975797 | Iustin Pop | for idx, disk in enumerate(instance.disks): |
2106 | 60975797 | Iustin Pop | per_node_disks[pnode].append((instance, idx, disk)) |
2107 | 60975797 | Iustin Pop | |
2108 | 60975797 | Iustin Pop | changed = [] |
2109 | 60975797 | Iustin Pop | for node, dskl in per_node_disks.items(): |
2110 | 4d9e6835 | Iustin Pop | newl = [v[2].Copy() for v in dskl] |
2111 | 4d9e6835 | Iustin Pop | for dsk in newl: |
2112 | 4d9e6835 | Iustin Pop | self.cfg.SetDiskID(dsk, node)
|
2113 | 4d9e6835 | Iustin Pop | result = self.rpc.call_blockdev_getsizes(node, newl)
|
2114 | 3cebe102 | Michael Hanselmann | if result.fail_msg:
|
2115 | 60975797 | Iustin Pop | self.LogWarning("Failure in blockdev_getsizes call to node" |
2116 | 60975797 | Iustin Pop | " %s, ignoring", node)
|
2117 | 60975797 | Iustin Pop | continue
|
2118 | 60975797 | Iustin Pop | if len(result.data) != len(dskl): |
2119 | 60975797 | Iustin Pop | self.LogWarning("Invalid result from node %s, ignoring node results", |
2120 | 60975797 | Iustin Pop | node) |
2121 | 60975797 | Iustin Pop | continue
|
2122 | 60975797 | Iustin Pop | for ((instance, idx, disk), size) in zip(dskl, result.data): |
2123 | 60975797 | Iustin Pop | if size is None: |
2124 | 60975797 | Iustin Pop | self.LogWarning("Disk %d of instance %s did not return size" |
2125 | 60975797 | Iustin Pop | " information, ignoring", idx, instance.name)
|
2126 | 60975797 | Iustin Pop | continue
|
2127 | 60975797 | Iustin Pop | if not isinstance(size, (int, long)): |
2128 | 60975797 | Iustin Pop | self.LogWarning("Disk %d of instance %s did not return valid" |
2129 | 60975797 | Iustin Pop | " size information, ignoring", idx, instance.name)
|
2130 | 60975797 | Iustin Pop | continue
|
2131 | 60975797 | Iustin Pop | size = size >> 20
|
2132 | 60975797 | Iustin Pop | if size != disk.size:
|
2133 | 60975797 | Iustin Pop | self.LogInfo("Disk %d of instance %s has mismatched size," |
2134 | 60975797 | Iustin Pop | " correcting: recorded %d, actual %d", idx,
|
2135 | 60975797 | Iustin Pop | instance.name, disk.size, size) |
2136 | 60975797 | Iustin Pop | disk.size = size |
2137 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
2138 | 60975797 | Iustin Pop | changed.append((instance.name, idx, size)) |
2139 | b775c337 | Iustin Pop | if self._EnsureChildSizes(disk): |
2140 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
2141 | b775c337 | Iustin Pop | changed.append((instance.name, idx, disk.size)) |
2142 | 60975797 | Iustin Pop | return changed
|
2143 | 60975797 | Iustin Pop | |
2144 | 60975797 | Iustin Pop | |
2145 | 07bd8a51 | Iustin Pop | class LURenameCluster(LogicalUnit): |
2146 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
2147 | 07bd8a51 | Iustin Pop |
|
2148 | 07bd8a51 | Iustin Pop | """
|
2149 | 07bd8a51 | Iustin Pop | HPATH = "cluster-rename"
|
2150 | 07bd8a51 | Iustin Pop | HTYPE = constants.HTYPE_CLUSTER |
2151 | 07bd8a51 | Iustin Pop | _OP_REQP = ["name"]
|
2152 | 07bd8a51 | Iustin Pop | |
2153 | 07bd8a51 | Iustin Pop | def BuildHooksEnv(self): |
2154 | 07bd8a51 | Iustin Pop | """Build hooks env.
|
2155 | 07bd8a51 | Iustin Pop |
|
2156 | 07bd8a51 | Iustin Pop | """
|
2157 | 07bd8a51 | Iustin Pop | env = { |
2158 | d6a02168 | Michael Hanselmann | "OP_TARGET": self.cfg.GetClusterName(), |
2159 | 07bd8a51 | Iustin Pop | "NEW_NAME": self.op.name, |
2160 | 07bd8a51 | Iustin Pop | } |
2161 | d6a02168 | Michael Hanselmann | mn = self.cfg.GetMasterNode()
|
2162 | 47a72f18 | Iustin Pop | all_nodes = self.cfg.GetNodeList()
|
2163 | 47a72f18 | Iustin Pop | return env, [mn], all_nodes
|
2164 | 07bd8a51 | Iustin Pop | |
2165 | 07bd8a51 | Iustin Pop | def CheckPrereq(self): |
2166 | 07bd8a51 | Iustin Pop | """Verify that the passed name is a valid one.
|
2167 | 07bd8a51 | Iustin Pop |
|
2168 | 07bd8a51 | Iustin Pop | """
|
2169 | 104f4ca1 | Iustin Pop | hostname = utils.GetHostInfo(self.op.name)
|
2170 | 07bd8a51 | Iustin Pop | |
2171 | bcf043c9 | Iustin Pop | new_name = hostname.name |
2172 | bcf043c9 | Iustin Pop | self.ip = new_ip = hostname.ip
|
2173 | d6a02168 | Michael Hanselmann | old_name = self.cfg.GetClusterName()
|
2174 | d6a02168 | Michael Hanselmann | old_ip = self.cfg.GetMasterIP()
|
2175 | 07bd8a51 | Iustin Pop | if new_name == old_name and new_ip == old_ip: |
2176 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("Neither the name nor the IP address of the" |
2177 | 5c983ee5 | Iustin Pop | " cluster has changed",
|
2178 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2179 | 07bd8a51 | Iustin Pop | if new_ip != old_ip:
|
2180 | 937f983d | Guido Trotter | if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
|
2181 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("The given cluster IP address (%s) is" |
2182 | 07bd8a51 | Iustin Pop | " reachable on the network. Aborting." %
|
2183 | 5c983ee5 | Iustin Pop | new_ip, errors.ECODE_NOTUNIQUE) |
2184 | 07bd8a51 | Iustin Pop | |
2185 | 07bd8a51 | Iustin Pop | self.op.name = new_name
|
2186 | 07bd8a51 | Iustin Pop | |
2187 | 07bd8a51 | Iustin Pop | def Exec(self, feedback_fn): |
2188 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
2189 | 07bd8a51 | Iustin Pop |
|
2190 | 07bd8a51 | Iustin Pop | """
|
2191 | 07bd8a51 | Iustin Pop | clustername = self.op.name
|
2192 | 07bd8a51 | Iustin Pop | ip = self.ip
|
2193 | 07bd8a51 | Iustin Pop | |
2194 | 07bd8a51 | Iustin Pop | # shutdown the master IP
|
2195 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
2196 | 781de953 | Iustin Pop | result = self.rpc.call_node_stop_master(master, False) |
2197 | 4c4e4e1e | Iustin Pop | result.Raise("Could not disable the master role")
|
2198 | 07bd8a51 | Iustin Pop | |
2199 | 07bd8a51 | Iustin Pop | try:
|
2200 | 55cf7d83 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
2201 | 55cf7d83 | Iustin Pop | cluster.cluster_name = clustername |
2202 | 55cf7d83 | Iustin Pop | cluster.master_ip = ip |
2203 | a4eae71f | Michael Hanselmann | self.cfg.Update(cluster, feedback_fn)
|
2204 | ec85e3d5 | Iustin Pop | |
2205 | ec85e3d5 | Iustin Pop | # update the known hosts file
|
2206 | ec85e3d5 | Iustin Pop | ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
|
2207 | ec85e3d5 | Iustin Pop | node_list = self.cfg.GetNodeList()
|
2208 | ec85e3d5 | Iustin Pop | try:
|
2209 | ec85e3d5 | Iustin Pop | node_list.remove(master) |
2210 | ec85e3d5 | Iustin Pop | except ValueError: |
2211 | ec85e3d5 | Iustin Pop | pass
|
2212 | ec85e3d5 | Iustin Pop | result = self.rpc.call_upload_file(node_list,
|
2213 | ec85e3d5 | Iustin Pop | constants.SSH_KNOWN_HOSTS_FILE) |
2214 | ec85e3d5 | Iustin Pop | for to_node, to_result in result.iteritems(): |
2215 | 6f7d4e75 | Iustin Pop | msg = to_result.fail_msg |
2216 | 6f7d4e75 | Iustin Pop | if msg:
|
2217 | 6f7d4e75 | Iustin Pop | msg = ("Copy of file %s to node %s failed: %s" %
|
2218 | 6f7d4e75 | Iustin Pop | (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg)) |
2219 | 6f7d4e75 | Iustin Pop | self.proc.LogWarning(msg)
|
2220 | ec85e3d5 | Iustin Pop | |
2221 | 07bd8a51 | Iustin Pop | finally:
|
2222 | 3583908a | Guido Trotter | result = self.rpc.call_node_start_master(master, False, False) |
2223 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
2224 | b726aff0 | Iustin Pop | if msg:
|
2225 | 86d9d3bb | Iustin Pop | self.LogWarning("Could not re-enable the master role on" |
2226 | b726aff0 | Iustin Pop | " the master, please restart manually: %s", msg)
|
2227 | 07bd8a51 | Iustin Pop | |
2228 | 07bd8a51 | Iustin Pop | |
2229 | 8084f9f6 | Manuel Franceschini | def _RecursiveCheckIfLVMBased(disk): |
2230 | 8084f9f6 | Manuel Franceschini | """Check if the given disk or its children are lvm-based.
|
2231 | 8084f9f6 | Manuel Franceschini |
|
2232 | e4376078 | Iustin Pop | @type disk: L{objects.Disk}
|
2233 | e4376078 | Iustin Pop | @param disk: the disk to check
|
2234 | 5bbd3f7f | Michael Hanselmann | @rtype: boolean
|
2235 | e4376078 | Iustin Pop | @return: boolean indicating whether a LD_LV dev_type was found or not
|
2236 | 8084f9f6 | Manuel Franceschini |
|
2237 | 8084f9f6 | Manuel Franceschini | """
|
2238 | 8084f9f6 | Manuel Franceschini | if disk.children:
|
2239 | 8084f9f6 | Manuel Franceschini | for chdisk in disk.children: |
2240 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(chdisk):
|
2241 | 8084f9f6 | Manuel Franceschini | return True |
2242 | 8084f9f6 | Manuel Franceschini | return disk.dev_type == constants.LD_LV
|
2243 | 8084f9f6 | Manuel Franceschini | |
2244 | 8084f9f6 | Manuel Franceschini | |
2245 | 8084f9f6 | Manuel Franceschini | class LUSetClusterParams(LogicalUnit): |
2246 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
2247 | 8084f9f6 | Manuel Franceschini |
|
2248 | 8084f9f6 | Manuel Franceschini | """
|
2249 | 8084f9f6 | Manuel Franceschini | HPATH = "cluster-modify"
|
2250 | 8084f9f6 | Manuel Franceschini | HTYPE = constants.HTYPE_CLUSTER |
2251 | 8084f9f6 | Manuel Franceschini | _OP_REQP = [] |
2252 | c53279cf | Guido Trotter | REQ_BGL = False
|
2253 | c53279cf | Guido Trotter | |
2254 | 3994f455 | Iustin Pop | def CheckArguments(self): |
2255 | 4b7735f9 | Iustin Pop | """Check parameters
|
2256 | 4b7735f9 | Iustin Pop |
|
2257 | 4b7735f9 | Iustin Pop | """
|
2258 | 4b7735f9 | Iustin Pop | if not hasattr(self.op, "candidate_pool_size"): |
2259 | 4b7735f9 | Iustin Pop | self.op.candidate_pool_size = None |
2260 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size is not None: |
2261 | 4b7735f9 | Iustin Pop | try:
|
2262 | 4b7735f9 | Iustin Pop | self.op.candidate_pool_size = int(self.op.candidate_pool_size) |
2263 | 3994f455 | Iustin Pop | except (ValueError, TypeError), err: |
2264 | 4b7735f9 | Iustin Pop | raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" % |
2265 | 5c983ee5 | Iustin Pop | str(err), errors.ECODE_INVAL)
|
2266 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size < 1: |
2267 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("At least one master candidate needed", |
2268 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2269 | 1338f2b4 | Balazs Lecz | |
2270 | 3953242f | Iustin Pop | _CheckBooleanOpField(self.op, "maintain_node_health") |
2271 | 4b7735f9 | Iustin Pop | |
2272 | 1338f2b4 | Balazs Lecz | if self.op.uid_pool: |
2273 | 1338f2b4 | Balazs Lecz | uidpool.CheckUidPool(self.op.uid_pool)
|
2274 | 1338f2b4 | Balazs Lecz | |
2275 | c53279cf | Guido Trotter | def ExpandNames(self): |
2276 | c53279cf | Guido Trotter | # FIXME: in the future maybe other cluster params won't require checking on
|
2277 | c53279cf | Guido Trotter | # all nodes to be modified.
|
2278 | c53279cf | Guido Trotter | self.needed_locks = {
|
2279 | c53279cf | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
2280 | c53279cf | Guido Trotter | } |
2281 | c53279cf | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
2282 | 8084f9f6 | Manuel Franceschini | |
2283 | 8084f9f6 | Manuel Franceschini | def BuildHooksEnv(self): |
2284 | 8084f9f6 | Manuel Franceschini | """Build hooks env.
|
2285 | 8084f9f6 | Manuel Franceschini |
|
2286 | 8084f9f6 | Manuel Franceschini | """
|
2287 | 8084f9f6 | Manuel Franceschini | env = { |
2288 | d6a02168 | Michael Hanselmann | "OP_TARGET": self.cfg.GetClusterName(), |
2289 | 8084f9f6 | Manuel Franceschini | "NEW_VG_NAME": self.op.vg_name, |
2290 | 8084f9f6 | Manuel Franceschini | } |
2291 | d6a02168 | Michael Hanselmann | mn = self.cfg.GetMasterNode()
|
2292 | 8084f9f6 | Manuel Franceschini | return env, [mn], [mn]
|
2293 | 8084f9f6 | Manuel Franceschini | |
2294 | 8084f9f6 | Manuel Franceschini | def CheckPrereq(self): |
2295 | 8084f9f6 | Manuel Franceschini | """Check prerequisites.
|
2296 | 8084f9f6 | Manuel Franceschini |
|
2297 | 8084f9f6 | Manuel Franceschini | This checks whether the given params don't conflict and
|
2298 | 5f83e263 | Iustin Pop | if the given volume group is valid.
|
2299 | 8084f9f6 | Manuel Franceschini |
|
2300 | 8084f9f6 | Manuel Franceschini | """
|
2301 | 779c15bb | Iustin Pop | if self.op.vg_name is not None and not self.op.vg_name: |
2302 | c53279cf | Guido Trotter | instances = self.cfg.GetAllInstancesInfo().values()
|
2303 | 8084f9f6 | Manuel Franceschini | for inst in instances: |
2304 | 8084f9f6 | Manuel Franceschini | for disk in inst.disks: |
2305 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(disk):
|
2306 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Cannot disable lvm storage while" |
2307 | 5c983ee5 | Iustin Pop | " lvm-based instances exist",
|
2308 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2309 | 8084f9f6 | Manuel Franceschini | |
2310 | 779c15bb | Iustin Pop | node_list = self.acquired_locks[locking.LEVEL_NODE]
|
2311 | 779c15bb | Iustin Pop | |
2312 | 8084f9f6 | Manuel Franceschini | # if vg_name not None, checks given volume group on all nodes
|
2313 | 8084f9f6 | Manuel Franceschini | if self.op.vg_name: |
2314 | 72737a7f | Iustin Pop | vglist = self.rpc.call_vg_list(node_list)
|
2315 | 8084f9f6 | Manuel Franceschini | for node in node_list: |
2316 | 4c4e4e1e | Iustin Pop | msg = vglist[node].fail_msg |
2317 | e480923b | Iustin Pop | if msg:
|
2318 | 781de953 | Iustin Pop | # ignoring down node
|
2319 | e480923b | Iustin Pop | self.LogWarning("Error while gathering data on node %s" |
2320 | e480923b | Iustin Pop | " (ignoring node): %s", node, msg)
|
2321 | 781de953 | Iustin Pop | continue
|
2322 | e480923b | Iustin Pop | vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload, |
2323 | 781de953 | Iustin Pop | self.op.vg_name,
|
2324 | 8d1a2a64 | Michael Hanselmann | constants.MIN_VG_SIZE) |
2325 | 8084f9f6 | Manuel Franceschini | if vgstatus:
|
2326 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Error on node '%s': %s" % |
2327 | 5c983ee5 | Iustin Pop | (node, vgstatus), errors.ECODE_ENVIRON) |
2328 | 8084f9f6 | Manuel Franceschini | |
2329 | 779c15bb | Iustin Pop | self.cluster = cluster = self.cfg.GetClusterInfo() |
2330 | 5af3da74 | Guido Trotter | # validate params changes
|
2331 | 779c15bb | Iustin Pop | if self.op.beparams: |
2332 | a5728081 | Guido Trotter | utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
|
2333 | abe609b2 | Guido Trotter | self.new_beparams = objects.FillDict(
|
2334 | 4ef7f423 | Guido Trotter | cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
|
2335 | 779c15bb | Iustin Pop | |
2336 | 5af3da74 | Guido Trotter | if self.op.nicparams: |
2337 | 5af3da74 | Guido Trotter | utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
|
2338 | 5af3da74 | Guido Trotter | self.new_nicparams = objects.FillDict(
|
2339 | 5af3da74 | Guido Trotter | cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
|
2340 | 5af3da74 | Guido Trotter | objects.NIC.CheckParameterSyntax(self.new_nicparams)
|
2341 | 90b704a1 | Guido Trotter | nic_errors = [] |
2342 | 90b704a1 | Guido Trotter | |
2343 | 90b704a1 | Guido Trotter | # check all instances for consistency
|
2344 | 90b704a1 | Guido Trotter | for instance in self.cfg.GetAllInstancesInfo().values(): |
2345 | 90b704a1 | Guido Trotter | for nic_idx, nic in enumerate(instance.nics): |
2346 | 90b704a1 | Guido Trotter | params_copy = copy.deepcopy(nic.nicparams) |
2347 | 90b704a1 | Guido Trotter | params_filled = objects.FillDict(self.new_nicparams, params_copy)
|
2348 | 90b704a1 | Guido Trotter | |
2349 | 90b704a1 | Guido Trotter | # check parameter syntax
|
2350 | 90b704a1 | Guido Trotter | try:
|
2351 | 90b704a1 | Guido Trotter | objects.NIC.CheckParameterSyntax(params_filled) |
2352 | 90b704a1 | Guido Trotter | except errors.ConfigurationError, err:
|
2353 | 90b704a1 | Guido Trotter | nic_errors.append("Instance %s, nic/%d: %s" %
|
2354 | 90b704a1 | Guido Trotter | (instance.name, nic_idx, err)) |
2355 | 90b704a1 | Guido Trotter | |
2356 | 90b704a1 | Guido Trotter | # if we're moving instances to routed, check that they have an ip
|
2357 | 90b704a1 | Guido Trotter | target_mode = params_filled[constants.NIC_MODE] |
2358 | 90b704a1 | Guido Trotter | if target_mode == constants.NIC_MODE_ROUTED and not nic.ip: |
2359 | 90b704a1 | Guido Trotter | nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
|
2360 | 90b704a1 | Guido Trotter | (instance.name, nic_idx)) |
2361 | 90b704a1 | Guido Trotter | if nic_errors:
|
2362 | 90b704a1 | Guido Trotter | raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" % |
2363 | 90b704a1 | Guido Trotter | "\n".join(nic_errors))
|
2364 | 5af3da74 | Guido Trotter | |
2365 | 779c15bb | Iustin Pop | # hypervisor list/parameters
|
2366 | abe609b2 | Guido Trotter | self.new_hvparams = objects.FillDict(cluster.hvparams, {})
|
2367 | 779c15bb | Iustin Pop | if self.op.hvparams: |
2368 | 779c15bb | Iustin Pop | if not isinstance(self.op.hvparams, dict): |
2369 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid 'hvparams' parameter on input", |
2370 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2371 | 779c15bb | Iustin Pop | for hv_name, hv_dict in self.op.hvparams.items(): |
2372 | 779c15bb | Iustin Pop | if hv_name not in self.new_hvparams: |
2373 | 779c15bb | Iustin Pop | self.new_hvparams[hv_name] = hv_dict
|
2374 | 779c15bb | Iustin Pop | else:
|
2375 | 779c15bb | Iustin Pop | self.new_hvparams[hv_name].update(hv_dict)
|
2376 | 779c15bb | Iustin Pop | |
2377 | 17463d22 | Renรฉ Nussbaumer | # os hypervisor parameters
|
2378 | 17463d22 | Renรฉ Nussbaumer | self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
|
2379 | 17463d22 | Renรฉ Nussbaumer | if self.op.os_hvp: |
2380 | 17463d22 | Renรฉ Nussbaumer | if not isinstance(self.op.os_hvp, dict): |
2381 | 17463d22 | Renรฉ Nussbaumer | raise errors.OpPrereqError("Invalid 'os_hvp' parameter on input", |
2382 | 17463d22 | Renรฉ Nussbaumer | errors.ECODE_INVAL) |
2383 | 17463d22 | Renรฉ Nussbaumer | for os_name, hvs in self.op.os_hvp.items(): |
2384 | 17463d22 | Renรฉ Nussbaumer | if not isinstance(hvs, dict): |
2385 | 17463d22 | Renรฉ Nussbaumer | raise errors.OpPrereqError(("Invalid 'os_hvp' parameter on" |
2386 | 17463d22 | Renรฉ Nussbaumer | " input"), errors.ECODE_INVAL)
|
2387 | 17463d22 | Renรฉ Nussbaumer | if os_name not in self.new_os_hvp: |
2388 | 17463d22 | Renรฉ Nussbaumer | self.new_os_hvp[os_name] = hvs
|
2389 | 17463d22 | Renรฉ Nussbaumer | else:
|
2390 | 17463d22 | Renรฉ Nussbaumer | for hv_name, hv_dict in hvs.items(): |
2391 | 17463d22 | Renรฉ Nussbaumer | if hv_name not in self.new_os_hvp[os_name]: |
2392 | 17463d22 | Renรฉ Nussbaumer | self.new_os_hvp[os_name][hv_name] = hv_dict
|
2393 | 17463d22 | Renรฉ Nussbaumer | else:
|
2394 | 17463d22 | Renรฉ Nussbaumer | self.new_os_hvp[os_name][hv_name].update(hv_dict)
|
2395 | 17463d22 | Renรฉ Nussbaumer | |
2396 | 779c15bb | Iustin Pop | if self.op.enabled_hypervisors is not None: |
2397 | 779c15bb | Iustin Pop | self.hv_list = self.op.enabled_hypervisors |
2398 | b119bccb | Guido Trotter | if not self.hv_list: |
2399 | b119bccb | Guido Trotter | raise errors.OpPrereqError("Enabled hypervisors list must contain at" |
2400 | 5c983ee5 | Iustin Pop | " least one member",
|
2401 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2402 | b119bccb | Guido Trotter | invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES |
2403 | b119bccb | Guido Trotter | if invalid_hvs:
|
2404 | b119bccb | Guido Trotter | raise errors.OpPrereqError("Enabled hypervisors contains invalid" |
2405 | ab3e6da8 | Iustin Pop | " entries: %s" %
|
2406 | ab3e6da8 | Iustin Pop | utils.CommaJoin(invalid_hvs), |
2407 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2408 | 779c15bb | Iustin Pop | else:
|
2409 | 779c15bb | Iustin Pop | self.hv_list = cluster.enabled_hypervisors
|
2410 | 779c15bb | Iustin Pop | |
2411 | 779c15bb | Iustin Pop | if self.op.hvparams or self.op.enabled_hypervisors is not None: |
2412 | 779c15bb | Iustin Pop | # either the enabled list has changed, or the parameters have, validate
|
2413 | 779c15bb | Iustin Pop | for hv_name, hv_params in self.new_hvparams.items(): |
2414 | 779c15bb | Iustin Pop | if ((self.op.hvparams and hv_name in self.op.hvparams) or |
2415 | 779c15bb | Iustin Pop | (self.op.enabled_hypervisors and |
2416 | 779c15bb | Iustin Pop | hv_name in self.op.enabled_hypervisors)): |
2417 | 779c15bb | Iustin Pop | # either this is a new hypervisor, or its parameters have changed
|
2418 | 779c15bb | Iustin Pop | hv_class = hypervisor.GetHypervisor(hv_name) |
2419 | a5728081 | Guido Trotter | utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES) |
2420 | 779c15bb | Iustin Pop | hv_class.CheckParameterSyntax(hv_params) |
2421 | 779c15bb | Iustin Pop | _CheckHVParams(self, node_list, hv_name, hv_params)
|
2422 | 779c15bb | Iustin Pop | |
2423 | cced4c39 | Iustin Pop | if self.op.os_hvp: |
2424 | cced4c39 | Iustin Pop | # no need to check any newly-enabled hypervisors, since the
|
2425 | cced4c39 | Iustin Pop | # defaults have already been checked in the above code-block
|
2426 | cced4c39 | Iustin Pop | for os_name, os_hvp in self.new_os_hvp.items(): |
2427 | cced4c39 | Iustin Pop | for hv_name, hv_params in os_hvp.items(): |
2428 | cced4c39 | Iustin Pop | utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES) |
2429 | cced4c39 | Iustin Pop | # we need to fill in the new os_hvp on top of the actual hv_p
|
2430 | cced4c39 | Iustin Pop | cluster_defaults = self.new_hvparams.get(hv_name, {})
|
2431 | cced4c39 | Iustin Pop | new_osp = objects.FillDict(cluster_defaults, hv_params) |
2432 | cced4c39 | Iustin Pop | hv_class = hypervisor.GetHypervisor(hv_name) |
2433 | cced4c39 | Iustin Pop | hv_class.CheckParameterSyntax(new_osp) |
2434 | cced4c39 | Iustin Pop | _CheckHVParams(self, node_list, hv_name, new_osp)
|
2435 | cced4c39 | Iustin Pop | |
2436 | cced4c39 | Iustin Pop | |
2437 | 8084f9f6 | Manuel Franceschini | def Exec(self, feedback_fn): |
2438 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
2439 | 8084f9f6 | Manuel Franceschini |
|
2440 | 8084f9f6 | Manuel Franceschini | """
|
2441 | 779c15bb | Iustin Pop | if self.op.vg_name is not None: |
2442 | b2482333 | Guido Trotter | new_volume = self.op.vg_name
|
2443 | b2482333 | Guido Trotter | if not new_volume: |
2444 | b2482333 | Guido Trotter | new_volume = None
|
2445 | b2482333 | Guido Trotter | if new_volume != self.cfg.GetVGName(): |
2446 | b2482333 | Guido Trotter | self.cfg.SetVGName(new_volume)
|
2447 | 779c15bb | Iustin Pop | else:
|
2448 | 779c15bb | Iustin Pop | feedback_fn("Cluster LVM configuration already in desired"
|
2449 | 779c15bb | Iustin Pop | " state, not changing")
|
2450 | 779c15bb | Iustin Pop | if self.op.hvparams: |
2451 | 779c15bb | Iustin Pop | self.cluster.hvparams = self.new_hvparams |
2452 | 17463d22 | Renรฉ Nussbaumer | if self.op.os_hvp: |
2453 | 17463d22 | Renรฉ Nussbaumer | self.cluster.os_hvp = self.new_os_hvp |
2454 | 779c15bb | Iustin Pop | if self.op.enabled_hypervisors is not None: |
2455 | 779c15bb | Iustin Pop | self.cluster.enabled_hypervisors = self.op.enabled_hypervisors |
2456 | 779c15bb | Iustin Pop | if self.op.beparams: |
2457 | 4ef7f423 | Guido Trotter | self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams |
2458 | 5af3da74 | Guido Trotter | if self.op.nicparams: |
2459 | 5af3da74 | Guido Trotter | self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams |
2460 | 5af3da74 | Guido Trotter | |
2461 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size is not None: |
2462 | 4b7735f9 | Iustin Pop | self.cluster.candidate_pool_size = self.op.candidate_pool_size |
2463 | 75e914fb | Iustin Pop | # we need to update the pool size here, otherwise the save will fail
|
2464 | 44485f49 | Guido Trotter | _AdjustCandidatePool(self, [])
|
2465 | 4b7735f9 | Iustin Pop | |
2466 | 3953242f | Iustin Pop | if self.op.maintain_node_health is not None: |
2467 | 3953242f | Iustin Pop | self.cluster.maintain_node_health = self.op.maintain_node_health |
2468 | 3953242f | Iustin Pop | |
2469 | 1338f2b4 | Balazs Lecz | if self.op.uid_pool is not None: |
2470 | 1338f2b4 | Balazs Lecz | self.cluster.uid_pool = self.op.uid_pool |
2471 | 1338f2b4 | Balazs Lecz | |
2472 | a4eae71f | Michael Hanselmann | self.cfg.Update(self.cluster, feedback_fn) |
2473 | 8084f9f6 | Manuel Franceschini | |
2474 | 8084f9f6 | Manuel Franceschini | |
2475 | 28eddce5 | Guido Trotter | def _RedistributeAncillaryFiles(lu, additional_nodes=None): |
2476 | 28eddce5 | Guido Trotter | """Distribute additional files which are part of the cluster configuration.
|
2477 | 28eddce5 | Guido Trotter |
|
2478 | 28eddce5 | Guido Trotter | ConfigWriter takes care of distributing the config and ssconf files, but
|
2479 | 28eddce5 | Guido Trotter | there are more files which should be distributed to all nodes. This function
|
2480 | 28eddce5 | Guido Trotter | makes sure those are copied.
|
2481 | 28eddce5 | Guido Trotter |
|
2482 | 28eddce5 | Guido Trotter | @param lu: calling logical unit
|
2483 | 28eddce5 | Guido Trotter | @param additional_nodes: list of nodes not in the config to distribute to
|
2484 | 28eddce5 | Guido Trotter |
|
2485 | 28eddce5 | Guido Trotter | """
|
2486 | 28eddce5 | Guido Trotter | # 1. Gather target nodes
|
2487 | 28eddce5 | Guido Trotter | myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode()) |
2488 | 6819dc49 | Iustin Pop | dist_nodes = lu.cfg.GetOnlineNodeList() |
2489 | 28eddce5 | Guido Trotter | if additional_nodes is not None: |
2490 | 28eddce5 | Guido Trotter | dist_nodes.extend(additional_nodes) |
2491 | 28eddce5 | Guido Trotter | if myself.name in dist_nodes: |
2492 | 28eddce5 | Guido Trotter | dist_nodes.remove(myself.name) |
2493 | a4eae71f | Michael Hanselmann | |
2494 | 28eddce5 | Guido Trotter | # 2. Gather files to distribute
|
2495 | 28eddce5 | Guido Trotter | dist_files = set([constants.ETC_HOSTS,
|
2496 | 28eddce5 | Guido Trotter | constants.SSH_KNOWN_HOSTS_FILE, |
2497 | 28eddce5 | Guido Trotter | constants.RAPI_CERT_FILE, |
2498 | 28eddce5 | Guido Trotter | constants.RAPI_USERS_FILE, |
2499 | 6b7d5878 | Michael Hanselmann | constants.CONFD_HMAC_KEY, |
2500 | 28eddce5 | Guido Trotter | ]) |
2501 | e1b8653f | Guido Trotter | |
2502 | e1b8653f | Guido Trotter | enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors |
2503 | e1b8653f | Guido Trotter | for hv_name in enabled_hypervisors: |
2504 | e1b8653f | Guido Trotter | hv_class = hypervisor.GetHypervisor(hv_name) |
2505 | e1b8653f | Guido Trotter | dist_files.update(hv_class.GetAncillaryFiles()) |
2506 | e1b8653f | Guido Trotter | |
2507 | 28eddce5 | Guido Trotter | # 3. Perform the files upload
|
2508 | 28eddce5 | Guido Trotter | for fname in dist_files: |
2509 | 28eddce5 | Guido Trotter | if os.path.exists(fname):
|
2510 | 28eddce5 | Guido Trotter | result = lu.rpc.call_upload_file(dist_nodes, fname) |
2511 | 28eddce5 | Guido Trotter | for to_node, to_result in result.items(): |
2512 | 6f7d4e75 | Iustin Pop | msg = to_result.fail_msg |
2513 | 6f7d4e75 | Iustin Pop | if msg:
|
2514 | 6f7d4e75 | Iustin Pop | msg = ("Copy of file %s to node %s failed: %s" %
|
2515 | 6f7d4e75 | Iustin Pop | (fname, to_node, msg)) |
2516 | 6f7d4e75 | Iustin Pop | lu.proc.LogWarning(msg) |
2517 | 28eddce5 | Guido Trotter | |
2518 | 28eddce5 | Guido Trotter | |
2519 | afee0879 | Iustin Pop | class LURedistributeConfig(NoHooksLU): |
2520 | afee0879 | Iustin Pop | """Force the redistribution of cluster configuration.
|
2521 | afee0879 | Iustin Pop |
|
2522 | afee0879 | Iustin Pop | This is a very simple LU.
|
2523 | afee0879 | Iustin Pop |
|
2524 | afee0879 | Iustin Pop | """
|
2525 | afee0879 | Iustin Pop | _OP_REQP = [] |
2526 | afee0879 | Iustin Pop | REQ_BGL = False
|
2527 | afee0879 | Iustin Pop | |
2528 | afee0879 | Iustin Pop | def ExpandNames(self): |
2529 | afee0879 | Iustin Pop | self.needed_locks = {
|
2530 | afee0879 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET, |
2531 | afee0879 | Iustin Pop | } |
2532 | afee0879 | Iustin Pop | self.share_locks[locking.LEVEL_NODE] = 1 |
2533 | afee0879 | Iustin Pop | |
2534 | afee0879 | Iustin Pop | def CheckPrereq(self): |
2535 | afee0879 | Iustin Pop | """Check prerequisites.
|
2536 | afee0879 | Iustin Pop |
|
2537 | afee0879 | Iustin Pop | """
|
2538 | afee0879 | Iustin Pop | |
2539 | afee0879 | Iustin Pop | def Exec(self, feedback_fn): |
2540 | afee0879 | Iustin Pop | """Redistribute the configuration.
|
2541 | afee0879 | Iustin Pop |
|
2542 | afee0879 | Iustin Pop | """
|
2543 | a4eae71f | Michael Hanselmann | self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn) |
2544 | 28eddce5 | Guido Trotter | _RedistributeAncillaryFiles(self)
|
2545 | afee0879 | Iustin Pop | |
2546 | afee0879 | Iustin Pop | |
2547 | b6c07b79 | Michael Hanselmann | def _WaitForSync(lu, instance, oneshot=False): |
2548 | a8083063 | Iustin Pop | """Sleep and poll for an instance's disk to sync.
|
2549 | a8083063 | Iustin Pop |
|
2550 | a8083063 | Iustin Pop | """
|
2551 | a8083063 | Iustin Pop | if not instance.disks: |
2552 | a8083063 | Iustin Pop | return True |
2553 | a8083063 | Iustin Pop | |
2554 | a8083063 | Iustin Pop | if not oneshot: |
2555 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
|
2556 | a8083063 | Iustin Pop | |
2557 | a8083063 | Iustin Pop | node = instance.primary_node |
2558 | a8083063 | Iustin Pop | |
2559 | a8083063 | Iustin Pop | for dev in instance.disks: |
2560 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(dev, node) |
2561 | a8083063 | Iustin Pop | |
2562 | 6bcb1446 | Michael Hanselmann | # TODO: Convert to utils.Retry
|
2563 | 6bcb1446 | Michael Hanselmann | |
2564 | a8083063 | Iustin Pop | retries = 0
|
2565 | fbafd7a8 | Iustin Pop | degr_retries = 10 # in seconds, as we sleep 1 second each time |
2566 | a8083063 | Iustin Pop | while True: |
2567 | a8083063 | Iustin Pop | max_time = 0
|
2568 | a8083063 | Iustin Pop | done = True
|
2569 | a8083063 | Iustin Pop | cumul_degraded = False
|
2570 | 72737a7f | Iustin Pop | rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks) |
2571 | 4c4e4e1e | Iustin Pop | msg = rstats.fail_msg |
2572 | 3efa9051 | Iustin Pop | if msg:
|
2573 | 3efa9051 | Iustin Pop | lu.LogWarning("Can't get any data from node %s: %s", node, msg)
|
2574 | a8083063 | Iustin Pop | retries += 1
|
2575 | a8083063 | Iustin Pop | if retries >= 10: |
2576 | 3ecf6786 | Iustin Pop | raise errors.RemoteError("Can't contact node %s for mirror data," |
2577 | 3ecf6786 | Iustin Pop | " aborting." % node)
|
2578 | a8083063 | Iustin Pop | time.sleep(6)
|
2579 | a8083063 | Iustin Pop | continue
|
2580 | 3efa9051 | Iustin Pop | rstats = rstats.payload |
2581 | a8083063 | Iustin Pop | retries = 0
|
2582 | 1492cca7 | Iustin Pop | for i, mstat in enumerate(rstats): |
2583 | a8083063 | Iustin Pop | if mstat is None: |
2584 | 86d9d3bb | Iustin Pop | lu.LogWarning("Can't compute data for node %s/%s",
|
2585 | 86d9d3bb | Iustin Pop | node, instance.disks[i].iv_name) |
2586 | a8083063 | Iustin Pop | continue
|
2587 | 36145b12 | Michael Hanselmann | |
2588 | 36145b12 | Michael Hanselmann | cumul_degraded = (cumul_degraded or
|
2589 | 36145b12 | Michael Hanselmann | (mstat.is_degraded and mstat.sync_percent is None)) |
2590 | 36145b12 | Michael Hanselmann | if mstat.sync_percent is not None: |
2591 | a8083063 | Iustin Pop | done = False
|
2592 | 36145b12 | Michael Hanselmann | if mstat.estimated_time is not None: |
2593 | 36145b12 | Michael Hanselmann | rem_time = "%d estimated seconds remaining" % mstat.estimated_time
|
2594 | 36145b12 | Michael Hanselmann | max_time = mstat.estimated_time |
2595 | a8083063 | Iustin Pop | else:
|
2596 | a8083063 | Iustin Pop | rem_time = "no time estimate"
|
2597 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
|
2598 | 4d4a651d | Michael Hanselmann | (instance.disks[i].iv_name, mstat.sync_percent, |
2599 | 4d4a651d | Michael Hanselmann | rem_time)) |
2600 | fbafd7a8 | Iustin Pop | |
2601 | fbafd7a8 | Iustin Pop | # if we're done but degraded, let's do a few small retries, to
|
2602 | fbafd7a8 | Iustin Pop | # make sure we see a stable and not transient situation; therefore
|
2603 | fbafd7a8 | Iustin Pop | # we force restart of the loop
|
2604 | fbafd7a8 | Iustin Pop | if (done or oneshot) and cumul_degraded and degr_retries > 0: |
2605 | fbafd7a8 | Iustin Pop | logging.info("Degraded disks found, %d retries left", degr_retries)
|
2606 | fbafd7a8 | Iustin Pop | degr_retries -= 1
|
2607 | fbafd7a8 | Iustin Pop | time.sleep(1)
|
2608 | fbafd7a8 | Iustin Pop | continue
|
2609 | fbafd7a8 | Iustin Pop | |
2610 | a8083063 | Iustin Pop | if done or oneshot: |
2611 | a8083063 | Iustin Pop | break
|
2612 | a8083063 | Iustin Pop | |
2613 | d4fa5c23 | Iustin Pop | time.sleep(min(60, max_time)) |
2614 | a8083063 | Iustin Pop | |
2615 | a8083063 | Iustin Pop | if done:
|
2616 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
|
2617 | a8083063 | Iustin Pop | return not cumul_degraded |
2618 | a8083063 | Iustin Pop | |
2619 | a8083063 | Iustin Pop | |
2620 | b9bddb6b | Iustin Pop | def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False): |
2621 | a8083063 | Iustin Pop | """Check that mirrors are not degraded.
|
2622 | a8083063 | Iustin Pop |
|
2623 | 0834c866 | Iustin Pop | The ldisk parameter, if True, will change the test from the
|
2624 | 0834c866 | Iustin Pop | is_degraded attribute (which represents overall non-ok status for
|
2625 | 0834c866 | Iustin Pop | the device(s)) to the ldisk (representing the local storage status).
|
2626 | 0834c866 | Iustin Pop |
|
2627 | a8083063 | Iustin Pop | """
|
2628 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(dev, node) |
2629 | a8083063 | Iustin Pop | |
2630 | a8083063 | Iustin Pop | result = True
|
2631 | 96acbc09 | Michael Hanselmann | |
2632 | a8083063 | Iustin Pop | if on_primary or dev.AssembleOnSecondary(): |
2633 | 72737a7f | Iustin Pop | rstats = lu.rpc.call_blockdev_find(node, dev) |
2634 | 4c4e4e1e | Iustin Pop | msg = rstats.fail_msg |
2635 | 23829f6f | Iustin Pop | if msg:
|
2636 | 23829f6f | Iustin Pop | lu.LogWarning("Can't find disk on node %s: %s", node, msg)
|
2637 | 23829f6f | Iustin Pop | result = False
|
2638 | 23829f6f | Iustin Pop | elif not rstats.payload: |
2639 | 23829f6f | Iustin Pop | lu.LogWarning("Can't find disk on node %s", node)
|
2640 | a8083063 | Iustin Pop | result = False
|
2641 | a8083063 | Iustin Pop | else:
|
2642 | 96acbc09 | Michael Hanselmann | if ldisk:
|
2643 | f208978a | Michael Hanselmann | result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
|
2644 | 96acbc09 | Michael Hanselmann | else:
|
2645 | 96acbc09 | Michael Hanselmann | result = result and not rstats.payload.is_degraded |
2646 | 96acbc09 | Michael Hanselmann | |
2647 | a8083063 | Iustin Pop | if dev.children:
|
2648 | a8083063 | Iustin Pop | for child in dev.children: |
2649 | b9bddb6b | Iustin Pop | result = result and _CheckDiskConsistency(lu, child, node, on_primary)
|
2650 | a8083063 | Iustin Pop | |
2651 | a8083063 | Iustin Pop | return result
|
2652 | a8083063 | Iustin Pop | |
2653 | a8083063 | Iustin Pop | |
2654 | a8083063 | Iustin Pop | class LUDiagnoseOS(NoHooksLU): |
2655 | a8083063 | Iustin Pop | """Logical unit for OS diagnose/query.
|
2656 | a8083063 | Iustin Pop |
|
2657 | a8083063 | Iustin Pop | """
|
2658 | 1f9430d6 | Iustin Pop | _OP_REQP = ["output_fields", "names"] |
2659 | 6bf01bbb | Guido Trotter | REQ_BGL = False
|
2660 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet() |
2661 | 1e288a26 | Guido Trotter | _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants") |
2662 | 1e288a26 | Guido Trotter | # Fields that need calculation of global os validity
|
2663 | 1e288a26 | Guido Trotter | _FIELDS_NEEDVALID = frozenset(["valid", "variants"]) |
2664 | a8083063 | Iustin Pop | |
2665 | 6bf01bbb | Guido Trotter | def ExpandNames(self): |
2666 | 1f9430d6 | Iustin Pop | if self.op.names: |
2667 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Selective OS query not supported", |
2668 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2669 | 1f9430d6 | Iustin Pop | |
2670 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2671 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
2672 | 1f9430d6 | Iustin Pop | selected=self.op.output_fields)
|
2673 | 1f9430d6 | Iustin Pop | |
2674 | 6bf01bbb | Guido Trotter | # Lock all nodes, in shared mode
|
2675 | a6ab004b | Iustin Pop | # Temporary removal of locks, should be reverted later
|
2676 | a6ab004b | Iustin Pop | # TODO: reintroduce locks when they are lighter-weight
|
2677 | 6bf01bbb | Guido Trotter | self.needed_locks = {}
|
2678 | a6ab004b | Iustin Pop | #self.share_locks[locking.LEVEL_NODE] = 1
|
2679 | a6ab004b | Iustin Pop | #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
2680 | 6bf01bbb | Guido Trotter | |
2681 | 6bf01bbb | Guido Trotter | def CheckPrereq(self): |
2682 | 6bf01bbb | Guido Trotter | """Check prerequisites.
|
2683 | 6bf01bbb | Guido Trotter |
|
2684 | 6bf01bbb | Guido Trotter | """
|
2685 | 6bf01bbb | Guido Trotter | |
2686 | 1f9430d6 | Iustin Pop | @staticmethod
|
2687 | 857121ad | Iustin Pop | def _DiagnoseByOS(rlist): |
2688 | 1f9430d6 | Iustin Pop | """Remaps a per-node return list into an a per-os per-node dictionary
|
2689 | 1f9430d6 | Iustin Pop |
|
2690 | e4376078 | Iustin Pop | @param rlist: a map with node names as keys and OS objects as values
|
2691 | 1f9430d6 | Iustin Pop |
|
2692 | e4376078 | Iustin Pop | @rtype: dict
|
2693 | 5fcc718f | Iustin Pop | @return: a dictionary with osnames as keys and as value another map, with
|
2694 | 255dcebd | Iustin Pop | nodes as keys and tuples of (path, status, diagnose) as values, eg::
|
2695 | e4376078 | Iustin Pop |
|
2696 | 255dcebd | Iustin Pop | {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
|
2697 | 255dcebd | Iustin Pop | (/srv/..., False, "invalid api")],
|
2698 | 255dcebd | Iustin Pop | "node2": [(/srv/..., True, "")]}
|
2699 | e4376078 | Iustin Pop | }
|
2700 | 1f9430d6 | Iustin Pop |
|
2701 | 1f9430d6 | Iustin Pop | """
|
2702 | 1f9430d6 | Iustin Pop | all_os = {} |
2703 | a6ab004b | Iustin Pop | # we build here the list of nodes that didn't fail the RPC (at RPC
|
2704 | a6ab004b | Iustin Pop | # level), so that nodes with a non-responding node daemon don't
|
2705 | a6ab004b | Iustin Pop | # make all OSes invalid
|
2706 | a6ab004b | Iustin Pop | good_nodes = [node_name for node_name in rlist |
2707 | 4c4e4e1e | Iustin Pop | if not rlist[node_name].fail_msg] |
2708 | 83d92ad8 | Iustin Pop | for node_name, nr in rlist.items(): |
2709 | 4c4e4e1e | Iustin Pop | if nr.fail_msg or not nr.payload: |
2710 | 1f9430d6 | Iustin Pop | continue
|
2711 | ba00557a | Guido Trotter | for name, path, status, diagnose, variants in nr.payload: |
2712 | 255dcebd | Iustin Pop | if name not in all_os: |
2713 | 1f9430d6 | Iustin Pop | # build a list of nodes for this os containing empty lists
|
2714 | 1f9430d6 | Iustin Pop | # for each node in node_list
|
2715 | 255dcebd | Iustin Pop | all_os[name] = {} |
2716 | a6ab004b | Iustin Pop | for nname in good_nodes: |
2717 | 255dcebd | Iustin Pop | all_os[name][nname] = [] |
2718 | ba00557a | Guido Trotter | all_os[name][node_name].append((path, status, diagnose, variants)) |
2719 | 1f9430d6 | Iustin Pop | return all_os
|
2720 | a8083063 | Iustin Pop | |
2721 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2722 | a8083063 | Iustin Pop | """Compute the list of OSes.
|
2723 | a8083063 | Iustin Pop |
|
2724 | a8083063 | Iustin Pop | """
|
2725 | a6ab004b | Iustin Pop | valid_nodes = [node for node in self.cfg.GetOnlineNodeList()] |
2726 | 94a02bb5 | Iustin Pop | node_data = self.rpc.call_os_diagnose(valid_nodes)
|
2727 | 857121ad | Iustin Pop | pol = self._DiagnoseByOS(node_data)
|
2728 | 1f9430d6 | Iustin Pop | output = [] |
2729 | 1e288a26 | Guido Trotter | calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields) |
2730 | 1e288a26 | Guido Trotter | calc_variants = "variants" in self.op.output_fields |
2731 | 1e288a26 | Guido Trotter | |
2732 | 83d92ad8 | Iustin Pop | for os_name, os_data in pol.items(): |
2733 | 1f9430d6 | Iustin Pop | row = [] |
2734 | 1e288a26 | Guido Trotter | if calc_valid:
|
2735 | 1e288a26 | Guido Trotter | valid = True
|
2736 | 1e288a26 | Guido Trotter | variants = None
|
2737 | 1e288a26 | Guido Trotter | for osl in os_data.values(): |
2738 | 1e288a26 | Guido Trotter | valid = valid and osl and osl[0][1] |
2739 | 1e288a26 | Guido Trotter | if not valid: |
2740 | 1e288a26 | Guido Trotter | variants = None
|
2741 | 1e288a26 | Guido Trotter | break
|
2742 | 1e288a26 | Guido Trotter | if calc_variants:
|
2743 | 1e288a26 | Guido Trotter | node_variants = osl[0][3] |
2744 | 1e288a26 | Guido Trotter | if variants is None: |
2745 | 1e288a26 | Guido Trotter | variants = node_variants |
2746 | 1e288a26 | Guido Trotter | else:
|
2747 | 1e288a26 | Guido Trotter | variants = [v for v in variants if v in node_variants] |
2748 | 1e288a26 | Guido Trotter | |
2749 | 1f9430d6 | Iustin Pop | for field in self.op.output_fields: |
2750 | 1f9430d6 | Iustin Pop | if field == "name": |
2751 | 1f9430d6 | Iustin Pop | val = os_name |
2752 | 1f9430d6 | Iustin Pop | elif field == "valid": |
2753 | 1e288a26 | Guido Trotter | val = valid |
2754 | 1f9430d6 | Iustin Pop | elif field == "node_status": |
2755 | 255dcebd | Iustin Pop | # this is just a copy of the dict
|
2756 | 1f9430d6 | Iustin Pop | val = {} |
2757 | 255dcebd | Iustin Pop | for node_name, nos_list in os_data.items(): |
2758 | 255dcebd | Iustin Pop | val[node_name] = nos_list |
2759 | 1e288a26 | Guido Trotter | elif field == "variants": |
2760 | 1e288a26 | Guido Trotter | val = variants |
2761 | 1f9430d6 | Iustin Pop | else:
|
2762 | 1f9430d6 | Iustin Pop | raise errors.ParameterError(field)
|
2763 | 1f9430d6 | Iustin Pop | row.append(val) |
2764 | 1f9430d6 | Iustin Pop | output.append(row) |
2765 | 1f9430d6 | Iustin Pop | |
2766 | 1f9430d6 | Iustin Pop | return output
|
2767 | a8083063 | Iustin Pop | |
2768 | a8083063 | Iustin Pop | |
2769 | a8083063 | Iustin Pop | class LURemoveNode(LogicalUnit): |
2770 | a8083063 | Iustin Pop | """Logical unit for removing a node.
|
2771 | a8083063 | Iustin Pop |
|
2772 | a8083063 | Iustin Pop | """
|
2773 | a8083063 | Iustin Pop | HPATH = "node-remove"
|
2774 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
2775 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
2776 | a8083063 | Iustin Pop | |
2777 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2778 | a8083063 | Iustin Pop | """Build hooks env.
|
2779 | a8083063 | Iustin Pop |
|
2780 | a8083063 | Iustin Pop | This doesn't run on the target node in the pre phase as a failed
|
2781 | d08869ee | Guido Trotter | node would then be impossible to remove.
|
2782 | a8083063 | Iustin Pop |
|
2783 | a8083063 | Iustin Pop | """
|
2784 | 396e1b78 | Michael Hanselmann | env = { |
2785 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
2786 | 396e1b78 | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
2787 | 396e1b78 | Michael Hanselmann | } |
2788 | a8083063 | Iustin Pop | all_nodes = self.cfg.GetNodeList()
|
2789 | 9bb31ea8 | Iustin Pop | try:
|
2790 | cd46f3b4 | Luca Bigliardi | all_nodes.remove(self.op.node_name)
|
2791 | 9bb31ea8 | Iustin Pop | except ValueError: |
2792 | 9bb31ea8 | Iustin Pop | logging.warning("Node %s which is about to be removed not found"
|
2793 | 9bb31ea8 | Iustin Pop | " in the all nodes list", self.op.node_name) |
2794 | 396e1b78 | Michael Hanselmann | return env, all_nodes, all_nodes
|
2795 | a8083063 | Iustin Pop | |
2796 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2797 | a8083063 | Iustin Pop | """Check prerequisites.
|
2798 | a8083063 | Iustin Pop |
|
2799 | a8083063 | Iustin Pop | This checks:
|
2800 | a8083063 | Iustin Pop | - the node exists in the configuration
|
2801 | a8083063 | Iustin Pop | - it does not have primary or secondary instances
|
2802 | a8083063 | Iustin Pop | - it's not the master
|
2803 | a8083063 | Iustin Pop |
|
2804 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
2805 | a8083063 | Iustin Pop |
|
2806 | a8083063 | Iustin Pop | """
|
2807 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
2808 | cf26a87a | Iustin Pop | node = self.cfg.GetNodeInfo(self.op.node_name) |
2809 | cf26a87a | Iustin Pop | assert node is not None |
2810 | a8083063 | Iustin Pop | |
2811 | a8083063 | Iustin Pop | instance_list = self.cfg.GetInstanceList()
|
2812 | a8083063 | Iustin Pop | |
2813 | d6a02168 | Michael Hanselmann | masternode = self.cfg.GetMasterNode()
|
2814 | a8083063 | Iustin Pop | if node.name == masternode:
|
2815 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node is the master node," |
2816 | 5c983ee5 | Iustin Pop | " you need to failover first.",
|
2817 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2818 | a8083063 | Iustin Pop | |
2819 | a8083063 | Iustin Pop | for instance_name in instance_list: |
2820 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(instance_name)
|
2821 | 6b12959c | Iustin Pop | if node.name in instance.all_nodes: |
2822 | 6b12959c | Iustin Pop | raise errors.OpPrereqError("Instance %s is still running on the node," |
2823 | 5c983ee5 | Iustin Pop | " please remove first." % instance_name,
|
2824 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
2825 | a8083063 | Iustin Pop | self.op.node_name = node.name
|
2826 | a8083063 | Iustin Pop | self.node = node
|
2827 | a8083063 | Iustin Pop | |
2828 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2829 | a8083063 | Iustin Pop | """Removes the node from the cluster.
|
2830 | a8083063 | Iustin Pop |
|
2831 | a8083063 | Iustin Pop | """
|
2832 | a8083063 | Iustin Pop | node = self.node
|
2833 | 9a4f63d1 | Iustin Pop | logging.info("Stopping the node daemon and removing configs from node %s",
|
2834 | 9a4f63d1 | Iustin Pop | node.name) |
2835 | a8083063 | Iustin Pop | |
2836 | b989b9d9 | Ken Wehr | modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
|
2837 | b989b9d9 | Ken Wehr | |
2838 | 44485f49 | Guido Trotter | # Promote nodes to master candidate as needed
|
2839 | 44485f49 | Guido Trotter | _AdjustCandidatePool(self, exceptions=[node.name])
|
2840 | d8470559 | Michael Hanselmann | self.context.RemoveNode(node.name)
|
2841 | a8083063 | Iustin Pop | |
2842 | cd46f3b4 | Luca Bigliardi | # Run post hooks on the node before it's removed
|
2843 | cd46f3b4 | Luca Bigliardi | hm = self.proc.hmclass(self.rpc.call_hooks_runner, self) |
2844 | cd46f3b4 | Luca Bigliardi | try:
|
2845 | 1122eb25 | Iustin Pop | hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name]) |
2846 | 3cb5c1e3 | Luca Bigliardi | except:
|
2847 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0702
|
2848 | 3cb5c1e3 | Luca Bigliardi | self.LogWarning("Errors occurred running hooks on %s" % node.name) |
2849 | cd46f3b4 | Luca Bigliardi | |
2850 | b989b9d9 | Ken Wehr | result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
|
2851 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
2852 | 0623d351 | Iustin Pop | if msg:
|
2853 | 0623d351 | Iustin Pop | self.LogWarning("Errors encountered on the remote node while leaving" |
2854 | 0623d351 | Iustin Pop | " the cluster: %s", msg)
|
2855 | c8a0948f | Michael Hanselmann | |
2856 | a8083063 | Iustin Pop | |
2857 | a8083063 | Iustin Pop | class LUQueryNodes(NoHooksLU): |
2858 | a8083063 | Iustin Pop | """Logical unit for querying nodes.
|
2859 | a8083063 | Iustin Pop |
|
2860 | a8083063 | Iustin Pop | """
|
2861 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0142
|
2862 | bc8e4a1a | Iustin Pop | _OP_REQP = ["output_fields", "names", "use_locking"] |
2863 | 35705d8f | Guido Trotter | REQ_BGL = False
|
2864 | 19bed813 | Iustin Pop | |
2865 | 19bed813 | Iustin Pop | _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid", |
2866 | 19bed813 | Iustin Pop | "master_candidate", "offline", "drained"] |
2867 | 19bed813 | Iustin Pop | |
2868 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet( |
2869 | 31bf511f | Iustin Pop | "dtotal", "dfree", |
2870 | 31bf511f | Iustin Pop | "mtotal", "mnode", "mfree", |
2871 | 31bf511f | Iustin Pop | "bootid",
|
2872 | 0105bad3 | Iustin Pop | "ctotal", "cnodes", "csockets", |
2873 | 31bf511f | Iustin Pop | ) |
2874 | 31bf511f | Iustin Pop | |
2875 | 19bed813 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet(*[ |
2876 | 19bed813 | Iustin Pop | "pinst_cnt", "sinst_cnt", |
2877 | 31bf511f | Iustin Pop | "pinst_list", "sinst_list", |
2878 | 31bf511f | Iustin Pop | "pip", "sip", "tags", |
2879 | 0e67cdbe | Iustin Pop | "master",
|
2880 | 19bed813 | Iustin Pop | "role"] + _SIMPLE_FIELDS
|
2881 | 31bf511f | Iustin Pop | ) |
2882 | a8083063 | Iustin Pop | |
2883 | 35705d8f | Guido Trotter | def ExpandNames(self): |
2884 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2885 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
2886 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
2887 | a8083063 | Iustin Pop | |
2888 | 35705d8f | Guido Trotter | self.needed_locks = {}
|
2889 | 35705d8f | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
2890 | c8d8b4c8 | Iustin Pop | |
2891 | c8d8b4c8 | Iustin Pop | if self.op.names: |
2892 | c8d8b4c8 | Iustin Pop | self.wanted = _GetWantedNodes(self, self.op.names) |
2893 | 35705d8f | Guido Trotter | else:
|
2894 | c8d8b4c8 | Iustin Pop | self.wanted = locking.ALL_SET
|
2895 | c8d8b4c8 | Iustin Pop | |
2896 | bc8e4a1a | Iustin Pop | self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields) |
2897 | bc8e4a1a | Iustin Pop | self.do_locking = self.do_node_query and self.op.use_locking |
2898 | c8d8b4c8 | Iustin Pop | if self.do_locking: |
2899 | c8d8b4c8 | Iustin Pop | # if we don't request only static fields, we need to lock the nodes
|
2900 | c8d8b4c8 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = self.wanted |
2901 | c8d8b4c8 | Iustin Pop | |
2902 | 35705d8f | Guido Trotter | def CheckPrereq(self): |
2903 | 35705d8f | Guido Trotter | """Check prerequisites.
|
2904 | 35705d8f | Guido Trotter |
|
2905 | 35705d8f | Guido Trotter | """
|
2906 | c8d8b4c8 | Iustin Pop | # The validation of the node list is done in the _GetWantedNodes,
|
2907 | c8d8b4c8 | Iustin Pop | # if non empty, and if empty, there's no validation to do
|
2908 | c8d8b4c8 | Iustin Pop | pass
|
2909 | a8083063 | Iustin Pop | |
2910 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2911 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
2912 | a8083063 | Iustin Pop |
|
2913 | a8083063 | Iustin Pop | """
|
2914 | c8d8b4c8 | Iustin Pop | all_info = self.cfg.GetAllNodesInfo()
|
2915 | c8d8b4c8 | Iustin Pop | if self.do_locking: |
2916 | c8d8b4c8 | Iustin Pop | nodenames = self.acquired_locks[locking.LEVEL_NODE]
|
2917 | 3fa93523 | Guido Trotter | elif self.wanted != locking.ALL_SET: |
2918 | 3fa93523 | Guido Trotter | nodenames = self.wanted
|
2919 | 3fa93523 | Guido Trotter | missing = set(nodenames).difference(all_info.keys())
|
2920 | 3fa93523 | Guido Trotter | if missing:
|
2921 | 7b3a8fb5 | Iustin Pop | raise errors.OpExecError(
|
2922 | 3fa93523 | Guido Trotter | "Some nodes were removed before retrieving their data: %s" % missing)
|
2923 | c8d8b4c8 | Iustin Pop | else:
|
2924 | c8d8b4c8 | Iustin Pop | nodenames = all_info.keys() |
2925 | c1f1cbb2 | Iustin Pop | |
2926 | c1f1cbb2 | Iustin Pop | nodenames = utils.NiceSort(nodenames) |
2927 | c8d8b4c8 | Iustin Pop | nodelist = [all_info[name] for name in nodenames] |
2928 | a8083063 | Iustin Pop | |
2929 | a8083063 | Iustin Pop | # begin data gathering
|
2930 | a8083063 | Iustin Pop | |
2931 | bc8e4a1a | Iustin Pop | if self.do_node_query: |
2932 | a8083063 | Iustin Pop | live_data = {} |
2933 | 72737a7f | Iustin Pop | node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(), |
2934 | 72737a7f | Iustin Pop | self.cfg.GetHypervisorType())
|
2935 | a8083063 | Iustin Pop | for name in nodenames: |
2936 | 781de953 | Iustin Pop | nodeinfo = node_data[name] |
2937 | 4c4e4e1e | Iustin Pop | if not nodeinfo.fail_msg and nodeinfo.payload: |
2938 | 070e998b | Iustin Pop | nodeinfo = nodeinfo.payload |
2939 | d599d686 | Iustin Pop | fn = utils.TryConvert |
2940 | a8083063 | Iustin Pop | live_data[name] = { |
2941 | d599d686 | Iustin Pop | "mtotal": fn(int, nodeinfo.get('memory_total', None)), |
2942 | d599d686 | Iustin Pop | "mnode": fn(int, nodeinfo.get('memory_dom0', None)), |
2943 | d599d686 | Iustin Pop | "mfree": fn(int, nodeinfo.get('memory_free', None)), |
2944 | d599d686 | Iustin Pop | "dtotal": fn(int, nodeinfo.get('vg_size', None)), |
2945 | d599d686 | Iustin Pop | "dfree": fn(int, nodeinfo.get('vg_free', None)), |
2946 | d599d686 | Iustin Pop | "ctotal": fn(int, nodeinfo.get('cpu_total', None)), |
2947 | d599d686 | Iustin Pop | "bootid": nodeinfo.get('bootid', None), |
2948 | 0105bad3 | Iustin Pop | "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)), |
2949 | 0105bad3 | Iustin Pop | "csockets": fn(int, nodeinfo.get('cpu_sockets', None)), |
2950 | a8083063 | Iustin Pop | } |
2951 | a8083063 | Iustin Pop | else:
|
2952 | a8083063 | Iustin Pop | live_data[name] = {} |
2953 | a8083063 | Iustin Pop | else:
|
2954 | a8083063 | Iustin Pop | live_data = dict.fromkeys(nodenames, {})
|
2955 | a8083063 | Iustin Pop | |
2956 | ec223efb | Iustin Pop | node_to_primary = dict([(name, set()) for name in nodenames]) |
2957 | ec223efb | Iustin Pop | node_to_secondary = dict([(name, set()) for name in nodenames]) |
2958 | a8083063 | Iustin Pop | |
2959 | ec223efb | Iustin Pop | inst_fields = frozenset(("pinst_cnt", "pinst_list", |
2960 | ec223efb | Iustin Pop | "sinst_cnt", "sinst_list")) |
2961 | ec223efb | Iustin Pop | if inst_fields & frozenset(self.op.output_fields): |
2962 | 4dfd6266 | Iustin Pop | inst_data = self.cfg.GetAllInstancesInfo()
|
2963 | a8083063 | Iustin Pop | |
2964 | 1122eb25 | Iustin Pop | for inst in inst_data.values(): |
2965 | ec223efb | Iustin Pop | if inst.primary_node in node_to_primary: |
2966 | ec223efb | Iustin Pop | node_to_primary[inst.primary_node].add(inst.name) |
2967 | ec223efb | Iustin Pop | for secnode in inst.secondary_nodes: |
2968 | ec223efb | Iustin Pop | if secnode in node_to_secondary: |
2969 | ec223efb | Iustin Pop | node_to_secondary[secnode].add(inst.name) |
2970 | a8083063 | Iustin Pop | |
2971 | 0e67cdbe | Iustin Pop | master_node = self.cfg.GetMasterNode()
|
2972 | 0e67cdbe | Iustin Pop | |
2973 | a8083063 | Iustin Pop | # end data gathering
|
2974 | a8083063 | Iustin Pop | |
2975 | a8083063 | Iustin Pop | output = [] |
2976 | a8083063 | Iustin Pop | for node in nodelist: |
2977 | a8083063 | Iustin Pop | node_output = [] |
2978 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
2979 | 19bed813 | Iustin Pop | if field in self._SIMPLE_FIELDS: |
2980 | 19bed813 | Iustin Pop | val = getattr(node, field)
|
2981 | ec223efb | Iustin Pop | elif field == "pinst_list": |
2982 | ec223efb | Iustin Pop | val = list(node_to_primary[node.name])
|
2983 | ec223efb | Iustin Pop | elif field == "sinst_list": |
2984 | ec223efb | Iustin Pop | val = list(node_to_secondary[node.name])
|
2985 | ec223efb | Iustin Pop | elif field == "pinst_cnt": |
2986 | ec223efb | Iustin Pop | val = len(node_to_primary[node.name])
|
2987 | ec223efb | Iustin Pop | elif field == "sinst_cnt": |
2988 | ec223efb | Iustin Pop | val = len(node_to_secondary[node.name])
|
2989 | a8083063 | Iustin Pop | elif field == "pip": |
2990 | a8083063 | Iustin Pop | val = node.primary_ip |
2991 | a8083063 | Iustin Pop | elif field == "sip": |
2992 | a8083063 | Iustin Pop | val = node.secondary_ip |
2993 | 130a6a6f | Iustin Pop | elif field == "tags": |
2994 | 130a6a6f | Iustin Pop | val = list(node.GetTags())
|
2995 | 0e67cdbe | Iustin Pop | elif field == "master": |
2996 | 0e67cdbe | Iustin Pop | val = node.name == master_node |
2997 | 31bf511f | Iustin Pop | elif self._FIELDS_DYNAMIC.Matches(field): |
2998 | ec223efb | Iustin Pop | val = live_data[node.name].get(field, None)
|
2999 | c120ff34 | Iustin Pop | elif field == "role": |
3000 | c120ff34 | Iustin Pop | if node.name == master_node:
|
3001 | c120ff34 | Iustin Pop | val = "M"
|
3002 | c120ff34 | Iustin Pop | elif node.master_candidate:
|
3003 | c120ff34 | Iustin Pop | val = "C"
|
3004 | c120ff34 | Iustin Pop | elif node.drained:
|
3005 | c120ff34 | Iustin Pop | val = "D"
|
3006 | c120ff34 | Iustin Pop | elif node.offline:
|
3007 | c120ff34 | Iustin Pop | val = "O"
|
3008 | c120ff34 | Iustin Pop | else:
|
3009 | c120ff34 | Iustin Pop | val = "R"
|
3010 | a8083063 | Iustin Pop | else:
|
3011 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
3012 | a8083063 | Iustin Pop | node_output.append(val) |
3013 | a8083063 | Iustin Pop | output.append(node_output) |
3014 | a8083063 | Iustin Pop | |
3015 | a8083063 | Iustin Pop | return output
|
3016 | a8083063 | Iustin Pop | |
3017 | a8083063 | Iustin Pop | |
3018 | dcb93971 | Michael Hanselmann | class LUQueryNodeVolumes(NoHooksLU): |
3019 | dcb93971 | Michael Hanselmann | """Logical unit for getting volumes on node(s).
|
3020 | dcb93971 | Michael Hanselmann |
|
3021 | dcb93971 | Michael Hanselmann | """
|
3022 | dcb93971 | Michael Hanselmann | _OP_REQP = ["nodes", "output_fields"] |
3023 | 21a15682 | Guido Trotter | REQ_BGL = False
|
3024 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance") |
3025 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet("node")
|
3026 | 21a15682 | Guido Trotter | |
3027 | 21a15682 | Guido Trotter | def ExpandNames(self): |
3028 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
3029 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
3030 | 21a15682 | Guido Trotter | selected=self.op.output_fields)
|
3031 | 21a15682 | Guido Trotter | |
3032 | 21a15682 | Guido Trotter | self.needed_locks = {}
|
3033 | 21a15682 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
3034 | 21a15682 | Guido Trotter | if not self.op.nodes: |
3035 | e310b019 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
3036 | 21a15682 | Guido Trotter | else:
|
3037 | 21a15682 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = \
|
3038 | 21a15682 | Guido Trotter | _GetWantedNodes(self, self.op.nodes) |
3039 | dcb93971 | Michael Hanselmann | |
3040 | dcb93971 | Michael Hanselmann | def CheckPrereq(self): |
3041 | dcb93971 | Michael Hanselmann | """Check prerequisites.
|
3042 | dcb93971 | Michael Hanselmann |
|
3043 | dcb93971 | Michael Hanselmann | This checks that the fields required are valid output fields.
|
3044 | dcb93971 | Michael Hanselmann |
|
3045 | dcb93971 | Michael Hanselmann | """
|
3046 | 21a15682 | Guido Trotter | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
3047 | dcb93971 | Michael Hanselmann | |
3048 | dcb93971 | Michael Hanselmann | def Exec(self, feedback_fn): |
3049 | dcb93971 | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
3050 | dcb93971 | Michael Hanselmann |
|
3051 | dcb93971 | Michael Hanselmann | """
|
3052 | a7ba5e53 | Iustin Pop | nodenames = self.nodes
|
3053 | 72737a7f | Iustin Pop | volumes = self.rpc.call_node_volumes(nodenames)
|
3054 | dcb93971 | Michael Hanselmann | |
3055 | dcb93971 | Michael Hanselmann | ilist = [self.cfg.GetInstanceInfo(iname) for iname |
3056 | dcb93971 | Michael Hanselmann | in self.cfg.GetInstanceList()] |
3057 | dcb93971 | Michael Hanselmann | |
3058 | dcb93971 | Michael Hanselmann | lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist]) |
3059 | dcb93971 | Michael Hanselmann | |
3060 | dcb93971 | Michael Hanselmann | output = [] |
3061 | dcb93971 | Michael Hanselmann | for node in nodenames: |
3062 | 10bfe6cb | Iustin Pop | nresult = volumes[node] |
3063 | 10bfe6cb | Iustin Pop | if nresult.offline:
|
3064 | 10bfe6cb | Iustin Pop | continue
|
3065 | 4c4e4e1e | Iustin Pop | msg = nresult.fail_msg |
3066 | 10bfe6cb | Iustin Pop | if msg:
|
3067 | 10bfe6cb | Iustin Pop | self.LogWarning("Can't compute volume data on node %s: %s", node, msg) |
3068 | 37d19eb2 | Michael Hanselmann | continue
|
3069 | 37d19eb2 | Michael Hanselmann | |
3070 | 10bfe6cb | Iustin Pop | node_vols = nresult.payload[:] |
3071 | dcb93971 | Michael Hanselmann | node_vols.sort(key=lambda vol: vol['dev']) |
3072 | dcb93971 | Michael Hanselmann | |
3073 | dcb93971 | Michael Hanselmann | for vol in node_vols: |
3074 | dcb93971 | Michael Hanselmann | node_output = [] |
3075 | dcb93971 | Michael Hanselmann | for field in self.op.output_fields: |
3076 | dcb93971 | Michael Hanselmann | if field == "node": |
3077 | dcb93971 | Michael Hanselmann | val = node |
3078 | dcb93971 | Michael Hanselmann | elif field == "phys": |
3079 | dcb93971 | Michael Hanselmann | val = vol['dev']
|
3080 | dcb93971 | Michael Hanselmann | elif field == "vg": |
3081 | dcb93971 | Michael Hanselmann | val = vol['vg']
|
3082 | dcb93971 | Michael Hanselmann | elif field == "name": |
3083 | dcb93971 | Michael Hanselmann | val = vol['name']
|
3084 | dcb93971 | Michael Hanselmann | elif field == "size": |
3085 | dcb93971 | Michael Hanselmann | val = int(float(vol['size'])) |
3086 | dcb93971 | Michael Hanselmann | elif field == "instance": |
3087 | dcb93971 | Michael Hanselmann | for inst in ilist: |
3088 | dcb93971 | Michael Hanselmann | if node not in lv_by_node[inst]: |
3089 | dcb93971 | Michael Hanselmann | continue
|
3090 | dcb93971 | Michael Hanselmann | if vol['name'] in lv_by_node[inst][node]: |
3091 | dcb93971 | Michael Hanselmann | val = inst.name |
3092 | dcb93971 | Michael Hanselmann | break
|
3093 | dcb93971 | Michael Hanselmann | else:
|
3094 | dcb93971 | Michael Hanselmann | val = '-'
|
3095 | dcb93971 | Michael Hanselmann | else:
|
3096 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
3097 | dcb93971 | Michael Hanselmann | node_output.append(str(val))
|
3098 | dcb93971 | Michael Hanselmann | |
3099 | dcb93971 | Michael Hanselmann | output.append(node_output) |
3100 | dcb93971 | Michael Hanselmann | |
3101 | dcb93971 | Michael Hanselmann | return output
|
3102 | dcb93971 | Michael Hanselmann | |
3103 | dcb93971 | Michael Hanselmann | |
3104 | 9e5442ce | Michael Hanselmann | class LUQueryNodeStorage(NoHooksLU): |
3105 | 9e5442ce | Michael Hanselmann | """Logical unit for getting information on storage units on node(s).
|
3106 | 9e5442ce | Michael Hanselmann |
|
3107 | 9e5442ce | Michael Hanselmann | """
|
3108 | 9e5442ce | Michael Hanselmann | _OP_REQP = ["nodes", "storage_type", "output_fields"] |
3109 | 9e5442ce | Michael Hanselmann | REQ_BGL = False
|
3110 | 620a85fd | Iustin Pop | _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE) |
3111 | 9e5442ce | Michael Hanselmann | |
3112 | 0e3baaf3 | Iustin Pop | def CheckArguments(self): |
3113 | 0e3baaf3 | Iustin Pop | _CheckStorageType(self.op.storage_type)
|
3114 | 9e5442ce | Michael Hanselmann | |
3115 | 9e5442ce | Michael Hanselmann | _CheckOutputFields(static=self._FIELDS_STATIC,
|
3116 | 620a85fd | Iustin Pop | dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS), |
3117 | 9e5442ce | Michael Hanselmann | selected=self.op.output_fields)
|
3118 | 9e5442ce | Michael Hanselmann | |
3119 | 0e3baaf3 | Iustin Pop | def ExpandNames(self): |
3120 | 9e5442ce | Michael Hanselmann | self.needed_locks = {}
|
3121 | 9e5442ce | Michael Hanselmann | self.share_locks[locking.LEVEL_NODE] = 1 |
3122 | 9e5442ce | Michael Hanselmann | |
3123 | 9e5442ce | Michael Hanselmann | if self.op.nodes: |
3124 | 9e5442ce | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = \
|
3125 | 9e5442ce | Michael Hanselmann | _GetWantedNodes(self, self.op.nodes) |
3126 | 9e5442ce | Michael Hanselmann | else:
|
3127 | 9e5442ce | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
3128 | 9e5442ce | Michael Hanselmann | |
3129 | 9e5442ce | Michael Hanselmann | def CheckPrereq(self): |
3130 | 9e5442ce | Michael Hanselmann | """Check prerequisites.
|
3131 | 9e5442ce | Michael Hanselmann |
|
3132 | 9e5442ce | Michael Hanselmann | This checks that the fields required are valid output fields.
|
3133 | 9e5442ce | Michael Hanselmann |
|
3134 | 9e5442ce | Michael Hanselmann | """
|
3135 | 9e5442ce | Michael Hanselmann | self.op.name = getattr(self.op, "name", None) |
3136 | 9e5442ce | Michael Hanselmann | |
3137 | 9e5442ce | Michael Hanselmann | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
3138 | 9e5442ce | Michael Hanselmann | |
3139 | 9e5442ce | Michael Hanselmann | def Exec(self, feedback_fn): |
3140 | 9e5442ce | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
3141 | 9e5442ce | Michael Hanselmann |
|
3142 | 9e5442ce | Michael Hanselmann | """
|
3143 | 9e5442ce | Michael Hanselmann | # Always get name to sort by
|
3144 | 9e5442ce | Michael Hanselmann | if constants.SF_NAME in self.op.output_fields: |
3145 | 9e5442ce | Michael Hanselmann | fields = self.op.output_fields[:]
|
3146 | 9e5442ce | Michael Hanselmann | else:
|
3147 | 9e5442ce | Michael Hanselmann | fields = [constants.SF_NAME] + self.op.output_fields
|
3148 | 9e5442ce | Michael Hanselmann | |
3149 | 620a85fd | Iustin Pop | # Never ask for node or type as it's only known to the LU
|
3150 | 620a85fd | Iustin Pop | for extra in [constants.SF_NODE, constants.SF_TYPE]: |
3151 | 620a85fd | Iustin Pop | while extra in fields: |
3152 | 620a85fd | Iustin Pop | fields.remove(extra) |
3153 | 9e5442ce | Michael Hanselmann | |
3154 | 9e5442ce | Michael Hanselmann | field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)]) |
3155 | 9e5442ce | Michael Hanselmann | name_idx = field_idx[constants.SF_NAME] |
3156 | 9e5442ce | Michael Hanselmann | |
3157 | efb8da02 | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
3158 | 9e5442ce | Michael Hanselmann | data = self.rpc.call_storage_list(self.nodes, |
3159 | 9e5442ce | Michael Hanselmann | self.op.storage_type, st_args,
|
3160 | 9e5442ce | Michael Hanselmann | self.op.name, fields)
|
3161 | 9e5442ce | Michael Hanselmann | |
3162 | 9e5442ce | Michael Hanselmann | result = [] |
3163 | 9e5442ce | Michael Hanselmann | |
3164 | 9e5442ce | Michael Hanselmann | for node in utils.NiceSort(self.nodes): |
3165 | 9e5442ce | Michael Hanselmann | nresult = data[node] |
3166 | 9e5442ce | Michael Hanselmann | if nresult.offline:
|
3167 | 9e5442ce | Michael Hanselmann | continue
|
3168 | 9e5442ce | Michael Hanselmann | |
3169 | 9e5442ce | Michael Hanselmann | msg = nresult.fail_msg |
3170 | 9e5442ce | Michael Hanselmann | if msg:
|
3171 | 9e5442ce | Michael Hanselmann | self.LogWarning("Can't get storage data from node %s: %s", node, msg) |
3172 | 9e5442ce | Michael Hanselmann | continue
|
3173 | 9e5442ce | Michael Hanselmann | |
3174 | 9e5442ce | Michael Hanselmann | rows = dict([(row[name_idx], row) for row in nresult.payload]) |
3175 | 9e5442ce | Michael Hanselmann | |
3176 | 9e5442ce | Michael Hanselmann | for name in utils.NiceSort(rows.keys()): |
3177 | 9e5442ce | Michael Hanselmann | row = rows[name] |
3178 | 9e5442ce | Michael Hanselmann | |
3179 | 9e5442ce | Michael Hanselmann | out = [] |
3180 | 9e5442ce | Michael Hanselmann | |
3181 | 9e5442ce | Michael Hanselmann | for field in self.op.output_fields: |
3182 | 620a85fd | Iustin Pop | if field == constants.SF_NODE:
|
3183 | 9e5442ce | Michael Hanselmann | val = node |
3184 | 620a85fd | Iustin Pop | elif field == constants.SF_TYPE:
|
3185 | 620a85fd | Iustin Pop | val = self.op.storage_type
|
3186 | 9e5442ce | Michael Hanselmann | elif field in field_idx: |
3187 | 9e5442ce | Michael Hanselmann | val = row[field_idx[field]] |
3188 | 9e5442ce | Michael Hanselmann | else:
|
3189 | 9e5442ce | Michael Hanselmann | raise errors.ParameterError(field)
|
3190 | 9e5442ce | Michael Hanselmann | |
3191 | 9e5442ce | Michael Hanselmann | out.append(val) |
3192 | 9e5442ce | Michael Hanselmann | |
3193 | 9e5442ce | Michael Hanselmann | result.append(out) |
3194 | 9e5442ce | Michael Hanselmann | |
3195 | 9e5442ce | Michael Hanselmann | return result
|
3196 | 9e5442ce | Michael Hanselmann | |
3197 | 9e5442ce | Michael Hanselmann | |
3198 | efb8da02 | Michael Hanselmann | class LUModifyNodeStorage(NoHooksLU): |
3199 | efb8da02 | Michael Hanselmann | """Logical unit for modifying a storage volume on a node.
|
3200 | efb8da02 | Michael Hanselmann |
|
3201 | efb8da02 | Michael Hanselmann | """
|
3202 | efb8da02 | Michael Hanselmann | _OP_REQP = ["node_name", "storage_type", "name", "changes"] |
3203 | efb8da02 | Michael Hanselmann | REQ_BGL = False
|
3204 | efb8da02 | Michael Hanselmann | |
3205 | efb8da02 | Michael Hanselmann | def CheckArguments(self): |
3206 | cf26a87a | Iustin Pop | self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name) |
3207 | efb8da02 | Michael Hanselmann | |
3208 | 0e3baaf3 | Iustin Pop | _CheckStorageType(self.op.storage_type)
|
3209 | efb8da02 | Michael Hanselmann | |
3210 | efb8da02 | Michael Hanselmann | def ExpandNames(self): |
3211 | efb8da02 | Michael Hanselmann | self.needed_locks = {
|
3212 | efb8da02 | Michael Hanselmann | locking.LEVEL_NODE: self.op.node_name,
|
3213 | efb8da02 | Michael Hanselmann | } |
3214 | efb8da02 | Michael Hanselmann | |
3215 | efb8da02 | Michael Hanselmann | def CheckPrereq(self): |
3216 | efb8da02 | Michael Hanselmann | """Check prerequisites.
|
3217 | efb8da02 | Michael Hanselmann |
|
3218 | efb8da02 | Michael Hanselmann | """
|
3219 | efb8da02 | Michael Hanselmann | storage_type = self.op.storage_type
|
3220 | efb8da02 | Michael Hanselmann | |
3221 | efb8da02 | Michael Hanselmann | try:
|
3222 | efb8da02 | Michael Hanselmann | modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type] |
3223 | efb8da02 | Michael Hanselmann | except KeyError: |
3224 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("Storage units of type '%s' can not be" |
3225 | 5c983ee5 | Iustin Pop | " modified" % storage_type,
|
3226 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3227 | efb8da02 | Michael Hanselmann | |
3228 | efb8da02 | Michael Hanselmann | diff = set(self.op.changes.keys()) - modifiable |
3229 | efb8da02 | Michael Hanselmann | if diff:
|
3230 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("The following fields can not be modified for" |
3231 | efb8da02 | Michael Hanselmann | " storage units of type '%s': %r" %
|
3232 | 5c983ee5 | Iustin Pop | (storage_type, list(diff)),
|
3233 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3234 | efb8da02 | Michael Hanselmann | |
3235 | efb8da02 | Michael Hanselmann | def Exec(self, feedback_fn): |
3236 | efb8da02 | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
3237 | efb8da02 | Michael Hanselmann |
|
3238 | efb8da02 | Michael Hanselmann | """
|
3239 | efb8da02 | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
3240 | efb8da02 | Michael Hanselmann | result = self.rpc.call_storage_modify(self.op.node_name, |
3241 | efb8da02 | Michael Hanselmann | self.op.storage_type, st_args,
|
3242 | efb8da02 | Michael Hanselmann | self.op.name, self.op.changes) |
3243 | efb8da02 | Michael Hanselmann | result.Raise("Failed to modify storage unit '%s' on %s" %
|
3244 | efb8da02 | Michael Hanselmann | (self.op.name, self.op.node_name)) |
3245 | efb8da02 | Michael Hanselmann | |
3246 | efb8da02 | Michael Hanselmann | |
3247 | a8083063 | Iustin Pop | class LUAddNode(LogicalUnit): |
3248 | a8083063 | Iustin Pop | """Logical unit for adding node to the cluster.
|
3249 | a8083063 | Iustin Pop |
|
3250 | a8083063 | Iustin Pop | """
|
3251 | a8083063 | Iustin Pop | HPATH = "node-add"
|
3252 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
3253 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
3254 | a8083063 | Iustin Pop | |
3255 | 44caf5a8 | Iustin Pop | def CheckArguments(self): |
3256 | 44caf5a8 | Iustin Pop | # validate/normalize the node name
|
3257 | 44caf5a8 | Iustin Pop | self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name) |
3258 | 44caf5a8 | Iustin Pop | |
3259 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3260 | a8083063 | Iustin Pop | """Build hooks env.
|
3261 | a8083063 | Iustin Pop |
|
3262 | a8083063 | Iustin Pop | This will run on all nodes before, and on all nodes + the new node after.
|
3263 | a8083063 | Iustin Pop |
|
3264 | a8083063 | Iustin Pop | """
|
3265 | a8083063 | Iustin Pop | env = { |
3266 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
3267 | a8083063 | Iustin Pop | "NODE_NAME": self.op.node_name, |
3268 | a8083063 | Iustin Pop | "NODE_PIP": self.op.primary_ip, |
3269 | a8083063 | Iustin Pop | "NODE_SIP": self.op.secondary_ip, |
3270 | a8083063 | Iustin Pop | } |
3271 | a8083063 | Iustin Pop | nodes_0 = self.cfg.GetNodeList()
|
3272 | a8083063 | Iustin Pop | nodes_1 = nodes_0 + [self.op.node_name, ]
|
3273 | a8083063 | Iustin Pop | return env, nodes_0, nodes_1
|
3274 | a8083063 | Iustin Pop | |
3275 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3276 | a8083063 | Iustin Pop | """Check prerequisites.
|
3277 | a8083063 | Iustin Pop |
|
3278 | a8083063 | Iustin Pop | This checks:
|
3279 | a8083063 | Iustin Pop | - the new node is not already in the config
|
3280 | a8083063 | Iustin Pop | - it is resolvable
|
3281 | a8083063 | Iustin Pop | - its parameters (single/dual homed) matches the cluster
|
3282 | a8083063 | Iustin Pop |
|
3283 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
3284 | a8083063 | Iustin Pop |
|
3285 | a8083063 | Iustin Pop | """
|
3286 | a8083063 | Iustin Pop | node_name = self.op.node_name
|
3287 | a8083063 | Iustin Pop | cfg = self.cfg
|
3288 | a8083063 | Iustin Pop | |
3289 | 104f4ca1 | Iustin Pop | dns_data = utils.GetHostInfo(node_name) |
3290 | a8083063 | Iustin Pop | |
3291 | bcf043c9 | Iustin Pop | node = dns_data.name |
3292 | bcf043c9 | Iustin Pop | primary_ip = self.op.primary_ip = dns_data.ip
|
3293 | a8083063 | Iustin Pop | secondary_ip = getattr(self.op, "secondary_ip", None) |
3294 | a8083063 | Iustin Pop | if secondary_ip is None: |
3295 | a8083063 | Iustin Pop | secondary_ip = primary_ip |
3296 | a8083063 | Iustin Pop | if not utils.IsValidIP(secondary_ip): |
3297 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid secondary IP given", |
3298 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3299 | a8083063 | Iustin Pop | self.op.secondary_ip = secondary_ip
|
3300 | e7c6e02b | Michael Hanselmann | |
3301 | a8083063 | Iustin Pop | node_list = cfg.GetNodeList() |
3302 | e7c6e02b | Michael Hanselmann | if not self.op.readd and node in node_list: |
3303 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Node %s is already in the configuration" % |
3304 | 5c983ee5 | Iustin Pop | node, errors.ECODE_EXISTS) |
3305 | e7c6e02b | Michael Hanselmann | elif self.op.readd and node not in node_list: |
3306 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Node %s is not in the configuration" % node, |
3307 | 5c983ee5 | Iustin Pop | errors.ECODE_NOENT) |
3308 | a8083063 | Iustin Pop | |
3309 | a8083063 | Iustin Pop | for existing_node_name in node_list: |
3310 | a8083063 | Iustin Pop | existing_node = cfg.GetNodeInfo(existing_node_name) |
3311 | e7c6e02b | Michael Hanselmann | |
3312 | e7c6e02b | Michael Hanselmann | if self.op.readd and node == existing_node_name: |
3313 | e7c6e02b | Michael Hanselmann | if (existing_node.primary_ip != primary_ip or |
3314 | e7c6e02b | Michael Hanselmann | existing_node.secondary_ip != secondary_ip): |
3315 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Readded node doesn't have the same IP" |
3316 | 5c983ee5 | Iustin Pop | " address configuration as before",
|
3317 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3318 | e7c6e02b | Michael Hanselmann | continue
|
3319 | e7c6e02b | Michael Hanselmann | |
3320 | a8083063 | Iustin Pop | if (existing_node.primary_ip == primary_ip or |
3321 | a8083063 | Iustin Pop | existing_node.secondary_ip == primary_ip or
|
3322 | a8083063 | Iustin Pop | existing_node.primary_ip == secondary_ip or
|
3323 | a8083063 | Iustin Pop | existing_node.secondary_ip == secondary_ip): |
3324 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("New node ip address(es) conflict with" |
3325 | 5c983ee5 | Iustin Pop | " existing node %s" % existing_node.name,
|
3326 | 5c983ee5 | Iustin Pop | errors.ECODE_NOTUNIQUE) |
3327 | a8083063 | Iustin Pop | |
3328 | a8083063 | Iustin Pop | # check that the type of the node (single versus dual homed) is the
|
3329 | a8083063 | Iustin Pop | # same as for the master
|
3330 | d6a02168 | Michael Hanselmann | myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
|
3331 | a8083063 | Iustin Pop | master_singlehomed = myself.secondary_ip == myself.primary_ip |
3332 | a8083063 | Iustin Pop | newbie_singlehomed = secondary_ip == primary_ip |
3333 | a8083063 | Iustin Pop | if master_singlehomed != newbie_singlehomed:
|
3334 | a8083063 | Iustin Pop | if master_singlehomed:
|
3335 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has no private ip but the" |
3336 | 5c983ee5 | Iustin Pop | " new node has one",
|
3337 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3338 | a8083063 | Iustin Pop | else:
|
3339 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has a private ip but the" |
3340 | 5c983ee5 | Iustin Pop | " new node doesn't have one",
|
3341 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3342 | a8083063 | Iustin Pop | |
3343 | 5bbd3f7f | Michael Hanselmann | # checks reachability
|
3344 | b15d625f | Iustin Pop | if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT): |
3345 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Node not reachable by ping", |
3346 | 5c983ee5 | Iustin Pop | errors.ECODE_ENVIRON) |
3347 | a8083063 | Iustin Pop | |
3348 | a8083063 | Iustin Pop | if not newbie_singlehomed: |
3349 | a8083063 | Iustin Pop | # check reachability from my secondary ip to newbie's secondary ip
|
3350 | b15d625f | Iustin Pop | if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT, |
3351 | b15d625f | Iustin Pop | source=myself.secondary_ip): |
3352 | f4bc1f2c | Michael Hanselmann | raise errors.OpPrereqError("Node secondary ip not reachable by TCP" |
3353 | 5c983ee5 | Iustin Pop | " based ping to noded port",
|
3354 | 5c983ee5 | Iustin Pop | errors.ECODE_ENVIRON) |
3355 | a8083063 | Iustin Pop | |
3356 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
3357 | a8ae3eb5 | Iustin Pop | exceptions = [node] |
3358 | a8ae3eb5 | Iustin Pop | else:
|
3359 | a8ae3eb5 | Iustin Pop | exceptions = [] |
3360 | 6d7e1f20 | Guido Trotter | |
3361 | 6d7e1f20 | Guido Trotter | self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions) |
3362 | 0fff97e9 | Guido Trotter | |
3363 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
3364 | a8ae3eb5 | Iustin Pop | self.new_node = self.cfg.GetNodeInfo(node) |
3365 | a8ae3eb5 | Iustin Pop | assert self.new_node is not None, "Can't retrieve locked node %s" % node |
3366 | a8ae3eb5 | Iustin Pop | else:
|
3367 | a8ae3eb5 | Iustin Pop | self.new_node = objects.Node(name=node,
|
3368 | a8ae3eb5 | Iustin Pop | primary_ip=primary_ip, |
3369 | a8ae3eb5 | Iustin Pop | secondary_ip=secondary_ip, |
3370 | a8ae3eb5 | Iustin Pop | master_candidate=self.master_candidate,
|
3371 | a8ae3eb5 | Iustin Pop | offline=False, drained=False) |
3372 | a8083063 | Iustin Pop | |
3373 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3374 | a8083063 | Iustin Pop | """Adds the new node to the cluster.
|
3375 | a8083063 | Iustin Pop |
|
3376 | a8083063 | Iustin Pop | """
|
3377 | a8083063 | Iustin Pop | new_node = self.new_node
|
3378 | a8083063 | Iustin Pop | node = new_node.name |
3379 | a8083063 | Iustin Pop | |
3380 | a8ae3eb5 | Iustin Pop | # for re-adds, reset the offline/drained/master-candidate flags;
|
3381 | a8ae3eb5 | Iustin Pop | # we need to reset here, otherwise offline would prevent RPC calls
|
3382 | a8ae3eb5 | Iustin Pop | # later in the procedure; this also means that if the re-add
|
3383 | a8ae3eb5 | Iustin Pop | # fails, we are left with a non-offlined, broken node
|
3384 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
3385 | 7260cfbe | Iustin Pop | new_node.drained = new_node.offline = False # pylint: disable-msg=W0201 |
3386 | a8ae3eb5 | Iustin Pop | self.LogInfo("Readding a node, the offline/drained flags were reset") |
3387 | a8ae3eb5 | Iustin Pop | # if we demote the node, we do cleanup later in the procedure
|
3388 | a8ae3eb5 | Iustin Pop | new_node.master_candidate = self.master_candidate
|
3389 | a8ae3eb5 | Iustin Pop | |
3390 | a8ae3eb5 | Iustin Pop | # notify the user about any possible mc promotion
|
3391 | a8ae3eb5 | Iustin Pop | if new_node.master_candidate:
|
3392 | a8ae3eb5 | Iustin Pop | self.LogInfo("Node will be a master candidate") |
3393 | a8ae3eb5 | Iustin Pop | |
3394 | a8083063 | Iustin Pop | # check connectivity
|
3395 | 72737a7f | Iustin Pop | result = self.rpc.call_version([node])[node]
|
3396 | 4c4e4e1e | Iustin Pop | result.Raise("Can't get version information from node %s" % node)
|
3397 | 90b54c26 | Iustin Pop | if constants.PROTOCOL_VERSION == result.payload:
|
3398 | 90b54c26 | Iustin Pop | logging.info("Communication to node %s fine, sw version %s match",
|
3399 | 90b54c26 | Iustin Pop | node, result.payload) |
3400 | a8083063 | Iustin Pop | else:
|
3401 | 90b54c26 | Iustin Pop | raise errors.OpExecError("Version mismatch master version %s," |
3402 | 90b54c26 | Iustin Pop | " node version %s" %
|
3403 | 90b54c26 | Iustin Pop | (constants.PROTOCOL_VERSION, result.payload)) |
3404 | a8083063 | Iustin Pop | |
3405 | a8083063 | Iustin Pop | # setup ssh on node
|
3406 | b989b9d9 | Ken Wehr | if self.cfg.GetClusterInfo().modify_ssh_setup: |
3407 | b989b9d9 | Ken Wehr | logging.info("Copy ssh key to node %s", node)
|
3408 | b989b9d9 | Ken Wehr | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
3409 | b989b9d9 | Ken Wehr | keyarray = [] |
3410 | b989b9d9 | Ken Wehr | keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB, |
3411 | b989b9d9 | Ken Wehr | constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB, |
3412 | b989b9d9 | Ken Wehr | priv_key, pub_key] |
3413 | b989b9d9 | Ken Wehr | |
3414 | b989b9d9 | Ken Wehr | for i in keyfiles: |
3415 | b989b9d9 | Ken Wehr | keyarray.append(utils.ReadFile(i)) |
3416 | b989b9d9 | Ken Wehr | |
3417 | b989b9d9 | Ken Wehr | result = self.rpc.call_node_add(node, keyarray[0], keyarray[1], |
3418 | b989b9d9 | Ken Wehr | keyarray[2], keyarray[3], keyarray[4], |
3419 | b989b9d9 | Ken Wehr | keyarray[5])
|
3420 | b989b9d9 | Ken Wehr | result.Raise("Cannot transfer ssh keys to the new node")
|
3421 | a8083063 | Iustin Pop | |
3422 | a8083063 | Iustin Pop | # Add node to our /etc/hosts, and add key to known_hosts
|
3423 | b86a6bcd | Guido Trotter | if self.cfg.GetClusterInfo().modify_etc_hosts: |
3424 | b86a6bcd | Guido Trotter | utils.AddHostToEtcHosts(new_node.name) |
3425 | c8a0948f | Michael Hanselmann | |
3426 | a8083063 | Iustin Pop | if new_node.secondary_ip != new_node.primary_ip:
|
3427 | 781de953 | Iustin Pop | result = self.rpc.call_node_has_ip_address(new_node.name,
|
3428 | 781de953 | Iustin Pop | new_node.secondary_ip) |
3429 | 4c4e4e1e | Iustin Pop | result.Raise("Failure checking secondary ip on node %s" % new_node.name,
|
3430 | 045dd6d9 | Iustin Pop | prereq=True, ecode=errors.ECODE_ENVIRON)
|
3431 | c2fc8250 | Iustin Pop | if not result.payload: |
3432 | f4bc1f2c | Michael Hanselmann | raise errors.OpExecError("Node claims it doesn't have the secondary ip" |
3433 | f4bc1f2c | Michael Hanselmann | " you gave (%s). Please fix and re-run this"
|
3434 | f4bc1f2c | Michael Hanselmann | " command." % new_node.secondary_ip)
|
3435 | a8083063 | Iustin Pop | |
3436 | d6a02168 | Michael Hanselmann | node_verify_list = [self.cfg.GetMasterNode()]
|
3437 | 5c0527ed | Guido Trotter | node_verify_param = { |
3438 | f60759f7 | Iustin Pop | constants.NV_NODELIST: [node], |
3439 | 5c0527ed | Guido Trotter | # TODO: do a node-net-test as well?
|
3440 | 5c0527ed | Guido Trotter | } |
3441 | 5c0527ed | Guido Trotter | |
3442 | 72737a7f | Iustin Pop | result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
|
3443 | 72737a7f | Iustin Pop | self.cfg.GetClusterName())
|
3444 | 5c0527ed | Guido Trotter | for verifier in node_verify_list: |
3445 | 4c4e4e1e | Iustin Pop | result[verifier].Raise("Cannot communicate with node %s" % verifier)
|
3446 | f60759f7 | Iustin Pop | nl_payload = result[verifier].payload[constants.NV_NODELIST] |
3447 | 6f68a739 | Iustin Pop | if nl_payload:
|
3448 | 6f68a739 | Iustin Pop | for failed in nl_payload: |
3449 | 31821208 | Iustin Pop | feedback_fn("ssh/hostname verification failed"
|
3450 | 31821208 | Iustin Pop | " (checking from %s): %s" %
|
3451 | 6f68a739 | Iustin Pop | (verifier, nl_payload[failed])) |
3452 | 5c0527ed | Guido Trotter | raise errors.OpExecError("ssh/hostname verification failed.") |
3453 | ff98055b | Iustin Pop | |
3454 | d8470559 | Michael Hanselmann | if self.op.readd: |
3455 | 28eddce5 | Guido Trotter | _RedistributeAncillaryFiles(self)
|
3456 | d8470559 | Michael Hanselmann | self.context.ReaddNode(new_node)
|
3457 | a8ae3eb5 | Iustin Pop | # make sure we redistribute the config
|
3458 | a4eae71f | Michael Hanselmann | self.cfg.Update(new_node, feedback_fn)
|
3459 | a8ae3eb5 | Iustin Pop | # and make sure the new node will not have old files around
|
3460 | a8ae3eb5 | Iustin Pop | if not new_node.master_candidate: |
3461 | a8ae3eb5 | Iustin Pop | result = self.rpc.call_node_demote_from_mc(new_node.name)
|
3462 | 3cebe102 | Michael Hanselmann | msg = result.fail_msg |
3463 | a8ae3eb5 | Iustin Pop | if msg:
|
3464 | a8ae3eb5 | Iustin Pop | self.LogWarning("Node failed to demote itself from master" |
3465 | a8ae3eb5 | Iustin Pop | " candidate status: %s" % msg)
|
3466 | d8470559 | Michael Hanselmann | else:
|
3467 | 035566e3 | Iustin Pop | _RedistributeAncillaryFiles(self, additional_nodes=[node])
|
3468 | 0debfb35 | Guido Trotter | self.context.AddNode(new_node, self.proc.GetECId()) |
3469 | a8083063 | Iustin Pop | |
3470 | a8083063 | Iustin Pop | |
3471 | b31c8676 | Iustin Pop | class LUSetNodeParams(LogicalUnit): |
3472 | b31c8676 | Iustin Pop | """Modifies the parameters of a node.
|
3473 | b31c8676 | Iustin Pop |
|
3474 | b31c8676 | Iustin Pop | """
|
3475 | b31c8676 | Iustin Pop | HPATH = "node-modify"
|
3476 | b31c8676 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
3477 | b31c8676 | Iustin Pop | _OP_REQP = ["node_name"]
|
3478 | b31c8676 | Iustin Pop | REQ_BGL = False
|
3479 | b31c8676 | Iustin Pop | |
3480 | b31c8676 | Iustin Pop | def CheckArguments(self): |
3481 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
3482 | 3a5ba66a | Iustin Pop | _CheckBooleanOpField(self.op, 'master_candidate') |
3483 | 3a5ba66a | Iustin Pop | _CheckBooleanOpField(self.op, 'offline') |
3484 | c9d443ea | Iustin Pop | _CheckBooleanOpField(self.op, 'drained') |
3485 | 601908d0 | Iustin Pop | _CheckBooleanOpField(self.op, 'auto_promote') |
3486 | c9d443ea | Iustin Pop | all_mods = [self.op.offline, self.op.master_candidate, self.op.drained] |
3487 | c9d443ea | Iustin Pop | if all_mods.count(None) == 3: |
3488 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Please pass at least one modification", |
3489 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3490 | c9d443ea | Iustin Pop | if all_mods.count(True) > 1: |
3491 | c9d443ea | Iustin Pop | raise errors.OpPrereqError("Can't set the node into more than one" |
3492 | 5c983ee5 | Iustin Pop | " state at the same time",
|
3493 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3494 | b31c8676 | Iustin Pop | |
3495 | 601908d0 | Iustin Pop | # Boolean value that tells us whether we're offlining or draining the node
|
3496 | 601908d0 | Iustin Pop | self.offline_or_drain = (self.op.offline == True or |
3497 | 601908d0 | Iustin Pop | self.op.drained == True) |
3498 | 601908d0 | Iustin Pop | self.deoffline_or_drain = (self.op.offline == False or |
3499 | 601908d0 | Iustin Pop | self.op.drained == False) |
3500 | 601908d0 | Iustin Pop | self.might_demote = (self.op.master_candidate == False or |
3501 | 601908d0 | Iustin Pop | self.offline_or_drain)
|
3502 | 601908d0 | Iustin Pop | |
3503 | 601908d0 | Iustin Pop | self.lock_all = self.op.auto_promote and self.might_demote |
3504 | 601908d0 | Iustin Pop | |
3505 | 601908d0 | Iustin Pop | |
3506 | b31c8676 | Iustin Pop | def ExpandNames(self): |
3507 | 601908d0 | Iustin Pop | if self.lock_all: |
3508 | 601908d0 | Iustin Pop | self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
|
3509 | 601908d0 | Iustin Pop | else:
|
3510 | 601908d0 | Iustin Pop | self.needed_locks = {locking.LEVEL_NODE: self.op.node_name} |
3511 | b31c8676 | Iustin Pop | |
3512 | b31c8676 | Iustin Pop | def BuildHooksEnv(self): |
3513 | b31c8676 | Iustin Pop | """Build hooks env.
|
3514 | b31c8676 | Iustin Pop |
|
3515 | b31c8676 | Iustin Pop | This runs on the master node.
|
3516 | b31c8676 | Iustin Pop |
|
3517 | b31c8676 | Iustin Pop | """
|
3518 | b31c8676 | Iustin Pop | env = { |
3519 | b31c8676 | Iustin Pop | "OP_TARGET": self.op.node_name, |
3520 | b31c8676 | Iustin Pop | "MASTER_CANDIDATE": str(self.op.master_candidate), |
3521 | 3a5ba66a | Iustin Pop | "OFFLINE": str(self.op.offline), |
3522 | c9d443ea | Iustin Pop | "DRAINED": str(self.op.drained), |
3523 | b31c8676 | Iustin Pop | } |
3524 | b31c8676 | Iustin Pop | nl = [self.cfg.GetMasterNode(),
|
3525 | b31c8676 | Iustin Pop | self.op.node_name]
|
3526 | b31c8676 | Iustin Pop | return env, nl, nl
|
3527 | b31c8676 | Iustin Pop | |
3528 | b31c8676 | Iustin Pop | def CheckPrereq(self): |
3529 | b31c8676 | Iustin Pop | """Check prerequisites.
|
3530 | b31c8676 | Iustin Pop |
|
3531 | b31c8676 | Iustin Pop | This only checks the instance list against the existing names.
|
3532 | b31c8676 | Iustin Pop |
|
3533 | b31c8676 | Iustin Pop | """
|
3534 | 3a5ba66a | Iustin Pop | node = self.node = self.cfg.GetNodeInfo(self.op.node_name) |
3535 | b31c8676 | Iustin Pop | |
3536 | 97c61d46 | Iustin Pop | if (self.op.master_candidate is not None or |
3537 | 97c61d46 | Iustin Pop | self.op.drained is not None or |
3538 | 97c61d46 | Iustin Pop | self.op.offline is not None): |
3539 | 97c61d46 | Iustin Pop | # we can't change the master's node flags
|
3540 | 97c61d46 | Iustin Pop | if self.op.node_name == self.cfg.GetMasterNode(): |
3541 | 97c61d46 | Iustin Pop | raise errors.OpPrereqError("The master role can be changed" |
3542 | 5c983ee5 | Iustin Pop | " only via masterfailover",
|
3543 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3544 | 97c61d46 | Iustin Pop | |
3545 | 601908d0 | Iustin Pop | |
3546 | 601908d0 | Iustin Pop | if node.master_candidate and self.might_demote and not self.lock_all: |
3547 | 601908d0 | Iustin Pop | assert not self.op.auto_promote, "auto-promote set but lock_all not" |
3548 | 601908d0 | Iustin Pop | # check if after removing the current node, we're missing master
|
3549 | 601908d0 | Iustin Pop | # candidates
|
3550 | 601908d0 | Iustin Pop | (mc_remaining, mc_should, _) = \ |
3551 | 601908d0 | Iustin Pop | self.cfg.GetMasterCandidateStats(exceptions=[node.name])
|
3552 | 8fe9239e | Iustin Pop | if mc_remaining < mc_should:
|
3553 | 601908d0 | Iustin Pop | raise errors.OpPrereqError("Not enough master candidates, please" |
3554 | 601908d0 | Iustin Pop | " pass auto_promote to allow promotion",
|
3555 | 601908d0 | Iustin Pop | errors.ECODE_INVAL) |
3556 | 3e83dd48 | Iustin Pop | |
3557 | c9d443ea | Iustin Pop | if (self.op.master_candidate == True and |
3558 | c9d443ea | Iustin Pop | ((node.offline and not self.op.offline == False) or |
3559 | c9d443ea | Iustin Pop | (node.drained and not self.op.drained == False))): |
3560 | c9d443ea | Iustin Pop | raise errors.OpPrereqError("Node '%s' is offline or drained, can't set" |
3561 | 5c983ee5 | Iustin Pop | " to master_candidate" % node.name,
|
3562 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3563 | 3a5ba66a | Iustin Pop | |
3564 | 3d9eb52b | Guido Trotter | # If we're being deofflined/drained, we'll MC ourself if needed
|
3565 | 601908d0 | Iustin Pop | if (self.deoffline_or_drain and not self.offline_or_drain and not |
3566 | cea0534a | Guido Trotter | self.op.master_candidate == True and not node.master_candidate): |
3567 | 3d9eb52b | Guido Trotter | self.op.master_candidate = _DecideSelfPromotion(self) |
3568 | 3d9eb52b | Guido Trotter | if self.op.master_candidate: |
3569 | 3d9eb52b | Guido Trotter | self.LogInfo("Autopromoting node to master candidate") |
3570 | 3d9eb52b | Guido Trotter | |
3571 | b31c8676 | Iustin Pop | return
|
3572 | b31c8676 | Iustin Pop | |
3573 | b31c8676 | Iustin Pop | def Exec(self, feedback_fn): |
3574 | b31c8676 | Iustin Pop | """Modifies a node.
|
3575 | b31c8676 | Iustin Pop |
|
3576 | b31c8676 | Iustin Pop | """
|
3577 | 3a5ba66a | Iustin Pop | node = self.node
|
3578 | b31c8676 | Iustin Pop | |
3579 | b31c8676 | Iustin Pop | result = [] |
3580 | c9d443ea | Iustin Pop | changed_mc = False
|
3581 | b31c8676 | Iustin Pop | |
3582 | 3a5ba66a | Iustin Pop | if self.op.offline is not None: |
3583 | 3a5ba66a | Iustin Pop | node.offline = self.op.offline
|
3584 | 3a5ba66a | Iustin Pop | result.append(("offline", str(self.op.offline))) |
3585 | c9d443ea | Iustin Pop | if self.op.offline == True: |
3586 | c9d443ea | Iustin Pop | if node.master_candidate:
|
3587 | c9d443ea | Iustin Pop | node.master_candidate = False
|
3588 | c9d443ea | Iustin Pop | changed_mc = True
|
3589 | c9d443ea | Iustin Pop | result.append(("master_candidate", "auto-demotion due to offline")) |
3590 | c9d443ea | Iustin Pop | if node.drained:
|
3591 | c9d443ea | Iustin Pop | node.drained = False
|
3592 | c9d443ea | Iustin Pop | result.append(("drained", "clear drained status due to offline")) |
3593 | 3a5ba66a | Iustin Pop | |
3594 | b31c8676 | Iustin Pop | if self.op.master_candidate is not None: |
3595 | b31c8676 | Iustin Pop | node.master_candidate = self.op.master_candidate
|
3596 | c9d443ea | Iustin Pop | changed_mc = True
|
3597 | b31c8676 | Iustin Pop | result.append(("master_candidate", str(self.op.master_candidate))) |
3598 | 56aa9fd5 | Iustin Pop | if self.op.master_candidate == False: |
3599 | 56aa9fd5 | Iustin Pop | rrc = self.rpc.call_node_demote_from_mc(node.name)
|
3600 | 4c4e4e1e | Iustin Pop | msg = rrc.fail_msg |
3601 | 0959c824 | Iustin Pop | if msg:
|
3602 | 0959c824 | Iustin Pop | self.LogWarning("Node failed to demote itself: %s" % msg) |
3603 | b31c8676 | Iustin Pop | |
3604 | c9d443ea | Iustin Pop | if self.op.drained is not None: |
3605 | c9d443ea | Iustin Pop | node.drained = self.op.drained
|
3606 | 82e12743 | Iustin Pop | result.append(("drained", str(self.op.drained))) |
3607 | c9d443ea | Iustin Pop | if self.op.drained == True: |
3608 | c9d443ea | Iustin Pop | if node.master_candidate:
|
3609 | c9d443ea | Iustin Pop | node.master_candidate = False
|
3610 | c9d443ea | Iustin Pop | changed_mc = True
|
3611 | c9d443ea | Iustin Pop | result.append(("master_candidate", "auto-demotion due to drain")) |
3612 | dec0d9da | Iustin Pop | rrc = self.rpc.call_node_demote_from_mc(node.name)
|
3613 | 3cebe102 | Michael Hanselmann | msg = rrc.fail_msg |
3614 | dec0d9da | Iustin Pop | if msg:
|
3615 | dec0d9da | Iustin Pop | self.LogWarning("Node failed to demote itself: %s" % msg) |
3616 | c9d443ea | Iustin Pop | if node.offline:
|
3617 | c9d443ea | Iustin Pop | node.offline = False
|
3618 | c9d443ea | Iustin Pop | result.append(("offline", "clear offline status due to drain")) |
3619 | c9d443ea | Iustin Pop | |
3620 | 601908d0 | Iustin Pop | # we locked all nodes, we adjust the CP before updating this node
|
3621 | 601908d0 | Iustin Pop | if self.lock_all: |
3622 | 601908d0 | Iustin Pop | _AdjustCandidatePool(self, [node.name])
|
3623 | 601908d0 | Iustin Pop | |
3624 | b31c8676 | Iustin Pop | # this will trigger configuration file update, if needed
|
3625 | a4eae71f | Michael Hanselmann | self.cfg.Update(node, feedback_fn)
|
3626 | 601908d0 | Iustin Pop | |
3627 | b31c8676 | Iustin Pop | # this will trigger job queue propagation or cleanup
|
3628 | c9d443ea | Iustin Pop | if changed_mc:
|
3629 | 3a26773f | Iustin Pop | self.context.ReaddNode(node)
|
3630 | b31c8676 | Iustin Pop | |
3631 | b31c8676 | Iustin Pop | return result
|
3632 | b31c8676 | Iustin Pop | |
3633 | b31c8676 | Iustin Pop | |
3634 | f5118ade | Iustin Pop | class LUPowercycleNode(NoHooksLU): |
3635 | f5118ade | Iustin Pop | """Powercycles a node.
|
3636 | f5118ade | Iustin Pop |
|
3637 | f5118ade | Iustin Pop | """
|
3638 | f5118ade | Iustin Pop | _OP_REQP = ["node_name", "force"] |
3639 | f5118ade | Iustin Pop | REQ_BGL = False
|
3640 | f5118ade | Iustin Pop | |
3641 | f5118ade | Iustin Pop | def CheckArguments(self): |
3642 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
3643 | cf26a87a | Iustin Pop | if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force: |
3644 | f5118ade | Iustin Pop | raise errors.OpPrereqError("The node is the master and the force" |
3645 | 5c983ee5 | Iustin Pop | " parameter was not set",
|
3646 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
3647 | f5118ade | Iustin Pop | |
3648 | f5118ade | Iustin Pop | def ExpandNames(self): |
3649 | f5118ade | Iustin Pop | """Locking for PowercycleNode.
|
3650 | f5118ade | Iustin Pop |
|
3651 | efb8da02 | Michael Hanselmann | This is a last-resort option and shouldn't block on other
|
3652 | f5118ade | Iustin Pop | jobs. Therefore, we grab no locks.
|
3653 | f5118ade | Iustin Pop |
|
3654 | f5118ade | Iustin Pop | """
|
3655 | f5118ade | Iustin Pop | self.needed_locks = {}
|
3656 | f5118ade | Iustin Pop | |
3657 | f5118ade | Iustin Pop | def CheckPrereq(self): |
3658 | f5118ade | Iustin Pop | """Check prerequisites.
|
3659 | f5118ade | Iustin Pop |
|
3660 | f5118ade | Iustin Pop | This LU has no prereqs.
|
3661 | f5118ade | Iustin Pop |
|
3662 | f5118ade | Iustin Pop | """
|
3663 | f5118ade | Iustin Pop | pass
|
3664 | f5118ade | Iustin Pop | |
3665 | f5118ade | Iustin Pop | def Exec(self, feedback_fn): |
3666 | f5118ade | Iustin Pop | """Reboots a node.
|
3667 | f5118ade | Iustin Pop |
|
3668 | f5118ade | Iustin Pop | """
|
3669 | f5118ade | Iustin Pop | result = self.rpc.call_node_powercycle(self.op.node_name, |
3670 | f5118ade | Iustin Pop | self.cfg.GetHypervisorType())
|
3671 | 4c4e4e1e | Iustin Pop | result.Raise("Failed to schedule the reboot")
|
3672 | f5118ade | Iustin Pop | return result.payload
|
3673 | f5118ade | Iustin Pop | |
3674 | f5118ade | Iustin Pop | |
3675 | a8083063 | Iustin Pop | class LUQueryClusterInfo(NoHooksLU): |
3676 | a8083063 | Iustin Pop | """Query cluster configuration.
|
3677 | a8083063 | Iustin Pop |
|
3678 | a8083063 | Iustin Pop | """
|
3679 | a8083063 | Iustin Pop | _OP_REQP = [] |
3680 | 642339cf | Guido Trotter | REQ_BGL = False
|
3681 | 642339cf | Guido Trotter | |
3682 | 642339cf | Guido Trotter | def ExpandNames(self): |
3683 | 642339cf | Guido Trotter | self.needed_locks = {}
|
3684 | a8083063 | Iustin Pop | |
3685 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3686 | a8083063 | Iustin Pop | """No prerequsites needed for this LU.
|
3687 | a8083063 | Iustin Pop |
|
3688 | a8083063 | Iustin Pop | """
|
3689 | a8083063 | Iustin Pop | pass
|
3690 | a8083063 | Iustin Pop | |
3691 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3692 | a8083063 | Iustin Pop | """Return cluster config.
|
3693 | a8083063 | Iustin Pop |
|
3694 | a8083063 | Iustin Pop | """
|
3695 | 469f88e1 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
3696 | 17463d22 | Renรฉ Nussbaumer | os_hvp = {} |
3697 | 17463d22 | Renรฉ Nussbaumer | |
3698 | 17463d22 | Renรฉ Nussbaumer | # Filter just for enabled hypervisors
|
3699 | 17463d22 | Renรฉ Nussbaumer | for os_name, hv_dict in cluster.os_hvp.items(): |
3700 | 17463d22 | Renรฉ Nussbaumer | os_hvp[os_name] = {} |
3701 | 17463d22 | Renรฉ Nussbaumer | for hv_name, hv_params in hv_dict.items(): |
3702 | 17463d22 | Renรฉ Nussbaumer | if hv_name in cluster.enabled_hypervisors: |
3703 | 17463d22 | Renรฉ Nussbaumer | os_hvp[os_name][hv_name] = hv_params |
3704 | 17463d22 | Renรฉ Nussbaumer | |
3705 | a8083063 | Iustin Pop | result = { |
3706 | a8083063 | Iustin Pop | "software_version": constants.RELEASE_VERSION,
|
3707 | a8083063 | Iustin Pop | "protocol_version": constants.PROTOCOL_VERSION,
|
3708 | a8083063 | Iustin Pop | "config_version": constants.CONFIG_VERSION,
|
3709 | d1a7d66f | Guido Trotter | "os_api_version": max(constants.OS_API_VERSIONS), |
3710 | a8083063 | Iustin Pop | "export_version": constants.EXPORT_VERSION,
|
3711 | a8083063 | Iustin Pop | "architecture": (platform.architecture()[0], platform.machine()), |
3712 | 469f88e1 | Iustin Pop | "name": cluster.cluster_name,
|
3713 | 469f88e1 | Iustin Pop | "master": cluster.master_node,
|
3714 | 066f465d | Guido Trotter | "default_hypervisor": cluster.enabled_hypervisors[0], |
3715 | 469f88e1 | Iustin Pop | "enabled_hypervisors": cluster.enabled_hypervisors,
|
3716 | b8810fec | Michael Hanselmann | "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name]) |
3717 | 7c4d6c7b | Michael Hanselmann | for hypervisor_name in cluster.enabled_hypervisors]), |
3718 | 17463d22 | Renรฉ Nussbaumer | "os_hvp": os_hvp,
|
3719 | 469f88e1 | Iustin Pop | "beparams": cluster.beparams,
|
3720 | 1094acda | Guido Trotter | "nicparams": cluster.nicparams,
|
3721 | 4b7735f9 | Iustin Pop | "candidate_pool_size": cluster.candidate_pool_size,
|
3722 | 7a56b411 | Guido Trotter | "master_netdev": cluster.master_netdev,
|
3723 | 7a56b411 | Guido Trotter | "volume_group_name": cluster.volume_group_name,
|
3724 | 7a56b411 | Guido Trotter | "file_storage_dir": cluster.file_storage_dir,
|
3725 | 3953242f | Iustin Pop | "maintain_node_health": cluster.maintain_node_health,
|
3726 | 90f72445 | Iustin Pop | "ctime": cluster.ctime,
|
3727 | 90f72445 | Iustin Pop | "mtime": cluster.mtime,
|
3728 | 259578eb | Iustin Pop | "uuid": cluster.uuid,
|
3729 | c118d1f4 | Michael Hanselmann | "tags": list(cluster.GetTags()), |
3730 | 1338f2b4 | Balazs Lecz | "uid_pool": cluster.uid_pool,
|
3731 | a8083063 | Iustin Pop | } |
3732 | a8083063 | Iustin Pop | |
3733 | a8083063 | Iustin Pop | return result
|
3734 | a8083063 | Iustin Pop | |
3735 | a8083063 | Iustin Pop | |
3736 | ae5849b5 | Michael Hanselmann | class LUQueryConfigValues(NoHooksLU): |
3737 | ae5849b5 | Michael Hanselmann | """Return configuration values.
|
3738 | a8083063 | Iustin Pop |
|
3739 | a8083063 | Iustin Pop | """
|
3740 | a8083063 | Iustin Pop | _OP_REQP = [] |
3741 | 642339cf | Guido Trotter | REQ_BGL = False
|
3742 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet() |
3743 | 05e50653 | Michael Hanselmann | _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag", |
3744 | 05e50653 | Michael Hanselmann | "watcher_pause")
|
3745 | 642339cf | Guido Trotter | |
3746 | 642339cf | Guido Trotter | def ExpandNames(self): |
3747 | 642339cf | Guido Trotter | self.needed_locks = {}
|
3748 | a8083063 | Iustin Pop | |
3749 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
3750 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
3751 | ae5849b5 | Michael Hanselmann | selected=self.op.output_fields)
|
3752 | ae5849b5 | Michael Hanselmann | |
3753 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3754 | a8083063 | Iustin Pop | """No prerequisites.
|
3755 | a8083063 | Iustin Pop |
|
3756 | a8083063 | Iustin Pop | """
|
3757 | a8083063 | Iustin Pop | pass
|
3758 | a8083063 | Iustin Pop | |
3759 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3760 | a8083063 | Iustin Pop | """Dump a representation of the cluster config to the standard output.
|
3761 | a8083063 | Iustin Pop |
|
3762 | a8083063 | Iustin Pop | """
|
3763 | ae5849b5 | Michael Hanselmann | values = [] |
3764 | ae5849b5 | Michael Hanselmann | for field in self.op.output_fields: |
3765 | ae5849b5 | Michael Hanselmann | if field == "cluster_name": |
3766 | 3ccafd0e | Iustin Pop | entry = self.cfg.GetClusterName()
|
3767 | ae5849b5 | Michael Hanselmann | elif field == "master_node": |
3768 | 3ccafd0e | Iustin Pop | entry = self.cfg.GetMasterNode()
|
3769 | 3ccafd0e | Iustin Pop | elif field == "drain_flag": |
3770 | 3ccafd0e | Iustin Pop | entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE) |
3771 | 05e50653 | Michael Hanselmann | elif field == "watcher_pause": |
3772 | cac599f1 | Michael Hanselmann | entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE) |
3773 | ae5849b5 | Michael Hanselmann | else:
|
3774 | ae5849b5 | Michael Hanselmann | raise errors.ParameterError(field)
|
3775 | 3ccafd0e | Iustin Pop | values.append(entry) |
3776 | ae5849b5 | Michael Hanselmann | return values
|
3777 | a8083063 | Iustin Pop | |
3778 | a8083063 | Iustin Pop | |
3779 | a8083063 | Iustin Pop | class LUActivateInstanceDisks(NoHooksLU): |
3780 | a8083063 | Iustin Pop | """Bring up an instance's disks.
|
3781 | a8083063 | Iustin Pop |
|
3782 | a8083063 | Iustin Pop | """
|
3783 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3784 | f22a8ba3 | Guido Trotter | REQ_BGL = False
|
3785 | f22a8ba3 | Guido Trotter | |
3786 | f22a8ba3 | Guido Trotter | def ExpandNames(self): |
3787 | f22a8ba3 | Guido Trotter | self._ExpandAndLockInstance()
|
3788 | f22a8ba3 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
3789 | f22a8ba3 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
3790 | f22a8ba3 | Guido Trotter | |
3791 | f22a8ba3 | Guido Trotter | def DeclareLocks(self, level): |
3792 | f22a8ba3 | Guido Trotter | if level == locking.LEVEL_NODE:
|
3793 | f22a8ba3 | Guido Trotter | self._LockInstancesNodes()
|
3794 | a8083063 | Iustin Pop | |
3795 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3796 | a8083063 | Iustin Pop | """Check prerequisites.
|
3797 | a8083063 | Iustin Pop |
|
3798 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3799 | a8083063 | Iustin Pop |
|
3800 | a8083063 | Iustin Pop | """
|
3801 | f22a8ba3 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3802 | f22a8ba3 | Guido Trotter | assert self.instance is not None, \ |
3803 | f22a8ba3 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3804 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
3805 | b4ec07f8 | Iustin Pop | if not hasattr(self.op, "ignore_size"): |
3806 | b4ec07f8 | Iustin Pop | self.op.ignore_size = False |
3807 | a8083063 | Iustin Pop | |
3808 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3809 | a8083063 | Iustin Pop | """Activate the disks.
|
3810 | a8083063 | Iustin Pop |
|
3811 | a8083063 | Iustin Pop | """
|
3812 | b4ec07f8 | Iustin Pop | disks_ok, disks_info = \ |
3813 | b4ec07f8 | Iustin Pop | _AssembleInstanceDisks(self, self.instance, |
3814 | b4ec07f8 | Iustin Pop | ignore_size=self.op.ignore_size)
|
3815 | a8083063 | Iustin Pop | if not disks_ok: |
3816 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Cannot activate block devices") |
3817 | a8083063 | Iustin Pop | |
3818 | a8083063 | Iustin Pop | return disks_info
|
3819 | a8083063 | Iustin Pop | |
3820 | a8083063 | Iustin Pop | |
3821 | e3443b36 | Iustin Pop | def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False, |
3822 | e3443b36 | Iustin Pop | ignore_size=False):
|
3823 | a8083063 | Iustin Pop | """Prepare the block devices for an instance.
|
3824 | a8083063 | Iustin Pop |
|
3825 | a8083063 | Iustin Pop | This sets up the block devices on all nodes.
|
3826 | a8083063 | Iustin Pop |
|
3827 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
3828 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
3829 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
3830 | e4376078 | Iustin Pop | @param instance: the instance for whose disks we assemble
|
3831 | e4376078 | Iustin Pop | @type ignore_secondaries: boolean
|
3832 | e4376078 | Iustin Pop | @param ignore_secondaries: if true, errors on secondary nodes
|
3833 | e4376078 | Iustin Pop | won't result in an error return from the function
|
3834 | e3443b36 | Iustin Pop | @type ignore_size: boolean
|
3835 | e3443b36 | Iustin Pop | @param ignore_size: if true, the current known size of the disk
|
3836 | e3443b36 | Iustin Pop | will not be used during the disk activation, useful for cases
|
3837 | e3443b36 | Iustin Pop | when the size is wrong
|
3838 | e4376078 | Iustin Pop | @return: False if the operation failed, otherwise a list of
|
3839 | e4376078 | Iustin Pop | (host, instance_visible_name, node_visible_name)
|
3840 | e4376078 | Iustin Pop | with the mapping from node devices to instance devices
|
3841 | a8083063 | Iustin Pop |
|
3842 | a8083063 | Iustin Pop | """
|
3843 | a8083063 | Iustin Pop | device_info = [] |
3844 | a8083063 | Iustin Pop | disks_ok = True
|
3845 | fdbd668d | Iustin Pop | iname = instance.name |
3846 | fdbd668d | Iustin Pop | # With the two passes mechanism we try to reduce the window of
|
3847 | fdbd668d | Iustin Pop | # opportunity for the race condition of switching DRBD to primary
|
3848 | fdbd668d | Iustin Pop | # before handshaking occured, but we do not eliminate it
|
3849 | fdbd668d | Iustin Pop | |
3850 | fdbd668d | Iustin Pop | # The proper fix would be to wait (with some limits) until the
|
3851 | fdbd668d | Iustin Pop | # connection has been made and drbd transitions from WFConnection
|
3852 | fdbd668d | Iustin Pop | # into any other network-connected state (Connected, SyncTarget,
|
3853 | fdbd668d | Iustin Pop | # SyncSource, etc.)
|
3854 | fdbd668d | Iustin Pop | |
3855 | fdbd668d | Iustin Pop | # 1st pass, assemble on all nodes in secondary mode
|
3856 | a8083063 | Iustin Pop | for inst_disk in instance.disks: |
3857 | a8083063 | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
3858 | e3443b36 | Iustin Pop | if ignore_size:
|
3859 | e3443b36 | Iustin Pop | node_disk = node_disk.Copy() |
3860 | e3443b36 | Iustin Pop | node_disk.UnsetSize() |
3861 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(node_disk, node) |
3862 | 72737a7f | Iustin Pop | result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
|
3863 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3864 | 53c14ef1 | Iustin Pop | if msg:
|
3865 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("Could not prepare block device %s on node %s"
|
3866 | 53c14ef1 | Iustin Pop | " (is_primary=False, pass=1): %s",
|
3867 | 53c14ef1 | Iustin Pop | inst_disk.iv_name, node, msg) |
3868 | fdbd668d | Iustin Pop | if not ignore_secondaries: |
3869 | a8083063 | Iustin Pop | disks_ok = False
|
3870 | fdbd668d | Iustin Pop | |
3871 | fdbd668d | Iustin Pop | # FIXME: race condition on drbd migration to primary
|
3872 | fdbd668d | Iustin Pop | |
3873 | fdbd668d | Iustin Pop | # 2nd pass, do only the primary node
|
3874 | fdbd668d | Iustin Pop | for inst_disk in instance.disks: |
3875 | d52ea991 | Michael Hanselmann | dev_path = None
|
3876 | d52ea991 | Michael Hanselmann | |
3877 | fdbd668d | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
3878 | fdbd668d | Iustin Pop | if node != instance.primary_node:
|
3879 | fdbd668d | Iustin Pop | continue
|
3880 | e3443b36 | Iustin Pop | if ignore_size:
|
3881 | e3443b36 | Iustin Pop | node_disk = node_disk.Copy() |
3882 | e3443b36 | Iustin Pop | node_disk.UnsetSize() |
3883 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(node_disk, node) |
3884 | 72737a7f | Iustin Pop | result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
|
3885 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3886 | 53c14ef1 | Iustin Pop | if msg:
|
3887 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("Could not prepare block device %s on node %s"
|
3888 | 53c14ef1 | Iustin Pop | " (is_primary=True, pass=2): %s",
|
3889 | 53c14ef1 | Iustin Pop | inst_disk.iv_name, node, msg) |
3890 | fdbd668d | Iustin Pop | disks_ok = False
|
3891 | d52ea991 | Michael Hanselmann | else:
|
3892 | d52ea991 | Michael Hanselmann | dev_path = result.payload |
3893 | d52ea991 | Michael Hanselmann | |
3894 | d52ea991 | Michael Hanselmann | device_info.append((instance.primary_node, inst_disk.iv_name, dev_path)) |
3895 | a8083063 | Iustin Pop | |
3896 | b352ab5b | Iustin Pop | # leave the disks configured for the primary node
|
3897 | b352ab5b | Iustin Pop | # this is a workaround that would be fixed better by
|
3898 | b352ab5b | Iustin Pop | # improving the logical/physical id handling
|
3899 | b352ab5b | Iustin Pop | for disk in instance.disks: |
3900 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(disk, instance.primary_node) |
3901 | b352ab5b | Iustin Pop | |
3902 | a8083063 | Iustin Pop | return disks_ok, device_info
|
3903 | a8083063 | Iustin Pop | |
3904 | a8083063 | Iustin Pop | |
3905 | b9bddb6b | Iustin Pop | def _StartInstanceDisks(lu, instance, force): |
3906 | 3ecf6786 | Iustin Pop | """Start the disks of an instance.
|
3907 | 3ecf6786 | Iustin Pop |
|
3908 | 3ecf6786 | Iustin Pop | """
|
3909 | 7c4d6c7b | Michael Hanselmann | disks_ok, _ = _AssembleInstanceDisks(lu, instance, |
3910 | fe7b0351 | Michael Hanselmann | ignore_secondaries=force) |
3911 | fe7b0351 | Michael Hanselmann | if not disks_ok: |
3912 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(lu, instance) |
3913 | fe7b0351 | Michael Hanselmann | if force is not None and not force: |
3914 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("", hint="If the message above refers to a" |
3915 | 86d9d3bb | Iustin Pop | " secondary node,"
|
3916 | 86d9d3bb | Iustin Pop | " you can retry the operation using '--force'.")
|
3917 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Disk consistency error") |
3918 | fe7b0351 | Michael Hanselmann | |
3919 | fe7b0351 | Michael Hanselmann | |
3920 | a8083063 | Iustin Pop | class LUDeactivateInstanceDisks(NoHooksLU): |
3921 | a8083063 | Iustin Pop | """Shutdown an instance's disks.
|
3922 | a8083063 | Iustin Pop |
|
3923 | a8083063 | Iustin Pop | """
|
3924 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3925 | f22a8ba3 | Guido Trotter | REQ_BGL = False
|
3926 | f22a8ba3 | Guido Trotter | |
3927 | f22a8ba3 | Guido Trotter | def ExpandNames(self): |
3928 | f22a8ba3 | Guido Trotter | self._ExpandAndLockInstance()
|
3929 | f22a8ba3 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
3930 | f22a8ba3 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
3931 | f22a8ba3 | Guido Trotter | |
3932 | f22a8ba3 | Guido Trotter | def DeclareLocks(self, level): |
3933 | f22a8ba3 | Guido Trotter | if level == locking.LEVEL_NODE:
|
3934 | f22a8ba3 | Guido Trotter | self._LockInstancesNodes()
|
3935 | a8083063 | Iustin Pop | |
3936 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3937 | a8083063 | Iustin Pop | """Check prerequisites.
|
3938 | a8083063 | Iustin Pop |
|
3939 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3940 | a8083063 | Iustin Pop |
|
3941 | a8083063 | Iustin Pop | """
|
3942 | f22a8ba3 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3943 | f22a8ba3 | Guido Trotter | assert self.instance is not None, \ |
3944 | f22a8ba3 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3945 | a8083063 | Iustin Pop | |
3946 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3947 | a8083063 | Iustin Pop | """Deactivate the disks
|
3948 | a8083063 | Iustin Pop |
|
3949 | a8083063 | Iustin Pop | """
|
3950 | a8083063 | Iustin Pop | instance = self.instance
|
3951 | b9bddb6b | Iustin Pop | _SafeShutdownInstanceDisks(self, instance)
|
3952 | a8083063 | Iustin Pop | |
3953 | a8083063 | Iustin Pop | |
3954 | b9bddb6b | Iustin Pop | def _SafeShutdownInstanceDisks(lu, instance): |
3955 | 155d6c75 | Guido Trotter | """Shutdown block devices of an instance.
|
3956 | 155d6c75 | Guido Trotter |
|
3957 | 155d6c75 | Guido Trotter | This function checks if an instance is running, before calling
|
3958 | 155d6c75 | Guido Trotter | _ShutdownInstanceDisks.
|
3959 | 155d6c75 | Guido Trotter |
|
3960 | 155d6c75 | Guido Trotter | """
|
3961 | 31624382 | Iustin Pop | _CheckInstanceDown(lu, instance, "cannot shutdown disks")
|
3962 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(lu, instance) |
3963 | a8083063 | Iustin Pop | |
3964 | a8083063 | Iustin Pop | |
3965 | b9bddb6b | Iustin Pop | def _ShutdownInstanceDisks(lu, instance, ignore_primary=False): |
3966 | a8083063 | Iustin Pop | """Shutdown block devices of an instance.
|
3967 | a8083063 | Iustin Pop |
|
3968 | a8083063 | Iustin Pop | This does the shutdown on all nodes of the instance.
|
3969 | a8083063 | Iustin Pop |
|
3970 | a8083063 | Iustin Pop | If the ignore_primary is false, errors on the primary node are
|
3971 | a8083063 | Iustin Pop | ignored.
|
3972 | a8083063 | Iustin Pop |
|
3973 | a8083063 | Iustin Pop | """
|
3974 | cacfd1fd | Iustin Pop | all_result = True
|
3975 | a8083063 | Iustin Pop | for disk in instance.disks: |
3976 | a8083063 | Iustin Pop | for node, top_disk in disk.ComputeNodeTree(instance.primary_node): |
3977 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(top_disk, node) |
3978 | 781de953 | Iustin Pop | result = lu.rpc.call_blockdev_shutdown(node, top_disk) |
3979 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3980 | cacfd1fd | Iustin Pop | if msg:
|
3981 | cacfd1fd | Iustin Pop | lu.LogWarning("Could not shutdown block device %s on node %s: %s",
|
3982 | cacfd1fd | Iustin Pop | disk.iv_name, node, msg) |
3983 | a8083063 | Iustin Pop | if not ignore_primary or node != instance.primary_node: |
3984 | cacfd1fd | Iustin Pop | all_result = False
|
3985 | cacfd1fd | Iustin Pop | return all_result
|
3986 | a8083063 | Iustin Pop | |
3987 | a8083063 | Iustin Pop | |
3988 | 9ca87a96 | Iustin Pop | def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name): |
3989 | d4f16fd9 | Iustin Pop | """Checks if a node has enough free memory.
|
3990 | d4f16fd9 | Iustin Pop |
|
3991 | d4f16fd9 | Iustin Pop | This function check if a given node has the needed amount of free
|
3992 | d4f16fd9 | Iustin Pop | memory. In case the node has less memory or we cannot get the
|
3993 | d4f16fd9 | Iustin Pop | information from the node, this function raise an OpPrereqError
|
3994 | d4f16fd9 | Iustin Pop | exception.
|
3995 | d4f16fd9 | Iustin Pop |
|
3996 | b9bddb6b | Iustin Pop | @type lu: C{LogicalUnit}
|
3997 | b9bddb6b | Iustin Pop | @param lu: a logical unit from which we get configuration data
|
3998 | e69d05fd | Iustin Pop | @type node: C{str}
|
3999 | e69d05fd | Iustin Pop | @param node: the node to check
|
4000 | e69d05fd | Iustin Pop | @type reason: C{str}
|
4001 | e69d05fd | Iustin Pop | @param reason: string to use in the error message
|
4002 | e69d05fd | Iustin Pop | @type requested: C{int}
|
4003 | e69d05fd | Iustin Pop | @param requested: the amount of memory in MiB to check for
|
4004 | 9ca87a96 | Iustin Pop | @type hypervisor_name: C{str}
|
4005 | 9ca87a96 | Iustin Pop | @param hypervisor_name: the hypervisor to ask for memory stats
|
4006 | e69d05fd | Iustin Pop | @raise errors.OpPrereqError: if the node doesn't have enough memory, or
|
4007 | e69d05fd | Iustin Pop | we cannot check the node
|
4008 | d4f16fd9 | Iustin Pop |
|
4009 | d4f16fd9 | Iustin Pop | """
|
4010 | 9ca87a96 | Iustin Pop | nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name) |
4011 | 045dd6d9 | Iustin Pop | nodeinfo[node].Raise("Can't get data from node %s" % node,
|
4012 | 045dd6d9 | Iustin Pop | prereq=True, ecode=errors.ECODE_ENVIRON)
|
4013 | 070e998b | Iustin Pop | free_mem = nodeinfo[node].payload.get('memory_free', None) |
4014 | d4f16fd9 | Iustin Pop | if not isinstance(free_mem, int): |
4015 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Can't compute free memory on node %s, result" |
4016 | 5c983ee5 | Iustin Pop | " was '%s'" % (node, free_mem),
|
4017 | 5c983ee5 | Iustin Pop | errors.ECODE_ENVIRON) |
4018 | d4f16fd9 | Iustin Pop | if requested > free_mem:
|
4019 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Not enough memory on node %s for %s:" |
4020 | 070e998b | Iustin Pop | " needed %s MiB, available %s MiB" %
|
4021 | 5c983ee5 | Iustin Pop | (node, reason, requested, free_mem), |
4022 | 5c983ee5 | Iustin Pop | errors.ECODE_NORES) |
4023 | d4f16fd9 | Iustin Pop | |
4024 | d4f16fd9 | Iustin Pop | |
4025 | 701384a9 | Iustin Pop | def _CheckNodesFreeDisk(lu, nodenames, requested): |
4026 | 701384a9 | Iustin Pop | """Checks if nodes have enough free disk space in the default VG.
|
4027 | 701384a9 | Iustin Pop |
|
4028 | 701384a9 | Iustin Pop | This function check if all given nodes have the needed amount of
|
4029 | 701384a9 | Iustin Pop | free disk. In case any node has less disk or we cannot get the
|
4030 | 701384a9 | Iustin Pop | information from the node, this function raise an OpPrereqError
|
4031 | 701384a9 | Iustin Pop | exception.
|
4032 | 701384a9 | Iustin Pop |
|
4033 | 701384a9 | Iustin Pop | @type lu: C{LogicalUnit}
|
4034 | 701384a9 | Iustin Pop | @param lu: a logical unit from which we get configuration data
|
4035 | 701384a9 | Iustin Pop | @type nodenames: C{list}
|
4036 | 3a488770 | Iustin Pop | @param nodenames: the list of node names to check
|
4037 | 701384a9 | Iustin Pop | @type requested: C{int}
|
4038 | 701384a9 | Iustin Pop | @param requested: the amount of disk in MiB to check for
|
4039 | 701384a9 | Iustin Pop | @raise errors.OpPrereqError: if the node doesn't have enough disk, or
|
4040 | 701384a9 | Iustin Pop | we cannot check the node
|
4041 | 701384a9 | Iustin Pop |
|
4042 | 701384a9 | Iustin Pop | """
|
4043 | 701384a9 | Iustin Pop | nodeinfo = lu.rpc.call_node_info(nodenames, lu.cfg.GetVGName(), |
4044 | 701384a9 | Iustin Pop | lu.cfg.GetHypervisorType()) |
4045 | 701384a9 | Iustin Pop | for node in nodenames: |
4046 | 701384a9 | Iustin Pop | info = nodeinfo[node] |
4047 | 701384a9 | Iustin Pop | info.Raise("Cannot get current information from node %s" % node,
|
4048 | 701384a9 | Iustin Pop | prereq=True, ecode=errors.ECODE_ENVIRON)
|
4049 | 701384a9 | Iustin Pop | vg_free = info.payload.get("vg_free", None) |
4050 | 701384a9 | Iustin Pop | if not isinstance(vg_free, int): |
4051 | 701384a9 | Iustin Pop | raise errors.OpPrereqError("Can't compute free disk space on node %s," |
4052 | 701384a9 | Iustin Pop | " result was '%s'" % (node, vg_free),
|
4053 | 701384a9 | Iustin Pop | errors.ECODE_ENVIRON) |
4054 | 701384a9 | Iustin Pop | if requested > vg_free:
|
4055 | 701384a9 | Iustin Pop | raise errors.OpPrereqError("Not enough disk space on target node %s:" |
4056 | 701384a9 | Iustin Pop | " required %d MiB, available %d MiB" %
|
4057 | 701384a9 | Iustin Pop | (node, requested, vg_free), |
4058 | 701384a9 | Iustin Pop | errors.ECODE_NORES) |
4059 | 701384a9 | Iustin Pop | |
4060 | 701384a9 | Iustin Pop | |
4061 | a8083063 | Iustin Pop | class LUStartupInstance(LogicalUnit): |
4062 | a8083063 | Iustin Pop | """Starts an instance.
|
4063 | a8083063 | Iustin Pop |
|
4064 | a8083063 | Iustin Pop | """
|
4065 | a8083063 | Iustin Pop | HPATH = "instance-start"
|
4066 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4067 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "force"] |
4068 | e873317a | Guido Trotter | REQ_BGL = False
|
4069 | e873317a | Guido Trotter | |
4070 | e873317a | Guido Trotter | def ExpandNames(self): |
4071 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
4072 | a8083063 | Iustin Pop | |
4073 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4074 | a8083063 | Iustin Pop | """Build hooks env.
|
4075 | a8083063 | Iustin Pop |
|
4076 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4077 | a8083063 | Iustin Pop |
|
4078 | a8083063 | Iustin Pop | """
|
4079 | a8083063 | Iustin Pop | env = { |
4080 | a8083063 | Iustin Pop | "FORCE": self.op.force, |
4081 | a8083063 | Iustin Pop | } |
4082 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
4083 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4084 | a8083063 | Iustin Pop | return env, nl, nl
|
4085 | a8083063 | Iustin Pop | |
4086 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4087 | a8083063 | Iustin Pop | """Check prerequisites.
|
4088 | a8083063 | Iustin Pop |
|
4089 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
4090 | a8083063 | Iustin Pop |
|
4091 | a8083063 | Iustin Pop | """
|
4092 | e873317a | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4093 | e873317a | Guido Trotter | assert self.instance is not None, \ |
4094 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4095 | a8083063 | Iustin Pop | |
4096 | d04aaa2f | Iustin Pop | # extra beparams
|
4097 | d04aaa2f | Iustin Pop | self.beparams = getattr(self.op, "beparams", {}) |
4098 | d04aaa2f | Iustin Pop | if self.beparams: |
4099 | d04aaa2f | Iustin Pop | if not isinstance(self.beparams, dict): |
4100 | d04aaa2f | Iustin Pop | raise errors.OpPrereqError("Invalid beparams passed: %s, expected" |
4101 | 5c983ee5 | Iustin Pop | " dict" % (type(self.beparams), ), |
4102 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
4103 | d04aaa2f | Iustin Pop | # fill the beparams dict
|
4104 | d04aaa2f | Iustin Pop | utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
|
4105 | d04aaa2f | Iustin Pop | self.op.beparams = self.beparams |
4106 | d04aaa2f | Iustin Pop | |
4107 | d04aaa2f | Iustin Pop | # extra hvparams
|
4108 | d04aaa2f | Iustin Pop | self.hvparams = getattr(self.op, "hvparams", {}) |
4109 | d04aaa2f | Iustin Pop | if self.hvparams: |
4110 | d04aaa2f | Iustin Pop | if not isinstance(self.hvparams, dict): |
4111 | d04aaa2f | Iustin Pop | raise errors.OpPrereqError("Invalid hvparams passed: %s, expected" |
4112 | 5c983ee5 | Iustin Pop | " dict" % (type(self.hvparams), ), |
4113 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
4114 | d04aaa2f | Iustin Pop | |
4115 | d04aaa2f | Iustin Pop | # check hypervisor parameter syntax (locally)
|
4116 | d04aaa2f | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
4117 | d04aaa2f | Iustin Pop | utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
|
4118 | abe609b2 | Guido Trotter | filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor], |
4119 | d04aaa2f | Iustin Pop | instance.hvparams) |
4120 | d04aaa2f | Iustin Pop | filled_hvp.update(self.hvparams)
|
4121 | d04aaa2f | Iustin Pop | hv_type = hypervisor.GetHypervisor(instance.hypervisor) |
4122 | d04aaa2f | Iustin Pop | hv_type.CheckParameterSyntax(filled_hvp) |
4123 | d04aaa2f | Iustin Pop | _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
|
4124 | d04aaa2f | Iustin Pop | self.op.hvparams = self.hvparams |
4125 | d04aaa2f | Iustin Pop | |
4126 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
4127 | 7527a8a4 | Iustin Pop | |
4128 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
4129 | 5bbd3f7f | Michael Hanselmann | # check bridges existence
|
4130 | b9bddb6b | Iustin Pop | _CheckInstanceBridgesExist(self, instance)
|
4131 | a8083063 | Iustin Pop | |
4132 | f1926756 | Guido Trotter | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
4133 | f1926756 | Guido Trotter | instance.name, |
4134 | f1926756 | Guido Trotter | instance.hypervisor) |
4135 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node,
|
4136 | 045dd6d9 | Iustin Pop | prereq=True, ecode=errors.ECODE_ENVIRON)
|
4137 | 7ad1af4a | Iustin Pop | if not remote_info.payload: # not running already |
4138 | f1926756 | Guido Trotter | _CheckNodeFreeMemory(self, instance.primary_node,
|
4139 | f1926756 | Guido Trotter | "starting instance %s" % instance.name,
|
4140 | f1926756 | Guido Trotter | bep[constants.BE_MEMORY], instance.hypervisor) |
4141 | d4f16fd9 | Iustin Pop | |
4142 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4143 | a8083063 | Iustin Pop | """Start the instance.
|
4144 | a8083063 | Iustin Pop |
|
4145 | a8083063 | Iustin Pop | """
|
4146 | a8083063 | Iustin Pop | instance = self.instance
|
4147 | a8083063 | Iustin Pop | force = self.op.force
|
4148 | a8083063 | Iustin Pop | |
4149 | fe482621 | Iustin Pop | self.cfg.MarkInstanceUp(instance.name)
|
4150 | fe482621 | Iustin Pop | |
4151 | a8083063 | Iustin Pop | node_current = instance.primary_node |
4152 | a8083063 | Iustin Pop | |
4153 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, instance, force)
|
4154 | a8083063 | Iustin Pop | |
4155 | d04aaa2f | Iustin Pop | result = self.rpc.call_instance_start(node_current, instance,
|
4156 | d04aaa2f | Iustin Pop | self.hvparams, self.beparams) |
4157 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4158 | dd279568 | Iustin Pop | if msg:
|
4159 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4160 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance: %s" % msg) |
4161 | a8083063 | Iustin Pop | |
4162 | a8083063 | Iustin Pop | |
4163 | bf6929a2 | Alexander Schreiber | class LURebootInstance(LogicalUnit): |
4164 | bf6929a2 | Alexander Schreiber | """Reboot an instance.
|
4165 | bf6929a2 | Alexander Schreiber |
|
4166 | bf6929a2 | Alexander Schreiber | """
|
4167 | bf6929a2 | Alexander Schreiber | HPATH = "instance-reboot"
|
4168 | bf6929a2 | Alexander Schreiber | HTYPE = constants.HTYPE_INSTANCE |
4169 | bf6929a2 | Alexander Schreiber | _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"] |
4170 | e873317a | Guido Trotter | REQ_BGL = False
|
4171 | e873317a | Guido Trotter | |
4172 | 17c3f802 | Guido Trotter | def CheckArguments(self): |
4173 | 17c3f802 | Guido Trotter | """Check the arguments.
|
4174 | 17c3f802 | Guido Trotter |
|
4175 | 17c3f802 | Guido Trotter | """
|
4176 | 17c3f802 | Guido Trotter | self.shutdown_timeout = getattr(self.op, "shutdown_timeout", |
4177 | 17c3f802 | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
4178 | 17c3f802 | Guido Trotter | |
4179 | e873317a | Guido Trotter | def ExpandNames(self): |
4180 | 0fcc5db3 | Guido Trotter | if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT, |
4181 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_HARD, |
4182 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_FULL]: |
4183 | 0fcc5db3 | Guido Trotter | raise errors.ParameterError("reboot type not in [%s, %s, %s]" % |
4184 | 0fcc5db3 | Guido Trotter | (constants.INSTANCE_REBOOT_SOFT, |
4185 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_HARD, |
4186 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_FULL)) |
4187 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
4188 | bf6929a2 | Alexander Schreiber | |
4189 | bf6929a2 | Alexander Schreiber | def BuildHooksEnv(self): |
4190 | bf6929a2 | Alexander Schreiber | """Build hooks env.
|
4191 | bf6929a2 | Alexander Schreiber |
|
4192 | bf6929a2 | Alexander Schreiber | This runs on master, primary and secondary nodes of the instance.
|
4193 | bf6929a2 | Alexander Schreiber |
|
4194 | bf6929a2 | Alexander Schreiber | """
|
4195 | bf6929a2 | Alexander Schreiber | env = { |
4196 | bf6929a2 | Alexander Schreiber | "IGNORE_SECONDARIES": self.op.ignore_secondaries, |
4197 | 2c2690c9 | Iustin Pop | "REBOOT_TYPE": self.op.reboot_type, |
4198 | 17c3f802 | Guido Trotter | "SHUTDOWN_TIMEOUT": self.shutdown_timeout, |
4199 | bf6929a2 | Alexander Schreiber | } |
4200 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
4201 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4202 | bf6929a2 | Alexander Schreiber | return env, nl, nl
|
4203 | bf6929a2 | Alexander Schreiber | |
4204 | bf6929a2 | Alexander Schreiber | def CheckPrereq(self): |
4205 | bf6929a2 | Alexander Schreiber | """Check prerequisites.
|
4206 | bf6929a2 | Alexander Schreiber |
|
4207 | bf6929a2 | Alexander Schreiber | This checks that the instance is in the cluster.
|
4208 | bf6929a2 | Alexander Schreiber |
|
4209 | bf6929a2 | Alexander Schreiber | """
|
4210 | e873317a | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4211 | e873317a | Guido Trotter | assert self.instance is not None, \ |
4212 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4213 | bf6929a2 | Alexander Schreiber | |
4214 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
4215 | 7527a8a4 | Iustin Pop | |
4216 | 5bbd3f7f | Michael Hanselmann | # check bridges existence
|
4217 | b9bddb6b | Iustin Pop | _CheckInstanceBridgesExist(self, instance)
|
4218 | bf6929a2 | Alexander Schreiber | |
4219 | bf6929a2 | Alexander Schreiber | def Exec(self, feedback_fn): |
4220 | bf6929a2 | Alexander Schreiber | """Reboot the instance.
|
4221 | bf6929a2 | Alexander Schreiber |
|
4222 | bf6929a2 | Alexander Schreiber | """
|
4223 | bf6929a2 | Alexander Schreiber | instance = self.instance
|
4224 | bf6929a2 | Alexander Schreiber | ignore_secondaries = self.op.ignore_secondaries
|
4225 | bf6929a2 | Alexander Schreiber | reboot_type = self.op.reboot_type
|
4226 | bf6929a2 | Alexander Schreiber | |
4227 | bf6929a2 | Alexander Schreiber | node_current = instance.primary_node |
4228 | bf6929a2 | Alexander Schreiber | |
4229 | bf6929a2 | Alexander Schreiber | if reboot_type in [constants.INSTANCE_REBOOT_SOFT, |
4230 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_HARD]: |
4231 | ae48ac32 | Iustin Pop | for disk in instance.disks: |
4232 | ae48ac32 | Iustin Pop | self.cfg.SetDiskID(disk, node_current)
|
4233 | 781de953 | Iustin Pop | result = self.rpc.call_instance_reboot(node_current, instance,
|
4234 | 17c3f802 | Guido Trotter | reboot_type, |
4235 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
4236 | 4c4e4e1e | Iustin Pop | result.Raise("Could not reboot instance")
|
4237 | bf6929a2 | Alexander Schreiber | else:
|
4238 | 17c3f802 | Guido Trotter | result = self.rpc.call_instance_shutdown(node_current, instance,
|
4239 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
4240 | 4c4e4e1e | Iustin Pop | result.Raise("Could not shutdown instance for full reboot")
|
4241 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4242 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, instance, ignore_secondaries)
|
4243 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(node_current, instance, None, None) |
4244 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4245 | dd279568 | Iustin Pop | if msg:
|
4246 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4247 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance for" |
4248 | dd279568 | Iustin Pop | " full reboot: %s" % msg)
|
4249 | bf6929a2 | Alexander Schreiber | |
4250 | bf6929a2 | Alexander Schreiber | self.cfg.MarkInstanceUp(instance.name)
|
4251 | bf6929a2 | Alexander Schreiber | |
4252 | bf6929a2 | Alexander Schreiber | |
4253 | a8083063 | Iustin Pop | class LUShutdownInstance(LogicalUnit): |
4254 | a8083063 | Iustin Pop | """Shutdown an instance.
|
4255 | a8083063 | Iustin Pop |
|
4256 | a8083063 | Iustin Pop | """
|
4257 | a8083063 | Iustin Pop | HPATH = "instance-stop"
|
4258 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4259 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
4260 | e873317a | Guido Trotter | REQ_BGL = False
|
4261 | e873317a | Guido Trotter | |
4262 | 6263189c | Guido Trotter | def CheckArguments(self): |
4263 | 6263189c | Guido Trotter | """Check the arguments.
|
4264 | 6263189c | Guido Trotter |
|
4265 | 6263189c | Guido Trotter | """
|
4266 | 6263189c | Guido Trotter | self.timeout = getattr(self.op, "timeout", |
4267 | 6263189c | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
4268 | 6263189c | Guido Trotter | |
4269 | e873317a | Guido Trotter | def ExpandNames(self): |
4270 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
4271 | a8083063 | Iustin Pop | |
4272 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4273 | a8083063 | Iustin Pop | """Build hooks env.
|
4274 | a8083063 | Iustin Pop |
|
4275 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4276 | a8083063 | Iustin Pop |
|
4277 | a8083063 | Iustin Pop | """
|
4278 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
4279 | 6263189c | Guido Trotter | env["TIMEOUT"] = self.timeout |
4280 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4281 | a8083063 | Iustin Pop | return env, nl, nl
|
4282 | a8083063 | Iustin Pop | |
4283 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4284 | a8083063 | Iustin Pop | """Check prerequisites.
|
4285 | a8083063 | Iustin Pop |
|
4286 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
4287 | a8083063 | Iustin Pop |
|
4288 | a8083063 | Iustin Pop | """
|
4289 | e873317a | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4290 | e873317a | Guido Trotter | assert self.instance is not None, \ |
4291 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4292 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
4293 | a8083063 | Iustin Pop | |
4294 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4295 | a8083063 | Iustin Pop | """Shutdown the instance.
|
4296 | a8083063 | Iustin Pop |
|
4297 | a8083063 | Iustin Pop | """
|
4298 | a8083063 | Iustin Pop | instance = self.instance
|
4299 | a8083063 | Iustin Pop | node_current = instance.primary_node |
4300 | 6263189c | Guido Trotter | timeout = self.timeout
|
4301 | fe482621 | Iustin Pop | self.cfg.MarkInstanceDown(instance.name)
|
4302 | 6263189c | Guido Trotter | result = self.rpc.call_instance_shutdown(node_current, instance, timeout)
|
4303 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4304 | 1fae010f | Iustin Pop | if msg:
|
4305 | 1fae010f | Iustin Pop | self.proc.LogWarning("Could not shutdown instance: %s" % msg) |
4306 | a8083063 | Iustin Pop | |
4307 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4308 | a8083063 | Iustin Pop | |
4309 | a8083063 | Iustin Pop | |
4310 | fe7b0351 | Michael Hanselmann | class LUReinstallInstance(LogicalUnit): |
4311 | fe7b0351 | Michael Hanselmann | """Reinstall an instance.
|
4312 | fe7b0351 | Michael Hanselmann |
|
4313 | fe7b0351 | Michael Hanselmann | """
|
4314 | fe7b0351 | Michael Hanselmann | HPATH = "instance-reinstall"
|
4315 | fe7b0351 | Michael Hanselmann | HTYPE = constants.HTYPE_INSTANCE |
4316 | fe7b0351 | Michael Hanselmann | _OP_REQP = ["instance_name"]
|
4317 | 4e0b4d2d | Guido Trotter | REQ_BGL = False
|
4318 | 4e0b4d2d | Guido Trotter | |
4319 | 4e0b4d2d | Guido Trotter | def ExpandNames(self): |
4320 | 4e0b4d2d | Guido Trotter | self._ExpandAndLockInstance()
|
4321 | fe7b0351 | Michael Hanselmann | |
4322 | fe7b0351 | Michael Hanselmann | def BuildHooksEnv(self): |
4323 | fe7b0351 | Michael Hanselmann | """Build hooks env.
|
4324 | fe7b0351 | Michael Hanselmann |
|
4325 | fe7b0351 | Michael Hanselmann | This runs on master, primary and secondary nodes of the instance.
|
4326 | fe7b0351 | Michael Hanselmann |
|
4327 | fe7b0351 | Michael Hanselmann | """
|
4328 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
4329 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4330 | fe7b0351 | Michael Hanselmann | return env, nl, nl
|
4331 | fe7b0351 | Michael Hanselmann | |
4332 | fe7b0351 | Michael Hanselmann | def CheckPrereq(self): |
4333 | fe7b0351 | Michael Hanselmann | """Check prerequisites.
|
4334 | fe7b0351 | Michael Hanselmann |
|
4335 | fe7b0351 | Michael Hanselmann | This checks that the instance is in the cluster and is not running.
|
4336 | fe7b0351 | Michael Hanselmann |
|
4337 | fe7b0351 | Michael Hanselmann | """
|
4338 | 4e0b4d2d | Guido Trotter | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4339 | 4e0b4d2d | Guido Trotter | assert instance is not None, \ |
4340 | 4e0b4d2d | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4341 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
4342 | 4e0b4d2d | Guido Trotter | |
4343 | fe7b0351 | Michael Hanselmann | if instance.disk_template == constants.DT_DISKLESS:
|
4344 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has no disks" % |
4345 | 5c983ee5 | Iustin Pop | self.op.instance_name,
|
4346 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
4347 | 31624382 | Iustin Pop | _CheckInstanceDown(self, instance, "cannot reinstall") |
4348 | d0834de3 | Michael Hanselmann | |
4349 | d0834de3 | Michael Hanselmann | self.op.os_type = getattr(self.op, "os_type", None) |
4350 | f2c05717 | Guido Trotter | self.op.force_variant = getattr(self.op, "force_variant", False) |
4351 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
4352 | d0834de3 | Michael Hanselmann | # OS verification
|
4353 | cf26a87a | Iustin Pop | pnode = _ExpandNodeName(self.cfg, instance.primary_node)
|
4354 | 231cd901 | Iustin Pop | _CheckNodeHasOS(self, pnode, self.op.os_type, self.op.force_variant) |
4355 | d0834de3 | Michael Hanselmann | |
4356 | fe7b0351 | Michael Hanselmann | self.instance = instance
|
4357 | fe7b0351 | Michael Hanselmann | |
4358 | fe7b0351 | Michael Hanselmann | def Exec(self, feedback_fn): |
4359 | fe7b0351 | Michael Hanselmann | """Reinstall the instance.
|
4360 | fe7b0351 | Michael Hanselmann |
|
4361 | fe7b0351 | Michael Hanselmann | """
|
4362 | fe7b0351 | Michael Hanselmann | inst = self.instance
|
4363 | fe7b0351 | Michael Hanselmann | |
4364 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
4365 | d0834de3 | Michael Hanselmann | feedback_fn("Changing OS to '%s'..." % self.op.os_type) |
4366 | d0834de3 | Michael Hanselmann | inst.os = self.op.os_type
|
4367 | a4eae71f | Michael Hanselmann | self.cfg.Update(inst, feedback_fn)
|
4368 | d0834de3 | Michael Hanselmann | |
4369 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, inst, None) |
4370 | fe7b0351 | Michael Hanselmann | try:
|
4371 | fe7b0351 | Michael Hanselmann | feedback_fn("Running the instance OS create scripts...")
|
4372 | 4a0e011f | Iustin Pop | # FIXME: pass debug option from opcode to backend
|
4373 | dd713605 | Iustin Pop | result = self.rpc.call_instance_os_add(inst.primary_node, inst, True, |
4374 | dd713605 | Iustin Pop | self.op.debug_level)
|
4375 | 4c4e4e1e | Iustin Pop | result.Raise("Could not install OS for instance %s on node %s" %
|
4376 | 4c4e4e1e | Iustin Pop | (inst.name, inst.primary_node)) |
4377 | fe7b0351 | Michael Hanselmann | finally:
|
4378 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, inst)
|
4379 | fe7b0351 | Michael Hanselmann | |
4380 | fe7b0351 | Michael Hanselmann | |
4381 | bd315bfa | Iustin Pop | class LURecreateInstanceDisks(LogicalUnit): |
4382 | bd315bfa | Iustin Pop | """Recreate an instance's missing disks.
|
4383 | bd315bfa | Iustin Pop |
|
4384 | bd315bfa | Iustin Pop | """
|
4385 | bd315bfa | Iustin Pop | HPATH = "instance-recreate-disks"
|
4386 | bd315bfa | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4387 | bd315bfa | Iustin Pop | _OP_REQP = ["instance_name", "disks"] |
4388 | bd315bfa | Iustin Pop | REQ_BGL = False
|
4389 | bd315bfa | Iustin Pop | |
4390 | bd315bfa | Iustin Pop | def CheckArguments(self): |
4391 | bd315bfa | Iustin Pop | """Check the arguments.
|
4392 | bd315bfa | Iustin Pop |
|
4393 | bd315bfa | Iustin Pop | """
|
4394 | bd315bfa | Iustin Pop | if not isinstance(self.op.disks, list): |
4395 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid disks parameter", errors.ECODE_INVAL) |
4396 | bd315bfa | Iustin Pop | for item in self.op.disks: |
4397 | bd315bfa | Iustin Pop | if (not isinstance(item, int) or |
4398 | bd315bfa | Iustin Pop | item < 0):
|
4399 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Invalid disk specification '%s'" % |
4400 | 5c983ee5 | Iustin Pop | str(item), errors.ECODE_INVAL)
|
4401 | bd315bfa | Iustin Pop | |
4402 | bd315bfa | Iustin Pop | def ExpandNames(self): |
4403 | bd315bfa | Iustin Pop | self._ExpandAndLockInstance()
|
4404 | bd315bfa | Iustin Pop | |
4405 | bd315bfa | Iustin Pop | def BuildHooksEnv(self): |
4406 | bd315bfa | Iustin Pop | """Build hooks env.
|
4407 | bd315bfa | Iustin Pop |
|
4408 | bd315bfa | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4409 | bd315bfa | Iustin Pop |
|
4410 | bd315bfa | Iustin Pop | """
|
4411 | bd315bfa | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
4412 | bd315bfa | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4413 | bd315bfa | Iustin Pop | return env, nl, nl
|
4414 | bd315bfa | Iustin Pop | |
4415 | bd315bfa | Iustin Pop | def CheckPrereq(self): |
4416 | bd315bfa | Iustin Pop | """Check prerequisites.
|
4417 | bd315bfa | Iustin Pop |
|
4418 | bd315bfa | Iustin Pop | This checks that the instance is in the cluster and is not running.
|
4419 | bd315bfa | Iustin Pop |
|
4420 | bd315bfa | Iustin Pop | """
|
4421 | bd315bfa | Iustin Pop | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4422 | bd315bfa | Iustin Pop | assert instance is not None, \ |
4423 | bd315bfa | Iustin Pop | "Cannot retrieve locked instance %s" % self.op.instance_name |
4424 | bd315bfa | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
4425 | bd315bfa | Iustin Pop | |
4426 | bd315bfa | Iustin Pop | if instance.disk_template == constants.DT_DISKLESS:
|
4427 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has no disks" % |
4428 | 5c983ee5 | Iustin Pop | self.op.instance_name, errors.ECODE_INVAL)
|
4429 | 31624382 | Iustin Pop | _CheckInstanceDown(self, instance, "cannot recreate disks") |
4430 | bd315bfa | Iustin Pop | |
4431 | bd315bfa | Iustin Pop | if not self.op.disks: |
4432 | bd315bfa | Iustin Pop | self.op.disks = range(len(instance.disks)) |
4433 | bd315bfa | Iustin Pop | else:
|
4434 | bd315bfa | Iustin Pop | for idx in self.op.disks: |
4435 | bd315bfa | Iustin Pop | if idx >= len(instance.disks): |
4436 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid disk index passed '%s'" % idx, |
4437 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
4438 | bd315bfa | Iustin Pop | |
4439 | bd315bfa | Iustin Pop | self.instance = instance
|
4440 | bd315bfa | Iustin Pop | |
4441 | bd315bfa | Iustin Pop | def Exec(self, feedback_fn): |
4442 | bd315bfa | Iustin Pop | """Recreate the disks.
|
4443 | bd315bfa | Iustin Pop |
|
4444 | bd315bfa | Iustin Pop | """
|
4445 | bd315bfa | Iustin Pop | to_skip = [] |
4446 | 1122eb25 | Iustin Pop | for idx, _ in enumerate(self.instance.disks): |
4447 | bd315bfa | Iustin Pop | if idx not in self.op.disks: # disk idx has not been passed in |
4448 | bd315bfa | Iustin Pop | to_skip.append(idx) |
4449 | bd315bfa | Iustin Pop | continue
|
4450 | bd315bfa | Iustin Pop | |
4451 | bd315bfa | Iustin Pop | _CreateDisks(self, self.instance, to_skip=to_skip) |
4452 | bd315bfa | Iustin Pop | |
4453 | bd315bfa | Iustin Pop | |
4454 | decd5f45 | Iustin Pop | class LURenameInstance(LogicalUnit): |
4455 | decd5f45 | Iustin Pop | """Rename an instance.
|
4456 | decd5f45 | Iustin Pop |
|
4457 | decd5f45 | Iustin Pop | """
|
4458 | decd5f45 | Iustin Pop | HPATH = "instance-rename"
|
4459 | decd5f45 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4460 | decd5f45 | Iustin Pop | _OP_REQP = ["instance_name", "new_name"] |
4461 | decd5f45 | Iustin Pop | |
4462 | decd5f45 | Iustin Pop | def BuildHooksEnv(self): |
4463 | decd5f45 | Iustin Pop | """Build hooks env.
|
4464 | decd5f45 | Iustin Pop |
|
4465 | decd5f45 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4466 | decd5f45 | Iustin Pop |
|
4467 | decd5f45 | Iustin Pop | """
|
4468 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
4469 | decd5f45 | Iustin Pop | env["INSTANCE_NEW_NAME"] = self.op.new_name |
4470 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
4471 | decd5f45 | Iustin Pop | return env, nl, nl
|
4472 | decd5f45 | Iustin Pop | |
4473 | decd5f45 | Iustin Pop | def CheckPrereq(self): |
4474 | decd5f45 | Iustin Pop | """Check prerequisites.
|
4475 | decd5f45 | Iustin Pop |
|
4476 | decd5f45 | Iustin Pop | This checks that the instance is in the cluster and is not running.
|
4477 | decd5f45 | Iustin Pop |
|
4478 | decd5f45 | Iustin Pop | """
|
4479 | cf26a87a | Iustin Pop | self.op.instance_name = _ExpandInstanceName(self.cfg, |
4480 | cf26a87a | Iustin Pop | self.op.instance_name)
|
4481 | cf26a87a | Iustin Pop | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4482 | cf26a87a | Iustin Pop | assert instance is not None |
4483 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
4484 | 31624382 | Iustin Pop | _CheckInstanceDown(self, instance, "cannot rename") |
4485 | decd5f45 | Iustin Pop | self.instance = instance
|
4486 | decd5f45 | Iustin Pop | |
4487 | decd5f45 | Iustin Pop | # new name verification
|
4488 | 104f4ca1 | Iustin Pop | name_info = utils.GetHostInfo(self.op.new_name)
|
4489 | decd5f45 | Iustin Pop | |
4490 | 89e1fc26 | Iustin Pop | self.op.new_name = new_name = name_info.name
|
4491 | 7bde3275 | Guido Trotter | instance_list = self.cfg.GetInstanceList()
|
4492 | 7bde3275 | Guido Trotter | if new_name in instance_list: |
4493 | 7bde3275 | Guido Trotter | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
4494 | 5c983ee5 | Iustin Pop | new_name, errors.ECODE_EXISTS) |
4495 | 7bde3275 | Guido Trotter | |
4496 | decd5f45 | Iustin Pop | if not getattr(self.op, "ignore_ip", False): |
4497 | 937f983d | Guido Trotter | if utils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
|
4498 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
4499 | 5c983ee5 | Iustin Pop | (name_info.ip, new_name), |
4500 | 5c983ee5 | Iustin Pop | errors.ECODE_NOTUNIQUE) |
4501 | decd5f45 | Iustin Pop | |
4502 | decd5f45 | Iustin Pop | |
4503 | decd5f45 | Iustin Pop | def Exec(self, feedback_fn): |
4504 | decd5f45 | Iustin Pop | """Reinstall the instance.
|
4505 | decd5f45 | Iustin Pop |
|
4506 | decd5f45 | Iustin Pop | """
|
4507 | decd5f45 | Iustin Pop | inst = self.instance
|
4508 | decd5f45 | Iustin Pop | old_name = inst.name |
4509 | decd5f45 | Iustin Pop | |
4510 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
4511 | b23c4333 | Manuel Franceschini | old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
4512 | b23c4333 | Manuel Franceschini | |
4513 | decd5f45 | Iustin Pop | self.cfg.RenameInstance(inst.name, self.op.new_name) |
4514 | 74b5913f | Guido Trotter | # Change the instance lock. This is definitely safe while we hold the BGL
|
4515 | cb4e8387 | Iustin Pop | self.context.glm.remove(locking.LEVEL_INSTANCE, old_name)
|
4516 | 74b5913f | Guido Trotter | self.context.glm.add(locking.LEVEL_INSTANCE, self.op.new_name) |
4517 | decd5f45 | Iustin Pop | |
4518 | decd5f45 | Iustin Pop | # re-read the instance from the configuration after rename
|
4519 | decd5f45 | Iustin Pop | inst = self.cfg.GetInstanceInfo(self.op.new_name) |
4520 | decd5f45 | Iustin Pop | |
4521 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
4522 | b23c4333 | Manuel Franceschini | new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
4523 | 72737a7f | Iustin Pop | result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
|
4524 | 72737a7f | Iustin Pop | old_file_storage_dir, |
4525 | 72737a7f | Iustin Pop | new_file_storage_dir) |
4526 | 4c4e4e1e | Iustin Pop | result.Raise("Could not rename on node %s directory '%s' to '%s'"
|
4527 | 4c4e4e1e | Iustin Pop | " (but the instance has been renamed in Ganeti)" %
|
4528 | 4c4e4e1e | Iustin Pop | (inst.primary_node, old_file_storage_dir, |
4529 | 4c4e4e1e | Iustin Pop | new_file_storage_dir)) |
4530 | b23c4333 | Manuel Franceschini | |
4531 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, inst, None) |
4532 | decd5f45 | Iustin Pop | try:
|
4533 | 781de953 | Iustin Pop | result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
|
4534 | dd713605 | Iustin Pop | old_name, self.op.debug_level)
|
4535 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4536 | 96841384 | Iustin Pop | if msg:
|
4537 | 6291574d | Alexander Schreiber | msg = ("Could not run OS rename script for instance %s on node %s"
|
4538 | 96841384 | Iustin Pop | " (but the instance has been renamed in Ganeti): %s" %
|
4539 | 96841384 | Iustin Pop | (inst.name, inst.primary_node, msg)) |
4540 | 86d9d3bb | Iustin Pop | self.proc.LogWarning(msg)
|
4541 | decd5f45 | Iustin Pop | finally:
|
4542 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, inst)
|
4543 | decd5f45 | Iustin Pop | |
4544 | decd5f45 | Iustin Pop | |
4545 | a8083063 | Iustin Pop | class LURemoveInstance(LogicalUnit): |
4546 | a8083063 | Iustin Pop | """Remove an instance.
|
4547 | a8083063 | Iustin Pop |
|
4548 | a8083063 | Iustin Pop | """
|
4549 | a8083063 | Iustin Pop | HPATH = "instance-remove"
|
4550 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4551 | 5c54b832 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_failures"] |
4552 | cf472233 | Guido Trotter | REQ_BGL = False
|
4553 | cf472233 | Guido Trotter | |
4554 | 17c3f802 | Guido Trotter | def CheckArguments(self): |
4555 | 17c3f802 | Guido Trotter | """Check the arguments.
|
4556 | 17c3f802 | Guido Trotter |
|
4557 | 17c3f802 | Guido Trotter | """
|
4558 | 17c3f802 | Guido Trotter | self.shutdown_timeout = getattr(self.op, "shutdown_timeout", |
4559 | 17c3f802 | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
4560 | 17c3f802 | Guido Trotter | |
4561 | cf472233 | Guido Trotter | def ExpandNames(self): |
4562 | cf472233 | Guido Trotter | self._ExpandAndLockInstance()
|
4563 | cf472233 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
4564 | cf472233 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4565 | cf472233 | Guido Trotter | |
4566 | cf472233 | Guido Trotter | def DeclareLocks(self, level): |
4567 | cf472233 | Guido Trotter | if level == locking.LEVEL_NODE:
|
4568 | cf472233 | Guido Trotter | self._LockInstancesNodes()
|
4569 | a8083063 | Iustin Pop | |
4570 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4571 | a8083063 | Iustin Pop | """Build hooks env.
|
4572 | a8083063 | Iustin Pop |
|
4573 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4574 | a8083063 | Iustin Pop |
|
4575 | a8083063 | Iustin Pop | """
|
4576 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
4577 | 17c3f802 | Guido Trotter | env["SHUTDOWN_TIMEOUT"] = self.shutdown_timeout |
4578 | d6a02168 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
4579 | abd8e836 | Iustin Pop | nl_post = list(self.instance.all_nodes) + nl |
4580 | abd8e836 | Iustin Pop | return env, nl, nl_post
|
4581 | a8083063 | Iustin Pop | |
4582 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4583 | a8083063 | Iustin Pop | """Check prerequisites.
|
4584 | a8083063 | Iustin Pop |
|
4585 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
4586 | a8083063 | Iustin Pop |
|
4587 | a8083063 | Iustin Pop | """
|
4588 | cf472233 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4589 | cf472233 | Guido Trotter | assert self.instance is not None, \ |
4590 | cf472233 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4591 | a8083063 | Iustin Pop | |
4592 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4593 | a8083063 | Iustin Pop | """Remove the instance.
|
4594 | a8083063 | Iustin Pop |
|
4595 | a8083063 | Iustin Pop | """
|
4596 | a8083063 | Iustin Pop | instance = self.instance
|
4597 | 9a4f63d1 | Iustin Pop | logging.info("Shutting down instance %s on node %s",
|
4598 | 9a4f63d1 | Iustin Pop | instance.name, instance.primary_node) |
4599 | a8083063 | Iustin Pop | |
4600 | 17c3f802 | Guido Trotter | result = self.rpc.call_instance_shutdown(instance.primary_node, instance,
|
4601 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
4602 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4603 | 1fae010f | Iustin Pop | if msg:
|
4604 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
4605 | 1fae010f | Iustin Pop | feedback_fn("Warning: can't shutdown instance: %s" % msg)
|
4606 | 1d67656e | Iustin Pop | else:
|
4607 | 1fae010f | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
4608 | 1fae010f | Iustin Pop | " node %s: %s" %
|
4609 | 1fae010f | Iustin Pop | (instance.name, instance.primary_node, msg)) |
4610 | a8083063 | Iustin Pop | |
4611 | 9a4f63d1 | Iustin Pop | logging.info("Removing block devices for instance %s", instance.name)
|
4612 | a8083063 | Iustin Pop | |
4613 | b9bddb6b | Iustin Pop | if not _RemoveDisks(self, instance): |
4614 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
4615 | 1d67656e | Iustin Pop | feedback_fn("Warning: can't remove instance's disks")
|
4616 | 1d67656e | Iustin Pop | else:
|
4617 | 1d67656e | Iustin Pop | raise errors.OpExecError("Can't remove instance's disks") |
4618 | a8083063 | Iustin Pop | |
4619 | 9a4f63d1 | Iustin Pop | logging.info("Removing instance %s out of cluster config", instance.name)
|
4620 | a8083063 | Iustin Pop | |
4621 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(instance.name)
|
4622 | cf472233 | Guido Trotter | self.remove_locks[locking.LEVEL_INSTANCE] = instance.name
|
4623 | a8083063 | Iustin Pop | |
4624 | a8083063 | Iustin Pop | |
4625 | a8083063 | Iustin Pop | class LUQueryInstances(NoHooksLU): |
4626 | a8083063 | Iustin Pop | """Logical unit for querying instances.
|
4627 | a8083063 | Iustin Pop |
|
4628 | a8083063 | Iustin Pop | """
|
4629 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0142
|
4630 | ec79568d | Iustin Pop | _OP_REQP = ["output_fields", "names", "use_locking"] |
4631 | 7eb9d8f7 | Guido Trotter | REQ_BGL = False
|
4632 | 19bed813 | Iustin Pop | _SIMPLE_FIELDS = ["name", "os", "network_port", "hypervisor", |
4633 | 19bed813 | Iustin Pop | "serial_no", "ctime", "mtime", "uuid"] |
4634 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet(*["name", "os", "pnode", "snodes", |
4635 | 5b460366 | Iustin Pop | "admin_state",
|
4636 | a2d2e1a7 | Iustin Pop | "disk_template", "ip", "mac", "bridge", |
4637 | 638c6349 | Guido Trotter | "nic_mode", "nic_link", |
4638 | a2d2e1a7 | Iustin Pop | "sda_size", "sdb_size", "vcpus", "tags", |
4639 | a2d2e1a7 | Iustin Pop | "network_port", "beparams", |
4640 | 8aec325c | Iustin Pop | r"(disk)\.(size)/([0-9]+)",
|
4641 | 8aec325c | Iustin Pop | r"(disk)\.(sizes)", "disk_usage", |
4642 | 638c6349 | Guido Trotter | r"(nic)\.(mac|ip|mode|link)/([0-9]+)",
|
4643 | 638c6349 | Guido Trotter | r"(nic)\.(bridge)/([0-9]+)",
|
4644 | 638c6349 | Guido Trotter | r"(nic)\.(macs|ips|modes|links|bridges)",
|
4645 | 8aec325c | Iustin Pop | r"(disk|nic)\.(count)",
|
4646 | 19bed813 | Iustin Pop | "hvparams",
|
4647 | 19bed813 | Iustin Pop | ] + _SIMPLE_FIELDS + |
4648 | a2d2e1a7 | Iustin Pop | ["hv/%s" % name
|
4649 | 7736a5f2 | Iustin Pop | for name in constants.HVS_PARAMETERS |
4650 | 7736a5f2 | Iustin Pop | if name not in constants.HVC_GLOBALS] + |
4651 | a2d2e1a7 | Iustin Pop | ["be/%s" % name
|
4652 | a2d2e1a7 | Iustin Pop | for name in constants.BES_PARAMETERS]) |
4653 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet("oper_state", "oper_ram", "status") |
4654 | 31bf511f | Iustin Pop | |
4655 | a8083063 | Iustin Pop | |
4656 | 7eb9d8f7 | Guido Trotter | def ExpandNames(self): |
4657 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
4658 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
4659 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
4660 | a8083063 | Iustin Pop | |
4661 | 7eb9d8f7 | Guido Trotter | self.needed_locks = {}
|
4662 | 7eb9d8f7 | Guido Trotter | self.share_locks[locking.LEVEL_INSTANCE] = 1 |
4663 | 7eb9d8f7 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
4664 | 7eb9d8f7 | Guido Trotter | |
4665 | 57a2fb91 | Iustin Pop | if self.op.names: |
4666 | 57a2fb91 | Iustin Pop | self.wanted = _GetWantedInstances(self, self.op.names) |
4667 | 7eb9d8f7 | Guido Trotter | else:
|
4668 | 57a2fb91 | Iustin Pop | self.wanted = locking.ALL_SET
|
4669 | 7eb9d8f7 | Guido Trotter | |
4670 | ec79568d | Iustin Pop | self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields) |
4671 | ec79568d | Iustin Pop | self.do_locking = self.do_node_query and self.op.use_locking |
4672 | 57a2fb91 | Iustin Pop | if self.do_locking: |
4673 | 57a2fb91 | Iustin Pop | self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted |
4674 | 57a2fb91 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
4675 | 57a2fb91 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4676 | 7eb9d8f7 | Guido Trotter | |
4677 | 7eb9d8f7 | Guido Trotter | def DeclareLocks(self, level): |
4678 | 57a2fb91 | Iustin Pop | if level == locking.LEVEL_NODE and self.do_locking: |
4679 | 7eb9d8f7 | Guido Trotter | self._LockInstancesNodes()
|
4680 | 7eb9d8f7 | Guido Trotter | |
4681 | 7eb9d8f7 | Guido Trotter | def CheckPrereq(self): |
4682 | 7eb9d8f7 | Guido Trotter | """Check prerequisites.
|
4683 | 7eb9d8f7 | Guido Trotter |
|
4684 | 7eb9d8f7 | Guido Trotter | """
|
4685 | 57a2fb91 | Iustin Pop | pass
|
4686 | 069dcc86 | Iustin Pop | |
4687 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4688 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
4689 | a8083063 | Iustin Pop |
|
4690 | a8083063 | Iustin Pop | """
|
4691 | 7260cfbe | Iustin Pop | # pylint: disable-msg=R0912
|
4692 | 7260cfbe | Iustin Pop | # way too many branches here
|
4693 | 57a2fb91 | Iustin Pop | all_info = self.cfg.GetAllInstancesInfo()
|
4694 | a7f5dc98 | Iustin Pop | if self.wanted == locking.ALL_SET: |
4695 | a7f5dc98 | Iustin Pop | # caller didn't specify instance names, so ordering is not important
|
4696 | a7f5dc98 | Iustin Pop | if self.do_locking: |
4697 | a7f5dc98 | Iustin Pop | instance_names = self.acquired_locks[locking.LEVEL_INSTANCE]
|
4698 | a7f5dc98 | Iustin Pop | else:
|
4699 | a7f5dc98 | Iustin Pop | instance_names = all_info.keys() |
4700 | a7f5dc98 | Iustin Pop | instance_names = utils.NiceSort(instance_names) |
4701 | 57a2fb91 | Iustin Pop | else:
|
4702 | a7f5dc98 | Iustin Pop | # caller did specify names, so we must keep the ordering
|
4703 | a7f5dc98 | Iustin Pop | if self.do_locking: |
4704 | a7f5dc98 | Iustin Pop | tgt_set = self.acquired_locks[locking.LEVEL_INSTANCE]
|
4705 | a7f5dc98 | Iustin Pop | else:
|
4706 | a7f5dc98 | Iustin Pop | tgt_set = all_info.keys() |
4707 | a7f5dc98 | Iustin Pop | missing = set(self.wanted).difference(tgt_set) |
4708 | a7f5dc98 | Iustin Pop | if missing:
|
4709 | a7f5dc98 | Iustin Pop | raise errors.OpExecError("Some instances were removed before" |
4710 | a7f5dc98 | Iustin Pop | " retrieving their data: %s" % missing)
|
4711 | a7f5dc98 | Iustin Pop | instance_names = self.wanted
|
4712 | c1f1cbb2 | Iustin Pop | |
4713 | 57a2fb91 | Iustin Pop | instance_list = [all_info[iname] for iname in instance_names] |
4714 | a8083063 | Iustin Pop | |
4715 | a8083063 | Iustin Pop | # begin data gathering
|
4716 | a8083063 | Iustin Pop | |
4717 | a8083063 | Iustin Pop | nodes = frozenset([inst.primary_node for inst in instance_list]) |
4718 | e69d05fd | Iustin Pop | hv_list = list(set([inst.hypervisor for inst in instance_list])) |
4719 | a8083063 | Iustin Pop | |
4720 | a8083063 | Iustin Pop | bad_nodes = [] |
4721 | cbfc4681 | Iustin Pop | off_nodes = [] |
4722 | ec79568d | Iustin Pop | if self.do_node_query: |
4723 | a8083063 | Iustin Pop | live_data = {} |
4724 | 72737a7f | Iustin Pop | node_data = self.rpc.call_all_instances_info(nodes, hv_list)
|
4725 | a8083063 | Iustin Pop | for name in nodes: |
4726 | a8083063 | Iustin Pop | result = node_data[name] |
4727 | cbfc4681 | Iustin Pop | if result.offline:
|
4728 | cbfc4681 | Iustin Pop | # offline nodes will be in both lists
|
4729 | cbfc4681 | Iustin Pop | off_nodes.append(name) |
4730 | 3cebe102 | Michael Hanselmann | if result.fail_msg:
|
4731 | a8083063 | Iustin Pop | bad_nodes.append(name) |
4732 | 781de953 | Iustin Pop | else:
|
4733 | 2fa74ef4 | Iustin Pop | if result.payload:
|
4734 | 2fa74ef4 | Iustin Pop | live_data.update(result.payload) |
4735 | 2fa74ef4 | Iustin Pop | # else no instance is alive
|
4736 | a8083063 | Iustin Pop | else:
|
4737 | a8083063 | Iustin Pop | live_data = dict([(name, {}) for name in instance_names]) |
4738 | a8083063 | Iustin Pop | |
4739 | a8083063 | Iustin Pop | # end data gathering
|
4740 | a8083063 | Iustin Pop | |
4741 | 5018a335 | Iustin Pop | HVPREFIX = "hv/"
|
4742 | 338e51e8 | Iustin Pop | BEPREFIX = "be/"
|
4743 | a8083063 | Iustin Pop | output = [] |
4744 | 638c6349 | Guido Trotter | cluster = self.cfg.GetClusterInfo()
|
4745 | a8083063 | Iustin Pop | for instance in instance_list: |
4746 | a8083063 | Iustin Pop | iout = [] |
4747 | 7736a5f2 | Iustin Pop | i_hv = cluster.FillHV(instance, skip_globals=True)
|
4748 | 638c6349 | Guido Trotter | i_be = cluster.FillBE(instance) |
4749 | 638c6349 | Guido Trotter | i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], |
4750 | 638c6349 | Guido Trotter | nic.nicparams) for nic in instance.nics] |
4751 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
4752 | 71c1af58 | Iustin Pop | st_match = self._FIELDS_STATIC.Matches(field)
|
4753 | 19bed813 | Iustin Pop | if field in self._SIMPLE_FIELDS: |
4754 | 19bed813 | Iustin Pop | val = getattr(instance, field)
|
4755 | a8083063 | Iustin Pop | elif field == "pnode": |
4756 | a8083063 | Iustin Pop | val = instance.primary_node |
4757 | a8083063 | Iustin Pop | elif field == "snodes": |
4758 | 8a23d2d3 | Iustin Pop | val = list(instance.secondary_nodes)
|
4759 | a8083063 | Iustin Pop | elif field == "admin_state": |
4760 | 0d68c45d | Iustin Pop | val = instance.admin_up |
4761 | a8083063 | Iustin Pop | elif field == "oper_state": |
4762 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
4763 | 8a23d2d3 | Iustin Pop | val = None
|
4764 | a8083063 | Iustin Pop | else:
|
4765 | 8a23d2d3 | Iustin Pop | val = bool(live_data.get(instance.name))
|
4766 | d8052456 | Iustin Pop | elif field == "status": |
4767 | cbfc4681 | Iustin Pop | if instance.primary_node in off_nodes: |
4768 | cbfc4681 | Iustin Pop | val = "ERROR_nodeoffline"
|
4769 | cbfc4681 | Iustin Pop | elif instance.primary_node in bad_nodes: |
4770 | d8052456 | Iustin Pop | val = "ERROR_nodedown"
|
4771 | d8052456 | Iustin Pop | else:
|
4772 | d8052456 | Iustin Pop | running = bool(live_data.get(instance.name))
|
4773 | d8052456 | Iustin Pop | if running:
|
4774 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
4775 | d8052456 | Iustin Pop | val = "running"
|
4776 | d8052456 | Iustin Pop | else:
|
4777 | d8052456 | Iustin Pop | val = "ERROR_up"
|
4778 | d8052456 | Iustin Pop | else:
|
4779 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
4780 | d8052456 | Iustin Pop | val = "ERROR_down"
|
4781 | d8052456 | Iustin Pop | else:
|
4782 | d8052456 | Iustin Pop | val = "ADMIN_down"
|
4783 | a8083063 | Iustin Pop | elif field == "oper_ram": |
4784 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
4785 | 8a23d2d3 | Iustin Pop | val = None
|
4786 | a8083063 | Iustin Pop | elif instance.name in live_data: |
4787 | a8083063 | Iustin Pop | val = live_data[instance.name].get("memory", "?") |
4788 | a8083063 | Iustin Pop | else:
|
4789 | a8083063 | Iustin Pop | val = "-"
|
4790 | c1ce76bb | Iustin Pop | elif field == "vcpus": |
4791 | c1ce76bb | Iustin Pop | val = i_be[constants.BE_VCPUS] |
4792 | a8083063 | Iustin Pop | elif field == "disk_template": |
4793 | a8083063 | Iustin Pop | val = instance.disk_template |
4794 | a8083063 | Iustin Pop | elif field == "ip": |
4795 | 39a02558 | Guido Trotter | if instance.nics:
|
4796 | 39a02558 | Guido Trotter | val = instance.nics[0].ip
|
4797 | 39a02558 | Guido Trotter | else:
|
4798 | 39a02558 | Guido Trotter | val = None
|
4799 | 638c6349 | Guido Trotter | elif field == "nic_mode": |
4800 | 638c6349 | Guido Trotter | if instance.nics:
|
4801 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_MODE]
|
4802 | 638c6349 | Guido Trotter | else:
|
4803 | 638c6349 | Guido Trotter | val = None
|
4804 | 638c6349 | Guido Trotter | elif field == "nic_link": |
4805 | 39a02558 | Guido Trotter | if instance.nics:
|
4806 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_LINK]
|
4807 | 638c6349 | Guido Trotter | else:
|
4808 | 638c6349 | Guido Trotter | val = None
|
4809 | 638c6349 | Guido Trotter | elif field == "bridge": |
4810 | 638c6349 | Guido Trotter | if (instance.nics and |
4811 | 638c6349 | Guido Trotter | i_nicp[0][constants.NIC_MODE] == constants.NIC_MODE_BRIDGED):
|
4812 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_LINK]
|
4813 | 39a02558 | Guido Trotter | else:
|
4814 | 39a02558 | Guido Trotter | val = None
|
4815 | a8083063 | Iustin Pop | elif field == "mac": |
4816 | 39a02558 | Guido Trotter | if instance.nics:
|
4817 | 39a02558 | Guido Trotter | val = instance.nics[0].mac
|
4818 | 39a02558 | Guido Trotter | else:
|
4819 | 39a02558 | Guido Trotter | val = None
|
4820 | 644eeef9 | Iustin Pop | elif field == "sda_size" or field == "sdb_size": |
4821 | ad24e046 | Iustin Pop | idx = ord(field[2]) - ord('a') |
4822 | ad24e046 | Iustin Pop | try:
|
4823 | ad24e046 | Iustin Pop | val = instance.FindDisk(idx).size |
4824 | ad24e046 | Iustin Pop | except errors.OpPrereqError:
|
4825 | 8a23d2d3 | Iustin Pop | val = None
|
4826 | 024e157f | Iustin Pop | elif field == "disk_usage": # total disk usage per node |
4827 | 024e157f | Iustin Pop | disk_sizes = [{'size': disk.size} for disk in instance.disks] |
4828 | 024e157f | Iustin Pop | val = _ComputeDiskSize(instance.disk_template, disk_sizes) |
4829 | 130a6a6f | Iustin Pop | elif field == "tags": |
4830 | 130a6a6f | Iustin Pop | val = list(instance.GetTags())
|
4831 | 338e51e8 | Iustin Pop | elif field == "hvparams": |
4832 | 338e51e8 | Iustin Pop | val = i_hv |
4833 | 5018a335 | Iustin Pop | elif (field.startswith(HVPREFIX) and |
4834 | 7736a5f2 | Iustin Pop | field[len(HVPREFIX):] in constants.HVS_PARAMETERS and |
4835 | 7736a5f2 | Iustin Pop | field[len(HVPREFIX):] not in constants.HVC_GLOBALS): |
4836 | 5018a335 | Iustin Pop | val = i_hv.get(field[len(HVPREFIX):], None) |
4837 | 338e51e8 | Iustin Pop | elif field == "beparams": |
4838 | 338e51e8 | Iustin Pop | val = i_be |
4839 | 338e51e8 | Iustin Pop | elif (field.startswith(BEPREFIX) and |
4840 | 338e51e8 | Iustin Pop | field[len(BEPREFIX):] in constants.BES_PARAMETERS): |
4841 | 338e51e8 | Iustin Pop | val = i_be.get(field[len(BEPREFIX):], None) |
4842 | 71c1af58 | Iustin Pop | elif st_match and st_match.groups(): |
4843 | 71c1af58 | Iustin Pop | # matches a variable list
|
4844 | 71c1af58 | Iustin Pop | st_groups = st_match.groups() |
4845 | 71c1af58 | Iustin Pop | if st_groups and st_groups[0] == "disk": |
4846 | 71c1af58 | Iustin Pop | if st_groups[1] == "count": |
4847 | 71c1af58 | Iustin Pop | val = len(instance.disks)
|
4848 | 41a776da | Iustin Pop | elif st_groups[1] == "sizes": |
4849 | 41a776da | Iustin Pop | val = [disk.size for disk in instance.disks] |
4850 | 71c1af58 | Iustin Pop | elif st_groups[1] == "size": |
4851 | 3e0cea06 | Iustin Pop | try:
|
4852 | 3e0cea06 | Iustin Pop | val = instance.FindDisk(st_groups[2]).size
|
4853 | 3e0cea06 | Iustin Pop | except errors.OpPrereqError:
|
4854 | 71c1af58 | Iustin Pop | val = None
|
4855 | 71c1af58 | Iustin Pop | else:
|
4856 | 71c1af58 | Iustin Pop | assert False, "Unhandled disk parameter" |
4857 | 71c1af58 | Iustin Pop | elif st_groups[0] == "nic": |
4858 | 71c1af58 | Iustin Pop | if st_groups[1] == "count": |
4859 | 71c1af58 | Iustin Pop | val = len(instance.nics)
|
4860 | 41a776da | Iustin Pop | elif st_groups[1] == "macs": |
4861 | 41a776da | Iustin Pop | val = [nic.mac for nic in instance.nics] |
4862 | 41a776da | Iustin Pop | elif st_groups[1] == "ips": |
4863 | 41a776da | Iustin Pop | val = [nic.ip for nic in instance.nics] |
4864 | 638c6349 | Guido Trotter | elif st_groups[1] == "modes": |
4865 | 638c6349 | Guido Trotter | val = [nicp[constants.NIC_MODE] for nicp in i_nicp] |
4866 | 638c6349 | Guido Trotter | elif st_groups[1] == "links": |
4867 | 638c6349 | Guido Trotter | val = [nicp[constants.NIC_LINK] for nicp in i_nicp] |
4868 | 41a776da | Iustin Pop | elif st_groups[1] == "bridges": |
4869 | 638c6349 | Guido Trotter | val = [] |
4870 | 638c6349 | Guido Trotter | for nicp in i_nicp: |
4871 | 638c6349 | Guido Trotter | if nicp[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
4872 | 638c6349 | Guido Trotter | val.append(nicp[constants.NIC_LINK]) |
4873 | 638c6349 | Guido Trotter | else:
|
4874 | 638c6349 | Guido Trotter | val.append(None)
|
4875 | 71c1af58 | Iustin Pop | else:
|
4876 | 71c1af58 | Iustin Pop | # index-based item
|
4877 | 71c1af58 | Iustin Pop | nic_idx = int(st_groups[2]) |
4878 | 71c1af58 | Iustin Pop | if nic_idx >= len(instance.nics): |
4879 | 71c1af58 | Iustin Pop | val = None
|
4880 | 71c1af58 | Iustin Pop | else:
|
4881 | 71c1af58 | Iustin Pop | if st_groups[1] == "mac": |
4882 | 71c1af58 | Iustin Pop | val = instance.nics[nic_idx].mac |
4883 | 71c1af58 | Iustin Pop | elif st_groups[1] == "ip": |
4884 | 71c1af58 | Iustin Pop | val = instance.nics[nic_idx].ip |
4885 | 638c6349 | Guido Trotter | elif st_groups[1] == "mode": |
4886 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_MODE] |
4887 | 638c6349 | Guido Trotter | elif st_groups[1] == "link": |
4888 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_LINK] |
4889 | 71c1af58 | Iustin Pop | elif st_groups[1] == "bridge": |
4890 | 638c6349 | Guido Trotter | nic_mode = i_nicp[nic_idx][constants.NIC_MODE] |
4891 | 638c6349 | Guido Trotter | if nic_mode == constants.NIC_MODE_BRIDGED:
|
4892 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_LINK] |
4893 | 638c6349 | Guido Trotter | else:
|
4894 | 638c6349 | Guido Trotter | val = None
|
4895 | 71c1af58 | Iustin Pop | else:
|
4896 | 71c1af58 | Iustin Pop | assert False, "Unhandled NIC parameter" |
4897 | 71c1af58 | Iustin Pop | else:
|
4898 | c1ce76bb | Iustin Pop | assert False, ("Declared but unhandled variable parameter '%s'" % |
4899 | c1ce76bb | Iustin Pop | field) |
4900 | a8083063 | Iustin Pop | else:
|
4901 | c1ce76bb | Iustin Pop | assert False, "Declared but unhandled parameter '%s'" % field |
4902 | a8083063 | Iustin Pop | iout.append(val) |
4903 | a8083063 | Iustin Pop | output.append(iout) |
4904 | a8083063 | Iustin Pop | |
4905 | a8083063 | Iustin Pop | return output
|
4906 | a8083063 | Iustin Pop | |
4907 | a8083063 | Iustin Pop | |
4908 | a8083063 | Iustin Pop | class LUFailoverInstance(LogicalUnit): |
4909 | a8083063 | Iustin Pop | """Failover an instance.
|
4910 | a8083063 | Iustin Pop |
|
4911 | a8083063 | Iustin Pop | """
|
4912 | a8083063 | Iustin Pop | HPATH = "instance-failover"
|
4913 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4914 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_consistency"] |
4915 | c9e5c064 | Guido Trotter | REQ_BGL = False
|
4916 | c9e5c064 | Guido Trotter | |
4917 | 17c3f802 | Guido Trotter | def CheckArguments(self): |
4918 | 17c3f802 | Guido Trotter | """Check the arguments.
|
4919 | 17c3f802 | Guido Trotter |
|
4920 | 17c3f802 | Guido Trotter | """
|
4921 | 17c3f802 | Guido Trotter | self.shutdown_timeout = getattr(self.op, "shutdown_timeout", |
4922 | 17c3f802 | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
4923 | 17c3f802 | Guido Trotter | |
4924 | c9e5c064 | Guido Trotter | def ExpandNames(self): |
4925 | c9e5c064 | Guido Trotter | self._ExpandAndLockInstance()
|
4926 | c9e5c064 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
4927 | f6d9a522 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4928 | c9e5c064 | Guido Trotter | |
4929 | c9e5c064 | Guido Trotter | def DeclareLocks(self, level): |
4930 | c9e5c064 | Guido Trotter | if level == locking.LEVEL_NODE:
|
4931 | c9e5c064 | Guido Trotter | self._LockInstancesNodes()
|
4932 | a8083063 | Iustin Pop | |
4933 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4934 | a8083063 | Iustin Pop | """Build hooks env.
|
4935 | a8083063 | Iustin Pop |
|
4936 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4937 | a8083063 | Iustin Pop |
|
4938 | a8083063 | Iustin Pop | """
|
4939 | 08eec276 | Iustin Pop | instance = self.instance
|
4940 | 08eec276 | Iustin Pop | source_node = instance.primary_node |
4941 | 08eec276 | Iustin Pop | target_node = instance.secondary_nodes[0]
|
4942 | a8083063 | Iustin Pop | env = { |
4943 | a8083063 | Iustin Pop | "IGNORE_CONSISTENCY": self.op.ignore_consistency, |
4944 | 17c3f802 | Guido Trotter | "SHUTDOWN_TIMEOUT": self.shutdown_timeout, |
4945 | 08eec276 | Iustin Pop | "OLD_PRIMARY": source_node,
|
4946 | 08eec276 | Iustin Pop | "OLD_SECONDARY": target_node,
|
4947 | 08eec276 | Iustin Pop | "NEW_PRIMARY": target_node,
|
4948 | 08eec276 | Iustin Pop | "NEW_SECONDARY": source_node,
|
4949 | a8083063 | Iustin Pop | } |
4950 | 08eec276 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, instance))
|
4951 | 08eec276 | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes) |
4952 | abd8e836 | Iustin Pop | nl_post = list(nl)
|
4953 | abd8e836 | Iustin Pop | nl_post.append(source_node) |
4954 | abd8e836 | Iustin Pop | return env, nl, nl_post
|
4955 | a8083063 | Iustin Pop | |
4956 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4957 | a8083063 | Iustin Pop | """Check prerequisites.
|
4958 | a8083063 | Iustin Pop |
|
4959 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
4960 | a8083063 | Iustin Pop |
|
4961 | a8083063 | Iustin Pop | """
|
4962 | c9e5c064 | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4963 | c9e5c064 | Guido Trotter | assert self.instance is not None, \ |
4964 | c9e5c064 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4965 | a8083063 | Iustin Pop | |
4966 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
4967 | a1f445d3 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
4968 | 2a710df1 | Michael Hanselmann | raise errors.OpPrereqError("Instance's disk layout is not" |
4969 | 5c983ee5 | Iustin Pop | " network mirrored, cannot failover.",
|
4970 | 5c983ee5 | Iustin Pop | errors.ECODE_STATE) |
4971 | 2a710df1 | Michael Hanselmann | |
4972 | 2a710df1 | Michael Hanselmann | secondary_nodes = instance.secondary_nodes |
4973 | 2a710df1 | Michael Hanselmann | if not secondary_nodes: |
4974 | 2a710df1 | Michael Hanselmann | raise errors.ProgrammerError("no secondary node but using " |
4975 | abdf0113 | Iustin Pop | "a mirrored disk template")
|
4976 | 2a710df1 | Michael Hanselmann | |
4977 | 2a710df1 | Michael Hanselmann | target_node = secondary_nodes[0]
|
4978 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, target_node)
|
4979 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
4980 | d27776f0 | Iustin Pop | if instance.admin_up:
|
4981 | d27776f0 | Iustin Pop | # check memory requirements on the secondary node
|
4982 | d27776f0 | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "failing over instance %s" % |
4983 | d27776f0 | Iustin Pop | instance.name, bep[constants.BE_MEMORY], |
4984 | d27776f0 | Iustin Pop | instance.hypervisor) |
4985 | d27776f0 | Iustin Pop | else:
|
4986 | d27776f0 | Iustin Pop | self.LogInfo("Not checking memory on the secondary node as" |
4987 | d27776f0 | Iustin Pop | " instance will not be started")
|
4988 | 3a7c308e | Guido Trotter | |
4989 | a8083063 | Iustin Pop | # check bridge existance
|
4990 | b165e77e | Guido Trotter | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
4991 | a8083063 | Iustin Pop | |
4992 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4993 | a8083063 | Iustin Pop | """Failover an instance.
|
4994 | a8083063 | Iustin Pop |
|
4995 | a8083063 | Iustin Pop | The failover is done by shutting it down on its present node and
|
4996 | a8083063 | Iustin Pop | starting it on the secondary.
|
4997 | a8083063 | Iustin Pop |
|
4998 | a8083063 | Iustin Pop | """
|
4999 | a8083063 | Iustin Pop | instance = self.instance
|
5000 | a8083063 | Iustin Pop | |
5001 | a8083063 | Iustin Pop | source_node = instance.primary_node |
5002 | a8083063 | Iustin Pop | target_node = instance.secondary_nodes[0]
|
5003 | a8083063 | Iustin Pop | |
5004 | 1df79ce6 | Michael Hanselmann | if instance.admin_up:
|
5005 | 1df79ce6 | Michael Hanselmann | feedback_fn("* checking disk consistency between source and target")
|
5006 | 1df79ce6 | Michael Hanselmann | for dev in instance.disks: |
5007 | 1df79ce6 | Michael Hanselmann | # for drbd, these are drbd over lvm
|
5008 | 1df79ce6 | Michael Hanselmann | if not _CheckDiskConsistency(self, dev, target_node, False): |
5009 | 1df79ce6 | Michael Hanselmann | if not self.op.ignore_consistency: |
5010 | 1df79ce6 | Michael Hanselmann | raise errors.OpExecError("Disk %s is degraded on target node," |
5011 | 1df79ce6 | Michael Hanselmann | " aborting failover." % dev.iv_name)
|
5012 | 1df79ce6 | Michael Hanselmann | else:
|
5013 | 1df79ce6 | Michael Hanselmann | feedback_fn("* not checking disk consistency as instance is not running")
|
5014 | a8083063 | Iustin Pop | |
5015 | a8083063 | Iustin Pop | feedback_fn("* shutting down instance on source node")
|
5016 | 9a4f63d1 | Iustin Pop | logging.info("Shutting down instance %s on node %s",
|
5017 | 9a4f63d1 | Iustin Pop | instance.name, source_node) |
5018 | a8083063 | Iustin Pop | |
5019 | 17c3f802 | Guido Trotter | result = self.rpc.call_instance_shutdown(source_node, instance,
|
5020 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
5021 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5022 | 1fae010f | Iustin Pop | if msg:
|
5023 | 24a40d57 | Iustin Pop | if self.op.ignore_consistency: |
5024 | 86d9d3bb | Iustin Pop | self.proc.LogWarning("Could not shutdown instance %s on node %s." |
5025 | 1fae010f | Iustin Pop | " Proceeding anyway. Please make sure node"
|
5026 | 1fae010f | Iustin Pop | " %s is down. Error details: %s",
|
5027 | 1fae010f | Iustin Pop | instance.name, source_node, source_node, msg) |
5028 | 24a40d57 | Iustin Pop | else:
|
5029 | 1fae010f | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
5030 | 1fae010f | Iustin Pop | " node %s: %s" %
|
5031 | 1fae010f | Iustin Pop | (instance.name, source_node, msg)) |
5032 | a8083063 | Iustin Pop | |
5033 | a8083063 | Iustin Pop | feedback_fn("* deactivating the instance's disks on source node")
|
5034 | b9bddb6b | Iustin Pop | if not _ShutdownInstanceDisks(self, instance, ignore_primary=True): |
5035 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't shut down the instance's disks.") |
5036 | a8083063 | Iustin Pop | |
5037 | a8083063 | Iustin Pop | instance.primary_node = target_node |
5038 | a8083063 | Iustin Pop | # distribute new instance config to the other nodes
|
5039 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
5040 | a8083063 | Iustin Pop | |
5041 | 12a0cfbe | Guido Trotter | # Only start the instance if it's marked as up
|
5042 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
5043 | 12a0cfbe | Guido Trotter | feedback_fn("* activating the instance's disks on target node")
|
5044 | 9a4f63d1 | Iustin Pop | logging.info("Starting instance %s on node %s",
|
5045 | 9a4f63d1 | Iustin Pop | instance.name, target_node) |
5046 | 12a0cfbe | Guido Trotter | |
5047 | 7c4d6c7b | Michael Hanselmann | disks_ok, _ = _AssembleInstanceDisks(self, instance,
|
5048 | 12a0cfbe | Guido Trotter | ignore_secondaries=True)
|
5049 | 12a0cfbe | Guido Trotter | if not disks_ok: |
5050 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
5051 | 12a0cfbe | Guido Trotter | raise errors.OpExecError("Can't activate the instance's disks") |
5052 | a8083063 | Iustin Pop | |
5053 | 12a0cfbe | Guido Trotter | feedback_fn("* starting the instance on the target node")
|
5054 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(target_node, instance, None, None) |
5055 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5056 | dd279568 | Iustin Pop | if msg:
|
5057 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
5058 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance %s on node %s: %s" % |
5059 | dd279568 | Iustin Pop | (instance.name, target_node, msg)) |
5060 | a8083063 | Iustin Pop | |
5061 | a8083063 | Iustin Pop | |
5062 | 53c776b5 | Iustin Pop | class LUMigrateInstance(LogicalUnit): |
5063 | 53c776b5 | Iustin Pop | """Migrate an instance.
|
5064 | 53c776b5 | Iustin Pop |
|
5065 | 53c776b5 | Iustin Pop | This is migration without shutting down, compared to the failover,
|
5066 | 53c776b5 | Iustin Pop | which is done with shutdown.
|
5067 | 53c776b5 | Iustin Pop |
|
5068 | 53c776b5 | Iustin Pop | """
|
5069 | 53c776b5 | Iustin Pop | HPATH = "instance-migrate"
|
5070 | 53c776b5 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
5071 | 53c776b5 | Iustin Pop | _OP_REQP = ["instance_name", "live", "cleanup"] |
5072 | 53c776b5 | Iustin Pop | |
5073 | 53c776b5 | Iustin Pop | REQ_BGL = False
|
5074 | 53c776b5 | Iustin Pop | |
5075 | 53c776b5 | Iustin Pop | def ExpandNames(self): |
5076 | 53c776b5 | Iustin Pop | self._ExpandAndLockInstance()
|
5077 | 3e06e001 | Michael Hanselmann | |
5078 | 53c776b5 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
5079 | 53c776b5 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
5080 | 53c776b5 | Iustin Pop | |
5081 | 3e06e001 | Michael Hanselmann | self._migrater = TLMigrateInstance(self, self.op.instance_name, |
5082 | 3e06e001 | Michael Hanselmann | self.op.live, self.op.cleanup) |
5083 | 3a012b41 | Michael Hanselmann | self.tasklets = [self._migrater] |
5084 | 3e06e001 | Michael Hanselmann | |
5085 | 53c776b5 | Iustin Pop | def DeclareLocks(self, level): |
5086 | 53c776b5 | Iustin Pop | if level == locking.LEVEL_NODE:
|
5087 | 53c776b5 | Iustin Pop | self._LockInstancesNodes()
|
5088 | 53c776b5 | Iustin Pop | |
5089 | 53c776b5 | Iustin Pop | def BuildHooksEnv(self): |
5090 | 53c776b5 | Iustin Pop | """Build hooks env.
|
5091 | 53c776b5 | Iustin Pop |
|
5092 | 53c776b5 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
5093 | 53c776b5 | Iustin Pop |
|
5094 | 53c776b5 | Iustin Pop | """
|
5095 | 3e06e001 | Michael Hanselmann | instance = self._migrater.instance
|
5096 | 08eec276 | Iustin Pop | source_node = instance.primary_node |
5097 | 08eec276 | Iustin Pop | target_node = instance.secondary_nodes[0]
|
5098 | 3e06e001 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self, instance)
|
5099 | 2c2690c9 | Iustin Pop | env["MIGRATE_LIVE"] = self.op.live |
5100 | 2c2690c9 | Iustin Pop | env["MIGRATE_CLEANUP"] = self.op.cleanup |
5101 | 08eec276 | Iustin Pop | env.update({ |
5102 | 08eec276 | Iustin Pop | "OLD_PRIMARY": source_node,
|
5103 | 08eec276 | Iustin Pop | "OLD_SECONDARY": target_node,
|
5104 | 08eec276 | Iustin Pop | "NEW_PRIMARY": target_node,
|
5105 | 08eec276 | Iustin Pop | "NEW_SECONDARY": source_node,
|
5106 | 08eec276 | Iustin Pop | }) |
5107 | 3e06e001 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes) |
5108 | abd8e836 | Iustin Pop | nl_post = list(nl)
|
5109 | abd8e836 | Iustin Pop | nl_post.append(source_node) |
5110 | abd8e836 | Iustin Pop | return env, nl, nl_post
|
5111 | 53c776b5 | Iustin Pop | |
5112 | 3e06e001 | Michael Hanselmann | |
5113 | 313bcead | Iustin Pop | class LUMoveInstance(LogicalUnit): |
5114 | 313bcead | Iustin Pop | """Move an instance by data-copying.
|
5115 | 313bcead | Iustin Pop |
|
5116 | 313bcead | Iustin Pop | """
|
5117 | 313bcead | Iustin Pop | HPATH = "instance-move"
|
5118 | 313bcead | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
5119 | 313bcead | Iustin Pop | _OP_REQP = ["instance_name", "target_node"] |
5120 | 313bcead | Iustin Pop | REQ_BGL = False
|
5121 | 313bcead | Iustin Pop | |
5122 | 17c3f802 | Guido Trotter | def CheckArguments(self): |
5123 | 17c3f802 | Guido Trotter | """Check the arguments.
|
5124 | 17c3f802 | Guido Trotter |
|
5125 | 17c3f802 | Guido Trotter | """
|
5126 | 17c3f802 | Guido Trotter | self.shutdown_timeout = getattr(self.op, "shutdown_timeout", |
5127 | 17c3f802 | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
5128 | 17c3f802 | Guido Trotter | |
5129 | 313bcead | Iustin Pop | def ExpandNames(self): |
5130 | 313bcead | Iustin Pop | self._ExpandAndLockInstance()
|
5131 | cf26a87a | Iustin Pop | target_node = _ExpandNodeName(self.cfg, self.op.target_node) |
5132 | 313bcead | Iustin Pop | self.op.target_node = target_node
|
5133 | 313bcead | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = [target_node]
|
5134 | 313bcead | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
5135 | 313bcead | Iustin Pop | |
5136 | 313bcead | Iustin Pop | def DeclareLocks(self, level): |
5137 | 313bcead | Iustin Pop | if level == locking.LEVEL_NODE:
|
5138 | 313bcead | Iustin Pop | self._LockInstancesNodes(primary_only=True) |
5139 | 313bcead | Iustin Pop | |
5140 | 313bcead | Iustin Pop | def BuildHooksEnv(self): |
5141 | 313bcead | Iustin Pop | """Build hooks env.
|
5142 | 313bcead | Iustin Pop |
|
5143 | 313bcead | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
5144 | 313bcead | Iustin Pop |
|
5145 | 313bcead | Iustin Pop | """
|
5146 | 313bcead | Iustin Pop | env = { |
5147 | 313bcead | Iustin Pop | "TARGET_NODE": self.op.target_node, |
5148 | 17c3f802 | Guido Trotter | "SHUTDOWN_TIMEOUT": self.shutdown_timeout, |
5149 | 313bcead | Iustin Pop | } |
5150 | 313bcead | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
5151 | 313bcead | Iustin Pop | nl = [self.cfg.GetMasterNode()] + [self.instance.primary_node, |
5152 | 313bcead | Iustin Pop | self.op.target_node]
|
5153 | 313bcead | Iustin Pop | return env, nl, nl
|
5154 | 313bcead | Iustin Pop | |
5155 | 313bcead | Iustin Pop | def CheckPrereq(self): |
5156 | 313bcead | Iustin Pop | """Check prerequisites.
|
5157 | 313bcead | Iustin Pop |
|
5158 | 313bcead | Iustin Pop | This checks that the instance is in the cluster.
|
5159 | 313bcead | Iustin Pop |
|
5160 | 313bcead | Iustin Pop | """
|
5161 | 313bcead | Iustin Pop | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
5162 | 313bcead | Iustin Pop | assert self.instance is not None, \ |
5163 | 313bcead | Iustin Pop | "Cannot retrieve locked instance %s" % self.op.instance_name |
5164 | 313bcead | Iustin Pop | |
5165 | 313bcead | Iustin Pop | node = self.cfg.GetNodeInfo(self.op.target_node) |
5166 | 313bcead | Iustin Pop | assert node is not None, \ |
5167 | 313bcead | Iustin Pop | "Cannot retrieve locked node %s" % self.op.target_node |
5168 | 313bcead | Iustin Pop | |
5169 | 313bcead | Iustin Pop | self.target_node = target_node = node.name
|
5170 | 313bcead | Iustin Pop | |
5171 | 313bcead | Iustin Pop | if target_node == instance.primary_node:
|
5172 | 313bcead | Iustin Pop | raise errors.OpPrereqError("Instance %s is already on the node %s" % |
5173 | 5c983ee5 | Iustin Pop | (instance.name, target_node), |
5174 | 5c983ee5 | Iustin Pop | errors.ECODE_STATE) |
5175 | 313bcead | Iustin Pop | |
5176 | 313bcead | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
5177 | 313bcead | Iustin Pop | |
5178 | 313bcead | Iustin Pop | for idx, dsk in enumerate(instance.disks): |
5179 | 313bcead | Iustin Pop | if dsk.dev_type not in (constants.LD_LV, constants.LD_FILE): |
5180 | 313bcead | Iustin Pop | raise errors.OpPrereqError("Instance disk %d has a complex layout," |
5181 | d1b83918 | Iustin Pop | " cannot copy" % idx, errors.ECODE_STATE)
|
5182 | 313bcead | Iustin Pop | |
5183 | 313bcead | Iustin Pop | _CheckNodeOnline(self, target_node)
|
5184 | 313bcead | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
5185 | 313bcead | Iustin Pop | |
5186 | 313bcead | Iustin Pop | if instance.admin_up:
|
5187 | 313bcead | Iustin Pop | # check memory requirements on the secondary node
|
5188 | 313bcead | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "failing over instance %s" % |
5189 | 313bcead | Iustin Pop | instance.name, bep[constants.BE_MEMORY], |
5190 | 313bcead | Iustin Pop | instance.hypervisor) |
5191 | 313bcead | Iustin Pop | else:
|
5192 | 313bcead | Iustin Pop | self.LogInfo("Not checking memory on the secondary node as" |
5193 | 313bcead | Iustin Pop | " instance will not be started")
|
5194 | 313bcead | Iustin Pop | |
5195 | 313bcead | Iustin Pop | # check bridge existance
|
5196 | 313bcead | Iustin Pop | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
5197 | 313bcead | Iustin Pop | |
5198 | 313bcead | Iustin Pop | def Exec(self, feedback_fn): |
5199 | 313bcead | Iustin Pop | """Move an instance.
|
5200 | 313bcead | Iustin Pop |
|
5201 | 313bcead | Iustin Pop | The move is done by shutting it down on its present node, copying
|
5202 | 313bcead | Iustin Pop | the data over (slow) and starting it on the new node.
|
5203 | 313bcead | Iustin Pop |
|
5204 | 313bcead | Iustin Pop | """
|
5205 | 313bcead | Iustin Pop | instance = self.instance
|
5206 | 313bcead | Iustin Pop | |
5207 | 313bcead | Iustin Pop | source_node = instance.primary_node |
5208 | 313bcead | Iustin Pop | target_node = self.target_node
|
5209 | 313bcead | Iustin Pop | |
5210 | 313bcead | Iustin Pop | self.LogInfo("Shutting down instance %s on source node %s", |
5211 | 313bcead | Iustin Pop | instance.name, source_node) |
5212 | 313bcead | Iustin Pop | |
5213 | 17c3f802 | Guido Trotter | result = self.rpc.call_instance_shutdown(source_node, instance,
|
5214 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
5215 | 313bcead | Iustin Pop | msg = result.fail_msg |
5216 | 313bcead | Iustin Pop | if msg:
|
5217 | 313bcead | Iustin Pop | if self.op.ignore_consistency: |
5218 | 313bcead | Iustin Pop | self.proc.LogWarning("Could not shutdown instance %s on node %s." |
5219 | 313bcead | Iustin Pop | " Proceeding anyway. Please make sure node"
|
5220 | 313bcead | Iustin Pop | " %s is down. Error details: %s",
|
5221 | 313bcead | Iustin Pop | instance.name, source_node, source_node, msg) |
5222 | 313bcead | Iustin Pop | else:
|
5223 | 313bcead | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
5224 | 313bcead | Iustin Pop | " node %s: %s" %
|
5225 | 313bcead | Iustin Pop | (instance.name, source_node, msg)) |
5226 | 313bcead | Iustin Pop | |
5227 | 313bcead | Iustin Pop | # create the target disks
|
5228 | 313bcead | Iustin Pop | try:
|
5229 | 313bcead | Iustin Pop | _CreateDisks(self, instance, target_node=target_node)
|
5230 | 313bcead | Iustin Pop | except errors.OpExecError:
|
5231 | 313bcead | Iustin Pop | self.LogWarning("Device creation failed, reverting...") |
5232 | 313bcead | Iustin Pop | try:
|
5233 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=target_node)
|
5234 | 313bcead | Iustin Pop | finally:
|
5235 | 313bcead | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance.name)
|
5236 | 313bcead | Iustin Pop | raise
|
5237 | 313bcead | Iustin Pop | |
5238 | 313bcead | Iustin Pop | cluster_name = self.cfg.GetClusterInfo().cluster_name
|
5239 | 313bcead | Iustin Pop | |
5240 | 313bcead | Iustin Pop | errs = [] |
5241 | 313bcead | Iustin Pop | # activate, get path, copy the data over
|
5242 | 313bcead | Iustin Pop | for idx, disk in enumerate(instance.disks): |
5243 | 313bcead | Iustin Pop | self.LogInfo("Copying data for disk %d", idx) |
5244 | 313bcead | Iustin Pop | result = self.rpc.call_blockdev_assemble(target_node, disk,
|
5245 | 313bcead | Iustin Pop | instance.name, True)
|
5246 | 313bcead | Iustin Pop | if result.fail_msg:
|
5247 | 313bcead | Iustin Pop | self.LogWarning("Can't assemble newly created disk %d: %s", |
5248 | 313bcead | Iustin Pop | idx, result.fail_msg) |
5249 | 313bcead | Iustin Pop | errs.append(result.fail_msg) |
5250 | 313bcead | Iustin Pop | break
|
5251 | 313bcead | Iustin Pop | dev_path = result.payload |
5252 | 313bcead | Iustin Pop | result = self.rpc.call_blockdev_export(source_node, disk,
|
5253 | 313bcead | Iustin Pop | target_node, dev_path, |
5254 | 313bcead | Iustin Pop | cluster_name) |
5255 | 313bcead | Iustin Pop | if result.fail_msg:
|
5256 | 313bcead | Iustin Pop | self.LogWarning("Can't copy data over for disk %d: %s", |
5257 | 313bcead | Iustin Pop | idx, result.fail_msg) |
5258 | 313bcead | Iustin Pop | errs.append(result.fail_msg) |
5259 | 313bcead | Iustin Pop | break
|
5260 | 313bcead | Iustin Pop | |
5261 | 313bcead | Iustin Pop | if errs:
|
5262 | 313bcead | Iustin Pop | self.LogWarning("Some disks failed to copy, aborting") |
5263 | 313bcead | Iustin Pop | try:
|
5264 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=target_node)
|
5265 | 313bcead | Iustin Pop | finally:
|
5266 | 313bcead | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance.name)
|
5267 | 313bcead | Iustin Pop | raise errors.OpExecError("Errors during disk copy: %s" % |
5268 | 313bcead | Iustin Pop | (",".join(errs),))
|
5269 | 313bcead | Iustin Pop | |
5270 | 313bcead | Iustin Pop | instance.primary_node = target_node |
5271 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
5272 | 313bcead | Iustin Pop | |
5273 | 313bcead | Iustin Pop | self.LogInfo("Removing the disks on the original node") |
5274 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=source_node)
|
5275 | 313bcead | Iustin Pop | |
5276 | 313bcead | Iustin Pop | # Only start the instance if it's marked as up
|
5277 | 313bcead | Iustin Pop | if instance.admin_up:
|
5278 | 313bcead | Iustin Pop | self.LogInfo("Starting instance %s on node %s", |
5279 | 313bcead | Iustin Pop | instance.name, target_node) |
5280 | 313bcead | Iustin Pop | |
5281 | 313bcead | Iustin Pop | disks_ok, _ = _AssembleInstanceDisks(self, instance,
|
5282 | 313bcead | Iustin Pop | ignore_secondaries=True)
|
5283 | 313bcead | Iustin Pop | if not disks_ok: |
5284 | 313bcead | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
5285 | 313bcead | Iustin Pop | raise errors.OpExecError("Can't activate the instance's disks") |
5286 | 313bcead | Iustin Pop | |
5287 | 313bcead | Iustin Pop | result = self.rpc.call_instance_start(target_node, instance, None, None) |
5288 | 313bcead | Iustin Pop | msg = result.fail_msg |
5289 | 313bcead | Iustin Pop | if msg:
|
5290 | 313bcead | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
5291 | 313bcead | Iustin Pop | raise errors.OpExecError("Could not start instance %s on node %s: %s" % |
5292 | 313bcead | Iustin Pop | (instance.name, target_node, msg)) |
5293 | 313bcead | Iustin Pop | |
5294 | 313bcead | Iustin Pop | |
5295 | 80cb875c | Michael Hanselmann | class LUMigrateNode(LogicalUnit): |
5296 | 80cb875c | Michael Hanselmann | """Migrate all instances from a node.
|
5297 | 80cb875c | Michael Hanselmann |
|
5298 | 80cb875c | Michael Hanselmann | """
|
5299 | 80cb875c | Michael Hanselmann | HPATH = "node-migrate"
|
5300 | 80cb875c | Michael Hanselmann | HTYPE = constants.HTYPE_NODE |
5301 | 80cb875c | Michael Hanselmann | _OP_REQP = ["node_name", "live"] |
5302 | 80cb875c | Michael Hanselmann | REQ_BGL = False
|
5303 | 80cb875c | Michael Hanselmann | |
5304 | 80cb875c | Michael Hanselmann | def ExpandNames(self): |
5305 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
5306 | 80cb875c | Michael Hanselmann | |
5307 | 80cb875c | Michael Hanselmann | self.needed_locks = {
|
5308 | 80cb875c | Michael Hanselmann | locking.LEVEL_NODE: [self.op.node_name],
|
5309 | 80cb875c | Michael Hanselmann | } |
5310 | 80cb875c | Michael Hanselmann | |
5311 | 80cb875c | Michael Hanselmann | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
5312 | 80cb875c | Michael Hanselmann | |
5313 | 80cb875c | Michael Hanselmann | # Create tasklets for migrating instances for all instances on this node
|
5314 | 80cb875c | Michael Hanselmann | names = [] |
5315 | 80cb875c | Michael Hanselmann | tasklets = [] |
5316 | 80cb875c | Michael Hanselmann | |
5317 | 80cb875c | Michael Hanselmann | for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name): |
5318 | 80cb875c | Michael Hanselmann | logging.debug("Migrating instance %s", inst.name)
|
5319 | 80cb875c | Michael Hanselmann | names.append(inst.name) |
5320 | 80cb875c | Michael Hanselmann | |
5321 | 80cb875c | Michael Hanselmann | tasklets.append(TLMigrateInstance(self, inst.name, self.op.live, False)) |
5322 | 80cb875c | Michael Hanselmann | |
5323 | 80cb875c | Michael Hanselmann | self.tasklets = tasklets
|
5324 | 80cb875c | Michael Hanselmann | |
5325 | 80cb875c | Michael Hanselmann | # Declare instance locks
|
5326 | 80cb875c | Michael Hanselmann | self.needed_locks[locking.LEVEL_INSTANCE] = names
|
5327 | 80cb875c | Michael Hanselmann | |
5328 | 80cb875c | Michael Hanselmann | def DeclareLocks(self, level): |
5329 | 80cb875c | Michael Hanselmann | if level == locking.LEVEL_NODE:
|
5330 | 80cb875c | Michael Hanselmann | self._LockInstancesNodes()
|
5331 | 80cb875c | Michael Hanselmann | |
5332 | 80cb875c | Michael Hanselmann | def BuildHooksEnv(self): |
5333 | 80cb875c | Michael Hanselmann | """Build hooks env.
|
5334 | 80cb875c | Michael Hanselmann |
|
5335 | 80cb875c | Michael Hanselmann | This runs on the master, the primary and all the secondaries.
|
5336 | 80cb875c | Michael Hanselmann |
|
5337 | 80cb875c | Michael Hanselmann | """
|
5338 | 80cb875c | Michael Hanselmann | env = { |
5339 | 80cb875c | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
5340 | 80cb875c | Michael Hanselmann | } |
5341 | 80cb875c | Michael Hanselmann | |
5342 | 80cb875c | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
5343 | 80cb875c | Michael Hanselmann | |
5344 | 80cb875c | Michael Hanselmann | return (env, nl, nl)
|
5345 | 80cb875c | Michael Hanselmann | |
5346 | 80cb875c | Michael Hanselmann | |
5347 | 3e06e001 | Michael Hanselmann | class TLMigrateInstance(Tasklet): |
5348 | 3e06e001 | Michael Hanselmann | def __init__(self, lu, instance_name, live, cleanup): |
5349 | 3e06e001 | Michael Hanselmann | """Initializes this class.
|
5350 | 3e06e001 | Michael Hanselmann |
|
5351 | 3e06e001 | Michael Hanselmann | """
|
5352 | 464243a7 | Michael Hanselmann | Tasklet.__init__(self, lu)
|
5353 | 464243a7 | Michael Hanselmann | |
5354 | 3e06e001 | Michael Hanselmann | # Parameters
|
5355 | 3e06e001 | Michael Hanselmann | self.instance_name = instance_name
|
5356 | 3e06e001 | Michael Hanselmann | self.live = live
|
5357 | 3e06e001 | Michael Hanselmann | self.cleanup = cleanup
|
5358 | 3e06e001 | Michael Hanselmann | |
5359 | 53c776b5 | Iustin Pop | def CheckPrereq(self): |
5360 | 53c776b5 | Iustin Pop | """Check prerequisites.
|
5361 | 53c776b5 | Iustin Pop |
|
5362 | 53c776b5 | Iustin Pop | This checks that the instance is in the cluster.
|
5363 | 53c776b5 | Iustin Pop |
|
5364 | 53c776b5 | Iustin Pop | """
|
5365 | cf26a87a | Iustin Pop | instance_name = _ExpandInstanceName(self.lu.cfg, self.instance_name) |
5366 | cf26a87a | Iustin Pop | instance = self.cfg.GetInstanceInfo(instance_name)
|
5367 | cf26a87a | Iustin Pop | assert instance is not None |
5368 | 53c776b5 | Iustin Pop | |
5369 | 53c776b5 | Iustin Pop | if instance.disk_template != constants.DT_DRBD8:
|
5370 | 53c776b5 | Iustin Pop | raise errors.OpPrereqError("Instance's disk layout is not" |
5371 | 5c983ee5 | Iustin Pop | " drbd8, cannot migrate.", errors.ECODE_STATE)
|
5372 | 53c776b5 | Iustin Pop | |
5373 | 53c776b5 | Iustin Pop | secondary_nodes = instance.secondary_nodes |
5374 | 53c776b5 | Iustin Pop | if not secondary_nodes: |
5375 | 733a2b6a | Iustin Pop | raise errors.ConfigurationError("No secondary node but using" |
5376 | 733a2b6a | Iustin Pop | " drbd8 disk template")
|
5377 | 53c776b5 | Iustin Pop | |
5378 | 53c776b5 | Iustin Pop | i_be = self.cfg.GetClusterInfo().FillBE(instance)
|
5379 | 53c776b5 | Iustin Pop | |
5380 | 53c776b5 | Iustin Pop | target_node = secondary_nodes[0]
|
5381 | 53c776b5 | Iustin Pop | # check memory requirements on the secondary node
|
5382 | 53c776b5 | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "migrating instance %s" % |
5383 | 53c776b5 | Iustin Pop | instance.name, i_be[constants.BE_MEMORY], |
5384 | 53c776b5 | Iustin Pop | instance.hypervisor) |
5385 | 53c776b5 | Iustin Pop | |
5386 | 53c776b5 | Iustin Pop | # check bridge existance
|
5387 | b165e77e | Guido Trotter | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
5388 | 53c776b5 | Iustin Pop | |
5389 | 3e06e001 | Michael Hanselmann | if not self.cleanup: |
5390 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
5391 | 53c776b5 | Iustin Pop | result = self.rpc.call_instance_migratable(instance.primary_node,
|
5392 | 53c776b5 | Iustin Pop | instance) |
5393 | 045dd6d9 | Iustin Pop | result.Raise("Can't migrate, please use failover",
|
5394 | 045dd6d9 | Iustin Pop | prereq=True, ecode=errors.ECODE_STATE)
|
5395 | 53c776b5 | Iustin Pop | |
5396 | 53c776b5 | Iustin Pop | self.instance = instance
|
5397 | 53c776b5 | Iustin Pop | |
5398 | 53c776b5 | Iustin Pop | def _WaitUntilSync(self): |
5399 | 53c776b5 | Iustin Pop | """Poll with custom rpc for disk sync.
|
5400 | 53c776b5 | Iustin Pop |
|
5401 | 53c776b5 | Iustin Pop | This uses our own step-based rpc call.
|
5402 | 53c776b5 | Iustin Pop |
|
5403 | 53c776b5 | Iustin Pop | """
|
5404 | 53c776b5 | Iustin Pop | self.feedback_fn("* wait until resync is done") |
5405 | 53c776b5 | Iustin Pop | all_done = False
|
5406 | 53c776b5 | Iustin Pop | while not all_done: |
5407 | 53c776b5 | Iustin Pop | all_done = True
|
5408 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_wait_sync(self.all_nodes, |
5409 | 53c776b5 | Iustin Pop | self.nodes_ip,
|
5410 | 53c776b5 | Iustin Pop | self.instance.disks)
|
5411 | 53c776b5 | Iustin Pop | min_percent = 100
|
5412 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
5413 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot resync disks on node %s" % node)
|
5414 | 0959c824 | Iustin Pop | node_done, node_percent = nres.payload |
5415 | 53c776b5 | Iustin Pop | all_done = all_done and node_done
|
5416 | 53c776b5 | Iustin Pop | if node_percent is not None: |
5417 | 53c776b5 | Iustin Pop | min_percent = min(min_percent, node_percent)
|
5418 | 53c776b5 | Iustin Pop | if not all_done: |
5419 | 53c776b5 | Iustin Pop | if min_percent < 100: |
5420 | 53c776b5 | Iustin Pop | self.feedback_fn(" - progress: %.1f%%" % min_percent) |
5421 | 53c776b5 | Iustin Pop | time.sleep(2)
|
5422 | 53c776b5 | Iustin Pop | |
5423 | 53c776b5 | Iustin Pop | def _EnsureSecondary(self, node): |
5424 | 53c776b5 | Iustin Pop | """Demote a node to secondary.
|
5425 | 53c776b5 | Iustin Pop |
|
5426 | 53c776b5 | Iustin Pop | """
|
5427 | 53c776b5 | Iustin Pop | self.feedback_fn("* switching node %s to secondary mode" % node) |
5428 | 53c776b5 | Iustin Pop | |
5429 | 53c776b5 | Iustin Pop | for dev in self.instance.disks: |
5430 | 53c776b5 | Iustin Pop | self.cfg.SetDiskID(dev, node)
|
5431 | 53c776b5 | Iustin Pop | |
5432 | 53c776b5 | Iustin Pop | result = self.rpc.call_blockdev_close(node, self.instance.name, |
5433 | 53c776b5 | Iustin Pop | self.instance.disks)
|
5434 | 4c4e4e1e | Iustin Pop | result.Raise("Cannot change disk to secondary on node %s" % node)
|
5435 | 53c776b5 | Iustin Pop | |
5436 | 53c776b5 | Iustin Pop | def _GoStandalone(self): |
5437 | 53c776b5 | Iustin Pop | """Disconnect from the network.
|
5438 | 53c776b5 | Iustin Pop |
|
5439 | 53c776b5 | Iustin Pop | """
|
5440 | 53c776b5 | Iustin Pop | self.feedback_fn("* changing into standalone mode") |
5441 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip, |
5442 | 53c776b5 | Iustin Pop | self.instance.disks)
|
5443 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
5444 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot disconnect disks node %s" % node)
|
5445 | 53c776b5 | Iustin Pop | |
5446 | 53c776b5 | Iustin Pop | def _GoReconnect(self, multimaster): |
5447 | 53c776b5 | Iustin Pop | """Reconnect to the network.
|
5448 | 53c776b5 | Iustin Pop |
|
5449 | 53c776b5 | Iustin Pop | """
|
5450 | 53c776b5 | Iustin Pop | if multimaster:
|
5451 | 53c776b5 | Iustin Pop | msg = "dual-master"
|
5452 | 53c776b5 | Iustin Pop | else:
|
5453 | 53c776b5 | Iustin Pop | msg = "single-master"
|
5454 | 53c776b5 | Iustin Pop | self.feedback_fn("* changing disks into %s mode" % msg) |
5455 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip, |
5456 | 53c776b5 | Iustin Pop | self.instance.disks,
|
5457 | 53c776b5 | Iustin Pop | self.instance.name, multimaster)
|
5458 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
5459 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot change disks config on node %s" % node)
|
5460 | 53c776b5 | Iustin Pop | |
5461 | 53c776b5 | Iustin Pop | def _ExecCleanup(self): |
5462 | 53c776b5 | Iustin Pop | """Try to cleanup after a failed migration.
|
5463 | 53c776b5 | Iustin Pop |
|
5464 | 53c776b5 | Iustin Pop | The cleanup is done by:
|
5465 | 53c776b5 | Iustin Pop | - check that the instance is running only on one node
|
5466 | 53c776b5 | Iustin Pop | (and update the config if needed)
|
5467 | 53c776b5 | Iustin Pop | - change disks on its secondary node to secondary
|
5468 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized
|
5469 | 53c776b5 | Iustin Pop | - disconnect from the network
|
5470 | 53c776b5 | Iustin Pop | - change disks into single-master mode
|
5471 | 53c776b5 | Iustin Pop | - wait again until disks are fully synchronized
|
5472 | 53c776b5 | Iustin Pop |
|
5473 | 53c776b5 | Iustin Pop | """
|
5474 | 53c776b5 | Iustin Pop | instance = self.instance
|
5475 | 53c776b5 | Iustin Pop | target_node = self.target_node
|
5476 | 53c776b5 | Iustin Pop | source_node = self.source_node
|
5477 | 53c776b5 | Iustin Pop | |
5478 | 53c776b5 | Iustin Pop | # check running on only one node
|
5479 | 53c776b5 | Iustin Pop | self.feedback_fn("* checking where the instance actually runs" |
5480 | 53c776b5 | Iustin Pop | " (if this hangs, the hypervisor might be in"
|
5481 | 53c776b5 | Iustin Pop | " a bad state)")
|
5482 | 53c776b5 | Iustin Pop | ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor]) |
5483 | 53c776b5 | Iustin Pop | for node, result in ins_l.items(): |
5484 | 4c4e4e1e | Iustin Pop | result.Raise("Can't contact node %s" % node)
|
5485 | 53c776b5 | Iustin Pop | |
5486 | aca13712 | Iustin Pop | runningon_source = instance.name in ins_l[source_node].payload
|
5487 | aca13712 | Iustin Pop | runningon_target = instance.name in ins_l[target_node].payload
|
5488 | 53c776b5 | Iustin Pop | |
5489 | 53c776b5 | Iustin Pop | if runningon_source and runningon_target: |
5490 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Instance seems to be running on two nodes," |
5491 | 53c776b5 | Iustin Pop | " or the hypervisor is confused. You will have"
|
5492 | 53c776b5 | Iustin Pop | " to ensure manually that it runs only on one"
|
5493 | 53c776b5 | Iustin Pop | " and restart this operation.")
|
5494 | 53c776b5 | Iustin Pop | |
5495 | 53c776b5 | Iustin Pop | if not (runningon_source or runningon_target): |
5496 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Instance does not seem to be running at all." |
5497 | 53c776b5 | Iustin Pop | " In this case, it's safer to repair by"
|
5498 | 53c776b5 | Iustin Pop | " running 'gnt-instance stop' to ensure disk"
|
5499 | 53c776b5 | Iustin Pop | " shutdown, and then restarting it.")
|
5500 | 53c776b5 | Iustin Pop | |
5501 | 53c776b5 | Iustin Pop | if runningon_target:
|
5502 | 53c776b5 | Iustin Pop | # the migration has actually succeeded, we need to update the config
|
5503 | 53c776b5 | Iustin Pop | self.feedback_fn("* instance running on secondary node (%s)," |
5504 | 53c776b5 | Iustin Pop | " updating config" % target_node)
|
5505 | 53c776b5 | Iustin Pop | instance.primary_node = target_node |
5506 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, self.feedback_fn) |
5507 | 53c776b5 | Iustin Pop | demoted_node = source_node |
5508 | 53c776b5 | Iustin Pop | else:
|
5509 | 53c776b5 | Iustin Pop | self.feedback_fn("* instance confirmed to be running on its" |
5510 | 53c776b5 | Iustin Pop | " primary node (%s)" % source_node)
|
5511 | 53c776b5 | Iustin Pop | demoted_node = target_node |
5512 | 53c776b5 | Iustin Pop | |
5513 | 53c776b5 | Iustin Pop | self._EnsureSecondary(demoted_node)
|
5514 | 53c776b5 | Iustin Pop | try:
|
5515 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
5516 | 53c776b5 | Iustin Pop | except errors.OpExecError:
|
5517 | 53c776b5 | Iustin Pop | # we ignore here errors, since if the device is standalone, it
|
5518 | 53c776b5 | Iustin Pop | # won't be able to sync
|
5519 | 53c776b5 | Iustin Pop | pass
|
5520 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
5521 | 53c776b5 | Iustin Pop | self._GoReconnect(False) |
5522 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
5523 | 53c776b5 | Iustin Pop | |
5524 | 53c776b5 | Iustin Pop | self.feedback_fn("* done") |
5525 | 53c776b5 | Iustin Pop | |
5526 | 6906a9d8 | Guido Trotter | def _RevertDiskStatus(self): |
5527 | 6906a9d8 | Guido Trotter | """Try to revert the disk status after a failed migration.
|
5528 | 6906a9d8 | Guido Trotter |
|
5529 | 6906a9d8 | Guido Trotter | """
|
5530 | 6906a9d8 | Guido Trotter | target_node = self.target_node
|
5531 | 6906a9d8 | Guido Trotter | try:
|
5532 | 6906a9d8 | Guido Trotter | self._EnsureSecondary(target_node)
|
5533 | 6906a9d8 | Guido Trotter | self._GoStandalone()
|
5534 | 6906a9d8 | Guido Trotter | self._GoReconnect(False) |
5535 | 6906a9d8 | Guido Trotter | self._WaitUntilSync()
|
5536 | 6906a9d8 | Guido Trotter | except errors.OpExecError, err:
|
5537 | 3e06e001 | Michael Hanselmann | self.lu.LogWarning("Migration failed and I can't reconnect the" |
5538 | 3e06e001 | Michael Hanselmann | " drives: error '%s'\n"
|
5539 | 3e06e001 | Michael Hanselmann | "Please look and recover the instance status" %
|
5540 | 3e06e001 | Michael Hanselmann | str(err))
|
5541 | 6906a9d8 | Guido Trotter | |
5542 | 6906a9d8 | Guido Trotter | def _AbortMigration(self): |
5543 | 6906a9d8 | Guido Trotter | """Call the hypervisor code to abort a started migration.
|
5544 | 6906a9d8 | Guido Trotter |
|
5545 | 6906a9d8 | Guido Trotter | """
|
5546 | 6906a9d8 | Guido Trotter | instance = self.instance
|
5547 | 6906a9d8 | Guido Trotter | target_node = self.target_node
|
5548 | 6906a9d8 | Guido Trotter | migration_info = self.migration_info
|
5549 | 6906a9d8 | Guido Trotter | |
5550 | 6906a9d8 | Guido Trotter | abort_result = self.rpc.call_finalize_migration(target_node,
|
5551 | 6906a9d8 | Guido Trotter | instance, |
5552 | 6906a9d8 | Guido Trotter | migration_info, |
5553 | 6906a9d8 | Guido Trotter | False)
|
5554 | 4c4e4e1e | Iustin Pop | abort_msg = abort_result.fail_msg |
5555 | 6906a9d8 | Guido Trotter | if abort_msg:
|
5556 | 099c52ad | Iustin Pop | logging.error("Aborting migration failed on target node %s: %s",
|
5557 | 099c52ad | Iustin Pop | target_node, abort_msg) |
5558 | 6906a9d8 | Guido Trotter | # Don't raise an exception here, as we stil have to try to revert the
|
5559 | 6906a9d8 | Guido Trotter | # disk status, even if this step failed.
|
5560 | 6906a9d8 | Guido Trotter | |
5561 | 53c776b5 | Iustin Pop | def _ExecMigration(self): |
5562 | 53c776b5 | Iustin Pop | """Migrate an instance.
|
5563 | 53c776b5 | Iustin Pop |
|
5564 | 53c776b5 | Iustin Pop | The migrate is done by:
|
5565 | 53c776b5 | Iustin Pop | - change the disks into dual-master mode
|
5566 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized again
|
5567 | 53c776b5 | Iustin Pop | - migrate the instance
|
5568 | 53c776b5 | Iustin Pop | - change disks on the new secondary node (the old primary) to secondary
|
5569 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized
|
5570 | 53c776b5 | Iustin Pop | - change disks into single-master mode
|
5571 | 53c776b5 | Iustin Pop |
|
5572 | 53c776b5 | Iustin Pop | """
|
5573 | 53c776b5 | Iustin Pop | instance = self.instance
|
5574 | 53c776b5 | Iustin Pop | target_node = self.target_node
|
5575 | 53c776b5 | Iustin Pop | source_node = self.source_node
|
5576 | 53c776b5 | Iustin Pop | |
5577 | 53c776b5 | Iustin Pop | self.feedback_fn("* checking disk consistency between source and target") |
5578 | 53c776b5 | Iustin Pop | for dev in instance.disks: |
5579 | 53c776b5 | Iustin Pop | if not _CheckDiskConsistency(self, dev, target_node, False): |
5580 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Disk %s is degraded or not fully" |
5581 | 53c776b5 | Iustin Pop | " synchronized on target node,"
|
5582 | 53c776b5 | Iustin Pop | " aborting migrate." % dev.iv_name)
|
5583 | 53c776b5 | Iustin Pop | |
5584 | 6906a9d8 | Guido Trotter | # First get the migration information from the remote node
|
5585 | 6906a9d8 | Guido Trotter | result = self.rpc.call_migration_info(source_node, instance)
|
5586 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5587 | 6906a9d8 | Guido Trotter | if msg:
|
5588 | 6906a9d8 | Guido Trotter | log_err = ("Failed fetching source migration information from %s: %s" %
|
5589 | 0959c824 | Iustin Pop | (source_node, msg)) |
5590 | 6906a9d8 | Guido Trotter | logging.error(log_err) |
5591 | 6906a9d8 | Guido Trotter | raise errors.OpExecError(log_err)
|
5592 | 6906a9d8 | Guido Trotter | |
5593 | 0959c824 | Iustin Pop | self.migration_info = migration_info = result.payload
|
5594 | 6906a9d8 | Guido Trotter | |
5595 | 6906a9d8 | Guido Trotter | # Then switch the disks to master/master mode
|
5596 | 53c776b5 | Iustin Pop | self._EnsureSecondary(target_node)
|
5597 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
5598 | 53c776b5 | Iustin Pop | self._GoReconnect(True) |
5599 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
5600 | 53c776b5 | Iustin Pop | |
5601 | 6906a9d8 | Guido Trotter | self.feedback_fn("* preparing %s to accept the instance" % target_node) |
5602 | 6906a9d8 | Guido Trotter | result = self.rpc.call_accept_instance(target_node,
|
5603 | 6906a9d8 | Guido Trotter | instance, |
5604 | 6906a9d8 | Guido Trotter | migration_info, |
5605 | 6906a9d8 | Guido Trotter | self.nodes_ip[target_node])
|
5606 | 6906a9d8 | Guido Trotter | |
5607 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5608 | 6906a9d8 | Guido Trotter | if msg:
|
5609 | 6906a9d8 | Guido Trotter | logging.error("Instance pre-migration failed, trying to revert"
|
5610 | 6906a9d8 | Guido Trotter | " disk status: %s", msg)
|
5611 | 78212a5d | Iustin Pop | self.feedback_fn("Pre-migration failed, aborting") |
5612 | 6906a9d8 | Guido Trotter | self._AbortMigration()
|
5613 | 6906a9d8 | Guido Trotter | self._RevertDiskStatus()
|
5614 | 6906a9d8 | Guido Trotter | raise errors.OpExecError("Could not pre-migrate instance %s: %s" % |
5615 | 6906a9d8 | Guido Trotter | (instance.name, msg)) |
5616 | 6906a9d8 | Guido Trotter | |
5617 | 53c776b5 | Iustin Pop | self.feedback_fn("* migrating instance to %s" % target_node) |
5618 | 53c776b5 | Iustin Pop | time.sleep(10)
|
5619 | 53c776b5 | Iustin Pop | result = self.rpc.call_instance_migrate(source_node, instance,
|
5620 | 53c776b5 | Iustin Pop | self.nodes_ip[target_node],
|
5621 | 3e06e001 | Michael Hanselmann | self.live)
|
5622 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5623 | 53c776b5 | Iustin Pop | if msg:
|
5624 | 53c776b5 | Iustin Pop | logging.error("Instance migration failed, trying to revert"
|
5625 | 53c776b5 | Iustin Pop | " disk status: %s", msg)
|
5626 | 78212a5d | Iustin Pop | self.feedback_fn("Migration failed, aborting") |
5627 | 6906a9d8 | Guido Trotter | self._AbortMigration()
|
5628 | 6906a9d8 | Guido Trotter | self._RevertDiskStatus()
|
5629 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Could not migrate instance %s: %s" % |
5630 | 53c776b5 | Iustin Pop | (instance.name, msg)) |
5631 | 53c776b5 | Iustin Pop | time.sleep(10)
|
5632 | 53c776b5 | Iustin Pop | |
5633 | 53c776b5 | Iustin Pop | instance.primary_node = target_node |
5634 | 53c776b5 | Iustin Pop | # distribute new instance config to the other nodes
|
5635 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, self.feedback_fn) |
5636 | 53c776b5 | Iustin Pop | |
5637 | 6906a9d8 | Guido Trotter | result = self.rpc.call_finalize_migration(target_node,
|
5638 | 6906a9d8 | Guido Trotter | instance, |
5639 | 6906a9d8 | Guido Trotter | migration_info, |
5640 | 6906a9d8 | Guido Trotter | True)
|
5641 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
5642 | 6906a9d8 | Guido Trotter | if msg:
|
5643 | 6906a9d8 | Guido Trotter | logging.error("Instance migration succeeded, but finalization failed:"
|
5644 | 099c52ad | Iustin Pop | " %s", msg)
|
5645 | 6906a9d8 | Guido Trotter | raise errors.OpExecError("Could not finalize instance migration: %s" % |
5646 | 6906a9d8 | Guido Trotter | msg) |
5647 | 6906a9d8 | Guido Trotter | |
5648 | 53c776b5 | Iustin Pop | self._EnsureSecondary(source_node)
|
5649 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
5650 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
5651 | 53c776b5 | Iustin Pop | self._GoReconnect(False) |
5652 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
5653 | 53c776b5 | Iustin Pop | |
5654 | 53c776b5 | Iustin Pop | self.feedback_fn("* done") |
5655 | 53c776b5 | Iustin Pop | |
5656 | 53c776b5 | Iustin Pop | def Exec(self, feedback_fn): |
5657 | 53c776b5 | Iustin Pop | """Perform the migration.
|
5658 | 53c776b5 | Iustin Pop |
|
5659 | 53c776b5 | Iustin Pop | """
|
5660 | 80cb875c | Michael Hanselmann | feedback_fn("Migrating instance %s" % self.instance.name) |
5661 | 80cb875c | Michael Hanselmann | |
5662 | 53c776b5 | Iustin Pop | self.feedback_fn = feedback_fn
|
5663 | 53c776b5 | Iustin Pop | |
5664 | 53c776b5 | Iustin Pop | self.source_node = self.instance.primary_node |
5665 | 53c776b5 | Iustin Pop | self.target_node = self.instance.secondary_nodes[0] |
5666 | 53c776b5 | Iustin Pop | self.all_nodes = [self.source_node, self.target_node] |
5667 | 53c776b5 | Iustin Pop | self.nodes_ip = {
|
5668 | 53c776b5 | Iustin Pop | self.source_node: self.cfg.GetNodeInfo(self.source_node).secondary_ip, |
5669 | 53c776b5 | Iustin Pop | self.target_node: self.cfg.GetNodeInfo(self.target_node).secondary_ip, |
5670 | 53c776b5 | Iustin Pop | } |
5671 | 3e06e001 | Michael Hanselmann | |
5672 | 3e06e001 | Michael Hanselmann | if self.cleanup: |
5673 | 53c776b5 | Iustin Pop | return self._ExecCleanup() |
5674 | 53c776b5 | Iustin Pop | else:
|
5675 | 53c776b5 | Iustin Pop | return self._ExecMigration() |
5676 | 53c776b5 | Iustin Pop | |
5677 | 53c776b5 | Iustin Pop | |
5678 | 428958aa | Iustin Pop | def _CreateBlockDev(lu, node, instance, device, force_create, |
5679 | 428958aa | Iustin Pop | info, force_open): |
5680 | 428958aa | Iustin Pop | """Create a tree of block devices on a given node.
|
5681 | a8083063 | Iustin Pop |
|
5682 | a8083063 | Iustin Pop | If this device type has to be created on secondaries, create it and
|
5683 | a8083063 | Iustin Pop | all its children.
|
5684 | a8083063 | Iustin Pop |
|
5685 | a8083063 | Iustin Pop | If not, just recurse to children keeping the same 'force' value.
|
5686 | a8083063 | Iustin Pop |
|
5687 | 428958aa | Iustin Pop | @param lu: the lu on whose behalf we execute
|
5688 | 428958aa | Iustin Pop | @param node: the node on which to create the device
|
5689 | 428958aa | Iustin Pop | @type instance: L{objects.Instance}
|
5690 | 428958aa | Iustin Pop | @param instance: the instance which owns the device
|
5691 | 428958aa | Iustin Pop | @type device: L{objects.Disk}
|
5692 | 428958aa | Iustin Pop | @param device: the device to create
|
5693 | 428958aa | Iustin Pop | @type force_create: boolean
|
5694 | 428958aa | Iustin Pop | @param force_create: whether to force creation of this device; this
|
5695 | 428958aa | Iustin Pop | will be change to True whenever we find a device which has
|
5696 | 428958aa | Iustin Pop | CreateOnSecondary() attribute
|
5697 | 428958aa | Iustin Pop | @param info: the extra 'metadata' we should attach to the device
|
5698 | 428958aa | Iustin Pop | (this will be represented as a LVM tag)
|
5699 | 428958aa | Iustin Pop | @type force_open: boolean
|
5700 | 428958aa | Iustin Pop | @param force_open: this parameter will be passes to the
|
5701 | 821d1bd1 | Iustin Pop | L{backend.BlockdevCreate} function where it specifies
|
5702 | 428958aa | Iustin Pop | whether we run on primary or not, and it affects both
|
5703 | 428958aa | Iustin Pop | the child assembly and the device own Open() execution
|
5704 | 428958aa | Iustin Pop |
|
5705 | a8083063 | Iustin Pop | """
|
5706 | a8083063 | Iustin Pop | if device.CreateOnSecondary():
|
5707 | 428958aa | Iustin Pop | force_create = True
|
5708 | 796cab27 | Iustin Pop | |
5709 | a8083063 | Iustin Pop | if device.children:
|
5710 | a8083063 | Iustin Pop | for child in device.children: |
5711 | 428958aa | Iustin Pop | _CreateBlockDev(lu, node, instance, child, force_create, |
5712 | 428958aa | Iustin Pop | info, force_open) |
5713 | a8083063 | Iustin Pop | |
5714 | 428958aa | Iustin Pop | if not force_create: |
5715 | 796cab27 | Iustin Pop | return
|
5716 | 796cab27 | Iustin Pop | |
5717 | de12473a | Iustin Pop | _CreateSingleBlockDev(lu, node, instance, device, info, force_open) |
5718 | de12473a | Iustin Pop | |
5719 | de12473a | Iustin Pop | |
5720 | de12473a | Iustin Pop | def _CreateSingleBlockDev(lu, node, instance, device, info, force_open): |
5721 | de12473a | Iustin Pop | """Create a single block device on a given node.
|
5722 | de12473a | Iustin Pop |
|
5723 | de12473a | Iustin Pop | This will not recurse over children of the device, so they must be
|
5724 | de12473a | Iustin Pop | created in advance.
|
5725 | de12473a | Iustin Pop |
|
5726 | de12473a | Iustin Pop | @param lu: the lu on whose behalf we execute
|
5727 | de12473a | Iustin Pop | @param node: the node on which to create the device
|
5728 | de12473a | Iustin Pop | @type instance: L{objects.Instance}
|
5729 | de12473a | Iustin Pop | @param instance: the instance which owns the device
|
5730 | de12473a | Iustin Pop | @type device: L{objects.Disk}
|
5731 | de12473a | Iustin Pop | @param device: the device to create
|
5732 | de12473a | Iustin Pop | @param info: the extra 'metadata' we should attach to the device
|
5733 | de12473a | Iustin Pop | (this will be represented as a LVM tag)
|
5734 | de12473a | Iustin Pop | @type force_open: boolean
|
5735 | de12473a | Iustin Pop | @param force_open: this parameter will be passes to the
|
5736 | 821d1bd1 | Iustin Pop | L{backend.BlockdevCreate} function where it specifies
|
5737 | de12473a | Iustin Pop | whether we run on primary or not, and it affects both
|
5738 | de12473a | Iustin Pop | the child assembly and the device own Open() execution
|
5739 | de12473a | Iustin Pop |
|
5740 | de12473a | Iustin Pop | """
|
5741 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(device, node) |
5742 | 7d81697f | Iustin Pop | result = lu.rpc.call_blockdev_create(node, device, device.size, |
5743 | 428958aa | Iustin Pop | instance.name, force_open, info) |
5744 | 4c4e4e1e | Iustin Pop | result.Raise("Can't create block device %s on"
|
5745 | 4c4e4e1e | Iustin Pop | " node %s for instance %s" % (device, node, instance.name))
|
5746 | a8083063 | Iustin Pop | if device.physical_id is None: |
5747 | 0959c824 | Iustin Pop | device.physical_id = result.payload |
5748 | a8083063 | Iustin Pop | |
5749 | a8083063 | Iustin Pop | |
5750 | b9bddb6b | Iustin Pop | def _GenerateUniqueNames(lu, exts): |
5751 | 923b1523 | Iustin Pop | """Generate a suitable LV name.
|
5752 | 923b1523 | Iustin Pop |
|
5753 | 923b1523 | Iustin Pop | This will generate a logical volume name for the given instance.
|
5754 | 923b1523 | Iustin Pop |
|
5755 | 923b1523 | Iustin Pop | """
|
5756 | 923b1523 | Iustin Pop | results = [] |
5757 | 923b1523 | Iustin Pop | for val in exts: |
5758 | 4fae38c5 | Guido Trotter | new_id = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
5759 | 923b1523 | Iustin Pop | results.append("%s%s" % (new_id, val))
|
5760 | 923b1523 | Iustin Pop | return results
|
5761 | 923b1523 | Iustin Pop | |
5762 | 923b1523 | Iustin Pop | |
5763 | b9bddb6b | Iustin Pop | def _GenerateDRBD8Branch(lu, primary, secondary, size, names, iv_name, |
5764 | ffa1c0dc | Iustin Pop | p_minor, s_minor): |
5765 | a1f445d3 | Iustin Pop | """Generate a drbd8 device complete with its children.
|
5766 | a1f445d3 | Iustin Pop |
|
5767 | a1f445d3 | Iustin Pop | """
|
5768 | b9bddb6b | Iustin Pop | port = lu.cfg.AllocatePort() |
5769 | b9bddb6b | Iustin Pop | vgname = lu.cfg.GetVGName() |
5770 | afa1386e | Guido Trotter | shared_secret = lu.cfg.GenerateDRBDSecret(lu.proc.GetECId()) |
5771 | a1f445d3 | Iustin Pop | dev_data = objects.Disk(dev_type=constants.LD_LV, size=size, |
5772 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[0]))
|
5773 | a1f445d3 | Iustin Pop | dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
5774 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[1]))
|
5775 | a1f445d3 | Iustin Pop | drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size, |
5776 | ffa1c0dc | Iustin Pop | logical_id=(primary, secondary, port, |
5777 | f9518d38 | Iustin Pop | p_minor, s_minor, |
5778 | f9518d38 | Iustin Pop | shared_secret), |
5779 | ffa1c0dc | Iustin Pop | children=[dev_data, dev_meta], |
5780 | a1f445d3 | Iustin Pop | iv_name=iv_name) |
5781 | a1f445d3 | Iustin Pop | return drbd_dev
|
5782 | a1f445d3 | Iustin Pop | |
5783 | 7c0d6283 | Michael Hanselmann | |
5784 | b9bddb6b | Iustin Pop | def _GenerateDiskTemplate(lu, template_name, |
5785 | a8083063 | Iustin Pop | instance_name, primary_node, |
5786 | 08db7c5c | Iustin Pop | secondary_nodes, disk_info, |
5787 | e2a65344 | Iustin Pop | file_storage_dir, file_driver, |
5788 | e2a65344 | Iustin Pop | base_index): |
5789 | a8083063 | Iustin Pop | """Generate the entire disk layout for a given template type.
|
5790 | a8083063 | Iustin Pop |
|
5791 | a8083063 | Iustin Pop | """
|
5792 | a8083063 | Iustin Pop | #TODO: compute space requirements
|
5793 | a8083063 | Iustin Pop | |
5794 | b9bddb6b | Iustin Pop | vgname = lu.cfg.GetVGName() |
5795 | 08db7c5c | Iustin Pop | disk_count = len(disk_info)
|
5796 | 08db7c5c | Iustin Pop | disks = [] |
5797 | 3517d9b9 | Manuel Franceschini | if template_name == constants.DT_DISKLESS:
|
5798 | 08db7c5c | Iustin Pop | pass
|
5799 | 3517d9b9 | Manuel Franceschini | elif template_name == constants.DT_PLAIN:
|
5800 | a8083063 | Iustin Pop | if len(secondary_nodes) != 0: |
5801 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
5802 | 923b1523 | Iustin Pop | |
5803 | fb4b324b | Guido Trotter | names = _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
|
5804 | 08db7c5c | Iustin Pop | for i in range(disk_count)]) |
5805 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5806 | e2a65344 | Iustin Pop | disk_index = idx + base_index |
5807 | 08db7c5c | Iustin Pop | disk_dev = objects.Disk(dev_type=constants.LD_LV, size=disk["size"],
|
5808 | 08db7c5c | Iustin Pop | logical_id=(vgname, names[idx]), |
5809 | 6ec66eae | Iustin Pop | iv_name="disk/%d" % disk_index,
|
5810 | 6ec66eae | Iustin Pop | mode=disk["mode"])
|
5811 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5812 | a1f445d3 | Iustin Pop | elif template_name == constants.DT_DRBD8:
|
5813 | a1f445d3 | Iustin Pop | if len(secondary_nodes) != 1: |
5814 | a1f445d3 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
5815 | a1f445d3 | Iustin Pop | remote_node = secondary_nodes[0]
|
5816 | 08db7c5c | Iustin Pop | minors = lu.cfg.AllocateDRBDMinor( |
5817 | 08db7c5c | Iustin Pop | [primary_node, remote_node] * len(disk_info), instance_name)
|
5818 | 08db7c5c | Iustin Pop | |
5819 | e6c1ff2f | Iustin Pop | names = [] |
5820 | fb4b324b | Guido Trotter | for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i) |
5821 | e6c1ff2f | Iustin Pop | for i in range(disk_count)]): |
5822 | e6c1ff2f | Iustin Pop | names.append(lv_prefix + "_data")
|
5823 | e6c1ff2f | Iustin Pop | names.append(lv_prefix + "_meta")
|
5824 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5825 | 112050d9 | Iustin Pop | disk_index = idx + base_index |
5826 | 08db7c5c | Iustin Pop | disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node, |
5827 | 08db7c5c | Iustin Pop | disk["size"], names[idx*2:idx*2+2], |
5828 | e2a65344 | Iustin Pop | "disk/%d" % disk_index,
|
5829 | 08db7c5c | Iustin Pop | minors[idx*2], minors[idx*2+1]) |
5830 | 6ec66eae | Iustin Pop | disk_dev.mode = disk["mode"]
|
5831 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5832 | 0f1a06e3 | Manuel Franceschini | elif template_name == constants.DT_FILE:
|
5833 | 0f1a06e3 | Manuel Franceschini | if len(secondary_nodes) != 0: |
5834 | 0f1a06e3 | Manuel Franceschini | raise errors.ProgrammerError("Wrong template configuration") |
5835 | 0f1a06e3 | Manuel Franceschini | |
5836 | 0e3baaf3 | Iustin Pop | _RequireFileStorage() |
5837 | 0e3baaf3 | Iustin Pop | |
5838 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5839 | 112050d9 | Iustin Pop | disk_index = idx + base_index |
5840 | 08db7c5c | Iustin Pop | disk_dev = objects.Disk(dev_type=constants.LD_FILE, size=disk["size"],
|
5841 | e2a65344 | Iustin Pop | iv_name="disk/%d" % disk_index,
|
5842 | 08db7c5c | Iustin Pop | logical_id=(file_driver, |
5843 | 08db7c5c | Iustin Pop | "%s/disk%d" % (file_storage_dir,
|
5844 | 43e99cff | Guido Trotter | disk_index)), |
5845 | 6ec66eae | Iustin Pop | mode=disk["mode"])
|
5846 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5847 | a8083063 | Iustin Pop | else:
|
5848 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid disk template '%s'" % template_name) |
5849 | a8083063 | Iustin Pop | return disks
|
5850 | a8083063 | Iustin Pop | |
5851 | a8083063 | Iustin Pop | |
5852 | a0c3fea1 | Michael Hanselmann | def _GetInstanceInfoText(instance): |
5853 | 3ecf6786 | Iustin Pop | """Compute that text that should be added to the disk's metadata.
|
5854 | 3ecf6786 | Iustin Pop |
|
5855 | 3ecf6786 | Iustin Pop | """
|
5856 | a0c3fea1 | Michael Hanselmann | return "originstname+%s" % instance.name |
5857 | a0c3fea1 | Michael Hanselmann | |
5858 | a0c3fea1 | Michael Hanselmann | |
5859 | 621b7678 | Iustin Pop | def _CreateDisks(lu, instance, to_skip=None, target_node=None): |
5860 | a8083063 | Iustin Pop | """Create all disks for an instance.
|
5861 | a8083063 | Iustin Pop |
|
5862 | a8083063 | Iustin Pop | This abstracts away some work from AddInstance.
|
5863 | a8083063 | Iustin Pop |
|
5864 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
5865 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
5866 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
5867 | e4376078 | Iustin Pop | @param instance: the instance whose disks we should create
|
5868 | bd315bfa | Iustin Pop | @type to_skip: list
|
5869 | bd315bfa | Iustin Pop | @param to_skip: list of indices to skip
|
5870 | 621b7678 | Iustin Pop | @type target_node: string
|
5871 | 621b7678 | Iustin Pop | @param target_node: if passed, overrides the target node for creation
|
5872 | e4376078 | Iustin Pop | @rtype: boolean
|
5873 | e4376078 | Iustin Pop | @return: the success of the creation
|
5874 | a8083063 | Iustin Pop |
|
5875 | a8083063 | Iustin Pop | """
|
5876 | a0c3fea1 | Michael Hanselmann | info = _GetInstanceInfoText(instance) |
5877 | 621b7678 | Iustin Pop | if target_node is None: |
5878 | 621b7678 | Iustin Pop | pnode = instance.primary_node |
5879 | 621b7678 | Iustin Pop | all_nodes = instance.all_nodes |
5880 | 621b7678 | Iustin Pop | else:
|
5881 | 621b7678 | Iustin Pop | pnode = target_node |
5882 | 621b7678 | Iustin Pop | all_nodes = [pnode] |
5883 | a0c3fea1 | Michael Hanselmann | |
5884 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
5885 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
5886 | 428958aa | Iustin Pop | result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir) |
5887 | 0f1a06e3 | Manuel Franceschini | |
5888 | 4c4e4e1e | Iustin Pop | result.Raise("Failed to create directory '%s' on"
|
5889 | 9b4127eb | Guido Trotter | " node %s" % (file_storage_dir, pnode))
|
5890 | 0f1a06e3 | Manuel Franceschini | |
5891 | 24991749 | Iustin Pop | # Note: this needs to be kept in sync with adding of disks in
|
5892 | 24991749 | Iustin Pop | # LUSetInstanceParams
|
5893 | bd315bfa | Iustin Pop | for idx, device in enumerate(instance.disks): |
5894 | bd315bfa | Iustin Pop | if to_skip and idx in to_skip: |
5895 | bd315bfa | Iustin Pop | continue
|
5896 | 9a4f63d1 | Iustin Pop | logging.info("Creating volume %s for instance %s",
|
5897 | 9a4f63d1 | Iustin Pop | device.iv_name, instance.name) |
5898 | a8083063 | Iustin Pop | #HARDCODE
|
5899 | 621b7678 | Iustin Pop | for node in all_nodes: |
5900 | 428958aa | Iustin Pop | f_create = node == pnode |
5901 | 428958aa | Iustin Pop | _CreateBlockDev(lu, node, instance, device, f_create, info, f_create) |
5902 | a8083063 | Iustin Pop | |
5903 | a8083063 | Iustin Pop | |
5904 | 621b7678 | Iustin Pop | def _RemoveDisks(lu, instance, target_node=None): |
5905 | a8083063 | Iustin Pop | """Remove all disks for an instance.
|
5906 | a8083063 | Iustin Pop |
|
5907 | a8083063 | Iustin Pop | This abstracts away some work from `AddInstance()` and
|
5908 | a8083063 | Iustin Pop | `RemoveInstance()`. Note that in case some of the devices couldn't
|
5909 | 1d67656e | Iustin Pop | be removed, the removal will continue with the other ones (compare
|
5910 | a8083063 | Iustin Pop | with `_CreateDisks()`).
|
5911 | a8083063 | Iustin Pop |
|
5912 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
5913 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
5914 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
5915 | e4376078 | Iustin Pop | @param instance: the instance whose disks we should remove
|
5916 | 621b7678 | Iustin Pop | @type target_node: string
|
5917 | 621b7678 | Iustin Pop | @param target_node: used to override the node on which to remove the disks
|
5918 | e4376078 | Iustin Pop | @rtype: boolean
|
5919 | e4376078 | Iustin Pop | @return: the success of the removal
|
5920 | a8083063 | Iustin Pop |
|
5921 | a8083063 | Iustin Pop | """
|
5922 | 9a4f63d1 | Iustin Pop | logging.info("Removing block devices for instance %s", instance.name)
|
5923 | a8083063 | Iustin Pop | |
5924 | e1bc0878 | Iustin Pop | all_result = True
|
5925 | a8083063 | Iustin Pop | for device in instance.disks: |
5926 | 621b7678 | Iustin Pop | if target_node:
|
5927 | 621b7678 | Iustin Pop | edata = [(target_node, device)] |
5928 | 621b7678 | Iustin Pop | else:
|
5929 | 621b7678 | Iustin Pop | edata = device.ComputeNodeTree(instance.primary_node) |
5930 | 621b7678 | Iustin Pop | for node, disk in edata: |
5931 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(disk, node) |
5932 | 4c4e4e1e | Iustin Pop | msg = lu.rpc.call_blockdev_remove(node, disk).fail_msg |
5933 | e1bc0878 | Iustin Pop | if msg:
|
5934 | e1bc0878 | Iustin Pop | lu.LogWarning("Could not remove block device %s on node %s,"
|
5935 | e1bc0878 | Iustin Pop | " continuing anyway: %s", device.iv_name, node, msg)
|
5936 | e1bc0878 | Iustin Pop | all_result = False
|
5937 | 0f1a06e3 | Manuel Franceschini | |
5938 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
5939 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
5940 | dfc2a24c | Guido Trotter | if target_node:
|
5941 | dfc2a24c | Guido Trotter | tgt = target_node |
5942 | 621b7678 | Iustin Pop | else:
|
5943 | dfc2a24c | Guido Trotter | tgt = instance.primary_node |
5944 | 621b7678 | Iustin Pop | result = lu.rpc.call_file_storage_dir_remove(tgt, file_storage_dir) |
5945 | 621b7678 | Iustin Pop | if result.fail_msg:
|
5946 | b2b8bcce | Iustin Pop | lu.LogWarning("Could not remove directory '%s' on node %s: %s",
|
5947 | 621b7678 | Iustin Pop | file_storage_dir, instance.primary_node, result.fail_msg) |
5948 | e1bc0878 | Iustin Pop | all_result = False
|
5949 | 0f1a06e3 | Manuel Franceschini | |
5950 | e1bc0878 | Iustin Pop | return all_result
|
5951 | a8083063 | Iustin Pop | |
5952 | a8083063 | Iustin Pop | |
5953 | 08db7c5c | Iustin Pop | def _ComputeDiskSize(disk_template, disks): |
5954 | e2fe6369 | Iustin Pop | """Compute disk size requirements in the volume group
|
5955 | e2fe6369 | Iustin Pop |
|
5956 | e2fe6369 | Iustin Pop | """
|
5957 | e2fe6369 | Iustin Pop | # Required free disk space as a function of disk and swap space
|
5958 | e2fe6369 | Iustin Pop | req_size_dict = { |
5959 | e2fe6369 | Iustin Pop | constants.DT_DISKLESS: None,
|
5960 | 08db7c5c | Iustin Pop | constants.DT_PLAIN: sum(d["size"] for d in disks), |
5961 | 08db7c5c | Iustin Pop | # 128 MB are added for drbd metadata for each disk
|
5962 | 08db7c5c | Iustin Pop | constants.DT_DRBD8: sum(d["size"] + 128 for d in disks), |
5963 | e2fe6369 | Iustin Pop | constants.DT_FILE: None,
|
5964 | e2fe6369 | Iustin Pop | } |
5965 | e2fe6369 | Iustin Pop | |
5966 | e2fe6369 | Iustin Pop | if disk_template not in req_size_dict: |
5967 | e2fe6369 | Iustin Pop | raise errors.ProgrammerError("Disk template '%s' size requirement" |
5968 | e2fe6369 | Iustin Pop | " is unknown" % disk_template)
|
5969 | e2fe6369 | Iustin Pop | |
5970 | e2fe6369 | Iustin Pop | return req_size_dict[disk_template]
|
5971 | e2fe6369 | Iustin Pop | |
5972 | e2fe6369 | Iustin Pop | |
5973 | 74409b12 | Iustin Pop | def _CheckHVParams(lu, nodenames, hvname, hvparams): |
5974 | 74409b12 | Iustin Pop | """Hypervisor parameter validation.
|
5975 | 74409b12 | Iustin Pop |
|
5976 | 74409b12 | Iustin Pop | This function abstract the hypervisor parameter validation to be
|
5977 | 74409b12 | Iustin Pop | used in both instance create and instance modify.
|
5978 | 74409b12 | Iustin Pop |
|
5979 | 74409b12 | Iustin Pop | @type lu: L{LogicalUnit}
|
5980 | 74409b12 | Iustin Pop | @param lu: the logical unit for which we check
|
5981 | 74409b12 | Iustin Pop | @type nodenames: list
|
5982 | 74409b12 | Iustin Pop | @param nodenames: the list of nodes on which we should check
|
5983 | 74409b12 | Iustin Pop | @type hvname: string
|
5984 | 74409b12 | Iustin Pop | @param hvname: the name of the hypervisor we should use
|
5985 | 74409b12 | Iustin Pop | @type hvparams: dict
|
5986 | 74409b12 | Iustin Pop | @param hvparams: the parameters which we need to check
|
5987 | 74409b12 | Iustin Pop | @raise errors.OpPrereqError: if the parameters are not valid
|
5988 | 74409b12 | Iustin Pop |
|
5989 | 74409b12 | Iustin Pop | """
|
5990 | 74409b12 | Iustin Pop | hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames, |
5991 | 74409b12 | Iustin Pop | hvname, |
5992 | 74409b12 | Iustin Pop | hvparams) |
5993 | 74409b12 | Iustin Pop | for node in nodenames: |
5994 | 781de953 | Iustin Pop | info = hvinfo[node] |
5995 | 68c6f21c | Iustin Pop | if info.offline:
|
5996 | 68c6f21c | Iustin Pop | continue
|
5997 | 4c4e4e1e | Iustin Pop | info.Raise("Hypervisor parameter validation failed on node %s" % node)
|
5998 | 74409b12 | Iustin Pop | |
5999 | 74409b12 | Iustin Pop | |
6000 | a8083063 | Iustin Pop | class LUCreateInstance(LogicalUnit): |
6001 | a8083063 | Iustin Pop | """Create an instance.
|
6002 | a8083063 | Iustin Pop |
|
6003 | a8083063 | Iustin Pop | """
|
6004 | a8083063 | Iustin Pop | HPATH = "instance-add"
|
6005 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
6006 | f276c4b5 | Iustin Pop | _OP_REQP = ["instance_name", "disks", |
6007 | 08db7c5c | Iustin Pop | "mode", "start", |
6008 | 08db7c5c | Iustin Pop | "wait_for_sync", "ip_check", "nics", |
6009 | 338e51e8 | Iustin Pop | "hvparams", "beparams"] |
6010 | 7baf741d | Guido Trotter | REQ_BGL = False
|
6011 | 7baf741d | Guido Trotter | |
6012 | 5f23e043 | Iustin Pop | def CheckArguments(self): |
6013 | 5f23e043 | Iustin Pop | """Check arguments.
|
6014 | 5f23e043 | Iustin Pop |
|
6015 | 5f23e043 | Iustin Pop | """
|
6016 | df4272e5 | Iustin Pop | # set optional parameters to none if they don't exist
|
6017 | f276c4b5 | Iustin Pop | for attr in ["pnode", "snode", "iallocator", "hypervisor", |
6018 | e588764d | Iustin Pop | "disk_template", "identify_defaults"]: |
6019 | df4272e5 | Iustin Pop | if not hasattr(self.op, attr): |
6020 | df4272e5 | Iustin Pop | setattr(self.op, attr, None) |
6021 | df4272e5 | Iustin Pop | |
6022 | 5f23e043 | Iustin Pop | # do not require name_check to ease forward/backward compatibility
|
6023 | 5f23e043 | Iustin Pop | # for tools
|
6024 | 5f23e043 | Iustin Pop | if not hasattr(self.op, "name_check"): |
6025 | 5f23e043 | Iustin Pop | self.op.name_check = True |
6026 | 25a8792c | Iustin Pop | if not hasattr(self.op, "no_install"): |
6027 | 25a8792c | Iustin Pop | self.op.no_install = False |
6028 | 25a8792c | Iustin Pop | if self.op.no_install and self.op.start: |
6029 | 25a8792c | Iustin Pop | self.LogInfo("No-installation mode selected, disabling startup") |
6030 | 25a8792c | Iustin Pop | self.op.start = False |
6031 | 44caf5a8 | Iustin Pop | # validate/normalize the instance name
|
6032 | 44caf5a8 | Iustin Pop | self.op.instance_name = utils.HostInfo.NormalizeName(self.op.instance_name) |
6033 | 5f23e043 | Iustin Pop | if self.op.ip_check and not self.op.name_check: |
6034 | 5f23e043 | Iustin Pop | # TODO: make the ip check more flexible and not depend on the name check
|
6035 | 5f23e043 | Iustin Pop | raise errors.OpPrereqError("Cannot do ip checks without a name check", |
6036 | 5f23e043 | Iustin Pop | errors.ECODE_INVAL) |
6037 | c3589cf8 | Iustin Pop | # check disk information: either all adopt, or no adopt
|
6038 | c3589cf8 | Iustin Pop | has_adopt = has_no_adopt = False
|
6039 | c3589cf8 | Iustin Pop | for disk in self.op.disks: |
6040 | c3589cf8 | Iustin Pop | if "adopt" in disk: |
6041 | c3589cf8 | Iustin Pop | has_adopt = True
|
6042 | c3589cf8 | Iustin Pop | else:
|
6043 | c3589cf8 | Iustin Pop | has_no_adopt = True
|
6044 | c3589cf8 | Iustin Pop | if has_adopt and has_no_adopt: |
6045 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("Either all disks are adopted or none is", |
6046 | c3589cf8 | Iustin Pop | errors.ECODE_INVAL) |
6047 | c3589cf8 | Iustin Pop | if has_adopt:
|
6048 | c3589cf8 | Iustin Pop | if self.op.disk_template != constants.DT_PLAIN: |
6049 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Disk adoption is only supported for the" |
6050 | c3589cf8 | Iustin Pop | " 'plain' disk template",
|
6051 | c3589cf8 | Iustin Pop | errors.ECODE_INVAL) |
6052 | c3589cf8 | Iustin Pop | if self.op.iallocator is not None: |
6053 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Disk adoption not allowed with an" |
6054 | c3589cf8 | Iustin Pop | " iallocator script", errors.ECODE_INVAL)
|
6055 | c3589cf8 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
6056 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Disk adoption not allowed for" |
6057 | c3589cf8 | Iustin Pop | " instance import", errors.ECODE_INVAL)
|
6058 | c3589cf8 | Iustin Pop | |
6059 | c3589cf8 | Iustin Pop | self.adopt_disks = has_adopt
|
6060 | 5f23e043 | Iustin Pop | |
6061 | 417eabe2 | Iustin Pop | # verify creation mode
|
6062 | 417eabe2 | Iustin Pop | if self.op.mode not in (constants.INSTANCE_CREATE, |
6063 | 417eabe2 | Iustin Pop | constants.INSTANCE_IMPORT): |
6064 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("Invalid instance creation mode '%s'" % |
6065 | 417eabe2 | Iustin Pop | self.op.mode, errors.ECODE_INVAL)
|
6066 | 417eabe2 | Iustin Pop | |
6067 | 417eabe2 | Iustin Pop | # instance name verification
|
6068 | 417eabe2 | Iustin Pop | if self.op.name_check: |
6069 | 417eabe2 | Iustin Pop | self.hostname1 = utils.GetHostInfo(self.op.instance_name) |
6070 | 417eabe2 | Iustin Pop | self.op.instance_name = self.hostname1.name |
6071 | 417eabe2 | Iustin Pop | # used in CheckPrereq for ip ping check
|
6072 | 417eabe2 | Iustin Pop | self.check_ip = self.hostname1.ip |
6073 | 417eabe2 | Iustin Pop | else:
|
6074 | 417eabe2 | Iustin Pop | self.check_ip = None |
6075 | 417eabe2 | Iustin Pop | |
6076 | 417eabe2 | Iustin Pop | # file storage checks
|
6077 | 417eabe2 | Iustin Pop | if (self.op.file_driver and |
6078 | 417eabe2 | Iustin Pop | not self.op.file_driver in constants.FILE_DRIVER): |
6079 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("Invalid file driver name '%s'" % |
6080 | 417eabe2 | Iustin Pop | self.op.file_driver, errors.ECODE_INVAL)
|
6081 | 417eabe2 | Iustin Pop | |
6082 | 417eabe2 | Iustin Pop | if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir): |
6083 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("File storage directory path not absolute", |
6084 | 417eabe2 | Iustin Pop | errors.ECODE_INVAL) |
6085 | 417eabe2 | Iustin Pop | |
6086 | 417eabe2 | Iustin Pop | ### Node/iallocator related checks
|
6087 | 417eabe2 | Iustin Pop | if [self.op.iallocator, self.op.pnode].count(None) != 1: |
6088 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("One and only one of iallocator and primary" |
6089 | 417eabe2 | Iustin Pop | " node must be given",
|
6090 | 417eabe2 | Iustin Pop | errors.ECODE_INVAL) |
6091 | 417eabe2 | Iustin Pop | |
6092 | 417eabe2 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
6093 | 417eabe2 | Iustin Pop | # On import force_variant must be True, because if we forced it at
|
6094 | 417eabe2 | Iustin Pop | # initial install, our only chance when importing it back is that it
|
6095 | 417eabe2 | Iustin Pop | # works again!
|
6096 | 417eabe2 | Iustin Pop | self.op.force_variant = True |
6097 | 417eabe2 | Iustin Pop | |
6098 | 417eabe2 | Iustin Pop | if self.op.no_install: |
6099 | 417eabe2 | Iustin Pop | self.LogInfo("No-installation mode has no effect during import") |
6100 | 417eabe2 | Iustin Pop | |
6101 | 417eabe2 | Iustin Pop | else: # INSTANCE_CREATE |
6102 | 417eabe2 | Iustin Pop | if getattr(self.op, "os_type", None) is None: |
6103 | 417eabe2 | Iustin Pop | raise errors.OpPrereqError("No guest OS specified", |
6104 | 417eabe2 | Iustin Pop | errors.ECODE_INVAL) |
6105 | 417eabe2 | Iustin Pop | self.op.force_variant = getattr(self.op, "force_variant", False) |
6106 | f276c4b5 | Iustin Pop | if self.op.disk_template is None: |
6107 | f276c4b5 | Iustin Pop | raise errors.OpPrereqError("No disk template specified", |
6108 | f276c4b5 | Iustin Pop | errors.ECODE_INVAL) |
6109 | 417eabe2 | Iustin Pop | |
6110 | 7baf741d | Guido Trotter | def ExpandNames(self): |
6111 | 7baf741d | Guido Trotter | """ExpandNames for CreateInstance.
|
6112 | 7baf741d | Guido Trotter |
|
6113 | 7baf741d | Guido Trotter | Figure out the right locks for instance creation.
|
6114 | 7baf741d | Guido Trotter |
|
6115 | 7baf741d | Guido Trotter | """
|
6116 | 7baf741d | Guido Trotter | self.needed_locks = {}
|
6117 | 7baf741d | Guido Trotter | |
6118 | 417eabe2 | Iustin Pop | instance_name = self.op.instance_name
|
6119 | 7baf741d | Guido Trotter | # this is just a preventive check, but someone might still add this
|
6120 | 7baf741d | Guido Trotter | # instance in the meantime, and creation will fail at lock-add time
|
6121 | 7baf741d | Guido Trotter | if instance_name in self.cfg.GetInstanceList(): |
6122 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
6123 | 5c983ee5 | Iustin Pop | instance_name, errors.ECODE_EXISTS) |
6124 | 7baf741d | Guido Trotter | |
6125 | 7baf741d | Guido Trotter | self.add_locks[locking.LEVEL_INSTANCE] = instance_name
|
6126 | 7baf741d | Guido Trotter | |
6127 | 7baf741d | Guido Trotter | if self.op.iallocator: |
6128 | 7baf741d | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
6129 | 7baf741d | Guido Trotter | else:
|
6130 | cf26a87a | Iustin Pop | self.op.pnode = _ExpandNodeName(self.cfg, self.op.pnode) |
6131 | 7baf741d | Guido Trotter | nodelist = [self.op.pnode]
|
6132 | 7baf741d | Guido Trotter | if self.op.snode is not None: |
6133 | cf26a87a | Iustin Pop | self.op.snode = _ExpandNodeName(self.cfg, self.op.snode) |
6134 | 7baf741d | Guido Trotter | nodelist.append(self.op.snode)
|
6135 | 7baf741d | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = nodelist
|
6136 | 7baf741d | Guido Trotter | |
6137 | 7baf741d | Guido Trotter | # in case of import lock the source node too
|
6138 | 7baf741d | Guido Trotter | if self.op.mode == constants.INSTANCE_IMPORT: |
6139 | 7baf741d | Guido Trotter | src_node = getattr(self.op, "src_node", None) |
6140 | 7baf741d | Guido Trotter | src_path = getattr(self.op, "src_path", None) |
6141 | 7baf741d | Guido Trotter | |
6142 | b9322a9f | Guido Trotter | if src_path is None: |
6143 | b9322a9f | Guido Trotter | self.op.src_path = src_path = self.op.instance_name |
6144 | b9322a9f | Guido Trotter | |
6145 | b9322a9f | Guido Trotter | if src_node is None: |
6146 | b9322a9f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
6147 | b9322a9f | Guido Trotter | self.op.src_node = None |
6148 | b9322a9f | Guido Trotter | if os.path.isabs(src_path):
|
6149 | b9322a9f | Guido Trotter | raise errors.OpPrereqError("Importing an instance from an absolute" |
6150 | 5c983ee5 | Iustin Pop | " path requires a source node option.",
|
6151 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
6152 | b9322a9f | Guido Trotter | else:
|
6153 | cf26a87a | Iustin Pop | self.op.src_node = src_node = _ExpandNodeName(self.cfg, src_node) |
6154 | b9322a9f | Guido Trotter | if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET: |
6155 | b9322a9f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE].append(src_node)
|
6156 | b9322a9f | Guido Trotter | if not os.path.isabs(src_path): |
6157 | b9322a9f | Guido Trotter | self.op.src_path = src_path = \
|
6158 | c4feafe8 | Iustin Pop | utils.PathJoin(constants.EXPORT_DIR, src_path) |
6159 | 7baf741d | Guido Trotter | |
6160 | 538475ca | Iustin Pop | def _RunAllocator(self): |
6161 | 538475ca | Iustin Pop | """Run the allocator based on input opcode.
|
6162 | 538475ca | Iustin Pop |
|
6163 | 538475ca | Iustin Pop | """
|
6164 | 08db7c5c | Iustin Pop | nics = [n.ToDict() for n in self.nics] |
6165 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
6166 | 29859cb7 | Iustin Pop | mode=constants.IALLOCATOR_MODE_ALLOC, |
6167 | d1c2dd75 | Iustin Pop | name=self.op.instance_name,
|
6168 | d1c2dd75 | Iustin Pop | disk_template=self.op.disk_template,
|
6169 | d1c2dd75 | Iustin Pop | tags=[], |
6170 | d1c2dd75 | Iustin Pop | os=self.op.os_type,
|
6171 | 338e51e8 | Iustin Pop | vcpus=self.be_full[constants.BE_VCPUS],
|
6172 | 338e51e8 | Iustin Pop | mem_size=self.be_full[constants.BE_MEMORY],
|
6173 | 08db7c5c | Iustin Pop | disks=self.disks,
|
6174 | d1c2dd75 | Iustin Pop | nics=nics, |
6175 | 8cc7e742 | Guido Trotter | hypervisor=self.op.hypervisor,
|
6176 | 29859cb7 | Iustin Pop | ) |
6177 | d1c2dd75 | Iustin Pop | |
6178 | d1c2dd75 | Iustin Pop | ial.Run(self.op.iallocator)
|
6179 | d1c2dd75 | Iustin Pop | |
6180 | d1c2dd75 | Iustin Pop | if not ial.success: |
6181 | 538475ca | Iustin Pop | raise errors.OpPrereqError("Can't compute nodes using" |
6182 | 5c983ee5 | Iustin Pop | " iallocator '%s': %s" %
|
6183 | 5c983ee5 | Iustin Pop | (self.op.iallocator, ial.info),
|
6184 | 5c983ee5 | Iustin Pop | errors.ECODE_NORES) |
6185 | 680f0a89 | Iustin Pop | if len(ial.result) != ial.required_nodes: |
6186 | 538475ca | Iustin Pop | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
6187 | 538475ca | Iustin Pop | " of nodes (%s), required %s" %
|
6188 | 680f0a89 | Iustin Pop | (self.op.iallocator, len(ial.result), |
6189 | 5c983ee5 | Iustin Pop | ial.required_nodes), errors.ECODE_FAULT) |
6190 | 680f0a89 | Iustin Pop | self.op.pnode = ial.result[0] |
6191 | 86d9d3bb | Iustin Pop | self.LogInfo("Selected nodes for instance %s via iallocator %s: %s", |
6192 | 86d9d3bb | Iustin Pop | self.op.instance_name, self.op.iallocator, |
6193 | 680f0a89 | Iustin Pop | utils.CommaJoin(ial.result)) |
6194 | 27579978 | Iustin Pop | if ial.required_nodes == 2: |
6195 | 680f0a89 | Iustin Pop | self.op.snode = ial.result[1] |
6196 | 538475ca | Iustin Pop | |
6197 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
6198 | a8083063 | Iustin Pop | """Build hooks env.
|
6199 | a8083063 | Iustin Pop |
|
6200 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
6201 | a8083063 | Iustin Pop |
|
6202 | a8083063 | Iustin Pop | """
|
6203 | a8083063 | Iustin Pop | env = { |
6204 | 2c2690c9 | Iustin Pop | "ADD_MODE": self.op.mode, |
6205 | a8083063 | Iustin Pop | } |
6206 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
6207 | 2c2690c9 | Iustin Pop | env["SRC_NODE"] = self.op.src_node |
6208 | 2c2690c9 | Iustin Pop | env["SRC_PATH"] = self.op.src_path |
6209 | 2c2690c9 | Iustin Pop | env["SRC_IMAGES"] = self.src_images |
6210 | 396e1b78 | Michael Hanselmann | |
6211 | 2c2690c9 | Iustin Pop | env.update(_BuildInstanceHookEnv( |
6212 | 2c2690c9 | Iustin Pop | name=self.op.instance_name,
|
6213 | 396e1b78 | Michael Hanselmann | primary_node=self.op.pnode,
|
6214 | 396e1b78 | Michael Hanselmann | secondary_nodes=self.secondaries,
|
6215 | 4978db17 | Iustin Pop | status=self.op.start,
|
6216 | ecb215b5 | Michael Hanselmann | os_type=self.op.os_type,
|
6217 | 338e51e8 | Iustin Pop | memory=self.be_full[constants.BE_MEMORY],
|
6218 | 338e51e8 | Iustin Pop | vcpus=self.be_full[constants.BE_VCPUS],
|
6219 | f9b10246 | Guido Trotter | nics=_NICListToTuple(self, self.nics), |
6220 | 2c2690c9 | Iustin Pop | disk_template=self.op.disk_template,
|
6221 | 2c2690c9 | Iustin Pop | disks=[(d["size"], d["mode"]) for d in self.disks], |
6222 | 67fc3042 | Iustin Pop | bep=self.be_full,
|
6223 | 67fc3042 | Iustin Pop | hvp=self.hv_full,
|
6224 | 3df6e710 | Iustin Pop | hypervisor_name=self.op.hypervisor,
|
6225 | 396e1b78 | Michael Hanselmann | )) |
6226 | a8083063 | Iustin Pop | |
6227 | d6a02168 | Michael Hanselmann | nl = ([self.cfg.GetMasterNode(), self.op.pnode] + |
6228 | a8083063 | Iustin Pop | self.secondaries)
|
6229 | a8083063 | Iustin Pop | return env, nl, nl
|
6230 | a8083063 | Iustin Pop | |
6231 | c1c31426 | Iustin Pop | def _ReadExportInfo(self): |
6232 | c1c31426 | Iustin Pop | """Reads the export information from disk.
|
6233 | c1c31426 | Iustin Pop |
|
6234 | c1c31426 | Iustin Pop | It will override the opcode source node and path with the actual
|
6235 | c1c31426 | Iustin Pop | information, if these two were not specified before.
|
6236 | c1c31426 | Iustin Pop |
|
6237 | c1c31426 | Iustin Pop | @return: the export information
|
6238 | c1c31426 | Iustin Pop |
|
6239 | c1c31426 | Iustin Pop | """
|
6240 | c1c31426 | Iustin Pop | assert self.op.mode == constants.INSTANCE_IMPORT |
6241 | c1c31426 | Iustin Pop | |
6242 | c1c31426 | Iustin Pop | src_node = self.op.src_node
|
6243 | c1c31426 | Iustin Pop | src_path = self.op.src_path
|
6244 | c1c31426 | Iustin Pop | |
6245 | c1c31426 | Iustin Pop | if src_node is None: |
6246 | c1c31426 | Iustin Pop | locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
|
6247 | c1c31426 | Iustin Pop | exp_list = self.rpc.call_export_list(locked_nodes)
|
6248 | c1c31426 | Iustin Pop | found = False
|
6249 | c1c31426 | Iustin Pop | for node in exp_list: |
6250 | c1c31426 | Iustin Pop | if exp_list[node].fail_msg:
|
6251 | c1c31426 | Iustin Pop | continue
|
6252 | c1c31426 | Iustin Pop | if src_path in exp_list[node].payload: |
6253 | c1c31426 | Iustin Pop | found = True
|
6254 | c1c31426 | Iustin Pop | self.op.src_node = src_node = node
|
6255 | c1c31426 | Iustin Pop | self.op.src_path = src_path = utils.PathJoin(constants.EXPORT_DIR,
|
6256 | c1c31426 | Iustin Pop | src_path) |
6257 | c1c31426 | Iustin Pop | break
|
6258 | c1c31426 | Iustin Pop | if not found: |
6259 | c1c31426 | Iustin Pop | raise errors.OpPrereqError("No export found for relative path %s" % |
6260 | c1c31426 | Iustin Pop | src_path, errors.ECODE_INVAL) |
6261 | c1c31426 | Iustin Pop | |
6262 | c1c31426 | Iustin Pop | _CheckNodeOnline(self, src_node)
|
6263 | c1c31426 | Iustin Pop | result = self.rpc.call_export_info(src_node, src_path)
|
6264 | c1c31426 | Iustin Pop | result.Raise("No export or invalid export found in dir %s" % src_path)
|
6265 | c1c31426 | Iustin Pop | |
6266 | c1c31426 | Iustin Pop | export_info = objects.SerializableConfigParser.Loads(str(result.payload))
|
6267 | c1c31426 | Iustin Pop | if not export_info.has_section(constants.INISECT_EXP): |
6268 | c1c31426 | Iustin Pop | raise errors.ProgrammerError("Corrupted export config", |
6269 | c1c31426 | Iustin Pop | errors.ECODE_ENVIRON) |
6270 | c1c31426 | Iustin Pop | |
6271 | c1c31426 | Iustin Pop | ei_version = export_info.get(constants.INISECT_EXP, "version")
|
6272 | c1c31426 | Iustin Pop | if (int(ei_version) != constants.EXPORT_VERSION): |
6273 | c1c31426 | Iustin Pop | raise errors.OpPrereqError("Wrong export version %s (wanted %d)" % |
6274 | c1c31426 | Iustin Pop | (ei_version, constants.EXPORT_VERSION), |
6275 | c1c31426 | Iustin Pop | errors.ECODE_ENVIRON) |
6276 | c1c31426 | Iustin Pop | return export_info
|
6277 | a8083063 | Iustin Pop | |
6278 | f276c4b5 | Iustin Pop | def _ReadExportParams(self, einfo): |
6279 | f276c4b5 | Iustin Pop | """Use export parameters as defaults.
|
6280 | f276c4b5 | Iustin Pop |
|
6281 | f276c4b5 | Iustin Pop | In case the opcode doesn't specify (as in override) some instance
|
6282 | f276c4b5 | Iustin Pop | parameters, then try to use them from the export information, if
|
6283 | f276c4b5 | Iustin Pop | that declares them.
|
6284 | f276c4b5 | Iustin Pop |
|
6285 | f276c4b5 | Iustin Pop | """
|
6286 | b6cd72b2 | Iustin Pop | self.op.os_type = einfo.get(constants.INISECT_EXP, "os") |
6287 | b6cd72b2 | Iustin Pop | |
6288 | f276c4b5 | Iustin Pop | if self.op.disk_template is None: |
6289 | f276c4b5 | Iustin Pop | if einfo.has_option(constants.INISECT_INS, "disk_template"): |
6290 | f276c4b5 | Iustin Pop | self.op.disk_template = einfo.get(constants.INISECT_INS,
|
6291 | f276c4b5 | Iustin Pop | "disk_template")
|
6292 | f276c4b5 | Iustin Pop | else:
|
6293 | f276c4b5 | Iustin Pop | raise errors.OpPrereqError("No disk template specified and the export" |
6294 | f276c4b5 | Iustin Pop | " is missing the disk_template information",
|
6295 | f276c4b5 | Iustin Pop | errors.ECODE_INVAL) |
6296 | f276c4b5 | Iustin Pop | |
6297 | 9b12ed0f | Iustin Pop | if not self.op.disks: |
6298 | 9b12ed0f | Iustin Pop | if einfo.has_option(constants.INISECT_INS, "disk_count"): |
6299 | 9b12ed0f | Iustin Pop | disks = [] |
6300 | 9b12ed0f | Iustin Pop | # TODO: import the disk iv_name too
|
6301 | 9b12ed0f | Iustin Pop | for idx in range(einfo.getint(constants.INISECT_INS, "disk_count")): |
6302 | 9b12ed0f | Iustin Pop | disk_sz = einfo.getint(constants.INISECT_INS, "disk%d_size" % idx)
|
6303 | 9b12ed0f | Iustin Pop | disks.append({"size": disk_sz})
|
6304 | 9b12ed0f | Iustin Pop | self.op.disks = disks
|
6305 | 9b12ed0f | Iustin Pop | else:
|
6306 | 9b12ed0f | Iustin Pop | raise errors.OpPrereqError("No disk info specified and the export" |
6307 | 9b12ed0f | Iustin Pop | " is missing the disk information",
|
6308 | 9b12ed0f | Iustin Pop | errors.ECODE_INVAL) |
6309 | 9b12ed0f | Iustin Pop | |
6310 | 0af0f641 | Iustin Pop | if (not self.op.nics and |
6311 | 0af0f641 | Iustin Pop | einfo.has_option(constants.INISECT_INS, "nic_count")):
|
6312 | 0af0f641 | Iustin Pop | nics = [] |
6313 | 0af0f641 | Iustin Pop | for idx in range(einfo.getint(constants.INISECT_INS, "nic_count")): |
6314 | 0af0f641 | Iustin Pop | ndict = {} |
6315 | 0af0f641 | Iustin Pop | for name in list(constants.NICS_PARAMETERS) + ["ip", "mac"]: |
6316 | 0af0f641 | Iustin Pop | v = einfo.get(constants.INISECT_INS, "nic%d_%s" % (idx, name))
|
6317 | 0af0f641 | Iustin Pop | ndict[name] = v |
6318 | 0af0f641 | Iustin Pop | nics.append(ndict) |
6319 | 0af0f641 | Iustin Pop | self.op.nics = nics
|
6320 | 0af0f641 | Iustin Pop | |
6321 | 9f88b0e8 | Iustin Pop | if (self.op.hypervisor is None and |
6322 | 9f88b0e8 | Iustin Pop | einfo.has_option(constants.INISECT_INS, "hypervisor")):
|
6323 | 9f88b0e8 | Iustin Pop | self.op.hypervisor = einfo.get(constants.INISECT_INS, "hypervisor") |
6324 | 9f88b0e8 | Iustin Pop | if einfo.has_section(constants.INISECT_HYP):
|
6325 | 9f88b0e8 | Iustin Pop | # use the export parameters but do not override the ones
|
6326 | 9f88b0e8 | Iustin Pop | # specified by the user
|
6327 | 9f88b0e8 | Iustin Pop | for name, value in einfo.items(constants.INISECT_HYP): |
6328 | 9f88b0e8 | Iustin Pop | if name not in self.op.hvparams: |
6329 | 9f88b0e8 | Iustin Pop | self.op.hvparams[name] = value
|
6330 | 9f88b0e8 | Iustin Pop | |
6331 | cc0d88e9 | Iustin Pop | if einfo.has_section(constants.INISECT_BEP):
|
6332 | cc0d88e9 | Iustin Pop | # use the parameters, without overriding
|
6333 | cc0d88e9 | Iustin Pop | for name, value in einfo.items(constants.INISECT_BEP): |
6334 | cc0d88e9 | Iustin Pop | if name not in self.op.beparams: |
6335 | cc0d88e9 | Iustin Pop | self.op.beparams[name] = value
|
6336 | cc0d88e9 | Iustin Pop | else:
|
6337 | cc0d88e9 | Iustin Pop | # try to read the parameters old style, from the main section
|
6338 | cc0d88e9 | Iustin Pop | for name in constants.BES_PARAMETERS: |
6339 | cc0d88e9 | Iustin Pop | if (name not in self.op.beparams and |
6340 | cc0d88e9 | Iustin Pop | einfo.has_option(constants.INISECT_INS, name)): |
6341 | cc0d88e9 | Iustin Pop | self.op.beparams[name] = einfo.get(constants.INISECT_INS, name)
|
6342 | cc0d88e9 | Iustin Pop | |
6343 | e588764d | Iustin Pop | def _RevertToDefaults(self, cluster): |
6344 | e588764d | Iustin Pop | """Revert the instance parameters to the default values.
|
6345 | e588764d | Iustin Pop |
|
6346 | e588764d | Iustin Pop | """
|
6347 | e588764d | Iustin Pop | # hvparams
|
6348 | e588764d | Iustin Pop | hv_defs = cluster.GetHVDefaults(self.op.hypervisor, self.op.os_type) |
6349 | e588764d | Iustin Pop | for name in self.op.hvparams.keys(): |
6350 | e588764d | Iustin Pop | if name in hv_defs and hv_defs[name] == self.op.hvparams[name]: |
6351 | e588764d | Iustin Pop | del self.op.hvparams[name] |
6352 | e588764d | Iustin Pop | # beparams
|
6353 | e588764d | Iustin Pop | be_defs = cluster.beparams.get(constants.PP_DEFAULT, {}) |
6354 | e588764d | Iustin Pop | for name in self.op.beparams.keys(): |
6355 | e588764d | Iustin Pop | if name in be_defs and be_defs[name] == self.op.beparams[name]: |
6356 | e588764d | Iustin Pop | del self.op.beparams[name] |
6357 | e588764d | Iustin Pop | # nic params
|
6358 | e588764d | Iustin Pop | nic_defs = cluster.nicparams.get(constants.PP_DEFAULT, {}) |
6359 | e588764d | Iustin Pop | for nic in self.op.nics: |
6360 | e588764d | Iustin Pop | for name in constants.NICS_PARAMETERS: |
6361 | e588764d | Iustin Pop | if name in nic and name in nic_defs and nic[name] == nic_defs[name]: |
6362 | e588764d | Iustin Pop | del nic[name]
|
6363 | e588764d | Iustin Pop | |
6364 | a8083063 | Iustin Pop | def CheckPrereq(self): |
6365 | a8083063 | Iustin Pop | """Check prerequisites.
|
6366 | a8083063 | Iustin Pop |
|
6367 | a8083063 | Iustin Pop | """
|
6368 | c1c31426 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
6369 | c1c31426 | Iustin Pop | export_info = self._ReadExportInfo()
|
6370 | f276c4b5 | Iustin Pop | self._ReadExportParams(export_info)
|
6371 | f276c4b5 | Iustin Pop | |
6372 | f276c4b5 | Iustin Pop | _CheckDiskTemplate(self.op.disk_template)
|
6373 | c1c31426 | Iustin Pop | |
6374 | eedc99de | Manuel Franceschini | if (not self.cfg.GetVGName() and |
6375 | eedc99de | Manuel Franceschini | self.op.disk_template not in constants.DTS_NOT_LVM): |
6376 | eedc99de | Manuel Franceschini | raise errors.OpPrereqError("Cluster does not support lvm-based" |
6377 | 5c983ee5 | Iustin Pop | " instances", errors.ECODE_STATE)
|
6378 | eedc99de | Manuel Franceschini | |
6379 | 22f50b1d | Iustin Pop | if self.op.hypervisor is None: |
6380 | 22f50b1d | Iustin Pop | self.op.hypervisor = self.cfg.GetHypervisorType() |
6381 | 22f50b1d | Iustin Pop | |
6382 | 22f50b1d | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
6383 | 22f50b1d | Iustin Pop | enabled_hvs = cluster.enabled_hypervisors |
6384 | 22f50b1d | Iustin Pop | if self.op.hypervisor not in enabled_hvs: |
6385 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the" |
6386 | 22f50b1d | Iustin Pop | " cluster (%s)" % (self.op.hypervisor, |
6387 | 22f50b1d | Iustin Pop | ",".join(enabled_hvs)),
|
6388 | 22f50b1d | Iustin Pop | errors.ECODE_STATE) |
6389 | 22f50b1d | Iustin Pop | |
6390 | 22f50b1d | Iustin Pop | # check hypervisor parameter syntax (locally)
|
6391 | 22f50b1d | Iustin Pop | utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
|
6392 | b6cd72b2 | Iustin Pop | filled_hvp = objects.FillDict(cluster.GetHVDefaults(self.op.hypervisor,
|
6393 | b6cd72b2 | Iustin Pop | self.op.os_type),
|
6394 | 22f50b1d | Iustin Pop | self.op.hvparams)
|
6395 | 22f50b1d | Iustin Pop | hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
|
6396 | 22f50b1d | Iustin Pop | hv_type.CheckParameterSyntax(filled_hvp) |
6397 | 22f50b1d | Iustin Pop | self.hv_full = filled_hvp
|
6398 | 22f50b1d | Iustin Pop | # check that we don't specify global parameters on an instance
|
6399 | 22f50b1d | Iustin Pop | _CheckGlobalHvParams(self.op.hvparams)
|
6400 | 22f50b1d | Iustin Pop | |
6401 | 22f50b1d | Iustin Pop | # fill and remember the beparams dict
|
6402 | 22f50b1d | Iustin Pop | utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
|
6403 | 22f50b1d | Iustin Pop | self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT],
|
6404 | 22f50b1d | Iustin Pop | self.op.beparams)
|
6405 | 22f50b1d | Iustin Pop | |
6406 | e588764d | Iustin Pop | # now that hvp/bep are in final format, let's reset to defaults,
|
6407 | e588764d | Iustin Pop | # if told to do so
|
6408 | e588764d | Iustin Pop | if self.op.identify_defaults: |
6409 | e588764d | Iustin Pop | self._RevertToDefaults(cluster)
|
6410 | e588764d | Iustin Pop | |
6411 | 22f50b1d | Iustin Pop | # NIC buildup
|
6412 | 22f50b1d | Iustin Pop | self.nics = []
|
6413 | 22f50b1d | Iustin Pop | for idx, nic in enumerate(self.op.nics): |
6414 | 22f50b1d | Iustin Pop | nic_mode_req = nic.get("mode", None) |
6415 | 22f50b1d | Iustin Pop | nic_mode = nic_mode_req |
6416 | 22f50b1d | Iustin Pop | if nic_mode is None: |
6417 | 22f50b1d | Iustin Pop | nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE] |
6418 | 22f50b1d | Iustin Pop | |
6419 | 22f50b1d | Iustin Pop | # in routed mode, for the first nic, the default ip is 'auto'
|
6420 | 22f50b1d | Iustin Pop | if nic_mode == constants.NIC_MODE_ROUTED and idx == 0: |
6421 | 22f50b1d | Iustin Pop | default_ip_mode = constants.VALUE_AUTO |
6422 | 22f50b1d | Iustin Pop | else:
|
6423 | 22f50b1d | Iustin Pop | default_ip_mode = constants.VALUE_NONE |
6424 | 22f50b1d | Iustin Pop | |
6425 | 22f50b1d | Iustin Pop | # ip validity checks
|
6426 | 22f50b1d | Iustin Pop | ip = nic.get("ip", default_ip_mode)
|
6427 | 22f50b1d | Iustin Pop | if ip is None or ip.lower() == constants.VALUE_NONE: |
6428 | 22f50b1d | Iustin Pop | nic_ip = None
|
6429 | 22f50b1d | Iustin Pop | elif ip.lower() == constants.VALUE_AUTO:
|
6430 | 22f50b1d | Iustin Pop | if not self.op.name_check: |
6431 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("IP address set to auto but name checks" |
6432 | 22f50b1d | Iustin Pop | " have been skipped. Aborting.",
|
6433 | 22f50b1d | Iustin Pop | errors.ECODE_INVAL) |
6434 | 22f50b1d | Iustin Pop | nic_ip = self.hostname1.ip
|
6435 | 22f50b1d | Iustin Pop | else:
|
6436 | 22f50b1d | Iustin Pop | if not utils.IsValidIP(ip): |
6437 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Given IP address '%s' doesn't look" |
6438 | 22f50b1d | Iustin Pop | " like a valid IP" % ip,
|
6439 | 22f50b1d | Iustin Pop | errors.ECODE_INVAL) |
6440 | 22f50b1d | Iustin Pop | nic_ip = ip |
6441 | 22f50b1d | Iustin Pop | |
6442 | 22f50b1d | Iustin Pop | # TODO: check the ip address for uniqueness
|
6443 | 22f50b1d | Iustin Pop | if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip: |
6444 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Routed nic mode requires an ip address", |
6445 | 22f50b1d | Iustin Pop | errors.ECODE_INVAL) |
6446 | 22f50b1d | Iustin Pop | |
6447 | 22f50b1d | Iustin Pop | # MAC address verification
|
6448 | 22f50b1d | Iustin Pop | mac = nic.get("mac", constants.VALUE_AUTO)
|
6449 | 22f50b1d | Iustin Pop | if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
6450 | 22f50b1d | Iustin Pop | mac = utils.NormalizeAndValidateMac(mac) |
6451 | 22f50b1d | Iustin Pop | |
6452 | 22f50b1d | Iustin Pop | try:
|
6453 | 22f50b1d | Iustin Pop | self.cfg.ReserveMAC(mac, self.proc.GetECId()) |
6454 | 22f50b1d | Iustin Pop | except errors.ReservationError:
|
6455 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("MAC address %s already in use" |
6456 | 22f50b1d | Iustin Pop | " in cluster" % mac,
|
6457 | 22f50b1d | Iustin Pop | errors.ECODE_NOTUNIQUE) |
6458 | 22f50b1d | Iustin Pop | |
6459 | 22f50b1d | Iustin Pop | # bridge verification
|
6460 | 22f50b1d | Iustin Pop | bridge = nic.get("bridge", None) |
6461 | 22f50b1d | Iustin Pop | link = nic.get("link", None) |
6462 | 22f50b1d | Iustin Pop | if bridge and link: |
6463 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'" |
6464 | 22f50b1d | Iustin Pop | " at the same time", errors.ECODE_INVAL)
|
6465 | 22f50b1d | Iustin Pop | elif bridge and nic_mode == constants.NIC_MODE_ROUTED: |
6466 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Cannot pass 'bridge' on a routed nic", |
6467 | 22f50b1d | Iustin Pop | errors.ECODE_INVAL) |
6468 | 22f50b1d | Iustin Pop | elif bridge:
|
6469 | 22f50b1d | Iustin Pop | link = bridge |
6470 | 22f50b1d | Iustin Pop | |
6471 | 22f50b1d | Iustin Pop | nicparams = {} |
6472 | 22f50b1d | Iustin Pop | if nic_mode_req:
|
6473 | 22f50b1d | Iustin Pop | nicparams[constants.NIC_MODE] = nic_mode_req |
6474 | 22f50b1d | Iustin Pop | if link:
|
6475 | 22f50b1d | Iustin Pop | nicparams[constants.NIC_LINK] = link |
6476 | 22f50b1d | Iustin Pop | |
6477 | 22f50b1d | Iustin Pop | check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], |
6478 | 22f50b1d | Iustin Pop | nicparams) |
6479 | 22f50b1d | Iustin Pop | objects.NIC.CheckParameterSyntax(check_params) |
6480 | 22f50b1d | Iustin Pop | self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
|
6481 | 22f50b1d | Iustin Pop | |
6482 | 22f50b1d | Iustin Pop | # disk checks/pre-build
|
6483 | 22f50b1d | Iustin Pop | self.disks = []
|
6484 | 22f50b1d | Iustin Pop | for disk in self.op.disks: |
6485 | 22f50b1d | Iustin Pop | mode = disk.get("mode", constants.DISK_RDWR)
|
6486 | 22f50b1d | Iustin Pop | if mode not in constants.DISK_ACCESS_SET: |
6487 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Invalid disk access mode '%s'" % |
6488 | 22f50b1d | Iustin Pop | mode, errors.ECODE_INVAL) |
6489 | 22f50b1d | Iustin Pop | size = disk.get("size", None) |
6490 | 22f50b1d | Iustin Pop | if size is None: |
6491 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL) |
6492 | 22f50b1d | Iustin Pop | try:
|
6493 | 22f50b1d | Iustin Pop | size = int(size)
|
6494 | 22f50b1d | Iustin Pop | except (TypeError, ValueError): |
6495 | 22f50b1d | Iustin Pop | raise errors.OpPrereqError("Invalid disk size '%s'" % size, |
6496 | 22f50b1d | Iustin Pop | errors.ECODE_INVAL) |
6497 | 22f50b1d | Iustin Pop | new_disk = {"size": size, "mode": mode} |
6498 | 22f50b1d | Iustin Pop | if "adopt" in disk: |
6499 | 22f50b1d | Iustin Pop | new_disk["adopt"] = disk["adopt"] |
6500 | 22f50b1d | Iustin Pop | self.disks.append(new_disk)
|
6501 | 22f50b1d | Iustin Pop | |
6502 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
6503 | a8083063 | Iustin Pop | |
6504 | 09acf207 | Guido Trotter | # Check that the new instance doesn't have less disks than the export
|
6505 | 08db7c5c | Iustin Pop | instance_disks = len(self.disks) |
6506 | 09acf207 | Guido Trotter | export_disks = export_info.getint(constants.INISECT_INS, 'disk_count')
|
6507 | 09acf207 | Guido Trotter | if instance_disks < export_disks:
|
6508 | 09acf207 | Guido Trotter | raise errors.OpPrereqError("Not enough disks to import." |
6509 | 09acf207 | Guido Trotter | " (instance: %d, export: %d)" %
|
6510 | 5c983ee5 | Iustin Pop | (instance_disks, export_disks), |
6511 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
6512 | a8083063 | Iustin Pop | |
6513 | 09acf207 | Guido Trotter | disk_images = [] |
6514 | 09acf207 | Guido Trotter | for idx in range(export_disks): |
6515 | 09acf207 | Guido Trotter | option = 'disk%d_dump' % idx
|
6516 | 09acf207 | Guido Trotter | if export_info.has_option(constants.INISECT_INS, option):
|
6517 | 09acf207 | Guido Trotter | # FIXME: are the old os-es, disk sizes, etc. useful?
|
6518 | 09acf207 | Guido Trotter | export_name = export_info.get(constants.INISECT_INS, option) |
6519 | c1c31426 | Iustin Pop | image = utils.PathJoin(self.op.src_path, export_name)
|
6520 | 09acf207 | Guido Trotter | disk_images.append(image) |
6521 | 09acf207 | Guido Trotter | else:
|
6522 | 09acf207 | Guido Trotter | disk_images.append(False)
|
6523 | 09acf207 | Guido Trotter | |
6524 | 09acf207 | Guido Trotter | self.src_images = disk_images
|
6525 | 901a65c1 | Iustin Pop | |
6526 | b4364a6b | Guido Trotter | old_name = export_info.get(constants.INISECT_INS, 'name')
|
6527 | 2a518543 | Iustin Pop | try:
|
6528 | 2a518543 | Iustin Pop | exp_nic_count = export_info.getint(constants.INISECT_INS, 'nic_count')
|
6529 | 2a518543 | Iustin Pop | except (TypeError, ValueError), err: |
6530 | 2a518543 | Iustin Pop | raise errors.OpPrereqError("Invalid export file, nic_count is not" |
6531 | 2a518543 | Iustin Pop | " an integer: %s" % str(err), |
6532 | 2a518543 | Iustin Pop | errors.ECODE_STATE) |
6533 | b4364a6b | Guido Trotter | if self.op.instance_name == old_name: |
6534 | b4364a6b | Guido Trotter | for idx, nic in enumerate(self.nics): |
6535 | b4364a6b | Guido Trotter | if nic.mac == constants.VALUE_AUTO and exp_nic_count >= idx: |
6536 | b4364a6b | Guido Trotter | nic_mac_ini = 'nic%d_mac' % idx
|
6537 | b4364a6b | Guido Trotter | nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini) |
6538 | bc89efc3 | Guido Trotter | |
6539 | 295728df | Guido Trotter | # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
|
6540 | 901a65c1 | Iustin Pop | |
6541 | 18c8f361 | Iustin Pop | # ip ping checks (we use the same ip that was resolved in ExpandNames)
|
6542 | 901a65c1 | Iustin Pop | if self.op.ip_check: |
6543 | 7baf741d | Guido Trotter | if utils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT): |
6544 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
6545 | 5c983ee5 | Iustin Pop | (self.check_ip, self.op.instance_name), |
6546 | 5c983ee5 | Iustin Pop | errors.ECODE_NOTUNIQUE) |
6547 | 901a65c1 | Iustin Pop | |
6548 | 295728df | Guido Trotter | #### mac address generation
|
6549 | 295728df | Guido Trotter | # By generating here the mac address both the allocator and the hooks get
|
6550 | 295728df | Guido Trotter | # the real final mac address rather than the 'auto' or 'generate' value.
|
6551 | 295728df | Guido Trotter | # There is a race condition between the generation and the instance object
|
6552 | 295728df | Guido Trotter | # creation, which means that we know the mac is valid now, but we're not
|
6553 | 295728df | Guido Trotter | # sure it will be when we actually add the instance. If things go bad
|
6554 | 295728df | Guido Trotter | # adding the instance will abort because of a duplicate mac, and the
|
6555 | 295728df | Guido Trotter | # creation job will fail.
|
6556 | 295728df | Guido Trotter | for nic in self.nics: |
6557 | 295728df | Guido Trotter | if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
6558 | 36b66e6e | Guido Trotter | nic.mac = self.cfg.GenerateMAC(self.proc.GetECId()) |
6559 | 295728df | Guido Trotter | |
6560 | 538475ca | Iustin Pop | #### allocator run
|
6561 | 538475ca | Iustin Pop | |
6562 | 538475ca | Iustin Pop | if self.op.iallocator is not None: |
6563 | 538475ca | Iustin Pop | self._RunAllocator()
|
6564 | 0f1a06e3 | Manuel Franceschini | |
6565 | 901a65c1 | Iustin Pop | #### node related checks
|
6566 | 901a65c1 | Iustin Pop | |
6567 | 901a65c1 | Iustin Pop | # check primary node
|
6568 | 7baf741d | Guido Trotter | self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode) |
6569 | 7baf741d | Guido Trotter | assert self.pnode is not None, \ |
6570 | 7baf741d | Guido Trotter | "Cannot retrieve locked node %s" % self.op.pnode |
6571 | 7527a8a4 | Iustin Pop | if pnode.offline:
|
6572 | 7527a8a4 | Iustin Pop | raise errors.OpPrereqError("Cannot use offline primary node '%s'" % |
6573 | 5c983ee5 | Iustin Pop | pnode.name, errors.ECODE_STATE) |
6574 | 733a2b6a | Iustin Pop | if pnode.drained:
|
6575 | 733a2b6a | Iustin Pop | raise errors.OpPrereqError("Cannot use drained primary node '%s'" % |
6576 | 5c983ee5 | Iustin Pop | pnode.name, errors.ECODE_STATE) |
6577 | 7527a8a4 | Iustin Pop | |
6578 | 901a65c1 | Iustin Pop | self.secondaries = []
|
6579 | 901a65c1 | Iustin Pop | |
6580 | 901a65c1 | Iustin Pop | # mirror node verification
|
6581 | a1f445d3 | Iustin Pop | if self.op.disk_template in constants.DTS_NET_MIRROR: |
6582 | 7baf741d | Guido Trotter | if self.op.snode is None: |
6583 | a1f445d3 | Iustin Pop | raise errors.OpPrereqError("The networked disk templates need" |
6584 | 5c983ee5 | Iustin Pop | " a mirror node", errors.ECODE_INVAL)
|
6585 | 7baf741d | Guido Trotter | if self.op.snode == pnode.name: |
6586 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("The secondary node cannot be the" |
6587 | 5c983ee5 | Iustin Pop | " primary node.", errors.ECODE_INVAL)
|
6588 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, self.op.snode) |
6589 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, self.op.snode) |
6590 | 733a2b6a | Iustin Pop | self.secondaries.append(self.op.snode) |
6591 | a8083063 | Iustin Pop | |
6592 | 6785674e | Iustin Pop | nodenames = [pnode.name] + self.secondaries
|
6593 | 6785674e | Iustin Pop | |
6594 | e2fe6369 | Iustin Pop | req_size = _ComputeDiskSize(self.op.disk_template,
|
6595 | 08db7c5c | Iustin Pop | self.disks)
|
6596 | ed1ebc60 | Guido Trotter | |
6597 | c3589cf8 | Iustin Pop | # Check lv size requirements, if not adopting
|
6598 | c3589cf8 | Iustin Pop | if req_size is not None and not self.adopt_disks: |
6599 | 701384a9 | Iustin Pop | _CheckNodesFreeDisk(self, nodenames, req_size)
|
6600 | ed1ebc60 | Guido Trotter | |
6601 | c3589cf8 | Iustin Pop | if self.adopt_disks: # instead, we must check the adoption data |
6602 | c3589cf8 | Iustin Pop | all_lvs = set([i["adopt"] for i in self.disks]) |
6603 | c3589cf8 | Iustin Pop | if len(all_lvs) != len(self.disks): |
6604 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Duplicate volume names given for adoption", |
6605 | c3589cf8 | Iustin Pop | errors.ECODE_INVAL) |
6606 | c3589cf8 | Iustin Pop | for lv_name in all_lvs: |
6607 | c3589cf8 | Iustin Pop | try:
|
6608 | c3589cf8 | Iustin Pop | self.cfg.ReserveLV(lv_name, self.proc.GetECId()) |
6609 | c3589cf8 | Iustin Pop | except errors.ReservationError:
|
6610 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("LV named %s used by another instance" % |
6611 | c3589cf8 | Iustin Pop | lv_name, errors.ECODE_NOTUNIQUE) |
6612 | c3589cf8 | Iustin Pop | |
6613 | c3589cf8 | Iustin Pop | node_lvs = self.rpc.call_lv_list([pnode.name],
|
6614 | c3589cf8 | Iustin Pop | self.cfg.GetVGName())[pnode.name]
|
6615 | c3589cf8 | Iustin Pop | node_lvs.Raise("Cannot get LV information from node %s" % pnode.name)
|
6616 | c3589cf8 | Iustin Pop | node_lvs = node_lvs.payload |
6617 | c3589cf8 | Iustin Pop | delta = all_lvs.difference(node_lvs.keys()) |
6618 | c3589cf8 | Iustin Pop | if delta:
|
6619 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Missing logical volume(s): %s" % |
6620 | c3589cf8 | Iustin Pop | utils.CommaJoin(delta), |
6621 | c3589cf8 | Iustin Pop | errors.ECODE_INVAL) |
6622 | c3589cf8 | Iustin Pop | online_lvs = [lv for lv in all_lvs if node_lvs[lv][2]] |
6623 | c3589cf8 | Iustin Pop | if online_lvs:
|
6624 | c3589cf8 | Iustin Pop | raise errors.OpPrereqError("Online logical volumes found, cannot" |
6625 | c3589cf8 | Iustin Pop | " adopt: %s" % utils.CommaJoin(online_lvs),
|
6626 | c3589cf8 | Iustin Pop | errors.ECODE_STATE) |
6627 | c3589cf8 | Iustin Pop | # update the size of disk based on what is found
|
6628 | c3589cf8 | Iustin Pop | for dsk in self.disks: |
6629 | c3589cf8 | Iustin Pop | dsk["size"] = int(float(node_lvs[dsk["adopt"]][0])) |
6630 | c3589cf8 | Iustin Pop | |
6631 | 74409b12 | Iustin Pop | _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams) |
6632 | 6785674e | Iustin Pop | |
6633 | 231cd901 | Iustin Pop | _CheckNodeHasOS(self, pnode.name, self.op.os_type, self.op.force_variant) |
6634 | a8083063 | Iustin Pop | |
6635 | b165e77e | Guido Trotter | _CheckNicsBridgesExist(self, self.nics, self.pnode.name) |
6636 | a8083063 | Iustin Pop | |
6637 | 49ce1563 | Iustin Pop | # memory check on primary node
|
6638 | 49ce1563 | Iustin Pop | if self.op.start: |
6639 | b9bddb6b | Iustin Pop | _CheckNodeFreeMemory(self, self.pnode.name, |
6640 | 49ce1563 | Iustin Pop | "creating instance %s" % self.op.instance_name, |
6641 | 338e51e8 | Iustin Pop | self.be_full[constants.BE_MEMORY],
|
6642 | 338e51e8 | Iustin Pop | self.op.hypervisor)
|
6643 | 49ce1563 | Iustin Pop | |
6644 | 08896026 | Iustin Pop | self.dry_run_result = list(nodenames) |
6645 | 08896026 | Iustin Pop | |
6646 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
6647 | a8083063 | Iustin Pop | """Create and add the instance to the cluster.
|
6648 | a8083063 | Iustin Pop |
|
6649 | a8083063 | Iustin Pop | """
|
6650 | a8083063 | Iustin Pop | instance = self.op.instance_name
|
6651 | a8083063 | Iustin Pop | pnode_name = self.pnode.name
|
6652 | a8083063 | Iustin Pop | |
6653 | e69d05fd | Iustin Pop | ht_kind = self.op.hypervisor
|
6654 | 2a6469d5 | Alexander Schreiber | if ht_kind in constants.HTS_REQ_PORT: |
6655 | 2a6469d5 | Alexander Schreiber | network_port = self.cfg.AllocatePort()
|
6656 | 2a6469d5 | Alexander Schreiber | else:
|
6657 | 2a6469d5 | Alexander Schreiber | network_port = None
|
6658 | 58acb49d | Alexander Schreiber | |
6659 | 0e3baaf3 | Iustin Pop | if constants.ENABLE_FILE_STORAGE:
|
6660 | 0e3baaf3 | Iustin Pop | # this is needed because os.path.join does not accept None arguments
|
6661 | 0e3baaf3 | Iustin Pop | if self.op.file_storage_dir is None: |
6662 | 0e3baaf3 | Iustin Pop | string_file_storage_dir = ""
|
6663 | 0e3baaf3 | Iustin Pop | else:
|
6664 | 0e3baaf3 | Iustin Pop | string_file_storage_dir = self.op.file_storage_dir
|
6665 | 31a853d2 | Iustin Pop | |
6666 | 0e3baaf3 | Iustin Pop | # build the full file storage dir path
|
6667 | 0e3baaf3 | Iustin Pop | file_storage_dir = utils.PathJoin(self.cfg.GetFileStorageDir(),
|
6668 | 0e3baaf3 | Iustin Pop | string_file_storage_dir, instance) |
6669 | 2c313123 | Manuel Franceschini | else:
|
6670 | 0e3baaf3 | Iustin Pop | file_storage_dir = ""
|
6671 | 0f1a06e3 | Manuel Franceschini | |
6672 | 0f1a06e3 | Manuel Franceschini | |
6673 | b9bddb6b | Iustin Pop | disks = _GenerateDiskTemplate(self,
|
6674 | a8083063 | Iustin Pop | self.op.disk_template,
|
6675 | a8083063 | Iustin Pop | instance, pnode_name, |
6676 | 08db7c5c | Iustin Pop | self.secondaries,
|
6677 | 08db7c5c | Iustin Pop | self.disks,
|
6678 | 0f1a06e3 | Manuel Franceschini | file_storage_dir, |
6679 | e2a65344 | Iustin Pop | self.op.file_driver,
|
6680 | e2a65344 | Iustin Pop | 0)
|
6681 | a8083063 | Iustin Pop | |
6682 | a8083063 | Iustin Pop | iobj = objects.Instance(name=instance, os=self.op.os_type,
|
6683 | a8083063 | Iustin Pop | primary_node=pnode_name, |
6684 | 08db7c5c | Iustin Pop | nics=self.nics, disks=disks,
|
6685 | a8083063 | Iustin Pop | disk_template=self.op.disk_template,
|
6686 | 4978db17 | Iustin Pop | admin_up=False,
|
6687 | 58acb49d | Alexander Schreiber | network_port=network_port, |
6688 | 338e51e8 | Iustin Pop | beparams=self.op.beparams,
|
6689 | 6785674e | Iustin Pop | hvparams=self.op.hvparams,
|
6690 | e69d05fd | Iustin Pop | hypervisor=self.op.hypervisor,
|
6691 | a8083063 | Iustin Pop | ) |
6692 | a8083063 | Iustin Pop | |
6693 | c3589cf8 | Iustin Pop | if self.adopt_disks: |
6694 | c3589cf8 | Iustin Pop | # rename LVs to the newly-generated names; we need to construct
|
6695 | c3589cf8 | Iustin Pop | # 'fake' LV disks with the old data, plus the new unique_id
|
6696 | c3589cf8 | Iustin Pop | tmp_disks = [objects.Disk.FromDict(v.ToDict()) for v in disks] |
6697 | c3589cf8 | Iustin Pop | rename_to = [] |
6698 | c3589cf8 | Iustin Pop | for t_dsk, a_dsk in zip (tmp_disks, self.disks): |
6699 | c3589cf8 | Iustin Pop | rename_to.append(t_dsk.logical_id) |
6700 | c3589cf8 | Iustin Pop | t_dsk.logical_id = (t_dsk.logical_id[0], a_dsk["adopt"]) |
6701 | c3589cf8 | Iustin Pop | self.cfg.SetDiskID(t_dsk, pnode_name)
|
6702 | c3589cf8 | Iustin Pop | result = self.rpc.call_blockdev_rename(pnode_name,
|
6703 | c3589cf8 | Iustin Pop | zip(tmp_disks, rename_to))
|
6704 | c3589cf8 | Iustin Pop | result.Raise("Failed to rename adoped LVs")
|
6705 | c3589cf8 | Iustin Pop | else:
|
6706 | c3589cf8 | Iustin Pop | feedback_fn("* creating instance disks...")
|
6707 | 796cab27 | Iustin Pop | try:
|
6708 | c3589cf8 | Iustin Pop | _CreateDisks(self, iobj)
|
6709 | c3589cf8 | Iustin Pop | except errors.OpExecError:
|
6710 | c3589cf8 | Iustin Pop | self.LogWarning("Device creation failed, reverting...") |
6711 | c3589cf8 | Iustin Pop | try:
|
6712 | c3589cf8 | Iustin Pop | _RemoveDisks(self, iobj)
|
6713 | c3589cf8 | Iustin Pop | finally:
|
6714 | c3589cf8 | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance)
|
6715 | c3589cf8 | Iustin Pop | raise
|
6716 | a8083063 | Iustin Pop | |
6717 | a8083063 | Iustin Pop | feedback_fn("adding instance %s to cluster config" % instance)
|
6718 | a8083063 | Iustin Pop | |
6719 | 0debfb35 | Guido Trotter | self.cfg.AddInstance(iobj, self.proc.GetECId()) |
6720 | 0debfb35 | Guido Trotter | |
6721 | 7baf741d | Guido Trotter | # Declare that we don't want to remove the instance lock anymore, as we've
|
6722 | 7baf741d | Guido Trotter | # added the instance to the config
|
6723 | 7baf741d | Guido Trotter | del self.remove_locks[locking.LEVEL_INSTANCE] |
6724 | e36e96b4 | Guido Trotter | # Unlock all the nodes
|
6725 | 9c8971d7 | Guido Trotter | if self.op.mode == constants.INSTANCE_IMPORT: |
6726 | 9c8971d7 | Guido Trotter | nodes_keep = [self.op.src_node]
|
6727 | 9c8971d7 | Guido Trotter | nodes_release = [node for node in self.acquired_locks[locking.LEVEL_NODE] |
6728 | 9c8971d7 | Guido Trotter | if node != self.op.src_node] |
6729 | 9c8971d7 | Guido Trotter | self.context.glm.release(locking.LEVEL_NODE, nodes_release)
|
6730 | 9c8971d7 | Guido Trotter | self.acquired_locks[locking.LEVEL_NODE] = nodes_keep
|
6731 | 9c8971d7 | Guido Trotter | else:
|
6732 | 9c8971d7 | Guido Trotter | self.context.glm.release(locking.LEVEL_NODE)
|
6733 | 9c8971d7 | Guido Trotter | del self.acquired_locks[locking.LEVEL_NODE] |
6734 | a8083063 | Iustin Pop | |
6735 | a8083063 | Iustin Pop | if self.op.wait_for_sync: |
6736 | b9bddb6b | Iustin Pop | disk_abort = not _WaitForSync(self, iobj) |
6737 | a1f445d3 | Iustin Pop | elif iobj.disk_template in constants.DTS_NET_MIRROR: |
6738 | a8083063 | Iustin Pop | # make sure the disks are not degraded (still sync-ing is ok)
|
6739 | a8083063 | Iustin Pop | time.sleep(15)
|
6740 | a8083063 | Iustin Pop | feedback_fn("* checking mirrors status")
|
6741 | b9bddb6b | Iustin Pop | disk_abort = not _WaitForSync(self, iobj, oneshot=True) |
6742 | a8083063 | Iustin Pop | else:
|
6743 | a8083063 | Iustin Pop | disk_abort = False
|
6744 | a8083063 | Iustin Pop | |
6745 | a8083063 | Iustin Pop | if disk_abort:
|
6746 | b9bddb6b | Iustin Pop | _RemoveDisks(self, iobj)
|
6747 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(iobj.name)
|
6748 | 7baf741d | Guido Trotter | # Make sure the instance lock gets removed
|
6749 | 7baf741d | Guido Trotter | self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
|
6750 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("There are some degraded disks for" |
6751 | 3ecf6786 | Iustin Pop | " this instance")
|
6752 | a8083063 | Iustin Pop | |
6753 | c3589cf8 | Iustin Pop | if iobj.disk_template != constants.DT_DISKLESS and not self.adopt_disks: |
6754 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_CREATE: |
6755 | 25a8792c | Iustin Pop | if not self.op.no_install: |
6756 | 25a8792c | Iustin Pop | feedback_fn("* running the instance OS create scripts...")
|
6757 | 25a8792c | Iustin Pop | # FIXME: pass debug option from opcode to backend
|
6758 | 25a8792c | Iustin Pop | result = self.rpc.call_instance_os_add(pnode_name, iobj, False, |
6759 | 25a8792c | Iustin Pop | self.op.debug_level)
|
6760 | 25a8792c | Iustin Pop | result.Raise("Could not add os for instance %s"
|
6761 | 25a8792c | Iustin Pop | " on node %s" % (instance, pnode_name))
|
6762 | a8083063 | Iustin Pop | |
6763 | a8083063 | Iustin Pop | elif self.op.mode == constants.INSTANCE_IMPORT: |
6764 | a8083063 | Iustin Pop | feedback_fn("* running the instance OS import scripts...")
|
6765 | a8083063 | Iustin Pop | src_node = self.op.src_node
|
6766 | 09acf207 | Guido Trotter | src_images = self.src_images
|
6767 | 62c9ec92 | Iustin Pop | cluster_name = self.cfg.GetClusterName()
|
6768 | 4a0e011f | Iustin Pop | # FIXME: pass debug option from opcode to backend
|
6769 | 6c0af70e | Guido Trotter | import_result = self.rpc.call_instance_os_import(pnode_name, iobj,
|
6770 | 09acf207 | Guido Trotter | src_node, src_images, |
6771 | dd713605 | Iustin Pop | cluster_name, |
6772 | dd713605 | Iustin Pop | self.op.debug_level)
|
6773 | 4c4e4e1e | Iustin Pop | msg = import_result.fail_msg |
6774 | 944bf548 | Iustin Pop | if msg:
|
6775 | 944bf548 | Iustin Pop | self.LogWarning("Error while importing the disk images for instance" |
6776 | 944bf548 | Iustin Pop | " %s on node %s: %s" % (instance, pnode_name, msg))
|
6777 | a8083063 | Iustin Pop | else:
|
6778 | a8083063 | Iustin Pop | # also checked in the prereq part
|
6779 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Unknown OS initialization mode '%s'" |
6780 | 3ecf6786 | Iustin Pop | % self.op.mode)
|
6781 | a8083063 | Iustin Pop | |
6782 | a8083063 | Iustin Pop | if self.op.start: |
6783 | 4978db17 | Iustin Pop | iobj.admin_up = True
|
6784 | a4eae71f | Michael Hanselmann | self.cfg.Update(iobj, feedback_fn)
|
6785 | 9a4f63d1 | Iustin Pop | logging.info("Starting instance %s on node %s", instance, pnode_name)
|
6786 | a8083063 | Iustin Pop | feedback_fn("* starting instance...")
|
6787 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(pnode_name, iobj, None, None) |
6788 | 4c4e4e1e | Iustin Pop | result.Raise("Could not start instance")
|
6789 | a8083063 | Iustin Pop | |
6790 | 08896026 | Iustin Pop | return list(iobj.all_nodes) |
6791 | 08896026 | Iustin Pop | |
6792 | a8083063 | Iustin Pop | |
6793 | a8083063 | Iustin Pop | class LUConnectConsole(NoHooksLU): |
6794 | a8083063 | Iustin Pop | """Connect to an instance's console.
|
6795 | a8083063 | Iustin Pop |
|
6796 | a8083063 | Iustin Pop | This is somewhat special in that it returns the command line that
|
6797 | a8083063 | Iustin Pop | you need to run on the master node in order to connect to the
|
6798 | a8083063 | Iustin Pop | console.
|
6799 | a8083063 | Iustin Pop |
|
6800 | a8083063 | Iustin Pop | """
|
6801 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
6802 | 8659b73e | Guido Trotter | REQ_BGL = False
|
6803 | 8659b73e | Guido Trotter | |
6804 | 8659b73e | Guido Trotter | def ExpandNames(self): |
6805 | 8659b73e | Guido Trotter | self._ExpandAndLockInstance()
|
6806 | a8083063 | Iustin Pop | |
6807 | a8083063 | Iustin Pop | def CheckPrereq(self): |
6808 | a8083063 | Iustin Pop | """Check prerequisites.
|
6809 | a8083063 | Iustin Pop |
|
6810 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
6811 | a8083063 | Iustin Pop |
|
6812 | a8083063 | Iustin Pop | """
|
6813 | 8659b73e | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
6814 | 8659b73e | Guido Trotter | assert self.instance is not None, \ |
6815 | 8659b73e | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
6816 | 513e896d | Guido Trotter | _CheckNodeOnline(self, self.instance.primary_node) |
6817 | a8083063 | Iustin Pop | |
6818 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
6819 | a8083063 | Iustin Pop | """Connect to the console of an instance
|
6820 | a8083063 | Iustin Pop |
|
6821 | a8083063 | Iustin Pop | """
|
6822 | a8083063 | Iustin Pop | instance = self.instance
|
6823 | a8083063 | Iustin Pop | node = instance.primary_node |
6824 | a8083063 | Iustin Pop | |
6825 | 72737a7f | Iustin Pop | node_insts = self.rpc.call_instance_list([node],
|
6826 | 72737a7f | Iustin Pop | [instance.hypervisor])[node] |
6827 | 4c4e4e1e | Iustin Pop | node_insts.Raise("Can't get node information from %s" % node)
|
6828 | a8083063 | Iustin Pop | |
6829 | aca13712 | Iustin Pop | if instance.name not in node_insts.payload: |
6830 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Instance %s is not running." % instance.name) |
6831 | a8083063 | Iustin Pop | |
6832 | 9a4f63d1 | Iustin Pop | logging.debug("Connecting to console of %s on %s", instance.name, node)
|
6833 | a8083063 | Iustin Pop | |
6834 | e69d05fd | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
6835 | 5431b2e4 | Guido Trotter | cluster = self.cfg.GetClusterInfo()
|
6836 | 5431b2e4 | Guido Trotter | # beparams and hvparams are passed separately, to avoid editing the
|
6837 | 5431b2e4 | Guido Trotter | # instance and then saving the defaults in the instance itself.
|
6838 | 5431b2e4 | Guido Trotter | hvparams = cluster.FillHV(instance) |
6839 | 5431b2e4 | Guido Trotter | beparams = cluster.FillBE(instance) |
6840 | 5431b2e4 | Guido Trotter | console_cmd = hyper.GetShellCommandForConsole(instance, hvparams, beparams) |
6841 | b047857b | Michael Hanselmann | |
6842 | 82122173 | Iustin Pop | # build ssh cmdline
|
6843 | 0a80a26f | Michael Hanselmann | return self.ssh.BuildCmd(node, "root", console_cmd, batch=True, tty=True) |
6844 | a8083063 | Iustin Pop | |
6845 | a8083063 | Iustin Pop | |
6846 | a8083063 | Iustin Pop | class LUReplaceDisks(LogicalUnit): |
6847 | a8083063 | Iustin Pop | """Replace the disks of an instance.
|
6848 | a8083063 | Iustin Pop |
|
6849 | a8083063 | Iustin Pop | """
|
6850 | a8083063 | Iustin Pop | HPATH = "mirrors-replace"
|
6851 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
6852 | a9e0c397 | Iustin Pop | _OP_REQP = ["instance_name", "mode", "disks"] |
6853 | efd990e4 | Guido Trotter | REQ_BGL = False
|
6854 | efd990e4 | Guido Trotter | |
6855 | 7e9366f7 | Iustin Pop | def CheckArguments(self): |
6856 | efd990e4 | Guido Trotter | if not hasattr(self.op, "remote_node"): |
6857 | efd990e4 | Guido Trotter | self.op.remote_node = None |
6858 | 7e9366f7 | Iustin Pop | if not hasattr(self.op, "iallocator"): |
6859 | 7e9366f7 | Iustin Pop | self.op.iallocator = None |
6860 | 7ea7bcf6 | Iustin Pop | if not hasattr(self.op, "early_release"): |
6861 | 7ea7bcf6 | Iustin Pop | self.op.early_release = False |
6862 | 7e9366f7 | Iustin Pop | |
6863 | c68174b6 | Michael Hanselmann | TLReplaceDisks.CheckArguments(self.op.mode, self.op.remote_node, |
6864 | c68174b6 | Michael Hanselmann | self.op.iallocator)
|
6865 | 7e9366f7 | Iustin Pop | |
6866 | 7e9366f7 | Iustin Pop | def ExpandNames(self): |
6867 | 7e9366f7 | Iustin Pop | self._ExpandAndLockInstance()
|
6868 | 7e9366f7 | Iustin Pop | |
6869 | 7e9366f7 | Iustin Pop | if self.op.iallocator is not None: |
6870 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
6871 | 2bb5c911 | Michael Hanselmann | |
6872 | efd990e4 | Guido Trotter | elif self.op.remote_node is not None: |
6873 | cf26a87a | Iustin Pop | remote_node = _ExpandNodeName(self.cfg, self.op.remote_node) |
6874 | efd990e4 | Guido Trotter | self.op.remote_node = remote_node
|
6875 | 2bb5c911 | Michael Hanselmann | |
6876 | 3b559640 | Iustin Pop | # Warning: do not remove the locking of the new secondary here
|
6877 | 3b559640 | Iustin Pop | # unless DRBD8.AddChildren is changed to work in parallel;
|
6878 | 3b559640 | Iustin Pop | # currently it doesn't since parallel invocations of
|
6879 | 3b559640 | Iustin Pop | # FindUnusedMinor will conflict
|
6880 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = [remote_node]
|
6881 | efd990e4 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
6882 | 2bb5c911 | Michael Hanselmann | |
6883 | efd990e4 | Guido Trotter | else:
|
6884 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
6885 | efd990e4 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
6886 | efd990e4 | Guido Trotter | |
6887 | c68174b6 | Michael Hanselmann | self.replacer = TLReplaceDisks(self, self.op.instance_name, self.op.mode, |
6888 | c68174b6 | Michael Hanselmann | self.op.iallocator, self.op.remote_node, |
6889 | 7ea7bcf6 | Iustin Pop | self.op.disks, False, self.op.early_release) |
6890 | c68174b6 | Michael Hanselmann | |
6891 | 3a012b41 | Michael Hanselmann | self.tasklets = [self.replacer] |
6892 | 2bb5c911 | Michael Hanselmann | |
6893 | efd990e4 | Guido Trotter | def DeclareLocks(self, level): |
6894 | efd990e4 | Guido Trotter | # If we're not already locking all nodes in the set we have to declare the
|
6895 | efd990e4 | Guido Trotter | # instance's primary/secondary nodes.
|
6896 | efd990e4 | Guido Trotter | if (level == locking.LEVEL_NODE and |
6897 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET): |
6898 | efd990e4 | Guido Trotter | self._LockInstancesNodes()
|
6899 | a8083063 | Iustin Pop | |
6900 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
6901 | a8083063 | Iustin Pop | """Build hooks env.
|
6902 | a8083063 | Iustin Pop |
|
6903 | a8083063 | Iustin Pop | This runs on the master, the primary and all the secondaries.
|
6904 | a8083063 | Iustin Pop |
|
6905 | a8083063 | Iustin Pop | """
|
6906 | 2bb5c911 | Michael Hanselmann | instance = self.replacer.instance
|
6907 | a8083063 | Iustin Pop | env = { |
6908 | a9e0c397 | Iustin Pop | "MODE": self.op.mode, |
6909 | a8083063 | Iustin Pop | "NEW_SECONDARY": self.op.remote_node, |
6910 | 2bb5c911 | Michael Hanselmann | "OLD_SECONDARY": instance.secondary_nodes[0], |
6911 | a8083063 | Iustin Pop | } |
6912 | 2bb5c911 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self, instance))
|
6913 | 0834c866 | Iustin Pop | nl = [ |
6914 | d6a02168 | Michael Hanselmann | self.cfg.GetMasterNode(),
|
6915 | 2bb5c911 | Michael Hanselmann | instance.primary_node, |
6916 | 0834c866 | Iustin Pop | ] |
6917 | 0834c866 | Iustin Pop | if self.op.remote_node is not None: |
6918 | 0834c866 | Iustin Pop | nl.append(self.op.remote_node)
|
6919 | a8083063 | Iustin Pop | return env, nl, nl
|
6920 | a8083063 | Iustin Pop | |
6921 | 2bb5c911 | Michael Hanselmann | |
6922 | 7ffc5a86 | Michael Hanselmann | class LUEvacuateNode(LogicalUnit): |
6923 | 7ffc5a86 | Michael Hanselmann | """Relocate the secondary instances from a node.
|
6924 | 7ffc5a86 | Michael Hanselmann |
|
6925 | 7ffc5a86 | Michael Hanselmann | """
|
6926 | 7ffc5a86 | Michael Hanselmann | HPATH = "node-evacuate"
|
6927 | 7ffc5a86 | Michael Hanselmann | HTYPE = constants.HTYPE_NODE |
6928 | 7ffc5a86 | Michael Hanselmann | _OP_REQP = ["node_name"]
|
6929 | 7ffc5a86 | Michael Hanselmann | REQ_BGL = False
|
6930 | 7ffc5a86 | Michael Hanselmann | |
6931 | 7ffc5a86 | Michael Hanselmann | def CheckArguments(self): |
6932 | 7ffc5a86 | Michael Hanselmann | if not hasattr(self.op, "remote_node"): |
6933 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node = None |
6934 | 7ffc5a86 | Michael Hanselmann | if not hasattr(self.op, "iallocator"): |
6935 | 7ffc5a86 | Michael Hanselmann | self.op.iallocator = None |
6936 | 7ea7bcf6 | Iustin Pop | if not hasattr(self.op, "early_release"): |
6937 | 7ea7bcf6 | Iustin Pop | self.op.early_release = False |
6938 | 7ffc5a86 | Michael Hanselmann | |
6939 | 7ffc5a86 | Michael Hanselmann | TLReplaceDisks.CheckArguments(constants.REPLACE_DISK_CHG, |
6940 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node,
|
6941 | 7ffc5a86 | Michael Hanselmann | self.op.iallocator)
|
6942 | 7ffc5a86 | Michael Hanselmann | |
6943 | 7ffc5a86 | Michael Hanselmann | def ExpandNames(self): |
6944 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
6945 | 7ffc5a86 | Michael Hanselmann | |
6946 | 7ffc5a86 | Michael Hanselmann | self.needed_locks = {}
|
6947 | 7ffc5a86 | Michael Hanselmann | |
6948 | 7ffc5a86 | Michael Hanselmann | # Declare node locks
|
6949 | 7ffc5a86 | Michael Hanselmann | if self.op.iallocator is not None: |
6950 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
6951 | 7ffc5a86 | Michael Hanselmann | |
6952 | 7ffc5a86 | Michael Hanselmann | elif self.op.remote_node is not None: |
6953 | cf26a87a | Iustin Pop | self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node) |
6954 | 7ffc5a86 | Michael Hanselmann | |
6955 | 7ffc5a86 | Michael Hanselmann | # Warning: do not remove the locking of the new secondary here
|
6956 | 7ffc5a86 | Michael Hanselmann | # unless DRBD8.AddChildren is changed to work in parallel;
|
6957 | 7ffc5a86 | Michael Hanselmann | # currently it doesn't since parallel invocations of
|
6958 | 7ffc5a86 | Michael Hanselmann | # FindUnusedMinor will conflict
|
6959 | cf26a87a | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = [self.op.remote_node] |
6960 | 7ffc5a86 | Michael Hanselmann | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
6961 | 7ffc5a86 | Michael Hanselmann | |
6962 | 7ffc5a86 | Michael Hanselmann | else:
|
6963 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid parameters", errors.ECODE_INVAL) |
6964 | 7ffc5a86 | Michael Hanselmann | |
6965 | 7ffc5a86 | Michael Hanselmann | # Create tasklets for replacing disks for all secondary instances on this
|
6966 | 7ffc5a86 | Michael Hanselmann | # node
|
6967 | 7ffc5a86 | Michael Hanselmann | names = [] |
6968 | 3a012b41 | Michael Hanselmann | tasklets = [] |
6969 | 7ffc5a86 | Michael Hanselmann | |
6970 | 7ffc5a86 | Michael Hanselmann | for inst in _GetNodeSecondaryInstances(self.cfg, self.op.node_name): |
6971 | 7ffc5a86 | Michael Hanselmann | logging.debug("Replacing disks for instance %s", inst.name)
|
6972 | 7ffc5a86 | Michael Hanselmann | names.append(inst.name) |
6973 | 7ffc5a86 | Michael Hanselmann | |
6974 | 7ffc5a86 | Michael Hanselmann | replacer = TLReplaceDisks(self, inst.name, constants.REPLACE_DISK_CHG,
|
6975 | 94a1b377 | Michael Hanselmann | self.op.iallocator, self.op.remote_node, [], |
6976 | 7ea7bcf6 | Iustin Pop | True, self.op.early_release) |
6977 | 3a012b41 | Michael Hanselmann | tasklets.append(replacer) |
6978 | 7ffc5a86 | Michael Hanselmann | |
6979 | 3a012b41 | Michael Hanselmann | self.tasklets = tasklets
|
6980 | 7ffc5a86 | Michael Hanselmann | self.instance_names = names
|
6981 | 7ffc5a86 | Michael Hanselmann | |
6982 | 7ffc5a86 | Michael Hanselmann | # Declare instance locks
|
6983 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_INSTANCE] = self.instance_names |
6984 | 7ffc5a86 | Michael Hanselmann | |
6985 | 7ffc5a86 | Michael Hanselmann | def DeclareLocks(self, level): |
6986 | 7ffc5a86 | Michael Hanselmann | # If we're not already locking all nodes in the set we have to declare the
|
6987 | 7ffc5a86 | Michael Hanselmann | # instance's primary/secondary nodes.
|
6988 | 7ffc5a86 | Michael Hanselmann | if (level == locking.LEVEL_NODE and |
6989 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET): |
6990 | 7ffc5a86 | Michael Hanselmann | self._LockInstancesNodes()
|
6991 | 7ffc5a86 | Michael Hanselmann | |
6992 | 7ffc5a86 | Michael Hanselmann | def BuildHooksEnv(self): |
6993 | 7ffc5a86 | Michael Hanselmann | """Build hooks env.
|
6994 | 7ffc5a86 | Michael Hanselmann |
|
6995 | 7ffc5a86 | Michael Hanselmann | This runs on the master, the primary and all the secondaries.
|
6996 | 7ffc5a86 | Michael Hanselmann |
|
6997 | 7ffc5a86 | Michael Hanselmann | """
|
6998 | 7ffc5a86 | Michael Hanselmann | env = { |
6999 | 7ffc5a86 | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
7000 | 7ffc5a86 | Michael Hanselmann | } |
7001 | 7ffc5a86 | Michael Hanselmann | |
7002 | 7ffc5a86 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
7003 | 7ffc5a86 | Michael Hanselmann | |
7004 | 7ffc5a86 | Michael Hanselmann | if self.op.remote_node is not None: |
7005 | 7ffc5a86 | Michael Hanselmann | env["NEW_SECONDARY"] = self.op.remote_node |
7006 | 7ffc5a86 | Michael Hanselmann | nl.append(self.op.remote_node)
|
7007 | 7ffc5a86 | Michael Hanselmann | |
7008 | 7ffc5a86 | Michael Hanselmann | return (env, nl, nl)
|
7009 | 7ffc5a86 | Michael Hanselmann | |
7010 | 7ffc5a86 | Michael Hanselmann | |
7011 | c68174b6 | Michael Hanselmann | class TLReplaceDisks(Tasklet): |
7012 | 2bb5c911 | Michael Hanselmann | """Replaces disks for an instance.
|
7013 | 2bb5c911 | Michael Hanselmann |
|
7014 | 2bb5c911 | Michael Hanselmann | Note: Locking is not within the scope of this class.
|
7015 | 2bb5c911 | Michael Hanselmann |
|
7016 | 2bb5c911 | Michael Hanselmann | """
|
7017 | 2bb5c911 | Michael Hanselmann | def __init__(self, lu, instance_name, mode, iallocator_name, remote_node, |
7018 | 7ea7bcf6 | Iustin Pop | disks, delay_iallocator, early_release): |
7019 | 2bb5c911 | Michael Hanselmann | """Initializes this class.
|
7020 | 2bb5c911 | Michael Hanselmann |
|
7021 | 2bb5c911 | Michael Hanselmann | """
|
7022 | 464243a7 | Michael Hanselmann | Tasklet.__init__(self, lu)
|
7023 | 464243a7 | Michael Hanselmann | |
7024 | 2bb5c911 | Michael Hanselmann | # Parameters
|
7025 | 2bb5c911 | Michael Hanselmann | self.instance_name = instance_name
|
7026 | 2bb5c911 | Michael Hanselmann | self.mode = mode
|
7027 | 2bb5c911 | Michael Hanselmann | self.iallocator_name = iallocator_name
|
7028 | 2bb5c911 | Michael Hanselmann | self.remote_node = remote_node
|
7029 | 2bb5c911 | Michael Hanselmann | self.disks = disks
|
7030 | 94a1b377 | Michael Hanselmann | self.delay_iallocator = delay_iallocator
|
7031 | 7ea7bcf6 | Iustin Pop | self.early_release = early_release
|
7032 | 2bb5c911 | Michael Hanselmann | |
7033 | 2bb5c911 | Michael Hanselmann | # Runtime data
|
7034 | 2bb5c911 | Michael Hanselmann | self.instance = None |
7035 | 2bb5c911 | Michael Hanselmann | self.new_node = None |
7036 | 2bb5c911 | Michael Hanselmann | self.target_node = None |
7037 | 2bb5c911 | Michael Hanselmann | self.other_node = None |
7038 | 2bb5c911 | Michael Hanselmann | self.remote_node_info = None |
7039 | 2bb5c911 | Michael Hanselmann | self.node_secondary_ip = None |
7040 | 2bb5c911 | Michael Hanselmann | |
7041 | 2bb5c911 | Michael Hanselmann | @staticmethod
|
7042 | 2bb5c911 | Michael Hanselmann | def CheckArguments(mode, remote_node, iallocator): |
7043 | c68174b6 | Michael Hanselmann | """Helper function for users of this class.
|
7044 | c68174b6 | Michael Hanselmann |
|
7045 | c68174b6 | Michael Hanselmann | """
|
7046 | 2bb5c911 | Michael Hanselmann | # check for valid parameter combination
|
7047 | 2bb5c911 | Michael Hanselmann | if mode == constants.REPLACE_DISK_CHG:
|
7048 | 02a00186 | Michael Hanselmann | if remote_node is None and iallocator is None: |
7049 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("When changing the secondary either an" |
7050 | 2bb5c911 | Michael Hanselmann | " iallocator script must be used or the"
|
7051 | 5c983ee5 | Iustin Pop | " new node given", errors.ECODE_INVAL)
|
7052 | 02a00186 | Michael Hanselmann | |
7053 | 02a00186 | Michael Hanselmann | if remote_node is not None and iallocator is not None: |
7054 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("Give either the iallocator or the new" |
7055 | 5c983ee5 | Iustin Pop | " secondary, not both", errors.ECODE_INVAL)
|
7056 | 02a00186 | Michael Hanselmann | |
7057 | 02a00186 | Michael Hanselmann | elif remote_node is not None or iallocator is not None: |
7058 | 02a00186 | Michael Hanselmann | # Not replacing the secondary
|
7059 | 02a00186 | Michael Hanselmann | raise errors.OpPrereqError("The iallocator and new node options can" |
7060 | 02a00186 | Michael Hanselmann | " only be used when changing the"
|
7061 | 5c983ee5 | Iustin Pop | " secondary node", errors.ECODE_INVAL)
|
7062 | 2bb5c911 | Michael Hanselmann | |
7063 | 2bb5c911 | Michael Hanselmann | @staticmethod
|
7064 | 2bb5c911 | Michael Hanselmann | def _RunAllocator(lu, iallocator_name, instance_name, relocate_from): |
7065 | 2bb5c911 | Michael Hanselmann | """Compute a new secondary node using an IAllocator.
|
7066 | 2bb5c911 | Michael Hanselmann |
|
7067 | 2bb5c911 | Michael Hanselmann | """
|
7068 | 2bb5c911 | Michael Hanselmann | ial = IAllocator(lu.cfg, lu.rpc, |
7069 | 2bb5c911 | Michael Hanselmann | mode=constants.IALLOCATOR_MODE_RELOC, |
7070 | 2bb5c911 | Michael Hanselmann | name=instance_name, |
7071 | 2bb5c911 | Michael Hanselmann | relocate_from=relocate_from) |
7072 | 2bb5c911 | Michael Hanselmann | |
7073 | 2bb5c911 | Michael Hanselmann | ial.Run(iallocator_name) |
7074 | 2bb5c911 | Michael Hanselmann | |
7075 | 2bb5c911 | Michael Hanselmann | if not ial.success: |
7076 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':" |
7077 | 5c983ee5 | Iustin Pop | " %s" % (iallocator_name, ial.info),
|
7078 | 5c983ee5 | Iustin Pop | errors.ECODE_NORES) |
7079 | 2bb5c911 | Michael Hanselmann | |
7080 | 680f0a89 | Iustin Pop | if len(ial.result) != ial.required_nodes: |
7081 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
7082 | 2bb5c911 | Michael Hanselmann | " of nodes (%s), required %s" %
|
7083 | d984846d | Iustin Pop | (iallocator_name, |
7084 | 680f0a89 | Iustin Pop | len(ial.result), ial.required_nodes),
|
7085 | 5c983ee5 | Iustin Pop | errors.ECODE_FAULT) |
7086 | 2bb5c911 | Michael Hanselmann | |
7087 | 680f0a89 | Iustin Pop | remote_node_name = ial.result[0]
|
7088 | 2bb5c911 | Michael Hanselmann | |
7089 | 2bb5c911 | Michael Hanselmann | lu.LogInfo("Selected new secondary for instance '%s': %s",
|
7090 | 2bb5c911 | Michael Hanselmann | instance_name, remote_node_name) |
7091 | 2bb5c911 | Michael Hanselmann | |
7092 | 2bb5c911 | Michael Hanselmann | return remote_node_name
|
7093 | 2bb5c911 | Michael Hanselmann | |
7094 | 942be002 | Michael Hanselmann | def _FindFaultyDisks(self, node_name): |
7095 | 2d9005d8 | Michael Hanselmann | return _FindFaultyInstanceDisks(self.cfg, self.rpc, self.instance, |
7096 | 2d9005d8 | Michael Hanselmann | node_name, True)
|
7097 | 942be002 | Michael Hanselmann | |
7098 | 2bb5c911 | Michael Hanselmann | def CheckPrereq(self): |
7099 | 2bb5c911 | Michael Hanselmann | """Check prerequisites.
|
7100 | 2bb5c911 | Michael Hanselmann |
|
7101 | 2bb5c911 | Michael Hanselmann | This checks that the instance is in the cluster.
|
7102 | 2bb5c911 | Michael Hanselmann |
|
7103 | 2bb5c911 | Michael Hanselmann | """
|
7104 | e9022531 | Iustin Pop | self.instance = instance = self.cfg.GetInstanceInfo(self.instance_name) |
7105 | e9022531 | Iustin Pop | assert instance is not None, \ |
7106 | 20eca47d | Iustin Pop | "Cannot retrieve locked instance %s" % self.instance_name |
7107 | 2bb5c911 | Michael Hanselmann | |
7108 | e9022531 | Iustin Pop | if instance.disk_template != constants.DT_DRBD8:
|
7109 | 7e9366f7 | Iustin Pop | raise errors.OpPrereqError("Can only run replace disks for DRBD8-based" |
7110 | 5c983ee5 | Iustin Pop | " instances", errors.ECODE_INVAL)
|
7111 | a8083063 | Iustin Pop | |
7112 | e9022531 | Iustin Pop | if len(instance.secondary_nodes) != 1: |
7113 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The instance has a strange layout," |
7114 | 3ecf6786 | Iustin Pop | " expected one secondary but found %d" %
|
7115 | 5c983ee5 | Iustin Pop | len(instance.secondary_nodes),
|
7116 | 5c983ee5 | Iustin Pop | errors.ECODE_FAULT) |
7117 | a8083063 | Iustin Pop | |
7118 | 94a1b377 | Michael Hanselmann | if not self.delay_iallocator: |
7119 | 94a1b377 | Michael Hanselmann | self._CheckPrereq2()
|
7120 | 94a1b377 | Michael Hanselmann | |
7121 | 94a1b377 | Michael Hanselmann | def _CheckPrereq2(self): |
7122 | 94a1b377 | Michael Hanselmann | """Check prerequisites, second part.
|
7123 | 94a1b377 | Michael Hanselmann |
|
7124 | 94a1b377 | Michael Hanselmann | This function should always be part of CheckPrereq. It was separated and is
|
7125 | 94a1b377 | Michael Hanselmann | now called from Exec because during node evacuation iallocator was only
|
7126 | 94a1b377 | Michael Hanselmann | called with an unmodified cluster model, not taking planned changes into
|
7127 | 94a1b377 | Michael Hanselmann | account.
|
7128 | 94a1b377 | Michael Hanselmann |
|
7129 | 94a1b377 | Michael Hanselmann | """
|
7130 | 94a1b377 | Michael Hanselmann | instance = self.instance
|
7131 | e9022531 | Iustin Pop | secondary_node = instance.secondary_nodes[0]
|
7132 | a9e0c397 | Iustin Pop | |
7133 | 2bb5c911 | Michael Hanselmann | if self.iallocator_name is None: |
7134 | 2bb5c911 | Michael Hanselmann | remote_node = self.remote_node
|
7135 | 2bb5c911 | Michael Hanselmann | else:
|
7136 | 2bb5c911 | Michael Hanselmann | remote_node = self._RunAllocator(self.lu, self.iallocator_name, |
7137 | e9022531 | Iustin Pop | instance.name, instance.secondary_nodes) |
7138 | b6e82a65 | Iustin Pop | |
7139 | a9e0c397 | Iustin Pop | if remote_node is not None: |
7140 | a9e0c397 | Iustin Pop | self.remote_node_info = self.cfg.GetNodeInfo(remote_node) |
7141 | efd990e4 | Guido Trotter | assert self.remote_node_info is not None, \ |
7142 | efd990e4 | Guido Trotter | "Cannot retrieve locked node %s" % remote_node
|
7143 | a9e0c397 | Iustin Pop | else:
|
7144 | a9e0c397 | Iustin Pop | self.remote_node_info = None |
7145 | 2bb5c911 | Michael Hanselmann | |
7146 | 2bb5c911 | Michael Hanselmann | if remote_node == self.instance.primary_node: |
7147 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The specified node is the primary node of" |
7148 | 5c983ee5 | Iustin Pop | " the instance.", errors.ECODE_INVAL)
|
7149 | 2bb5c911 | Michael Hanselmann | |
7150 | 2bb5c911 | Michael Hanselmann | if remote_node == secondary_node:
|
7151 | 7e9366f7 | Iustin Pop | raise errors.OpPrereqError("The specified node is already the" |
7152 | 5c983ee5 | Iustin Pop | " secondary node of the instance.",
|
7153 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
7154 | 7e9366f7 | Iustin Pop | |
7155 | 2945fd2d | Michael Hanselmann | if self.disks and self.mode in (constants.REPLACE_DISK_AUTO, |
7156 | 2945fd2d | Michael Hanselmann | constants.REPLACE_DISK_CHG): |
7157 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Cannot specify disks to be replaced", |
7158 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
7159 | 942be002 | Michael Hanselmann | |
7160 | 2945fd2d | Michael Hanselmann | if self.mode == constants.REPLACE_DISK_AUTO: |
7161 | e9022531 | Iustin Pop | faulty_primary = self._FindFaultyDisks(instance.primary_node)
|
7162 | 942be002 | Michael Hanselmann | faulty_secondary = self._FindFaultyDisks(secondary_node)
|
7163 | 942be002 | Michael Hanselmann | |
7164 | 942be002 | Michael Hanselmann | if faulty_primary and faulty_secondary: |
7165 | 942be002 | Michael Hanselmann | raise errors.OpPrereqError("Instance %s has faulty disks on more than" |
7166 | 942be002 | Michael Hanselmann | " one node and can not be repaired"
|
7167 | 5c983ee5 | Iustin Pop | " automatically" % self.instance_name, |
7168 | 5c983ee5 | Iustin Pop | errors.ECODE_STATE) |
7169 | 942be002 | Michael Hanselmann | |
7170 | 942be002 | Michael Hanselmann | if faulty_primary:
|
7171 | 942be002 | Michael Hanselmann | self.disks = faulty_primary
|
7172 | e9022531 | Iustin Pop | self.target_node = instance.primary_node
|
7173 | 942be002 | Michael Hanselmann | self.other_node = secondary_node
|
7174 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
7175 | 942be002 | Michael Hanselmann | elif faulty_secondary:
|
7176 | 942be002 | Michael Hanselmann | self.disks = faulty_secondary
|
7177 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
7178 | e9022531 | Iustin Pop | self.other_node = instance.primary_node
|
7179 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
7180 | 942be002 | Michael Hanselmann | else:
|
7181 | 942be002 | Michael Hanselmann | self.disks = []
|
7182 | 942be002 | Michael Hanselmann | check_nodes = [] |
7183 | 942be002 | Michael Hanselmann | |
7184 | 942be002 | Michael Hanselmann | else:
|
7185 | 942be002 | Michael Hanselmann | # Non-automatic modes
|
7186 | 942be002 | Michael Hanselmann | if self.mode == constants.REPLACE_DISK_PRI: |
7187 | e9022531 | Iustin Pop | self.target_node = instance.primary_node
|
7188 | 942be002 | Michael Hanselmann | self.other_node = secondary_node
|
7189 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
7190 | 7e9366f7 | Iustin Pop | |
7191 | 942be002 | Michael Hanselmann | elif self.mode == constants.REPLACE_DISK_SEC: |
7192 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
7193 | e9022531 | Iustin Pop | self.other_node = instance.primary_node
|
7194 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
7195 | a9e0c397 | Iustin Pop | |
7196 | 942be002 | Michael Hanselmann | elif self.mode == constants.REPLACE_DISK_CHG: |
7197 | 942be002 | Michael Hanselmann | self.new_node = remote_node
|
7198 | e9022531 | Iustin Pop | self.other_node = instance.primary_node
|
7199 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
7200 | 942be002 | Michael Hanselmann | check_nodes = [self.new_node, self.other_node] |
7201 | 54155f52 | Iustin Pop | |
7202 | 942be002 | Michael Hanselmann | _CheckNodeNotDrained(self.lu, remote_node)
|
7203 | a8083063 | Iustin Pop | |
7204 | 9af0fa6a | Iustin Pop | old_node_info = self.cfg.GetNodeInfo(secondary_node)
|
7205 | 9af0fa6a | Iustin Pop | assert old_node_info is not None |
7206 | 9af0fa6a | Iustin Pop | if old_node_info.offline and not self.early_release: |
7207 | 9af0fa6a | Iustin Pop | # doesn't make sense to delay the release
|
7208 | 9af0fa6a | Iustin Pop | self.early_release = True |
7209 | 9af0fa6a | Iustin Pop | self.lu.LogInfo("Old secondary %s is offline, automatically enabling" |
7210 | 9af0fa6a | Iustin Pop | " early-release mode", secondary_node)
|
7211 | 9af0fa6a | Iustin Pop | |
7212 | 942be002 | Michael Hanselmann | else:
|
7213 | 942be002 | Michael Hanselmann | raise errors.ProgrammerError("Unhandled disk replace mode (%s)" % |
7214 | 942be002 | Michael Hanselmann | self.mode)
|
7215 | 942be002 | Michael Hanselmann | |
7216 | 942be002 | Michael Hanselmann | # If not specified all disks should be replaced
|
7217 | 942be002 | Michael Hanselmann | if not self.disks: |
7218 | 942be002 | Michael Hanselmann | self.disks = range(len(self.instance.disks)) |
7219 | a9e0c397 | Iustin Pop | |
7220 | 2bb5c911 | Michael Hanselmann | for node in check_nodes: |
7221 | 2bb5c911 | Michael Hanselmann | _CheckNodeOnline(self.lu, node)
|
7222 | e4376078 | Iustin Pop | |
7223 | 2bb5c911 | Michael Hanselmann | # Check whether disks are valid
|
7224 | 2bb5c911 | Michael Hanselmann | for disk_idx in self.disks: |
7225 | e9022531 | Iustin Pop | instance.FindDisk(disk_idx) |
7226 | e4376078 | Iustin Pop | |
7227 | 2bb5c911 | Michael Hanselmann | # Get secondary node IP addresses
|
7228 | 2bb5c911 | Michael Hanselmann | node_2nd_ip = {} |
7229 | e4376078 | Iustin Pop | |
7230 | 2bb5c911 | Michael Hanselmann | for node_name in [self.target_node, self.other_node, self.new_node]: |
7231 | 2bb5c911 | Michael Hanselmann | if node_name is not None: |
7232 | 2bb5c911 | Michael Hanselmann | node_2nd_ip[node_name] = self.cfg.GetNodeInfo(node_name).secondary_ip
|
7233 | e4376078 | Iustin Pop | |
7234 | 2bb5c911 | Michael Hanselmann | self.node_secondary_ip = node_2nd_ip
|
7235 | a9e0c397 | Iustin Pop | |
7236 | c68174b6 | Michael Hanselmann | def Exec(self, feedback_fn): |
7237 | 2bb5c911 | Michael Hanselmann | """Execute disk replacement.
|
7238 | 2bb5c911 | Michael Hanselmann |
|
7239 | 2bb5c911 | Michael Hanselmann | This dispatches the disk replacement to the appropriate handler.
|
7240 | cff90b79 | Iustin Pop |
|
7241 | a9e0c397 | Iustin Pop | """
|
7242 | 94a1b377 | Michael Hanselmann | if self.delay_iallocator: |
7243 | 94a1b377 | Michael Hanselmann | self._CheckPrereq2()
|
7244 | 94a1b377 | Michael Hanselmann | |
7245 | 942be002 | Michael Hanselmann | if not self.disks: |
7246 | 942be002 | Michael Hanselmann | feedback_fn("No disks need replacement")
|
7247 | 942be002 | Michael Hanselmann | return
|
7248 | 942be002 | Michael Hanselmann | |
7249 | 942be002 | Michael Hanselmann | feedback_fn("Replacing disk(s) %s for %s" %
|
7250 | 1f864b60 | Iustin Pop | (utils.CommaJoin(self.disks), self.instance.name)) |
7251 | 7ffc5a86 | Michael Hanselmann | |
7252 | 2bb5c911 | Michael Hanselmann | activate_disks = (not self.instance.admin_up) |
7253 | 2bb5c911 | Michael Hanselmann | |
7254 | 2bb5c911 | Michael Hanselmann | # Activate the instance disks if we're replacing them on a down instance
|
7255 | 2bb5c911 | Michael Hanselmann | if activate_disks:
|
7256 | 2bb5c911 | Michael Hanselmann | _StartInstanceDisks(self.lu, self.instance, True) |
7257 | 2bb5c911 | Michael Hanselmann | |
7258 | 2bb5c911 | Michael Hanselmann | try:
|
7259 | 942be002 | Michael Hanselmann | # Should we replace the secondary node?
|
7260 | 942be002 | Michael Hanselmann | if self.new_node is not None: |
7261 | a4eae71f | Michael Hanselmann | fn = self._ExecDrbd8Secondary
|
7262 | 2bb5c911 | Michael Hanselmann | else:
|
7263 | a4eae71f | Michael Hanselmann | fn = self._ExecDrbd8DiskOnly
|
7264 | a4eae71f | Michael Hanselmann | |
7265 | a4eae71f | Michael Hanselmann | return fn(feedback_fn)
|
7266 | 2bb5c911 | Michael Hanselmann | |
7267 | 2bb5c911 | Michael Hanselmann | finally:
|
7268 | 5c983ee5 | Iustin Pop | # Deactivate the instance disks if we're replacing them on a
|
7269 | 5c983ee5 | Iustin Pop | # down instance
|
7270 | 2bb5c911 | Michael Hanselmann | if activate_disks:
|
7271 | 2bb5c911 | Michael Hanselmann | _SafeShutdownInstanceDisks(self.lu, self.instance) |
7272 | 2bb5c911 | Michael Hanselmann | |
7273 | 2bb5c911 | Michael Hanselmann | def _CheckVolumeGroup(self, nodes): |
7274 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking volume groups") |
7275 | 2bb5c911 | Michael Hanselmann | |
7276 | a9e0c397 | Iustin Pop | vgname = self.cfg.GetVGName()
|
7277 | cff90b79 | Iustin Pop | |
7278 | 2bb5c911 | Michael Hanselmann | # Make sure volume group exists on all involved nodes
|
7279 | 2bb5c911 | Michael Hanselmann | results = self.rpc.call_vg_list(nodes)
|
7280 | cff90b79 | Iustin Pop | if not results: |
7281 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't list volume groups on the nodes") |
7282 | 2bb5c911 | Michael Hanselmann | |
7283 | 2bb5c911 | Michael Hanselmann | for node in nodes: |
7284 | 781de953 | Iustin Pop | res = results[node] |
7285 | 4c4e4e1e | Iustin Pop | res.Raise("Error checking node %s" % node)
|
7286 | 2bb5c911 | Michael Hanselmann | if vgname not in res.payload: |
7287 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Volume group '%s' not found on node %s" % |
7288 | 2bb5c911 | Michael Hanselmann | (vgname, node)) |
7289 | 2bb5c911 | Michael Hanselmann | |
7290 | 2bb5c911 | Michael Hanselmann | def _CheckDisksExistence(self, nodes): |
7291 | 2bb5c911 | Michael Hanselmann | # Check disk existence
|
7292 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
7293 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
7294 | cff90b79 | Iustin Pop | continue
|
7295 | 2bb5c911 | Michael Hanselmann | |
7296 | 2bb5c911 | Michael Hanselmann | for node in nodes: |
7297 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking disk/%d on %s" % (idx, node)) |
7298 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node)
|
7299 | 2bb5c911 | Michael Hanselmann | |
7300 | 23829f6f | Iustin Pop | result = self.rpc.call_blockdev_find(node, dev)
|
7301 | 2bb5c911 | Michael Hanselmann | |
7302 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7303 | 2bb5c911 | Michael Hanselmann | if msg or not result.payload: |
7304 | 2bb5c911 | Michael Hanselmann | if not msg: |
7305 | 2bb5c911 | Michael Hanselmann | msg = "disk not found"
|
7306 | 23829f6f | Iustin Pop | raise errors.OpExecError("Can't find disk/%d on node %s: %s" % |
7307 | 23829f6f | Iustin Pop | (idx, node, msg)) |
7308 | cff90b79 | Iustin Pop | |
7309 | 2bb5c911 | Michael Hanselmann | def _CheckDisksConsistency(self, node_name, on_primary, ldisk): |
7310 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
7311 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
7312 | cff90b79 | Iustin Pop | continue
|
7313 | cff90b79 | Iustin Pop | |
7314 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking disk/%d consistency on node %s" % |
7315 | 2bb5c911 | Michael Hanselmann | (idx, node_name)) |
7316 | 2bb5c911 | Michael Hanselmann | |
7317 | 2bb5c911 | Michael Hanselmann | if not _CheckDiskConsistency(self.lu, dev, node_name, on_primary, |
7318 | 2bb5c911 | Michael Hanselmann | ldisk=ldisk): |
7319 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Node %s has degraded storage, unsafe to" |
7320 | 2bb5c911 | Michael Hanselmann | " replace disks for instance %s" %
|
7321 | 2bb5c911 | Michael Hanselmann | (node_name, self.instance.name))
|
7322 | 2bb5c911 | Michael Hanselmann | |
7323 | 2bb5c911 | Michael Hanselmann | def _CreateNewStorage(self, node_name): |
7324 | 2bb5c911 | Michael Hanselmann | vgname = self.cfg.GetVGName()
|
7325 | 2bb5c911 | Michael Hanselmann | iv_names = {} |
7326 | 2bb5c911 | Michael Hanselmann | |
7327 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
7328 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
7329 | a9e0c397 | Iustin Pop | continue
|
7330 | 2bb5c911 | Michael Hanselmann | |
7331 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding storage on %s for disk/%d" % (node_name, idx)) |
7332 | 2bb5c911 | Michael Hanselmann | |
7333 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node_name)
|
7334 | 2bb5c911 | Michael Hanselmann | |
7335 | 2bb5c911 | Michael Hanselmann | lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]] |
7336 | 2bb5c911 | Michael Hanselmann | names = _GenerateUniqueNames(self.lu, lv_names)
|
7337 | 2bb5c911 | Michael Hanselmann | |
7338 | 2bb5c911 | Michael Hanselmann | lv_data = objects.Disk(dev_type=constants.LD_LV, size=dev.size, |
7339 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[0]))
|
7340 | a9e0c397 | Iustin Pop | lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
7341 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[1]))
|
7342 | 2bb5c911 | Michael Hanselmann | |
7343 | a9e0c397 | Iustin Pop | new_lvs = [lv_data, lv_meta] |
7344 | a9e0c397 | Iustin Pop | old_lvs = dev.children |
7345 | a9e0c397 | Iustin Pop | iv_names[dev.iv_name] = (dev, old_lvs, new_lvs) |
7346 | 2bb5c911 | Michael Hanselmann | |
7347 | 428958aa | Iustin Pop | # we pass force_create=True to force the LVM creation
|
7348 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
7349 | 2bb5c911 | Michael Hanselmann | _CreateBlockDev(self.lu, node_name, self.instance, new_lv, True, |
7350 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
7351 | 2bb5c911 | Michael Hanselmann | |
7352 | 2bb5c911 | Michael Hanselmann | return iv_names
|
7353 | 2bb5c911 | Michael Hanselmann | |
7354 | 2bb5c911 | Michael Hanselmann | def _CheckDevices(self, node_name, iv_names): |
7355 | 1122eb25 | Iustin Pop | for name, (dev, _, _) in iv_names.iteritems(): |
7356 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node_name)
|
7357 | 2bb5c911 | Michael Hanselmann | |
7358 | 2bb5c911 | Michael Hanselmann | result = self.rpc.call_blockdev_find(node_name, dev)
|
7359 | 2bb5c911 | Michael Hanselmann | |
7360 | 2bb5c911 | Michael Hanselmann | msg = result.fail_msg |
7361 | 2bb5c911 | Michael Hanselmann | if msg or not result.payload: |
7362 | 2bb5c911 | Michael Hanselmann | if not msg: |
7363 | 2bb5c911 | Michael Hanselmann | msg = "disk not found"
|
7364 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Can't find DRBD device %s: %s" % |
7365 | 2bb5c911 | Michael Hanselmann | (name, msg)) |
7366 | 2bb5c911 | Michael Hanselmann | |
7367 | 96acbc09 | Michael Hanselmann | if result.payload.is_degraded:
|
7368 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("DRBD device %s is degraded!" % name) |
7369 | 2bb5c911 | Michael Hanselmann | |
7370 | 2bb5c911 | Michael Hanselmann | def _RemoveOldStorage(self, node_name, iv_names): |
7371 | 1122eb25 | Iustin Pop | for name, (_, old_lvs, _) in iv_names.iteritems(): |
7372 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Remove logical volumes for %s" % name) |
7373 | 2bb5c911 | Michael Hanselmann | |
7374 | 2bb5c911 | Michael Hanselmann | for lv in old_lvs: |
7375 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(lv, node_name)
|
7376 | 2bb5c911 | Michael Hanselmann | |
7377 | 2bb5c911 | Michael Hanselmann | msg = self.rpc.call_blockdev_remove(node_name, lv).fail_msg
|
7378 | 2bb5c911 | Michael Hanselmann | if msg:
|
7379 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Can't remove old LV: %s" % msg, |
7380 | 2bb5c911 | Michael Hanselmann | hint="remove unused LVs manually")
|
7381 | 2bb5c911 | Michael Hanselmann | |
7382 | 7ea7bcf6 | Iustin Pop | def _ReleaseNodeLock(self, node_name): |
7383 | 7ea7bcf6 | Iustin Pop | """Releases the lock for a given node."""
|
7384 | 7ea7bcf6 | Iustin Pop | self.lu.context.glm.release(locking.LEVEL_NODE, node_name)
|
7385 | 7ea7bcf6 | Iustin Pop | |
7386 | a4eae71f | Michael Hanselmann | def _ExecDrbd8DiskOnly(self, feedback_fn): |
7387 | 2bb5c911 | Michael Hanselmann | """Replace a disk on the primary or secondary for DRBD 8.
|
7388 | 2bb5c911 | Michael Hanselmann |
|
7389 | 2bb5c911 | Michael Hanselmann | The algorithm for replace is quite complicated:
|
7390 | 2bb5c911 | Michael Hanselmann |
|
7391 | 2bb5c911 | Michael Hanselmann | 1. for each disk to be replaced:
|
7392 | 2bb5c911 | Michael Hanselmann |
|
7393 | 2bb5c911 | Michael Hanselmann | 1. create new LVs on the target node with unique names
|
7394 | 2bb5c911 | Michael Hanselmann | 1. detach old LVs from the drbd device
|
7395 | 2bb5c911 | Michael Hanselmann | 1. rename old LVs to name_replaced.<time_t>
|
7396 | 2bb5c911 | Michael Hanselmann | 1. rename new LVs to old LVs
|
7397 | 2bb5c911 | Michael Hanselmann | 1. attach the new LVs (with the old names now) to the drbd device
|
7398 | 2bb5c911 | Michael Hanselmann |
|
7399 | 2bb5c911 | Michael Hanselmann | 1. wait for sync across all devices
|
7400 | 2bb5c911 | Michael Hanselmann |
|
7401 | 2bb5c911 | Michael Hanselmann | 1. for each modified disk:
|
7402 | 2bb5c911 | Michael Hanselmann |
|
7403 | 2bb5c911 | Michael Hanselmann | 1. remove old LVs (which have the name name_replaces.<time_t>)
|
7404 | 2bb5c911 | Michael Hanselmann |
|
7405 | 2bb5c911 | Michael Hanselmann | Failures are not very well handled.
|
7406 | 2bb5c911 | Michael Hanselmann |
|
7407 | 2bb5c911 | Michael Hanselmann | """
|
7408 | 2bb5c911 | Michael Hanselmann | steps_total = 6
|
7409 | 2bb5c911 | Michael Hanselmann | |
7410 | 2bb5c911 | Michael Hanselmann | # Step: check device activation
|
7411 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(1, steps_total, "Check device existence") |
7412 | 2bb5c911 | Michael Hanselmann | self._CheckDisksExistence([self.other_node, self.target_node]) |
7413 | 2bb5c911 | Michael Hanselmann | self._CheckVolumeGroup([self.target_node, self.other_node]) |
7414 | 2bb5c911 | Michael Hanselmann | |
7415 | 2bb5c911 | Michael Hanselmann | # Step: check other node consistency
|
7416 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(2, steps_total, "Check peer consistency") |
7417 | 2bb5c911 | Michael Hanselmann | self._CheckDisksConsistency(self.other_node, |
7418 | 2bb5c911 | Michael Hanselmann | self.other_node == self.instance.primary_node, |
7419 | 2bb5c911 | Michael Hanselmann | False)
|
7420 | 2bb5c911 | Michael Hanselmann | |
7421 | 2bb5c911 | Michael Hanselmann | # Step: create new storage
|
7422 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(3, steps_total, "Allocate new storage") |
7423 | 2bb5c911 | Michael Hanselmann | iv_names = self._CreateNewStorage(self.target_node) |
7424 | a9e0c397 | Iustin Pop | |
7425 | cff90b79 | Iustin Pop | # Step: for each lv, detach+rename*2+attach
|
7426 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
7427 | cff90b79 | Iustin Pop | for dev, old_lvs, new_lvs in iv_names.itervalues(): |
7428 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Detaching %s drbd from local storage" % dev.iv_name) |
7429 | 2bb5c911 | Michael Hanselmann | |
7430 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_removechildren(self.target_node, dev, |
7431 | 4d4a651d | Michael Hanselmann | old_lvs) |
7432 | 4c4e4e1e | Iustin Pop | result.Raise("Can't detach drbd from local storage on node"
|
7433 | 2bb5c911 | Michael Hanselmann | " %s for device %s" % (self.target_node, dev.iv_name)) |
7434 | cff90b79 | Iustin Pop | #dev.children = []
|
7435 | cff90b79 | Iustin Pop | #cfg.Update(instance)
|
7436 | a9e0c397 | Iustin Pop | |
7437 | a9e0c397 | Iustin Pop | # ok, we created the new LVs, so now we know we have the needed
|
7438 | a9e0c397 | Iustin Pop | # storage; as such, we proceed on the target node to rename
|
7439 | a9e0c397 | Iustin Pop | # old_lv to _old, and new_lv to old_lv; note that we rename LVs
|
7440 | c99a3cc0 | Manuel Franceschini | # using the assumption that logical_id == physical_id (which in
|
7441 | a9e0c397 | Iustin Pop | # turn is the unique_id on that node)
|
7442 | cff90b79 | Iustin Pop | |
7443 | cff90b79 | Iustin Pop | # FIXME(iustin): use a better name for the replaced LVs
|
7444 | a9e0c397 | Iustin Pop | temp_suffix = int(time.time())
|
7445 | a9e0c397 | Iustin Pop | ren_fn = lambda d, suff: (d.physical_id[0], |
7446 | a9e0c397 | Iustin Pop | d.physical_id[1] + "_replaced-%s" % suff) |
7447 | 2bb5c911 | Michael Hanselmann | |
7448 | 2bb5c911 | Michael Hanselmann | # Build the rename list based on what LVs exist on the node
|
7449 | 2bb5c911 | Michael Hanselmann | rename_old_to_new = [] |
7450 | cff90b79 | Iustin Pop | for to_ren in old_lvs: |
7451 | 2bb5c911 | Michael Hanselmann | result = self.rpc.call_blockdev_find(self.target_node, to_ren) |
7452 | 4c4e4e1e | Iustin Pop | if not result.fail_msg and result.payload: |
7453 | 23829f6f | Iustin Pop | # device exists
|
7454 | 2bb5c911 | Michael Hanselmann | rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix))) |
7455 | cff90b79 | Iustin Pop | |
7456 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Renaming the old LVs on the target node") |
7457 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_rename(self.target_node, |
7458 | 4d4a651d | Michael Hanselmann | rename_old_to_new) |
7459 | 2bb5c911 | Michael Hanselmann | result.Raise("Can't rename old LVs on node %s" % self.target_node) |
7460 | 2bb5c911 | Michael Hanselmann | |
7461 | 2bb5c911 | Michael Hanselmann | # Now we rename the new LVs to the old LVs
|
7462 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Renaming the new LVs on the target node") |
7463 | 2bb5c911 | Michael Hanselmann | rename_new_to_old = [(new, old.physical_id) |
7464 | 2bb5c911 | Michael Hanselmann | for old, new in zip(old_lvs, new_lvs)] |
7465 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_rename(self.target_node, |
7466 | 4d4a651d | Michael Hanselmann | rename_new_to_old) |
7467 | 2bb5c911 | Michael Hanselmann | result.Raise("Can't rename new LVs on node %s" % self.target_node) |
7468 | cff90b79 | Iustin Pop | |
7469 | cff90b79 | Iustin Pop | for old, new in zip(old_lvs, new_lvs): |
7470 | cff90b79 | Iustin Pop | new.logical_id = old.logical_id |
7471 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(new, self.target_node) |
7472 | a9e0c397 | Iustin Pop | |
7473 | cff90b79 | Iustin Pop | for disk in old_lvs: |
7474 | cff90b79 | Iustin Pop | disk.logical_id = ren_fn(disk, temp_suffix) |
7475 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(disk, self.target_node) |
7476 | a9e0c397 | Iustin Pop | |
7477 | 2bb5c911 | Michael Hanselmann | # Now that the new lvs have the old name, we can add them to the device
|
7478 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding new mirror component on %s" % self.target_node) |
7479 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_addchildren(self.target_node, dev, |
7480 | 4d4a651d | Michael Hanselmann | new_lvs) |
7481 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7482 | 2cc1da8b | Iustin Pop | if msg:
|
7483 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
7484 | 4d4a651d | Michael Hanselmann | msg2 = self.rpc.call_blockdev_remove(self.target_node, |
7485 | 4d4a651d | Michael Hanselmann | new_lv).fail_msg |
7486 | 4c4e4e1e | Iustin Pop | if msg2:
|
7487 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2, |
7488 | 2bb5c911 | Michael Hanselmann | hint=("cleanup manually the unused logical"
|
7489 | 2bb5c911 | Michael Hanselmann | "volumes"))
|
7490 | 2cc1da8b | Iustin Pop | raise errors.OpExecError("Can't add local storage to drbd: %s" % msg) |
7491 | a9e0c397 | Iustin Pop | |
7492 | a9e0c397 | Iustin Pop | dev.children = new_lvs |
7493 | a9e0c397 | Iustin Pop | |
7494 | a4eae71f | Michael Hanselmann | self.cfg.Update(self.instance, feedback_fn) |
7495 | a9e0c397 | Iustin Pop | |
7496 | 7ea7bcf6 | Iustin Pop | cstep = 5
|
7497 | 7ea7bcf6 | Iustin Pop | if self.early_release: |
7498 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Removing old storage") |
7499 | 7ea7bcf6 | Iustin Pop | cstep += 1
|
7500 | 7ea7bcf6 | Iustin Pop | self._RemoveOldStorage(self.target_node, iv_names) |
7501 | d5cd389c | Iustin Pop | # WARNING: we release both node locks here, do not do other RPCs
|
7502 | d5cd389c | Iustin Pop | # than WaitForSync to the primary node
|
7503 | d5cd389c | Iustin Pop | self._ReleaseNodeLock([self.target_node, self.other_node]) |
7504 | 7ea7bcf6 | Iustin Pop | |
7505 | 2bb5c911 | Michael Hanselmann | # Wait for sync
|
7506 | 2bb5c911 | Michael Hanselmann | # This can fail as the old devices are degraded and _WaitForSync
|
7507 | 2bb5c911 | Michael Hanselmann | # does a combined result over all disks, so we don't check its return value
|
7508 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Sync devices") |
7509 | 7ea7bcf6 | Iustin Pop | cstep += 1
|
7510 | b6c07b79 | Michael Hanselmann | _WaitForSync(self.lu, self.instance) |
7511 | a9e0c397 | Iustin Pop | |
7512 | 2bb5c911 | Michael Hanselmann | # Check all devices manually
|
7513 | 2bb5c911 | Michael Hanselmann | self._CheckDevices(self.instance.primary_node, iv_names) |
7514 | a9e0c397 | Iustin Pop | |
7515 | cff90b79 | Iustin Pop | # Step: remove old storage
|
7516 | 7ea7bcf6 | Iustin Pop | if not self.early_release: |
7517 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Removing old storage") |
7518 | 7ea7bcf6 | Iustin Pop | cstep += 1
|
7519 | 7ea7bcf6 | Iustin Pop | self._RemoveOldStorage(self.target_node, iv_names) |
7520 | a9e0c397 | Iustin Pop | |
7521 | a4eae71f | Michael Hanselmann | def _ExecDrbd8Secondary(self, feedback_fn): |
7522 | 2bb5c911 | Michael Hanselmann | """Replace the secondary node for DRBD 8.
|
7523 | a9e0c397 | Iustin Pop |
|
7524 | a9e0c397 | Iustin Pop | The algorithm for replace is quite complicated:
|
7525 | a9e0c397 | Iustin Pop | - for all disks of the instance:
|
7526 | a9e0c397 | Iustin Pop | - create new LVs on the new node with same names
|
7527 | a9e0c397 | Iustin Pop | - shutdown the drbd device on the old secondary
|
7528 | a9e0c397 | Iustin Pop | - disconnect the drbd network on the primary
|
7529 | a9e0c397 | Iustin Pop | - create the drbd device on the new secondary
|
7530 | a9e0c397 | Iustin Pop | - network attach the drbd on the primary, using an artifice:
|
7531 | a9e0c397 | Iustin Pop | the drbd code for Attach() will connect to the network if it
|
7532 | a9e0c397 | Iustin Pop | finds a device which is connected to the good local disks but
|
7533 | a9e0c397 | Iustin Pop | not network enabled
|
7534 | a9e0c397 | Iustin Pop | - wait for sync across all devices
|
7535 | a9e0c397 | Iustin Pop | - remove all disks from the old secondary
|
7536 | a9e0c397 | Iustin Pop |
|
7537 | a9e0c397 | Iustin Pop | Failures are not very well handled.
|
7538 | 0834c866 | Iustin Pop |
|
7539 | a9e0c397 | Iustin Pop | """
|
7540 | 0834c866 | Iustin Pop | steps_total = 6
|
7541 | 0834c866 | Iustin Pop | |
7542 | 0834c866 | Iustin Pop | # Step: check device activation
|
7543 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(1, steps_total, "Check device existence") |
7544 | 2bb5c911 | Michael Hanselmann | self._CheckDisksExistence([self.instance.primary_node]) |
7545 | 2bb5c911 | Michael Hanselmann | self._CheckVolumeGroup([self.instance.primary_node]) |
7546 | 0834c866 | Iustin Pop | |
7547 | 0834c866 | Iustin Pop | # Step: check other node consistency
|
7548 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(2, steps_total, "Check peer consistency") |
7549 | 2bb5c911 | Michael Hanselmann | self._CheckDisksConsistency(self.instance.primary_node, True, True) |
7550 | 0834c866 | Iustin Pop | |
7551 | 0834c866 | Iustin Pop | # Step: create new storage
|
7552 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(3, steps_total, "Allocate new storage") |
7553 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
7554 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding new local storage on %s for disk/%d" % |
7555 | 2bb5c911 | Michael Hanselmann | (self.new_node, idx))
|
7556 | 428958aa | Iustin Pop | # we pass force_create=True to force LVM creation
|
7557 | a9e0c397 | Iustin Pop | for new_lv in dev.children: |
7558 | 2bb5c911 | Michael Hanselmann | _CreateBlockDev(self.lu, self.new_node, self.instance, new_lv, True, |
7559 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
7560 | a9e0c397 | Iustin Pop | |
7561 | 468b46f9 | Iustin Pop | # Step 4: dbrd minors and drbd setups changes
|
7562 | a1578d63 | Iustin Pop | # after this, we must manually remove the drbd minors on both the
|
7563 | a1578d63 | Iustin Pop | # error and the success paths
|
7564 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
7565 | 4d4a651d | Michael Hanselmann | minors = self.cfg.AllocateDRBDMinor([self.new_node |
7566 | 4d4a651d | Michael Hanselmann | for dev in self.instance.disks], |
7567 | 2bb5c911 | Michael Hanselmann | self.instance.name)
|
7568 | 099c52ad | Iustin Pop | logging.debug("Allocated minors %r", minors)
|
7569 | 2bb5c911 | Michael Hanselmann | |
7570 | 2bb5c911 | Michael Hanselmann | iv_names = {} |
7571 | 2bb5c911 | Michael Hanselmann | for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)): |
7572 | 4d4a651d | Michael Hanselmann | self.lu.LogInfo("activating a new drbd on %s for disk/%d" % |
7573 | 4d4a651d | Michael Hanselmann | (self.new_node, idx))
|
7574 | a2d59d8b | Iustin Pop | # create new devices on new_node; note that we create two IDs:
|
7575 | a2d59d8b | Iustin Pop | # one without port, so the drbd will be activated without
|
7576 | a2d59d8b | Iustin Pop | # networking information on the new node at this stage, and one
|
7577 | a2d59d8b | Iustin Pop | # with network, for the latter activation in step 4
|
7578 | a2d59d8b | Iustin Pop | (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id |
7579 | 2bb5c911 | Michael Hanselmann | if self.instance.primary_node == o_node1: |
7580 | a2d59d8b | Iustin Pop | p_minor = o_minor1 |
7581 | ffa1c0dc | Iustin Pop | else:
|
7582 | 1122eb25 | Iustin Pop | assert self.instance.primary_node == o_node2, "Three-node instance?" |
7583 | a2d59d8b | Iustin Pop | p_minor = o_minor2 |
7584 | a2d59d8b | Iustin Pop | |
7585 | 4d4a651d | Michael Hanselmann | new_alone_id = (self.instance.primary_node, self.new_node, None, |
7586 | 4d4a651d | Michael Hanselmann | p_minor, new_minor, o_secret) |
7587 | 4d4a651d | Michael Hanselmann | new_net_id = (self.instance.primary_node, self.new_node, o_port, |
7588 | 4d4a651d | Michael Hanselmann | p_minor, new_minor, o_secret) |
7589 | a2d59d8b | Iustin Pop | |
7590 | a2d59d8b | Iustin Pop | iv_names[idx] = (dev, dev.children, new_net_id) |
7591 | a1578d63 | Iustin Pop | logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
|
7592 | a2d59d8b | Iustin Pop | new_net_id) |
7593 | a9e0c397 | Iustin Pop | new_drbd = objects.Disk(dev_type=constants.LD_DRBD8, |
7594 | a2d59d8b | Iustin Pop | logical_id=new_alone_id, |
7595 | 8a6c7011 | Iustin Pop | children=dev.children, |
7596 | 8a6c7011 | Iustin Pop | size=dev.size) |
7597 | 796cab27 | Iustin Pop | try:
|
7598 | 2bb5c911 | Michael Hanselmann | _CreateSingleBlockDev(self.lu, self.new_node, self.instance, new_drbd, |
7599 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
7600 | 82759cb1 | Iustin Pop | except errors.GenericError:
|
7601 | 2bb5c911 | Michael Hanselmann | self.cfg.ReleaseDRBDMinors(self.instance.name) |
7602 | 796cab27 | Iustin Pop | raise
|
7603 | a9e0c397 | Iustin Pop | |
7604 | 2bb5c911 | Michael Hanselmann | # We have new devices, shutdown the drbd on the old secondary
|
7605 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
7606 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Shutting down drbd for disk/%d on old node" % idx) |
7607 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, self.target_node) |
7608 | 2bb5c911 | Michael Hanselmann | msg = self.rpc.call_blockdev_shutdown(self.target_node, dev).fail_msg |
7609 | cacfd1fd | Iustin Pop | if msg:
|
7610 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old" |
7611 | 2bb5c911 | Michael Hanselmann | "node: %s" % (idx, msg),
|
7612 | 2bb5c911 | Michael Hanselmann | hint=("Please cleanup this device manually as"
|
7613 | 2bb5c911 | Michael Hanselmann | " soon as possible"))
|
7614 | a9e0c397 | Iustin Pop | |
7615 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)") |
7616 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_drbd_disconnect_net([self.instance.primary_node], |
7617 | 4d4a651d | Michael Hanselmann | self.node_secondary_ip,
|
7618 | 4d4a651d | Michael Hanselmann | self.instance.disks)\
|
7619 | 4d4a651d | Michael Hanselmann | [self.instance.primary_node]
|
7620 | 642445d9 | Iustin Pop | |
7621 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7622 | a2d59d8b | Iustin Pop | if msg:
|
7623 | a2d59d8b | Iustin Pop | # detaches didn't succeed (unlikely)
|
7624 | 2bb5c911 | Michael Hanselmann | self.cfg.ReleaseDRBDMinors(self.instance.name) |
7625 | a2d59d8b | Iustin Pop | raise errors.OpExecError("Can't detach the disks from the network on" |
7626 | a2d59d8b | Iustin Pop | " old node: %s" % (msg,))
|
7627 | 642445d9 | Iustin Pop | |
7628 | 642445d9 | Iustin Pop | # if we managed to detach at least one, we update all the disks of
|
7629 | 642445d9 | Iustin Pop | # the instance to point to the new secondary
|
7630 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Updating instance configuration") |
7631 | 468b46f9 | Iustin Pop | for dev, _, new_logical_id in iv_names.itervalues(): |
7632 | 468b46f9 | Iustin Pop | dev.logical_id = new_logical_id |
7633 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, self.instance.primary_node) |
7634 | 2bb5c911 | Michael Hanselmann | |
7635 | a4eae71f | Michael Hanselmann | self.cfg.Update(self.instance, feedback_fn) |
7636 | a9e0c397 | Iustin Pop | |
7637 | 642445d9 | Iustin Pop | # and now perform the drbd attach
|
7638 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Attaching primary drbds to new secondary" |
7639 | 2bb5c911 | Michael Hanselmann | " (standalone => connected)")
|
7640 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_drbd_attach_net([self.instance.primary_node, |
7641 | 4d4a651d | Michael Hanselmann | self.new_node],
|
7642 | 4d4a651d | Michael Hanselmann | self.node_secondary_ip,
|
7643 | 4d4a651d | Michael Hanselmann | self.instance.disks,
|
7644 | 4d4a651d | Michael Hanselmann | self.instance.name,
|
7645 | a2d59d8b | Iustin Pop | False)
|
7646 | a2d59d8b | Iustin Pop | for to_node, to_result in result.items(): |
7647 | 4c4e4e1e | Iustin Pop | msg = to_result.fail_msg |
7648 | a2d59d8b | Iustin Pop | if msg:
|
7649 | 4d4a651d | Michael Hanselmann | self.lu.LogWarning("Can't attach drbd disks on node %s: %s", |
7650 | 4d4a651d | Michael Hanselmann | to_node, msg, |
7651 | 2bb5c911 | Michael Hanselmann | hint=("please do a gnt-instance info to see the"
|
7652 | 2bb5c911 | Michael Hanselmann | " status of disks"))
|
7653 | 7ea7bcf6 | Iustin Pop | cstep = 5
|
7654 | 7ea7bcf6 | Iustin Pop | if self.early_release: |
7655 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Removing old storage") |
7656 | 7ea7bcf6 | Iustin Pop | cstep += 1
|
7657 | 7ea7bcf6 | Iustin Pop | self._RemoveOldStorage(self.target_node, iv_names) |
7658 | d5cd389c | Iustin Pop | # WARNING: we release all node locks here, do not do other RPCs
|
7659 | d5cd389c | Iustin Pop | # than WaitForSync to the primary node
|
7660 | d5cd389c | Iustin Pop | self._ReleaseNodeLock([self.instance.primary_node, |
7661 | d5cd389c | Iustin Pop | self.target_node,
|
7662 | d5cd389c | Iustin Pop | self.new_node])
|
7663 | a9e0c397 | Iustin Pop | |
7664 | 2bb5c911 | Michael Hanselmann | # Wait for sync
|
7665 | 2bb5c911 | Michael Hanselmann | # This can fail as the old devices are degraded and _WaitForSync
|
7666 | 2bb5c911 | Michael Hanselmann | # does a combined result over all disks, so we don't check its return value
|
7667 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Sync devices") |
7668 | 7ea7bcf6 | Iustin Pop | cstep += 1
|
7669 | b6c07b79 | Michael Hanselmann | _WaitForSync(self.lu, self.instance) |
7670 | a9e0c397 | Iustin Pop | |
7671 | 2bb5c911 | Michael Hanselmann | # Check all devices manually
|
7672 | 2bb5c911 | Michael Hanselmann | self._CheckDevices(self.instance.primary_node, iv_names) |
7673 | 22985314 | Guido Trotter | |
7674 | 2bb5c911 | Michael Hanselmann | # Step: remove old storage
|
7675 | 7ea7bcf6 | Iustin Pop | if not self.early_release: |
7676 | 7ea7bcf6 | Iustin Pop | self.lu.LogStep(cstep, steps_total, "Removing old storage") |
7677 | 7ea7bcf6 | Iustin Pop | self._RemoveOldStorage(self.target_node, iv_names) |
7678 | a9e0c397 | Iustin Pop | |
7679 | a8083063 | Iustin Pop | |
7680 | 76aef8fc | Michael Hanselmann | class LURepairNodeStorage(NoHooksLU): |
7681 | 76aef8fc | Michael Hanselmann | """Repairs the volume group on a node.
|
7682 | 76aef8fc | Michael Hanselmann |
|
7683 | 76aef8fc | Michael Hanselmann | """
|
7684 | 76aef8fc | Michael Hanselmann | _OP_REQP = ["node_name"]
|
7685 | 76aef8fc | Michael Hanselmann | REQ_BGL = False
|
7686 | 76aef8fc | Michael Hanselmann | |
7687 | 76aef8fc | Michael Hanselmann | def CheckArguments(self): |
7688 | cf26a87a | Iustin Pop | self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name) |
7689 | 76aef8fc | Michael Hanselmann | |
7690 | 0e3baaf3 | Iustin Pop | _CheckStorageType(self.op.storage_type)
|
7691 | 0e3baaf3 | Iustin Pop | |
7692 | 76aef8fc | Michael Hanselmann | def ExpandNames(self): |
7693 | 76aef8fc | Michael Hanselmann | self.needed_locks = {
|
7694 | 76aef8fc | Michael Hanselmann | locking.LEVEL_NODE: [self.op.node_name],
|
7695 | 76aef8fc | Michael Hanselmann | } |
7696 | 76aef8fc | Michael Hanselmann | |
7697 | 76aef8fc | Michael Hanselmann | def _CheckFaultyDisks(self, instance, node_name): |
7698 | 7e9c6a78 | Iustin Pop | """Ensure faulty disks abort the opcode or at least warn."""
|
7699 | 7e9c6a78 | Iustin Pop | try:
|
7700 | 7e9c6a78 | Iustin Pop | if _FindFaultyInstanceDisks(self.cfg, self.rpc, instance, |
7701 | 7e9c6a78 | Iustin Pop | node_name, True):
|
7702 | 7e9c6a78 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has faulty disks on" |
7703 | 7e9c6a78 | Iustin Pop | " node '%s'" % (instance.name, node_name),
|
7704 | 7e9c6a78 | Iustin Pop | errors.ECODE_STATE) |
7705 | 7e9c6a78 | Iustin Pop | except errors.OpPrereqError, err:
|
7706 | 7e9c6a78 | Iustin Pop | if self.op.ignore_consistency: |
7707 | 7e9c6a78 | Iustin Pop | self.proc.LogWarning(str(err.args[0])) |
7708 | 7e9c6a78 | Iustin Pop | else:
|
7709 | 7e9c6a78 | Iustin Pop | raise
|
7710 | 76aef8fc | Michael Hanselmann | |
7711 | 76aef8fc | Michael Hanselmann | def CheckPrereq(self): |
7712 | 76aef8fc | Michael Hanselmann | """Check prerequisites.
|
7713 | 76aef8fc | Michael Hanselmann |
|
7714 | 76aef8fc | Michael Hanselmann | """
|
7715 | 76aef8fc | Michael Hanselmann | storage_type = self.op.storage_type
|
7716 | 76aef8fc | Michael Hanselmann | |
7717 | 76aef8fc | Michael Hanselmann | if (constants.SO_FIX_CONSISTENCY not in |
7718 | 76aef8fc | Michael Hanselmann | constants.VALID_STORAGE_OPERATIONS.get(storage_type, [])): |
7719 | 76aef8fc | Michael Hanselmann | raise errors.OpPrereqError("Storage units of type '%s' can not be" |
7720 | 5c983ee5 | Iustin Pop | " repaired" % storage_type,
|
7721 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
7722 | 76aef8fc | Michael Hanselmann | |
7723 | 76aef8fc | Michael Hanselmann | # Check whether any instance on this node has faulty disks
|
7724 | 76aef8fc | Michael Hanselmann | for inst in _GetNodeInstances(self.cfg, self.op.node_name): |
7725 | 7e9c6a78 | Iustin Pop | if not inst.admin_up: |
7726 | 7e9c6a78 | Iustin Pop | continue
|
7727 | 76aef8fc | Michael Hanselmann | check_nodes = set(inst.all_nodes)
|
7728 | 76aef8fc | Michael Hanselmann | check_nodes.discard(self.op.node_name)
|
7729 | 76aef8fc | Michael Hanselmann | for inst_node_name in check_nodes: |
7730 | 76aef8fc | Michael Hanselmann | self._CheckFaultyDisks(inst, inst_node_name)
|
7731 | 76aef8fc | Michael Hanselmann | |
7732 | 76aef8fc | Michael Hanselmann | def Exec(self, feedback_fn): |
7733 | 76aef8fc | Michael Hanselmann | feedback_fn("Repairing storage unit '%s' on %s ..." %
|
7734 | 76aef8fc | Michael Hanselmann | (self.op.name, self.op.node_name)) |
7735 | 76aef8fc | Michael Hanselmann | |
7736 | 76aef8fc | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
7737 | 76aef8fc | Michael Hanselmann | result = self.rpc.call_storage_execute(self.op.node_name, |
7738 | 76aef8fc | Michael Hanselmann | self.op.storage_type, st_args,
|
7739 | 76aef8fc | Michael Hanselmann | self.op.name,
|
7740 | 76aef8fc | Michael Hanselmann | constants.SO_FIX_CONSISTENCY) |
7741 | 76aef8fc | Michael Hanselmann | result.Raise("Failed to repair storage unit '%s' on %s" %
|
7742 | 76aef8fc | Michael Hanselmann | (self.op.name, self.op.node_name)) |
7743 | 76aef8fc | Michael Hanselmann | |
7744 | 76aef8fc | Michael Hanselmann | |
7745 | f7e7689f | Iustin Pop | class LUNodeEvacuationStrategy(NoHooksLU): |
7746 | f7e7689f | Iustin Pop | """Computes the node evacuation strategy.
|
7747 | f7e7689f | Iustin Pop |
|
7748 | f7e7689f | Iustin Pop | """
|
7749 | f7e7689f | Iustin Pop | _OP_REQP = ["nodes"]
|
7750 | f7e7689f | Iustin Pop | REQ_BGL = False
|
7751 | f7e7689f | Iustin Pop | |
7752 | f7e7689f | Iustin Pop | def CheckArguments(self): |
7753 | f7e7689f | Iustin Pop | if not hasattr(self.op, "remote_node"): |
7754 | f7e7689f | Iustin Pop | self.op.remote_node = None |
7755 | f7e7689f | Iustin Pop | if not hasattr(self.op, "iallocator"): |
7756 | f7e7689f | Iustin Pop | self.op.iallocator = None |
7757 | f7e7689f | Iustin Pop | if self.op.remote_node is not None and self.op.iallocator is not None: |
7758 | f7e7689f | Iustin Pop | raise errors.OpPrereqError("Give either the iallocator or the new" |
7759 | f7e7689f | Iustin Pop | " secondary, not both", errors.ECODE_INVAL)
|
7760 | f7e7689f | Iustin Pop | |
7761 | f7e7689f | Iustin Pop | def ExpandNames(self): |
7762 | f7e7689f | Iustin Pop | self.op.nodes = _GetWantedNodes(self, self.op.nodes) |
7763 | f7e7689f | Iustin Pop | self.needed_locks = locks = {}
|
7764 | f7e7689f | Iustin Pop | if self.op.remote_node is None: |
7765 | f7e7689f | Iustin Pop | locks[locking.LEVEL_NODE] = locking.ALL_SET |
7766 | f7e7689f | Iustin Pop | else:
|
7767 | f7e7689f | Iustin Pop | self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node) |
7768 | f7e7689f | Iustin Pop | locks[locking.LEVEL_NODE] = self.op.nodes + [self.op.remote_node] |
7769 | f7e7689f | Iustin Pop | |
7770 | f7e7689f | Iustin Pop | def CheckPrereq(self): |
7771 | f7e7689f | Iustin Pop | pass
|
7772 | f7e7689f | Iustin Pop | |
7773 | f7e7689f | Iustin Pop | def Exec(self, feedback_fn): |
7774 | f7e7689f | Iustin Pop | if self.op.remote_node is not None: |
7775 | f7e7689f | Iustin Pop | instances = [] |
7776 | f7e7689f | Iustin Pop | for node in self.op.nodes: |
7777 | f7e7689f | Iustin Pop | instances.extend(_GetNodeSecondaryInstances(self.cfg, node))
|
7778 | f7e7689f | Iustin Pop | result = [] |
7779 | f7e7689f | Iustin Pop | for i in instances: |
7780 | f7e7689f | Iustin Pop | if i.primary_node == self.op.remote_node: |
7781 | f7e7689f | Iustin Pop | raise errors.OpPrereqError("Node %s is the primary node of" |
7782 | f7e7689f | Iustin Pop | " instance %s, cannot use it as"
|
7783 | f7e7689f | Iustin Pop | " secondary" %
|
7784 | f7e7689f | Iustin Pop | (self.op.remote_node, i.name),
|
7785 | f7e7689f | Iustin Pop | errors.ECODE_INVAL) |
7786 | f7e7689f | Iustin Pop | result.append([i.name, self.op.remote_node])
|
7787 | f7e7689f | Iustin Pop | else:
|
7788 | f7e7689f | Iustin Pop | ial = IAllocator(self.cfg, self.rpc, |
7789 | f7e7689f | Iustin Pop | mode=constants.IALLOCATOR_MODE_MEVAC, |
7790 | f7e7689f | Iustin Pop | evac_nodes=self.op.nodes)
|
7791 | f7e7689f | Iustin Pop | ial.Run(self.op.iallocator, validate=True) |
7792 | f7e7689f | Iustin Pop | if not ial.success: |
7793 | f7e7689f | Iustin Pop | raise errors.OpExecError("No valid evacuation solution: %s" % ial.info, |
7794 | f7e7689f | Iustin Pop | errors.ECODE_NORES) |
7795 | f7e7689f | Iustin Pop | result = ial.result |
7796 | f7e7689f | Iustin Pop | return result
|
7797 | f7e7689f | Iustin Pop | |
7798 | f7e7689f | Iustin Pop | |
7799 | 8729e0d7 | Iustin Pop | class LUGrowDisk(LogicalUnit): |
7800 | 8729e0d7 | Iustin Pop | """Grow a disk of an instance.
|
7801 | 8729e0d7 | Iustin Pop |
|
7802 | 8729e0d7 | Iustin Pop | """
|
7803 | 8729e0d7 | Iustin Pop | HPATH = "disk-grow"
|
7804 | 8729e0d7 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
7805 | 6605411d | Iustin Pop | _OP_REQP = ["instance_name", "disk", "amount", "wait_for_sync"] |
7806 | 31e63dbf | Guido Trotter | REQ_BGL = False
|
7807 | 31e63dbf | Guido Trotter | |
7808 | 31e63dbf | Guido Trotter | def ExpandNames(self): |
7809 | 31e63dbf | Guido Trotter | self._ExpandAndLockInstance()
|
7810 | 31e63dbf | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
7811 | f6d9a522 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
7812 | 31e63dbf | Guido Trotter | |
7813 | 31e63dbf | Guido Trotter | def DeclareLocks(self, level): |
7814 | 31e63dbf | Guido Trotter | if level == locking.LEVEL_NODE:
|
7815 | 31e63dbf | Guido Trotter | self._LockInstancesNodes()
|
7816 | 8729e0d7 | Iustin Pop | |
7817 | 8729e0d7 | Iustin Pop | def BuildHooksEnv(self): |
7818 | 8729e0d7 | Iustin Pop | """Build hooks env.
|
7819 | 8729e0d7 | Iustin Pop |
|
7820 | 8729e0d7 | Iustin Pop | This runs on the master, the primary and all the secondaries.
|
7821 | 8729e0d7 | Iustin Pop |
|
7822 | 8729e0d7 | Iustin Pop | """
|
7823 | 8729e0d7 | Iustin Pop | env = { |
7824 | 8729e0d7 | Iustin Pop | "DISK": self.op.disk, |
7825 | 8729e0d7 | Iustin Pop | "AMOUNT": self.op.amount, |
7826 | 8729e0d7 | Iustin Pop | } |
7827 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
7828 | abd8e836 | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
7829 | 8729e0d7 | Iustin Pop | return env, nl, nl
|
7830 | 8729e0d7 | Iustin Pop | |
7831 | 8729e0d7 | Iustin Pop | def CheckPrereq(self): |
7832 | 8729e0d7 | Iustin Pop | """Check prerequisites.
|
7833 | 8729e0d7 | Iustin Pop |
|
7834 | 8729e0d7 | Iustin Pop | This checks that the instance is in the cluster.
|
7835 | 8729e0d7 | Iustin Pop |
|
7836 | 8729e0d7 | Iustin Pop | """
|
7837 | 31e63dbf | Guido Trotter | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
7838 | 31e63dbf | Guido Trotter | assert instance is not None, \ |
7839 | 31e63dbf | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
7840 | 6b12959c | Iustin Pop | nodenames = list(instance.all_nodes)
|
7841 | 6b12959c | Iustin Pop | for node in nodenames: |
7842 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, node)
|
7843 | 7527a8a4 | Iustin Pop | |
7844 | 31e63dbf | Guido Trotter | |
7845 | 8729e0d7 | Iustin Pop | self.instance = instance
|
7846 | 8729e0d7 | Iustin Pop | |
7847 | 728489a3 | Guido Trotter | if instance.disk_template not in constants.DTS_GROWABLE: |
7848 | 8729e0d7 | Iustin Pop | raise errors.OpPrereqError("Instance's disk layout does not support" |
7849 | 5c983ee5 | Iustin Pop | " growing.", errors.ECODE_INVAL)
|
7850 | 8729e0d7 | Iustin Pop | |
7851 | ad24e046 | Iustin Pop | self.disk = instance.FindDisk(self.op.disk) |
7852 | 8729e0d7 | Iustin Pop | |
7853 | 2c42c5df | Guido Trotter | if instance.disk_template != constants.DT_FILE:
|
7854 | 2c42c5df | Guido Trotter | # TODO: check the free disk space for file, when that feature will be
|
7855 | 2c42c5df | Guido Trotter | # supported
|
7856 | 2c42c5df | Guido Trotter | _CheckNodesFreeDisk(self, nodenames, self.op.amount) |
7857 | 8729e0d7 | Iustin Pop | |
7858 | 8729e0d7 | Iustin Pop | def Exec(self, feedback_fn): |
7859 | 8729e0d7 | Iustin Pop | """Execute disk grow.
|
7860 | 8729e0d7 | Iustin Pop |
|
7861 | 8729e0d7 | Iustin Pop | """
|
7862 | 8729e0d7 | Iustin Pop | instance = self.instance
|
7863 | ad24e046 | Iustin Pop | disk = self.disk
|
7864 | 6b12959c | Iustin Pop | for node in instance.all_nodes: |
7865 | 8729e0d7 | Iustin Pop | self.cfg.SetDiskID(disk, node)
|
7866 | 72737a7f | Iustin Pop | result = self.rpc.call_blockdev_grow(node, disk, self.op.amount) |
7867 | 4c4e4e1e | Iustin Pop | result.Raise("Grow request failed to node %s" % node)
|
7868 | 5bc556dd | Michael Hanselmann | |
7869 | 5bc556dd | Michael Hanselmann | # TODO: Rewrite code to work properly
|
7870 | 5bc556dd | Michael Hanselmann | # DRBD goes into sync mode for a short amount of time after executing the
|
7871 | 5bc556dd | Michael Hanselmann | # "resize" command. DRBD 8.x below version 8.0.13 contains a bug whereby
|
7872 | 5bc556dd | Michael Hanselmann | # calling "resize" in sync mode fails. Sleeping for a short amount of
|
7873 | 5bc556dd | Michael Hanselmann | # time is a work-around.
|
7874 | 5bc556dd | Michael Hanselmann | time.sleep(5)
|
7875 | 5bc556dd | Michael Hanselmann | |
7876 | 8729e0d7 | Iustin Pop | disk.RecordGrow(self.op.amount)
|
7877 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
7878 | 6605411d | Iustin Pop | if self.op.wait_for_sync: |
7879 | cd4d138f | Guido Trotter | disk_abort = not _WaitForSync(self, instance) |
7880 | 6605411d | Iustin Pop | if disk_abort:
|
7881 | 86d9d3bb | Iustin Pop | self.proc.LogWarning("Warning: disk sync-ing has not returned a good" |
7882 | 86d9d3bb | Iustin Pop | " status.\nPlease check the instance.")
|
7883 | 8729e0d7 | Iustin Pop | |
7884 | 8729e0d7 | Iustin Pop | |
7885 | a8083063 | Iustin Pop | class LUQueryInstanceData(NoHooksLU): |
7886 | a8083063 | Iustin Pop | """Query runtime instance data.
|
7887 | a8083063 | Iustin Pop |
|
7888 | a8083063 | Iustin Pop | """
|
7889 | 57821cac | Iustin Pop | _OP_REQP = ["instances", "static"] |
7890 | a987fa48 | Guido Trotter | REQ_BGL = False
|
7891 | ae5849b5 | Michael Hanselmann | |
7892 | a987fa48 | Guido Trotter | def ExpandNames(self): |
7893 | a987fa48 | Guido Trotter | self.needed_locks = {}
|
7894 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
7895 | a987fa48 | Guido Trotter | |
7896 | a987fa48 | Guido Trotter | if not isinstance(self.op.instances, list): |
7897 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'", |
7898 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
7899 | a987fa48 | Guido Trotter | |
7900 | a987fa48 | Guido Trotter | if self.op.instances: |
7901 | a987fa48 | Guido Trotter | self.wanted_names = []
|
7902 | a987fa48 | Guido Trotter | for name in self.op.instances: |
7903 | cf26a87a | Iustin Pop | full_name = _ExpandInstanceName(self.cfg, name)
|
7904 | a987fa48 | Guido Trotter | self.wanted_names.append(full_name)
|
7905 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names |
7906 | a987fa48 | Guido Trotter | else:
|
7907 | a987fa48 | Guido Trotter | self.wanted_names = None |
7908 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
|
7909 | a987fa48 | Guido Trotter | |
7910 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
7911 | a987fa48 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
7912 | a987fa48 | Guido Trotter | |
7913 | a987fa48 | Guido Trotter | def DeclareLocks(self, level): |
7914 | a987fa48 | Guido Trotter | if level == locking.LEVEL_NODE:
|
7915 | a987fa48 | Guido Trotter | self._LockInstancesNodes()
|
7916 | a8083063 | Iustin Pop | |
7917 | a8083063 | Iustin Pop | def CheckPrereq(self): |
7918 | a8083063 | Iustin Pop | """Check prerequisites.
|
7919 | a8083063 | Iustin Pop |
|
7920 | a8083063 | Iustin Pop | This only checks the optional instance list against the existing names.
|
7921 | a8083063 | Iustin Pop |
|
7922 | a8083063 | Iustin Pop | """
|
7923 | a987fa48 | Guido Trotter | if self.wanted_names is None: |
7924 | a987fa48 | Guido Trotter | self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE] |
7925 | a8083063 | Iustin Pop | |
7926 | a987fa48 | Guido Trotter | self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name |
7927 | a987fa48 | Guido Trotter | in self.wanted_names] |
7928 | a987fa48 | Guido Trotter | return
|
7929 | a8083063 | Iustin Pop | |
7930 | 98825740 | Michael Hanselmann | def _ComputeBlockdevStatus(self, node, instance_name, dev): |
7931 | 98825740 | Michael Hanselmann | """Returns the status of a block device
|
7932 | 98825740 | Michael Hanselmann |
|
7933 | 98825740 | Michael Hanselmann | """
|
7934 | 4dce1a83 | Michael Hanselmann | if self.op.static or not node: |
7935 | 98825740 | Michael Hanselmann | return None |
7936 | 98825740 | Michael Hanselmann | |
7937 | 98825740 | Michael Hanselmann | self.cfg.SetDiskID(dev, node)
|
7938 | 98825740 | Michael Hanselmann | |
7939 | 98825740 | Michael Hanselmann | result = self.rpc.call_blockdev_find(node, dev)
|
7940 | 98825740 | Michael Hanselmann | if result.offline:
|
7941 | 98825740 | Michael Hanselmann | return None |
7942 | 98825740 | Michael Hanselmann | |
7943 | 98825740 | Michael Hanselmann | result.Raise("Can't compute disk status for %s" % instance_name)
|
7944 | 98825740 | Michael Hanselmann | |
7945 | 98825740 | Michael Hanselmann | status = result.payload |
7946 | ddfe2228 | Michael Hanselmann | if status is None: |
7947 | ddfe2228 | Michael Hanselmann | return None |
7948 | 98825740 | Michael Hanselmann | |
7949 | 98825740 | Michael Hanselmann | return (status.dev_path, status.major, status.minor,
|
7950 | 98825740 | Michael Hanselmann | status.sync_percent, status.estimated_time, |
7951 | f208978a | Michael Hanselmann | status.is_degraded, status.ldisk_status) |
7952 | 98825740 | Michael Hanselmann | |
7953 | a8083063 | Iustin Pop | def _ComputeDiskStatus(self, instance, snode, dev): |
7954 | a8083063 | Iustin Pop | """Compute block device status.
|
7955 | a8083063 | Iustin Pop |
|
7956 | a8083063 | Iustin Pop | """
|
7957 | a1f445d3 | Iustin Pop | if dev.dev_type in constants.LDS_DRBD: |
7958 | a8083063 | Iustin Pop | # we change the snode then (otherwise we use the one passed in)
|
7959 | a8083063 | Iustin Pop | if dev.logical_id[0] == instance.primary_node: |
7960 | a8083063 | Iustin Pop | snode = dev.logical_id[1]
|
7961 | a8083063 | Iustin Pop | else:
|
7962 | a8083063 | Iustin Pop | snode = dev.logical_id[0]
|
7963 | a8083063 | Iustin Pop | |
7964 | 98825740 | Michael Hanselmann | dev_pstatus = self._ComputeBlockdevStatus(instance.primary_node,
|
7965 | 98825740 | Michael Hanselmann | instance.name, dev) |
7966 | 98825740 | Michael Hanselmann | dev_sstatus = self._ComputeBlockdevStatus(snode, instance.name, dev)
|
7967 | a8083063 | Iustin Pop | |
7968 | a8083063 | Iustin Pop | if dev.children:
|
7969 | a8083063 | Iustin Pop | dev_children = [self._ComputeDiskStatus(instance, snode, child)
|
7970 | a8083063 | Iustin Pop | for child in dev.children] |
7971 | a8083063 | Iustin Pop | else:
|
7972 | a8083063 | Iustin Pop | dev_children = [] |
7973 | a8083063 | Iustin Pop | |
7974 | a8083063 | Iustin Pop | data = { |
7975 | a8083063 | Iustin Pop | "iv_name": dev.iv_name,
|
7976 | a8083063 | Iustin Pop | "dev_type": dev.dev_type,
|
7977 | a8083063 | Iustin Pop | "logical_id": dev.logical_id,
|
7978 | a8083063 | Iustin Pop | "physical_id": dev.physical_id,
|
7979 | a8083063 | Iustin Pop | "pstatus": dev_pstatus,
|
7980 | a8083063 | Iustin Pop | "sstatus": dev_sstatus,
|
7981 | a8083063 | Iustin Pop | "children": dev_children,
|
7982 | b6fdf8b8 | Iustin Pop | "mode": dev.mode,
|
7983 | c98162a7 | Iustin Pop | "size": dev.size,
|
7984 | a8083063 | Iustin Pop | } |
7985 | a8083063 | Iustin Pop | |
7986 | a8083063 | Iustin Pop | return data
|
7987 | a8083063 | Iustin Pop | |
7988 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
7989 | a8083063 | Iustin Pop | """Gather and return data"""
|
7990 | a8083063 | Iustin Pop | result = {} |
7991 | 338e51e8 | Iustin Pop | |
7992 | 338e51e8 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
7993 | 338e51e8 | Iustin Pop | |
7994 | a8083063 | Iustin Pop | for instance in self.wanted_instances: |
7995 | 57821cac | Iustin Pop | if not self.op.static: |
7996 | 57821cac | Iustin Pop | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
7997 | 57821cac | Iustin Pop | instance.name, |
7998 | 57821cac | Iustin Pop | instance.hypervisor) |
7999 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node)
|
8000 | 7ad1af4a | Iustin Pop | remote_info = remote_info.payload |
8001 | 57821cac | Iustin Pop | if remote_info and "state" in remote_info: |
8002 | 57821cac | Iustin Pop | remote_state = "up"
|
8003 | 57821cac | Iustin Pop | else:
|
8004 | 57821cac | Iustin Pop | remote_state = "down"
|
8005 | a8083063 | Iustin Pop | else:
|
8006 | 57821cac | Iustin Pop | remote_state = None
|
8007 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
8008 | a8083063 | Iustin Pop | config_state = "up"
|
8009 | 0d68c45d | Iustin Pop | else:
|
8010 | 0d68c45d | Iustin Pop | config_state = "down"
|
8011 | a8083063 | Iustin Pop | |
8012 | a8083063 | Iustin Pop | disks = [self._ComputeDiskStatus(instance, None, device) |
8013 | a8083063 | Iustin Pop | for device in instance.disks] |
8014 | a8083063 | Iustin Pop | |
8015 | a8083063 | Iustin Pop | idict = { |
8016 | a8083063 | Iustin Pop | "name": instance.name,
|
8017 | a8083063 | Iustin Pop | "config_state": config_state,
|
8018 | a8083063 | Iustin Pop | "run_state": remote_state,
|
8019 | a8083063 | Iustin Pop | "pnode": instance.primary_node,
|
8020 | a8083063 | Iustin Pop | "snodes": instance.secondary_nodes,
|
8021 | a8083063 | Iustin Pop | "os": instance.os,
|
8022 | 0b13832c | Guido Trotter | # this happens to be the same format used for hooks
|
8023 | 0b13832c | Guido Trotter | "nics": _NICListToTuple(self, instance.nics), |
8024 | a8083063 | Iustin Pop | "disks": disks,
|
8025 | e69d05fd | Iustin Pop | "hypervisor": instance.hypervisor,
|
8026 | 24838135 | Iustin Pop | "network_port": instance.network_port,
|
8027 | 24838135 | Iustin Pop | "hv_instance": instance.hvparams,
|
8028 | 7736a5f2 | Iustin Pop | "hv_actual": cluster.FillHV(instance, skip_globals=True), |
8029 | 338e51e8 | Iustin Pop | "be_instance": instance.beparams,
|
8030 | 338e51e8 | Iustin Pop | "be_actual": cluster.FillBE(instance),
|
8031 | 90f72445 | Iustin Pop | "serial_no": instance.serial_no,
|
8032 | 90f72445 | Iustin Pop | "mtime": instance.mtime,
|
8033 | 90f72445 | Iustin Pop | "ctime": instance.ctime,
|
8034 | 033d58b0 | Iustin Pop | "uuid": instance.uuid,
|
8035 | a8083063 | Iustin Pop | } |
8036 | a8083063 | Iustin Pop | |
8037 | a8083063 | Iustin Pop | result[instance.name] = idict |
8038 | a8083063 | Iustin Pop | |
8039 | a8083063 | Iustin Pop | return result
|
8040 | a8083063 | Iustin Pop | |
8041 | a8083063 | Iustin Pop | |
8042 | 7767bbf5 | Manuel Franceschini | class LUSetInstanceParams(LogicalUnit): |
8043 | a8083063 | Iustin Pop | """Modifies an instances's parameters.
|
8044 | a8083063 | Iustin Pop |
|
8045 | a8083063 | Iustin Pop | """
|
8046 | a8083063 | Iustin Pop | HPATH = "instance-modify"
|
8047 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
8048 | 24991749 | Iustin Pop | _OP_REQP = ["instance_name"]
|
8049 | 1a5c7281 | Guido Trotter | REQ_BGL = False
|
8050 | 1a5c7281 | Guido Trotter | |
8051 | 24991749 | Iustin Pop | def CheckArguments(self): |
8052 | 24991749 | Iustin Pop | if not hasattr(self.op, 'nics'): |
8053 | 24991749 | Iustin Pop | self.op.nics = []
|
8054 | 24991749 | Iustin Pop | if not hasattr(self.op, 'disks'): |
8055 | 24991749 | Iustin Pop | self.op.disks = []
|
8056 | 24991749 | Iustin Pop | if not hasattr(self.op, 'beparams'): |
8057 | 24991749 | Iustin Pop | self.op.beparams = {}
|
8058 | 24991749 | Iustin Pop | if not hasattr(self.op, 'hvparams'): |
8059 | 24991749 | Iustin Pop | self.op.hvparams = {}
|
8060 | e29e9550 | Iustin Pop | if not hasattr(self.op, "disk_template"): |
8061 | e29e9550 | Iustin Pop | self.op.disk_template = None |
8062 | e29e9550 | Iustin Pop | if not hasattr(self.op, "remote_node"): |
8063 | e29e9550 | Iustin Pop | self.op.remote_node = None |
8064 | 96b39bcc | Iustin Pop | if not hasattr(self.op, "os_name"): |
8065 | 96b39bcc | Iustin Pop | self.op.os_name = None |
8066 | 96b39bcc | Iustin Pop | if not hasattr(self.op, "force_variant"): |
8067 | 96b39bcc | Iustin Pop | self.op.force_variant = False |
8068 | 24991749 | Iustin Pop | self.op.force = getattr(self.op, "force", False) |
8069 | e29e9550 | Iustin Pop | if not (self.op.nics or self.op.disks or self.op.disk_template or |
8070 | 96b39bcc | Iustin Pop | self.op.hvparams or self.op.beparams or self.op.os_name): |
8071 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("No changes submitted", errors.ECODE_INVAL) |
8072 | 24991749 | Iustin Pop | |
8073 | 7736a5f2 | Iustin Pop | if self.op.hvparams: |
8074 | 7736a5f2 | Iustin Pop | _CheckGlobalHvParams(self.op.hvparams)
|
8075 | 7736a5f2 | Iustin Pop | |
8076 | 24991749 | Iustin Pop | # Disk validation
|
8077 | 24991749 | Iustin Pop | disk_addremove = 0
|
8078 | 24991749 | Iustin Pop | for disk_op, disk_dict in self.op.disks: |
8079 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
8080 | 24991749 | Iustin Pop | disk_addremove += 1
|
8081 | 24991749 | Iustin Pop | continue
|
8082 | 24991749 | Iustin Pop | elif disk_op == constants.DDM_ADD:
|
8083 | 24991749 | Iustin Pop | disk_addremove += 1
|
8084 | 24991749 | Iustin Pop | else:
|
8085 | 24991749 | Iustin Pop | if not isinstance(disk_op, int): |
8086 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid disk index", errors.ECODE_INVAL) |
8087 | 8b46606c | Guido Trotter | if not isinstance(disk_dict, dict): |
8088 | 8b46606c | Guido Trotter | msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
|
8089 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
8090 | 8b46606c | Guido Trotter | |
8091 | 24991749 | Iustin Pop | if disk_op == constants.DDM_ADD:
|
8092 | 24991749 | Iustin Pop | mode = disk_dict.setdefault('mode', constants.DISK_RDWR)
|
8093 | 6ec66eae | Iustin Pop | if mode not in constants.DISK_ACCESS_SET: |
8094 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode, |
8095 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8096 | 24991749 | Iustin Pop | size = disk_dict.get('size', None) |
8097 | 24991749 | Iustin Pop | if size is None: |
8098 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Required disk parameter size missing", |
8099 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8100 | 24991749 | Iustin Pop | try:
|
8101 | 24991749 | Iustin Pop | size = int(size)
|
8102 | 691744c4 | Iustin Pop | except (TypeError, ValueError), err: |
8103 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk size parameter: %s" % |
8104 | 5c983ee5 | Iustin Pop | str(err), errors.ECODE_INVAL)
|
8105 | 24991749 | Iustin Pop | disk_dict['size'] = size
|
8106 | 24991749 | Iustin Pop | else:
|
8107 | 24991749 | Iustin Pop | # modification of disk
|
8108 | 24991749 | Iustin Pop | if 'size' in disk_dict: |
8109 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Disk size change not possible, use" |
8110 | 5c983ee5 | Iustin Pop | " grow-disk", errors.ECODE_INVAL)
|
8111 | 24991749 | Iustin Pop | |
8112 | 24991749 | Iustin Pop | if disk_addremove > 1: |
8113 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Only one disk add or remove operation" |
8114 | 5c983ee5 | Iustin Pop | " supported at a time", errors.ECODE_INVAL)
|
8115 | 24991749 | Iustin Pop | |
8116 | e29e9550 | Iustin Pop | if self.op.disks and self.op.disk_template is not None: |
8117 | e29e9550 | Iustin Pop | raise errors.OpPrereqError("Disk template conversion and other disk" |
8118 | e29e9550 | Iustin Pop | " changes not supported at the same time",
|
8119 | e29e9550 | Iustin Pop | errors.ECODE_INVAL) |
8120 | e29e9550 | Iustin Pop | |
8121 | e29e9550 | Iustin Pop | if self.op.disk_template: |
8122 | e29e9550 | Iustin Pop | _CheckDiskTemplate(self.op.disk_template)
|
8123 | e29e9550 | Iustin Pop | if (self.op.disk_template in constants.DTS_NET_MIRROR and |
8124 | e29e9550 | Iustin Pop | self.op.remote_node is None): |
8125 | e29e9550 | Iustin Pop | raise errors.OpPrereqError("Changing the disk template to a mirrored" |
8126 | e29e9550 | Iustin Pop | " one requires specifying a secondary node",
|
8127 | e29e9550 | Iustin Pop | errors.ECODE_INVAL) |
8128 | e29e9550 | Iustin Pop | |
8129 | 24991749 | Iustin Pop | # NIC validation
|
8130 | 24991749 | Iustin Pop | nic_addremove = 0
|
8131 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
8132 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
8133 | 24991749 | Iustin Pop | nic_addremove += 1
|
8134 | 24991749 | Iustin Pop | continue
|
8135 | 24991749 | Iustin Pop | elif nic_op == constants.DDM_ADD:
|
8136 | 24991749 | Iustin Pop | nic_addremove += 1
|
8137 | 24991749 | Iustin Pop | else:
|
8138 | 24991749 | Iustin Pop | if not isinstance(nic_op, int): |
8139 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid nic index", errors.ECODE_INVAL) |
8140 | 8b46606c | Guido Trotter | if not isinstance(nic_dict, dict): |
8141 | 8b46606c | Guido Trotter | msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
|
8142 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
|
8143 | 24991749 | Iustin Pop | |
8144 | 24991749 | Iustin Pop | # nic_dict should be a dict
|
8145 | 24991749 | Iustin Pop | nic_ip = nic_dict.get('ip', None) |
8146 | 24991749 | Iustin Pop | if nic_ip is not None: |
8147 | 5c44da6a | Guido Trotter | if nic_ip.lower() == constants.VALUE_NONE:
|
8148 | 24991749 | Iustin Pop | nic_dict['ip'] = None |
8149 | 24991749 | Iustin Pop | else:
|
8150 | 24991749 | Iustin Pop | if not utils.IsValidIP(nic_ip): |
8151 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip, |
8152 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8153 | 5c44da6a | Guido Trotter | |
8154 | cd098c41 | Guido Trotter | nic_bridge = nic_dict.get('bridge', None) |
8155 | cd098c41 | Guido Trotter | nic_link = nic_dict.get('link', None) |
8156 | cd098c41 | Guido Trotter | if nic_bridge and nic_link: |
8157 | 29921401 | Iustin Pop | raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'" |
8158 | 5c983ee5 | Iustin Pop | " at the same time", errors.ECODE_INVAL)
|
8159 | cd098c41 | Guido Trotter | elif nic_bridge and nic_bridge.lower() == constants.VALUE_NONE: |
8160 | cd098c41 | Guido Trotter | nic_dict['bridge'] = None |
8161 | cd098c41 | Guido Trotter | elif nic_link and nic_link.lower() == constants.VALUE_NONE: |
8162 | cd098c41 | Guido Trotter | nic_dict['link'] = None |
8163 | cd098c41 | Guido Trotter | |
8164 | 5c44da6a | Guido Trotter | if nic_op == constants.DDM_ADD:
|
8165 | 5c44da6a | Guido Trotter | nic_mac = nic_dict.get('mac', None) |
8166 | 5c44da6a | Guido Trotter | if nic_mac is None: |
8167 | 5c44da6a | Guido Trotter | nic_dict['mac'] = constants.VALUE_AUTO
|
8168 | 5c44da6a | Guido Trotter | |
8169 | 5c44da6a | Guido Trotter | if 'mac' in nic_dict: |
8170 | 5c44da6a | Guido Trotter | nic_mac = nic_dict['mac']
|
8171 | 24991749 | Iustin Pop | if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
8172 | 82187135 | Renรฉ Nussbaumer | nic_mac = utils.NormalizeAndValidateMac(nic_mac) |
8173 | 82187135 | Renรฉ Nussbaumer | |
8174 | 5c44da6a | Guido Trotter | if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO: |
8175 | 5c44da6a | Guido Trotter | raise errors.OpPrereqError("'auto' is not a valid MAC address when" |
8176 | 5c983ee5 | Iustin Pop | " modifying an existing nic",
|
8177 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8178 | 5c44da6a | Guido Trotter | |
8179 | 24991749 | Iustin Pop | if nic_addremove > 1: |
8180 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Only one NIC add or remove operation" |
8181 | 5c983ee5 | Iustin Pop | " supported at a time", errors.ECODE_INVAL)
|
8182 | 24991749 | Iustin Pop | |
8183 | 1a5c7281 | Guido Trotter | def ExpandNames(self): |
8184 | 1a5c7281 | Guido Trotter | self._ExpandAndLockInstance()
|
8185 | 74409b12 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
8186 | 74409b12 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
8187 | 74409b12 | Iustin Pop | |
8188 | 74409b12 | Iustin Pop | def DeclareLocks(self, level): |
8189 | 74409b12 | Iustin Pop | if level == locking.LEVEL_NODE:
|
8190 | 74409b12 | Iustin Pop | self._LockInstancesNodes()
|
8191 | e29e9550 | Iustin Pop | if self.op.disk_template and self.op.remote_node: |
8192 | e29e9550 | Iustin Pop | self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node) |
8193 | e29e9550 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE].append(self.op.remote_node) |
8194 | a8083063 | Iustin Pop | |
8195 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
8196 | a8083063 | Iustin Pop | """Build hooks env.
|
8197 | a8083063 | Iustin Pop |
|
8198 | a8083063 | Iustin Pop | This runs on the master, primary and secondaries.
|
8199 | a8083063 | Iustin Pop |
|
8200 | a8083063 | Iustin Pop | """
|
8201 | 396e1b78 | Michael Hanselmann | args = dict()
|
8202 | 338e51e8 | Iustin Pop | if constants.BE_MEMORY in self.be_new: |
8203 | 338e51e8 | Iustin Pop | args['memory'] = self.be_new[constants.BE_MEMORY] |
8204 | 338e51e8 | Iustin Pop | if constants.BE_VCPUS in self.be_new: |
8205 | 61be6ba4 | Iustin Pop | args['vcpus'] = self.be_new[constants.BE_VCPUS] |
8206 | d8dcf3c9 | Guido Trotter | # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
|
8207 | d8dcf3c9 | Guido Trotter | # information at all.
|
8208 | d8dcf3c9 | Guido Trotter | if self.op.nics: |
8209 | d8dcf3c9 | Guido Trotter | args['nics'] = []
|
8210 | d8dcf3c9 | Guido Trotter | nic_override = dict(self.op.nics) |
8211 | 62f0dd02 | Guido Trotter | c_nicparams = self.cluster.nicparams[constants.PP_DEFAULT]
|
8212 | d8dcf3c9 | Guido Trotter | for idx, nic in enumerate(self.instance.nics): |
8213 | d8dcf3c9 | Guido Trotter | if idx in nic_override: |
8214 | d8dcf3c9 | Guido Trotter | this_nic_override = nic_override[idx] |
8215 | d8dcf3c9 | Guido Trotter | else:
|
8216 | d8dcf3c9 | Guido Trotter | this_nic_override = {} |
8217 | d8dcf3c9 | Guido Trotter | if 'ip' in this_nic_override: |
8218 | d8dcf3c9 | Guido Trotter | ip = this_nic_override['ip']
|
8219 | d8dcf3c9 | Guido Trotter | else:
|
8220 | d8dcf3c9 | Guido Trotter | ip = nic.ip |
8221 | d8dcf3c9 | Guido Trotter | if 'mac' in this_nic_override: |
8222 | d8dcf3c9 | Guido Trotter | mac = this_nic_override['mac']
|
8223 | d8dcf3c9 | Guido Trotter | else:
|
8224 | d8dcf3c9 | Guido Trotter | mac = nic.mac |
8225 | 62f0dd02 | Guido Trotter | if idx in self.nic_pnew: |
8226 | 62f0dd02 | Guido Trotter | nicparams = self.nic_pnew[idx]
|
8227 | 62f0dd02 | Guido Trotter | else:
|
8228 | 62f0dd02 | Guido Trotter | nicparams = objects.FillDict(c_nicparams, nic.nicparams) |
8229 | 62f0dd02 | Guido Trotter | mode = nicparams[constants.NIC_MODE] |
8230 | 62f0dd02 | Guido Trotter | link = nicparams[constants.NIC_LINK] |
8231 | 62f0dd02 | Guido Trotter | args['nics'].append((ip, mac, mode, link))
|
8232 | d8dcf3c9 | Guido Trotter | if constants.DDM_ADD in nic_override: |
8233 | d8dcf3c9 | Guido Trotter | ip = nic_override[constants.DDM_ADD].get('ip', None) |
8234 | d8dcf3c9 | Guido Trotter | mac = nic_override[constants.DDM_ADD]['mac']
|
8235 | 62f0dd02 | Guido Trotter | nicparams = self.nic_pnew[constants.DDM_ADD]
|
8236 | 62f0dd02 | Guido Trotter | mode = nicparams[constants.NIC_MODE] |
8237 | 62f0dd02 | Guido Trotter | link = nicparams[constants.NIC_LINK] |
8238 | 62f0dd02 | Guido Trotter | args['nics'].append((ip, mac, mode, link))
|
8239 | d8dcf3c9 | Guido Trotter | elif constants.DDM_REMOVE in nic_override: |
8240 | d8dcf3c9 | Guido Trotter | del args['nics'][-1] |
8241 | d8dcf3c9 | Guido Trotter | |
8242 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance, override=args) |
8243 | e29e9550 | Iustin Pop | if self.op.disk_template: |
8244 | e29e9550 | Iustin Pop | env["NEW_DISK_TEMPLATE"] = self.op.disk_template |
8245 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
8246 | a8083063 | Iustin Pop | return env, nl, nl
|
8247 | a8083063 | Iustin Pop | |
8248 | 7e950d31 | Iustin Pop | @staticmethod
|
8249 | 7e950d31 | Iustin Pop | def _GetUpdatedParams(old_params, update_dict, |
8250 | 0329617a | Guido Trotter | default_values, parameter_types): |
8251 | 0329617a | Guido Trotter | """Return the new params dict for the given params.
|
8252 | 0329617a | Guido Trotter |
|
8253 | 0329617a | Guido Trotter | @type old_params: dict
|
8254 | f2fd87d7 | Iustin Pop | @param old_params: old parameters
|
8255 | 0329617a | Guido Trotter | @type update_dict: dict
|
8256 | f2fd87d7 | Iustin Pop | @param update_dict: dict containing new parameter values,
|
8257 | f2fd87d7 | Iustin Pop | or constants.VALUE_DEFAULT to reset the
|
8258 | f2fd87d7 | Iustin Pop | parameter to its default value
|
8259 | 0329617a | Guido Trotter | @type default_values: dict
|
8260 | 0329617a | Guido Trotter | @param default_values: default values for the filled parameters
|
8261 | 0329617a | Guido Trotter | @type parameter_types: dict
|
8262 | 0329617a | Guido Trotter | @param parameter_types: dict mapping target dict keys to types
|
8263 | 0329617a | Guido Trotter | in constants.ENFORCEABLE_TYPES
|
8264 | 0329617a | Guido Trotter | @rtype: (dict, dict)
|
8265 | 0329617a | Guido Trotter | @return: (new_parameters, filled_parameters)
|
8266 | 0329617a | Guido Trotter |
|
8267 | 0329617a | Guido Trotter | """
|
8268 | 0329617a | Guido Trotter | params_copy = copy.deepcopy(old_params) |
8269 | 0329617a | Guido Trotter | for key, val in update_dict.iteritems(): |
8270 | 0329617a | Guido Trotter | if val == constants.VALUE_DEFAULT:
|
8271 | 0329617a | Guido Trotter | try:
|
8272 | 0329617a | Guido Trotter | del params_copy[key]
|
8273 | 0329617a | Guido Trotter | except KeyError: |
8274 | 0329617a | Guido Trotter | pass
|
8275 | 0329617a | Guido Trotter | else:
|
8276 | 0329617a | Guido Trotter | params_copy[key] = val |
8277 | 0329617a | Guido Trotter | utils.ForceDictType(params_copy, parameter_types) |
8278 | 0329617a | Guido Trotter | params_filled = objects.FillDict(default_values, params_copy) |
8279 | 0329617a | Guido Trotter | return (params_copy, params_filled)
|
8280 | 0329617a | Guido Trotter | |
8281 | a8083063 | Iustin Pop | def CheckPrereq(self): |
8282 | a8083063 | Iustin Pop | """Check prerequisites.
|
8283 | a8083063 | Iustin Pop |
|
8284 | a8083063 | Iustin Pop | This only checks the instance list against the existing names.
|
8285 | a8083063 | Iustin Pop |
|
8286 | a8083063 | Iustin Pop | """
|
8287 | 7c4d6c7b | Michael Hanselmann | self.force = self.op.force |
8288 | a8083063 | Iustin Pop | |
8289 | 74409b12 | Iustin Pop | # checking the new params on the primary/secondary nodes
|
8290 | 31a853d2 | Iustin Pop | |
8291 | cfefe007 | Guido Trotter | instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
8292 | 2ee88aeb | Guido Trotter | cluster = self.cluster = self.cfg.GetClusterInfo() |
8293 | 1a5c7281 | Guido Trotter | assert self.instance is not None, \ |
8294 | 1a5c7281 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
8295 | 6b12959c | Iustin Pop | pnode = instance.primary_node |
8296 | 6b12959c | Iustin Pop | nodelist = list(instance.all_nodes)
|
8297 | 74409b12 | Iustin Pop | |
8298 | e29e9550 | Iustin Pop | if self.op.disk_template: |
8299 | e29e9550 | Iustin Pop | if instance.disk_template == self.op.disk_template: |
8300 | e29e9550 | Iustin Pop | raise errors.OpPrereqError("Instance already has disk template %s" % |
8301 | e29e9550 | Iustin Pop | instance.disk_template, errors.ECODE_INVAL) |
8302 | e29e9550 | Iustin Pop | |
8303 | e29e9550 | Iustin Pop | if (instance.disk_template,
|
8304 | e29e9550 | Iustin Pop | self.op.disk_template) not in self._DISK_CONVERSIONS: |
8305 | e29e9550 | Iustin Pop | raise errors.OpPrereqError("Unsupported disk template conversion from" |
8306 | e29e9550 | Iustin Pop | " %s to %s" % (instance.disk_template,
|
8307 | e29e9550 | Iustin Pop | self.op.disk_template),
|
8308 | e29e9550 | Iustin Pop | errors.ECODE_INVAL) |
8309 | e29e9550 | Iustin Pop | if self.op.disk_template in constants.DTS_NET_MIRROR: |
8310 | e29e9550 | Iustin Pop | _CheckNodeOnline(self, self.op.remote_node) |
8311 | e29e9550 | Iustin Pop | _CheckNodeNotDrained(self, self.op.remote_node) |
8312 | e29e9550 | Iustin Pop | disks = [{"size": d.size} for d in instance.disks] |
8313 | e29e9550 | Iustin Pop | required = _ComputeDiskSize(self.op.disk_template, disks)
|
8314 | e29e9550 | Iustin Pop | _CheckNodesFreeDisk(self, [self.op.remote_node], required) |
8315 | e29e9550 | Iustin Pop | _CheckInstanceDown(self, instance, "cannot change disk template") |
8316 | e29e9550 | Iustin Pop | |
8317 | 338e51e8 | Iustin Pop | # hvparams processing
|
8318 | 74409b12 | Iustin Pop | if self.op.hvparams: |
8319 | 0329617a | Guido Trotter | i_hvdict, hv_new = self._GetUpdatedParams(
|
8320 | 0329617a | Guido Trotter | instance.hvparams, self.op.hvparams,
|
8321 | 0329617a | Guido Trotter | cluster.hvparams[instance.hypervisor], |
8322 | 0329617a | Guido Trotter | constants.HVS_PARAMETER_TYPES) |
8323 | 74409b12 | Iustin Pop | # local check
|
8324 | 74409b12 | Iustin Pop | hypervisor.GetHypervisor( |
8325 | 74409b12 | Iustin Pop | instance.hypervisor).CheckParameterSyntax(hv_new) |
8326 | 74409b12 | Iustin Pop | _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
|
8327 | 338e51e8 | Iustin Pop | self.hv_new = hv_new # the new actual values |
8328 | 338e51e8 | Iustin Pop | self.hv_inst = i_hvdict # the new dict (without defaults) |
8329 | 338e51e8 | Iustin Pop | else:
|
8330 | 338e51e8 | Iustin Pop | self.hv_new = self.hv_inst = {} |
8331 | 338e51e8 | Iustin Pop | |
8332 | 338e51e8 | Iustin Pop | # beparams processing
|
8333 | 338e51e8 | Iustin Pop | if self.op.beparams: |
8334 | 0329617a | Guido Trotter | i_bedict, be_new = self._GetUpdatedParams(
|
8335 | 0329617a | Guido Trotter | instance.beparams, self.op.beparams,
|
8336 | 0329617a | Guido Trotter | cluster.beparams[constants.PP_DEFAULT], |
8337 | 0329617a | Guido Trotter | constants.BES_PARAMETER_TYPES) |
8338 | 338e51e8 | Iustin Pop | self.be_new = be_new # the new actual values |
8339 | 338e51e8 | Iustin Pop | self.be_inst = i_bedict # the new dict (without defaults) |
8340 | 338e51e8 | Iustin Pop | else:
|
8341 | b637ae4d | Iustin Pop | self.be_new = self.be_inst = {} |
8342 | 74409b12 | Iustin Pop | |
8343 | cfefe007 | Guido Trotter | self.warn = []
|
8344 | 647a5d80 | Iustin Pop | |
8345 | 338e51e8 | Iustin Pop | if constants.BE_MEMORY in self.op.beparams and not self.force: |
8346 | 647a5d80 | Iustin Pop | mem_check_list = [pnode] |
8347 | c0f2b229 | Iustin Pop | if be_new[constants.BE_AUTO_BALANCE]:
|
8348 | c0f2b229 | Iustin Pop | # either we changed auto_balance to yes or it was from before
|
8349 | 647a5d80 | Iustin Pop | mem_check_list.extend(instance.secondary_nodes) |
8350 | 72737a7f | Iustin Pop | instance_info = self.rpc.call_instance_info(pnode, instance.name,
|
8351 | 72737a7f | Iustin Pop | instance.hypervisor) |
8352 | 647a5d80 | Iustin Pop | nodeinfo = self.rpc.call_node_info(mem_check_list, self.cfg.GetVGName(), |
8353 | 72737a7f | Iustin Pop | instance.hypervisor) |
8354 | 070e998b | Iustin Pop | pninfo = nodeinfo[pnode] |
8355 | 4c4e4e1e | Iustin Pop | msg = pninfo.fail_msg |
8356 | 070e998b | Iustin Pop | if msg:
|
8357 | cfefe007 | Guido Trotter | # Assume the primary node is unreachable and go ahead
|
8358 | 070e998b | Iustin Pop | self.warn.append("Can't get info from primary node %s: %s" % |
8359 | 070e998b | Iustin Pop | (pnode, msg)) |
8360 | 070e998b | Iustin Pop | elif not isinstance(pninfo.payload.get('memory_free', None), int): |
8361 | 070e998b | Iustin Pop | self.warn.append("Node data from primary node %s doesn't contain" |
8362 | 070e998b | Iustin Pop | " free memory information" % pnode)
|
8363 | 4c4e4e1e | Iustin Pop | elif instance_info.fail_msg:
|
8364 | 7ad1af4a | Iustin Pop | self.warn.append("Can't get instance runtime information: %s" % |
8365 | 4c4e4e1e | Iustin Pop | instance_info.fail_msg) |
8366 | cfefe007 | Guido Trotter | else:
|
8367 | 7ad1af4a | Iustin Pop | if instance_info.payload:
|
8368 | 7ad1af4a | Iustin Pop | current_mem = int(instance_info.payload['memory']) |
8369 | cfefe007 | Guido Trotter | else:
|
8370 | cfefe007 | Guido Trotter | # Assume instance not running
|
8371 | cfefe007 | Guido Trotter | # (there is a slight race condition here, but it's not very probable,
|
8372 | cfefe007 | Guido Trotter | # and we have no other way to check)
|
8373 | cfefe007 | Guido Trotter | current_mem = 0
|
8374 | 338e51e8 | Iustin Pop | miss_mem = (be_new[constants.BE_MEMORY] - current_mem - |
8375 | 070e998b | Iustin Pop | pninfo.payload['memory_free'])
|
8376 | cfefe007 | Guido Trotter | if miss_mem > 0: |
8377 | cfefe007 | Guido Trotter | raise errors.OpPrereqError("This change will prevent the instance" |
8378 | cfefe007 | Guido Trotter | " from starting, due to %d MB of memory"
|
8379 | 5c983ee5 | Iustin Pop | " missing on its primary node" % miss_mem,
|
8380 | 5c983ee5 | Iustin Pop | errors.ECODE_NORES) |
8381 | cfefe007 | Guido Trotter | |
8382 | c0f2b229 | Iustin Pop | if be_new[constants.BE_AUTO_BALANCE]:
|
8383 | 070e998b | Iustin Pop | for node, nres in nodeinfo.items(): |
8384 | ea33068f | Iustin Pop | if node not in instance.secondary_nodes: |
8385 | ea33068f | Iustin Pop | continue
|
8386 | 4c4e4e1e | Iustin Pop | msg = nres.fail_msg |
8387 | 070e998b | Iustin Pop | if msg:
|
8388 | 070e998b | Iustin Pop | self.warn.append("Can't get info from secondary node %s: %s" % |
8389 | 070e998b | Iustin Pop | (node, msg)) |
8390 | 070e998b | Iustin Pop | elif not isinstance(nres.payload.get('memory_free', None), int): |
8391 | 070e998b | Iustin Pop | self.warn.append("Secondary node %s didn't return free" |
8392 | 070e998b | Iustin Pop | " memory information" % node)
|
8393 | 070e998b | Iustin Pop | elif be_new[constants.BE_MEMORY] > nres.payload['memory_free']: |
8394 | 647a5d80 | Iustin Pop | self.warn.append("Not enough memory to failover instance to" |
8395 | 647a5d80 | Iustin Pop | " secondary node %s" % node)
|
8396 | 5bc84f33 | Alexander Schreiber | |
8397 | 24991749 | Iustin Pop | # NIC processing
|
8398 | cd098c41 | Guido Trotter | self.nic_pnew = {}
|
8399 | cd098c41 | Guido Trotter | self.nic_pinst = {}
|
8400 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
8401 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
8402 | 24991749 | Iustin Pop | if not instance.nics: |
8403 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Instance has no NICs, cannot remove", |
8404 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8405 | 24991749 | Iustin Pop | continue
|
8406 | 24991749 | Iustin Pop | if nic_op != constants.DDM_ADD:
|
8407 | 24991749 | Iustin Pop | # an existing nic
|
8408 | 21bcb9aa | Michael Hanselmann | if not instance.nics: |
8409 | 21bcb9aa | Michael Hanselmann | raise errors.OpPrereqError("Invalid NIC index %s, instance has" |
8410 | 21bcb9aa | Michael Hanselmann | " no NICs" % nic_op,
|
8411 | 21bcb9aa | Michael Hanselmann | errors.ECODE_INVAL) |
8412 | 24991749 | Iustin Pop | if nic_op < 0 or nic_op >= len(instance.nics): |
8413 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid NIC index %s, valid values" |
8414 | 24991749 | Iustin Pop | " are 0 to %d" %
|
8415 | 21bcb9aa | Michael Hanselmann | (nic_op, len(instance.nics) - 1), |
8416 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8417 | cd098c41 | Guido Trotter | old_nic_params = instance.nics[nic_op].nicparams |
8418 | cd098c41 | Guido Trotter | old_nic_ip = instance.nics[nic_op].ip |
8419 | cd098c41 | Guido Trotter | else:
|
8420 | cd098c41 | Guido Trotter | old_nic_params = {} |
8421 | cd098c41 | Guido Trotter | old_nic_ip = None
|
8422 | cd098c41 | Guido Trotter | |
8423 | cd098c41 | Guido Trotter | update_params_dict = dict([(key, nic_dict[key])
|
8424 | cd098c41 | Guido Trotter | for key in constants.NICS_PARAMETERS |
8425 | cd098c41 | Guido Trotter | if key in nic_dict]) |
8426 | cd098c41 | Guido Trotter | |
8427 | 5c44da6a | Guido Trotter | if 'bridge' in nic_dict: |
8428 | cd098c41 | Guido Trotter | update_params_dict[constants.NIC_LINK] = nic_dict['bridge']
|
8429 | cd098c41 | Guido Trotter | |
8430 | cd098c41 | Guido Trotter | new_nic_params, new_filled_nic_params = \ |
8431 | cd098c41 | Guido Trotter | self._GetUpdatedParams(old_nic_params, update_params_dict,
|
8432 | cd098c41 | Guido Trotter | cluster.nicparams[constants.PP_DEFAULT], |
8433 | cd098c41 | Guido Trotter | constants.NICS_PARAMETER_TYPES) |
8434 | cd098c41 | Guido Trotter | objects.NIC.CheckParameterSyntax(new_filled_nic_params) |
8435 | cd098c41 | Guido Trotter | self.nic_pinst[nic_op] = new_nic_params
|
8436 | cd098c41 | Guido Trotter | self.nic_pnew[nic_op] = new_filled_nic_params
|
8437 | cd098c41 | Guido Trotter | new_nic_mode = new_filled_nic_params[constants.NIC_MODE] |
8438 | cd098c41 | Guido Trotter | |
8439 | cd098c41 | Guido Trotter | if new_nic_mode == constants.NIC_MODE_BRIDGED:
|
8440 | cd098c41 | Guido Trotter | nic_bridge = new_filled_nic_params[constants.NIC_LINK] |
8441 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_bridges_exist(pnode, [nic_bridge]).fail_msg
|
8442 | 35c0c8da | Iustin Pop | if msg:
|
8443 | 35c0c8da | Iustin Pop | msg = "Error checking bridges on node %s: %s" % (pnode, msg)
|
8444 | 24991749 | Iustin Pop | if self.force: |
8445 | 24991749 | Iustin Pop | self.warn.append(msg)
|
8446 | 24991749 | Iustin Pop | else:
|
8447 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
|
8448 | cd098c41 | Guido Trotter | if new_nic_mode == constants.NIC_MODE_ROUTED:
|
8449 | cd098c41 | Guido Trotter | if 'ip' in nic_dict: |
8450 | cd098c41 | Guido Trotter | nic_ip = nic_dict['ip']
|
8451 | cd098c41 | Guido Trotter | else:
|
8452 | cd098c41 | Guido Trotter | nic_ip = old_nic_ip |
8453 | cd098c41 | Guido Trotter | if nic_ip is None: |
8454 | cd098c41 | Guido Trotter | raise errors.OpPrereqError('Cannot set the nic ip to None' |
8455 | 5c983ee5 | Iustin Pop | ' on a routed nic', errors.ECODE_INVAL)
|
8456 | 5c44da6a | Guido Trotter | if 'mac' in nic_dict: |
8457 | 5c44da6a | Guido Trotter | nic_mac = nic_dict['mac']
|
8458 | 5c44da6a | Guido Trotter | if nic_mac is None: |
8459 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError('Cannot set the nic mac to None', |
8460 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8461 | 5c44da6a | Guido Trotter | elif nic_mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
8462 | 5c44da6a | Guido Trotter | # otherwise generate the mac
|
8463 | 36b66e6e | Guido Trotter | nic_dict['mac'] = self.cfg.GenerateMAC(self.proc.GetECId()) |
8464 | 5c44da6a | Guido Trotter | else:
|
8465 | 5c44da6a | Guido Trotter | # or validate/reserve the current one
|
8466 | 36b66e6e | Guido Trotter | try:
|
8467 | 36b66e6e | Guido Trotter | self.cfg.ReserveMAC(nic_mac, self.proc.GetECId()) |
8468 | 36b66e6e | Guido Trotter | except errors.ReservationError:
|
8469 | 5c44da6a | Guido Trotter | raise errors.OpPrereqError("MAC address %s already in use" |
8470 | 5c983ee5 | Iustin Pop | " in cluster" % nic_mac,
|
8471 | 5c983ee5 | Iustin Pop | errors.ECODE_NOTUNIQUE) |
8472 | 24991749 | Iustin Pop | |
8473 | 24991749 | Iustin Pop | # DISK processing
|
8474 | 24991749 | Iustin Pop | if self.op.disks and instance.disk_template == constants.DT_DISKLESS: |
8475 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Disk operations not supported for" |
8476 | 5c983ee5 | Iustin Pop | " diskless instances",
|
8477 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8478 | 1122eb25 | Iustin Pop | for disk_op, _ in self.op.disks: |
8479 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
8480 | 24991749 | Iustin Pop | if len(instance.disks) == 1: |
8481 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Cannot remove the last disk of" |
8482 | 31624382 | Iustin Pop | " an instance", errors.ECODE_INVAL)
|
8483 | 31624382 | Iustin Pop | _CheckInstanceDown(self, instance, "cannot remove disks") |
8484 | 24991749 | Iustin Pop | |
8485 | 24991749 | Iustin Pop | if (disk_op == constants.DDM_ADD and |
8486 | 24991749 | Iustin Pop | len(instance.nics) >= constants.MAX_DISKS):
|
8487 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Instance has too many disks (%d), cannot" |
8488 | 5c983ee5 | Iustin Pop | " add more" % constants.MAX_DISKS,
|
8489 | 5c983ee5 | Iustin Pop | errors.ECODE_STATE) |
8490 | 24991749 | Iustin Pop | if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE): |
8491 | 24991749 | Iustin Pop | # an existing disk
|
8492 | 24991749 | Iustin Pop | if disk_op < 0 or disk_op >= len(instance.disks): |
8493 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk index %s, valid values" |
8494 | 24991749 | Iustin Pop | " are 0 to %d" %
|
8495 | 5c983ee5 | Iustin Pop | (disk_op, len(instance.disks)),
|
8496 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
8497 | 24991749 | Iustin Pop | |
8498 | 96b39bcc | Iustin Pop | # OS change
|
8499 | 96b39bcc | Iustin Pop | if self.op.os_name and not self.op.force: |
8500 | 96b39bcc | Iustin Pop | _CheckNodeHasOS(self, instance.primary_node, self.op.os_name, |
8501 | 96b39bcc | Iustin Pop | self.op.force_variant)
|
8502 | 96b39bcc | Iustin Pop | |
8503 | a8083063 | Iustin Pop | return
|
8504 | a8083063 | Iustin Pop | |
8505 | e29e9550 | Iustin Pop | def _ConvertPlainToDrbd(self, feedback_fn): |
8506 | e29e9550 | Iustin Pop | """Converts an instance from plain to drbd.
|
8507 | e29e9550 | Iustin Pop |
|
8508 | e29e9550 | Iustin Pop | """
|
8509 | e29e9550 | Iustin Pop | feedback_fn("Converting template to drbd")
|
8510 | e29e9550 | Iustin Pop | instance = self.instance
|
8511 | e29e9550 | Iustin Pop | pnode = instance.primary_node |
8512 | e29e9550 | Iustin Pop | snode = self.op.remote_node
|
8513 | e29e9550 | Iustin Pop | |
8514 | e29e9550 | Iustin Pop | # create a fake disk info for _GenerateDiskTemplate
|
8515 | e29e9550 | Iustin Pop | disk_info = [{"size": d.size, "mode": d.mode} for d in instance.disks] |
8516 | e29e9550 | Iustin Pop | new_disks = _GenerateDiskTemplate(self, self.op.disk_template, |
8517 | e29e9550 | Iustin Pop | instance.name, pnode, [snode], |
8518 | e29e9550 | Iustin Pop | disk_info, None, None, 0) |
8519 | e29e9550 | Iustin Pop | info = _GetInstanceInfoText(instance) |
8520 | e29e9550 | Iustin Pop | feedback_fn("Creating aditional volumes...")
|
8521 | e29e9550 | Iustin Pop | # first, create the missing data and meta devices
|
8522 | e29e9550 | Iustin Pop | for disk in new_disks: |
8523 | e29e9550 | Iustin Pop | # unfortunately this is... not too nice
|
8524 | e29e9550 | Iustin Pop | _CreateSingleBlockDev(self, pnode, instance, disk.children[1], |
8525 | e29e9550 | Iustin Pop | info, True)
|
8526 | e29e9550 | Iustin Pop | for child in disk.children: |
8527 | e29e9550 | Iustin Pop | _CreateSingleBlockDev(self, snode, instance, child, info, True) |
8528 | e29e9550 | Iustin Pop | # at this stage, all new LVs have been created, we can rename the
|
8529 | e29e9550 | Iustin Pop | # old ones
|
8530 | e29e9550 | Iustin Pop | feedback_fn("Renaming original volumes...")
|
8531 | e29e9550 | Iustin Pop | rename_list = [(o, n.children[0].logical_id)
|
8532 | e29e9550 | Iustin Pop | for (o, n) in zip(instance.disks, new_disks)] |
8533 | e29e9550 | Iustin Pop | result = self.rpc.call_blockdev_rename(pnode, rename_list)
|
8534 | e29e9550 | Iustin Pop | result.Raise("Failed to rename original LVs")
|
8535 | e29e9550 | Iustin Pop | |
8536 | e29e9550 | Iustin Pop | feedback_fn("Initializing DRBD devices...")
|
8537 | e29e9550 | Iustin Pop | # all child devices are in place, we can now create the DRBD devices
|
8538 | e29e9550 | Iustin Pop | for disk in new_disks: |
8539 | e29e9550 | Iustin Pop | for node in [pnode, snode]: |
8540 | e29e9550 | Iustin Pop | f_create = node == pnode |
8541 | e29e9550 | Iustin Pop | _CreateSingleBlockDev(self, node, instance, disk, info, f_create)
|
8542 | e29e9550 | Iustin Pop | |
8543 | e29e9550 | Iustin Pop | # at this point, the instance has been modified
|
8544 | e29e9550 | Iustin Pop | instance.disk_template = constants.DT_DRBD8 |
8545 | e29e9550 | Iustin Pop | instance.disks = new_disks |
8546 | e29e9550 | Iustin Pop | self.cfg.Update(instance, feedback_fn)
|
8547 | e29e9550 | Iustin Pop | |
8548 | e29e9550 | Iustin Pop | # disks are created, waiting for sync
|
8549 | e29e9550 | Iustin Pop | disk_abort = not _WaitForSync(self, instance) |
8550 | e29e9550 | Iustin Pop | if disk_abort:
|
8551 | e29e9550 | Iustin Pop | raise errors.OpExecError("There are some degraded disks for" |
8552 | e29e9550 | Iustin Pop | " this instance, please cleanup manually")
|
8553 | e29e9550 | Iustin Pop | |
8554 | 2f414c48 | Iustin Pop | def _ConvertDrbdToPlain(self, feedback_fn): |
8555 | 2f414c48 | Iustin Pop | """Converts an instance from drbd to plain.
|
8556 | 2f414c48 | Iustin Pop |
|
8557 | 2f414c48 | Iustin Pop | """
|
8558 | 2f414c48 | Iustin Pop | instance = self.instance
|
8559 | 2f414c48 | Iustin Pop | assert len(instance.secondary_nodes) == 1 |
8560 | 2f414c48 | Iustin Pop | pnode = instance.primary_node |
8561 | 2f414c48 | Iustin Pop | snode = instance.secondary_nodes[0]
|
8562 | 2f414c48 | Iustin Pop | feedback_fn("Converting template to plain")
|
8563 | 2f414c48 | Iustin Pop | |
8564 | 2f414c48 | Iustin Pop | old_disks = instance.disks |
8565 | 2f414c48 | Iustin Pop | new_disks = [d.children[0] for d in old_disks] |
8566 | 2f414c48 | Iustin Pop | |
8567 | 2f414c48 | Iustin Pop | # copy over size and mode
|
8568 | 2f414c48 | Iustin Pop | for parent, child in zip(old_disks, new_disks): |
8569 | 2f414c48 | Iustin Pop | child.size = parent.size |
8570 | 2f414c48 | Iustin Pop | child.mode = parent.mode |
8571 | 2f414c48 | Iustin Pop | |
8572 | 2f414c48 | Iustin Pop | # update instance structure
|
8573 | 2f414c48 | Iustin Pop | instance.disks = new_disks |
8574 | 2f414c48 | Iustin Pop | instance.disk_template = constants.DT_PLAIN |
8575 | 2f414c48 | Iustin Pop | self.cfg.Update(instance, feedback_fn)
|
8576 | 2f414c48 | Iustin Pop | |
8577 | 2f414c48 | Iustin Pop | feedback_fn("Removing volumes on the secondary node...")
|
8578 | 2f414c48 | Iustin Pop | for disk in old_disks: |
8579 | 2f414c48 | Iustin Pop | self.cfg.SetDiskID(disk, snode)
|
8580 | 2f414c48 | Iustin Pop | msg = self.rpc.call_blockdev_remove(snode, disk).fail_msg
|
8581 | 2f414c48 | Iustin Pop | if msg:
|
8582 | 2f414c48 | Iustin Pop | self.LogWarning("Could not remove block device %s on node %s," |
8583 | 2f414c48 | Iustin Pop | " continuing anyway: %s", disk.iv_name, snode, msg)
|
8584 | 2f414c48 | Iustin Pop | |
8585 | 2f414c48 | Iustin Pop | feedback_fn("Removing unneeded volumes on the primary node...")
|
8586 | 2f414c48 | Iustin Pop | for idx, disk in enumerate(old_disks): |
8587 | 2f414c48 | Iustin Pop | meta = disk.children[1]
|
8588 | 2f414c48 | Iustin Pop | self.cfg.SetDiskID(meta, pnode)
|
8589 | 2f414c48 | Iustin Pop | msg = self.rpc.call_blockdev_remove(pnode, meta).fail_msg
|
8590 | 2f414c48 | Iustin Pop | if msg:
|
8591 | 2f414c48 | Iustin Pop | self.LogWarning("Could not remove metadata for disk %d on node %s," |
8592 | 2f414c48 | Iustin Pop | " continuing anyway: %s", idx, pnode, msg)
|
8593 | 2f414c48 | Iustin Pop | |
8594 | 2f414c48 | Iustin Pop | |
8595 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
8596 | a8083063 | Iustin Pop | """Modifies an instance.
|
8597 | a8083063 | Iustin Pop |
|
8598 | a8083063 | Iustin Pop | All parameters take effect only at the next restart of the instance.
|
8599 | 24991749 | Iustin Pop |
|
8600 | a8083063 | Iustin Pop | """
|
8601 | cfefe007 | Guido Trotter | # Process here the warnings from CheckPrereq, as we don't have a
|
8602 | cfefe007 | Guido Trotter | # feedback_fn there.
|
8603 | cfefe007 | Guido Trotter | for warn in self.warn: |
8604 | cfefe007 | Guido Trotter | feedback_fn("WARNING: %s" % warn)
|
8605 | cfefe007 | Guido Trotter | |
8606 | a8083063 | Iustin Pop | result = [] |
8607 | a8083063 | Iustin Pop | instance = self.instance
|
8608 | 24991749 | Iustin Pop | # disk changes
|
8609 | 24991749 | Iustin Pop | for disk_op, disk_dict in self.op.disks: |
8610 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
8611 | 24991749 | Iustin Pop | # remove the last disk
|
8612 | 24991749 | Iustin Pop | device = instance.disks.pop() |
8613 | 24991749 | Iustin Pop | device_idx = len(instance.disks)
|
8614 | 24991749 | Iustin Pop | for node, disk in device.ComputeNodeTree(instance.primary_node): |
8615 | 24991749 | Iustin Pop | self.cfg.SetDiskID(disk, node)
|
8616 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
|
8617 | e1bc0878 | Iustin Pop | if msg:
|
8618 | e1bc0878 | Iustin Pop | self.LogWarning("Could not remove disk/%d on node %s: %s," |
8619 | e1bc0878 | Iustin Pop | " continuing anyway", device_idx, node, msg)
|
8620 | 24991749 | Iustin Pop | result.append(("disk/%d" % device_idx, "remove")) |
8621 | 24991749 | Iustin Pop | elif disk_op == constants.DDM_ADD:
|
8622 | 24991749 | Iustin Pop | # add a new disk
|
8623 | 24991749 | Iustin Pop | if instance.disk_template == constants.DT_FILE:
|
8624 | 24991749 | Iustin Pop | file_driver, file_path = instance.disks[0].logical_id
|
8625 | 24991749 | Iustin Pop | file_path = os.path.dirname(file_path) |
8626 | 24991749 | Iustin Pop | else:
|
8627 | 24991749 | Iustin Pop | file_driver = file_path = None
|
8628 | 24991749 | Iustin Pop | disk_idx_base = len(instance.disks)
|
8629 | 24991749 | Iustin Pop | new_disk = _GenerateDiskTemplate(self,
|
8630 | 24991749 | Iustin Pop | instance.disk_template, |
8631 | 32388e6d | Iustin Pop | instance.name, instance.primary_node, |
8632 | 24991749 | Iustin Pop | instance.secondary_nodes, |
8633 | 24991749 | Iustin Pop | [disk_dict], |
8634 | 24991749 | Iustin Pop | file_path, |
8635 | 24991749 | Iustin Pop | file_driver, |
8636 | 24991749 | Iustin Pop | disk_idx_base)[0]
|
8637 | 24991749 | Iustin Pop | instance.disks.append(new_disk) |
8638 | 24991749 | Iustin Pop | info = _GetInstanceInfoText(instance) |
8639 | 24991749 | Iustin Pop | |
8640 | 24991749 | Iustin Pop | logging.info("Creating volume %s for instance %s",
|
8641 | 24991749 | Iustin Pop | new_disk.iv_name, instance.name) |
8642 | 24991749 | Iustin Pop | # Note: this needs to be kept in sync with _CreateDisks
|
8643 | 24991749 | Iustin Pop | #HARDCODE
|
8644 | 428958aa | Iustin Pop | for node in instance.all_nodes: |
8645 | 428958aa | Iustin Pop | f_create = node == instance.primary_node |
8646 | 796cab27 | Iustin Pop | try:
|
8647 | 428958aa | Iustin Pop | _CreateBlockDev(self, node, instance, new_disk,
|
8648 | 428958aa | Iustin Pop | f_create, info, f_create) |
8649 | 1492cca7 | Iustin Pop | except errors.OpExecError, err:
|
8650 | 24991749 | Iustin Pop | self.LogWarning("Failed to create volume %s (%s) on" |
8651 | 428958aa | Iustin Pop | " node %s: %s",
|
8652 | 428958aa | Iustin Pop | new_disk.iv_name, new_disk, node, err) |
8653 | 24991749 | Iustin Pop | result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" % |
8654 | 24991749 | Iustin Pop | (new_disk.size, new_disk.mode))) |
8655 | 24991749 | Iustin Pop | else:
|
8656 | 24991749 | Iustin Pop | # change a given disk
|
8657 | 24991749 | Iustin Pop | instance.disks[disk_op].mode = disk_dict['mode']
|
8658 | 24991749 | Iustin Pop | result.append(("disk.mode/%d" % disk_op, disk_dict['mode'])) |
8659 | e29e9550 | Iustin Pop | |
8660 | e29e9550 | Iustin Pop | if self.op.disk_template: |
8661 | e29e9550 | Iustin Pop | r_shut = _ShutdownInstanceDisks(self, instance)
|
8662 | e29e9550 | Iustin Pop | if not r_shut: |
8663 | e29e9550 | Iustin Pop | raise errors.OpExecError("Cannot shutdow instance disks, unable to" |
8664 | e29e9550 | Iustin Pop | " proceed with disk template conversion")
|
8665 | e29e9550 | Iustin Pop | mode = (instance.disk_template, self.op.disk_template)
|
8666 | e29e9550 | Iustin Pop | try:
|
8667 | e29e9550 | Iustin Pop | self._DISK_CONVERSIONS[mode](self, feedback_fn) |
8668 | e29e9550 | Iustin Pop | except:
|
8669 | e29e9550 | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance.name)
|
8670 | e29e9550 | Iustin Pop | raise
|
8671 | e29e9550 | Iustin Pop | result.append(("disk_template", self.op.disk_template)) |
8672 | e29e9550 | Iustin Pop | |
8673 | 24991749 | Iustin Pop | # NIC changes
|
8674 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
8675 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
8676 | 24991749 | Iustin Pop | # remove the last nic
|
8677 | 24991749 | Iustin Pop | del instance.nics[-1] |
8678 | 24991749 | Iustin Pop | result.append(("nic.%d" % len(instance.nics), "remove")) |
8679 | 24991749 | Iustin Pop | elif nic_op == constants.DDM_ADD:
|
8680 | 5c44da6a | Guido Trotter | # mac and bridge should be set, by now
|
8681 | 5c44da6a | Guido Trotter | mac = nic_dict['mac']
|
8682 | cd098c41 | Guido Trotter | ip = nic_dict.get('ip', None) |
8683 | cd098c41 | Guido Trotter | nicparams = self.nic_pinst[constants.DDM_ADD]
|
8684 | cd098c41 | Guido Trotter | new_nic = objects.NIC(mac=mac, ip=ip, nicparams=nicparams) |
8685 | 24991749 | Iustin Pop | instance.nics.append(new_nic) |
8686 | 24991749 | Iustin Pop | result.append(("nic.%d" % (len(instance.nics) - 1), |
8687 | cd098c41 | Guido Trotter | "add:mac=%s,ip=%s,mode=%s,link=%s" %
|
8688 | cd098c41 | Guido Trotter | (new_nic.mac, new_nic.ip, |
8689 | cd098c41 | Guido Trotter | self.nic_pnew[constants.DDM_ADD][constants.NIC_MODE],
|
8690 | cd098c41 | Guido Trotter | self.nic_pnew[constants.DDM_ADD][constants.NIC_LINK]
|
8691 | cd098c41 | Guido Trotter | ))) |
8692 | 24991749 | Iustin Pop | else:
|
8693 | cd098c41 | Guido Trotter | for key in 'mac', 'ip': |
8694 | 24991749 | Iustin Pop | if key in nic_dict: |
8695 | 24991749 | Iustin Pop | setattr(instance.nics[nic_op], key, nic_dict[key])
|
8696 | beabf067 | Guido Trotter | if nic_op in self.nic_pinst: |
8697 | beabf067 | Guido Trotter | instance.nics[nic_op].nicparams = self.nic_pinst[nic_op]
|
8698 | cd098c41 | Guido Trotter | for key, val in nic_dict.iteritems(): |
8699 | cd098c41 | Guido Trotter | result.append(("nic.%s/%d" % (key, nic_op), val))
|
8700 | 24991749 | Iustin Pop | |
8701 | 24991749 | Iustin Pop | # hvparams changes
|
8702 | 74409b12 | Iustin Pop | if self.op.hvparams: |
8703 | 12649e35 | Guido Trotter | instance.hvparams = self.hv_inst
|
8704 | 74409b12 | Iustin Pop | for key, val in self.op.hvparams.iteritems(): |
8705 | 74409b12 | Iustin Pop | result.append(("hv/%s" % key, val))
|
8706 | 24991749 | Iustin Pop | |
8707 | 24991749 | Iustin Pop | # beparams changes
|
8708 | 338e51e8 | Iustin Pop | if self.op.beparams: |
8709 | 338e51e8 | Iustin Pop | instance.beparams = self.be_inst
|
8710 | 338e51e8 | Iustin Pop | for key, val in self.op.beparams.iteritems(): |
8711 | 338e51e8 | Iustin Pop | result.append(("be/%s" % key, val))
|
8712 | a8083063 | Iustin Pop | |
8713 | 96b39bcc | Iustin Pop | # OS change
|
8714 | 96b39bcc | Iustin Pop | if self.op.os_name: |
8715 | 96b39bcc | Iustin Pop | instance.os = self.op.os_name
|
8716 | 96b39bcc | Iustin Pop | |
8717 | a4eae71f | Michael Hanselmann | self.cfg.Update(instance, feedback_fn)
|
8718 | a8083063 | Iustin Pop | |
8719 | a8083063 | Iustin Pop | return result
|
8720 | a8083063 | Iustin Pop | |
8721 | e29e9550 | Iustin Pop | _DISK_CONVERSIONS = { |
8722 | e29e9550 | Iustin Pop | (constants.DT_PLAIN, constants.DT_DRBD8): _ConvertPlainToDrbd, |
8723 | 2f414c48 | Iustin Pop | (constants.DT_DRBD8, constants.DT_PLAIN): _ConvertDrbdToPlain, |
8724 | e29e9550 | Iustin Pop | } |
8725 | a8083063 | Iustin Pop | |
8726 | a8083063 | Iustin Pop | class LUQueryExports(NoHooksLU): |
8727 | a8083063 | Iustin Pop | """Query the exports list
|
8728 | a8083063 | Iustin Pop |
|
8729 | a8083063 | Iustin Pop | """
|
8730 | 895ecd9c | Guido Trotter | _OP_REQP = ['nodes']
|
8731 | 21a15682 | Guido Trotter | REQ_BGL = False
|
8732 | 21a15682 | Guido Trotter | |
8733 | 21a15682 | Guido Trotter | def ExpandNames(self): |
8734 | 21a15682 | Guido Trotter | self.needed_locks = {}
|
8735 | 21a15682 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
8736 | 21a15682 | Guido Trotter | if not self.op.nodes: |
8737 | e310b019 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
8738 | 21a15682 | Guido Trotter | else:
|
8739 | 21a15682 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = \
|
8740 | 21a15682 | Guido Trotter | _GetWantedNodes(self, self.op.nodes) |
8741 | a8083063 | Iustin Pop | |
8742 | a8083063 | Iustin Pop | def CheckPrereq(self): |
8743 | 21a15682 | Guido Trotter | """Check prerequisites.
|
8744 | a8083063 | Iustin Pop |
|
8745 | a8083063 | Iustin Pop | """
|
8746 | 21a15682 | Guido Trotter | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
8747 | a8083063 | Iustin Pop | |
8748 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
8749 | a8083063 | Iustin Pop | """Compute the list of all the exported system images.
|
8750 | a8083063 | Iustin Pop |
|
8751 | e4376078 | Iustin Pop | @rtype: dict
|
8752 | e4376078 | Iustin Pop | @return: a dictionary with the structure node->(export-list)
|
8753 | e4376078 | Iustin Pop | where export-list is a list of the instances exported on
|
8754 | e4376078 | Iustin Pop | that node.
|
8755 | a8083063 | Iustin Pop |
|
8756 | a8083063 | Iustin Pop | """
|
8757 | b04285f2 | Guido Trotter | rpcresult = self.rpc.call_export_list(self.nodes) |
8758 | b04285f2 | Guido Trotter | result = {} |
8759 | b04285f2 | Guido Trotter | for node in rpcresult: |
8760 | 4c4e4e1e | Iustin Pop | if rpcresult[node].fail_msg:
|
8761 | b04285f2 | Guido Trotter | result[node] = False
|
8762 | b04285f2 | Guido Trotter | else:
|
8763 | 1b7bfbb7 | Iustin Pop | result[node] = rpcresult[node].payload |
8764 | b04285f2 | Guido Trotter | |
8765 | b04285f2 | Guido Trotter | return result
|
8766 | a8083063 | Iustin Pop | |
8767 | a8083063 | Iustin Pop | |
8768 | a8083063 | Iustin Pop | class LUExportInstance(LogicalUnit): |
8769 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
8770 | a8083063 | Iustin Pop |
|
8771 | a8083063 | Iustin Pop | """
|
8772 | a8083063 | Iustin Pop | HPATH = "instance-export"
|
8773 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
8774 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "target_node", "shutdown"] |
8775 | 6657590e | Guido Trotter | REQ_BGL = False
|
8776 | 6657590e | Guido Trotter | |
8777 | 17c3f802 | Guido Trotter | def CheckArguments(self): |
8778 | 17c3f802 | Guido Trotter | """Check the arguments.
|
8779 | 17c3f802 | Guido Trotter |
|
8780 | 17c3f802 | Guido Trotter | """
|
8781 | 17c3f802 | Guido Trotter | self.shutdown_timeout = getattr(self.op, "shutdown_timeout", |
8782 | 17c3f802 | Guido Trotter | constants.DEFAULT_SHUTDOWN_TIMEOUT) |
8783 | 17c3f802 | Guido Trotter | |
8784 | 6657590e | Guido Trotter | def ExpandNames(self): |
8785 | 6657590e | Guido Trotter | self._ExpandAndLockInstance()
|
8786 | 6657590e | Guido Trotter | # FIXME: lock only instance primary and destination node
|
8787 | 6657590e | Guido Trotter | #
|
8788 | 6657590e | Guido Trotter | # Sad but true, for now we have do lock all nodes, as we don't know where
|
8789 | 6657590e | Guido Trotter | # the previous export might be, and and in this LU we search for it and
|
8790 | 6657590e | Guido Trotter | # remove it from its current node. In the future we could fix this by:
|
8791 | 6657590e | Guido Trotter | # - making a tasklet to search (share-lock all), then create the new one,
|
8792 | 6657590e | Guido Trotter | # then one to remove, after
|
8793 | 5bbd3f7f | Michael Hanselmann | # - removing the removal operation altogether
|
8794 | 6657590e | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
8795 | 6657590e | Guido Trotter | |
8796 | 6657590e | Guido Trotter | def DeclareLocks(self, level): |
8797 | 6657590e | Guido Trotter | """Last minute lock declaration."""
|
8798 | 6657590e | Guido Trotter | # All nodes are locked anyway, so nothing to do here.
|
8799 | a8083063 | Iustin Pop | |
8800 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
8801 | a8083063 | Iustin Pop | """Build hooks env.
|
8802 | a8083063 | Iustin Pop |
|
8803 | a8083063 | Iustin Pop | This will run on the master, primary node and target node.
|
8804 | a8083063 | Iustin Pop |
|
8805 | a8083063 | Iustin Pop | """
|
8806 | a8083063 | Iustin Pop | env = { |
8807 | a8083063 | Iustin Pop | "EXPORT_NODE": self.op.target_node, |
8808 | a8083063 | Iustin Pop | "EXPORT_DO_SHUTDOWN": self.op.shutdown, |
8809 | 17c3f802 | Guido Trotter | "SHUTDOWN_TIMEOUT": self.shutdown_timeout, |
8810 | a8083063 | Iustin Pop | } |
8811 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
8812 | d6a02168 | Michael Hanselmann | nl = [self.cfg.GetMasterNode(), self.instance.primary_node, |
8813 | a8083063 | Iustin Pop | self.op.target_node]
|
8814 | a8083063 | Iustin Pop | return env, nl, nl
|
8815 | a8083063 | Iustin Pop | |
8816 | a8083063 | Iustin Pop | def CheckPrereq(self): |
8817 | a8083063 | Iustin Pop | """Check prerequisites.
|
8818 | a8083063 | Iustin Pop |
|
8819 | 9ac99fda | Guido Trotter | This checks that the instance and node names are valid.
|
8820 | a8083063 | Iustin Pop |
|
8821 | a8083063 | Iustin Pop | """
|
8822 | 6657590e | Guido Trotter | instance_name = self.op.instance_name
|
8823 | a8083063 | Iustin Pop | self.instance = self.cfg.GetInstanceInfo(instance_name) |
8824 | 6657590e | Guido Trotter | assert self.instance is not None, \ |
8825 | 6657590e | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
8826 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
8827 | a8083063 | Iustin Pop | |
8828 | cf26a87a | Iustin Pop | self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node) |
8829 | cf26a87a | Iustin Pop | self.dst_node = self.cfg.GetNodeInfo(self.op.target_node) |
8830 | cf26a87a | Iustin Pop | assert self.dst_node is not None |
8831 | a8083063 | Iustin Pop | |
8832 | aeb83a2b | Iustin Pop | _CheckNodeOnline(self, self.dst_node.name) |
8833 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, self.dst_node.name) |
8834 | a8083063 | Iustin Pop | |
8835 | b6023d6c | Manuel Franceschini | # instance disk type verification
|
8836 | b6023d6c | Manuel Franceschini | for disk in self.instance.disks: |
8837 | b6023d6c | Manuel Franceschini | if disk.dev_type == constants.LD_FILE:
|
8838 | b6023d6c | Manuel Franceschini | raise errors.OpPrereqError("Export not supported for instances with" |
8839 | 5c983ee5 | Iustin Pop | " file-based disks", errors.ECODE_INVAL)
|
8840 | b6023d6c | Manuel Franceschini | |
8841 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
8842 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
8843 | a8083063 | Iustin Pop |
|
8844 | a8083063 | Iustin Pop | """
|
8845 | a8083063 | Iustin Pop | instance = self.instance
|
8846 | a8083063 | Iustin Pop | dst_node = self.dst_node
|
8847 | a8083063 | Iustin Pop | src_node = instance.primary_node |
8848 | 37972df0 | Michael Hanselmann | |
8849 | a8083063 | Iustin Pop | if self.op.shutdown: |
8850 | fb300fb7 | Guido Trotter | # shutdown the instance, but not the disks
|
8851 | 37972df0 | Michael Hanselmann | feedback_fn("Shutting down instance %s" % instance.name)
|
8852 | 17c3f802 | Guido Trotter | result = self.rpc.call_instance_shutdown(src_node, instance,
|
8853 | 17c3f802 | Guido Trotter | self.shutdown_timeout)
|
8854 | 4c4e4e1e | Iustin Pop | result.Raise("Could not shutdown instance %s on"
|
8855 | 4c4e4e1e | Iustin Pop | " node %s" % (instance.name, src_node))
|
8856 | a8083063 | Iustin Pop | |
8857 | a8083063 | Iustin Pop | vgname = self.cfg.GetVGName()
|
8858 | a8083063 | Iustin Pop | |
8859 | a8083063 | Iustin Pop | snap_disks = [] |
8860 | a8083063 | Iustin Pop | |
8861 | 998c712c | Iustin Pop | # set the disks ID correctly since call_instance_start needs the
|
8862 | 998c712c | Iustin Pop | # correct drbd minor to create the symlinks
|
8863 | 998c712c | Iustin Pop | for disk in instance.disks: |
8864 | 998c712c | Iustin Pop | self.cfg.SetDiskID(disk, src_node)
|
8865 | 998c712c | Iustin Pop | |
8866 | 3e53a60b | Michael Hanselmann | activate_disks = (not instance.admin_up)
|
8867 | 3e53a60b | Michael Hanselmann | |
8868 | 3e53a60b | Michael Hanselmann | if activate_disks:
|
8869 | 3e53a60b | Michael Hanselmann | # Activate the instance disks if we'exporting a stopped instance
|
8870 | 3e53a60b | Michael Hanselmann | feedback_fn("Activating disks for %s" % instance.name)
|
8871 | 3e53a60b | Michael Hanselmann | _StartInstanceDisks(self, instance, None) |
8872 | 3e53a60b | Michael Hanselmann | |
8873 | a8083063 | Iustin Pop | try:
|
8874 | 3e53a60b | Michael Hanselmann | # per-disk results
|
8875 | 3e53a60b | Michael Hanselmann | dresults = [] |
8876 | 3e53a60b | Michael Hanselmann | try:
|
8877 | 3e53a60b | Michael Hanselmann | for idx, disk in enumerate(instance.disks): |
8878 | 3e53a60b | Michael Hanselmann | feedback_fn("Creating a snapshot of disk/%s on node %s" %
|
8879 | 3e53a60b | Michael Hanselmann | (idx, src_node)) |
8880 | 3e53a60b | Michael Hanselmann | |
8881 | 3e53a60b | Michael Hanselmann | # result.payload will be a snapshot of an lvm leaf of the one we
|
8882 | 3e53a60b | Michael Hanselmann | # passed
|
8883 | 3e53a60b | Michael Hanselmann | result = self.rpc.call_blockdev_snapshot(src_node, disk)
|
8884 | 3e53a60b | Michael Hanselmann | msg = result.fail_msg |
8885 | 3e53a60b | Michael Hanselmann | if msg:
|
8886 | 3e53a60b | Michael Hanselmann | self.LogWarning("Could not snapshot disk/%s on node %s: %s", |
8887 | 3e53a60b | Michael Hanselmann | idx, src_node, msg) |
8888 | 3e53a60b | Michael Hanselmann | snap_disks.append(False)
|
8889 | 3e53a60b | Michael Hanselmann | else:
|
8890 | 3e53a60b | Michael Hanselmann | disk_id = (vgname, result.payload) |
8891 | 3e53a60b | Michael Hanselmann | new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size, |
8892 | 3e53a60b | Michael Hanselmann | logical_id=disk_id, physical_id=disk_id, |
8893 | 3e53a60b | Michael Hanselmann | iv_name=disk.iv_name) |
8894 | 3e53a60b | Michael Hanselmann | snap_disks.append(new_dev) |
8895 | 37972df0 | Michael Hanselmann | |
8896 | 3e53a60b | Michael Hanselmann | finally:
|
8897 | 3e53a60b | Michael Hanselmann | if self.op.shutdown and instance.admin_up: |
8898 | 3e53a60b | Michael Hanselmann | feedback_fn("Starting instance %s" % instance.name)
|
8899 | 3e53a60b | Michael Hanselmann | result = self.rpc.call_instance_start(src_node, instance, None, None) |
8900 | 3e53a60b | Michael Hanselmann | msg = result.fail_msg |
8901 | 3e53a60b | Michael Hanselmann | if msg:
|
8902 | 3e53a60b | Michael Hanselmann | _ShutdownInstanceDisks(self, instance)
|
8903 | 3e53a60b | Michael Hanselmann | raise errors.OpExecError("Could not start instance: %s" % msg) |
8904 | 3e53a60b | Michael Hanselmann | |
8905 | 3e53a60b | Michael Hanselmann | # TODO: check for size
|
8906 | 3e53a60b | Michael Hanselmann | |
8907 | 3e53a60b | Michael Hanselmann | cluster_name = self.cfg.GetClusterName()
|
8908 | 3e53a60b | Michael Hanselmann | for idx, dev in enumerate(snap_disks): |
8909 | 3e53a60b | Michael Hanselmann | feedback_fn("Exporting snapshot %s from %s to %s" %
|
8910 | 3e53a60b | Michael Hanselmann | (idx, src_node, dst_node.name)) |
8911 | 3e53a60b | Michael Hanselmann | if dev:
|
8912 | 4a0e011f | Iustin Pop | # FIXME: pass debug from opcode to backend
|
8913 | 3e53a60b | Michael Hanselmann | result = self.rpc.call_snapshot_export(src_node, dev, dst_node.name,
|
8914 | 4a0e011f | Iustin Pop | instance, cluster_name, |
8915 | dd713605 | Iustin Pop | idx, self.op.debug_level)
|
8916 | 3e53a60b | Michael Hanselmann | msg = result.fail_msg |
8917 | 3e53a60b | Michael Hanselmann | if msg:
|
8918 | 3e53a60b | Michael Hanselmann | self.LogWarning("Could not export disk/%s from node %s to" |
8919 | 3e53a60b | Michael Hanselmann | " node %s: %s", idx, src_node, dst_node.name, msg)
|
8920 | 3e53a60b | Michael Hanselmann | dresults.append(False)
|
8921 | 3e53a60b | Michael Hanselmann | else:
|
8922 | 3e53a60b | Michael Hanselmann | dresults.append(True)
|
8923 | 3e53a60b | Michael Hanselmann | msg = self.rpc.call_blockdev_remove(src_node, dev).fail_msg
|
8924 | 3e53a60b | Michael Hanselmann | if msg:
|
8925 | 3e53a60b | Michael Hanselmann | self.LogWarning("Could not remove snapshot for disk/%d from node" |
8926 | 3e53a60b | Michael Hanselmann | " %s: %s", idx, src_node, msg)
|
8927 | 19d7f90a | Guido Trotter | else:
|
8928 | 084f05a5 | Iustin Pop | dresults.append(False)
|
8929 | a8083063 | Iustin Pop | |
8930 | 3e53a60b | Michael Hanselmann | feedback_fn("Finalizing export on %s" % dst_node.name)
|
8931 | 3e53a60b | Michael Hanselmann | result = self.rpc.call_finalize_export(dst_node.name, instance,
|
8932 | 3e53a60b | Michael Hanselmann | snap_disks) |
8933 | 3e53a60b | Michael Hanselmann | fin_resu = True
|
8934 | 3e53a60b | Michael Hanselmann | msg = result.fail_msg |
8935 | 3e53a60b | Michael Hanselmann | if msg:
|
8936 | 3e53a60b | Michael Hanselmann | self.LogWarning("Could not finalize export for instance %s" |
8937 | 3e53a60b | Michael Hanselmann | " on node %s: %s", instance.name, dst_node.name, msg)
|
8938 | 3e53a60b | Michael Hanselmann | fin_resu = False
|
8939 | 3e53a60b | Michael Hanselmann | |
8940 | 3e53a60b | Michael Hanselmann | finally:
|
8941 | 3e53a60b | Michael Hanselmann | if activate_disks:
|
8942 | 3e53a60b | Michael Hanselmann | feedback_fn("Deactivating disks for %s" % instance.name)
|
8943 | 3e53a60b | Michael Hanselmann | _ShutdownInstanceDisks(self, instance)
|
8944 | a8083063 | Iustin Pop | |
8945 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
8946 | a8083063 | Iustin Pop | nodelist.remove(dst_node.name) |
8947 | a8083063 | Iustin Pop | |
8948 | a8083063 | Iustin Pop | # on one-node clusters nodelist will be empty after the removal
|
8949 | a8083063 | Iustin Pop | # if we proceed the backup would be removed because OpQueryExports
|
8950 | a8083063 | Iustin Pop | # substitutes an empty list with the full cluster node list.
|
8951 | 35fbcd11 | Iustin Pop | iname = instance.name |
8952 | a8083063 | Iustin Pop | if nodelist:
|
8953 | 37972df0 | Michael Hanselmann | feedback_fn("Removing old exports for instance %s" % iname)
|
8954 | 72737a7f | Iustin Pop | exportlist = self.rpc.call_export_list(nodelist)
|
8955 | a8083063 | Iustin Pop | for node in exportlist: |
8956 | 4c4e4e1e | Iustin Pop | if exportlist[node].fail_msg:
|
8957 | 781de953 | Iustin Pop | continue
|
8958 | 35fbcd11 | Iustin Pop | if iname in exportlist[node].payload: |
8959 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_export_remove(node, iname).fail_msg
|
8960 | 35fbcd11 | Iustin Pop | if msg:
|
8961 | 19d7f90a | Guido Trotter | self.LogWarning("Could not remove older export for instance %s" |
8962 | 35fbcd11 | Iustin Pop | " on node %s: %s", iname, node, msg)
|
8963 | 084f05a5 | Iustin Pop | return fin_resu, dresults
|
8964 | 5c947f38 | Iustin Pop | |
8965 | 5c947f38 | Iustin Pop | |
8966 | 9ac99fda | Guido Trotter | class LURemoveExport(NoHooksLU): |
8967 | 9ac99fda | Guido Trotter | """Remove exports related to the named instance.
|
8968 | 9ac99fda | Guido Trotter |
|
8969 | 9ac99fda | Guido Trotter | """
|
8970 | 9ac99fda | Guido Trotter | _OP_REQP = ["instance_name"]
|
8971 | 3656b3af | Guido Trotter | REQ_BGL = False
|
8972 | 3656b3af | Guido Trotter | |
8973 | 3656b3af | Guido Trotter | def ExpandNames(self): |
8974 | 3656b3af | Guido Trotter | self.needed_locks = {}
|
8975 | 3656b3af | Guido Trotter | # We need all nodes to be locked in order for RemoveExport to work, but we
|
8976 | 3656b3af | Guido Trotter | # don't need to lock the instance itself, as nothing will happen to it (and
|
8977 | 3656b3af | Guido Trotter | # we can remove exports also for a removed instance)
|
8978 | 3656b3af | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
8979 | 9ac99fda | Guido Trotter | |
8980 | 9ac99fda | Guido Trotter | def CheckPrereq(self): |
8981 | 9ac99fda | Guido Trotter | """Check prerequisites.
|
8982 | 9ac99fda | Guido Trotter | """
|
8983 | 9ac99fda | Guido Trotter | pass
|
8984 | 9ac99fda | Guido Trotter | |
8985 | 9ac99fda | Guido Trotter | def Exec(self, feedback_fn): |
8986 | 9ac99fda | Guido Trotter | """Remove any export.
|
8987 | 9ac99fda | Guido Trotter |
|
8988 | 9ac99fda | Guido Trotter | """
|
8989 | 9ac99fda | Guido Trotter | instance_name = self.cfg.ExpandInstanceName(self.op.instance_name) |
8990 | 9ac99fda | Guido Trotter | # If the instance was not found we'll try with the name that was passed in.
|
8991 | 9ac99fda | Guido Trotter | # This will only work if it was an FQDN, though.
|
8992 | 9ac99fda | Guido Trotter | fqdn_warn = False
|
8993 | 9ac99fda | Guido Trotter | if not instance_name: |
8994 | 9ac99fda | Guido Trotter | fqdn_warn = True
|
8995 | 9ac99fda | Guido Trotter | instance_name = self.op.instance_name
|
8996 | 9ac99fda | Guido Trotter | |
8997 | 1b7bfbb7 | Iustin Pop | locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
|
8998 | 1b7bfbb7 | Iustin Pop | exportlist = self.rpc.call_export_list(locked_nodes)
|
8999 | 9ac99fda | Guido Trotter | found = False
|
9000 | 9ac99fda | Guido Trotter | for node in exportlist: |
9001 | 4c4e4e1e | Iustin Pop | msg = exportlist[node].fail_msg |
9002 | 1b7bfbb7 | Iustin Pop | if msg:
|
9003 | 1b7bfbb7 | Iustin Pop | self.LogWarning("Failed to query node %s (continuing): %s", node, msg) |
9004 | 781de953 | Iustin Pop | continue
|
9005 | 1b7bfbb7 | Iustin Pop | if instance_name in exportlist[node].payload: |
9006 | 9ac99fda | Guido Trotter | found = True
|
9007 | 781de953 | Iustin Pop | result = self.rpc.call_export_remove(node, instance_name)
|
9008 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
9009 | 35fbcd11 | Iustin Pop | if msg:
|
9010 | 9a4f63d1 | Iustin Pop | logging.error("Could not remove export for instance %s"
|
9011 | 35fbcd11 | Iustin Pop | " on node %s: %s", instance_name, node, msg)
|
9012 | 9ac99fda | Guido Trotter | |
9013 | 9ac99fda | Guido Trotter | if fqdn_warn and not found: |
9014 | 9ac99fda | Guido Trotter | feedback_fn("Export not found. If trying to remove an export belonging"
|
9015 | 9ac99fda | Guido Trotter | " to a deleted instance please use its Fully Qualified"
|
9016 | 9ac99fda | Guido Trotter | " Domain Name.")
|
9017 | 9ac99fda | Guido Trotter | |
9018 | 9ac99fda | Guido Trotter | |
9019 | fe267188 | Iustin Pop | class TagsLU(NoHooksLU): # pylint: disable-msg=W0223 |
9020 | 5c947f38 | Iustin Pop | """Generic tags LU.
|
9021 | 5c947f38 | Iustin Pop |
|
9022 | 5c947f38 | Iustin Pop | This is an abstract class which is the parent of all the other tags LUs.
|
9023 | 5c947f38 | Iustin Pop |
|
9024 | 5c947f38 | Iustin Pop | """
|
9025 | 5c947f38 | Iustin Pop | |
9026 | 8646adce | Guido Trotter | def ExpandNames(self): |
9027 | 8646adce | Guido Trotter | self.needed_locks = {}
|
9028 | 8646adce | Guido Trotter | if self.op.kind == constants.TAG_NODE: |
9029 | cf26a87a | Iustin Pop | self.op.name = _ExpandNodeName(self.cfg, self.op.name) |
9030 | cf26a87a | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = self.op.name |
9031 | 5c947f38 | Iustin Pop | elif self.op.kind == constants.TAG_INSTANCE: |
9032 | cf26a87a | Iustin Pop | self.op.name = _ExpandInstanceName(self.cfg, self.op.name) |
9033 | cf26a87a | Iustin Pop | self.needed_locks[locking.LEVEL_INSTANCE] = self.op.name |
9034 | 8646adce | Guido Trotter | |
9035 | 8646adce | Guido Trotter | def CheckPrereq(self): |
9036 | 8646adce | Guido Trotter | """Check prerequisites.
|
9037 | 8646adce | Guido Trotter |
|
9038 | 8646adce | Guido Trotter | """
|
9039 | 8646adce | Guido Trotter | if self.op.kind == constants.TAG_CLUSTER: |
9040 | 8646adce | Guido Trotter | self.target = self.cfg.GetClusterInfo() |
9041 | 8646adce | Guido Trotter | elif self.op.kind == constants.TAG_NODE: |
9042 | 8646adce | Guido Trotter | self.target = self.cfg.GetNodeInfo(self.op.name) |
9043 | 8646adce | Guido Trotter | elif self.op.kind == constants.TAG_INSTANCE: |
9044 | 8646adce | Guido Trotter | self.target = self.cfg.GetInstanceInfo(self.op.name) |
9045 | 5c947f38 | Iustin Pop | else:
|
9046 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Wrong tag type requested (%s)" % |
9047 | 5c983ee5 | Iustin Pop | str(self.op.kind), errors.ECODE_INVAL) |
9048 | 5c947f38 | Iustin Pop | |
9049 | 5c947f38 | Iustin Pop | |
9050 | 5c947f38 | Iustin Pop | class LUGetTags(TagsLU): |
9051 | 5c947f38 | Iustin Pop | """Returns the tags of a given object.
|
9052 | 5c947f38 | Iustin Pop |
|
9053 | 5c947f38 | Iustin Pop | """
|
9054 | 5c947f38 | Iustin Pop | _OP_REQP = ["kind", "name"] |
9055 | 8646adce | Guido Trotter | REQ_BGL = False
|
9056 | 5c947f38 | Iustin Pop | |
9057 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
9058 | 5c947f38 | Iustin Pop | """Returns the tag list.
|
9059 | 5c947f38 | Iustin Pop |
|
9060 | 5c947f38 | Iustin Pop | """
|
9061 | 5d414478 | Oleksiy Mishchenko | return list(self.target.GetTags()) |
9062 | 5c947f38 | Iustin Pop | |
9063 | 5c947f38 | Iustin Pop | |
9064 | 73415719 | Iustin Pop | class LUSearchTags(NoHooksLU): |
9065 | 73415719 | Iustin Pop | """Searches the tags for a given pattern.
|
9066 | 73415719 | Iustin Pop |
|
9067 | 73415719 | Iustin Pop | """
|
9068 | 73415719 | Iustin Pop | _OP_REQP = ["pattern"]
|
9069 | 8646adce | Guido Trotter | REQ_BGL = False
|
9070 | 8646adce | Guido Trotter | |
9071 | 8646adce | Guido Trotter | def ExpandNames(self): |
9072 | 8646adce | Guido Trotter | self.needed_locks = {}
|
9073 | 73415719 | Iustin Pop | |
9074 | 73415719 | Iustin Pop | def CheckPrereq(self): |
9075 | 73415719 | Iustin Pop | """Check prerequisites.
|
9076 | 73415719 | Iustin Pop |
|
9077 | 73415719 | Iustin Pop | This checks the pattern passed for validity by compiling it.
|
9078 | 73415719 | Iustin Pop |
|
9079 | 73415719 | Iustin Pop | """
|
9080 | 73415719 | Iustin Pop | try:
|
9081 | 73415719 | Iustin Pop | self.re = re.compile(self.op.pattern) |
9082 | 73415719 | Iustin Pop | except re.error, err:
|
9083 | 73415719 | Iustin Pop | raise errors.OpPrereqError("Invalid search pattern '%s': %s" % |
9084 | 5c983ee5 | Iustin Pop | (self.op.pattern, err), errors.ECODE_INVAL)
|
9085 | 73415719 | Iustin Pop | |
9086 | 73415719 | Iustin Pop | def Exec(self, feedback_fn): |
9087 | 73415719 | Iustin Pop | """Returns the tag list.
|
9088 | 73415719 | Iustin Pop |
|
9089 | 73415719 | Iustin Pop | """
|
9090 | 73415719 | Iustin Pop | cfg = self.cfg
|
9091 | 73415719 | Iustin Pop | tgts = [("/cluster", cfg.GetClusterInfo())]
|
9092 | 8646adce | Guido Trotter | ilist = cfg.GetAllInstancesInfo().values() |
9093 | 73415719 | Iustin Pop | tgts.extend([("/instances/%s" % i.name, i) for i in ilist]) |
9094 | 8646adce | Guido Trotter | nlist = cfg.GetAllNodesInfo().values() |
9095 | 73415719 | Iustin Pop | tgts.extend([("/nodes/%s" % n.name, n) for n in nlist]) |
9096 | 73415719 | Iustin Pop | results = [] |
9097 | 73415719 | Iustin Pop | for path, target in tgts: |
9098 | 73415719 | Iustin Pop | for tag in target.GetTags(): |
9099 | 73415719 | Iustin Pop | if self.re.search(tag): |
9100 | 73415719 | Iustin Pop | results.append((path, tag)) |
9101 | 73415719 | Iustin Pop | return results
|
9102 | 73415719 | Iustin Pop | |
9103 | 73415719 | Iustin Pop | |
9104 | f27302fa | Iustin Pop | class LUAddTags(TagsLU): |
9105 | 5c947f38 | Iustin Pop | """Sets a tag on a given object.
|
9106 | 5c947f38 | Iustin Pop |
|
9107 | 5c947f38 | Iustin Pop | """
|
9108 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
9109 | 8646adce | Guido Trotter | REQ_BGL = False
|
9110 | 5c947f38 | Iustin Pop | |
9111 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
9112 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
9113 | 5c947f38 | Iustin Pop |
|
9114 | 5c947f38 | Iustin Pop | This checks the type and length of the tag name and value.
|
9115 | 5c947f38 | Iustin Pop |
|
9116 | 5c947f38 | Iustin Pop | """
|
9117 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
9118 | f27302fa | Iustin Pop | for tag in self.op.tags: |
9119 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
9120 | 5c947f38 | Iustin Pop | |
9121 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
9122 | 5c947f38 | Iustin Pop | """Sets the tag.
|
9123 | 5c947f38 | Iustin Pop |
|
9124 | 5c947f38 | Iustin Pop | """
|
9125 | 5c947f38 | Iustin Pop | try:
|
9126 | f27302fa | Iustin Pop | for tag in self.op.tags: |
9127 | f27302fa | Iustin Pop | self.target.AddTag(tag)
|
9128 | 5c947f38 | Iustin Pop | except errors.TagError, err:
|
9129 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Error while setting tag: %s" % str(err)) |
9130 | 159d4ec6 | Iustin Pop | self.cfg.Update(self.target, feedback_fn) |
9131 | 5c947f38 | Iustin Pop | |
9132 | 5c947f38 | Iustin Pop | |
9133 | f27302fa | Iustin Pop | class LUDelTags(TagsLU): |
9134 | f27302fa | Iustin Pop | """Delete a list of tags from a given object.
|
9135 | 5c947f38 | Iustin Pop |
|
9136 | 5c947f38 | Iustin Pop | """
|
9137 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
9138 | 8646adce | Guido Trotter | REQ_BGL = False
|
9139 | 5c947f38 | Iustin Pop | |
9140 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
9141 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
9142 | 5c947f38 | Iustin Pop |
|
9143 | 5c947f38 | Iustin Pop | This checks that we have the given tag.
|
9144 | 5c947f38 | Iustin Pop |
|
9145 | 5c947f38 | Iustin Pop | """
|
9146 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
9147 | f27302fa | Iustin Pop | for tag in self.op.tags: |
9148 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
9149 | f27302fa | Iustin Pop | del_tags = frozenset(self.op.tags) |
9150 | f27302fa | Iustin Pop | cur_tags = self.target.GetTags()
|
9151 | f27302fa | Iustin Pop | if not del_tags <= cur_tags: |
9152 | f27302fa | Iustin Pop | diff_tags = del_tags - cur_tags |
9153 | f27302fa | Iustin Pop | diff_names = ["'%s'" % tag for tag in diff_tags] |
9154 | f27302fa | Iustin Pop | diff_names.sort() |
9155 | f27302fa | Iustin Pop | raise errors.OpPrereqError("Tag(s) %s not found" % |
9156 | 5c983ee5 | Iustin Pop | (",".join(diff_names)), errors.ECODE_NOENT)
|
9157 | 5c947f38 | Iustin Pop | |
9158 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
9159 | 5c947f38 | Iustin Pop | """Remove the tag from the object.
|
9160 | 5c947f38 | Iustin Pop |
|
9161 | 5c947f38 | Iustin Pop | """
|
9162 | f27302fa | Iustin Pop | for tag in self.op.tags: |
9163 | f27302fa | Iustin Pop | self.target.RemoveTag(tag)
|
9164 | 159d4ec6 | Iustin Pop | self.cfg.Update(self.target, feedback_fn) |
9165 | 06009e27 | Iustin Pop | |
9166 | 0eed6e61 | Guido Trotter | |
9167 | 06009e27 | Iustin Pop | class LUTestDelay(NoHooksLU): |
9168 | 06009e27 | Iustin Pop | """Sleep for a specified amount of time.
|
9169 | 06009e27 | Iustin Pop |
|
9170 | 0b097284 | Guido Trotter | This LU sleeps on the master and/or nodes for a specified amount of
|
9171 | 06009e27 | Iustin Pop | time.
|
9172 | 06009e27 | Iustin Pop |
|
9173 | 06009e27 | Iustin Pop | """
|
9174 | 06009e27 | Iustin Pop | _OP_REQP = ["duration", "on_master", "on_nodes"] |
9175 | fbe9022f | Guido Trotter | REQ_BGL = False
|
9176 | 06009e27 | Iustin Pop | |
9177 | fbe9022f | Guido Trotter | def ExpandNames(self): |
9178 | fbe9022f | Guido Trotter | """Expand names and set required locks.
|
9179 | 06009e27 | Iustin Pop |
|
9180 | fbe9022f | Guido Trotter | This expands the node list, if any.
|
9181 | 06009e27 | Iustin Pop |
|
9182 | 06009e27 | Iustin Pop | """
|
9183 | fbe9022f | Guido Trotter | self.needed_locks = {}
|
9184 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
9185 | fbe9022f | Guido Trotter | # _GetWantedNodes can be used here, but is not always appropriate to use
|
9186 | fbe9022f | Guido Trotter | # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
|
9187 | fbe9022f | Guido Trotter | # more information.
|
9188 | 06009e27 | Iustin Pop | self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes) |
9189 | fbe9022f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes |
9190 | fbe9022f | Guido Trotter | |
9191 | fbe9022f | Guido Trotter | def CheckPrereq(self): |
9192 | fbe9022f | Guido Trotter | """Check prerequisites.
|
9193 | fbe9022f | Guido Trotter |
|
9194 | fbe9022f | Guido Trotter | """
|
9195 | 06009e27 | Iustin Pop | |
9196 | 06009e27 | Iustin Pop | def Exec(self, feedback_fn): |
9197 | 06009e27 | Iustin Pop | """Do the actual sleep.
|
9198 | 06009e27 | Iustin Pop |
|
9199 | 06009e27 | Iustin Pop | """
|
9200 | 06009e27 | Iustin Pop | if self.op.on_master: |
9201 | 06009e27 | Iustin Pop | if not utils.TestDelay(self.op.duration): |
9202 | 06009e27 | Iustin Pop | raise errors.OpExecError("Error during master delay test") |
9203 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
9204 | 72737a7f | Iustin Pop | result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration) |
9205 | 06009e27 | Iustin Pop | for node, node_result in result.items(): |
9206 | 4c4e4e1e | Iustin Pop | node_result.Raise("Failure during rpc call to node %s" % node)
|
9207 | d61df03e | Iustin Pop | |
9208 | d61df03e | Iustin Pop | |
9209 | d1c2dd75 | Iustin Pop | class IAllocator(object): |
9210 | d1c2dd75 | Iustin Pop | """IAllocator framework.
|
9211 | d61df03e | Iustin Pop |
|
9212 | d1c2dd75 | Iustin Pop | An IAllocator instance has three sets of attributes:
|
9213 | d6a02168 | Michael Hanselmann | - cfg that is needed to query the cluster
|
9214 | d1c2dd75 | Iustin Pop | - input data (all members of the _KEYS class attribute are required)
|
9215 | d1c2dd75 | Iustin Pop | - four buffer attributes (in|out_data|text), that represent the
|
9216 | d1c2dd75 | Iustin Pop | input (to the external script) in text and data structure format,
|
9217 | d1c2dd75 | Iustin Pop | and the output from it, again in two formats
|
9218 | d1c2dd75 | Iustin Pop | - the result variables from the script (success, info, nodes) for
|
9219 | d1c2dd75 | Iustin Pop | easy usage
|
9220 | d61df03e | Iustin Pop |
|
9221 | d61df03e | Iustin Pop | """
|
9222 | 7260cfbe | Iustin Pop | # pylint: disable-msg=R0902
|
9223 | 7260cfbe | Iustin Pop | # lots of instance attributes
|
9224 | 29859cb7 | Iustin Pop | _ALLO_KEYS = [ |
9225 | 8d3f86a0 | Iustin Pop | "name", "mem_size", "disks", "disk_template", |
9226 | 8cc7e742 | Guido Trotter | "os", "tags", "nics", "vcpus", "hypervisor", |
9227 | d1c2dd75 | Iustin Pop | ] |
9228 | 29859cb7 | Iustin Pop | _RELO_KEYS = [ |
9229 | 8d3f86a0 | Iustin Pop | "name", "relocate_from", |
9230 | 29859cb7 | Iustin Pop | ] |
9231 | 7f60a422 | Iustin Pop | _EVAC_KEYS = [ |
9232 | 7f60a422 | Iustin Pop | "evac_nodes",
|
9233 | 7f60a422 | Iustin Pop | ] |
9234 | d1c2dd75 | Iustin Pop | |
9235 | 8d3f86a0 | Iustin Pop | def __init__(self, cfg, rpc, mode, **kwargs): |
9236 | 923ddac0 | Michael Hanselmann | self.cfg = cfg
|
9237 | 923ddac0 | Michael Hanselmann | self.rpc = rpc
|
9238 | d1c2dd75 | Iustin Pop | # init buffer variables
|
9239 | d1c2dd75 | Iustin Pop | self.in_text = self.out_text = self.in_data = self.out_data = None |
9240 | d1c2dd75 | Iustin Pop | # init all input fields so that pylint is happy
|
9241 | 29859cb7 | Iustin Pop | self.mode = mode
|
9242 | d1c2dd75 | Iustin Pop | self.mem_size = self.disks = self.disk_template = None |
9243 | d1c2dd75 | Iustin Pop | self.os = self.tags = self.nics = self.vcpus = None |
9244 | a0add446 | Iustin Pop | self.hypervisor = None |
9245 | 29859cb7 | Iustin Pop | self.relocate_from = None |
9246 | 8d3f86a0 | Iustin Pop | self.name = None |
9247 | 7f60a422 | Iustin Pop | self.evac_nodes = None |
9248 | 27579978 | Iustin Pop | # computed fields
|
9249 | 27579978 | Iustin Pop | self.required_nodes = None |
9250 | d1c2dd75 | Iustin Pop | # init result fields
|
9251 | 680f0a89 | Iustin Pop | self.success = self.info = self.result = None |
9252 | 29859cb7 | Iustin Pop | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
9253 | 29859cb7 | Iustin Pop | keyset = self._ALLO_KEYS
|
9254 | 9757cc90 | Iustin Pop | fn = self._AddNewInstance
|
9255 | 29859cb7 | Iustin Pop | elif self.mode == constants.IALLOCATOR_MODE_RELOC: |
9256 | 29859cb7 | Iustin Pop | keyset = self._RELO_KEYS
|
9257 | 9757cc90 | Iustin Pop | fn = self._AddRelocateInstance
|
9258 | 7f60a422 | Iustin Pop | elif self.mode == constants.IALLOCATOR_MODE_MEVAC: |
9259 | 7f60a422 | Iustin Pop | keyset = self._EVAC_KEYS
|
9260 | 7f60a422 | Iustin Pop | fn = self._AddEvacuateNodes
|
9261 | 29859cb7 | Iustin Pop | else:
|
9262 | 29859cb7 | Iustin Pop | raise errors.ProgrammerError("Unknown mode '%s' passed to the" |
9263 | 29859cb7 | Iustin Pop | " IAllocator" % self.mode) |
9264 | d1c2dd75 | Iustin Pop | for key in kwargs: |
9265 | 29859cb7 | Iustin Pop | if key not in keyset: |
9266 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Invalid input parameter '%s' to" |
9267 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
9268 | d1c2dd75 | Iustin Pop | setattr(self, key, kwargs[key]) |
9269 | 7f60a422 | Iustin Pop | |
9270 | 29859cb7 | Iustin Pop | for key in keyset: |
9271 | d1c2dd75 | Iustin Pop | if key not in kwargs: |
9272 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Missing input parameter '%s' to" |
9273 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
9274 | 9757cc90 | Iustin Pop | self._BuildInputData(fn)
|
9275 | d1c2dd75 | Iustin Pop | |
9276 | d1c2dd75 | Iustin Pop | def _ComputeClusterData(self): |
9277 | d1c2dd75 | Iustin Pop | """Compute the generic allocator input data.
|
9278 | d1c2dd75 | Iustin Pop |
|
9279 | d1c2dd75 | Iustin Pop | This is the data that is independent of the actual operation.
|
9280 | d1c2dd75 | Iustin Pop |
|
9281 | d1c2dd75 | Iustin Pop | """
|
9282 | 923ddac0 | Michael Hanselmann | cfg = self.cfg
|
9283 | e69d05fd | Iustin Pop | cluster_info = cfg.GetClusterInfo() |
9284 | d1c2dd75 | Iustin Pop | # cluster data
|
9285 | d1c2dd75 | Iustin Pop | data = { |
9286 | 77031881 | Iustin Pop | "version": constants.IALLOCATOR_VERSION,
|
9287 | 72737a7f | Iustin Pop | "cluster_name": cfg.GetClusterName(),
|
9288 | e69d05fd | Iustin Pop | "cluster_tags": list(cluster_info.GetTags()), |
9289 | 1325da74 | Iustin Pop | "enabled_hypervisors": list(cluster_info.enabled_hypervisors), |
9290 | d1c2dd75 | Iustin Pop | # we don't have job IDs
|
9291 | d61df03e | Iustin Pop | } |
9292 | b57e9819 | Guido Trotter | iinfo = cfg.GetAllInstancesInfo().values() |
9293 | b57e9819 | Guido Trotter | i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo] |
9294 | 6286519f | Iustin Pop | |
9295 | d1c2dd75 | Iustin Pop | # node data
|
9296 | d1c2dd75 | Iustin Pop | node_results = {} |
9297 | d1c2dd75 | Iustin Pop | node_list = cfg.GetNodeList() |
9298 | 8cc7e742 | Guido Trotter | |
9299 | 8cc7e742 | Guido Trotter | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
9300 | a0add446 | Iustin Pop | hypervisor_name = self.hypervisor
|
9301 | 8cc7e742 | Guido Trotter | elif self.mode == constants.IALLOCATOR_MODE_RELOC: |
9302 | a0add446 | Iustin Pop | hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
|
9303 | 7f60a422 | Iustin Pop | elif self.mode == constants.IALLOCATOR_MODE_MEVAC: |
9304 | 7f60a422 | Iustin Pop | hypervisor_name = cluster_info.enabled_hypervisors[0]
|
9305 | 8cc7e742 | Guido Trotter | |
9306 | 923ddac0 | Michael Hanselmann | node_data = self.rpc.call_node_info(node_list, cfg.GetVGName(),
|
9307 | 923ddac0 | Michael Hanselmann | hypervisor_name) |
9308 | 923ddac0 | Michael Hanselmann | node_iinfo = \ |
9309 | 923ddac0 | Michael Hanselmann | self.rpc.call_all_instances_info(node_list,
|
9310 | 923ddac0 | Michael Hanselmann | cluster_info.enabled_hypervisors) |
9311 | 1325da74 | Iustin Pop | for nname, nresult in node_data.items(): |
9312 | 1325da74 | Iustin Pop | # first fill in static (config-based) values
|
9313 | d1c2dd75 | Iustin Pop | ninfo = cfg.GetNodeInfo(nname) |
9314 | d1c2dd75 | Iustin Pop | pnr = { |
9315 | d1c2dd75 | Iustin Pop | "tags": list(ninfo.GetTags()), |
9316 | d1c2dd75 | Iustin Pop | "primary_ip": ninfo.primary_ip,
|
9317 | d1c2dd75 | Iustin Pop | "secondary_ip": ninfo.secondary_ip,
|
9318 | fc0fe88c | Iustin Pop | "offline": ninfo.offline,
|
9319 | 0b2454b9 | Iustin Pop | "drained": ninfo.drained,
|
9320 | 1325da74 | Iustin Pop | "master_candidate": ninfo.master_candidate,
|
9321 | d1c2dd75 | Iustin Pop | } |
9322 | 1325da74 | Iustin Pop | |
9323 | 0d853843 | Iustin Pop | if not (ninfo.offline or ninfo.drained): |
9324 | 4c4e4e1e | Iustin Pop | nresult.Raise("Can't get data for node %s" % nname)
|
9325 | 4c4e4e1e | Iustin Pop | node_iinfo[nname].Raise("Can't get node instance info from node %s" %
|
9326 | 4c4e4e1e | Iustin Pop | nname) |
9327 | 070e998b | Iustin Pop | remote_info = nresult.payload |
9328 | b142ef15 | Iustin Pop | |
9329 | 1325da74 | Iustin Pop | for attr in ['memory_total', 'memory_free', 'memory_dom0', |
9330 | 1325da74 | Iustin Pop | 'vg_size', 'vg_free', 'cpu_total']: |
9331 | 1325da74 | Iustin Pop | if attr not in remote_info: |
9332 | 1325da74 | Iustin Pop | raise errors.OpExecError("Node '%s' didn't return attribute" |
9333 | 1325da74 | Iustin Pop | " '%s'" % (nname, attr))
|
9334 | 070e998b | Iustin Pop | if not isinstance(remote_info[attr], int): |
9335 | 1325da74 | Iustin Pop | raise errors.OpExecError("Node '%s' returned invalid value" |
9336 | 070e998b | Iustin Pop | " for '%s': %s" %
|
9337 | 070e998b | Iustin Pop | (nname, attr, remote_info[attr])) |
9338 | 1325da74 | Iustin Pop | # compute memory used by primary instances
|
9339 | 1325da74 | Iustin Pop | i_p_mem = i_p_up_mem = 0
|
9340 | 1325da74 | Iustin Pop | for iinfo, beinfo in i_list: |
9341 | 1325da74 | Iustin Pop | if iinfo.primary_node == nname:
|
9342 | 1325da74 | Iustin Pop | i_p_mem += beinfo[constants.BE_MEMORY] |
9343 | 2fa74ef4 | Iustin Pop | if iinfo.name not in node_iinfo[nname].payload: |
9344 | 1325da74 | Iustin Pop | i_used_mem = 0
|
9345 | 1325da74 | Iustin Pop | else:
|
9346 | 2fa74ef4 | Iustin Pop | i_used_mem = int(node_iinfo[nname].payload[iinfo.name]['memory']) |
9347 | 1325da74 | Iustin Pop | i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem |
9348 | 1325da74 | Iustin Pop | remote_info['memory_free'] -= max(0, i_mem_diff) |
9349 | 1325da74 | Iustin Pop | |
9350 | 1325da74 | Iustin Pop | if iinfo.admin_up:
|
9351 | 1325da74 | Iustin Pop | i_p_up_mem += beinfo[constants.BE_MEMORY] |
9352 | 1325da74 | Iustin Pop | |
9353 | 1325da74 | Iustin Pop | # compute memory used by instances
|
9354 | 1325da74 | Iustin Pop | pnr_dyn = { |
9355 | 1325da74 | Iustin Pop | "total_memory": remote_info['memory_total'], |
9356 | 1325da74 | Iustin Pop | "reserved_memory": remote_info['memory_dom0'], |
9357 | 1325da74 | Iustin Pop | "free_memory": remote_info['memory_free'], |
9358 | 1325da74 | Iustin Pop | "total_disk": remote_info['vg_size'], |
9359 | 1325da74 | Iustin Pop | "free_disk": remote_info['vg_free'], |
9360 | 1325da74 | Iustin Pop | "total_cpus": remote_info['cpu_total'], |
9361 | 1325da74 | Iustin Pop | "i_pri_memory": i_p_mem,
|
9362 | 1325da74 | Iustin Pop | "i_pri_up_memory": i_p_up_mem,
|
9363 | 1325da74 | Iustin Pop | } |
9364 | 1325da74 | Iustin Pop | pnr.update(pnr_dyn) |
9365 | 1325da74 | Iustin Pop | |
9366 | d1c2dd75 | Iustin Pop | node_results[nname] = pnr |
9367 | d1c2dd75 | Iustin Pop | data["nodes"] = node_results
|
9368 | d1c2dd75 | Iustin Pop | |
9369 | d1c2dd75 | Iustin Pop | # instance data
|
9370 | d1c2dd75 | Iustin Pop | instance_data = {} |
9371 | 338e51e8 | Iustin Pop | for iinfo, beinfo in i_list: |
9372 | a9fe7e8f | Guido Trotter | nic_data = [] |
9373 | a9fe7e8f | Guido Trotter | for nic in iinfo.nics: |
9374 | a9fe7e8f | Guido Trotter | filled_params = objects.FillDict( |
9375 | a9fe7e8f | Guido Trotter | cluster_info.nicparams[constants.PP_DEFAULT], |
9376 | a9fe7e8f | Guido Trotter | nic.nicparams) |
9377 | a9fe7e8f | Guido Trotter | nic_dict = {"mac": nic.mac,
|
9378 | a9fe7e8f | Guido Trotter | "ip": nic.ip,
|
9379 | a9fe7e8f | Guido Trotter | "mode": filled_params[constants.NIC_MODE],
|
9380 | a9fe7e8f | Guido Trotter | "link": filled_params[constants.NIC_LINK],
|
9381 | a9fe7e8f | Guido Trotter | } |
9382 | a9fe7e8f | Guido Trotter | if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
9383 | a9fe7e8f | Guido Trotter | nic_dict["bridge"] = filled_params[constants.NIC_LINK]
|
9384 | a9fe7e8f | Guido Trotter | nic_data.append(nic_dict) |
9385 | d1c2dd75 | Iustin Pop | pir = { |
9386 | d1c2dd75 | Iustin Pop | "tags": list(iinfo.GetTags()), |
9387 | 1325da74 | Iustin Pop | "admin_up": iinfo.admin_up,
|
9388 | 338e51e8 | Iustin Pop | "vcpus": beinfo[constants.BE_VCPUS],
|
9389 | 338e51e8 | Iustin Pop | "memory": beinfo[constants.BE_MEMORY],
|
9390 | d1c2dd75 | Iustin Pop | "os": iinfo.os,
|
9391 | 1325da74 | Iustin Pop | "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes), |
9392 | d1c2dd75 | Iustin Pop | "nics": nic_data,
|
9393 | 1325da74 | Iustin Pop | "disks": [{"size": dsk.size, "mode": dsk.mode} for dsk in iinfo.disks], |
9394 | d1c2dd75 | Iustin Pop | "disk_template": iinfo.disk_template,
|
9395 | e69d05fd | Iustin Pop | "hypervisor": iinfo.hypervisor,
|
9396 | d1c2dd75 | Iustin Pop | } |
9397 | 88ae4f85 | Iustin Pop | pir["disk_space_total"] = _ComputeDiskSize(iinfo.disk_template,
|
9398 | 88ae4f85 | Iustin Pop | pir["disks"])
|
9399 | 768f0a80 | Iustin Pop | instance_data[iinfo.name] = pir |
9400 | d61df03e | Iustin Pop | |
9401 | d1c2dd75 | Iustin Pop | data["instances"] = instance_data
|
9402 | d61df03e | Iustin Pop | |
9403 | d1c2dd75 | Iustin Pop | self.in_data = data
|
9404 | d61df03e | Iustin Pop | |
9405 | d1c2dd75 | Iustin Pop | def _AddNewInstance(self): |
9406 | d1c2dd75 | Iustin Pop | """Add new instance data to allocator structure.
|
9407 | d61df03e | Iustin Pop |
|
9408 | d1c2dd75 | Iustin Pop | This in combination with _AllocatorGetClusterData will create the
|
9409 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
9410 | d61df03e | Iustin Pop |
|
9411 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
9412 | d1c2dd75 | Iustin Pop | done.
|
9413 | d61df03e | Iustin Pop |
|
9414 | d1c2dd75 | Iustin Pop | """
|
9415 | dafc7302 | Guido Trotter | disk_space = _ComputeDiskSize(self.disk_template, self.disks) |
9416 | d1c2dd75 | Iustin Pop | |
9417 | 27579978 | Iustin Pop | if self.disk_template in constants.DTS_NET_MIRROR: |
9418 | 27579978 | Iustin Pop | self.required_nodes = 2 |
9419 | 27579978 | Iustin Pop | else:
|
9420 | 27579978 | Iustin Pop | self.required_nodes = 1 |
9421 | d1c2dd75 | Iustin Pop | request = { |
9422 | d1c2dd75 | Iustin Pop | "name": self.name, |
9423 | d1c2dd75 | Iustin Pop | "disk_template": self.disk_template, |
9424 | d1c2dd75 | Iustin Pop | "tags": self.tags, |
9425 | d1c2dd75 | Iustin Pop | "os": self.os, |
9426 | d1c2dd75 | Iustin Pop | "vcpus": self.vcpus, |
9427 | d1c2dd75 | Iustin Pop | "memory": self.mem_size, |
9428 | d1c2dd75 | Iustin Pop | "disks": self.disks, |
9429 | d1c2dd75 | Iustin Pop | "disk_space_total": disk_space,
|
9430 | d1c2dd75 | Iustin Pop | "nics": self.nics, |
9431 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
9432 | d1c2dd75 | Iustin Pop | } |
9433 | 9757cc90 | Iustin Pop | return request
|
9434 | 298fe380 | Iustin Pop | |
9435 | d1c2dd75 | Iustin Pop | def _AddRelocateInstance(self): |
9436 | d1c2dd75 | Iustin Pop | """Add relocate instance data to allocator structure.
|
9437 | 298fe380 | Iustin Pop |
|
9438 | d1c2dd75 | Iustin Pop | This in combination with _IAllocatorGetClusterData will create the
|
9439 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
9440 | d61df03e | Iustin Pop |
|
9441 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
9442 | d1c2dd75 | Iustin Pop | done.
|
9443 | d61df03e | Iustin Pop |
|
9444 | d1c2dd75 | Iustin Pop | """
|
9445 | 923ddac0 | Michael Hanselmann | instance = self.cfg.GetInstanceInfo(self.name) |
9446 | 27579978 | Iustin Pop | if instance is None: |
9447 | 27579978 | Iustin Pop | raise errors.ProgrammerError("Unknown instance '%s' passed to" |
9448 | 27579978 | Iustin Pop | " IAllocator" % self.name) |
9449 | 27579978 | Iustin Pop | |
9450 | 27579978 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
9451 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Can't relocate non-mirrored instances", |
9452 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
9453 | 27579978 | Iustin Pop | |
9454 | 2a139bb0 | Iustin Pop | if len(instance.secondary_nodes) != 1: |
9455 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Instance has not exactly one secondary node", |
9456 | 5c983ee5 | Iustin Pop | errors.ECODE_STATE) |
9457 | 2a139bb0 | Iustin Pop | |
9458 | 27579978 | Iustin Pop | self.required_nodes = 1 |
9459 | dafc7302 | Guido Trotter | disk_sizes = [{'size': disk.size} for disk in instance.disks] |
9460 | dafc7302 | Guido Trotter | disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes) |
9461 | 27579978 | Iustin Pop | |
9462 | d1c2dd75 | Iustin Pop | request = { |
9463 | d1c2dd75 | Iustin Pop | "name": self.name, |
9464 | 27579978 | Iustin Pop | "disk_space_total": disk_space,
|
9465 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
9466 | 29859cb7 | Iustin Pop | "relocate_from": self.relocate_from, |
9467 | d1c2dd75 | Iustin Pop | } |
9468 | 9757cc90 | Iustin Pop | return request
|
9469 | d61df03e | Iustin Pop | |
9470 | 7f60a422 | Iustin Pop | def _AddEvacuateNodes(self): |
9471 | 7f60a422 | Iustin Pop | """Add evacuate nodes data to allocator structure.
|
9472 | 7f60a422 | Iustin Pop |
|
9473 | 7f60a422 | Iustin Pop | """
|
9474 | 7f60a422 | Iustin Pop | request = { |
9475 | 7f60a422 | Iustin Pop | "evac_nodes": self.evac_nodes |
9476 | 7f60a422 | Iustin Pop | } |
9477 | 7f60a422 | Iustin Pop | return request
|
9478 | 7f60a422 | Iustin Pop | |
9479 | 9757cc90 | Iustin Pop | def _BuildInputData(self, fn): |
9480 | d1c2dd75 | Iustin Pop | """Build input data structures.
|
9481 | d61df03e | Iustin Pop |
|
9482 | d1c2dd75 | Iustin Pop | """
|
9483 | d1c2dd75 | Iustin Pop | self._ComputeClusterData()
|
9484 | d61df03e | Iustin Pop | |
9485 | 9757cc90 | Iustin Pop | request = fn() |
9486 | 9757cc90 | Iustin Pop | request["type"] = self.mode |
9487 | 9757cc90 | Iustin Pop | self.in_data["request"] = request |
9488 | d61df03e | Iustin Pop | |
9489 | d1c2dd75 | Iustin Pop | self.in_text = serializer.Dump(self.in_data) |
9490 | d61df03e | Iustin Pop | |
9491 | 72737a7f | Iustin Pop | def Run(self, name, validate=True, call_fn=None): |
9492 | d1c2dd75 | Iustin Pop | """Run an instance allocator and return the results.
|
9493 | 298fe380 | Iustin Pop |
|
9494 | d1c2dd75 | Iustin Pop | """
|
9495 | 72737a7f | Iustin Pop | if call_fn is None: |
9496 | 923ddac0 | Michael Hanselmann | call_fn = self.rpc.call_iallocator_runner
|
9497 | 298fe380 | Iustin Pop | |
9498 | 923ddac0 | Michael Hanselmann | result = call_fn(self.cfg.GetMasterNode(), name, self.in_text) |
9499 | 4c4e4e1e | Iustin Pop | result.Raise("Failure while running the iallocator script")
|
9500 | 8d528b7c | Iustin Pop | |
9501 | 87f5c298 | Iustin Pop | self.out_text = result.payload
|
9502 | d1c2dd75 | Iustin Pop | if validate:
|
9503 | d1c2dd75 | Iustin Pop | self._ValidateResult()
|
9504 | 298fe380 | Iustin Pop | |
9505 | d1c2dd75 | Iustin Pop | def _ValidateResult(self): |
9506 | d1c2dd75 | Iustin Pop | """Process the allocator results.
|
9507 | 538475ca | Iustin Pop |
|
9508 | d1c2dd75 | Iustin Pop | This will process and if successful save the result in
|
9509 | d1c2dd75 | Iustin Pop | self.out_data and the other parameters.
|
9510 | 538475ca | Iustin Pop |
|
9511 | d1c2dd75 | Iustin Pop | """
|
9512 | d1c2dd75 | Iustin Pop | try:
|
9513 | d1c2dd75 | Iustin Pop | rdict = serializer.Load(self.out_text)
|
9514 | d1c2dd75 | Iustin Pop | except Exception, err: |
9515 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: %s" % str(err)) |
9516 | d1c2dd75 | Iustin Pop | |
9517 | d1c2dd75 | Iustin Pop | if not isinstance(rdict, dict): |
9518 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: not a dict") |
9519 | 538475ca | Iustin Pop | |
9520 | 680f0a89 | Iustin Pop | # TODO: remove backwards compatiblity in later versions
|
9521 | 680f0a89 | Iustin Pop | if "nodes" in rdict and "result" not in rdict: |
9522 | 680f0a89 | Iustin Pop | rdict["result"] = rdict["nodes"] |
9523 | 680f0a89 | Iustin Pop | del rdict["nodes"] |
9524 | 680f0a89 | Iustin Pop | |
9525 | 680f0a89 | Iustin Pop | for key in "success", "info", "result": |
9526 | d1c2dd75 | Iustin Pop | if key not in rdict: |
9527 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results:" |
9528 | d1c2dd75 | Iustin Pop | " missing key '%s'" % key)
|
9529 | d1c2dd75 | Iustin Pop | setattr(self, key, rdict[key]) |
9530 | 538475ca | Iustin Pop | |
9531 | 680f0a89 | Iustin Pop | if not isinstance(rdict["result"], list): |
9532 | 680f0a89 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: 'result' key" |
9533 | d1c2dd75 | Iustin Pop | " is not a list")
|
9534 | d1c2dd75 | Iustin Pop | self.out_data = rdict
|
9535 | 538475ca | Iustin Pop | |
9536 | 538475ca | Iustin Pop | |
9537 | d61df03e | Iustin Pop | class LUTestAllocator(NoHooksLU): |
9538 | d61df03e | Iustin Pop | """Run allocator tests.
|
9539 | d61df03e | Iustin Pop |
|
9540 | d61df03e | Iustin Pop | This LU runs the allocator tests
|
9541 | d61df03e | Iustin Pop |
|
9542 | d61df03e | Iustin Pop | """
|
9543 | d61df03e | Iustin Pop | _OP_REQP = ["direction", "mode", "name"] |
9544 | d61df03e | Iustin Pop | |
9545 | d61df03e | Iustin Pop | def CheckPrereq(self): |
9546 | d61df03e | Iustin Pop | """Check prerequisites.
|
9547 | d61df03e | Iustin Pop |
|
9548 | d61df03e | Iustin Pop | This checks the opcode parameters depending on the director and mode test.
|
9549 | d61df03e | Iustin Pop |
|
9550 | d61df03e | Iustin Pop | """
|
9551 | 298fe380 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
9552 | d61df03e | Iustin Pop | for attr in ["name", "mem_size", "disks", "disk_template", |
9553 | d61df03e | Iustin Pop | "os", "tags", "nics", "vcpus"]: |
9554 | d61df03e | Iustin Pop | if not hasattr(self.op, attr): |
9555 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing attribute '%s' on opcode input" % |
9556 | 5c983ee5 | Iustin Pop | attr, errors.ECODE_INVAL) |
9557 | d61df03e | Iustin Pop | iname = self.cfg.ExpandInstanceName(self.op.name) |
9558 | d61df03e | Iustin Pop | if iname is not None: |
9559 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Instance '%s' already in the cluster" % |
9560 | 5c983ee5 | Iustin Pop | iname, errors.ECODE_EXISTS) |
9561 | d61df03e | Iustin Pop | if not isinstance(self.op.nics, list): |
9562 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'nics'", |
9563 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
9564 | d61df03e | Iustin Pop | for row in self.op.nics: |
9565 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
9566 | d61df03e | Iustin Pop | "mac" not in row or |
9567 | d61df03e | Iustin Pop | "ip" not in row or |
9568 | d61df03e | Iustin Pop | "bridge" not in row): |
9569 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the 'nics'" |
9570 | 5c983ee5 | Iustin Pop | " parameter", errors.ECODE_INVAL)
|
9571 | d61df03e | Iustin Pop | if not isinstance(self.op.disks, list): |
9572 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'disks'", |
9573 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
9574 | d61df03e | Iustin Pop | for row in self.op.disks: |
9575 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
9576 | d61df03e | Iustin Pop | "size" not in row or |
9577 | d61df03e | Iustin Pop | not isinstance(row["size"], int) or |
9578 | d61df03e | Iustin Pop | "mode" not in row or |
9579 | d61df03e | Iustin Pop | row["mode"] not in ['r', 'w']): |
9580 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the 'disks'" |
9581 | 5c983ee5 | Iustin Pop | " parameter", errors.ECODE_INVAL)
|
9582 | 8901997e | Iustin Pop | if not hasattr(self.op, "hypervisor") or self.op.hypervisor is None: |
9583 | 8cc7e742 | Guido Trotter | self.op.hypervisor = self.cfg.GetHypervisorType() |
9584 | 298fe380 | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_RELOC: |
9585 | d61df03e | Iustin Pop | if not hasattr(self.op, "name"): |
9586 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Missing attribute 'name' on opcode input", |
9587 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
9588 | cf26a87a | Iustin Pop | fname = _ExpandInstanceName(self.cfg, self.op.name) |
9589 | d61df03e | Iustin Pop | self.op.name = fname
|
9590 | 29859cb7 | Iustin Pop | self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes |
9591 | 823a72bc | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_MEVAC: |
9592 | 823a72bc | Iustin Pop | if not hasattr(self.op, "evac_nodes"): |
9593 | 823a72bc | Iustin Pop | raise errors.OpPrereqError("Missing attribute 'evac_nodes' on" |
9594 | 823a72bc | Iustin Pop | " opcode input", errors.ECODE_INVAL)
|
9595 | d61df03e | Iustin Pop | else:
|
9596 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid test allocator mode '%s'" % |
9597 | 5c983ee5 | Iustin Pop | self.op.mode, errors.ECODE_INVAL)
|
9598 | d61df03e | Iustin Pop | |
9599 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_OUT: |
9600 | 298fe380 | Iustin Pop | if not hasattr(self.op, "allocator") or self.op.allocator is None: |
9601 | 5c983ee5 | Iustin Pop | raise errors.OpPrereqError("Missing allocator name", |
9602 | 5c983ee5 | Iustin Pop | errors.ECODE_INVAL) |
9603 | 298fe380 | Iustin Pop | elif self.op.direction != constants.IALLOCATOR_DIR_IN: |
9604 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Wrong allocator test '%s'" % |
9605 | 5c983ee5 | Iustin Pop | self.op.direction, errors.ECODE_INVAL)
|
9606 | d61df03e | Iustin Pop | |
9607 | d61df03e | Iustin Pop | def Exec(self, feedback_fn): |
9608 | d61df03e | Iustin Pop | """Run the allocator test.
|
9609 | d61df03e | Iustin Pop |
|
9610 | d61df03e | Iustin Pop | """
|
9611 | 29859cb7 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
9612 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
9613 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
9614 | 29859cb7 | Iustin Pop | name=self.op.name,
|
9615 | 29859cb7 | Iustin Pop | mem_size=self.op.mem_size,
|
9616 | 29859cb7 | Iustin Pop | disks=self.op.disks,
|
9617 | 29859cb7 | Iustin Pop | disk_template=self.op.disk_template,
|
9618 | 29859cb7 | Iustin Pop | os=self.op.os,
|
9619 | 29859cb7 | Iustin Pop | tags=self.op.tags,
|
9620 | 29859cb7 | Iustin Pop | nics=self.op.nics,
|
9621 | 29859cb7 | Iustin Pop | vcpus=self.op.vcpus,
|
9622 | 8cc7e742 | Guido Trotter | hypervisor=self.op.hypervisor,
|
9623 | 29859cb7 | Iustin Pop | ) |
9624 | 823a72bc | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_RELOC: |
9625 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
9626 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
9627 | 29859cb7 | Iustin Pop | name=self.op.name,
|
9628 | 29859cb7 | Iustin Pop | relocate_from=list(self.relocate_from), |
9629 | 29859cb7 | Iustin Pop | ) |
9630 | 823a72bc | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_MEVAC: |
9631 | 823a72bc | Iustin Pop | ial = IAllocator(self.cfg, self.rpc, |
9632 | 823a72bc | Iustin Pop | mode=self.op.mode,
|
9633 | 823a72bc | Iustin Pop | evac_nodes=self.op.evac_nodes)
|
9634 | 823a72bc | Iustin Pop | else:
|
9635 | 823a72bc | Iustin Pop | raise errors.ProgrammerError("Uncatched mode %s in" |
9636 | 823a72bc | Iustin Pop | " LUTestAllocator.Exec", self.op.mode) |
9637 | d61df03e | Iustin Pop | |
9638 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_IN: |
9639 | d1c2dd75 | Iustin Pop | result = ial.in_text |
9640 | 298fe380 | Iustin Pop | else:
|
9641 | d1c2dd75 | Iustin Pop | ial.Run(self.op.allocator, validate=False) |
9642 | d1c2dd75 | Iustin Pop | result = ial.out_text |
9643 | 298fe380 | Iustin Pop | return result |