root / lib / cmdlib.py @ 19bed813
History | View | Annotate | Download (289.3 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 | a8083063 | Iustin Pop | # pylint: disable-msg=W0613,W0201
|
25 | a8083063 | Iustin Pop | |
26 | a8083063 | Iustin Pop | import os |
27 | a8083063 | Iustin Pop | import os.path |
28 | a8083063 | Iustin Pop | import time |
29 | a8083063 | Iustin Pop | import re |
30 | a8083063 | Iustin Pop | import platform |
31 | ffa1c0dc | Iustin Pop | import logging |
32 | 74409b12 | Iustin Pop | import copy |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | from ganeti import ssh |
35 | a8083063 | Iustin Pop | from ganeti import utils |
36 | a8083063 | Iustin Pop | from ganeti import errors |
37 | a8083063 | Iustin Pop | from ganeti import hypervisor |
38 | 6048c986 | Guido Trotter | from ganeti import locking |
39 | a8083063 | Iustin Pop | from ganeti import constants |
40 | a8083063 | Iustin Pop | from ganeti import objects |
41 | 8d14b30d | Iustin Pop | from ganeti import serializer |
42 | 112f18a5 | Iustin Pop | from ganeti import ssconf |
43 | d61df03e | Iustin Pop | |
44 | d61df03e | Iustin Pop | |
45 | a8083063 | Iustin Pop | class LogicalUnit(object): |
46 | 396e1b78 | Michael Hanselmann | """Logical Unit base class.
|
47 | a8083063 | Iustin Pop |
|
48 | a8083063 | Iustin Pop | Subclasses must follow these rules:
|
49 | d465bdc8 | Guido Trotter | - implement ExpandNames
|
50 | 6fd35c4d | Michael Hanselmann | - implement CheckPrereq (except when tasklets are used)
|
51 | 6fd35c4d | Michael Hanselmann | - implement Exec (except when tasklets are used)
|
52 | a8083063 | Iustin Pop | - implement BuildHooksEnv
|
53 | a8083063 | Iustin Pop | - redefine HPATH and HTYPE
|
54 | 05f86716 | Guido Trotter | - optionally redefine their run requirements:
|
55 | 7e55040e | Guido Trotter | REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
|
56 | 05f86716 | Guido Trotter |
|
57 | 05f86716 | Guido Trotter | Note that all commands require root permissions.
|
58 | a8083063 | Iustin Pop |
|
59 | 20777413 | Iustin Pop | @ivar dry_run_result: the value (if any) that will be returned to the caller
|
60 | 20777413 | Iustin Pop | in dry-run mode (signalled by opcode dry_run parameter)
|
61 | 20777413 | Iustin Pop |
|
62 | a8083063 | Iustin Pop | """
|
63 | a8083063 | Iustin Pop | HPATH = None
|
64 | a8083063 | Iustin Pop | HTYPE = None
|
65 | a8083063 | Iustin Pop | _OP_REQP = [] |
66 | 7e55040e | Guido Trotter | REQ_BGL = True
|
67 | a8083063 | Iustin Pop | |
68 | 72737a7f | Iustin Pop | def __init__(self, processor, op, context, rpc): |
69 | a8083063 | Iustin Pop | """Constructor for LogicalUnit.
|
70 | a8083063 | Iustin Pop |
|
71 | 5bbd3f7f | Michael Hanselmann | This needs to be overridden in derived classes in order to check op
|
72 | a8083063 | Iustin Pop | validity.
|
73 | a8083063 | Iustin Pop |
|
74 | a8083063 | Iustin Pop | """
|
75 | 5bfac263 | Iustin Pop | self.proc = processor
|
76 | a8083063 | Iustin Pop | self.op = op
|
77 | 77b657a3 | Guido Trotter | self.cfg = context.cfg
|
78 | 77b657a3 | Guido Trotter | self.context = context
|
79 | 72737a7f | Iustin Pop | self.rpc = rpc
|
80 | ca2a79e1 | Guido Trotter | # Dicts used to declare locking needs to mcpu
|
81 | d465bdc8 | Guido Trotter | self.needed_locks = None |
82 | 6683bba2 | Guido Trotter | self.acquired_locks = {}
|
83 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 0) |
84 | ca2a79e1 | Guido Trotter | self.add_locks = {}
|
85 | ca2a79e1 | Guido Trotter | self.remove_locks = {}
|
86 | c4a2fee1 | Guido Trotter | # Used to force good behavior when calling helper functions
|
87 | c4a2fee1 | Guido Trotter | self.recalculate_locks = {}
|
88 | c92b310a | Michael Hanselmann | self.__ssh = None |
89 | 86d9d3bb | Iustin Pop | # logging
|
90 | 86d9d3bb | Iustin Pop | self.LogWarning = processor.LogWarning
|
91 | 86d9d3bb | Iustin Pop | self.LogInfo = processor.LogInfo
|
92 | 2bb5c911 | Michael Hanselmann | self.LogStep = processor.LogStep
|
93 | 20777413 | Iustin Pop | # support for dry-run
|
94 | 20777413 | Iustin Pop | self.dry_run_result = None |
95 | c92b310a | Michael Hanselmann | |
96 | 6fd35c4d | Michael Hanselmann | # Tasklets
|
97 | 3a012b41 | Michael Hanselmann | self.tasklets = None |
98 | 6fd35c4d | Michael Hanselmann | |
99 | a8083063 | Iustin Pop | for attr_name in self._OP_REQP: |
100 | a8083063 | Iustin Pop | attr_val = getattr(op, attr_name, None) |
101 | a8083063 | Iustin Pop | if attr_val is None: |
102 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Required parameter '%s' missing" % |
103 | 3ecf6786 | Iustin Pop | attr_name) |
104 | 6fd35c4d | Michael Hanselmann | |
105 | 4be4691d | Iustin Pop | self.CheckArguments()
|
106 | a8083063 | Iustin Pop | |
107 | c92b310a | Michael Hanselmann | def __GetSSH(self): |
108 | c92b310a | Michael Hanselmann | """Returns the SshRunner object
|
109 | c92b310a | Michael Hanselmann |
|
110 | c92b310a | Michael Hanselmann | """
|
111 | c92b310a | Michael Hanselmann | if not self.__ssh: |
112 | 6b0469d2 | Iustin Pop | self.__ssh = ssh.SshRunner(self.cfg.GetClusterName()) |
113 | c92b310a | Michael Hanselmann | return self.__ssh |
114 | c92b310a | Michael Hanselmann | |
115 | c92b310a | Michael Hanselmann | ssh = property(fget=__GetSSH)
|
116 | c92b310a | Michael Hanselmann | |
117 | 4be4691d | Iustin Pop | def CheckArguments(self): |
118 | 4be4691d | Iustin Pop | """Check syntactic validity for the opcode arguments.
|
119 | 4be4691d | Iustin Pop |
|
120 | 4be4691d | Iustin Pop | This method is for doing a simple syntactic check and ensure
|
121 | 4be4691d | Iustin Pop | validity of opcode parameters, without any cluster-related
|
122 | 4be4691d | Iustin Pop | checks. While the same can be accomplished in ExpandNames and/or
|
123 | 4be4691d | Iustin Pop | CheckPrereq, doing these separate is better because:
|
124 | 4be4691d | Iustin Pop |
|
125 | 4be4691d | Iustin Pop | - ExpandNames is left as as purely a lock-related function
|
126 | 5bbd3f7f | Michael Hanselmann | - CheckPrereq is run after we have acquired locks (and possible
|
127 | 4be4691d | Iustin Pop | waited for them)
|
128 | 4be4691d | Iustin Pop |
|
129 | 4be4691d | Iustin Pop | The function is allowed to change the self.op attribute so that
|
130 | 4be4691d | Iustin Pop | later methods can no longer worry about missing parameters.
|
131 | 4be4691d | Iustin Pop |
|
132 | 4be4691d | Iustin Pop | """
|
133 | 4be4691d | Iustin Pop | pass
|
134 | 4be4691d | Iustin Pop | |
135 | d465bdc8 | Guido Trotter | def ExpandNames(self): |
136 | d465bdc8 | Guido Trotter | """Expand names for this LU.
|
137 | d465bdc8 | Guido Trotter |
|
138 | d465bdc8 | Guido Trotter | This method is called before starting to execute the opcode, and it should
|
139 | d465bdc8 | Guido Trotter | update all the parameters of the opcode to their canonical form (e.g. a
|
140 | d465bdc8 | Guido Trotter | short node name must be fully expanded after this method has successfully
|
141 | d465bdc8 | Guido Trotter | completed). This way locking, hooks, logging, ecc. can work correctly.
|
142 | d465bdc8 | Guido Trotter |
|
143 | d465bdc8 | Guido Trotter | LUs which implement this method must also populate the self.needed_locks
|
144 | d465bdc8 | Guido Trotter | member, as a dict with lock levels as keys, and a list of needed lock names
|
145 | d465bdc8 | Guido Trotter | as values. Rules:
|
146 | e4376078 | Iustin Pop |
|
147 | e4376078 | Iustin Pop | - use an empty dict if you don't need any lock
|
148 | e4376078 | Iustin Pop | - if you don't need any lock at a particular level omit that level
|
149 | e4376078 | Iustin Pop | - don't put anything for the BGL level
|
150 | e4376078 | Iustin Pop | - if you want all locks at a level use locking.ALL_SET as a value
|
151 | d465bdc8 | Guido Trotter |
|
152 | 3977a4c1 | Guido Trotter | If you need to share locks (rather than acquire them exclusively) at one
|
153 | 3977a4c1 | Guido Trotter | level you can modify self.share_locks, setting a true value (usually 1) for
|
154 | 3977a4c1 | Guido Trotter | that level. By default locks are not shared.
|
155 | 3977a4c1 | Guido Trotter |
|
156 | 6fd35c4d | Michael Hanselmann | This function can also define a list of tasklets, which then will be
|
157 | 6fd35c4d | Michael Hanselmann | executed in order instead of the usual LU-level CheckPrereq and Exec
|
158 | 6fd35c4d | Michael Hanselmann | functions, if those are not defined by the LU.
|
159 | 6fd35c4d | Michael Hanselmann |
|
160 | e4376078 | Iustin Pop | Examples::
|
161 | e4376078 | Iustin Pop |
|
162 | e4376078 | Iustin Pop | # Acquire all nodes and one instance
|
163 | e4376078 | Iustin Pop | self.needed_locks = {
|
164 | e4376078 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET,
|
165 | e4376078 | Iustin Pop | locking.LEVEL_INSTANCE: ['instance1.example.tld'],
|
166 | e4376078 | Iustin Pop | }
|
167 | e4376078 | Iustin Pop | # Acquire just two nodes
|
168 | e4376078 | Iustin Pop | self.needed_locks = {
|
169 | e4376078 | Iustin Pop | locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
|
170 | e4376078 | Iustin Pop | }
|
171 | e4376078 | Iustin Pop | # Acquire no locks
|
172 | e4376078 | Iustin Pop | self.needed_locks = {} # No, you can't leave it to the default value None
|
173 | d465bdc8 | Guido Trotter |
|
174 | d465bdc8 | Guido Trotter | """
|
175 | d465bdc8 | Guido Trotter | # The implementation of this method is mandatory only if the new LU is
|
176 | d465bdc8 | Guido Trotter | # concurrent, so that old LUs don't need to be changed all at the same
|
177 | d465bdc8 | Guido Trotter | # time.
|
178 | d465bdc8 | Guido Trotter | if self.REQ_BGL: |
179 | d465bdc8 | Guido Trotter | self.needed_locks = {} # Exclusive LUs don't need locks. |
180 | d465bdc8 | Guido Trotter | else:
|
181 | d465bdc8 | Guido Trotter | raise NotImplementedError |
182 | d465bdc8 | Guido Trotter | |
183 | fb8dcb62 | Guido Trotter | def DeclareLocks(self, level): |
184 | fb8dcb62 | Guido Trotter | """Declare LU locking needs for a level
|
185 | fb8dcb62 | Guido Trotter |
|
186 | fb8dcb62 | Guido Trotter | While most LUs can just declare their locking needs at ExpandNames time,
|
187 | fb8dcb62 | Guido Trotter | sometimes there's the need to calculate some locks after having acquired
|
188 | fb8dcb62 | Guido Trotter | the ones before. This function is called just before acquiring locks at a
|
189 | fb8dcb62 | Guido Trotter | particular level, but after acquiring the ones at lower levels, and permits
|
190 | fb8dcb62 | Guido Trotter | such calculations. It can be used to modify self.needed_locks, and by
|
191 | fb8dcb62 | Guido Trotter | default it does nothing.
|
192 | fb8dcb62 | Guido Trotter |
|
193 | fb8dcb62 | Guido Trotter | This function is only called if you have something already set in
|
194 | fb8dcb62 | Guido Trotter | self.needed_locks for the level.
|
195 | fb8dcb62 | Guido Trotter |
|
196 | fb8dcb62 | Guido Trotter | @param level: Locking level which is going to be locked
|
197 | fb8dcb62 | Guido Trotter | @type level: member of ganeti.locking.LEVELS
|
198 | fb8dcb62 | Guido Trotter |
|
199 | fb8dcb62 | Guido Trotter | """
|
200 | fb8dcb62 | Guido Trotter | |
201 | a8083063 | Iustin Pop | def CheckPrereq(self): |
202 | a8083063 | Iustin Pop | """Check prerequisites for this LU.
|
203 | a8083063 | Iustin Pop |
|
204 | a8083063 | Iustin Pop | This method should check that the prerequisites for the execution
|
205 | a8083063 | Iustin Pop | of this LU are fulfilled. It can do internode communication, but
|
206 | a8083063 | Iustin Pop | it should be idempotent - no cluster or system changes are
|
207 | a8083063 | Iustin Pop | allowed.
|
208 | a8083063 | Iustin Pop |
|
209 | a8083063 | Iustin Pop | The method should raise errors.OpPrereqError in case something is
|
210 | a8083063 | Iustin Pop | not fulfilled. Its return value is ignored.
|
211 | a8083063 | Iustin Pop |
|
212 | a8083063 | Iustin Pop | This method should also update all the parameters of the opcode to
|
213 | d465bdc8 | Guido Trotter | their canonical form if it hasn't been done by ExpandNames before.
|
214 | a8083063 | Iustin Pop |
|
215 | a8083063 | Iustin Pop | """
|
216 | 3a012b41 | Michael Hanselmann | if self.tasklets is not None: |
217 | b4a9eb66 | Michael Hanselmann | for (idx, tl) in enumerate(self.tasklets): |
218 | abae1b2b | Michael Hanselmann | logging.debug("Checking prerequisites for tasklet %s/%s",
|
219 | abae1b2b | Michael Hanselmann | idx + 1, len(self.tasklets)) |
220 | 6fd35c4d | Michael Hanselmann | tl.CheckPrereq() |
221 | 6fd35c4d | Michael Hanselmann | else:
|
222 | 6fd35c4d | Michael Hanselmann | raise NotImplementedError |
223 | a8083063 | Iustin Pop | |
224 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
225 | a8083063 | Iustin Pop | """Execute the LU.
|
226 | a8083063 | Iustin Pop |
|
227 | a8083063 | Iustin Pop | This method should implement the actual work. It should raise
|
228 | a8083063 | Iustin Pop | errors.OpExecError for failures that are somewhat dealt with in
|
229 | a8083063 | Iustin Pop | code, or expected.
|
230 | a8083063 | Iustin Pop |
|
231 | a8083063 | Iustin Pop | """
|
232 | 3a012b41 | Michael Hanselmann | if self.tasklets is not None: |
233 | b4a9eb66 | Michael Hanselmann | for (idx, tl) in enumerate(self.tasklets): |
234 | abae1b2b | Michael Hanselmann | logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets)) |
235 | 6fd35c4d | Michael Hanselmann | tl.Exec(feedback_fn) |
236 | 6fd35c4d | Michael Hanselmann | else:
|
237 | 6fd35c4d | Michael Hanselmann | raise NotImplementedError |
238 | a8083063 | Iustin Pop | |
239 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
240 | a8083063 | Iustin Pop | """Build hooks environment for this LU.
|
241 | a8083063 | Iustin Pop |
|
242 | a8083063 | Iustin Pop | This method should return a three-node tuple consisting of: a dict
|
243 | a8083063 | Iustin Pop | containing the environment that will be used for running the
|
244 | a8083063 | Iustin Pop | specific hook for this LU, a list of node names on which the hook
|
245 | a8083063 | Iustin Pop | should run before the execution, and a list of node names on which
|
246 | a8083063 | Iustin Pop | the hook should run after the execution.
|
247 | a8083063 | Iustin Pop |
|
248 | a8083063 | Iustin Pop | The keys of the dict must not have 'GANETI_' prefixed as this will
|
249 | a8083063 | Iustin Pop | be handled in the hooks runner. Also note additional keys will be
|
250 | a8083063 | Iustin Pop | added by the hooks runner. If the LU doesn't define any
|
251 | a8083063 | Iustin Pop | environment, an empty dict (and not None) should be returned.
|
252 | a8083063 | Iustin Pop |
|
253 | 8a3fe350 | Guido Trotter | No nodes should be returned as an empty list (and not None).
|
254 | a8083063 | Iustin Pop |
|
255 | a8083063 | Iustin Pop | Note that if the HPATH for a LU class is None, this function will
|
256 | a8083063 | Iustin Pop | not be called.
|
257 | a8083063 | Iustin Pop |
|
258 | a8083063 | Iustin Pop | """
|
259 | a8083063 | Iustin Pop | raise NotImplementedError |
260 | a8083063 | Iustin Pop | |
261 | 1fce5219 | Guido Trotter | def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result): |
262 | 1fce5219 | Guido Trotter | """Notify the LU about the results of its hooks.
|
263 | 1fce5219 | Guido Trotter |
|
264 | 1fce5219 | Guido Trotter | This method is called every time a hooks phase is executed, and notifies
|
265 | 1fce5219 | Guido Trotter | the Logical Unit about the hooks' result. The LU can then use it to alter
|
266 | 1fce5219 | Guido Trotter | its result based on the hooks. By default the method does nothing and the
|
267 | 1fce5219 | Guido Trotter | previous result is passed back unchanged but any LU can define it if it
|
268 | 1fce5219 | Guido Trotter | wants to use the local cluster hook-scripts somehow.
|
269 | 1fce5219 | Guido Trotter |
|
270 | e4376078 | Iustin Pop | @param phase: one of L{constants.HOOKS_PHASE_POST} or
|
271 | e4376078 | Iustin Pop | L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
|
272 | e4376078 | Iustin Pop | @param hook_results: the results of the multi-node hooks rpc call
|
273 | e4376078 | Iustin Pop | @param feedback_fn: function used send feedback back to the caller
|
274 | e4376078 | Iustin Pop | @param lu_result: the previous Exec result this LU had, or None
|
275 | e4376078 | Iustin Pop | in the PRE phase
|
276 | e4376078 | Iustin Pop | @return: the new Exec result, based on the previous result
|
277 | e4376078 | Iustin Pop | and hook results
|
278 | 1fce5219 | Guido Trotter |
|
279 | 1fce5219 | Guido Trotter | """
|
280 | 1fce5219 | Guido Trotter | return lu_result
|
281 | 1fce5219 | Guido Trotter | |
282 | 43905206 | Guido Trotter | def _ExpandAndLockInstance(self): |
283 | 43905206 | Guido Trotter | """Helper function to expand and lock an instance.
|
284 | 43905206 | Guido Trotter |
|
285 | 43905206 | Guido Trotter | Many LUs that work on an instance take its name in self.op.instance_name
|
286 | 43905206 | Guido Trotter | and need to expand it and then declare the expanded name for locking. This
|
287 | 43905206 | Guido Trotter | function does it, and then updates self.op.instance_name to the expanded
|
288 | 43905206 | Guido Trotter | name. It also initializes needed_locks as a dict, if this hasn't been done
|
289 | 43905206 | Guido Trotter | before.
|
290 | 43905206 | Guido Trotter |
|
291 | 43905206 | Guido Trotter | """
|
292 | 43905206 | Guido Trotter | if self.needed_locks is None: |
293 | 43905206 | Guido Trotter | self.needed_locks = {}
|
294 | 43905206 | Guido Trotter | else:
|
295 | 43905206 | Guido Trotter | assert locking.LEVEL_INSTANCE not in self.needed_locks, \ |
296 | 43905206 | Guido Trotter | "_ExpandAndLockInstance called with instance-level locks set"
|
297 | 43905206 | Guido Trotter | expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name) |
298 | 43905206 | Guido Trotter | if expanded_name is None: |
299 | 43905206 | Guido Trotter | raise errors.OpPrereqError("Instance '%s' not known" % |
300 | 43905206 | Guido Trotter | self.op.instance_name)
|
301 | 43905206 | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
|
302 | 43905206 | Guido Trotter | self.op.instance_name = expanded_name
|
303 | 43905206 | Guido Trotter | |
304 | a82ce292 | Guido Trotter | def _LockInstancesNodes(self, primary_only=False): |
305 | c4a2fee1 | Guido Trotter | """Helper function to declare instances' nodes for locking.
|
306 | c4a2fee1 | Guido Trotter |
|
307 | c4a2fee1 | Guido Trotter | This function should be called after locking one or more instances to lock
|
308 | c4a2fee1 | Guido Trotter | their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
|
309 | c4a2fee1 | Guido Trotter | with all primary or secondary nodes for instances already locked and
|
310 | c4a2fee1 | Guido Trotter | present in self.needed_locks[locking.LEVEL_INSTANCE].
|
311 | c4a2fee1 | Guido Trotter |
|
312 | c4a2fee1 | Guido Trotter | It should be called from DeclareLocks, and for safety only works if
|
313 | c4a2fee1 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] is set.
|
314 | c4a2fee1 | Guido Trotter |
|
315 | c4a2fee1 | Guido Trotter | In the future it may grow parameters to just lock some instance's nodes, or
|
316 | c4a2fee1 | Guido Trotter | to just lock primaries or secondary nodes, if needed.
|
317 | c4a2fee1 | Guido Trotter |
|
318 | e4376078 | Iustin Pop | If should be called in DeclareLocks in a way similar to::
|
319 | c4a2fee1 | Guido Trotter |
|
320 | e4376078 | Iustin Pop | if level == locking.LEVEL_NODE:
|
321 | e4376078 | Iustin Pop | self._LockInstancesNodes()
|
322 | c4a2fee1 | Guido Trotter |
|
323 | a82ce292 | Guido Trotter | @type primary_only: boolean
|
324 | a82ce292 | Guido Trotter | @param primary_only: only lock primary nodes of locked instances
|
325 | a82ce292 | Guido Trotter |
|
326 | c4a2fee1 | Guido Trotter | """
|
327 | c4a2fee1 | Guido Trotter | assert locking.LEVEL_NODE in self.recalculate_locks, \ |
328 | c4a2fee1 | Guido Trotter | "_LockInstancesNodes helper function called with no nodes to recalculate"
|
329 | c4a2fee1 | Guido Trotter | |
330 | c4a2fee1 | Guido Trotter | # TODO: check if we're really been called with the instance locks held
|
331 | c4a2fee1 | Guido Trotter | |
332 | c4a2fee1 | Guido Trotter | # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
|
333 | c4a2fee1 | Guido Trotter | # future we might want to have different behaviors depending on the value
|
334 | c4a2fee1 | Guido Trotter | # of self.recalculate_locks[locking.LEVEL_NODE]
|
335 | c4a2fee1 | Guido Trotter | wanted_nodes = [] |
336 | 6683bba2 | Guido Trotter | for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]: |
337 | c4a2fee1 | Guido Trotter | instance = self.context.cfg.GetInstanceInfo(instance_name)
|
338 | c4a2fee1 | Guido Trotter | wanted_nodes.append(instance.primary_node) |
339 | a82ce292 | Guido Trotter | if not primary_only: |
340 | a82ce292 | Guido Trotter | wanted_nodes.extend(instance.secondary_nodes) |
341 | 9513b6ab | Guido Trotter | |
342 | 9513b6ab | Guido Trotter | if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE: |
343 | 9513b6ab | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
|
344 | 9513b6ab | Guido Trotter | elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND: |
345 | 9513b6ab | Guido Trotter | self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
|
346 | c4a2fee1 | Guido Trotter | |
347 | c4a2fee1 | Guido Trotter | del self.recalculate_locks[locking.LEVEL_NODE] |
348 | c4a2fee1 | Guido Trotter | |
349 | a8083063 | Iustin Pop | |
350 | a8083063 | Iustin Pop | class NoHooksLU(LogicalUnit): |
351 | a8083063 | Iustin Pop | """Simple LU which runs no hooks.
|
352 | a8083063 | Iustin Pop |
|
353 | a8083063 | Iustin Pop | This LU is intended as a parent for other LogicalUnits which will
|
354 | a8083063 | Iustin Pop | run no hooks, in order to reduce duplicate code.
|
355 | a8083063 | Iustin Pop |
|
356 | a8083063 | Iustin Pop | """
|
357 | a8083063 | Iustin Pop | HPATH = None
|
358 | a8083063 | Iustin Pop | HTYPE = None
|
359 | a8083063 | Iustin Pop | |
360 | a8083063 | Iustin Pop | |
361 | 9a6800e1 | Michael Hanselmann | class Tasklet: |
362 | 9a6800e1 | Michael Hanselmann | """Tasklet base class.
|
363 | 9a6800e1 | Michael Hanselmann |
|
364 | 9a6800e1 | Michael Hanselmann | Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
|
365 | 9a6800e1 | Michael Hanselmann | they can mix legacy code with tasklets. Locking needs to be done in the LU,
|
366 | 9a6800e1 | Michael Hanselmann | tasklets know nothing about locks.
|
367 | 9a6800e1 | Michael Hanselmann |
|
368 | 9a6800e1 | Michael Hanselmann | Subclasses must follow these rules:
|
369 | 9a6800e1 | Michael Hanselmann | - Implement CheckPrereq
|
370 | 9a6800e1 | Michael Hanselmann | - Implement Exec
|
371 | 9a6800e1 | Michael Hanselmann |
|
372 | 9a6800e1 | Michael Hanselmann | """
|
373 | 464243a7 | Michael Hanselmann | def __init__(self, lu): |
374 | 464243a7 | Michael Hanselmann | self.lu = lu
|
375 | 464243a7 | Michael Hanselmann | |
376 | 464243a7 | Michael Hanselmann | # Shortcuts
|
377 | 464243a7 | Michael Hanselmann | self.cfg = lu.cfg
|
378 | 464243a7 | Michael Hanselmann | self.rpc = lu.rpc
|
379 | 464243a7 | Michael Hanselmann | |
380 | 9a6800e1 | Michael Hanselmann | def CheckPrereq(self): |
381 | 9a6800e1 | Michael Hanselmann | """Check prerequisites for this tasklets.
|
382 | 9a6800e1 | Michael Hanselmann |
|
383 | 9a6800e1 | Michael Hanselmann | This method should check whether the prerequisites for the execution of
|
384 | 9a6800e1 | Michael Hanselmann | this tasklet are fulfilled. It can do internode communication, but it
|
385 | 9a6800e1 | Michael Hanselmann | should be idempotent - no cluster or system changes are allowed.
|
386 | 9a6800e1 | Michael Hanselmann |
|
387 | 9a6800e1 | Michael Hanselmann | The method should raise errors.OpPrereqError in case something is not
|
388 | 9a6800e1 | Michael Hanselmann | fulfilled. Its return value is ignored.
|
389 | 9a6800e1 | Michael Hanselmann |
|
390 | 9a6800e1 | Michael Hanselmann | This method should also update all parameters to their canonical form if it
|
391 | 9a6800e1 | Michael Hanselmann | hasn't been done before.
|
392 | 9a6800e1 | Michael Hanselmann |
|
393 | 9a6800e1 | Michael Hanselmann | """
|
394 | 9a6800e1 | Michael Hanselmann | raise NotImplementedError |
395 | 9a6800e1 | Michael Hanselmann | |
396 | 9a6800e1 | Michael Hanselmann | def Exec(self, feedback_fn): |
397 | 9a6800e1 | Michael Hanselmann | """Execute the tasklet.
|
398 | 9a6800e1 | Michael Hanselmann |
|
399 | 9a6800e1 | Michael Hanselmann | This method should implement the actual work. It should raise
|
400 | 9a6800e1 | Michael Hanselmann | errors.OpExecError for failures that are somewhat dealt with in code, or
|
401 | 9a6800e1 | Michael Hanselmann | expected.
|
402 | 9a6800e1 | Michael Hanselmann |
|
403 | 9a6800e1 | Michael Hanselmann | """
|
404 | 9a6800e1 | Michael Hanselmann | raise NotImplementedError |
405 | 9a6800e1 | Michael Hanselmann | |
406 | 9a6800e1 | Michael Hanselmann | |
407 | dcb93971 | Michael Hanselmann | def _GetWantedNodes(lu, nodes): |
408 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded node names.
|
409 | 83120a01 | Michael Hanselmann |
|
410 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
411 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
412 | e4376078 | Iustin Pop | @type nodes: list
|
413 | e4376078 | Iustin Pop | @param nodes: list of node names or None for all nodes
|
414 | e4376078 | Iustin Pop | @rtype: list
|
415 | e4376078 | Iustin Pop | @return: the list of nodes, sorted
|
416 | e4376078 | Iustin Pop | @raise errors.OpProgrammerError: if the nodes parameter is wrong type
|
417 | 83120a01 | Michael Hanselmann |
|
418 | 83120a01 | Michael Hanselmann | """
|
419 | 3312b702 | Iustin Pop | if not isinstance(nodes, list): |
420 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'nodes'") |
421 | dcb93971 | Michael Hanselmann | |
422 | ea47808a | Guido Trotter | if not nodes: |
423 | ea47808a | Guido Trotter | raise errors.ProgrammerError("_GetWantedNodes should only be called with a" |
424 | ea47808a | Guido Trotter | " non-empty list of nodes whose name is to be expanded.")
|
425 | dcb93971 | Michael Hanselmann | |
426 | ea47808a | Guido Trotter | wanted = [] |
427 | ea47808a | Guido Trotter | for name in nodes: |
428 | ea47808a | Guido Trotter | node = lu.cfg.ExpandNodeName(name) |
429 | ea47808a | Guido Trotter | if node is None: |
430 | ea47808a | Guido Trotter | raise errors.OpPrereqError("No such node name '%s'" % name) |
431 | ea47808a | Guido Trotter | wanted.append(node) |
432 | dcb93971 | Michael Hanselmann | |
433 | a7ba5e53 | Iustin Pop | return utils.NiceSort(wanted)
|
434 | 3312b702 | Iustin Pop | |
435 | 3312b702 | Iustin Pop | |
436 | 3312b702 | Iustin Pop | def _GetWantedInstances(lu, instances): |
437 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded instance names.
|
438 | 3312b702 | Iustin Pop |
|
439 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
440 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
441 | e4376078 | Iustin Pop | @type instances: list
|
442 | e4376078 | Iustin Pop | @param instances: list of instance names or None for all instances
|
443 | e4376078 | Iustin Pop | @rtype: list
|
444 | e4376078 | Iustin Pop | @return: the list of instances, sorted
|
445 | e4376078 | Iustin Pop | @raise errors.OpPrereqError: if the instances parameter is wrong type
|
446 | e4376078 | Iustin Pop | @raise errors.OpPrereqError: if any of the passed instances is not found
|
447 | 3312b702 | Iustin Pop |
|
448 | 3312b702 | Iustin Pop | """
|
449 | 3312b702 | Iustin Pop | if not isinstance(instances, list): |
450 | 3312b702 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'") |
451 | 3312b702 | Iustin Pop | |
452 | 3312b702 | Iustin Pop | if instances:
|
453 | 3312b702 | Iustin Pop | wanted = [] |
454 | 3312b702 | Iustin Pop | |
455 | 3312b702 | Iustin Pop | for name in instances: |
456 | a7ba5e53 | Iustin Pop | instance = lu.cfg.ExpandInstanceName(name) |
457 | 3312b702 | Iustin Pop | if instance is None: |
458 | 3312b702 | Iustin Pop | raise errors.OpPrereqError("No such instance name '%s'" % name) |
459 | 3312b702 | Iustin Pop | wanted.append(instance) |
460 | 3312b702 | Iustin Pop | |
461 | 3312b702 | Iustin Pop | else:
|
462 | a7f5dc98 | Iustin Pop | wanted = utils.NiceSort(lu.cfg.GetInstanceList()) |
463 | a7f5dc98 | Iustin Pop | return wanted
|
464 | dcb93971 | Michael Hanselmann | |
465 | dcb93971 | Michael Hanselmann | |
466 | dcb93971 | Michael Hanselmann | def _CheckOutputFields(static, dynamic, selected): |
467 | 83120a01 | Michael Hanselmann | """Checks whether all selected fields are valid.
|
468 | 83120a01 | Michael Hanselmann |
|
469 | a2d2e1a7 | Iustin Pop | @type static: L{utils.FieldSet}
|
470 | 31bf511f | Iustin Pop | @param static: static fields set
|
471 | a2d2e1a7 | Iustin Pop | @type dynamic: L{utils.FieldSet}
|
472 | 31bf511f | Iustin Pop | @param dynamic: dynamic fields set
|
473 | 83120a01 | Michael Hanselmann |
|
474 | 83120a01 | Michael Hanselmann | """
|
475 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet() |
476 | 31bf511f | Iustin Pop | f.Extend(static) |
477 | 31bf511f | Iustin Pop | f.Extend(dynamic) |
478 | dcb93971 | Michael Hanselmann | |
479 | 31bf511f | Iustin Pop | delta = f.NonMatching(selected) |
480 | 31bf511f | Iustin Pop | if delta:
|
481 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Unknown output fields selected: %s" |
482 | 31bf511f | Iustin Pop | % ",".join(delta))
|
483 | dcb93971 | Michael Hanselmann | |
484 | dcb93971 | Michael Hanselmann | |
485 | a5961235 | Iustin Pop | def _CheckBooleanOpField(op, name): |
486 | a5961235 | Iustin Pop | """Validates boolean opcode parameters.
|
487 | a5961235 | Iustin Pop |
|
488 | a5961235 | Iustin Pop | This will ensure that an opcode parameter is either a boolean value,
|
489 | a5961235 | Iustin Pop | or None (but that it always exists).
|
490 | a5961235 | Iustin Pop |
|
491 | a5961235 | Iustin Pop | """
|
492 | a5961235 | Iustin Pop | val = getattr(op, name, None) |
493 | a5961235 | Iustin Pop | if not (val is None or isinstance(val, bool)): |
494 | a5961235 | Iustin Pop | raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" % |
495 | a5961235 | Iustin Pop | (name, str(val)))
|
496 | a5961235 | Iustin Pop | setattr(op, name, val)
|
497 | a5961235 | Iustin Pop | |
498 | a5961235 | Iustin Pop | |
499 | a5961235 | Iustin Pop | def _CheckNodeOnline(lu, node): |
500 | a5961235 | Iustin Pop | """Ensure that a given node is online.
|
501 | a5961235 | Iustin Pop |
|
502 | a5961235 | Iustin Pop | @param lu: the LU on behalf of which we make the check
|
503 | a5961235 | Iustin Pop | @param node: the node to check
|
504 | 733a2b6a | Iustin Pop | @raise errors.OpPrereqError: if the node is offline
|
505 | a5961235 | Iustin Pop |
|
506 | a5961235 | Iustin Pop | """
|
507 | a5961235 | Iustin Pop | if lu.cfg.GetNodeInfo(node).offline:
|
508 | a5961235 | Iustin Pop | raise errors.OpPrereqError("Can't use offline node %s" % node) |
509 | a5961235 | Iustin Pop | |
510 | a5961235 | Iustin Pop | |
511 | 733a2b6a | Iustin Pop | def _CheckNodeNotDrained(lu, node): |
512 | 733a2b6a | Iustin Pop | """Ensure that a given node is not drained.
|
513 | 733a2b6a | Iustin Pop |
|
514 | 733a2b6a | Iustin Pop | @param lu: the LU on behalf of which we make the check
|
515 | 733a2b6a | Iustin Pop | @param node: the node to check
|
516 | 733a2b6a | Iustin Pop | @raise errors.OpPrereqError: if the node is drained
|
517 | 733a2b6a | Iustin Pop |
|
518 | 733a2b6a | Iustin Pop | """
|
519 | 733a2b6a | Iustin Pop | if lu.cfg.GetNodeInfo(node).drained:
|
520 | 733a2b6a | Iustin Pop | raise errors.OpPrereqError("Can't use drained node %s" % node) |
521 | 733a2b6a | Iustin Pop | |
522 | 733a2b6a | Iustin Pop | |
523 | ecb215b5 | Michael Hanselmann | def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, |
524 | 67fc3042 | Iustin Pop | memory, vcpus, nics, disk_template, disks, |
525 | 7c4d6c7b | Michael Hanselmann | bep, hvp, hypervisor_name): |
526 | e4376078 | Iustin Pop | """Builds instance related env variables for hooks
|
527 | e4376078 | Iustin Pop |
|
528 | e4376078 | Iustin Pop | This builds the hook environment from individual variables.
|
529 | e4376078 | Iustin Pop |
|
530 | e4376078 | Iustin Pop | @type name: string
|
531 | e4376078 | Iustin Pop | @param name: the name of the instance
|
532 | e4376078 | Iustin Pop | @type primary_node: string
|
533 | e4376078 | Iustin Pop | @param primary_node: the name of the instance's primary node
|
534 | e4376078 | Iustin Pop | @type secondary_nodes: list
|
535 | e4376078 | Iustin Pop | @param secondary_nodes: list of secondary nodes as strings
|
536 | e4376078 | Iustin Pop | @type os_type: string
|
537 | e4376078 | Iustin Pop | @param os_type: the name of the instance's OS
|
538 | 0d68c45d | Iustin Pop | @type status: boolean
|
539 | 0d68c45d | Iustin Pop | @param status: the should_run status of the instance
|
540 | e4376078 | Iustin Pop | @type memory: string
|
541 | e4376078 | Iustin Pop | @param memory: the memory size of the instance
|
542 | e4376078 | Iustin Pop | @type vcpus: string
|
543 | e4376078 | Iustin Pop | @param vcpus: the count of VCPUs the instance has
|
544 | e4376078 | Iustin Pop | @type nics: list
|
545 | 5e3d3eb3 | Guido Trotter | @param nics: list of tuples (ip, mac, mode, link) representing
|
546 | 5e3d3eb3 | Guido Trotter | the NICs the instance has
|
547 | 2c2690c9 | Iustin Pop | @type disk_template: string
|
548 | 5bbd3f7f | Michael Hanselmann | @param disk_template: the disk template of the instance
|
549 | 2c2690c9 | Iustin Pop | @type disks: list
|
550 | 2c2690c9 | Iustin Pop | @param disks: the list of (size, mode) pairs
|
551 | 67fc3042 | Iustin Pop | @type bep: dict
|
552 | 67fc3042 | Iustin Pop | @param bep: the backend parameters for the instance
|
553 | 67fc3042 | Iustin Pop | @type hvp: dict
|
554 | 67fc3042 | Iustin Pop | @param hvp: the hypervisor parameters for the instance
|
555 | 7c4d6c7b | Michael Hanselmann | @type hypervisor_name: string
|
556 | 7c4d6c7b | Michael Hanselmann | @param hypervisor_name: the hypervisor for the instance
|
557 | e4376078 | Iustin Pop | @rtype: dict
|
558 | e4376078 | Iustin Pop | @return: the hook environment for this instance
|
559 | ecb215b5 | Michael Hanselmann |
|
560 | 396e1b78 | Michael Hanselmann | """
|
561 | 0d68c45d | Iustin Pop | if status:
|
562 | 0d68c45d | Iustin Pop | str_status = "up"
|
563 | 0d68c45d | Iustin Pop | else:
|
564 | 0d68c45d | Iustin Pop | str_status = "down"
|
565 | 396e1b78 | Michael Hanselmann | env = { |
566 | 0e137c28 | Iustin Pop | "OP_TARGET": name,
|
567 | 396e1b78 | Michael Hanselmann | "INSTANCE_NAME": name,
|
568 | 396e1b78 | Michael Hanselmann | "INSTANCE_PRIMARY": primary_node,
|
569 | 396e1b78 | Michael Hanselmann | "INSTANCE_SECONDARIES": " ".join(secondary_nodes), |
570 | ecb215b5 | Michael Hanselmann | "INSTANCE_OS_TYPE": os_type,
|
571 | 0d68c45d | Iustin Pop | "INSTANCE_STATUS": str_status,
|
572 | 396e1b78 | Michael Hanselmann | "INSTANCE_MEMORY": memory,
|
573 | 396e1b78 | Michael Hanselmann | "INSTANCE_VCPUS": vcpus,
|
574 | 2c2690c9 | Iustin Pop | "INSTANCE_DISK_TEMPLATE": disk_template,
|
575 | 7c4d6c7b | Michael Hanselmann | "INSTANCE_HYPERVISOR": hypervisor_name,
|
576 | 396e1b78 | Michael Hanselmann | } |
577 | 396e1b78 | Michael Hanselmann | |
578 | 396e1b78 | Michael Hanselmann | if nics:
|
579 | 396e1b78 | Michael Hanselmann | nic_count = len(nics)
|
580 | 62f0dd02 | Guido Trotter | for idx, (ip, mac, mode, link) in enumerate(nics): |
581 | 396e1b78 | Michael Hanselmann | if ip is None: |
582 | 396e1b78 | Michael Hanselmann | ip = ""
|
583 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC%d_IP" % idx] = ip
|
584 | 2c2690c9 | Iustin Pop | env["INSTANCE_NIC%d_MAC" % idx] = mac
|
585 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_MODE" % idx] = mode
|
586 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_LINK" % idx] = link
|
587 | 62f0dd02 | Guido Trotter | if mode == constants.NIC_MODE_BRIDGED:
|
588 | 62f0dd02 | Guido Trotter | env["INSTANCE_NIC%d_BRIDGE" % idx] = link
|
589 | 396e1b78 | Michael Hanselmann | else:
|
590 | 396e1b78 | Michael Hanselmann | nic_count = 0
|
591 | 396e1b78 | Michael Hanselmann | |
592 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC_COUNT"] = nic_count
|
593 | 396e1b78 | Michael Hanselmann | |
594 | 2c2690c9 | Iustin Pop | if disks:
|
595 | 2c2690c9 | Iustin Pop | disk_count = len(disks)
|
596 | 2c2690c9 | Iustin Pop | for idx, (size, mode) in enumerate(disks): |
597 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK%d_SIZE" % idx] = size
|
598 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK%d_MODE" % idx] = mode
|
599 | 2c2690c9 | Iustin Pop | else:
|
600 | 2c2690c9 | Iustin Pop | disk_count = 0
|
601 | 2c2690c9 | Iustin Pop | |
602 | 2c2690c9 | Iustin Pop | env["INSTANCE_DISK_COUNT"] = disk_count
|
603 | 2c2690c9 | Iustin Pop | |
604 | 67fc3042 | Iustin Pop | for source, kind in [(bep, "BE"), (hvp, "HV")]: |
605 | 67fc3042 | Iustin Pop | for key, value in source.items(): |
606 | 67fc3042 | Iustin Pop | env["INSTANCE_%s_%s" % (kind, key)] = value
|
607 | 67fc3042 | Iustin Pop | |
608 | 396e1b78 | Michael Hanselmann | return env
|
609 | 396e1b78 | Michael Hanselmann | |
610 | 96acbc09 | Michael Hanselmann | |
611 | f9b10246 | Guido Trotter | def _NICListToTuple(lu, nics): |
612 | 62f0dd02 | Guido Trotter | """Build a list of nic information tuples.
|
613 | 62f0dd02 | Guido Trotter |
|
614 | f9b10246 | Guido Trotter | This list is suitable to be passed to _BuildInstanceHookEnv or as a return
|
615 | f9b10246 | Guido Trotter | value in LUQueryInstanceData.
|
616 | 62f0dd02 | Guido Trotter |
|
617 | 62f0dd02 | Guido Trotter | @type lu: L{LogicalUnit}
|
618 | 62f0dd02 | Guido Trotter | @param lu: the logical unit on whose behalf we execute
|
619 | 62f0dd02 | Guido Trotter | @type nics: list of L{objects.NIC}
|
620 | 62f0dd02 | Guido Trotter | @param nics: list of nics to convert to hooks tuples
|
621 | 62f0dd02 | Guido Trotter |
|
622 | 62f0dd02 | Guido Trotter | """
|
623 | 62f0dd02 | Guido Trotter | hooks_nics = [] |
624 | 62f0dd02 | Guido Trotter | c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT] |
625 | 62f0dd02 | Guido Trotter | for nic in nics: |
626 | 62f0dd02 | Guido Trotter | ip = nic.ip |
627 | 62f0dd02 | Guido Trotter | mac = nic.mac |
628 | 62f0dd02 | Guido Trotter | filled_params = objects.FillDict(c_nicparams, nic.nicparams) |
629 | 62f0dd02 | Guido Trotter | mode = filled_params[constants.NIC_MODE] |
630 | 62f0dd02 | Guido Trotter | link = filled_params[constants.NIC_LINK] |
631 | 62f0dd02 | Guido Trotter | hooks_nics.append((ip, mac, mode, link)) |
632 | 62f0dd02 | Guido Trotter | return hooks_nics
|
633 | 396e1b78 | Michael Hanselmann | |
634 | 96acbc09 | Michael Hanselmann | |
635 | 338e51e8 | Iustin Pop | def _BuildInstanceHookEnvByObject(lu, instance, override=None): |
636 | ecb215b5 | Michael Hanselmann | """Builds instance related env variables for hooks from an object.
|
637 | ecb215b5 | Michael Hanselmann |
|
638 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
639 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
640 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
641 | e4376078 | Iustin Pop | @param instance: the instance for which we should build the
|
642 | e4376078 | Iustin Pop | environment
|
643 | e4376078 | Iustin Pop | @type override: dict
|
644 | e4376078 | Iustin Pop | @param override: dictionary with key/values that will override
|
645 | e4376078 | Iustin Pop | our values
|
646 | e4376078 | Iustin Pop | @rtype: dict
|
647 | e4376078 | Iustin Pop | @return: the hook environment dictionary
|
648 | e4376078 | Iustin Pop |
|
649 | ecb215b5 | Michael Hanselmann | """
|
650 | 67fc3042 | Iustin Pop | cluster = lu.cfg.GetClusterInfo() |
651 | 67fc3042 | Iustin Pop | bep = cluster.FillBE(instance) |
652 | 67fc3042 | Iustin Pop | hvp = cluster.FillHV(instance) |
653 | 396e1b78 | Michael Hanselmann | args = { |
654 | 396e1b78 | Michael Hanselmann | 'name': instance.name,
|
655 | 396e1b78 | Michael Hanselmann | 'primary_node': instance.primary_node,
|
656 | 396e1b78 | Michael Hanselmann | 'secondary_nodes': instance.secondary_nodes,
|
657 | ecb215b5 | Michael Hanselmann | 'os_type': instance.os,
|
658 | 0d68c45d | Iustin Pop | 'status': instance.admin_up,
|
659 | 338e51e8 | Iustin Pop | 'memory': bep[constants.BE_MEMORY],
|
660 | 338e51e8 | Iustin Pop | 'vcpus': bep[constants.BE_VCPUS],
|
661 | f9b10246 | Guido Trotter | 'nics': _NICListToTuple(lu, instance.nics),
|
662 | 2c2690c9 | Iustin Pop | 'disk_template': instance.disk_template,
|
663 | 2c2690c9 | Iustin Pop | 'disks': [(disk.size, disk.mode) for disk in instance.disks], |
664 | 67fc3042 | Iustin Pop | 'bep': bep,
|
665 | 67fc3042 | Iustin Pop | 'hvp': hvp,
|
666 | b0c63e2b | Iustin Pop | 'hypervisor_name': instance.hypervisor,
|
667 | 396e1b78 | Michael Hanselmann | } |
668 | 396e1b78 | Michael Hanselmann | if override:
|
669 | 396e1b78 | Michael Hanselmann | args.update(override) |
670 | 396e1b78 | Michael Hanselmann | return _BuildInstanceHookEnv(**args)
|
671 | 396e1b78 | Michael Hanselmann | |
672 | 396e1b78 | Michael Hanselmann | |
673 | ec0292f1 | Iustin Pop | def _AdjustCandidatePool(lu): |
674 | ec0292f1 | Iustin Pop | """Adjust the candidate pool after node operations.
|
675 | ec0292f1 | Iustin Pop |
|
676 | ec0292f1 | Iustin Pop | """
|
677 | ec0292f1 | Iustin Pop | mod_list = lu.cfg.MaintainCandidatePool() |
678 | ec0292f1 | Iustin Pop | if mod_list:
|
679 | ec0292f1 | Iustin Pop | lu.LogInfo("Promoted nodes to master candidate role: %s",
|
680 | ee513a66 | Iustin Pop | ", ".join(node.name for node in mod_list)) |
681 | ec0292f1 | Iustin Pop | for name in mod_list: |
682 | ec0292f1 | Iustin Pop | lu.context.ReaddNode(name) |
683 | ec0292f1 | Iustin Pop | mc_now, mc_max = lu.cfg.GetMasterCandidateStats() |
684 | ec0292f1 | Iustin Pop | if mc_now > mc_max:
|
685 | ec0292f1 | Iustin Pop | lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
|
686 | ec0292f1 | Iustin Pop | (mc_now, mc_max)) |
687 | ec0292f1 | Iustin Pop | |
688 | ec0292f1 | Iustin Pop | |
689 | b165e77e | Guido Trotter | def _CheckNicsBridgesExist(lu, target_nics, target_node, |
690 | b165e77e | Guido Trotter | profile=constants.PP_DEFAULT): |
691 | b165e77e | Guido Trotter | """Check that the brigdes needed by a list of nics exist.
|
692 | b165e77e | Guido Trotter |
|
693 | b165e77e | Guido Trotter | """
|
694 | b165e77e | Guido Trotter | c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile] |
695 | b165e77e | Guido Trotter | paramslist = [objects.FillDict(c_nicparams, nic.nicparams) |
696 | b165e77e | Guido Trotter | for nic in target_nics] |
697 | b165e77e | Guido Trotter | brlist = [params[constants.NIC_LINK] for params in paramslist |
698 | b165e77e | Guido Trotter | if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
|
699 | b165e77e | Guido Trotter | if brlist:
|
700 | b165e77e | Guido Trotter | result = lu.rpc.call_bridges_exist(target_node, brlist) |
701 | 4c4e4e1e | Iustin Pop | result.Raise("Error checking bridges on destination node '%s'" %
|
702 | 4c4e4e1e | Iustin Pop | target_node, prereq=True)
|
703 | b165e77e | Guido Trotter | |
704 | b165e77e | Guido Trotter | |
705 | b165e77e | Guido Trotter | def _CheckInstanceBridgesExist(lu, instance, node=None): |
706 | bf6929a2 | Alexander Schreiber | """Check that the brigdes needed by an instance exist.
|
707 | bf6929a2 | Alexander Schreiber |
|
708 | bf6929a2 | Alexander Schreiber | """
|
709 | b165e77e | Guido Trotter | if node is None: |
710 | 29921401 | Iustin Pop | node = instance.primary_node |
711 | b165e77e | Guido Trotter | _CheckNicsBridgesExist(lu, instance.nics, node) |
712 | bf6929a2 | Alexander Schreiber | |
713 | bf6929a2 | Alexander Schreiber | |
714 | 5ba9701d | Michael Hanselmann | def _GetNodeInstancesInner(cfg, fn): |
715 | 5ba9701d | Michael Hanselmann | return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)] |
716 | 5ba9701d | Michael Hanselmann | |
717 | 5ba9701d | Michael Hanselmann | |
718 | e9721add | Michael Hanselmann | def _GetNodeInstances(cfg, node_name): |
719 | e9721add | Michael Hanselmann | """Returns a list of all primary and secondary instances on a node.
|
720 | e9721add | Michael Hanselmann |
|
721 | e9721add | Michael Hanselmann | """
|
722 | e9721add | Michael Hanselmann | |
723 | e9721add | Michael Hanselmann | return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes) |
724 | e9721add | Michael Hanselmann | |
725 | e9721add | Michael Hanselmann | |
726 | 80cb875c | Michael Hanselmann | def _GetNodePrimaryInstances(cfg, node_name): |
727 | 80cb875c | Michael Hanselmann | """Returns primary instances on a node.
|
728 | 80cb875c | Michael Hanselmann |
|
729 | 80cb875c | Michael Hanselmann | """
|
730 | 5ba9701d | Michael Hanselmann | return _GetNodeInstancesInner(cfg,
|
731 | 5ba9701d | Michael Hanselmann | lambda inst: node_name == inst.primary_node)
|
732 | 80cb875c | Michael Hanselmann | |
733 | 80cb875c | Michael Hanselmann | |
734 | 692738fc | Michael Hanselmann | def _GetNodeSecondaryInstances(cfg, node_name): |
735 | 692738fc | Michael Hanselmann | """Returns secondary instances on a node.
|
736 | 692738fc | Michael Hanselmann |
|
737 | 692738fc | Michael Hanselmann | """
|
738 | 5ba9701d | Michael Hanselmann | return _GetNodeInstancesInner(cfg,
|
739 | 5ba9701d | Michael Hanselmann | lambda inst: node_name in inst.secondary_nodes) |
740 | 692738fc | Michael Hanselmann | |
741 | 692738fc | Michael Hanselmann | |
742 | efb8da02 | Michael Hanselmann | def _GetStorageTypeArgs(cfg, storage_type): |
743 | efb8da02 | Michael Hanselmann | """Returns the arguments for a storage type.
|
744 | efb8da02 | Michael Hanselmann |
|
745 | efb8da02 | Michael Hanselmann | """
|
746 | efb8da02 | Michael Hanselmann | # Special case for file storage
|
747 | efb8da02 | Michael Hanselmann | if storage_type == constants.ST_FILE:
|
748 | a4d138b7 | Michael Hanselmann | # storage.FileStorage wants a list of storage directories
|
749 | a4d138b7 | Michael Hanselmann | return [[cfg.GetFileStorageDir()]]
|
750 | efb8da02 | Michael Hanselmann | |
751 | efb8da02 | Michael Hanselmann | return []
|
752 | efb8da02 | Michael Hanselmann | |
753 | efb8da02 | Michael Hanselmann | |
754 | 2d9005d8 | Michael Hanselmann | def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq): |
755 | 2d9005d8 | Michael Hanselmann | faulty = [] |
756 | 2d9005d8 | Michael Hanselmann | |
757 | 2d9005d8 | Michael Hanselmann | for dev in instance.disks: |
758 | 2d9005d8 | Michael Hanselmann | cfg.SetDiskID(dev, node_name) |
759 | 2d9005d8 | Michael Hanselmann | |
760 | 2d9005d8 | Michael Hanselmann | result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks) |
761 | 2d9005d8 | Michael Hanselmann | result.Raise("Failed to get disk status from node %s" % node_name,
|
762 | 2d9005d8 | Michael Hanselmann | prereq=prereq) |
763 | 2d9005d8 | Michael Hanselmann | |
764 | 2d9005d8 | Michael Hanselmann | for idx, bdev_status in enumerate(result.payload): |
765 | 2d9005d8 | Michael Hanselmann | if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY: |
766 | 2d9005d8 | Michael Hanselmann | faulty.append(idx) |
767 | 2d9005d8 | Michael Hanselmann | |
768 | 2d9005d8 | Michael Hanselmann | return faulty
|
769 | 2d9005d8 | Michael Hanselmann | |
770 | 2d9005d8 | Michael Hanselmann | |
771 | b5f5fae9 | Luca Bigliardi | class LUPostInitCluster(LogicalUnit): |
772 | b5f5fae9 | Luca Bigliardi | """Logical unit for running hooks after cluster initialization.
|
773 | b5f5fae9 | Luca Bigliardi |
|
774 | b5f5fae9 | Luca Bigliardi | """
|
775 | b5f5fae9 | Luca Bigliardi | HPATH = "cluster-init"
|
776 | b5f5fae9 | Luca Bigliardi | HTYPE = constants.HTYPE_CLUSTER |
777 | b5f5fae9 | Luca Bigliardi | _OP_REQP = [] |
778 | b5f5fae9 | Luca Bigliardi | |
779 | b5f5fae9 | Luca Bigliardi | def BuildHooksEnv(self): |
780 | b5f5fae9 | Luca Bigliardi | """Build hooks env.
|
781 | b5f5fae9 | Luca Bigliardi |
|
782 | b5f5fae9 | Luca Bigliardi | """
|
783 | b5f5fae9 | Luca Bigliardi | env = {"OP_TARGET": self.cfg.GetClusterName()} |
784 | b5f5fae9 | Luca Bigliardi | mn = self.cfg.GetMasterNode()
|
785 | b5f5fae9 | Luca Bigliardi | return env, [], [mn]
|
786 | b5f5fae9 | Luca Bigliardi | |
787 | b5f5fae9 | Luca Bigliardi | def CheckPrereq(self): |
788 | b5f5fae9 | Luca Bigliardi | """No prerequisites to check.
|
789 | b5f5fae9 | Luca Bigliardi |
|
790 | b5f5fae9 | Luca Bigliardi | """
|
791 | b5f5fae9 | Luca Bigliardi | return True |
792 | b5f5fae9 | Luca Bigliardi | |
793 | b5f5fae9 | Luca Bigliardi | def Exec(self, feedback_fn): |
794 | b5f5fae9 | Luca Bigliardi | """Nothing to do.
|
795 | b5f5fae9 | Luca Bigliardi |
|
796 | b5f5fae9 | Luca Bigliardi | """
|
797 | b5f5fae9 | Luca Bigliardi | return True |
798 | b5f5fae9 | Luca Bigliardi | |
799 | b5f5fae9 | Luca Bigliardi | |
800 | b2c750a4 | Luca Bigliardi | class LUDestroyCluster(LogicalUnit): |
801 | a8083063 | Iustin Pop | """Logical unit for destroying the cluster.
|
802 | a8083063 | Iustin Pop |
|
803 | a8083063 | Iustin Pop | """
|
804 | b2c750a4 | Luca Bigliardi | HPATH = "cluster-destroy"
|
805 | b2c750a4 | Luca Bigliardi | HTYPE = constants.HTYPE_CLUSTER |
806 | a8083063 | Iustin Pop | _OP_REQP = [] |
807 | a8083063 | Iustin Pop | |
808 | b2c750a4 | Luca Bigliardi | def BuildHooksEnv(self): |
809 | b2c750a4 | Luca Bigliardi | """Build hooks env.
|
810 | b2c750a4 | Luca Bigliardi |
|
811 | b2c750a4 | Luca Bigliardi | """
|
812 | b2c750a4 | Luca Bigliardi | env = {"OP_TARGET": self.cfg.GetClusterName()} |
813 | b2c750a4 | Luca Bigliardi | return env, [], []
|
814 | b2c750a4 | Luca Bigliardi | |
815 | a8083063 | Iustin Pop | def CheckPrereq(self): |
816 | a8083063 | Iustin Pop | """Check prerequisites.
|
817 | a8083063 | Iustin Pop |
|
818 | a8083063 | Iustin Pop | This checks whether the cluster is empty.
|
819 | a8083063 | Iustin Pop |
|
820 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
821 | a8083063 | Iustin Pop |
|
822 | a8083063 | Iustin Pop | """
|
823 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
824 | a8083063 | Iustin Pop | |
825 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
826 | db915bd1 | Michael Hanselmann | if len(nodelist) != 1 or nodelist[0] != master: |
827 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d node(s) in" |
828 | 3ecf6786 | Iustin Pop | " this cluster." % (len(nodelist) - 1)) |
829 | db915bd1 | Michael Hanselmann | instancelist = self.cfg.GetInstanceList()
|
830 | db915bd1 | Michael Hanselmann | if instancelist:
|
831 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d instance(s) in" |
832 | 3ecf6786 | Iustin Pop | " this cluster." % len(instancelist)) |
833 | a8083063 | Iustin Pop | |
834 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
835 | a8083063 | Iustin Pop | """Destroys the cluster.
|
836 | a8083063 | Iustin Pop |
|
837 | a8083063 | Iustin Pop | """
|
838 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
839 | 3141ad3b | Luca Bigliardi | |
840 | 3141ad3b | Luca Bigliardi | # Run post hooks on master node before it's removed
|
841 | 3141ad3b | Luca Bigliardi | hm = self.proc.hmclass(self.rpc.call_hooks_runner, self) |
842 | 3141ad3b | Luca Bigliardi | try:
|
843 | 3141ad3b | Luca Bigliardi | hm.RunPhase(constants.HOOKS_PHASE_POST, [master]) |
844 | 3141ad3b | Luca Bigliardi | except:
|
845 | 3141ad3b | Luca Bigliardi | self.LogWarning("Errors occurred running hooks on %s" % master) |
846 | 3141ad3b | Luca Bigliardi | |
847 | 781de953 | Iustin Pop | result = self.rpc.call_node_stop_master(master, False) |
848 | 4c4e4e1e | Iustin Pop | result.Raise("Could not disable the master role")
|
849 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
850 | 70d9e3d8 | Iustin Pop | utils.CreateBackup(priv_key) |
851 | 70d9e3d8 | Iustin Pop | utils.CreateBackup(pub_key) |
852 | 140aa4a8 | Iustin Pop | return master
|
853 | a8083063 | Iustin Pop | |
854 | a8083063 | Iustin Pop | |
855 | d8fff41c | Guido Trotter | class LUVerifyCluster(LogicalUnit): |
856 | a8083063 | Iustin Pop | """Verifies the cluster status.
|
857 | a8083063 | Iustin Pop |
|
858 | a8083063 | Iustin Pop | """
|
859 | d8fff41c | Guido Trotter | HPATH = "cluster-verify"
|
860 | d8fff41c | Guido Trotter | HTYPE = constants.HTYPE_CLUSTER |
861 | a0c9776a | Iustin Pop | _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"] |
862 | d4b9d97f | Guido Trotter | REQ_BGL = False
|
863 | d4b9d97f | Guido Trotter | |
864 | 7c874ee1 | Iustin Pop | TCLUSTER = "cluster"
|
865 | 7c874ee1 | Iustin Pop | TNODE = "node"
|
866 | 7c874ee1 | Iustin Pop | TINSTANCE = "instance"
|
867 | 7c874ee1 | Iustin Pop | |
868 | 7c874ee1 | Iustin Pop | ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
|
869 | 7c874ee1 | Iustin Pop | EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
|
870 | 7c874ee1 | Iustin Pop | EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
|
871 | 7c874ee1 | Iustin Pop | EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
|
872 | 7c874ee1 | Iustin Pop | EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
|
873 | 7c874ee1 | Iustin Pop | EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
|
874 | 7c874ee1 | Iustin Pop | EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
|
875 | 7c874ee1 | Iustin Pop | ENODEDRBD = (TNODE, "ENODEDRBD")
|
876 | 7c874ee1 | Iustin Pop | ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
|
877 | 7c874ee1 | Iustin Pop | ENODEHOOKS = (TNODE, "ENODEHOOKS")
|
878 | 7c874ee1 | Iustin Pop | ENODEHV = (TNODE, "ENODEHV")
|
879 | 7c874ee1 | Iustin Pop | ENODELVM = (TNODE, "ENODELVM")
|
880 | 7c874ee1 | Iustin Pop | ENODEN1 = (TNODE, "ENODEN1")
|
881 | 7c874ee1 | Iustin Pop | ENODENET = (TNODE, "ENODENET")
|
882 | 7c874ee1 | Iustin Pop | ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
|
883 | 7c874ee1 | Iustin Pop | ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
|
884 | 7c874ee1 | Iustin Pop | ENODERPC = (TNODE, "ENODERPC")
|
885 | 7c874ee1 | Iustin Pop | ENODESSH = (TNODE, "ENODESSH")
|
886 | 7c874ee1 | Iustin Pop | ENODEVERSION = (TNODE, "ENODEVERSION")
|
887 | 7c874ee1 | Iustin Pop | |
888 | a0c9776a | Iustin Pop | ETYPE_FIELD = "code"
|
889 | a0c9776a | Iustin Pop | ETYPE_ERROR = "ERROR"
|
890 | a0c9776a | Iustin Pop | ETYPE_WARNING = "WARNING"
|
891 | a0c9776a | Iustin Pop | |
892 | d4b9d97f | Guido Trotter | def ExpandNames(self): |
893 | d4b9d97f | Guido Trotter | self.needed_locks = {
|
894 | d4b9d97f | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
895 | d4b9d97f | Guido Trotter | locking.LEVEL_INSTANCE: locking.ALL_SET, |
896 | d4b9d97f | Guido Trotter | } |
897 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
898 | a8083063 | Iustin Pop | |
899 | 7c874ee1 | Iustin Pop | def _Error(self, ecode, item, msg, *args, **kwargs): |
900 | 7c874ee1 | Iustin Pop | """Format an error message.
|
901 | 7c874ee1 | Iustin Pop |
|
902 | 7c874ee1 | Iustin Pop | Based on the opcode's error_codes parameter, either format a
|
903 | 7c874ee1 | Iustin Pop | parseable error code, or a simpler error string.
|
904 | 7c874ee1 | Iustin Pop |
|
905 | 7c874ee1 | Iustin Pop | This must be called only from Exec and functions called from Exec.
|
906 | 7c874ee1 | Iustin Pop |
|
907 | 7c874ee1 | Iustin Pop | """
|
908 | a0c9776a | Iustin Pop | ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) |
909 | 7c874ee1 | Iustin Pop | itype, etxt = ecode |
910 | 7c874ee1 | Iustin Pop | # first complete the msg
|
911 | 7c874ee1 | Iustin Pop | if args:
|
912 | 7c874ee1 | Iustin Pop | msg = msg % args |
913 | 7c874ee1 | Iustin Pop | # then format the whole message
|
914 | 7c874ee1 | Iustin Pop | if self.op.error_codes: |
915 | 7c874ee1 | Iustin Pop | msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
|
916 | 7c874ee1 | Iustin Pop | else:
|
917 | 7c874ee1 | Iustin Pop | if item:
|
918 | 7c874ee1 | Iustin Pop | item = " " + item
|
919 | 7c874ee1 | Iustin Pop | else:
|
920 | 7c874ee1 | Iustin Pop | item = ""
|
921 | 7c874ee1 | Iustin Pop | msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
|
922 | 7c874ee1 | Iustin Pop | # and finally report it via the feedback_fn
|
923 | 7c874ee1 | Iustin Pop | self._feedback_fn(" - %s" % msg) |
924 | 7c874ee1 | Iustin Pop | |
925 | a0c9776a | Iustin Pop | def _ErrorIf(self, cond, *args, **kwargs): |
926 | a0c9776a | Iustin Pop | """Log an error message if the passed condition is True.
|
927 | a0c9776a | Iustin Pop |
|
928 | a0c9776a | Iustin Pop | """
|
929 | a0c9776a | Iustin Pop | cond = bool(cond) or self.op.debug_simulate_errors |
930 | a0c9776a | Iustin Pop | if cond:
|
931 | a0c9776a | Iustin Pop | self._Error(*args, **kwargs)
|
932 | a0c9776a | Iustin Pop | # do not mark the operation as failed for WARN cases only
|
933 | a0c9776a | Iustin Pop | if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR: |
934 | a0c9776a | Iustin Pop | self.bad = self.bad or cond |
935 | a0c9776a | Iustin Pop | |
936 | 25361b9a | Iustin Pop | def _VerifyNode(self, nodeinfo, file_list, local_cksum, |
937 | 7c874ee1 | Iustin Pop | node_result, master_files, drbd_map, vg_name): |
938 | a8083063 | Iustin Pop | """Run multiple tests against a node.
|
939 | a8083063 | Iustin Pop |
|
940 | 112f18a5 | Iustin Pop | Test list:
|
941 | e4376078 | Iustin Pop |
|
942 | a8083063 | Iustin Pop | - compares ganeti version
|
943 | 5bbd3f7f | Michael Hanselmann | - checks vg existence and size > 20G
|
944 | a8083063 | Iustin Pop | - checks config file checksum
|
945 | a8083063 | Iustin Pop | - checks ssh to other nodes
|
946 | a8083063 | Iustin Pop |
|
947 | 112f18a5 | Iustin Pop | @type nodeinfo: L{objects.Node}
|
948 | 112f18a5 | Iustin Pop | @param nodeinfo: the node to check
|
949 | e4376078 | Iustin Pop | @param file_list: required list of files
|
950 | e4376078 | Iustin Pop | @param local_cksum: dictionary of local files and their checksums
|
951 | e4376078 | Iustin Pop | @param node_result: the results from the node
|
952 | 112f18a5 | Iustin Pop | @param master_files: list of files that only masters should have
|
953 | 6d2e83d5 | Iustin Pop | @param drbd_map: the useddrbd minors for this node, in
|
954 | 6d2e83d5 | Iustin Pop | form of minor: (instance, must_exist) which correspond to instances
|
955 | 6d2e83d5 | Iustin Pop | and their running status
|
956 | cc9e1230 | Guido Trotter | @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
|
957 | 098c0958 | Michael Hanselmann |
|
958 | a8083063 | Iustin Pop | """
|
959 | 112f18a5 | Iustin Pop | node = nodeinfo.name |
960 | a0c9776a | Iustin Pop | _ErrorIf = self._ErrorIf
|
961 | 25361b9a | Iustin Pop | |
962 | 25361b9a | Iustin Pop | # main result, node_result should be a non-empty dict
|
963 | a0c9776a | Iustin Pop | test = not node_result or not isinstance(node_result, dict) |
964 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODERPC, node,
|
965 | 7c874ee1 | Iustin Pop | "unable to verify node: no data returned")
|
966 | a0c9776a | Iustin Pop | if test:
|
967 | a0c9776a | Iustin Pop | return
|
968 | 25361b9a | Iustin Pop | |
969 | a8083063 | Iustin Pop | # compares ganeti version
|
970 | a8083063 | Iustin Pop | local_version = constants.PROTOCOL_VERSION |
971 | 25361b9a | Iustin Pop | remote_version = node_result.get('version', None) |
972 | a0c9776a | Iustin Pop | test = not (remote_version and |
973 | a0c9776a | Iustin Pop | isinstance(remote_version, (list, tuple)) and |
974 | a0c9776a | Iustin Pop | len(remote_version) == 2) |
975 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODERPC, node,
|
976 | a0c9776a | Iustin Pop | "connection to node returned invalid data")
|
977 | a0c9776a | Iustin Pop | if test:
|
978 | a0c9776a | Iustin Pop | return
|
979 | a0c9776a | Iustin Pop | |
980 | a0c9776a | Iustin Pop | test = local_version != remote_version[0]
|
981 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEVERSION, node,
|
982 | a0c9776a | Iustin Pop | "incompatible protocol versions: master %s,"
|
983 | a0c9776a | Iustin Pop | " node %s", local_version, remote_version[0]) |
984 | a0c9776a | Iustin Pop | if test:
|
985 | a0c9776a | Iustin Pop | return
|
986 | a8083063 | Iustin Pop | |
987 | e9ce0a64 | Iustin Pop | # node seems compatible, we can actually try to look into its results
|
988 | a8083063 | Iustin Pop | |
989 | e9ce0a64 | Iustin Pop | # full package version
|
990 | a0c9776a | Iustin Pop | self._ErrorIf(constants.RELEASE_VERSION != remote_version[1], |
991 | a0c9776a | Iustin Pop | self.ENODEVERSION, node,
|
992 | 7c874ee1 | Iustin Pop | "software version mismatch: master %s, node %s",
|
993 | 7c874ee1 | Iustin Pop | constants.RELEASE_VERSION, remote_version[1],
|
994 | a0c9776a | Iustin Pop | code=self.ETYPE_WARNING)
|
995 | e9ce0a64 | Iustin Pop | |
996 | e9ce0a64 | Iustin Pop | # checks vg existence and size > 20G
|
997 | cc9e1230 | Guido Trotter | if vg_name is not None: |
998 | cc9e1230 | Guido Trotter | vglist = node_result.get(constants.NV_VGLIST, None)
|
999 | a0c9776a | Iustin Pop | test = not vglist
|
1000 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups") |
1001 | a0c9776a | Iustin Pop | if not test: |
1002 | cc9e1230 | Guido Trotter | vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name, |
1003 | cc9e1230 | Guido Trotter | constants.MIN_VG_SIZE) |
1004 | a0c9776a | Iustin Pop | _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
|
1005 | a8083063 | Iustin Pop | |
1006 | a8083063 | Iustin Pop | # checks config file checksum
|
1007 | a8083063 | Iustin Pop | |
1008 | 25361b9a | Iustin Pop | remote_cksum = node_result.get(constants.NV_FILELIST, None)
|
1009 | a0c9776a | Iustin Pop | test = not isinstance(remote_cksum, dict) |
1010 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEFILECHECK, node,
|
1011 | a0c9776a | Iustin Pop | "node hasn't returned file checksum data")
|
1012 | a0c9776a | Iustin Pop | if not test: |
1013 | a8083063 | Iustin Pop | for file_name in file_list: |
1014 | 112f18a5 | Iustin Pop | node_is_mc = nodeinfo.master_candidate |
1015 | a0c9776a | Iustin Pop | must_have = (file_name not in master_files) or node_is_mc |
1016 | a0c9776a | Iustin Pop | # missing
|
1017 | a0c9776a | Iustin Pop | test1 = file_name not in remote_cksum |
1018 | a0c9776a | Iustin Pop | # invalid checksum
|
1019 | a0c9776a | Iustin Pop | test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name] |
1020 | a0c9776a | Iustin Pop | # existing and good
|
1021 | a0c9776a | Iustin Pop | test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name] |
1022 | a0c9776a | Iustin Pop | _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node, |
1023 | a0c9776a | Iustin Pop | "file '%s' missing", file_name)
|
1024 | a0c9776a | Iustin Pop | _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node, |
1025 | a0c9776a | Iustin Pop | "file '%s' has wrong checksum", file_name)
|
1026 | a0c9776a | Iustin Pop | # not candidate and this is not a must-have file
|
1027 | a0c9776a | Iustin Pop | _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node, |
1028 | a0c9776a | Iustin Pop | "file '%s' should not exist on non master"
|
1029 | a0c9776a | Iustin Pop | " candidates (and the file is outdated)", file_name)
|
1030 | a0c9776a | Iustin Pop | # all good, except non-master/non-must have combination
|
1031 | a0c9776a | Iustin Pop | _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node, |
1032 | a0c9776a | Iustin Pop | "file '%s' should not exist"
|
1033 | a0c9776a | Iustin Pop | " on non master candidates", file_name)
|
1034 | a8083063 | Iustin Pop | |
1035 | 25361b9a | Iustin Pop | # checks ssh to any
|
1036 | 25361b9a | Iustin Pop | |
1037 | a0c9776a | Iustin Pop | test = constants.NV_NODELIST not in node_result |
1038 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODESSH, node,
|
1039 | a0c9776a | Iustin Pop | "node hasn't returned node ssh connectivity data")
|
1040 | a0c9776a | Iustin Pop | if not test: |
1041 | 25361b9a | Iustin Pop | if node_result[constants.NV_NODELIST]:
|
1042 | 7c874ee1 | Iustin Pop | for a_node, a_msg in node_result[constants.NV_NODELIST].items(): |
1043 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODESSH, node, |
1044 | a0c9776a | Iustin Pop | "ssh communication with node '%s': %s", a_node, a_msg)
|
1045 | 25361b9a | Iustin Pop | |
1046 | a0c9776a | Iustin Pop | test = constants.NV_NODENETTEST not in node_result |
1047 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODENET, node,
|
1048 | a0c9776a | Iustin Pop | "node hasn't returned node tcp connectivity data")
|
1049 | a0c9776a | Iustin Pop | if not test: |
1050 | 25361b9a | Iustin Pop | if node_result[constants.NV_NODENETTEST]:
|
1051 | 25361b9a | Iustin Pop | nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys()) |
1052 | 7c874ee1 | Iustin Pop | for anode in nlist: |
1053 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODENET, node, |
1054 | a0c9776a | Iustin Pop | "tcp communication with node '%s': %s",
|
1055 | a0c9776a | Iustin Pop | anode, node_result[constants.NV_NODENETTEST][anode]) |
1056 | 9d4bfc96 | Iustin Pop | |
1057 | 25361b9a | Iustin Pop | hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
|
1058 | e69d05fd | Iustin Pop | if isinstance(hyp_result, dict): |
1059 | e69d05fd | Iustin Pop | for hv_name, hv_result in hyp_result.iteritems(): |
1060 | a0c9776a | Iustin Pop | test = hv_result is not None |
1061 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEHV, node,
|
1062 | a0c9776a | Iustin Pop | "hypervisor %s verify failure: '%s'", hv_name, hv_result)
|
1063 | 6d2e83d5 | Iustin Pop | |
1064 | 6d2e83d5 | Iustin Pop | # check used drbd list
|
1065 | cc9e1230 | Guido Trotter | if vg_name is not None: |
1066 | cc9e1230 | Guido Trotter | used_minors = node_result.get(constants.NV_DRBDLIST, []) |
1067 | a0c9776a | Iustin Pop | test = not isinstance(used_minors, (tuple, list)) |
1068 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1069 | a0c9776a | Iustin Pop | "cannot parse drbd status file: %s", str(used_minors)) |
1070 | a0c9776a | Iustin Pop | if not test: |
1071 | cc9e1230 | Guido Trotter | for minor, (iname, must_exist) in drbd_map.items(): |
1072 | a0c9776a | Iustin Pop | test = minor not in used_minors and must_exist |
1073 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1074 | a0c9776a | Iustin Pop | "drbd minor %d of instance %s is not active",
|
1075 | a0c9776a | Iustin Pop | minor, iname) |
1076 | cc9e1230 | Guido Trotter | for minor in used_minors: |
1077 | a0c9776a | Iustin Pop | test = minor not in drbd_map |
1078 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEDRBD, node,
|
1079 | a0c9776a | Iustin Pop | "unallocated drbd minor %d is in use", minor)
|
1080 | a8083063 | Iustin Pop | |
1081 | c5705f58 | Guido Trotter | def _VerifyInstance(self, instance, instanceconfig, node_vol_is, |
1082 | 7c874ee1 | Iustin Pop | node_instance, n_offline): |
1083 | a8083063 | Iustin Pop | """Verify an instance.
|
1084 | a8083063 | Iustin Pop |
|
1085 | a8083063 | Iustin Pop | This function checks to see if the required block devices are
|
1086 | a8083063 | Iustin Pop | available on the instance's node.
|
1087 | a8083063 | Iustin Pop |
|
1088 | a8083063 | Iustin Pop | """
|
1089 | a0c9776a | Iustin Pop | _ErrorIf = self._ErrorIf
|
1090 | a8083063 | Iustin Pop | node_current = instanceconfig.primary_node |
1091 | a8083063 | Iustin Pop | |
1092 | a8083063 | Iustin Pop | node_vol_should = {} |
1093 | a8083063 | Iustin Pop | instanceconfig.MapLVsByNode(node_vol_should) |
1094 | a8083063 | Iustin Pop | |
1095 | a8083063 | Iustin Pop | for node in node_vol_should: |
1096 | 0a66c968 | Iustin Pop | if node in n_offline: |
1097 | 0a66c968 | Iustin Pop | # ignore missing volumes on offline nodes
|
1098 | 0a66c968 | Iustin Pop | continue
|
1099 | a8083063 | Iustin Pop | for volume in node_vol_should[node]: |
1100 | a0c9776a | Iustin Pop | test = node not in node_vol_is or volume not in node_vol_is[node] |
1101 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
|
1102 | a0c9776a | Iustin Pop | "volume %s missing on node %s", volume, node)
|
1103 | a8083063 | Iustin Pop | |
1104 | 0d68c45d | Iustin Pop | if instanceconfig.admin_up:
|
1105 | a0c9776a | Iustin Pop | test = ((node_current not in node_instance or |
1106 | a0c9776a | Iustin Pop | not instance in node_instance[node_current]) and |
1107 | a0c9776a | Iustin Pop | node_current not in n_offline) |
1108 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEDOWN, instance,
|
1109 | a0c9776a | Iustin Pop | "instance not running on its primary node %s",
|
1110 | a0c9776a | Iustin Pop | node_current) |
1111 | a8083063 | Iustin Pop | |
1112 | a8083063 | Iustin Pop | for node in node_instance: |
1113 | a8083063 | Iustin Pop | if (not node == node_current): |
1114 | a0c9776a | Iustin Pop | test = instance in node_instance[node]
|
1115 | a0c9776a | Iustin Pop | _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
|
1116 | a0c9776a | Iustin Pop | "instance should not run on node %s", node)
|
1117 | a8083063 | Iustin Pop | |
1118 | 7c874ee1 | Iustin Pop | def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is): |
1119 | a8083063 | Iustin Pop | """Verify if there are any unknown volumes in the cluster.
|
1120 | a8083063 | Iustin Pop |
|
1121 | a8083063 | Iustin Pop | The .os, .swap and backup volumes are ignored. All other volumes are
|
1122 | a8083063 | Iustin Pop | reported as unknown.
|
1123 | a8083063 | Iustin Pop |
|
1124 | a8083063 | Iustin Pop | """
|
1125 | a8083063 | Iustin Pop | for node in node_vol_is: |
1126 | a8083063 | Iustin Pop | for volume in node_vol_is[node]: |
1127 | a0c9776a | Iustin Pop | test = (node not in node_vol_should or |
1128 | a0c9776a | Iustin Pop | volume not in node_vol_should[node]) |
1129 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEORPHANLV, node, |
1130 | 7c874ee1 | Iustin Pop | "volume %s is unknown", volume)
|
1131 | a8083063 | Iustin Pop | |
1132 | 7c874ee1 | Iustin Pop | def _VerifyOrphanInstances(self, instancelist, node_instance): |
1133 | a8083063 | Iustin Pop | """Verify the list of running instances.
|
1134 | a8083063 | Iustin Pop |
|
1135 | a8083063 | Iustin Pop | This checks what instances are running but unknown to the cluster.
|
1136 | a8083063 | Iustin Pop |
|
1137 | a8083063 | Iustin Pop | """
|
1138 | a8083063 | Iustin Pop | for node in node_instance: |
1139 | 7c874ee1 | Iustin Pop | for o_inst in node_instance[node]: |
1140 | a0c9776a | Iustin Pop | test = o_inst not in instancelist |
1141 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEORPHANINSTANCE, node, |
1142 | 7c874ee1 | Iustin Pop | "instance %s on node %s should not exist", o_inst, node)
|
1143 | a8083063 | Iustin Pop | |
1144 | 7c874ee1 | Iustin Pop | def _VerifyNPlusOneMemory(self, node_info, instance_cfg): |
1145 | 2b3b6ddd | Guido Trotter | """Verify N+1 Memory Resilience.
|
1146 | 2b3b6ddd | Guido Trotter |
|
1147 | 2b3b6ddd | Guido Trotter | Check that if one single node dies we can still start all the instances it
|
1148 | 2b3b6ddd | Guido Trotter | was primary for.
|
1149 | 2b3b6ddd | Guido Trotter |
|
1150 | 2b3b6ddd | Guido Trotter | """
|
1151 | 2b3b6ddd | Guido Trotter | for node, nodeinfo in node_info.iteritems(): |
1152 | 2b3b6ddd | Guido Trotter | # This code checks that every node which is now listed as secondary has
|
1153 | 2b3b6ddd | Guido Trotter | # enough memory to host all instances it is supposed to should a single
|
1154 | 2b3b6ddd | Guido Trotter | # other node in the cluster fail.
|
1155 | 2b3b6ddd | Guido Trotter | # FIXME: not ready for failover to an arbitrary node
|
1156 | 2b3b6ddd | Guido Trotter | # FIXME: does not support file-backed instances
|
1157 | 2b3b6ddd | Guido Trotter | # WARNING: we currently take into account down instances as well as up
|
1158 | 2b3b6ddd | Guido Trotter | # ones, considering that even if they're down someone might want to start
|
1159 | 2b3b6ddd | Guido Trotter | # them even in the event of a node failure.
|
1160 | 2b3b6ddd | Guido Trotter | for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems(): |
1161 | 2b3b6ddd | Guido Trotter | needed_mem = 0
|
1162 | 2b3b6ddd | Guido Trotter | for instance in instances: |
1163 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
|
1164 | c0f2b229 | Iustin Pop | if bep[constants.BE_AUTO_BALANCE]:
|
1165 | 3924700f | Iustin Pop | needed_mem += bep[constants.BE_MEMORY] |
1166 | a0c9776a | Iustin Pop | test = nodeinfo['mfree'] < needed_mem
|
1167 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEN1, node, |
1168 | 7c874ee1 | Iustin Pop | "not enough memory on to accommodate"
|
1169 | 7c874ee1 | Iustin Pop | " failovers should peer node %s fail", prinode)
|
1170 | 2b3b6ddd | Guido Trotter | |
1171 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1172 | a8083063 | Iustin Pop | """Check prerequisites.
|
1173 | a8083063 | Iustin Pop |
|
1174 | e54c4c5e | Guido Trotter | Transform the list of checks we're going to skip into a set and check that
|
1175 | e54c4c5e | Guido Trotter | all its members are valid.
|
1176 | a8083063 | Iustin Pop |
|
1177 | a8083063 | Iustin Pop | """
|
1178 | e54c4c5e | Guido Trotter | self.skip_set = frozenset(self.op.skip_checks) |
1179 | e54c4c5e | Guido Trotter | if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set): |
1180 | e54c4c5e | Guido Trotter | raise errors.OpPrereqError("Invalid checks to be skipped specified") |
1181 | a8083063 | Iustin Pop | |
1182 | d8fff41c | Guido Trotter | def BuildHooksEnv(self): |
1183 | d8fff41c | Guido Trotter | """Build hooks env.
|
1184 | d8fff41c | Guido Trotter |
|
1185 | 5bbd3f7f | Michael Hanselmann | Cluster-Verify hooks just ran in the post phase and their failure makes
|
1186 | d8fff41c | Guido Trotter | the output be logged in the verify output and the verification to fail.
|
1187 | d8fff41c | Guido Trotter |
|
1188 | d8fff41c | Guido Trotter | """
|
1189 | d8fff41c | Guido Trotter | all_nodes = self.cfg.GetNodeList()
|
1190 | 35e994e9 | Iustin Pop | env = { |
1191 | 35e994e9 | Iustin Pop | "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags()) |
1192 | 35e994e9 | Iustin Pop | } |
1193 | 35e994e9 | Iustin Pop | for node in self.cfg.GetAllNodesInfo().values(): |
1194 | 35e994e9 | Iustin Pop | env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags()) |
1195 | 35e994e9 | Iustin Pop | |
1196 | d8fff41c | Guido Trotter | return env, [], all_nodes
|
1197 | d8fff41c | Guido Trotter | |
1198 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1199 | a8083063 | Iustin Pop | """Verify integrity of cluster, performing various test on nodes.
|
1200 | a8083063 | Iustin Pop |
|
1201 | a8083063 | Iustin Pop | """
|
1202 | a0c9776a | Iustin Pop | self.bad = False |
1203 | a0c9776a | Iustin Pop | _ErrorIf = self._ErrorIf
|
1204 | 7c874ee1 | Iustin Pop | verbose = self.op.verbose
|
1205 | 7c874ee1 | Iustin Pop | self._feedback_fn = feedback_fn
|
1206 | a8083063 | Iustin Pop | feedback_fn("* Verifying global settings")
|
1207 | 8522ceeb | Iustin Pop | for msg in self.cfg.VerifyConfig(): |
1208 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ECLUSTERCFG, None, msg) |
1209 | a8083063 | Iustin Pop | |
1210 | a8083063 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
1211 | e69d05fd | Iustin Pop | hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
|
1212 | a8083063 | Iustin Pop | nodelist = utils.NiceSort(self.cfg.GetNodeList())
|
1213 | 9d4bfc96 | Iustin Pop | nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist] |
1214 | a8083063 | Iustin Pop | instancelist = utils.NiceSort(self.cfg.GetInstanceList())
|
1215 | 6d2e83d5 | Iustin Pop | instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname)) |
1216 | 6d2e83d5 | Iustin Pop | for iname in instancelist) |
1217 | 93e4c50b | Guido Trotter | i_non_redundant = [] # Non redundant instances
|
1218 | 3924700f | Iustin Pop | i_non_a_balanced = [] # Non auto-balanced instances
|
1219 | 0a66c968 | Iustin Pop | n_offline = [] # List of offline nodes
|
1220 | 22f0f71d | Iustin Pop | n_drained = [] # List of nodes being drained
|
1221 | a8083063 | Iustin Pop | node_volume = {} |
1222 | a8083063 | Iustin Pop | node_instance = {} |
1223 | 9c9c7d30 | Guido Trotter | node_info = {} |
1224 | 26b6af5e | Guido Trotter | instance_cfg = {} |
1225 | a8083063 | Iustin Pop | |
1226 | a8083063 | Iustin Pop | # FIXME: verify OS list
|
1227 | a8083063 | Iustin Pop | # do local checksums
|
1228 | 112f18a5 | Iustin Pop | master_files = [constants.CLUSTER_CONF_FILE] |
1229 | 112f18a5 | Iustin Pop | |
1230 | 112f18a5 | Iustin Pop | file_names = ssconf.SimpleStore().GetFileList() |
1231 | cb91d46e | Iustin Pop | file_names.append(constants.SSL_CERT_FILE) |
1232 | 699777f2 | Michael Hanselmann | file_names.append(constants.RAPI_CERT_FILE) |
1233 | 112f18a5 | Iustin Pop | file_names.extend(master_files) |
1234 | 112f18a5 | Iustin Pop | |
1235 | a8083063 | Iustin Pop | local_checksums = utils.FingerprintFiles(file_names) |
1236 | a8083063 | Iustin Pop | |
1237 | a8083063 | Iustin Pop | feedback_fn("* Gathering data (%d nodes)" % len(nodelist)) |
1238 | a8083063 | Iustin Pop | node_verify_param = { |
1239 | 25361b9a | Iustin Pop | constants.NV_FILELIST: file_names, |
1240 | 82e37788 | Iustin Pop | constants.NV_NODELIST: [node.name for node in nodeinfo |
1241 | 82e37788 | Iustin Pop | if not node.offline], |
1242 | 25361b9a | Iustin Pop | constants.NV_HYPERVISOR: hypervisors, |
1243 | 25361b9a | Iustin Pop | constants.NV_NODENETTEST: [(node.name, node.primary_ip, |
1244 | 82e37788 | Iustin Pop | node.secondary_ip) for node in nodeinfo |
1245 | 82e37788 | Iustin Pop | if not node.offline], |
1246 | 25361b9a | Iustin Pop | constants.NV_INSTANCELIST: hypervisors, |
1247 | 25361b9a | Iustin Pop | constants.NV_VERSION: None,
|
1248 | 25361b9a | Iustin Pop | constants.NV_HVINFO: self.cfg.GetHypervisorType(),
|
1249 | a8083063 | Iustin Pop | } |
1250 | cc9e1230 | Guido Trotter | if vg_name is not None: |
1251 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_VGLIST] = None
|
1252 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_LVLIST] = vg_name |
1253 | cc9e1230 | Guido Trotter | node_verify_param[constants.NV_DRBDLIST] = None
|
1254 | 72737a7f | Iustin Pop | all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
|
1255 | 72737a7f | Iustin Pop | self.cfg.GetClusterName())
|
1256 | a8083063 | Iustin Pop | |
1257 | 3924700f | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
1258 | 112f18a5 | Iustin Pop | master_node = self.cfg.GetMasterNode()
|
1259 | 6d2e83d5 | Iustin Pop | all_drbd_map = self.cfg.ComputeDRBDMap()
|
1260 | 6d2e83d5 | Iustin Pop | |
1261 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying node status")
|
1262 | 112f18a5 | Iustin Pop | for node_i in nodeinfo: |
1263 | 112f18a5 | Iustin Pop | node = node_i.name |
1264 | 25361b9a | Iustin Pop | |
1265 | 0a66c968 | Iustin Pop | if node_i.offline:
|
1266 | 7c874ee1 | Iustin Pop | if verbose:
|
1267 | 7c874ee1 | Iustin Pop | feedback_fn("* Skipping offline node %s" % (node,))
|
1268 | 0a66c968 | Iustin Pop | n_offline.append(node) |
1269 | 0a66c968 | Iustin Pop | continue
|
1270 | 0a66c968 | Iustin Pop | |
1271 | 112f18a5 | Iustin Pop | if node == master_node:
|
1272 | 25361b9a | Iustin Pop | ntype = "master"
|
1273 | 112f18a5 | Iustin Pop | elif node_i.master_candidate:
|
1274 | 25361b9a | Iustin Pop | ntype = "master candidate"
|
1275 | 22f0f71d | Iustin Pop | elif node_i.drained:
|
1276 | 22f0f71d | Iustin Pop | ntype = "drained"
|
1277 | 22f0f71d | Iustin Pop | n_drained.append(node) |
1278 | 112f18a5 | Iustin Pop | else:
|
1279 | 25361b9a | Iustin Pop | ntype = "regular"
|
1280 | 7c874ee1 | Iustin Pop | if verbose:
|
1281 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying node %s (%s)" % (node, ntype))
|
1282 | 25361b9a | Iustin Pop | |
1283 | 4c4e4e1e | Iustin Pop | msg = all_nvinfo[node].fail_msg |
1284 | a0c9776a | Iustin Pop | _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg) |
1285 | 6f68a739 | Iustin Pop | if msg:
|
1286 | 25361b9a | Iustin Pop | continue
|
1287 | 25361b9a | Iustin Pop | |
1288 | 6f68a739 | Iustin Pop | nresult = all_nvinfo[node].payload |
1289 | 6d2e83d5 | Iustin Pop | node_drbd = {} |
1290 | 6d2e83d5 | Iustin Pop | for minor, instance in all_drbd_map[node].items(): |
1291 | a0c9776a | Iustin Pop | test = instance not in instanceinfo |
1292 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ECLUSTERCFG, None, |
1293 | a0c9776a | Iustin Pop | "ghost instance '%s' in temporary DRBD map", instance)
|
1294 | c614e5fb | Iustin Pop | # ghost instance should not be running, but otherwise we
|
1295 | c614e5fb | Iustin Pop | # don't give double warnings (both ghost instance and
|
1296 | c614e5fb | Iustin Pop | # unallocated minor in use)
|
1297 | a0c9776a | Iustin Pop | if test:
|
1298 | c614e5fb | Iustin Pop | node_drbd[minor] = (instance, False)
|
1299 | c614e5fb | Iustin Pop | else:
|
1300 | c614e5fb | Iustin Pop | instance = instanceinfo[instance] |
1301 | c614e5fb | Iustin Pop | node_drbd[minor] = (instance.name, instance.admin_up) |
1302 | a0c9776a | Iustin Pop | self._VerifyNode(node_i, file_names, local_checksums,
|
1303 | a0c9776a | Iustin Pop | nresult, master_files, node_drbd, vg_name) |
1304 | a8083063 | Iustin Pop | |
1305 | 25361b9a | Iustin Pop | lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
|
1306 | cc9e1230 | Guido Trotter | if vg_name is None: |
1307 | cc9e1230 | Guido Trotter | node_volume[node] = {} |
1308 | cc9e1230 | Guido Trotter | elif isinstance(lvdata, basestring): |
1309 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s", |
1310 | a0c9776a | Iustin Pop | utils.SafeEncode(lvdata)) |
1311 | b63ed789 | Iustin Pop | node_volume[node] = {} |
1312 | 25361b9a | Iustin Pop | elif not isinstance(lvdata, dict): |
1313 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)") |
1314 | a8083063 | Iustin Pop | continue
|
1315 | b63ed789 | Iustin Pop | else:
|
1316 | 25361b9a | Iustin Pop | node_volume[node] = lvdata |
1317 | a8083063 | Iustin Pop | |
1318 | a8083063 | Iustin Pop | # node_instance
|
1319 | 25361b9a | Iustin Pop | idata = nresult.get(constants.NV_INSTANCELIST, None)
|
1320 | a0c9776a | Iustin Pop | test = not isinstance(idata, list) |
1321 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEHV, node,
|
1322 | a0c9776a | Iustin Pop | "rpc call to node failed (instancelist)")
|
1323 | a0c9776a | Iustin Pop | if test:
|
1324 | a8083063 | Iustin Pop | continue
|
1325 | a8083063 | Iustin Pop | |
1326 | 25361b9a | Iustin Pop | node_instance[node] = idata |
1327 | a8083063 | Iustin Pop | |
1328 | 9c9c7d30 | Guido Trotter | # node_info
|
1329 | 25361b9a | Iustin Pop | nodeinfo = nresult.get(constants.NV_HVINFO, None)
|
1330 | a0c9776a | Iustin Pop | test = not isinstance(nodeinfo, dict) |
1331 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)") |
1332 | a0c9776a | Iustin Pop | if test:
|
1333 | 9c9c7d30 | Guido Trotter | continue
|
1334 | 9c9c7d30 | Guido Trotter | |
1335 | 9c9c7d30 | Guido Trotter | try:
|
1336 | 9c9c7d30 | Guido Trotter | node_info[node] = { |
1337 | 9c9c7d30 | Guido Trotter | "mfree": int(nodeinfo['memory_free']), |
1338 | 93e4c50b | Guido Trotter | "pinst": [],
|
1339 | 93e4c50b | Guido Trotter | "sinst": [],
|
1340 | 36e7da50 | Guido Trotter | # dictionary holding all instances this node is secondary for,
|
1341 | 36e7da50 | Guido Trotter | # grouped by their primary node. Each key is a cluster node, and each
|
1342 | 36e7da50 | Guido Trotter | # value is a list of instances which have the key as primary and the
|
1343 | 36e7da50 | Guido Trotter | # current node as secondary. this is handy to calculate N+1 memory
|
1344 | 36e7da50 | Guido Trotter | # availability if you can only failover from a primary to its
|
1345 | 36e7da50 | Guido Trotter | # secondary.
|
1346 | 36e7da50 | Guido Trotter | "sinst-by-pnode": {},
|
1347 | 9c9c7d30 | Guido Trotter | } |
1348 | cc9e1230 | Guido Trotter | # FIXME: devise a free space model for file based instances as well
|
1349 | cc9e1230 | Guido Trotter | if vg_name is not None: |
1350 | a0c9776a | Iustin Pop | test = (constants.NV_VGLIST not in nresult or |
1351 | a0c9776a | Iustin Pop | vg_name not in nresult[constants.NV_VGLIST]) |
1352 | a0c9776a | Iustin Pop | _ErrorIf(test, self.ENODELVM, node,
|
1353 | a0c9776a | Iustin Pop | "node didn't return data for the volume group '%s'"
|
1354 | a0c9776a | Iustin Pop | " - it is either missing or broken", vg_name)
|
1355 | a0c9776a | Iustin Pop | if test:
|
1356 | 9a198532 | Iustin Pop | continue
|
1357 | cc9e1230 | Guido Trotter | node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name]) |
1358 | 9a198532 | Iustin Pop | except (ValueError, KeyError): |
1359 | a0c9776a | Iustin Pop | _ErrorIf(True, self.ENODERPC, node, |
1360 | a0c9776a | Iustin Pop | "node returned invalid nodeinfo, check lvm/hypervisor")
|
1361 | 9c9c7d30 | Guido Trotter | continue
|
1362 | 9c9c7d30 | Guido Trotter | |
1363 | a8083063 | Iustin Pop | node_vol_should = {} |
1364 | a8083063 | Iustin Pop | |
1365 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying instance status")
|
1366 | a8083063 | Iustin Pop | for instance in instancelist: |
1367 | 7c874ee1 | Iustin Pop | if verbose:
|
1368 | 7c874ee1 | Iustin Pop | feedback_fn("* Verifying instance %s" % instance)
|
1369 | 6d2e83d5 | Iustin Pop | inst_config = instanceinfo[instance] |
1370 | a0c9776a | Iustin Pop | self._VerifyInstance(instance, inst_config, node_volume,
|
1371 | a0c9776a | Iustin Pop | node_instance, n_offline) |
1372 | 832261fd | Iustin Pop | inst_nodes_offline = [] |
1373 | a8083063 | Iustin Pop | |
1374 | a8083063 | Iustin Pop | inst_config.MapLVsByNode(node_vol_should) |
1375 | a8083063 | Iustin Pop | |
1376 | 26b6af5e | Guido Trotter | instance_cfg[instance] = inst_config |
1377 | 26b6af5e | Guido Trotter | |
1378 | 93e4c50b | Guido Trotter | pnode = inst_config.primary_node |
1379 | a0c9776a | Iustin Pop | _ErrorIf(pnode not in node_info and pnode not in n_offline, |
1380 | a0c9776a | Iustin Pop | self.ENODERPC, pnode, "instance %s, connection to" |
1381 | a0c9776a | Iustin Pop | " primary node failed", instance)
|
1382 | 93e4c50b | Guido Trotter | if pnode in node_info: |
1383 | 93e4c50b | Guido Trotter | node_info[pnode]['pinst'].append(instance)
|
1384 | 93e4c50b | Guido Trotter | |
1385 | 832261fd | Iustin Pop | if pnode in n_offline: |
1386 | 832261fd | Iustin Pop | inst_nodes_offline.append(pnode) |
1387 | 832261fd | Iustin Pop | |
1388 | 93e4c50b | Guido Trotter | # If the instance is non-redundant we cannot survive losing its primary
|
1389 | 93e4c50b | Guido Trotter | # node, so we are not N+1 compliant. On the other hand we have no disk
|
1390 | 93e4c50b | Guido Trotter | # templates with more than one secondary so that situation is not well
|
1391 | 93e4c50b | Guido Trotter | # supported either.
|
1392 | 93e4c50b | Guido Trotter | # FIXME: does not support file-backed instances
|
1393 | 93e4c50b | Guido Trotter | if len(inst_config.secondary_nodes) == 0: |
1394 | 93e4c50b | Guido Trotter | i_non_redundant.append(instance) |
1395 | a0c9776a | Iustin Pop | _ErrorIf(len(inst_config.secondary_nodes) > 1, |
1396 | a0c9776a | Iustin Pop | self.EINSTANCELAYOUT, instance,
|
1397 | a0c9776a | Iustin Pop | "instance has multiple secondary nodes", code="WARNING") |
1398 | 93e4c50b | Guido Trotter | |
1399 | c0f2b229 | Iustin Pop | if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]: |
1400 | 3924700f | Iustin Pop | i_non_a_balanced.append(instance) |
1401 | 3924700f | Iustin Pop | |
1402 | 93e4c50b | Guido Trotter | for snode in inst_config.secondary_nodes: |
1403 | a0c9776a | Iustin Pop | _ErrorIf(snode not in node_info and snode not in n_offline, |
1404 | a0c9776a | Iustin Pop | self.ENODERPC, snode,
|
1405 | a0c9776a | Iustin Pop | "instance %s, connection to secondary node"
|
1406 | a0c9776a | Iustin Pop | "failed", instance)
|
1407 | a0c9776a | Iustin Pop | |
1408 | 93e4c50b | Guido Trotter | if snode in node_info: |
1409 | 93e4c50b | Guido Trotter | node_info[snode]['sinst'].append(instance)
|
1410 | 36e7da50 | Guido Trotter | if pnode not in node_info[snode]['sinst-by-pnode']: |
1411 | 36e7da50 | Guido Trotter | node_info[snode]['sinst-by-pnode'][pnode] = []
|
1412 | 36e7da50 | Guido Trotter | node_info[snode]['sinst-by-pnode'][pnode].append(instance)
|
1413 | a0c9776a | Iustin Pop | |
1414 | 832261fd | Iustin Pop | if snode in n_offline: |
1415 | 832261fd | Iustin Pop | inst_nodes_offline.append(snode) |
1416 | 832261fd | Iustin Pop | |
1417 | a0c9776a | Iustin Pop | # warn that the instance lives on offline nodes
|
1418 | a0c9776a | Iustin Pop | _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
|
1419 | a0c9776a | Iustin Pop | "instance lives on offline node(s) %s",
|
1420 | a0c9776a | Iustin Pop | ", ".join(inst_nodes_offline))
|
1421 | 93e4c50b | Guido Trotter | |
1422 | a8083063 | Iustin Pop | feedback_fn("* Verifying orphan volumes")
|
1423 | a0c9776a | Iustin Pop | self._VerifyOrphanVolumes(node_vol_should, node_volume)
|
1424 | a8083063 | Iustin Pop | |
1425 | a8083063 | Iustin Pop | feedback_fn("* Verifying remaining instances")
|
1426 | a0c9776a | Iustin Pop | self._VerifyOrphanInstances(instancelist, node_instance)
|
1427 | a8083063 | Iustin Pop | |
1428 | e54c4c5e | Guido Trotter | if constants.VERIFY_NPLUSONE_MEM not in self.skip_set: |
1429 | e54c4c5e | Guido Trotter | feedback_fn("* Verifying N+1 Memory redundancy")
|
1430 | a0c9776a | Iustin Pop | self._VerifyNPlusOneMemory(node_info, instance_cfg)
|
1431 | 2b3b6ddd | Guido Trotter | |
1432 | 2b3b6ddd | Guido Trotter | feedback_fn("* Other Notes")
|
1433 | 2b3b6ddd | Guido Trotter | if i_non_redundant:
|
1434 | 2b3b6ddd | Guido Trotter | feedback_fn(" - NOTICE: %d non-redundant instance(s) found."
|
1435 | 2b3b6ddd | Guido Trotter | % len(i_non_redundant))
|
1436 | 2b3b6ddd | Guido Trotter | |
1437 | 3924700f | Iustin Pop | if i_non_a_balanced:
|
1438 | 3924700f | Iustin Pop | feedback_fn(" - NOTICE: %d non-auto-balanced instance(s) found."
|
1439 | 3924700f | Iustin Pop | % len(i_non_a_balanced))
|
1440 | 3924700f | Iustin Pop | |
1441 | 0a66c968 | Iustin Pop | if n_offline:
|
1442 | 0a66c968 | Iustin Pop | feedback_fn(" - NOTICE: %d offline node(s) found." % len(n_offline)) |
1443 | 0a66c968 | Iustin Pop | |
1444 | 22f0f71d | Iustin Pop | if n_drained:
|
1445 | 22f0f71d | Iustin Pop | feedback_fn(" - NOTICE: %d drained node(s) found." % len(n_drained)) |
1446 | 22f0f71d | Iustin Pop | |
1447 | a0c9776a | Iustin Pop | return not self.bad |
1448 | a8083063 | Iustin Pop | |
1449 | d8fff41c | Guido Trotter | def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result): |
1450 | 5bbd3f7f | Michael Hanselmann | """Analyze the post-hooks' result
|
1451 | e4376078 | Iustin Pop |
|
1452 | e4376078 | Iustin Pop | This method analyses the hook result, handles it, and sends some
|
1453 | d8fff41c | Guido Trotter | nicely-formatted feedback back to the user.
|
1454 | d8fff41c | Guido Trotter |
|
1455 | e4376078 | Iustin Pop | @param phase: one of L{constants.HOOKS_PHASE_POST} or
|
1456 | e4376078 | Iustin Pop | L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
|
1457 | e4376078 | Iustin Pop | @param hooks_results: the results of the multi-node hooks rpc call
|
1458 | e4376078 | Iustin Pop | @param feedback_fn: function used send feedback back to the caller
|
1459 | e4376078 | Iustin Pop | @param lu_result: previous Exec result
|
1460 | e4376078 | Iustin Pop | @return: the new Exec result, based on the previous result
|
1461 | e4376078 | Iustin Pop | and hook results
|
1462 | d8fff41c | Guido Trotter |
|
1463 | d8fff41c | Guido Trotter | """
|
1464 | 38206f3c | Iustin Pop | # We only really run POST phase hooks, and are only interested in
|
1465 | 38206f3c | Iustin Pop | # their results
|
1466 | d8fff41c | Guido Trotter | if phase == constants.HOOKS_PHASE_POST:
|
1467 | d8fff41c | Guido Trotter | # Used to change hooks' output to proper indentation
|
1468 | d8fff41c | Guido Trotter | indent_re = re.compile('^', re.M)
|
1469 | d8fff41c | Guido Trotter | feedback_fn("* Hooks Results")
|
1470 | 7c874ee1 | Iustin Pop | assert hooks_results, "invalid result from hooks" |
1471 | 7c874ee1 | Iustin Pop | |
1472 | 7c874ee1 | Iustin Pop | for node_name in hooks_results: |
1473 | 7c874ee1 | Iustin Pop | show_node_header = True
|
1474 | 7c874ee1 | Iustin Pop | res = hooks_results[node_name] |
1475 | 7c874ee1 | Iustin Pop | msg = res.fail_msg |
1476 | a0c9776a | Iustin Pop | test = msg and not res.offline |
1477 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEHOOKS, node_name, |
1478 | 7c874ee1 | Iustin Pop | "Communication failure in hooks execution: %s", msg)
|
1479 | a0c9776a | Iustin Pop | if test:
|
1480 | a0c9776a | Iustin Pop | # override manually lu_result here as _ErrorIf only
|
1481 | a0c9776a | Iustin Pop | # overrides self.bad
|
1482 | 7c874ee1 | Iustin Pop | lu_result = 1
|
1483 | 7c874ee1 | Iustin Pop | continue
|
1484 | 7c874ee1 | Iustin Pop | for script, hkr, output in res.payload: |
1485 | a0c9776a | Iustin Pop | test = hkr == constants.HKR_FAIL |
1486 | a0c9776a | Iustin Pop | self._ErrorIf(test, self.ENODEHOOKS, node_name, |
1487 | 7c874ee1 | Iustin Pop | "Script %s failed, output:", script)
|
1488 | a0c9776a | Iustin Pop | if test:
|
1489 | 7c874ee1 | Iustin Pop | output = indent_re.sub(' ', output)
|
1490 | 7c874ee1 | Iustin Pop | feedback_fn("%s" % output)
|
1491 | 7c874ee1 | Iustin Pop | lu_result = 1
|
1492 | d8fff41c | Guido Trotter | |
1493 | d8fff41c | Guido Trotter | return lu_result
|
1494 | d8fff41c | Guido Trotter | |
1495 | a8083063 | Iustin Pop | |
1496 | 2c95a8d4 | Iustin Pop | class LUVerifyDisks(NoHooksLU): |
1497 | 2c95a8d4 | Iustin Pop | """Verifies the cluster disks status.
|
1498 | 2c95a8d4 | Iustin Pop |
|
1499 | 2c95a8d4 | Iustin Pop | """
|
1500 | 2c95a8d4 | Iustin Pop | _OP_REQP = [] |
1501 | d4b9d97f | Guido Trotter | REQ_BGL = False
|
1502 | d4b9d97f | Guido Trotter | |
1503 | d4b9d97f | Guido Trotter | def ExpandNames(self): |
1504 | d4b9d97f | Guido Trotter | self.needed_locks = {
|
1505 | d4b9d97f | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
1506 | d4b9d97f | Guido Trotter | locking.LEVEL_INSTANCE: locking.ALL_SET, |
1507 | d4b9d97f | Guido Trotter | } |
1508 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
1509 | 2c95a8d4 | Iustin Pop | |
1510 | 2c95a8d4 | Iustin Pop | def CheckPrereq(self): |
1511 | 2c95a8d4 | Iustin Pop | """Check prerequisites.
|
1512 | 2c95a8d4 | Iustin Pop |
|
1513 | 2c95a8d4 | Iustin Pop | This has no prerequisites.
|
1514 | 2c95a8d4 | Iustin Pop |
|
1515 | 2c95a8d4 | Iustin Pop | """
|
1516 | 2c95a8d4 | Iustin Pop | pass
|
1517 | 2c95a8d4 | Iustin Pop | |
1518 | 2c95a8d4 | Iustin Pop | def Exec(self, feedback_fn): |
1519 | 2c95a8d4 | Iustin Pop | """Verify integrity of cluster disks.
|
1520 | 2c95a8d4 | Iustin Pop |
|
1521 | 29d376ec | Iustin Pop | @rtype: tuple of three items
|
1522 | 29d376ec | Iustin Pop | @return: a tuple of (dict of node-to-node_error, list of instances
|
1523 | 29d376ec | Iustin Pop | which need activate-disks, dict of instance: (node, volume) for
|
1524 | 29d376ec | Iustin Pop | missing volumes
|
1525 | 29d376ec | Iustin Pop |
|
1526 | 2c95a8d4 | Iustin Pop | """
|
1527 | 29d376ec | Iustin Pop | result = res_nodes, res_instances, res_missing = {}, [], {} |
1528 | 2c95a8d4 | Iustin Pop | |
1529 | 2c95a8d4 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
1530 | 2c95a8d4 | Iustin Pop | nodes = utils.NiceSort(self.cfg.GetNodeList())
|
1531 | 2c95a8d4 | Iustin Pop | instances = [self.cfg.GetInstanceInfo(name)
|
1532 | 2c95a8d4 | Iustin Pop | for name in self.cfg.GetInstanceList()] |
1533 | 2c95a8d4 | Iustin Pop | |
1534 | 2c95a8d4 | Iustin Pop | nv_dict = {} |
1535 | 2c95a8d4 | Iustin Pop | for inst in instances: |
1536 | 2c95a8d4 | Iustin Pop | inst_lvs = {} |
1537 | 0d68c45d | Iustin Pop | if (not inst.admin_up or |
1538 | 2c95a8d4 | Iustin Pop | inst.disk_template not in constants.DTS_NET_MIRROR): |
1539 | 2c95a8d4 | Iustin Pop | continue
|
1540 | 2c95a8d4 | Iustin Pop | inst.MapLVsByNode(inst_lvs) |
1541 | 2c95a8d4 | Iustin Pop | # transform { iname: {node: [vol,],},} to {(node, vol): iname}
|
1542 | 2c95a8d4 | Iustin Pop | for node, vol_list in inst_lvs.iteritems(): |
1543 | 2c95a8d4 | Iustin Pop | for vol in vol_list: |
1544 | 2c95a8d4 | Iustin Pop | nv_dict[(node, vol)] = inst |
1545 | 2c95a8d4 | Iustin Pop | |
1546 | 2c95a8d4 | Iustin Pop | if not nv_dict: |
1547 | 2c95a8d4 | Iustin Pop | return result
|
1548 | 2c95a8d4 | Iustin Pop | |
1549 | b2a6ccd4 | Iustin Pop | node_lvs = self.rpc.call_lv_list(nodes, vg_name)
|
1550 | 2c95a8d4 | Iustin Pop | |
1551 | 2c95a8d4 | Iustin Pop | for node in nodes: |
1552 | 2c95a8d4 | Iustin Pop | # node_volume
|
1553 | 29d376ec | Iustin Pop | node_res = node_lvs[node] |
1554 | 29d376ec | Iustin Pop | if node_res.offline:
|
1555 | ea9ddc07 | Iustin Pop | continue
|
1556 | 4c4e4e1e | Iustin Pop | msg = node_res.fail_msg |
1557 | 29d376ec | Iustin Pop | if msg:
|
1558 | 29d376ec | Iustin Pop | logging.warning("Error enumerating LVs on node %s: %s", node, msg)
|
1559 | 29d376ec | Iustin Pop | res_nodes[node] = msg |
1560 | 2c95a8d4 | Iustin Pop | continue
|
1561 | 2c95a8d4 | Iustin Pop | |
1562 | 29d376ec | Iustin Pop | lvs = node_res.payload |
1563 | 29d376ec | Iustin Pop | for lv_name, (_, lv_inactive, lv_online) in lvs.items(): |
1564 | b63ed789 | Iustin Pop | inst = nv_dict.pop((node, lv_name), None)
|
1565 | b63ed789 | Iustin Pop | if (not lv_online and inst is not None |
1566 | b63ed789 | Iustin Pop | and inst.name not in res_instances): |
1567 | b08d5a87 | Iustin Pop | res_instances.append(inst.name) |
1568 | 2c95a8d4 | Iustin Pop | |
1569 | b63ed789 | Iustin Pop | # any leftover items in nv_dict are missing LVs, let's arrange the
|
1570 | b63ed789 | Iustin Pop | # data better
|
1571 | b63ed789 | Iustin Pop | for key, inst in nv_dict.iteritems(): |
1572 | b63ed789 | Iustin Pop | if inst.name not in res_missing: |
1573 | b63ed789 | Iustin Pop | res_missing[inst.name] = [] |
1574 | b63ed789 | Iustin Pop | res_missing[inst.name].append(key) |
1575 | b63ed789 | Iustin Pop | |
1576 | 2c95a8d4 | Iustin Pop | return result
|
1577 | 2c95a8d4 | Iustin Pop | |
1578 | 2c95a8d4 | Iustin Pop | |
1579 | 60975797 | Iustin Pop | class LURepairDiskSizes(NoHooksLU): |
1580 | 60975797 | Iustin Pop | """Verifies the cluster disks sizes.
|
1581 | 60975797 | Iustin Pop |
|
1582 | 60975797 | Iustin Pop | """
|
1583 | 60975797 | Iustin Pop | _OP_REQP = ["instances"]
|
1584 | 60975797 | Iustin Pop | REQ_BGL = False
|
1585 | 60975797 | Iustin Pop | |
1586 | 60975797 | Iustin Pop | def ExpandNames(self): |
1587 | 60975797 | Iustin Pop | if not isinstance(self.op.instances, list): |
1588 | 60975797 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'") |
1589 | 60975797 | Iustin Pop | |
1590 | 60975797 | Iustin Pop | if self.op.instances: |
1591 | 60975797 | Iustin Pop | self.wanted_names = []
|
1592 | 60975797 | Iustin Pop | for name in self.op.instances: |
1593 | 60975797 | Iustin Pop | full_name = self.cfg.ExpandInstanceName(name)
|
1594 | 60975797 | Iustin Pop | if full_name is None: |
1595 | 60975797 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % name) |
1596 | 60975797 | Iustin Pop | self.wanted_names.append(full_name)
|
1597 | 60975797 | Iustin Pop | self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names |
1598 | 60975797 | Iustin Pop | self.needed_locks = {
|
1599 | 60975797 | Iustin Pop | locking.LEVEL_NODE: [], |
1600 | 60975797 | Iustin Pop | locking.LEVEL_INSTANCE: self.wanted_names,
|
1601 | 60975797 | Iustin Pop | } |
1602 | 60975797 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
1603 | 60975797 | Iustin Pop | else:
|
1604 | 60975797 | Iustin Pop | self.wanted_names = None |
1605 | 60975797 | Iustin Pop | self.needed_locks = {
|
1606 | 60975797 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET, |
1607 | 60975797 | Iustin Pop | locking.LEVEL_INSTANCE: locking.ALL_SET, |
1608 | 60975797 | Iustin Pop | } |
1609 | 60975797 | Iustin Pop | self.share_locks = dict(((i, 1) for i in locking.LEVELS)) |
1610 | 60975797 | Iustin Pop | |
1611 | 60975797 | Iustin Pop | def DeclareLocks(self, level): |
1612 | 60975797 | Iustin Pop | if level == locking.LEVEL_NODE and self.wanted_names is not None: |
1613 | 60975797 | Iustin Pop | self._LockInstancesNodes(primary_only=True) |
1614 | 60975797 | Iustin Pop | |
1615 | 60975797 | Iustin Pop | def CheckPrereq(self): |
1616 | 60975797 | Iustin Pop | """Check prerequisites.
|
1617 | 60975797 | Iustin Pop |
|
1618 | 60975797 | Iustin Pop | This only checks the optional instance list against the existing names.
|
1619 | 60975797 | Iustin Pop |
|
1620 | 60975797 | Iustin Pop | """
|
1621 | 60975797 | Iustin Pop | if self.wanted_names is None: |
1622 | 60975797 | Iustin Pop | self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE] |
1623 | 60975797 | Iustin Pop | |
1624 | 60975797 | Iustin Pop | self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name |
1625 | 60975797 | Iustin Pop | in self.wanted_names] |
1626 | 60975797 | Iustin Pop | |
1627 | 60975797 | Iustin Pop | def Exec(self, feedback_fn): |
1628 | 60975797 | Iustin Pop | """Verify the size of cluster disks.
|
1629 | 60975797 | Iustin Pop |
|
1630 | 60975797 | Iustin Pop | """
|
1631 | 60975797 | Iustin Pop | # TODO: check child disks too
|
1632 | 60975797 | Iustin Pop | # TODO: check differences in size between primary/secondary nodes
|
1633 | 60975797 | Iustin Pop | per_node_disks = {} |
1634 | 60975797 | Iustin Pop | for instance in self.wanted_instances: |
1635 | 60975797 | Iustin Pop | pnode = instance.primary_node |
1636 | 60975797 | Iustin Pop | if pnode not in per_node_disks: |
1637 | 60975797 | Iustin Pop | per_node_disks[pnode] = [] |
1638 | 60975797 | Iustin Pop | for idx, disk in enumerate(instance.disks): |
1639 | 60975797 | Iustin Pop | per_node_disks[pnode].append((instance, idx, disk)) |
1640 | 60975797 | Iustin Pop | |
1641 | 60975797 | Iustin Pop | changed = [] |
1642 | 60975797 | Iustin Pop | for node, dskl in per_node_disks.items(): |
1643 | 60975797 | Iustin Pop | result = self.rpc.call_blockdev_getsizes(node, [v[2] for v in dskl]) |
1644 | 3cebe102 | Michael Hanselmann | if result.fail_msg:
|
1645 | 60975797 | Iustin Pop | self.LogWarning("Failure in blockdev_getsizes call to node" |
1646 | 60975797 | Iustin Pop | " %s, ignoring", node)
|
1647 | 60975797 | Iustin Pop | continue
|
1648 | 60975797 | Iustin Pop | if len(result.data) != len(dskl): |
1649 | 60975797 | Iustin Pop | self.LogWarning("Invalid result from node %s, ignoring node results", |
1650 | 60975797 | Iustin Pop | node) |
1651 | 60975797 | Iustin Pop | continue
|
1652 | 60975797 | Iustin Pop | for ((instance, idx, disk), size) in zip(dskl, result.data): |
1653 | 60975797 | Iustin Pop | if size is None: |
1654 | 60975797 | Iustin Pop | self.LogWarning("Disk %d of instance %s did not return size" |
1655 | 60975797 | Iustin Pop | " information, ignoring", idx, instance.name)
|
1656 | 60975797 | Iustin Pop | continue
|
1657 | 60975797 | Iustin Pop | if not isinstance(size, (int, long)): |
1658 | 60975797 | Iustin Pop | self.LogWarning("Disk %d of instance %s did not return valid" |
1659 | 60975797 | Iustin Pop | " size information, ignoring", idx, instance.name)
|
1660 | 60975797 | Iustin Pop | continue
|
1661 | 60975797 | Iustin Pop | size = size >> 20
|
1662 | 60975797 | Iustin Pop | if size != disk.size:
|
1663 | 60975797 | Iustin Pop | self.LogInfo("Disk %d of instance %s has mismatched size," |
1664 | 60975797 | Iustin Pop | " correcting: recorded %d, actual %d", idx,
|
1665 | 60975797 | Iustin Pop | instance.name, disk.size, size) |
1666 | 60975797 | Iustin Pop | disk.size = size |
1667 | 60975797 | Iustin Pop | self.cfg.Update(instance)
|
1668 | 60975797 | Iustin Pop | changed.append((instance.name, idx, size)) |
1669 | 60975797 | Iustin Pop | return changed
|
1670 | 60975797 | Iustin Pop | |
1671 | 60975797 | Iustin Pop | |
1672 | 07bd8a51 | Iustin Pop | class LURenameCluster(LogicalUnit): |
1673 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
1674 | 07bd8a51 | Iustin Pop |
|
1675 | 07bd8a51 | Iustin Pop | """
|
1676 | 07bd8a51 | Iustin Pop | HPATH = "cluster-rename"
|
1677 | 07bd8a51 | Iustin Pop | HTYPE = constants.HTYPE_CLUSTER |
1678 | 07bd8a51 | Iustin Pop | _OP_REQP = ["name"]
|
1679 | 07bd8a51 | Iustin Pop | |
1680 | 07bd8a51 | Iustin Pop | def BuildHooksEnv(self): |
1681 | 07bd8a51 | Iustin Pop | """Build hooks env.
|
1682 | 07bd8a51 | Iustin Pop |
|
1683 | 07bd8a51 | Iustin Pop | """
|
1684 | 07bd8a51 | Iustin Pop | env = { |
1685 | d6a02168 | Michael Hanselmann | "OP_TARGET": self.cfg.GetClusterName(), |
1686 | 07bd8a51 | Iustin Pop | "NEW_NAME": self.op.name, |
1687 | 07bd8a51 | Iustin Pop | } |
1688 | d6a02168 | Michael Hanselmann | mn = self.cfg.GetMasterNode()
|
1689 | 07bd8a51 | Iustin Pop | return env, [mn], [mn]
|
1690 | 07bd8a51 | Iustin Pop | |
1691 | 07bd8a51 | Iustin Pop | def CheckPrereq(self): |
1692 | 07bd8a51 | Iustin Pop | """Verify that the passed name is a valid one.
|
1693 | 07bd8a51 | Iustin Pop |
|
1694 | 07bd8a51 | Iustin Pop | """
|
1695 | 89e1fc26 | Iustin Pop | hostname = utils.HostInfo(self.op.name)
|
1696 | 07bd8a51 | Iustin Pop | |
1697 | bcf043c9 | Iustin Pop | new_name = hostname.name |
1698 | bcf043c9 | Iustin Pop | self.ip = new_ip = hostname.ip
|
1699 | d6a02168 | Michael Hanselmann | old_name = self.cfg.GetClusterName()
|
1700 | d6a02168 | Michael Hanselmann | old_ip = self.cfg.GetMasterIP()
|
1701 | 07bd8a51 | Iustin Pop | if new_name == old_name and new_ip == old_ip: |
1702 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("Neither the name nor the IP address of the" |
1703 | 07bd8a51 | Iustin Pop | " cluster has changed")
|
1704 | 07bd8a51 | Iustin Pop | if new_ip != old_ip:
|
1705 | 937f983d | Guido Trotter | if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
|
1706 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("The given cluster IP address (%s) is" |
1707 | 07bd8a51 | Iustin Pop | " reachable on the network. Aborting." %
|
1708 | 07bd8a51 | Iustin Pop | new_ip) |
1709 | 07bd8a51 | Iustin Pop | |
1710 | 07bd8a51 | Iustin Pop | self.op.name = new_name
|
1711 | 07bd8a51 | Iustin Pop | |
1712 | 07bd8a51 | Iustin Pop | def Exec(self, feedback_fn): |
1713 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
1714 | 07bd8a51 | Iustin Pop |
|
1715 | 07bd8a51 | Iustin Pop | """
|
1716 | 07bd8a51 | Iustin Pop | clustername = self.op.name
|
1717 | 07bd8a51 | Iustin Pop | ip = self.ip
|
1718 | 07bd8a51 | Iustin Pop | |
1719 | 07bd8a51 | Iustin Pop | # shutdown the master IP
|
1720 | d6a02168 | Michael Hanselmann | master = self.cfg.GetMasterNode()
|
1721 | 781de953 | Iustin Pop | result = self.rpc.call_node_stop_master(master, False) |
1722 | 4c4e4e1e | Iustin Pop | result.Raise("Could not disable the master role")
|
1723 | 07bd8a51 | Iustin Pop | |
1724 | 07bd8a51 | Iustin Pop | try:
|
1725 | 55cf7d83 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
1726 | 55cf7d83 | Iustin Pop | cluster.cluster_name = clustername |
1727 | 55cf7d83 | Iustin Pop | cluster.master_ip = ip |
1728 | 55cf7d83 | Iustin Pop | self.cfg.Update(cluster)
|
1729 | ec85e3d5 | Iustin Pop | |
1730 | ec85e3d5 | Iustin Pop | # update the known hosts file
|
1731 | ec85e3d5 | Iustin Pop | ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
|
1732 | ec85e3d5 | Iustin Pop | node_list = self.cfg.GetNodeList()
|
1733 | ec85e3d5 | Iustin Pop | try:
|
1734 | ec85e3d5 | Iustin Pop | node_list.remove(master) |
1735 | ec85e3d5 | Iustin Pop | except ValueError: |
1736 | ec85e3d5 | Iustin Pop | pass
|
1737 | ec85e3d5 | Iustin Pop | result = self.rpc.call_upload_file(node_list,
|
1738 | ec85e3d5 | Iustin Pop | constants.SSH_KNOWN_HOSTS_FILE) |
1739 | ec85e3d5 | Iustin Pop | for to_node, to_result in result.iteritems(): |
1740 | 6f7d4e75 | Iustin Pop | msg = to_result.fail_msg |
1741 | 6f7d4e75 | Iustin Pop | if msg:
|
1742 | 6f7d4e75 | Iustin Pop | msg = ("Copy of file %s to node %s failed: %s" %
|
1743 | 6f7d4e75 | Iustin Pop | (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg)) |
1744 | 6f7d4e75 | Iustin Pop | self.proc.LogWarning(msg)
|
1745 | ec85e3d5 | Iustin Pop | |
1746 | 07bd8a51 | Iustin Pop | finally:
|
1747 | 3583908a | Guido Trotter | result = self.rpc.call_node_start_master(master, False, False) |
1748 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
1749 | b726aff0 | Iustin Pop | if msg:
|
1750 | 86d9d3bb | Iustin Pop | self.LogWarning("Could not re-enable the master role on" |
1751 | b726aff0 | Iustin Pop | " the master, please restart manually: %s", msg)
|
1752 | 07bd8a51 | Iustin Pop | |
1753 | 07bd8a51 | Iustin Pop | |
1754 | 8084f9f6 | Manuel Franceschini | def _RecursiveCheckIfLVMBased(disk): |
1755 | 8084f9f6 | Manuel Franceschini | """Check if the given disk or its children are lvm-based.
|
1756 | 8084f9f6 | Manuel Franceschini |
|
1757 | e4376078 | Iustin Pop | @type disk: L{objects.Disk}
|
1758 | e4376078 | Iustin Pop | @param disk: the disk to check
|
1759 | 5bbd3f7f | Michael Hanselmann | @rtype: boolean
|
1760 | e4376078 | Iustin Pop | @return: boolean indicating whether a LD_LV dev_type was found or not
|
1761 | 8084f9f6 | Manuel Franceschini |
|
1762 | 8084f9f6 | Manuel Franceschini | """
|
1763 | 8084f9f6 | Manuel Franceschini | if disk.children:
|
1764 | 8084f9f6 | Manuel Franceschini | for chdisk in disk.children: |
1765 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(chdisk):
|
1766 | 8084f9f6 | Manuel Franceschini | return True |
1767 | 8084f9f6 | Manuel Franceschini | return disk.dev_type == constants.LD_LV
|
1768 | 8084f9f6 | Manuel Franceschini | |
1769 | 8084f9f6 | Manuel Franceschini | |
1770 | 8084f9f6 | Manuel Franceschini | class LUSetClusterParams(LogicalUnit): |
1771 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
1772 | 8084f9f6 | Manuel Franceschini |
|
1773 | 8084f9f6 | Manuel Franceschini | """
|
1774 | 8084f9f6 | Manuel Franceschini | HPATH = "cluster-modify"
|
1775 | 8084f9f6 | Manuel Franceschini | HTYPE = constants.HTYPE_CLUSTER |
1776 | 8084f9f6 | Manuel Franceschini | _OP_REQP = [] |
1777 | c53279cf | Guido Trotter | REQ_BGL = False
|
1778 | c53279cf | Guido Trotter | |
1779 | 3994f455 | Iustin Pop | def CheckArguments(self): |
1780 | 4b7735f9 | Iustin Pop | """Check parameters
|
1781 | 4b7735f9 | Iustin Pop |
|
1782 | 4b7735f9 | Iustin Pop | """
|
1783 | 4b7735f9 | Iustin Pop | if not hasattr(self.op, "candidate_pool_size"): |
1784 | 4b7735f9 | Iustin Pop | self.op.candidate_pool_size = None |
1785 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size is not None: |
1786 | 4b7735f9 | Iustin Pop | try:
|
1787 | 4b7735f9 | Iustin Pop | self.op.candidate_pool_size = int(self.op.candidate_pool_size) |
1788 | 3994f455 | Iustin Pop | except (ValueError, TypeError), err: |
1789 | 4b7735f9 | Iustin Pop | raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" % |
1790 | 4b7735f9 | Iustin Pop | str(err))
|
1791 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size < 1: |
1792 | 4b7735f9 | Iustin Pop | raise errors.OpPrereqError("At least one master candidate needed") |
1793 | 4b7735f9 | Iustin Pop | |
1794 | c53279cf | Guido Trotter | def ExpandNames(self): |
1795 | c53279cf | Guido Trotter | # FIXME: in the future maybe other cluster params won't require checking on
|
1796 | c53279cf | Guido Trotter | # all nodes to be modified.
|
1797 | c53279cf | Guido Trotter | self.needed_locks = {
|
1798 | c53279cf | Guido Trotter | locking.LEVEL_NODE: locking.ALL_SET, |
1799 | c53279cf | Guido Trotter | } |
1800 | c53279cf | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
1801 | 8084f9f6 | Manuel Franceschini | |
1802 | 8084f9f6 | Manuel Franceschini | def BuildHooksEnv(self): |
1803 | 8084f9f6 | Manuel Franceschini | """Build hooks env.
|
1804 | 8084f9f6 | Manuel Franceschini |
|
1805 | 8084f9f6 | Manuel Franceschini | """
|
1806 | 8084f9f6 | Manuel Franceschini | env = { |
1807 | d6a02168 | Michael Hanselmann | "OP_TARGET": self.cfg.GetClusterName(), |
1808 | 8084f9f6 | Manuel Franceschini | "NEW_VG_NAME": self.op.vg_name, |
1809 | 8084f9f6 | Manuel Franceschini | } |
1810 | d6a02168 | Michael Hanselmann | mn = self.cfg.GetMasterNode()
|
1811 | 8084f9f6 | Manuel Franceschini | return env, [mn], [mn]
|
1812 | 8084f9f6 | Manuel Franceschini | |
1813 | 8084f9f6 | Manuel Franceschini | def CheckPrereq(self): |
1814 | 8084f9f6 | Manuel Franceschini | """Check prerequisites.
|
1815 | 8084f9f6 | Manuel Franceschini |
|
1816 | 8084f9f6 | Manuel Franceschini | This checks whether the given params don't conflict and
|
1817 | 5f83e263 | Iustin Pop | if the given volume group is valid.
|
1818 | 8084f9f6 | Manuel Franceschini |
|
1819 | 8084f9f6 | Manuel Franceschini | """
|
1820 | 779c15bb | Iustin Pop | if self.op.vg_name is not None and not self.op.vg_name: |
1821 | c53279cf | Guido Trotter | instances = self.cfg.GetAllInstancesInfo().values()
|
1822 | 8084f9f6 | Manuel Franceschini | for inst in instances: |
1823 | 8084f9f6 | Manuel Franceschini | for disk in inst.disks: |
1824 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(disk):
|
1825 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Cannot disable lvm storage while" |
1826 | 8084f9f6 | Manuel Franceschini | " lvm-based instances exist")
|
1827 | 8084f9f6 | Manuel Franceschini | |
1828 | 779c15bb | Iustin Pop | node_list = self.acquired_locks[locking.LEVEL_NODE]
|
1829 | 779c15bb | Iustin Pop | |
1830 | 8084f9f6 | Manuel Franceschini | # if vg_name not None, checks given volume group on all nodes
|
1831 | 8084f9f6 | Manuel Franceschini | if self.op.vg_name: |
1832 | 72737a7f | Iustin Pop | vglist = self.rpc.call_vg_list(node_list)
|
1833 | 8084f9f6 | Manuel Franceschini | for node in node_list: |
1834 | 4c4e4e1e | Iustin Pop | msg = vglist[node].fail_msg |
1835 | e480923b | Iustin Pop | if msg:
|
1836 | 781de953 | Iustin Pop | # ignoring down node
|
1837 | e480923b | Iustin Pop | self.LogWarning("Error while gathering data on node %s" |
1838 | e480923b | Iustin Pop | " (ignoring node): %s", node, msg)
|
1839 | 781de953 | Iustin Pop | continue
|
1840 | e480923b | Iustin Pop | vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload, |
1841 | 781de953 | Iustin Pop | self.op.vg_name,
|
1842 | 8d1a2a64 | Michael Hanselmann | constants.MIN_VG_SIZE) |
1843 | 8084f9f6 | Manuel Franceschini | if vgstatus:
|
1844 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Error on node '%s': %s" % |
1845 | 8084f9f6 | Manuel Franceschini | (node, vgstatus)) |
1846 | 8084f9f6 | Manuel Franceschini | |
1847 | 779c15bb | Iustin Pop | self.cluster = cluster = self.cfg.GetClusterInfo() |
1848 | 5af3da74 | Guido Trotter | # validate params changes
|
1849 | 779c15bb | Iustin Pop | if self.op.beparams: |
1850 | a5728081 | Guido Trotter | utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
|
1851 | abe609b2 | Guido Trotter | self.new_beparams = objects.FillDict(
|
1852 | 4ef7f423 | Guido Trotter | cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
|
1853 | 779c15bb | Iustin Pop | |
1854 | 5af3da74 | Guido Trotter | if self.op.nicparams: |
1855 | 5af3da74 | Guido Trotter | utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
|
1856 | 5af3da74 | Guido Trotter | self.new_nicparams = objects.FillDict(
|
1857 | 5af3da74 | Guido Trotter | cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
|
1858 | 5af3da74 | Guido Trotter | objects.NIC.CheckParameterSyntax(self.new_nicparams)
|
1859 | 5af3da74 | Guido Trotter | |
1860 | 779c15bb | Iustin Pop | # hypervisor list/parameters
|
1861 | abe609b2 | Guido Trotter | self.new_hvparams = objects.FillDict(cluster.hvparams, {})
|
1862 | 779c15bb | Iustin Pop | if self.op.hvparams: |
1863 | 779c15bb | Iustin Pop | if not isinstance(self.op.hvparams, dict): |
1864 | 779c15bb | Iustin Pop | raise errors.OpPrereqError("Invalid 'hvparams' parameter on input") |
1865 | 779c15bb | Iustin Pop | for hv_name, hv_dict in self.op.hvparams.items(): |
1866 | 779c15bb | Iustin Pop | if hv_name not in self.new_hvparams: |
1867 | 779c15bb | Iustin Pop | self.new_hvparams[hv_name] = hv_dict
|
1868 | 779c15bb | Iustin Pop | else:
|
1869 | 779c15bb | Iustin Pop | self.new_hvparams[hv_name].update(hv_dict)
|
1870 | 779c15bb | Iustin Pop | |
1871 | 779c15bb | Iustin Pop | if self.op.enabled_hypervisors is not None: |
1872 | 779c15bb | Iustin Pop | self.hv_list = self.op.enabled_hypervisors |
1873 | b119bccb | Guido Trotter | if not self.hv_list: |
1874 | b119bccb | Guido Trotter | raise errors.OpPrereqError("Enabled hypervisors list must contain at" |
1875 | b119bccb | Guido Trotter | " least one member")
|
1876 | b119bccb | Guido Trotter | invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES |
1877 | b119bccb | Guido Trotter | if invalid_hvs:
|
1878 | b119bccb | Guido Trotter | raise errors.OpPrereqError("Enabled hypervisors contains invalid" |
1879 | 46af447b | Iustin Pop | " entries: %s" %
|
1880 | 46af447b | Iustin Pop | utils.CommaJoin(invalid_hvs)) |
1881 | 779c15bb | Iustin Pop | else:
|
1882 | 779c15bb | Iustin Pop | self.hv_list = cluster.enabled_hypervisors
|
1883 | 779c15bb | Iustin Pop | |
1884 | 779c15bb | Iustin Pop | if self.op.hvparams or self.op.enabled_hypervisors is not None: |
1885 | 779c15bb | Iustin Pop | # either the enabled list has changed, or the parameters have, validate
|
1886 | 779c15bb | Iustin Pop | for hv_name, hv_params in self.new_hvparams.items(): |
1887 | 779c15bb | Iustin Pop | if ((self.op.hvparams and hv_name in self.op.hvparams) or |
1888 | 779c15bb | Iustin Pop | (self.op.enabled_hypervisors and |
1889 | 779c15bb | Iustin Pop | hv_name in self.op.enabled_hypervisors)): |
1890 | 779c15bb | Iustin Pop | # either this is a new hypervisor, or its parameters have changed
|
1891 | 779c15bb | Iustin Pop | hv_class = hypervisor.GetHypervisor(hv_name) |
1892 | a5728081 | Guido Trotter | utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES) |
1893 | 779c15bb | Iustin Pop | hv_class.CheckParameterSyntax(hv_params) |
1894 | 779c15bb | Iustin Pop | _CheckHVParams(self, node_list, hv_name, hv_params)
|
1895 | 779c15bb | Iustin Pop | |
1896 | 8084f9f6 | Manuel Franceschini | def Exec(self, feedback_fn): |
1897 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
1898 | 8084f9f6 | Manuel Franceschini |
|
1899 | 8084f9f6 | Manuel Franceschini | """
|
1900 | 779c15bb | Iustin Pop | if self.op.vg_name is not None: |
1901 | b2482333 | Guido Trotter | new_volume = self.op.vg_name
|
1902 | b2482333 | Guido Trotter | if not new_volume: |
1903 | b2482333 | Guido Trotter | new_volume = None
|
1904 | b2482333 | Guido Trotter | if new_volume != self.cfg.GetVGName(): |
1905 | b2482333 | Guido Trotter | self.cfg.SetVGName(new_volume)
|
1906 | 779c15bb | Iustin Pop | else:
|
1907 | 779c15bb | Iustin Pop | feedback_fn("Cluster LVM configuration already in desired"
|
1908 | 779c15bb | Iustin Pop | " state, not changing")
|
1909 | 779c15bb | Iustin Pop | if self.op.hvparams: |
1910 | 779c15bb | Iustin Pop | self.cluster.hvparams = self.new_hvparams |
1911 | 779c15bb | Iustin Pop | if self.op.enabled_hypervisors is not None: |
1912 | 779c15bb | Iustin Pop | self.cluster.enabled_hypervisors = self.op.enabled_hypervisors |
1913 | 779c15bb | Iustin Pop | if self.op.beparams: |
1914 | 4ef7f423 | Guido Trotter | self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams |
1915 | 5af3da74 | Guido Trotter | if self.op.nicparams: |
1916 | 5af3da74 | Guido Trotter | self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams |
1917 | 5af3da74 | Guido Trotter | |
1918 | 4b7735f9 | Iustin Pop | if self.op.candidate_pool_size is not None: |
1919 | 4b7735f9 | Iustin Pop | self.cluster.candidate_pool_size = self.op.candidate_pool_size |
1920 | 75e914fb | Iustin Pop | # we need to update the pool size here, otherwise the save will fail
|
1921 | 75e914fb | Iustin Pop | _AdjustCandidatePool(self)
|
1922 | 4b7735f9 | Iustin Pop | |
1923 | 779c15bb | Iustin Pop | self.cfg.Update(self.cluster) |
1924 | 8084f9f6 | Manuel Franceschini | |
1925 | 8084f9f6 | Manuel Franceschini | |
1926 | 28eddce5 | Guido Trotter | def _RedistributeAncillaryFiles(lu, additional_nodes=None): |
1927 | 28eddce5 | Guido Trotter | """Distribute additional files which are part of the cluster configuration.
|
1928 | 28eddce5 | Guido Trotter |
|
1929 | 28eddce5 | Guido Trotter | ConfigWriter takes care of distributing the config and ssconf files, but
|
1930 | 28eddce5 | Guido Trotter | there are more files which should be distributed to all nodes. This function
|
1931 | 28eddce5 | Guido Trotter | makes sure those are copied.
|
1932 | 28eddce5 | Guido Trotter |
|
1933 | 28eddce5 | Guido Trotter | @param lu: calling logical unit
|
1934 | 28eddce5 | Guido Trotter | @param additional_nodes: list of nodes not in the config to distribute to
|
1935 | 28eddce5 | Guido Trotter |
|
1936 | 28eddce5 | Guido Trotter | """
|
1937 | 28eddce5 | Guido Trotter | # 1. Gather target nodes
|
1938 | 28eddce5 | Guido Trotter | myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode()) |
1939 | 28eddce5 | Guido Trotter | dist_nodes = lu.cfg.GetNodeList() |
1940 | 28eddce5 | Guido Trotter | if additional_nodes is not None: |
1941 | 28eddce5 | Guido Trotter | dist_nodes.extend(additional_nodes) |
1942 | 28eddce5 | Guido Trotter | if myself.name in dist_nodes: |
1943 | 28eddce5 | Guido Trotter | dist_nodes.remove(myself.name) |
1944 | 28eddce5 | Guido Trotter | # 2. Gather files to distribute
|
1945 | 28eddce5 | Guido Trotter | dist_files = set([constants.ETC_HOSTS,
|
1946 | 28eddce5 | Guido Trotter | constants.SSH_KNOWN_HOSTS_FILE, |
1947 | 28eddce5 | Guido Trotter | constants.RAPI_CERT_FILE, |
1948 | 28eddce5 | Guido Trotter | constants.RAPI_USERS_FILE, |
1949 | 4a34c5cf | Guido Trotter | constants.HMAC_CLUSTER_KEY, |
1950 | 28eddce5 | Guido Trotter | ]) |
1951 | e1b8653f | Guido Trotter | |
1952 | e1b8653f | Guido Trotter | enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors |
1953 | e1b8653f | Guido Trotter | for hv_name in enabled_hypervisors: |
1954 | e1b8653f | Guido Trotter | hv_class = hypervisor.GetHypervisor(hv_name) |
1955 | e1b8653f | Guido Trotter | dist_files.update(hv_class.GetAncillaryFiles()) |
1956 | e1b8653f | Guido Trotter | |
1957 | 28eddce5 | Guido Trotter | # 3. Perform the files upload
|
1958 | 28eddce5 | Guido Trotter | for fname in dist_files: |
1959 | 28eddce5 | Guido Trotter | if os.path.exists(fname):
|
1960 | 28eddce5 | Guido Trotter | result = lu.rpc.call_upload_file(dist_nodes, fname) |
1961 | 28eddce5 | Guido Trotter | for to_node, to_result in result.items(): |
1962 | 6f7d4e75 | Iustin Pop | msg = to_result.fail_msg |
1963 | 6f7d4e75 | Iustin Pop | if msg:
|
1964 | 6f7d4e75 | Iustin Pop | msg = ("Copy of file %s to node %s failed: %s" %
|
1965 | 6f7d4e75 | Iustin Pop | (fname, to_node, msg)) |
1966 | 6f7d4e75 | Iustin Pop | lu.proc.LogWarning(msg) |
1967 | 28eddce5 | Guido Trotter | |
1968 | 28eddce5 | Guido Trotter | |
1969 | afee0879 | Iustin Pop | class LURedistributeConfig(NoHooksLU): |
1970 | afee0879 | Iustin Pop | """Force the redistribution of cluster configuration.
|
1971 | afee0879 | Iustin Pop |
|
1972 | afee0879 | Iustin Pop | This is a very simple LU.
|
1973 | afee0879 | Iustin Pop |
|
1974 | afee0879 | Iustin Pop | """
|
1975 | afee0879 | Iustin Pop | _OP_REQP = [] |
1976 | afee0879 | Iustin Pop | REQ_BGL = False
|
1977 | afee0879 | Iustin Pop | |
1978 | afee0879 | Iustin Pop | def ExpandNames(self): |
1979 | afee0879 | Iustin Pop | self.needed_locks = {
|
1980 | afee0879 | Iustin Pop | locking.LEVEL_NODE: locking.ALL_SET, |
1981 | afee0879 | Iustin Pop | } |
1982 | afee0879 | Iustin Pop | self.share_locks[locking.LEVEL_NODE] = 1 |
1983 | afee0879 | Iustin Pop | |
1984 | afee0879 | Iustin Pop | def CheckPrereq(self): |
1985 | afee0879 | Iustin Pop | """Check prerequisites.
|
1986 | afee0879 | Iustin Pop |
|
1987 | afee0879 | Iustin Pop | """
|
1988 | afee0879 | Iustin Pop | |
1989 | afee0879 | Iustin Pop | def Exec(self, feedback_fn): |
1990 | afee0879 | Iustin Pop | """Redistribute the configuration.
|
1991 | afee0879 | Iustin Pop |
|
1992 | afee0879 | Iustin Pop | """
|
1993 | afee0879 | Iustin Pop | self.cfg.Update(self.cfg.GetClusterInfo()) |
1994 | 28eddce5 | Guido Trotter | _RedistributeAncillaryFiles(self)
|
1995 | afee0879 | Iustin Pop | |
1996 | afee0879 | Iustin Pop | |
1997 | b9bddb6b | Iustin Pop | def _WaitForSync(lu, instance, oneshot=False, unlock=False): |
1998 | a8083063 | Iustin Pop | """Sleep and poll for an instance's disk to sync.
|
1999 | a8083063 | Iustin Pop |
|
2000 | a8083063 | Iustin Pop | """
|
2001 | a8083063 | Iustin Pop | if not instance.disks: |
2002 | a8083063 | Iustin Pop | return True |
2003 | a8083063 | Iustin Pop | |
2004 | a8083063 | Iustin Pop | if not oneshot: |
2005 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
|
2006 | a8083063 | Iustin Pop | |
2007 | a8083063 | Iustin Pop | node = instance.primary_node |
2008 | a8083063 | Iustin Pop | |
2009 | a8083063 | Iustin Pop | for dev in instance.disks: |
2010 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(dev, node) |
2011 | a8083063 | Iustin Pop | |
2012 | a8083063 | Iustin Pop | retries = 0
|
2013 | fbafd7a8 | Iustin Pop | degr_retries = 10 # in seconds, as we sleep 1 second each time |
2014 | a8083063 | Iustin Pop | while True: |
2015 | a8083063 | Iustin Pop | max_time = 0
|
2016 | a8083063 | Iustin Pop | done = True
|
2017 | a8083063 | Iustin Pop | cumul_degraded = False
|
2018 | 72737a7f | Iustin Pop | rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks) |
2019 | 4c4e4e1e | Iustin Pop | msg = rstats.fail_msg |
2020 | 3efa9051 | Iustin Pop | if msg:
|
2021 | 3efa9051 | Iustin Pop | lu.LogWarning("Can't get any data from node %s: %s", node, msg)
|
2022 | a8083063 | Iustin Pop | retries += 1
|
2023 | a8083063 | Iustin Pop | if retries >= 10: |
2024 | 3ecf6786 | Iustin Pop | raise errors.RemoteError("Can't contact node %s for mirror data," |
2025 | 3ecf6786 | Iustin Pop | " aborting." % node)
|
2026 | a8083063 | Iustin Pop | time.sleep(6)
|
2027 | a8083063 | Iustin Pop | continue
|
2028 | 3efa9051 | Iustin Pop | rstats = rstats.payload |
2029 | a8083063 | Iustin Pop | retries = 0
|
2030 | 1492cca7 | Iustin Pop | for i, mstat in enumerate(rstats): |
2031 | a8083063 | Iustin Pop | if mstat is None: |
2032 | 86d9d3bb | Iustin Pop | lu.LogWarning("Can't compute data for node %s/%s",
|
2033 | 86d9d3bb | Iustin Pop | node, instance.disks[i].iv_name) |
2034 | a8083063 | Iustin Pop | continue
|
2035 | 36145b12 | Michael Hanselmann | |
2036 | 36145b12 | Michael Hanselmann | cumul_degraded = (cumul_degraded or
|
2037 | 36145b12 | Michael Hanselmann | (mstat.is_degraded and mstat.sync_percent is None)) |
2038 | 36145b12 | Michael Hanselmann | if mstat.sync_percent is not None: |
2039 | a8083063 | Iustin Pop | done = False
|
2040 | 36145b12 | Michael Hanselmann | if mstat.estimated_time is not None: |
2041 | 36145b12 | Michael Hanselmann | rem_time = "%d estimated seconds remaining" % mstat.estimated_time
|
2042 | 36145b12 | Michael Hanselmann | max_time = mstat.estimated_time |
2043 | a8083063 | Iustin Pop | else:
|
2044 | a8083063 | Iustin Pop | rem_time = "no time estimate"
|
2045 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
|
2046 | 4d4a651d | Michael Hanselmann | (instance.disks[i].iv_name, mstat.sync_percent, |
2047 | 4d4a651d | Michael Hanselmann | rem_time)) |
2048 | fbafd7a8 | Iustin Pop | |
2049 | fbafd7a8 | Iustin Pop | # if we're done but degraded, let's do a few small retries, to
|
2050 | fbafd7a8 | Iustin Pop | # make sure we see a stable and not transient situation; therefore
|
2051 | fbafd7a8 | Iustin Pop | # we force restart of the loop
|
2052 | fbafd7a8 | Iustin Pop | if (done or oneshot) and cumul_degraded and degr_retries > 0: |
2053 | fbafd7a8 | Iustin Pop | logging.info("Degraded disks found, %d retries left", degr_retries)
|
2054 | fbafd7a8 | Iustin Pop | degr_retries -= 1
|
2055 | fbafd7a8 | Iustin Pop | time.sleep(1)
|
2056 | fbafd7a8 | Iustin Pop | continue
|
2057 | fbafd7a8 | Iustin Pop | |
2058 | a8083063 | Iustin Pop | if done or oneshot: |
2059 | a8083063 | Iustin Pop | break
|
2060 | a8083063 | Iustin Pop | |
2061 | d4fa5c23 | Iustin Pop | time.sleep(min(60, max_time)) |
2062 | a8083063 | Iustin Pop | |
2063 | a8083063 | Iustin Pop | if done:
|
2064 | b9bddb6b | Iustin Pop | lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
|
2065 | a8083063 | Iustin Pop | return not cumul_degraded |
2066 | a8083063 | Iustin Pop | |
2067 | a8083063 | Iustin Pop | |
2068 | b9bddb6b | Iustin Pop | def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False): |
2069 | a8083063 | Iustin Pop | """Check that mirrors are not degraded.
|
2070 | a8083063 | Iustin Pop |
|
2071 | 0834c866 | Iustin Pop | The ldisk parameter, if True, will change the test from the
|
2072 | 0834c866 | Iustin Pop | is_degraded attribute (which represents overall non-ok status for
|
2073 | 0834c866 | Iustin Pop | the device(s)) to the ldisk (representing the local storage status).
|
2074 | 0834c866 | Iustin Pop |
|
2075 | a8083063 | Iustin Pop | """
|
2076 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(dev, node) |
2077 | a8083063 | Iustin Pop | |
2078 | a8083063 | Iustin Pop | result = True
|
2079 | 96acbc09 | Michael Hanselmann | |
2080 | a8083063 | Iustin Pop | if on_primary or dev.AssembleOnSecondary(): |
2081 | 72737a7f | Iustin Pop | rstats = lu.rpc.call_blockdev_find(node, dev) |
2082 | 4c4e4e1e | Iustin Pop | msg = rstats.fail_msg |
2083 | 23829f6f | Iustin Pop | if msg:
|
2084 | 23829f6f | Iustin Pop | lu.LogWarning("Can't find disk on node %s: %s", node, msg)
|
2085 | 23829f6f | Iustin Pop | result = False
|
2086 | 23829f6f | Iustin Pop | elif not rstats.payload: |
2087 | 23829f6f | Iustin Pop | lu.LogWarning("Can't find disk on node %s", node)
|
2088 | a8083063 | Iustin Pop | result = False
|
2089 | a8083063 | Iustin Pop | else:
|
2090 | 96acbc09 | Michael Hanselmann | if ldisk:
|
2091 | f208978a | Michael Hanselmann | result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
|
2092 | 96acbc09 | Michael Hanselmann | else:
|
2093 | 96acbc09 | Michael Hanselmann | result = result and not rstats.payload.is_degraded |
2094 | 96acbc09 | Michael Hanselmann | |
2095 | a8083063 | Iustin Pop | if dev.children:
|
2096 | a8083063 | Iustin Pop | for child in dev.children: |
2097 | b9bddb6b | Iustin Pop | result = result and _CheckDiskConsistency(lu, child, node, on_primary)
|
2098 | a8083063 | Iustin Pop | |
2099 | a8083063 | Iustin Pop | return result
|
2100 | a8083063 | Iustin Pop | |
2101 | a8083063 | Iustin Pop | |
2102 | a8083063 | Iustin Pop | class LUDiagnoseOS(NoHooksLU): |
2103 | a8083063 | Iustin Pop | """Logical unit for OS diagnose/query.
|
2104 | a8083063 | Iustin Pop |
|
2105 | a8083063 | Iustin Pop | """
|
2106 | 1f9430d6 | Iustin Pop | _OP_REQP = ["output_fields", "names"] |
2107 | 6bf01bbb | Guido Trotter | REQ_BGL = False
|
2108 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet() |
2109 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status") |
2110 | a8083063 | Iustin Pop | |
2111 | 6bf01bbb | Guido Trotter | def ExpandNames(self): |
2112 | 1f9430d6 | Iustin Pop | if self.op.names: |
2113 | 1f9430d6 | Iustin Pop | raise errors.OpPrereqError("Selective OS query not supported") |
2114 | 1f9430d6 | Iustin Pop | |
2115 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2116 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
2117 | 1f9430d6 | Iustin Pop | selected=self.op.output_fields)
|
2118 | 1f9430d6 | Iustin Pop | |
2119 | 6bf01bbb | Guido Trotter | # Lock all nodes, in shared mode
|
2120 | a6ab004b | Iustin Pop | # Temporary removal of locks, should be reverted later
|
2121 | a6ab004b | Iustin Pop | # TODO: reintroduce locks when they are lighter-weight
|
2122 | 6bf01bbb | Guido Trotter | self.needed_locks = {}
|
2123 | a6ab004b | Iustin Pop | #self.share_locks[locking.LEVEL_NODE] = 1
|
2124 | a6ab004b | Iustin Pop | #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
2125 | 6bf01bbb | Guido Trotter | |
2126 | 6bf01bbb | Guido Trotter | def CheckPrereq(self): |
2127 | 6bf01bbb | Guido Trotter | """Check prerequisites.
|
2128 | 6bf01bbb | Guido Trotter |
|
2129 | 6bf01bbb | Guido Trotter | """
|
2130 | 6bf01bbb | Guido Trotter | |
2131 | 1f9430d6 | Iustin Pop | @staticmethod
|
2132 | 1f9430d6 | Iustin Pop | def _DiagnoseByOS(node_list, rlist): |
2133 | 1f9430d6 | Iustin Pop | """Remaps a per-node return list into an a per-os per-node dictionary
|
2134 | 1f9430d6 | Iustin Pop |
|
2135 | e4376078 | Iustin Pop | @param node_list: a list with the names of all nodes
|
2136 | e4376078 | Iustin Pop | @param rlist: a map with node names as keys and OS objects as values
|
2137 | 1f9430d6 | Iustin Pop |
|
2138 | e4376078 | Iustin Pop | @rtype: dict
|
2139 | 5fcc718f | Iustin Pop | @return: a dictionary with osnames as keys and as value another map, with
|
2140 | 255dcebd | Iustin Pop | nodes as keys and tuples of (path, status, diagnose) as values, eg::
|
2141 | e4376078 | Iustin Pop |
|
2142 | 255dcebd | Iustin Pop | {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
|
2143 | 255dcebd | Iustin Pop | (/srv/..., False, "invalid api")],
|
2144 | 255dcebd | Iustin Pop | "node2": [(/srv/..., True, "")]}
|
2145 | e4376078 | Iustin Pop | }
|
2146 | 1f9430d6 | Iustin Pop |
|
2147 | 1f9430d6 | Iustin Pop | """
|
2148 | 1f9430d6 | Iustin Pop | all_os = {} |
2149 | a6ab004b | Iustin Pop | # we build here the list of nodes that didn't fail the RPC (at RPC
|
2150 | a6ab004b | Iustin Pop | # level), so that nodes with a non-responding node daemon don't
|
2151 | a6ab004b | Iustin Pop | # make all OSes invalid
|
2152 | a6ab004b | Iustin Pop | good_nodes = [node_name for node_name in rlist |
2153 | 4c4e4e1e | Iustin Pop | if not rlist[node_name].fail_msg] |
2154 | 83d92ad8 | Iustin Pop | for node_name, nr in rlist.items(): |
2155 | 4c4e4e1e | Iustin Pop | if nr.fail_msg or not nr.payload: |
2156 | 1f9430d6 | Iustin Pop | continue
|
2157 | 255dcebd | Iustin Pop | for name, path, status, diagnose in nr.payload: |
2158 | 255dcebd | Iustin Pop | if name not in all_os: |
2159 | 1f9430d6 | Iustin Pop | # build a list of nodes for this os containing empty lists
|
2160 | 1f9430d6 | Iustin Pop | # for each node in node_list
|
2161 | 255dcebd | Iustin Pop | all_os[name] = {} |
2162 | a6ab004b | Iustin Pop | for nname in good_nodes: |
2163 | 255dcebd | Iustin Pop | all_os[name][nname] = [] |
2164 | 255dcebd | Iustin Pop | all_os[name][node_name].append((path, status, diagnose)) |
2165 | 1f9430d6 | Iustin Pop | return all_os
|
2166 | a8083063 | Iustin Pop | |
2167 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2168 | a8083063 | Iustin Pop | """Compute the list of OSes.
|
2169 | a8083063 | Iustin Pop |
|
2170 | a8083063 | Iustin Pop | """
|
2171 | a6ab004b | Iustin Pop | valid_nodes = [node for node in self.cfg.GetOnlineNodeList()] |
2172 | 94a02bb5 | Iustin Pop | node_data = self.rpc.call_os_diagnose(valid_nodes)
|
2173 | 94a02bb5 | Iustin Pop | pol = self._DiagnoseByOS(valid_nodes, node_data)
|
2174 | 1f9430d6 | Iustin Pop | output = [] |
2175 | 83d92ad8 | Iustin Pop | for os_name, os_data in pol.items(): |
2176 | 1f9430d6 | Iustin Pop | row = [] |
2177 | 1f9430d6 | Iustin Pop | for field in self.op.output_fields: |
2178 | 1f9430d6 | Iustin Pop | if field == "name": |
2179 | 1f9430d6 | Iustin Pop | val = os_name |
2180 | 1f9430d6 | Iustin Pop | elif field == "valid": |
2181 | 255dcebd | Iustin Pop | val = utils.all([osl and osl[0][1] for osl in os_data.values()]) |
2182 | 1f9430d6 | Iustin Pop | elif field == "node_status": |
2183 | 255dcebd | Iustin Pop | # this is just a copy of the dict
|
2184 | 1f9430d6 | Iustin Pop | val = {} |
2185 | 255dcebd | Iustin Pop | for node_name, nos_list in os_data.items(): |
2186 | 255dcebd | Iustin Pop | val[node_name] = nos_list |
2187 | 1f9430d6 | Iustin Pop | else:
|
2188 | 1f9430d6 | Iustin Pop | raise errors.ParameterError(field)
|
2189 | 1f9430d6 | Iustin Pop | row.append(val) |
2190 | 1f9430d6 | Iustin Pop | output.append(row) |
2191 | 1f9430d6 | Iustin Pop | |
2192 | 1f9430d6 | Iustin Pop | return output
|
2193 | a8083063 | Iustin Pop | |
2194 | a8083063 | Iustin Pop | |
2195 | a8083063 | Iustin Pop | class LURemoveNode(LogicalUnit): |
2196 | a8083063 | Iustin Pop | """Logical unit for removing a node.
|
2197 | a8083063 | Iustin Pop |
|
2198 | a8083063 | Iustin Pop | """
|
2199 | a8083063 | Iustin Pop | HPATH = "node-remove"
|
2200 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
2201 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
2202 | a8083063 | Iustin Pop | |
2203 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2204 | a8083063 | Iustin Pop | """Build hooks env.
|
2205 | a8083063 | Iustin Pop |
|
2206 | a8083063 | Iustin Pop | This doesn't run on the target node in the pre phase as a failed
|
2207 | d08869ee | Guido Trotter | node would then be impossible to remove.
|
2208 | a8083063 | Iustin Pop |
|
2209 | a8083063 | Iustin Pop | """
|
2210 | 396e1b78 | Michael Hanselmann | env = { |
2211 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
2212 | 396e1b78 | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
2213 | 396e1b78 | Michael Hanselmann | } |
2214 | a8083063 | Iustin Pop | all_nodes = self.cfg.GetNodeList()
|
2215 | cd46f3b4 | Luca Bigliardi | if self.op.node_name in all_nodes: |
2216 | cd46f3b4 | Luca Bigliardi | all_nodes.remove(self.op.node_name)
|
2217 | 396e1b78 | Michael Hanselmann | return env, all_nodes, all_nodes
|
2218 | a8083063 | Iustin Pop | |
2219 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2220 | a8083063 | Iustin Pop | """Check prerequisites.
|
2221 | a8083063 | Iustin Pop |
|
2222 | a8083063 | Iustin Pop | This checks:
|
2223 | a8083063 | Iustin Pop | - the node exists in the configuration
|
2224 | a8083063 | Iustin Pop | - it does not have primary or secondary instances
|
2225 | a8083063 | Iustin Pop | - it's not the master
|
2226 | a8083063 | Iustin Pop |
|
2227 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
2228 | a8083063 | Iustin Pop |
|
2229 | a8083063 | Iustin Pop | """
|
2230 | a8083063 | Iustin Pop | node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name)) |
2231 | a8083063 | Iustin Pop | if node is None: |
2232 | a02bc76e | Iustin Pop | raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name) |
2233 | a8083063 | Iustin Pop | |
2234 | a8083063 | Iustin Pop | instance_list = self.cfg.GetInstanceList()
|
2235 | a8083063 | Iustin Pop | |
2236 | d6a02168 | Michael Hanselmann | masternode = self.cfg.GetMasterNode()
|
2237 | a8083063 | Iustin Pop | if node.name == masternode:
|
2238 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node is the master node," |
2239 | 3ecf6786 | Iustin Pop | " you need to failover first.")
|
2240 | a8083063 | Iustin Pop | |
2241 | a8083063 | Iustin Pop | for instance_name in instance_list: |
2242 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(instance_name)
|
2243 | 6b12959c | Iustin Pop | if node.name in instance.all_nodes: |
2244 | 6b12959c | Iustin Pop | raise errors.OpPrereqError("Instance %s is still running on the node," |
2245 | 3ecf6786 | Iustin Pop | " please remove first." % instance_name)
|
2246 | a8083063 | Iustin Pop | self.op.node_name = node.name
|
2247 | a8083063 | Iustin Pop | self.node = node
|
2248 | a8083063 | Iustin Pop | |
2249 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2250 | a8083063 | Iustin Pop | """Removes the node from the cluster.
|
2251 | a8083063 | Iustin Pop |
|
2252 | a8083063 | Iustin Pop | """
|
2253 | a8083063 | Iustin Pop | node = self.node
|
2254 | 9a4f63d1 | Iustin Pop | logging.info("Stopping the node daemon and removing configs from node %s",
|
2255 | 9a4f63d1 | Iustin Pop | node.name) |
2256 | a8083063 | Iustin Pop | |
2257 | d8470559 | Michael Hanselmann | self.context.RemoveNode(node.name)
|
2258 | a8083063 | Iustin Pop | |
2259 | cd46f3b4 | Luca Bigliardi | # Run post hooks on the node before it's removed
|
2260 | cd46f3b4 | Luca Bigliardi | hm = self.proc.hmclass(self.rpc.call_hooks_runner, self) |
2261 | cd46f3b4 | Luca Bigliardi | try:
|
2262 | cd46f3b4 | Luca Bigliardi | h_results = hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name]) |
2263 | 3cb5c1e3 | Luca Bigliardi | except:
|
2264 | 3cb5c1e3 | Luca Bigliardi | self.LogWarning("Errors occurred running hooks on %s" % node.name) |
2265 | cd46f3b4 | Luca Bigliardi | |
2266 | 0623d351 | Iustin Pop | result = self.rpc.call_node_leave_cluster(node.name)
|
2267 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
2268 | 0623d351 | Iustin Pop | if msg:
|
2269 | 0623d351 | Iustin Pop | self.LogWarning("Errors encountered on the remote node while leaving" |
2270 | 0623d351 | Iustin Pop | " the cluster: %s", msg)
|
2271 | c8a0948f | Michael Hanselmann | |
2272 | eb1742d5 | Guido Trotter | # Promote nodes to master candidate as needed
|
2273 | ec0292f1 | Iustin Pop | _AdjustCandidatePool(self)
|
2274 | eb1742d5 | Guido Trotter | |
2275 | a8083063 | Iustin Pop | |
2276 | a8083063 | Iustin Pop | class LUQueryNodes(NoHooksLU): |
2277 | a8083063 | Iustin Pop | """Logical unit for querying nodes.
|
2278 | a8083063 | Iustin Pop |
|
2279 | a8083063 | Iustin Pop | """
|
2280 | bc8e4a1a | Iustin Pop | _OP_REQP = ["output_fields", "names", "use_locking"] |
2281 | 35705d8f | Guido Trotter | REQ_BGL = False
|
2282 | 19bed813 | Iustin Pop | |
2283 | 19bed813 | Iustin Pop | _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid", |
2284 | 19bed813 | Iustin Pop | "master_candidate", "offline", "drained"] |
2285 | 19bed813 | Iustin Pop | |
2286 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet( |
2287 | 31bf511f | Iustin Pop | "dtotal", "dfree", |
2288 | 31bf511f | Iustin Pop | "mtotal", "mnode", "mfree", |
2289 | 31bf511f | Iustin Pop | "bootid",
|
2290 | 0105bad3 | Iustin Pop | "ctotal", "cnodes", "csockets", |
2291 | 31bf511f | Iustin Pop | ) |
2292 | 31bf511f | Iustin Pop | |
2293 | 19bed813 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet(*[ |
2294 | 19bed813 | Iustin Pop | "pinst_cnt", "sinst_cnt", |
2295 | 31bf511f | Iustin Pop | "pinst_list", "sinst_list", |
2296 | 31bf511f | Iustin Pop | "pip", "sip", "tags", |
2297 | 0e67cdbe | Iustin Pop | "master",
|
2298 | 19bed813 | Iustin Pop | "role"] + _SIMPLE_FIELDS
|
2299 | 31bf511f | Iustin Pop | ) |
2300 | a8083063 | Iustin Pop | |
2301 | 35705d8f | Guido Trotter | def ExpandNames(self): |
2302 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2303 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
2304 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
2305 | a8083063 | Iustin Pop | |
2306 | 35705d8f | Guido Trotter | self.needed_locks = {}
|
2307 | 35705d8f | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
2308 | c8d8b4c8 | Iustin Pop | |
2309 | c8d8b4c8 | Iustin Pop | if self.op.names: |
2310 | c8d8b4c8 | Iustin Pop | self.wanted = _GetWantedNodes(self, self.op.names) |
2311 | 35705d8f | Guido Trotter | else:
|
2312 | c8d8b4c8 | Iustin Pop | self.wanted = locking.ALL_SET
|
2313 | c8d8b4c8 | Iustin Pop | |
2314 | bc8e4a1a | Iustin Pop | self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields) |
2315 | bc8e4a1a | Iustin Pop | self.do_locking = self.do_node_query and self.op.use_locking |
2316 | c8d8b4c8 | Iustin Pop | if self.do_locking: |
2317 | c8d8b4c8 | Iustin Pop | # if we don't request only static fields, we need to lock the nodes
|
2318 | c8d8b4c8 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = self.wanted |
2319 | c8d8b4c8 | Iustin Pop | |
2320 | 35705d8f | Guido Trotter | |
2321 | 35705d8f | Guido Trotter | def CheckPrereq(self): |
2322 | 35705d8f | Guido Trotter | """Check prerequisites.
|
2323 | 35705d8f | Guido Trotter |
|
2324 | 35705d8f | Guido Trotter | """
|
2325 | c8d8b4c8 | Iustin Pop | # The validation of the node list is done in the _GetWantedNodes,
|
2326 | c8d8b4c8 | Iustin Pop | # if non empty, and if empty, there's no validation to do
|
2327 | c8d8b4c8 | Iustin Pop | pass
|
2328 | a8083063 | Iustin Pop | |
2329 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2330 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
2331 | a8083063 | Iustin Pop |
|
2332 | a8083063 | Iustin Pop | """
|
2333 | c8d8b4c8 | Iustin Pop | all_info = self.cfg.GetAllNodesInfo()
|
2334 | c8d8b4c8 | Iustin Pop | if self.do_locking: |
2335 | c8d8b4c8 | Iustin Pop | nodenames = self.acquired_locks[locking.LEVEL_NODE]
|
2336 | 3fa93523 | Guido Trotter | elif self.wanted != locking.ALL_SET: |
2337 | 3fa93523 | Guido Trotter | nodenames = self.wanted
|
2338 | 3fa93523 | Guido Trotter | missing = set(nodenames).difference(all_info.keys())
|
2339 | 3fa93523 | Guido Trotter | if missing:
|
2340 | 7b3a8fb5 | Iustin Pop | raise errors.OpExecError(
|
2341 | 3fa93523 | Guido Trotter | "Some nodes were removed before retrieving their data: %s" % missing)
|
2342 | c8d8b4c8 | Iustin Pop | else:
|
2343 | c8d8b4c8 | Iustin Pop | nodenames = all_info.keys() |
2344 | c1f1cbb2 | Iustin Pop | |
2345 | c1f1cbb2 | Iustin Pop | nodenames = utils.NiceSort(nodenames) |
2346 | c8d8b4c8 | Iustin Pop | nodelist = [all_info[name] for name in nodenames] |
2347 | a8083063 | Iustin Pop | |
2348 | a8083063 | Iustin Pop | # begin data gathering
|
2349 | a8083063 | Iustin Pop | |
2350 | bc8e4a1a | Iustin Pop | if self.do_node_query: |
2351 | a8083063 | Iustin Pop | live_data = {} |
2352 | 72737a7f | Iustin Pop | node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(), |
2353 | 72737a7f | Iustin Pop | self.cfg.GetHypervisorType())
|
2354 | a8083063 | Iustin Pop | for name in nodenames: |
2355 | 781de953 | Iustin Pop | nodeinfo = node_data[name] |
2356 | 4c4e4e1e | Iustin Pop | if not nodeinfo.fail_msg and nodeinfo.payload: |
2357 | 070e998b | Iustin Pop | nodeinfo = nodeinfo.payload |
2358 | d599d686 | Iustin Pop | fn = utils.TryConvert |
2359 | a8083063 | Iustin Pop | live_data[name] = { |
2360 | d599d686 | Iustin Pop | "mtotal": fn(int, nodeinfo.get('memory_total', None)), |
2361 | d599d686 | Iustin Pop | "mnode": fn(int, nodeinfo.get('memory_dom0', None)), |
2362 | d599d686 | Iustin Pop | "mfree": fn(int, nodeinfo.get('memory_free', None)), |
2363 | d599d686 | Iustin Pop | "dtotal": fn(int, nodeinfo.get('vg_size', None)), |
2364 | d599d686 | Iustin Pop | "dfree": fn(int, nodeinfo.get('vg_free', None)), |
2365 | d599d686 | Iustin Pop | "ctotal": fn(int, nodeinfo.get('cpu_total', None)), |
2366 | d599d686 | Iustin Pop | "bootid": nodeinfo.get('bootid', None), |
2367 | 0105bad3 | Iustin Pop | "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)), |
2368 | 0105bad3 | Iustin Pop | "csockets": fn(int, nodeinfo.get('cpu_sockets', None)), |
2369 | a8083063 | Iustin Pop | } |
2370 | a8083063 | Iustin Pop | else:
|
2371 | a8083063 | Iustin Pop | live_data[name] = {} |
2372 | a8083063 | Iustin Pop | else:
|
2373 | a8083063 | Iustin Pop | live_data = dict.fromkeys(nodenames, {})
|
2374 | a8083063 | Iustin Pop | |
2375 | ec223efb | Iustin Pop | node_to_primary = dict([(name, set()) for name in nodenames]) |
2376 | ec223efb | Iustin Pop | node_to_secondary = dict([(name, set()) for name in nodenames]) |
2377 | a8083063 | Iustin Pop | |
2378 | ec223efb | Iustin Pop | inst_fields = frozenset(("pinst_cnt", "pinst_list", |
2379 | ec223efb | Iustin Pop | "sinst_cnt", "sinst_list")) |
2380 | ec223efb | Iustin Pop | if inst_fields & frozenset(self.op.output_fields): |
2381 | a8083063 | Iustin Pop | instancelist = self.cfg.GetInstanceList()
|
2382 | a8083063 | Iustin Pop | |
2383 | ec223efb | Iustin Pop | for instance_name in instancelist: |
2384 | ec223efb | Iustin Pop | inst = self.cfg.GetInstanceInfo(instance_name)
|
2385 | ec223efb | Iustin Pop | if inst.primary_node in node_to_primary: |
2386 | ec223efb | Iustin Pop | node_to_primary[inst.primary_node].add(inst.name) |
2387 | ec223efb | Iustin Pop | for secnode in inst.secondary_nodes: |
2388 | ec223efb | Iustin Pop | if secnode in node_to_secondary: |
2389 | ec223efb | Iustin Pop | node_to_secondary[secnode].add(inst.name) |
2390 | a8083063 | Iustin Pop | |
2391 | 0e67cdbe | Iustin Pop | master_node = self.cfg.GetMasterNode()
|
2392 | 0e67cdbe | Iustin Pop | |
2393 | a8083063 | Iustin Pop | # end data gathering
|
2394 | a8083063 | Iustin Pop | |
2395 | a8083063 | Iustin Pop | output = [] |
2396 | a8083063 | Iustin Pop | for node in nodelist: |
2397 | a8083063 | Iustin Pop | node_output = [] |
2398 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
2399 | 19bed813 | Iustin Pop | if field in self._SIMPLE_FIELDS: |
2400 | 19bed813 | Iustin Pop | val = getattr(node, field)
|
2401 | ec223efb | Iustin Pop | elif field == "pinst_list": |
2402 | ec223efb | Iustin Pop | val = list(node_to_primary[node.name])
|
2403 | ec223efb | Iustin Pop | elif field == "sinst_list": |
2404 | ec223efb | Iustin Pop | val = list(node_to_secondary[node.name])
|
2405 | ec223efb | Iustin Pop | elif field == "pinst_cnt": |
2406 | ec223efb | Iustin Pop | val = len(node_to_primary[node.name])
|
2407 | ec223efb | Iustin Pop | elif field == "sinst_cnt": |
2408 | ec223efb | Iustin Pop | val = len(node_to_secondary[node.name])
|
2409 | a8083063 | Iustin Pop | elif field == "pip": |
2410 | a8083063 | Iustin Pop | val = node.primary_ip |
2411 | a8083063 | Iustin Pop | elif field == "sip": |
2412 | a8083063 | Iustin Pop | val = node.secondary_ip |
2413 | 130a6a6f | Iustin Pop | elif field == "tags": |
2414 | 130a6a6f | Iustin Pop | val = list(node.GetTags())
|
2415 | 0e67cdbe | Iustin Pop | elif field == "master": |
2416 | 0e67cdbe | Iustin Pop | val = node.name == master_node |
2417 | 31bf511f | Iustin Pop | elif self._FIELDS_DYNAMIC.Matches(field): |
2418 | ec223efb | Iustin Pop | val = live_data[node.name].get(field, None)
|
2419 | c120ff34 | Iustin Pop | elif field == "role": |
2420 | c120ff34 | Iustin Pop | if node.name == master_node:
|
2421 | c120ff34 | Iustin Pop | val = "M"
|
2422 | c120ff34 | Iustin Pop | elif node.master_candidate:
|
2423 | c120ff34 | Iustin Pop | val = "C"
|
2424 | c120ff34 | Iustin Pop | elif node.drained:
|
2425 | c120ff34 | Iustin Pop | val = "D"
|
2426 | c120ff34 | Iustin Pop | elif node.offline:
|
2427 | c120ff34 | Iustin Pop | val = "O"
|
2428 | c120ff34 | Iustin Pop | else:
|
2429 | c120ff34 | Iustin Pop | val = "R"
|
2430 | a8083063 | Iustin Pop | else:
|
2431 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
2432 | a8083063 | Iustin Pop | node_output.append(val) |
2433 | a8083063 | Iustin Pop | output.append(node_output) |
2434 | a8083063 | Iustin Pop | |
2435 | a8083063 | Iustin Pop | return output
|
2436 | a8083063 | Iustin Pop | |
2437 | a8083063 | Iustin Pop | |
2438 | dcb93971 | Michael Hanselmann | class LUQueryNodeVolumes(NoHooksLU): |
2439 | dcb93971 | Michael Hanselmann | """Logical unit for getting volumes on node(s).
|
2440 | dcb93971 | Michael Hanselmann |
|
2441 | dcb93971 | Michael Hanselmann | """
|
2442 | dcb93971 | Michael Hanselmann | _OP_REQP = ["nodes", "output_fields"] |
2443 | 21a15682 | Guido Trotter | REQ_BGL = False
|
2444 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance") |
2445 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet("node")
|
2446 | 21a15682 | Guido Trotter | |
2447 | 21a15682 | Guido Trotter | def ExpandNames(self): |
2448 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2449 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
2450 | 21a15682 | Guido Trotter | selected=self.op.output_fields)
|
2451 | 21a15682 | Guido Trotter | |
2452 | 21a15682 | Guido Trotter | self.needed_locks = {}
|
2453 | 21a15682 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
2454 | 21a15682 | Guido Trotter | if not self.op.nodes: |
2455 | e310b019 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
2456 | 21a15682 | Guido Trotter | else:
|
2457 | 21a15682 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = \
|
2458 | 21a15682 | Guido Trotter | _GetWantedNodes(self, self.op.nodes) |
2459 | dcb93971 | Michael Hanselmann | |
2460 | dcb93971 | Michael Hanselmann | def CheckPrereq(self): |
2461 | dcb93971 | Michael Hanselmann | """Check prerequisites.
|
2462 | dcb93971 | Michael Hanselmann |
|
2463 | dcb93971 | Michael Hanselmann | This checks that the fields required are valid output fields.
|
2464 | dcb93971 | Michael Hanselmann |
|
2465 | dcb93971 | Michael Hanselmann | """
|
2466 | 21a15682 | Guido Trotter | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
2467 | dcb93971 | Michael Hanselmann | |
2468 | dcb93971 | Michael Hanselmann | def Exec(self, feedback_fn): |
2469 | dcb93971 | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
2470 | dcb93971 | Michael Hanselmann |
|
2471 | dcb93971 | Michael Hanselmann | """
|
2472 | a7ba5e53 | Iustin Pop | nodenames = self.nodes
|
2473 | 72737a7f | Iustin Pop | volumes = self.rpc.call_node_volumes(nodenames)
|
2474 | dcb93971 | Michael Hanselmann | |
2475 | dcb93971 | Michael Hanselmann | ilist = [self.cfg.GetInstanceInfo(iname) for iname |
2476 | dcb93971 | Michael Hanselmann | in self.cfg.GetInstanceList()] |
2477 | dcb93971 | Michael Hanselmann | |
2478 | dcb93971 | Michael Hanselmann | lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist]) |
2479 | dcb93971 | Michael Hanselmann | |
2480 | dcb93971 | Michael Hanselmann | output = [] |
2481 | dcb93971 | Michael Hanselmann | for node in nodenames: |
2482 | 10bfe6cb | Iustin Pop | nresult = volumes[node] |
2483 | 10bfe6cb | Iustin Pop | if nresult.offline:
|
2484 | 10bfe6cb | Iustin Pop | continue
|
2485 | 4c4e4e1e | Iustin Pop | msg = nresult.fail_msg |
2486 | 10bfe6cb | Iustin Pop | if msg:
|
2487 | 10bfe6cb | Iustin Pop | self.LogWarning("Can't compute volume data on node %s: %s", node, msg) |
2488 | 37d19eb2 | Michael Hanselmann | continue
|
2489 | 37d19eb2 | Michael Hanselmann | |
2490 | 10bfe6cb | Iustin Pop | node_vols = nresult.payload[:] |
2491 | dcb93971 | Michael Hanselmann | node_vols.sort(key=lambda vol: vol['dev']) |
2492 | dcb93971 | Michael Hanselmann | |
2493 | dcb93971 | Michael Hanselmann | for vol in node_vols: |
2494 | dcb93971 | Michael Hanselmann | node_output = [] |
2495 | dcb93971 | Michael Hanselmann | for field in self.op.output_fields: |
2496 | dcb93971 | Michael Hanselmann | if field == "node": |
2497 | dcb93971 | Michael Hanselmann | val = node |
2498 | dcb93971 | Michael Hanselmann | elif field == "phys": |
2499 | dcb93971 | Michael Hanselmann | val = vol['dev']
|
2500 | dcb93971 | Michael Hanselmann | elif field == "vg": |
2501 | dcb93971 | Michael Hanselmann | val = vol['vg']
|
2502 | dcb93971 | Michael Hanselmann | elif field == "name": |
2503 | dcb93971 | Michael Hanselmann | val = vol['name']
|
2504 | dcb93971 | Michael Hanselmann | elif field == "size": |
2505 | dcb93971 | Michael Hanselmann | val = int(float(vol['size'])) |
2506 | dcb93971 | Michael Hanselmann | elif field == "instance": |
2507 | dcb93971 | Michael Hanselmann | for inst in ilist: |
2508 | dcb93971 | Michael Hanselmann | if node not in lv_by_node[inst]: |
2509 | dcb93971 | Michael Hanselmann | continue
|
2510 | dcb93971 | Michael Hanselmann | if vol['name'] in lv_by_node[inst][node]: |
2511 | dcb93971 | Michael Hanselmann | val = inst.name |
2512 | dcb93971 | Michael Hanselmann | break
|
2513 | dcb93971 | Michael Hanselmann | else:
|
2514 | dcb93971 | Michael Hanselmann | val = '-'
|
2515 | dcb93971 | Michael Hanselmann | else:
|
2516 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
2517 | dcb93971 | Michael Hanselmann | node_output.append(str(val))
|
2518 | dcb93971 | Michael Hanselmann | |
2519 | dcb93971 | Michael Hanselmann | output.append(node_output) |
2520 | dcb93971 | Michael Hanselmann | |
2521 | dcb93971 | Michael Hanselmann | return output
|
2522 | dcb93971 | Michael Hanselmann | |
2523 | dcb93971 | Michael Hanselmann | |
2524 | 9e5442ce | Michael Hanselmann | class LUQueryNodeStorage(NoHooksLU): |
2525 | 9e5442ce | Michael Hanselmann | """Logical unit for getting information on storage units on node(s).
|
2526 | 9e5442ce | Michael Hanselmann |
|
2527 | 9e5442ce | Michael Hanselmann | """
|
2528 | 9e5442ce | Michael Hanselmann | _OP_REQP = ["nodes", "storage_type", "output_fields"] |
2529 | 9e5442ce | Michael Hanselmann | REQ_BGL = False
|
2530 | 9e5442ce | Michael Hanselmann | _FIELDS_STATIC = utils.FieldSet("node")
|
2531 | 9e5442ce | Michael Hanselmann | |
2532 | 9e5442ce | Michael Hanselmann | def ExpandNames(self): |
2533 | 9e5442ce | Michael Hanselmann | storage_type = self.op.storage_type
|
2534 | 9e5442ce | Michael Hanselmann | |
2535 | 9e5442ce | Michael Hanselmann | if storage_type not in constants.VALID_STORAGE_FIELDS: |
2536 | 9e5442ce | Michael Hanselmann | raise errors.OpPrereqError("Unknown storage type: %s" % storage_type) |
2537 | 9e5442ce | Michael Hanselmann | |
2538 | 9e5442ce | Michael Hanselmann | dynamic_fields = constants.VALID_STORAGE_FIELDS[storage_type] |
2539 | 9e5442ce | Michael Hanselmann | |
2540 | 9e5442ce | Michael Hanselmann | _CheckOutputFields(static=self._FIELDS_STATIC,
|
2541 | 9e5442ce | Michael Hanselmann | dynamic=utils.FieldSet(*dynamic_fields), |
2542 | 9e5442ce | Michael Hanselmann | selected=self.op.output_fields)
|
2543 | 9e5442ce | Michael Hanselmann | |
2544 | 9e5442ce | Michael Hanselmann | self.needed_locks = {}
|
2545 | 9e5442ce | Michael Hanselmann | self.share_locks[locking.LEVEL_NODE] = 1 |
2546 | 9e5442ce | Michael Hanselmann | |
2547 | 9e5442ce | Michael Hanselmann | if self.op.nodes: |
2548 | 9e5442ce | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = \
|
2549 | 9e5442ce | Michael Hanselmann | _GetWantedNodes(self, self.op.nodes) |
2550 | 9e5442ce | Michael Hanselmann | else:
|
2551 | 9e5442ce | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
2552 | 9e5442ce | Michael Hanselmann | |
2553 | 9e5442ce | Michael Hanselmann | def CheckPrereq(self): |
2554 | 9e5442ce | Michael Hanselmann | """Check prerequisites.
|
2555 | 9e5442ce | Michael Hanselmann |
|
2556 | 9e5442ce | Michael Hanselmann | This checks that the fields required are valid output fields.
|
2557 | 9e5442ce | Michael Hanselmann |
|
2558 | 9e5442ce | Michael Hanselmann | """
|
2559 | 9e5442ce | Michael Hanselmann | self.op.name = getattr(self.op, "name", None) |
2560 | 9e5442ce | Michael Hanselmann | |
2561 | 9e5442ce | Michael Hanselmann | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
2562 | 9e5442ce | Michael Hanselmann | |
2563 | 9e5442ce | Michael Hanselmann | def Exec(self, feedback_fn): |
2564 | 9e5442ce | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
2565 | 9e5442ce | Michael Hanselmann |
|
2566 | 9e5442ce | Michael Hanselmann | """
|
2567 | 9e5442ce | Michael Hanselmann | # Always get name to sort by
|
2568 | 9e5442ce | Michael Hanselmann | if constants.SF_NAME in self.op.output_fields: |
2569 | 9e5442ce | Michael Hanselmann | fields = self.op.output_fields[:]
|
2570 | 9e5442ce | Michael Hanselmann | else:
|
2571 | 9e5442ce | Michael Hanselmann | fields = [constants.SF_NAME] + self.op.output_fields
|
2572 | 9e5442ce | Michael Hanselmann | |
2573 | 9e5442ce | Michael Hanselmann | # Never ask for node as it's only known to the LU
|
2574 | 9e5442ce | Michael Hanselmann | while "node" in fields: |
2575 | 9e5442ce | Michael Hanselmann | fields.remove("node")
|
2576 | 9e5442ce | Michael Hanselmann | |
2577 | 9e5442ce | Michael Hanselmann | field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)]) |
2578 | 9e5442ce | Michael Hanselmann | name_idx = field_idx[constants.SF_NAME] |
2579 | 9e5442ce | Michael Hanselmann | |
2580 | efb8da02 | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
2581 | 9e5442ce | Michael Hanselmann | data = self.rpc.call_storage_list(self.nodes, |
2582 | 9e5442ce | Michael Hanselmann | self.op.storage_type, st_args,
|
2583 | 9e5442ce | Michael Hanselmann | self.op.name, fields)
|
2584 | 9e5442ce | Michael Hanselmann | |
2585 | 9e5442ce | Michael Hanselmann | result = [] |
2586 | 9e5442ce | Michael Hanselmann | |
2587 | 9e5442ce | Michael Hanselmann | for node in utils.NiceSort(self.nodes): |
2588 | 9e5442ce | Michael Hanselmann | nresult = data[node] |
2589 | 9e5442ce | Michael Hanselmann | if nresult.offline:
|
2590 | 9e5442ce | Michael Hanselmann | continue
|
2591 | 9e5442ce | Michael Hanselmann | |
2592 | 9e5442ce | Michael Hanselmann | msg = nresult.fail_msg |
2593 | 9e5442ce | Michael Hanselmann | if msg:
|
2594 | 9e5442ce | Michael Hanselmann | self.LogWarning("Can't get storage data from node %s: %s", node, msg) |
2595 | 9e5442ce | Michael Hanselmann | continue
|
2596 | 9e5442ce | Michael Hanselmann | |
2597 | 9e5442ce | Michael Hanselmann | rows = dict([(row[name_idx], row) for row in nresult.payload]) |
2598 | 9e5442ce | Michael Hanselmann | |
2599 | 9e5442ce | Michael Hanselmann | for name in utils.NiceSort(rows.keys()): |
2600 | 9e5442ce | Michael Hanselmann | row = rows[name] |
2601 | 9e5442ce | Michael Hanselmann | |
2602 | 9e5442ce | Michael Hanselmann | out = [] |
2603 | 9e5442ce | Michael Hanselmann | |
2604 | 9e5442ce | Michael Hanselmann | for field in self.op.output_fields: |
2605 | 9e5442ce | Michael Hanselmann | if field == "node": |
2606 | 9e5442ce | Michael Hanselmann | val = node |
2607 | 9e5442ce | Michael Hanselmann | elif field in field_idx: |
2608 | 9e5442ce | Michael Hanselmann | val = row[field_idx[field]] |
2609 | 9e5442ce | Michael Hanselmann | else:
|
2610 | 9e5442ce | Michael Hanselmann | raise errors.ParameterError(field)
|
2611 | 9e5442ce | Michael Hanselmann | |
2612 | 9e5442ce | Michael Hanselmann | out.append(val) |
2613 | 9e5442ce | Michael Hanselmann | |
2614 | 9e5442ce | Michael Hanselmann | result.append(out) |
2615 | 9e5442ce | Michael Hanselmann | |
2616 | 9e5442ce | Michael Hanselmann | return result
|
2617 | 9e5442ce | Michael Hanselmann | |
2618 | 9e5442ce | Michael Hanselmann | |
2619 | efb8da02 | Michael Hanselmann | class LUModifyNodeStorage(NoHooksLU): |
2620 | efb8da02 | Michael Hanselmann | """Logical unit for modifying a storage volume on a node.
|
2621 | efb8da02 | Michael Hanselmann |
|
2622 | efb8da02 | Michael Hanselmann | """
|
2623 | efb8da02 | Michael Hanselmann | _OP_REQP = ["node_name", "storage_type", "name", "changes"] |
2624 | efb8da02 | Michael Hanselmann | REQ_BGL = False
|
2625 | efb8da02 | Michael Hanselmann | |
2626 | efb8da02 | Michael Hanselmann | def CheckArguments(self): |
2627 | efb8da02 | Michael Hanselmann | node_name = self.cfg.ExpandNodeName(self.op.node_name) |
2628 | efb8da02 | Michael Hanselmann | if node_name is None: |
2629 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name) |
2630 | efb8da02 | Michael Hanselmann | |
2631 | efb8da02 | Michael Hanselmann | self.op.node_name = node_name
|
2632 | efb8da02 | Michael Hanselmann | |
2633 | efb8da02 | Michael Hanselmann | storage_type = self.op.storage_type
|
2634 | efb8da02 | Michael Hanselmann | if storage_type not in constants.VALID_STORAGE_FIELDS: |
2635 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("Unknown storage type: %s" % storage_type) |
2636 | efb8da02 | Michael Hanselmann | |
2637 | efb8da02 | Michael Hanselmann | def ExpandNames(self): |
2638 | efb8da02 | Michael Hanselmann | self.needed_locks = {
|
2639 | efb8da02 | Michael Hanselmann | locking.LEVEL_NODE: self.op.node_name,
|
2640 | efb8da02 | Michael Hanselmann | } |
2641 | efb8da02 | Michael Hanselmann | |
2642 | efb8da02 | Michael Hanselmann | def CheckPrereq(self): |
2643 | efb8da02 | Michael Hanselmann | """Check prerequisites.
|
2644 | efb8da02 | Michael Hanselmann |
|
2645 | efb8da02 | Michael Hanselmann | """
|
2646 | efb8da02 | Michael Hanselmann | storage_type = self.op.storage_type
|
2647 | efb8da02 | Michael Hanselmann | |
2648 | efb8da02 | Michael Hanselmann | try:
|
2649 | efb8da02 | Michael Hanselmann | modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type] |
2650 | efb8da02 | Michael Hanselmann | except KeyError: |
2651 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("Storage units of type '%s' can not be" |
2652 | efb8da02 | Michael Hanselmann | " modified" % storage_type)
|
2653 | efb8da02 | Michael Hanselmann | |
2654 | efb8da02 | Michael Hanselmann | diff = set(self.op.changes.keys()) - modifiable |
2655 | efb8da02 | Michael Hanselmann | if diff:
|
2656 | efb8da02 | Michael Hanselmann | raise errors.OpPrereqError("The following fields can not be modified for" |
2657 | efb8da02 | Michael Hanselmann | " storage units of type '%s': %r" %
|
2658 | efb8da02 | Michael Hanselmann | (storage_type, list(diff)))
|
2659 | efb8da02 | Michael Hanselmann | |
2660 | efb8da02 | Michael Hanselmann | def Exec(self, feedback_fn): |
2661 | efb8da02 | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
2662 | efb8da02 | Michael Hanselmann |
|
2663 | efb8da02 | Michael Hanselmann | """
|
2664 | efb8da02 | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
2665 | efb8da02 | Michael Hanselmann | result = self.rpc.call_storage_modify(self.op.node_name, |
2666 | efb8da02 | Michael Hanselmann | self.op.storage_type, st_args,
|
2667 | efb8da02 | Michael Hanselmann | self.op.name, self.op.changes) |
2668 | efb8da02 | Michael Hanselmann | result.Raise("Failed to modify storage unit '%s' on %s" %
|
2669 | efb8da02 | Michael Hanselmann | (self.op.name, self.op.node_name)) |
2670 | efb8da02 | Michael Hanselmann | |
2671 | efb8da02 | Michael Hanselmann | |
2672 | a8083063 | Iustin Pop | class LUAddNode(LogicalUnit): |
2673 | a8083063 | Iustin Pop | """Logical unit for adding node to the cluster.
|
2674 | a8083063 | Iustin Pop |
|
2675 | a8083063 | Iustin Pop | """
|
2676 | a8083063 | Iustin Pop | HPATH = "node-add"
|
2677 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
2678 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
2679 | a8083063 | Iustin Pop | |
2680 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2681 | a8083063 | Iustin Pop | """Build hooks env.
|
2682 | a8083063 | Iustin Pop |
|
2683 | a8083063 | Iustin Pop | This will run on all nodes before, and on all nodes + the new node after.
|
2684 | a8083063 | Iustin Pop |
|
2685 | a8083063 | Iustin Pop | """
|
2686 | a8083063 | Iustin Pop | env = { |
2687 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
2688 | a8083063 | Iustin Pop | "NODE_NAME": self.op.node_name, |
2689 | a8083063 | Iustin Pop | "NODE_PIP": self.op.primary_ip, |
2690 | a8083063 | Iustin Pop | "NODE_SIP": self.op.secondary_ip, |
2691 | a8083063 | Iustin Pop | } |
2692 | a8083063 | Iustin Pop | nodes_0 = self.cfg.GetNodeList()
|
2693 | a8083063 | Iustin Pop | nodes_1 = nodes_0 + [self.op.node_name, ]
|
2694 | a8083063 | Iustin Pop | return env, nodes_0, nodes_1
|
2695 | a8083063 | Iustin Pop | |
2696 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2697 | a8083063 | Iustin Pop | """Check prerequisites.
|
2698 | a8083063 | Iustin Pop |
|
2699 | a8083063 | Iustin Pop | This checks:
|
2700 | a8083063 | Iustin Pop | - the new node is not already in the config
|
2701 | a8083063 | Iustin Pop | - it is resolvable
|
2702 | a8083063 | Iustin Pop | - its parameters (single/dual homed) matches the cluster
|
2703 | a8083063 | Iustin Pop |
|
2704 | 5bbd3f7f | Michael Hanselmann | Any errors are signaled by raising errors.OpPrereqError.
|
2705 | a8083063 | Iustin Pop |
|
2706 | a8083063 | Iustin Pop | """
|
2707 | a8083063 | Iustin Pop | node_name = self.op.node_name
|
2708 | a8083063 | Iustin Pop | cfg = self.cfg
|
2709 | a8083063 | Iustin Pop | |
2710 | 89e1fc26 | Iustin Pop | dns_data = utils.HostInfo(node_name) |
2711 | a8083063 | Iustin Pop | |
2712 | bcf043c9 | Iustin Pop | node = dns_data.name |
2713 | bcf043c9 | Iustin Pop | primary_ip = self.op.primary_ip = dns_data.ip
|
2714 | a8083063 | Iustin Pop | secondary_ip = getattr(self.op, "secondary_ip", None) |
2715 | a8083063 | Iustin Pop | if secondary_ip is None: |
2716 | a8083063 | Iustin Pop | secondary_ip = primary_ip |
2717 | a8083063 | Iustin Pop | if not utils.IsValidIP(secondary_ip): |
2718 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid secondary IP given") |
2719 | a8083063 | Iustin Pop | self.op.secondary_ip = secondary_ip
|
2720 | e7c6e02b | Michael Hanselmann | |
2721 | a8083063 | Iustin Pop | node_list = cfg.GetNodeList() |
2722 | e7c6e02b | Michael Hanselmann | if not self.op.readd and node in node_list: |
2723 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Node %s is already in the configuration" % |
2724 | e7c6e02b | Michael Hanselmann | node) |
2725 | e7c6e02b | Michael Hanselmann | elif self.op.readd and node not in node_list: |
2726 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Node %s is not in the configuration" % node) |
2727 | a8083063 | Iustin Pop | |
2728 | a8083063 | Iustin Pop | for existing_node_name in node_list: |
2729 | a8083063 | Iustin Pop | existing_node = cfg.GetNodeInfo(existing_node_name) |
2730 | e7c6e02b | Michael Hanselmann | |
2731 | e7c6e02b | Michael Hanselmann | if self.op.readd and node == existing_node_name: |
2732 | e7c6e02b | Michael Hanselmann | if (existing_node.primary_ip != primary_ip or |
2733 | e7c6e02b | Michael Hanselmann | existing_node.secondary_ip != secondary_ip): |
2734 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Readded node doesn't have the same IP" |
2735 | e7c6e02b | Michael Hanselmann | " address configuration as before")
|
2736 | e7c6e02b | Michael Hanselmann | continue
|
2737 | e7c6e02b | Michael Hanselmann | |
2738 | a8083063 | Iustin Pop | if (existing_node.primary_ip == primary_ip or |
2739 | a8083063 | Iustin Pop | existing_node.secondary_ip == primary_ip or
|
2740 | a8083063 | Iustin Pop | existing_node.primary_ip == secondary_ip or
|
2741 | a8083063 | Iustin Pop | existing_node.secondary_ip == secondary_ip): |
2742 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("New node ip address(es) conflict with" |
2743 | 3ecf6786 | Iustin Pop | " existing node %s" % existing_node.name)
|
2744 | a8083063 | Iustin Pop | |
2745 | a8083063 | Iustin Pop | # check that the type of the node (single versus dual homed) is the
|
2746 | a8083063 | Iustin Pop | # same as for the master
|
2747 | d6a02168 | Michael Hanselmann | myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
|
2748 | a8083063 | Iustin Pop | master_singlehomed = myself.secondary_ip == myself.primary_ip |
2749 | a8083063 | Iustin Pop | newbie_singlehomed = secondary_ip == primary_ip |
2750 | a8083063 | Iustin Pop | if master_singlehomed != newbie_singlehomed:
|
2751 | a8083063 | Iustin Pop | if master_singlehomed:
|
2752 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has no private ip but the" |
2753 | 3ecf6786 | Iustin Pop | " new node has one")
|
2754 | a8083063 | Iustin Pop | else:
|
2755 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has a private ip but the" |
2756 | 3ecf6786 | Iustin Pop | " new node doesn't have one")
|
2757 | a8083063 | Iustin Pop | |
2758 | 5bbd3f7f | Michael Hanselmann | # checks reachability
|
2759 | b15d625f | Iustin Pop | if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT): |
2760 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node not reachable by ping") |
2761 | a8083063 | Iustin Pop | |
2762 | a8083063 | Iustin Pop | if not newbie_singlehomed: |
2763 | a8083063 | Iustin Pop | # check reachability from my secondary ip to newbie's secondary ip
|
2764 | b15d625f | Iustin Pop | if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT, |
2765 | b15d625f | Iustin Pop | source=myself.secondary_ip): |
2766 | f4bc1f2c | Michael Hanselmann | raise errors.OpPrereqError("Node secondary ip not reachable by TCP" |
2767 | f4bc1f2c | Michael Hanselmann | " based ping to noded port")
|
2768 | a8083063 | Iustin Pop | |
2769 | 0fff97e9 | Guido Trotter | cp_size = self.cfg.GetClusterInfo().candidate_pool_size
|
2770 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
2771 | a8ae3eb5 | Iustin Pop | exceptions = [node] |
2772 | a8ae3eb5 | Iustin Pop | else:
|
2773 | a8ae3eb5 | Iustin Pop | exceptions = [] |
2774 | a8ae3eb5 | Iustin Pop | mc_now, mc_max = self.cfg.GetMasterCandidateStats(exceptions)
|
2775 | a8ae3eb5 | Iustin Pop | # the new node will increase mc_max with one, so:
|
2776 | a8ae3eb5 | Iustin Pop | mc_max = min(mc_max + 1, cp_size) |
2777 | a8ae3eb5 | Iustin Pop | self.master_candidate = mc_now < mc_max
|
2778 | 0fff97e9 | Guido Trotter | |
2779 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
2780 | a8ae3eb5 | Iustin Pop | self.new_node = self.cfg.GetNodeInfo(node) |
2781 | a8ae3eb5 | Iustin Pop | assert self.new_node is not None, "Can't retrieve locked node %s" % node |
2782 | a8ae3eb5 | Iustin Pop | else:
|
2783 | a8ae3eb5 | Iustin Pop | self.new_node = objects.Node(name=node,
|
2784 | a8ae3eb5 | Iustin Pop | primary_ip=primary_ip, |
2785 | a8ae3eb5 | Iustin Pop | secondary_ip=secondary_ip, |
2786 | a8ae3eb5 | Iustin Pop | master_candidate=self.master_candidate,
|
2787 | a8ae3eb5 | Iustin Pop | offline=False, drained=False) |
2788 | a8083063 | Iustin Pop | |
2789 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2790 | a8083063 | Iustin Pop | """Adds the new node to the cluster.
|
2791 | a8083063 | Iustin Pop |
|
2792 | a8083063 | Iustin Pop | """
|
2793 | a8083063 | Iustin Pop | new_node = self.new_node
|
2794 | a8083063 | Iustin Pop | node = new_node.name |
2795 | a8083063 | Iustin Pop | |
2796 | a8ae3eb5 | Iustin Pop | # for re-adds, reset the offline/drained/master-candidate flags;
|
2797 | a8ae3eb5 | Iustin Pop | # we need to reset here, otherwise offline would prevent RPC calls
|
2798 | a8ae3eb5 | Iustin Pop | # later in the procedure; this also means that if the re-add
|
2799 | a8ae3eb5 | Iustin Pop | # fails, we are left with a non-offlined, broken node
|
2800 | a8ae3eb5 | Iustin Pop | if self.op.readd: |
2801 | a8ae3eb5 | Iustin Pop | new_node.drained = new_node.offline = False
|
2802 | a8ae3eb5 | Iustin Pop | self.LogInfo("Readding a node, the offline/drained flags were reset") |
2803 | a8ae3eb5 | Iustin Pop | # if we demote the node, we do cleanup later in the procedure
|
2804 | a8ae3eb5 | Iustin Pop | new_node.master_candidate = self.master_candidate
|
2805 | a8ae3eb5 | Iustin Pop | |
2806 | a8ae3eb5 | Iustin Pop | # notify the user about any possible mc promotion
|
2807 | a8ae3eb5 | Iustin Pop | if new_node.master_candidate:
|
2808 | a8ae3eb5 | Iustin Pop | self.LogInfo("Node will be a master candidate") |
2809 | a8ae3eb5 | Iustin Pop | |
2810 | a8083063 | Iustin Pop | # check connectivity
|
2811 | 72737a7f | Iustin Pop | result = self.rpc.call_version([node])[node]
|
2812 | 4c4e4e1e | Iustin Pop | result.Raise("Can't get version information from node %s" % node)
|
2813 | 90b54c26 | Iustin Pop | if constants.PROTOCOL_VERSION == result.payload:
|
2814 | 90b54c26 | Iustin Pop | logging.info("Communication to node %s fine, sw version %s match",
|
2815 | 90b54c26 | Iustin Pop | node, result.payload) |
2816 | a8083063 | Iustin Pop | else:
|
2817 | 90b54c26 | Iustin Pop | raise errors.OpExecError("Version mismatch master version %s," |
2818 | 90b54c26 | Iustin Pop | " node version %s" %
|
2819 | 90b54c26 | Iustin Pop | (constants.PROTOCOL_VERSION, result.payload)) |
2820 | a8083063 | Iustin Pop | |
2821 | a8083063 | Iustin Pop | # setup ssh on node
|
2822 | 9a4f63d1 | Iustin Pop | logging.info("Copy ssh key to node %s", node)
|
2823 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
2824 | a8083063 | Iustin Pop | keyarray = [] |
2825 | 70d9e3d8 | Iustin Pop | keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB, |
2826 | 70d9e3d8 | Iustin Pop | constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB, |
2827 | 70d9e3d8 | Iustin Pop | priv_key, pub_key] |
2828 | a8083063 | Iustin Pop | |
2829 | a8083063 | Iustin Pop | for i in keyfiles: |
2830 | 13998ef2 | Michael Hanselmann | keyarray.append(utils.ReadFile(i)) |
2831 | a8083063 | Iustin Pop | |
2832 | 72737a7f | Iustin Pop | result = self.rpc.call_node_add(node, keyarray[0], keyarray[1], |
2833 | 72737a7f | Iustin Pop | keyarray[2],
|
2834 | 72737a7f | Iustin Pop | keyarray[3], keyarray[4], keyarray[5]) |
2835 | 4c4e4e1e | Iustin Pop | result.Raise("Cannot transfer ssh keys to the new node")
|
2836 | a8083063 | Iustin Pop | |
2837 | a8083063 | Iustin Pop | # Add node to our /etc/hosts, and add key to known_hosts
|
2838 | b86a6bcd | Guido Trotter | if self.cfg.GetClusterInfo().modify_etc_hosts: |
2839 | b86a6bcd | Guido Trotter | utils.AddHostToEtcHosts(new_node.name) |
2840 | c8a0948f | Michael Hanselmann | |
2841 | a8083063 | Iustin Pop | if new_node.secondary_ip != new_node.primary_ip:
|
2842 | 781de953 | Iustin Pop | result = self.rpc.call_node_has_ip_address(new_node.name,
|
2843 | 781de953 | Iustin Pop | new_node.secondary_ip) |
2844 | 4c4e4e1e | Iustin Pop | result.Raise("Failure checking secondary ip on node %s" % new_node.name,
|
2845 | 4c4e4e1e | Iustin Pop | prereq=True)
|
2846 | c2fc8250 | Iustin Pop | if not result.payload: |
2847 | f4bc1f2c | Michael Hanselmann | raise errors.OpExecError("Node claims it doesn't have the secondary ip" |
2848 | f4bc1f2c | Michael Hanselmann | " you gave (%s). Please fix and re-run this"
|
2849 | f4bc1f2c | Michael Hanselmann | " command." % new_node.secondary_ip)
|
2850 | a8083063 | Iustin Pop | |
2851 | d6a02168 | Michael Hanselmann | node_verify_list = [self.cfg.GetMasterNode()]
|
2852 | 5c0527ed | Guido Trotter | node_verify_param = { |
2853 | f60759f7 | Iustin Pop | constants.NV_NODELIST: [node], |
2854 | 5c0527ed | Guido Trotter | # TODO: do a node-net-test as well?
|
2855 | 5c0527ed | Guido Trotter | } |
2856 | 5c0527ed | Guido Trotter | |
2857 | 72737a7f | Iustin Pop | result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
|
2858 | 72737a7f | Iustin Pop | self.cfg.GetClusterName())
|
2859 | 5c0527ed | Guido Trotter | for verifier in node_verify_list: |
2860 | 4c4e4e1e | Iustin Pop | result[verifier].Raise("Cannot communicate with node %s" % verifier)
|
2861 | f60759f7 | Iustin Pop | nl_payload = result[verifier].payload[constants.NV_NODELIST] |
2862 | 6f68a739 | Iustin Pop | if nl_payload:
|
2863 | 6f68a739 | Iustin Pop | for failed in nl_payload: |
2864 | 5c0527ed | Guido Trotter | feedback_fn("ssh/hostname verification failed %s -> %s" %
|
2865 | 6f68a739 | Iustin Pop | (verifier, nl_payload[failed])) |
2866 | 5c0527ed | Guido Trotter | raise errors.OpExecError("ssh/hostname verification failed.") |
2867 | ff98055b | Iustin Pop | |
2868 | d8470559 | Michael Hanselmann | if self.op.readd: |
2869 | 28eddce5 | Guido Trotter | _RedistributeAncillaryFiles(self)
|
2870 | d8470559 | Michael Hanselmann | self.context.ReaddNode(new_node)
|
2871 | a8ae3eb5 | Iustin Pop | # make sure we redistribute the config
|
2872 | a8ae3eb5 | Iustin Pop | self.cfg.Update(new_node)
|
2873 | a8ae3eb5 | Iustin Pop | # and make sure the new node will not have old files around
|
2874 | a8ae3eb5 | Iustin Pop | if not new_node.master_candidate: |
2875 | a8ae3eb5 | Iustin Pop | result = self.rpc.call_node_demote_from_mc(new_node.name)
|
2876 | 3cebe102 | Michael Hanselmann | msg = result.fail_msg |
2877 | a8ae3eb5 | Iustin Pop | if msg:
|
2878 | a8ae3eb5 | Iustin Pop | self.LogWarning("Node failed to demote itself from master" |
2879 | a8ae3eb5 | Iustin Pop | " candidate status: %s" % msg)
|
2880 | d8470559 | Michael Hanselmann | else:
|
2881 | 035566e3 | Iustin Pop | _RedistributeAncillaryFiles(self, additional_nodes=[node])
|
2882 | d8470559 | Michael Hanselmann | self.context.AddNode(new_node)
|
2883 | a8083063 | Iustin Pop | |
2884 | a8083063 | Iustin Pop | |
2885 | b31c8676 | Iustin Pop | class LUSetNodeParams(LogicalUnit): |
2886 | b31c8676 | Iustin Pop | """Modifies the parameters of a node.
|
2887 | b31c8676 | Iustin Pop |
|
2888 | b31c8676 | Iustin Pop | """
|
2889 | b31c8676 | Iustin Pop | HPATH = "node-modify"
|
2890 | b31c8676 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
2891 | b31c8676 | Iustin Pop | _OP_REQP = ["node_name"]
|
2892 | b31c8676 | Iustin Pop | REQ_BGL = False
|
2893 | b31c8676 | Iustin Pop | |
2894 | b31c8676 | Iustin Pop | def CheckArguments(self): |
2895 | b31c8676 | Iustin Pop | node_name = self.cfg.ExpandNodeName(self.op.node_name) |
2896 | b31c8676 | Iustin Pop | if node_name is None: |
2897 | b31c8676 | Iustin Pop | raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name) |
2898 | b31c8676 | Iustin Pop | self.op.node_name = node_name
|
2899 | 3a5ba66a | Iustin Pop | _CheckBooleanOpField(self.op, 'master_candidate') |
2900 | 3a5ba66a | Iustin Pop | _CheckBooleanOpField(self.op, 'offline') |
2901 | c9d443ea | Iustin Pop | _CheckBooleanOpField(self.op, 'drained') |
2902 | c9d443ea | Iustin Pop | all_mods = [self.op.offline, self.op.master_candidate, self.op.drained] |
2903 | c9d443ea | Iustin Pop | if all_mods.count(None) == 3: |
2904 | b31c8676 | Iustin Pop | raise errors.OpPrereqError("Please pass at least one modification") |
2905 | c9d443ea | Iustin Pop | if all_mods.count(True) > 1: |
2906 | c9d443ea | Iustin Pop | raise errors.OpPrereqError("Can't set the node into more than one" |
2907 | c9d443ea | Iustin Pop | " state at the same time")
|
2908 | b31c8676 | Iustin Pop | |
2909 | b31c8676 | Iustin Pop | def ExpandNames(self): |
2910 | b31c8676 | Iustin Pop | self.needed_locks = {locking.LEVEL_NODE: self.op.node_name} |
2911 | b31c8676 | Iustin Pop | |
2912 | b31c8676 | Iustin Pop | def BuildHooksEnv(self): |
2913 | b31c8676 | Iustin Pop | """Build hooks env.
|
2914 | b31c8676 | Iustin Pop |
|
2915 | b31c8676 | Iustin Pop | This runs on the master node.
|
2916 | b31c8676 | Iustin Pop |
|
2917 | b31c8676 | Iustin Pop | """
|
2918 | b31c8676 | Iustin Pop | env = { |
2919 | b31c8676 | Iustin Pop | "OP_TARGET": self.op.node_name, |
2920 | b31c8676 | Iustin Pop | "MASTER_CANDIDATE": str(self.op.master_candidate), |
2921 | 3a5ba66a | Iustin Pop | "OFFLINE": str(self.op.offline), |
2922 | c9d443ea | Iustin Pop | "DRAINED": str(self.op.drained), |
2923 | b31c8676 | Iustin Pop | } |
2924 | b31c8676 | Iustin Pop | nl = [self.cfg.GetMasterNode(),
|
2925 | b31c8676 | Iustin Pop | self.op.node_name]
|
2926 | b31c8676 | Iustin Pop | return env, nl, nl
|
2927 | b31c8676 | Iustin Pop | |
2928 | b31c8676 | Iustin Pop | def CheckPrereq(self): |
2929 | b31c8676 | Iustin Pop | """Check prerequisites.
|
2930 | b31c8676 | Iustin Pop |
|
2931 | b31c8676 | Iustin Pop | This only checks the instance list against the existing names.
|
2932 | b31c8676 | Iustin Pop |
|
2933 | b31c8676 | Iustin Pop | """
|
2934 | 3a5ba66a | Iustin Pop | node = self.node = self.cfg.GetNodeInfo(self.op.node_name) |
2935 | b31c8676 | Iustin Pop | |
2936 | 97c61d46 | Iustin Pop | if (self.op.master_candidate is not None or |
2937 | 97c61d46 | Iustin Pop | self.op.drained is not None or |
2938 | 97c61d46 | Iustin Pop | self.op.offline is not None): |
2939 | 97c61d46 | Iustin Pop | # we can't change the master's node flags
|
2940 | 97c61d46 | Iustin Pop | if self.op.node_name == self.cfg.GetMasterNode(): |
2941 | 97c61d46 | Iustin Pop | raise errors.OpPrereqError("The master role can be changed" |
2942 | 97c61d46 | Iustin Pop | " only via masterfailover")
|
2943 | 97c61d46 | Iustin Pop | |
2944 | c9d443ea | Iustin Pop | if ((self.op.master_candidate == False or self.op.offline == True or |
2945 | c9d443ea | Iustin Pop | self.op.drained == True) and node.master_candidate): |
2946 | 3e83dd48 | Iustin Pop | cp_size = self.cfg.GetClusterInfo().candidate_pool_size
|
2947 | 3a5ba66a | Iustin Pop | num_candidates, _ = self.cfg.GetMasterCandidateStats()
|
2948 | 3e83dd48 | Iustin Pop | if num_candidates <= cp_size:
|
2949 | 3e83dd48 | Iustin Pop | msg = ("Not enough master candidates (desired"
|
2950 | 3e83dd48 | Iustin Pop | " %d, new value will be %d)" % (cp_size, num_candidates-1)) |
2951 | 3a5ba66a | Iustin Pop | if self.op.force: |
2952 | 3e83dd48 | Iustin Pop | self.LogWarning(msg)
|
2953 | 3e83dd48 | Iustin Pop | else:
|
2954 | 3e83dd48 | Iustin Pop | raise errors.OpPrereqError(msg)
|
2955 | 3e83dd48 | Iustin Pop | |
2956 | c9d443ea | Iustin Pop | if (self.op.master_candidate == True and |
2957 | c9d443ea | Iustin Pop | ((node.offline and not self.op.offline == False) or |
2958 | c9d443ea | Iustin Pop | (node.drained and not self.op.drained == False))): |
2959 | c9d443ea | Iustin Pop | raise errors.OpPrereqError("Node '%s' is offline or drained, can't set" |
2960 | 949bdabe | Iustin Pop | " to master_candidate" % node.name)
|
2961 | 3a5ba66a | Iustin Pop | |
2962 | b31c8676 | Iustin Pop | return
|
2963 | b31c8676 | Iustin Pop | |
2964 | b31c8676 | Iustin Pop | def Exec(self, feedback_fn): |
2965 | b31c8676 | Iustin Pop | """Modifies a node.
|
2966 | b31c8676 | Iustin Pop |
|
2967 | b31c8676 | Iustin Pop | """
|
2968 | 3a5ba66a | Iustin Pop | node = self.node
|
2969 | b31c8676 | Iustin Pop | |
2970 | b31c8676 | Iustin Pop | result = [] |
2971 | c9d443ea | Iustin Pop | changed_mc = False
|
2972 | b31c8676 | Iustin Pop | |
2973 | 3a5ba66a | Iustin Pop | if self.op.offline is not None: |
2974 | 3a5ba66a | Iustin Pop | node.offline = self.op.offline
|
2975 | 3a5ba66a | Iustin Pop | result.append(("offline", str(self.op.offline))) |
2976 | c9d443ea | Iustin Pop | if self.op.offline == True: |
2977 | c9d443ea | Iustin Pop | if node.master_candidate:
|
2978 | c9d443ea | Iustin Pop | node.master_candidate = False
|
2979 | c9d443ea | Iustin Pop | changed_mc = True
|
2980 | c9d443ea | Iustin Pop | result.append(("master_candidate", "auto-demotion due to offline")) |
2981 | c9d443ea | Iustin Pop | if node.drained:
|
2982 | c9d443ea | Iustin Pop | node.drained = False
|
2983 | c9d443ea | Iustin Pop | result.append(("drained", "clear drained status due to offline")) |
2984 | 3a5ba66a | Iustin Pop | |
2985 | b31c8676 | Iustin Pop | if self.op.master_candidate is not None: |
2986 | b31c8676 | Iustin Pop | node.master_candidate = self.op.master_candidate
|
2987 | c9d443ea | Iustin Pop | changed_mc = True
|
2988 | b31c8676 | Iustin Pop | result.append(("master_candidate", str(self.op.master_candidate))) |
2989 | 56aa9fd5 | Iustin Pop | if self.op.master_candidate == False: |
2990 | 56aa9fd5 | Iustin Pop | rrc = self.rpc.call_node_demote_from_mc(node.name)
|
2991 | 4c4e4e1e | Iustin Pop | msg = rrc.fail_msg |
2992 | 0959c824 | Iustin Pop | if msg:
|
2993 | 0959c824 | Iustin Pop | self.LogWarning("Node failed to demote itself: %s" % msg) |
2994 | b31c8676 | Iustin Pop | |
2995 | c9d443ea | Iustin Pop | if self.op.drained is not None: |
2996 | c9d443ea | Iustin Pop | node.drained = self.op.drained
|
2997 | 82e12743 | Iustin Pop | result.append(("drained", str(self.op.drained))) |
2998 | c9d443ea | Iustin Pop | if self.op.drained == True: |
2999 | c9d443ea | Iustin Pop | if node.master_candidate:
|
3000 | c9d443ea | Iustin Pop | node.master_candidate = False
|
3001 | c9d443ea | Iustin Pop | changed_mc = True
|
3002 | c9d443ea | Iustin Pop | result.append(("master_candidate", "auto-demotion due to drain")) |
3003 | dec0d9da | Iustin Pop | rrc = self.rpc.call_node_demote_from_mc(node.name)
|
3004 | 3cebe102 | Michael Hanselmann | msg = rrc.fail_msg |
3005 | dec0d9da | Iustin Pop | if msg:
|
3006 | dec0d9da | Iustin Pop | self.LogWarning("Node failed to demote itself: %s" % msg) |
3007 | c9d443ea | Iustin Pop | if node.offline:
|
3008 | c9d443ea | Iustin Pop | node.offline = False
|
3009 | c9d443ea | Iustin Pop | result.append(("offline", "clear offline status due to drain")) |
3010 | c9d443ea | Iustin Pop | |
3011 | b31c8676 | Iustin Pop | # this will trigger configuration file update, if needed
|
3012 | b31c8676 | Iustin Pop | self.cfg.Update(node)
|
3013 | b31c8676 | Iustin Pop | # this will trigger job queue propagation or cleanup
|
3014 | c9d443ea | Iustin Pop | if changed_mc:
|
3015 | 3a26773f | Iustin Pop | self.context.ReaddNode(node)
|
3016 | b31c8676 | Iustin Pop | |
3017 | b31c8676 | Iustin Pop | return result
|
3018 | b31c8676 | Iustin Pop | |
3019 | b31c8676 | Iustin Pop | |
3020 | f5118ade | Iustin Pop | class LUPowercycleNode(NoHooksLU): |
3021 | f5118ade | Iustin Pop | """Powercycles a node.
|
3022 | f5118ade | Iustin Pop |
|
3023 | f5118ade | Iustin Pop | """
|
3024 | f5118ade | Iustin Pop | _OP_REQP = ["node_name", "force"] |
3025 | f5118ade | Iustin Pop | REQ_BGL = False
|
3026 | f5118ade | Iustin Pop | |
3027 | f5118ade | Iustin Pop | def CheckArguments(self): |
3028 | f5118ade | Iustin Pop | node_name = self.cfg.ExpandNodeName(self.op.node_name) |
3029 | f5118ade | Iustin Pop | if node_name is None: |
3030 | f5118ade | Iustin Pop | raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name) |
3031 | f5118ade | Iustin Pop | self.op.node_name = node_name
|
3032 | f5118ade | Iustin Pop | if node_name == self.cfg.GetMasterNode() and not self.op.force: |
3033 | f5118ade | Iustin Pop | raise errors.OpPrereqError("The node is the master and the force" |
3034 | f5118ade | Iustin Pop | " parameter was not set")
|
3035 | f5118ade | Iustin Pop | |
3036 | f5118ade | Iustin Pop | def ExpandNames(self): |
3037 | f5118ade | Iustin Pop | """Locking for PowercycleNode.
|
3038 | f5118ade | Iustin Pop |
|
3039 | efb8da02 | Michael Hanselmann | This is a last-resort option and shouldn't block on other
|
3040 | f5118ade | Iustin Pop | jobs. Therefore, we grab no locks.
|
3041 | f5118ade | Iustin Pop |
|
3042 | f5118ade | Iustin Pop | """
|
3043 | f5118ade | Iustin Pop | self.needed_locks = {}
|
3044 | f5118ade | Iustin Pop | |
3045 | f5118ade | Iustin Pop | def CheckPrereq(self): |
3046 | f5118ade | Iustin Pop | """Check prerequisites.
|
3047 | f5118ade | Iustin Pop |
|
3048 | f5118ade | Iustin Pop | This LU has no prereqs.
|
3049 | f5118ade | Iustin Pop |
|
3050 | f5118ade | Iustin Pop | """
|
3051 | f5118ade | Iustin Pop | pass
|
3052 | f5118ade | Iustin Pop | |
3053 | f5118ade | Iustin Pop | def Exec(self, feedback_fn): |
3054 | f5118ade | Iustin Pop | """Reboots a node.
|
3055 | f5118ade | Iustin Pop |
|
3056 | f5118ade | Iustin Pop | """
|
3057 | f5118ade | Iustin Pop | result = self.rpc.call_node_powercycle(self.op.node_name, |
3058 | f5118ade | Iustin Pop | self.cfg.GetHypervisorType())
|
3059 | 4c4e4e1e | Iustin Pop | result.Raise("Failed to schedule the reboot")
|
3060 | f5118ade | Iustin Pop | return result.payload
|
3061 | f5118ade | Iustin Pop | |
3062 | f5118ade | Iustin Pop | |
3063 | a8083063 | Iustin Pop | class LUQueryClusterInfo(NoHooksLU): |
3064 | a8083063 | Iustin Pop | """Query cluster configuration.
|
3065 | a8083063 | Iustin Pop |
|
3066 | a8083063 | Iustin Pop | """
|
3067 | a8083063 | Iustin Pop | _OP_REQP = [] |
3068 | 642339cf | Guido Trotter | REQ_BGL = False
|
3069 | 642339cf | Guido Trotter | |
3070 | 642339cf | Guido Trotter | def ExpandNames(self): |
3071 | 642339cf | Guido Trotter | self.needed_locks = {}
|
3072 | a8083063 | Iustin Pop | |
3073 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3074 | a8083063 | Iustin Pop | """No prerequsites needed for this LU.
|
3075 | a8083063 | Iustin Pop |
|
3076 | a8083063 | Iustin Pop | """
|
3077 | a8083063 | Iustin Pop | pass
|
3078 | a8083063 | Iustin Pop | |
3079 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3080 | a8083063 | Iustin Pop | """Return cluster config.
|
3081 | a8083063 | Iustin Pop |
|
3082 | a8083063 | Iustin Pop | """
|
3083 | 469f88e1 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
3084 | a8083063 | Iustin Pop | result = { |
3085 | a8083063 | Iustin Pop | "software_version": constants.RELEASE_VERSION,
|
3086 | a8083063 | Iustin Pop | "protocol_version": constants.PROTOCOL_VERSION,
|
3087 | a8083063 | Iustin Pop | "config_version": constants.CONFIG_VERSION,
|
3088 | d1a7d66f | Guido Trotter | "os_api_version": max(constants.OS_API_VERSIONS), |
3089 | a8083063 | Iustin Pop | "export_version": constants.EXPORT_VERSION,
|
3090 | a8083063 | Iustin Pop | "architecture": (platform.architecture()[0], platform.machine()), |
3091 | 469f88e1 | Iustin Pop | "name": cluster.cluster_name,
|
3092 | 469f88e1 | Iustin Pop | "master": cluster.master_node,
|
3093 | 066f465d | Guido Trotter | "default_hypervisor": cluster.enabled_hypervisors[0], |
3094 | 469f88e1 | Iustin Pop | "enabled_hypervisors": cluster.enabled_hypervisors,
|
3095 | b8810fec | Michael Hanselmann | "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name]) |
3096 | 7c4d6c7b | Michael Hanselmann | for hypervisor_name in cluster.enabled_hypervisors]), |
3097 | 469f88e1 | Iustin Pop | "beparams": cluster.beparams,
|
3098 | 1094acda | Guido Trotter | "nicparams": cluster.nicparams,
|
3099 | 4b7735f9 | Iustin Pop | "candidate_pool_size": cluster.candidate_pool_size,
|
3100 | 7a56b411 | Guido Trotter | "master_netdev": cluster.master_netdev,
|
3101 | 7a56b411 | Guido Trotter | "volume_group_name": cluster.volume_group_name,
|
3102 | 7a56b411 | Guido Trotter | "file_storage_dir": cluster.file_storage_dir,
|
3103 | 90f72445 | Iustin Pop | "ctime": cluster.ctime,
|
3104 | 90f72445 | Iustin Pop | "mtime": cluster.mtime,
|
3105 | c118d1f4 | Michael Hanselmann | "tags": list(cluster.GetTags()), |
3106 | a8083063 | Iustin Pop | } |
3107 | a8083063 | Iustin Pop | |
3108 | a8083063 | Iustin Pop | return result
|
3109 | a8083063 | Iustin Pop | |
3110 | a8083063 | Iustin Pop | |
3111 | ae5849b5 | Michael Hanselmann | class LUQueryConfigValues(NoHooksLU): |
3112 | ae5849b5 | Michael Hanselmann | """Return configuration values.
|
3113 | a8083063 | Iustin Pop |
|
3114 | a8083063 | Iustin Pop | """
|
3115 | a8083063 | Iustin Pop | _OP_REQP = [] |
3116 | 642339cf | Guido Trotter | REQ_BGL = False
|
3117 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet() |
3118 | 05e50653 | Michael Hanselmann | _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag", |
3119 | 05e50653 | Michael Hanselmann | "watcher_pause")
|
3120 | 642339cf | Guido Trotter | |
3121 | 642339cf | Guido Trotter | def ExpandNames(self): |
3122 | 642339cf | Guido Trotter | self.needed_locks = {}
|
3123 | a8083063 | Iustin Pop | |
3124 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
3125 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
3126 | ae5849b5 | Michael Hanselmann | selected=self.op.output_fields)
|
3127 | ae5849b5 | Michael Hanselmann | |
3128 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3129 | a8083063 | Iustin Pop | """No prerequisites.
|
3130 | a8083063 | Iustin Pop |
|
3131 | a8083063 | Iustin Pop | """
|
3132 | a8083063 | Iustin Pop | pass
|
3133 | a8083063 | Iustin Pop | |
3134 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3135 | a8083063 | Iustin Pop | """Dump a representation of the cluster config to the standard output.
|
3136 | a8083063 | Iustin Pop |
|
3137 | a8083063 | Iustin Pop | """
|
3138 | ae5849b5 | Michael Hanselmann | values = [] |
3139 | ae5849b5 | Michael Hanselmann | for field in self.op.output_fields: |
3140 | ae5849b5 | Michael Hanselmann | if field == "cluster_name": |
3141 | 3ccafd0e | Iustin Pop | entry = self.cfg.GetClusterName()
|
3142 | ae5849b5 | Michael Hanselmann | elif field == "master_node": |
3143 | 3ccafd0e | Iustin Pop | entry = self.cfg.GetMasterNode()
|
3144 | 3ccafd0e | Iustin Pop | elif field == "drain_flag": |
3145 | 3ccafd0e | Iustin Pop | entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE) |
3146 | 05e50653 | Michael Hanselmann | elif field == "watcher_pause": |
3147 | 05e50653 | Michael Hanselmann | return utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
|
3148 | ae5849b5 | Michael Hanselmann | else:
|
3149 | ae5849b5 | Michael Hanselmann | raise errors.ParameterError(field)
|
3150 | 3ccafd0e | Iustin Pop | values.append(entry) |
3151 | ae5849b5 | Michael Hanselmann | return values
|
3152 | a8083063 | Iustin Pop | |
3153 | a8083063 | Iustin Pop | |
3154 | a8083063 | Iustin Pop | class LUActivateInstanceDisks(NoHooksLU): |
3155 | a8083063 | Iustin Pop | """Bring up an instance's disks.
|
3156 | a8083063 | Iustin Pop |
|
3157 | a8083063 | Iustin Pop | """
|
3158 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3159 | f22a8ba3 | Guido Trotter | REQ_BGL = False
|
3160 | f22a8ba3 | Guido Trotter | |
3161 | f22a8ba3 | Guido Trotter | def ExpandNames(self): |
3162 | f22a8ba3 | Guido Trotter | self._ExpandAndLockInstance()
|
3163 | f22a8ba3 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
3164 | f22a8ba3 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
3165 | f22a8ba3 | Guido Trotter | |
3166 | f22a8ba3 | Guido Trotter | def DeclareLocks(self, level): |
3167 | f22a8ba3 | Guido Trotter | if level == locking.LEVEL_NODE:
|
3168 | f22a8ba3 | Guido Trotter | self._LockInstancesNodes()
|
3169 | a8083063 | Iustin Pop | |
3170 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3171 | a8083063 | Iustin Pop | """Check prerequisites.
|
3172 | a8083063 | Iustin Pop |
|
3173 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3174 | a8083063 | Iustin Pop |
|
3175 | a8083063 | Iustin Pop | """
|
3176 | f22a8ba3 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3177 | f22a8ba3 | Guido Trotter | assert self.instance is not None, \ |
3178 | f22a8ba3 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3179 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
3180 | b4ec07f8 | Iustin Pop | if not hasattr(self.op, "ignore_size"): |
3181 | b4ec07f8 | Iustin Pop | self.op.ignore_size = False |
3182 | a8083063 | Iustin Pop | |
3183 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3184 | a8083063 | Iustin Pop | """Activate the disks.
|
3185 | a8083063 | Iustin Pop |
|
3186 | a8083063 | Iustin Pop | """
|
3187 | b4ec07f8 | Iustin Pop | disks_ok, disks_info = \ |
3188 | b4ec07f8 | Iustin Pop | _AssembleInstanceDisks(self, self.instance, |
3189 | b4ec07f8 | Iustin Pop | ignore_size=self.op.ignore_size)
|
3190 | a8083063 | Iustin Pop | if not disks_ok: |
3191 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Cannot activate block devices") |
3192 | a8083063 | Iustin Pop | |
3193 | a8083063 | Iustin Pop | return disks_info
|
3194 | a8083063 | Iustin Pop | |
3195 | a8083063 | Iustin Pop | |
3196 | e3443b36 | Iustin Pop | def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False, |
3197 | e3443b36 | Iustin Pop | ignore_size=False):
|
3198 | a8083063 | Iustin Pop | """Prepare the block devices for an instance.
|
3199 | a8083063 | Iustin Pop |
|
3200 | a8083063 | Iustin Pop | This sets up the block devices on all nodes.
|
3201 | a8083063 | Iustin Pop |
|
3202 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
3203 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
3204 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
3205 | e4376078 | Iustin Pop | @param instance: the instance for whose disks we assemble
|
3206 | e4376078 | Iustin Pop | @type ignore_secondaries: boolean
|
3207 | e4376078 | Iustin Pop | @param ignore_secondaries: if true, errors on secondary nodes
|
3208 | e4376078 | Iustin Pop | won't result in an error return from the function
|
3209 | e3443b36 | Iustin Pop | @type ignore_size: boolean
|
3210 | e3443b36 | Iustin Pop | @param ignore_size: if true, the current known size of the disk
|
3211 | e3443b36 | Iustin Pop | will not be used during the disk activation, useful for cases
|
3212 | e3443b36 | Iustin Pop | when the size is wrong
|
3213 | e4376078 | Iustin Pop | @return: False if the operation failed, otherwise a list of
|
3214 | e4376078 | Iustin Pop | (host, instance_visible_name, node_visible_name)
|
3215 | e4376078 | Iustin Pop | with the mapping from node devices to instance devices
|
3216 | a8083063 | Iustin Pop |
|
3217 | a8083063 | Iustin Pop | """
|
3218 | a8083063 | Iustin Pop | device_info = [] |
3219 | a8083063 | Iustin Pop | disks_ok = True
|
3220 | fdbd668d | Iustin Pop | iname = instance.name |
3221 | fdbd668d | Iustin Pop | # With the two passes mechanism we try to reduce the window of
|
3222 | fdbd668d | Iustin Pop | # opportunity for the race condition of switching DRBD to primary
|
3223 | fdbd668d | Iustin Pop | # before handshaking occured, but we do not eliminate it
|
3224 | fdbd668d | Iustin Pop | |
3225 | fdbd668d | Iustin Pop | # The proper fix would be to wait (with some limits) until the
|
3226 | fdbd668d | Iustin Pop | # connection has been made and drbd transitions from WFConnection
|
3227 | fdbd668d | Iustin Pop | # into any other network-connected state (Connected, SyncTarget,
|
3228 | fdbd668d | Iustin Pop | # SyncSource, etc.)
|
3229 | fdbd668d | Iustin Pop | |
3230 | fdbd668d | Iustin Pop | # 1st pass, assemble on all nodes in secondary mode
|
3231 | a8083063 | Iustin Pop | for inst_disk in instance.disks: |
3232 | a8083063 | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
3233 | e3443b36 | Iustin Pop | if ignore_size:
|
3234 | e3443b36 | Iustin Pop | node_disk = node_disk.Copy() |
3235 | e3443b36 | Iustin Pop | node_disk.UnsetSize() |
3236 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(node_disk, node) |
3237 | 72737a7f | Iustin Pop | result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
|
3238 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3239 | 53c14ef1 | Iustin Pop | if msg:
|
3240 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("Could not prepare block device %s on node %s"
|
3241 | 53c14ef1 | Iustin Pop | " (is_primary=False, pass=1): %s",
|
3242 | 53c14ef1 | Iustin Pop | inst_disk.iv_name, node, msg) |
3243 | fdbd668d | Iustin Pop | if not ignore_secondaries: |
3244 | a8083063 | Iustin Pop | disks_ok = False
|
3245 | fdbd668d | Iustin Pop | |
3246 | fdbd668d | Iustin Pop | # FIXME: race condition on drbd migration to primary
|
3247 | fdbd668d | Iustin Pop | |
3248 | fdbd668d | Iustin Pop | # 2nd pass, do only the primary node
|
3249 | fdbd668d | Iustin Pop | for inst_disk in instance.disks: |
3250 | fdbd668d | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
3251 | fdbd668d | Iustin Pop | if node != instance.primary_node:
|
3252 | fdbd668d | Iustin Pop | continue
|
3253 | e3443b36 | Iustin Pop | if ignore_size:
|
3254 | e3443b36 | Iustin Pop | node_disk = node_disk.Copy() |
3255 | e3443b36 | Iustin Pop | node_disk.UnsetSize() |
3256 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(node_disk, node) |
3257 | 72737a7f | Iustin Pop | result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
|
3258 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3259 | 53c14ef1 | Iustin Pop | if msg:
|
3260 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("Could not prepare block device %s on node %s"
|
3261 | 53c14ef1 | Iustin Pop | " (is_primary=True, pass=2): %s",
|
3262 | 53c14ef1 | Iustin Pop | inst_disk.iv_name, node, msg) |
3263 | fdbd668d | Iustin Pop | disks_ok = False
|
3264 | 1dff8e07 | Iustin Pop | device_info.append((instance.primary_node, inst_disk.iv_name, |
3265 | 1dff8e07 | Iustin Pop | result.payload)) |
3266 | a8083063 | Iustin Pop | |
3267 | b352ab5b | Iustin Pop | # leave the disks configured for the primary node
|
3268 | b352ab5b | Iustin Pop | # this is a workaround that would be fixed better by
|
3269 | b352ab5b | Iustin Pop | # improving the logical/physical id handling
|
3270 | b352ab5b | Iustin Pop | for disk in instance.disks: |
3271 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(disk, instance.primary_node) |
3272 | b352ab5b | Iustin Pop | |
3273 | a8083063 | Iustin Pop | return disks_ok, device_info
|
3274 | a8083063 | Iustin Pop | |
3275 | a8083063 | Iustin Pop | |
3276 | b9bddb6b | Iustin Pop | def _StartInstanceDisks(lu, instance, force): |
3277 | 3ecf6786 | Iustin Pop | """Start the disks of an instance.
|
3278 | 3ecf6786 | Iustin Pop |
|
3279 | 3ecf6786 | Iustin Pop | """
|
3280 | 7c4d6c7b | Michael Hanselmann | disks_ok, _ = _AssembleInstanceDisks(lu, instance, |
3281 | fe7b0351 | Michael Hanselmann | ignore_secondaries=force) |
3282 | fe7b0351 | Michael Hanselmann | if not disks_ok: |
3283 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(lu, instance) |
3284 | fe7b0351 | Michael Hanselmann | if force is not None and not force: |
3285 | 86d9d3bb | Iustin Pop | lu.proc.LogWarning("", hint="If the message above refers to a" |
3286 | 86d9d3bb | Iustin Pop | " secondary node,"
|
3287 | 86d9d3bb | Iustin Pop | " you can retry the operation using '--force'.")
|
3288 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Disk consistency error") |
3289 | fe7b0351 | Michael Hanselmann | |
3290 | fe7b0351 | Michael Hanselmann | |
3291 | a8083063 | Iustin Pop | class LUDeactivateInstanceDisks(NoHooksLU): |
3292 | a8083063 | Iustin Pop | """Shutdown an instance's disks.
|
3293 | a8083063 | Iustin Pop |
|
3294 | a8083063 | Iustin Pop | """
|
3295 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3296 | f22a8ba3 | Guido Trotter | REQ_BGL = False
|
3297 | f22a8ba3 | Guido Trotter | |
3298 | f22a8ba3 | Guido Trotter | def ExpandNames(self): |
3299 | f22a8ba3 | Guido Trotter | self._ExpandAndLockInstance()
|
3300 | f22a8ba3 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
3301 | f22a8ba3 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
3302 | f22a8ba3 | Guido Trotter | |
3303 | f22a8ba3 | Guido Trotter | def DeclareLocks(self, level): |
3304 | f22a8ba3 | Guido Trotter | if level == locking.LEVEL_NODE:
|
3305 | f22a8ba3 | Guido Trotter | self._LockInstancesNodes()
|
3306 | a8083063 | Iustin Pop | |
3307 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3308 | a8083063 | Iustin Pop | """Check prerequisites.
|
3309 | a8083063 | Iustin Pop |
|
3310 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3311 | a8083063 | Iustin Pop |
|
3312 | a8083063 | Iustin Pop | """
|
3313 | f22a8ba3 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3314 | f22a8ba3 | Guido Trotter | assert self.instance is not None, \ |
3315 | f22a8ba3 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3316 | a8083063 | Iustin Pop | |
3317 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3318 | a8083063 | Iustin Pop | """Deactivate the disks
|
3319 | a8083063 | Iustin Pop |
|
3320 | a8083063 | Iustin Pop | """
|
3321 | a8083063 | Iustin Pop | instance = self.instance
|
3322 | b9bddb6b | Iustin Pop | _SafeShutdownInstanceDisks(self, instance)
|
3323 | a8083063 | Iustin Pop | |
3324 | a8083063 | Iustin Pop | |
3325 | b9bddb6b | Iustin Pop | def _SafeShutdownInstanceDisks(lu, instance): |
3326 | 155d6c75 | Guido Trotter | """Shutdown block devices of an instance.
|
3327 | 155d6c75 | Guido Trotter |
|
3328 | 155d6c75 | Guido Trotter | This function checks if an instance is running, before calling
|
3329 | 155d6c75 | Guido Trotter | _ShutdownInstanceDisks.
|
3330 | 155d6c75 | Guido Trotter |
|
3331 | 155d6c75 | Guido Trotter | """
|
3332 | aca13712 | Iustin Pop | pnode = instance.primary_node |
3333 | 4c4e4e1e | Iustin Pop | ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode] |
3334 | 4c4e4e1e | Iustin Pop | ins_l.Raise("Can't contact node %s" % pnode)
|
3335 | aca13712 | Iustin Pop | |
3336 | aca13712 | Iustin Pop | if instance.name in ins_l.payload: |
3337 | 155d6c75 | Guido Trotter | raise errors.OpExecError("Instance is running, can't shutdown" |
3338 | 155d6c75 | Guido Trotter | " block devices.")
|
3339 | 155d6c75 | Guido Trotter | |
3340 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(lu, instance) |
3341 | a8083063 | Iustin Pop | |
3342 | a8083063 | Iustin Pop | |
3343 | b9bddb6b | Iustin Pop | def _ShutdownInstanceDisks(lu, instance, ignore_primary=False): |
3344 | a8083063 | Iustin Pop | """Shutdown block devices of an instance.
|
3345 | a8083063 | Iustin Pop |
|
3346 | a8083063 | Iustin Pop | This does the shutdown on all nodes of the instance.
|
3347 | a8083063 | Iustin Pop |
|
3348 | a8083063 | Iustin Pop | If the ignore_primary is false, errors on the primary node are
|
3349 | a8083063 | Iustin Pop | ignored.
|
3350 | a8083063 | Iustin Pop |
|
3351 | a8083063 | Iustin Pop | """
|
3352 | cacfd1fd | Iustin Pop | all_result = True
|
3353 | a8083063 | Iustin Pop | for disk in instance.disks: |
3354 | a8083063 | Iustin Pop | for node, top_disk in disk.ComputeNodeTree(instance.primary_node): |
3355 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(top_disk, node) |
3356 | 781de953 | Iustin Pop | result = lu.rpc.call_blockdev_shutdown(node, top_disk) |
3357 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3358 | cacfd1fd | Iustin Pop | if msg:
|
3359 | cacfd1fd | Iustin Pop | lu.LogWarning("Could not shutdown block device %s on node %s: %s",
|
3360 | cacfd1fd | Iustin Pop | disk.iv_name, node, msg) |
3361 | a8083063 | Iustin Pop | if not ignore_primary or node != instance.primary_node: |
3362 | cacfd1fd | Iustin Pop | all_result = False
|
3363 | cacfd1fd | Iustin Pop | return all_result
|
3364 | a8083063 | Iustin Pop | |
3365 | a8083063 | Iustin Pop | |
3366 | 9ca87a96 | Iustin Pop | def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name): |
3367 | d4f16fd9 | Iustin Pop | """Checks if a node has enough free memory.
|
3368 | d4f16fd9 | Iustin Pop |
|
3369 | d4f16fd9 | Iustin Pop | This function check if a given node has the needed amount of free
|
3370 | d4f16fd9 | Iustin Pop | memory. In case the node has less memory or we cannot get the
|
3371 | d4f16fd9 | Iustin Pop | information from the node, this function raise an OpPrereqError
|
3372 | d4f16fd9 | Iustin Pop | exception.
|
3373 | d4f16fd9 | Iustin Pop |
|
3374 | b9bddb6b | Iustin Pop | @type lu: C{LogicalUnit}
|
3375 | b9bddb6b | Iustin Pop | @param lu: a logical unit from which we get configuration data
|
3376 | e69d05fd | Iustin Pop | @type node: C{str}
|
3377 | e69d05fd | Iustin Pop | @param node: the node to check
|
3378 | e69d05fd | Iustin Pop | @type reason: C{str}
|
3379 | e69d05fd | Iustin Pop | @param reason: string to use in the error message
|
3380 | e69d05fd | Iustin Pop | @type requested: C{int}
|
3381 | e69d05fd | Iustin Pop | @param requested: the amount of memory in MiB to check for
|
3382 | 9ca87a96 | Iustin Pop | @type hypervisor_name: C{str}
|
3383 | 9ca87a96 | Iustin Pop | @param hypervisor_name: the hypervisor to ask for memory stats
|
3384 | e69d05fd | Iustin Pop | @raise errors.OpPrereqError: if the node doesn't have enough memory, or
|
3385 | e69d05fd | Iustin Pop | we cannot check the node
|
3386 | d4f16fd9 | Iustin Pop |
|
3387 | d4f16fd9 | Iustin Pop | """
|
3388 | 9ca87a96 | Iustin Pop | nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name) |
3389 | 4c4e4e1e | Iustin Pop | nodeinfo[node].Raise("Can't get data from node %s" % node, prereq=True) |
3390 | 070e998b | Iustin Pop | free_mem = nodeinfo[node].payload.get('memory_free', None) |
3391 | d4f16fd9 | Iustin Pop | if not isinstance(free_mem, int): |
3392 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Can't compute free memory on node %s, result" |
3393 | 070e998b | Iustin Pop | " was '%s'" % (node, free_mem))
|
3394 | d4f16fd9 | Iustin Pop | if requested > free_mem:
|
3395 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Not enough memory on node %s for %s:" |
3396 | 070e998b | Iustin Pop | " needed %s MiB, available %s MiB" %
|
3397 | 070e998b | Iustin Pop | (node, reason, requested, free_mem)) |
3398 | d4f16fd9 | Iustin Pop | |
3399 | d4f16fd9 | Iustin Pop | |
3400 | a8083063 | Iustin Pop | class LUStartupInstance(LogicalUnit): |
3401 | a8083063 | Iustin Pop | """Starts an instance.
|
3402 | a8083063 | Iustin Pop |
|
3403 | a8083063 | Iustin Pop | """
|
3404 | a8083063 | Iustin Pop | HPATH = "instance-start"
|
3405 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3406 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "force"] |
3407 | e873317a | Guido Trotter | REQ_BGL = False
|
3408 | e873317a | Guido Trotter | |
3409 | e873317a | Guido Trotter | def ExpandNames(self): |
3410 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
3411 | a8083063 | Iustin Pop | |
3412 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3413 | a8083063 | Iustin Pop | """Build hooks env.
|
3414 | a8083063 | Iustin Pop |
|
3415 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3416 | a8083063 | Iustin Pop |
|
3417 | a8083063 | Iustin Pop | """
|
3418 | a8083063 | Iustin Pop | env = { |
3419 | a8083063 | Iustin Pop | "FORCE": self.op.force, |
3420 | a8083063 | Iustin Pop | } |
3421 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
3422 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3423 | a8083063 | Iustin Pop | return env, nl, nl
|
3424 | a8083063 | Iustin Pop | |
3425 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3426 | a8083063 | Iustin Pop | """Check prerequisites.
|
3427 | a8083063 | Iustin Pop |
|
3428 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3429 | a8083063 | Iustin Pop |
|
3430 | a8083063 | Iustin Pop | """
|
3431 | e873317a | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3432 | e873317a | Guido Trotter | assert self.instance is not None, \ |
3433 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3434 | a8083063 | Iustin Pop | |
3435 | d04aaa2f | Iustin Pop | # extra beparams
|
3436 | d04aaa2f | Iustin Pop | self.beparams = getattr(self.op, "beparams", {}) |
3437 | d04aaa2f | Iustin Pop | if self.beparams: |
3438 | d04aaa2f | Iustin Pop | if not isinstance(self.beparams, dict): |
3439 | d04aaa2f | Iustin Pop | raise errors.OpPrereqError("Invalid beparams passed: %s, expected" |
3440 | d04aaa2f | Iustin Pop | " dict" % (type(self.beparams), )) |
3441 | d04aaa2f | Iustin Pop | # fill the beparams dict
|
3442 | d04aaa2f | Iustin Pop | utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
|
3443 | d04aaa2f | Iustin Pop | self.op.beparams = self.beparams |
3444 | d04aaa2f | Iustin Pop | |
3445 | d04aaa2f | Iustin Pop | # extra hvparams
|
3446 | d04aaa2f | Iustin Pop | self.hvparams = getattr(self.op, "hvparams", {}) |
3447 | d04aaa2f | Iustin Pop | if self.hvparams: |
3448 | d04aaa2f | Iustin Pop | if not isinstance(self.hvparams, dict): |
3449 | d04aaa2f | Iustin Pop | raise errors.OpPrereqError("Invalid hvparams passed: %s, expected" |
3450 | d04aaa2f | Iustin Pop | " dict" % (type(self.hvparams), )) |
3451 | d04aaa2f | Iustin Pop | |
3452 | d04aaa2f | Iustin Pop | # check hypervisor parameter syntax (locally)
|
3453 | d04aaa2f | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
3454 | d04aaa2f | Iustin Pop | utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
|
3455 | abe609b2 | Guido Trotter | filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor], |
3456 | d04aaa2f | Iustin Pop | instance.hvparams) |
3457 | d04aaa2f | Iustin Pop | filled_hvp.update(self.hvparams)
|
3458 | d04aaa2f | Iustin Pop | hv_type = hypervisor.GetHypervisor(instance.hypervisor) |
3459 | d04aaa2f | Iustin Pop | hv_type.CheckParameterSyntax(filled_hvp) |
3460 | d04aaa2f | Iustin Pop | _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
|
3461 | d04aaa2f | Iustin Pop | self.op.hvparams = self.hvparams |
3462 | d04aaa2f | Iustin Pop | |
3463 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
3464 | 7527a8a4 | Iustin Pop | |
3465 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
3466 | 5bbd3f7f | Michael Hanselmann | # check bridges existence
|
3467 | b9bddb6b | Iustin Pop | _CheckInstanceBridgesExist(self, instance)
|
3468 | a8083063 | Iustin Pop | |
3469 | f1926756 | Guido Trotter | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
3470 | f1926756 | Guido Trotter | instance.name, |
3471 | f1926756 | Guido Trotter | instance.hypervisor) |
3472 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node,
|
3473 | 4c4e4e1e | Iustin Pop | prereq=True)
|
3474 | 7ad1af4a | Iustin Pop | if not remote_info.payload: # not running already |
3475 | f1926756 | Guido Trotter | _CheckNodeFreeMemory(self, instance.primary_node,
|
3476 | f1926756 | Guido Trotter | "starting instance %s" % instance.name,
|
3477 | f1926756 | Guido Trotter | bep[constants.BE_MEMORY], instance.hypervisor) |
3478 | d4f16fd9 | Iustin Pop | |
3479 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3480 | a8083063 | Iustin Pop | """Start the instance.
|
3481 | a8083063 | Iustin Pop |
|
3482 | a8083063 | Iustin Pop | """
|
3483 | a8083063 | Iustin Pop | instance = self.instance
|
3484 | a8083063 | Iustin Pop | force = self.op.force
|
3485 | a8083063 | Iustin Pop | |
3486 | fe482621 | Iustin Pop | self.cfg.MarkInstanceUp(instance.name)
|
3487 | fe482621 | Iustin Pop | |
3488 | a8083063 | Iustin Pop | node_current = instance.primary_node |
3489 | a8083063 | Iustin Pop | |
3490 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, instance, force)
|
3491 | a8083063 | Iustin Pop | |
3492 | d04aaa2f | Iustin Pop | result = self.rpc.call_instance_start(node_current, instance,
|
3493 | d04aaa2f | Iustin Pop | self.hvparams, self.beparams) |
3494 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3495 | dd279568 | Iustin Pop | if msg:
|
3496 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
3497 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance: %s" % msg) |
3498 | a8083063 | Iustin Pop | |
3499 | a8083063 | Iustin Pop | |
3500 | bf6929a2 | Alexander Schreiber | class LURebootInstance(LogicalUnit): |
3501 | bf6929a2 | Alexander Schreiber | """Reboot an instance.
|
3502 | bf6929a2 | Alexander Schreiber |
|
3503 | bf6929a2 | Alexander Schreiber | """
|
3504 | bf6929a2 | Alexander Schreiber | HPATH = "instance-reboot"
|
3505 | bf6929a2 | Alexander Schreiber | HTYPE = constants.HTYPE_INSTANCE |
3506 | bf6929a2 | Alexander Schreiber | _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"] |
3507 | e873317a | Guido Trotter | REQ_BGL = False
|
3508 | e873317a | Guido Trotter | |
3509 | e873317a | Guido Trotter | def ExpandNames(self): |
3510 | 0fcc5db3 | Guido Trotter | if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT, |
3511 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_HARD, |
3512 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_FULL]: |
3513 | 0fcc5db3 | Guido Trotter | raise errors.ParameterError("reboot type not in [%s, %s, %s]" % |
3514 | 0fcc5db3 | Guido Trotter | (constants.INSTANCE_REBOOT_SOFT, |
3515 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_HARD, |
3516 | 0fcc5db3 | Guido Trotter | constants.INSTANCE_REBOOT_FULL)) |
3517 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
3518 | bf6929a2 | Alexander Schreiber | |
3519 | bf6929a2 | Alexander Schreiber | def BuildHooksEnv(self): |
3520 | bf6929a2 | Alexander Schreiber | """Build hooks env.
|
3521 | bf6929a2 | Alexander Schreiber |
|
3522 | bf6929a2 | Alexander Schreiber | This runs on master, primary and secondary nodes of the instance.
|
3523 | bf6929a2 | Alexander Schreiber |
|
3524 | bf6929a2 | Alexander Schreiber | """
|
3525 | bf6929a2 | Alexander Schreiber | env = { |
3526 | bf6929a2 | Alexander Schreiber | "IGNORE_SECONDARIES": self.op.ignore_secondaries, |
3527 | 2c2690c9 | Iustin Pop | "REBOOT_TYPE": self.op.reboot_type, |
3528 | bf6929a2 | Alexander Schreiber | } |
3529 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
3530 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3531 | bf6929a2 | Alexander Schreiber | return env, nl, nl
|
3532 | bf6929a2 | Alexander Schreiber | |
3533 | bf6929a2 | Alexander Schreiber | def CheckPrereq(self): |
3534 | bf6929a2 | Alexander Schreiber | """Check prerequisites.
|
3535 | bf6929a2 | Alexander Schreiber |
|
3536 | bf6929a2 | Alexander Schreiber | This checks that the instance is in the cluster.
|
3537 | bf6929a2 | Alexander Schreiber |
|
3538 | bf6929a2 | Alexander Schreiber | """
|
3539 | e873317a | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3540 | e873317a | Guido Trotter | assert self.instance is not None, \ |
3541 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3542 | bf6929a2 | Alexander Schreiber | |
3543 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
3544 | 7527a8a4 | Iustin Pop | |
3545 | 5bbd3f7f | Michael Hanselmann | # check bridges existence
|
3546 | b9bddb6b | Iustin Pop | _CheckInstanceBridgesExist(self, instance)
|
3547 | bf6929a2 | Alexander Schreiber | |
3548 | bf6929a2 | Alexander Schreiber | def Exec(self, feedback_fn): |
3549 | bf6929a2 | Alexander Schreiber | """Reboot the instance.
|
3550 | bf6929a2 | Alexander Schreiber |
|
3551 | bf6929a2 | Alexander Schreiber | """
|
3552 | bf6929a2 | Alexander Schreiber | instance = self.instance
|
3553 | bf6929a2 | Alexander Schreiber | ignore_secondaries = self.op.ignore_secondaries
|
3554 | bf6929a2 | Alexander Schreiber | reboot_type = self.op.reboot_type
|
3555 | bf6929a2 | Alexander Schreiber | |
3556 | bf6929a2 | Alexander Schreiber | node_current = instance.primary_node |
3557 | bf6929a2 | Alexander Schreiber | |
3558 | bf6929a2 | Alexander Schreiber | if reboot_type in [constants.INSTANCE_REBOOT_SOFT, |
3559 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_HARD]: |
3560 | ae48ac32 | Iustin Pop | for disk in instance.disks: |
3561 | ae48ac32 | Iustin Pop | self.cfg.SetDiskID(disk, node_current)
|
3562 | 781de953 | Iustin Pop | result = self.rpc.call_instance_reboot(node_current, instance,
|
3563 | 07813a9e | Iustin Pop | reboot_type) |
3564 | 4c4e4e1e | Iustin Pop | result.Raise("Could not reboot instance")
|
3565 | bf6929a2 | Alexander Schreiber | else:
|
3566 | 1fae010f | Iustin Pop | result = self.rpc.call_instance_shutdown(node_current, instance)
|
3567 | 4c4e4e1e | Iustin Pop | result.Raise("Could not shutdown instance for full reboot")
|
3568 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
3569 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, instance, ignore_secondaries)
|
3570 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(node_current, instance, None, None) |
3571 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3572 | dd279568 | Iustin Pop | if msg:
|
3573 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
3574 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance for" |
3575 | dd279568 | Iustin Pop | " full reboot: %s" % msg)
|
3576 | bf6929a2 | Alexander Schreiber | |
3577 | bf6929a2 | Alexander Schreiber | self.cfg.MarkInstanceUp(instance.name)
|
3578 | bf6929a2 | Alexander Schreiber | |
3579 | bf6929a2 | Alexander Schreiber | |
3580 | a8083063 | Iustin Pop | class LUShutdownInstance(LogicalUnit): |
3581 | a8083063 | Iustin Pop | """Shutdown an instance.
|
3582 | a8083063 | Iustin Pop |
|
3583 | a8083063 | Iustin Pop | """
|
3584 | a8083063 | Iustin Pop | HPATH = "instance-stop"
|
3585 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3586 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3587 | e873317a | Guido Trotter | REQ_BGL = False
|
3588 | e873317a | Guido Trotter | |
3589 | e873317a | Guido Trotter | def ExpandNames(self): |
3590 | e873317a | Guido Trotter | self._ExpandAndLockInstance()
|
3591 | a8083063 | Iustin Pop | |
3592 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3593 | a8083063 | Iustin Pop | """Build hooks env.
|
3594 | a8083063 | Iustin Pop |
|
3595 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3596 | a8083063 | Iustin Pop |
|
3597 | a8083063 | Iustin Pop | """
|
3598 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
3599 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3600 | a8083063 | Iustin Pop | return env, nl, nl
|
3601 | a8083063 | Iustin Pop | |
3602 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3603 | a8083063 | Iustin Pop | """Check prerequisites.
|
3604 | a8083063 | Iustin Pop |
|
3605 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3606 | a8083063 | Iustin Pop |
|
3607 | a8083063 | Iustin Pop | """
|
3608 | e873317a | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3609 | e873317a | Guido Trotter | assert self.instance is not None, \ |
3610 | e873317a | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3611 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
3612 | a8083063 | Iustin Pop | |
3613 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3614 | a8083063 | Iustin Pop | """Shutdown the instance.
|
3615 | a8083063 | Iustin Pop |
|
3616 | a8083063 | Iustin Pop | """
|
3617 | a8083063 | Iustin Pop | instance = self.instance
|
3618 | a8083063 | Iustin Pop | node_current = instance.primary_node |
3619 | fe482621 | Iustin Pop | self.cfg.MarkInstanceDown(instance.name)
|
3620 | 781de953 | Iustin Pop | result = self.rpc.call_instance_shutdown(node_current, instance)
|
3621 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3622 | 1fae010f | Iustin Pop | if msg:
|
3623 | 1fae010f | Iustin Pop | self.proc.LogWarning("Could not shutdown instance: %s" % msg) |
3624 | a8083063 | Iustin Pop | |
3625 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
3626 | a8083063 | Iustin Pop | |
3627 | a8083063 | Iustin Pop | |
3628 | fe7b0351 | Michael Hanselmann | class LUReinstallInstance(LogicalUnit): |
3629 | fe7b0351 | Michael Hanselmann | """Reinstall an instance.
|
3630 | fe7b0351 | Michael Hanselmann |
|
3631 | fe7b0351 | Michael Hanselmann | """
|
3632 | fe7b0351 | Michael Hanselmann | HPATH = "instance-reinstall"
|
3633 | fe7b0351 | Michael Hanselmann | HTYPE = constants.HTYPE_INSTANCE |
3634 | fe7b0351 | Michael Hanselmann | _OP_REQP = ["instance_name"]
|
3635 | 4e0b4d2d | Guido Trotter | REQ_BGL = False
|
3636 | 4e0b4d2d | Guido Trotter | |
3637 | 4e0b4d2d | Guido Trotter | def ExpandNames(self): |
3638 | 4e0b4d2d | Guido Trotter | self._ExpandAndLockInstance()
|
3639 | fe7b0351 | Michael Hanselmann | |
3640 | fe7b0351 | Michael Hanselmann | def BuildHooksEnv(self): |
3641 | fe7b0351 | Michael Hanselmann | """Build hooks env.
|
3642 | fe7b0351 | Michael Hanselmann |
|
3643 | fe7b0351 | Michael Hanselmann | This runs on master, primary and secondary nodes of the instance.
|
3644 | fe7b0351 | Michael Hanselmann |
|
3645 | fe7b0351 | Michael Hanselmann | """
|
3646 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
3647 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3648 | fe7b0351 | Michael Hanselmann | return env, nl, nl
|
3649 | fe7b0351 | Michael Hanselmann | |
3650 | fe7b0351 | Michael Hanselmann | def CheckPrereq(self): |
3651 | fe7b0351 | Michael Hanselmann | """Check prerequisites.
|
3652 | fe7b0351 | Michael Hanselmann |
|
3653 | fe7b0351 | Michael Hanselmann | This checks that the instance is in the cluster and is not running.
|
3654 | fe7b0351 | Michael Hanselmann |
|
3655 | fe7b0351 | Michael Hanselmann | """
|
3656 | 4e0b4d2d | Guido Trotter | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3657 | 4e0b4d2d | Guido Trotter | assert instance is not None, \ |
3658 | 4e0b4d2d | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3659 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
3660 | 4e0b4d2d | Guido Trotter | |
3661 | fe7b0351 | Michael Hanselmann | if instance.disk_template == constants.DT_DISKLESS:
|
3662 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has no disks" % |
3663 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
3664 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
3665 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is marked to be up" % |
3666 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
3667 | 72737a7f | Iustin Pop | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
3668 | 72737a7f | Iustin Pop | instance.name, |
3669 | 72737a7f | Iustin Pop | instance.hypervisor) |
3670 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node,
|
3671 | 4c4e4e1e | Iustin Pop | prereq=True)
|
3672 | 7ad1af4a | Iustin Pop | if remote_info.payload:
|
3673 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is running on the node %s" % |
3674 | 3ecf6786 | Iustin Pop | (self.op.instance_name,
|
3675 | 3ecf6786 | Iustin Pop | instance.primary_node)) |
3676 | d0834de3 | Michael Hanselmann | |
3677 | d0834de3 | Michael Hanselmann | self.op.os_type = getattr(self.op, "os_type", None) |
3678 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
3679 | d0834de3 | Michael Hanselmann | # OS verification
|
3680 | d0834de3 | Michael Hanselmann | pnode = self.cfg.GetNodeInfo(
|
3681 | d0834de3 | Michael Hanselmann | self.cfg.ExpandNodeName(instance.primary_node))
|
3682 | d0834de3 | Michael Hanselmann | if pnode is None: |
3683 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Primary node '%s' is unknown" % |
3684 | 3ecf6786 | Iustin Pop | self.op.pnode)
|
3685 | 781de953 | Iustin Pop | result = self.rpc.call_os_get(pnode.name, self.op.os_type) |
3686 | 4c4e4e1e | Iustin Pop | result.Raise("OS '%s' not in supported OS list for primary node %s" %
|
3687 | 4c4e4e1e | Iustin Pop | (self.op.os_type, pnode.name), prereq=True) |
3688 | d0834de3 | Michael Hanselmann | |
3689 | fe7b0351 | Michael Hanselmann | self.instance = instance
|
3690 | fe7b0351 | Michael Hanselmann | |
3691 | fe7b0351 | Michael Hanselmann | def Exec(self, feedback_fn): |
3692 | fe7b0351 | Michael Hanselmann | """Reinstall the instance.
|
3693 | fe7b0351 | Michael Hanselmann |
|
3694 | fe7b0351 | Michael Hanselmann | """
|
3695 | fe7b0351 | Michael Hanselmann | inst = self.instance
|
3696 | fe7b0351 | Michael Hanselmann | |
3697 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
3698 | d0834de3 | Michael Hanselmann | feedback_fn("Changing OS to '%s'..." % self.op.os_type) |
3699 | d0834de3 | Michael Hanselmann | inst.os = self.op.os_type
|
3700 | 97abc79f | Iustin Pop | self.cfg.Update(inst)
|
3701 | d0834de3 | Michael Hanselmann | |
3702 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, inst, None) |
3703 | fe7b0351 | Michael Hanselmann | try:
|
3704 | fe7b0351 | Michael Hanselmann | feedback_fn("Running the instance OS create scripts...")
|
3705 | e557bae9 | Guido Trotter | result = self.rpc.call_instance_os_add(inst.primary_node, inst, True) |
3706 | 4c4e4e1e | Iustin Pop | result.Raise("Could not install OS for instance %s on node %s" %
|
3707 | 4c4e4e1e | Iustin Pop | (inst.name, inst.primary_node)) |
3708 | fe7b0351 | Michael Hanselmann | finally:
|
3709 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, inst)
|
3710 | fe7b0351 | Michael Hanselmann | |
3711 | fe7b0351 | Michael Hanselmann | |
3712 | bd315bfa | Iustin Pop | class LURecreateInstanceDisks(LogicalUnit): |
3713 | bd315bfa | Iustin Pop | """Recreate an instance's missing disks.
|
3714 | bd315bfa | Iustin Pop |
|
3715 | bd315bfa | Iustin Pop | """
|
3716 | bd315bfa | Iustin Pop | HPATH = "instance-recreate-disks"
|
3717 | bd315bfa | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3718 | bd315bfa | Iustin Pop | _OP_REQP = ["instance_name", "disks"] |
3719 | bd315bfa | Iustin Pop | REQ_BGL = False
|
3720 | bd315bfa | Iustin Pop | |
3721 | bd315bfa | Iustin Pop | def CheckArguments(self): |
3722 | bd315bfa | Iustin Pop | """Check the arguments.
|
3723 | bd315bfa | Iustin Pop |
|
3724 | bd315bfa | Iustin Pop | """
|
3725 | bd315bfa | Iustin Pop | if not isinstance(self.op.disks, list): |
3726 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Invalid disks parameter") |
3727 | bd315bfa | Iustin Pop | for item in self.op.disks: |
3728 | bd315bfa | Iustin Pop | if (not isinstance(item, int) or |
3729 | bd315bfa | Iustin Pop | item < 0):
|
3730 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Invalid disk specification '%s'" % |
3731 | bd315bfa | Iustin Pop | str(item))
|
3732 | bd315bfa | Iustin Pop | |
3733 | bd315bfa | Iustin Pop | def ExpandNames(self): |
3734 | bd315bfa | Iustin Pop | self._ExpandAndLockInstance()
|
3735 | bd315bfa | Iustin Pop | |
3736 | bd315bfa | Iustin Pop | def BuildHooksEnv(self): |
3737 | bd315bfa | Iustin Pop | """Build hooks env.
|
3738 | bd315bfa | Iustin Pop |
|
3739 | bd315bfa | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3740 | bd315bfa | Iustin Pop |
|
3741 | bd315bfa | Iustin Pop | """
|
3742 | bd315bfa | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
3743 | bd315bfa | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3744 | bd315bfa | Iustin Pop | return env, nl, nl
|
3745 | bd315bfa | Iustin Pop | |
3746 | bd315bfa | Iustin Pop | def CheckPrereq(self): |
3747 | bd315bfa | Iustin Pop | """Check prerequisites.
|
3748 | bd315bfa | Iustin Pop |
|
3749 | bd315bfa | Iustin Pop | This checks that the instance is in the cluster and is not running.
|
3750 | bd315bfa | Iustin Pop |
|
3751 | bd315bfa | Iustin Pop | """
|
3752 | bd315bfa | Iustin Pop | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3753 | bd315bfa | Iustin Pop | assert instance is not None, \ |
3754 | bd315bfa | Iustin Pop | "Cannot retrieve locked instance %s" % self.op.instance_name |
3755 | bd315bfa | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
3756 | bd315bfa | Iustin Pop | |
3757 | bd315bfa | Iustin Pop | if instance.disk_template == constants.DT_DISKLESS:
|
3758 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has no disks" % |
3759 | bd315bfa | Iustin Pop | self.op.instance_name)
|
3760 | bd315bfa | Iustin Pop | if instance.admin_up:
|
3761 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is marked to be up" % |
3762 | bd315bfa | Iustin Pop | self.op.instance_name)
|
3763 | bd315bfa | Iustin Pop | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
3764 | bd315bfa | Iustin Pop | instance.name, |
3765 | bd315bfa | Iustin Pop | instance.hypervisor) |
3766 | bd315bfa | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node,
|
3767 | bd315bfa | Iustin Pop | prereq=True)
|
3768 | bd315bfa | Iustin Pop | if remote_info.payload:
|
3769 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is running on the node %s" % |
3770 | bd315bfa | Iustin Pop | (self.op.instance_name,
|
3771 | bd315bfa | Iustin Pop | instance.primary_node)) |
3772 | bd315bfa | Iustin Pop | |
3773 | bd315bfa | Iustin Pop | if not self.op.disks: |
3774 | bd315bfa | Iustin Pop | self.op.disks = range(len(instance.disks)) |
3775 | bd315bfa | Iustin Pop | else:
|
3776 | bd315bfa | Iustin Pop | for idx in self.op.disks: |
3777 | bd315bfa | Iustin Pop | if idx >= len(instance.disks): |
3778 | bd315bfa | Iustin Pop | raise errors.OpPrereqError("Invalid disk index passed '%s'" % idx) |
3779 | bd315bfa | Iustin Pop | |
3780 | bd315bfa | Iustin Pop | self.instance = instance
|
3781 | bd315bfa | Iustin Pop | |
3782 | bd315bfa | Iustin Pop | def Exec(self, feedback_fn): |
3783 | bd315bfa | Iustin Pop | """Recreate the disks.
|
3784 | bd315bfa | Iustin Pop |
|
3785 | bd315bfa | Iustin Pop | """
|
3786 | bd315bfa | Iustin Pop | to_skip = [] |
3787 | bd315bfa | Iustin Pop | for idx, disk in enumerate(self.instance.disks): |
3788 | bd315bfa | Iustin Pop | if idx not in self.op.disks: # disk idx has not been passed in |
3789 | bd315bfa | Iustin Pop | to_skip.append(idx) |
3790 | bd315bfa | Iustin Pop | continue
|
3791 | bd315bfa | Iustin Pop | |
3792 | bd315bfa | Iustin Pop | _CreateDisks(self, self.instance, to_skip=to_skip) |
3793 | bd315bfa | Iustin Pop | |
3794 | bd315bfa | Iustin Pop | |
3795 | decd5f45 | Iustin Pop | class LURenameInstance(LogicalUnit): |
3796 | decd5f45 | Iustin Pop | """Rename an instance.
|
3797 | decd5f45 | Iustin Pop |
|
3798 | decd5f45 | Iustin Pop | """
|
3799 | decd5f45 | Iustin Pop | HPATH = "instance-rename"
|
3800 | decd5f45 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3801 | decd5f45 | Iustin Pop | _OP_REQP = ["instance_name", "new_name"] |
3802 | decd5f45 | Iustin Pop | |
3803 | decd5f45 | Iustin Pop | def BuildHooksEnv(self): |
3804 | decd5f45 | Iustin Pop | """Build hooks env.
|
3805 | decd5f45 | Iustin Pop |
|
3806 | decd5f45 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3807 | decd5f45 | Iustin Pop |
|
3808 | decd5f45 | Iustin Pop | """
|
3809 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
3810 | decd5f45 | Iustin Pop | env["INSTANCE_NEW_NAME"] = self.op.new_name |
3811 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
3812 | decd5f45 | Iustin Pop | return env, nl, nl
|
3813 | decd5f45 | Iustin Pop | |
3814 | decd5f45 | Iustin Pop | def CheckPrereq(self): |
3815 | decd5f45 | Iustin Pop | """Check prerequisites.
|
3816 | decd5f45 | Iustin Pop |
|
3817 | decd5f45 | Iustin Pop | This checks that the instance is in the cluster and is not running.
|
3818 | decd5f45 | Iustin Pop |
|
3819 | decd5f45 | Iustin Pop | """
|
3820 | decd5f45 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
3821 | decd5f45 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
3822 | decd5f45 | Iustin Pop | if instance is None: |
3823 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
3824 | decd5f45 | Iustin Pop | self.op.instance_name)
|
3825 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, instance.primary_node)
|
3826 | 7527a8a4 | Iustin Pop | |
3827 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
3828 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is marked to be up" % |
3829 | decd5f45 | Iustin Pop | self.op.instance_name)
|
3830 | 72737a7f | Iustin Pop | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
3831 | 72737a7f | Iustin Pop | instance.name, |
3832 | 72737a7f | Iustin Pop | instance.hypervisor) |
3833 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node,
|
3834 | 4c4e4e1e | Iustin Pop | prereq=True)
|
3835 | 7ad1af4a | Iustin Pop | if remote_info.payload:
|
3836 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is running on the node %s" % |
3837 | decd5f45 | Iustin Pop | (self.op.instance_name,
|
3838 | decd5f45 | Iustin Pop | instance.primary_node)) |
3839 | decd5f45 | Iustin Pop | self.instance = instance
|
3840 | decd5f45 | Iustin Pop | |
3841 | decd5f45 | Iustin Pop | # new name verification
|
3842 | 89e1fc26 | Iustin Pop | name_info = utils.HostInfo(self.op.new_name)
|
3843 | decd5f45 | Iustin Pop | |
3844 | 89e1fc26 | Iustin Pop | self.op.new_name = new_name = name_info.name
|
3845 | 7bde3275 | Guido Trotter | instance_list = self.cfg.GetInstanceList()
|
3846 | 7bde3275 | Guido Trotter | if new_name in instance_list: |
3847 | 7bde3275 | Guido Trotter | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
3848 | c09f363f | Manuel Franceschini | new_name) |
3849 | 7bde3275 | Guido Trotter | |
3850 | decd5f45 | Iustin Pop | if not getattr(self.op, "ignore_ip", False): |
3851 | 937f983d | Guido Trotter | if utils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
|
3852 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
3853 | 89e1fc26 | Iustin Pop | (name_info.ip, new_name)) |
3854 | decd5f45 | Iustin Pop | |
3855 | decd5f45 | Iustin Pop | |
3856 | decd5f45 | Iustin Pop | def Exec(self, feedback_fn): |
3857 | decd5f45 | Iustin Pop | """Reinstall the instance.
|
3858 | decd5f45 | Iustin Pop |
|
3859 | decd5f45 | Iustin Pop | """
|
3860 | decd5f45 | Iustin Pop | inst = self.instance
|
3861 | decd5f45 | Iustin Pop | old_name = inst.name |
3862 | decd5f45 | Iustin Pop | |
3863 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
3864 | b23c4333 | Manuel Franceschini | old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
3865 | b23c4333 | Manuel Franceschini | |
3866 | decd5f45 | Iustin Pop | self.cfg.RenameInstance(inst.name, self.op.new_name) |
3867 | 74b5913f | Guido Trotter | # Change the instance lock. This is definitely safe while we hold the BGL
|
3868 | cb4e8387 | Iustin Pop | self.context.glm.remove(locking.LEVEL_INSTANCE, old_name)
|
3869 | 74b5913f | Guido Trotter | self.context.glm.add(locking.LEVEL_INSTANCE, self.op.new_name) |
3870 | decd5f45 | Iustin Pop | |
3871 | decd5f45 | Iustin Pop | # re-read the instance from the configuration after rename
|
3872 | decd5f45 | Iustin Pop | inst = self.cfg.GetInstanceInfo(self.op.new_name) |
3873 | decd5f45 | Iustin Pop | |
3874 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
3875 | b23c4333 | Manuel Franceschini | new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
3876 | 72737a7f | Iustin Pop | result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
|
3877 | 72737a7f | Iustin Pop | old_file_storage_dir, |
3878 | 72737a7f | Iustin Pop | new_file_storage_dir) |
3879 | 4c4e4e1e | Iustin Pop | result.Raise("Could not rename on node %s directory '%s' to '%s'"
|
3880 | 4c4e4e1e | Iustin Pop | " (but the instance has been renamed in Ganeti)" %
|
3881 | 4c4e4e1e | Iustin Pop | (inst.primary_node, old_file_storage_dir, |
3882 | 4c4e4e1e | Iustin Pop | new_file_storage_dir)) |
3883 | b23c4333 | Manuel Franceschini | |
3884 | b9bddb6b | Iustin Pop | _StartInstanceDisks(self, inst, None) |
3885 | decd5f45 | Iustin Pop | try:
|
3886 | 781de953 | Iustin Pop | result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
|
3887 | 781de953 | Iustin Pop | old_name) |
3888 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3889 | 96841384 | Iustin Pop | if msg:
|
3890 | 6291574d | Alexander Schreiber | msg = ("Could not run OS rename script for instance %s on node %s"
|
3891 | 96841384 | Iustin Pop | " (but the instance has been renamed in Ganeti): %s" %
|
3892 | 96841384 | Iustin Pop | (inst.name, inst.primary_node, msg)) |
3893 | 86d9d3bb | Iustin Pop | self.proc.LogWarning(msg)
|
3894 | decd5f45 | Iustin Pop | finally:
|
3895 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, inst)
|
3896 | decd5f45 | Iustin Pop | |
3897 | decd5f45 | Iustin Pop | |
3898 | a8083063 | Iustin Pop | class LURemoveInstance(LogicalUnit): |
3899 | a8083063 | Iustin Pop | """Remove an instance.
|
3900 | a8083063 | Iustin Pop |
|
3901 | a8083063 | Iustin Pop | """
|
3902 | a8083063 | Iustin Pop | HPATH = "instance-remove"
|
3903 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3904 | 5c54b832 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_failures"] |
3905 | cf472233 | Guido Trotter | REQ_BGL = False
|
3906 | cf472233 | Guido Trotter | |
3907 | cf472233 | Guido Trotter | def ExpandNames(self): |
3908 | cf472233 | Guido Trotter | self._ExpandAndLockInstance()
|
3909 | cf472233 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
3910 | cf472233 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
3911 | cf472233 | Guido Trotter | |
3912 | cf472233 | Guido Trotter | def DeclareLocks(self, level): |
3913 | cf472233 | Guido Trotter | if level == locking.LEVEL_NODE:
|
3914 | cf472233 | Guido Trotter | self._LockInstancesNodes()
|
3915 | a8083063 | Iustin Pop | |
3916 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3917 | a8083063 | Iustin Pop | """Build hooks env.
|
3918 | a8083063 | Iustin Pop |
|
3919 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3920 | a8083063 | Iustin Pop |
|
3921 | a8083063 | Iustin Pop | """
|
3922 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance) |
3923 | d6a02168 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
3924 | a8083063 | Iustin Pop | return env, nl, nl
|
3925 | a8083063 | Iustin Pop | |
3926 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3927 | a8083063 | Iustin Pop | """Check prerequisites.
|
3928 | a8083063 | Iustin Pop |
|
3929 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3930 | a8083063 | Iustin Pop |
|
3931 | a8083063 | Iustin Pop | """
|
3932 | cf472233 | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
3933 | cf472233 | Guido Trotter | assert self.instance is not None, \ |
3934 | cf472233 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
3935 | a8083063 | Iustin Pop | |
3936 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3937 | a8083063 | Iustin Pop | """Remove the instance.
|
3938 | a8083063 | Iustin Pop |
|
3939 | a8083063 | Iustin Pop | """
|
3940 | a8083063 | Iustin Pop | instance = self.instance
|
3941 | 9a4f63d1 | Iustin Pop | logging.info("Shutting down instance %s on node %s",
|
3942 | 9a4f63d1 | Iustin Pop | instance.name, instance.primary_node) |
3943 | a8083063 | Iustin Pop | |
3944 | 781de953 | Iustin Pop | result = self.rpc.call_instance_shutdown(instance.primary_node, instance)
|
3945 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
3946 | 1fae010f | Iustin Pop | if msg:
|
3947 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
3948 | 1fae010f | Iustin Pop | feedback_fn("Warning: can't shutdown instance: %s" % msg)
|
3949 | 1d67656e | Iustin Pop | else:
|
3950 | 1fae010f | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
3951 | 1fae010f | Iustin Pop | " node %s: %s" %
|
3952 | 1fae010f | Iustin Pop | (instance.name, instance.primary_node, msg)) |
3953 | a8083063 | Iustin Pop | |
3954 | 9a4f63d1 | Iustin Pop | logging.info("Removing block devices for instance %s", instance.name)
|
3955 | a8083063 | Iustin Pop | |
3956 | b9bddb6b | Iustin Pop | if not _RemoveDisks(self, instance): |
3957 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
3958 | 1d67656e | Iustin Pop | feedback_fn("Warning: can't remove instance's disks")
|
3959 | 1d67656e | Iustin Pop | else:
|
3960 | 1d67656e | Iustin Pop | raise errors.OpExecError("Can't remove instance's disks") |
3961 | a8083063 | Iustin Pop | |
3962 | 9a4f63d1 | Iustin Pop | logging.info("Removing instance %s out of cluster config", instance.name)
|
3963 | a8083063 | Iustin Pop | |
3964 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(instance.name)
|
3965 | cf472233 | Guido Trotter | self.remove_locks[locking.LEVEL_INSTANCE] = instance.name
|
3966 | a8083063 | Iustin Pop | |
3967 | a8083063 | Iustin Pop | |
3968 | a8083063 | Iustin Pop | class LUQueryInstances(NoHooksLU): |
3969 | a8083063 | Iustin Pop | """Logical unit for querying instances.
|
3970 | a8083063 | Iustin Pop |
|
3971 | a8083063 | Iustin Pop | """
|
3972 | ec79568d | Iustin Pop | _OP_REQP = ["output_fields", "names", "use_locking"] |
3973 | 7eb9d8f7 | Guido Trotter | REQ_BGL = False
|
3974 | 19bed813 | Iustin Pop | _SIMPLE_FIELDS = ["name", "os", "network_port", "hypervisor", |
3975 | 19bed813 | Iustin Pop | "serial_no", "ctime", "mtime", "uuid"] |
3976 | a2d2e1a7 | Iustin Pop | _FIELDS_STATIC = utils.FieldSet(*["name", "os", "pnode", "snodes", |
3977 | 5b460366 | Iustin Pop | "admin_state",
|
3978 | a2d2e1a7 | Iustin Pop | "disk_template", "ip", "mac", "bridge", |
3979 | 638c6349 | Guido Trotter | "nic_mode", "nic_link", |
3980 | a2d2e1a7 | Iustin Pop | "sda_size", "sdb_size", "vcpus", "tags", |
3981 | a2d2e1a7 | Iustin Pop | "network_port", "beparams", |
3982 | 8aec325c | Iustin Pop | r"(disk)\.(size)/([0-9]+)",
|
3983 | 8aec325c | Iustin Pop | r"(disk)\.(sizes)", "disk_usage", |
3984 | 638c6349 | Guido Trotter | r"(nic)\.(mac|ip|mode|link)/([0-9]+)",
|
3985 | 638c6349 | Guido Trotter | r"(nic)\.(bridge)/([0-9]+)",
|
3986 | 638c6349 | Guido Trotter | r"(nic)\.(macs|ips|modes|links|bridges)",
|
3987 | 8aec325c | Iustin Pop | r"(disk|nic)\.(count)",
|
3988 | 19bed813 | Iustin Pop | "hvparams",
|
3989 | 19bed813 | Iustin Pop | ] + _SIMPLE_FIELDS + |
3990 | a2d2e1a7 | Iustin Pop | ["hv/%s" % name
|
3991 | a2d2e1a7 | Iustin Pop | for name in constants.HVS_PARAMETERS] + |
3992 | a2d2e1a7 | Iustin Pop | ["be/%s" % name
|
3993 | a2d2e1a7 | Iustin Pop | for name in constants.BES_PARAMETERS]) |
3994 | a2d2e1a7 | Iustin Pop | _FIELDS_DYNAMIC = utils.FieldSet("oper_state", "oper_ram", "status") |
3995 | 31bf511f | Iustin Pop | |
3996 | a8083063 | Iustin Pop | |
3997 | 7eb9d8f7 | Guido Trotter | def ExpandNames(self): |
3998 | 31bf511f | Iustin Pop | _CheckOutputFields(static=self._FIELDS_STATIC,
|
3999 | 31bf511f | Iustin Pop | dynamic=self._FIELDS_DYNAMIC,
|
4000 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
4001 | a8083063 | Iustin Pop | |
4002 | 7eb9d8f7 | Guido Trotter | self.needed_locks = {}
|
4003 | 7eb9d8f7 | Guido Trotter | self.share_locks[locking.LEVEL_INSTANCE] = 1 |
4004 | 7eb9d8f7 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
4005 | 7eb9d8f7 | Guido Trotter | |
4006 | 57a2fb91 | Iustin Pop | if self.op.names: |
4007 | 57a2fb91 | Iustin Pop | self.wanted = _GetWantedInstances(self, self.op.names) |
4008 | 7eb9d8f7 | Guido Trotter | else:
|
4009 | 57a2fb91 | Iustin Pop | self.wanted = locking.ALL_SET
|
4010 | 7eb9d8f7 | Guido Trotter | |
4011 | ec79568d | Iustin Pop | self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields) |
4012 | ec79568d | Iustin Pop | self.do_locking = self.do_node_query and self.op.use_locking |
4013 | 57a2fb91 | Iustin Pop | if self.do_locking: |
4014 | 57a2fb91 | Iustin Pop | self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted |
4015 | 57a2fb91 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
4016 | 57a2fb91 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4017 | 7eb9d8f7 | Guido Trotter | |
4018 | 7eb9d8f7 | Guido Trotter | def DeclareLocks(self, level): |
4019 | 57a2fb91 | Iustin Pop | if level == locking.LEVEL_NODE and self.do_locking: |
4020 | 7eb9d8f7 | Guido Trotter | self._LockInstancesNodes()
|
4021 | 7eb9d8f7 | Guido Trotter | |
4022 | 7eb9d8f7 | Guido Trotter | def CheckPrereq(self): |
4023 | 7eb9d8f7 | Guido Trotter | """Check prerequisites.
|
4024 | 7eb9d8f7 | Guido Trotter |
|
4025 | 7eb9d8f7 | Guido Trotter | """
|
4026 | 57a2fb91 | Iustin Pop | pass
|
4027 | 069dcc86 | Iustin Pop | |
4028 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4029 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
4030 | a8083063 | Iustin Pop |
|
4031 | a8083063 | Iustin Pop | """
|
4032 | 57a2fb91 | Iustin Pop | all_info = self.cfg.GetAllInstancesInfo()
|
4033 | a7f5dc98 | Iustin Pop | if self.wanted == locking.ALL_SET: |
4034 | a7f5dc98 | Iustin Pop | # caller didn't specify instance names, so ordering is not important
|
4035 | a7f5dc98 | Iustin Pop | if self.do_locking: |
4036 | a7f5dc98 | Iustin Pop | instance_names = self.acquired_locks[locking.LEVEL_INSTANCE]
|
4037 | a7f5dc98 | Iustin Pop | else:
|
4038 | a7f5dc98 | Iustin Pop | instance_names = all_info.keys() |
4039 | a7f5dc98 | Iustin Pop | instance_names = utils.NiceSort(instance_names) |
4040 | 57a2fb91 | Iustin Pop | else:
|
4041 | a7f5dc98 | Iustin Pop | # caller did specify names, so we must keep the ordering
|
4042 | a7f5dc98 | Iustin Pop | if self.do_locking: |
4043 | a7f5dc98 | Iustin Pop | tgt_set = self.acquired_locks[locking.LEVEL_INSTANCE]
|
4044 | a7f5dc98 | Iustin Pop | else:
|
4045 | a7f5dc98 | Iustin Pop | tgt_set = all_info.keys() |
4046 | a7f5dc98 | Iustin Pop | missing = set(self.wanted).difference(tgt_set) |
4047 | a7f5dc98 | Iustin Pop | if missing:
|
4048 | a7f5dc98 | Iustin Pop | raise errors.OpExecError("Some instances were removed before" |
4049 | a7f5dc98 | Iustin Pop | " retrieving their data: %s" % missing)
|
4050 | a7f5dc98 | Iustin Pop | instance_names = self.wanted
|
4051 | c1f1cbb2 | Iustin Pop | |
4052 | 57a2fb91 | Iustin Pop | instance_list = [all_info[iname] for iname in instance_names] |
4053 | a8083063 | Iustin Pop | |
4054 | a8083063 | Iustin Pop | # begin data gathering
|
4055 | a8083063 | Iustin Pop | |
4056 | a8083063 | Iustin Pop | nodes = frozenset([inst.primary_node for inst in instance_list]) |
4057 | e69d05fd | Iustin Pop | hv_list = list(set([inst.hypervisor for inst in instance_list])) |
4058 | a8083063 | Iustin Pop | |
4059 | a8083063 | Iustin Pop | bad_nodes = [] |
4060 | cbfc4681 | Iustin Pop | off_nodes = [] |
4061 | ec79568d | Iustin Pop | if self.do_node_query: |
4062 | a8083063 | Iustin Pop | live_data = {} |
4063 | 72737a7f | Iustin Pop | node_data = self.rpc.call_all_instances_info(nodes, hv_list)
|
4064 | a8083063 | Iustin Pop | for name in nodes: |
4065 | a8083063 | Iustin Pop | result = node_data[name] |
4066 | cbfc4681 | Iustin Pop | if result.offline:
|
4067 | cbfc4681 | Iustin Pop | # offline nodes will be in both lists
|
4068 | cbfc4681 | Iustin Pop | off_nodes.append(name) |
4069 | 3cebe102 | Michael Hanselmann | if result.fail_msg:
|
4070 | a8083063 | Iustin Pop | bad_nodes.append(name) |
4071 | 781de953 | Iustin Pop | else:
|
4072 | 2fa74ef4 | Iustin Pop | if result.payload:
|
4073 | 2fa74ef4 | Iustin Pop | live_data.update(result.payload) |
4074 | 2fa74ef4 | Iustin Pop | # else no instance is alive
|
4075 | a8083063 | Iustin Pop | else:
|
4076 | a8083063 | Iustin Pop | live_data = dict([(name, {}) for name in instance_names]) |
4077 | a8083063 | Iustin Pop | |
4078 | a8083063 | Iustin Pop | # end data gathering
|
4079 | a8083063 | Iustin Pop | |
4080 | 5018a335 | Iustin Pop | HVPREFIX = "hv/"
|
4081 | 338e51e8 | Iustin Pop | BEPREFIX = "be/"
|
4082 | a8083063 | Iustin Pop | output = [] |
4083 | 638c6349 | Guido Trotter | cluster = self.cfg.GetClusterInfo()
|
4084 | a8083063 | Iustin Pop | for instance in instance_list: |
4085 | a8083063 | Iustin Pop | iout = [] |
4086 | 638c6349 | Guido Trotter | i_hv = cluster.FillHV(instance) |
4087 | 638c6349 | Guido Trotter | i_be = cluster.FillBE(instance) |
4088 | 638c6349 | Guido Trotter | i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], |
4089 | 638c6349 | Guido Trotter | nic.nicparams) for nic in instance.nics] |
4090 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
4091 | 71c1af58 | Iustin Pop | st_match = self._FIELDS_STATIC.Matches(field)
|
4092 | 19bed813 | Iustin Pop | if field in self._SIMPLE_FIELDS: |
4093 | 19bed813 | Iustin Pop | val = getattr(instance, field)
|
4094 | a8083063 | Iustin Pop | elif field == "pnode": |
4095 | a8083063 | Iustin Pop | val = instance.primary_node |
4096 | a8083063 | Iustin Pop | elif field == "snodes": |
4097 | 8a23d2d3 | Iustin Pop | val = list(instance.secondary_nodes)
|
4098 | a8083063 | Iustin Pop | elif field == "admin_state": |
4099 | 0d68c45d | Iustin Pop | val = instance.admin_up |
4100 | a8083063 | Iustin Pop | elif field == "oper_state": |
4101 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
4102 | 8a23d2d3 | Iustin Pop | val = None
|
4103 | a8083063 | Iustin Pop | else:
|
4104 | 8a23d2d3 | Iustin Pop | val = bool(live_data.get(instance.name))
|
4105 | d8052456 | Iustin Pop | elif field == "status": |
4106 | cbfc4681 | Iustin Pop | if instance.primary_node in off_nodes: |
4107 | cbfc4681 | Iustin Pop | val = "ERROR_nodeoffline"
|
4108 | cbfc4681 | Iustin Pop | elif instance.primary_node in bad_nodes: |
4109 | d8052456 | Iustin Pop | val = "ERROR_nodedown"
|
4110 | d8052456 | Iustin Pop | else:
|
4111 | d8052456 | Iustin Pop | running = bool(live_data.get(instance.name))
|
4112 | d8052456 | Iustin Pop | if running:
|
4113 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
4114 | d8052456 | Iustin Pop | val = "running"
|
4115 | d8052456 | Iustin Pop | else:
|
4116 | d8052456 | Iustin Pop | val = "ERROR_up"
|
4117 | d8052456 | Iustin Pop | else:
|
4118 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
4119 | d8052456 | Iustin Pop | val = "ERROR_down"
|
4120 | d8052456 | Iustin Pop | else:
|
4121 | d8052456 | Iustin Pop | val = "ADMIN_down"
|
4122 | a8083063 | Iustin Pop | elif field == "oper_ram": |
4123 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
4124 | 8a23d2d3 | Iustin Pop | val = None
|
4125 | a8083063 | Iustin Pop | elif instance.name in live_data: |
4126 | a8083063 | Iustin Pop | val = live_data[instance.name].get("memory", "?") |
4127 | a8083063 | Iustin Pop | else:
|
4128 | a8083063 | Iustin Pop | val = "-"
|
4129 | c1ce76bb | Iustin Pop | elif field == "vcpus": |
4130 | c1ce76bb | Iustin Pop | val = i_be[constants.BE_VCPUS] |
4131 | a8083063 | Iustin Pop | elif field == "disk_template": |
4132 | a8083063 | Iustin Pop | val = instance.disk_template |
4133 | a8083063 | Iustin Pop | elif field == "ip": |
4134 | 39a02558 | Guido Trotter | if instance.nics:
|
4135 | 39a02558 | Guido Trotter | val = instance.nics[0].ip
|
4136 | 39a02558 | Guido Trotter | else:
|
4137 | 39a02558 | Guido Trotter | val = None
|
4138 | 638c6349 | Guido Trotter | elif field == "nic_mode": |
4139 | 638c6349 | Guido Trotter | if instance.nics:
|
4140 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_MODE]
|
4141 | 638c6349 | Guido Trotter | else:
|
4142 | 638c6349 | Guido Trotter | val = None
|
4143 | 638c6349 | Guido Trotter | elif field == "nic_link": |
4144 | 39a02558 | Guido Trotter | if instance.nics:
|
4145 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_LINK]
|
4146 | 638c6349 | Guido Trotter | else:
|
4147 | 638c6349 | Guido Trotter | val = None
|
4148 | 638c6349 | Guido Trotter | elif field == "bridge": |
4149 | 638c6349 | Guido Trotter | if (instance.nics and |
4150 | 638c6349 | Guido Trotter | i_nicp[0][constants.NIC_MODE] == constants.NIC_MODE_BRIDGED):
|
4151 | 638c6349 | Guido Trotter | val = i_nicp[0][constants.NIC_LINK]
|
4152 | 39a02558 | Guido Trotter | else:
|
4153 | 39a02558 | Guido Trotter | val = None
|
4154 | a8083063 | Iustin Pop | elif field == "mac": |
4155 | 39a02558 | Guido Trotter | if instance.nics:
|
4156 | 39a02558 | Guido Trotter | val = instance.nics[0].mac
|
4157 | 39a02558 | Guido Trotter | else:
|
4158 | 39a02558 | Guido Trotter | val = None
|
4159 | 644eeef9 | Iustin Pop | elif field == "sda_size" or field == "sdb_size": |
4160 | ad24e046 | Iustin Pop | idx = ord(field[2]) - ord('a') |
4161 | ad24e046 | Iustin Pop | try:
|
4162 | ad24e046 | Iustin Pop | val = instance.FindDisk(idx).size |
4163 | ad24e046 | Iustin Pop | except errors.OpPrereqError:
|
4164 | 8a23d2d3 | Iustin Pop | val = None
|
4165 | 024e157f | Iustin Pop | elif field == "disk_usage": # total disk usage per node |
4166 | 024e157f | Iustin Pop | disk_sizes = [{'size': disk.size} for disk in instance.disks] |
4167 | 024e157f | Iustin Pop | val = _ComputeDiskSize(instance.disk_template, disk_sizes) |
4168 | 130a6a6f | Iustin Pop | elif field == "tags": |
4169 | 130a6a6f | Iustin Pop | val = list(instance.GetTags())
|
4170 | 338e51e8 | Iustin Pop | elif field == "hvparams": |
4171 | 338e51e8 | Iustin Pop | val = i_hv |
4172 | 5018a335 | Iustin Pop | elif (field.startswith(HVPREFIX) and |
4173 | 5018a335 | Iustin Pop | field[len(HVPREFIX):] in constants.HVS_PARAMETERS): |
4174 | 5018a335 | Iustin Pop | val = i_hv.get(field[len(HVPREFIX):], None) |
4175 | 338e51e8 | Iustin Pop | elif field == "beparams": |
4176 | 338e51e8 | Iustin Pop | val = i_be |
4177 | 338e51e8 | Iustin Pop | elif (field.startswith(BEPREFIX) and |
4178 | 338e51e8 | Iustin Pop | field[len(BEPREFIX):] in constants.BES_PARAMETERS): |
4179 | 338e51e8 | Iustin Pop | val = i_be.get(field[len(BEPREFIX):], None) |
4180 | 71c1af58 | Iustin Pop | elif st_match and st_match.groups(): |
4181 | 71c1af58 | Iustin Pop | # matches a variable list
|
4182 | 71c1af58 | Iustin Pop | st_groups = st_match.groups() |
4183 | 71c1af58 | Iustin Pop | if st_groups and st_groups[0] == "disk": |
4184 | 71c1af58 | Iustin Pop | if st_groups[1] == "count": |
4185 | 71c1af58 | Iustin Pop | val = len(instance.disks)
|
4186 | 41a776da | Iustin Pop | elif st_groups[1] == "sizes": |
4187 | 41a776da | Iustin Pop | val = [disk.size for disk in instance.disks] |
4188 | 71c1af58 | Iustin Pop | elif st_groups[1] == "size": |
4189 | 3e0cea06 | Iustin Pop | try:
|
4190 | 3e0cea06 | Iustin Pop | val = instance.FindDisk(st_groups[2]).size
|
4191 | 3e0cea06 | Iustin Pop | except errors.OpPrereqError:
|
4192 | 71c1af58 | Iustin Pop | val = None
|
4193 | 71c1af58 | Iustin Pop | else:
|
4194 | 71c1af58 | Iustin Pop | assert False, "Unhandled disk parameter" |
4195 | 71c1af58 | Iustin Pop | elif st_groups[0] == "nic": |
4196 | 71c1af58 | Iustin Pop | if st_groups[1] == "count": |
4197 | 71c1af58 | Iustin Pop | val = len(instance.nics)
|
4198 | 41a776da | Iustin Pop | elif st_groups[1] == "macs": |
4199 | 41a776da | Iustin Pop | val = [nic.mac for nic in instance.nics] |
4200 | 41a776da | Iustin Pop | elif st_groups[1] == "ips": |
4201 | 41a776da | Iustin Pop | val = [nic.ip for nic in instance.nics] |
4202 | 638c6349 | Guido Trotter | elif st_groups[1] == "modes": |
4203 | 638c6349 | Guido Trotter | val = [nicp[constants.NIC_MODE] for nicp in i_nicp] |
4204 | 638c6349 | Guido Trotter | elif st_groups[1] == "links": |
4205 | 638c6349 | Guido Trotter | val = [nicp[constants.NIC_LINK] for nicp in i_nicp] |
4206 | 41a776da | Iustin Pop | elif st_groups[1] == "bridges": |
4207 | 638c6349 | Guido Trotter | val = [] |
4208 | 638c6349 | Guido Trotter | for nicp in i_nicp: |
4209 | 638c6349 | Guido Trotter | if nicp[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
4210 | 638c6349 | Guido Trotter | val.append(nicp[constants.NIC_LINK]) |
4211 | 638c6349 | Guido Trotter | else:
|
4212 | 638c6349 | Guido Trotter | val.append(None)
|
4213 | 71c1af58 | Iustin Pop | else:
|
4214 | 71c1af58 | Iustin Pop | # index-based item
|
4215 | 71c1af58 | Iustin Pop | nic_idx = int(st_groups[2]) |
4216 | 71c1af58 | Iustin Pop | if nic_idx >= len(instance.nics): |
4217 | 71c1af58 | Iustin Pop | val = None
|
4218 | 71c1af58 | Iustin Pop | else:
|
4219 | 71c1af58 | Iustin Pop | if st_groups[1] == "mac": |
4220 | 71c1af58 | Iustin Pop | val = instance.nics[nic_idx].mac |
4221 | 71c1af58 | Iustin Pop | elif st_groups[1] == "ip": |
4222 | 71c1af58 | Iustin Pop | val = instance.nics[nic_idx].ip |
4223 | 638c6349 | Guido Trotter | elif st_groups[1] == "mode": |
4224 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_MODE] |
4225 | 638c6349 | Guido Trotter | elif st_groups[1] == "link": |
4226 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_LINK] |
4227 | 71c1af58 | Iustin Pop | elif st_groups[1] == "bridge": |
4228 | 638c6349 | Guido Trotter | nic_mode = i_nicp[nic_idx][constants.NIC_MODE] |
4229 | 638c6349 | Guido Trotter | if nic_mode == constants.NIC_MODE_BRIDGED:
|
4230 | 638c6349 | Guido Trotter | val = i_nicp[nic_idx][constants.NIC_LINK] |
4231 | 638c6349 | Guido Trotter | else:
|
4232 | 638c6349 | Guido Trotter | val = None
|
4233 | 71c1af58 | Iustin Pop | else:
|
4234 | 71c1af58 | Iustin Pop | assert False, "Unhandled NIC parameter" |
4235 | 71c1af58 | Iustin Pop | else:
|
4236 | c1ce76bb | Iustin Pop | assert False, ("Declared but unhandled variable parameter '%s'" % |
4237 | c1ce76bb | Iustin Pop | field) |
4238 | a8083063 | Iustin Pop | else:
|
4239 | c1ce76bb | Iustin Pop | assert False, "Declared but unhandled parameter '%s'" % field |
4240 | a8083063 | Iustin Pop | iout.append(val) |
4241 | a8083063 | Iustin Pop | output.append(iout) |
4242 | a8083063 | Iustin Pop | |
4243 | a8083063 | Iustin Pop | return output
|
4244 | a8083063 | Iustin Pop | |
4245 | a8083063 | Iustin Pop | |
4246 | a8083063 | Iustin Pop | class LUFailoverInstance(LogicalUnit): |
4247 | a8083063 | Iustin Pop | """Failover an instance.
|
4248 | a8083063 | Iustin Pop |
|
4249 | a8083063 | Iustin Pop | """
|
4250 | a8083063 | Iustin Pop | HPATH = "instance-failover"
|
4251 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4252 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_consistency"] |
4253 | c9e5c064 | Guido Trotter | REQ_BGL = False
|
4254 | c9e5c064 | Guido Trotter | |
4255 | c9e5c064 | Guido Trotter | def ExpandNames(self): |
4256 | c9e5c064 | Guido Trotter | self._ExpandAndLockInstance()
|
4257 | c9e5c064 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
4258 | f6d9a522 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4259 | c9e5c064 | Guido Trotter | |
4260 | c9e5c064 | Guido Trotter | def DeclareLocks(self, level): |
4261 | c9e5c064 | Guido Trotter | if level == locking.LEVEL_NODE:
|
4262 | c9e5c064 | Guido Trotter | self._LockInstancesNodes()
|
4263 | a8083063 | Iustin Pop | |
4264 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4265 | a8083063 | Iustin Pop | """Build hooks env.
|
4266 | a8083063 | Iustin Pop |
|
4267 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4268 | a8083063 | Iustin Pop |
|
4269 | a8083063 | Iustin Pop | """
|
4270 | a8083063 | Iustin Pop | env = { |
4271 | a8083063 | Iustin Pop | "IGNORE_CONSISTENCY": self.op.ignore_consistency, |
4272 | a8083063 | Iustin Pop | } |
4273 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
4274 | d6a02168 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()] + list(self.instance.secondary_nodes) |
4275 | a8083063 | Iustin Pop | return env, nl, nl
|
4276 | a8083063 | Iustin Pop | |
4277 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4278 | a8083063 | Iustin Pop | """Check prerequisites.
|
4279 | a8083063 | Iustin Pop |
|
4280 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
4281 | a8083063 | Iustin Pop |
|
4282 | a8083063 | Iustin Pop | """
|
4283 | c9e5c064 | Guido Trotter | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4284 | c9e5c064 | Guido Trotter | assert self.instance is not None, \ |
4285 | c9e5c064 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
4286 | a8083063 | Iustin Pop | |
4287 | 338e51e8 | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
4288 | a1f445d3 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
4289 | 2a710df1 | Michael Hanselmann | raise errors.OpPrereqError("Instance's disk layout is not" |
4290 | a1f445d3 | Iustin Pop | " network mirrored, cannot failover.")
|
4291 | 2a710df1 | Michael Hanselmann | |
4292 | 2a710df1 | Michael Hanselmann | secondary_nodes = instance.secondary_nodes |
4293 | 2a710df1 | Michael Hanselmann | if not secondary_nodes: |
4294 | 2a710df1 | Michael Hanselmann | raise errors.ProgrammerError("no secondary node but using " |
4295 | abdf0113 | Iustin Pop | "a mirrored disk template")
|
4296 | 2a710df1 | Michael Hanselmann | |
4297 | 2a710df1 | Michael Hanselmann | target_node = secondary_nodes[0]
|
4298 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, target_node)
|
4299 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
4300 | d27776f0 | Iustin Pop | if instance.admin_up:
|
4301 | d27776f0 | Iustin Pop | # check memory requirements on the secondary node
|
4302 | d27776f0 | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "failing over instance %s" % |
4303 | d27776f0 | Iustin Pop | instance.name, bep[constants.BE_MEMORY], |
4304 | d27776f0 | Iustin Pop | instance.hypervisor) |
4305 | d27776f0 | Iustin Pop | else:
|
4306 | d27776f0 | Iustin Pop | self.LogInfo("Not checking memory on the secondary node as" |
4307 | d27776f0 | Iustin Pop | " instance will not be started")
|
4308 | 3a7c308e | Guido Trotter | |
4309 | a8083063 | Iustin Pop | # check bridge existance
|
4310 | b165e77e | Guido Trotter | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
4311 | a8083063 | Iustin Pop | |
4312 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4313 | a8083063 | Iustin Pop | """Failover an instance.
|
4314 | a8083063 | Iustin Pop |
|
4315 | a8083063 | Iustin Pop | The failover is done by shutting it down on its present node and
|
4316 | a8083063 | Iustin Pop | starting it on the secondary.
|
4317 | a8083063 | Iustin Pop |
|
4318 | a8083063 | Iustin Pop | """
|
4319 | a8083063 | Iustin Pop | instance = self.instance
|
4320 | a8083063 | Iustin Pop | |
4321 | a8083063 | Iustin Pop | source_node = instance.primary_node |
4322 | a8083063 | Iustin Pop | target_node = instance.secondary_nodes[0]
|
4323 | a8083063 | Iustin Pop | |
4324 | a8083063 | Iustin Pop | feedback_fn("* checking disk consistency between source and target")
|
4325 | a8083063 | Iustin Pop | for dev in instance.disks: |
4326 | abdf0113 | Iustin Pop | # for drbd, these are drbd over lvm
|
4327 | b9bddb6b | Iustin Pop | if not _CheckDiskConsistency(self, dev, target_node, False): |
4328 | 0d68c45d | Iustin Pop | if instance.admin_up and not self.op.ignore_consistency: |
4329 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Disk %s is degraded on target node," |
4330 | 3ecf6786 | Iustin Pop | " aborting failover." % dev.iv_name)
|
4331 | a8083063 | Iustin Pop | |
4332 | a8083063 | Iustin Pop | feedback_fn("* shutting down instance on source node")
|
4333 | 9a4f63d1 | Iustin Pop | logging.info("Shutting down instance %s on node %s",
|
4334 | 9a4f63d1 | Iustin Pop | instance.name, source_node) |
4335 | a8083063 | Iustin Pop | |
4336 | 781de953 | Iustin Pop | result = self.rpc.call_instance_shutdown(source_node, instance)
|
4337 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4338 | 1fae010f | Iustin Pop | if msg:
|
4339 | 24a40d57 | Iustin Pop | if self.op.ignore_consistency: |
4340 | 86d9d3bb | Iustin Pop | self.proc.LogWarning("Could not shutdown instance %s on node %s." |
4341 | 1fae010f | Iustin Pop | " Proceeding anyway. Please make sure node"
|
4342 | 1fae010f | Iustin Pop | " %s is down. Error details: %s",
|
4343 | 1fae010f | Iustin Pop | instance.name, source_node, source_node, msg) |
4344 | 24a40d57 | Iustin Pop | else:
|
4345 | 1fae010f | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
4346 | 1fae010f | Iustin Pop | " node %s: %s" %
|
4347 | 1fae010f | Iustin Pop | (instance.name, source_node, msg)) |
4348 | a8083063 | Iustin Pop | |
4349 | a8083063 | Iustin Pop | feedback_fn("* deactivating the instance's disks on source node")
|
4350 | b9bddb6b | Iustin Pop | if not _ShutdownInstanceDisks(self, instance, ignore_primary=True): |
4351 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't shut down the instance's disks.") |
4352 | a8083063 | Iustin Pop | |
4353 | a8083063 | Iustin Pop | instance.primary_node = target_node |
4354 | a8083063 | Iustin Pop | # distribute new instance config to the other nodes
|
4355 | b6102dab | Guido Trotter | self.cfg.Update(instance)
|
4356 | a8083063 | Iustin Pop | |
4357 | 12a0cfbe | Guido Trotter | # Only start the instance if it's marked as up
|
4358 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
4359 | 12a0cfbe | Guido Trotter | feedback_fn("* activating the instance's disks on target node")
|
4360 | 9a4f63d1 | Iustin Pop | logging.info("Starting instance %s on node %s",
|
4361 | 9a4f63d1 | Iustin Pop | instance.name, target_node) |
4362 | 12a0cfbe | Guido Trotter | |
4363 | 7c4d6c7b | Michael Hanselmann | disks_ok, _ = _AssembleInstanceDisks(self, instance,
|
4364 | 12a0cfbe | Guido Trotter | ignore_secondaries=True)
|
4365 | 12a0cfbe | Guido Trotter | if not disks_ok: |
4366 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4367 | 12a0cfbe | Guido Trotter | raise errors.OpExecError("Can't activate the instance's disks") |
4368 | a8083063 | Iustin Pop | |
4369 | 12a0cfbe | Guido Trotter | feedback_fn("* starting the instance on the target node")
|
4370 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(target_node, instance, None, None) |
4371 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4372 | dd279568 | Iustin Pop | if msg:
|
4373 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4374 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance %s on node %s: %s" % |
4375 | dd279568 | Iustin Pop | (instance.name, target_node, msg)) |
4376 | a8083063 | Iustin Pop | |
4377 | a8083063 | Iustin Pop | |
4378 | 53c776b5 | Iustin Pop | class LUMigrateInstance(LogicalUnit): |
4379 | 53c776b5 | Iustin Pop | """Migrate an instance.
|
4380 | 53c776b5 | Iustin Pop |
|
4381 | 53c776b5 | Iustin Pop | This is migration without shutting down, compared to the failover,
|
4382 | 53c776b5 | Iustin Pop | which is done with shutdown.
|
4383 | 53c776b5 | Iustin Pop |
|
4384 | 53c776b5 | Iustin Pop | """
|
4385 | 53c776b5 | Iustin Pop | HPATH = "instance-migrate"
|
4386 | 53c776b5 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4387 | 53c776b5 | Iustin Pop | _OP_REQP = ["instance_name", "live", "cleanup"] |
4388 | 53c776b5 | Iustin Pop | |
4389 | 53c776b5 | Iustin Pop | REQ_BGL = False
|
4390 | 53c776b5 | Iustin Pop | |
4391 | 53c776b5 | Iustin Pop | def ExpandNames(self): |
4392 | 53c776b5 | Iustin Pop | self._ExpandAndLockInstance()
|
4393 | 3e06e001 | Michael Hanselmann | |
4394 | 53c776b5 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
4395 | 53c776b5 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
4396 | 53c776b5 | Iustin Pop | |
4397 | 3e06e001 | Michael Hanselmann | self._migrater = TLMigrateInstance(self, self.op.instance_name, |
4398 | 3e06e001 | Michael Hanselmann | self.op.live, self.op.cleanup) |
4399 | 3a012b41 | Michael Hanselmann | self.tasklets = [self._migrater] |
4400 | 3e06e001 | Michael Hanselmann | |
4401 | 53c776b5 | Iustin Pop | def DeclareLocks(self, level): |
4402 | 53c776b5 | Iustin Pop | if level == locking.LEVEL_NODE:
|
4403 | 53c776b5 | Iustin Pop | self._LockInstancesNodes()
|
4404 | 53c776b5 | Iustin Pop | |
4405 | 53c776b5 | Iustin Pop | def BuildHooksEnv(self): |
4406 | 53c776b5 | Iustin Pop | """Build hooks env.
|
4407 | 53c776b5 | Iustin Pop |
|
4408 | 53c776b5 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4409 | 53c776b5 | Iustin Pop |
|
4410 | 53c776b5 | Iustin Pop | """
|
4411 | 3e06e001 | Michael Hanselmann | instance = self._migrater.instance
|
4412 | 3e06e001 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self, instance)
|
4413 | 2c2690c9 | Iustin Pop | env["MIGRATE_LIVE"] = self.op.live |
4414 | 2c2690c9 | Iustin Pop | env["MIGRATE_CLEANUP"] = self.op.cleanup |
4415 | 3e06e001 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes) |
4416 | 53c776b5 | Iustin Pop | return env, nl, nl
|
4417 | 53c776b5 | Iustin Pop | |
4418 | 3e06e001 | Michael Hanselmann | |
4419 | 313bcead | Iustin Pop | class LUMoveInstance(LogicalUnit): |
4420 | 313bcead | Iustin Pop | """Move an instance by data-copying.
|
4421 | 313bcead | Iustin Pop |
|
4422 | 313bcead | Iustin Pop | """
|
4423 | 313bcead | Iustin Pop | HPATH = "instance-move"
|
4424 | 313bcead | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4425 | 313bcead | Iustin Pop | _OP_REQP = ["instance_name", "target_node"] |
4426 | 313bcead | Iustin Pop | REQ_BGL = False
|
4427 | 313bcead | Iustin Pop | |
4428 | 313bcead | Iustin Pop | def ExpandNames(self): |
4429 | 313bcead | Iustin Pop | self._ExpandAndLockInstance()
|
4430 | 313bcead | Iustin Pop | target_node = self.cfg.ExpandNodeName(self.op.target_node) |
4431 | 313bcead | Iustin Pop | if target_node is None: |
4432 | 313bcead | Iustin Pop | raise errors.OpPrereqError("Node '%s' not known" % |
4433 | 313bcead | Iustin Pop | self.op.target_node)
|
4434 | 313bcead | Iustin Pop | self.op.target_node = target_node
|
4435 | 313bcead | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = [target_node]
|
4436 | 313bcead | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
4437 | 313bcead | Iustin Pop | |
4438 | 313bcead | Iustin Pop | def DeclareLocks(self, level): |
4439 | 313bcead | Iustin Pop | if level == locking.LEVEL_NODE:
|
4440 | 313bcead | Iustin Pop | self._LockInstancesNodes(primary_only=True) |
4441 | 313bcead | Iustin Pop | |
4442 | 313bcead | Iustin Pop | def BuildHooksEnv(self): |
4443 | 313bcead | Iustin Pop | """Build hooks env.
|
4444 | 313bcead | Iustin Pop |
|
4445 | 313bcead | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
4446 | 313bcead | Iustin Pop |
|
4447 | 313bcead | Iustin Pop | """
|
4448 | 313bcead | Iustin Pop | env = { |
4449 | 313bcead | Iustin Pop | "TARGET_NODE": self.op.target_node, |
4450 | 313bcead | Iustin Pop | } |
4451 | 313bcead | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
4452 | 313bcead | Iustin Pop | nl = [self.cfg.GetMasterNode()] + [self.instance.primary_node, |
4453 | 313bcead | Iustin Pop | self.op.target_node]
|
4454 | 313bcead | Iustin Pop | return env, nl, nl
|
4455 | 313bcead | Iustin Pop | |
4456 | 313bcead | Iustin Pop | def CheckPrereq(self): |
4457 | 313bcead | Iustin Pop | """Check prerequisites.
|
4458 | 313bcead | Iustin Pop |
|
4459 | 313bcead | Iustin Pop | This checks that the instance is in the cluster.
|
4460 | 313bcead | Iustin Pop |
|
4461 | 313bcead | Iustin Pop | """
|
4462 | 313bcead | Iustin Pop | self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
4463 | 313bcead | Iustin Pop | assert self.instance is not None, \ |
4464 | 313bcead | Iustin Pop | "Cannot retrieve locked instance %s" % self.op.instance_name |
4465 | 313bcead | Iustin Pop | |
4466 | 313bcead | Iustin Pop | node = self.cfg.GetNodeInfo(self.op.target_node) |
4467 | 313bcead | Iustin Pop | assert node is not None, \ |
4468 | 313bcead | Iustin Pop | "Cannot retrieve locked node %s" % self.op.target_node |
4469 | 313bcead | Iustin Pop | |
4470 | 313bcead | Iustin Pop | self.target_node = target_node = node.name
|
4471 | 313bcead | Iustin Pop | |
4472 | 313bcead | Iustin Pop | if target_node == instance.primary_node:
|
4473 | 313bcead | Iustin Pop | raise errors.OpPrereqError("Instance %s is already on the node %s" % |
4474 | 313bcead | Iustin Pop | (instance.name, target_node)) |
4475 | 313bcead | Iustin Pop | |
4476 | 313bcead | Iustin Pop | bep = self.cfg.GetClusterInfo().FillBE(instance)
|
4477 | 313bcead | Iustin Pop | |
4478 | 313bcead | Iustin Pop | for idx, dsk in enumerate(instance.disks): |
4479 | 313bcead | Iustin Pop | if dsk.dev_type not in (constants.LD_LV, constants.LD_FILE): |
4480 | 313bcead | Iustin Pop | raise errors.OpPrereqError("Instance disk %d has a complex layout," |
4481 | 313bcead | Iustin Pop | " cannot copy")
|
4482 | 313bcead | Iustin Pop | |
4483 | 313bcead | Iustin Pop | _CheckNodeOnline(self, target_node)
|
4484 | 313bcead | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
4485 | 313bcead | Iustin Pop | |
4486 | 313bcead | Iustin Pop | if instance.admin_up:
|
4487 | 313bcead | Iustin Pop | # check memory requirements on the secondary node
|
4488 | 313bcead | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "failing over instance %s" % |
4489 | 313bcead | Iustin Pop | instance.name, bep[constants.BE_MEMORY], |
4490 | 313bcead | Iustin Pop | instance.hypervisor) |
4491 | 313bcead | Iustin Pop | else:
|
4492 | 313bcead | Iustin Pop | self.LogInfo("Not checking memory on the secondary node as" |
4493 | 313bcead | Iustin Pop | " instance will not be started")
|
4494 | 313bcead | Iustin Pop | |
4495 | 313bcead | Iustin Pop | # check bridge existance
|
4496 | 313bcead | Iustin Pop | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
4497 | 313bcead | Iustin Pop | |
4498 | 313bcead | Iustin Pop | def Exec(self, feedback_fn): |
4499 | 313bcead | Iustin Pop | """Move an instance.
|
4500 | 313bcead | Iustin Pop |
|
4501 | 313bcead | Iustin Pop | The move is done by shutting it down on its present node, copying
|
4502 | 313bcead | Iustin Pop | the data over (slow) and starting it on the new node.
|
4503 | 313bcead | Iustin Pop |
|
4504 | 313bcead | Iustin Pop | """
|
4505 | 313bcead | Iustin Pop | instance = self.instance
|
4506 | 313bcead | Iustin Pop | |
4507 | 313bcead | Iustin Pop | source_node = instance.primary_node |
4508 | 313bcead | Iustin Pop | target_node = self.target_node
|
4509 | 313bcead | Iustin Pop | |
4510 | 313bcead | Iustin Pop | self.LogInfo("Shutting down instance %s on source node %s", |
4511 | 313bcead | Iustin Pop | instance.name, source_node) |
4512 | 313bcead | Iustin Pop | |
4513 | 313bcead | Iustin Pop | result = self.rpc.call_instance_shutdown(source_node, instance)
|
4514 | 313bcead | Iustin Pop | msg = result.fail_msg |
4515 | 313bcead | Iustin Pop | if msg:
|
4516 | 313bcead | Iustin Pop | if self.op.ignore_consistency: |
4517 | 313bcead | Iustin Pop | self.proc.LogWarning("Could not shutdown instance %s on node %s." |
4518 | 313bcead | Iustin Pop | " Proceeding anyway. Please make sure node"
|
4519 | 313bcead | Iustin Pop | " %s is down. Error details: %s",
|
4520 | 313bcead | Iustin Pop | instance.name, source_node, source_node, msg) |
4521 | 313bcead | Iustin Pop | else:
|
4522 | 313bcead | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on" |
4523 | 313bcead | Iustin Pop | " node %s: %s" %
|
4524 | 313bcead | Iustin Pop | (instance.name, source_node, msg)) |
4525 | 313bcead | Iustin Pop | |
4526 | 313bcead | Iustin Pop | # create the target disks
|
4527 | 313bcead | Iustin Pop | try:
|
4528 | 313bcead | Iustin Pop | _CreateDisks(self, instance, target_node=target_node)
|
4529 | 313bcead | Iustin Pop | except errors.OpExecError:
|
4530 | 313bcead | Iustin Pop | self.LogWarning("Device creation failed, reverting...") |
4531 | 313bcead | Iustin Pop | try:
|
4532 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=target_node)
|
4533 | 313bcead | Iustin Pop | finally:
|
4534 | 313bcead | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance.name)
|
4535 | 313bcead | Iustin Pop | raise
|
4536 | 313bcead | Iustin Pop | |
4537 | 313bcead | Iustin Pop | cluster_name = self.cfg.GetClusterInfo().cluster_name
|
4538 | 313bcead | Iustin Pop | |
4539 | 313bcead | Iustin Pop | errs = [] |
4540 | 313bcead | Iustin Pop | # activate, get path, copy the data over
|
4541 | 313bcead | Iustin Pop | for idx, disk in enumerate(instance.disks): |
4542 | 313bcead | Iustin Pop | self.LogInfo("Copying data for disk %d", idx) |
4543 | 313bcead | Iustin Pop | result = self.rpc.call_blockdev_assemble(target_node, disk,
|
4544 | 313bcead | Iustin Pop | instance.name, True)
|
4545 | 313bcead | Iustin Pop | if result.fail_msg:
|
4546 | 313bcead | Iustin Pop | self.LogWarning("Can't assemble newly created disk %d: %s", |
4547 | 313bcead | Iustin Pop | idx, result.fail_msg) |
4548 | 313bcead | Iustin Pop | errs.append(result.fail_msg) |
4549 | 313bcead | Iustin Pop | break
|
4550 | 313bcead | Iustin Pop | dev_path = result.payload |
4551 | 313bcead | Iustin Pop | result = self.rpc.call_blockdev_export(source_node, disk,
|
4552 | 313bcead | Iustin Pop | target_node, dev_path, |
4553 | 313bcead | Iustin Pop | cluster_name) |
4554 | 313bcead | Iustin Pop | if result.fail_msg:
|
4555 | 313bcead | Iustin Pop | self.LogWarning("Can't copy data over for disk %d: %s", |
4556 | 313bcead | Iustin Pop | idx, result.fail_msg) |
4557 | 313bcead | Iustin Pop | errs.append(result.fail_msg) |
4558 | 313bcead | Iustin Pop | break
|
4559 | 313bcead | Iustin Pop | |
4560 | 313bcead | Iustin Pop | if errs:
|
4561 | 313bcead | Iustin Pop | self.LogWarning("Some disks failed to copy, aborting") |
4562 | 313bcead | Iustin Pop | try:
|
4563 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=target_node)
|
4564 | 313bcead | Iustin Pop | finally:
|
4565 | 313bcead | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance.name)
|
4566 | 313bcead | Iustin Pop | raise errors.OpExecError("Errors during disk copy: %s" % |
4567 | 313bcead | Iustin Pop | (",".join(errs),))
|
4568 | 313bcead | Iustin Pop | |
4569 | 313bcead | Iustin Pop | instance.primary_node = target_node |
4570 | 313bcead | Iustin Pop | self.cfg.Update(instance)
|
4571 | 313bcead | Iustin Pop | |
4572 | 313bcead | Iustin Pop | self.LogInfo("Removing the disks on the original node") |
4573 | 313bcead | Iustin Pop | _RemoveDisks(self, instance, target_node=source_node)
|
4574 | 313bcead | Iustin Pop | |
4575 | 313bcead | Iustin Pop | # Only start the instance if it's marked as up
|
4576 | 313bcead | Iustin Pop | if instance.admin_up:
|
4577 | 313bcead | Iustin Pop | self.LogInfo("Starting instance %s on node %s", |
4578 | 313bcead | Iustin Pop | instance.name, target_node) |
4579 | 313bcead | Iustin Pop | |
4580 | 313bcead | Iustin Pop | disks_ok, _ = _AssembleInstanceDisks(self, instance,
|
4581 | 313bcead | Iustin Pop | ignore_secondaries=True)
|
4582 | 313bcead | Iustin Pop | if not disks_ok: |
4583 | 313bcead | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4584 | 313bcead | Iustin Pop | raise errors.OpExecError("Can't activate the instance's disks") |
4585 | 313bcead | Iustin Pop | |
4586 | 313bcead | Iustin Pop | result = self.rpc.call_instance_start(target_node, instance, None, None) |
4587 | 313bcead | Iustin Pop | msg = result.fail_msg |
4588 | 313bcead | Iustin Pop | if msg:
|
4589 | 313bcead | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
4590 | 313bcead | Iustin Pop | raise errors.OpExecError("Could not start instance %s on node %s: %s" % |
4591 | 313bcead | Iustin Pop | (instance.name, target_node, msg)) |
4592 | 313bcead | Iustin Pop | |
4593 | 313bcead | Iustin Pop | |
4594 | 80cb875c | Michael Hanselmann | class LUMigrateNode(LogicalUnit): |
4595 | 80cb875c | Michael Hanselmann | """Migrate all instances from a node.
|
4596 | 80cb875c | Michael Hanselmann |
|
4597 | 80cb875c | Michael Hanselmann | """
|
4598 | 80cb875c | Michael Hanselmann | HPATH = "node-migrate"
|
4599 | 80cb875c | Michael Hanselmann | HTYPE = constants.HTYPE_NODE |
4600 | 80cb875c | Michael Hanselmann | _OP_REQP = ["node_name", "live"] |
4601 | 80cb875c | Michael Hanselmann | REQ_BGL = False
|
4602 | 80cb875c | Michael Hanselmann | |
4603 | 80cb875c | Michael Hanselmann | def ExpandNames(self): |
4604 | 80cb875c | Michael Hanselmann | self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name) |
4605 | 80cb875c | Michael Hanselmann | if self.op.node_name is None: |
4606 | 80cb875c | Michael Hanselmann | raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name) |
4607 | 80cb875c | Michael Hanselmann | |
4608 | 80cb875c | Michael Hanselmann | self.needed_locks = {
|
4609 | 80cb875c | Michael Hanselmann | locking.LEVEL_NODE: [self.op.node_name],
|
4610 | 80cb875c | Michael Hanselmann | } |
4611 | 80cb875c | Michael Hanselmann | |
4612 | 80cb875c | Michael Hanselmann | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
4613 | 80cb875c | Michael Hanselmann | |
4614 | 80cb875c | Michael Hanselmann | # Create tasklets for migrating instances for all instances on this node
|
4615 | 80cb875c | Michael Hanselmann | names = [] |
4616 | 80cb875c | Michael Hanselmann | tasklets = [] |
4617 | 80cb875c | Michael Hanselmann | |
4618 | 80cb875c | Michael Hanselmann | for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name): |
4619 | 80cb875c | Michael Hanselmann | logging.debug("Migrating instance %s", inst.name)
|
4620 | 80cb875c | Michael Hanselmann | names.append(inst.name) |
4621 | 80cb875c | Michael Hanselmann | |
4622 | 80cb875c | Michael Hanselmann | tasklets.append(TLMigrateInstance(self, inst.name, self.op.live, False)) |
4623 | 80cb875c | Michael Hanselmann | |
4624 | 80cb875c | Michael Hanselmann | self.tasklets = tasklets
|
4625 | 80cb875c | Michael Hanselmann | |
4626 | 80cb875c | Michael Hanselmann | # Declare instance locks
|
4627 | 80cb875c | Michael Hanselmann | self.needed_locks[locking.LEVEL_INSTANCE] = names
|
4628 | 80cb875c | Michael Hanselmann | |
4629 | 80cb875c | Michael Hanselmann | def DeclareLocks(self, level): |
4630 | 80cb875c | Michael Hanselmann | if level == locking.LEVEL_NODE:
|
4631 | 80cb875c | Michael Hanselmann | self._LockInstancesNodes()
|
4632 | 80cb875c | Michael Hanselmann | |
4633 | 80cb875c | Michael Hanselmann | def BuildHooksEnv(self): |
4634 | 80cb875c | Michael Hanselmann | """Build hooks env.
|
4635 | 80cb875c | Michael Hanselmann |
|
4636 | 80cb875c | Michael Hanselmann | This runs on the master, the primary and all the secondaries.
|
4637 | 80cb875c | Michael Hanselmann |
|
4638 | 80cb875c | Michael Hanselmann | """
|
4639 | 80cb875c | Michael Hanselmann | env = { |
4640 | 80cb875c | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
4641 | 80cb875c | Michael Hanselmann | } |
4642 | 80cb875c | Michael Hanselmann | |
4643 | 80cb875c | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
4644 | 80cb875c | Michael Hanselmann | |
4645 | 80cb875c | Michael Hanselmann | return (env, nl, nl)
|
4646 | 80cb875c | Michael Hanselmann | |
4647 | 80cb875c | Michael Hanselmann | |
4648 | 3e06e001 | Michael Hanselmann | class TLMigrateInstance(Tasklet): |
4649 | 3e06e001 | Michael Hanselmann | def __init__(self, lu, instance_name, live, cleanup): |
4650 | 3e06e001 | Michael Hanselmann | """Initializes this class.
|
4651 | 3e06e001 | Michael Hanselmann |
|
4652 | 3e06e001 | Michael Hanselmann | """
|
4653 | 464243a7 | Michael Hanselmann | Tasklet.__init__(self, lu)
|
4654 | 464243a7 | Michael Hanselmann | |
4655 | 3e06e001 | Michael Hanselmann | # Parameters
|
4656 | 3e06e001 | Michael Hanselmann | self.instance_name = instance_name
|
4657 | 3e06e001 | Michael Hanselmann | self.live = live
|
4658 | 3e06e001 | Michael Hanselmann | self.cleanup = cleanup
|
4659 | 3e06e001 | Michael Hanselmann | |
4660 | 53c776b5 | Iustin Pop | def CheckPrereq(self): |
4661 | 53c776b5 | Iustin Pop | """Check prerequisites.
|
4662 | 53c776b5 | Iustin Pop |
|
4663 | 53c776b5 | Iustin Pop | This checks that the instance is in the cluster.
|
4664 | 53c776b5 | Iustin Pop |
|
4665 | 53c776b5 | Iustin Pop | """
|
4666 | 53c776b5 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
4667 | 3e06e001 | Michael Hanselmann | self.cfg.ExpandInstanceName(self.instance_name)) |
4668 | 53c776b5 | Iustin Pop | if instance is None: |
4669 | 53c776b5 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
4670 | 3e06e001 | Michael Hanselmann | self.instance_name)
|
4671 | 53c776b5 | Iustin Pop | |
4672 | 53c776b5 | Iustin Pop | if instance.disk_template != constants.DT_DRBD8:
|
4673 | 53c776b5 | Iustin Pop | raise errors.OpPrereqError("Instance's disk layout is not" |
4674 | 53c776b5 | Iustin Pop | " drbd8, cannot migrate.")
|
4675 | 53c776b5 | Iustin Pop | |
4676 | 53c776b5 | Iustin Pop | secondary_nodes = instance.secondary_nodes |
4677 | 53c776b5 | Iustin Pop | if not secondary_nodes: |
4678 | 733a2b6a | Iustin Pop | raise errors.ConfigurationError("No secondary node but using" |
4679 | 733a2b6a | Iustin Pop | " drbd8 disk template")
|
4680 | 53c776b5 | Iustin Pop | |
4681 | 53c776b5 | Iustin Pop | i_be = self.cfg.GetClusterInfo().FillBE(instance)
|
4682 | 53c776b5 | Iustin Pop | |
4683 | 53c776b5 | Iustin Pop | target_node = secondary_nodes[0]
|
4684 | 53c776b5 | Iustin Pop | # check memory requirements on the secondary node
|
4685 | 53c776b5 | Iustin Pop | _CheckNodeFreeMemory(self, target_node, "migrating instance %s" % |
4686 | 53c776b5 | Iustin Pop | instance.name, i_be[constants.BE_MEMORY], |
4687 | 53c776b5 | Iustin Pop | instance.hypervisor) |
4688 | 53c776b5 | Iustin Pop | |
4689 | 53c776b5 | Iustin Pop | # check bridge existance
|
4690 | b165e77e | Guido Trotter | _CheckInstanceBridgesExist(self, instance, node=target_node)
|
4691 | 53c776b5 | Iustin Pop | |
4692 | 3e06e001 | Michael Hanselmann | if not self.cleanup: |
4693 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, target_node)
|
4694 | 53c776b5 | Iustin Pop | result = self.rpc.call_instance_migratable(instance.primary_node,
|
4695 | 53c776b5 | Iustin Pop | instance) |
4696 | 4c4e4e1e | Iustin Pop | result.Raise("Can't migrate, please use failover", prereq=True) |
4697 | 53c776b5 | Iustin Pop | |
4698 | 53c776b5 | Iustin Pop | self.instance = instance
|
4699 | 53c776b5 | Iustin Pop | |
4700 | 53c776b5 | Iustin Pop | def _WaitUntilSync(self): |
4701 | 53c776b5 | Iustin Pop | """Poll with custom rpc for disk sync.
|
4702 | 53c776b5 | Iustin Pop |
|
4703 | 53c776b5 | Iustin Pop | This uses our own step-based rpc call.
|
4704 | 53c776b5 | Iustin Pop |
|
4705 | 53c776b5 | Iustin Pop | """
|
4706 | 53c776b5 | Iustin Pop | self.feedback_fn("* wait until resync is done") |
4707 | 53c776b5 | Iustin Pop | all_done = False
|
4708 | 53c776b5 | Iustin Pop | while not all_done: |
4709 | 53c776b5 | Iustin Pop | all_done = True
|
4710 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_wait_sync(self.all_nodes, |
4711 | 53c776b5 | Iustin Pop | self.nodes_ip,
|
4712 | 53c776b5 | Iustin Pop | self.instance.disks)
|
4713 | 53c776b5 | Iustin Pop | min_percent = 100
|
4714 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
4715 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot resync disks on node %s" % node)
|
4716 | 0959c824 | Iustin Pop | node_done, node_percent = nres.payload |
4717 | 53c776b5 | Iustin Pop | all_done = all_done and node_done
|
4718 | 53c776b5 | Iustin Pop | if node_percent is not None: |
4719 | 53c776b5 | Iustin Pop | min_percent = min(min_percent, node_percent)
|
4720 | 53c776b5 | Iustin Pop | if not all_done: |
4721 | 53c776b5 | Iustin Pop | if min_percent < 100: |
4722 | 53c776b5 | Iustin Pop | self.feedback_fn(" - progress: %.1f%%" % min_percent) |
4723 | 53c776b5 | Iustin Pop | time.sleep(2)
|
4724 | 53c776b5 | Iustin Pop | |
4725 | 53c776b5 | Iustin Pop | def _EnsureSecondary(self, node): |
4726 | 53c776b5 | Iustin Pop | """Demote a node to secondary.
|
4727 | 53c776b5 | Iustin Pop |
|
4728 | 53c776b5 | Iustin Pop | """
|
4729 | 53c776b5 | Iustin Pop | self.feedback_fn("* switching node %s to secondary mode" % node) |
4730 | 53c776b5 | Iustin Pop | |
4731 | 53c776b5 | Iustin Pop | for dev in self.instance.disks: |
4732 | 53c776b5 | Iustin Pop | self.cfg.SetDiskID(dev, node)
|
4733 | 53c776b5 | Iustin Pop | |
4734 | 53c776b5 | Iustin Pop | result = self.rpc.call_blockdev_close(node, self.instance.name, |
4735 | 53c776b5 | Iustin Pop | self.instance.disks)
|
4736 | 4c4e4e1e | Iustin Pop | result.Raise("Cannot change disk to secondary on node %s" % node)
|
4737 | 53c776b5 | Iustin Pop | |
4738 | 53c776b5 | Iustin Pop | def _GoStandalone(self): |
4739 | 53c776b5 | Iustin Pop | """Disconnect from the network.
|
4740 | 53c776b5 | Iustin Pop |
|
4741 | 53c776b5 | Iustin Pop | """
|
4742 | 53c776b5 | Iustin Pop | self.feedback_fn("* changing into standalone mode") |
4743 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip, |
4744 | 53c776b5 | Iustin Pop | self.instance.disks)
|
4745 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
4746 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot disconnect disks node %s" % node)
|
4747 | 53c776b5 | Iustin Pop | |
4748 | 53c776b5 | Iustin Pop | def _GoReconnect(self, multimaster): |
4749 | 53c776b5 | Iustin Pop | """Reconnect to the network.
|
4750 | 53c776b5 | Iustin Pop |
|
4751 | 53c776b5 | Iustin Pop | """
|
4752 | 53c776b5 | Iustin Pop | if multimaster:
|
4753 | 53c776b5 | Iustin Pop | msg = "dual-master"
|
4754 | 53c776b5 | Iustin Pop | else:
|
4755 | 53c776b5 | Iustin Pop | msg = "single-master"
|
4756 | 53c776b5 | Iustin Pop | self.feedback_fn("* changing disks into %s mode" % msg) |
4757 | 53c776b5 | Iustin Pop | result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip, |
4758 | 53c776b5 | Iustin Pop | self.instance.disks,
|
4759 | 53c776b5 | Iustin Pop | self.instance.name, multimaster)
|
4760 | 53c776b5 | Iustin Pop | for node, nres in result.items(): |
4761 | 4c4e4e1e | Iustin Pop | nres.Raise("Cannot change disks config on node %s" % node)
|
4762 | 53c776b5 | Iustin Pop | |
4763 | 53c776b5 | Iustin Pop | def _ExecCleanup(self): |
4764 | 53c776b5 | Iustin Pop | """Try to cleanup after a failed migration.
|
4765 | 53c776b5 | Iustin Pop |
|
4766 | 53c776b5 | Iustin Pop | The cleanup is done by:
|
4767 | 53c776b5 | Iustin Pop | - check that the instance is running only on one node
|
4768 | 53c776b5 | Iustin Pop | (and update the config if needed)
|
4769 | 53c776b5 | Iustin Pop | - change disks on its secondary node to secondary
|
4770 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized
|
4771 | 53c776b5 | Iustin Pop | - disconnect from the network
|
4772 | 53c776b5 | Iustin Pop | - change disks into single-master mode
|
4773 | 53c776b5 | Iustin Pop | - wait again until disks are fully synchronized
|
4774 | 53c776b5 | Iustin Pop |
|
4775 | 53c776b5 | Iustin Pop | """
|
4776 | 53c776b5 | Iustin Pop | instance = self.instance
|
4777 | 53c776b5 | Iustin Pop | target_node = self.target_node
|
4778 | 53c776b5 | Iustin Pop | source_node = self.source_node
|
4779 | 53c776b5 | Iustin Pop | |
4780 | 53c776b5 | Iustin Pop | # check running on only one node
|
4781 | 53c776b5 | Iustin Pop | self.feedback_fn("* checking where the instance actually runs" |
4782 | 53c776b5 | Iustin Pop | " (if this hangs, the hypervisor might be in"
|
4783 | 53c776b5 | Iustin Pop | " a bad state)")
|
4784 | 53c776b5 | Iustin Pop | ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor]) |
4785 | 53c776b5 | Iustin Pop | for node, result in ins_l.items(): |
4786 | 4c4e4e1e | Iustin Pop | result.Raise("Can't contact node %s" % node)
|
4787 | 53c776b5 | Iustin Pop | |
4788 | aca13712 | Iustin Pop | runningon_source = instance.name in ins_l[source_node].payload
|
4789 | aca13712 | Iustin Pop | runningon_target = instance.name in ins_l[target_node].payload
|
4790 | 53c776b5 | Iustin Pop | |
4791 | 53c776b5 | Iustin Pop | if runningon_source and runningon_target: |
4792 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Instance seems to be running on two nodes," |
4793 | 53c776b5 | Iustin Pop | " or the hypervisor is confused. You will have"
|
4794 | 53c776b5 | Iustin Pop | " to ensure manually that it runs only on one"
|
4795 | 53c776b5 | Iustin Pop | " and restart this operation.")
|
4796 | 53c776b5 | Iustin Pop | |
4797 | 53c776b5 | Iustin Pop | if not (runningon_source or runningon_target): |
4798 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Instance does not seem to be running at all." |
4799 | 53c776b5 | Iustin Pop | " In this case, it's safer to repair by"
|
4800 | 53c776b5 | Iustin Pop | " running 'gnt-instance stop' to ensure disk"
|
4801 | 53c776b5 | Iustin Pop | " shutdown, and then restarting it.")
|
4802 | 53c776b5 | Iustin Pop | |
4803 | 53c776b5 | Iustin Pop | if runningon_target:
|
4804 | 53c776b5 | Iustin Pop | # the migration has actually succeeded, we need to update the config
|
4805 | 53c776b5 | Iustin Pop | self.feedback_fn("* instance running on secondary node (%s)," |
4806 | 53c776b5 | Iustin Pop | " updating config" % target_node)
|
4807 | 53c776b5 | Iustin Pop | instance.primary_node = target_node |
4808 | 53c776b5 | Iustin Pop | self.cfg.Update(instance)
|
4809 | 53c776b5 | Iustin Pop | demoted_node = source_node |
4810 | 53c776b5 | Iustin Pop | else:
|
4811 | 53c776b5 | Iustin Pop | self.feedback_fn("* instance confirmed to be running on its" |
4812 | 53c776b5 | Iustin Pop | " primary node (%s)" % source_node)
|
4813 | 53c776b5 | Iustin Pop | demoted_node = target_node |
4814 | 53c776b5 | Iustin Pop | |
4815 | 53c776b5 | Iustin Pop | self._EnsureSecondary(demoted_node)
|
4816 | 53c776b5 | Iustin Pop | try:
|
4817 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
4818 | 53c776b5 | Iustin Pop | except errors.OpExecError:
|
4819 | 53c776b5 | Iustin Pop | # we ignore here errors, since if the device is standalone, it
|
4820 | 53c776b5 | Iustin Pop | # won't be able to sync
|
4821 | 53c776b5 | Iustin Pop | pass
|
4822 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
4823 | 53c776b5 | Iustin Pop | self._GoReconnect(False) |
4824 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
4825 | 53c776b5 | Iustin Pop | |
4826 | 53c776b5 | Iustin Pop | self.feedback_fn("* done") |
4827 | 53c776b5 | Iustin Pop | |
4828 | 6906a9d8 | Guido Trotter | def _RevertDiskStatus(self): |
4829 | 6906a9d8 | Guido Trotter | """Try to revert the disk status after a failed migration.
|
4830 | 6906a9d8 | Guido Trotter |
|
4831 | 6906a9d8 | Guido Trotter | """
|
4832 | 6906a9d8 | Guido Trotter | target_node = self.target_node
|
4833 | 6906a9d8 | Guido Trotter | try:
|
4834 | 6906a9d8 | Guido Trotter | self._EnsureSecondary(target_node)
|
4835 | 6906a9d8 | Guido Trotter | self._GoStandalone()
|
4836 | 6906a9d8 | Guido Trotter | self._GoReconnect(False) |
4837 | 6906a9d8 | Guido Trotter | self._WaitUntilSync()
|
4838 | 6906a9d8 | Guido Trotter | except errors.OpExecError, err:
|
4839 | 3e06e001 | Michael Hanselmann | self.lu.LogWarning("Migration failed and I can't reconnect the" |
4840 | 3e06e001 | Michael Hanselmann | " drives: error '%s'\n"
|
4841 | 3e06e001 | Michael Hanselmann | "Please look and recover the instance status" %
|
4842 | 3e06e001 | Michael Hanselmann | str(err))
|
4843 | 6906a9d8 | Guido Trotter | |
4844 | 6906a9d8 | Guido Trotter | def _AbortMigration(self): |
4845 | 6906a9d8 | Guido Trotter | """Call the hypervisor code to abort a started migration.
|
4846 | 6906a9d8 | Guido Trotter |
|
4847 | 6906a9d8 | Guido Trotter | """
|
4848 | 6906a9d8 | Guido Trotter | instance = self.instance
|
4849 | 6906a9d8 | Guido Trotter | target_node = self.target_node
|
4850 | 6906a9d8 | Guido Trotter | migration_info = self.migration_info
|
4851 | 6906a9d8 | Guido Trotter | |
4852 | 6906a9d8 | Guido Trotter | abort_result = self.rpc.call_finalize_migration(target_node,
|
4853 | 6906a9d8 | Guido Trotter | instance, |
4854 | 6906a9d8 | Guido Trotter | migration_info, |
4855 | 6906a9d8 | Guido Trotter | False)
|
4856 | 4c4e4e1e | Iustin Pop | abort_msg = abort_result.fail_msg |
4857 | 6906a9d8 | Guido Trotter | if abort_msg:
|
4858 | 6906a9d8 | Guido Trotter | logging.error("Aborting migration failed on target node %s: %s" %
|
4859 | 6906a9d8 | Guido Trotter | (target_node, abort_msg)) |
4860 | 6906a9d8 | Guido Trotter | # Don't raise an exception here, as we stil have to try to revert the
|
4861 | 6906a9d8 | Guido Trotter | # disk status, even if this step failed.
|
4862 | 6906a9d8 | Guido Trotter | |
4863 | 53c776b5 | Iustin Pop | def _ExecMigration(self): |
4864 | 53c776b5 | Iustin Pop | """Migrate an instance.
|
4865 | 53c776b5 | Iustin Pop |
|
4866 | 53c776b5 | Iustin Pop | The migrate is done by:
|
4867 | 53c776b5 | Iustin Pop | - change the disks into dual-master mode
|
4868 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized again
|
4869 | 53c776b5 | Iustin Pop | - migrate the instance
|
4870 | 53c776b5 | Iustin Pop | - change disks on the new secondary node (the old primary) to secondary
|
4871 | 53c776b5 | Iustin Pop | - wait until disks are fully synchronized
|
4872 | 53c776b5 | Iustin Pop | - change disks into single-master mode
|
4873 | 53c776b5 | Iustin Pop |
|
4874 | 53c776b5 | Iustin Pop | """
|
4875 | 53c776b5 | Iustin Pop | instance = self.instance
|
4876 | 53c776b5 | Iustin Pop | target_node = self.target_node
|
4877 | 53c776b5 | Iustin Pop | source_node = self.source_node
|
4878 | 53c776b5 | Iustin Pop | |
4879 | 53c776b5 | Iustin Pop | self.feedback_fn("* checking disk consistency between source and target") |
4880 | 53c776b5 | Iustin Pop | for dev in instance.disks: |
4881 | 53c776b5 | Iustin Pop | if not _CheckDiskConsistency(self, dev, target_node, False): |
4882 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Disk %s is degraded or not fully" |
4883 | 53c776b5 | Iustin Pop | " synchronized on target node,"
|
4884 | 53c776b5 | Iustin Pop | " aborting migrate." % dev.iv_name)
|
4885 | 53c776b5 | Iustin Pop | |
4886 | 6906a9d8 | Guido Trotter | # First get the migration information from the remote node
|
4887 | 6906a9d8 | Guido Trotter | result = self.rpc.call_migration_info(source_node, instance)
|
4888 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4889 | 6906a9d8 | Guido Trotter | if msg:
|
4890 | 6906a9d8 | Guido Trotter | log_err = ("Failed fetching source migration information from %s: %s" %
|
4891 | 0959c824 | Iustin Pop | (source_node, msg)) |
4892 | 6906a9d8 | Guido Trotter | logging.error(log_err) |
4893 | 6906a9d8 | Guido Trotter | raise errors.OpExecError(log_err)
|
4894 | 6906a9d8 | Guido Trotter | |
4895 | 0959c824 | Iustin Pop | self.migration_info = migration_info = result.payload
|
4896 | 6906a9d8 | Guido Trotter | |
4897 | 6906a9d8 | Guido Trotter | # Then switch the disks to master/master mode
|
4898 | 53c776b5 | Iustin Pop | self._EnsureSecondary(target_node)
|
4899 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
4900 | 53c776b5 | Iustin Pop | self._GoReconnect(True) |
4901 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
4902 | 53c776b5 | Iustin Pop | |
4903 | 6906a9d8 | Guido Trotter | self.feedback_fn("* preparing %s to accept the instance" % target_node) |
4904 | 6906a9d8 | Guido Trotter | result = self.rpc.call_accept_instance(target_node,
|
4905 | 6906a9d8 | Guido Trotter | instance, |
4906 | 6906a9d8 | Guido Trotter | migration_info, |
4907 | 6906a9d8 | Guido Trotter | self.nodes_ip[target_node])
|
4908 | 6906a9d8 | Guido Trotter | |
4909 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4910 | 6906a9d8 | Guido Trotter | if msg:
|
4911 | 6906a9d8 | Guido Trotter | logging.error("Instance pre-migration failed, trying to revert"
|
4912 | 6906a9d8 | Guido Trotter | " disk status: %s", msg)
|
4913 | 6906a9d8 | Guido Trotter | self._AbortMigration()
|
4914 | 6906a9d8 | Guido Trotter | self._RevertDiskStatus()
|
4915 | 6906a9d8 | Guido Trotter | raise errors.OpExecError("Could not pre-migrate instance %s: %s" % |
4916 | 6906a9d8 | Guido Trotter | (instance.name, msg)) |
4917 | 6906a9d8 | Guido Trotter | |
4918 | 53c776b5 | Iustin Pop | self.feedback_fn("* migrating instance to %s" % target_node) |
4919 | 53c776b5 | Iustin Pop | time.sleep(10)
|
4920 | 53c776b5 | Iustin Pop | result = self.rpc.call_instance_migrate(source_node, instance,
|
4921 | 53c776b5 | Iustin Pop | self.nodes_ip[target_node],
|
4922 | 3e06e001 | Michael Hanselmann | self.live)
|
4923 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4924 | 53c776b5 | Iustin Pop | if msg:
|
4925 | 53c776b5 | Iustin Pop | logging.error("Instance migration failed, trying to revert"
|
4926 | 53c776b5 | Iustin Pop | " disk status: %s", msg)
|
4927 | 6906a9d8 | Guido Trotter | self._AbortMigration()
|
4928 | 6906a9d8 | Guido Trotter | self._RevertDiskStatus()
|
4929 | 53c776b5 | Iustin Pop | raise errors.OpExecError("Could not migrate instance %s: %s" % |
4930 | 53c776b5 | Iustin Pop | (instance.name, msg)) |
4931 | 53c776b5 | Iustin Pop | time.sleep(10)
|
4932 | 53c776b5 | Iustin Pop | |
4933 | 53c776b5 | Iustin Pop | instance.primary_node = target_node |
4934 | 53c776b5 | Iustin Pop | # distribute new instance config to the other nodes
|
4935 | 53c776b5 | Iustin Pop | self.cfg.Update(instance)
|
4936 | 53c776b5 | Iustin Pop | |
4937 | 6906a9d8 | Guido Trotter | result = self.rpc.call_finalize_migration(target_node,
|
4938 | 6906a9d8 | Guido Trotter | instance, |
4939 | 6906a9d8 | Guido Trotter | migration_info, |
4940 | 6906a9d8 | Guido Trotter | True)
|
4941 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
4942 | 6906a9d8 | Guido Trotter | if msg:
|
4943 | 6906a9d8 | Guido Trotter | logging.error("Instance migration succeeded, but finalization failed:"
|
4944 | 6906a9d8 | Guido Trotter | " %s" % msg)
|
4945 | 6906a9d8 | Guido Trotter | raise errors.OpExecError("Could not finalize instance migration: %s" % |
4946 | 6906a9d8 | Guido Trotter | msg) |
4947 | 6906a9d8 | Guido Trotter | |
4948 | 53c776b5 | Iustin Pop | self._EnsureSecondary(source_node)
|
4949 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
4950 | 53c776b5 | Iustin Pop | self._GoStandalone()
|
4951 | 53c776b5 | Iustin Pop | self._GoReconnect(False) |
4952 | 53c776b5 | Iustin Pop | self._WaitUntilSync()
|
4953 | 53c776b5 | Iustin Pop | |
4954 | 53c776b5 | Iustin Pop | self.feedback_fn("* done") |
4955 | 53c776b5 | Iustin Pop | |
4956 | 53c776b5 | Iustin Pop | def Exec(self, feedback_fn): |
4957 | 53c776b5 | Iustin Pop | """Perform the migration.
|
4958 | 53c776b5 | Iustin Pop |
|
4959 | 53c776b5 | Iustin Pop | """
|
4960 | 80cb875c | Michael Hanselmann | feedback_fn("Migrating instance %s" % self.instance.name) |
4961 | 80cb875c | Michael Hanselmann | |
4962 | 53c776b5 | Iustin Pop | self.feedback_fn = feedback_fn
|
4963 | 53c776b5 | Iustin Pop | |
4964 | 53c776b5 | Iustin Pop | self.source_node = self.instance.primary_node |
4965 | 53c776b5 | Iustin Pop | self.target_node = self.instance.secondary_nodes[0] |
4966 | 53c776b5 | Iustin Pop | self.all_nodes = [self.source_node, self.target_node] |
4967 | 53c776b5 | Iustin Pop | self.nodes_ip = {
|
4968 | 53c776b5 | Iustin Pop | self.source_node: self.cfg.GetNodeInfo(self.source_node).secondary_ip, |
4969 | 53c776b5 | Iustin Pop | self.target_node: self.cfg.GetNodeInfo(self.target_node).secondary_ip, |
4970 | 53c776b5 | Iustin Pop | } |
4971 | 3e06e001 | Michael Hanselmann | |
4972 | 3e06e001 | Michael Hanselmann | if self.cleanup: |
4973 | 53c776b5 | Iustin Pop | return self._ExecCleanup() |
4974 | 53c776b5 | Iustin Pop | else:
|
4975 | 53c776b5 | Iustin Pop | return self._ExecMigration() |
4976 | 53c776b5 | Iustin Pop | |
4977 | 53c776b5 | Iustin Pop | |
4978 | 428958aa | Iustin Pop | def _CreateBlockDev(lu, node, instance, device, force_create, |
4979 | 428958aa | Iustin Pop | info, force_open): |
4980 | 428958aa | Iustin Pop | """Create a tree of block devices on a given node.
|
4981 | a8083063 | Iustin Pop |
|
4982 | a8083063 | Iustin Pop | If this device type has to be created on secondaries, create it and
|
4983 | a8083063 | Iustin Pop | all its children.
|
4984 | a8083063 | Iustin Pop |
|
4985 | a8083063 | Iustin Pop | If not, just recurse to children keeping the same 'force' value.
|
4986 | a8083063 | Iustin Pop |
|
4987 | 428958aa | Iustin Pop | @param lu: the lu on whose behalf we execute
|
4988 | 428958aa | Iustin Pop | @param node: the node on which to create the device
|
4989 | 428958aa | Iustin Pop | @type instance: L{objects.Instance}
|
4990 | 428958aa | Iustin Pop | @param instance: the instance which owns the device
|
4991 | 428958aa | Iustin Pop | @type device: L{objects.Disk}
|
4992 | 428958aa | Iustin Pop | @param device: the device to create
|
4993 | 428958aa | Iustin Pop | @type force_create: boolean
|
4994 | 428958aa | Iustin Pop | @param force_create: whether to force creation of this device; this
|
4995 | 428958aa | Iustin Pop | will be change to True whenever we find a device which has
|
4996 | 428958aa | Iustin Pop | CreateOnSecondary() attribute
|
4997 | 428958aa | Iustin Pop | @param info: the extra 'metadata' we should attach to the device
|
4998 | 428958aa | Iustin Pop | (this will be represented as a LVM tag)
|
4999 | 428958aa | Iustin Pop | @type force_open: boolean
|
5000 | 428958aa | Iustin Pop | @param force_open: this parameter will be passes to the
|
5001 | 821d1bd1 | Iustin Pop | L{backend.BlockdevCreate} function where it specifies
|
5002 | 428958aa | Iustin Pop | whether we run on primary or not, and it affects both
|
5003 | 428958aa | Iustin Pop | the child assembly and the device own Open() execution
|
5004 | 428958aa | Iustin Pop |
|
5005 | a8083063 | Iustin Pop | """
|
5006 | a8083063 | Iustin Pop | if device.CreateOnSecondary():
|
5007 | 428958aa | Iustin Pop | force_create = True
|
5008 | 796cab27 | Iustin Pop | |
5009 | a8083063 | Iustin Pop | if device.children:
|
5010 | a8083063 | Iustin Pop | for child in device.children: |
5011 | 428958aa | Iustin Pop | _CreateBlockDev(lu, node, instance, child, force_create, |
5012 | 428958aa | Iustin Pop | info, force_open) |
5013 | a8083063 | Iustin Pop | |
5014 | 428958aa | Iustin Pop | if not force_create: |
5015 | 796cab27 | Iustin Pop | return
|
5016 | 796cab27 | Iustin Pop | |
5017 | de12473a | Iustin Pop | _CreateSingleBlockDev(lu, node, instance, device, info, force_open) |
5018 | de12473a | Iustin Pop | |
5019 | de12473a | Iustin Pop | |
5020 | de12473a | Iustin Pop | def _CreateSingleBlockDev(lu, node, instance, device, info, force_open): |
5021 | de12473a | Iustin Pop | """Create a single block device on a given node.
|
5022 | de12473a | Iustin Pop |
|
5023 | de12473a | Iustin Pop | This will not recurse over children of the device, so they must be
|
5024 | de12473a | Iustin Pop | created in advance.
|
5025 | de12473a | Iustin Pop |
|
5026 | de12473a | Iustin Pop | @param lu: the lu on whose behalf we execute
|
5027 | de12473a | Iustin Pop | @param node: the node on which to create the device
|
5028 | de12473a | Iustin Pop | @type instance: L{objects.Instance}
|
5029 | de12473a | Iustin Pop | @param instance: the instance which owns the device
|
5030 | de12473a | Iustin Pop | @type device: L{objects.Disk}
|
5031 | de12473a | Iustin Pop | @param device: the device to create
|
5032 | de12473a | Iustin Pop | @param info: the extra 'metadata' we should attach to the device
|
5033 | de12473a | Iustin Pop | (this will be represented as a LVM tag)
|
5034 | de12473a | Iustin Pop | @type force_open: boolean
|
5035 | de12473a | Iustin Pop | @param force_open: this parameter will be passes to the
|
5036 | 821d1bd1 | Iustin Pop | L{backend.BlockdevCreate} function where it specifies
|
5037 | de12473a | Iustin Pop | whether we run on primary or not, and it affects both
|
5038 | de12473a | Iustin Pop | the child assembly and the device own Open() execution
|
5039 | de12473a | Iustin Pop |
|
5040 | de12473a | Iustin Pop | """
|
5041 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(device, node) |
5042 | 7d81697f | Iustin Pop | result = lu.rpc.call_blockdev_create(node, device, device.size, |
5043 | 428958aa | Iustin Pop | instance.name, force_open, info) |
5044 | 4c4e4e1e | Iustin Pop | result.Raise("Can't create block device %s on"
|
5045 | 4c4e4e1e | Iustin Pop | " node %s for instance %s" % (device, node, instance.name))
|
5046 | a8083063 | Iustin Pop | if device.physical_id is None: |
5047 | 0959c824 | Iustin Pop | device.physical_id = result.payload |
5048 | a8083063 | Iustin Pop | |
5049 | a8083063 | Iustin Pop | |
5050 | b9bddb6b | Iustin Pop | def _GenerateUniqueNames(lu, exts): |
5051 | 923b1523 | Iustin Pop | """Generate a suitable LV name.
|
5052 | 923b1523 | Iustin Pop |
|
5053 | 923b1523 | Iustin Pop | This will generate a logical volume name for the given instance.
|
5054 | 923b1523 | Iustin Pop |
|
5055 | 923b1523 | Iustin Pop | """
|
5056 | 923b1523 | Iustin Pop | results = [] |
5057 | 923b1523 | Iustin Pop | for val in exts: |
5058 | b9bddb6b | Iustin Pop | new_id = lu.cfg.GenerateUniqueID() |
5059 | 923b1523 | Iustin Pop | results.append("%s%s" % (new_id, val))
|
5060 | 923b1523 | Iustin Pop | return results
|
5061 | 923b1523 | Iustin Pop | |
5062 | 923b1523 | Iustin Pop | |
5063 | b9bddb6b | Iustin Pop | def _GenerateDRBD8Branch(lu, primary, secondary, size, names, iv_name, |
5064 | ffa1c0dc | Iustin Pop | p_minor, s_minor): |
5065 | a1f445d3 | Iustin Pop | """Generate a drbd8 device complete with its children.
|
5066 | a1f445d3 | Iustin Pop |
|
5067 | a1f445d3 | Iustin Pop | """
|
5068 | b9bddb6b | Iustin Pop | port = lu.cfg.AllocatePort() |
5069 | b9bddb6b | Iustin Pop | vgname = lu.cfg.GetVGName() |
5070 | b9bddb6b | Iustin Pop | shared_secret = lu.cfg.GenerateDRBDSecret() |
5071 | a1f445d3 | Iustin Pop | dev_data = objects.Disk(dev_type=constants.LD_LV, size=size, |
5072 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[0]))
|
5073 | a1f445d3 | Iustin Pop | dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
5074 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[1]))
|
5075 | a1f445d3 | Iustin Pop | drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size, |
5076 | ffa1c0dc | Iustin Pop | logical_id=(primary, secondary, port, |
5077 | f9518d38 | Iustin Pop | p_minor, s_minor, |
5078 | f9518d38 | Iustin Pop | shared_secret), |
5079 | ffa1c0dc | Iustin Pop | children=[dev_data, dev_meta], |
5080 | a1f445d3 | Iustin Pop | iv_name=iv_name) |
5081 | a1f445d3 | Iustin Pop | return drbd_dev
|
5082 | a1f445d3 | Iustin Pop | |
5083 | 7c0d6283 | Michael Hanselmann | |
5084 | b9bddb6b | Iustin Pop | def _GenerateDiskTemplate(lu, template_name, |
5085 | a8083063 | Iustin Pop | instance_name, primary_node, |
5086 | 08db7c5c | Iustin Pop | secondary_nodes, disk_info, |
5087 | e2a65344 | Iustin Pop | file_storage_dir, file_driver, |
5088 | e2a65344 | Iustin Pop | base_index): |
5089 | a8083063 | Iustin Pop | """Generate the entire disk layout for a given template type.
|
5090 | a8083063 | Iustin Pop |
|
5091 | a8083063 | Iustin Pop | """
|
5092 | a8083063 | Iustin Pop | #TODO: compute space requirements
|
5093 | a8083063 | Iustin Pop | |
5094 | b9bddb6b | Iustin Pop | vgname = lu.cfg.GetVGName() |
5095 | 08db7c5c | Iustin Pop | disk_count = len(disk_info)
|
5096 | 08db7c5c | Iustin Pop | disks = [] |
5097 | 3517d9b9 | Manuel Franceschini | if template_name == constants.DT_DISKLESS:
|
5098 | 08db7c5c | Iustin Pop | pass
|
5099 | 3517d9b9 | Manuel Franceschini | elif template_name == constants.DT_PLAIN:
|
5100 | a8083063 | Iustin Pop | if len(secondary_nodes) != 0: |
5101 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
5102 | 923b1523 | Iustin Pop | |
5103 | fb4b324b | Guido Trotter | names = _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
|
5104 | 08db7c5c | Iustin Pop | for i in range(disk_count)]) |
5105 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5106 | e2a65344 | Iustin Pop | disk_index = idx + base_index |
5107 | 08db7c5c | Iustin Pop | disk_dev = objects.Disk(dev_type=constants.LD_LV, size=disk["size"],
|
5108 | 08db7c5c | Iustin Pop | logical_id=(vgname, names[idx]), |
5109 | 6ec66eae | Iustin Pop | iv_name="disk/%d" % disk_index,
|
5110 | 6ec66eae | Iustin Pop | mode=disk["mode"])
|
5111 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5112 | a1f445d3 | Iustin Pop | elif template_name == constants.DT_DRBD8:
|
5113 | a1f445d3 | Iustin Pop | if len(secondary_nodes) != 1: |
5114 | a1f445d3 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
5115 | a1f445d3 | Iustin Pop | remote_node = secondary_nodes[0]
|
5116 | 08db7c5c | Iustin Pop | minors = lu.cfg.AllocateDRBDMinor( |
5117 | 08db7c5c | Iustin Pop | [primary_node, remote_node] * len(disk_info), instance_name)
|
5118 | 08db7c5c | Iustin Pop | |
5119 | e6c1ff2f | Iustin Pop | names = [] |
5120 | fb4b324b | Guido Trotter | for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i) |
5121 | e6c1ff2f | Iustin Pop | for i in range(disk_count)]): |
5122 | e6c1ff2f | Iustin Pop | names.append(lv_prefix + "_data")
|
5123 | e6c1ff2f | Iustin Pop | names.append(lv_prefix + "_meta")
|
5124 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5125 | 112050d9 | Iustin Pop | disk_index = idx + base_index |
5126 | 08db7c5c | Iustin Pop | disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node, |
5127 | 08db7c5c | Iustin Pop | disk["size"], names[idx*2:idx*2+2], |
5128 | e2a65344 | Iustin Pop | "disk/%d" % disk_index,
|
5129 | 08db7c5c | Iustin Pop | minors[idx*2], minors[idx*2+1]) |
5130 | 6ec66eae | Iustin Pop | disk_dev.mode = disk["mode"]
|
5131 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5132 | 0f1a06e3 | Manuel Franceschini | elif template_name == constants.DT_FILE:
|
5133 | 0f1a06e3 | Manuel Franceschini | if len(secondary_nodes) != 0: |
5134 | 0f1a06e3 | Manuel Franceschini | raise errors.ProgrammerError("Wrong template configuration") |
5135 | 0f1a06e3 | Manuel Franceschini | |
5136 | 08db7c5c | Iustin Pop | for idx, disk in enumerate(disk_info): |
5137 | 112050d9 | Iustin Pop | disk_index = idx + base_index |
5138 | 08db7c5c | Iustin Pop | disk_dev = objects.Disk(dev_type=constants.LD_FILE, size=disk["size"],
|
5139 | e2a65344 | Iustin Pop | iv_name="disk/%d" % disk_index,
|
5140 | 08db7c5c | Iustin Pop | logical_id=(file_driver, |
5141 | 08db7c5c | Iustin Pop | "%s/disk%d" % (file_storage_dir,
|
5142 | 43e99cff | Guido Trotter | disk_index)), |
5143 | 6ec66eae | Iustin Pop | mode=disk["mode"])
|
5144 | 08db7c5c | Iustin Pop | disks.append(disk_dev) |
5145 | a8083063 | Iustin Pop | else:
|
5146 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid disk template '%s'" % template_name) |
5147 | a8083063 | Iustin Pop | return disks
|
5148 | a8083063 | Iustin Pop | |
5149 | a8083063 | Iustin Pop | |
5150 | a0c3fea1 | Michael Hanselmann | def _GetInstanceInfoText(instance): |
5151 | 3ecf6786 | Iustin Pop | """Compute that text that should be added to the disk's metadata.
|
5152 | 3ecf6786 | Iustin Pop |
|
5153 | 3ecf6786 | Iustin Pop | """
|
5154 | a0c3fea1 | Michael Hanselmann | return "originstname+%s" % instance.name |
5155 | a0c3fea1 | Michael Hanselmann | |
5156 | a0c3fea1 | Michael Hanselmann | |
5157 | 621b7678 | Iustin Pop | def _CreateDisks(lu, instance, to_skip=None, target_node=None): |
5158 | a8083063 | Iustin Pop | """Create all disks for an instance.
|
5159 | a8083063 | Iustin Pop |
|
5160 | a8083063 | Iustin Pop | This abstracts away some work from AddInstance.
|
5161 | a8083063 | Iustin Pop |
|
5162 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
5163 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
5164 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
5165 | e4376078 | Iustin Pop | @param instance: the instance whose disks we should create
|
5166 | bd315bfa | Iustin Pop | @type to_skip: list
|
5167 | bd315bfa | Iustin Pop | @param to_skip: list of indices to skip
|
5168 | 621b7678 | Iustin Pop | @type target_node: string
|
5169 | 621b7678 | Iustin Pop | @param target_node: if passed, overrides the target node for creation
|
5170 | e4376078 | Iustin Pop | @rtype: boolean
|
5171 | e4376078 | Iustin Pop | @return: the success of the creation
|
5172 | a8083063 | Iustin Pop |
|
5173 | a8083063 | Iustin Pop | """
|
5174 | a0c3fea1 | Michael Hanselmann | info = _GetInstanceInfoText(instance) |
5175 | 621b7678 | Iustin Pop | if target_node is None: |
5176 | 621b7678 | Iustin Pop | pnode = instance.primary_node |
5177 | 621b7678 | Iustin Pop | all_nodes = instance.all_nodes |
5178 | 621b7678 | Iustin Pop | else:
|
5179 | 621b7678 | Iustin Pop | pnode = target_node |
5180 | 621b7678 | Iustin Pop | all_nodes = [pnode] |
5181 | a0c3fea1 | Michael Hanselmann | |
5182 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
5183 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
5184 | 428958aa | Iustin Pop | result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir) |
5185 | 0f1a06e3 | Manuel Franceschini | |
5186 | 4c4e4e1e | Iustin Pop | result.Raise("Failed to create directory '%s' on"
|
5187 | 9b4127eb | Guido Trotter | " node %s" % (file_storage_dir, pnode))
|
5188 | 0f1a06e3 | Manuel Franceschini | |
5189 | 24991749 | Iustin Pop | # Note: this needs to be kept in sync with adding of disks in
|
5190 | 24991749 | Iustin Pop | # LUSetInstanceParams
|
5191 | bd315bfa | Iustin Pop | for idx, device in enumerate(instance.disks): |
5192 | bd315bfa | Iustin Pop | if to_skip and idx in to_skip: |
5193 | bd315bfa | Iustin Pop | continue
|
5194 | 9a4f63d1 | Iustin Pop | logging.info("Creating volume %s for instance %s",
|
5195 | 9a4f63d1 | Iustin Pop | device.iv_name, instance.name) |
5196 | a8083063 | Iustin Pop | #HARDCODE
|
5197 | 621b7678 | Iustin Pop | for node in all_nodes: |
5198 | 428958aa | Iustin Pop | f_create = node == pnode |
5199 | 428958aa | Iustin Pop | _CreateBlockDev(lu, node, instance, device, f_create, info, f_create) |
5200 | a8083063 | Iustin Pop | |
5201 | a8083063 | Iustin Pop | |
5202 | 621b7678 | Iustin Pop | def _RemoveDisks(lu, instance, target_node=None): |
5203 | a8083063 | Iustin Pop | """Remove all disks for an instance.
|
5204 | a8083063 | Iustin Pop |
|
5205 | a8083063 | Iustin Pop | This abstracts away some work from `AddInstance()` and
|
5206 | a8083063 | Iustin Pop | `RemoveInstance()`. Note that in case some of the devices couldn't
|
5207 | 1d67656e | Iustin Pop | be removed, the removal will continue with the other ones (compare
|
5208 | a8083063 | Iustin Pop | with `_CreateDisks()`).
|
5209 | a8083063 | Iustin Pop |
|
5210 | e4376078 | Iustin Pop | @type lu: L{LogicalUnit}
|
5211 | e4376078 | Iustin Pop | @param lu: the logical unit on whose behalf we execute
|
5212 | e4376078 | Iustin Pop | @type instance: L{objects.Instance}
|
5213 | e4376078 | Iustin Pop | @param instance: the instance whose disks we should remove
|
5214 | 621b7678 | Iustin Pop | @type target_node: string
|
5215 | 621b7678 | Iustin Pop | @param target_node: used to override the node on which to remove the disks
|
5216 | e4376078 | Iustin Pop | @rtype: boolean
|
5217 | e4376078 | Iustin Pop | @return: the success of the removal
|
5218 | a8083063 | Iustin Pop |
|
5219 | a8083063 | Iustin Pop | """
|
5220 | 9a4f63d1 | Iustin Pop | logging.info("Removing block devices for instance %s", instance.name)
|
5221 | a8083063 | Iustin Pop | |
5222 | e1bc0878 | Iustin Pop | all_result = True
|
5223 | a8083063 | Iustin Pop | for device in instance.disks: |
5224 | 621b7678 | Iustin Pop | if target_node:
|
5225 | 621b7678 | Iustin Pop | edata = [(target_node, device)] |
5226 | 621b7678 | Iustin Pop | else:
|
5227 | 621b7678 | Iustin Pop | edata = device.ComputeNodeTree(instance.primary_node) |
5228 | 621b7678 | Iustin Pop | for node, disk in edata: |
5229 | b9bddb6b | Iustin Pop | lu.cfg.SetDiskID(disk, node) |
5230 | 4c4e4e1e | Iustin Pop | msg = lu.rpc.call_blockdev_remove(node, disk).fail_msg |
5231 | e1bc0878 | Iustin Pop | if msg:
|
5232 | e1bc0878 | Iustin Pop | lu.LogWarning("Could not remove block device %s on node %s,"
|
5233 | e1bc0878 | Iustin Pop | " continuing anyway: %s", device.iv_name, node, msg)
|
5234 | e1bc0878 | Iustin Pop | all_result = False
|
5235 | 0f1a06e3 | Manuel Franceschini | |
5236 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
5237 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
5238 | dfc2a24c | Guido Trotter | if target_node:
|
5239 | dfc2a24c | Guido Trotter | tgt = target_node |
5240 | 621b7678 | Iustin Pop | else:
|
5241 | dfc2a24c | Guido Trotter | tgt = instance.primary_node |
5242 | 621b7678 | Iustin Pop | result = lu.rpc.call_file_storage_dir_remove(tgt, file_storage_dir) |
5243 | 621b7678 | Iustin Pop | if result.fail_msg:
|
5244 | b2b8bcce | Iustin Pop | lu.LogWarning("Could not remove directory '%s' on node %s: %s",
|
5245 | 621b7678 | Iustin Pop | file_storage_dir, instance.primary_node, result.fail_msg) |
5246 | e1bc0878 | Iustin Pop | all_result = False
|
5247 | 0f1a06e3 | Manuel Franceschini | |
5248 | e1bc0878 | Iustin Pop | return all_result
|
5249 | a8083063 | Iustin Pop | |
5250 | a8083063 | Iustin Pop | |
5251 | 08db7c5c | Iustin Pop | def _ComputeDiskSize(disk_template, disks): |
5252 | e2fe6369 | Iustin Pop | """Compute disk size requirements in the volume group
|
5253 | e2fe6369 | Iustin Pop |
|
5254 | e2fe6369 | Iustin Pop | """
|
5255 | e2fe6369 | Iustin Pop | # Required free disk space as a function of disk and swap space
|
5256 | e2fe6369 | Iustin Pop | req_size_dict = { |
5257 | e2fe6369 | Iustin Pop | constants.DT_DISKLESS: None,
|
5258 | 08db7c5c | Iustin Pop | constants.DT_PLAIN: sum(d["size"] for d in disks), |
5259 | 08db7c5c | Iustin Pop | # 128 MB are added for drbd metadata for each disk
|
5260 | 08db7c5c | Iustin Pop | constants.DT_DRBD8: sum(d["size"] + 128 for d in disks), |
5261 | e2fe6369 | Iustin Pop | constants.DT_FILE: None,
|
5262 | e2fe6369 | Iustin Pop | } |
5263 | e2fe6369 | Iustin Pop | |
5264 | e2fe6369 | Iustin Pop | if disk_template not in req_size_dict: |
5265 | e2fe6369 | Iustin Pop | raise errors.ProgrammerError("Disk template '%s' size requirement" |
5266 | e2fe6369 | Iustin Pop | " is unknown" % disk_template)
|
5267 | e2fe6369 | Iustin Pop | |
5268 | e2fe6369 | Iustin Pop | return req_size_dict[disk_template]
|
5269 | e2fe6369 | Iustin Pop | |
5270 | e2fe6369 | Iustin Pop | |
5271 | 74409b12 | Iustin Pop | def _CheckHVParams(lu, nodenames, hvname, hvparams): |
5272 | 74409b12 | Iustin Pop | """Hypervisor parameter validation.
|
5273 | 74409b12 | Iustin Pop |
|
5274 | 74409b12 | Iustin Pop | This function abstract the hypervisor parameter validation to be
|
5275 | 74409b12 | Iustin Pop | used in both instance create and instance modify.
|
5276 | 74409b12 | Iustin Pop |
|
5277 | 74409b12 | Iustin Pop | @type lu: L{LogicalUnit}
|
5278 | 74409b12 | Iustin Pop | @param lu: the logical unit for which we check
|
5279 | 74409b12 | Iustin Pop | @type nodenames: list
|
5280 | 74409b12 | Iustin Pop | @param nodenames: the list of nodes on which we should check
|
5281 | 74409b12 | Iustin Pop | @type hvname: string
|
5282 | 74409b12 | Iustin Pop | @param hvname: the name of the hypervisor we should use
|
5283 | 74409b12 | Iustin Pop | @type hvparams: dict
|
5284 | 74409b12 | Iustin Pop | @param hvparams: the parameters which we need to check
|
5285 | 74409b12 | Iustin Pop | @raise errors.OpPrereqError: if the parameters are not valid
|
5286 | 74409b12 | Iustin Pop |
|
5287 | 74409b12 | Iustin Pop | """
|
5288 | 74409b12 | Iustin Pop | hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames, |
5289 | 74409b12 | Iustin Pop | hvname, |
5290 | 74409b12 | Iustin Pop | hvparams) |
5291 | 74409b12 | Iustin Pop | for node in nodenames: |
5292 | 781de953 | Iustin Pop | info = hvinfo[node] |
5293 | 68c6f21c | Iustin Pop | if info.offline:
|
5294 | 68c6f21c | Iustin Pop | continue
|
5295 | 4c4e4e1e | Iustin Pop | info.Raise("Hypervisor parameter validation failed on node %s" % node)
|
5296 | 74409b12 | Iustin Pop | |
5297 | 74409b12 | Iustin Pop | |
5298 | a8083063 | Iustin Pop | class LUCreateInstance(LogicalUnit): |
5299 | a8083063 | Iustin Pop | """Create an instance.
|
5300 | a8083063 | Iustin Pop |
|
5301 | a8083063 | Iustin Pop | """
|
5302 | a8083063 | Iustin Pop | HPATH = "instance-add"
|
5303 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
5304 | 08db7c5c | Iustin Pop | _OP_REQP = ["instance_name", "disks", "disk_template", |
5305 | 08db7c5c | Iustin Pop | "mode", "start", |
5306 | 08db7c5c | Iustin Pop | "wait_for_sync", "ip_check", "nics", |
5307 | 338e51e8 | Iustin Pop | "hvparams", "beparams"] |
5308 | 7baf741d | Guido Trotter | REQ_BGL = False
|
5309 | 7baf741d | Guido Trotter | |
5310 | 7baf741d | Guido Trotter | def _ExpandNode(self, node): |
5311 | 7baf741d | Guido Trotter | """Expands and checks one node name.
|
5312 | 7baf741d | Guido Trotter |
|
5313 | 7baf741d | Guido Trotter | """
|
5314 | 7baf741d | Guido Trotter | node_full = self.cfg.ExpandNodeName(node)
|
5315 | 7baf741d | Guido Trotter | if node_full is None: |
5316 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Unknown node %s" % node) |
5317 | 7baf741d | Guido Trotter | return node_full
|
5318 | 7baf741d | Guido Trotter | |
5319 | 7baf741d | Guido Trotter | def ExpandNames(self): |
5320 | 7baf741d | Guido Trotter | """ExpandNames for CreateInstance.
|
5321 | 7baf741d | Guido Trotter |
|
5322 | 7baf741d | Guido Trotter | Figure out the right locks for instance creation.
|
5323 | 7baf741d | Guido Trotter |
|
5324 | 7baf741d | Guido Trotter | """
|
5325 | 7baf741d | Guido Trotter | self.needed_locks = {}
|
5326 | 7baf741d | Guido Trotter | |
5327 | 7baf741d | Guido Trotter | # set optional parameters to none if they don't exist
|
5328 | 6785674e | Iustin Pop | for attr in ["pnode", "snode", "iallocator", "hypervisor"]: |
5329 | 7baf741d | Guido Trotter | if not hasattr(self.op, attr): |
5330 | 7baf741d | Guido Trotter | setattr(self.op, attr, None) |
5331 | 7baf741d | Guido Trotter | |
5332 | 4b2f38dd | Iustin Pop | # cheap checks, mostly valid constants given
|
5333 | 4b2f38dd | Iustin Pop | |
5334 | 7baf741d | Guido Trotter | # verify creation mode
|
5335 | 7baf741d | Guido Trotter | if self.op.mode not in (constants.INSTANCE_CREATE, |
5336 | 7baf741d | Guido Trotter | constants.INSTANCE_IMPORT): |
5337 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Invalid instance creation mode '%s'" % |
5338 | 7baf741d | Guido Trotter | self.op.mode)
|
5339 | 4b2f38dd | Iustin Pop | |
5340 | 7baf741d | Guido Trotter | # disk template and mirror node verification
|
5341 | 7baf741d | Guido Trotter | if self.op.disk_template not in constants.DISK_TEMPLATES: |
5342 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Invalid disk template name") |
5343 | 7baf741d | Guido Trotter | |
5344 | 4b2f38dd | Iustin Pop | if self.op.hypervisor is None: |
5345 | 4b2f38dd | Iustin Pop | self.op.hypervisor = self.cfg.GetHypervisorType() |
5346 | 4b2f38dd | Iustin Pop | |
5347 | 8705eb96 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
5348 | 8705eb96 | Iustin Pop | enabled_hvs = cluster.enabled_hypervisors |
5349 | 4b2f38dd | Iustin Pop | if self.op.hypervisor not in enabled_hvs: |
5350 | 4b2f38dd | Iustin Pop | raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the" |
5351 | 4b2f38dd | Iustin Pop | " cluster (%s)" % (self.op.hypervisor, |
5352 | 4b2f38dd | Iustin Pop | ",".join(enabled_hvs)))
|
5353 | 4b2f38dd | Iustin Pop | |
5354 | 6785674e | Iustin Pop | # check hypervisor parameter syntax (locally)
|
5355 | a5728081 | Guido Trotter | utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
|
5356 | abe609b2 | Guido Trotter | filled_hvp = objects.FillDict(cluster.hvparams[self.op.hypervisor],
|
5357 | 8705eb96 | Iustin Pop | self.op.hvparams)
|
5358 | 6785674e | Iustin Pop | hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
|
5359 | 8705eb96 | Iustin Pop | hv_type.CheckParameterSyntax(filled_hvp) |
5360 | 67fc3042 | Iustin Pop | self.hv_full = filled_hvp
|
5361 | 6785674e | Iustin Pop | |
5362 | 338e51e8 | Iustin Pop | # fill and remember the beparams dict
|
5363 | a5728081 | Guido Trotter | utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
|
5364 | 4ef7f423 | Guido Trotter | self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT],
|
5365 | 338e51e8 | Iustin Pop | self.op.beparams)
|
5366 | 338e51e8 | Iustin Pop | |
5367 | 7baf741d | Guido Trotter | #### instance parameters check
|
5368 | 7baf741d | Guido Trotter | |
5369 | 7baf741d | Guido Trotter | # instance name verification
|
5370 | 7baf741d | Guido Trotter | hostname1 = utils.HostInfo(self.op.instance_name)
|
5371 | 7baf741d | Guido Trotter | self.op.instance_name = instance_name = hostname1.name
|
5372 | 7baf741d | Guido Trotter | |
5373 | 7baf741d | Guido Trotter | # this is just a preventive check, but someone might still add this
|
5374 | 7baf741d | Guido Trotter | # instance in the meantime, and creation will fail at lock-add time
|
5375 | 7baf741d | Guido Trotter | if instance_name in self.cfg.GetInstanceList(): |
5376 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
5377 | 7baf741d | Guido Trotter | instance_name) |
5378 | 7baf741d | Guido Trotter | |
5379 | 7baf741d | Guido Trotter | self.add_locks[locking.LEVEL_INSTANCE] = instance_name
|
5380 | 7baf741d | Guido Trotter | |
5381 | 08db7c5c | Iustin Pop | # NIC buildup
|
5382 | 08db7c5c | Iustin Pop | self.nics = []
|
5383 | 9dce4771 | Guido Trotter | for idx, nic in enumerate(self.op.nics): |
5384 | 9dce4771 | Guido Trotter | nic_mode_req = nic.get("mode", None) |
5385 | 9dce4771 | Guido Trotter | nic_mode = nic_mode_req |
5386 | 9dce4771 | Guido Trotter | if nic_mode is None: |
5387 | 9dce4771 | Guido Trotter | nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE] |
5388 | 9dce4771 | Guido Trotter | |
5389 | 9dce4771 | Guido Trotter | # in routed mode, for the first nic, the default ip is 'auto'
|
5390 | 9dce4771 | Guido Trotter | if nic_mode == constants.NIC_MODE_ROUTED and idx == 0: |
5391 | 9dce4771 | Guido Trotter | default_ip_mode = constants.VALUE_AUTO |
5392 | 9dce4771 | Guido Trotter | else:
|
5393 | 9dce4771 | Guido Trotter | default_ip_mode = constants.VALUE_NONE |
5394 | 9dce4771 | Guido Trotter | |
5395 | 08db7c5c | Iustin Pop | # ip validity checks
|
5396 | 9dce4771 | Guido Trotter | ip = nic.get("ip", default_ip_mode)
|
5397 | 9dce4771 | Guido Trotter | if ip is None or ip.lower() == constants.VALUE_NONE: |
5398 | 08db7c5c | Iustin Pop | nic_ip = None
|
5399 | 08db7c5c | Iustin Pop | elif ip.lower() == constants.VALUE_AUTO:
|
5400 | 08db7c5c | Iustin Pop | nic_ip = hostname1.ip |
5401 | 08db7c5c | Iustin Pop | else:
|
5402 | 08db7c5c | Iustin Pop | if not utils.IsValidIP(ip): |
5403 | 08db7c5c | Iustin Pop | raise errors.OpPrereqError("Given IP address '%s' doesn't look" |
5404 | 08db7c5c | Iustin Pop | " like a valid IP" % ip)
|
5405 | 08db7c5c | Iustin Pop | nic_ip = ip |
5406 | 08db7c5c | Iustin Pop | |
5407 | 9dce4771 | Guido Trotter | # TODO: check the ip for uniqueness !!
|
5408 | 9dce4771 | Guido Trotter | if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip: |
5409 | 9dce4771 | Guido Trotter | raise errors.OpPrereqError("Routed nic mode requires an ip address") |
5410 | 9dce4771 | Guido Trotter | |
5411 | 08db7c5c | Iustin Pop | # MAC address verification
|
5412 | 08db7c5c | Iustin Pop | mac = nic.get("mac", constants.VALUE_AUTO)
|
5413 | 08db7c5c | Iustin Pop | if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
5414 | 08db7c5c | Iustin Pop | if not utils.IsValidMac(mac.lower()): |
5415 | 08db7c5c | Iustin Pop | raise errors.OpPrereqError("Invalid MAC address specified: %s" % |
5416 | 08db7c5c | Iustin Pop | mac) |
5417 | 87e43988 | Iustin Pop | else:
|
5418 | 87e43988 | Iustin Pop | # or validate/reserve the current one
|
5419 | 87e43988 | Iustin Pop | if self.cfg.IsMacInUse(mac): |
5420 | 87e43988 | Iustin Pop | raise errors.OpPrereqError("MAC address %s already in use" |
5421 | 87e43988 | Iustin Pop | " in cluster" % mac)
|
5422 | 87e43988 | Iustin Pop | |
5423 | 08db7c5c | Iustin Pop | # bridge verification
|
5424 | 9939547b | Iustin Pop | bridge = nic.get("bridge", None) |
5425 | 9dce4771 | Guido Trotter | link = nic.get("link", None) |
5426 | 9dce4771 | Guido Trotter | if bridge and link: |
5427 | 29921401 | Iustin Pop | raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'" |
5428 | 29921401 | Iustin Pop | " at the same time")
|
5429 | 9dce4771 | Guido Trotter | elif bridge and nic_mode == constants.NIC_MODE_ROUTED: |
5430 | 9dce4771 | Guido Trotter | raise errors.OpPrereqError("Cannot pass 'bridge' on a routed nic") |
5431 | 9dce4771 | Guido Trotter | elif bridge:
|
5432 | 9dce4771 | Guido Trotter | link = bridge |
5433 | 9dce4771 | Guido Trotter | |
5434 | 9dce4771 | Guido Trotter | nicparams = {} |
5435 | 9dce4771 | Guido Trotter | if nic_mode_req:
|
5436 | 9dce4771 | Guido Trotter | nicparams[constants.NIC_MODE] = nic_mode_req |
5437 | 9dce4771 | Guido Trotter | if link:
|
5438 | 9dce4771 | Guido Trotter | nicparams[constants.NIC_LINK] = link |
5439 | 9dce4771 | Guido Trotter | |
5440 | 9dce4771 | Guido Trotter | check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], |
5441 | 9dce4771 | Guido Trotter | nicparams) |
5442 | 9dce4771 | Guido Trotter | objects.NIC.CheckParameterSyntax(check_params) |
5443 | 9dce4771 | Guido Trotter | self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
|
5444 | 08db7c5c | Iustin Pop | |
5445 | 08db7c5c | Iustin Pop | # disk checks/pre-build
|
5446 | 08db7c5c | Iustin Pop | self.disks = []
|
5447 | 08db7c5c | Iustin Pop | for disk in self.op.disks: |
5448 | 08db7c5c | Iustin Pop | mode = disk.get("mode", constants.DISK_RDWR)
|
5449 | 08db7c5c | Iustin Pop | if mode not in constants.DISK_ACCESS_SET: |
5450 | 08db7c5c | Iustin Pop | raise errors.OpPrereqError("Invalid disk access mode '%s'" % |
5451 | 08db7c5c | Iustin Pop | mode) |
5452 | 08db7c5c | Iustin Pop | size = disk.get("size", None) |
5453 | 08db7c5c | Iustin Pop | if size is None: |
5454 | 08db7c5c | Iustin Pop | raise errors.OpPrereqError("Missing disk size") |
5455 | 08db7c5c | Iustin Pop | try:
|
5456 | 08db7c5c | Iustin Pop | size = int(size)
|
5457 | 08db7c5c | Iustin Pop | except ValueError: |
5458 | 08db7c5c | Iustin Pop | raise errors.OpPrereqError("Invalid disk size '%s'" % size) |
5459 | 08db7c5c | Iustin Pop | self.disks.append({"size": size, "mode": mode}) |
5460 | 08db7c5c | Iustin Pop | |
5461 | 7baf741d | Guido Trotter | # used in CheckPrereq for ip ping check
|
5462 | 7baf741d | Guido Trotter | self.check_ip = hostname1.ip
|
5463 | 7baf741d | Guido Trotter | |
5464 | 7baf741d | Guido Trotter | # file storage checks
|
5465 | 7baf741d | Guido Trotter | if (self.op.file_driver and |
5466 | 7baf741d | Guido Trotter | not self.op.file_driver in constants.FILE_DRIVER): |
5467 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("Invalid file driver name '%s'" % |
5468 | 7baf741d | Guido Trotter | self.op.file_driver)
|
5469 | 7baf741d | Guido Trotter | |
5470 | 7baf741d | Guido Trotter | if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir): |
5471 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("File storage directory path not absolute") |
5472 | 7baf741d | Guido Trotter | |
5473 | 7baf741d | Guido Trotter | ### Node/iallocator related checks
|
5474 | 7baf741d | Guido Trotter | if [self.op.iallocator, self.op.pnode].count(None) != 1: |
5475 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("One and only one of iallocator and primary" |
5476 | 7baf741d | Guido Trotter | " node must be given")
|
5477 | 7baf741d | Guido Trotter | |
5478 | 7baf741d | Guido Trotter | if self.op.iallocator: |
5479 | 7baf741d | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
5480 | 7baf741d | Guido Trotter | else:
|
5481 | 7baf741d | Guido Trotter | self.op.pnode = self._ExpandNode(self.op.pnode) |
5482 | 7baf741d | Guido Trotter | nodelist = [self.op.pnode]
|
5483 | 7baf741d | Guido Trotter | if self.op.snode is not None: |
5484 | 7baf741d | Guido Trotter | self.op.snode = self._ExpandNode(self.op.snode) |
5485 | 7baf741d | Guido Trotter | nodelist.append(self.op.snode)
|
5486 | 7baf741d | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = nodelist
|
5487 | 7baf741d | Guido Trotter | |
5488 | 7baf741d | Guido Trotter | # in case of import lock the source node too
|
5489 | 7baf741d | Guido Trotter | if self.op.mode == constants.INSTANCE_IMPORT: |
5490 | 7baf741d | Guido Trotter | src_node = getattr(self.op, "src_node", None) |
5491 | 7baf741d | Guido Trotter | src_path = getattr(self.op, "src_path", None) |
5492 | 7baf741d | Guido Trotter | |
5493 | b9322a9f | Guido Trotter | if src_path is None: |
5494 | b9322a9f | Guido Trotter | self.op.src_path = src_path = self.op.instance_name |
5495 | b9322a9f | Guido Trotter | |
5496 | b9322a9f | Guido Trotter | if src_node is None: |
5497 | b9322a9f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
5498 | b9322a9f | Guido Trotter | self.op.src_node = None |
5499 | b9322a9f | Guido Trotter | if os.path.isabs(src_path):
|
5500 | b9322a9f | Guido Trotter | raise errors.OpPrereqError("Importing an instance from an absolute" |
5501 | b9322a9f | Guido Trotter | " path requires a source node option.")
|
5502 | b9322a9f | Guido Trotter | else:
|
5503 | b9322a9f | Guido Trotter | self.op.src_node = src_node = self._ExpandNode(src_node) |
5504 | b9322a9f | Guido Trotter | if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET: |
5505 | b9322a9f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE].append(src_node)
|
5506 | b9322a9f | Guido Trotter | if not os.path.isabs(src_path): |
5507 | b9322a9f | Guido Trotter | self.op.src_path = src_path = \
|
5508 | b9322a9f | Guido Trotter | os.path.join(constants.EXPORT_DIR, src_path) |
5509 | 7baf741d | Guido Trotter | |
5510 | 7baf741d | Guido Trotter | else: # INSTANCE_CREATE |
5511 | 7baf741d | Guido Trotter | if getattr(self.op, "os_type", None) is None: |
5512 | 7baf741d | Guido Trotter | raise errors.OpPrereqError("No guest OS specified") |
5513 | a8083063 | Iustin Pop | |
5514 | 538475ca | Iustin Pop | def _RunAllocator(self): |
5515 | 538475ca | Iustin Pop | """Run the allocator based on input opcode.
|
5516 | 538475ca | Iustin Pop |
|
5517 | 538475ca | Iustin Pop | """
|
5518 | 08db7c5c | Iustin Pop | nics = [n.ToDict() for n in self.nics] |
5519 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
5520 | 29859cb7 | Iustin Pop | mode=constants.IALLOCATOR_MODE_ALLOC, |
5521 | d1c2dd75 | Iustin Pop | name=self.op.instance_name,
|
5522 | d1c2dd75 | Iustin Pop | disk_template=self.op.disk_template,
|
5523 | d1c2dd75 | Iustin Pop | tags=[], |
5524 | d1c2dd75 | Iustin Pop | os=self.op.os_type,
|
5525 | 338e51e8 | Iustin Pop | vcpus=self.be_full[constants.BE_VCPUS],
|
5526 | 338e51e8 | Iustin Pop | mem_size=self.be_full[constants.BE_MEMORY],
|
5527 | 08db7c5c | Iustin Pop | disks=self.disks,
|
5528 | d1c2dd75 | Iustin Pop | nics=nics, |
5529 | 8cc7e742 | Guido Trotter | hypervisor=self.op.hypervisor,
|
5530 | 29859cb7 | Iustin Pop | ) |
5531 | d1c2dd75 | Iustin Pop | |
5532 | d1c2dd75 | Iustin Pop | ial.Run(self.op.iallocator)
|
5533 | d1c2dd75 | Iustin Pop | |
5534 | d1c2dd75 | Iustin Pop | if not ial.success: |
5535 | 538475ca | Iustin Pop | raise errors.OpPrereqError("Can't compute nodes using" |
5536 | 538475ca | Iustin Pop | " iallocator '%s': %s" % (self.op.iallocator, |
5537 | d1c2dd75 | Iustin Pop | ial.info)) |
5538 | 27579978 | Iustin Pop | if len(ial.nodes) != ial.required_nodes: |
5539 | 538475ca | Iustin Pop | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
5540 | 538475ca | Iustin Pop | " of nodes (%s), required %s" %
|
5541 | 97abc79f | Iustin Pop | (self.op.iallocator, len(ial.nodes), |
5542 | 1ce4bbe3 | Renรฉ Nussbaumer | ial.required_nodes)) |
5543 | d1c2dd75 | Iustin Pop | self.op.pnode = ial.nodes[0] |
5544 | 86d9d3bb | Iustin Pop | self.LogInfo("Selected nodes for instance %s via iallocator %s: %s", |
5545 | 86d9d3bb | Iustin Pop | self.op.instance_name, self.op.iallocator, |
5546 | 86d9d3bb | Iustin Pop | ", ".join(ial.nodes))
|
5547 | 27579978 | Iustin Pop | if ial.required_nodes == 2: |
5548 | d1c2dd75 | Iustin Pop | self.op.snode = ial.nodes[1] |
5549 | 538475ca | Iustin Pop | |
5550 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
5551 | a8083063 | Iustin Pop | """Build hooks env.
|
5552 | a8083063 | Iustin Pop |
|
5553 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
5554 | a8083063 | Iustin Pop |
|
5555 | a8083063 | Iustin Pop | """
|
5556 | a8083063 | Iustin Pop | env = { |
5557 | 2c2690c9 | Iustin Pop | "ADD_MODE": self.op.mode, |
5558 | a8083063 | Iustin Pop | } |
5559 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
5560 | 2c2690c9 | Iustin Pop | env["SRC_NODE"] = self.op.src_node |
5561 | 2c2690c9 | Iustin Pop | env["SRC_PATH"] = self.op.src_path |
5562 | 2c2690c9 | Iustin Pop | env["SRC_IMAGES"] = self.src_images |
5563 | 396e1b78 | Michael Hanselmann | |
5564 | 2c2690c9 | Iustin Pop | env.update(_BuildInstanceHookEnv( |
5565 | 2c2690c9 | Iustin Pop | name=self.op.instance_name,
|
5566 | 396e1b78 | Michael Hanselmann | primary_node=self.op.pnode,
|
5567 | 396e1b78 | Michael Hanselmann | secondary_nodes=self.secondaries,
|
5568 | 4978db17 | Iustin Pop | status=self.op.start,
|
5569 | ecb215b5 | Michael Hanselmann | os_type=self.op.os_type,
|
5570 | 338e51e8 | Iustin Pop | memory=self.be_full[constants.BE_MEMORY],
|
5571 | 338e51e8 | Iustin Pop | vcpus=self.be_full[constants.BE_VCPUS],
|
5572 | f9b10246 | Guido Trotter | nics=_NICListToTuple(self, self.nics), |
5573 | 2c2690c9 | Iustin Pop | disk_template=self.op.disk_template,
|
5574 | 2c2690c9 | Iustin Pop | disks=[(d["size"], d["mode"]) for d in self.disks], |
5575 | 67fc3042 | Iustin Pop | bep=self.be_full,
|
5576 | 67fc3042 | Iustin Pop | hvp=self.hv_full,
|
5577 | 3df6e710 | Iustin Pop | hypervisor_name=self.op.hypervisor,
|
5578 | 396e1b78 | Michael Hanselmann | )) |
5579 | a8083063 | Iustin Pop | |
5580 | d6a02168 | Michael Hanselmann | nl = ([self.cfg.GetMasterNode(), self.op.pnode] + |
5581 | a8083063 | Iustin Pop | self.secondaries)
|
5582 | a8083063 | Iustin Pop | return env, nl, nl
|
5583 | a8083063 | Iustin Pop | |
5584 | a8083063 | Iustin Pop | |
5585 | a8083063 | Iustin Pop | def CheckPrereq(self): |
5586 | a8083063 | Iustin Pop | """Check prerequisites.
|
5587 | a8083063 | Iustin Pop |
|
5588 | a8083063 | Iustin Pop | """
|
5589 | eedc99de | Manuel Franceschini | if (not self.cfg.GetVGName() and |
5590 | eedc99de | Manuel Franceschini | self.op.disk_template not in constants.DTS_NOT_LVM): |
5591 | eedc99de | Manuel Franceschini | raise errors.OpPrereqError("Cluster does not support lvm-based" |
5592 | eedc99de | Manuel Franceschini | " instances")
|
5593 | eedc99de | Manuel Franceschini | |
5594 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
5595 | 7baf741d | Guido Trotter | src_node = self.op.src_node
|
5596 | 7baf741d | Guido Trotter | src_path = self.op.src_path
|
5597 | a8083063 | Iustin Pop | |
5598 | c0cbdc67 | Guido Trotter | if src_node is None: |
5599 | 1b7bfbb7 | Iustin Pop | locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
|
5600 | 1b7bfbb7 | Iustin Pop | exp_list = self.rpc.call_export_list(locked_nodes)
|
5601 | c0cbdc67 | Guido Trotter | found = False
|
5602 | c0cbdc67 | Guido Trotter | for node in exp_list: |
5603 | 4c4e4e1e | Iustin Pop | if exp_list[node].fail_msg:
|
5604 | 1b7bfbb7 | Iustin Pop | continue
|
5605 | 1b7bfbb7 | Iustin Pop | if src_path in exp_list[node].payload: |
5606 | c0cbdc67 | Guido Trotter | found = True
|
5607 | c0cbdc67 | Guido Trotter | self.op.src_node = src_node = node
|
5608 | c0cbdc67 | Guido Trotter | self.op.src_path = src_path = os.path.join(constants.EXPORT_DIR,
|
5609 | c0cbdc67 | Guido Trotter | src_path) |
5610 | c0cbdc67 | Guido Trotter | break
|
5611 | c0cbdc67 | Guido Trotter | if not found: |
5612 | c0cbdc67 | Guido Trotter | raise errors.OpPrereqError("No export found for relative path %s" % |
5613 | c0cbdc67 | Guido Trotter | src_path) |
5614 | c0cbdc67 | Guido Trotter | |
5615 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, src_node)
|
5616 | 781de953 | Iustin Pop | result = self.rpc.call_export_info(src_node, src_path)
|
5617 | 4c4e4e1e | Iustin Pop | result.Raise("No export or invalid export found in dir %s" % src_path)
|
5618 | a8083063 | Iustin Pop | |
5619 | 3eccac06 | Iustin Pop | export_info = objects.SerializableConfigParser.Loads(str(result.payload))
|
5620 | a8083063 | Iustin Pop | if not export_info.has_section(constants.INISECT_EXP): |
5621 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Corrupted export config") |
5622 | a8083063 | Iustin Pop | |
5623 | a8083063 | Iustin Pop | ei_version = export_info.get(constants.INISECT_EXP, 'version')
|
5624 | a8083063 | Iustin Pop | if (int(ei_version) != constants.EXPORT_VERSION): |
5625 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Wrong export version %s (wanted %d)" % |
5626 | 3ecf6786 | Iustin Pop | (ei_version, constants.EXPORT_VERSION)) |
5627 | a8083063 | Iustin Pop | |
5628 | 09acf207 | Guido Trotter | # Check that the new instance doesn't have less disks than the export
|
5629 | 08db7c5c | Iustin Pop | instance_disks = len(self.disks) |
5630 | 09acf207 | Guido Trotter | export_disks = export_info.getint(constants.INISECT_INS, 'disk_count')
|
5631 | 09acf207 | Guido Trotter | if instance_disks < export_disks:
|
5632 | 09acf207 | Guido Trotter | raise errors.OpPrereqError("Not enough disks to import." |
5633 | 09acf207 | Guido Trotter | " (instance: %d, export: %d)" %
|
5634 | 726d7d68 | Iustin Pop | (instance_disks, export_disks)) |
5635 | a8083063 | Iustin Pop | |
5636 | a8083063 | Iustin Pop | self.op.os_type = export_info.get(constants.INISECT_EXP, 'os') |
5637 | 09acf207 | Guido Trotter | disk_images = [] |
5638 | 09acf207 | Guido Trotter | for idx in range(export_disks): |
5639 | 09acf207 | Guido Trotter | option = 'disk%d_dump' % idx
|
5640 | 09acf207 | Guido Trotter | if export_info.has_option(constants.INISECT_INS, option):
|
5641 | 09acf207 | Guido Trotter | # FIXME: are the old os-es, disk sizes, etc. useful?
|
5642 | 09acf207 | Guido Trotter | export_name = export_info.get(constants.INISECT_INS, option) |
5643 | 09acf207 | Guido Trotter | image = os.path.join(src_path, export_name) |
5644 | 09acf207 | Guido Trotter | disk_images.append(image) |
5645 | 09acf207 | Guido Trotter | else:
|
5646 | 09acf207 | Guido Trotter | disk_images.append(False)
|
5647 | 09acf207 | Guido Trotter | |
5648 | 09acf207 | Guido Trotter | self.src_images = disk_images
|
5649 | 901a65c1 | Iustin Pop | |
5650 | b4364a6b | Guido Trotter | old_name = export_info.get(constants.INISECT_INS, 'name')
|
5651 | b4364a6b | Guido Trotter | # FIXME: int() here could throw a ValueError on broken exports
|
5652 | b4364a6b | Guido Trotter | exp_nic_count = int(export_info.get(constants.INISECT_INS, 'nic_count')) |
5653 | b4364a6b | Guido Trotter | if self.op.instance_name == old_name: |
5654 | b4364a6b | Guido Trotter | for idx, nic in enumerate(self.nics): |
5655 | b4364a6b | Guido Trotter | if nic.mac == constants.VALUE_AUTO and exp_nic_count >= idx: |
5656 | b4364a6b | Guido Trotter | nic_mac_ini = 'nic%d_mac' % idx
|
5657 | b4364a6b | Guido Trotter | nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini) |
5658 | bc89efc3 | Guido Trotter | |
5659 | 295728df | Guido Trotter | # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
|
5660 | 7baf741d | Guido Trotter | # ip ping checks (we use the same ip that was resolved in ExpandNames)
|
5661 | 901a65c1 | Iustin Pop | if self.op.start and not self.op.ip_check: |
5662 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("Cannot ignore IP address conflicts when" |
5663 | 901a65c1 | Iustin Pop | " adding an instance in start mode")
|
5664 | 901a65c1 | Iustin Pop | |
5665 | 901a65c1 | Iustin Pop | if self.op.ip_check: |
5666 | 7baf741d | Guido Trotter | if utils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT): |
5667 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
5668 | 7b3a8fb5 | Iustin Pop | (self.check_ip, self.op.instance_name)) |
5669 | 901a65c1 | Iustin Pop | |
5670 | 295728df | Guido Trotter | #### mac address generation
|
5671 | 295728df | Guido Trotter | # By generating here the mac address both the allocator and the hooks get
|
5672 | 295728df | Guido Trotter | # the real final mac address rather than the 'auto' or 'generate' value.
|
5673 | 295728df | Guido Trotter | # There is a race condition between the generation and the instance object
|
5674 | 295728df | Guido Trotter | # creation, which means that we know the mac is valid now, but we're not
|
5675 | 295728df | Guido Trotter | # sure it will be when we actually add the instance. If things go bad
|
5676 | 295728df | Guido Trotter | # adding the instance will abort because of a duplicate mac, and the
|
5677 | 295728df | Guido Trotter | # creation job will fail.
|
5678 | 295728df | Guido Trotter | for nic in self.nics: |
5679 | 295728df | Guido Trotter | if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
5680 | 295728df | Guido Trotter | nic.mac = self.cfg.GenerateMAC()
|
5681 | 295728df | Guido Trotter | |
5682 | 538475ca | Iustin Pop | #### allocator run
|
5683 | 538475ca | Iustin Pop | |
5684 | 538475ca | Iustin Pop | if self.op.iallocator is not None: |
5685 | 538475ca | Iustin Pop | self._RunAllocator()
|
5686 | 0f1a06e3 | Manuel Franceschini | |
5687 | 901a65c1 | Iustin Pop | #### node related checks
|
5688 | 901a65c1 | Iustin Pop | |
5689 | 901a65c1 | Iustin Pop | # check primary node
|
5690 | 7baf741d | Guido Trotter | self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode) |
5691 | 7baf741d | Guido Trotter | assert self.pnode is not None, \ |
5692 | 7baf741d | Guido Trotter | "Cannot retrieve locked node %s" % self.op.pnode |
5693 | 7527a8a4 | Iustin Pop | if pnode.offline:
|
5694 | 7527a8a4 | Iustin Pop | raise errors.OpPrereqError("Cannot use offline primary node '%s'" % |
5695 | 7527a8a4 | Iustin Pop | pnode.name) |
5696 | 733a2b6a | Iustin Pop | if pnode.drained:
|
5697 | 733a2b6a | Iustin Pop | raise errors.OpPrereqError("Cannot use drained primary node '%s'" % |
5698 | 733a2b6a | Iustin Pop | pnode.name) |
5699 | 7527a8a4 | Iustin Pop | |
5700 | 901a65c1 | Iustin Pop | self.secondaries = []
|
5701 | 901a65c1 | Iustin Pop | |
5702 | 901a65c1 | Iustin Pop | # mirror node verification
|
5703 | a1f445d3 | Iustin Pop | if self.op.disk_template in constants.DTS_NET_MIRROR: |
5704 | 7baf741d | Guido Trotter | if self.op.snode is None: |
5705 | a1f445d3 | Iustin Pop | raise errors.OpPrereqError("The networked disk templates need" |
5706 | 3ecf6786 | Iustin Pop | " a mirror node")
|
5707 | 7baf741d | Guido Trotter | if self.op.snode == pnode.name: |
5708 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The secondary node cannot be" |
5709 | 3ecf6786 | Iustin Pop | " the primary node.")
|
5710 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, self.op.snode) |
5711 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, self.op.snode) |
5712 | 733a2b6a | Iustin Pop | self.secondaries.append(self.op.snode) |
5713 | a8083063 | Iustin Pop | |
5714 | 6785674e | Iustin Pop | nodenames = [pnode.name] + self.secondaries
|
5715 | 6785674e | Iustin Pop | |
5716 | e2fe6369 | Iustin Pop | req_size = _ComputeDiskSize(self.op.disk_template,
|
5717 | 08db7c5c | Iustin Pop | self.disks)
|
5718 | ed1ebc60 | Guido Trotter | |
5719 | 8d75db10 | Iustin Pop | # Check lv size requirements
|
5720 | 8d75db10 | Iustin Pop | if req_size is not None: |
5721 | 72737a7f | Iustin Pop | nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(), |
5722 | 72737a7f | Iustin Pop | self.op.hypervisor)
|
5723 | 8d75db10 | Iustin Pop | for node in nodenames: |
5724 | 781de953 | Iustin Pop | info = nodeinfo[node] |
5725 | 4c4e4e1e | Iustin Pop | info.Raise("Cannot get current information from node %s" % node)
|
5726 | 070e998b | Iustin Pop | info = info.payload |
5727 | 8d75db10 | Iustin Pop | vg_free = info.get('vg_free', None) |
5728 | 8d75db10 | Iustin Pop | if not isinstance(vg_free, int): |
5729 | 8d75db10 | Iustin Pop | raise errors.OpPrereqError("Can't compute free disk space on" |
5730 | 8d75db10 | Iustin Pop | " node %s" % node)
|
5731 | 070e998b | Iustin Pop | if req_size > vg_free:
|
5732 | 8d75db10 | Iustin Pop | raise errors.OpPrereqError("Not enough disk space on target node %s." |
5733 | 8d75db10 | Iustin Pop | " %d MB available, %d MB required" %
|
5734 | 070e998b | Iustin Pop | (node, vg_free, req_size)) |
5735 | ed1ebc60 | Guido Trotter | |
5736 | 74409b12 | Iustin Pop | _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams) |
5737 | 6785674e | Iustin Pop | |
5738 | a8083063 | Iustin Pop | # os verification
|
5739 | 781de953 | Iustin Pop | result = self.rpc.call_os_get(pnode.name, self.op.os_type) |
5740 | 4c4e4e1e | Iustin Pop | result.Raise("OS '%s' not in supported os list for primary node %s" %
|
5741 | 4c4e4e1e | Iustin Pop | (self.op.os_type, pnode.name), prereq=True) |
5742 | a8083063 | Iustin Pop | |
5743 | b165e77e | Guido Trotter | _CheckNicsBridgesExist(self, self.nics, self.pnode.name) |
5744 | a8083063 | Iustin Pop | |
5745 | 49ce1563 | Iustin Pop | # memory check on primary node
|
5746 | 49ce1563 | Iustin Pop | if self.op.start: |
5747 | b9bddb6b | Iustin Pop | _CheckNodeFreeMemory(self, self.pnode.name, |
5748 | 49ce1563 | Iustin Pop | "creating instance %s" % self.op.instance_name, |
5749 | 338e51e8 | Iustin Pop | self.be_full[constants.BE_MEMORY],
|
5750 | 338e51e8 | Iustin Pop | self.op.hypervisor)
|
5751 | 49ce1563 | Iustin Pop | |
5752 | 08896026 | Iustin Pop | self.dry_run_result = list(nodenames) |
5753 | 08896026 | Iustin Pop | |
5754 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
5755 | a8083063 | Iustin Pop | """Create and add the instance to the cluster.
|
5756 | a8083063 | Iustin Pop |
|
5757 | a8083063 | Iustin Pop | """
|
5758 | a8083063 | Iustin Pop | instance = self.op.instance_name
|
5759 | a8083063 | Iustin Pop | pnode_name = self.pnode.name
|
5760 | a8083063 | Iustin Pop | |
5761 | e69d05fd | Iustin Pop | ht_kind = self.op.hypervisor
|
5762 | 2a6469d5 | Alexander Schreiber | if ht_kind in constants.HTS_REQ_PORT: |
5763 | 2a6469d5 | Alexander Schreiber | network_port = self.cfg.AllocatePort()
|
5764 | 2a6469d5 | Alexander Schreiber | else:
|
5765 | 2a6469d5 | Alexander Schreiber | network_port = None
|
5766 | 58acb49d | Alexander Schreiber | |
5767 | 6785674e | Iustin Pop | ##if self.op.vnc_bind_address is None:
|
5768 | 6785674e | Iustin Pop | ## self.op.vnc_bind_address = constants.VNC_DEFAULT_BIND_ADDRESS
|
5769 | 31a853d2 | Iustin Pop | |
5770 | 2c313123 | Manuel Franceschini | # this is needed because os.path.join does not accept None arguments
|
5771 | 2c313123 | Manuel Franceschini | if self.op.file_storage_dir is None: |
5772 | 2c313123 | Manuel Franceschini | string_file_storage_dir = ""
|
5773 | 2c313123 | Manuel Franceschini | else:
|
5774 | 2c313123 | Manuel Franceschini | string_file_storage_dir = self.op.file_storage_dir
|
5775 | 2c313123 | Manuel Franceschini | |
5776 | 0f1a06e3 | Manuel Franceschini | # build the full file storage dir path
|
5777 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.normpath(os.path.join( |
5778 | d6a02168 | Michael Hanselmann | self.cfg.GetFileStorageDir(),
|
5779 | 2c313123 | Manuel Franceschini | string_file_storage_dir, instance)) |
5780 | 0f1a06e3 | Manuel Franceschini | |
5781 | 0f1a06e3 | Manuel Franceschini | |
5782 | b9bddb6b | Iustin Pop | disks = _GenerateDiskTemplate(self,
|
5783 | a8083063 | Iustin Pop | self.op.disk_template,
|
5784 | a8083063 | Iustin Pop | instance, pnode_name, |
5785 | 08db7c5c | Iustin Pop | self.secondaries,
|
5786 | 08db7c5c | Iustin Pop | self.disks,
|
5787 | 0f1a06e3 | Manuel Franceschini | file_storage_dir, |
5788 | e2a65344 | Iustin Pop | self.op.file_driver,
|
5789 | e2a65344 | Iustin Pop | 0)
|
5790 | a8083063 | Iustin Pop | |
5791 | a8083063 | Iustin Pop | iobj = objects.Instance(name=instance, os=self.op.os_type,
|
5792 | a8083063 | Iustin Pop | primary_node=pnode_name, |
5793 | 08db7c5c | Iustin Pop | nics=self.nics, disks=disks,
|
5794 | a8083063 | Iustin Pop | disk_template=self.op.disk_template,
|
5795 | 4978db17 | Iustin Pop | admin_up=False,
|
5796 | 58acb49d | Alexander Schreiber | network_port=network_port, |
5797 | 338e51e8 | Iustin Pop | beparams=self.op.beparams,
|
5798 | 6785674e | Iustin Pop | hvparams=self.op.hvparams,
|
5799 | e69d05fd | Iustin Pop | hypervisor=self.op.hypervisor,
|
5800 | a8083063 | Iustin Pop | ) |
5801 | a8083063 | Iustin Pop | |
5802 | a8083063 | Iustin Pop | feedback_fn("* creating instance disks...")
|
5803 | 796cab27 | Iustin Pop | try:
|
5804 | 796cab27 | Iustin Pop | _CreateDisks(self, iobj)
|
5805 | 796cab27 | Iustin Pop | except errors.OpExecError:
|
5806 | 796cab27 | Iustin Pop | self.LogWarning("Device creation failed, reverting...") |
5807 | 796cab27 | Iustin Pop | try:
|
5808 | 796cab27 | Iustin Pop | _RemoveDisks(self, iobj)
|
5809 | 796cab27 | Iustin Pop | finally:
|
5810 | 796cab27 | Iustin Pop | self.cfg.ReleaseDRBDMinors(instance)
|
5811 | 796cab27 | Iustin Pop | raise
|
5812 | a8083063 | Iustin Pop | |
5813 | a8083063 | Iustin Pop | feedback_fn("adding instance %s to cluster config" % instance)
|
5814 | a8083063 | Iustin Pop | |
5815 | a8083063 | Iustin Pop | self.cfg.AddInstance(iobj)
|
5816 | 7baf741d | Guido Trotter | # Declare that we don't want to remove the instance lock anymore, as we've
|
5817 | 7baf741d | Guido Trotter | # added the instance to the config
|
5818 | 7baf741d | Guido Trotter | del self.remove_locks[locking.LEVEL_INSTANCE] |
5819 | e36e96b4 | Guido Trotter | # Unlock all the nodes
|
5820 | 9c8971d7 | Guido Trotter | if self.op.mode == constants.INSTANCE_IMPORT: |
5821 | 9c8971d7 | Guido Trotter | nodes_keep = [self.op.src_node]
|
5822 | 9c8971d7 | Guido Trotter | nodes_release = [node for node in self.acquired_locks[locking.LEVEL_NODE] |
5823 | 9c8971d7 | Guido Trotter | if node != self.op.src_node] |
5824 | 9c8971d7 | Guido Trotter | self.context.glm.release(locking.LEVEL_NODE, nodes_release)
|
5825 | 9c8971d7 | Guido Trotter | self.acquired_locks[locking.LEVEL_NODE] = nodes_keep
|
5826 | 9c8971d7 | Guido Trotter | else:
|
5827 | 9c8971d7 | Guido Trotter | self.context.glm.release(locking.LEVEL_NODE)
|
5828 | 9c8971d7 | Guido Trotter | del self.acquired_locks[locking.LEVEL_NODE] |
5829 | a8083063 | Iustin Pop | |
5830 | a8083063 | Iustin Pop | if self.op.wait_for_sync: |
5831 | b9bddb6b | Iustin Pop | disk_abort = not _WaitForSync(self, iobj) |
5832 | a1f445d3 | Iustin Pop | elif iobj.disk_template in constants.DTS_NET_MIRROR: |
5833 | a8083063 | Iustin Pop | # make sure the disks are not degraded (still sync-ing is ok)
|
5834 | a8083063 | Iustin Pop | time.sleep(15)
|
5835 | a8083063 | Iustin Pop | feedback_fn("* checking mirrors status")
|
5836 | b9bddb6b | Iustin Pop | disk_abort = not _WaitForSync(self, iobj, oneshot=True) |
5837 | a8083063 | Iustin Pop | else:
|
5838 | a8083063 | Iustin Pop | disk_abort = False
|
5839 | a8083063 | Iustin Pop | |
5840 | a8083063 | Iustin Pop | if disk_abort:
|
5841 | b9bddb6b | Iustin Pop | _RemoveDisks(self, iobj)
|
5842 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(iobj.name)
|
5843 | 7baf741d | Guido Trotter | # Make sure the instance lock gets removed
|
5844 | 7baf741d | Guido Trotter | self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
|
5845 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("There are some degraded disks for" |
5846 | 3ecf6786 | Iustin Pop | " this instance")
|
5847 | a8083063 | Iustin Pop | |
5848 | a8083063 | Iustin Pop | feedback_fn("creating os for instance %s on node %s" %
|
5849 | a8083063 | Iustin Pop | (instance, pnode_name)) |
5850 | a8083063 | Iustin Pop | |
5851 | a8083063 | Iustin Pop | if iobj.disk_template != constants.DT_DISKLESS:
|
5852 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_CREATE: |
5853 | a8083063 | Iustin Pop | feedback_fn("* running the instance OS create scripts...")
|
5854 | e557bae9 | Guido Trotter | result = self.rpc.call_instance_os_add(pnode_name, iobj, False) |
5855 | 4c4e4e1e | Iustin Pop | result.Raise("Could not add os for instance %s"
|
5856 | 4c4e4e1e | Iustin Pop | " on node %s" % (instance, pnode_name))
|
5857 | a8083063 | Iustin Pop | |
5858 | a8083063 | Iustin Pop | elif self.op.mode == constants.INSTANCE_IMPORT: |
5859 | a8083063 | Iustin Pop | feedback_fn("* running the instance OS import scripts...")
|
5860 | a8083063 | Iustin Pop | src_node = self.op.src_node
|
5861 | 09acf207 | Guido Trotter | src_images = self.src_images
|
5862 | 62c9ec92 | Iustin Pop | cluster_name = self.cfg.GetClusterName()
|
5863 | 6c0af70e | Guido Trotter | import_result = self.rpc.call_instance_os_import(pnode_name, iobj,
|
5864 | 09acf207 | Guido Trotter | src_node, src_images, |
5865 | 6c0af70e | Guido Trotter | cluster_name) |
5866 | 4c4e4e1e | Iustin Pop | msg = import_result.fail_msg |
5867 | 944bf548 | Iustin Pop | if msg:
|
5868 | 944bf548 | Iustin Pop | self.LogWarning("Error while importing the disk images for instance" |
5869 | 944bf548 | Iustin Pop | " %s on node %s: %s" % (instance, pnode_name, msg))
|
5870 | a8083063 | Iustin Pop | else:
|
5871 | a8083063 | Iustin Pop | # also checked in the prereq part
|
5872 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Unknown OS initialization mode '%s'" |
5873 | 3ecf6786 | Iustin Pop | % self.op.mode)
|
5874 | a8083063 | Iustin Pop | |
5875 | a8083063 | Iustin Pop | if self.op.start: |
5876 | 4978db17 | Iustin Pop | iobj.admin_up = True
|
5877 | 4978db17 | Iustin Pop | self.cfg.Update(iobj)
|
5878 | 9a4f63d1 | Iustin Pop | logging.info("Starting instance %s on node %s", instance, pnode_name)
|
5879 | a8083063 | Iustin Pop | feedback_fn("* starting instance...")
|
5880 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(pnode_name, iobj, None, None) |
5881 | 4c4e4e1e | Iustin Pop | result.Raise("Could not start instance")
|
5882 | a8083063 | Iustin Pop | |
5883 | 08896026 | Iustin Pop | return list(iobj.all_nodes) |
5884 | 08896026 | Iustin Pop | |
5885 | a8083063 | Iustin Pop | |
5886 | a8083063 | Iustin Pop | class LUConnectConsole(NoHooksLU): |
5887 | a8083063 | Iustin Pop | """Connect to an instance's console.
|
5888 | a8083063 | Iustin Pop |
|
5889 | a8083063 | Iustin Pop | This is somewhat special in that it returns the command line that
|
5890 | a8083063 | Iustin Pop | you need to run on the master node in order to connect to the
|
5891 | a8083063 | Iustin Pop | console.
|
5892 | a8083063 | Iustin Pop |
|
5893 | a8083063 | Iustin Pop | """
|
5894 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
5895 | 8659b73e | Guido Trotter | REQ_BGL = False
|
5896 | 8659b73e | Guido Trotter | |
5897 | 8659b73e | Guido Trotter | def ExpandNames(self): |
5898 | 8659b73e | Guido Trotter | self._ExpandAndLockInstance()
|
5899 | a8083063 | Iustin Pop | |
5900 | a8083063 | Iustin Pop | def CheckPrereq(self): |
5901 | a8083063 | Iustin Pop | """Check prerequisites.
|
5902 | a8083063 | Iustin Pop |
|
5903 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
5904 | a8083063 | Iustin Pop |
|
5905 | a8083063 | Iustin Pop | """
|
5906 | 8659b73e | Guido Trotter | self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
5907 | 8659b73e | Guido Trotter | assert self.instance is not None, \ |
5908 | 8659b73e | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
5909 | 513e896d | Guido Trotter | _CheckNodeOnline(self, self.instance.primary_node) |
5910 | a8083063 | Iustin Pop | |
5911 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
5912 | a8083063 | Iustin Pop | """Connect to the console of an instance
|
5913 | a8083063 | Iustin Pop |
|
5914 | a8083063 | Iustin Pop | """
|
5915 | a8083063 | Iustin Pop | instance = self.instance
|
5916 | a8083063 | Iustin Pop | node = instance.primary_node |
5917 | a8083063 | Iustin Pop | |
5918 | 72737a7f | Iustin Pop | node_insts = self.rpc.call_instance_list([node],
|
5919 | 72737a7f | Iustin Pop | [instance.hypervisor])[node] |
5920 | 4c4e4e1e | Iustin Pop | node_insts.Raise("Can't get node information from %s" % node)
|
5921 | a8083063 | Iustin Pop | |
5922 | aca13712 | Iustin Pop | if instance.name not in node_insts.payload: |
5923 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Instance %s is not running." % instance.name) |
5924 | a8083063 | Iustin Pop | |
5925 | 9a4f63d1 | Iustin Pop | logging.debug("Connecting to console of %s on %s", instance.name, node)
|
5926 | a8083063 | Iustin Pop | |
5927 | e69d05fd | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
5928 | 5431b2e4 | Guido Trotter | cluster = self.cfg.GetClusterInfo()
|
5929 | 5431b2e4 | Guido Trotter | # beparams and hvparams are passed separately, to avoid editing the
|
5930 | 5431b2e4 | Guido Trotter | # instance and then saving the defaults in the instance itself.
|
5931 | 5431b2e4 | Guido Trotter | hvparams = cluster.FillHV(instance) |
5932 | 5431b2e4 | Guido Trotter | beparams = cluster.FillBE(instance) |
5933 | 5431b2e4 | Guido Trotter | console_cmd = hyper.GetShellCommandForConsole(instance, hvparams, beparams) |
5934 | b047857b | Michael Hanselmann | |
5935 | 82122173 | Iustin Pop | # build ssh cmdline
|
5936 | 0a80a26f | Michael Hanselmann | return self.ssh.BuildCmd(node, "root", console_cmd, batch=True, tty=True) |
5937 | a8083063 | Iustin Pop | |
5938 | a8083063 | Iustin Pop | |
5939 | a8083063 | Iustin Pop | class LUReplaceDisks(LogicalUnit): |
5940 | a8083063 | Iustin Pop | """Replace the disks of an instance.
|
5941 | a8083063 | Iustin Pop |
|
5942 | a8083063 | Iustin Pop | """
|
5943 | a8083063 | Iustin Pop | HPATH = "mirrors-replace"
|
5944 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
5945 | a9e0c397 | Iustin Pop | _OP_REQP = ["instance_name", "mode", "disks"] |
5946 | efd990e4 | Guido Trotter | REQ_BGL = False
|
5947 | efd990e4 | Guido Trotter | |
5948 | 7e9366f7 | Iustin Pop | def CheckArguments(self): |
5949 | efd990e4 | Guido Trotter | if not hasattr(self.op, "remote_node"): |
5950 | efd990e4 | Guido Trotter | self.op.remote_node = None |
5951 | 7e9366f7 | Iustin Pop | if not hasattr(self.op, "iallocator"): |
5952 | 7e9366f7 | Iustin Pop | self.op.iallocator = None |
5953 | 7e9366f7 | Iustin Pop | |
5954 | c68174b6 | Michael Hanselmann | TLReplaceDisks.CheckArguments(self.op.mode, self.op.remote_node, |
5955 | c68174b6 | Michael Hanselmann | self.op.iallocator)
|
5956 | 7e9366f7 | Iustin Pop | |
5957 | 7e9366f7 | Iustin Pop | def ExpandNames(self): |
5958 | 7e9366f7 | Iustin Pop | self._ExpandAndLockInstance()
|
5959 | 7e9366f7 | Iustin Pop | |
5960 | 7e9366f7 | Iustin Pop | if self.op.iallocator is not None: |
5961 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
5962 | 2bb5c911 | Michael Hanselmann | |
5963 | efd990e4 | Guido Trotter | elif self.op.remote_node is not None: |
5964 | efd990e4 | Guido Trotter | remote_node = self.cfg.ExpandNodeName(self.op.remote_node) |
5965 | efd990e4 | Guido Trotter | if remote_node is None: |
5966 | efd990e4 | Guido Trotter | raise errors.OpPrereqError("Node '%s' not known" % |
5967 | efd990e4 | Guido Trotter | self.op.remote_node)
|
5968 | 2bb5c911 | Michael Hanselmann | |
5969 | efd990e4 | Guido Trotter | self.op.remote_node = remote_node
|
5970 | 2bb5c911 | Michael Hanselmann | |
5971 | 3b559640 | Iustin Pop | # Warning: do not remove the locking of the new secondary here
|
5972 | 3b559640 | Iustin Pop | # unless DRBD8.AddChildren is changed to work in parallel;
|
5973 | 3b559640 | Iustin Pop | # currently it doesn't since parallel invocations of
|
5974 | 3b559640 | Iustin Pop | # FindUnusedMinor will conflict
|
5975 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = [remote_node]
|
5976 | efd990e4 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
5977 | 2bb5c911 | Michael Hanselmann | |
5978 | efd990e4 | Guido Trotter | else:
|
5979 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
5980 | efd990e4 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
5981 | efd990e4 | Guido Trotter | |
5982 | c68174b6 | Michael Hanselmann | self.replacer = TLReplaceDisks(self, self.op.instance_name, self.op.mode, |
5983 | c68174b6 | Michael Hanselmann | self.op.iallocator, self.op.remote_node, |
5984 | c68174b6 | Michael Hanselmann | self.op.disks)
|
5985 | c68174b6 | Michael Hanselmann | |
5986 | 3a012b41 | Michael Hanselmann | self.tasklets = [self.replacer] |
5987 | 2bb5c911 | Michael Hanselmann | |
5988 | efd990e4 | Guido Trotter | def DeclareLocks(self, level): |
5989 | efd990e4 | Guido Trotter | # If we're not already locking all nodes in the set we have to declare the
|
5990 | efd990e4 | Guido Trotter | # instance's primary/secondary nodes.
|
5991 | efd990e4 | Guido Trotter | if (level == locking.LEVEL_NODE and |
5992 | efd990e4 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET): |
5993 | efd990e4 | Guido Trotter | self._LockInstancesNodes()
|
5994 | a8083063 | Iustin Pop | |
5995 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
5996 | a8083063 | Iustin Pop | """Build hooks env.
|
5997 | a8083063 | Iustin Pop |
|
5998 | a8083063 | Iustin Pop | This runs on the master, the primary and all the secondaries.
|
5999 | a8083063 | Iustin Pop |
|
6000 | a8083063 | Iustin Pop | """
|
6001 | 2bb5c911 | Michael Hanselmann | instance = self.replacer.instance
|
6002 | a8083063 | Iustin Pop | env = { |
6003 | a9e0c397 | Iustin Pop | "MODE": self.op.mode, |
6004 | a8083063 | Iustin Pop | "NEW_SECONDARY": self.op.remote_node, |
6005 | 2bb5c911 | Michael Hanselmann | "OLD_SECONDARY": instance.secondary_nodes[0], |
6006 | a8083063 | Iustin Pop | } |
6007 | 2bb5c911 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self, instance))
|
6008 | 0834c866 | Iustin Pop | nl = [ |
6009 | d6a02168 | Michael Hanselmann | self.cfg.GetMasterNode(),
|
6010 | 2bb5c911 | Michael Hanselmann | instance.primary_node, |
6011 | 0834c866 | Iustin Pop | ] |
6012 | 0834c866 | Iustin Pop | if self.op.remote_node is not None: |
6013 | 0834c866 | Iustin Pop | nl.append(self.op.remote_node)
|
6014 | a8083063 | Iustin Pop | return env, nl, nl
|
6015 | a8083063 | Iustin Pop | |
6016 | 2bb5c911 | Michael Hanselmann | |
6017 | 7ffc5a86 | Michael Hanselmann | class LUEvacuateNode(LogicalUnit): |
6018 | 7ffc5a86 | Michael Hanselmann | """Relocate the secondary instances from a node.
|
6019 | 7ffc5a86 | Michael Hanselmann |
|
6020 | 7ffc5a86 | Michael Hanselmann | """
|
6021 | 7ffc5a86 | Michael Hanselmann | HPATH = "node-evacuate"
|
6022 | 7ffc5a86 | Michael Hanselmann | HTYPE = constants.HTYPE_NODE |
6023 | 7ffc5a86 | Michael Hanselmann | _OP_REQP = ["node_name"]
|
6024 | 7ffc5a86 | Michael Hanselmann | REQ_BGL = False
|
6025 | 7ffc5a86 | Michael Hanselmann | |
6026 | 7ffc5a86 | Michael Hanselmann | def CheckArguments(self): |
6027 | 7ffc5a86 | Michael Hanselmann | if not hasattr(self.op, "remote_node"): |
6028 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node = None |
6029 | 7ffc5a86 | Michael Hanselmann | if not hasattr(self.op, "iallocator"): |
6030 | 7ffc5a86 | Michael Hanselmann | self.op.iallocator = None |
6031 | 7ffc5a86 | Michael Hanselmann | |
6032 | 7ffc5a86 | Michael Hanselmann | TLReplaceDisks.CheckArguments(constants.REPLACE_DISK_CHG, |
6033 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node,
|
6034 | 7ffc5a86 | Michael Hanselmann | self.op.iallocator)
|
6035 | 7ffc5a86 | Michael Hanselmann | |
6036 | 7ffc5a86 | Michael Hanselmann | def ExpandNames(self): |
6037 | 7ffc5a86 | Michael Hanselmann | self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name) |
6038 | 7ffc5a86 | Michael Hanselmann | if self.op.node_name is None: |
6039 | 7ffc5a86 | Michael Hanselmann | raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name) |
6040 | 7ffc5a86 | Michael Hanselmann | |
6041 | 7ffc5a86 | Michael Hanselmann | self.needed_locks = {}
|
6042 | 7ffc5a86 | Michael Hanselmann | |
6043 | 7ffc5a86 | Michael Hanselmann | # Declare node locks
|
6044 | 7ffc5a86 | Michael Hanselmann | if self.op.iallocator is not None: |
6045 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
6046 | 7ffc5a86 | Michael Hanselmann | |
6047 | 7ffc5a86 | Michael Hanselmann | elif self.op.remote_node is not None: |
6048 | 7ffc5a86 | Michael Hanselmann | remote_node = self.cfg.ExpandNodeName(self.op.remote_node) |
6049 | 7ffc5a86 | Michael Hanselmann | if remote_node is None: |
6050 | 7ffc5a86 | Michael Hanselmann | raise errors.OpPrereqError("Node '%s' not known" % |
6051 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node)
|
6052 | 7ffc5a86 | Michael Hanselmann | |
6053 | 7ffc5a86 | Michael Hanselmann | self.op.remote_node = remote_node
|
6054 | 7ffc5a86 | Michael Hanselmann | |
6055 | 7ffc5a86 | Michael Hanselmann | # Warning: do not remove the locking of the new secondary here
|
6056 | 7ffc5a86 | Michael Hanselmann | # unless DRBD8.AddChildren is changed to work in parallel;
|
6057 | 7ffc5a86 | Michael Hanselmann | # currently it doesn't since parallel invocations of
|
6058 | 7ffc5a86 | Michael Hanselmann | # FindUnusedMinor will conflict
|
6059 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] = [remote_node]
|
6060 | 7ffc5a86 | Michael Hanselmann | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
6061 | 7ffc5a86 | Michael Hanselmann | |
6062 | 7ffc5a86 | Michael Hanselmann | else:
|
6063 | 7ffc5a86 | Michael Hanselmann | raise errors.OpPrereqError("Invalid parameters") |
6064 | 7ffc5a86 | Michael Hanselmann | |
6065 | 7ffc5a86 | Michael Hanselmann | # Create tasklets for replacing disks for all secondary instances on this
|
6066 | 7ffc5a86 | Michael Hanselmann | # node
|
6067 | 7ffc5a86 | Michael Hanselmann | names = [] |
6068 | 3a012b41 | Michael Hanselmann | tasklets = [] |
6069 | 7ffc5a86 | Michael Hanselmann | |
6070 | 7ffc5a86 | Michael Hanselmann | for inst in _GetNodeSecondaryInstances(self.cfg, self.op.node_name): |
6071 | 7ffc5a86 | Michael Hanselmann | logging.debug("Replacing disks for instance %s", inst.name)
|
6072 | 7ffc5a86 | Michael Hanselmann | names.append(inst.name) |
6073 | 7ffc5a86 | Michael Hanselmann | |
6074 | 7ffc5a86 | Michael Hanselmann | replacer = TLReplaceDisks(self, inst.name, constants.REPLACE_DISK_CHG,
|
6075 | 7ffc5a86 | Michael Hanselmann | self.op.iallocator, self.op.remote_node, []) |
6076 | 3a012b41 | Michael Hanselmann | tasklets.append(replacer) |
6077 | 7ffc5a86 | Michael Hanselmann | |
6078 | 3a012b41 | Michael Hanselmann | self.tasklets = tasklets
|
6079 | 7ffc5a86 | Michael Hanselmann | self.instance_names = names
|
6080 | 7ffc5a86 | Michael Hanselmann | |
6081 | 7ffc5a86 | Michael Hanselmann | # Declare instance locks
|
6082 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_INSTANCE] = self.instance_names |
6083 | 7ffc5a86 | Michael Hanselmann | |
6084 | 7ffc5a86 | Michael Hanselmann | def DeclareLocks(self, level): |
6085 | 7ffc5a86 | Michael Hanselmann | # If we're not already locking all nodes in the set we have to declare the
|
6086 | 7ffc5a86 | Michael Hanselmann | # instance's primary/secondary nodes.
|
6087 | 7ffc5a86 | Michael Hanselmann | if (level == locking.LEVEL_NODE and |
6088 | 7ffc5a86 | Michael Hanselmann | self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET): |
6089 | 7ffc5a86 | Michael Hanselmann | self._LockInstancesNodes()
|
6090 | 7ffc5a86 | Michael Hanselmann | |
6091 | 7ffc5a86 | Michael Hanselmann | def BuildHooksEnv(self): |
6092 | 7ffc5a86 | Michael Hanselmann | """Build hooks env.
|
6093 | 7ffc5a86 | Michael Hanselmann |
|
6094 | 7ffc5a86 | Michael Hanselmann | This runs on the master, the primary and all the secondaries.
|
6095 | 7ffc5a86 | Michael Hanselmann |
|
6096 | 7ffc5a86 | Michael Hanselmann | """
|
6097 | 7ffc5a86 | Michael Hanselmann | env = { |
6098 | 7ffc5a86 | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
6099 | 7ffc5a86 | Michael Hanselmann | } |
6100 | 7ffc5a86 | Michael Hanselmann | |
6101 | 7ffc5a86 | Michael Hanselmann | nl = [self.cfg.GetMasterNode()]
|
6102 | 7ffc5a86 | Michael Hanselmann | |
6103 | 7ffc5a86 | Michael Hanselmann | if self.op.remote_node is not None: |
6104 | 7ffc5a86 | Michael Hanselmann | env["NEW_SECONDARY"] = self.op.remote_node |
6105 | 7ffc5a86 | Michael Hanselmann | nl.append(self.op.remote_node)
|
6106 | 7ffc5a86 | Michael Hanselmann | |
6107 | 7ffc5a86 | Michael Hanselmann | return (env, nl, nl)
|
6108 | 7ffc5a86 | Michael Hanselmann | |
6109 | 7ffc5a86 | Michael Hanselmann | |
6110 | c68174b6 | Michael Hanselmann | class TLReplaceDisks(Tasklet): |
6111 | 2bb5c911 | Michael Hanselmann | """Replaces disks for an instance.
|
6112 | 2bb5c911 | Michael Hanselmann |
|
6113 | 2bb5c911 | Michael Hanselmann | Note: Locking is not within the scope of this class.
|
6114 | 2bb5c911 | Michael Hanselmann |
|
6115 | 2bb5c911 | Michael Hanselmann | """
|
6116 | 2bb5c911 | Michael Hanselmann | def __init__(self, lu, instance_name, mode, iallocator_name, remote_node, |
6117 | 2bb5c911 | Michael Hanselmann | disks): |
6118 | 2bb5c911 | Michael Hanselmann | """Initializes this class.
|
6119 | 2bb5c911 | Michael Hanselmann |
|
6120 | 2bb5c911 | Michael Hanselmann | """
|
6121 | 464243a7 | Michael Hanselmann | Tasklet.__init__(self, lu)
|
6122 | 464243a7 | Michael Hanselmann | |
6123 | 2bb5c911 | Michael Hanselmann | # Parameters
|
6124 | 2bb5c911 | Michael Hanselmann | self.instance_name = instance_name
|
6125 | 2bb5c911 | Michael Hanselmann | self.mode = mode
|
6126 | 2bb5c911 | Michael Hanselmann | self.iallocator_name = iallocator_name
|
6127 | 2bb5c911 | Michael Hanselmann | self.remote_node = remote_node
|
6128 | 2bb5c911 | Michael Hanselmann | self.disks = disks
|
6129 | 2bb5c911 | Michael Hanselmann | |
6130 | 2bb5c911 | Michael Hanselmann | # Runtime data
|
6131 | 2bb5c911 | Michael Hanselmann | self.instance = None |
6132 | 2bb5c911 | Michael Hanselmann | self.new_node = None |
6133 | 2bb5c911 | Michael Hanselmann | self.target_node = None |
6134 | 2bb5c911 | Michael Hanselmann | self.other_node = None |
6135 | 2bb5c911 | Michael Hanselmann | self.remote_node_info = None |
6136 | 2bb5c911 | Michael Hanselmann | self.node_secondary_ip = None |
6137 | 2bb5c911 | Michael Hanselmann | |
6138 | 2bb5c911 | Michael Hanselmann | @staticmethod
|
6139 | 2bb5c911 | Michael Hanselmann | def CheckArguments(mode, remote_node, iallocator): |
6140 | c68174b6 | Michael Hanselmann | """Helper function for users of this class.
|
6141 | c68174b6 | Michael Hanselmann |
|
6142 | c68174b6 | Michael Hanselmann | """
|
6143 | 2bb5c911 | Michael Hanselmann | # check for valid parameter combination
|
6144 | 2bb5c911 | Michael Hanselmann | if mode == constants.REPLACE_DISK_CHG:
|
6145 | 02a00186 | Michael Hanselmann | if remote_node is None and iallocator is None: |
6146 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("When changing the secondary either an" |
6147 | 2bb5c911 | Michael Hanselmann | " iallocator script must be used or the"
|
6148 | 2bb5c911 | Michael Hanselmann | " new node given")
|
6149 | 02a00186 | Michael Hanselmann | |
6150 | 02a00186 | Michael Hanselmann | if remote_node is not None and iallocator is not None: |
6151 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("Give either the iallocator or the new" |
6152 | 2bb5c911 | Michael Hanselmann | " secondary, not both")
|
6153 | 02a00186 | Michael Hanselmann | |
6154 | 02a00186 | Michael Hanselmann | elif remote_node is not None or iallocator is not None: |
6155 | 02a00186 | Michael Hanselmann | # Not replacing the secondary
|
6156 | 02a00186 | Michael Hanselmann | raise errors.OpPrereqError("The iallocator and new node options can" |
6157 | 02a00186 | Michael Hanselmann | " only be used when changing the"
|
6158 | 02a00186 | Michael Hanselmann | " secondary node")
|
6159 | 2bb5c911 | Michael Hanselmann | |
6160 | 2bb5c911 | Michael Hanselmann | @staticmethod
|
6161 | 2bb5c911 | Michael Hanselmann | def _RunAllocator(lu, iallocator_name, instance_name, relocate_from): |
6162 | 2bb5c911 | Michael Hanselmann | """Compute a new secondary node using an IAllocator.
|
6163 | 2bb5c911 | Michael Hanselmann |
|
6164 | 2bb5c911 | Michael Hanselmann | """
|
6165 | 2bb5c911 | Michael Hanselmann | ial = IAllocator(lu.cfg, lu.rpc, |
6166 | 2bb5c911 | Michael Hanselmann | mode=constants.IALLOCATOR_MODE_RELOC, |
6167 | 2bb5c911 | Michael Hanselmann | name=instance_name, |
6168 | 2bb5c911 | Michael Hanselmann | relocate_from=relocate_from) |
6169 | 2bb5c911 | Michael Hanselmann | |
6170 | 2bb5c911 | Michael Hanselmann | ial.Run(iallocator_name) |
6171 | 2bb5c911 | Michael Hanselmann | |
6172 | 2bb5c911 | Michael Hanselmann | if not ial.success: |
6173 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':" |
6174 | 2bb5c911 | Michael Hanselmann | " %s" % (iallocator_name, ial.info))
|
6175 | 2bb5c911 | Michael Hanselmann | |
6176 | 2bb5c911 | Michael Hanselmann | if len(ial.nodes) != ial.required_nodes: |
6177 | 2bb5c911 | Michael Hanselmann | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
6178 | 2bb5c911 | Michael Hanselmann | " of nodes (%s), required %s" %
|
6179 | 2bb5c911 | Michael Hanselmann | (len(ial.nodes), ial.required_nodes))
|
6180 | 2bb5c911 | Michael Hanselmann | |
6181 | 2bb5c911 | Michael Hanselmann | remote_node_name = ial.nodes[0]
|
6182 | 2bb5c911 | Michael Hanselmann | |
6183 | 2bb5c911 | Michael Hanselmann | lu.LogInfo("Selected new secondary for instance '%s': %s",
|
6184 | 2bb5c911 | Michael Hanselmann | instance_name, remote_node_name) |
6185 | 2bb5c911 | Michael Hanselmann | |
6186 | 2bb5c911 | Michael Hanselmann | return remote_node_name
|
6187 | 2bb5c911 | Michael Hanselmann | |
6188 | 942be002 | Michael Hanselmann | def _FindFaultyDisks(self, node_name): |
6189 | 2d9005d8 | Michael Hanselmann | return _FindFaultyInstanceDisks(self.cfg, self.rpc, self.instance, |
6190 | 2d9005d8 | Michael Hanselmann | node_name, True)
|
6191 | 942be002 | Michael Hanselmann | |
6192 | 2bb5c911 | Michael Hanselmann | def CheckPrereq(self): |
6193 | 2bb5c911 | Michael Hanselmann | """Check prerequisites.
|
6194 | 2bb5c911 | Michael Hanselmann |
|
6195 | 2bb5c911 | Michael Hanselmann | This checks that the instance is in the cluster.
|
6196 | 2bb5c911 | Michael Hanselmann |
|
6197 | 2bb5c911 | Michael Hanselmann | """
|
6198 | 2bb5c911 | Michael Hanselmann | self.instance = self.cfg.GetInstanceInfo(self.instance_name) |
6199 | 2bb5c911 | Michael Hanselmann | assert self.instance is not None, \ |
6200 | 2bb5c911 | Michael Hanselmann | "Cannot retrieve locked instance %s" % self.instance_name |
6201 | 2bb5c911 | Michael Hanselmann | |
6202 | 2bb5c911 | Michael Hanselmann | if self.instance.disk_template != constants.DT_DRBD8: |
6203 | 7e9366f7 | Iustin Pop | raise errors.OpPrereqError("Can only run replace disks for DRBD8-based" |
6204 | 7e9366f7 | Iustin Pop | " instances")
|
6205 | a8083063 | Iustin Pop | |
6206 | 2bb5c911 | Michael Hanselmann | if len(self.instance.secondary_nodes) != 1: |
6207 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The instance has a strange layout," |
6208 | 3ecf6786 | Iustin Pop | " expected one secondary but found %d" %
|
6209 | 2bb5c911 | Michael Hanselmann | len(self.instance.secondary_nodes)) |
6210 | a8083063 | Iustin Pop | |
6211 | 2bb5c911 | Michael Hanselmann | secondary_node = self.instance.secondary_nodes[0] |
6212 | a9e0c397 | Iustin Pop | |
6213 | 2bb5c911 | Michael Hanselmann | if self.iallocator_name is None: |
6214 | 2bb5c911 | Michael Hanselmann | remote_node = self.remote_node
|
6215 | 2bb5c911 | Michael Hanselmann | else:
|
6216 | 2bb5c911 | Michael Hanselmann | remote_node = self._RunAllocator(self.lu, self.iallocator_name, |
6217 | 2bb5c911 | Michael Hanselmann | self.instance.name, secondary_node)
|
6218 | b6e82a65 | Iustin Pop | |
6219 | a9e0c397 | Iustin Pop | if remote_node is not None: |
6220 | a9e0c397 | Iustin Pop | self.remote_node_info = self.cfg.GetNodeInfo(remote_node) |
6221 | efd990e4 | Guido Trotter | assert self.remote_node_info is not None, \ |
6222 | efd990e4 | Guido Trotter | "Cannot retrieve locked node %s" % remote_node
|
6223 | a9e0c397 | Iustin Pop | else:
|
6224 | a9e0c397 | Iustin Pop | self.remote_node_info = None |
6225 | 2bb5c911 | Michael Hanselmann | |
6226 | 2bb5c911 | Michael Hanselmann | if remote_node == self.instance.primary_node: |
6227 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The specified node is the primary node of" |
6228 | 3ecf6786 | Iustin Pop | " the instance.")
|
6229 | 2bb5c911 | Michael Hanselmann | |
6230 | 2bb5c911 | Michael Hanselmann | if remote_node == secondary_node:
|
6231 | 7e9366f7 | Iustin Pop | raise errors.OpPrereqError("The specified node is already the" |
6232 | 7e9366f7 | Iustin Pop | " secondary node of the instance.")
|
6233 | 7e9366f7 | Iustin Pop | |
6234 | 2945fd2d | Michael Hanselmann | if self.disks and self.mode in (constants.REPLACE_DISK_AUTO, |
6235 | 2945fd2d | Michael Hanselmann | constants.REPLACE_DISK_CHG): |
6236 | 2945fd2d | Michael Hanselmann | raise errors.OpPrereqError("Cannot specify disks to be replaced") |
6237 | 942be002 | Michael Hanselmann | |
6238 | 2945fd2d | Michael Hanselmann | if self.mode == constants.REPLACE_DISK_AUTO: |
6239 | 942be002 | Michael Hanselmann | faulty_primary = self._FindFaultyDisks(self.instance.primary_node) |
6240 | 942be002 | Michael Hanselmann | faulty_secondary = self._FindFaultyDisks(secondary_node)
|
6241 | 942be002 | Michael Hanselmann | |
6242 | 942be002 | Michael Hanselmann | if faulty_primary and faulty_secondary: |
6243 | 942be002 | Michael Hanselmann | raise errors.OpPrereqError("Instance %s has faulty disks on more than" |
6244 | 942be002 | Michael Hanselmann | " one node and can not be repaired"
|
6245 | 942be002 | Michael Hanselmann | " automatically" % self.instance_name) |
6246 | 942be002 | Michael Hanselmann | |
6247 | 942be002 | Michael Hanselmann | if faulty_primary:
|
6248 | 942be002 | Michael Hanselmann | self.disks = faulty_primary
|
6249 | 942be002 | Michael Hanselmann | self.target_node = self.instance.primary_node |
6250 | 942be002 | Michael Hanselmann | self.other_node = secondary_node
|
6251 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
6252 | 942be002 | Michael Hanselmann | elif faulty_secondary:
|
6253 | 942be002 | Michael Hanselmann | self.disks = faulty_secondary
|
6254 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
6255 | 942be002 | Michael Hanselmann | self.other_node = self.instance.primary_node |
6256 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
6257 | 942be002 | Michael Hanselmann | else:
|
6258 | 942be002 | Michael Hanselmann | self.disks = []
|
6259 | 942be002 | Michael Hanselmann | check_nodes = [] |
6260 | 942be002 | Michael Hanselmann | |
6261 | 942be002 | Michael Hanselmann | else:
|
6262 | 942be002 | Michael Hanselmann | # Non-automatic modes
|
6263 | 942be002 | Michael Hanselmann | if self.mode == constants.REPLACE_DISK_PRI: |
6264 | 942be002 | Michael Hanselmann | self.target_node = self.instance.primary_node |
6265 | 942be002 | Michael Hanselmann | self.other_node = secondary_node
|
6266 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
6267 | 7e9366f7 | Iustin Pop | |
6268 | 942be002 | Michael Hanselmann | elif self.mode == constants.REPLACE_DISK_SEC: |
6269 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
6270 | 942be002 | Michael Hanselmann | self.other_node = self.instance.primary_node |
6271 | 942be002 | Michael Hanselmann | check_nodes = [self.target_node, self.other_node] |
6272 | a9e0c397 | Iustin Pop | |
6273 | 942be002 | Michael Hanselmann | elif self.mode == constants.REPLACE_DISK_CHG: |
6274 | 942be002 | Michael Hanselmann | self.new_node = remote_node
|
6275 | 942be002 | Michael Hanselmann | self.other_node = self.instance.primary_node |
6276 | 942be002 | Michael Hanselmann | self.target_node = secondary_node
|
6277 | 942be002 | Michael Hanselmann | check_nodes = [self.new_node, self.other_node] |
6278 | 54155f52 | Iustin Pop | |
6279 | 942be002 | Michael Hanselmann | _CheckNodeNotDrained(self.lu, remote_node)
|
6280 | a8083063 | Iustin Pop | |
6281 | 942be002 | Michael Hanselmann | else:
|
6282 | 942be002 | Michael Hanselmann | raise errors.ProgrammerError("Unhandled disk replace mode (%s)" % |
6283 | 942be002 | Michael Hanselmann | self.mode)
|
6284 | 942be002 | Michael Hanselmann | |
6285 | 942be002 | Michael Hanselmann | # If not specified all disks should be replaced
|
6286 | 942be002 | Michael Hanselmann | if not self.disks: |
6287 | 942be002 | Michael Hanselmann | self.disks = range(len(self.instance.disks)) |
6288 | a9e0c397 | Iustin Pop | |
6289 | 2bb5c911 | Michael Hanselmann | for node in check_nodes: |
6290 | 2bb5c911 | Michael Hanselmann | _CheckNodeOnline(self.lu, node)
|
6291 | e4376078 | Iustin Pop | |
6292 | 2bb5c911 | Michael Hanselmann | # Check whether disks are valid
|
6293 | 2bb5c911 | Michael Hanselmann | for disk_idx in self.disks: |
6294 | 2bb5c911 | Michael Hanselmann | self.instance.FindDisk(disk_idx)
|
6295 | e4376078 | Iustin Pop | |
6296 | 2bb5c911 | Michael Hanselmann | # Get secondary node IP addresses
|
6297 | 2bb5c911 | Michael Hanselmann | node_2nd_ip = {} |
6298 | e4376078 | Iustin Pop | |
6299 | 2bb5c911 | Michael Hanselmann | for node_name in [self.target_node, self.other_node, self.new_node]: |
6300 | 2bb5c911 | Michael Hanselmann | if node_name is not None: |
6301 | 2bb5c911 | Michael Hanselmann | node_2nd_ip[node_name] = self.cfg.GetNodeInfo(node_name).secondary_ip
|
6302 | e4376078 | Iustin Pop | |
6303 | 2bb5c911 | Michael Hanselmann | self.node_secondary_ip = node_2nd_ip
|
6304 | a9e0c397 | Iustin Pop | |
6305 | c68174b6 | Michael Hanselmann | def Exec(self, feedback_fn): |
6306 | 2bb5c911 | Michael Hanselmann | """Execute disk replacement.
|
6307 | 2bb5c911 | Michael Hanselmann |
|
6308 | 2bb5c911 | Michael Hanselmann | This dispatches the disk replacement to the appropriate handler.
|
6309 | cff90b79 | Iustin Pop |
|
6310 | a9e0c397 | Iustin Pop | """
|
6311 | 942be002 | Michael Hanselmann | if not self.disks: |
6312 | 942be002 | Michael Hanselmann | feedback_fn("No disks need replacement")
|
6313 | 942be002 | Michael Hanselmann | return
|
6314 | 942be002 | Michael Hanselmann | |
6315 | 942be002 | Michael Hanselmann | feedback_fn("Replacing disk(s) %s for %s" %
|
6316 | 942be002 | Michael Hanselmann | (", ".join([str(i) for i in self.disks]), self.instance.name)) |
6317 | 7ffc5a86 | Michael Hanselmann | |
6318 | 2bb5c911 | Michael Hanselmann | activate_disks = (not self.instance.admin_up) |
6319 | 2bb5c911 | Michael Hanselmann | |
6320 | 2bb5c911 | Michael Hanselmann | # Activate the instance disks if we're replacing them on a down instance
|
6321 | 2bb5c911 | Michael Hanselmann | if activate_disks:
|
6322 | 2bb5c911 | Michael Hanselmann | _StartInstanceDisks(self.lu, self.instance, True) |
6323 | 2bb5c911 | Michael Hanselmann | |
6324 | 2bb5c911 | Michael Hanselmann | try:
|
6325 | 942be002 | Michael Hanselmann | # Should we replace the secondary node?
|
6326 | 942be002 | Michael Hanselmann | if self.new_node is not None: |
6327 | 2bb5c911 | Michael Hanselmann | return self._ExecDrbd8Secondary() |
6328 | 2bb5c911 | Michael Hanselmann | else:
|
6329 | 2bb5c911 | Michael Hanselmann | return self._ExecDrbd8DiskOnly() |
6330 | 2bb5c911 | Michael Hanselmann | |
6331 | 2bb5c911 | Michael Hanselmann | finally:
|
6332 | 2bb5c911 | Michael Hanselmann | # Deactivate the instance disks if we're replacing them on a down instance
|
6333 | 2bb5c911 | Michael Hanselmann | if activate_disks:
|
6334 | 2bb5c911 | Michael Hanselmann | _SafeShutdownInstanceDisks(self.lu, self.instance) |
6335 | 2bb5c911 | Michael Hanselmann | |
6336 | 2bb5c911 | Michael Hanselmann | def _CheckVolumeGroup(self, nodes): |
6337 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking volume groups") |
6338 | 2bb5c911 | Michael Hanselmann | |
6339 | a9e0c397 | Iustin Pop | vgname = self.cfg.GetVGName()
|
6340 | cff90b79 | Iustin Pop | |
6341 | 2bb5c911 | Michael Hanselmann | # Make sure volume group exists on all involved nodes
|
6342 | 2bb5c911 | Michael Hanselmann | results = self.rpc.call_vg_list(nodes)
|
6343 | cff90b79 | Iustin Pop | if not results: |
6344 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't list volume groups on the nodes") |
6345 | 2bb5c911 | Michael Hanselmann | |
6346 | 2bb5c911 | Michael Hanselmann | for node in nodes: |
6347 | 781de953 | Iustin Pop | res = results[node] |
6348 | 4c4e4e1e | Iustin Pop | res.Raise("Error checking node %s" % node)
|
6349 | 2bb5c911 | Michael Hanselmann | if vgname not in res.payload: |
6350 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Volume group '%s' not found on node %s" % |
6351 | 2bb5c911 | Michael Hanselmann | (vgname, node)) |
6352 | 2bb5c911 | Michael Hanselmann | |
6353 | 2bb5c911 | Michael Hanselmann | def _CheckDisksExistence(self, nodes): |
6354 | 2bb5c911 | Michael Hanselmann | # Check disk existence
|
6355 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
6356 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
6357 | cff90b79 | Iustin Pop | continue
|
6358 | 2bb5c911 | Michael Hanselmann | |
6359 | 2bb5c911 | Michael Hanselmann | for node in nodes: |
6360 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking disk/%d on %s" % (idx, node)) |
6361 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node)
|
6362 | 2bb5c911 | Michael Hanselmann | |
6363 | 23829f6f | Iustin Pop | result = self.rpc.call_blockdev_find(node, dev)
|
6364 | 2bb5c911 | Michael Hanselmann | |
6365 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
6366 | 2bb5c911 | Michael Hanselmann | if msg or not result.payload: |
6367 | 2bb5c911 | Michael Hanselmann | if not msg: |
6368 | 2bb5c911 | Michael Hanselmann | msg = "disk not found"
|
6369 | 23829f6f | Iustin Pop | raise errors.OpExecError("Can't find disk/%d on node %s: %s" % |
6370 | 23829f6f | Iustin Pop | (idx, node, msg)) |
6371 | cff90b79 | Iustin Pop | |
6372 | 2bb5c911 | Michael Hanselmann | def _CheckDisksConsistency(self, node_name, on_primary, ldisk): |
6373 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
6374 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
6375 | cff90b79 | Iustin Pop | continue
|
6376 | cff90b79 | Iustin Pop | |
6377 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Checking disk/%d consistency on node %s" % |
6378 | 2bb5c911 | Michael Hanselmann | (idx, node_name)) |
6379 | 2bb5c911 | Michael Hanselmann | |
6380 | 2bb5c911 | Michael Hanselmann | if not _CheckDiskConsistency(self.lu, dev, node_name, on_primary, |
6381 | 2bb5c911 | Michael Hanselmann | ldisk=ldisk): |
6382 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Node %s has degraded storage, unsafe to" |
6383 | 2bb5c911 | Michael Hanselmann | " replace disks for instance %s" %
|
6384 | 2bb5c911 | Michael Hanselmann | (node_name, self.instance.name))
|
6385 | 2bb5c911 | Michael Hanselmann | |
6386 | 2bb5c911 | Michael Hanselmann | def _CreateNewStorage(self, node_name): |
6387 | 2bb5c911 | Michael Hanselmann | vgname = self.cfg.GetVGName()
|
6388 | 2bb5c911 | Michael Hanselmann | iv_names = {} |
6389 | 2bb5c911 | Michael Hanselmann | |
6390 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
6391 | 2bb5c911 | Michael Hanselmann | if idx not in self.disks: |
6392 | a9e0c397 | Iustin Pop | continue
|
6393 | 2bb5c911 | Michael Hanselmann | |
6394 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding storage on %s for disk/%d" % (node_name, idx)) |
6395 | 2bb5c911 | Michael Hanselmann | |
6396 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node_name)
|
6397 | 2bb5c911 | Michael Hanselmann | |
6398 | 2bb5c911 | Michael Hanselmann | lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]] |
6399 | 2bb5c911 | Michael Hanselmann | names = _GenerateUniqueNames(self.lu, lv_names)
|
6400 | 2bb5c911 | Michael Hanselmann | |
6401 | 2bb5c911 | Michael Hanselmann | lv_data = objects.Disk(dev_type=constants.LD_LV, size=dev.size, |
6402 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[0]))
|
6403 | a9e0c397 | Iustin Pop | lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
6404 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[1]))
|
6405 | 2bb5c911 | Michael Hanselmann | |
6406 | a9e0c397 | Iustin Pop | new_lvs = [lv_data, lv_meta] |
6407 | a9e0c397 | Iustin Pop | old_lvs = dev.children |
6408 | a9e0c397 | Iustin Pop | iv_names[dev.iv_name] = (dev, old_lvs, new_lvs) |
6409 | 2bb5c911 | Michael Hanselmann | |
6410 | 428958aa | Iustin Pop | # we pass force_create=True to force the LVM creation
|
6411 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
6412 | 2bb5c911 | Michael Hanselmann | _CreateBlockDev(self.lu, node_name, self.instance, new_lv, True, |
6413 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
6414 | 2bb5c911 | Michael Hanselmann | |
6415 | 2bb5c911 | Michael Hanselmann | return iv_names
|
6416 | 2bb5c911 | Michael Hanselmann | |
6417 | 2bb5c911 | Michael Hanselmann | def _CheckDevices(self, node_name, iv_names): |
6418 | 2bb5c911 | Michael Hanselmann | for name, (dev, old_lvs, new_lvs) in iv_names.iteritems(): |
6419 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, node_name)
|
6420 | 2bb5c911 | Michael Hanselmann | |
6421 | 2bb5c911 | Michael Hanselmann | result = self.rpc.call_blockdev_find(node_name, dev)
|
6422 | 2bb5c911 | Michael Hanselmann | |
6423 | 2bb5c911 | Michael Hanselmann | msg = result.fail_msg |
6424 | 2bb5c911 | Michael Hanselmann | if msg or not result.payload: |
6425 | 2bb5c911 | Michael Hanselmann | if not msg: |
6426 | 2bb5c911 | Michael Hanselmann | msg = "disk not found"
|
6427 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("Can't find DRBD device %s: %s" % |
6428 | 2bb5c911 | Michael Hanselmann | (name, msg)) |
6429 | 2bb5c911 | Michael Hanselmann | |
6430 | 96acbc09 | Michael Hanselmann | if result.payload.is_degraded:
|
6431 | 2bb5c911 | Michael Hanselmann | raise errors.OpExecError("DRBD device %s is degraded!" % name) |
6432 | 2bb5c911 | Michael Hanselmann | |
6433 | 2bb5c911 | Michael Hanselmann | def _RemoveOldStorage(self, node_name, iv_names): |
6434 | 2bb5c911 | Michael Hanselmann | for name, (dev, old_lvs, _) in iv_names.iteritems(): |
6435 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Remove logical volumes for %s" % name) |
6436 | 2bb5c911 | Michael Hanselmann | |
6437 | 2bb5c911 | Michael Hanselmann | for lv in old_lvs: |
6438 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(lv, node_name)
|
6439 | 2bb5c911 | Michael Hanselmann | |
6440 | 2bb5c911 | Michael Hanselmann | msg = self.rpc.call_blockdev_remove(node_name, lv).fail_msg
|
6441 | 2bb5c911 | Michael Hanselmann | if msg:
|
6442 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Can't remove old LV: %s" % msg, |
6443 | 2bb5c911 | Michael Hanselmann | hint="remove unused LVs manually")
|
6444 | 2bb5c911 | Michael Hanselmann | |
6445 | 2bb5c911 | Michael Hanselmann | def _ExecDrbd8DiskOnly(self): |
6446 | 2bb5c911 | Michael Hanselmann | """Replace a disk on the primary or secondary for DRBD 8.
|
6447 | 2bb5c911 | Michael Hanselmann |
|
6448 | 2bb5c911 | Michael Hanselmann | The algorithm for replace is quite complicated:
|
6449 | 2bb5c911 | Michael Hanselmann |
|
6450 | 2bb5c911 | Michael Hanselmann | 1. for each disk to be replaced:
|
6451 | 2bb5c911 | Michael Hanselmann |
|
6452 | 2bb5c911 | Michael Hanselmann | 1. create new LVs on the target node with unique names
|
6453 | 2bb5c911 | Michael Hanselmann | 1. detach old LVs from the drbd device
|
6454 | 2bb5c911 | Michael Hanselmann | 1. rename old LVs to name_replaced.<time_t>
|
6455 | 2bb5c911 | Michael Hanselmann | 1. rename new LVs to old LVs
|
6456 | 2bb5c911 | Michael Hanselmann | 1. attach the new LVs (with the old names now) to the drbd device
|
6457 | 2bb5c911 | Michael Hanselmann |
|
6458 | 2bb5c911 | Michael Hanselmann | 1. wait for sync across all devices
|
6459 | 2bb5c911 | Michael Hanselmann |
|
6460 | 2bb5c911 | Michael Hanselmann | 1. for each modified disk:
|
6461 | 2bb5c911 | Michael Hanselmann |
|
6462 | 2bb5c911 | Michael Hanselmann | 1. remove old LVs (which have the name name_replaces.<time_t>)
|
6463 | 2bb5c911 | Michael Hanselmann |
|
6464 | 2bb5c911 | Michael Hanselmann | Failures are not very well handled.
|
6465 | 2bb5c911 | Michael Hanselmann |
|
6466 | 2bb5c911 | Michael Hanselmann | """
|
6467 | 2bb5c911 | Michael Hanselmann | steps_total = 6
|
6468 | 2bb5c911 | Michael Hanselmann | |
6469 | 2bb5c911 | Michael Hanselmann | # Step: check device activation
|
6470 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(1, steps_total, "Check device existence") |
6471 | 2bb5c911 | Michael Hanselmann | self._CheckDisksExistence([self.other_node, self.target_node]) |
6472 | 2bb5c911 | Michael Hanselmann | self._CheckVolumeGroup([self.target_node, self.other_node]) |
6473 | 2bb5c911 | Michael Hanselmann | |
6474 | 2bb5c911 | Michael Hanselmann | # Step: check other node consistency
|
6475 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(2, steps_total, "Check peer consistency") |
6476 | 2bb5c911 | Michael Hanselmann | self._CheckDisksConsistency(self.other_node, |
6477 | 2bb5c911 | Michael Hanselmann | self.other_node == self.instance.primary_node, |
6478 | 2bb5c911 | Michael Hanselmann | False)
|
6479 | 2bb5c911 | Michael Hanselmann | |
6480 | 2bb5c911 | Michael Hanselmann | # Step: create new storage
|
6481 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(3, steps_total, "Allocate new storage") |
6482 | 2bb5c911 | Michael Hanselmann | iv_names = self._CreateNewStorage(self.target_node) |
6483 | a9e0c397 | Iustin Pop | |
6484 | cff90b79 | Iustin Pop | # Step: for each lv, detach+rename*2+attach
|
6485 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
6486 | cff90b79 | Iustin Pop | for dev, old_lvs, new_lvs in iv_names.itervalues(): |
6487 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Detaching %s drbd from local storage" % dev.iv_name) |
6488 | 2bb5c911 | Michael Hanselmann | |
6489 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_removechildren(self.target_node, dev, |
6490 | 4d4a651d | Michael Hanselmann | old_lvs) |
6491 | 4c4e4e1e | Iustin Pop | result.Raise("Can't detach drbd from local storage on node"
|
6492 | 2bb5c911 | Michael Hanselmann | " %s for device %s" % (self.target_node, dev.iv_name)) |
6493 | cff90b79 | Iustin Pop | #dev.children = []
|
6494 | cff90b79 | Iustin Pop | #cfg.Update(instance)
|
6495 | a9e0c397 | Iustin Pop | |
6496 | a9e0c397 | Iustin Pop | # ok, we created the new LVs, so now we know we have the needed
|
6497 | a9e0c397 | Iustin Pop | # storage; as such, we proceed on the target node to rename
|
6498 | a9e0c397 | Iustin Pop | # old_lv to _old, and new_lv to old_lv; note that we rename LVs
|
6499 | c99a3cc0 | Manuel Franceschini | # using the assumption that logical_id == physical_id (which in
|
6500 | a9e0c397 | Iustin Pop | # turn is the unique_id on that node)
|
6501 | cff90b79 | Iustin Pop | |
6502 | cff90b79 | Iustin Pop | # FIXME(iustin): use a better name for the replaced LVs
|
6503 | a9e0c397 | Iustin Pop | temp_suffix = int(time.time())
|
6504 | a9e0c397 | Iustin Pop | ren_fn = lambda d, suff: (d.physical_id[0], |
6505 | a9e0c397 | Iustin Pop | d.physical_id[1] + "_replaced-%s" % suff) |
6506 | 2bb5c911 | Michael Hanselmann | |
6507 | 2bb5c911 | Michael Hanselmann | # Build the rename list based on what LVs exist on the node
|
6508 | 2bb5c911 | Michael Hanselmann | rename_old_to_new = [] |
6509 | cff90b79 | Iustin Pop | for to_ren in old_lvs: |
6510 | 2bb5c911 | Michael Hanselmann | result = self.rpc.call_blockdev_find(self.target_node, to_ren) |
6511 | 4c4e4e1e | Iustin Pop | if not result.fail_msg and result.payload: |
6512 | 23829f6f | Iustin Pop | # device exists
|
6513 | 2bb5c911 | Michael Hanselmann | rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix))) |
6514 | cff90b79 | Iustin Pop | |
6515 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Renaming the old LVs on the target node") |
6516 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_rename(self.target_node, |
6517 | 4d4a651d | Michael Hanselmann | rename_old_to_new) |
6518 | 2bb5c911 | Michael Hanselmann | result.Raise("Can't rename old LVs on node %s" % self.target_node) |
6519 | 2bb5c911 | Michael Hanselmann | |
6520 | 2bb5c911 | Michael Hanselmann | # Now we rename the new LVs to the old LVs
|
6521 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Renaming the new LVs on the target node") |
6522 | 2bb5c911 | Michael Hanselmann | rename_new_to_old = [(new, old.physical_id) |
6523 | 2bb5c911 | Michael Hanselmann | for old, new in zip(old_lvs, new_lvs)] |
6524 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_rename(self.target_node, |
6525 | 4d4a651d | Michael Hanselmann | rename_new_to_old) |
6526 | 2bb5c911 | Michael Hanselmann | result.Raise("Can't rename new LVs on node %s" % self.target_node) |
6527 | cff90b79 | Iustin Pop | |
6528 | cff90b79 | Iustin Pop | for old, new in zip(old_lvs, new_lvs): |
6529 | cff90b79 | Iustin Pop | new.logical_id = old.logical_id |
6530 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(new, self.target_node) |
6531 | a9e0c397 | Iustin Pop | |
6532 | cff90b79 | Iustin Pop | for disk in old_lvs: |
6533 | cff90b79 | Iustin Pop | disk.logical_id = ren_fn(disk, temp_suffix) |
6534 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(disk, self.target_node) |
6535 | a9e0c397 | Iustin Pop | |
6536 | 2bb5c911 | Michael Hanselmann | # Now that the new lvs have the old name, we can add them to the device
|
6537 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding new mirror component on %s" % self.target_node) |
6538 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_blockdev_addchildren(self.target_node, dev, |
6539 | 4d4a651d | Michael Hanselmann | new_lvs) |
6540 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
6541 | 2cc1da8b | Iustin Pop | if msg:
|
6542 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
6543 | 4d4a651d | Michael Hanselmann | msg2 = self.rpc.call_blockdev_remove(self.target_node, |
6544 | 4d4a651d | Michael Hanselmann | new_lv).fail_msg |
6545 | 4c4e4e1e | Iustin Pop | if msg2:
|
6546 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2, |
6547 | 2bb5c911 | Michael Hanselmann | hint=("cleanup manually the unused logical"
|
6548 | 2bb5c911 | Michael Hanselmann | "volumes"))
|
6549 | 2cc1da8b | Iustin Pop | raise errors.OpExecError("Can't add local storage to drbd: %s" % msg) |
6550 | a9e0c397 | Iustin Pop | |
6551 | a9e0c397 | Iustin Pop | dev.children = new_lvs |
6552 | a9e0c397 | Iustin Pop | |
6553 | 2bb5c911 | Michael Hanselmann | self.cfg.Update(self.instance) |
6554 | a9e0c397 | Iustin Pop | |
6555 | 2bb5c911 | Michael Hanselmann | # Wait for sync
|
6556 | 2bb5c911 | Michael Hanselmann | # This can fail as the old devices are degraded and _WaitForSync
|
6557 | 2bb5c911 | Michael Hanselmann | # does a combined result over all disks, so we don't check its return value
|
6558 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(5, steps_total, "Sync devices") |
6559 | 2bb5c911 | Michael Hanselmann | _WaitForSync(self.lu, self.instance, unlock=True) |
6560 | a9e0c397 | Iustin Pop | |
6561 | 2bb5c911 | Michael Hanselmann | # Check all devices manually
|
6562 | 2bb5c911 | Michael Hanselmann | self._CheckDevices(self.instance.primary_node, iv_names) |
6563 | a9e0c397 | Iustin Pop | |
6564 | cff90b79 | Iustin Pop | # Step: remove old storage
|
6565 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(6, steps_total, "Removing old storage") |
6566 | 2bb5c911 | Michael Hanselmann | self._RemoveOldStorage(self.target_node, iv_names) |
6567 | a9e0c397 | Iustin Pop | |
6568 | 2bb5c911 | Michael Hanselmann | def _ExecDrbd8Secondary(self): |
6569 | 2bb5c911 | Michael Hanselmann | """Replace the secondary node for DRBD 8.
|
6570 | a9e0c397 | Iustin Pop |
|
6571 | a9e0c397 | Iustin Pop | The algorithm for replace is quite complicated:
|
6572 | a9e0c397 | Iustin Pop | - for all disks of the instance:
|
6573 | a9e0c397 | Iustin Pop | - create new LVs on the new node with same names
|
6574 | a9e0c397 | Iustin Pop | - shutdown the drbd device on the old secondary
|
6575 | a9e0c397 | Iustin Pop | - disconnect the drbd network on the primary
|
6576 | a9e0c397 | Iustin Pop | - create the drbd device on the new secondary
|
6577 | a9e0c397 | Iustin Pop | - network attach the drbd on the primary, using an artifice:
|
6578 | a9e0c397 | Iustin Pop | the drbd code for Attach() will connect to the network if it
|
6579 | a9e0c397 | Iustin Pop | finds a device which is connected to the good local disks but
|
6580 | a9e0c397 | Iustin Pop | not network enabled
|
6581 | a9e0c397 | Iustin Pop | - wait for sync across all devices
|
6582 | a9e0c397 | Iustin Pop | - remove all disks from the old secondary
|
6583 | a9e0c397 | Iustin Pop |
|
6584 | a9e0c397 | Iustin Pop | Failures are not very well handled.
|
6585 | 0834c866 | Iustin Pop |
|
6586 | a9e0c397 | Iustin Pop | """
|
6587 | 0834c866 | Iustin Pop | steps_total = 6
|
6588 | 0834c866 | Iustin Pop | |
6589 | 0834c866 | Iustin Pop | # Step: check device activation
|
6590 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(1, steps_total, "Check device existence") |
6591 | 2bb5c911 | Michael Hanselmann | self._CheckDisksExistence([self.instance.primary_node]) |
6592 | 2bb5c911 | Michael Hanselmann | self._CheckVolumeGroup([self.instance.primary_node]) |
6593 | 0834c866 | Iustin Pop | |
6594 | 0834c866 | Iustin Pop | # Step: check other node consistency
|
6595 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(2, steps_total, "Check peer consistency") |
6596 | 2bb5c911 | Michael Hanselmann | self._CheckDisksConsistency(self.instance.primary_node, True, True) |
6597 | 0834c866 | Iustin Pop | |
6598 | 0834c866 | Iustin Pop | # Step: create new storage
|
6599 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(3, steps_total, "Allocate new storage") |
6600 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
6601 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Adding new local storage on %s for disk/%d" % |
6602 | 2bb5c911 | Michael Hanselmann | (self.new_node, idx))
|
6603 | 428958aa | Iustin Pop | # we pass force_create=True to force LVM creation
|
6604 | a9e0c397 | Iustin Pop | for new_lv in dev.children: |
6605 | 2bb5c911 | Michael Hanselmann | _CreateBlockDev(self.lu, self.new_node, self.instance, new_lv, True, |
6606 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
6607 | a9e0c397 | Iustin Pop | |
6608 | 468b46f9 | Iustin Pop | # Step 4: dbrd minors and drbd setups changes
|
6609 | a1578d63 | Iustin Pop | # after this, we must manually remove the drbd minors on both the
|
6610 | a1578d63 | Iustin Pop | # error and the success paths
|
6611 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
6612 | 4d4a651d | Michael Hanselmann | minors = self.cfg.AllocateDRBDMinor([self.new_node |
6613 | 4d4a651d | Michael Hanselmann | for dev in self.instance.disks], |
6614 | 2bb5c911 | Michael Hanselmann | self.instance.name)
|
6615 | 2bb5c911 | Michael Hanselmann | logging.debug("Allocated minors %r" % (minors,))
|
6616 | 2bb5c911 | Michael Hanselmann | |
6617 | 2bb5c911 | Michael Hanselmann | iv_names = {} |
6618 | 2bb5c911 | Michael Hanselmann | for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)): |
6619 | 4d4a651d | Michael Hanselmann | self.lu.LogInfo("activating a new drbd on %s for disk/%d" % |
6620 | 4d4a651d | Michael Hanselmann | (self.new_node, idx))
|
6621 | a2d59d8b | Iustin Pop | # create new devices on new_node; note that we create two IDs:
|
6622 | a2d59d8b | Iustin Pop | # one without port, so the drbd will be activated without
|
6623 | a2d59d8b | Iustin Pop | # networking information on the new node at this stage, and one
|
6624 | a2d59d8b | Iustin Pop | # with network, for the latter activation in step 4
|
6625 | a2d59d8b | Iustin Pop | (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id |
6626 | 2bb5c911 | Michael Hanselmann | if self.instance.primary_node == o_node1: |
6627 | a2d59d8b | Iustin Pop | p_minor = o_minor1 |
6628 | ffa1c0dc | Iustin Pop | else:
|
6629 | a2d59d8b | Iustin Pop | p_minor = o_minor2 |
6630 | a2d59d8b | Iustin Pop | |
6631 | 4d4a651d | Michael Hanselmann | new_alone_id = (self.instance.primary_node, self.new_node, None, |
6632 | 4d4a651d | Michael Hanselmann | p_minor, new_minor, o_secret) |
6633 | 4d4a651d | Michael Hanselmann | new_net_id = (self.instance.primary_node, self.new_node, o_port, |
6634 | 4d4a651d | Michael Hanselmann | p_minor, new_minor, o_secret) |
6635 | a2d59d8b | Iustin Pop | |
6636 | a2d59d8b | Iustin Pop | iv_names[idx] = (dev, dev.children, new_net_id) |
6637 | a1578d63 | Iustin Pop | logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
|
6638 | a2d59d8b | Iustin Pop | new_net_id) |
6639 | a9e0c397 | Iustin Pop | new_drbd = objects.Disk(dev_type=constants.LD_DRBD8, |
6640 | a2d59d8b | Iustin Pop | logical_id=new_alone_id, |
6641 | 8a6c7011 | Iustin Pop | children=dev.children, |
6642 | 8a6c7011 | Iustin Pop | size=dev.size) |
6643 | 796cab27 | Iustin Pop | try:
|
6644 | 2bb5c911 | Michael Hanselmann | _CreateSingleBlockDev(self.lu, self.new_node, self.instance, new_drbd, |
6645 | 2bb5c911 | Michael Hanselmann | _GetInstanceInfoText(self.instance), False) |
6646 | 82759cb1 | Iustin Pop | except errors.GenericError:
|
6647 | 2bb5c911 | Michael Hanselmann | self.cfg.ReleaseDRBDMinors(self.instance.name) |
6648 | 796cab27 | Iustin Pop | raise
|
6649 | a9e0c397 | Iustin Pop | |
6650 | 2bb5c911 | Michael Hanselmann | # We have new devices, shutdown the drbd on the old secondary
|
6651 | 2bb5c911 | Michael Hanselmann | for idx, dev in enumerate(self.instance.disks): |
6652 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Shutting down drbd for disk/%d on old node" % idx) |
6653 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, self.target_node) |
6654 | 2bb5c911 | Michael Hanselmann | msg = self.rpc.call_blockdev_shutdown(self.target_node, dev).fail_msg |
6655 | cacfd1fd | Iustin Pop | if msg:
|
6656 | 2bb5c911 | Michael Hanselmann | self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old" |
6657 | 2bb5c911 | Michael Hanselmann | "node: %s" % (idx, msg),
|
6658 | 2bb5c911 | Michael Hanselmann | hint=("Please cleanup this device manually as"
|
6659 | 2bb5c911 | Michael Hanselmann | " soon as possible"))
|
6660 | a9e0c397 | Iustin Pop | |
6661 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)") |
6662 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_drbd_disconnect_net([self.instance.primary_node], |
6663 | 4d4a651d | Michael Hanselmann | self.node_secondary_ip,
|
6664 | 4d4a651d | Michael Hanselmann | self.instance.disks)\
|
6665 | 4d4a651d | Michael Hanselmann | [self.instance.primary_node]
|
6666 | 642445d9 | Iustin Pop | |
6667 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
6668 | a2d59d8b | Iustin Pop | if msg:
|
6669 | a2d59d8b | Iustin Pop | # detaches didn't succeed (unlikely)
|
6670 | 2bb5c911 | Michael Hanselmann | self.cfg.ReleaseDRBDMinors(self.instance.name) |
6671 | a2d59d8b | Iustin Pop | raise errors.OpExecError("Can't detach the disks from the network on" |
6672 | a2d59d8b | Iustin Pop | " old node: %s" % (msg,))
|
6673 | 642445d9 | Iustin Pop | |
6674 | 642445d9 | Iustin Pop | # if we managed to detach at least one, we update all the disks of
|
6675 | 642445d9 | Iustin Pop | # the instance to point to the new secondary
|
6676 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Updating instance configuration") |
6677 | 468b46f9 | Iustin Pop | for dev, _, new_logical_id in iv_names.itervalues(): |
6678 | 468b46f9 | Iustin Pop | dev.logical_id = new_logical_id |
6679 | 2bb5c911 | Michael Hanselmann | self.cfg.SetDiskID(dev, self.instance.primary_node) |
6680 | 2bb5c911 | Michael Hanselmann | |
6681 | 2bb5c911 | Michael Hanselmann | self.cfg.Update(self.instance) |
6682 | a9e0c397 | Iustin Pop | |
6683 | 642445d9 | Iustin Pop | # and now perform the drbd attach
|
6684 | 2bb5c911 | Michael Hanselmann | self.lu.LogInfo("Attaching primary drbds to new secondary" |
6685 | 2bb5c911 | Michael Hanselmann | " (standalone => connected)")
|
6686 | 4d4a651d | Michael Hanselmann | result = self.rpc.call_drbd_attach_net([self.instance.primary_node, |
6687 | 4d4a651d | Michael Hanselmann | self.new_node],
|
6688 | 4d4a651d | Michael Hanselmann | self.node_secondary_ip,
|
6689 | 4d4a651d | Michael Hanselmann | self.instance.disks,
|
6690 | 4d4a651d | Michael Hanselmann | self.instance.name,
|
6691 | a2d59d8b | Iustin Pop | False)
|
6692 | a2d59d8b | Iustin Pop | for to_node, to_result in result.items(): |
6693 | 4c4e4e1e | Iustin Pop | msg = to_result.fail_msg |
6694 | a2d59d8b | Iustin Pop | if msg:
|
6695 | 4d4a651d | Michael Hanselmann | self.lu.LogWarning("Can't attach drbd disks on node %s: %s", |
6696 | 4d4a651d | Michael Hanselmann | to_node, msg, |
6697 | 2bb5c911 | Michael Hanselmann | hint=("please do a gnt-instance info to see the"
|
6698 | 2bb5c911 | Michael Hanselmann | " status of disks"))
|
6699 | a9e0c397 | Iustin Pop | |
6700 | 2bb5c911 | Michael Hanselmann | # Wait for sync
|
6701 | 2bb5c911 | Michael Hanselmann | # This can fail as the old devices are degraded and _WaitForSync
|
6702 | 2bb5c911 | Michael Hanselmann | # does a combined result over all disks, so we don't check its return value
|
6703 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(5, steps_total, "Sync devices") |
6704 | 2bb5c911 | Michael Hanselmann | _WaitForSync(self.lu, self.instance, unlock=True) |
6705 | a9e0c397 | Iustin Pop | |
6706 | 2bb5c911 | Michael Hanselmann | # Check all devices manually
|
6707 | 2bb5c911 | Michael Hanselmann | self._CheckDevices(self.instance.primary_node, iv_names) |
6708 | 22985314 | Guido Trotter | |
6709 | 2bb5c911 | Michael Hanselmann | # Step: remove old storage
|
6710 | 2bb5c911 | Michael Hanselmann | self.lu.LogStep(6, steps_total, "Removing old storage") |
6711 | 2bb5c911 | Michael Hanselmann | self._RemoveOldStorage(self.target_node, iv_names) |
6712 | a9e0c397 | Iustin Pop | |
6713 | a8083063 | Iustin Pop | |
6714 | 76aef8fc | Michael Hanselmann | class LURepairNodeStorage(NoHooksLU): |
6715 | 76aef8fc | Michael Hanselmann | """Repairs the volume group on a node.
|
6716 | 76aef8fc | Michael Hanselmann |
|
6717 | 76aef8fc | Michael Hanselmann | """
|
6718 | 76aef8fc | Michael Hanselmann | _OP_REQP = ["node_name"]
|
6719 | 76aef8fc | Michael Hanselmann | REQ_BGL = False
|
6720 | 76aef8fc | Michael Hanselmann | |
6721 | 76aef8fc | Michael Hanselmann | def CheckArguments(self): |
6722 | 76aef8fc | Michael Hanselmann | node_name = self.cfg.ExpandNodeName(self.op.node_name) |
6723 | 76aef8fc | Michael Hanselmann | if node_name is None: |
6724 | 76aef8fc | Michael Hanselmann | raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name) |
6725 | 76aef8fc | Michael Hanselmann | |
6726 | 76aef8fc | Michael Hanselmann | self.op.node_name = node_name
|
6727 | 76aef8fc | Michael Hanselmann | |
6728 | 76aef8fc | Michael Hanselmann | def ExpandNames(self): |
6729 | 76aef8fc | Michael Hanselmann | self.needed_locks = {
|
6730 | 76aef8fc | Michael Hanselmann | locking.LEVEL_NODE: [self.op.node_name],
|
6731 | 76aef8fc | Michael Hanselmann | } |
6732 | 76aef8fc | Michael Hanselmann | |
6733 | 76aef8fc | Michael Hanselmann | def _CheckFaultyDisks(self, instance, node_name): |
6734 | 76aef8fc | Michael Hanselmann | if _FindFaultyInstanceDisks(self.cfg, self.rpc, instance, |
6735 | 76aef8fc | Michael Hanselmann | node_name, True):
|
6736 | 76aef8fc | Michael Hanselmann | raise errors.OpPrereqError("Instance '%s' has faulty disks on" |
6737 | aa053071 | Michael Hanselmann | " node '%s'" % (instance.name, node_name))
|
6738 | 76aef8fc | Michael Hanselmann | |
6739 | 76aef8fc | Michael Hanselmann | def CheckPrereq(self): |
6740 | 76aef8fc | Michael Hanselmann | """Check prerequisites.
|
6741 | 76aef8fc | Michael Hanselmann |
|
6742 | 76aef8fc | Michael Hanselmann | """
|
6743 | 76aef8fc | Michael Hanselmann | storage_type = self.op.storage_type
|
6744 | 76aef8fc | Michael Hanselmann | |
6745 | 76aef8fc | Michael Hanselmann | if (constants.SO_FIX_CONSISTENCY not in |
6746 | 76aef8fc | Michael Hanselmann | constants.VALID_STORAGE_OPERATIONS.get(storage_type, [])): |
6747 | 76aef8fc | Michael Hanselmann | raise errors.OpPrereqError("Storage units of type '%s' can not be" |
6748 | 76aef8fc | Michael Hanselmann | " repaired" % storage_type)
|
6749 | 76aef8fc | Michael Hanselmann | |
6750 | 76aef8fc | Michael Hanselmann | # Check whether any instance on this node has faulty disks
|
6751 | 76aef8fc | Michael Hanselmann | for inst in _GetNodeInstances(self.cfg, self.op.node_name): |
6752 | 76aef8fc | Michael Hanselmann | check_nodes = set(inst.all_nodes)
|
6753 | 76aef8fc | Michael Hanselmann | check_nodes.discard(self.op.node_name)
|
6754 | 76aef8fc | Michael Hanselmann | for inst_node_name in check_nodes: |
6755 | 76aef8fc | Michael Hanselmann | self._CheckFaultyDisks(inst, inst_node_name)
|
6756 | 76aef8fc | Michael Hanselmann | |
6757 | 76aef8fc | Michael Hanselmann | def Exec(self, feedback_fn): |
6758 | 76aef8fc | Michael Hanselmann | feedback_fn("Repairing storage unit '%s' on %s ..." %
|
6759 | 76aef8fc | Michael Hanselmann | (self.op.name, self.op.node_name)) |
6760 | 76aef8fc | Michael Hanselmann | |
6761 | 76aef8fc | Michael Hanselmann | st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type) |
6762 | 76aef8fc | Michael Hanselmann | result = self.rpc.call_storage_execute(self.op.node_name, |
6763 | 76aef8fc | Michael Hanselmann | self.op.storage_type, st_args,
|
6764 | 76aef8fc | Michael Hanselmann | self.op.name,
|
6765 | 76aef8fc | Michael Hanselmann | constants.SO_FIX_CONSISTENCY) |
6766 | 76aef8fc | Michael Hanselmann | result.Raise("Failed to repair storage unit '%s' on %s" %
|
6767 | 76aef8fc | Michael Hanselmann | (self.op.name, self.op.node_name)) |
6768 | 76aef8fc | Michael Hanselmann | |
6769 | 76aef8fc | Michael Hanselmann | |
6770 | 8729e0d7 | Iustin Pop | class LUGrowDisk(LogicalUnit): |
6771 | 8729e0d7 | Iustin Pop | """Grow a disk of an instance.
|
6772 | 8729e0d7 | Iustin Pop |
|
6773 | 8729e0d7 | Iustin Pop | """
|
6774 | 8729e0d7 | Iustin Pop | HPATH = "disk-grow"
|
6775 | 8729e0d7 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
6776 | 6605411d | Iustin Pop | _OP_REQP = ["instance_name", "disk", "amount", "wait_for_sync"] |
6777 | 31e63dbf | Guido Trotter | REQ_BGL = False
|
6778 | 31e63dbf | Guido Trotter | |
6779 | 31e63dbf | Guido Trotter | def ExpandNames(self): |
6780 | 31e63dbf | Guido Trotter | self._ExpandAndLockInstance()
|
6781 | 31e63dbf | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
6782 | f6d9a522 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
6783 | 31e63dbf | Guido Trotter | |
6784 | 31e63dbf | Guido Trotter | def DeclareLocks(self, level): |
6785 | 31e63dbf | Guido Trotter | if level == locking.LEVEL_NODE:
|
6786 | 31e63dbf | Guido Trotter | self._LockInstancesNodes()
|
6787 | 8729e0d7 | Iustin Pop | |
6788 | 8729e0d7 | Iustin Pop | def BuildHooksEnv(self): |
6789 | 8729e0d7 | Iustin Pop | """Build hooks env.
|
6790 | 8729e0d7 | Iustin Pop |
|
6791 | 8729e0d7 | Iustin Pop | This runs on the master, the primary and all the secondaries.
|
6792 | 8729e0d7 | Iustin Pop |
|
6793 | 8729e0d7 | Iustin Pop | """
|
6794 | 8729e0d7 | Iustin Pop | env = { |
6795 | 8729e0d7 | Iustin Pop | "DISK": self.op.disk, |
6796 | 8729e0d7 | Iustin Pop | "AMOUNT": self.op.amount, |
6797 | 8729e0d7 | Iustin Pop | } |
6798 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
6799 | 8729e0d7 | Iustin Pop | nl = [ |
6800 | d6a02168 | Michael Hanselmann | self.cfg.GetMasterNode(),
|
6801 | 8729e0d7 | Iustin Pop | self.instance.primary_node,
|
6802 | 8729e0d7 | Iustin Pop | ] |
6803 | 8729e0d7 | Iustin Pop | return env, nl, nl
|
6804 | 8729e0d7 | Iustin Pop | |
6805 | 8729e0d7 | Iustin Pop | def CheckPrereq(self): |
6806 | 8729e0d7 | Iustin Pop | """Check prerequisites.
|
6807 | 8729e0d7 | Iustin Pop |
|
6808 | 8729e0d7 | Iustin Pop | This checks that the instance is in the cluster.
|
6809 | 8729e0d7 | Iustin Pop |
|
6810 | 8729e0d7 | Iustin Pop | """
|
6811 | 31e63dbf | Guido Trotter | instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
6812 | 31e63dbf | Guido Trotter | assert instance is not None, \ |
6813 | 31e63dbf | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
6814 | 6b12959c | Iustin Pop | nodenames = list(instance.all_nodes)
|
6815 | 6b12959c | Iustin Pop | for node in nodenames: |
6816 | 7527a8a4 | Iustin Pop | _CheckNodeOnline(self, node)
|
6817 | 7527a8a4 | Iustin Pop | |
6818 | 31e63dbf | Guido Trotter | |
6819 | 8729e0d7 | Iustin Pop | self.instance = instance
|
6820 | 8729e0d7 | Iustin Pop | |
6821 | 8729e0d7 | Iustin Pop | if instance.disk_template not in (constants.DT_PLAIN, constants.DT_DRBD8): |
6822 | 8729e0d7 | Iustin Pop | raise errors.OpPrereqError("Instance's disk layout does not support" |
6823 | 8729e0d7 | Iustin Pop | " growing.")
|
6824 | 8729e0d7 | Iustin Pop | |
6825 | ad24e046 | Iustin Pop | self.disk = instance.FindDisk(self.op.disk) |
6826 | 8729e0d7 | Iustin Pop | |
6827 | 72737a7f | Iustin Pop | nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(), |
6828 | 72737a7f | Iustin Pop | instance.hypervisor) |
6829 | 8729e0d7 | Iustin Pop | for node in nodenames: |
6830 | 781de953 | Iustin Pop | info = nodeinfo[node] |
6831 | 4c4e4e1e | Iustin Pop | info.Raise("Cannot get current information from node %s" % node)
|
6832 | 070e998b | Iustin Pop | vg_free = info.payload.get('vg_free', None) |
6833 | 8729e0d7 | Iustin Pop | if not isinstance(vg_free, int): |
6834 | 8729e0d7 | Iustin Pop | raise errors.OpPrereqError("Can't compute free disk space on" |
6835 | 8729e0d7 | Iustin Pop | " node %s" % node)
|
6836 | 781de953 | Iustin Pop | if self.op.amount > vg_free: |
6837 | 8729e0d7 | Iustin Pop | raise errors.OpPrereqError("Not enough disk space on target node %s:" |
6838 | 8729e0d7 | Iustin Pop | " %d MiB available, %d MiB required" %
|
6839 | 781de953 | Iustin Pop | (node, vg_free, self.op.amount))
|
6840 | 8729e0d7 | Iustin Pop | |
6841 | 8729e0d7 | Iustin Pop | def Exec(self, feedback_fn): |
6842 | 8729e0d7 | Iustin Pop | """Execute disk grow.
|
6843 | 8729e0d7 | Iustin Pop |
|
6844 | 8729e0d7 | Iustin Pop | """
|
6845 | 8729e0d7 | Iustin Pop | instance = self.instance
|
6846 | ad24e046 | Iustin Pop | disk = self.disk
|
6847 | 6b12959c | Iustin Pop | for node in instance.all_nodes: |
6848 | 8729e0d7 | Iustin Pop | self.cfg.SetDiskID(disk, node)
|
6849 | 72737a7f | Iustin Pop | result = self.rpc.call_blockdev_grow(node, disk, self.op.amount) |
6850 | 4c4e4e1e | Iustin Pop | result.Raise("Grow request failed to node %s" % node)
|
6851 | 8729e0d7 | Iustin Pop | disk.RecordGrow(self.op.amount)
|
6852 | 8729e0d7 | Iustin Pop | self.cfg.Update(instance)
|
6853 | 6605411d | Iustin Pop | if self.op.wait_for_sync: |
6854 | cd4d138f | Guido Trotter | disk_abort = not _WaitForSync(self, instance) |
6855 | 6605411d | Iustin Pop | if disk_abort:
|
6856 | 86d9d3bb | Iustin Pop | self.proc.LogWarning("Warning: disk sync-ing has not returned a good" |
6857 | 86d9d3bb | Iustin Pop | " status.\nPlease check the instance.")
|
6858 | 8729e0d7 | Iustin Pop | |
6859 | 8729e0d7 | Iustin Pop | |
6860 | a8083063 | Iustin Pop | class LUQueryInstanceData(NoHooksLU): |
6861 | a8083063 | Iustin Pop | """Query runtime instance data.
|
6862 | a8083063 | Iustin Pop |
|
6863 | a8083063 | Iustin Pop | """
|
6864 | 57821cac | Iustin Pop | _OP_REQP = ["instances", "static"] |
6865 | a987fa48 | Guido Trotter | REQ_BGL = False
|
6866 | ae5849b5 | Michael Hanselmann | |
6867 | a987fa48 | Guido Trotter | def ExpandNames(self): |
6868 | a987fa48 | Guido Trotter | self.needed_locks = {}
|
6869 | c772d142 | Michael Hanselmann | self.share_locks = dict.fromkeys(locking.LEVELS, 1) |
6870 | a987fa48 | Guido Trotter | |
6871 | a987fa48 | Guido Trotter | if not isinstance(self.op.instances, list): |
6872 | a987fa48 | Guido Trotter | raise errors.OpPrereqError("Invalid argument type 'instances'") |
6873 | a987fa48 | Guido Trotter | |
6874 | a987fa48 | Guido Trotter | if self.op.instances: |
6875 | a987fa48 | Guido Trotter | self.wanted_names = []
|
6876 | a987fa48 | Guido Trotter | for name in self.op.instances: |
6877 | a987fa48 | Guido Trotter | full_name = self.cfg.ExpandInstanceName(name)
|
6878 | a987fa48 | Guido Trotter | if full_name is None: |
6879 | f57c76e4 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % name) |
6880 | a987fa48 | Guido Trotter | self.wanted_names.append(full_name)
|
6881 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names |
6882 | a987fa48 | Guido Trotter | else:
|
6883 | a987fa48 | Guido Trotter | self.wanted_names = None |
6884 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
|
6885 | a987fa48 | Guido Trotter | |
6886 | a987fa48 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = []
|
6887 | a987fa48 | Guido Trotter | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
6888 | a987fa48 | Guido Trotter | |
6889 | a987fa48 | Guido Trotter | def DeclareLocks(self, level): |
6890 | a987fa48 | Guido Trotter | if level == locking.LEVEL_NODE:
|
6891 | a987fa48 | Guido Trotter | self._LockInstancesNodes()
|
6892 | a8083063 | Iustin Pop | |
6893 | a8083063 | Iustin Pop | def CheckPrereq(self): |
6894 | a8083063 | Iustin Pop | """Check prerequisites.
|
6895 | a8083063 | Iustin Pop |
|
6896 | a8083063 | Iustin Pop | This only checks the optional instance list against the existing names.
|
6897 | a8083063 | Iustin Pop |
|
6898 | a8083063 | Iustin Pop | """
|
6899 | a987fa48 | Guido Trotter | if self.wanted_names is None: |
6900 | a987fa48 | Guido Trotter | self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE] |
6901 | a8083063 | Iustin Pop | |
6902 | a987fa48 | Guido Trotter | self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name |
6903 | a987fa48 | Guido Trotter | in self.wanted_names] |
6904 | a987fa48 | Guido Trotter | return
|
6905 | a8083063 | Iustin Pop | |
6906 | 98825740 | Michael Hanselmann | def _ComputeBlockdevStatus(self, node, instance_name, dev): |
6907 | 98825740 | Michael Hanselmann | """Returns the status of a block device
|
6908 | 98825740 | Michael Hanselmann |
|
6909 | 98825740 | Michael Hanselmann | """
|
6910 | 4dce1a83 | Michael Hanselmann | if self.op.static or not node: |
6911 | 98825740 | Michael Hanselmann | return None |
6912 | 98825740 | Michael Hanselmann | |
6913 | 98825740 | Michael Hanselmann | self.cfg.SetDiskID(dev, node)
|
6914 | 98825740 | Michael Hanselmann | |
6915 | 98825740 | Michael Hanselmann | result = self.rpc.call_blockdev_find(node, dev)
|
6916 | 98825740 | Michael Hanselmann | if result.offline:
|
6917 | 98825740 | Michael Hanselmann | return None |
6918 | 98825740 | Michael Hanselmann | |
6919 | 98825740 | Michael Hanselmann | result.Raise("Can't compute disk status for %s" % instance_name)
|
6920 | 98825740 | Michael Hanselmann | |
6921 | 98825740 | Michael Hanselmann | status = result.payload |
6922 | ddfe2228 | Michael Hanselmann | if status is None: |
6923 | ddfe2228 | Michael Hanselmann | return None |
6924 | 98825740 | Michael Hanselmann | |
6925 | 98825740 | Michael Hanselmann | return (status.dev_path, status.major, status.minor,
|
6926 | 98825740 | Michael Hanselmann | status.sync_percent, status.estimated_time, |
6927 | f208978a | Michael Hanselmann | status.is_degraded, status.ldisk_status) |
6928 | 98825740 | Michael Hanselmann | |
6929 | a8083063 | Iustin Pop | def _ComputeDiskStatus(self, instance, snode, dev): |
6930 | a8083063 | Iustin Pop | """Compute block device status.
|
6931 | a8083063 | Iustin Pop |
|
6932 | a8083063 | Iustin Pop | """
|
6933 | a1f445d3 | Iustin Pop | if dev.dev_type in constants.LDS_DRBD: |
6934 | a8083063 | Iustin Pop | # we change the snode then (otherwise we use the one passed in)
|
6935 | a8083063 | Iustin Pop | if dev.logical_id[0] == instance.primary_node: |
6936 | a8083063 | Iustin Pop | snode = dev.logical_id[1]
|
6937 | a8083063 | Iustin Pop | else:
|
6938 | a8083063 | Iustin Pop | snode = dev.logical_id[0]
|
6939 | a8083063 | Iustin Pop | |
6940 | 98825740 | Michael Hanselmann | dev_pstatus = self._ComputeBlockdevStatus(instance.primary_node,
|
6941 | 98825740 | Michael Hanselmann | instance.name, dev) |
6942 | 98825740 | Michael Hanselmann | dev_sstatus = self._ComputeBlockdevStatus(snode, instance.name, dev)
|
6943 | a8083063 | Iustin Pop | |
6944 | a8083063 | Iustin Pop | if dev.children:
|
6945 | a8083063 | Iustin Pop | dev_children = [self._ComputeDiskStatus(instance, snode, child)
|
6946 | a8083063 | Iustin Pop | for child in dev.children] |
6947 | a8083063 | Iustin Pop | else:
|
6948 | a8083063 | Iustin Pop | dev_children = [] |
6949 | a8083063 | Iustin Pop | |
6950 | a8083063 | Iustin Pop | data = { |
6951 | a8083063 | Iustin Pop | "iv_name": dev.iv_name,
|
6952 | a8083063 | Iustin Pop | "dev_type": dev.dev_type,
|
6953 | a8083063 | Iustin Pop | "logical_id": dev.logical_id,
|
6954 | a8083063 | Iustin Pop | "physical_id": dev.physical_id,
|
6955 | a8083063 | Iustin Pop | "pstatus": dev_pstatus,
|
6956 | a8083063 | Iustin Pop | "sstatus": dev_sstatus,
|
6957 | a8083063 | Iustin Pop | "children": dev_children,
|
6958 | b6fdf8b8 | Iustin Pop | "mode": dev.mode,
|
6959 | c98162a7 | Iustin Pop | "size": dev.size,
|
6960 | a8083063 | Iustin Pop | } |
6961 | a8083063 | Iustin Pop | |
6962 | a8083063 | Iustin Pop | return data
|
6963 | a8083063 | Iustin Pop | |
6964 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
6965 | a8083063 | Iustin Pop | """Gather and return data"""
|
6966 | a8083063 | Iustin Pop | result = {} |
6967 | 338e51e8 | Iustin Pop | |
6968 | 338e51e8 | Iustin Pop | cluster = self.cfg.GetClusterInfo()
|
6969 | 338e51e8 | Iustin Pop | |
6970 | a8083063 | Iustin Pop | for instance in self.wanted_instances: |
6971 | 57821cac | Iustin Pop | if not self.op.static: |
6972 | 57821cac | Iustin Pop | remote_info = self.rpc.call_instance_info(instance.primary_node,
|
6973 | 57821cac | Iustin Pop | instance.name, |
6974 | 57821cac | Iustin Pop | instance.hypervisor) |
6975 | 4c4e4e1e | Iustin Pop | remote_info.Raise("Error checking node %s" % instance.primary_node)
|
6976 | 7ad1af4a | Iustin Pop | remote_info = remote_info.payload |
6977 | 57821cac | Iustin Pop | if remote_info and "state" in remote_info: |
6978 | 57821cac | Iustin Pop | remote_state = "up"
|
6979 | 57821cac | Iustin Pop | else:
|
6980 | 57821cac | Iustin Pop | remote_state = "down"
|
6981 | a8083063 | Iustin Pop | else:
|
6982 | 57821cac | Iustin Pop | remote_state = None
|
6983 | 0d68c45d | Iustin Pop | if instance.admin_up:
|
6984 | a8083063 | Iustin Pop | config_state = "up"
|
6985 | 0d68c45d | Iustin Pop | else:
|
6986 | 0d68c45d | Iustin Pop | config_state = "down"
|
6987 | a8083063 | Iustin Pop | |
6988 | a8083063 | Iustin Pop | disks = [self._ComputeDiskStatus(instance, None, device) |
6989 | a8083063 | Iustin Pop | for device in instance.disks] |
6990 | a8083063 | Iustin Pop | |
6991 | a8083063 | Iustin Pop | idict = { |
6992 | a8083063 | Iustin Pop | "name": instance.name,
|
6993 | a8083063 | Iustin Pop | "config_state": config_state,
|
6994 | a8083063 | Iustin Pop | "run_state": remote_state,
|
6995 | a8083063 | Iustin Pop | "pnode": instance.primary_node,
|
6996 | a8083063 | Iustin Pop | "snodes": instance.secondary_nodes,
|
6997 | a8083063 | Iustin Pop | "os": instance.os,
|
6998 | 0b13832c | Guido Trotter | # this happens to be the same format used for hooks
|
6999 | 0b13832c | Guido Trotter | "nics": _NICListToTuple(self, instance.nics), |
7000 | a8083063 | Iustin Pop | "disks": disks,
|
7001 | e69d05fd | Iustin Pop | "hypervisor": instance.hypervisor,
|
7002 | 24838135 | Iustin Pop | "network_port": instance.network_port,
|
7003 | 24838135 | Iustin Pop | "hv_instance": instance.hvparams,
|
7004 | 338e51e8 | Iustin Pop | "hv_actual": cluster.FillHV(instance),
|
7005 | 338e51e8 | Iustin Pop | "be_instance": instance.beparams,
|
7006 | 338e51e8 | Iustin Pop | "be_actual": cluster.FillBE(instance),
|
7007 | 90f72445 | Iustin Pop | "serial_no": instance.serial_no,
|
7008 | 90f72445 | Iustin Pop | "mtime": instance.mtime,
|
7009 | 90f72445 | Iustin Pop | "ctime": instance.ctime,
|
7010 | a8083063 | Iustin Pop | } |
7011 | a8083063 | Iustin Pop | |
7012 | a8083063 | Iustin Pop | result[instance.name] = idict |
7013 | a8083063 | Iustin Pop | |
7014 | a8083063 | Iustin Pop | return result
|
7015 | a8083063 | Iustin Pop | |
7016 | a8083063 | Iustin Pop | |
7017 | 7767bbf5 | Manuel Franceschini | class LUSetInstanceParams(LogicalUnit): |
7018 | a8083063 | Iustin Pop | """Modifies an instances's parameters.
|
7019 | a8083063 | Iustin Pop |
|
7020 | a8083063 | Iustin Pop | """
|
7021 | a8083063 | Iustin Pop | HPATH = "instance-modify"
|
7022 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
7023 | 24991749 | Iustin Pop | _OP_REQP = ["instance_name"]
|
7024 | 1a5c7281 | Guido Trotter | REQ_BGL = False
|
7025 | 1a5c7281 | Guido Trotter | |
7026 | 24991749 | Iustin Pop | def CheckArguments(self): |
7027 | 24991749 | Iustin Pop | if not hasattr(self.op, 'nics'): |
7028 | 24991749 | Iustin Pop | self.op.nics = []
|
7029 | 24991749 | Iustin Pop | if not hasattr(self.op, 'disks'): |
7030 | 24991749 | Iustin Pop | self.op.disks = []
|
7031 | 24991749 | Iustin Pop | if not hasattr(self.op, 'beparams'): |
7032 | 24991749 | Iustin Pop | self.op.beparams = {}
|
7033 | 24991749 | Iustin Pop | if not hasattr(self.op, 'hvparams'): |
7034 | 24991749 | Iustin Pop | self.op.hvparams = {}
|
7035 | 24991749 | Iustin Pop | self.op.force = getattr(self.op, "force", False) |
7036 | 24991749 | Iustin Pop | if not (self.op.nics or self.op.disks or |
7037 | 24991749 | Iustin Pop | self.op.hvparams or self.op.beparams): |
7038 | 24991749 | Iustin Pop | raise errors.OpPrereqError("No changes submitted") |
7039 | 24991749 | Iustin Pop | |
7040 | 24991749 | Iustin Pop | # Disk validation
|
7041 | 24991749 | Iustin Pop | disk_addremove = 0
|
7042 | 24991749 | Iustin Pop | for disk_op, disk_dict in self.op.disks: |
7043 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
7044 | 24991749 | Iustin Pop | disk_addremove += 1
|
7045 | 24991749 | Iustin Pop | continue
|
7046 | 24991749 | Iustin Pop | elif disk_op == constants.DDM_ADD:
|
7047 | 24991749 | Iustin Pop | disk_addremove += 1
|
7048 | 24991749 | Iustin Pop | else:
|
7049 | 24991749 | Iustin Pop | if not isinstance(disk_op, int): |
7050 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk index") |
7051 | 8b46606c | Guido Trotter | if not isinstance(disk_dict, dict): |
7052 | 8b46606c | Guido Trotter | msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
|
7053 | 8b46606c | Guido Trotter | raise errors.OpPrereqError(msg)
|
7054 | 8b46606c | Guido Trotter | |
7055 | 24991749 | Iustin Pop | if disk_op == constants.DDM_ADD:
|
7056 | 24991749 | Iustin Pop | mode = disk_dict.setdefault('mode', constants.DISK_RDWR)
|
7057 | 6ec66eae | Iustin Pop | if mode not in constants.DISK_ACCESS_SET: |
7058 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode) |
7059 | 24991749 | Iustin Pop | size = disk_dict.get('size', None) |
7060 | 24991749 | Iustin Pop | if size is None: |
7061 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Required disk parameter size missing") |
7062 | 24991749 | Iustin Pop | try:
|
7063 | 24991749 | Iustin Pop | size = int(size)
|
7064 | 24991749 | Iustin Pop | except ValueError, err: |
7065 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk size parameter: %s" % |
7066 | 24991749 | Iustin Pop | str(err))
|
7067 | 24991749 | Iustin Pop | disk_dict['size'] = size
|
7068 | 24991749 | Iustin Pop | else:
|
7069 | 24991749 | Iustin Pop | # modification of disk
|
7070 | 24991749 | Iustin Pop | if 'size' in disk_dict: |
7071 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Disk size change not possible, use" |
7072 | 24991749 | Iustin Pop | " grow-disk")
|
7073 | 24991749 | Iustin Pop | |
7074 | 24991749 | Iustin Pop | if disk_addremove > 1: |
7075 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Only one disk add or remove operation" |
7076 | 24991749 | Iustin Pop | " supported at a time")
|
7077 | 24991749 | Iustin Pop | |
7078 | 24991749 | Iustin Pop | # NIC validation
|
7079 | 24991749 | Iustin Pop | nic_addremove = 0
|
7080 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
7081 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
7082 | 24991749 | Iustin Pop | nic_addremove += 1
|
7083 | 24991749 | Iustin Pop | continue
|
7084 | 24991749 | Iustin Pop | elif nic_op == constants.DDM_ADD:
|
7085 | 24991749 | Iustin Pop | nic_addremove += 1
|
7086 | 24991749 | Iustin Pop | else:
|
7087 | 24991749 | Iustin Pop | if not isinstance(nic_op, int): |
7088 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid nic index") |
7089 | 8b46606c | Guido Trotter | if not isinstance(nic_dict, dict): |
7090 | 8b46606c | Guido Trotter | msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
|
7091 | 8b46606c | Guido Trotter | raise errors.OpPrereqError(msg)
|
7092 | 24991749 | Iustin Pop | |
7093 | 24991749 | Iustin Pop | # nic_dict should be a dict
|
7094 | 24991749 | Iustin Pop | nic_ip = nic_dict.get('ip', None) |
7095 | 24991749 | Iustin Pop | if nic_ip is not None: |
7096 | 5c44da6a | Guido Trotter | if nic_ip.lower() == constants.VALUE_NONE:
|
7097 | 24991749 | Iustin Pop | nic_dict['ip'] = None |
7098 | 24991749 | Iustin Pop | else:
|
7099 | 24991749 | Iustin Pop | if not utils.IsValidIP(nic_ip): |
7100 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip) |
7101 | 5c44da6a | Guido Trotter | |
7102 | cd098c41 | Guido Trotter | nic_bridge = nic_dict.get('bridge', None) |
7103 | cd098c41 | Guido Trotter | nic_link = nic_dict.get('link', None) |
7104 | cd098c41 | Guido Trotter | if nic_bridge and nic_link: |
7105 | 29921401 | Iustin Pop | raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'" |
7106 | 29921401 | Iustin Pop | " at the same time")
|
7107 | cd098c41 | Guido Trotter | elif nic_bridge and nic_bridge.lower() == constants.VALUE_NONE: |
7108 | cd098c41 | Guido Trotter | nic_dict['bridge'] = None |
7109 | cd098c41 | Guido Trotter | elif nic_link and nic_link.lower() == constants.VALUE_NONE: |
7110 | cd098c41 | Guido Trotter | nic_dict['link'] = None |
7111 | cd098c41 | Guido Trotter | |
7112 | 5c44da6a | Guido Trotter | if nic_op == constants.DDM_ADD:
|
7113 | 5c44da6a | Guido Trotter | nic_mac = nic_dict.get('mac', None) |
7114 | 5c44da6a | Guido Trotter | if nic_mac is None: |
7115 | 5c44da6a | Guido Trotter | nic_dict['mac'] = constants.VALUE_AUTO
|
7116 | 5c44da6a | Guido Trotter | |
7117 | 5c44da6a | Guido Trotter | if 'mac' in nic_dict: |
7118 | 5c44da6a | Guido Trotter | nic_mac = nic_dict['mac']
|
7119 | 24991749 | Iustin Pop | if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
7120 | 24991749 | Iustin Pop | if not utils.IsValidMac(nic_mac): |
7121 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac) |
7122 | 5c44da6a | Guido Trotter | if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO: |
7123 | 5c44da6a | Guido Trotter | raise errors.OpPrereqError("'auto' is not a valid MAC address when" |
7124 | 5c44da6a | Guido Trotter | " modifying an existing nic")
|
7125 | 5c44da6a | Guido Trotter | |
7126 | 24991749 | Iustin Pop | if nic_addremove > 1: |
7127 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Only one NIC add or remove operation" |
7128 | 24991749 | Iustin Pop | " supported at a time")
|
7129 | 24991749 | Iustin Pop | |
7130 | 1a5c7281 | Guido Trotter | def ExpandNames(self): |
7131 | 1a5c7281 | Guido Trotter | self._ExpandAndLockInstance()
|
7132 | 74409b12 | Iustin Pop | self.needed_locks[locking.LEVEL_NODE] = []
|
7133 | 74409b12 | Iustin Pop | self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
7134 | 74409b12 | Iustin Pop | |
7135 | 74409b12 | Iustin Pop | def DeclareLocks(self, level): |
7136 | 74409b12 | Iustin Pop | if level == locking.LEVEL_NODE:
|
7137 | 74409b12 | Iustin Pop | self._LockInstancesNodes()
|
7138 | a8083063 | Iustin Pop | |
7139 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
7140 | a8083063 | Iustin Pop | """Build hooks env.
|
7141 | a8083063 | Iustin Pop |
|
7142 | a8083063 | Iustin Pop | This runs on the master, primary and secondaries.
|
7143 | a8083063 | Iustin Pop |
|
7144 | a8083063 | Iustin Pop | """
|
7145 | 396e1b78 | Michael Hanselmann | args = dict()
|
7146 | 338e51e8 | Iustin Pop | if constants.BE_MEMORY in self.be_new: |
7147 | 338e51e8 | Iustin Pop | args['memory'] = self.be_new[constants.BE_MEMORY] |
7148 | 338e51e8 | Iustin Pop | if constants.BE_VCPUS in self.be_new: |
7149 | 61be6ba4 | Iustin Pop | args['vcpus'] = self.be_new[constants.BE_VCPUS] |
7150 | d8dcf3c9 | Guido Trotter | # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
|
7151 | d8dcf3c9 | Guido Trotter | # information at all.
|
7152 | d8dcf3c9 | Guido Trotter | if self.op.nics: |
7153 | d8dcf3c9 | Guido Trotter | args['nics'] = []
|
7154 | d8dcf3c9 | Guido Trotter | nic_override = dict(self.op.nics) |
7155 | 62f0dd02 | Guido Trotter | c_nicparams = self.cluster.nicparams[constants.PP_DEFAULT]
|
7156 | d8dcf3c9 | Guido Trotter | for idx, nic in enumerate(self.instance.nics): |
7157 | d8dcf3c9 | Guido Trotter | if idx in nic_override: |
7158 | d8dcf3c9 | Guido Trotter | this_nic_override = nic_override[idx] |
7159 | d8dcf3c9 | Guido Trotter | else:
|
7160 | d8dcf3c9 | Guido Trotter | this_nic_override = {} |
7161 | d8dcf3c9 | Guido Trotter | if 'ip' in this_nic_override: |
7162 | d8dcf3c9 | Guido Trotter | ip = this_nic_override['ip']
|
7163 | d8dcf3c9 | Guido Trotter | else:
|
7164 | d8dcf3c9 | Guido Trotter | ip = nic.ip |
7165 | d8dcf3c9 | Guido Trotter | if 'mac' in this_nic_override: |
7166 | d8dcf3c9 | Guido Trotter | mac = this_nic_override['mac']
|
7167 | d8dcf3c9 | Guido Trotter | else:
|
7168 | d8dcf3c9 | Guido Trotter | mac = nic.mac |
7169 | 62f0dd02 | Guido Trotter | if idx in self.nic_pnew: |
7170 | 62f0dd02 | Guido Trotter | nicparams = self.nic_pnew[idx]
|
7171 | 62f0dd02 | Guido Trotter | else:
|
7172 | 62f0dd02 | Guido Trotter | nicparams = objects.FillDict(c_nicparams, nic.nicparams) |
7173 | 62f0dd02 | Guido Trotter | mode = nicparams[constants.NIC_MODE] |
7174 | 62f0dd02 | Guido Trotter | link = nicparams[constants.NIC_LINK] |
7175 | 62f0dd02 | Guido Trotter | args['nics'].append((ip, mac, mode, link))
|
7176 | d8dcf3c9 | Guido Trotter | if constants.DDM_ADD in nic_override: |
7177 | d8dcf3c9 | Guido Trotter | ip = nic_override[constants.DDM_ADD].get('ip', None) |
7178 | d8dcf3c9 | Guido Trotter | mac = nic_override[constants.DDM_ADD]['mac']
|
7179 | 62f0dd02 | Guido Trotter | nicparams = self.nic_pnew[constants.DDM_ADD]
|
7180 | 62f0dd02 | Guido Trotter | mode = nicparams[constants.NIC_MODE] |
7181 | 62f0dd02 | Guido Trotter | link = nicparams[constants.NIC_LINK] |
7182 | 62f0dd02 | Guido Trotter | args['nics'].append((ip, mac, mode, link))
|
7183 | d8dcf3c9 | Guido Trotter | elif constants.DDM_REMOVE in nic_override: |
7184 | d8dcf3c9 | Guido Trotter | del args['nics'][-1] |
7185 | d8dcf3c9 | Guido Trotter | |
7186 | 338e51e8 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self, self.instance, override=args) |
7187 | 6b12959c | Iustin Pop | nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes) |
7188 | a8083063 | Iustin Pop | return env, nl, nl
|
7189 | a8083063 | Iustin Pop | |
7190 | 0329617a | Guido Trotter | def _GetUpdatedParams(self, old_params, update_dict, |
7191 | 0329617a | Guido Trotter | default_values, parameter_types): |
7192 | 0329617a | Guido Trotter | """Return the new params dict for the given params.
|
7193 | 0329617a | Guido Trotter |
|
7194 | 0329617a | Guido Trotter | @type old_params: dict
|
7195 | f2fd87d7 | Iustin Pop | @param old_params: old parameters
|
7196 | 0329617a | Guido Trotter | @type update_dict: dict
|
7197 | f2fd87d7 | Iustin Pop | @param update_dict: dict containing new parameter values,
|
7198 | f2fd87d7 | Iustin Pop | or constants.VALUE_DEFAULT to reset the
|
7199 | f2fd87d7 | Iustin Pop | parameter to its default value
|
7200 | 0329617a | Guido Trotter | @type default_values: dict
|
7201 | 0329617a | Guido Trotter | @param default_values: default values for the filled parameters
|
7202 | 0329617a | Guido Trotter | @type parameter_types: dict
|
7203 | 0329617a | Guido Trotter | @param parameter_types: dict mapping target dict keys to types
|
7204 | 0329617a | Guido Trotter | in constants.ENFORCEABLE_TYPES
|
7205 | 0329617a | Guido Trotter | @rtype: (dict, dict)
|
7206 | 0329617a | Guido Trotter | @return: (new_parameters, filled_parameters)
|
7207 | 0329617a | Guido Trotter |
|
7208 | 0329617a | Guido Trotter | """
|
7209 | 0329617a | Guido Trotter | params_copy = copy.deepcopy(old_params) |
7210 | 0329617a | Guido Trotter | for key, val in update_dict.iteritems(): |
7211 | 0329617a | Guido Trotter | if val == constants.VALUE_DEFAULT:
|
7212 | 0329617a | Guido Trotter | try:
|
7213 | 0329617a | Guido Trotter | del params_copy[key]
|
7214 | 0329617a | Guido Trotter | except KeyError: |
7215 | 0329617a | Guido Trotter | pass
|
7216 | 0329617a | Guido Trotter | else:
|
7217 | 0329617a | Guido Trotter | params_copy[key] = val |
7218 | 0329617a | Guido Trotter | utils.ForceDictType(params_copy, parameter_types) |
7219 | 0329617a | Guido Trotter | params_filled = objects.FillDict(default_values, params_copy) |
7220 | 0329617a | Guido Trotter | return (params_copy, params_filled)
|
7221 | 0329617a | Guido Trotter | |
7222 | a8083063 | Iustin Pop | def CheckPrereq(self): |
7223 | a8083063 | Iustin Pop | """Check prerequisites.
|
7224 | a8083063 | Iustin Pop |
|
7225 | a8083063 | Iustin Pop | This only checks the instance list against the existing names.
|
7226 | a8083063 | Iustin Pop |
|
7227 | a8083063 | Iustin Pop | """
|
7228 | 7c4d6c7b | Michael Hanselmann | self.force = self.op.force |
7229 | a8083063 | Iustin Pop | |
7230 | 74409b12 | Iustin Pop | # checking the new params on the primary/secondary nodes
|
7231 | 31a853d2 | Iustin Pop | |
7232 | cfefe007 | Guido Trotter | instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name) |
7233 | 2ee88aeb | Guido Trotter | cluster = self.cluster = self.cfg.GetClusterInfo() |
7234 | 1a5c7281 | Guido Trotter | assert self.instance is not None, \ |
7235 | 1a5c7281 | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
7236 | 6b12959c | Iustin Pop | pnode = instance.primary_node |
7237 | 6b12959c | Iustin Pop | nodelist = list(instance.all_nodes)
|
7238 | 74409b12 | Iustin Pop | |
7239 | 338e51e8 | Iustin Pop | # hvparams processing
|
7240 | 74409b12 | Iustin Pop | if self.op.hvparams: |
7241 | 0329617a | Guido Trotter | i_hvdict, hv_new = self._GetUpdatedParams(
|
7242 | 0329617a | Guido Trotter | instance.hvparams, self.op.hvparams,
|
7243 | 0329617a | Guido Trotter | cluster.hvparams[instance.hypervisor], |
7244 | 0329617a | Guido Trotter | constants.HVS_PARAMETER_TYPES) |
7245 | 74409b12 | Iustin Pop | # local check
|
7246 | 74409b12 | Iustin Pop | hypervisor.GetHypervisor( |
7247 | 74409b12 | Iustin Pop | instance.hypervisor).CheckParameterSyntax(hv_new) |
7248 | 74409b12 | Iustin Pop | _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
|
7249 | 338e51e8 | Iustin Pop | self.hv_new = hv_new # the new actual values |
7250 | 338e51e8 | Iustin Pop | self.hv_inst = i_hvdict # the new dict (without defaults) |
7251 | 338e51e8 | Iustin Pop | else:
|
7252 | 338e51e8 | Iustin Pop | self.hv_new = self.hv_inst = {} |
7253 | 338e51e8 | Iustin Pop | |
7254 | 338e51e8 | Iustin Pop | # beparams processing
|
7255 | 338e51e8 | Iustin Pop | if self.op.beparams: |
7256 | 0329617a | Guido Trotter | i_bedict, be_new = self._GetUpdatedParams(
|
7257 | 0329617a | Guido Trotter | instance.beparams, self.op.beparams,
|
7258 | 0329617a | Guido Trotter | cluster.beparams[constants.PP_DEFAULT], |
7259 | 0329617a | Guido Trotter | constants.BES_PARAMETER_TYPES) |
7260 | 338e51e8 | Iustin Pop | self.be_new = be_new # the new actual values |
7261 | 338e51e8 | Iustin Pop | self.be_inst = i_bedict # the new dict (without defaults) |
7262 | 338e51e8 | Iustin Pop | else:
|
7263 | b637ae4d | Iustin Pop | self.be_new = self.be_inst = {} |
7264 | 74409b12 | Iustin Pop | |
7265 | cfefe007 | Guido Trotter | self.warn = []
|
7266 | 647a5d80 | Iustin Pop | |
7267 | 338e51e8 | Iustin Pop | if constants.BE_MEMORY in self.op.beparams and not self.force: |
7268 | 647a5d80 | Iustin Pop | mem_check_list = [pnode] |
7269 | c0f2b229 | Iustin Pop | if be_new[constants.BE_AUTO_BALANCE]:
|
7270 | c0f2b229 | Iustin Pop | # either we changed auto_balance to yes or it was from before
|
7271 | 647a5d80 | Iustin Pop | mem_check_list.extend(instance.secondary_nodes) |
7272 | 72737a7f | Iustin Pop | instance_info = self.rpc.call_instance_info(pnode, instance.name,
|
7273 | 72737a7f | Iustin Pop | instance.hypervisor) |
7274 | 647a5d80 | Iustin Pop | nodeinfo = self.rpc.call_node_info(mem_check_list, self.cfg.GetVGName(), |
7275 | 72737a7f | Iustin Pop | instance.hypervisor) |
7276 | 070e998b | Iustin Pop | pninfo = nodeinfo[pnode] |
7277 | 4c4e4e1e | Iustin Pop | msg = pninfo.fail_msg |
7278 | 070e998b | Iustin Pop | if msg:
|
7279 | cfefe007 | Guido Trotter | # Assume the primary node is unreachable and go ahead
|
7280 | 070e998b | Iustin Pop | self.warn.append("Can't get info from primary node %s: %s" % |
7281 | 070e998b | Iustin Pop | (pnode, msg)) |
7282 | 070e998b | Iustin Pop | elif not isinstance(pninfo.payload.get('memory_free', None), int): |
7283 | 070e998b | Iustin Pop | self.warn.append("Node data from primary node %s doesn't contain" |
7284 | 070e998b | Iustin Pop | " free memory information" % pnode)
|
7285 | 4c4e4e1e | Iustin Pop | elif instance_info.fail_msg:
|
7286 | 7ad1af4a | Iustin Pop | self.warn.append("Can't get instance runtime information: %s" % |
7287 | 4c4e4e1e | Iustin Pop | instance_info.fail_msg) |
7288 | cfefe007 | Guido Trotter | else:
|
7289 | 7ad1af4a | Iustin Pop | if instance_info.payload:
|
7290 | 7ad1af4a | Iustin Pop | current_mem = int(instance_info.payload['memory']) |
7291 | cfefe007 | Guido Trotter | else:
|
7292 | cfefe007 | Guido Trotter | # Assume instance not running
|
7293 | cfefe007 | Guido Trotter | # (there is a slight race condition here, but it's not very probable,
|
7294 | cfefe007 | Guido Trotter | # and we have no other way to check)
|
7295 | cfefe007 | Guido Trotter | current_mem = 0
|
7296 | 338e51e8 | Iustin Pop | miss_mem = (be_new[constants.BE_MEMORY] - current_mem - |
7297 | 070e998b | Iustin Pop | pninfo.payload['memory_free'])
|
7298 | cfefe007 | Guido Trotter | if miss_mem > 0: |
7299 | cfefe007 | Guido Trotter | raise errors.OpPrereqError("This change will prevent the instance" |
7300 | cfefe007 | Guido Trotter | " from starting, due to %d MB of memory"
|
7301 | cfefe007 | Guido Trotter | " missing on its primary node" % miss_mem)
|
7302 | cfefe007 | Guido Trotter | |
7303 | c0f2b229 | Iustin Pop | if be_new[constants.BE_AUTO_BALANCE]:
|
7304 | 070e998b | Iustin Pop | for node, nres in nodeinfo.items(): |
7305 | ea33068f | Iustin Pop | if node not in instance.secondary_nodes: |
7306 | ea33068f | Iustin Pop | continue
|
7307 | 4c4e4e1e | Iustin Pop | msg = nres.fail_msg |
7308 | 070e998b | Iustin Pop | if msg:
|
7309 | 070e998b | Iustin Pop | self.warn.append("Can't get info from secondary node %s: %s" % |
7310 | 070e998b | Iustin Pop | (node, msg)) |
7311 | 070e998b | Iustin Pop | elif not isinstance(nres.payload.get('memory_free', None), int): |
7312 | 070e998b | Iustin Pop | self.warn.append("Secondary node %s didn't return free" |
7313 | 070e998b | Iustin Pop | " memory information" % node)
|
7314 | 070e998b | Iustin Pop | elif be_new[constants.BE_MEMORY] > nres.payload['memory_free']: |
7315 | 647a5d80 | Iustin Pop | self.warn.append("Not enough memory to failover instance to" |
7316 | 647a5d80 | Iustin Pop | " secondary node %s" % node)
|
7317 | 5bc84f33 | Alexander Schreiber | |
7318 | 24991749 | Iustin Pop | # NIC processing
|
7319 | cd098c41 | Guido Trotter | self.nic_pnew = {}
|
7320 | cd098c41 | Guido Trotter | self.nic_pinst = {}
|
7321 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
7322 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
7323 | 24991749 | Iustin Pop | if not instance.nics: |
7324 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Instance has no NICs, cannot remove") |
7325 | 24991749 | Iustin Pop | continue
|
7326 | 24991749 | Iustin Pop | if nic_op != constants.DDM_ADD:
|
7327 | 24991749 | Iustin Pop | # an existing nic
|
7328 | 24991749 | Iustin Pop | if nic_op < 0 or nic_op >= len(instance.nics): |
7329 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid NIC index %s, valid values" |
7330 | 24991749 | Iustin Pop | " are 0 to %d" %
|
7331 | 24991749 | Iustin Pop | (nic_op, len(instance.nics)))
|
7332 | cd098c41 | Guido Trotter | old_nic_params = instance.nics[nic_op].nicparams |
7333 | cd098c41 | Guido Trotter | old_nic_ip = instance.nics[nic_op].ip |
7334 | cd098c41 | Guido Trotter | else:
|
7335 | cd098c41 | Guido Trotter | old_nic_params = {} |
7336 | cd098c41 | Guido Trotter | old_nic_ip = None
|
7337 | cd098c41 | Guido Trotter | |
7338 | cd098c41 | Guido Trotter | update_params_dict = dict([(key, nic_dict[key])
|
7339 | cd098c41 | Guido Trotter | for key in constants.NICS_PARAMETERS |
7340 | cd098c41 | Guido Trotter | if key in nic_dict]) |
7341 | cd098c41 | Guido Trotter | |
7342 | 5c44da6a | Guido Trotter | if 'bridge' in nic_dict: |
7343 | cd098c41 | Guido Trotter | update_params_dict[constants.NIC_LINK] = nic_dict['bridge']
|
7344 | cd098c41 | Guido Trotter | |
7345 | cd098c41 | Guido Trotter | new_nic_params, new_filled_nic_params = \ |
7346 | cd098c41 | Guido Trotter | self._GetUpdatedParams(old_nic_params, update_params_dict,
|
7347 | cd098c41 | Guido Trotter | cluster.nicparams[constants.PP_DEFAULT], |
7348 | cd098c41 | Guido Trotter | constants.NICS_PARAMETER_TYPES) |
7349 | cd098c41 | Guido Trotter | objects.NIC.CheckParameterSyntax(new_filled_nic_params) |
7350 | cd098c41 | Guido Trotter | self.nic_pinst[nic_op] = new_nic_params
|
7351 | cd098c41 | Guido Trotter | self.nic_pnew[nic_op] = new_filled_nic_params
|
7352 | cd098c41 | Guido Trotter | new_nic_mode = new_filled_nic_params[constants.NIC_MODE] |
7353 | cd098c41 | Guido Trotter | |
7354 | cd098c41 | Guido Trotter | if new_nic_mode == constants.NIC_MODE_BRIDGED:
|
7355 | cd098c41 | Guido Trotter | nic_bridge = new_filled_nic_params[constants.NIC_LINK] |
7356 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_bridges_exist(pnode, [nic_bridge]).fail_msg
|
7357 | 35c0c8da | Iustin Pop | if msg:
|
7358 | 35c0c8da | Iustin Pop | msg = "Error checking bridges on node %s: %s" % (pnode, msg)
|
7359 | 24991749 | Iustin Pop | if self.force: |
7360 | 24991749 | Iustin Pop | self.warn.append(msg)
|
7361 | 24991749 | Iustin Pop | else:
|
7362 | 24991749 | Iustin Pop | raise errors.OpPrereqError(msg)
|
7363 | cd098c41 | Guido Trotter | if new_nic_mode == constants.NIC_MODE_ROUTED:
|
7364 | cd098c41 | Guido Trotter | if 'ip' in nic_dict: |
7365 | cd098c41 | Guido Trotter | nic_ip = nic_dict['ip']
|
7366 | cd098c41 | Guido Trotter | else:
|
7367 | cd098c41 | Guido Trotter | nic_ip = old_nic_ip |
7368 | cd098c41 | Guido Trotter | if nic_ip is None: |
7369 | cd098c41 | Guido Trotter | raise errors.OpPrereqError('Cannot set the nic ip to None' |
7370 | cd098c41 | Guido Trotter | ' on a routed nic')
|
7371 | 5c44da6a | Guido Trotter | if 'mac' in nic_dict: |
7372 | 5c44da6a | Guido Trotter | nic_mac = nic_dict['mac']
|
7373 | 5c44da6a | Guido Trotter | if nic_mac is None: |
7374 | 5c44da6a | Guido Trotter | raise errors.OpPrereqError('Cannot set the nic mac to None') |
7375 | 5c44da6a | Guido Trotter | elif nic_mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE): |
7376 | 5c44da6a | Guido Trotter | # otherwise generate the mac
|
7377 | 5c44da6a | Guido Trotter | nic_dict['mac'] = self.cfg.GenerateMAC() |
7378 | 5c44da6a | Guido Trotter | else:
|
7379 | 5c44da6a | Guido Trotter | # or validate/reserve the current one
|
7380 | 5c44da6a | Guido Trotter | if self.cfg.IsMacInUse(nic_mac): |
7381 | 5c44da6a | Guido Trotter | raise errors.OpPrereqError("MAC address %s already in use" |
7382 | 5c44da6a | Guido Trotter | " in cluster" % nic_mac)
|
7383 | 24991749 | Iustin Pop | |
7384 | 24991749 | Iustin Pop | # DISK processing
|
7385 | 24991749 | Iustin Pop | if self.op.disks and instance.disk_template == constants.DT_DISKLESS: |
7386 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Disk operations not supported for" |
7387 | 24991749 | Iustin Pop | " diskless instances")
|
7388 | 24991749 | Iustin Pop | for disk_op, disk_dict in self.op.disks: |
7389 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
7390 | 24991749 | Iustin Pop | if len(instance.disks) == 1: |
7391 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Cannot remove the last disk of" |
7392 | 24991749 | Iustin Pop | " an instance")
|
7393 | 24991749 | Iustin Pop | ins_l = self.rpc.call_instance_list([pnode], [instance.hypervisor])
|
7394 | 24991749 | Iustin Pop | ins_l = ins_l[pnode] |
7395 | 4c4e4e1e | Iustin Pop | msg = ins_l.fail_msg |
7396 | aca13712 | Iustin Pop | if msg:
|
7397 | aca13712 | Iustin Pop | raise errors.OpPrereqError("Can't contact node %s: %s" % |
7398 | aca13712 | Iustin Pop | (pnode, msg)) |
7399 | aca13712 | Iustin Pop | if instance.name in ins_l.payload: |
7400 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Instance is running, can't remove" |
7401 | 24991749 | Iustin Pop | " disks.")
|
7402 | 24991749 | Iustin Pop | |
7403 | 24991749 | Iustin Pop | if (disk_op == constants.DDM_ADD and |
7404 | 24991749 | Iustin Pop | len(instance.nics) >= constants.MAX_DISKS):
|
7405 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Instance has too many disks (%d), cannot" |
7406 | 24991749 | Iustin Pop | " add more" % constants.MAX_DISKS)
|
7407 | 24991749 | Iustin Pop | if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE): |
7408 | 24991749 | Iustin Pop | # an existing disk
|
7409 | 24991749 | Iustin Pop | if disk_op < 0 or disk_op >= len(instance.disks): |
7410 | 24991749 | Iustin Pop | raise errors.OpPrereqError("Invalid disk index %s, valid values" |
7411 | 24991749 | Iustin Pop | " are 0 to %d" %
|
7412 | 24991749 | Iustin Pop | (disk_op, len(instance.disks)))
|
7413 | 24991749 | Iustin Pop | |
7414 | a8083063 | Iustin Pop | return
|
7415 | a8083063 | Iustin Pop | |
7416 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
7417 | a8083063 | Iustin Pop | """Modifies an instance.
|
7418 | a8083063 | Iustin Pop |
|
7419 | a8083063 | Iustin Pop | All parameters take effect only at the next restart of the instance.
|
7420 | 24991749 | Iustin Pop |
|
7421 | a8083063 | Iustin Pop | """
|
7422 | cfefe007 | Guido Trotter | # Process here the warnings from CheckPrereq, as we don't have a
|
7423 | cfefe007 | Guido Trotter | # feedback_fn there.
|
7424 | cfefe007 | Guido Trotter | for warn in self.warn: |
7425 | cfefe007 | Guido Trotter | feedback_fn("WARNING: %s" % warn)
|
7426 | cfefe007 | Guido Trotter | |
7427 | a8083063 | Iustin Pop | result = [] |
7428 | a8083063 | Iustin Pop | instance = self.instance
|
7429 | cd098c41 | Guido Trotter | cluster = self.cluster
|
7430 | 24991749 | Iustin Pop | # disk changes
|
7431 | 24991749 | Iustin Pop | for disk_op, disk_dict in self.op.disks: |
7432 | 24991749 | Iustin Pop | if disk_op == constants.DDM_REMOVE:
|
7433 | 24991749 | Iustin Pop | # remove the last disk
|
7434 | 24991749 | Iustin Pop | device = instance.disks.pop() |
7435 | 24991749 | Iustin Pop | device_idx = len(instance.disks)
|
7436 | 24991749 | Iustin Pop | for node, disk in device.ComputeNodeTree(instance.primary_node): |
7437 | 24991749 | Iustin Pop | self.cfg.SetDiskID(disk, node)
|
7438 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
|
7439 | e1bc0878 | Iustin Pop | if msg:
|
7440 | e1bc0878 | Iustin Pop | self.LogWarning("Could not remove disk/%d on node %s: %s," |
7441 | e1bc0878 | Iustin Pop | " continuing anyway", device_idx, node, msg)
|
7442 | 24991749 | Iustin Pop | result.append(("disk/%d" % device_idx, "remove")) |
7443 | 24991749 | Iustin Pop | elif disk_op == constants.DDM_ADD:
|
7444 | 24991749 | Iustin Pop | # add a new disk
|
7445 | 24991749 | Iustin Pop | if instance.disk_template == constants.DT_FILE:
|
7446 | 24991749 | Iustin Pop | file_driver, file_path = instance.disks[0].logical_id
|
7447 | 24991749 | Iustin Pop | file_path = os.path.dirname(file_path) |
7448 | 24991749 | Iustin Pop | else:
|
7449 | 24991749 | Iustin Pop | file_driver = file_path = None
|
7450 | 24991749 | Iustin Pop | disk_idx_base = len(instance.disks)
|
7451 | 24991749 | Iustin Pop | new_disk = _GenerateDiskTemplate(self,
|
7452 | 24991749 | Iustin Pop | instance.disk_template, |
7453 | 32388e6d | Iustin Pop | instance.name, instance.primary_node, |
7454 | 24991749 | Iustin Pop | instance.secondary_nodes, |
7455 | 24991749 | Iustin Pop | [disk_dict], |
7456 | 24991749 | Iustin Pop | file_path, |
7457 | 24991749 | Iustin Pop | file_driver, |
7458 | 24991749 | Iustin Pop | disk_idx_base)[0]
|
7459 | 24991749 | Iustin Pop | instance.disks.append(new_disk) |
7460 | 24991749 | Iustin Pop | info = _GetInstanceInfoText(instance) |
7461 | 24991749 | Iustin Pop | |
7462 | 24991749 | Iustin Pop | logging.info("Creating volume %s for instance %s",
|
7463 | 24991749 | Iustin Pop | new_disk.iv_name, instance.name) |
7464 | 24991749 | Iustin Pop | # Note: this needs to be kept in sync with _CreateDisks
|
7465 | 24991749 | Iustin Pop | #HARDCODE
|
7466 | 428958aa | Iustin Pop | for node in instance.all_nodes: |
7467 | 428958aa | Iustin Pop | f_create = node == instance.primary_node |
7468 | 796cab27 | Iustin Pop | try:
|
7469 | 428958aa | Iustin Pop | _CreateBlockDev(self, node, instance, new_disk,
|
7470 | 428958aa | Iustin Pop | f_create, info, f_create) |
7471 | 1492cca7 | Iustin Pop | except errors.OpExecError, err:
|
7472 | 24991749 | Iustin Pop | self.LogWarning("Failed to create volume %s (%s) on" |
7473 | 428958aa | Iustin Pop | " node %s: %s",
|
7474 | 428958aa | Iustin Pop | new_disk.iv_name, new_disk, node, err) |
7475 | 24991749 | Iustin Pop | result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" % |
7476 | 24991749 | Iustin Pop | (new_disk.size, new_disk.mode))) |
7477 | 24991749 | Iustin Pop | else:
|
7478 | 24991749 | Iustin Pop | # change a given disk
|
7479 | 24991749 | Iustin Pop | instance.disks[disk_op].mode = disk_dict['mode']
|
7480 | 24991749 | Iustin Pop | result.append(("disk.mode/%d" % disk_op, disk_dict['mode'])) |
7481 | 24991749 | Iustin Pop | # NIC changes
|
7482 | 24991749 | Iustin Pop | for nic_op, nic_dict in self.op.nics: |
7483 | 24991749 | Iustin Pop | if nic_op == constants.DDM_REMOVE:
|
7484 | 24991749 | Iustin Pop | # remove the last nic
|
7485 | 24991749 | Iustin Pop | del instance.nics[-1] |
7486 | 24991749 | Iustin Pop | result.append(("nic.%d" % len(instance.nics), "remove")) |
7487 | 24991749 | Iustin Pop | elif nic_op == constants.DDM_ADD:
|
7488 | 5c44da6a | Guido Trotter | # mac and bridge should be set, by now
|
7489 | 5c44da6a | Guido Trotter | mac = nic_dict['mac']
|
7490 | cd098c41 | Guido Trotter | ip = nic_dict.get('ip', None) |
7491 | cd098c41 | Guido Trotter | nicparams = self.nic_pinst[constants.DDM_ADD]
|
7492 | cd098c41 | Guido Trotter | new_nic = objects.NIC(mac=mac, ip=ip, nicparams=nicparams) |
7493 | 24991749 | Iustin Pop | instance.nics.append(new_nic) |
7494 | 24991749 | Iustin Pop | result.append(("nic.%d" % (len(instance.nics) - 1), |
7495 | cd098c41 | Guido Trotter | "add:mac=%s,ip=%s,mode=%s,link=%s" %
|
7496 | cd098c41 | Guido Trotter | (new_nic.mac, new_nic.ip, |
7497 | cd098c41 | Guido Trotter | self.nic_pnew[constants.DDM_ADD][constants.NIC_MODE],
|
7498 | cd098c41 | Guido Trotter | self.nic_pnew[constants.DDM_ADD][constants.NIC_LINK]
|
7499 | cd098c41 | Guido Trotter | ))) |
7500 | 24991749 | Iustin Pop | else:
|
7501 | cd098c41 | Guido Trotter | for key in 'mac', 'ip': |
7502 | 24991749 | Iustin Pop | if key in nic_dict: |
7503 | 24991749 | Iustin Pop | setattr(instance.nics[nic_op], key, nic_dict[key])
|
7504 | cd098c41 | Guido Trotter | if nic_op in self.nic_pnew: |
7505 | cd098c41 | Guido Trotter | instance.nics[nic_op].nicparams = self.nic_pnew[nic_op]
|
7506 | cd098c41 | Guido Trotter | for key, val in nic_dict.iteritems(): |
7507 | cd098c41 | Guido Trotter | result.append(("nic.%s/%d" % (key, nic_op), val))
|
7508 | 24991749 | Iustin Pop | |
7509 | 24991749 | Iustin Pop | # hvparams changes
|
7510 | 74409b12 | Iustin Pop | if self.op.hvparams: |
7511 | 12649e35 | Guido Trotter | instance.hvparams = self.hv_inst
|
7512 | 74409b12 | Iustin Pop | for key, val in self.op.hvparams.iteritems(): |
7513 | 74409b12 | Iustin Pop | result.append(("hv/%s" % key, val))
|
7514 | 24991749 | Iustin Pop | |
7515 | 24991749 | Iustin Pop | # beparams changes
|
7516 | 338e51e8 | Iustin Pop | if self.op.beparams: |
7517 | 338e51e8 | Iustin Pop | instance.beparams = self.be_inst
|
7518 | 338e51e8 | Iustin Pop | for key, val in self.op.beparams.iteritems(): |
7519 | 338e51e8 | Iustin Pop | result.append(("be/%s" % key, val))
|
7520 | a8083063 | Iustin Pop | |
7521 | ea94e1cd | Guido Trotter | self.cfg.Update(instance)
|
7522 | a8083063 | Iustin Pop | |
7523 | a8083063 | Iustin Pop | return result
|
7524 | a8083063 | Iustin Pop | |
7525 | a8083063 | Iustin Pop | |
7526 | a8083063 | Iustin Pop | class LUQueryExports(NoHooksLU): |
7527 | a8083063 | Iustin Pop | """Query the exports list
|
7528 | a8083063 | Iustin Pop |
|
7529 | a8083063 | Iustin Pop | """
|
7530 | 895ecd9c | Guido Trotter | _OP_REQP = ['nodes']
|
7531 | 21a15682 | Guido Trotter | REQ_BGL = False
|
7532 | 21a15682 | Guido Trotter | |
7533 | 21a15682 | Guido Trotter | def ExpandNames(self): |
7534 | 21a15682 | Guido Trotter | self.needed_locks = {}
|
7535 | 21a15682 | Guido Trotter | self.share_locks[locking.LEVEL_NODE] = 1 |
7536 | 21a15682 | Guido Trotter | if not self.op.nodes: |
7537 | e310b019 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
7538 | 21a15682 | Guido Trotter | else:
|
7539 | 21a15682 | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = \
|
7540 | 21a15682 | Guido Trotter | _GetWantedNodes(self, self.op.nodes) |
7541 | a8083063 | Iustin Pop | |
7542 | a8083063 | Iustin Pop | def CheckPrereq(self): |
7543 | 21a15682 | Guido Trotter | """Check prerequisites.
|
7544 | a8083063 | Iustin Pop |
|
7545 | a8083063 | Iustin Pop | """
|
7546 | 21a15682 | Guido Trotter | self.nodes = self.acquired_locks[locking.LEVEL_NODE] |
7547 | a8083063 | Iustin Pop | |
7548 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
7549 | a8083063 | Iustin Pop | """Compute the list of all the exported system images.
|
7550 | a8083063 | Iustin Pop |
|
7551 | e4376078 | Iustin Pop | @rtype: dict
|
7552 | e4376078 | Iustin Pop | @return: a dictionary with the structure node->(export-list)
|
7553 | e4376078 | Iustin Pop | where export-list is a list of the instances exported on
|
7554 | e4376078 | Iustin Pop | that node.
|
7555 | a8083063 | Iustin Pop |
|
7556 | a8083063 | Iustin Pop | """
|
7557 | b04285f2 | Guido Trotter | rpcresult = self.rpc.call_export_list(self.nodes) |
7558 | b04285f2 | Guido Trotter | result = {} |
7559 | b04285f2 | Guido Trotter | for node in rpcresult: |
7560 | 4c4e4e1e | Iustin Pop | if rpcresult[node].fail_msg:
|
7561 | b04285f2 | Guido Trotter | result[node] = False
|
7562 | b04285f2 | Guido Trotter | else:
|
7563 | 1b7bfbb7 | Iustin Pop | result[node] = rpcresult[node].payload |
7564 | b04285f2 | Guido Trotter | |
7565 | b04285f2 | Guido Trotter | return result
|
7566 | a8083063 | Iustin Pop | |
7567 | a8083063 | Iustin Pop | |
7568 | a8083063 | Iustin Pop | class LUExportInstance(LogicalUnit): |
7569 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
7570 | a8083063 | Iustin Pop |
|
7571 | a8083063 | Iustin Pop | """
|
7572 | a8083063 | Iustin Pop | HPATH = "instance-export"
|
7573 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
7574 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "target_node", "shutdown"] |
7575 | 6657590e | Guido Trotter | REQ_BGL = False
|
7576 | 6657590e | Guido Trotter | |
7577 | 6657590e | Guido Trotter | def ExpandNames(self): |
7578 | 6657590e | Guido Trotter | self._ExpandAndLockInstance()
|
7579 | 6657590e | Guido Trotter | # FIXME: lock only instance primary and destination node
|
7580 | 6657590e | Guido Trotter | #
|
7581 | 6657590e | Guido Trotter | # Sad but true, for now we have do lock all nodes, as we don't know where
|
7582 | 6657590e | Guido Trotter | # the previous export might be, and and in this LU we search for it and
|
7583 | 6657590e | Guido Trotter | # remove it from its current node. In the future we could fix this by:
|
7584 | 6657590e | Guido Trotter | # - making a tasklet to search (share-lock all), then create the new one,
|
7585 | 6657590e | Guido Trotter | # then one to remove, after
|
7586 | 5bbd3f7f | Michael Hanselmann | # - removing the removal operation altogether
|
7587 | 6657590e | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
7588 | 6657590e | Guido Trotter | |
7589 | 6657590e | Guido Trotter | def DeclareLocks(self, level): |
7590 | 6657590e | Guido Trotter | """Last minute lock declaration."""
|
7591 | 6657590e | Guido Trotter | # All nodes are locked anyway, so nothing to do here.
|
7592 | a8083063 | Iustin Pop | |
7593 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
7594 | a8083063 | Iustin Pop | """Build hooks env.
|
7595 | a8083063 | Iustin Pop |
|
7596 | a8083063 | Iustin Pop | This will run on the master, primary node and target node.
|
7597 | a8083063 | Iustin Pop |
|
7598 | a8083063 | Iustin Pop | """
|
7599 | a8083063 | Iustin Pop | env = { |
7600 | a8083063 | Iustin Pop | "EXPORT_NODE": self.op.target_node, |
7601 | a8083063 | Iustin Pop | "EXPORT_DO_SHUTDOWN": self.op.shutdown, |
7602 | a8083063 | Iustin Pop | } |
7603 | 338e51e8 | Iustin Pop | env.update(_BuildInstanceHookEnvByObject(self, self.instance)) |
7604 | d6a02168 | Michael Hanselmann | nl = [self.cfg.GetMasterNode(), self.instance.primary_node, |
7605 | a8083063 | Iustin Pop | self.op.target_node]
|
7606 | a8083063 | Iustin Pop | return env, nl, nl
|
7607 | a8083063 | Iustin Pop | |
7608 | a8083063 | Iustin Pop | def CheckPrereq(self): |
7609 | a8083063 | Iustin Pop | """Check prerequisites.
|
7610 | a8083063 | Iustin Pop |
|
7611 | 9ac99fda | Guido Trotter | This checks that the instance and node names are valid.
|
7612 | a8083063 | Iustin Pop |
|
7613 | a8083063 | Iustin Pop | """
|
7614 | 6657590e | Guido Trotter | instance_name = self.op.instance_name
|
7615 | a8083063 | Iustin Pop | self.instance = self.cfg.GetInstanceInfo(instance_name) |
7616 | 6657590e | Guido Trotter | assert self.instance is not None, \ |
7617 | 6657590e | Guido Trotter | "Cannot retrieve locked instance %s" % self.op.instance_name |
7618 | 43017d26 | Iustin Pop | _CheckNodeOnline(self, self.instance.primary_node) |
7619 | a8083063 | Iustin Pop | |
7620 | 6657590e | Guido Trotter | self.dst_node = self.cfg.GetNodeInfo( |
7621 | 6657590e | Guido Trotter | self.cfg.ExpandNodeName(self.op.target_node)) |
7622 | a8083063 | Iustin Pop | |
7623 | 268b8e42 | Iustin Pop | if self.dst_node is None: |
7624 | 268b8e42 | Iustin Pop | # This is wrong node name, not a non-locked node
|
7625 | 268b8e42 | Iustin Pop | raise errors.OpPrereqError("Wrong node name %s" % self.op.target_node) |
7626 | aeb83a2b | Iustin Pop | _CheckNodeOnline(self, self.dst_node.name) |
7627 | 733a2b6a | Iustin Pop | _CheckNodeNotDrained(self, self.dst_node.name) |
7628 | a8083063 | Iustin Pop | |
7629 | b6023d6c | Manuel Franceschini | # instance disk type verification
|
7630 | b6023d6c | Manuel Franceschini | for disk in self.instance.disks: |
7631 | b6023d6c | Manuel Franceschini | if disk.dev_type == constants.LD_FILE:
|
7632 | b6023d6c | Manuel Franceschini | raise errors.OpPrereqError("Export not supported for instances with" |
7633 | b6023d6c | Manuel Franceschini | " file-based disks")
|
7634 | b6023d6c | Manuel Franceschini | |
7635 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
7636 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
7637 | a8083063 | Iustin Pop |
|
7638 | a8083063 | Iustin Pop | """
|
7639 | a8083063 | Iustin Pop | instance = self.instance
|
7640 | a8083063 | Iustin Pop | dst_node = self.dst_node
|
7641 | a8083063 | Iustin Pop | src_node = instance.primary_node |
7642 | 37972df0 | Michael Hanselmann | |
7643 | a8083063 | Iustin Pop | if self.op.shutdown: |
7644 | fb300fb7 | Guido Trotter | # shutdown the instance, but not the disks
|
7645 | 37972df0 | Michael Hanselmann | feedback_fn("Shutting down instance %s" % instance.name)
|
7646 | 781de953 | Iustin Pop | result = self.rpc.call_instance_shutdown(src_node, instance)
|
7647 | 4c4e4e1e | Iustin Pop | result.Raise("Could not shutdown instance %s on"
|
7648 | 4c4e4e1e | Iustin Pop | " node %s" % (instance.name, src_node))
|
7649 | a8083063 | Iustin Pop | |
7650 | a8083063 | Iustin Pop | vgname = self.cfg.GetVGName()
|
7651 | a8083063 | Iustin Pop | |
7652 | a8083063 | Iustin Pop | snap_disks = [] |
7653 | a8083063 | Iustin Pop | |
7654 | 998c712c | Iustin Pop | # set the disks ID correctly since call_instance_start needs the
|
7655 | 998c712c | Iustin Pop | # correct drbd minor to create the symlinks
|
7656 | 998c712c | Iustin Pop | for disk in instance.disks: |
7657 | 998c712c | Iustin Pop | self.cfg.SetDiskID(disk, src_node)
|
7658 | 998c712c | Iustin Pop | |
7659 | 084f05a5 | Iustin Pop | # per-disk results
|
7660 | 084f05a5 | Iustin Pop | dresults = [] |
7661 | a8083063 | Iustin Pop | try:
|
7662 | a97da6b7 | Iustin Pop | for idx, disk in enumerate(instance.disks): |
7663 | 37972df0 | Michael Hanselmann | feedback_fn("Creating a snapshot of disk/%s on node %s" %
|
7664 | 37972df0 | Michael Hanselmann | (idx, src_node)) |
7665 | 37972df0 | Michael Hanselmann | |
7666 | 87812fd3 | Iustin Pop | # result.payload will be a snapshot of an lvm leaf of the one we passed
|
7667 | 87812fd3 | Iustin Pop | result = self.rpc.call_blockdev_snapshot(src_node, disk)
|
7668 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7669 | 87812fd3 | Iustin Pop | if msg:
|
7670 | af0413bb | Guido Trotter | self.LogWarning("Could not snapshot disk/%s on node %s: %s", |
7671 | af0413bb | Guido Trotter | idx, src_node, msg) |
7672 | 19d7f90a | Guido Trotter | snap_disks.append(False)
|
7673 | 19d7f90a | Guido Trotter | else:
|
7674 | 87812fd3 | Iustin Pop | disk_id = (vgname, result.payload) |
7675 | 19d7f90a | Guido Trotter | new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size, |
7676 | 87812fd3 | Iustin Pop | logical_id=disk_id, physical_id=disk_id, |
7677 | 19d7f90a | Guido Trotter | iv_name=disk.iv_name) |
7678 | 19d7f90a | Guido Trotter | snap_disks.append(new_dev) |
7679 | a8083063 | Iustin Pop | |
7680 | a8083063 | Iustin Pop | finally:
|
7681 | 0d68c45d | Iustin Pop | if self.op.shutdown and instance.admin_up: |
7682 | 37972df0 | Michael Hanselmann | feedback_fn("Starting instance %s" % instance.name)
|
7683 | 0eca8e0c | Iustin Pop | result = self.rpc.call_instance_start(src_node, instance, None, None) |
7684 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7685 | dd279568 | Iustin Pop | if msg:
|
7686 | b9bddb6b | Iustin Pop | _ShutdownInstanceDisks(self, instance)
|
7687 | dd279568 | Iustin Pop | raise errors.OpExecError("Could not start instance: %s" % msg) |
7688 | a8083063 | Iustin Pop | |
7689 | a8083063 | Iustin Pop | # TODO: check for size
|
7690 | a8083063 | Iustin Pop | |
7691 | 62c9ec92 | Iustin Pop | cluster_name = self.cfg.GetClusterName()
|
7692 | 74c47259 | Iustin Pop | for idx, dev in enumerate(snap_disks): |
7693 | 37972df0 | Michael Hanselmann | feedback_fn("Exporting snapshot %s from %s to %s" %
|
7694 | 37972df0 | Michael Hanselmann | (idx, src_node, dst_node.name)) |
7695 | 19d7f90a | Guido Trotter | if dev:
|
7696 | 781de953 | Iustin Pop | result = self.rpc.call_snapshot_export(src_node, dev, dst_node.name,
|
7697 | 781de953 | Iustin Pop | instance, cluster_name, idx) |
7698 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7699 | ba55d062 | Iustin Pop | if msg:
|
7700 | af0413bb | Guido Trotter | self.LogWarning("Could not export disk/%s from node %s to" |
7701 | af0413bb | Guido Trotter | " node %s: %s", idx, src_node, dst_node.name, msg)
|
7702 | 084f05a5 | Iustin Pop | dresults.append(False)
|
7703 | 084f05a5 | Iustin Pop | else:
|
7704 | 084f05a5 | Iustin Pop | dresults.append(True)
|
7705 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_blockdev_remove(src_node, dev).fail_msg
|
7706 | e1bc0878 | Iustin Pop | if msg:
|
7707 | a97da6b7 | Iustin Pop | self.LogWarning("Could not remove snapshot for disk/%d from node" |
7708 | a97da6b7 | Iustin Pop | " %s: %s", idx, src_node, msg)
|
7709 | 084f05a5 | Iustin Pop | else:
|
7710 | 084f05a5 | Iustin Pop | dresults.append(False)
|
7711 | a8083063 | Iustin Pop | |
7712 | 37972df0 | Michael Hanselmann | feedback_fn("Finalizing export on %s" % dst_node.name)
|
7713 | 781de953 | Iustin Pop | result = self.rpc.call_finalize_export(dst_node.name, instance, snap_disks)
|
7714 | 084f05a5 | Iustin Pop | fin_resu = True
|
7715 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7716 | 9b201a0d | Iustin Pop | if msg:
|
7717 | 9b201a0d | Iustin Pop | self.LogWarning("Could not finalize export for instance %s" |
7718 | 9b201a0d | Iustin Pop | " on node %s: %s", instance.name, dst_node.name, msg)
|
7719 | 084f05a5 | Iustin Pop | fin_resu = False
|
7720 | a8083063 | Iustin Pop | |
7721 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
7722 | a8083063 | Iustin Pop | nodelist.remove(dst_node.name) |
7723 | a8083063 | Iustin Pop | |
7724 | a8083063 | Iustin Pop | # on one-node clusters nodelist will be empty after the removal
|
7725 | a8083063 | Iustin Pop | # if we proceed the backup would be removed because OpQueryExports
|
7726 | a8083063 | Iustin Pop | # substitutes an empty list with the full cluster node list.
|
7727 | 35fbcd11 | Iustin Pop | iname = instance.name |
7728 | a8083063 | Iustin Pop | if nodelist:
|
7729 | 37972df0 | Michael Hanselmann | feedback_fn("Removing old exports for instance %s" % iname)
|
7730 | 72737a7f | Iustin Pop | exportlist = self.rpc.call_export_list(nodelist)
|
7731 | a8083063 | Iustin Pop | for node in exportlist: |
7732 | 4c4e4e1e | Iustin Pop | if exportlist[node].fail_msg:
|
7733 | 781de953 | Iustin Pop | continue
|
7734 | 35fbcd11 | Iustin Pop | if iname in exportlist[node].payload: |
7735 | 4c4e4e1e | Iustin Pop | msg = self.rpc.call_export_remove(node, iname).fail_msg
|
7736 | 35fbcd11 | Iustin Pop | if msg:
|
7737 | 19d7f90a | Guido Trotter | self.LogWarning("Could not remove older export for instance %s" |
7738 | 35fbcd11 | Iustin Pop | " on node %s: %s", iname, node, msg)
|
7739 | 084f05a5 | Iustin Pop | return fin_resu, dresults
|
7740 | 5c947f38 | Iustin Pop | |
7741 | 5c947f38 | Iustin Pop | |
7742 | 9ac99fda | Guido Trotter | class LURemoveExport(NoHooksLU): |
7743 | 9ac99fda | Guido Trotter | """Remove exports related to the named instance.
|
7744 | 9ac99fda | Guido Trotter |
|
7745 | 9ac99fda | Guido Trotter | """
|
7746 | 9ac99fda | Guido Trotter | _OP_REQP = ["instance_name"]
|
7747 | 3656b3af | Guido Trotter | REQ_BGL = False
|
7748 | 3656b3af | Guido Trotter | |
7749 | 3656b3af | Guido Trotter | def ExpandNames(self): |
7750 | 3656b3af | Guido Trotter | self.needed_locks = {}
|
7751 | 3656b3af | Guido Trotter | # We need all nodes to be locked in order for RemoveExport to work, but we
|
7752 | 3656b3af | Guido Trotter | # don't need to lock the instance itself, as nothing will happen to it (and
|
7753 | 3656b3af | Guido Trotter | # we can remove exports also for a removed instance)
|
7754 | 3656b3af | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
|
7755 | 9ac99fda | Guido Trotter | |
7756 | 9ac99fda | Guido Trotter | def CheckPrereq(self): |
7757 | 9ac99fda | Guido Trotter | """Check prerequisites.
|
7758 | 9ac99fda | Guido Trotter | """
|
7759 | 9ac99fda | Guido Trotter | pass
|
7760 | 9ac99fda | Guido Trotter | |
7761 | 9ac99fda | Guido Trotter | def Exec(self, feedback_fn): |
7762 | 9ac99fda | Guido Trotter | """Remove any export.
|
7763 | 9ac99fda | Guido Trotter |
|
7764 | 9ac99fda | Guido Trotter | """
|
7765 | 9ac99fda | Guido Trotter | instance_name = self.cfg.ExpandInstanceName(self.op.instance_name) |
7766 | 9ac99fda | Guido Trotter | # If the instance was not found we'll try with the name that was passed in.
|
7767 | 9ac99fda | Guido Trotter | # This will only work if it was an FQDN, though.
|
7768 | 9ac99fda | Guido Trotter | fqdn_warn = False
|
7769 | 9ac99fda | Guido Trotter | if not instance_name: |
7770 | 9ac99fda | Guido Trotter | fqdn_warn = True
|
7771 | 9ac99fda | Guido Trotter | instance_name = self.op.instance_name
|
7772 | 9ac99fda | Guido Trotter | |
7773 | 1b7bfbb7 | Iustin Pop | locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
|
7774 | 1b7bfbb7 | Iustin Pop | exportlist = self.rpc.call_export_list(locked_nodes)
|
7775 | 9ac99fda | Guido Trotter | found = False
|
7776 | 9ac99fda | Guido Trotter | for node in exportlist: |
7777 | 4c4e4e1e | Iustin Pop | msg = exportlist[node].fail_msg |
7778 | 1b7bfbb7 | Iustin Pop | if msg:
|
7779 | 1b7bfbb7 | Iustin Pop | self.LogWarning("Failed to query node %s (continuing): %s", node, msg) |
7780 | 781de953 | Iustin Pop | continue
|
7781 | 1b7bfbb7 | Iustin Pop | if instance_name in exportlist[node].payload: |
7782 | 9ac99fda | Guido Trotter | found = True
|
7783 | 781de953 | Iustin Pop | result = self.rpc.call_export_remove(node, instance_name)
|
7784 | 4c4e4e1e | Iustin Pop | msg = result.fail_msg |
7785 | 35fbcd11 | Iustin Pop | if msg:
|
7786 | 9a4f63d1 | Iustin Pop | logging.error("Could not remove export for instance %s"
|
7787 | 35fbcd11 | Iustin Pop | " on node %s: %s", instance_name, node, msg)
|
7788 | 9ac99fda | Guido Trotter | |
7789 | 9ac99fda | Guido Trotter | if fqdn_warn and not found: |
7790 | 9ac99fda | Guido Trotter | feedback_fn("Export not found. If trying to remove an export belonging"
|
7791 | 9ac99fda | Guido Trotter | " to a deleted instance please use its Fully Qualified"
|
7792 | 9ac99fda | Guido Trotter | " Domain Name.")
|
7793 | 9ac99fda | Guido Trotter | |
7794 | 9ac99fda | Guido Trotter | |
7795 | 5c947f38 | Iustin Pop | class TagsLU(NoHooksLU): |
7796 | 5c947f38 | Iustin Pop | """Generic tags LU.
|
7797 | 5c947f38 | Iustin Pop |
|
7798 | 5c947f38 | Iustin Pop | This is an abstract class which is the parent of all the other tags LUs.
|
7799 | 5c947f38 | Iustin Pop |
|
7800 | 5c947f38 | Iustin Pop | """
|
7801 | 5c947f38 | Iustin Pop | |
7802 | 8646adce | Guido Trotter | def ExpandNames(self): |
7803 | 8646adce | Guido Trotter | self.needed_locks = {}
|
7804 | 8646adce | Guido Trotter | if self.op.kind == constants.TAG_NODE: |
7805 | 5c947f38 | Iustin Pop | name = self.cfg.ExpandNodeName(self.op.name) |
7806 | 5c947f38 | Iustin Pop | if name is None: |
7807 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid node name (%s)" % |
7808 | 3ecf6786 | Iustin Pop | (self.op.name,))
|
7809 | 5c947f38 | Iustin Pop | self.op.name = name
|
7810 | 8646adce | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = name
|
7811 | 5c947f38 | Iustin Pop | elif self.op.kind == constants.TAG_INSTANCE: |
7812 | 8f684e16 | Iustin Pop | name = self.cfg.ExpandInstanceName(self.op.name) |
7813 | 5c947f38 | Iustin Pop | if name is None: |
7814 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid instance name (%s)" % |
7815 | 3ecf6786 | Iustin Pop | (self.op.name,))
|
7816 | 5c947f38 | Iustin Pop | self.op.name = name
|
7817 | 8646adce | Guido Trotter | self.needed_locks[locking.LEVEL_INSTANCE] = name
|
7818 | 8646adce | Guido Trotter | |
7819 | 8646adce | Guido Trotter | def CheckPrereq(self): |
7820 | 8646adce | Guido Trotter | """Check prerequisites.
|
7821 | 8646adce | Guido Trotter |
|
7822 | 8646adce | Guido Trotter | """
|
7823 | 8646adce | Guido Trotter | if self.op.kind == constants.TAG_CLUSTER: |
7824 | 8646adce | Guido Trotter | self.target = self.cfg.GetClusterInfo() |
7825 | 8646adce | Guido Trotter | elif self.op.kind == constants.TAG_NODE: |
7826 | 8646adce | Guido Trotter | self.target = self.cfg.GetNodeInfo(self.op.name) |
7827 | 8646adce | Guido Trotter | elif self.op.kind == constants.TAG_INSTANCE: |
7828 | 8646adce | Guido Trotter | self.target = self.cfg.GetInstanceInfo(self.op.name) |
7829 | 5c947f38 | Iustin Pop | else:
|
7830 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Wrong tag type requested (%s)" % |
7831 | 3ecf6786 | Iustin Pop | str(self.op.kind)) |
7832 | 5c947f38 | Iustin Pop | |
7833 | 5c947f38 | Iustin Pop | |
7834 | 5c947f38 | Iustin Pop | class LUGetTags(TagsLU): |
7835 | 5c947f38 | Iustin Pop | """Returns the tags of a given object.
|
7836 | 5c947f38 | Iustin Pop |
|
7837 | 5c947f38 | Iustin Pop | """
|
7838 | 5c947f38 | Iustin Pop | _OP_REQP = ["kind", "name"] |
7839 | 8646adce | Guido Trotter | REQ_BGL = False
|
7840 | 5c947f38 | Iustin Pop | |
7841 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
7842 | 5c947f38 | Iustin Pop | """Returns the tag list.
|
7843 | 5c947f38 | Iustin Pop |
|
7844 | 5c947f38 | Iustin Pop | """
|
7845 | 5d414478 | Oleksiy Mishchenko | return list(self.target.GetTags()) |
7846 | 5c947f38 | Iustin Pop | |
7847 | 5c947f38 | Iustin Pop | |
7848 | 73415719 | Iustin Pop | class LUSearchTags(NoHooksLU): |
7849 | 73415719 | Iustin Pop | """Searches the tags for a given pattern.
|
7850 | 73415719 | Iustin Pop |
|
7851 | 73415719 | Iustin Pop | """
|
7852 | 73415719 | Iustin Pop | _OP_REQP = ["pattern"]
|
7853 | 8646adce | Guido Trotter | REQ_BGL = False
|
7854 | 8646adce | Guido Trotter | |
7855 | 8646adce | Guido Trotter | def ExpandNames(self): |
7856 | 8646adce | Guido Trotter | self.needed_locks = {}
|
7857 | 73415719 | Iustin Pop | |
7858 | 73415719 | Iustin Pop | def CheckPrereq(self): |
7859 | 73415719 | Iustin Pop | """Check prerequisites.
|
7860 | 73415719 | Iustin Pop |
|
7861 | 73415719 | Iustin Pop | This checks the pattern passed for validity by compiling it.
|
7862 | 73415719 | Iustin Pop |
|
7863 | 73415719 | Iustin Pop | """
|
7864 | 73415719 | Iustin Pop | try:
|
7865 | 73415719 | Iustin Pop | self.re = re.compile(self.op.pattern) |
7866 | 73415719 | Iustin Pop | except re.error, err:
|
7867 | 73415719 | Iustin Pop | raise errors.OpPrereqError("Invalid search pattern '%s': %s" % |
7868 | 73415719 | Iustin Pop | (self.op.pattern, err))
|
7869 | 73415719 | Iustin Pop | |
7870 | 73415719 | Iustin Pop | def Exec(self, feedback_fn): |
7871 | 73415719 | Iustin Pop | """Returns the tag list.
|
7872 | 73415719 | Iustin Pop |
|
7873 | 73415719 | Iustin Pop | """
|
7874 | 73415719 | Iustin Pop | cfg = self.cfg
|
7875 | 73415719 | Iustin Pop | tgts = [("/cluster", cfg.GetClusterInfo())]
|
7876 | 8646adce | Guido Trotter | ilist = cfg.GetAllInstancesInfo().values() |
7877 | 73415719 | Iustin Pop | tgts.extend([("/instances/%s" % i.name, i) for i in ilist]) |
7878 | 8646adce | Guido Trotter | nlist = cfg.GetAllNodesInfo().values() |
7879 | 73415719 | Iustin Pop | tgts.extend([("/nodes/%s" % n.name, n) for n in nlist]) |
7880 | 73415719 | Iustin Pop | results = [] |
7881 | 73415719 | Iustin Pop | for path, target in tgts: |
7882 | 73415719 | Iustin Pop | for tag in target.GetTags(): |
7883 | 73415719 | Iustin Pop | if self.re.search(tag): |
7884 | 73415719 | Iustin Pop | results.append((path, tag)) |
7885 | 73415719 | Iustin Pop | return results
|
7886 | 73415719 | Iustin Pop | |
7887 | 73415719 | Iustin Pop | |
7888 | f27302fa | Iustin Pop | class LUAddTags(TagsLU): |
7889 | 5c947f38 | Iustin Pop | """Sets a tag on a given object.
|
7890 | 5c947f38 | Iustin Pop |
|
7891 | 5c947f38 | Iustin Pop | """
|
7892 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
7893 | 8646adce | Guido Trotter | REQ_BGL = False
|
7894 | 5c947f38 | Iustin Pop | |
7895 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
7896 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
7897 | 5c947f38 | Iustin Pop |
|
7898 | 5c947f38 | Iustin Pop | This checks the type and length of the tag name and value.
|
7899 | 5c947f38 | Iustin Pop |
|
7900 | 5c947f38 | Iustin Pop | """
|
7901 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
7902 | f27302fa | Iustin Pop | for tag in self.op.tags: |
7903 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
7904 | 5c947f38 | Iustin Pop | |
7905 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
7906 | 5c947f38 | Iustin Pop | """Sets the tag.
|
7907 | 5c947f38 | Iustin Pop |
|
7908 | 5c947f38 | Iustin Pop | """
|
7909 | 5c947f38 | Iustin Pop | try:
|
7910 | f27302fa | Iustin Pop | for tag in self.op.tags: |
7911 | f27302fa | Iustin Pop | self.target.AddTag(tag)
|
7912 | 5c947f38 | Iustin Pop | except errors.TagError, err:
|
7913 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Error while setting tag: %s" % str(err)) |
7914 | 5c947f38 | Iustin Pop | try:
|
7915 | 5c947f38 | Iustin Pop | self.cfg.Update(self.target) |
7916 | 5c947f38 | Iustin Pop | except errors.ConfigurationError:
|
7917 | 3ecf6786 | Iustin Pop | raise errors.OpRetryError("There has been a modification to the" |
7918 | 3ecf6786 | Iustin Pop | " config file and the operation has been"
|
7919 | 3ecf6786 | Iustin Pop | " aborted. Please retry.")
|
7920 | 5c947f38 | Iustin Pop | |
7921 | 5c947f38 | Iustin Pop | |
7922 | f27302fa | Iustin Pop | class LUDelTags(TagsLU): |
7923 | f27302fa | Iustin Pop | """Delete a list of tags from a given object.
|
7924 | 5c947f38 | Iustin Pop |
|
7925 | 5c947f38 | Iustin Pop | """
|
7926 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
7927 | 8646adce | Guido Trotter | REQ_BGL = False
|
7928 | 5c947f38 | Iustin Pop | |
7929 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
7930 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
7931 | 5c947f38 | Iustin Pop |
|
7932 | 5c947f38 | Iustin Pop | This checks that we have the given tag.
|
7933 | 5c947f38 | Iustin Pop |
|
7934 | 5c947f38 | Iustin Pop | """
|
7935 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
7936 | f27302fa | Iustin Pop | for tag in self.op.tags: |
7937 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
7938 | f27302fa | Iustin Pop | del_tags = frozenset(self.op.tags) |
7939 | f27302fa | Iustin Pop | cur_tags = self.target.GetTags()
|
7940 | f27302fa | Iustin Pop | if not del_tags <= cur_tags: |
7941 | f27302fa | Iustin Pop | diff_tags = del_tags - cur_tags |
7942 | f27302fa | Iustin Pop | diff_names = ["'%s'" % tag for tag in diff_tags] |
7943 | f27302fa | Iustin Pop | diff_names.sort() |
7944 | f27302fa | Iustin Pop | raise errors.OpPrereqError("Tag(s) %s not found" % |
7945 | f27302fa | Iustin Pop | (",".join(diff_names)))
|
7946 | 5c947f38 | Iustin Pop | |
7947 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
7948 | 5c947f38 | Iustin Pop | """Remove the tag from the object.
|
7949 | 5c947f38 | Iustin Pop |
|
7950 | 5c947f38 | Iustin Pop | """
|
7951 | f27302fa | Iustin Pop | for tag in self.op.tags: |
7952 | f27302fa | Iustin Pop | self.target.RemoveTag(tag)
|
7953 | 5c947f38 | Iustin Pop | try:
|
7954 | 5c947f38 | Iustin Pop | self.cfg.Update(self.target) |
7955 | 5c947f38 | Iustin Pop | except errors.ConfigurationError:
|
7956 | 3ecf6786 | Iustin Pop | raise errors.OpRetryError("There has been a modification to the" |
7957 | 3ecf6786 | Iustin Pop | " config file and the operation has been"
|
7958 | 3ecf6786 | Iustin Pop | " aborted. Please retry.")
|
7959 | 06009e27 | Iustin Pop | |
7960 | 0eed6e61 | Guido Trotter | |
7961 | 06009e27 | Iustin Pop | class LUTestDelay(NoHooksLU): |
7962 | 06009e27 | Iustin Pop | """Sleep for a specified amount of time.
|
7963 | 06009e27 | Iustin Pop |
|
7964 | 0b097284 | Guido Trotter | This LU sleeps on the master and/or nodes for a specified amount of
|
7965 | 06009e27 | Iustin Pop | time.
|
7966 | 06009e27 | Iustin Pop |
|
7967 | 06009e27 | Iustin Pop | """
|
7968 | 06009e27 | Iustin Pop | _OP_REQP = ["duration", "on_master", "on_nodes"] |
7969 | fbe9022f | Guido Trotter | REQ_BGL = False
|
7970 | 06009e27 | Iustin Pop | |
7971 | fbe9022f | Guido Trotter | def ExpandNames(self): |
7972 | fbe9022f | Guido Trotter | """Expand names and set required locks.
|
7973 | 06009e27 | Iustin Pop |
|
7974 | fbe9022f | Guido Trotter | This expands the node list, if any.
|
7975 | 06009e27 | Iustin Pop |
|
7976 | 06009e27 | Iustin Pop | """
|
7977 | fbe9022f | Guido Trotter | self.needed_locks = {}
|
7978 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
7979 | fbe9022f | Guido Trotter | # _GetWantedNodes can be used here, but is not always appropriate to use
|
7980 | fbe9022f | Guido Trotter | # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
|
7981 | fbe9022f | Guido Trotter | # more information.
|
7982 | 06009e27 | Iustin Pop | self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes) |
7983 | fbe9022f | Guido Trotter | self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes |
7984 | fbe9022f | Guido Trotter | |
7985 | fbe9022f | Guido Trotter | def CheckPrereq(self): |
7986 | fbe9022f | Guido Trotter | """Check prerequisites.
|
7987 | fbe9022f | Guido Trotter |
|
7988 | fbe9022f | Guido Trotter | """
|
7989 | 06009e27 | Iustin Pop | |
7990 | 06009e27 | Iustin Pop | def Exec(self, feedback_fn): |
7991 | 06009e27 | Iustin Pop | """Do the actual sleep.
|
7992 | 06009e27 | Iustin Pop |
|
7993 | 06009e27 | Iustin Pop | """
|
7994 | 06009e27 | Iustin Pop | if self.op.on_master: |
7995 | 06009e27 | Iustin Pop | if not utils.TestDelay(self.op.duration): |
7996 | 06009e27 | Iustin Pop | raise errors.OpExecError("Error during master delay test") |
7997 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
7998 | 72737a7f | Iustin Pop | result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration) |
7999 | 06009e27 | Iustin Pop | for node, node_result in result.items(): |
8000 | 4c4e4e1e | Iustin Pop | node_result.Raise("Failure during rpc call to node %s" % node)
|
8001 | d61df03e | Iustin Pop | |
8002 | d61df03e | Iustin Pop | |
8003 | d1c2dd75 | Iustin Pop | class IAllocator(object): |
8004 | d1c2dd75 | Iustin Pop | """IAllocator framework.
|
8005 | d61df03e | Iustin Pop |
|
8006 | d1c2dd75 | Iustin Pop | An IAllocator instance has three sets of attributes:
|
8007 | d6a02168 | Michael Hanselmann | - cfg that is needed to query the cluster
|
8008 | d1c2dd75 | Iustin Pop | - input data (all members of the _KEYS class attribute are required)
|
8009 | d1c2dd75 | Iustin Pop | - four buffer attributes (in|out_data|text), that represent the
|
8010 | d1c2dd75 | Iustin Pop | input (to the external script) in text and data structure format,
|
8011 | d1c2dd75 | Iustin Pop | and the output from it, again in two formats
|
8012 | d1c2dd75 | Iustin Pop | - the result variables from the script (success, info, nodes) for
|
8013 | d1c2dd75 | Iustin Pop | easy usage
|
8014 | d61df03e | Iustin Pop |
|
8015 | d61df03e | Iustin Pop | """
|
8016 | 29859cb7 | Iustin Pop | _ALLO_KEYS = [ |
8017 | d1c2dd75 | Iustin Pop | "mem_size", "disks", "disk_template", |
8018 | 8cc7e742 | Guido Trotter | "os", "tags", "nics", "vcpus", "hypervisor", |
8019 | d1c2dd75 | Iustin Pop | ] |
8020 | 29859cb7 | Iustin Pop | _RELO_KEYS = [ |
8021 | 29859cb7 | Iustin Pop | "relocate_from",
|
8022 | 29859cb7 | Iustin Pop | ] |
8023 | d1c2dd75 | Iustin Pop | |
8024 | 923ddac0 | Michael Hanselmann | def __init__(self, cfg, rpc, mode, name, **kwargs): |
8025 | 923ddac0 | Michael Hanselmann | self.cfg = cfg
|
8026 | 923ddac0 | Michael Hanselmann | self.rpc = rpc
|
8027 | d1c2dd75 | Iustin Pop | # init buffer variables
|
8028 | d1c2dd75 | Iustin Pop | self.in_text = self.out_text = self.in_data = self.out_data = None |
8029 | d1c2dd75 | Iustin Pop | # init all input fields so that pylint is happy
|
8030 | 29859cb7 | Iustin Pop | self.mode = mode
|
8031 | 29859cb7 | Iustin Pop | self.name = name
|
8032 | d1c2dd75 | Iustin Pop | self.mem_size = self.disks = self.disk_template = None |
8033 | d1c2dd75 | Iustin Pop | self.os = self.tags = self.nics = self.vcpus = None |
8034 | a0add446 | Iustin Pop | self.hypervisor = None |
8035 | 29859cb7 | Iustin Pop | self.relocate_from = None |
8036 | 27579978 | Iustin Pop | # computed fields
|
8037 | 27579978 | Iustin Pop | self.required_nodes = None |
8038 | d1c2dd75 | Iustin Pop | # init result fields
|
8039 | d1c2dd75 | Iustin Pop | self.success = self.info = self.nodes = None |
8040 | 29859cb7 | Iustin Pop | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
8041 | 29859cb7 | Iustin Pop | keyset = self._ALLO_KEYS
|
8042 | 29859cb7 | Iustin Pop | elif self.mode == constants.IALLOCATOR_MODE_RELOC: |
8043 | 29859cb7 | Iustin Pop | keyset = self._RELO_KEYS
|
8044 | 29859cb7 | Iustin Pop | else:
|
8045 | 29859cb7 | Iustin Pop | raise errors.ProgrammerError("Unknown mode '%s' passed to the" |
8046 | 29859cb7 | Iustin Pop | " IAllocator" % self.mode) |
8047 | d1c2dd75 | Iustin Pop | for key in kwargs: |
8048 | 29859cb7 | Iustin Pop | if key not in keyset: |
8049 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Invalid input parameter '%s' to" |
8050 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
8051 | d1c2dd75 | Iustin Pop | setattr(self, key, kwargs[key]) |
8052 | 29859cb7 | Iustin Pop | for key in keyset: |
8053 | d1c2dd75 | Iustin Pop | if key not in kwargs: |
8054 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Missing input parameter '%s' to" |
8055 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
8056 | d1c2dd75 | Iustin Pop | self._BuildInputData()
|
8057 | d1c2dd75 | Iustin Pop | |
8058 | d1c2dd75 | Iustin Pop | def _ComputeClusterData(self): |
8059 | d1c2dd75 | Iustin Pop | """Compute the generic allocator input data.
|
8060 | d1c2dd75 | Iustin Pop |
|
8061 | d1c2dd75 | Iustin Pop | This is the data that is independent of the actual operation.
|
8062 | d1c2dd75 | Iustin Pop |
|
8063 | d1c2dd75 | Iustin Pop | """
|
8064 | 923ddac0 | Michael Hanselmann | cfg = self.cfg
|
8065 | e69d05fd | Iustin Pop | cluster_info = cfg.GetClusterInfo() |
8066 | d1c2dd75 | Iustin Pop | # cluster data
|
8067 | d1c2dd75 | Iustin Pop | data = { |
8068 | 77031881 | Iustin Pop | "version": constants.IALLOCATOR_VERSION,
|
8069 | 72737a7f | Iustin Pop | "cluster_name": cfg.GetClusterName(),
|
8070 | e69d05fd | Iustin Pop | "cluster_tags": list(cluster_info.GetTags()), |
8071 | 1325da74 | Iustin Pop | "enabled_hypervisors": list(cluster_info.enabled_hypervisors), |
8072 | d1c2dd75 | Iustin Pop | # we don't have job IDs
|
8073 | d61df03e | Iustin Pop | } |
8074 | b57e9819 | Guido Trotter | iinfo = cfg.GetAllInstancesInfo().values() |
8075 | b57e9819 | Guido Trotter | i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo] |
8076 | 6286519f | Iustin Pop | |
8077 | d1c2dd75 | Iustin Pop | # node data
|
8078 | d1c2dd75 | Iustin Pop | node_results = {} |
8079 | d1c2dd75 | Iustin Pop | node_list = cfg.GetNodeList() |
8080 | 8cc7e742 | Guido Trotter | |
8081 | 8cc7e742 | Guido Trotter | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
8082 | a0add446 | Iustin Pop | hypervisor_name = self.hypervisor
|
8083 | 8cc7e742 | Guido Trotter | elif self.mode == constants.IALLOCATOR_MODE_RELOC: |
8084 | a0add446 | Iustin Pop | hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
|
8085 | 8cc7e742 | Guido Trotter | |
8086 | 923ddac0 | Michael Hanselmann | node_data = self.rpc.call_node_info(node_list, cfg.GetVGName(),
|
8087 | 923ddac0 | Michael Hanselmann | hypervisor_name) |
8088 | 923ddac0 | Michael Hanselmann | node_iinfo = \ |
8089 | 923ddac0 | Michael Hanselmann | self.rpc.call_all_instances_info(node_list,
|
8090 | 923ddac0 | Michael Hanselmann | cluster_info.enabled_hypervisors) |
8091 | 1325da74 | Iustin Pop | for nname, nresult in node_data.items(): |
8092 | 1325da74 | Iustin Pop | # first fill in static (config-based) values
|
8093 | d1c2dd75 | Iustin Pop | ninfo = cfg.GetNodeInfo(nname) |
8094 | d1c2dd75 | Iustin Pop | pnr = { |
8095 | d1c2dd75 | Iustin Pop | "tags": list(ninfo.GetTags()), |
8096 | d1c2dd75 | Iustin Pop | "primary_ip": ninfo.primary_ip,
|
8097 | d1c2dd75 | Iustin Pop | "secondary_ip": ninfo.secondary_ip,
|
8098 | fc0fe88c | Iustin Pop | "offline": ninfo.offline,
|
8099 | 0b2454b9 | Iustin Pop | "drained": ninfo.drained,
|
8100 | 1325da74 | Iustin Pop | "master_candidate": ninfo.master_candidate,
|
8101 | d1c2dd75 | Iustin Pop | } |
8102 | 1325da74 | Iustin Pop | |
8103 | 0d853843 | Iustin Pop | if not (ninfo.offline or ninfo.drained): |
8104 | 4c4e4e1e | Iustin Pop | nresult.Raise("Can't get data for node %s" % nname)
|
8105 | 4c4e4e1e | Iustin Pop | node_iinfo[nname].Raise("Can't get node instance info from node %s" %
|
8106 | 4c4e4e1e | Iustin Pop | nname) |
8107 | 070e998b | Iustin Pop | remote_info = nresult.payload |
8108 | b142ef15 | Iustin Pop | |
8109 | 1325da74 | Iustin Pop | for attr in ['memory_total', 'memory_free', 'memory_dom0', |
8110 | 1325da74 | Iustin Pop | 'vg_size', 'vg_free', 'cpu_total']: |
8111 | 1325da74 | Iustin Pop | if attr not in remote_info: |
8112 | 1325da74 | Iustin Pop | raise errors.OpExecError("Node '%s' didn't return attribute" |
8113 | 1325da74 | Iustin Pop | " '%s'" % (nname, attr))
|
8114 | 070e998b | Iustin Pop | if not isinstance(remote_info[attr], int): |
8115 | 1325da74 | Iustin Pop | raise errors.OpExecError("Node '%s' returned invalid value" |
8116 | 070e998b | Iustin Pop | " for '%s': %s" %
|
8117 | 070e998b | Iustin Pop | (nname, attr, remote_info[attr])) |
8118 | 1325da74 | Iustin Pop | # compute memory used by primary instances
|
8119 | 1325da74 | Iustin Pop | i_p_mem = i_p_up_mem = 0
|
8120 | 1325da74 | Iustin Pop | for iinfo, beinfo in i_list: |
8121 | 1325da74 | Iustin Pop | if iinfo.primary_node == nname:
|
8122 | 1325da74 | Iustin Pop | i_p_mem += beinfo[constants.BE_MEMORY] |
8123 | 2fa74ef4 | Iustin Pop | if iinfo.name not in node_iinfo[nname].payload: |
8124 | 1325da74 | Iustin Pop | i_used_mem = 0
|
8125 | 1325da74 | Iustin Pop | else:
|
8126 | 2fa74ef4 | Iustin Pop | i_used_mem = int(node_iinfo[nname].payload[iinfo.name]['memory']) |
8127 | 1325da74 | Iustin Pop | i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem |
8128 | 1325da74 | Iustin Pop | remote_info['memory_free'] -= max(0, i_mem_diff) |
8129 | 1325da74 | Iustin Pop | |
8130 | 1325da74 | Iustin Pop | if iinfo.admin_up:
|
8131 | 1325da74 | Iustin Pop | i_p_up_mem += beinfo[constants.BE_MEMORY] |
8132 | 1325da74 | Iustin Pop | |
8133 | 1325da74 | Iustin Pop | # compute memory used by instances
|
8134 | 1325da74 | Iustin Pop | pnr_dyn = { |
8135 | 1325da74 | Iustin Pop | "total_memory": remote_info['memory_total'], |
8136 | 1325da74 | Iustin Pop | "reserved_memory": remote_info['memory_dom0'], |
8137 | 1325da74 | Iustin Pop | "free_memory": remote_info['memory_free'], |
8138 | 1325da74 | Iustin Pop | "total_disk": remote_info['vg_size'], |
8139 | 1325da74 | Iustin Pop | "free_disk": remote_info['vg_free'], |
8140 | 1325da74 | Iustin Pop | "total_cpus": remote_info['cpu_total'], |
8141 | 1325da74 | Iustin Pop | "i_pri_memory": i_p_mem,
|
8142 | 1325da74 | Iustin Pop | "i_pri_up_memory": i_p_up_mem,
|
8143 | 1325da74 | Iustin Pop | } |
8144 | 1325da74 | Iustin Pop | pnr.update(pnr_dyn) |
8145 | 1325da74 | Iustin Pop | |
8146 | d1c2dd75 | Iustin Pop | node_results[nname] = pnr |
8147 | d1c2dd75 | Iustin Pop | data["nodes"] = node_results
|
8148 | d1c2dd75 | Iustin Pop | |
8149 | d1c2dd75 | Iustin Pop | # instance data
|
8150 | d1c2dd75 | Iustin Pop | instance_data = {} |
8151 | 338e51e8 | Iustin Pop | for iinfo, beinfo in i_list: |
8152 | a9fe7e8f | Guido Trotter | nic_data = [] |
8153 | a9fe7e8f | Guido Trotter | for nic in iinfo.nics: |
8154 | a9fe7e8f | Guido Trotter | filled_params = objects.FillDict( |
8155 | a9fe7e8f | Guido Trotter | cluster_info.nicparams[constants.PP_DEFAULT], |
8156 | a9fe7e8f | Guido Trotter | nic.nicparams) |
8157 | a9fe7e8f | Guido Trotter | nic_dict = {"mac": nic.mac,
|
8158 | a9fe7e8f | Guido Trotter | "ip": nic.ip,
|
8159 | a9fe7e8f | Guido Trotter | "mode": filled_params[constants.NIC_MODE],
|
8160 | a9fe7e8f | Guido Trotter | "link": filled_params[constants.NIC_LINK],
|
8161 | a9fe7e8f | Guido Trotter | } |
8162 | a9fe7e8f | Guido Trotter | if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
8163 | a9fe7e8f | Guido Trotter | nic_dict["bridge"] = filled_params[constants.NIC_LINK]
|
8164 | a9fe7e8f | Guido Trotter | nic_data.append(nic_dict) |
8165 | d1c2dd75 | Iustin Pop | pir = { |
8166 | d1c2dd75 | Iustin Pop | "tags": list(iinfo.GetTags()), |
8167 | 1325da74 | Iustin Pop | "admin_up": iinfo.admin_up,
|
8168 | 338e51e8 | Iustin Pop | "vcpus": beinfo[constants.BE_VCPUS],
|
8169 | 338e51e8 | Iustin Pop | "memory": beinfo[constants.BE_MEMORY],
|
8170 | d1c2dd75 | Iustin Pop | "os": iinfo.os,
|
8171 | 1325da74 | Iustin Pop | "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes), |
8172 | d1c2dd75 | Iustin Pop | "nics": nic_data,
|
8173 | 1325da74 | Iustin Pop | "disks": [{"size": dsk.size, "mode": dsk.mode} for dsk in iinfo.disks], |
8174 | d1c2dd75 | Iustin Pop | "disk_template": iinfo.disk_template,
|
8175 | e69d05fd | Iustin Pop | "hypervisor": iinfo.hypervisor,
|
8176 | d1c2dd75 | Iustin Pop | } |
8177 | 88ae4f85 | Iustin Pop | pir["disk_space_total"] = _ComputeDiskSize(iinfo.disk_template,
|
8178 | 88ae4f85 | Iustin Pop | pir["disks"])
|
8179 | 768f0a80 | Iustin Pop | instance_data[iinfo.name] = pir |
8180 | d61df03e | Iustin Pop | |
8181 | d1c2dd75 | Iustin Pop | data["instances"] = instance_data
|
8182 | d61df03e | Iustin Pop | |
8183 | d1c2dd75 | Iustin Pop | self.in_data = data
|
8184 | d61df03e | Iustin Pop | |
8185 | d1c2dd75 | Iustin Pop | def _AddNewInstance(self): |
8186 | d1c2dd75 | Iustin Pop | """Add new instance data to allocator structure.
|
8187 | d61df03e | Iustin Pop |
|
8188 | d1c2dd75 | Iustin Pop | This in combination with _AllocatorGetClusterData will create the
|
8189 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
8190 | d61df03e | Iustin Pop |
|
8191 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
8192 | d1c2dd75 | Iustin Pop | done.
|
8193 | d61df03e | Iustin Pop |
|
8194 | d1c2dd75 | Iustin Pop | """
|
8195 | d1c2dd75 | Iustin Pop | data = self.in_data
|
8196 | d1c2dd75 | Iustin Pop | |
8197 | dafc7302 | Guido Trotter | disk_space = _ComputeDiskSize(self.disk_template, self.disks) |
8198 | d1c2dd75 | Iustin Pop | |
8199 | 27579978 | Iustin Pop | if self.disk_template in constants.DTS_NET_MIRROR: |
8200 | 27579978 | Iustin Pop | self.required_nodes = 2 |
8201 | 27579978 | Iustin Pop | else:
|
8202 | 27579978 | Iustin Pop | self.required_nodes = 1 |
8203 | d1c2dd75 | Iustin Pop | request = { |
8204 | d1c2dd75 | Iustin Pop | "type": "allocate", |
8205 | d1c2dd75 | Iustin Pop | "name": self.name, |
8206 | d1c2dd75 | Iustin Pop | "disk_template": self.disk_template, |
8207 | d1c2dd75 | Iustin Pop | "tags": self.tags, |
8208 | d1c2dd75 | Iustin Pop | "os": self.os, |
8209 | d1c2dd75 | Iustin Pop | "vcpus": self.vcpus, |
8210 | d1c2dd75 | Iustin Pop | "memory": self.mem_size, |
8211 | d1c2dd75 | Iustin Pop | "disks": self.disks, |
8212 | d1c2dd75 | Iustin Pop | "disk_space_total": disk_space,
|
8213 | d1c2dd75 | Iustin Pop | "nics": self.nics, |
8214 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
8215 | d1c2dd75 | Iustin Pop | } |
8216 | d1c2dd75 | Iustin Pop | data["request"] = request
|
8217 | 298fe380 | Iustin Pop | |
8218 | d1c2dd75 | Iustin Pop | def _AddRelocateInstance(self): |
8219 | d1c2dd75 | Iustin Pop | """Add relocate instance data to allocator structure.
|
8220 | 298fe380 | Iustin Pop |
|
8221 | d1c2dd75 | Iustin Pop | This in combination with _IAllocatorGetClusterData will create the
|
8222 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
8223 | d61df03e | Iustin Pop |
|
8224 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
8225 | d1c2dd75 | Iustin Pop | done.
|
8226 | d61df03e | Iustin Pop |
|
8227 | d1c2dd75 | Iustin Pop | """
|
8228 | 923ddac0 | Michael Hanselmann | instance = self.cfg.GetInstanceInfo(self.name) |
8229 | 27579978 | Iustin Pop | if instance is None: |
8230 | 27579978 | Iustin Pop | raise errors.ProgrammerError("Unknown instance '%s' passed to" |
8231 | 27579978 | Iustin Pop | " IAllocator" % self.name) |
8232 | 27579978 | Iustin Pop | |
8233 | 27579978 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
8234 | 27579978 | Iustin Pop | raise errors.OpPrereqError("Can't relocate non-mirrored instances") |
8235 | 27579978 | Iustin Pop | |
8236 | 2a139bb0 | Iustin Pop | if len(instance.secondary_nodes) != 1: |
8237 | 2a139bb0 | Iustin Pop | raise errors.OpPrereqError("Instance has not exactly one secondary node") |
8238 | 2a139bb0 | Iustin Pop | |
8239 | 27579978 | Iustin Pop | self.required_nodes = 1 |
8240 | dafc7302 | Guido Trotter | disk_sizes = [{'size': disk.size} for disk in instance.disks] |
8241 | dafc7302 | Guido Trotter | disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes) |
8242 | 27579978 | Iustin Pop | |
8243 | d1c2dd75 | Iustin Pop | request = { |
8244 | 2a139bb0 | Iustin Pop | "type": "relocate", |
8245 | d1c2dd75 | Iustin Pop | "name": self.name, |
8246 | 27579978 | Iustin Pop | "disk_space_total": disk_space,
|
8247 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
8248 | 29859cb7 | Iustin Pop | "relocate_from": self.relocate_from, |
8249 | d1c2dd75 | Iustin Pop | } |
8250 | 27579978 | Iustin Pop | self.in_data["request"] = request |
8251 | d61df03e | Iustin Pop | |
8252 | d1c2dd75 | Iustin Pop | def _BuildInputData(self): |
8253 | d1c2dd75 | Iustin Pop | """Build input data structures.
|
8254 | d61df03e | Iustin Pop |
|
8255 | d1c2dd75 | Iustin Pop | """
|
8256 | d1c2dd75 | Iustin Pop | self._ComputeClusterData()
|
8257 | d61df03e | Iustin Pop | |
8258 | d1c2dd75 | Iustin Pop | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
8259 | d1c2dd75 | Iustin Pop | self._AddNewInstance()
|
8260 | d1c2dd75 | Iustin Pop | else:
|
8261 | d1c2dd75 | Iustin Pop | self._AddRelocateInstance()
|
8262 | d61df03e | Iustin Pop | |
8263 | d1c2dd75 | Iustin Pop | self.in_text = serializer.Dump(self.in_data) |
8264 | d61df03e | Iustin Pop | |
8265 | 72737a7f | Iustin Pop | def Run(self, name, validate=True, call_fn=None): |
8266 | d1c2dd75 | Iustin Pop | """Run an instance allocator and return the results.
|
8267 | 298fe380 | Iustin Pop |
|
8268 | d1c2dd75 | Iustin Pop | """
|
8269 | 72737a7f | Iustin Pop | if call_fn is None: |
8270 | 923ddac0 | Michael Hanselmann | call_fn = self.rpc.call_iallocator_runner
|
8271 | 298fe380 | Iustin Pop | |
8272 | 923ddac0 | Michael Hanselmann | result = call_fn(self.cfg.GetMasterNode(), name, self.in_text) |
8273 | 4c4e4e1e | Iustin Pop | result.Raise("Failure while running the iallocator script")
|
8274 | 8d528b7c | Iustin Pop | |
8275 | 87f5c298 | Iustin Pop | self.out_text = result.payload
|
8276 | d1c2dd75 | Iustin Pop | if validate:
|
8277 | d1c2dd75 | Iustin Pop | self._ValidateResult()
|
8278 | 298fe380 | Iustin Pop | |
8279 | d1c2dd75 | Iustin Pop | def _ValidateResult(self): |
8280 | d1c2dd75 | Iustin Pop | """Process the allocator results.
|
8281 | 538475ca | Iustin Pop |
|
8282 | d1c2dd75 | Iustin Pop | This will process and if successful save the result in
|
8283 | d1c2dd75 | Iustin Pop | self.out_data and the other parameters.
|
8284 | 538475ca | Iustin Pop |
|
8285 | d1c2dd75 | Iustin Pop | """
|
8286 | d1c2dd75 | Iustin Pop | try:
|
8287 | d1c2dd75 | Iustin Pop | rdict = serializer.Load(self.out_text)
|
8288 | d1c2dd75 | Iustin Pop | except Exception, err: |
8289 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: %s" % str(err)) |
8290 | d1c2dd75 | Iustin Pop | |
8291 | d1c2dd75 | Iustin Pop | if not isinstance(rdict, dict): |
8292 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: not a dict") |
8293 | 538475ca | Iustin Pop | |
8294 | d1c2dd75 | Iustin Pop | for key in "success", "info", "nodes": |
8295 | d1c2dd75 | Iustin Pop | if key not in rdict: |
8296 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results:" |
8297 | d1c2dd75 | Iustin Pop | " missing key '%s'" % key)
|
8298 | d1c2dd75 | Iustin Pop | setattr(self, key, rdict[key]) |
8299 | 538475ca | Iustin Pop | |
8300 | d1c2dd75 | Iustin Pop | if not isinstance(rdict["nodes"], list): |
8301 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: 'nodes' key" |
8302 | d1c2dd75 | Iustin Pop | " is not a list")
|
8303 | d1c2dd75 | Iustin Pop | self.out_data = rdict
|
8304 | 538475ca | Iustin Pop | |
8305 | 538475ca | Iustin Pop | |
8306 | d61df03e | Iustin Pop | class LUTestAllocator(NoHooksLU): |
8307 | d61df03e | Iustin Pop | """Run allocator tests.
|
8308 | d61df03e | Iustin Pop |
|
8309 | d61df03e | Iustin Pop | This LU runs the allocator tests
|
8310 | d61df03e | Iustin Pop |
|
8311 | d61df03e | Iustin Pop | """
|
8312 | d61df03e | Iustin Pop | _OP_REQP = ["direction", "mode", "name"] |
8313 | d61df03e | Iustin Pop | |
8314 | d61df03e | Iustin Pop | def CheckPrereq(self): |
8315 | d61df03e | Iustin Pop | """Check prerequisites.
|
8316 | d61df03e | Iustin Pop |
|
8317 | d61df03e | Iustin Pop | This checks the opcode parameters depending on the director and mode test.
|
8318 | d61df03e | Iustin Pop |
|
8319 | d61df03e | Iustin Pop | """
|
8320 | 298fe380 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
8321 | d61df03e | Iustin Pop | for attr in ["name", "mem_size", "disks", "disk_template", |
8322 | d61df03e | Iustin Pop | "os", "tags", "nics", "vcpus"]: |
8323 | d61df03e | Iustin Pop | if not hasattr(self.op, attr): |
8324 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing attribute '%s' on opcode input" % |
8325 | d61df03e | Iustin Pop | attr) |
8326 | d61df03e | Iustin Pop | iname = self.cfg.ExpandInstanceName(self.op.name) |
8327 | d61df03e | Iustin Pop | if iname is not None: |
8328 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Instance '%s' already in the cluster" % |
8329 | d61df03e | Iustin Pop | iname) |
8330 | d61df03e | Iustin Pop | if not isinstance(self.op.nics, list): |
8331 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'nics'") |
8332 | d61df03e | Iustin Pop | for row in self.op.nics: |
8333 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
8334 | d61df03e | Iustin Pop | "mac" not in row or |
8335 | d61df03e | Iustin Pop | "ip" not in row or |
8336 | d61df03e | Iustin Pop | "bridge" not in row): |
8337 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the" |
8338 | d61df03e | Iustin Pop | " 'nics' parameter")
|
8339 | d61df03e | Iustin Pop | if not isinstance(self.op.disks, list): |
8340 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'disks'") |
8341 | d61df03e | Iustin Pop | for row in self.op.disks: |
8342 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
8343 | d61df03e | Iustin Pop | "size" not in row or |
8344 | d61df03e | Iustin Pop | not isinstance(row["size"], int) or |
8345 | d61df03e | Iustin Pop | "mode" not in row or |
8346 | d61df03e | Iustin Pop | row["mode"] not in ['r', 'w']): |
8347 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the" |
8348 | d61df03e | Iustin Pop | " 'disks' parameter")
|
8349 | 8901997e | Iustin Pop | if not hasattr(self.op, "hypervisor") or self.op.hypervisor is None: |
8350 | 8cc7e742 | Guido Trotter | self.op.hypervisor = self.cfg.GetHypervisorType() |
8351 | 298fe380 | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_RELOC: |
8352 | d61df03e | Iustin Pop | if not hasattr(self.op, "name"): |
8353 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing attribute 'name' on opcode input") |
8354 | d61df03e | Iustin Pop | fname = self.cfg.ExpandInstanceName(self.op.name) |
8355 | d61df03e | Iustin Pop | if fname is None: |
8356 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not found for relocation" % |
8357 | d61df03e | Iustin Pop | self.op.name)
|
8358 | d61df03e | Iustin Pop | self.op.name = fname
|
8359 | 29859cb7 | Iustin Pop | self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes |
8360 | d61df03e | Iustin Pop | else:
|
8361 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid test allocator mode '%s'" % |
8362 | d61df03e | Iustin Pop | self.op.mode)
|
8363 | d61df03e | Iustin Pop | |
8364 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_OUT: |
8365 | 298fe380 | Iustin Pop | if not hasattr(self.op, "allocator") or self.op.allocator is None: |
8366 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing allocator name") |
8367 | 298fe380 | Iustin Pop | elif self.op.direction != constants.IALLOCATOR_DIR_IN: |
8368 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Wrong allocator test '%s'" % |
8369 | d61df03e | Iustin Pop | self.op.direction)
|
8370 | d61df03e | Iustin Pop | |
8371 | d61df03e | Iustin Pop | def Exec(self, feedback_fn): |
8372 | d61df03e | Iustin Pop | """Run the allocator test.
|
8373 | d61df03e | Iustin Pop |
|
8374 | d61df03e | Iustin Pop | """
|
8375 | 29859cb7 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
8376 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
8377 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
8378 | 29859cb7 | Iustin Pop | name=self.op.name,
|
8379 | 29859cb7 | Iustin Pop | mem_size=self.op.mem_size,
|
8380 | 29859cb7 | Iustin Pop | disks=self.op.disks,
|
8381 | 29859cb7 | Iustin Pop | disk_template=self.op.disk_template,
|
8382 | 29859cb7 | Iustin Pop | os=self.op.os,
|
8383 | 29859cb7 | Iustin Pop | tags=self.op.tags,
|
8384 | 29859cb7 | Iustin Pop | nics=self.op.nics,
|
8385 | 29859cb7 | Iustin Pop | vcpus=self.op.vcpus,
|
8386 | 8cc7e742 | Guido Trotter | hypervisor=self.op.hypervisor,
|
8387 | 29859cb7 | Iustin Pop | ) |
8388 | 29859cb7 | Iustin Pop | else:
|
8389 | 923ddac0 | Michael Hanselmann | ial = IAllocator(self.cfg, self.rpc, |
8390 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
8391 | 29859cb7 | Iustin Pop | name=self.op.name,
|
8392 | 29859cb7 | Iustin Pop | relocate_from=list(self.relocate_from), |
8393 | 29859cb7 | Iustin Pop | ) |
8394 | d61df03e | Iustin Pop | |
8395 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_IN: |
8396 | d1c2dd75 | Iustin Pop | result = ial.in_text |
8397 | 298fe380 | Iustin Pop | else:
|
8398 | d1c2dd75 | Iustin Pop | ial.Run(self.op.allocator, validate=False) |
8399 | d1c2dd75 | Iustin Pop | result = ial.out_text |
8400 | 298fe380 | Iustin Pop | return result |