root / lib / cmdlib.py @ 3e91897b
History | View | Annotate | Download (168.1 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 sha |
29 | a8083063 | Iustin Pop | import time |
30 | a8083063 | Iustin Pop | import tempfile |
31 | a8083063 | Iustin Pop | import re |
32 | a8083063 | Iustin Pop | import platform |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | from ganeti import rpc |
35 | a8083063 | Iustin Pop | from ganeti import ssh |
36 | a8083063 | Iustin Pop | from ganeti import logger |
37 | a8083063 | Iustin Pop | from ganeti import utils |
38 | a8083063 | Iustin Pop | from ganeti import errors |
39 | a8083063 | Iustin Pop | from ganeti import hypervisor |
40 | a8083063 | Iustin Pop | from ganeti import config |
41 | a8083063 | Iustin Pop | from ganeti import constants |
42 | a8083063 | Iustin Pop | from ganeti import objects |
43 | a8083063 | Iustin Pop | from ganeti import opcodes |
44 | a8083063 | Iustin Pop | from ganeti import ssconf |
45 | 8d14b30d | Iustin Pop | from ganeti import serializer |
46 | d61df03e | Iustin Pop | |
47 | d61df03e | Iustin Pop | |
48 | a8083063 | Iustin Pop | class LogicalUnit(object): |
49 | 396e1b78 | Michael Hanselmann | """Logical Unit base class.
|
50 | a8083063 | Iustin Pop |
|
51 | a8083063 | Iustin Pop | Subclasses must follow these rules:
|
52 | a8083063 | Iustin Pop | - implement CheckPrereq which also fills in the opcode instance
|
53 | a8083063 | Iustin Pop | with all the fields (even if as None)
|
54 | a8083063 | Iustin Pop | - implement Exec
|
55 | a8083063 | Iustin Pop | - implement BuildHooksEnv
|
56 | a8083063 | Iustin Pop | - redefine HPATH and HTYPE
|
57 | c6d58a2b | Michael Hanselmann | - optionally redefine their run requirements (REQ_MASTER); note that all
|
58 | c6d58a2b | Michael Hanselmann | commands require root permissions
|
59 | a8083063 | Iustin Pop |
|
60 | a8083063 | Iustin Pop | """
|
61 | a8083063 | Iustin Pop | HPATH = None
|
62 | a8083063 | Iustin Pop | HTYPE = None
|
63 | a8083063 | Iustin Pop | _OP_REQP = [] |
64 | a8083063 | Iustin Pop | REQ_MASTER = True
|
65 | a8083063 | Iustin Pop | |
66 | a8083063 | Iustin Pop | def __init__(self, processor, op, cfg, sstore): |
67 | a8083063 | Iustin Pop | """Constructor for LogicalUnit.
|
68 | a8083063 | Iustin Pop |
|
69 | a8083063 | Iustin Pop | This needs to be overriden in derived classes in order to check op
|
70 | a8083063 | Iustin Pop | validity.
|
71 | a8083063 | Iustin Pop |
|
72 | a8083063 | Iustin Pop | """
|
73 | 5bfac263 | Iustin Pop | self.proc = processor
|
74 | a8083063 | Iustin Pop | self.op = op
|
75 | a8083063 | Iustin Pop | self.cfg = cfg
|
76 | a8083063 | Iustin Pop | self.sstore = sstore
|
77 | c92b310a | Michael Hanselmann | self.__ssh = None |
78 | c92b310a | Michael Hanselmann | |
79 | a8083063 | Iustin Pop | for attr_name in self._OP_REQP: |
80 | a8083063 | Iustin Pop | attr_val = getattr(op, attr_name, None) |
81 | a8083063 | Iustin Pop | if attr_val is None: |
82 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Required parameter '%s' missing" % |
83 | 3ecf6786 | Iustin Pop | attr_name) |
84 | c6d58a2b | Michael Hanselmann | |
85 | c6d58a2b | Michael Hanselmann | if not cfg.IsCluster(): |
86 | c6d58a2b | Michael Hanselmann | raise errors.OpPrereqError("Cluster not initialized yet," |
87 | c6d58a2b | Michael Hanselmann | " use 'gnt-cluster init' first.")
|
88 | c6d58a2b | Michael Hanselmann | if self.REQ_MASTER: |
89 | c6d58a2b | Michael Hanselmann | master = sstore.GetMasterNode() |
90 | c6d58a2b | Michael Hanselmann | if master != utils.HostInfo().name:
|
91 | c6d58a2b | Michael Hanselmann | raise errors.OpPrereqError("Commands must be run on the master" |
92 | c6d58a2b | Michael Hanselmann | " node %s" % master)
|
93 | a8083063 | Iustin Pop | |
94 | c92b310a | Michael Hanselmann | def __GetSSH(self): |
95 | c92b310a | Michael Hanselmann | """Returns the SshRunner object
|
96 | c92b310a | Michael Hanselmann |
|
97 | c92b310a | Michael Hanselmann | """
|
98 | c92b310a | Michael Hanselmann | if not self.__ssh: |
99 | 1ff08570 | Michael Hanselmann | self.__ssh = ssh.SshRunner(self.sstore) |
100 | c92b310a | Michael Hanselmann | return self.__ssh |
101 | c92b310a | Michael Hanselmann | |
102 | c92b310a | Michael Hanselmann | ssh = property(fget=__GetSSH)
|
103 | c92b310a | Michael Hanselmann | |
104 | a8083063 | Iustin Pop | def CheckPrereq(self): |
105 | a8083063 | Iustin Pop | """Check prerequisites for this LU.
|
106 | a8083063 | Iustin Pop |
|
107 | a8083063 | Iustin Pop | This method should check that the prerequisites for the execution
|
108 | a8083063 | Iustin Pop | of this LU are fulfilled. It can do internode communication, but
|
109 | a8083063 | Iustin Pop | it should be idempotent - no cluster or system changes are
|
110 | a8083063 | Iustin Pop | allowed.
|
111 | a8083063 | Iustin Pop |
|
112 | a8083063 | Iustin Pop | The method should raise errors.OpPrereqError in case something is
|
113 | a8083063 | Iustin Pop | not fulfilled. Its return value is ignored.
|
114 | a8083063 | Iustin Pop |
|
115 | a8083063 | Iustin Pop | This method should also update all the parameters of the opcode to
|
116 | a8083063 | Iustin Pop | their canonical form; e.g. a short node name must be fully
|
117 | a8083063 | Iustin Pop | expanded after this method has successfully completed (so that
|
118 | a8083063 | Iustin Pop | hooks, logging, etc. work correctly).
|
119 | a8083063 | Iustin Pop |
|
120 | a8083063 | Iustin Pop | """
|
121 | a8083063 | Iustin Pop | raise NotImplementedError |
122 | a8083063 | Iustin Pop | |
123 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
124 | a8083063 | Iustin Pop | """Execute the LU.
|
125 | a8083063 | Iustin Pop |
|
126 | a8083063 | Iustin Pop | This method should implement the actual work. It should raise
|
127 | a8083063 | Iustin Pop | errors.OpExecError for failures that are somewhat dealt with in
|
128 | a8083063 | Iustin Pop | code, or expected.
|
129 | a8083063 | Iustin Pop |
|
130 | a8083063 | Iustin Pop | """
|
131 | a8083063 | Iustin Pop | raise NotImplementedError |
132 | a8083063 | Iustin Pop | |
133 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
134 | a8083063 | Iustin Pop | """Build hooks environment for this LU.
|
135 | a8083063 | Iustin Pop |
|
136 | a8083063 | Iustin Pop | This method should return a three-node tuple consisting of: a dict
|
137 | a8083063 | Iustin Pop | containing the environment that will be used for running the
|
138 | a8083063 | Iustin Pop | specific hook for this LU, a list of node names on which the hook
|
139 | a8083063 | Iustin Pop | should run before the execution, and a list of node names on which
|
140 | a8083063 | Iustin Pop | the hook should run after the execution.
|
141 | a8083063 | Iustin Pop |
|
142 | a8083063 | Iustin Pop | The keys of the dict must not have 'GANETI_' prefixed as this will
|
143 | a8083063 | Iustin Pop | be handled in the hooks runner. Also note additional keys will be
|
144 | a8083063 | Iustin Pop | added by the hooks runner. If the LU doesn't define any
|
145 | a8083063 | Iustin Pop | environment, an empty dict (and not None) should be returned.
|
146 | a8083063 | Iustin Pop |
|
147 | 8a3fe350 | Guido Trotter | No nodes should be returned as an empty list (and not None).
|
148 | a8083063 | Iustin Pop |
|
149 | a8083063 | Iustin Pop | Note that if the HPATH for a LU class is None, this function will
|
150 | a8083063 | Iustin Pop | not be called.
|
151 | a8083063 | Iustin Pop |
|
152 | a8083063 | Iustin Pop | """
|
153 | a8083063 | Iustin Pop | raise NotImplementedError |
154 | a8083063 | Iustin Pop | |
155 | 1fce5219 | Guido Trotter | def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result): |
156 | 1fce5219 | Guido Trotter | """Notify the LU about the results of its hooks.
|
157 | 1fce5219 | Guido Trotter |
|
158 | 1fce5219 | Guido Trotter | This method is called every time a hooks phase is executed, and notifies
|
159 | 1fce5219 | Guido Trotter | the Logical Unit about the hooks' result. The LU can then use it to alter
|
160 | 1fce5219 | Guido Trotter | its result based on the hooks. By default the method does nothing and the
|
161 | 1fce5219 | Guido Trotter | previous result is passed back unchanged but any LU can define it if it
|
162 | 1fce5219 | Guido Trotter | wants to use the local cluster hook-scripts somehow.
|
163 | 1fce5219 | Guido Trotter |
|
164 | 1fce5219 | Guido Trotter | Args:
|
165 | 1fce5219 | Guido Trotter | phase: the hooks phase that has just been run
|
166 | 1fce5219 | Guido Trotter | hooks_results: the results of the multi-node hooks rpc call
|
167 | 1fce5219 | Guido Trotter | feedback_fn: function to send feedback back to the caller
|
168 | 1fce5219 | Guido Trotter | lu_result: the previous result this LU had, or None in the PRE phase.
|
169 | 1fce5219 | Guido Trotter |
|
170 | 1fce5219 | Guido Trotter | """
|
171 | 1fce5219 | Guido Trotter | return lu_result
|
172 | 1fce5219 | Guido Trotter | |
173 | a8083063 | Iustin Pop | |
174 | a8083063 | Iustin Pop | class NoHooksLU(LogicalUnit): |
175 | a8083063 | Iustin Pop | """Simple LU which runs no hooks.
|
176 | a8083063 | Iustin Pop |
|
177 | a8083063 | Iustin Pop | This LU is intended as a parent for other LogicalUnits which will
|
178 | a8083063 | Iustin Pop | run no hooks, in order to reduce duplicate code.
|
179 | a8083063 | Iustin Pop |
|
180 | a8083063 | Iustin Pop | """
|
181 | a8083063 | Iustin Pop | HPATH = None
|
182 | a8083063 | Iustin Pop | HTYPE = None
|
183 | a8083063 | Iustin Pop | |
184 | a8083063 | Iustin Pop | |
185 | dcb93971 | Michael Hanselmann | def _GetWantedNodes(lu, nodes): |
186 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded node names.
|
187 | 83120a01 | Michael Hanselmann |
|
188 | 83120a01 | Michael Hanselmann | Args:
|
189 | 83120a01 | Michael Hanselmann | nodes: List of nodes (strings) or None for all
|
190 | 83120a01 | Michael Hanselmann |
|
191 | 83120a01 | Michael Hanselmann | """
|
192 | 3312b702 | Iustin Pop | if not isinstance(nodes, list): |
193 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'nodes'") |
194 | dcb93971 | Michael Hanselmann | |
195 | dcb93971 | Michael Hanselmann | if nodes:
|
196 | 3312b702 | Iustin Pop | wanted = [] |
197 | dcb93971 | Michael Hanselmann | |
198 | dcb93971 | Michael Hanselmann | for name in nodes: |
199 | a7ba5e53 | Iustin Pop | node = lu.cfg.ExpandNodeName(name) |
200 | dcb93971 | Michael Hanselmann | if node is None: |
201 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No such node name '%s'" % name) |
202 | 3312b702 | Iustin Pop | wanted.append(node) |
203 | dcb93971 | Michael Hanselmann | |
204 | dcb93971 | Michael Hanselmann | else:
|
205 | a7ba5e53 | Iustin Pop | wanted = lu.cfg.GetNodeList() |
206 | a7ba5e53 | Iustin Pop | return utils.NiceSort(wanted)
|
207 | 3312b702 | Iustin Pop | |
208 | 3312b702 | Iustin Pop | |
209 | 3312b702 | Iustin Pop | def _GetWantedInstances(lu, instances): |
210 | a7ba5e53 | Iustin Pop | """Returns list of checked and expanded instance names.
|
211 | 3312b702 | Iustin Pop |
|
212 | 3312b702 | Iustin Pop | Args:
|
213 | 3312b702 | Iustin Pop | instances: List of instances (strings) or None for all
|
214 | 3312b702 | Iustin Pop |
|
215 | 3312b702 | Iustin Pop | """
|
216 | 3312b702 | Iustin Pop | if not isinstance(instances, list): |
217 | 3312b702 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'") |
218 | 3312b702 | Iustin Pop | |
219 | 3312b702 | Iustin Pop | if instances:
|
220 | 3312b702 | Iustin Pop | wanted = [] |
221 | 3312b702 | Iustin Pop | |
222 | 3312b702 | Iustin Pop | for name in instances: |
223 | a7ba5e53 | Iustin Pop | instance = lu.cfg.ExpandInstanceName(name) |
224 | 3312b702 | Iustin Pop | if instance is None: |
225 | 3312b702 | Iustin Pop | raise errors.OpPrereqError("No such instance name '%s'" % name) |
226 | 3312b702 | Iustin Pop | wanted.append(instance) |
227 | 3312b702 | Iustin Pop | |
228 | 3312b702 | Iustin Pop | else:
|
229 | a7ba5e53 | Iustin Pop | wanted = lu.cfg.GetInstanceList() |
230 | a7ba5e53 | Iustin Pop | return utils.NiceSort(wanted)
|
231 | dcb93971 | Michael Hanselmann | |
232 | dcb93971 | Michael Hanselmann | |
233 | dcb93971 | Michael Hanselmann | def _CheckOutputFields(static, dynamic, selected): |
234 | 83120a01 | Michael Hanselmann | """Checks whether all selected fields are valid.
|
235 | 83120a01 | Michael Hanselmann |
|
236 | 83120a01 | Michael Hanselmann | Args:
|
237 | 83120a01 | Michael Hanselmann | static: Static fields
|
238 | 83120a01 | Michael Hanselmann | dynamic: Dynamic fields
|
239 | 83120a01 | Michael Hanselmann |
|
240 | 83120a01 | Michael Hanselmann | """
|
241 | 83120a01 | Michael Hanselmann | static_fields = frozenset(static)
|
242 | 83120a01 | Michael Hanselmann | dynamic_fields = frozenset(dynamic)
|
243 | dcb93971 | Michael Hanselmann | |
244 | 83120a01 | Michael Hanselmann | all_fields = static_fields | dynamic_fields |
245 | dcb93971 | Michael Hanselmann | |
246 | 83120a01 | Michael Hanselmann | if not all_fields.issuperset(selected): |
247 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Unknown output fields selected: %s" |
248 | 3ecf6786 | Iustin Pop | % ",".join(frozenset(selected). |
249 | 3ecf6786 | Iustin Pop | difference(all_fields))) |
250 | dcb93971 | Michael Hanselmann | |
251 | dcb93971 | Michael Hanselmann | |
252 | ecb215b5 | Michael Hanselmann | def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, |
253 | 396e1b78 | Michael Hanselmann | memory, vcpus, nics): |
254 | ecb215b5 | Michael Hanselmann | """Builds instance related env variables for hooks from single variables.
|
255 | ecb215b5 | Michael Hanselmann |
|
256 | ecb215b5 | Michael Hanselmann | Args:
|
257 | ecb215b5 | Michael Hanselmann | secondary_nodes: List of secondary nodes as strings
|
258 | 396e1b78 | Michael Hanselmann | """
|
259 | 396e1b78 | Michael Hanselmann | env = { |
260 | 0e137c28 | Iustin Pop | "OP_TARGET": name,
|
261 | 396e1b78 | Michael Hanselmann | "INSTANCE_NAME": name,
|
262 | 396e1b78 | Michael Hanselmann | "INSTANCE_PRIMARY": primary_node,
|
263 | 396e1b78 | Michael Hanselmann | "INSTANCE_SECONDARIES": " ".join(secondary_nodes), |
264 | ecb215b5 | Michael Hanselmann | "INSTANCE_OS_TYPE": os_type,
|
265 | 396e1b78 | Michael Hanselmann | "INSTANCE_STATUS": status,
|
266 | 396e1b78 | Michael Hanselmann | "INSTANCE_MEMORY": memory,
|
267 | 396e1b78 | Michael Hanselmann | "INSTANCE_VCPUS": vcpus,
|
268 | 396e1b78 | Michael Hanselmann | } |
269 | 396e1b78 | Michael Hanselmann | |
270 | 396e1b78 | Michael Hanselmann | if nics:
|
271 | 396e1b78 | Michael Hanselmann | nic_count = len(nics)
|
272 | 53e4e875 | Guido Trotter | for idx, (ip, bridge, mac) in enumerate(nics): |
273 | 396e1b78 | Michael Hanselmann | if ip is None: |
274 | 396e1b78 | Michael Hanselmann | ip = ""
|
275 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC%d_IP" % idx] = ip
|
276 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
|
277 | 53e4e875 | Guido Trotter | env["INSTANCE_NIC%d_HWADDR" % idx] = mac
|
278 | 396e1b78 | Michael Hanselmann | else:
|
279 | 396e1b78 | Michael Hanselmann | nic_count = 0
|
280 | 396e1b78 | Michael Hanselmann | |
281 | 396e1b78 | Michael Hanselmann | env["INSTANCE_NIC_COUNT"] = nic_count
|
282 | 396e1b78 | Michael Hanselmann | |
283 | 396e1b78 | Michael Hanselmann | return env
|
284 | 396e1b78 | Michael Hanselmann | |
285 | 396e1b78 | Michael Hanselmann | |
286 | 396e1b78 | Michael Hanselmann | def _BuildInstanceHookEnvByObject(instance, override=None): |
287 | ecb215b5 | Michael Hanselmann | """Builds instance related env variables for hooks from an object.
|
288 | ecb215b5 | Michael Hanselmann |
|
289 | ecb215b5 | Michael Hanselmann | Args:
|
290 | ecb215b5 | Michael Hanselmann | instance: objects.Instance object of instance
|
291 | ecb215b5 | Michael Hanselmann | override: dict of values to override
|
292 | ecb215b5 | Michael Hanselmann | """
|
293 | 396e1b78 | Michael Hanselmann | args = { |
294 | 396e1b78 | Michael Hanselmann | 'name': instance.name,
|
295 | 396e1b78 | Michael Hanselmann | 'primary_node': instance.primary_node,
|
296 | 396e1b78 | Michael Hanselmann | 'secondary_nodes': instance.secondary_nodes,
|
297 | ecb215b5 | Michael Hanselmann | 'os_type': instance.os,
|
298 | 396e1b78 | Michael Hanselmann | 'status': instance.os,
|
299 | 396e1b78 | Michael Hanselmann | 'memory': instance.memory,
|
300 | 396e1b78 | Michael Hanselmann | 'vcpus': instance.vcpus,
|
301 | 53e4e875 | Guido Trotter | 'nics': [(nic.ip, nic.bridge, nic.mac) for nic in instance.nics], |
302 | 396e1b78 | Michael Hanselmann | } |
303 | 396e1b78 | Michael Hanselmann | if override:
|
304 | 396e1b78 | Michael Hanselmann | args.update(override) |
305 | 396e1b78 | Michael Hanselmann | return _BuildInstanceHookEnv(**args)
|
306 | 396e1b78 | Michael Hanselmann | |
307 | 396e1b78 | Michael Hanselmann | |
308 | bf6929a2 | Alexander Schreiber | def _CheckInstanceBridgesExist(instance): |
309 | bf6929a2 | Alexander Schreiber | """Check that the brigdes needed by an instance exist.
|
310 | bf6929a2 | Alexander Schreiber |
|
311 | bf6929a2 | Alexander Schreiber | """
|
312 | bf6929a2 | Alexander Schreiber | # check bridges existance
|
313 | bf6929a2 | Alexander Schreiber | brlist = [nic.bridge for nic in instance.nics] |
314 | bf6929a2 | Alexander Schreiber | if not rpc.call_bridges_exist(instance.primary_node, brlist): |
315 | bf6929a2 | Alexander Schreiber | raise errors.OpPrereqError("one or more target bridges %s does not" |
316 | bf6929a2 | Alexander Schreiber | " exist on destination node '%s'" %
|
317 | bf6929a2 | Alexander Schreiber | (brlist, instance.primary_node)) |
318 | bf6929a2 | Alexander Schreiber | |
319 | bf6929a2 | Alexander Schreiber | |
320 | a8083063 | Iustin Pop | class LUDestroyCluster(NoHooksLU): |
321 | a8083063 | Iustin Pop | """Logical unit for destroying the cluster.
|
322 | a8083063 | Iustin Pop |
|
323 | a8083063 | Iustin Pop | """
|
324 | a8083063 | Iustin Pop | _OP_REQP = [] |
325 | a8083063 | Iustin Pop | |
326 | a8083063 | Iustin Pop | def CheckPrereq(self): |
327 | a8083063 | Iustin Pop | """Check prerequisites.
|
328 | a8083063 | Iustin Pop |
|
329 | a8083063 | Iustin Pop | This checks whether the cluster is empty.
|
330 | a8083063 | Iustin Pop |
|
331 | a8083063 | Iustin Pop | Any errors are signalled by raising errors.OpPrereqError.
|
332 | a8083063 | Iustin Pop |
|
333 | a8083063 | Iustin Pop | """
|
334 | 880478f8 | Iustin Pop | master = self.sstore.GetMasterNode()
|
335 | a8083063 | Iustin Pop | |
336 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
337 | db915bd1 | Michael Hanselmann | if len(nodelist) != 1 or nodelist[0] != master: |
338 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d node(s) in" |
339 | 3ecf6786 | Iustin Pop | " this cluster." % (len(nodelist) - 1)) |
340 | db915bd1 | Michael Hanselmann | instancelist = self.cfg.GetInstanceList()
|
341 | db915bd1 | Michael Hanselmann | if instancelist:
|
342 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("There are still %d instance(s) in" |
343 | 3ecf6786 | Iustin Pop | " this cluster." % len(instancelist)) |
344 | a8083063 | Iustin Pop | |
345 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
346 | a8083063 | Iustin Pop | """Destroys the cluster.
|
347 | a8083063 | Iustin Pop |
|
348 | a8083063 | Iustin Pop | """
|
349 | c8a0948f | Michael Hanselmann | master = self.sstore.GetMasterNode()
|
350 | c9064964 | Iustin Pop | if not rpc.call_node_stop_master(master): |
351 | c9064964 | Iustin Pop | raise errors.OpExecError("Could not disable the master role") |
352 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
353 | 70d9e3d8 | Iustin Pop | utils.CreateBackup(priv_key) |
354 | 70d9e3d8 | Iustin Pop | utils.CreateBackup(pub_key) |
355 | c8a0948f | Michael Hanselmann | rpc.call_node_leave_cluster(master) |
356 | a8083063 | Iustin Pop | |
357 | a8083063 | Iustin Pop | |
358 | d8fff41c | Guido Trotter | class LUVerifyCluster(LogicalUnit): |
359 | a8083063 | Iustin Pop | """Verifies the cluster status.
|
360 | a8083063 | Iustin Pop |
|
361 | a8083063 | Iustin Pop | """
|
362 | d8fff41c | Guido Trotter | HPATH = "cluster-verify"
|
363 | d8fff41c | Guido Trotter | HTYPE = constants.HTYPE_CLUSTER |
364 | e54c4c5e | Guido Trotter | _OP_REQP = ["skip_checks"]
|
365 | a8083063 | Iustin Pop | |
366 | a8083063 | Iustin Pop | def _VerifyNode(self, node, file_list, local_cksum, vglist, node_result, |
367 | a8083063 | Iustin Pop | remote_version, feedback_fn): |
368 | a8083063 | Iustin Pop | """Run multiple tests against a node.
|
369 | a8083063 | Iustin Pop |
|
370 | a8083063 | Iustin Pop | Test list:
|
371 | a8083063 | Iustin Pop | - compares ganeti version
|
372 | a8083063 | Iustin Pop | - checks vg existance and size > 20G
|
373 | a8083063 | Iustin Pop | - checks config file checksum
|
374 | a8083063 | Iustin Pop | - checks ssh to other nodes
|
375 | a8083063 | Iustin Pop |
|
376 | a8083063 | Iustin Pop | Args:
|
377 | a8083063 | Iustin Pop | node: name of the node to check
|
378 | a8083063 | Iustin Pop | file_list: required list of files
|
379 | a8083063 | Iustin Pop | local_cksum: dictionary of local files and their checksums
|
380 | 098c0958 | Michael Hanselmann |
|
381 | a8083063 | Iustin Pop | """
|
382 | a8083063 | Iustin Pop | # compares ganeti version
|
383 | a8083063 | Iustin Pop | local_version = constants.PROTOCOL_VERSION |
384 | a8083063 | Iustin Pop | if not remote_version: |
385 | c840ae6f | Guido Trotter | feedback_fn(" - ERROR: connection to %s failed" % (node))
|
386 | a8083063 | Iustin Pop | return True |
387 | a8083063 | Iustin Pop | |
388 | a8083063 | Iustin Pop | if local_version != remote_version:
|
389 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: sw version mismatch: master %s, node(%s) %s" %
|
390 | a8083063 | Iustin Pop | (local_version, node, remote_version)) |
391 | a8083063 | Iustin Pop | return True |
392 | a8083063 | Iustin Pop | |
393 | a8083063 | Iustin Pop | # checks vg existance and size > 20G
|
394 | a8083063 | Iustin Pop | |
395 | a8083063 | Iustin Pop | bad = False
|
396 | a8083063 | Iustin Pop | if not vglist: |
397 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: unable to check volume groups on node %s." %
|
398 | a8083063 | Iustin Pop | (node,)) |
399 | a8083063 | Iustin Pop | bad = True
|
400 | a8083063 | Iustin Pop | else:
|
401 | 8d1a2a64 | Michael Hanselmann | vgstatus = utils.CheckVolumeGroupSize(vglist, self.cfg.GetVGName(),
|
402 | 8d1a2a64 | Michael Hanselmann | constants.MIN_VG_SIZE) |
403 | a8083063 | Iustin Pop | if vgstatus:
|
404 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: %s on node %s" % (vgstatus, node))
|
405 | a8083063 | Iustin Pop | bad = True
|
406 | a8083063 | Iustin Pop | |
407 | a8083063 | Iustin Pop | # checks config file checksum
|
408 | a8083063 | Iustin Pop | # checks ssh to any
|
409 | a8083063 | Iustin Pop | |
410 | a8083063 | Iustin Pop | if 'filelist' not in node_result: |
411 | a8083063 | Iustin Pop | bad = True
|
412 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: node hasn't returned file checksum data")
|
413 | a8083063 | Iustin Pop | else:
|
414 | a8083063 | Iustin Pop | remote_cksum = node_result['filelist']
|
415 | a8083063 | Iustin Pop | for file_name in file_list: |
416 | a8083063 | Iustin Pop | if file_name not in remote_cksum: |
417 | a8083063 | Iustin Pop | bad = True
|
418 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: file '%s' missing" % file_name)
|
419 | a8083063 | Iustin Pop | elif remote_cksum[file_name] != local_cksum[file_name]:
|
420 | a8083063 | Iustin Pop | bad = True
|
421 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: file '%s' has wrong checksum" % file_name)
|
422 | a8083063 | Iustin Pop | |
423 | a8083063 | Iustin Pop | if 'nodelist' not in node_result: |
424 | a8083063 | Iustin Pop | bad = True
|
425 | 9d4bfc96 | Iustin Pop | feedback_fn(" - ERROR: node hasn't returned node ssh connectivity data")
|
426 | a8083063 | Iustin Pop | else:
|
427 | a8083063 | Iustin Pop | if node_result['nodelist']: |
428 | a8083063 | Iustin Pop | bad = True
|
429 | a8083063 | Iustin Pop | for node in node_result['nodelist']: |
430 | 9d4bfc96 | Iustin Pop | feedback_fn(" - ERROR: ssh communication with node '%s': %s" %
|
431 | a8083063 | Iustin Pop | (node, node_result['nodelist'][node]))
|
432 | 9d4bfc96 | Iustin Pop | if 'node-net-test' not in node_result: |
433 | 9d4bfc96 | Iustin Pop | bad = True
|
434 | 9d4bfc96 | Iustin Pop | feedback_fn(" - ERROR: node hasn't returned node tcp connectivity data")
|
435 | 9d4bfc96 | Iustin Pop | else:
|
436 | 9d4bfc96 | Iustin Pop | if node_result['node-net-test']: |
437 | 9d4bfc96 | Iustin Pop | bad = True
|
438 | 9d4bfc96 | Iustin Pop | nlist = utils.NiceSort(node_result['node-net-test'].keys())
|
439 | 9d4bfc96 | Iustin Pop | for node in nlist: |
440 | 9d4bfc96 | Iustin Pop | feedback_fn(" - ERROR: tcp communication with node '%s': %s" %
|
441 | 9d4bfc96 | Iustin Pop | (node, node_result['node-net-test'][node]))
|
442 | 9d4bfc96 | Iustin Pop | |
443 | a8083063 | Iustin Pop | hyp_result = node_result.get('hypervisor', None) |
444 | a8083063 | Iustin Pop | if hyp_result is not None: |
445 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: hypervisor verify failure: '%s'" % hyp_result)
|
446 | a8083063 | Iustin Pop | return bad
|
447 | a8083063 | Iustin Pop | |
448 | c5705f58 | Guido Trotter | def _VerifyInstance(self, instance, instanceconfig, node_vol_is, |
449 | c5705f58 | Guido Trotter | node_instance, feedback_fn): |
450 | a8083063 | Iustin Pop | """Verify an instance.
|
451 | a8083063 | Iustin Pop |
|
452 | a8083063 | Iustin Pop | This function checks to see if the required block devices are
|
453 | a8083063 | Iustin Pop | available on the instance's node.
|
454 | a8083063 | Iustin Pop |
|
455 | a8083063 | Iustin Pop | """
|
456 | a8083063 | Iustin Pop | bad = False
|
457 | a8083063 | Iustin Pop | |
458 | a8083063 | Iustin Pop | node_current = instanceconfig.primary_node |
459 | a8083063 | Iustin Pop | |
460 | a8083063 | Iustin Pop | node_vol_should = {} |
461 | a8083063 | Iustin Pop | instanceconfig.MapLVsByNode(node_vol_should) |
462 | a8083063 | Iustin Pop | |
463 | a8083063 | Iustin Pop | for node in node_vol_should: |
464 | a8083063 | Iustin Pop | for volume in node_vol_should[node]: |
465 | a8083063 | Iustin Pop | if node not in node_vol_is or volume not in node_vol_is[node]: |
466 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: volume %s missing on node %s" %
|
467 | a8083063 | Iustin Pop | (volume, node)) |
468 | a8083063 | Iustin Pop | bad = True
|
469 | a8083063 | Iustin Pop | |
470 | a8083063 | Iustin Pop | if not instanceconfig.status == 'down': |
471 | a872dae6 | Guido Trotter | if (node_current not in node_instance or |
472 | a872dae6 | Guido Trotter | not instance in node_instance[node_current]): |
473 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: instance %s not running on node %s" %
|
474 | a8083063 | Iustin Pop | (instance, node_current)) |
475 | a8083063 | Iustin Pop | bad = True
|
476 | a8083063 | Iustin Pop | |
477 | a8083063 | Iustin Pop | for node in node_instance: |
478 | a8083063 | Iustin Pop | if (not node == node_current): |
479 | a8083063 | Iustin Pop | if instance in node_instance[node]: |
480 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: instance %s should not run on node %s" %
|
481 | a8083063 | Iustin Pop | (instance, node)) |
482 | a8083063 | Iustin Pop | bad = True
|
483 | a8083063 | Iustin Pop | |
484 | 6a438c98 | Michael Hanselmann | return bad
|
485 | a8083063 | Iustin Pop | |
486 | a8083063 | Iustin Pop | def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn): |
487 | a8083063 | Iustin Pop | """Verify if there are any unknown volumes in the cluster.
|
488 | a8083063 | Iustin Pop |
|
489 | a8083063 | Iustin Pop | The .os, .swap and backup volumes are ignored. All other volumes are
|
490 | a8083063 | Iustin Pop | reported as unknown.
|
491 | a8083063 | Iustin Pop |
|
492 | a8083063 | Iustin Pop | """
|
493 | a8083063 | Iustin Pop | bad = False
|
494 | a8083063 | Iustin Pop | |
495 | a8083063 | Iustin Pop | for node in node_vol_is: |
496 | a8083063 | Iustin Pop | for volume in node_vol_is[node]: |
497 | a8083063 | Iustin Pop | if node not in node_vol_should or volume not in node_vol_should[node]: |
498 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: volume %s on node %s should not exist" %
|
499 | a8083063 | Iustin Pop | (volume, node)) |
500 | a8083063 | Iustin Pop | bad = True
|
501 | a8083063 | Iustin Pop | return bad
|
502 | a8083063 | Iustin Pop | |
503 | a8083063 | Iustin Pop | def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn): |
504 | a8083063 | Iustin Pop | """Verify the list of running instances.
|
505 | a8083063 | Iustin Pop |
|
506 | a8083063 | Iustin Pop | This checks what instances are running but unknown to the cluster.
|
507 | a8083063 | Iustin Pop |
|
508 | a8083063 | Iustin Pop | """
|
509 | a8083063 | Iustin Pop | bad = False
|
510 | a8083063 | Iustin Pop | for node in node_instance: |
511 | a8083063 | Iustin Pop | for runninginstance in node_instance[node]: |
512 | a8083063 | Iustin Pop | if runninginstance not in instancelist: |
513 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: instance %s on node %s should not exist" %
|
514 | a8083063 | Iustin Pop | (runninginstance, node)) |
515 | a8083063 | Iustin Pop | bad = True
|
516 | a8083063 | Iustin Pop | return bad
|
517 | a8083063 | Iustin Pop | |
518 | 2b3b6ddd | Guido Trotter | def _VerifyNPlusOneMemory(self, node_info, instance_cfg, feedback_fn): |
519 | 2b3b6ddd | Guido Trotter | """Verify N+1 Memory Resilience.
|
520 | 2b3b6ddd | Guido Trotter |
|
521 | 2b3b6ddd | Guido Trotter | Check that if one single node dies we can still start all the instances it
|
522 | 2b3b6ddd | Guido Trotter | was primary for.
|
523 | 2b3b6ddd | Guido Trotter |
|
524 | 2b3b6ddd | Guido Trotter | """
|
525 | 2b3b6ddd | Guido Trotter | bad = False
|
526 | 2b3b6ddd | Guido Trotter | |
527 | 2b3b6ddd | Guido Trotter | for node, nodeinfo in node_info.iteritems(): |
528 | 2b3b6ddd | Guido Trotter | # This code checks that every node which is now listed as secondary has
|
529 | 2b3b6ddd | Guido Trotter | # enough memory to host all instances it is supposed to should a single
|
530 | 2b3b6ddd | Guido Trotter | # other node in the cluster fail.
|
531 | 2b3b6ddd | Guido Trotter | # FIXME: not ready for failover to an arbitrary node
|
532 | 2b3b6ddd | Guido Trotter | # FIXME: does not support file-backed instances
|
533 | 2b3b6ddd | Guido Trotter | # WARNING: we currently take into account down instances as well as up
|
534 | 2b3b6ddd | Guido Trotter | # ones, considering that even if they're down someone might want to start
|
535 | 2b3b6ddd | Guido Trotter | # them even in the event of a node failure.
|
536 | 2b3b6ddd | Guido Trotter | for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems(): |
537 | 2b3b6ddd | Guido Trotter | needed_mem = 0
|
538 | 2b3b6ddd | Guido Trotter | for instance in instances: |
539 | 2b3b6ddd | Guido Trotter | needed_mem += instance_cfg[instance].memory |
540 | 2b3b6ddd | Guido Trotter | if nodeinfo['mfree'] < needed_mem: |
541 | 2b3b6ddd | Guido Trotter | feedback_fn(" - ERROR: not enough memory on node %s to accomodate"
|
542 | 2b3b6ddd | Guido Trotter | " failovers should node %s fail" % (node, prinode))
|
543 | 2b3b6ddd | Guido Trotter | bad = True
|
544 | 2b3b6ddd | Guido Trotter | return bad
|
545 | 2b3b6ddd | Guido Trotter | |
546 | a8083063 | Iustin Pop | def CheckPrereq(self): |
547 | a8083063 | Iustin Pop | """Check prerequisites.
|
548 | a8083063 | Iustin Pop |
|
549 | e54c4c5e | Guido Trotter | Transform the list of checks we're going to skip into a set and check that
|
550 | e54c4c5e | Guido Trotter | all its members are valid.
|
551 | a8083063 | Iustin Pop |
|
552 | a8083063 | Iustin Pop | """
|
553 | e54c4c5e | Guido Trotter | self.skip_set = frozenset(self.op.skip_checks) |
554 | e54c4c5e | Guido Trotter | if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set): |
555 | e54c4c5e | Guido Trotter | raise errors.OpPrereqError("Invalid checks to be skipped specified") |
556 | a8083063 | Iustin Pop | |
557 | d8fff41c | Guido Trotter | def BuildHooksEnv(self): |
558 | d8fff41c | Guido Trotter | """Build hooks env.
|
559 | d8fff41c | Guido Trotter |
|
560 | d8fff41c | Guido Trotter | Cluster-Verify hooks just rone in the post phase and their failure makes
|
561 | d8fff41c | Guido Trotter | the output be logged in the verify output and the verification to fail.
|
562 | d8fff41c | Guido Trotter |
|
563 | d8fff41c | Guido Trotter | """
|
564 | d8fff41c | Guido Trotter | all_nodes = self.cfg.GetNodeList()
|
565 | d8fff41c | Guido Trotter | # TODO: populate the environment with useful information for verify hooks
|
566 | d8fff41c | Guido Trotter | env = {} |
567 | d8fff41c | Guido Trotter | return env, [], all_nodes
|
568 | d8fff41c | Guido Trotter | |
569 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
570 | a8083063 | Iustin Pop | """Verify integrity of cluster, performing various test on nodes.
|
571 | a8083063 | Iustin Pop |
|
572 | a8083063 | Iustin Pop | """
|
573 | a8083063 | Iustin Pop | bad = False
|
574 | a8083063 | Iustin Pop | feedback_fn("* Verifying global settings")
|
575 | 8522ceeb | Iustin Pop | for msg in self.cfg.VerifyConfig(): |
576 | 8522ceeb | Iustin Pop | feedback_fn(" - ERROR: %s" % msg)
|
577 | a8083063 | Iustin Pop | |
578 | a8083063 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
579 | a8083063 | Iustin Pop | nodelist = utils.NiceSort(self.cfg.GetNodeList())
|
580 | 9d4bfc96 | Iustin Pop | nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist] |
581 | a8083063 | Iustin Pop | instancelist = utils.NiceSort(self.cfg.GetInstanceList())
|
582 | 93e4c50b | Guido Trotter | i_non_redundant = [] # Non redundant instances
|
583 | a8083063 | Iustin Pop | node_volume = {} |
584 | a8083063 | Iustin Pop | node_instance = {} |
585 | 9c9c7d30 | Guido Trotter | node_info = {} |
586 | 26b6af5e | Guido Trotter | instance_cfg = {} |
587 | a8083063 | Iustin Pop | |
588 | a8083063 | Iustin Pop | # FIXME: verify OS list
|
589 | a8083063 | Iustin Pop | # do local checksums
|
590 | cb91d46e | Iustin Pop | file_names = list(self.sstore.GetFileList()) |
591 | cb91d46e | Iustin Pop | file_names.append(constants.SSL_CERT_FILE) |
592 | cb91d46e | Iustin Pop | file_names.append(constants.CLUSTER_CONF_FILE) |
593 | a8083063 | Iustin Pop | local_checksums = utils.FingerprintFiles(file_names) |
594 | a8083063 | Iustin Pop | |
595 | a8083063 | Iustin Pop | feedback_fn("* Gathering data (%d nodes)" % len(nodelist)) |
596 | a8083063 | Iustin Pop | all_volumeinfo = rpc.call_volume_list(nodelist, vg_name) |
597 | a8083063 | Iustin Pop | all_instanceinfo = rpc.call_instance_list(nodelist) |
598 | a8083063 | Iustin Pop | all_vglist = rpc.call_vg_list(nodelist) |
599 | a8083063 | Iustin Pop | node_verify_param = { |
600 | a8083063 | Iustin Pop | 'filelist': file_names,
|
601 | a8083063 | Iustin Pop | 'nodelist': nodelist,
|
602 | a8083063 | Iustin Pop | 'hypervisor': None, |
603 | 9d4bfc96 | Iustin Pop | 'node-net-test': [(node.name, node.primary_ip, node.secondary_ip)
|
604 | 9d4bfc96 | Iustin Pop | for node in nodeinfo] |
605 | a8083063 | Iustin Pop | } |
606 | a8083063 | Iustin Pop | all_nvinfo = rpc.call_node_verify(nodelist, node_verify_param) |
607 | a8083063 | Iustin Pop | all_rversion = rpc.call_version(nodelist) |
608 | 9c9c7d30 | Guido Trotter | all_ninfo = rpc.call_node_info(nodelist, self.cfg.GetVGName())
|
609 | a8083063 | Iustin Pop | |
610 | a8083063 | Iustin Pop | for node in nodelist: |
611 | a8083063 | Iustin Pop | feedback_fn("* Verifying node %s" % node)
|
612 | a8083063 | Iustin Pop | result = self._VerifyNode(node, file_names, local_checksums,
|
613 | a8083063 | Iustin Pop | all_vglist[node], all_nvinfo[node], |
614 | a8083063 | Iustin Pop | all_rversion[node], feedback_fn) |
615 | a8083063 | Iustin Pop | bad = bad or result
|
616 | a8083063 | Iustin Pop | |
617 | a8083063 | Iustin Pop | # node_volume
|
618 | a8083063 | Iustin Pop | volumeinfo = all_volumeinfo[node] |
619 | a8083063 | Iustin Pop | |
620 | b63ed789 | Iustin Pop | if isinstance(volumeinfo, basestring): |
621 | b63ed789 | Iustin Pop | feedback_fn(" - ERROR: LVM problem on node %s: %s" %
|
622 | b63ed789 | Iustin Pop | (node, volumeinfo[-400:].encode('string_escape'))) |
623 | b63ed789 | Iustin Pop | bad = True
|
624 | b63ed789 | Iustin Pop | node_volume[node] = {} |
625 | b63ed789 | Iustin Pop | elif not isinstance(volumeinfo, dict): |
626 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: connection to %s failed" % (node,))
|
627 | a8083063 | Iustin Pop | bad = True
|
628 | a8083063 | Iustin Pop | continue
|
629 | b63ed789 | Iustin Pop | else:
|
630 | b63ed789 | Iustin Pop | node_volume[node] = volumeinfo |
631 | a8083063 | Iustin Pop | |
632 | a8083063 | Iustin Pop | # node_instance
|
633 | a8083063 | Iustin Pop | nodeinstance = all_instanceinfo[node] |
634 | a8083063 | Iustin Pop | if type(nodeinstance) != list: |
635 | a8083063 | Iustin Pop | feedback_fn(" - ERROR: connection to %s failed" % (node,))
|
636 | a8083063 | Iustin Pop | bad = True
|
637 | a8083063 | Iustin Pop | continue
|
638 | a8083063 | Iustin Pop | |
639 | a8083063 | Iustin Pop | node_instance[node] = nodeinstance |
640 | a8083063 | Iustin Pop | |
641 | 9c9c7d30 | Guido Trotter | # node_info
|
642 | 9c9c7d30 | Guido Trotter | nodeinfo = all_ninfo[node] |
643 | 9c9c7d30 | Guido Trotter | if not isinstance(nodeinfo, dict): |
644 | 9c9c7d30 | Guido Trotter | feedback_fn(" - ERROR: connection to %s failed" % (node,))
|
645 | 9c9c7d30 | Guido Trotter | bad = True
|
646 | 9c9c7d30 | Guido Trotter | continue
|
647 | 9c9c7d30 | Guido Trotter | |
648 | 9c9c7d30 | Guido Trotter | try:
|
649 | 9c9c7d30 | Guido Trotter | node_info[node] = { |
650 | 9c9c7d30 | Guido Trotter | "mfree": int(nodeinfo['memory_free']), |
651 | 9c9c7d30 | Guido Trotter | "dfree": int(nodeinfo['vg_free']), |
652 | 93e4c50b | Guido Trotter | "pinst": [],
|
653 | 93e4c50b | Guido Trotter | "sinst": [],
|
654 | 36e7da50 | Guido Trotter | # dictionary holding all instances this node is secondary for,
|
655 | 36e7da50 | Guido Trotter | # grouped by their primary node. Each key is a cluster node, and each
|
656 | 36e7da50 | Guido Trotter | # value is a list of instances which have the key as primary and the
|
657 | 36e7da50 | Guido Trotter | # current node as secondary. this is handy to calculate N+1 memory
|
658 | 36e7da50 | Guido Trotter | # availability if you can only failover from a primary to its
|
659 | 36e7da50 | Guido Trotter | # secondary.
|
660 | 36e7da50 | Guido Trotter | "sinst-by-pnode": {},
|
661 | 9c9c7d30 | Guido Trotter | } |
662 | 9c9c7d30 | Guido Trotter | except ValueError: |
663 | 9c9c7d30 | Guido Trotter | feedback_fn(" - ERROR: invalid value returned from node %s" % (node,))
|
664 | 9c9c7d30 | Guido Trotter | bad = True
|
665 | 9c9c7d30 | Guido Trotter | continue
|
666 | 9c9c7d30 | Guido Trotter | |
667 | a8083063 | Iustin Pop | node_vol_should = {} |
668 | a8083063 | Iustin Pop | |
669 | a8083063 | Iustin Pop | for instance in instancelist: |
670 | a8083063 | Iustin Pop | feedback_fn("* Verifying instance %s" % instance)
|
671 | a8083063 | Iustin Pop | inst_config = self.cfg.GetInstanceInfo(instance)
|
672 | c5705f58 | Guido Trotter | result = self._VerifyInstance(instance, inst_config, node_volume,
|
673 | c5705f58 | Guido Trotter | node_instance, feedback_fn) |
674 | c5705f58 | Guido Trotter | bad = bad or result
|
675 | a8083063 | Iustin Pop | |
676 | a8083063 | Iustin Pop | inst_config.MapLVsByNode(node_vol_should) |
677 | a8083063 | Iustin Pop | |
678 | 26b6af5e | Guido Trotter | instance_cfg[instance] = inst_config |
679 | 26b6af5e | Guido Trotter | |
680 | 93e4c50b | Guido Trotter | pnode = inst_config.primary_node |
681 | 93e4c50b | Guido Trotter | if pnode in node_info: |
682 | 93e4c50b | Guido Trotter | node_info[pnode]['pinst'].append(instance)
|
683 | 93e4c50b | Guido Trotter | else:
|
684 | 93e4c50b | Guido Trotter | feedback_fn(" - ERROR: instance %s, connection to primary node"
|
685 | 93e4c50b | Guido Trotter | " %s failed" % (instance, pnode))
|
686 | 93e4c50b | Guido Trotter | bad = True
|
687 | 93e4c50b | Guido Trotter | |
688 | 93e4c50b | Guido Trotter | # If the instance is non-redundant we cannot survive losing its primary
|
689 | 93e4c50b | Guido Trotter | # node, so we are not N+1 compliant. On the other hand we have no disk
|
690 | 93e4c50b | Guido Trotter | # templates with more than one secondary so that situation is not well
|
691 | 93e4c50b | Guido Trotter | # supported either.
|
692 | 93e4c50b | Guido Trotter | # FIXME: does not support file-backed instances
|
693 | 93e4c50b | Guido Trotter | if len(inst_config.secondary_nodes) == 0: |
694 | 93e4c50b | Guido Trotter | i_non_redundant.append(instance) |
695 | 93e4c50b | Guido Trotter | elif len(inst_config.secondary_nodes) > 1: |
696 | 93e4c50b | Guido Trotter | feedback_fn(" - WARNING: multiple secondaries for instance %s"
|
697 | 93e4c50b | Guido Trotter | % instance) |
698 | 93e4c50b | Guido Trotter | |
699 | 93e4c50b | Guido Trotter | for snode in inst_config.secondary_nodes: |
700 | 93e4c50b | Guido Trotter | if snode in node_info: |
701 | 93e4c50b | Guido Trotter | node_info[snode]['sinst'].append(instance)
|
702 | 36e7da50 | Guido Trotter | if pnode not in node_info[snode]['sinst-by-pnode']: |
703 | 36e7da50 | Guido Trotter | node_info[snode]['sinst-by-pnode'][pnode] = []
|
704 | 36e7da50 | Guido Trotter | node_info[snode]['sinst-by-pnode'][pnode].append(instance)
|
705 | 93e4c50b | Guido Trotter | else:
|
706 | 93e4c50b | Guido Trotter | feedback_fn(" - ERROR: instance %s, connection to secondary node"
|
707 | 93e4c50b | Guido Trotter | " %s failed" % (instance, snode))
|
708 | 93e4c50b | Guido Trotter | |
709 | a8083063 | Iustin Pop | feedback_fn("* Verifying orphan volumes")
|
710 | a8083063 | Iustin Pop | result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
|
711 | a8083063 | Iustin Pop | feedback_fn) |
712 | a8083063 | Iustin Pop | bad = bad or result
|
713 | a8083063 | Iustin Pop | |
714 | a8083063 | Iustin Pop | feedback_fn("* Verifying remaining instances")
|
715 | a8083063 | Iustin Pop | result = self._VerifyOrphanInstances(instancelist, node_instance,
|
716 | a8083063 | Iustin Pop | feedback_fn) |
717 | a8083063 | Iustin Pop | bad = bad or result
|
718 | a8083063 | Iustin Pop | |
719 | e54c4c5e | Guido Trotter | if constants.VERIFY_NPLUSONE_MEM not in self.skip_set: |
720 | e54c4c5e | Guido Trotter | feedback_fn("* Verifying N+1 Memory redundancy")
|
721 | e54c4c5e | Guido Trotter | result = self._VerifyNPlusOneMemory(node_info, instance_cfg, feedback_fn)
|
722 | e54c4c5e | Guido Trotter | bad = bad or result
|
723 | 2b3b6ddd | Guido Trotter | |
724 | 2b3b6ddd | Guido Trotter | feedback_fn("* Other Notes")
|
725 | 2b3b6ddd | Guido Trotter | if i_non_redundant:
|
726 | 2b3b6ddd | Guido Trotter | feedback_fn(" - NOTICE: %d non-redundant instance(s) found."
|
727 | 2b3b6ddd | Guido Trotter | % len(i_non_redundant))
|
728 | 2b3b6ddd | Guido Trotter | |
729 | a8083063 | Iustin Pop | return int(bad) |
730 | a8083063 | Iustin Pop | |
731 | d8fff41c | Guido Trotter | def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result): |
732 | d8fff41c | Guido Trotter | """Analize the post-hooks' result, handle it, and send some
|
733 | d8fff41c | Guido Trotter | nicely-formatted feedback back to the user.
|
734 | d8fff41c | Guido Trotter |
|
735 | d8fff41c | Guido Trotter | Args:
|
736 | d8fff41c | Guido Trotter | phase: the hooks phase that has just been run
|
737 | d8fff41c | Guido Trotter | hooks_results: the results of the multi-node hooks rpc call
|
738 | d8fff41c | Guido Trotter | feedback_fn: function to send feedback back to the caller
|
739 | d8fff41c | Guido Trotter | lu_result: previous Exec result
|
740 | d8fff41c | Guido Trotter |
|
741 | d8fff41c | Guido Trotter | """
|
742 | d8fff41c | Guido Trotter | # We only really run POST phase hooks, and are only interested in their results
|
743 | d8fff41c | Guido Trotter | if phase == constants.HOOKS_PHASE_POST:
|
744 | d8fff41c | Guido Trotter | # Used to change hooks' output to proper indentation
|
745 | d8fff41c | Guido Trotter | indent_re = re.compile('^', re.M)
|
746 | d8fff41c | Guido Trotter | feedback_fn("* Hooks Results")
|
747 | d8fff41c | Guido Trotter | if not hooks_results: |
748 | d8fff41c | Guido Trotter | feedback_fn(" - ERROR: general communication failure")
|
749 | d8fff41c | Guido Trotter | lu_result = 1
|
750 | d8fff41c | Guido Trotter | else:
|
751 | d8fff41c | Guido Trotter | for node_name in hooks_results: |
752 | d8fff41c | Guido Trotter | show_node_header = True
|
753 | d8fff41c | Guido Trotter | res = hooks_results[node_name] |
754 | d8fff41c | Guido Trotter | if res is False or not isinstance(res, list): |
755 | d8fff41c | Guido Trotter | feedback_fn(" Communication failure")
|
756 | d8fff41c | Guido Trotter | lu_result = 1
|
757 | d8fff41c | Guido Trotter | continue
|
758 | d8fff41c | Guido Trotter | for script, hkr, output in res: |
759 | d8fff41c | Guido Trotter | if hkr == constants.HKR_FAIL:
|
760 | d8fff41c | Guido Trotter | # The node header is only shown once, if there are
|
761 | d8fff41c | Guido Trotter | # failing hooks on that node
|
762 | d8fff41c | Guido Trotter | if show_node_header:
|
763 | d8fff41c | Guido Trotter | feedback_fn(" Node %s:" % node_name)
|
764 | d8fff41c | Guido Trotter | show_node_header = False
|
765 | d8fff41c | Guido Trotter | feedback_fn(" ERROR: Script %s failed, output:" % script)
|
766 | d8fff41c | Guido Trotter | output = indent_re.sub(' ', output)
|
767 | d8fff41c | Guido Trotter | feedback_fn("%s" % output)
|
768 | d8fff41c | Guido Trotter | lu_result = 1
|
769 | d8fff41c | Guido Trotter | |
770 | d8fff41c | Guido Trotter | return lu_result
|
771 | d8fff41c | Guido Trotter | |
772 | a8083063 | Iustin Pop | |
773 | 2c95a8d4 | Iustin Pop | class LUVerifyDisks(NoHooksLU): |
774 | 2c95a8d4 | Iustin Pop | """Verifies the cluster disks status.
|
775 | 2c95a8d4 | Iustin Pop |
|
776 | 2c95a8d4 | Iustin Pop | """
|
777 | 2c95a8d4 | Iustin Pop | _OP_REQP = [] |
778 | 2c95a8d4 | Iustin Pop | |
779 | 2c95a8d4 | Iustin Pop | def CheckPrereq(self): |
780 | 2c95a8d4 | Iustin Pop | """Check prerequisites.
|
781 | 2c95a8d4 | Iustin Pop |
|
782 | 2c95a8d4 | Iustin Pop | This has no prerequisites.
|
783 | 2c95a8d4 | Iustin Pop |
|
784 | 2c95a8d4 | Iustin Pop | """
|
785 | 2c95a8d4 | Iustin Pop | pass
|
786 | 2c95a8d4 | Iustin Pop | |
787 | 2c95a8d4 | Iustin Pop | def Exec(self, feedback_fn): |
788 | 2c95a8d4 | Iustin Pop | """Verify integrity of cluster disks.
|
789 | 2c95a8d4 | Iustin Pop |
|
790 | 2c95a8d4 | Iustin Pop | """
|
791 | b63ed789 | Iustin Pop | result = res_nodes, res_nlvm, res_instances, res_missing = [], {}, [], {} |
792 | 2c95a8d4 | Iustin Pop | |
793 | 2c95a8d4 | Iustin Pop | vg_name = self.cfg.GetVGName()
|
794 | 2c95a8d4 | Iustin Pop | nodes = utils.NiceSort(self.cfg.GetNodeList())
|
795 | 2c95a8d4 | Iustin Pop | instances = [self.cfg.GetInstanceInfo(name)
|
796 | 2c95a8d4 | Iustin Pop | for name in self.cfg.GetInstanceList()] |
797 | 2c95a8d4 | Iustin Pop | |
798 | 2c95a8d4 | Iustin Pop | nv_dict = {} |
799 | 2c95a8d4 | Iustin Pop | for inst in instances: |
800 | 2c95a8d4 | Iustin Pop | inst_lvs = {} |
801 | 2c95a8d4 | Iustin Pop | if (inst.status != "up" or |
802 | 2c95a8d4 | Iustin Pop | inst.disk_template not in constants.DTS_NET_MIRROR): |
803 | 2c95a8d4 | Iustin Pop | continue
|
804 | 2c95a8d4 | Iustin Pop | inst.MapLVsByNode(inst_lvs) |
805 | 2c95a8d4 | Iustin Pop | # transform { iname: {node: [vol,],},} to {(node, vol): iname}
|
806 | 2c95a8d4 | Iustin Pop | for node, vol_list in inst_lvs.iteritems(): |
807 | 2c95a8d4 | Iustin Pop | for vol in vol_list: |
808 | 2c95a8d4 | Iustin Pop | nv_dict[(node, vol)] = inst |
809 | 2c95a8d4 | Iustin Pop | |
810 | 2c95a8d4 | Iustin Pop | if not nv_dict: |
811 | 2c95a8d4 | Iustin Pop | return result
|
812 | 2c95a8d4 | Iustin Pop | |
813 | 2c95a8d4 | Iustin Pop | node_lvs = rpc.call_volume_list(nodes, vg_name) |
814 | 2c95a8d4 | Iustin Pop | |
815 | 2c95a8d4 | Iustin Pop | to_act = set()
|
816 | 2c95a8d4 | Iustin Pop | for node in nodes: |
817 | 2c95a8d4 | Iustin Pop | # node_volume
|
818 | 2c95a8d4 | Iustin Pop | lvs = node_lvs[node] |
819 | 2c95a8d4 | Iustin Pop | |
820 | b63ed789 | Iustin Pop | if isinstance(lvs, basestring): |
821 | b63ed789 | Iustin Pop | logger.Info("error enumerating LVs on node %s: %s" % (node, lvs))
|
822 | b63ed789 | Iustin Pop | res_nlvm[node] = lvs |
823 | b63ed789 | Iustin Pop | elif not isinstance(lvs, dict): |
824 | 2c95a8d4 | Iustin Pop | logger.Info("connection to node %s failed or invalid data returned" %
|
825 | 2c95a8d4 | Iustin Pop | (node,)) |
826 | 2c95a8d4 | Iustin Pop | res_nodes.append(node) |
827 | 2c95a8d4 | Iustin Pop | continue
|
828 | 2c95a8d4 | Iustin Pop | |
829 | 2c95a8d4 | Iustin Pop | for lv_name, (_, lv_inactive, lv_online) in lvs.iteritems(): |
830 | b63ed789 | Iustin Pop | inst = nv_dict.pop((node, lv_name), None)
|
831 | b63ed789 | Iustin Pop | if (not lv_online and inst is not None |
832 | b63ed789 | Iustin Pop | and inst.name not in res_instances): |
833 | b08d5a87 | Iustin Pop | res_instances.append(inst.name) |
834 | 2c95a8d4 | Iustin Pop | |
835 | b63ed789 | Iustin Pop | # any leftover items in nv_dict are missing LVs, let's arrange the
|
836 | b63ed789 | Iustin Pop | # data better
|
837 | b63ed789 | Iustin Pop | for key, inst in nv_dict.iteritems(): |
838 | b63ed789 | Iustin Pop | if inst.name not in res_missing: |
839 | b63ed789 | Iustin Pop | res_missing[inst.name] = [] |
840 | b63ed789 | Iustin Pop | res_missing[inst.name].append(key) |
841 | b63ed789 | Iustin Pop | |
842 | 2c95a8d4 | Iustin Pop | return result
|
843 | 2c95a8d4 | Iustin Pop | |
844 | 2c95a8d4 | Iustin Pop | |
845 | 07bd8a51 | Iustin Pop | class LURenameCluster(LogicalUnit): |
846 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
847 | 07bd8a51 | Iustin Pop |
|
848 | 07bd8a51 | Iustin Pop | """
|
849 | 07bd8a51 | Iustin Pop | HPATH = "cluster-rename"
|
850 | 07bd8a51 | Iustin Pop | HTYPE = constants.HTYPE_CLUSTER |
851 | 07bd8a51 | Iustin Pop | _OP_REQP = ["name"]
|
852 | 07bd8a51 | Iustin Pop | |
853 | 07bd8a51 | Iustin Pop | def BuildHooksEnv(self): |
854 | 07bd8a51 | Iustin Pop | """Build hooks env.
|
855 | 07bd8a51 | Iustin Pop |
|
856 | 07bd8a51 | Iustin Pop | """
|
857 | 07bd8a51 | Iustin Pop | env = { |
858 | 488b540d | Iustin Pop | "OP_TARGET": self.sstore.GetClusterName(), |
859 | 07bd8a51 | Iustin Pop | "NEW_NAME": self.op.name, |
860 | 07bd8a51 | Iustin Pop | } |
861 | 07bd8a51 | Iustin Pop | mn = self.sstore.GetMasterNode()
|
862 | 07bd8a51 | Iustin Pop | return env, [mn], [mn]
|
863 | 07bd8a51 | Iustin Pop | |
864 | 07bd8a51 | Iustin Pop | def CheckPrereq(self): |
865 | 07bd8a51 | Iustin Pop | """Verify that the passed name is a valid one.
|
866 | 07bd8a51 | Iustin Pop |
|
867 | 07bd8a51 | Iustin Pop | """
|
868 | 89e1fc26 | Iustin Pop | hostname = utils.HostInfo(self.op.name)
|
869 | 07bd8a51 | Iustin Pop | |
870 | bcf043c9 | Iustin Pop | new_name = hostname.name |
871 | bcf043c9 | Iustin Pop | self.ip = new_ip = hostname.ip
|
872 | 07bd8a51 | Iustin Pop | old_name = self.sstore.GetClusterName()
|
873 | 07bd8a51 | Iustin Pop | old_ip = self.sstore.GetMasterIP()
|
874 | 07bd8a51 | Iustin Pop | if new_name == old_name and new_ip == old_ip: |
875 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("Neither the name nor the IP address of the" |
876 | 07bd8a51 | Iustin Pop | " cluster has changed")
|
877 | 07bd8a51 | Iustin Pop | if new_ip != old_ip:
|
878 | 07bd8a51 | Iustin Pop | result = utils.RunCmd(["fping", "-q", new_ip]) |
879 | 07bd8a51 | Iustin Pop | if not result.failed: |
880 | 07bd8a51 | Iustin Pop | raise errors.OpPrereqError("The given cluster IP address (%s) is" |
881 | 07bd8a51 | Iustin Pop | " reachable on the network. Aborting." %
|
882 | 07bd8a51 | Iustin Pop | new_ip) |
883 | 07bd8a51 | Iustin Pop | |
884 | 07bd8a51 | Iustin Pop | self.op.name = new_name
|
885 | 07bd8a51 | Iustin Pop | |
886 | 07bd8a51 | Iustin Pop | def Exec(self, feedback_fn): |
887 | 07bd8a51 | Iustin Pop | """Rename the cluster.
|
888 | 07bd8a51 | Iustin Pop |
|
889 | 07bd8a51 | Iustin Pop | """
|
890 | 07bd8a51 | Iustin Pop | clustername = self.op.name
|
891 | 07bd8a51 | Iustin Pop | ip = self.ip
|
892 | 07bd8a51 | Iustin Pop | ss = self.sstore
|
893 | 07bd8a51 | Iustin Pop | |
894 | 07bd8a51 | Iustin Pop | # shutdown the master IP
|
895 | 07bd8a51 | Iustin Pop | master = ss.GetMasterNode() |
896 | 07bd8a51 | Iustin Pop | if not rpc.call_node_stop_master(master): |
897 | 07bd8a51 | Iustin Pop | raise errors.OpExecError("Could not disable the master role") |
898 | 07bd8a51 | Iustin Pop | |
899 | 07bd8a51 | Iustin Pop | try:
|
900 | 07bd8a51 | Iustin Pop | # modify the sstore
|
901 | 07bd8a51 | Iustin Pop | ss.SetKey(ss.SS_MASTER_IP, ip) |
902 | 07bd8a51 | Iustin Pop | ss.SetKey(ss.SS_CLUSTER_NAME, clustername) |
903 | 07bd8a51 | Iustin Pop | |
904 | 07bd8a51 | Iustin Pop | # Distribute updated ss config to all nodes
|
905 | 07bd8a51 | Iustin Pop | myself = self.cfg.GetNodeInfo(master)
|
906 | 07bd8a51 | Iustin Pop | dist_nodes = self.cfg.GetNodeList()
|
907 | 07bd8a51 | Iustin Pop | if myself.name in dist_nodes: |
908 | 07bd8a51 | Iustin Pop | dist_nodes.remove(myself.name) |
909 | 07bd8a51 | Iustin Pop | |
910 | 07bd8a51 | Iustin Pop | logger.Debug("Copying updated ssconf data to all nodes")
|
911 | 07bd8a51 | Iustin Pop | for keyname in [ss.SS_CLUSTER_NAME, ss.SS_MASTER_IP]: |
912 | 07bd8a51 | Iustin Pop | fname = ss.KeyToFilename(keyname) |
913 | 07bd8a51 | Iustin Pop | result = rpc.call_upload_file(dist_nodes, fname) |
914 | 07bd8a51 | Iustin Pop | for to_node in dist_nodes: |
915 | 07bd8a51 | Iustin Pop | if not result[to_node]: |
916 | 07bd8a51 | Iustin Pop | logger.Error("copy of file %s to node %s failed" %
|
917 | 07bd8a51 | Iustin Pop | (fname, to_node)) |
918 | 07bd8a51 | Iustin Pop | finally:
|
919 | 07bd8a51 | Iustin Pop | if not rpc.call_node_start_master(master): |
920 | f4bc1f2c | Michael Hanselmann | logger.Error("Could not re-enable the master role on the master,"
|
921 | f4bc1f2c | Michael Hanselmann | " please restart manually.")
|
922 | 07bd8a51 | Iustin Pop | |
923 | 07bd8a51 | Iustin Pop | |
924 | 8084f9f6 | Manuel Franceschini | def _RecursiveCheckIfLVMBased(disk): |
925 | 8084f9f6 | Manuel Franceschini | """Check if the given disk or its children are lvm-based.
|
926 | 8084f9f6 | Manuel Franceschini |
|
927 | 8084f9f6 | Manuel Franceschini | Args:
|
928 | 8084f9f6 | Manuel Franceschini | disk: ganeti.objects.Disk object
|
929 | 8084f9f6 | Manuel Franceschini |
|
930 | 8084f9f6 | Manuel Franceschini | Returns:
|
931 | 8084f9f6 | Manuel Franceschini | boolean indicating whether a LD_LV dev_type was found or not
|
932 | 8084f9f6 | Manuel Franceschini |
|
933 | 8084f9f6 | Manuel Franceschini | """
|
934 | 8084f9f6 | Manuel Franceschini | if disk.children:
|
935 | 8084f9f6 | Manuel Franceschini | for chdisk in disk.children: |
936 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(chdisk):
|
937 | 8084f9f6 | Manuel Franceschini | return True |
938 | 8084f9f6 | Manuel Franceschini | return disk.dev_type == constants.LD_LV
|
939 | 8084f9f6 | Manuel Franceschini | |
940 | 8084f9f6 | Manuel Franceschini | |
941 | 8084f9f6 | Manuel Franceschini | class LUSetClusterParams(LogicalUnit): |
942 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
943 | 8084f9f6 | Manuel Franceschini |
|
944 | 8084f9f6 | Manuel Franceschini | """
|
945 | 8084f9f6 | Manuel Franceschini | HPATH = "cluster-modify"
|
946 | 8084f9f6 | Manuel Franceschini | HTYPE = constants.HTYPE_CLUSTER |
947 | 8084f9f6 | Manuel Franceschini | _OP_REQP = [] |
948 | 8084f9f6 | Manuel Franceschini | |
949 | 8084f9f6 | Manuel Franceschini | def BuildHooksEnv(self): |
950 | 8084f9f6 | Manuel Franceschini | """Build hooks env.
|
951 | 8084f9f6 | Manuel Franceschini |
|
952 | 8084f9f6 | Manuel Franceschini | """
|
953 | 8084f9f6 | Manuel Franceschini | env = { |
954 | 8084f9f6 | Manuel Franceschini | "OP_TARGET": self.sstore.GetClusterName(), |
955 | 8084f9f6 | Manuel Franceschini | "NEW_VG_NAME": self.op.vg_name, |
956 | 8084f9f6 | Manuel Franceschini | } |
957 | 8084f9f6 | Manuel Franceschini | mn = self.sstore.GetMasterNode()
|
958 | 8084f9f6 | Manuel Franceschini | return env, [mn], [mn]
|
959 | 8084f9f6 | Manuel Franceschini | |
960 | 8084f9f6 | Manuel Franceschini | def CheckPrereq(self): |
961 | 8084f9f6 | Manuel Franceschini | """Check prerequisites.
|
962 | 8084f9f6 | Manuel Franceschini |
|
963 | 8084f9f6 | Manuel Franceschini | This checks whether the given params don't conflict and
|
964 | 5f83e263 | Iustin Pop | if the given volume group is valid.
|
965 | 8084f9f6 | Manuel Franceschini |
|
966 | 8084f9f6 | Manuel Franceschini | """
|
967 | 8084f9f6 | Manuel Franceschini | if not self.op.vg_name: |
968 | 8084f9f6 | Manuel Franceschini | instances = [self.cfg.GetInstanceInfo(name)
|
969 | 8084f9f6 | Manuel Franceschini | for name in self.cfg.GetInstanceList()] |
970 | 8084f9f6 | Manuel Franceschini | for inst in instances: |
971 | 8084f9f6 | Manuel Franceschini | for disk in inst.disks: |
972 | 8084f9f6 | Manuel Franceschini | if _RecursiveCheckIfLVMBased(disk):
|
973 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Cannot disable lvm storage while" |
974 | 8084f9f6 | Manuel Franceschini | " lvm-based instances exist")
|
975 | 8084f9f6 | Manuel Franceschini | |
976 | 8084f9f6 | Manuel Franceschini | # if vg_name not None, checks given volume group on all nodes
|
977 | 8084f9f6 | Manuel Franceschini | if self.op.vg_name: |
978 | 8084f9f6 | Manuel Franceschini | node_list = self.cfg.GetNodeList()
|
979 | 8084f9f6 | Manuel Franceschini | vglist = rpc.call_vg_list(node_list) |
980 | 8084f9f6 | Manuel Franceschini | for node in node_list: |
981 | 8d1a2a64 | Michael Hanselmann | vgstatus = utils.CheckVolumeGroupSize(vglist[node], self.op.vg_name,
|
982 | 8d1a2a64 | Michael Hanselmann | constants.MIN_VG_SIZE) |
983 | 8084f9f6 | Manuel Franceschini | if vgstatus:
|
984 | 8084f9f6 | Manuel Franceschini | raise errors.OpPrereqError("Error on node '%s': %s" % |
985 | 8084f9f6 | Manuel Franceschini | (node, vgstatus)) |
986 | 8084f9f6 | Manuel Franceschini | |
987 | 8084f9f6 | Manuel Franceschini | def Exec(self, feedback_fn): |
988 | 8084f9f6 | Manuel Franceschini | """Change the parameters of the cluster.
|
989 | 8084f9f6 | Manuel Franceschini |
|
990 | 8084f9f6 | Manuel Franceschini | """
|
991 | 8084f9f6 | Manuel Franceschini | if self.op.vg_name != self.cfg.GetVGName(): |
992 | 8084f9f6 | Manuel Franceschini | self.cfg.SetVGName(self.op.vg_name) |
993 | 8084f9f6 | Manuel Franceschini | else:
|
994 | 8084f9f6 | Manuel Franceschini | feedback_fn("Cluster LVM configuration already in desired"
|
995 | 8084f9f6 | Manuel Franceschini | " state, not changing")
|
996 | 8084f9f6 | Manuel Franceschini | |
997 | 8084f9f6 | Manuel Franceschini | |
998 | 5bfac263 | Iustin Pop | def _WaitForSync(cfgw, instance, proc, oneshot=False, unlock=False): |
999 | a8083063 | Iustin Pop | """Sleep and poll for an instance's disk to sync.
|
1000 | a8083063 | Iustin Pop |
|
1001 | a8083063 | Iustin Pop | """
|
1002 | a8083063 | Iustin Pop | if not instance.disks: |
1003 | a8083063 | Iustin Pop | return True |
1004 | a8083063 | Iustin Pop | |
1005 | a8083063 | Iustin Pop | if not oneshot: |
1006 | 5bfac263 | Iustin Pop | proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
|
1007 | a8083063 | Iustin Pop | |
1008 | a8083063 | Iustin Pop | node = instance.primary_node |
1009 | a8083063 | Iustin Pop | |
1010 | a8083063 | Iustin Pop | for dev in instance.disks: |
1011 | a8083063 | Iustin Pop | cfgw.SetDiskID(dev, node) |
1012 | a8083063 | Iustin Pop | |
1013 | a8083063 | Iustin Pop | retries = 0
|
1014 | a8083063 | Iustin Pop | while True: |
1015 | a8083063 | Iustin Pop | max_time = 0
|
1016 | a8083063 | Iustin Pop | done = True
|
1017 | a8083063 | Iustin Pop | cumul_degraded = False
|
1018 | a8083063 | Iustin Pop | rstats = rpc.call_blockdev_getmirrorstatus(node, instance.disks) |
1019 | a8083063 | Iustin Pop | if not rstats: |
1020 | 5bfac263 | Iustin Pop | proc.LogWarning("Can't get any data from node %s" % node)
|
1021 | a8083063 | Iustin Pop | retries += 1
|
1022 | a8083063 | Iustin Pop | if retries >= 10: |
1023 | 3ecf6786 | Iustin Pop | raise errors.RemoteError("Can't contact node %s for mirror data," |
1024 | 3ecf6786 | Iustin Pop | " aborting." % node)
|
1025 | a8083063 | Iustin Pop | time.sleep(6)
|
1026 | a8083063 | Iustin Pop | continue
|
1027 | a8083063 | Iustin Pop | retries = 0
|
1028 | a8083063 | Iustin Pop | for i in range(len(rstats)): |
1029 | a8083063 | Iustin Pop | mstat = rstats[i] |
1030 | a8083063 | Iustin Pop | if mstat is None: |
1031 | 5bfac263 | Iustin Pop | proc.LogWarning("Can't compute data for node %s/%s" %
|
1032 | a8083063 | Iustin Pop | (node, instance.disks[i].iv_name)) |
1033 | a8083063 | Iustin Pop | continue
|
1034 | 0834c866 | Iustin Pop | # we ignore the ldisk parameter
|
1035 | 0834c866 | Iustin Pop | perc_done, est_time, is_degraded, _ = mstat |
1036 | a8083063 | Iustin Pop | cumul_degraded = cumul_degraded or (is_degraded and perc_done is None) |
1037 | a8083063 | Iustin Pop | if perc_done is not None: |
1038 | a8083063 | Iustin Pop | done = False
|
1039 | a8083063 | Iustin Pop | if est_time is not None: |
1040 | a8083063 | Iustin Pop | rem_time = "%d estimated seconds remaining" % est_time
|
1041 | a8083063 | Iustin Pop | max_time = est_time |
1042 | a8083063 | Iustin Pop | else:
|
1043 | a8083063 | Iustin Pop | rem_time = "no time estimate"
|
1044 | 5bfac263 | Iustin Pop | proc.LogInfo("- device %s: %5.2f%% done, %s" %
|
1045 | 5bfac263 | Iustin Pop | (instance.disks[i].iv_name, perc_done, rem_time)) |
1046 | a8083063 | Iustin Pop | if done or oneshot: |
1047 | a8083063 | Iustin Pop | break
|
1048 | a8083063 | Iustin Pop | |
1049 | a8083063 | Iustin Pop | if unlock:
|
1050 | 685ee993 | Iustin Pop | #utils.Unlock('cmd')
|
1051 | 685ee993 | Iustin Pop | pass
|
1052 | a8083063 | Iustin Pop | try:
|
1053 | a8083063 | Iustin Pop | time.sleep(min(60, max_time)) |
1054 | a8083063 | Iustin Pop | finally:
|
1055 | a8083063 | Iustin Pop | if unlock:
|
1056 | 685ee993 | Iustin Pop | #utils.Lock('cmd')
|
1057 | 685ee993 | Iustin Pop | pass
|
1058 | a8083063 | Iustin Pop | |
1059 | a8083063 | Iustin Pop | if done:
|
1060 | 5bfac263 | Iustin Pop | proc.LogInfo("Instance %s's disks are in sync." % instance.name)
|
1061 | a8083063 | Iustin Pop | return not cumul_degraded |
1062 | a8083063 | Iustin Pop | |
1063 | a8083063 | Iustin Pop | |
1064 | 0834c866 | Iustin Pop | def _CheckDiskConsistency(cfgw, dev, node, on_primary, ldisk=False): |
1065 | a8083063 | Iustin Pop | """Check that mirrors are not degraded.
|
1066 | a8083063 | Iustin Pop |
|
1067 | 0834c866 | Iustin Pop | The ldisk parameter, if True, will change the test from the
|
1068 | 0834c866 | Iustin Pop | is_degraded attribute (which represents overall non-ok status for
|
1069 | 0834c866 | Iustin Pop | the device(s)) to the ldisk (representing the local storage status).
|
1070 | 0834c866 | Iustin Pop |
|
1071 | a8083063 | Iustin Pop | """
|
1072 | a8083063 | Iustin Pop | cfgw.SetDiskID(dev, node) |
1073 | 0834c866 | Iustin Pop | if ldisk:
|
1074 | 0834c866 | Iustin Pop | idx = 6
|
1075 | 0834c866 | Iustin Pop | else:
|
1076 | 0834c866 | Iustin Pop | idx = 5
|
1077 | a8083063 | Iustin Pop | |
1078 | a8083063 | Iustin Pop | result = True
|
1079 | a8083063 | Iustin Pop | if on_primary or dev.AssembleOnSecondary(): |
1080 | a8083063 | Iustin Pop | rstats = rpc.call_blockdev_find(node, dev) |
1081 | a8083063 | Iustin Pop | if not rstats: |
1082 | aa9d0c32 | Guido Trotter | logger.ToStderr("Node %s: Disk degraded, not found or node down" % node)
|
1083 | a8083063 | Iustin Pop | result = False
|
1084 | a8083063 | Iustin Pop | else:
|
1085 | 0834c866 | Iustin Pop | result = result and (not rstats[idx]) |
1086 | a8083063 | Iustin Pop | if dev.children:
|
1087 | a8083063 | Iustin Pop | for child in dev.children: |
1088 | a8083063 | Iustin Pop | result = result and _CheckDiskConsistency(cfgw, child, node, on_primary)
|
1089 | a8083063 | Iustin Pop | |
1090 | a8083063 | Iustin Pop | return result
|
1091 | a8083063 | Iustin Pop | |
1092 | a8083063 | Iustin Pop | |
1093 | a8083063 | Iustin Pop | class LUDiagnoseOS(NoHooksLU): |
1094 | a8083063 | Iustin Pop | """Logical unit for OS diagnose/query.
|
1095 | a8083063 | Iustin Pop |
|
1096 | a8083063 | Iustin Pop | """
|
1097 | 1f9430d6 | Iustin Pop | _OP_REQP = ["output_fields", "names"] |
1098 | a8083063 | Iustin Pop | |
1099 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1100 | a8083063 | Iustin Pop | """Check prerequisites.
|
1101 | a8083063 | Iustin Pop |
|
1102 | a8083063 | Iustin Pop | This always succeeds, since this is a pure query LU.
|
1103 | a8083063 | Iustin Pop |
|
1104 | a8083063 | Iustin Pop | """
|
1105 | 1f9430d6 | Iustin Pop | if self.op.names: |
1106 | 1f9430d6 | Iustin Pop | raise errors.OpPrereqError("Selective OS query not supported") |
1107 | 1f9430d6 | Iustin Pop | |
1108 | 1f9430d6 | Iustin Pop | self.dynamic_fields = frozenset(["name", "valid", "node_status"]) |
1109 | 1f9430d6 | Iustin Pop | _CheckOutputFields(static=[], |
1110 | 1f9430d6 | Iustin Pop | dynamic=self.dynamic_fields,
|
1111 | 1f9430d6 | Iustin Pop | selected=self.op.output_fields)
|
1112 | 1f9430d6 | Iustin Pop | |
1113 | 1f9430d6 | Iustin Pop | @staticmethod
|
1114 | 1f9430d6 | Iustin Pop | def _DiagnoseByOS(node_list, rlist): |
1115 | 1f9430d6 | Iustin Pop | """Remaps a per-node return list into an a per-os per-node dictionary
|
1116 | 1f9430d6 | Iustin Pop |
|
1117 | 1f9430d6 | Iustin Pop | Args:
|
1118 | 1f9430d6 | Iustin Pop | node_list: a list with the names of all nodes
|
1119 | 1f9430d6 | Iustin Pop | rlist: a map with node names as keys and OS objects as values
|
1120 | 1f9430d6 | Iustin Pop |
|
1121 | 1f9430d6 | Iustin Pop | Returns:
|
1122 | 1f9430d6 | Iustin Pop | map: a map with osnames as keys and as value another map, with
|
1123 | 1f9430d6 | Iustin Pop | nodes as
|
1124 | 1f9430d6 | Iustin Pop | keys and list of OS objects as values
|
1125 | 1f9430d6 | Iustin Pop | e.g. {"debian-etch": {"node1": [<object>,...],
|
1126 | 1f9430d6 | Iustin Pop | "node2": [<object>,]}
|
1127 | 1f9430d6 | Iustin Pop | }
|
1128 | 1f9430d6 | Iustin Pop |
|
1129 | 1f9430d6 | Iustin Pop | """
|
1130 | 1f9430d6 | Iustin Pop | all_os = {} |
1131 | 1f9430d6 | Iustin Pop | for node_name, nr in rlist.iteritems(): |
1132 | 1f9430d6 | Iustin Pop | if not nr: |
1133 | 1f9430d6 | Iustin Pop | continue
|
1134 | b4de68a9 | Iustin Pop | for os_obj in nr: |
1135 | b4de68a9 | Iustin Pop | if os_obj.name not in all_os: |
1136 | 1f9430d6 | Iustin Pop | # build a list of nodes for this os containing empty lists
|
1137 | 1f9430d6 | Iustin Pop | # for each node in node_list
|
1138 | b4de68a9 | Iustin Pop | all_os[os_obj.name] = {} |
1139 | 1f9430d6 | Iustin Pop | for nname in node_list: |
1140 | b4de68a9 | Iustin Pop | all_os[os_obj.name][nname] = [] |
1141 | b4de68a9 | Iustin Pop | all_os[os_obj.name][node_name].append(os_obj) |
1142 | 1f9430d6 | Iustin Pop | return all_os
|
1143 | a8083063 | Iustin Pop | |
1144 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1145 | a8083063 | Iustin Pop | """Compute the list of OSes.
|
1146 | a8083063 | Iustin Pop |
|
1147 | a8083063 | Iustin Pop | """
|
1148 | a8083063 | Iustin Pop | node_list = self.cfg.GetNodeList()
|
1149 | a8083063 | Iustin Pop | node_data = rpc.call_os_diagnose(node_list) |
1150 | a8083063 | Iustin Pop | if node_data == False: |
1151 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't gather the list of OSes") |
1152 | 1f9430d6 | Iustin Pop | pol = self._DiagnoseByOS(node_list, node_data)
|
1153 | 1f9430d6 | Iustin Pop | output = [] |
1154 | 1f9430d6 | Iustin Pop | for os_name, os_data in pol.iteritems(): |
1155 | 1f9430d6 | Iustin Pop | row = [] |
1156 | 1f9430d6 | Iustin Pop | for field in self.op.output_fields: |
1157 | 1f9430d6 | Iustin Pop | if field == "name": |
1158 | 1f9430d6 | Iustin Pop | val = os_name |
1159 | 1f9430d6 | Iustin Pop | elif field == "valid": |
1160 | 1f9430d6 | Iustin Pop | val = utils.all([osl and osl[0] for osl in os_data.values()]) |
1161 | 1f9430d6 | Iustin Pop | elif field == "node_status": |
1162 | 1f9430d6 | Iustin Pop | val = {} |
1163 | 1f9430d6 | Iustin Pop | for node_name, nos_list in os_data.iteritems(): |
1164 | 1f9430d6 | Iustin Pop | val[node_name] = [(v.status, v.path) for v in nos_list] |
1165 | 1f9430d6 | Iustin Pop | else:
|
1166 | 1f9430d6 | Iustin Pop | raise errors.ParameterError(field)
|
1167 | 1f9430d6 | Iustin Pop | row.append(val) |
1168 | 1f9430d6 | Iustin Pop | output.append(row) |
1169 | 1f9430d6 | Iustin Pop | |
1170 | 1f9430d6 | Iustin Pop | return output
|
1171 | a8083063 | Iustin Pop | |
1172 | a8083063 | Iustin Pop | |
1173 | a8083063 | Iustin Pop | class LURemoveNode(LogicalUnit): |
1174 | a8083063 | Iustin Pop | """Logical unit for removing a node.
|
1175 | a8083063 | Iustin Pop |
|
1176 | a8083063 | Iustin Pop | """
|
1177 | a8083063 | Iustin Pop | HPATH = "node-remove"
|
1178 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
1179 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
1180 | a8083063 | Iustin Pop | |
1181 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
1182 | a8083063 | Iustin Pop | """Build hooks env.
|
1183 | a8083063 | Iustin Pop |
|
1184 | a8083063 | Iustin Pop | This doesn't run on the target node in the pre phase as a failed
|
1185 | a8083063 | Iustin Pop | node would not allows itself to run.
|
1186 | a8083063 | Iustin Pop |
|
1187 | a8083063 | Iustin Pop | """
|
1188 | 396e1b78 | Michael Hanselmann | env = { |
1189 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
1190 | 396e1b78 | Michael Hanselmann | "NODE_NAME": self.op.node_name, |
1191 | 396e1b78 | Michael Hanselmann | } |
1192 | a8083063 | Iustin Pop | all_nodes = self.cfg.GetNodeList()
|
1193 | a8083063 | Iustin Pop | all_nodes.remove(self.op.node_name)
|
1194 | 396e1b78 | Michael Hanselmann | return env, all_nodes, all_nodes
|
1195 | a8083063 | Iustin Pop | |
1196 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1197 | a8083063 | Iustin Pop | """Check prerequisites.
|
1198 | a8083063 | Iustin Pop |
|
1199 | a8083063 | Iustin Pop | This checks:
|
1200 | a8083063 | Iustin Pop | - the node exists in the configuration
|
1201 | a8083063 | Iustin Pop | - it does not have primary or secondary instances
|
1202 | a8083063 | Iustin Pop | - it's not the master
|
1203 | a8083063 | Iustin Pop |
|
1204 | a8083063 | Iustin Pop | Any errors are signalled by raising errors.OpPrereqError.
|
1205 | a8083063 | Iustin Pop |
|
1206 | a8083063 | Iustin Pop | """
|
1207 | a8083063 | Iustin Pop | node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name)) |
1208 | a8083063 | Iustin Pop | if node is None: |
1209 | a02bc76e | Iustin Pop | raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name) |
1210 | a8083063 | Iustin Pop | |
1211 | a8083063 | Iustin Pop | instance_list = self.cfg.GetInstanceList()
|
1212 | a8083063 | Iustin Pop | |
1213 | 880478f8 | Iustin Pop | masternode = self.sstore.GetMasterNode()
|
1214 | a8083063 | Iustin Pop | if node.name == masternode:
|
1215 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node is the master node," |
1216 | 3ecf6786 | Iustin Pop | " you need to failover first.")
|
1217 | a8083063 | Iustin Pop | |
1218 | a8083063 | Iustin Pop | for instance_name in instance_list: |
1219 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(instance_name)
|
1220 | a8083063 | Iustin Pop | if node.name == instance.primary_node:
|
1221 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance %s still running on the node," |
1222 | 3ecf6786 | Iustin Pop | " please remove first." % instance_name)
|
1223 | a8083063 | Iustin Pop | if node.name in instance.secondary_nodes: |
1224 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance %s has node as a secondary," |
1225 | 3ecf6786 | Iustin Pop | " please remove first." % instance_name)
|
1226 | a8083063 | Iustin Pop | self.op.node_name = node.name
|
1227 | a8083063 | Iustin Pop | self.node = node
|
1228 | a8083063 | Iustin Pop | |
1229 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1230 | a8083063 | Iustin Pop | """Removes the node from the cluster.
|
1231 | a8083063 | Iustin Pop |
|
1232 | a8083063 | Iustin Pop | """
|
1233 | a8083063 | Iustin Pop | node = self.node
|
1234 | a8083063 | Iustin Pop | logger.Info("stopping the node daemon and removing configs from node %s" %
|
1235 | a8083063 | Iustin Pop | node.name) |
1236 | a8083063 | Iustin Pop | |
1237 | a8083063 | Iustin Pop | rpc.call_node_leave_cluster(node.name) |
1238 | a8083063 | Iustin Pop | |
1239 | c92b310a | Michael Hanselmann | self.ssh.Run(node.name, 'root', "%s stop" % constants.NODE_INITD_SCRIPT) |
1240 | a8083063 | Iustin Pop | |
1241 | a8083063 | Iustin Pop | logger.Info("Removing node %s from config" % node.name)
|
1242 | a8083063 | Iustin Pop | |
1243 | a8083063 | Iustin Pop | self.cfg.RemoveNode(node.name)
|
1244 | a8083063 | Iustin Pop | |
1245 | d9c02ca6 | Michael Hanselmann | utils.RemoveHostFromEtcHosts(node.name) |
1246 | c8a0948f | Michael Hanselmann | |
1247 | a8083063 | Iustin Pop | |
1248 | a8083063 | Iustin Pop | class LUQueryNodes(NoHooksLU): |
1249 | a8083063 | Iustin Pop | """Logical unit for querying nodes.
|
1250 | a8083063 | Iustin Pop |
|
1251 | a8083063 | Iustin Pop | """
|
1252 | 246e180a | Iustin Pop | _OP_REQP = ["output_fields", "names"] |
1253 | a8083063 | Iustin Pop | |
1254 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1255 | a8083063 | Iustin Pop | """Check prerequisites.
|
1256 | a8083063 | Iustin Pop |
|
1257 | a8083063 | Iustin Pop | This checks that the fields required are valid output fields.
|
1258 | a8083063 | Iustin Pop |
|
1259 | a8083063 | Iustin Pop | """
|
1260 | e8a4c138 | Iustin Pop | self.dynamic_fields = frozenset([ |
1261 | e8a4c138 | Iustin Pop | "dtotal", "dfree", |
1262 | e8a4c138 | Iustin Pop | "mtotal", "mnode", "mfree", |
1263 | e8a4c138 | Iustin Pop | "bootid",
|
1264 | e8a4c138 | Iustin Pop | "ctotal",
|
1265 | e8a4c138 | Iustin Pop | ]) |
1266 | a8083063 | Iustin Pop | |
1267 | ec223efb | Iustin Pop | _CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt", |
1268 | ec223efb | Iustin Pop | "pinst_list", "sinst_list", |
1269 | ec223efb | Iustin Pop | "pip", "sip"], |
1270 | dcb93971 | Michael Hanselmann | dynamic=self.dynamic_fields,
|
1271 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
1272 | a8083063 | Iustin Pop | |
1273 | 246e180a | Iustin Pop | self.wanted = _GetWantedNodes(self, self.op.names) |
1274 | a8083063 | Iustin Pop | |
1275 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1276 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
1277 | a8083063 | Iustin Pop |
|
1278 | a8083063 | Iustin Pop | """
|
1279 | 246e180a | Iustin Pop | nodenames = self.wanted
|
1280 | a8083063 | Iustin Pop | nodelist = [self.cfg.GetNodeInfo(name) for name in nodenames] |
1281 | a8083063 | Iustin Pop | |
1282 | a8083063 | Iustin Pop | # begin data gathering
|
1283 | a8083063 | Iustin Pop | |
1284 | a8083063 | Iustin Pop | if self.dynamic_fields.intersection(self.op.output_fields): |
1285 | a8083063 | Iustin Pop | live_data = {} |
1286 | a8083063 | Iustin Pop | node_data = rpc.call_node_info(nodenames, self.cfg.GetVGName())
|
1287 | a8083063 | Iustin Pop | for name in nodenames: |
1288 | a8083063 | Iustin Pop | nodeinfo = node_data.get(name, None)
|
1289 | a8083063 | Iustin Pop | if nodeinfo:
|
1290 | a8083063 | Iustin Pop | live_data[name] = { |
1291 | a8083063 | Iustin Pop | "mtotal": utils.TryConvert(int, nodeinfo['memory_total']), |
1292 | a8083063 | Iustin Pop | "mnode": utils.TryConvert(int, nodeinfo['memory_dom0']), |
1293 | a8083063 | Iustin Pop | "mfree": utils.TryConvert(int, nodeinfo['memory_free']), |
1294 | a8083063 | Iustin Pop | "dtotal": utils.TryConvert(int, nodeinfo['vg_size']), |
1295 | a8083063 | Iustin Pop | "dfree": utils.TryConvert(int, nodeinfo['vg_free']), |
1296 | e8a4c138 | Iustin Pop | "ctotal": utils.TryConvert(int, nodeinfo['cpu_total']), |
1297 | 3ef10550 | Michael Hanselmann | "bootid": nodeinfo['bootid'], |
1298 | a8083063 | Iustin Pop | } |
1299 | a8083063 | Iustin Pop | else:
|
1300 | a8083063 | Iustin Pop | live_data[name] = {} |
1301 | a8083063 | Iustin Pop | else:
|
1302 | a8083063 | Iustin Pop | live_data = dict.fromkeys(nodenames, {})
|
1303 | a8083063 | Iustin Pop | |
1304 | ec223efb | Iustin Pop | node_to_primary = dict([(name, set()) for name in nodenames]) |
1305 | ec223efb | Iustin Pop | node_to_secondary = dict([(name, set()) for name in nodenames]) |
1306 | a8083063 | Iustin Pop | |
1307 | ec223efb | Iustin Pop | inst_fields = frozenset(("pinst_cnt", "pinst_list", |
1308 | ec223efb | Iustin Pop | "sinst_cnt", "sinst_list")) |
1309 | ec223efb | Iustin Pop | if inst_fields & frozenset(self.op.output_fields): |
1310 | a8083063 | Iustin Pop | instancelist = self.cfg.GetInstanceList()
|
1311 | a8083063 | Iustin Pop | |
1312 | ec223efb | Iustin Pop | for instance_name in instancelist: |
1313 | ec223efb | Iustin Pop | inst = self.cfg.GetInstanceInfo(instance_name)
|
1314 | ec223efb | Iustin Pop | if inst.primary_node in node_to_primary: |
1315 | ec223efb | Iustin Pop | node_to_primary[inst.primary_node].add(inst.name) |
1316 | ec223efb | Iustin Pop | for secnode in inst.secondary_nodes: |
1317 | ec223efb | Iustin Pop | if secnode in node_to_secondary: |
1318 | ec223efb | Iustin Pop | node_to_secondary[secnode].add(inst.name) |
1319 | a8083063 | Iustin Pop | |
1320 | a8083063 | Iustin Pop | # end data gathering
|
1321 | a8083063 | Iustin Pop | |
1322 | a8083063 | Iustin Pop | output = [] |
1323 | a8083063 | Iustin Pop | for node in nodelist: |
1324 | a8083063 | Iustin Pop | node_output = [] |
1325 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
1326 | a8083063 | Iustin Pop | if field == "name": |
1327 | a8083063 | Iustin Pop | val = node.name |
1328 | ec223efb | Iustin Pop | elif field == "pinst_list": |
1329 | ec223efb | Iustin Pop | val = list(node_to_primary[node.name])
|
1330 | ec223efb | Iustin Pop | elif field == "sinst_list": |
1331 | ec223efb | Iustin Pop | val = list(node_to_secondary[node.name])
|
1332 | ec223efb | Iustin Pop | elif field == "pinst_cnt": |
1333 | ec223efb | Iustin Pop | val = len(node_to_primary[node.name])
|
1334 | ec223efb | Iustin Pop | elif field == "sinst_cnt": |
1335 | ec223efb | Iustin Pop | val = len(node_to_secondary[node.name])
|
1336 | a8083063 | Iustin Pop | elif field == "pip": |
1337 | a8083063 | Iustin Pop | val = node.primary_ip |
1338 | a8083063 | Iustin Pop | elif field == "sip": |
1339 | a8083063 | Iustin Pop | val = node.secondary_ip |
1340 | a8083063 | Iustin Pop | elif field in self.dynamic_fields: |
1341 | ec223efb | Iustin Pop | val = live_data[node.name].get(field, None)
|
1342 | a8083063 | Iustin Pop | else:
|
1343 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
1344 | a8083063 | Iustin Pop | node_output.append(val) |
1345 | a8083063 | Iustin Pop | output.append(node_output) |
1346 | a8083063 | Iustin Pop | |
1347 | a8083063 | Iustin Pop | return output
|
1348 | a8083063 | Iustin Pop | |
1349 | a8083063 | Iustin Pop | |
1350 | dcb93971 | Michael Hanselmann | class LUQueryNodeVolumes(NoHooksLU): |
1351 | dcb93971 | Michael Hanselmann | """Logical unit for getting volumes on node(s).
|
1352 | dcb93971 | Michael Hanselmann |
|
1353 | dcb93971 | Michael Hanselmann | """
|
1354 | dcb93971 | Michael Hanselmann | _OP_REQP = ["nodes", "output_fields"] |
1355 | dcb93971 | Michael Hanselmann | |
1356 | dcb93971 | Michael Hanselmann | def CheckPrereq(self): |
1357 | dcb93971 | Michael Hanselmann | """Check prerequisites.
|
1358 | dcb93971 | Michael Hanselmann |
|
1359 | dcb93971 | Michael Hanselmann | This checks that the fields required are valid output fields.
|
1360 | dcb93971 | Michael Hanselmann |
|
1361 | dcb93971 | Michael Hanselmann | """
|
1362 | dcb93971 | Michael Hanselmann | self.nodes = _GetWantedNodes(self, self.op.nodes) |
1363 | dcb93971 | Michael Hanselmann | |
1364 | dcb93971 | Michael Hanselmann | _CheckOutputFields(static=["node"],
|
1365 | dcb93971 | Michael Hanselmann | dynamic=["phys", "vg", "name", "size", "instance"], |
1366 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
1367 | dcb93971 | Michael Hanselmann | |
1368 | dcb93971 | Michael Hanselmann | |
1369 | dcb93971 | Michael Hanselmann | def Exec(self, feedback_fn): |
1370 | dcb93971 | Michael Hanselmann | """Computes the list of nodes and their attributes.
|
1371 | dcb93971 | Michael Hanselmann |
|
1372 | dcb93971 | Michael Hanselmann | """
|
1373 | a7ba5e53 | Iustin Pop | nodenames = self.nodes
|
1374 | dcb93971 | Michael Hanselmann | volumes = rpc.call_node_volumes(nodenames) |
1375 | dcb93971 | Michael Hanselmann | |
1376 | dcb93971 | Michael Hanselmann | ilist = [self.cfg.GetInstanceInfo(iname) for iname |
1377 | dcb93971 | Michael Hanselmann | in self.cfg.GetInstanceList()] |
1378 | dcb93971 | Michael Hanselmann | |
1379 | dcb93971 | Michael Hanselmann | lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist]) |
1380 | dcb93971 | Michael Hanselmann | |
1381 | dcb93971 | Michael Hanselmann | output = [] |
1382 | dcb93971 | Michael Hanselmann | for node in nodenames: |
1383 | 37d19eb2 | Michael Hanselmann | if node not in volumes or not volumes[node]: |
1384 | 37d19eb2 | Michael Hanselmann | continue
|
1385 | 37d19eb2 | Michael Hanselmann | |
1386 | dcb93971 | Michael Hanselmann | node_vols = volumes[node][:] |
1387 | dcb93971 | Michael Hanselmann | node_vols.sort(key=lambda vol: vol['dev']) |
1388 | dcb93971 | Michael Hanselmann | |
1389 | dcb93971 | Michael Hanselmann | for vol in node_vols: |
1390 | dcb93971 | Michael Hanselmann | node_output = [] |
1391 | dcb93971 | Michael Hanselmann | for field in self.op.output_fields: |
1392 | dcb93971 | Michael Hanselmann | if field == "node": |
1393 | dcb93971 | Michael Hanselmann | val = node |
1394 | dcb93971 | Michael Hanselmann | elif field == "phys": |
1395 | dcb93971 | Michael Hanselmann | val = vol['dev']
|
1396 | dcb93971 | Michael Hanselmann | elif field == "vg": |
1397 | dcb93971 | Michael Hanselmann | val = vol['vg']
|
1398 | dcb93971 | Michael Hanselmann | elif field == "name": |
1399 | dcb93971 | Michael Hanselmann | val = vol['name']
|
1400 | dcb93971 | Michael Hanselmann | elif field == "size": |
1401 | dcb93971 | Michael Hanselmann | val = int(float(vol['size'])) |
1402 | dcb93971 | Michael Hanselmann | elif field == "instance": |
1403 | dcb93971 | Michael Hanselmann | for inst in ilist: |
1404 | dcb93971 | Michael Hanselmann | if node not in lv_by_node[inst]: |
1405 | dcb93971 | Michael Hanselmann | continue
|
1406 | dcb93971 | Michael Hanselmann | if vol['name'] in lv_by_node[inst][node]: |
1407 | dcb93971 | Michael Hanselmann | val = inst.name |
1408 | dcb93971 | Michael Hanselmann | break
|
1409 | dcb93971 | Michael Hanselmann | else:
|
1410 | dcb93971 | Michael Hanselmann | val = '-'
|
1411 | dcb93971 | Michael Hanselmann | else:
|
1412 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
1413 | dcb93971 | Michael Hanselmann | node_output.append(str(val))
|
1414 | dcb93971 | Michael Hanselmann | |
1415 | dcb93971 | Michael Hanselmann | output.append(node_output) |
1416 | dcb93971 | Michael Hanselmann | |
1417 | dcb93971 | Michael Hanselmann | return output
|
1418 | dcb93971 | Michael Hanselmann | |
1419 | dcb93971 | Michael Hanselmann | |
1420 | a8083063 | Iustin Pop | class LUAddNode(LogicalUnit): |
1421 | a8083063 | Iustin Pop | """Logical unit for adding node to the cluster.
|
1422 | a8083063 | Iustin Pop |
|
1423 | a8083063 | Iustin Pop | """
|
1424 | a8083063 | Iustin Pop | HPATH = "node-add"
|
1425 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_NODE |
1426 | a8083063 | Iustin Pop | _OP_REQP = ["node_name"]
|
1427 | a8083063 | Iustin Pop | |
1428 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
1429 | a8083063 | Iustin Pop | """Build hooks env.
|
1430 | a8083063 | Iustin Pop |
|
1431 | a8083063 | Iustin Pop | This will run on all nodes before, and on all nodes + the new node after.
|
1432 | a8083063 | Iustin Pop |
|
1433 | a8083063 | Iustin Pop | """
|
1434 | a8083063 | Iustin Pop | env = { |
1435 | 0e137c28 | Iustin Pop | "OP_TARGET": self.op.node_name, |
1436 | a8083063 | Iustin Pop | "NODE_NAME": self.op.node_name, |
1437 | a8083063 | Iustin Pop | "NODE_PIP": self.op.primary_ip, |
1438 | a8083063 | Iustin Pop | "NODE_SIP": self.op.secondary_ip, |
1439 | a8083063 | Iustin Pop | } |
1440 | a8083063 | Iustin Pop | nodes_0 = self.cfg.GetNodeList()
|
1441 | a8083063 | Iustin Pop | nodes_1 = nodes_0 + [self.op.node_name, ]
|
1442 | a8083063 | Iustin Pop | return env, nodes_0, nodes_1
|
1443 | a8083063 | Iustin Pop | |
1444 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1445 | a8083063 | Iustin Pop | """Check prerequisites.
|
1446 | a8083063 | Iustin Pop |
|
1447 | a8083063 | Iustin Pop | This checks:
|
1448 | a8083063 | Iustin Pop | - the new node is not already in the config
|
1449 | a8083063 | Iustin Pop | - it is resolvable
|
1450 | a8083063 | Iustin Pop | - its parameters (single/dual homed) matches the cluster
|
1451 | a8083063 | Iustin Pop |
|
1452 | a8083063 | Iustin Pop | Any errors are signalled by raising errors.OpPrereqError.
|
1453 | a8083063 | Iustin Pop |
|
1454 | a8083063 | Iustin Pop | """
|
1455 | a8083063 | Iustin Pop | node_name = self.op.node_name
|
1456 | a8083063 | Iustin Pop | cfg = self.cfg
|
1457 | a8083063 | Iustin Pop | |
1458 | 89e1fc26 | Iustin Pop | dns_data = utils.HostInfo(node_name) |
1459 | a8083063 | Iustin Pop | |
1460 | bcf043c9 | Iustin Pop | node = dns_data.name |
1461 | bcf043c9 | Iustin Pop | primary_ip = self.op.primary_ip = dns_data.ip
|
1462 | a8083063 | Iustin Pop | secondary_ip = getattr(self.op, "secondary_ip", None) |
1463 | a8083063 | Iustin Pop | if secondary_ip is None: |
1464 | a8083063 | Iustin Pop | secondary_ip = primary_ip |
1465 | a8083063 | Iustin Pop | if not utils.IsValidIP(secondary_ip): |
1466 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid secondary IP given") |
1467 | a8083063 | Iustin Pop | self.op.secondary_ip = secondary_ip
|
1468 | e7c6e02b | Michael Hanselmann | |
1469 | a8083063 | Iustin Pop | node_list = cfg.GetNodeList() |
1470 | e7c6e02b | Michael Hanselmann | if not self.op.readd and node in node_list: |
1471 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Node %s is already in the configuration" % |
1472 | e7c6e02b | Michael Hanselmann | node) |
1473 | e7c6e02b | Michael Hanselmann | elif self.op.readd and node not in node_list: |
1474 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Node %s is not in the configuration" % node) |
1475 | a8083063 | Iustin Pop | |
1476 | a8083063 | Iustin Pop | for existing_node_name in node_list: |
1477 | a8083063 | Iustin Pop | existing_node = cfg.GetNodeInfo(existing_node_name) |
1478 | e7c6e02b | Michael Hanselmann | |
1479 | e7c6e02b | Michael Hanselmann | if self.op.readd and node == existing_node_name: |
1480 | e7c6e02b | Michael Hanselmann | if (existing_node.primary_ip != primary_ip or |
1481 | e7c6e02b | Michael Hanselmann | existing_node.secondary_ip != secondary_ip): |
1482 | e7c6e02b | Michael Hanselmann | raise errors.OpPrereqError("Readded node doesn't have the same IP" |
1483 | e7c6e02b | Michael Hanselmann | " address configuration as before")
|
1484 | e7c6e02b | Michael Hanselmann | continue
|
1485 | e7c6e02b | Michael Hanselmann | |
1486 | a8083063 | Iustin Pop | if (existing_node.primary_ip == primary_ip or |
1487 | a8083063 | Iustin Pop | existing_node.secondary_ip == primary_ip or
|
1488 | a8083063 | Iustin Pop | existing_node.primary_ip == secondary_ip or
|
1489 | a8083063 | Iustin Pop | existing_node.secondary_ip == secondary_ip): |
1490 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("New node ip address(es) conflict with" |
1491 | 3ecf6786 | Iustin Pop | " existing node %s" % existing_node.name)
|
1492 | a8083063 | Iustin Pop | |
1493 | a8083063 | Iustin Pop | # check that the type of the node (single versus dual homed) is the
|
1494 | a8083063 | Iustin Pop | # same as for the master
|
1495 | 880478f8 | Iustin Pop | myself = cfg.GetNodeInfo(self.sstore.GetMasterNode())
|
1496 | a8083063 | Iustin Pop | master_singlehomed = myself.secondary_ip == myself.primary_ip |
1497 | a8083063 | Iustin Pop | newbie_singlehomed = secondary_ip == primary_ip |
1498 | a8083063 | Iustin Pop | if master_singlehomed != newbie_singlehomed:
|
1499 | a8083063 | Iustin Pop | if master_singlehomed:
|
1500 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has no private ip but the" |
1501 | 3ecf6786 | Iustin Pop | " new node has one")
|
1502 | a8083063 | Iustin Pop | else:
|
1503 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The master has a private ip but the" |
1504 | 3ecf6786 | Iustin Pop | " new node doesn't have one")
|
1505 | a8083063 | Iustin Pop | |
1506 | a8083063 | Iustin Pop | # checks reachablity
|
1507 | b15d625f | Iustin Pop | if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT): |
1508 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node not reachable by ping") |
1509 | a8083063 | Iustin Pop | |
1510 | a8083063 | Iustin Pop | if not newbie_singlehomed: |
1511 | a8083063 | Iustin Pop | # check reachability from my secondary ip to newbie's secondary ip
|
1512 | b15d625f | Iustin Pop | if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT, |
1513 | b15d625f | Iustin Pop | source=myself.secondary_ip): |
1514 | f4bc1f2c | Michael Hanselmann | raise errors.OpPrereqError("Node secondary ip not reachable by TCP" |
1515 | f4bc1f2c | Michael Hanselmann | " based ping to noded port")
|
1516 | a8083063 | Iustin Pop | |
1517 | a8083063 | Iustin Pop | self.new_node = objects.Node(name=node,
|
1518 | a8083063 | Iustin Pop | primary_ip=primary_ip, |
1519 | a8083063 | Iustin Pop | secondary_ip=secondary_ip) |
1520 | a8083063 | Iustin Pop | |
1521 | 2a6469d5 | Alexander Schreiber | if self.sstore.GetHypervisorType() == constants.HT_XEN_HVM31: |
1522 | 2a6469d5 | Alexander Schreiber | if not os.path.exists(constants.VNC_PASSWORD_FILE): |
1523 | 2a6469d5 | Alexander Schreiber | raise errors.OpPrereqError("Cluster VNC password file %s missing" % |
1524 | 2a6469d5 | Alexander Schreiber | constants.VNC_PASSWORD_FILE) |
1525 | 2a6469d5 | Alexander Schreiber | |
1526 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1527 | a8083063 | Iustin Pop | """Adds the new node to the cluster.
|
1528 | a8083063 | Iustin Pop |
|
1529 | a8083063 | Iustin Pop | """
|
1530 | a8083063 | Iustin Pop | new_node = self.new_node
|
1531 | a8083063 | Iustin Pop | node = new_node.name |
1532 | a8083063 | Iustin Pop | |
1533 | a8083063 | Iustin Pop | # set up inter-node password and certificate and restarts the node daemon
|
1534 | a8083063 | Iustin Pop | gntpass = self.sstore.GetNodeDaemonPassword()
|
1535 | a8083063 | Iustin Pop | if not re.match('^[a-zA-Z0-9.]{1,64}$', gntpass): |
1536 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("ganeti password corruption detected") |
1537 | a8083063 | Iustin Pop | f = open(constants.SSL_CERT_FILE)
|
1538 | a8083063 | Iustin Pop | try:
|
1539 | a8083063 | Iustin Pop | gntpem = f.read(8192)
|
1540 | a8083063 | Iustin Pop | finally:
|
1541 | a8083063 | Iustin Pop | f.close() |
1542 | a8083063 | Iustin Pop | # in the base64 pem encoding, neither '!' nor '.' are valid chars,
|
1543 | a8083063 | Iustin Pop | # so we use this to detect an invalid certificate; as long as the
|
1544 | a8083063 | Iustin Pop | # cert doesn't contain this, the here-document will be correctly
|
1545 | a8083063 | Iustin Pop | # parsed by the shell sequence below
|
1546 | a8083063 | Iustin Pop | if re.search('^!EOF\.', gntpem, re.MULTILINE): |
1547 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("invalid PEM encoding in the SSL certificate") |
1548 | a8083063 | Iustin Pop | if not gntpem.endswith("\n"): |
1549 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("PEM must end with newline") |
1550 | a8083063 | Iustin Pop | logger.Info("copy cluster pass to %s and starting the node daemon" % node)
|
1551 | a8083063 | Iustin Pop | |
1552 | a8083063 | Iustin Pop | # and then connect with ssh to set password and start ganeti-noded
|
1553 | a8083063 | Iustin Pop | # note that all the below variables are sanitized at this point,
|
1554 | a8083063 | Iustin Pop | # either by being constants or by the checks above
|
1555 | a8083063 | Iustin Pop | ss = self.sstore
|
1556 | a8083063 | Iustin Pop | mycommand = ("umask 077 && "
|
1557 | a8083063 | Iustin Pop | "echo '%s' > '%s' && "
|
1558 | a8083063 | Iustin Pop | "cat > '%s' << '!EOF.' && \n"
|
1559 | a8083063 | Iustin Pop | "%s!EOF.\n%s restart" %
|
1560 | a8083063 | Iustin Pop | (gntpass, ss.KeyToFilename(ss.SS_NODED_PASS), |
1561 | a8083063 | Iustin Pop | constants.SSL_CERT_FILE, gntpem, |
1562 | a8083063 | Iustin Pop | constants.NODE_INITD_SCRIPT)) |
1563 | a8083063 | Iustin Pop | |
1564 | c92b310a | Michael Hanselmann | result = self.ssh.Run(node, 'root', mycommand, batch=False, ask_key=True) |
1565 | a8083063 | Iustin Pop | if result.failed:
|
1566 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Remote command on node %s, error: %s," |
1567 | 3ecf6786 | Iustin Pop | " output: %s" %
|
1568 | 3ecf6786 | Iustin Pop | (node, result.fail_reason, result.output)) |
1569 | a8083063 | Iustin Pop | |
1570 | a8083063 | Iustin Pop | # check connectivity
|
1571 | a8083063 | Iustin Pop | time.sleep(4)
|
1572 | a8083063 | Iustin Pop | |
1573 | a8083063 | Iustin Pop | result = rpc.call_version([node])[node] |
1574 | a8083063 | Iustin Pop | if result:
|
1575 | a8083063 | Iustin Pop | if constants.PROTOCOL_VERSION == result:
|
1576 | a8083063 | Iustin Pop | logger.Info("communication to node %s fine, sw version %s match" %
|
1577 | a8083063 | Iustin Pop | (node, result)) |
1578 | a8083063 | Iustin Pop | else:
|
1579 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Version mismatch master version %s," |
1580 | 3ecf6786 | Iustin Pop | " node version %s" %
|
1581 | 3ecf6786 | Iustin Pop | (constants.PROTOCOL_VERSION, result)) |
1582 | a8083063 | Iustin Pop | else:
|
1583 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Cannot get version from the new node") |
1584 | a8083063 | Iustin Pop | |
1585 | a8083063 | Iustin Pop | # setup ssh on node
|
1586 | a8083063 | Iustin Pop | logger.Info("copy ssh key to node %s" % node)
|
1587 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
1588 | a8083063 | Iustin Pop | keyarray = [] |
1589 | 70d9e3d8 | Iustin Pop | keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB, |
1590 | 70d9e3d8 | Iustin Pop | constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB, |
1591 | 70d9e3d8 | Iustin Pop | priv_key, pub_key] |
1592 | a8083063 | Iustin Pop | |
1593 | a8083063 | Iustin Pop | for i in keyfiles: |
1594 | a8083063 | Iustin Pop | f = open(i, 'r') |
1595 | a8083063 | Iustin Pop | try:
|
1596 | a8083063 | Iustin Pop | keyarray.append(f.read()) |
1597 | a8083063 | Iustin Pop | finally:
|
1598 | a8083063 | Iustin Pop | f.close() |
1599 | a8083063 | Iustin Pop | |
1600 | a8083063 | Iustin Pop | result = rpc.call_node_add(node, keyarray[0], keyarray[1], keyarray[2], |
1601 | a8083063 | Iustin Pop | keyarray[3], keyarray[4], keyarray[5]) |
1602 | a8083063 | Iustin Pop | |
1603 | a8083063 | Iustin Pop | if not result: |
1604 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Cannot transfer ssh keys to the new node") |
1605 | a8083063 | Iustin Pop | |
1606 | a8083063 | Iustin Pop | # Add node to our /etc/hosts, and add key to known_hosts
|
1607 | d9c02ca6 | Michael Hanselmann | utils.AddHostToEtcHosts(new_node.name) |
1608 | c8a0948f | Michael Hanselmann | |
1609 | a8083063 | Iustin Pop | if new_node.secondary_ip != new_node.primary_ip:
|
1610 | 16abfbc2 | Alexander Schreiber | if not rpc.call_node_tcp_ping(new_node.name, |
1611 | 16abfbc2 | Alexander Schreiber | constants.LOCALHOST_IP_ADDRESS, |
1612 | 16abfbc2 | Alexander Schreiber | new_node.secondary_ip, |
1613 | 16abfbc2 | Alexander Schreiber | constants.DEFAULT_NODED_PORT, |
1614 | 16abfbc2 | Alexander Schreiber | 10, False): |
1615 | f4bc1f2c | Michael Hanselmann | raise errors.OpExecError("Node claims it doesn't have the secondary ip" |
1616 | f4bc1f2c | Michael Hanselmann | " you gave (%s). Please fix and re-run this"
|
1617 | f4bc1f2c | Michael Hanselmann | " command." % new_node.secondary_ip)
|
1618 | a8083063 | Iustin Pop | |
1619 | c92b310a | Michael Hanselmann | success, msg = self.ssh.VerifyNodeHostname(node)
|
1620 | ff98055b | Iustin Pop | if not success: |
1621 | ff98055b | Iustin Pop | raise errors.OpExecError("Node '%s' claims it has a different hostname" |
1622 | f4bc1f2c | Michael Hanselmann | " than the one the resolver gives: %s."
|
1623 | f4bc1f2c | Michael Hanselmann | " Please fix and re-run this command." %
|
1624 | ff98055b | Iustin Pop | (node, msg)) |
1625 | ff98055b | Iustin Pop | |
1626 | a8083063 | Iustin Pop | # Distribute updated /etc/hosts and known_hosts to all nodes,
|
1627 | a8083063 | Iustin Pop | # including the node just added
|
1628 | 880478f8 | Iustin Pop | myself = self.cfg.GetNodeInfo(self.sstore.GetMasterNode()) |
1629 | 102b115b | Michael Hanselmann | dist_nodes = self.cfg.GetNodeList()
|
1630 | 102b115b | Michael Hanselmann | if not self.op.readd: |
1631 | 102b115b | Michael Hanselmann | dist_nodes.append(node) |
1632 | a8083063 | Iustin Pop | if myself.name in dist_nodes: |
1633 | a8083063 | Iustin Pop | dist_nodes.remove(myself.name) |
1634 | a8083063 | Iustin Pop | |
1635 | a8083063 | Iustin Pop | logger.Debug("Copying hosts and known_hosts to all nodes")
|
1636 | 107711b0 | Michael Hanselmann | for fname in (constants.ETC_HOSTS, constants.SSH_KNOWN_HOSTS_FILE): |
1637 | a8083063 | Iustin Pop | result = rpc.call_upload_file(dist_nodes, fname) |
1638 | a8083063 | Iustin Pop | for to_node in dist_nodes: |
1639 | a8083063 | Iustin Pop | if not result[to_node]: |
1640 | a8083063 | Iustin Pop | logger.Error("copy of file %s to node %s failed" %
|
1641 | a8083063 | Iustin Pop | (fname, to_node)) |
1642 | a8083063 | Iustin Pop | |
1643 | cb91d46e | Iustin Pop | to_copy = ss.GetFileList() |
1644 | 2a6469d5 | Alexander Schreiber | if self.sstore.GetHypervisorType() == constants.HT_XEN_HVM31: |
1645 | 2a6469d5 | Alexander Schreiber | to_copy.append(constants.VNC_PASSWORD_FILE) |
1646 | a8083063 | Iustin Pop | for fname in to_copy: |
1647 | c92b310a | Michael Hanselmann | if not self.ssh.CopyFileToNode(node, fname): |
1648 | a8083063 | Iustin Pop | logger.Error("could not copy file %s to node %s" % (fname, node))
|
1649 | a8083063 | Iustin Pop | |
1650 | e7c6e02b | Michael Hanselmann | if not self.op.readd: |
1651 | e7c6e02b | Michael Hanselmann | logger.Info("adding node %s to cluster.conf" % node)
|
1652 | e7c6e02b | Michael Hanselmann | self.cfg.AddNode(new_node)
|
1653 | a8083063 | Iustin Pop | |
1654 | a8083063 | Iustin Pop | |
1655 | a8083063 | Iustin Pop | class LUMasterFailover(LogicalUnit): |
1656 | a8083063 | Iustin Pop | """Failover the master node to the current node.
|
1657 | a8083063 | Iustin Pop |
|
1658 | a8083063 | Iustin Pop | This is a special LU in that it must run on a non-master node.
|
1659 | a8083063 | Iustin Pop |
|
1660 | a8083063 | Iustin Pop | """
|
1661 | a8083063 | Iustin Pop | HPATH = "master-failover"
|
1662 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_CLUSTER |
1663 | a8083063 | Iustin Pop | REQ_MASTER = False
|
1664 | a8083063 | Iustin Pop | _OP_REQP = [] |
1665 | a8083063 | Iustin Pop | |
1666 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
1667 | a8083063 | Iustin Pop | """Build hooks env.
|
1668 | a8083063 | Iustin Pop |
|
1669 | a8083063 | Iustin Pop | This will run on the new master only in the pre phase, and on all
|
1670 | a8083063 | Iustin Pop | the nodes in the post phase.
|
1671 | a8083063 | Iustin Pop |
|
1672 | a8083063 | Iustin Pop | """
|
1673 | a8083063 | Iustin Pop | env = { |
1674 | 0e137c28 | Iustin Pop | "OP_TARGET": self.new_master, |
1675 | a8083063 | Iustin Pop | "NEW_MASTER": self.new_master, |
1676 | a8083063 | Iustin Pop | "OLD_MASTER": self.old_master, |
1677 | a8083063 | Iustin Pop | } |
1678 | a8083063 | Iustin Pop | return env, [self.new_master], self.cfg.GetNodeList() |
1679 | a8083063 | Iustin Pop | |
1680 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1681 | a8083063 | Iustin Pop | """Check prerequisites.
|
1682 | a8083063 | Iustin Pop |
|
1683 | a8083063 | Iustin Pop | This checks that we are not already the master.
|
1684 | a8083063 | Iustin Pop |
|
1685 | a8083063 | Iustin Pop | """
|
1686 | 89e1fc26 | Iustin Pop | self.new_master = utils.HostInfo().name
|
1687 | 880478f8 | Iustin Pop | self.old_master = self.sstore.GetMasterNode() |
1688 | a8083063 | Iustin Pop | |
1689 | a8083063 | Iustin Pop | if self.old_master == self.new_master: |
1690 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("This commands must be run on the node" |
1691 | f4bc1f2c | Michael Hanselmann | " where you want the new master to be."
|
1692 | f4bc1f2c | Michael Hanselmann | " %s is already the master" %
|
1693 | 3ecf6786 | Iustin Pop | self.old_master)
|
1694 | a8083063 | Iustin Pop | |
1695 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1696 | a8083063 | Iustin Pop | """Failover the master node.
|
1697 | a8083063 | Iustin Pop |
|
1698 | a8083063 | Iustin Pop | This command, when run on a non-master node, will cause the current
|
1699 | a8083063 | Iustin Pop | master to cease being master, and the non-master to become new
|
1700 | a8083063 | Iustin Pop | master.
|
1701 | a8083063 | Iustin Pop |
|
1702 | a8083063 | Iustin Pop | """
|
1703 | a8083063 | Iustin Pop | #TODO: do not rely on gethostname returning the FQDN
|
1704 | a8083063 | Iustin Pop | logger.Info("setting master to %s, old master: %s" %
|
1705 | a8083063 | Iustin Pop | (self.new_master, self.old_master)) |
1706 | a8083063 | Iustin Pop | |
1707 | a8083063 | Iustin Pop | if not rpc.call_node_stop_master(self.old_master): |
1708 | a8083063 | Iustin Pop | logger.Error("could disable the master role on the old master"
|
1709 | a8083063 | Iustin Pop | " %s, please disable manually" % self.old_master) |
1710 | a8083063 | Iustin Pop | |
1711 | 880478f8 | Iustin Pop | ss = self.sstore
|
1712 | 880478f8 | Iustin Pop | ss.SetKey(ss.SS_MASTER_NODE, self.new_master)
|
1713 | 880478f8 | Iustin Pop | if not rpc.call_upload_file(self.cfg.GetNodeList(), |
1714 | 880478f8 | Iustin Pop | ss.KeyToFilename(ss.SS_MASTER_NODE)): |
1715 | 880478f8 | Iustin Pop | logger.Error("could not distribute the new simple store master file"
|
1716 | 880478f8 | Iustin Pop | " to the other nodes, please check.")
|
1717 | 880478f8 | Iustin Pop | |
1718 | a8083063 | Iustin Pop | if not rpc.call_node_start_master(self.new_master): |
1719 | a8083063 | Iustin Pop | logger.Error("could not start the master role on the new master"
|
1720 | a8083063 | Iustin Pop | " %s, please check" % self.new_master) |
1721 | f4bc1f2c | Michael Hanselmann | feedback_fn("Error in activating the master IP on the new master,"
|
1722 | f4bc1f2c | Michael Hanselmann | " please fix manually.")
|
1723 | a8083063 | Iustin Pop | |
1724 | a8083063 | Iustin Pop | |
1725 | a8083063 | Iustin Pop | |
1726 | a8083063 | Iustin Pop | class LUQueryClusterInfo(NoHooksLU): |
1727 | a8083063 | Iustin Pop | """Query cluster configuration.
|
1728 | a8083063 | Iustin Pop |
|
1729 | a8083063 | Iustin Pop | """
|
1730 | a8083063 | Iustin Pop | _OP_REQP = [] |
1731 | 59322403 | Iustin Pop | REQ_MASTER = False
|
1732 | a8083063 | Iustin Pop | |
1733 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1734 | a8083063 | Iustin Pop | """No prerequsites needed for this LU.
|
1735 | a8083063 | Iustin Pop |
|
1736 | a8083063 | Iustin Pop | """
|
1737 | a8083063 | Iustin Pop | pass
|
1738 | a8083063 | Iustin Pop | |
1739 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1740 | a8083063 | Iustin Pop | """Return cluster config.
|
1741 | a8083063 | Iustin Pop |
|
1742 | a8083063 | Iustin Pop | """
|
1743 | a8083063 | Iustin Pop | result = { |
1744 | 5fcdc80d | Iustin Pop | "name": self.sstore.GetClusterName(), |
1745 | a8083063 | Iustin Pop | "software_version": constants.RELEASE_VERSION,
|
1746 | a8083063 | Iustin Pop | "protocol_version": constants.PROTOCOL_VERSION,
|
1747 | a8083063 | Iustin Pop | "config_version": constants.CONFIG_VERSION,
|
1748 | a8083063 | Iustin Pop | "os_api_version": constants.OS_API_VERSION,
|
1749 | a8083063 | Iustin Pop | "export_version": constants.EXPORT_VERSION,
|
1750 | 880478f8 | Iustin Pop | "master": self.sstore.GetMasterNode(), |
1751 | a8083063 | Iustin Pop | "architecture": (platform.architecture()[0], platform.machine()), |
1752 | 8a12ce45 | Iustin Pop | "hypervisor_type": self.sstore.GetHypervisorType(), |
1753 | a8083063 | Iustin Pop | } |
1754 | a8083063 | Iustin Pop | |
1755 | a8083063 | Iustin Pop | return result
|
1756 | a8083063 | Iustin Pop | |
1757 | a8083063 | Iustin Pop | |
1758 | a8083063 | Iustin Pop | class LUClusterCopyFile(NoHooksLU): |
1759 | a8083063 | Iustin Pop | """Copy file to cluster.
|
1760 | a8083063 | Iustin Pop |
|
1761 | a8083063 | Iustin Pop | """
|
1762 | a8083063 | Iustin Pop | _OP_REQP = ["nodes", "filename"] |
1763 | a8083063 | Iustin Pop | |
1764 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1765 | a8083063 | Iustin Pop | """Check prerequisites.
|
1766 | a8083063 | Iustin Pop |
|
1767 | a8083063 | Iustin Pop | It should check that the named file exists and that the given list
|
1768 | a8083063 | Iustin Pop | of nodes is valid.
|
1769 | a8083063 | Iustin Pop |
|
1770 | a8083063 | Iustin Pop | """
|
1771 | a8083063 | Iustin Pop | if not os.path.exists(self.op.filename): |
1772 | a8083063 | Iustin Pop | raise errors.OpPrereqError("No such filename '%s'" % self.op.filename) |
1773 | dcb93971 | Michael Hanselmann | |
1774 | dcb93971 | Michael Hanselmann | self.nodes = _GetWantedNodes(self, self.op.nodes) |
1775 | a8083063 | Iustin Pop | |
1776 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1777 | a8083063 | Iustin Pop | """Copy a file from master to some nodes.
|
1778 | a8083063 | Iustin Pop |
|
1779 | a8083063 | Iustin Pop | Args:
|
1780 | a8083063 | Iustin Pop | opts - class with options as members
|
1781 | a8083063 | Iustin Pop | args - list containing a single element, the file name
|
1782 | a8083063 | Iustin Pop | Opts used:
|
1783 | a8083063 | Iustin Pop | nodes - list containing the name of target nodes; if empty, all nodes
|
1784 | a8083063 | Iustin Pop |
|
1785 | a8083063 | Iustin Pop | """
|
1786 | a8083063 | Iustin Pop | filename = self.op.filename
|
1787 | a8083063 | Iustin Pop | |
1788 | 89e1fc26 | Iustin Pop | myname = utils.HostInfo().name |
1789 | a8083063 | Iustin Pop | |
1790 | a7ba5e53 | Iustin Pop | for node in self.nodes: |
1791 | a8083063 | Iustin Pop | if node == myname:
|
1792 | a8083063 | Iustin Pop | continue
|
1793 | c92b310a | Michael Hanselmann | if not self.ssh.CopyFileToNode(node, filename): |
1794 | a8083063 | Iustin Pop | logger.Error("Copy of file %s to node %s failed" % (filename, node))
|
1795 | a8083063 | Iustin Pop | |
1796 | a8083063 | Iustin Pop | |
1797 | a8083063 | Iustin Pop | class LUDumpClusterConfig(NoHooksLU): |
1798 | a8083063 | Iustin Pop | """Return a text-representation of the cluster-config.
|
1799 | a8083063 | Iustin Pop |
|
1800 | a8083063 | Iustin Pop | """
|
1801 | a8083063 | Iustin Pop | _OP_REQP = [] |
1802 | a8083063 | Iustin Pop | |
1803 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1804 | a8083063 | Iustin Pop | """No prerequisites.
|
1805 | a8083063 | Iustin Pop |
|
1806 | a8083063 | Iustin Pop | """
|
1807 | a8083063 | Iustin Pop | pass
|
1808 | a8083063 | Iustin Pop | |
1809 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1810 | a8083063 | Iustin Pop | """Dump a representation of the cluster config to the standard output.
|
1811 | a8083063 | Iustin Pop |
|
1812 | a8083063 | Iustin Pop | """
|
1813 | a8083063 | Iustin Pop | return self.cfg.DumpConfig() |
1814 | a8083063 | Iustin Pop | |
1815 | a8083063 | Iustin Pop | |
1816 | a8083063 | Iustin Pop | class LURunClusterCommand(NoHooksLU): |
1817 | a8083063 | Iustin Pop | """Run a command on some nodes.
|
1818 | a8083063 | Iustin Pop |
|
1819 | a8083063 | Iustin Pop | """
|
1820 | a8083063 | Iustin Pop | _OP_REQP = ["command", "nodes"] |
1821 | a8083063 | Iustin Pop | |
1822 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1823 | a8083063 | Iustin Pop | """Check prerequisites.
|
1824 | a8083063 | Iustin Pop |
|
1825 | a8083063 | Iustin Pop | It checks that the given list of nodes is valid.
|
1826 | a8083063 | Iustin Pop |
|
1827 | a8083063 | Iustin Pop | """
|
1828 | dcb93971 | Michael Hanselmann | self.nodes = _GetWantedNodes(self, self.op.nodes) |
1829 | a8083063 | Iustin Pop | |
1830 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1831 | a8083063 | Iustin Pop | """Run a command on some nodes.
|
1832 | a8083063 | Iustin Pop |
|
1833 | a8083063 | Iustin Pop | """
|
1834 | 5f83e263 | Iustin Pop | # put the master at the end of the nodes list
|
1835 | 5f83e263 | Iustin Pop | master_node = self.sstore.GetMasterNode()
|
1836 | 5f83e263 | Iustin Pop | if master_node in self.nodes: |
1837 | 5f83e263 | Iustin Pop | self.nodes.remove(master_node)
|
1838 | 5f83e263 | Iustin Pop | self.nodes.append(master_node)
|
1839 | 5f83e263 | Iustin Pop | |
1840 | a8083063 | Iustin Pop | data = [] |
1841 | a8083063 | Iustin Pop | for node in self.nodes: |
1842 | c92b310a | Michael Hanselmann | result = self.ssh.Run(node, "root", self.op.command) |
1843 | a7ba5e53 | Iustin Pop | data.append((node, result.output, result.exit_code)) |
1844 | a8083063 | Iustin Pop | |
1845 | a8083063 | Iustin Pop | return data
|
1846 | a8083063 | Iustin Pop | |
1847 | a8083063 | Iustin Pop | |
1848 | a8083063 | Iustin Pop | class LUActivateInstanceDisks(NoHooksLU): |
1849 | a8083063 | Iustin Pop | """Bring up an instance's disks.
|
1850 | a8083063 | Iustin Pop |
|
1851 | a8083063 | Iustin Pop | """
|
1852 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
1853 | a8083063 | Iustin Pop | |
1854 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1855 | a8083063 | Iustin Pop | """Check prerequisites.
|
1856 | a8083063 | Iustin Pop |
|
1857 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
1858 | a8083063 | Iustin Pop |
|
1859 | a8083063 | Iustin Pop | """
|
1860 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
1861 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
1862 | a8083063 | Iustin Pop | if instance is None: |
1863 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
1864 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
1865 | a8083063 | Iustin Pop | self.instance = instance
|
1866 | a8083063 | Iustin Pop | |
1867 | a8083063 | Iustin Pop | |
1868 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1869 | a8083063 | Iustin Pop | """Activate the disks.
|
1870 | a8083063 | Iustin Pop |
|
1871 | a8083063 | Iustin Pop | """
|
1872 | a8083063 | Iustin Pop | disks_ok, disks_info = _AssembleInstanceDisks(self.instance, self.cfg) |
1873 | a8083063 | Iustin Pop | if not disks_ok: |
1874 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Cannot activate block devices") |
1875 | a8083063 | Iustin Pop | |
1876 | a8083063 | Iustin Pop | return disks_info
|
1877 | a8083063 | Iustin Pop | |
1878 | a8083063 | Iustin Pop | |
1879 | a8083063 | Iustin Pop | def _AssembleInstanceDisks(instance, cfg, ignore_secondaries=False): |
1880 | a8083063 | Iustin Pop | """Prepare the block devices for an instance.
|
1881 | a8083063 | Iustin Pop |
|
1882 | a8083063 | Iustin Pop | This sets up the block devices on all nodes.
|
1883 | a8083063 | Iustin Pop |
|
1884 | a8083063 | Iustin Pop | Args:
|
1885 | a8083063 | Iustin Pop | instance: a ganeti.objects.Instance object
|
1886 | a8083063 | Iustin Pop | ignore_secondaries: if true, errors on secondary nodes won't result
|
1887 | a8083063 | Iustin Pop | in an error return from the function
|
1888 | a8083063 | Iustin Pop |
|
1889 | a8083063 | Iustin Pop | Returns:
|
1890 | a8083063 | Iustin Pop | false if the operation failed
|
1891 | a8083063 | Iustin Pop | list of (host, instance_visible_name, node_visible_name) if the operation
|
1892 | a8083063 | Iustin Pop | suceeded with the mapping from node devices to instance devices
|
1893 | a8083063 | Iustin Pop | """
|
1894 | a8083063 | Iustin Pop | device_info = [] |
1895 | a8083063 | Iustin Pop | disks_ok = True
|
1896 | fdbd668d | Iustin Pop | iname = instance.name |
1897 | fdbd668d | Iustin Pop | # With the two passes mechanism we try to reduce the window of
|
1898 | fdbd668d | Iustin Pop | # opportunity for the race condition of switching DRBD to primary
|
1899 | fdbd668d | Iustin Pop | # before handshaking occured, but we do not eliminate it
|
1900 | fdbd668d | Iustin Pop | |
1901 | fdbd668d | Iustin Pop | # The proper fix would be to wait (with some limits) until the
|
1902 | fdbd668d | Iustin Pop | # connection has been made and drbd transitions from WFConnection
|
1903 | fdbd668d | Iustin Pop | # into any other network-connected state (Connected, SyncTarget,
|
1904 | fdbd668d | Iustin Pop | # SyncSource, etc.)
|
1905 | fdbd668d | Iustin Pop | |
1906 | fdbd668d | Iustin Pop | # 1st pass, assemble on all nodes in secondary mode
|
1907 | a8083063 | Iustin Pop | for inst_disk in instance.disks: |
1908 | a8083063 | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
1909 | a8083063 | Iustin Pop | cfg.SetDiskID(node_disk, node) |
1910 | fdbd668d | Iustin Pop | result = rpc.call_blockdev_assemble(node, node_disk, iname, False)
|
1911 | a8083063 | Iustin Pop | if not result: |
1912 | f4bc1f2c | Michael Hanselmann | logger.Error("could not prepare block device %s on node %s"
|
1913 | fdbd668d | Iustin Pop | " (is_primary=False, pass=1)" % (inst_disk.iv_name, node))
|
1914 | fdbd668d | Iustin Pop | if not ignore_secondaries: |
1915 | a8083063 | Iustin Pop | disks_ok = False
|
1916 | fdbd668d | Iustin Pop | |
1917 | fdbd668d | Iustin Pop | # FIXME: race condition on drbd migration to primary
|
1918 | fdbd668d | Iustin Pop | |
1919 | fdbd668d | Iustin Pop | # 2nd pass, do only the primary node
|
1920 | fdbd668d | Iustin Pop | for inst_disk in instance.disks: |
1921 | fdbd668d | Iustin Pop | for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node): |
1922 | fdbd668d | Iustin Pop | if node != instance.primary_node:
|
1923 | fdbd668d | Iustin Pop | continue
|
1924 | fdbd668d | Iustin Pop | cfg.SetDiskID(node_disk, node) |
1925 | fdbd668d | Iustin Pop | result = rpc.call_blockdev_assemble(node, node_disk, iname, True)
|
1926 | fdbd668d | Iustin Pop | if not result: |
1927 | fdbd668d | Iustin Pop | logger.Error("could not prepare block device %s on node %s"
|
1928 | fdbd668d | Iustin Pop | " (is_primary=True, pass=2)" % (inst_disk.iv_name, node))
|
1929 | fdbd668d | Iustin Pop | disks_ok = False
|
1930 | fdbd668d | Iustin Pop | device_info.append((instance.primary_node, inst_disk.iv_name, result)) |
1931 | a8083063 | Iustin Pop | |
1932 | b352ab5b | Iustin Pop | # leave the disks configured for the primary node
|
1933 | b352ab5b | Iustin Pop | # this is a workaround that would be fixed better by
|
1934 | b352ab5b | Iustin Pop | # improving the logical/physical id handling
|
1935 | b352ab5b | Iustin Pop | for disk in instance.disks: |
1936 | b352ab5b | Iustin Pop | cfg.SetDiskID(disk, instance.primary_node) |
1937 | b352ab5b | Iustin Pop | |
1938 | a8083063 | Iustin Pop | return disks_ok, device_info
|
1939 | a8083063 | Iustin Pop | |
1940 | a8083063 | Iustin Pop | |
1941 | fe7b0351 | Michael Hanselmann | def _StartInstanceDisks(cfg, instance, force): |
1942 | 3ecf6786 | Iustin Pop | """Start the disks of an instance.
|
1943 | 3ecf6786 | Iustin Pop |
|
1944 | 3ecf6786 | Iustin Pop | """
|
1945 | fe7b0351 | Michael Hanselmann | disks_ok, dummy = _AssembleInstanceDisks(instance, cfg, |
1946 | fe7b0351 | Michael Hanselmann | ignore_secondaries=force) |
1947 | fe7b0351 | Michael Hanselmann | if not disks_ok: |
1948 | fe7b0351 | Michael Hanselmann | _ShutdownInstanceDisks(instance, cfg) |
1949 | fe7b0351 | Michael Hanselmann | if force is not None and not force: |
1950 | fe7b0351 | Michael Hanselmann | logger.Error("If the message above refers to a secondary node,"
|
1951 | fe7b0351 | Michael Hanselmann | " you can retry the operation using '--force'.")
|
1952 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Disk consistency error") |
1953 | fe7b0351 | Michael Hanselmann | |
1954 | fe7b0351 | Michael Hanselmann | |
1955 | a8083063 | Iustin Pop | class LUDeactivateInstanceDisks(NoHooksLU): |
1956 | a8083063 | Iustin Pop | """Shutdown an instance's disks.
|
1957 | a8083063 | Iustin Pop |
|
1958 | a8083063 | Iustin Pop | """
|
1959 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
1960 | a8083063 | Iustin Pop | |
1961 | a8083063 | Iustin Pop | def CheckPrereq(self): |
1962 | a8083063 | Iustin Pop | """Check prerequisites.
|
1963 | a8083063 | Iustin Pop |
|
1964 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
1965 | a8083063 | Iustin Pop |
|
1966 | a8083063 | Iustin Pop | """
|
1967 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
1968 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
1969 | a8083063 | Iustin Pop | if instance is None: |
1970 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
1971 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
1972 | a8083063 | Iustin Pop | self.instance = instance
|
1973 | a8083063 | Iustin Pop | |
1974 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
1975 | a8083063 | Iustin Pop | """Deactivate the disks
|
1976 | a8083063 | Iustin Pop |
|
1977 | a8083063 | Iustin Pop | """
|
1978 | a8083063 | Iustin Pop | instance = self.instance
|
1979 | a8083063 | Iustin Pop | ins_l = rpc.call_instance_list([instance.primary_node]) |
1980 | a8083063 | Iustin Pop | ins_l = ins_l[instance.primary_node] |
1981 | a8083063 | Iustin Pop | if not type(ins_l) is list: |
1982 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't contact node '%s'" % |
1983 | 3ecf6786 | Iustin Pop | instance.primary_node) |
1984 | a8083063 | Iustin Pop | |
1985 | a8083063 | Iustin Pop | if self.instance.name in ins_l: |
1986 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Instance is running, can't shutdown" |
1987 | 3ecf6786 | Iustin Pop | " block devices.")
|
1988 | a8083063 | Iustin Pop | |
1989 | a8083063 | Iustin Pop | _ShutdownInstanceDisks(instance, self.cfg)
|
1990 | a8083063 | Iustin Pop | |
1991 | a8083063 | Iustin Pop | |
1992 | a8083063 | Iustin Pop | def _ShutdownInstanceDisks(instance, cfg, ignore_primary=False): |
1993 | a8083063 | Iustin Pop | """Shutdown block devices of an instance.
|
1994 | a8083063 | Iustin Pop |
|
1995 | a8083063 | Iustin Pop | This does the shutdown on all nodes of the instance.
|
1996 | a8083063 | Iustin Pop |
|
1997 | a8083063 | Iustin Pop | If the ignore_primary is false, errors on the primary node are
|
1998 | a8083063 | Iustin Pop | ignored.
|
1999 | a8083063 | Iustin Pop |
|
2000 | a8083063 | Iustin Pop | """
|
2001 | a8083063 | Iustin Pop | result = True
|
2002 | a8083063 | Iustin Pop | for disk in instance.disks: |
2003 | a8083063 | Iustin Pop | for node, top_disk in disk.ComputeNodeTree(instance.primary_node): |
2004 | a8083063 | Iustin Pop | cfg.SetDiskID(top_disk, node) |
2005 | a8083063 | Iustin Pop | if not rpc.call_blockdev_shutdown(node, top_disk): |
2006 | a8083063 | Iustin Pop | logger.Error("could not shutdown block device %s on node %s" %
|
2007 | a8083063 | Iustin Pop | (disk.iv_name, node)) |
2008 | a8083063 | Iustin Pop | if not ignore_primary or node != instance.primary_node: |
2009 | a8083063 | Iustin Pop | result = False
|
2010 | a8083063 | Iustin Pop | return result
|
2011 | a8083063 | Iustin Pop | |
2012 | a8083063 | Iustin Pop | |
2013 | d4f16fd9 | Iustin Pop | def _CheckNodeFreeMemory(cfg, node, reason, requested): |
2014 | d4f16fd9 | Iustin Pop | """Checks if a node has enough free memory.
|
2015 | d4f16fd9 | Iustin Pop |
|
2016 | d4f16fd9 | Iustin Pop | This function check if a given node has the needed amount of free
|
2017 | d4f16fd9 | Iustin Pop | memory. In case the node has less memory or we cannot get the
|
2018 | d4f16fd9 | Iustin Pop | information from the node, this function raise an OpPrereqError
|
2019 | d4f16fd9 | Iustin Pop | exception.
|
2020 | d4f16fd9 | Iustin Pop |
|
2021 | d4f16fd9 | Iustin Pop | Args:
|
2022 | d4f16fd9 | Iustin Pop | - cfg: a ConfigWriter instance
|
2023 | d4f16fd9 | Iustin Pop | - node: the node name
|
2024 | d4f16fd9 | Iustin Pop | - reason: string to use in the error message
|
2025 | d4f16fd9 | Iustin Pop | - requested: the amount of memory in MiB
|
2026 | d4f16fd9 | Iustin Pop |
|
2027 | d4f16fd9 | Iustin Pop | """
|
2028 | d4f16fd9 | Iustin Pop | nodeinfo = rpc.call_node_info([node], cfg.GetVGName()) |
2029 | d4f16fd9 | Iustin Pop | if not nodeinfo or not isinstance(nodeinfo, dict): |
2030 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Could not contact node %s for resource" |
2031 | d4f16fd9 | Iustin Pop | " information" % (node,))
|
2032 | d4f16fd9 | Iustin Pop | |
2033 | d4f16fd9 | Iustin Pop | free_mem = nodeinfo[node].get('memory_free')
|
2034 | d4f16fd9 | Iustin Pop | if not isinstance(free_mem, int): |
2035 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Can't compute free memory on node %s, result" |
2036 | d4f16fd9 | Iustin Pop | " was '%s'" % (node, free_mem))
|
2037 | d4f16fd9 | Iustin Pop | if requested > free_mem:
|
2038 | d4f16fd9 | Iustin Pop | raise errors.OpPrereqError("Not enough memory on node %s for %s:" |
2039 | d4f16fd9 | Iustin Pop | " needed %s MiB, available %s MiB" %
|
2040 | d4f16fd9 | Iustin Pop | (node, reason, requested, free_mem)) |
2041 | d4f16fd9 | Iustin Pop | |
2042 | d4f16fd9 | Iustin Pop | |
2043 | a8083063 | Iustin Pop | class LUStartupInstance(LogicalUnit): |
2044 | a8083063 | Iustin Pop | """Starts an instance.
|
2045 | a8083063 | Iustin Pop |
|
2046 | a8083063 | Iustin Pop | """
|
2047 | a8083063 | Iustin Pop | HPATH = "instance-start"
|
2048 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2049 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "force"] |
2050 | a8083063 | Iustin Pop | |
2051 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2052 | a8083063 | Iustin Pop | """Build hooks env.
|
2053 | a8083063 | Iustin Pop |
|
2054 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
2055 | a8083063 | Iustin Pop |
|
2056 | a8083063 | Iustin Pop | """
|
2057 | a8083063 | Iustin Pop | env = { |
2058 | a8083063 | Iustin Pop | "FORCE": self.op.force, |
2059 | a8083063 | Iustin Pop | } |
2060 | 396e1b78 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self.instance))
|
2061 | 880478f8 | Iustin Pop | nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + |
2062 | a8083063 | Iustin Pop | list(self.instance.secondary_nodes)) |
2063 | a8083063 | Iustin Pop | return env, nl, nl
|
2064 | a8083063 | Iustin Pop | |
2065 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2066 | a8083063 | Iustin Pop | """Check prerequisites.
|
2067 | a8083063 | Iustin Pop |
|
2068 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
2069 | a8083063 | Iustin Pop |
|
2070 | a8083063 | Iustin Pop | """
|
2071 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
2072 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2073 | a8083063 | Iustin Pop | if instance is None: |
2074 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2075 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2076 | a8083063 | Iustin Pop | |
2077 | a8083063 | Iustin Pop | # check bridges existance
|
2078 | bf6929a2 | Alexander Schreiber | _CheckInstanceBridgesExist(instance) |
2079 | a8083063 | Iustin Pop | |
2080 | d4f16fd9 | Iustin Pop | _CheckNodeFreeMemory(self.cfg, instance.primary_node,
|
2081 | d4f16fd9 | Iustin Pop | "starting instance %s" % instance.name,
|
2082 | d4f16fd9 | Iustin Pop | instance.memory) |
2083 | d4f16fd9 | Iustin Pop | |
2084 | a8083063 | Iustin Pop | self.instance = instance
|
2085 | a8083063 | Iustin Pop | self.op.instance_name = instance.name
|
2086 | a8083063 | Iustin Pop | |
2087 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2088 | a8083063 | Iustin Pop | """Start the instance.
|
2089 | a8083063 | Iustin Pop |
|
2090 | a8083063 | Iustin Pop | """
|
2091 | a8083063 | Iustin Pop | instance = self.instance
|
2092 | a8083063 | Iustin Pop | force = self.op.force
|
2093 | a8083063 | Iustin Pop | extra_args = getattr(self.op, "extra_args", "") |
2094 | a8083063 | Iustin Pop | |
2095 | fe482621 | Iustin Pop | self.cfg.MarkInstanceUp(instance.name)
|
2096 | fe482621 | Iustin Pop | |
2097 | a8083063 | Iustin Pop | node_current = instance.primary_node |
2098 | a8083063 | Iustin Pop | |
2099 | fe7b0351 | Michael Hanselmann | _StartInstanceDisks(self.cfg, instance, force)
|
2100 | a8083063 | Iustin Pop | |
2101 | a8083063 | Iustin Pop | if not rpc.call_instance_start(node_current, instance, extra_args): |
2102 | a8083063 | Iustin Pop | _ShutdownInstanceDisks(instance, self.cfg)
|
2103 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Could not start instance") |
2104 | a8083063 | Iustin Pop | |
2105 | a8083063 | Iustin Pop | |
2106 | bf6929a2 | Alexander Schreiber | class LURebootInstance(LogicalUnit): |
2107 | bf6929a2 | Alexander Schreiber | """Reboot an instance.
|
2108 | bf6929a2 | Alexander Schreiber |
|
2109 | bf6929a2 | Alexander Schreiber | """
|
2110 | bf6929a2 | Alexander Schreiber | HPATH = "instance-reboot"
|
2111 | bf6929a2 | Alexander Schreiber | HTYPE = constants.HTYPE_INSTANCE |
2112 | bf6929a2 | Alexander Schreiber | _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"] |
2113 | bf6929a2 | Alexander Schreiber | |
2114 | bf6929a2 | Alexander Schreiber | def BuildHooksEnv(self): |
2115 | bf6929a2 | Alexander Schreiber | """Build hooks env.
|
2116 | bf6929a2 | Alexander Schreiber |
|
2117 | bf6929a2 | Alexander Schreiber | This runs on master, primary and secondary nodes of the instance.
|
2118 | bf6929a2 | Alexander Schreiber |
|
2119 | bf6929a2 | Alexander Schreiber | """
|
2120 | bf6929a2 | Alexander Schreiber | env = { |
2121 | bf6929a2 | Alexander Schreiber | "IGNORE_SECONDARIES": self.op.ignore_secondaries, |
2122 | bf6929a2 | Alexander Schreiber | } |
2123 | bf6929a2 | Alexander Schreiber | env.update(_BuildInstanceHookEnvByObject(self.instance))
|
2124 | bf6929a2 | Alexander Schreiber | nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + |
2125 | bf6929a2 | Alexander Schreiber | list(self.instance.secondary_nodes)) |
2126 | bf6929a2 | Alexander Schreiber | return env, nl, nl
|
2127 | bf6929a2 | Alexander Schreiber | |
2128 | bf6929a2 | Alexander Schreiber | def CheckPrereq(self): |
2129 | bf6929a2 | Alexander Schreiber | """Check prerequisites.
|
2130 | bf6929a2 | Alexander Schreiber |
|
2131 | bf6929a2 | Alexander Schreiber | This checks that the instance is in the cluster.
|
2132 | bf6929a2 | Alexander Schreiber |
|
2133 | bf6929a2 | Alexander Schreiber | """
|
2134 | bf6929a2 | Alexander Schreiber | instance = self.cfg.GetInstanceInfo(
|
2135 | bf6929a2 | Alexander Schreiber | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2136 | bf6929a2 | Alexander Schreiber | if instance is None: |
2137 | bf6929a2 | Alexander Schreiber | raise errors.OpPrereqError("Instance '%s' not known" % |
2138 | bf6929a2 | Alexander Schreiber | self.op.instance_name)
|
2139 | bf6929a2 | Alexander Schreiber | |
2140 | bf6929a2 | Alexander Schreiber | # check bridges existance
|
2141 | bf6929a2 | Alexander Schreiber | _CheckInstanceBridgesExist(instance) |
2142 | bf6929a2 | Alexander Schreiber | |
2143 | bf6929a2 | Alexander Schreiber | self.instance = instance
|
2144 | bf6929a2 | Alexander Schreiber | self.op.instance_name = instance.name
|
2145 | bf6929a2 | Alexander Schreiber | |
2146 | bf6929a2 | Alexander Schreiber | def Exec(self, feedback_fn): |
2147 | bf6929a2 | Alexander Schreiber | """Reboot the instance.
|
2148 | bf6929a2 | Alexander Schreiber |
|
2149 | bf6929a2 | Alexander Schreiber | """
|
2150 | bf6929a2 | Alexander Schreiber | instance = self.instance
|
2151 | bf6929a2 | Alexander Schreiber | ignore_secondaries = self.op.ignore_secondaries
|
2152 | bf6929a2 | Alexander Schreiber | reboot_type = self.op.reboot_type
|
2153 | bf6929a2 | Alexander Schreiber | extra_args = getattr(self.op, "extra_args", "") |
2154 | bf6929a2 | Alexander Schreiber | |
2155 | bf6929a2 | Alexander Schreiber | node_current = instance.primary_node |
2156 | bf6929a2 | Alexander Schreiber | |
2157 | bf6929a2 | Alexander Schreiber | if reboot_type not in [constants.INSTANCE_REBOOT_SOFT, |
2158 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_HARD, |
2159 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_FULL]: |
2160 | bf6929a2 | Alexander Schreiber | raise errors.ParameterError("reboot type not in [%s, %s, %s]" % |
2161 | bf6929a2 | Alexander Schreiber | (constants.INSTANCE_REBOOT_SOFT, |
2162 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_HARD, |
2163 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_FULL)) |
2164 | bf6929a2 | Alexander Schreiber | |
2165 | bf6929a2 | Alexander Schreiber | if reboot_type in [constants.INSTANCE_REBOOT_SOFT, |
2166 | bf6929a2 | Alexander Schreiber | constants.INSTANCE_REBOOT_HARD]: |
2167 | bf6929a2 | Alexander Schreiber | if not rpc.call_instance_reboot(node_current, instance, |
2168 | bf6929a2 | Alexander Schreiber | reboot_type, extra_args): |
2169 | bf6929a2 | Alexander Schreiber | raise errors.OpExecError("Could not reboot instance") |
2170 | bf6929a2 | Alexander Schreiber | else:
|
2171 | bf6929a2 | Alexander Schreiber | if not rpc.call_instance_shutdown(node_current, instance): |
2172 | bf6929a2 | Alexander Schreiber | raise errors.OpExecError("could not shutdown instance for full reboot") |
2173 | bf6929a2 | Alexander Schreiber | _ShutdownInstanceDisks(instance, self.cfg)
|
2174 | bf6929a2 | Alexander Schreiber | _StartInstanceDisks(self.cfg, instance, ignore_secondaries)
|
2175 | bf6929a2 | Alexander Schreiber | if not rpc.call_instance_start(node_current, instance, extra_args): |
2176 | bf6929a2 | Alexander Schreiber | _ShutdownInstanceDisks(instance, self.cfg)
|
2177 | bf6929a2 | Alexander Schreiber | raise errors.OpExecError("Could not start instance for full reboot") |
2178 | bf6929a2 | Alexander Schreiber | |
2179 | bf6929a2 | Alexander Schreiber | self.cfg.MarkInstanceUp(instance.name)
|
2180 | bf6929a2 | Alexander Schreiber | |
2181 | bf6929a2 | Alexander Schreiber | |
2182 | a8083063 | Iustin Pop | class LUShutdownInstance(LogicalUnit): |
2183 | a8083063 | Iustin Pop | """Shutdown an instance.
|
2184 | a8083063 | Iustin Pop |
|
2185 | a8083063 | Iustin Pop | """
|
2186 | a8083063 | Iustin Pop | HPATH = "instance-stop"
|
2187 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2188 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
2189 | a8083063 | Iustin Pop | |
2190 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2191 | a8083063 | Iustin Pop | """Build hooks env.
|
2192 | a8083063 | Iustin Pop |
|
2193 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
2194 | a8083063 | Iustin Pop |
|
2195 | a8083063 | Iustin Pop | """
|
2196 | 396e1b78 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self.instance)
|
2197 | 880478f8 | Iustin Pop | nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + |
2198 | a8083063 | Iustin Pop | list(self.instance.secondary_nodes)) |
2199 | a8083063 | Iustin Pop | return env, nl, nl
|
2200 | a8083063 | Iustin Pop | |
2201 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2202 | a8083063 | Iustin Pop | """Check prerequisites.
|
2203 | a8083063 | Iustin Pop |
|
2204 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
2205 | a8083063 | Iustin Pop |
|
2206 | a8083063 | Iustin Pop | """
|
2207 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
2208 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2209 | a8083063 | Iustin Pop | if instance is None: |
2210 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2211 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2212 | a8083063 | Iustin Pop | self.instance = instance
|
2213 | a8083063 | Iustin Pop | |
2214 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2215 | a8083063 | Iustin Pop | """Shutdown the instance.
|
2216 | a8083063 | Iustin Pop |
|
2217 | a8083063 | Iustin Pop | """
|
2218 | a8083063 | Iustin Pop | instance = self.instance
|
2219 | a8083063 | Iustin Pop | node_current = instance.primary_node |
2220 | fe482621 | Iustin Pop | self.cfg.MarkInstanceDown(instance.name)
|
2221 | a8083063 | Iustin Pop | if not rpc.call_instance_shutdown(node_current, instance): |
2222 | a8083063 | Iustin Pop | logger.Error("could not shutdown instance")
|
2223 | a8083063 | Iustin Pop | |
2224 | a8083063 | Iustin Pop | _ShutdownInstanceDisks(instance, self.cfg)
|
2225 | a8083063 | Iustin Pop | |
2226 | a8083063 | Iustin Pop | |
2227 | fe7b0351 | Michael Hanselmann | class LUReinstallInstance(LogicalUnit): |
2228 | fe7b0351 | Michael Hanselmann | """Reinstall an instance.
|
2229 | fe7b0351 | Michael Hanselmann |
|
2230 | fe7b0351 | Michael Hanselmann | """
|
2231 | fe7b0351 | Michael Hanselmann | HPATH = "instance-reinstall"
|
2232 | fe7b0351 | Michael Hanselmann | HTYPE = constants.HTYPE_INSTANCE |
2233 | fe7b0351 | Michael Hanselmann | _OP_REQP = ["instance_name"]
|
2234 | fe7b0351 | Michael Hanselmann | |
2235 | fe7b0351 | Michael Hanselmann | def BuildHooksEnv(self): |
2236 | fe7b0351 | Michael Hanselmann | """Build hooks env.
|
2237 | fe7b0351 | Michael Hanselmann |
|
2238 | fe7b0351 | Michael Hanselmann | This runs on master, primary and secondary nodes of the instance.
|
2239 | fe7b0351 | Michael Hanselmann |
|
2240 | fe7b0351 | Michael Hanselmann | """
|
2241 | 396e1b78 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self.instance)
|
2242 | fe7b0351 | Michael Hanselmann | nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + |
2243 | fe7b0351 | Michael Hanselmann | list(self.instance.secondary_nodes)) |
2244 | fe7b0351 | Michael Hanselmann | return env, nl, nl
|
2245 | fe7b0351 | Michael Hanselmann | |
2246 | fe7b0351 | Michael Hanselmann | def CheckPrereq(self): |
2247 | fe7b0351 | Michael Hanselmann | """Check prerequisites.
|
2248 | fe7b0351 | Michael Hanselmann |
|
2249 | fe7b0351 | Michael Hanselmann | This checks that the instance is in the cluster and is not running.
|
2250 | fe7b0351 | Michael Hanselmann |
|
2251 | fe7b0351 | Michael Hanselmann | """
|
2252 | fe7b0351 | Michael Hanselmann | instance = self.cfg.GetInstanceInfo(
|
2253 | fe7b0351 | Michael Hanselmann | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2254 | fe7b0351 | Michael Hanselmann | if instance is None: |
2255 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2256 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2257 | fe7b0351 | Michael Hanselmann | if instance.disk_template == constants.DT_DISKLESS:
|
2258 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' has no disks" % |
2259 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2260 | fe7b0351 | Michael Hanselmann | if instance.status != "down": |
2261 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is marked to be up" % |
2262 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2263 | fe7b0351 | Michael Hanselmann | remote_info = rpc.call_instance_info(instance.primary_node, instance.name) |
2264 | fe7b0351 | Michael Hanselmann | if remote_info:
|
2265 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is running on the node %s" % |
2266 | 3ecf6786 | Iustin Pop | (self.op.instance_name,
|
2267 | 3ecf6786 | Iustin Pop | instance.primary_node)) |
2268 | d0834de3 | Michael Hanselmann | |
2269 | d0834de3 | Michael Hanselmann | self.op.os_type = getattr(self.op, "os_type", None) |
2270 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
2271 | d0834de3 | Michael Hanselmann | # OS verification
|
2272 | d0834de3 | Michael Hanselmann | pnode = self.cfg.GetNodeInfo(
|
2273 | d0834de3 | Michael Hanselmann | self.cfg.ExpandNodeName(instance.primary_node))
|
2274 | d0834de3 | Michael Hanselmann | if pnode is None: |
2275 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Primary node '%s' is unknown" % |
2276 | 3ecf6786 | Iustin Pop | self.op.pnode)
|
2277 | 00fe9e38 | Guido Trotter | os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
|
2278 | dfa96ded | Guido Trotter | if not os_obj: |
2279 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("OS '%s' not in supported OS list for" |
2280 | 3ecf6786 | Iustin Pop | " primary node" % self.op.os_type) |
2281 | d0834de3 | Michael Hanselmann | |
2282 | fe7b0351 | Michael Hanselmann | self.instance = instance
|
2283 | fe7b0351 | Michael Hanselmann | |
2284 | fe7b0351 | Michael Hanselmann | def Exec(self, feedback_fn): |
2285 | fe7b0351 | Michael Hanselmann | """Reinstall the instance.
|
2286 | fe7b0351 | Michael Hanselmann |
|
2287 | fe7b0351 | Michael Hanselmann | """
|
2288 | fe7b0351 | Michael Hanselmann | inst = self.instance
|
2289 | fe7b0351 | Michael Hanselmann | |
2290 | d0834de3 | Michael Hanselmann | if self.op.os_type is not None: |
2291 | d0834de3 | Michael Hanselmann | feedback_fn("Changing OS to '%s'..." % self.op.os_type) |
2292 | d0834de3 | Michael Hanselmann | inst.os = self.op.os_type
|
2293 | d0834de3 | Michael Hanselmann | self.cfg.AddInstance(inst)
|
2294 | d0834de3 | Michael Hanselmann | |
2295 | fe7b0351 | Michael Hanselmann | _StartInstanceDisks(self.cfg, inst, None) |
2296 | fe7b0351 | Michael Hanselmann | try:
|
2297 | fe7b0351 | Michael Hanselmann | feedback_fn("Running the instance OS create scripts...")
|
2298 | fe7b0351 | Michael Hanselmann | if not rpc.call_instance_os_add(inst.primary_node, inst, "sda", "sdb"): |
2299 | f4bc1f2c | Michael Hanselmann | raise errors.OpExecError("Could not install OS for instance %s" |
2300 | f4bc1f2c | Michael Hanselmann | " on node %s" %
|
2301 | 3ecf6786 | Iustin Pop | (inst.name, inst.primary_node)) |
2302 | fe7b0351 | Michael Hanselmann | finally:
|
2303 | fe7b0351 | Michael Hanselmann | _ShutdownInstanceDisks(inst, self.cfg)
|
2304 | fe7b0351 | Michael Hanselmann | |
2305 | fe7b0351 | Michael Hanselmann | |
2306 | decd5f45 | Iustin Pop | class LURenameInstance(LogicalUnit): |
2307 | decd5f45 | Iustin Pop | """Rename an instance.
|
2308 | decd5f45 | Iustin Pop |
|
2309 | decd5f45 | Iustin Pop | """
|
2310 | decd5f45 | Iustin Pop | HPATH = "instance-rename"
|
2311 | decd5f45 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2312 | decd5f45 | Iustin Pop | _OP_REQP = ["instance_name", "new_name"] |
2313 | decd5f45 | Iustin Pop | |
2314 | decd5f45 | Iustin Pop | def BuildHooksEnv(self): |
2315 | decd5f45 | Iustin Pop | """Build hooks env.
|
2316 | decd5f45 | Iustin Pop |
|
2317 | decd5f45 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
2318 | decd5f45 | Iustin Pop |
|
2319 | decd5f45 | Iustin Pop | """
|
2320 | decd5f45 | Iustin Pop | env = _BuildInstanceHookEnvByObject(self.instance)
|
2321 | decd5f45 | Iustin Pop | env["INSTANCE_NEW_NAME"] = self.op.new_name |
2322 | decd5f45 | Iustin Pop | nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + |
2323 | decd5f45 | Iustin Pop | list(self.instance.secondary_nodes)) |
2324 | decd5f45 | Iustin Pop | return env, nl, nl
|
2325 | decd5f45 | Iustin Pop | |
2326 | decd5f45 | Iustin Pop | def CheckPrereq(self): |
2327 | decd5f45 | Iustin Pop | """Check prerequisites.
|
2328 | decd5f45 | Iustin Pop |
|
2329 | decd5f45 | Iustin Pop | This checks that the instance is in the cluster and is not running.
|
2330 | decd5f45 | Iustin Pop |
|
2331 | decd5f45 | Iustin Pop | """
|
2332 | decd5f45 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
2333 | decd5f45 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2334 | decd5f45 | Iustin Pop | if instance is None: |
2335 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2336 | decd5f45 | Iustin Pop | self.op.instance_name)
|
2337 | decd5f45 | Iustin Pop | if instance.status != "down": |
2338 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is marked to be up" % |
2339 | decd5f45 | Iustin Pop | self.op.instance_name)
|
2340 | decd5f45 | Iustin Pop | remote_info = rpc.call_instance_info(instance.primary_node, instance.name) |
2341 | decd5f45 | Iustin Pop | if remote_info:
|
2342 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is running on the node %s" % |
2343 | decd5f45 | Iustin Pop | (self.op.instance_name,
|
2344 | decd5f45 | Iustin Pop | instance.primary_node)) |
2345 | decd5f45 | Iustin Pop | self.instance = instance
|
2346 | decd5f45 | Iustin Pop | |
2347 | decd5f45 | Iustin Pop | # new name verification
|
2348 | 89e1fc26 | Iustin Pop | name_info = utils.HostInfo(self.op.new_name)
|
2349 | decd5f45 | Iustin Pop | |
2350 | 89e1fc26 | Iustin Pop | self.op.new_name = new_name = name_info.name
|
2351 | 7bde3275 | Guido Trotter | instance_list = self.cfg.GetInstanceList()
|
2352 | 7bde3275 | Guido Trotter | if new_name in instance_list: |
2353 | 7bde3275 | Guido Trotter | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
2354 | c09f363f | Manuel Franceschini | new_name) |
2355 | 7bde3275 | Guido Trotter | |
2356 | decd5f45 | Iustin Pop | if not getattr(self.op, "ignore_ip", False): |
2357 | 89e1fc26 | Iustin Pop | command = ["fping", "-q", name_info.ip] |
2358 | decd5f45 | Iustin Pop | result = utils.RunCmd(command) |
2359 | decd5f45 | Iustin Pop | if not result.failed: |
2360 | decd5f45 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
2361 | 89e1fc26 | Iustin Pop | (name_info.ip, new_name)) |
2362 | decd5f45 | Iustin Pop | |
2363 | decd5f45 | Iustin Pop | |
2364 | decd5f45 | Iustin Pop | def Exec(self, feedback_fn): |
2365 | decd5f45 | Iustin Pop | """Reinstall the instance.
|
2366 | decd5f45 | Iustin Pop |
|
2367 | decd5f45 | Iustin Pop | """
|
2368 | decd5f45 | Iustin Pop | inst = self.instance
|
2369 | decd5f45 | Iustin Pop | old_name = inst.name |
2370 | decd5f45 | Iustin Pop | |
2371 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
2372 | b23c4333 | Manuel Franceschini | old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
2373 | b23c4333 | Manuel Franceschini | |
2374 | decd5f45 | Iustin Pop | self.cfg.RenameInstance(inst.name, self.op.new_name) |
2375 | decd5f45 | Iustin Pop | |
2376 | decd5f45 | Iustin Pop | # re-read the instance from the configuration after rename
|
2377 | decd5f45 | Iustin Pop | inst = self.cfg.GetInstanceInfo(self.op.new_name) |
2378 | decd5f45 | Iustin Pop | |
2379 | b23c4333 | Manuel Franceschini | if inst.disk_template == constants.DT_FILE:
|
2380 | b23c4333 | Manuel Franceschini | new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1]) |
2381 | b23c4333 | Manuel Franceschini | result = rpc.call_file_storage_dir_rename(inst.primary_node, |
2382 | b23c4333 | Manuel Franceschini | old_file_storage_dir, |
2383 | b23c4333 | Manuel Franceschini | new_file_storage_dir) |
2384 | b23c4333 | Manuel Franceschini | |
2385 | b23c4333 | Manuel Franceschini | if not result: |
2386 | b23c4333 | Manuel Franceschini | raise errors.OpExecError("Could not connect to node '%s' to rename" |
2387 | b23c4333 | Manuel Franceschini | " directory '%s' to '%s' (but the instance"
|
2388 | b23c4333 | Manuel Franceschini | " has been renamed in Ganeti)" % (
|
2389 | b23c4333 | Manuel Franceschini | inst.primary_node, old_file_storage_dir, |
2390 | b23c4333 | Manuel Franceschini | new_file_storage_dir)) |
2391 | b23c4333 | Manuel Franceschini | |
2392 | b23c4333 | Manuel Franceschini | if not result[0]: |
2393 | b23c4333 | Manuel Franceschini | raise errors.OpExecError("Could not rename directory '%s' to '%s'" |
2394 | b23c4333 | Manuel Franceschini | " (but the instance has been renamed in"
|
2395 | b23c4333 | Manuel Franceschini | " Ganeti)" % (old_file_storage_dir,
|
2396 | b23c4333 | Manuel Franceschini | new_file_storage_dir)) |
2397 | b23c4333 | Manuel Franceschini | |
2398 | decd5f45 | Iustin Pop | _StartInstanceDisks(self.cfg, inst, None) |
2399 | decd5f45 | Iustin Pop | try:
|
2400 | decd5f45 | Iustin Pop | if not rpc.call_instance_run_rename(inst.primary_node, inst, old_name, |
2401 | decd5f45 | Iustin Pop | "sda", "sdb"): |
2402 | f4bc1f2c | Michael Hanselmann | msg = ("Could run OS rename script for instance %s on node %s (but the"
|
2403 | f4bc1f2c | Michael Hanselmann | " instance has been renamed in Ganeti)" %
|
2404 | decd5f45 | Iustin Pop | (inst.name, inst.primary_node)) |
2405 | decd5f45 | Iustin Pop | logger.Error(msg) |
2406 | decd5f45 | Iustin Pop | finally:
|
2407 | decd5f45 | Iustin Pop | _ShutdownInstanceDisks(inst, self.cfg)
|
2408 | decd5f45 | Iustin Pop | |
2409 | decd5f45 | Iustin Pop | |
2410 | a8083063 | Iustin Pop | class LURemoveInstance(LogicalUnit): |
2411 | a8083063 | Iustin Pop | """Remove an instance.
|
2412 | a8083063 | Iustin Pop |
|
2413 | a8083063 | Iustin Pop | """
|
2414 | a8083063 | Iustin Pop | HPATH = "instance-remove"
|
2415 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2416 | 5c54b832 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_failures"] |
2417 | a8083063 | Iustin Pop | |
2418 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2419 | a8083063 | Iustin Pop | """Build hooks env.
|
2420 | a8083063 | Iustin Pop |
|
2421 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
2422 | a8083063 | Iustin Pop |
|
2423 | a8083063 | Iustin Pop | """
|
2424 | 396e1b78 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self.instance)
|
2425 | 1d67656e | Iustin Pop | nl = [self.sstore.GetMasterNode()]
|
2426 | a8083063 | Iustin Pop | return env, nl, nl
|
2427 | a8083063 | Iustin Pop | |
2428 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2429 | a8083063 | Iustin Pop | """Check prerequisites.
|
2430 | a8083063 | Iustin Pop |
|
2431 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
2432 | a8083063 | Iustin Pop |
|
2433 | a8083063 | Iustin Pop | """
|
2434 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
2435 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2436 | a8083063 | Iustin Pop | if instance is None: |
2437 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2438 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2439 | a8083063 | Iustin Pop | self.instance = instance
|
2440 | a8083063 | Iustin Pop | |
2441 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2442 | a8083063 | Iustin Pop | """Remove the instance.
|
2443 | a8083063 | Iustin Pop |
|
2444 | a8083063 | Iustin Pop | """
|
2445 | a8083063 | Iustin Pop | instance = self.instance
|
2446 | a8083063 | Iustin Pop | logger.Info("shutting down instance %s on node %s" %
|
2447 | a8083063 | Iustin Pop | (instance.name, instance.primary_node)) |
2448 | a8083063 | Iustin Pop | |
2449 | a8083063 | Iustin Pop | if not rpc.call_instance_shutdown(instance.primary_node, instance): |
2450 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
2451 | 1d67656e | Iustin Pop | feedback_fn("Warning: can't shutdown instance")
|
2452 | 1d67656e | Iustin Pop | else:
|
2453 | 1d67656e | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on node %s" % |
2454 | 1d67656e | Iustin Pop | (instance.name, instance.primary_node)) |
2455 | a8083063 | Iustin Pop | |
2456 | a8083063 | Iustin Pop | logger.Info("removing block devices for instance %s" % instance.name)
|
2457 | a8083063 | Iustin Pop | |
2458 | 1d67656e | Iustin Pop | if not _RemoveDisks(instance, self.cfg): |
2459 | 1d67656e | Iustin Pop | if self.op.ignore_failures: |
2460 | 1d67656e | Iustin Pop | feedback_fn("Warning: can't remove instance's disks")
|
2461 | 1d67656e | Iustin Pop | else:
|
2462 | 1d67656e | Iustin Pop | raise errors.OpExecError("Can't remove instance's disks") |
2463 | a8083063 | Iustin Pop | |
2464 | a8083063 | Iustin Pop | logger.Info("removing instance %s out of cluster config" % instance.name)
|
2465 | a8083063 | Iustin Pop | |
2466 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(instance.name)
|
2467 | a8083063 | Iustin Pop | |
2468 | a8083063 | Iustin Pop | |
2469 | a8083063 | Iustin Pop | class LUQueryInstances(NoHooksLU): |
2470 | a8083063 | Iustin Pop | """Logical unit for querying instances.
|
2471 | a8083063 | Iustin Pop |
|
2472 | a8083063 | Iustin Pop | """
|
2473 | 069dcc86 | Iustin Pop | _OP_REQP = ["output_fields", "names"] |
2474 | a8083063 | Iustin Pop | |
2475 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2476 | a8083063 | Iustin Pop | """Check prerequisites.
|
2477 | a8083063 | Iustin Pop |
|
2478 | a8083063 | Iustin Pop | This checks that the fields required are valid output fields.
|
2479 | a8083063 | Iustin Pop |
|
2480 | a8083063 | Iustin Pop | """
|
2481 | d8052456 | Iustin Pop | self.dynamic_fields = frozenset(["oper_state", "oper_ram", "status"]) |
2482 | dcb93971 | Michael Hanselmann | _CheckOutputFields(static=["name", "os", "pnode", "snodes", |
2483 | dcb93971 | Michael Hanselmann | "admin_state", "admin_ram", |
2484 | 644eeef9 | Iustin Pop | "disk_template", "ip", "mac", "bridge", |
2485 | d6d415e8 | Iustin Pop | "sda_size", "sdb_size", "vcpus"], |
2486 | dcb93971 | Michael Hanselmann | dynamic=self.dynamic_fields,
|
2487 | dcb93971 | Michael Hanselmann | selected=self.op.output_fields)
|
2488 | a8083063 | Iustin Pop | |
2489 | 069dcc86 | Iustin Pop | self.wanted = _GetWantedInstances(self, self.op.names) |
2490 | 069dcc86 | Iustin Pop | |
2491 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2492 | a8083063 | Iustin Pop | """Computes the list of nodes and their attributes.
|
2493 | a8083063 | Iustin Pop |
|
2494 | a8083063 | Iustin Pop | """
|
2495 | 069dcc86 | Iustin Pop | instance_names = self.wanted
|
2496 | a8083063 | Iustin Pop | instance_list = [self.cfg.GetInstanceInfo(iname) for iname |
2497 | a8083063 | Iustin Pop | in instance_names]
|
2498 | a8083063 | Iustin Pop | |
2499 | a8083063 | Iustin Pop | # begin data gathering
|
2500 | a8083063 | Iustin Pop | |
2501 | a8083063 | Iustin Pop | nodes = frozenset([inst.primary_node for inst in instance_list]) |
2502 | a8083063 | Iustin Pop | |
2503 | a8083063 | Iustin Pop | bad_nodes = [] |
2504 | a8083063 | Iustin Pop | if self.dynamic_fields.intersection(self.op.output_fields): |
2505 | a8083063 | Iustin Pop | live_data = {} |
2506 | a8083063 | Iustin Pop | node_data = rpc.call_all_instances_info(nodes) |
2507 | a8083063 | Iustin Pop | for name in nodes: |
2508 | a8083063 | Iustin Pop | result = node_data[name] |
2509 | a8083063 | Iustin Pop | if result:
|
2510 | a8083063 | Iustin Pop | live_data.update(result) |
2511 | a8083063 | Iustin Pop | elif result == False: |
2512 | a8083063 | Iustin Pop | bad_nodes.append(name) |
2513 | a8083063 | Iustin Pop | # else no instance is alive
|
2514 | a8083063 | Iustin Pop | else:
|
2515 | a8083063 | Iustin Pop | live_data = dict([(name, {}) for name in instance_names]) |
2516 | a8083063 | Iustin Pop | |
2517 | a8083063 | Iustin Pop | # end data gathering
|
2518 | a8083063 | Iustin Pop | |
2519 | a8083063 | Iustin Pop | output = [] |
2520 | a8083063 | Iustin Pop | for instance in instance_list: |
2521 | a8083063 | Iustin Pop | iout = [] |
2522 | a8083063 | Iustin Pop | for field in self.op.output_fields: |
2523 | a8083063 | Iustin Pop | if field == "name": |
2524 | a8083063 | Iustin Pop | val = instance.name |
2525 | a8083063 | Iustin Pop | elif field == "os": |
2526 | a8083063 | Iustin Pop | val = instance.os |
2527 | a8083063 | Iustin Pop | elif field == "pnode": |
2528 | a8083063 | Iustin Pop | val = instance.primary_node |
2529 | a8083063 | Iustin Pop | elif field == "snodes": |
2530 | 8a23d2d3 | Iustin Pop | val = list(instance.secondary_nodes)
|
2531 | a8083063 | Iustin Pop | elif field == "admin_state": |
2532 | 8a23d2d3 | Iustin Pop | val = (instance.status != "down")
|
2533 | a8083063 | Iustin Pop | elif field == "oper_state": |
2534 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
2535 | 8a23d2d3 | Iustin Pop | val = None
|
2536 | a8083063 | Iustin Pop | else:
|
2537 | 8a23d2d3 | Iustin Pop | val = bool(live_data.get(instance.name))
|
2538 | d8052456 | Iustin Pop | elif field == "status": |
2539 | d8052456 | Iustin Pop | if instance.primary_node in bad_nodes: |
2540 | d8052456 | Iustin Pop | val = "ERROR_nodedown"
|
2541 | d8052456 | Iustin Pop | else:
|
2542 | d8052456 | Iustin Pop | running = bool(live_data.get(instance.name))
|
2543 | d8052456 | Iustin Pop | if running:
|
2544 | d8052456 | Iustin Pop | if instance.status != "down": |
2545 | d8052456 | Iustin Pop | val = "running"
|
2546 | d8052456 | Iustin Pop | else:
|
2547 | d8052456 | Iustin Pop | val = "ERROR_up"
|
2548 | d8052456 | Iustin Pop | else:
|
2549 | d8052456 | Iustin Pop | if instance.status != "down": |
2550 | d8052456 | Iustin Pop | val = "ERROR_down"
|
2551 | d8052456 | Iustin Pop | else:
|
2552 | d8052456 | Iustin Pop | val = "ADMIN_down"
|
2553 | a8083063 | Iustin Pop | elif field == "admin_ram": |
2554 | a8083063 | Iustin Pop | val = instance.memory |
2555 | a8083063 | Iustin Pop | elif field == "oper_ram": |
2556 | a8083063 | Iustin Pop | if instance.primary_node in bad_nodes: |
2557 | 8a23d2d3 | Iustin Pop | val = None
|
2558 | a8083063 | Iustin Pop | elif instance.name in live_data: |
2559 | a8083063 | Iustin Pop | val = live_data[instance.name].get("memory", "?") |
2560 | a8083063 | Iustin Pop | else:
|
2561 | a8083063 | Iustin Pop | val = "-"
|
2562 | a8083063 | Iustin Pop | elif field == "disk_template": |
2563 | a8083063 | Iustin Pop | val = instance.disk_template |
2564 | a8083063 | Iustin Pop | elif field == "ip": |
2565 | a8083063 | Iustin Pop | val = instance.nics[0].ip
|
2566 | a8083063 | Iustin Pop | elif field == "bridge": |
2567 | a8083063 | Iustin Pop | val = instance.nics[0].bridge
|
2568 | a8083063 | Iustin Pop | elif field == "mac": |
2569 | a8083063 | Iustin Pop | val = instance.nics[0].mac
|
2570 | 644eeef9 | Iustin Pop | elif field == "sda_size" or field == "sdb_size": |
2571 | 644eeef9 | Iustin Pop | disk = instance.FindDisk(field[:3])
|
2572 | 644eeef9 | Iustin Pop | if disk is None: |
2573 | 8a23d2d3 | Iustin Pop | val = None
|
2574 | 644eeef9 | Iustin Pop | else:
|
2575 | 644eeef9 | Iustin Pop | val = disk.size |
2576 | d6d415e8 | Iustin Pop | elif field == "vcpus": |
2577 | d6d415e8 | Iustin Pop | val = instance.vcpus |
2578 | a8083063 | Iustin Pop | else:
|
2579 | 3ecf6786 | Iustin Pop | raise errors.ParameterError(field)
|
2580 | a8083063 | Iustin Pop | iout.append(val) |
2581 | a8083063 | Iustin Pop | output.append(iout) |
2582 | a8083063 | Iustin Pop | |
2583 | a8083063 | Iustin Pop | return output
|
2584 | a8083063 | Iustin Pop | |
2585 | a8083063 | Iustin Pop | |
2586 | a8083063 | Iustin Pop | class LUFailoverInstance(LogicalUnit): |
2587 | a8083063 | Iustin Pop | """Failover an instance.
|
2588 | a8083063 | Iustin Pop |
|
2589 | a8083063 | Iustin Pop | """
|
2590 | a8083063 | Iustin Pop | HPATH = "instance-failover"
|
2591 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2592 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "ignore_consistency"] |
2593 | a8083063 | Iustin Pop | |
2594 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
2595 | a8083063 | Iustin Pop | """Build hooks env.
|
2596 | a8083063 | Iustin Pop |
|
2597 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
2598 | a8083063 | Iustin Pop |
|
2599 | a8083063 | Iustin Pop | """
|
2600 | a8083063 | Iustin Pop | env = { |
2601 | a8083063 | Iustin Pop | "IGNORE_CONSISTENCY": self.op.ignore_consistency, |
2602 | a8083063 | Iustin Pop | } |
2603 | 396e1b78 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self.instance))
|
2604 | 880478f8 | Iustin Pop | nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes) |
2605 | a8083063 | Iustin Pop | return env, nl, nl
|
2606 | a8083063 | Iustin Pop | |
2607 | a8083063 | Iustin Pop | def CheckPrereq(self): |
2608 | a8083063 | Iustin Pop | """Check prerequisites.
|
2609 | a8083063 | Iustin Pop |
|
2610 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
2611 | a8083063 | Iustin Pop |
|
2612 | a8083063 | Iustin Pop | """
|
2613 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
2614 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
2615 | a8083063 | Iustin Pop | if instance is None: |
2616 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
2617 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
2618 | a8083063 | Iustin Pop | |
2619 | a1f445d3 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
2620 | 2a710df1 | Michael Hanselmann | raise errors.OpPrereqError("Instance's disk layout is not" |
2621 | a1f445d3 | Iustin Pop | " network mirrored, cannot failover.")
|
2622 | 2a710df1 | Michael Hanselmann | |
2623 | 2a710df1 | Michael Hanselmann | secondary_nodes = instance.secondary_nodes |
2624 | 2a710df1 | Michael Hanselmann | if not secondary_nodes: |
2625 | 2a710df1 | Michael Hanselmann | raise errors.ProgrammerError("no secondary node but using " |
2626 | abdf0113 | Iustin Pop | "a mirrored disk template")
|
2627 | 2a710df1 | Michael Hanselmann | |
2628 | 2a710df1 | Michael Hanselmann | target_node = secondary_nodes[0]
|
2629 | d4f16fd9 | Iustin Pop | # check memory requirements on the secondary node
|
2630 | d4f16fd9 | Iustin Pop | _CheckNodeFreeMemory(self.cfg, target_node, "failing over instance %s" % |
2631 | d4f16fd9 | Iustin Pop | instance.name, instance.memory) |
2632 | 3a7c308e | Guido Trotter | |
2633 | a8083063 | Iustin Pop | # check bridge existance
|
2634 | a8083063 | Iustin Pop | brlist = [nic.bridge for nic in instance.nics] |
2635 | 50ff9a7a | Iustin Pop | if not rpc.call_bridges_exist(target_node, brlist): |
2636 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("One or more target bridges %s does not" |
2637 | 3ecf6786 | Iustin Pop | " exist on destination node '%s'" %
|
2638 | 50ff9a7a | Iustin Pop | (brlist, target_node)) |
2639 | a8083063 | Iustin Pop | |
2640 | a8083063 | Iustin Pop | self.instance = instance
|
2641 | a8083063 | Iustin Pop | |
2642 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
2643 | a8083063 | Iustin Pop | """Failover an instance.
|
2644 | a8083063 | Iustin Pop |
|
2645 | a8083063 | Iustin Pop | The failover is done by shutting it down on its present node and
|
2646 | a8083063 | Iustin Pop | starting it on the secondary.
|
2647 | a8083063 | Iustin Pop |
|
2648 | a8083063 | Iustin Pop | """
|
2649 | a8083063 | Iustin Pop | instance = self.instance
|
2650 | a8083063 | Iustin Pop | |
2651 | a8083063 | Iustin Pop | source_node = instance.primary_node |
2652 | a8083063 | Iustin Pop | target_node = instance.secondary_nodes[0]
|
2653 | a8083063 | Iustin Pop | |
2654 | a8083063 | Iustin Pop | feedback_fn("* checking disk consistency between source and target")
|
2655 | a8083063 | Iustin Pop | for dev in instance.disks: |
2656 | abdf0113 | Iustin Pop | # for drbd, these are drbd over lvm
|
2657 | a8083063 | Iustin Pop | if not _CheckDiskConsistency(self.cfg, dev, target_node, False): |
2658 | a0aaa0d0 | Guido Trotter | if instance.status == "up" and not self.op.ignore_consistency: |
2659 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Disk %s is degraded on target node," |
2660 | 3ecf6786 | Iustin Pop | " aborting failover." % dev.iv_name)
|
2661 | a8083063 | Iustin Pop | |
2662 | a8083063 | Iustin Pop | feedback_fn("* shutting down instance on source node")
|
2663 | a8083063 | Iustin Pop | logger.Info("Shutting down instance %s on node %s" %
|
2664 | a8083063 | Iustin Pop | (instance.name, source_node)) |
2665 | a8083063 | Iustin Pop | |
2666 | a8083063 | Iustin Pop | if not rpc.call_instance_shutdown(source_node, instance): |
2667 | 24a40d57 | Iustin Pop | if self.op.ignore_consistency: |
2668 | 24a40d57 | Iustin Pop | logger.Error("Could not shutdown instance %s on node %s. Proceeding"
|
2669 | 24a40d57 | Iustin Pop | " anyway. Please make sure node %s is down" %
|
2670 | 24a40d57 | Iustin Pop | (instance.name, source_node, source_node)) |
2671 | 24a40d57 | Iustin Pop | else:
|
2672 | 24a40d57 | Iustin Pop | raise errors.OpExecError("Could not shutdown instance %s on node %s" % |
2673 | 24a40d57 | Iustin Pop | (instance.name, source_node)) |
2674 | a8083063 | Iustin Pop | |
2675 | a8083063 | Iustin Pop | feedback_fn("* deactivating the instance's disks on source node")
|
2676 | a8083063 | Iustin Pop | if not _ShutdownInstanceDisks(instance, self.cfg, ignore_primary=True): |
2677 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't shut down the instance's disks.") |
2678 | a8083063 | Iustin Pop | |
2679 | a8083063 | Iustin Pop | instance.primary_node = target_node |
2680 | a8083063 | Iustin Pop | # distribute new instance config to the other nodes
|
2681 | a8083063 | Iustin Pop | self.cfg.AddInstance(instance)
|
2682 | a8083063 | Iustin Pop | |
2683 | 12a0cfbe | Guido Trotter | # Only start the instance if it's marked as up
|
2684 | 12a0cfbe | Guido Trotter | if instance.status == "up": |
2685 | 12a0cfbe | Guido Trotter | feedback_fn("* activating the instance's disks on target node")
|
2686 | 12a0cfbe | Guido Trotter | logger.Info("Starting instance %s on node %s" %
|
2687 | 12a0cfbe | Guido Trotter | (instance.name, target_node)) |
2688 | 12a0cfbe | Guido Trotter | |
2689 | 12a0cfbe | Guido Trotter | disks_ok, dummy = _AssembleInstanceDisks(instance, self.cfg,
|
2690 | 12a0cfbe | Guido Trotter | ignore_secondaries=True)
|
2691 | 12a0cfbe | Guido Trotter | if not disks_ok: |
2692 | 12a0cfbe | Guido Trotter | _ShutdownInstanceDisks(instance, self.cfg)
|
2693 | 12a0cfbe | Guido Trotter | raise errors.OpExecError("Can't activate the instance's disks") |
2694 | a8083063 | Iustin Pop | |
2695 | 12a0cfbe | Guido Trotter | feedback_fn("* starting the instance on the target node")
|
2696 | 12a0cfbe | Guido Trotter | if not rpc.call_instance_start(target_node, instance, None): |
2697 | 12a0cfbe | Guido Trotter | _ShutdownInstanceDisks(instance, self.cfg)
|
2698 | 12a0cfbe | Guido Trotter | raise errors.OpExecError("Could not start instance %s on node %s." % |
2699 | 12a0cfbe | Guido Trotter | (instance.name, target_node)) |
2700 | a8083063 | Iustin Pop | |
2701 | a8083063 | Iustin Pop | |
2702 | 3f78eef2 | Iustin Pop | def _CreateBlockDevOnPrimary(cfg, node, instance, device, info): |
2703 | a8083063 | Iustin Pop | """Create a tree of block devices on the primary node.
|
2704 | a8083063 | Iustin Pop |
|
2705 | a8083063 | Iustin Pop | This always creates all devices.
|
2706 | a8083063 | Iustin Pop |
|
2707 | a8083063 | Iustin Pop | """
|
2708 | a8083063 | Iustin Pop | if device.children:
|
2709 | a8083063 | Iustin Pop | for child in device.children: |
2710 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnPrimary(cfg, node, instance, child, info): |
2711 | a8083063 | Iustin Pop | return False |
2712 | a8083063 | Iustin Pop | |
2713 | a8083063 | Iustin Pop | cfg.SetDiskID(device, node) |
2714 | 3f78eef2 | Iustin Pop | new_id = rpc.call_blockdev_create(node, device, device.size, |
2715 | 3f78eef2 | Iustin Pop | instance.name, True, info)
|
2716 | a8083063 | Iustin Pop | if not new_id: |
2717 | a8083063 | Iustin Pop | return False |
2718 | a8083063 | Iustin Pop | if device.physical_id is None: |
2719 | a8083063 | Iustin Pop | device.physical_id = new_id |
2720 | a8083063 | Iustin Pop | return True |
2721 | a8083063 | Iustin Pop | |
2722 | a8083063 | Iustin Pop | |
2723 | 3f78eef2 | Iustin Pop | def _CreateBlockDevOnSecondary(cfg, node, instance, device, force, info): |
2724 | a8083063 | Iustin Pop | """Create a tree of block devices on a secondary node.
|
2725 | a8083063 | Iustin Pop |
|
2726 | a8083063 | Iustin Pop | If this device type has to be created on secondaries, create it and
|
2727 | a8083063 | Iustin Pop | all its children.
|
2728 | a8083063 | Iustin Pop |
|
2729 | a8083063 | Iustin Pop | If not, just recurse to children keeping the same 'force' value.
|
2730 | a8083063 | Iustin Pop |
|
2731 | a8083063 | Iustin Pop | """
|
2732 | a8083063 | Iustin Pop | if device.CreateOnSecondary():
|
2733 | a8083063 | Iustin Pop | force = True
|
2734 | a8083063 | Iustin Pop | if device.children:
|
2735 | a8083063 | Iustin Pop | for child in device.children: |
2736 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnSecondary(cfg, node, instance, |
2737 | 3f78eef2 | Iustin Pop | child, force, info): |
2738 | a8083063 | Iustin Pop | return False |
2739 | a8083063 | Iustin Pop | |
2740 | a8083063 | Iustin Pop | if not force: |
2741 | a8083063 | Iustin Pop | return True |
2742 | a8083063 | Iustin Pop | cfg.SetDiskID(device, node) |
2743 | 3f78eef2 | Iustin Pop | new_id = rpc.call_blockdev_create(node, device, device.size, |
2744 | 3f78eef2 | Iustin Pop | instance.name, False, info)
|
2745 | a8083063 | Iustin Pop | if not new_id: |
2746 | a8083063 | Iustin Pop | return False |
2747 | a8083063 | Iustin Pop | if device.physical_id is None: |
2748 | a8083063 | Iustin Pop | device.physical_id = new_id |
2749 | a8083063 | Iustin Pop | return True |
2750 | a8083063 | Iustin Pop | |
2751 | a8083063 | Iustin Pop | |
2752 | 923b1523 | Iustin Pop | def _GenerateUniqueNames(cfg, exts): |
2753 | 923b1523 | Iustin Pop | """Generate a suitable LV name.
|
2754 | 923b1523 | Iustin Pop |
|
2755 | 923b1523 | Iustin Pop | This will generate a logical volume name for the given instance.
|
2756 | 923b1523 | Iustin Pop |
|
2757 | 923b1523 | Iustin Pop | """
|
2758 | 923b1523 | Iustin Pop | results = [] |
2759 | 923b1523 | Iustin Pop | for val in exts: |
2760 | 923b1523 | Iustin Pop | new_id = cfg.GenerateUniqueID() |
2761 | 923b1523 | Iustin Pop | results.append("%s%s" % (new_id, val))
|
2762 | 923b1523 | Iustin Pop | return results
|
2763 | 923b1523 | Iustin Pop | |
2764 | 923b1523 | Iustin Pop | |
2765 | 923b1523 | Iustin Pop | def _GenerateMDDRBDBranch(cfg, primary, secondary, size, names): |
2766 | a8083063 | Iustin Pop | """Generate a drbd device complete with its children.
|
2767 | a8083063 | Iustin Pop |
|
2768 | a8083063 | Iustin Pop | """
|
2769 | a8083063 | Iustin Pop | port = cfg.AllocatePort() |
2770 | 923b1523 | Iustin Pop | vgname = cfg.GetVGName() |
2771 | fe96220b | Iustin Pop | dev_data = objects.Disk(dev_type=constants.LD_LV, size=size, |
2772 | 923b1523 | Iustin Pop | logical_id=(vgname, names[0]))
|
2773 | fe96220b | Iustin Pop | dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
2774 | 923b1523 | Iustin Pop | logical_id=(vgname, names[1]))
|
2775 | fe96220b | Iustin Pop | drbd_dev = objects.Disk(dev_type=constants.LD_DRBD7, size=size, |
2776 | a8083063 | Iustin Pop | logical_id = (primary, secondary, port), |
2777 | a8083063 | Iustin Pop | children = [dev_data, dev_meta]) |
2778 | a8083063 | Iustin Pop | return drbd_dev
|
2779 | a8083063 | Iustin Pop | |
2780 | a8083063 | Iustin Pop | |
2781 | a1f445d3 | Iustin Pop | def _GenerateDRBD8Branch(cfg, primary, secondary, size, names, iv_name): |
2782 | a1f445d3 | Iustin Pop | """Generate a drbd8 device complete with its children.
|
2783 | a1f445d3 | Iustin Pop |
|
2784 | a1f445d3 | Iustin Pop | """
|
2785 | a1f445d3 | Iustin Pop | port = cfg.AllocatePort() |
2786 | a1f445d3 | Iustin Pop | vgname = cfg.GetVGName() |
2787 | a1f445d3 | Iustin Pop | dev_data = objects.Disk(dev_type=constants.LD_LV, size=size, |
2788 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[0]))
|
2789 | a1f445d3 | Iustin Pop | dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
2790 | a1f445d3 | Iustin Pop | logical_id=(vgname, names[1]))
|
2791 | a1f445d3 | Iustin Pop | drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size, |
2792 | a1f445d3 | Iustin Pop | logical_id = (primary, secondary, port), |
2793 | a1f445d3 | Iustin Pop | children = [dev_data, dev_meta], |
2794 | a1f445d3 | Iustin Pop | iv_name=iv_name) |
2795 | a1f445d3 | Iustin Pop | return drbd_dev
|
2796 | a1f445d3 | Iustin Pop | |
2797 | 7c0d6283 | Michael Hanselmann | |
2798 | 923b1523 | Iustin Pop | def _GenerateDiskTemplate(cfg, template_name, |
2799 | a8083063 | Iustin Pop | instance_name, primary_node, |
2800 | 0f1a06e3 | Manuel Franceschini | secondary_nodes, disk_sz, swap_sz, |
2801 | 0f1a06e3 | Manuel Franceschini | file_storage_dir, file_driver): |
2802 | a8083063 | Iustin Pop | """Generate the entire disk layout for a given template type.
|
2803 | a8083063 | Iustin Pop |
|
2804 | a8083063 | Iustin Pop | """
|
2805 | a8083063 | Iustin Pop | #TODO: compute space requirements
|
2806 | a8083063 | Iustin Pop | |
2807 | 923b1523 | Iustin Pop | vgname = cfg.GetVGName() |
2808 | 3517d9b9 | Manuel Franceschini | if template_name == constants.DT_DISKLESS:
|
2809 | a8083063 | Iustin Pop | disks = [] |
2810 | 3517d9b9 | Manuel Franceschini | elif template_name == constants.DT_PLAIN:
|
2811 | a8083063 | Iustin Pop | if len(secondary_nodes) != 0: |
2812 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
2813 | 923b1523 | Iustin Pop | |
2814 | 923b1523 | Iustin Pop | names = _GenerateUniqueNames(cfg, [".sda", ".sdb"]) |
2815 | fe96220b | Iustin Pop | sda_dev = objects.Disk(dev_type=constants.LD_LV, size=disk_sz, |
2816 | 923b1523 | Iustin Pop | logical_id=(vgname, names[0]),
|
2817 | a8083063 | Iustin Pop | iv_name = "sda")
|
2818 | fe96220b | Iustin Pop | sdb_dev = objects.Disk(dev_type=constants.LD_LV, size=swap_sz, |
2819 | 923b1523 | Iustin Pop | logical_id=(vgname, names[1]),
|
2820 | a8083063 | Iustin Pop | iv_name = "sdb")
|
2821 | a8083063 | Iustin Pop | disks = [sda_dev, sdb_dev] |
2822 | a1f445d3 | Iustin Pop | elif template_name == constants.DT_DRBD8:
|
2823 | a1f445d3 | Iustin Pop | if len(secondary_nodes) != 1: |
2824 | a1f445d3 | Iustin Pop | raise errors.ProgrammerError("Wrong template configuration") |
2825 | a1f445d3 | Iustin Pop | remote_node = secondary_nodes[0]
|
2826 | a1f445d3 | Iustin Pop | names = _GenerateUniqueNames(cfg, [".sda_data", ".sda_meta", |
2827 | a1f445d3 | Iustin Pop | ".sdb_data", ".sdb_meta"]) |
2828 | a1f445d3 | Iustin Pop | drbd_sda_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node, |
2829 | a1f445d3 | Iustin Pop | disk_sz, names[0:2], "sda") |
2830 | a1f445d3 | Iustin Pop | drbd_sdb_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node, |
2831 | a1f445d3 | Iustin Pop | swap_sz, names[2:4], "sdb") |
2832 | a1f445d3 | Iustin Pop | disks = [drbd_sda_dev, drbd_sdb_dev] |
2833 | 0f1a06e3 | Manuel Franceschini | elif template_name == constants.DT_FILE:
|
2834 | 0f1a06e3 | Manuel Franceschini | if len(secondary_nodes) != 0: |
2835 | 0f1a06e3 | Manuel Franceschini | raise errors.ProgrammerError("Wrong template configuration") |
2836 | 0f1a06e3 | Manuel Franceschini | |
2837 | 0f1a06e3 | Manuel Franceschini | file_sda_dev = objects.Disk(dev_type=constants.LD_FILE, size=disk_sz, |
2838 | 0f1a06e3 | Manuel Franceschini | iv_name="sda", logical_id=(file_driver,
|
2839 | 0f1a06e3 | Manuel Franceschini | "%s/sda" % file_storage_dir))
|
2840 | 0f1a06e3 | Manuel Franceschini | file_sdb_dev = objects.Disk(dev_type=constants.LD_FILE, size=swap_sz, |
2841 | 0f1a06e3 | Manuel Franceschini | iv_name="sdb", logical_id=(file_driver,
|
2842 | 0f1a06e3 | Manuel Franceschini | "%s/sdb" % file_storage_dir))
|
2843 | 0f1a06e3 | Manuel Franceschini | disks = [file_sda_dev, file_sdb_dev] |
2844 | a8083063 | Iustin Pop | else:
|
2845 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid disk template '%s'" % template_name) |
2846 | a8083063 | Iustin Pop | return disks
|
2847 | a8083063 | Iustin Pop | |
2848 | a8083063 | Iustin Pop | |
2849 | a0c3fea1 | Michael Hanselmann | def _GetInstanceInfoText(instance): |
2850 | 3ecf6786 | Iustin Pop | """Compute that text that should be added to the disk's metadata.
|
2851 | 3ecf6786 | Iustin Pop |
|
2852 | 3ecf6786 | Iustin Pop | """
|
2853 | a0c3fea1 | Michael Hanselmann | return "originstname+%s" % instance.name |
2854 | a0c3fea1 | Michael Hanselmann | |
2855 | a0c3fea1 | Michael Hanselmann | |
2856 | a8083063 | Iustin Pop | def _CreateDisks(cfg, instance): |
2857 | a8083063 | Iustin Pop | """Create all disks for an instance.
|
2858 | a8083063 | Iustin Pop |
|
2859 | a8083063 | Iustin Pop | This abstracts away some work from AddInstance.
|
2860 | a8083063 | Iustin Pop |
|
2861 | a8083063 | Iustin Pop | Args:
|
2862 | a8083063 | Iustin Pop | instance: the instance object
|
2863 | a8083063 | Iustin Pop |
|
2864 | a8083063 | Iustin Pop | Returns:
|
2865 | a8083063 | Iustin Pop | True or False showing the success of the creation process
|
2866 | a8083063 | Iustin Pop |
|
2867 | a8083063 | Iustin Pop | """
|
2868 | a0c3fea1 | Michael Hanselmann | info = _GetInstanceInfoText(instance) |
2869 | a0c3fea1 | Michael Hanselmann | |
2870 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
2871 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
2872 | 0f1a06e3 | Manuel Franceschini | result = rpc.call_file_storage_dir_create(instance.primary_node, |
2873 | 0f1a06e3 | Manuel Franceschini | file_storage_dir) |
2874 | 0f1a06e3 | Manuel Franceschini | |
2875 | 0f1a06e3 | Manuel Franceschini | if not result: |
2876 | b62ddbe5 | Guido Trotter | logger.Error("Could not connect to node '%s'" % instance.primary_node)
|
2877 | 0f1a06e3 | Manuel Franceschini | return False |
2878 | 0f1a06e3 | Manuel Franceschini | |
2879 | 0f1a06e3 | Manuel Franceschini | if not result[0]: |
2880 | 0f1a06e3 | Manuel Franceschini | logger.Error("failed to create directory '%s'" % file_storage_dir)
|
2881 | 0f1a06e3 | Manuel Franceschini | return False |
2882 | 0f1a06e3 | Manuel Franceschini | |
2883 | a8083063 | Iustin Pop | for device in instance.disks: |
2884 | a8083063 | Iustin Pop | logger.Info("creating volume %s for instance %s" %
|
2885 | 1c6e3627 | Manuel Franceschini | (device.iv_name, instance.name)) |
2886 | a8083063 | Iustin Pop | #HARDCODE
|
2887 | a8083063 | Iustin Pop | for secondary_node in instance.secondary_nodes: |
2888 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnSecondary(cfg, secondary_node, instance, |
2889 | 3f78eef2 | Iustin Pop | device, False, info):
|
2890 | a8083063 | Iustin Pop | logger.Error("failed to create volume %s (%s) on secondary node %s!" %
|
2891 | a8083063 | Iustin Pop | (device.iv_name, device, secondary_node)) |
2892 | a8083063 | Iustin Pop | return False |
2893 | a8083063 | Iustin Pop | #HARDCODE
|
2894 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnPrimary(cfg, instance.primary_node, |
2895 | 3f78eef2 | Iustin Pop | instance, device, info): |
2896 | a8083063 | Iustin Pop | logger.Error("failed to create volume %s on primary!" %
|
2897 | a8083063 | Iustin Pop | device.iv_name) |
2898 | a8083063 | Iustin Pop | return False |
2899 | 1c6e3627 | Manuel Franceschini | |
2900 | a8083063 | Iustin Pop | return True |
2901 | a8083063 | Iustin Pop | |
2902 | a8083063 | Iustin Pop | |
2903 | a8083063 | Iustin Pop | def _RemoveDisks(instance, cfg): |
2904 | a8083063 | Iustin Pop | """Remove all disks for an instance.
|
2905 | a8083063 | Iustin Pop |
|
2906 | a8083063 | Iustin Pop | This abstracts away some work from `AddInstance()` and
|
2907 | a8083063 | Iustin Pop | `RemoveInstance()`. Note that in case some of the devices couldn't
|
2908 | 1d67656e | Iustin Pop | be removed, the removal will continue with the other ones (compare
|
2909 | a8083063 | Iustin Pop | with `_CreateDisks()`).
|
2910 | a8083063 | Iustin Pop |
|
2911 | a8083063 | Iustin Pop | Args:
|
2912 | a8083063 | Iustin Pop | instance: the instance object
|
2913 | a8083063 | Iustin Pop |
|
2914 | a8083063 | Iustin Pop | Returns:
|
2915 | a8083063 | Iustin Pop | True or False showing the success of the removal proces
|
2916 | a8083063 | Iustin Pop |
|
2917 | a8083063 | Iustin Pop | """
|
2918 | a8083063 | Iustin Pop | logger.Info("removing block devices for instance %s" % instance.name)
|
2919 | a8083063 | Iustin Pop | |
2920 | a8083063 | Iustin Pop | result = True
|
2921 | a8083063 | Iustin Pop | for device in instance.disks: |
2922 | a8083063 | Iustin Pop | for node, disk in device.ComputeNodeTree(instance.primary_node): |
2923 | a8083063 | Iustin Pop | cfg.SetDiskID(disk, node) |
2924 | a8083063 | Iustin Pop | if not rpc.call_blockdev_remove(node, disk): |
2925 | a8083063 | Iustin Pop | logger.Error("could not remove block device %s on node %s,"
|
2926 | a8083063 | Iustin Pop | " continuing anyway" %
|
2927 | a8083063 | Iustin Pop | (device.iv_name, node)) |
2928 | a8083063 | Iustin Pop | result = False
|
2929 | 0f1a06e3 | Manuel Franceschini | |
2930 | 0f1a06e3 | Manuel Franceschini | if instance.disk_template == constants.DT_FILE:
|
2931 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
2932 | 0f1a06e3 | Manuel Franceschini | if not rpc.call_file_storage_dir_remove(instance.primary_node, |
2933 | 0f1a06e3 | Manuel Franceschini | file_storage_dir): |
2934 | 0f1a06e3 | Manuel Franceschini | logger.Error("could not remove directory '%s'" % file_storage_dir)
|
2935 | 0f1a06e3 | Manuel Franceschini | result = False
|
2936 | 0f1a06e3 | Manuel Franceschini | |
2937 | a8083063 | Iustin Pop | return result
|
2938 | a8083063 | Iustin Pop | |
2939 | a8083063 | Iustin Pop | |
2940 | e2fe6369 | Iustin Pop | def _ComputeDiskSize(disk_template, disk_size, swap_size): |
2941 | e2fe6369 | Iustin Pop | """Compute disk size requirements in the volume group
|
2942 | e2fe6369 | Iustin Pop |
|
2943 | e2fe6369 | Iustin Pop | This is currently hard-coded for the two-drive layout.
|
2944 | e2fe6369 | Iustin Pop |
|
2945 | e2fe6369 | Iustin Pop | """
|
2946 | e2fe6369 | Iustin Pop | # Required free disk space as a function of disk and swap space
|
2947 | e2fe6369 | Iustin Pop | req_size_dict = { |
2948 | e2fe6369 | Iustin Pop | constants.DT_DISKLESS: None,
|
2949 | e2fe6369 | Iustin Pop | constants.DT_PLAIN: disk_size + swap_size, |
2950 | e2fe6369 | Iustin Pop | # 256 MB are added for drbd metadata, 128MB for each drbd device
|
2951 | e2fe6369 | Iustin Pop | constants.DT_DRBD8: disk_size + swap_size + 256,
|
2952 | e2fe6369 | Iustin Pop | constants.DT_FILE: None,
|
2953 | e2fe6369 | Iustin Pop | } |
2954 | e2fe6369 | Iustin Pop | |
2955 | e2fe6369 | Iustin Pop | if disk_template not in req_size_dict: |
2956 | e2fe6369 | Iustin Pop | raise errors.ProgrammerError("Disk template '%s' size requirement" |
2957 | e2fe6369 | Iustin Pop | " is unknown" % disk_template)
|
2958 | e2fe6369 | Iustin Pop | |
2959 | e2fe6369 | Iustin Pop | return req_size_dict[disk_template]
|
2960 | e2fe6369 | Iustin Pop | |
2961 | e2fe6369 | Iustin Pop | |
2962 | a8083063 | Iustin Pop | class LUCreateInstance(LogicalUnit): |
2963 | a8083063 | Iustin Pop | """Create an instance.
|
2964 | a8083063 | Iustin Pop |
|
2965 | a8083063 | Iustin Pop | """
|
2966 | a8083063 | Iustin Pop | HPATH = "instance-add"
|
2967 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
2968 | 538475ca | Iustin Pop | _OP_REQP = ["instance_name", "mem_size", "disk_size", |
2969 | a8083063 | Iustin Pop | "disk_template", "swap_size", "mode", "start", "vcpus", |
2970 | 1862d460 | Alexander Schreiber | "wait_for_sync", "ip_check", "mac"] |
2971 | a8083063 | Iustin Pop | |
2972 | 538475ca | Iustin Pop | def _RunAllocator(self): |
2973 | 538475ca | Iustin Pop | """Run the allocator based on input opcode.
|
2974 | 538475ca | Iustin Pop |
|
2975 | 538475ca | Iustin Pop | """
|
2976 | 538475ca | Iustin Pop | disks = [{"size": self.op.disk_size, "mode": "w"}, |
2977 | 538475ca | Iustin Pop | {"size": self.op.swap_size, "mode": "w"}] |
2978 | 538475ca | Iustin Pop | nics = [{"mac": self.op.mac, "ip": getattr(self.op, "ip", None), |
2979 | 538475ca | Iustin Pop | "bridge": self.op.bridge}] |
2980 | d1c2dd75 | Iustin Pop | ial = IAllocator(self.cfg, self.sstore, |
2981 | 29859cb7 | Iustin Pop | mode=constants.IALLOCATOR_MODE_ALLOC, |
2982 | d1c2dd75 | Iustin Pop | name=self.op.instance_name,
|
2983 | d1c2dd75 | Iustin Pop | disk_template=self.op.disk_template,
|
2984 | d1c2dd75 | Iustin Pop | tags=[], |
2985 | d1c2dd75 | Iustin Pop | os=self.op.os_type,
|
2986 | d1c2dd75 | Iustin Pop | vcpus=self.op.vcpus,
|
2987 | d1c2dd75 | Iustin Pop | mem_size=self.op.mem_size,
|
2988 | d1c2dd75 | Iustin Pop | disks=disks, |
2989 | d1c2dd75 | Iustin Pop | nics=nics, |
2990 | 29859cb7 | Iustin Pop | ) |
2991 | d1c2dd75 | Iustin Pop | |
2992 | d1c2dd75 | Iustin Pop | ial.Run(self.op.iallocator)
|
2993 | d1c2dd75 | Iustin Pop | |
2994 | d1c2dd75 | Iustin Pop | if not ial.success: |
2995 | 538475ca | Iustin Pop | raise errors.OpPrereqError("Can't compute nodes using" |
2996 | 538475ca | Iustin Pop | " iallocator '%s': %s" % (self.op.iallocator, |
2997 | d1c2dd75 | Iustin Pop | ial.info)) |
2998 | 27579978 | Iustin Pop | if len(ial.nodes) != ial.required_nodes: |
2999 | 538475ca | Iustin Pop | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
3000 | 538475ca | Iustin Pop | " of nodes (%s), required %s" %
|
3001 | 27579978 | Iustin Pop | (len(ial.nodes), ial.required_nodes))
|
3002 | d1c2dd75 | Iustin Pop | self.op.pnode = ial.nodes[0] |
3003 | 538475ca | Iustin Pop | logger.ToStdout("Selected nodes for the instance: %s" %
|
3004 | d1c2dd75 | Iustin Pop | (", ".join(ial.nodes),))
|
3005 | 538475ca | Iustin Pop | logger.Info("Selected nodes for instance %s via iallocator %s: %s" %
|
3006 | d1c2dd75 | Iustin Pop | (self.op.instance_name, self.op.iallocator, ial.nodes)) |
3007 | 27579978 | Iustin Pop | if ial.required_nodes == 2: |
3008 | d1c2dd75 | Iustin Pop | self.op.snode = ial.nodes[1] |
3009 | 538475ca | Iustin Pop | |
3010 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3011 | a8083063 | Iustin Pop | """Build hooks env.
|
3012 | a8083063 | Iustin Pop |
|
3013 | a8083063 | Iustin Pop | This runs on master, primary and secondary nodes of the instance.
|
3014 | a8083063 | Iustin Pop |
|
3015 | a8083063 | Iustin Pop | """
|
3016 | a8083063 | Iustin Pop | env = { |
3017 | 396e1b78 | Michael Hanselmann | "INSTANCE_DISK_TEMPLATE": self.op.disk_template, |
3018 | 396e1b78 | Michael Hanselmann | "INSTANCE_DISK_SIZE": self.op.disk_size, |
3019 | 396e1b78 | Michael Hanselmann | "INSTANCE_SWAP_SIZE": self.op.swap_size, |
3020 | a8083063 | Iustin Pop | "INSTANCE_ADD_MODE": self.op.mode, |
3021 | a8083063 | Iustin Pop | } |
3022 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
3023 | 396e1b78 | Michael Hanselmann | env["INSTANCE_SRC_NODE"] = self.op.src_node |
3024 | 396e1b78 | Michael Hanselmann | env["INSTANCE_SRC_PATH"] = self.op.src_path |
3025 | 396e1b78 | Michael Hanselmann | env["INSTANCE_SRC_IMAGE"] = self.src_image |
3026 | 396e1b78 | Michael Hanselmann | |
3027 | 396e1b78 | Michael Hanselmann | env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
|
3028 | 396e1b78 | Michael Hanselmann | primary_node=self.op.pnode,
|
3029 | 396e1b78 | Michael Hanselmann | secondary_nodes=self.secondaries,
|
3030 | 396e1b78 | Michael Hanselmann | status=self.instance_status,
|
3031 | ecb215b5 | Michael Hanselmann | os_type=self.op.os_type,
|
3032 | 396e1b78 | Michael Hanselmann | memory=self.op.mem_size,
|
3033 | 396e1b78 | Michael Hanselmann | vcpus=self.op.vcpus,
|
3034 | c7b27e9e | Iustin Pop | nics=[(self.inst_ip, self.op.bridge, self.op.mac)], |
3035 | 396e1b78 | Michael Hanselmann | )) |
3036 | a8083063 | Iustin Pop | |
3037 | 880478f8 | Iustin Pop | nl = ([self.sstore.GetMasterNode(), self.op.pnode] + |
3038 | a8083063 | Iustin Pop | self.secondaries)
|
3039 | a8083063 | Iustin Pop | return env, nl, nl
|
3040 | a8083063 | Iustin Pop | |
3041 | a8083063 | Iustin Pop | |
3042 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3043 | a8083063 | Iustin Pop | """Check prerequisites.
|
3044 | a8083063 | Iustin Pop |
|
3045 | a8083063 | Iustin Pop | """
|
3046 | 538475ca | Iustin Pop | # set optional parameters to none if they don't exist
|
3047 | 538475ca | Iustin Pop | for attr in ["kernel_path", "initrd_path", "hvm_boot_order", "pnode", |
3048 | 31a853d2 | Iustin Pop | "iallocator", "hvm_acpi", "hvm_pae", "hvm_cdrom_image_path", |
3049 | 31a853d2 | Iustin Pop | "vnc_bind_address"]:
|
3050 | 40ed12dd | Guido Trotter | if not hasattr(self.op, attr): |
3051 | 40ed12dd | Guido Trotter | setattr(self.op, attr, None) |
3052 | 40ed12dd | Guido Trotter | |
3053 | a8083063 | Iustin Pop | if self.op.mode not in (constants.INSTANCE_CREATE, |
3054 | a8083063 | Iustin Pop | constants.INSTANCE_IMPORT): |
3055 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid instance creation mode '%s'" % |
3056 | 3ecf6786 | Iustin Pop | self.op.mode)
|
3057 | a8083063 | Iustin Pop | |
3058 | eedc99de | Manuel Franceschini | if (not self.cfg.GetVGName() and |
3059 | eedc99de | Manuel Franceschini | self.op.disk_template not in constants.DTS_NOT_LVM): |
3060 | eedc99de | Manuel Franceschini | raise errors.OpPrereqError("Cluster does not support lvm-based" |
3061 | eedc99de | Manuel Franceschini | " instances")
|
3062 | eedc99de | Manuel Franceschini | |
3063 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_IMPORT: |
3064 | a8083063 | Iustin Pop | src_node = getattr(self.op, "src_node", None) |
3065 | a8083063 | Iustin Pop | src_path = getattr(self.op, "src_path", None) |
3066 | a8083063 | Iustin Pop | if src_node is None or src_path is None: |
3067 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Importing an instance requires source" |
3068 | 3ecf6786 | Iustin Pop | " node and path options")
|
3069 | a8083063 | Iustin Pop | src_node_full = self.cfg.ExpandNodeName(src_node)
|
3070 | a8083063 | Iustin Pop | if src_node_full is None: |
3071 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Unknown source node '%s'" % src_node) |
3072 | a8083063 | Iustin Pop | self.op.src_node = src_node = src_node_full
|
3073 | a8083063 | Iustin Pop | |
3074 | a8083063 | Iustin Pop | if not os.path.isabs(src_path): |
3075 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The source path must be absolute") |
3076 | a8083063 | Iustin Pop | |
3077 | a8083063 | Iustin Pop | export_info = rpc.call_export_info(src_node, src_path) |
3078 | a8083063 | Iustin Pop | |
3079 | a8083063 | Iustin Pop | if not export_info: |
3080 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No export found in dir %s" % src_path) |
3081 | a8083063 | Iustin Pop | |
3082 | a8083063 | Iustin Pop | if not export_info.has_section(constants.INISECT_EXP): |
3083 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Corrupted export config") |
3084 | a8083063 | Iustin Pop | |
3085 | a8083063 | Iustin Pop | ei_version = export_info.get(constants.INISECT_EXP, 'version')
|
3086 | a8083063 | Iustin Pop | if (int(ei_version) != constants.EXPORT_VERSION): |
3087 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Wrong export version %s (wanted %d)" % |
3088 | 3ecf6786 | Iustin Pop | (ei_version, constants.EXPORT_VERSION)) |
3089 | a8083063 | Iustin Pop | |
3090 | a8083063 | Iustin Pop | if int(export_info.get(constants.INISECT_INS, 'disk_count')) > 1: |
3091 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Can't import instance with more than" |
3092 | 3ecf6786 | Iustin Pop | " one data disk")
|
3093 | a8083063 | Iustin Pop | |
3094 | a8083063 | Iustin Pop | # FIXME: are the old os-es, disk sizes, etc. useful?
|
3095 | a8083063 | Iustin Pop | self.op.os_type = export_info.get(constants.INISECT_EXP, 'os') |
3096 | a8083063 | Iustin Pop | diskimage = os.path.join(src_path, export_info.get(constants.INISECT_INS, |
3097 | a8083063 | Iustin Pop | 'disk0_dump'))
|
3098 | a8083063 | Iustin Pop | self.src_image = diskimage
|
3099 | a8083063 | Iustin Pop | else: # INSTANCE_CREATE |
3100 | a8083063 | Iustin Pop | if getattr(self.op, "os_type", None) is None: |
3101 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No guest OS specified") |
3102 | a8083063 | Iustin Pop | |
3103 | 901a65c1 | Iustin Pop | #### instance parameters check
|
3104 | 901a65c1 | Iustin Pop | |
3105 | a8083063 | Iustin Pop | # disk template and mirror node verification
|
3106 | a8083063 | Iustin Pop | if self.op.disk_template not in constants.DISK_TEMPLATES: |
3107 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid disk template name") |
3108 | a8083063 | Iustin Pop | |
3109 | 901a65c1 | Iustin Pop | # instance name verification
|
3110 | 901a65c1 | Iustin Pop | hostname1 = utils.HostInfo(self.op.instance_name)
|
3111 | 901a65c1 | Iustin Pop | |
3112 | 901a65c1 | Iustin Pop | self.op.instance_name = instance_name = hostname1.name
|
3113 | 901a65c1 | Iustin Pop | instance_list = self.cfg.GetInstanceList()
|
3114 | 901a65c1 | Iustin Pop | if instance_name in instance_list: |
3115 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' is already in the cluster" % |
3116 | 901a65c1 | Iustin Pop | instance_name) |
3117 | 901a65c1 | Iustin Pop | |
3118 | 901a65c1 | Iustin Pop | # ip validity checks
|
3119 | 901a65c1 | Iustin Pop | ip = getattr(self.op, "ip", None) |
3120 | 901a65c1 | Iustin Pop | if ip is None or ip.lower() == "none": |
3121 | 901a65c1 | Iustin Pop | inst_ip = None
|
3122 | 901a65c1 | Iustin Pop | elif ip.lower() == "auto": |
3123 | 901a65c1 | Iustin Pop | inst_ip = hostname1.ip |
3124 | 901a65c1 | Iustin Pop | else:
|
3125 | 901a65c1 | Iustin Pop | if not utils.IsValidIP(ip): |
3126 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("given IP address '%s' doesn't look" |
3127 | 901a65c1 | Iustin Pop | " like a valid IP" % ip)
|
3128 | 901a65c1 | Iustin Pop | inst_ip = ip |
3129 | 901a65c1 | Iustin Pop | self.inst_ip = self.op.ip = inst_ip |
3130 | 901a65c1 | Iustin Pop | |
3131 | 901a65c1 | Iustin Pop | if self.op.start and not self.op.ip_check: |
3132 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("Cannot ignore IP address conflicts when" |
3133 | 901a65c1 | Iustin Pop | " adding an instance in start mode")
|
3134 | 901a65c1 | Iustin Pop | |
3135 | 901a65c1 | Iustin Pop | if self.op.ip_check: |
3136 | 901a65c1 | Iustin Pop | if utils.TcpPing(hostname1.ip, constants.DEFAULT_NODED_PORT):
|
3137 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("IP %s of instance %s already in use" % |
3138 | 901a65c1 | Iustin Pop | (hostname1.ip, instance_name)) |
3139 | 901a65c1 | Iustin Pop | |
3140 | 901a65c1 | Iustin Pop | # MAC address verification
|
3141 | 901a65c1 | Iustin Pop | if self.op.mac != "auto": |
3142 | 901a65c1 | Iustin Pop | if not utils.IsValidMac(self.op.mac.lower()): |
3143 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("invalid MAC address specified: %s" % |
3144 | 901a65c1 | Iustin Pop | self.op.mac)
|
3145 | 901a65c1 | Iustin Pop | |
3146 | 901a65c1 | Iustin Pop | # bridge verification
|
3147 | 901a65c1 | Iustin Pop | bridge = getattr(self.op, "bridge", None) |
3148 | 901a65c1 | Iustin Pop | if bridge is None: |
3149 | 901a65c1 | Iustin Pop | self.op.bridge = self.cfg.GetDefBridge() |
3150 | 901a65c1 | Iustin Pop | else:
|
3151 | 901a65c1 | Iustin Pop | self.op.bridge = bridge
|
3152 | 901a65c1 | Iustin Pop | |
3153 | 901a65c1 | Iustin Pop | # boot order verification
|
3154 | 901a65c1 | Iustin Pop | if self.op.hvm_boot_order is not None: |
3155 | 901a65c1 | Iustin Pop | if len(self.op.hvm_boot_order.strip("acdn")) != 0: |
3156 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("invalid boot order specified," |
3157 | 901a65c1 | Iustin Pop | " must be one or more of [acdn]")
|
3158 | 901a65c1 | Iustin Pop | # file storage checks
|
3159 | 0f1a06e3 | Manuel Franceschini | if (self.op.file_driver and |
3160 | 0f1a06e3 | Manuel Franceschini | not self.op.file_driver in constants.FILE_DRIVER): |
3161 | 0f1a06e3 | Manuel Franceschini | raise errors.OpPrereqError("Invalid file driver name '%s'" % |
3162 | 0f1a06e3 | Manuel Franceschini | self.op.file_driver)
|
3163 | 0f1a06e3 | Manuel Franceschini | |
3164 | 0f1a06e3 | Manuel Franceschini | if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir): |
3165 | b4de68a9 | Iustin Pop | raise errors.OpPrereqError("File storage directory not a relative" |
3166 | b4de68a9 | Iustin Pop | " path")
|
3167 | 538475ca | Iustin Pop | #### allocator run
|
3168 | 538475ca | Iustin Pop | |
3169 | 538475ca | Iustin Pop | if [self.op.iallocator, self.op.pnode].count(None) != 1: |
3170 | 538475ca | Iustin Pop | raise errors.OpPrereqError("One and only one of iallocator and primary" |
3171 | 538475ca | Iustin Pop | " node must be given")
|
3172 | 538475ca | Iustin Pop | |
3173 | 538475ca | Iustin Pop | if self.op.iallocator is not None: |
3174 | 538475ca | Iustin Pop | self._RunAllocator()
|
3175 | 0f1a06e3 | Manuel Franceschini | |
3176 | 901a65c1 | Iustin Pop | #### node related checks
|
3177 | 901a65c1 | Iustin Pop | |
3178 | 901a65c1 | Iustin Pop | # check primary node
|
3179 | 901a65c1 | Iustin Pop | pnode = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.pnode)) |
3180 | 901a65c1 | Iustin Pop | if pnode is None: |
3181 | 901a65c1 | Iustin Pop | raise errors.OpPrereqError("Primary node '%s' is unknown" % |
3182 | 901a65c1 | Iustin Pop | self.op.pnode)
|
3183 | 901a65c1 | Iustin Pop | self.op.pnode = pnode.name
|
3184 | 901a65c1 | Iustin Pop | self.pnode = pnode
|
3185 | 901a65c1 | Iustin Pop | self.secondaries = []
|
3186 | 901a65c1 | Iustin Pop | |
3187 | 901a65c1 | Iustin Pop | # mirror node verification
|
3188 | a1f445d3 | Iustin Pop | if self.op.disk_template in constants.DTS_NET_MIRROR: |
3189 | a8083063 | Iustin Pop | if getattr(self.op, "snode", None) is None: |
3190 | a1f445d3 | Iustin Pop | raise errors.OpPrereqError("The networked disk templates need" |
3191 | 3ecf6786 | Iustin Pop | " a mirror node")
|
3192 | a8083063 | Iustin Pop | |
3193 | a8083063 | Iustin Pop | snode_name = self.cfg.ExpandNodeName(self.op.snode) |
3194 | a8083063 | Iustin Pop | if snode_name is None: |
3195 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Unknown secondary node '%s'" % |
3196 | 3ecf6786 | Iustin Pop | self.op.snode)
|
3197 | a8083063 | Iustin Pop | elif snode_name == pnode.name:
|
3198 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The secondary node cannot be" |
3199 | 3ecf6786 | Iustin Pop | " the primary node.")
|
3200 | a8083063 | Iustin Pop | self.secondaries.append(snode_name)
|
3201 | a8083063 | Iustin Pop | |
3202 | e2fe6369 | Iustin Pop | req_size = _ComputeDiskSize(self.op.disk_template,
|
3203 | e2fe6369 | Iustin Pop | self.op.disk_size, self.op.swap_size) |
3204 | ed1ebc60 | Guido Trotter | |
3205 | 8d75db10 | Iustin Pop | # Check lv size requirements
|
3206 | 8d75db10 | Iustin Pop | if req_size is not None: |
3207 | 8d75db10 | Iustin Pop | nodenames = [pnode.name] + self.secondaries
|
3208 | 8d75db10 | Iustin Pop | nodeinfo = rpc.call_node_info(nodenames, self.cfg.GetVGName())
|
3209 | 8d75db10 | Iustin Pop | for node in nodenames: |
3210 | 8d75db10 | Iustin Pop | info = nodeinfo.get(node, None)
|
3211 | 8d75db10 | Iustin Pop | if not info: |
3212 | 8d75db10 | Iustin Pop | raise errors.OpPrereqError("Cannot get current information" |
3213 | 3e91897b | Iustin Pop | " from node '%s'" % node)
|
3214 | 8d75db10 | Iustin Pop | vg_free = info.get('vg_free', None) |
3215 | 8d75db10 | Iustin Pop | if not isinstance(vg_free, int): |
3216 | 8d75db10 | Iustin Pop | raise errors.OpPrereqError("Can't compute free disk space on" |
3217 | 8d75db10 | Iustin Pop | " node %s" % node)
|
3218 | 8d75db10 | Iustin Pop | if req_size > info['vg_free']: |
3219 | 8d75db10 | Iustin Pop | raise errors.OpPrereqError("Not enough disk space on target node %s." |
3220 | 8d75db10 | Iustin Pop | " %d MB available, %d MB required" %
|
3221 | 8d75db10 | Iustin Pop | (node, info['vg_free'], req_size))
|
3222 | ed1ebc60 | Guido Trotter | |
3223 | a8083063 | Iustin Pop | # os verification
|
3224 | 00fe9e38 | Guido Trotter | os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
|
3225 | dfa96ded | Guido Trotter | if not os_obj: |
3226 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("OS '%s' not in supported os list for" |
3227 | 3ecf6786 | Iustin Pop | " primary node" % self.op.os_type) |
3228 | a8083063 | Iustin Pop | |
3229 | 3b6d8c9b | Iustin Pop | if self.op.kernel_path == constants.VALUE_NONE: |
3230 | 3b6d8c9b | Iustin Pop | raise errors.OpPrereqError("Can't set instance kernel to none") |
3231 | 3b6d8c9b | Iustin Pop | |
3232 | a8083063 | Iustin Pop | |
3233 | 901a65c1 | Iustin Pop | # bridge check on primary node
|
3234 | a8083063 | Iustin Pop | if not rpc.call_bridges_exist(self.pnode.name, [self.op.bridge]): |
3235 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("target bridge '%s' does not exist on" |
3236 | 3ecf6786 | Iustin Pop | " destination node '%s'" %
|
3237 | 3ecf6786 | Iustin Pop | (self.op.bridge, pnode.name))
|
3238 | a8083063 | Iustin Pop | |
3239 | 49ce1563 | Iustin Pop | # memory check on primary node
|
3240 | 49ce1563 | Iustin Pop | if self.op.start: |
3241 | 49ce1563 | Iustin Pop | _CheckNodeFreeMemory(self.cfg, self.pnode.name, |
3242 | 49ce1563 | Iustin Pop | "creating instance %s" % self.op.instance_name, |
3243 | 49ce1563 | Iustin Pop | self.op.mem_size)
|
3244 | 49ce1563 | Iustin Pop | |
3245 | 31a853d2 | Iustin Pop | # hvm_cdrom_image_path verification
|
3246 | 31a853d2 | Iustin Pop | if self.op.hvm_cdrom_image_path is not None: |
3247 | 31a853d2 | Iustin Pop | if not os.path.isabs(self.op.hvm_cdrom_image_path): |
3248 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("The path to the HVM CDROM image must" |
3249 | 31a853d2 | Iustin Pop | " be an absolute path or None, not %s" %
|
3250 | 31a853d2 | Iustin Pop | self.op.hvm_cdrom_image_path)
|
3251 | 31a853d2 | Iustin Pop | if not os.path.isfile(self.op.hvm_cdrom_image_path): |
3252 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("The HVM CDROM image must either be a" |
3253 | 31a853d2 | Iustin Pop | " regular file or a symlink pointing to"
|
3254 | 31a853d2 | Iustin Pop | " an existing regular file, not %s" %
|
3255 | 31a853d2 | Iustin Pop | self.op.hvm_cdrom_image_path)
|
3256 | 31a853d2 | Iustin Pop | |
3257 | 31a853d2 | Iustin Pop | # vnc_bind_address verification
|
3258 | 31a853d2 | Iustin Pop | if self.op.vnc_bind_address is not None: |
3259 | 31a853d2 | Iustin Pop | if not utils.IsValidIP(self.op.vnc_bind_address): |
3260 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("given VNC bind address '%s' doesn't look" |
3261 | 31a853d2 | Iustin Pop | " like a valid IP address" %
|
3262 | 31a853d2 | Iustin Pop | self.op.vnc_bind_address)
|
3263 | 31a853d2 | Iustin Pop | |
3264 | a8083063 | Iustin Pop | if self.op.start: |
3265 | a8083063 | Iustin Pop | self.instance_status = 'up' |
3266 | a8083063 | Iustin Pop | else:
|
3267 | a8083063 | Iustin Pop | self.instance_status = 'down' |
3268 | a8083063 | Iustin Pop | |
3269 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3270 | a8083063 | Iustin Pop | """Create and add the instance to the cluster.
|
3271 | a8083063 | Iustin Pop |
|
3272 | a8083063 | Iustin Pop | """
|
3273 | a8083063 | Iustin Pop | instance = self.op.instance_name
|
3274 | a8083063 | Iustin Pop | pnode_name = self.pnode.name
|
3275 | a8083063 | Iustin Pop | |
3276 | 1862d460 | Alexander Schreiber | if self.op.mac == "auto": |
3277 | ba4b62cf | Iustin Pop | mac_address = self.cfg.GenerateMAC()
|
3278 | 1862d460 | Alexander Schreiber | else:
|
3279 | ba4b62cf | Iustin Pop | mac_address = self.op.mac
|
3280 | 1862d460 | Alexander Schreiber | |
3281 | 1862d460 | Alexander Schreiber | nic = objects.NIC(bridge=self.op.bridge, mac=mac_address)
|
3282 | a8083063 | Iustin Pop | if self.inst_ip is not None: |
3283 | a8083063 | Iustin Pop | nic.ip = self.inst_ip
|
3284 | a8083063 | Iustin Pop | |
3285 | 2a6469d5 | Alexander Schreiber | ht_kind = self.sstore.GetHypervisorType()
|
3286 | 2a6469d5 | Alexander Schreiber | if ht_kind in constants.HTS_REQ_PORT: |
3287 | 2a6469d5 | Alexander Schreiber | network_port = self.cfg.AllocatePort()
|
3288 | 2a6469d5 | Alexander Schreiber | else:
|
3289 | 2a6469d5 | Alexander Schreiber | network_port = None
|
3290 | 58acb49d | Alexander Schreiber | |
3291 | 31a853d2 | Iustin Pop | if self.op.vnc_bind_address is None: |
3292 | 31a853d2 | Iustin Pop | self.op.vnc_bind_address = constants.VNC_DEFAULT_BIND_ADDRESS
|
3293 | 31a853d2 | Iustin Pop | |
3294 | 2c313123 | Manuel Franceschini | # this is needed because os.path.join does not accept None arguments
|
3295 | 2c313123 | Manuel Franceschini | if self.op.file_storage_dir is None: |
3296 | 2c313123 | Manuel Franceschini | string_file_storage_dir = ""
|
3297 | 2c313123 | Manuel Franceschini | else:
|
3298 | 2c313123 | Manuel Franceschini | string_file_storage_dir = self.op.file_storage_dir
|
3299 | 2c313123 | Manuel Franceschini | |
3300 | 0f1a06e3 | Manuel Franceschini | # build the full file storage dir path
|
3301 | 0f1a06e3 | Manuel Franceschini | file_storage_dir = os.path.normpath(os.path.join( |
3302 | 0f1a06e3 | Manuel Franceschini | self.sstore.GetFileStorageDir(),
|
3303 | 2c313123 | Manuel Franceschini | string_file_storage_dir, instance)) |
3304 | 0f1a06e3 | Manuel Franceschini | |
3305 | 0f1a06e3 | Manuel Franceschini | |
3306 | 923b1523 | Iustin Pop | disks = _GenerateDiskTemplate(self.cfg,
|
3307 | a8083063 | Iustin Pop | self.op.disk_template,
|
3308 | a8083063 | Iustin Pop | instance, pnode_name, |
3309 | a8083063 | Iustin Pop | self.secondaries, self.op.disk_size, |
3310 | 0f1a06e3 | Manuel Franceschini | self.op.swap_size,
|
3311 | 0f1a06e3 | Manuel Franceschini | file_storage_dir, |
3312 | 0f1a06e3 | Manuel Franceschini | self.op.file_driver)
|
3313 | a8083063 | Iustin Pop | |
3314 | a8083063 | Iustin Pop | iobj = objects.Instance(name=instance, os=self.op.os_type,
|
3315 | a8083063 | Iustin Pop | primary_node=pnode_name, |
3316 | a8083063 | Iustin Pop | memory=self.op.mem_size,
|
3317 | a8083063 | Iustin Pop | vcpus=self.op.vcpus,
|
3318 | a8083063 | Iustin Pop | nics=[nic], disks=disks, |
3319 | a8083063 | Iustin Pop | disk_template=self.op.disk_template,
|
3320 | a8083063 | Iustin Pop | status=self.instance_status,
|
3321 | 58acb49d | Alexander Schreiber | network_port=network_port, |
3322 | 3b6d8c9b | Iustin Pop | kernel_path=self.op.kernel_path,
|
3323 | 3b6d8c9b | Iustin Pop | initrd_path=self.op.initrd_path,
|
3324 | 25c5878d | Alexander Schreiber | hvm_boot_order=self.op.hvm_boot_order,
|
3325 | 31a853d2 | Iustin Pop | hvm_acpi=self.op.hvm_acpi,
|
3326 | 31a853d2 | Iustin Pop | hvm_pae=self.op.hvm_pae,
|
3327 | 31a853d2 | Iustin Pop | hvm_cdrom_image_path=self.op.hvm_cdrom_image_path,
|
3328 | 31a853d2 | Iustin Pop | vnc_bind_address=self.op.vnc_bind_address,
|
3329 | a8083063 | Iustin Pop | ) |
3330 | a8083063 | Iustin Pop | |
3331 | a8083063 | Iustin Pop | feedback_fn("* creating instance disks...")
|
3332 | a8083063 | Iustin Pop | if not _CreateDisks(self.cfg, iobj): |
3333 | a8083063 | Iustin Pop | _RemoveDisks(iobj, self.cfg)
|
3334 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Device creation failed, reverting...") |
3335 | a8083063 | Iustin Pop | |
3336 | a8083063 | Iustin Pop | feedback_fn("adding instance %s to cluster config" % instance)
|
3337 | a8083063 | Iustin Pop | |
3338 | a8083063 | Iustin Pop | self.cfg.AddInstance(iobj)
|
3339 | a8083063 | Iustin Pop | |
3340 | a8083063 | Iustin Pop | if self.op.wait_for_sync: |
3341 | 5bfac263 | Iustin Pop | disk_abort = not _WaitForSync(self.cfg, iobj, self.proc) |
3342 | a1f445d3 | Iustin Pop | elif iobj.disk_template in constants.DTS_NET_MIRROR: |
3343 | a8083063 | Iustin Pop | # make sure the disks are not degraded (still sync-ing is ok)
|
3344 | a8083063 | Iustin Pop | time.sleep(15)
|
3345 | a8083063 | Iustin Pop | feedback_fn("* checking mirrors status")
|
3346 | 5bfac263 | Iustin Pop | disk_abort = not _WaitForSync(self.cfg, iobj, self.proc, oneshot=True) |
3347 | a8083063 | Iustin Pop | else:
|
3348 | a8083063 | Iustin Pop | disk_abort = False
|
3349 | a8083063 | Iustin Pop | |
3350 | a8083063 | Iustin Pop | if disk_abort:
|
3351 | a8083063 | Iustin Pop | _RemoveDisks(iobj, self.cfg)
|
3352 | a8083063 | Iustin Pop | self.cfg.RemoveInstance(iobj.name)
|
3353 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("There are some degraded disks for" |
3354 | 3ecf6786 | Iustin Pop | " this instance")
|
3355 | a8083063 | Iustin Pop | |
3356 | a8083063 | Iustin Pop | feedback_fn("creating os for instance %s on node %s" %
|
3357 | a8083063 | Iustin Pop | (instance, pnode_name)) |
3358 | a8083063 | Iustin Pop | |
3359 | a8083063 | Iustin Pop | if iobj.disk_template != constants.DT_DISKLESS:
|
3360 | a8083063 | Iustin Pop | if self.op.mode == constants.INSTANCE_CREATE: |
3361 | a8083063 | Iustin Pop | feedback_fn("* running the instance OS create scripts...")
|
3362 | a8083063 | Iustin Pop | if not rpc.call_instance_os_add(pnode_name, iobj, "sda", "sdb"): |
3363 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("could not add os for instance %s" |
3364 | 3ecf6786 | Iustin Pop | " on node %s" %
|
3365 | 3ecf6786 | Iustin Pop | (instance, pnode_name)) |
3366 | a8083063 | Iustin Pop | |
3367 | a8083063 | Iustin Pop | elif self.op.mode == constants.INSTANCE_IMPORT: |
3368 | a8083063 | Iustin Pop | feedback_fn("* running the instance OS import scripts...")
|
3369 | a8083063 | Iustin Pop | src_node = self.op.src_node
|
3370 | a8083063 | Iustin Pop | src_image = self.src_image
|
3371 | a8083063 | Iustin Pop | if not rpc.call_instance_os_import(pnode_name, iobj, "sda", "sdb", |
3372 | a8083063 | Iustin Pop | src_node, src_image): |
3373 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Could not import os for instance" |
3374 | 3ecf6786 | Iustin Pop | " %s on node %s" %
|
3375 | 3ecf6786 | Iustin Pop | (instance, pnode_name)) |
3376 | a8083063 | Iustin Pop | else:
|
3377 | a8083063 | Iustin Pop | # also checked in the prereq part
|
3378 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Unknown OS initialization mode '%s'" |
3379 | 3ecf6786 | Iustin Pop | % self.op.mode)
|
3380 | a8083063 | Iustin Pop | |
3381 | a8083063 | Iustin Pop | if self.op.start: |
3382 | a8083063 | Iustin Pop | logger.Info("starting instance %s on node %s" % (instance, pnode_name))
|
3383 | a8083063 | Iustin Pop | feedback_fn("* starting instance...")
|
3384 | a8083063 | Iustin Pop | if not rpc.call_instance_start(pnode_name, iobj, None): |
3385 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Could not start instance") |
3386 | a8083063 | Iustin Pop | |
3387 | a8083063 | Iustin Pop | |
3388 | a8083063 | Iustin Pop | class LUConnectConsole(NoHooksLU): |
3389 | a8083063 | Iustin Pop | """Connect to an instance's console.
|
3390 | a8083063 | Iustin Pop |
|
3391 | a8083063 | Iustin Pop | This is somewhat special in that it returns the command line that
|
3392 | a8083063 | Iustin Pop | you need to run on the master node in order to connect to the
|
3393 | a8083063 | Iustin Pop | console.
|
3394 | a8083063 | Iustin Pop |
|
3395 | a8083063 | Iustin Pop | """
|
3396 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
3397 | a8083063 | Iustin Pop | |
3398 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3399 | a8083063 | Iustin Pop | """Check prerequisites.
|
3400 | a8083063 | Iustin Pop |
|
3401 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3402 | a8083063 | Iustin Pop |
|
3403 | a8083063 | Iustin Pop | """
|
3404 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
3405 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
3406 | a8083063 | Iustin Pop | if instance is None: |
3407 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
3408 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
3409 | a8083063 | Iustin Pop | self.instance = instance
|
3410 | a8083063 | Iustin Pop | |
3411 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3412 | a8083063 | Iustin Pop | """Connect to the console of an instance
|
3413 | a8083063 | Iustin Pop |
|
3414 | a8083063 | Iustin Pop | """
|
3415 | a8083063 | Iustin Pop | instance = self.instance
|
3416 | a8083063 | Iustin Pop | node = instance.primary_node |
3417 | a8083063 | Iustin Pop | |
3418 | a8083063 | Iustin Pop | node_insts = rpc.call_instance_list([node])[node] |
3419 | a8083063 | Iustin Pop | if node_insts is False: |
3420 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Can't connect to node %s." % node) |
3421 | a8083063 | Iustin Pop | |
3422 | a8083063 | Iustin Pop | if instance.name not in node_insts: |
3423 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Instance %s is not running." % instance.name) |
3424 | a8083063 | Iustin Pop | |
3425 | a8083063 | Iustin Pop | logger.Debug("connecting to console of %s on %s" % (instance.name, node))
|
3426 | a8083063 | Iustin Pop | |
3427 | a8083063 | Iustin Pop | hyper = hypervisor.GetHypervisor() |
3428 | 30989e69 | Alexander Schreiber | console_cmd = hyper.GetShellCommandForConsole(instance) |
3429 | b047857b | Michael Hanselmann | |
3430 | 82122173 | Iustin Pop | # build ssh cmdline
|
3431 | 0a80a26f | Michael Hanselmann | return self.ssh.BuildCmd(node, "root", console_cmd, batch=True, tty=True) |
3432 | a8083063 | Iustin Pop | |
3433 | a8083063 | Iustin Pop | |
3434 | a8083063 | Iustin Pop | class LUReplaceDisks(LogicalUnit): |
3435 | a8083063 | Iustin Pop | """Replace the disks of an instance.
|
3436 | a8083063 | Iustin Pop |
|
3437 | a8083063 | Iustin Pop | """
|
3438 | a8083063 | Iustin Pop | HPATH = "mirrors-replace"
|
3439 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
3440 | a9e0c397 | Iustin Pop | _OP_REQP = ["instance_name", "mode", "disks"] |
3441 | a8083063 | Iustin Pop | |
3442 | b6e82a65 | Iustin Pop | def _RunAllocator(self): |
3443 | b6e82a65 | Iustin Pop | """Compute a new secondary node using an IAllocator.
|
3444 | b6e82a65 | Iustin Pop |
|
3445 | b6e82a65 | Iustin Pop | """
|
3446 | b6e82a65 | Iustin Pop | ial = IAllocator(self.cfg, self.sstore, |
3447 | b6e82a65 | Iustin Pop | mode=constants.IALLOCATOR_MODE_RELOC, |
3448 | b6e82a65 | Iustin Pop | name=self.op.instance_name,
|
3449 | b6e82a65 | Iustin Pop | relocate_from=[self.sec_node])
|
3450 | b6e82a65 | Iustin Pop | |
3451 | b6e82a65 | Iustin Pop | ial.Run(self.op.iallocator)
|
3452 | b6e82a65 | Iustin Pop | |
3453 | b6e82a65 | Iustin Pop | if not ial.success: |
3454 | b6e82a65 | Iustin Pop | raise errors.OpPrereqError("Can't compute nodes using" |
3455 | b6e82a65 | Iustin Pop | " iallocator '%s': %s" % (self.op.iallocator, |
3456 | b6e82a65 | Iustin Pop | ial.info)) |
3457 | b6e82a65 | Iustin Pop | if len(ial.nodes) != ial.required_nodes: |
3458 | b6e82a65 | Iustin Pop | raise errors.OpPrereqError("iallocator '%s' returned invalid number" |
3459 | b6e82a65 | Iustin Pop | " of nodes (%s), required %s" %
|
3460 | b6e82a65 | Iustin Pop | (len(ial.nodes), ial.required_nodes))
|
3461 | b6e82a65 | Iustin Pop | self.op.remote_node = ial.nodes[0] |
3462 | b6e82a65 | Iustin Pop | logger.ToStdout("Selected new secondary for the instance: %s" %
|
3463 | b6e82a65 | Iustin Pop | self.op.remote_node)
|
3464 | b6e82a65 | Iustin Pop | |
3465 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
3466 | a8083063 | Iustin Pop | """Build hooks env.
|
3467 | a8083063 | Iustin Pop |
|
3468 | a8083063 | Iustin Pop | This runs on the master, the primary and all the secondaries.
|
3469 | a8083063 | Iustin Pop |
|
3470 | a8083063 | Iustin Pop | """
|
3471 | a8083063 | Iustin Pop | env = { |
3472 | a9e0c397 | Iustin Pop | "MODE": self.op.mode, |
3473 | a8083063 | Iustin Pop | "NEW_SECONDARY": self.op.remote_node, |
3474 | a8083063 | Iustin Pop | "OLD_SECONDARY": self.instance.secondary_nodes[0], |
3475 | a8083063 | Iustin Pop | } |
3476 | 396e1b78 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self.instance))
|
3477 | 0834c866 | Iustin Pop | nl = [ |
3478 | 0834c866 | Iustin Pop | self.sstore.GetMasterNode(),
|
3479 | 0834c866 | Iustin Pop | self.instance.primary_node,
|
3480 | 0834c866 | Iustin Pop | ] |
3481 | 0834c866 | Iustin Pop | if self.op.remote_node is not None: |
3482 | 0834c866 | Iustin Pop | nl.append(self.op.remote_node)
|
3483 | a8083063 | Iustin Pop | return env, nl, nl
|
3484 | a8083063 | Iustin Pop | |
3485 | a8083063 | Iustin Pop | def CheckPrereq(self): |
3486 | a8083063 | Iustin Pop | """Check prerequisites.
|
3487 | a8083063 | Iustin Pop |
|
3488 | a8083063 | Iustin Pop | This checks that the instance is in the cluster.
|
3489 | a8083063 | Iustin Pop |
|
3490 | a8083063 | Iustin Pop | """
|
3491 | b6e82a65 | Iustin Pop | if not hasattr(self.op, "remote_node"): |
3492 | b6e82a65 | Iustin Pop | self.op.remote_node = None |
3493 | b6e82a65 | Iustin Pop | |
3494 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
3495 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
3496 | a8083063 | Iustin Pop | if instance is None: |
3497 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not known" % |
3498 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
3499 | a8083063 | Iustin Pop | self.instance = instance
|
3500 | 7df43a76 | Iustin Pop | self.op.instance_name = instance.name
|
3501 | a8083063 | Iustin Pop | |
3502 | a9e0c397 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
3503 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance's disk layout is not" |
3504 | a9e0c397 | Iustin Pop | " network mirrored.")
|
3505 | a8083063 | Iustin Pop | |
3506 | a8083063 | Iustin Pop | if len(instance.secondary_nodes) != 1: |
3507 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The instance has a strange layout," |
3508 | 3ecf6786 | Iustin Pop | " expected one secondary but found %d" %
|
3509 | 3ecf6786 | Iustin Pop | len(instance.secondary_nodes))
|
3510 | a8083063 | Iustin Pop | |
3511 | a9e0c397 | Iustin Pop | self.sec_node = instance.secondary_nodes[0] |
3512 | a9e0c397 | Iustin Pop | |
3513 | b6e82a65 | Iustin Pop | ia_name = getattr(self.op, "iallocator", None) |
3514 | b6e82a65 | Iustin Pop | if ia_name is not None: |
3515 | b6e82a65 | Iustin Pop | if self.op.remote_node is not None: |
3516 | b6e82a65 | Iustin Pop | raise errors.OpPrereqError("Give either the iallocator or the new" |
3517 | b6e82a65 | Iustin Pop | " secondary, not both")
|
3518 | b6e82a65 | Iustin Pop | self.op.remote_node = self._RunAllocator() |
3519 | b6e82a65 | Iustin Pop | |
3520 | b6e82a65 | Iustin Pop | remote_node = self.op.remote_node
|
3521 | a9e0c397 | Iustin Pop | if remote_node is not None: |
3522 | a8083063 | Iustin Pop | remote_node = self.cfg.ExpandNodeName(remote_node)
|
3523 | a8083063 | Iustin Pop | if remote_node is None: |
3524 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Node '%s' not known" % |
3525 | 3ecf6786 | Iustin Pop | self.op.remote_node)
|
3526 | a9e0c397 | Iustin Pop | self.remote_node_info = self.cfg.GetNodeInfo(remote_node) |
3527 | a9e0c397 | Iustin Pop | else:
|
3528 | a9e0c397 | Iustin Pop | self.remote_node_info = None |
3529 | a8083063 | Iustin Pop | if remote_node == instance.primary_node:
|
3530 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("The specified node is the primary node of" |
3531 | 3ecf6786 | Iustin Pop | " the instance.")
|
3532 | a9e0c397 | Iustin Pop | elif remote_node == self.sec_node: |
3533 | 0834c866 | Iustin Pop | if self.op.mode == constants.REPLACE_DISK_SEC: |
3534 | 0834c866 | Iustin Pop | # this is for DRBD8, where we can't execute the same mode of
|
3535 | 0834c866 | Iustin Pop | # replacement as for drbd7 (no different port allocated)
|
3536 | 0834c866 | Iustin Pop | raise errors.OpPrereqError("Same secondary given, cannot execute" |
3537 | 0834c866 | Iustin Pop | " replacement")
|
3538 | a9e0c397 | Iustin Pop | if instance.disk_template == constants.DT_DRBD8:
|
3539 | 7df43a76 | Iustin Pop | if (self.op.mode == constants.REPLACE_DISK_ALL and |
3540 | 7df43a76 | Iustin Pop | remote_node is not None): |
3541 | 7df43a76 | Iustin Pop | # switch to replace secondary mode
|
3542 | 7df43a76 | Iustin Pop | self.op.mode = constants.REPLACE_DISK_SEC
|
3543 | 7df43a76 | Iustin Pop | |
3544 | a9e0c397 | Iustin Pop | if self.op.mode == constants.REPLACE_DISK_ALL: |
3545 | 12c3449a | Michael Hanselmann | raise errors.OpPrereqError("Template 'drbd' only allows primary or" |
3546 | a9e0c397 | Iustin Pop | " secondary disk replacement, not"
|
3547 | a9e0c397 | Iustin Pop | " both at once")
|
3548 | a9e0c397 | Iustin Pop | elif self.op.mode == constants.REPLACE_DISK_PRI: |
3549 | a9e0c397 | Iustin Pop | if remote_node is not None: |
3550 | 12c3449a | Michael Hanselmann | raise errors.OpPrereqError("Template 'drbd' does not allow changing" |
3551 | a9e0c397 | Iustin Pop | " the secondary while doing a primary"
|
3552 | a9e0c397 | Iustin Pop | " node disk replacement")
|
3553 | a9e0c397 | Iustin Pop | self.tgt_node = instance.primary_node
|
3554 | cff90b79 | Iustin Pop | self.oth_node = instance.secondary_nodes[0] |
3555 | a9e0c397 | Iustin Pop | elif self.op.mode == constants.REPLACE_DISK_SEC: |
3556 | a9e0c397 | Iustin Pop | self.new_node = remote_node # this can be None, in which case |
3557 | a9e0c397 | Iustin Pop | # we don't change the secondary
|
3558 | a9e0c397 | Iustin Pop | self.tgt_node = instance.secondary_nodes[0] |
3559 | cff90b79 | Iustin Pop | self.oth_node = instance.primary_node
|
3560 | a9e0c397 | Iustin Pop | else:
|
3561 | a9e0c397 | Iustin Pop | raise errors.ProgrammerError("Unhandled disk replace mode") |
3562 | a9e0c397 | Iustin Pop | |
3563 | a9e0c397 | Iustin Pop | for name in self.op.disks: |
3564 | a9e0c397 | Iustin Pop | if instance.FindDisk(name) is None: |
3565 | a9e0c397 | Iustin Pop | raise errors.OpPrereqError("Disk '%s' not found for instance '%s'" % |
3566 | a9e0c397 | Iustin Pop | (name, instance.name)) |
3567 | a8083063 | Iustin Pop | self.op.remote_node = remote_node
|
3568 | a8083063 | Iustin Pop | |
3569 | a9e0c397 | Iustin Pop | def _ExecD8DiskOnly(self, feedback_fn): |
3570 | a9e0c397 | Iustin Pop | """Replace a disk on the primary or secondary for dbrd8.
|
3571 | a9e0c397 | Iustin Pop |
|
3572 | a9e0c397 | Iustin Pop | The algorithm for replace is quite complicated:
|
3573 | a9e0c397 | Iustin Pop | - for each disk to be replaced:
|
3574 | a9e0c397 | Iustin Pop | - create new LVs on the target node with unique names
|
3575 | a9e0c397 | Iustin Pop | - detach old LVs from the drbd device
|
3576 | a9e0c397 | Iustin Pop | - rename old LVs to name_replaced.<time_t>
|
3577 | a9e0c397 | Iustin Pop | - rename new LVs to old LVs
|
3578 | a9e0c397 | Iustin Pop | - attach the new LVs (with the old names now) to the drbd device
|
3579 | a9e0c397 | Iustin Pop | - wait for sync across all devices
|
3580 | a9e0c397 | Iustin Pop | - for each modified disk:
|
3581 | a9e0c397 | Iustin Pop | - remove old LVs (which have the name name_replaces.<time_t>)
|
3582 | a9e0c397 | Iustin Pop |
|
3583 | a9e0c397 | Iustin Pop | Failures are not very well handled.
|
3584 | cff90b79 | Iustin Pop |
|
3585 | a9e0c397 | Iustin Pop | """
|
3586 | cff90b79 | Iustin Pop | steps_total = 6
|
3587 | 5bfac263 | Iustin Pop | warning, info = (self.proc.LogWarning, self.proc.LogInfo) |
3588 | a9e0c397 | Iustin Pop | instance = self.instance
|
3589 | a9e0c397 | Iustin Pop | iv_names = {} |
3590 | a9e0c397 | Iustin Pop | vgname = self.cfg.GetVGName()
|
3591 | a9e0c397 | Iustin Pop | # start of work
|
3592 | a9e0c397 | Iustin Pop | cfg = self.cfg
|
3593 | a9e0c397 | Iustin Pop | tgt_node = self.tgt_node
|
3594 | cff90b79 | Iustin Pop | oth_node = self.oth_node
|
3595 | cff90b79 | Iustin Pop | |
3596 | cff90b79 | Iustin Pop | # Step: check device activation
|
3597 | 5bfac263 | Iustin Pop | self.proc.LogStep(1, steps_total, "check device existence") |
3598 | cff90b79 | Iustin Pop | info("checking volume groups")
|
3599 | cff90b79 | Iustin Pop | my_vg = cfg.GetVGName() |
3600 | cff90b79 | Iustin Pop | results = rpc.call_vg_list([oth_node, tgt_node]) |
3601 | cff90b79 | Iustin Pop | if not results: |
3602 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't list volume groups on the nodes") |
3603 | cff90b79 | Iustin Pop | for node in oth_node, tgt_node: |
3604 | cff90b79 | Iustin Pop | res = results.get(node, False)
|
3605 | cff90b79 | Iustin Pop | if not res or my_vg not in res: |
3606 | cff90b79 | Iustin Pop | raise errors.OpExecError("Volume group '%s' not found on %s" % |
3607 | cff90b79 | Iustin Pop | (my_vg, node)) |
3608 | cff90b79 | Iustin Pop | for dev in instance.disks: |
3609 | cff90b79 | Iustin Pop | if not dev.iv_name in self.op.disks: |
3610 | cff90b79 | Iustin Pop | continue
|
3611 | cff90b79 | Iustin Pop | for node in tgt_node, oth_node: |
3612 | cff90b79 | Iustin Pop | info("checking %s on %s" % (dev.iv_name, node))
|
3613 | cff90b79 | Iustin Pop | cfg.SetDiskID(dev, node) |
3614 | cff90b79 | Iustin Pop | if not rpc.call_blockdev_find(node, dev): |
3615 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't find device %s on node %s" % |
3616 | cff90b79 | Iustin Pop | (dev.iv_name, node)) |
3617 | cff90b79 | Iustin Pop | |
3618 | cff90b79 | Iustin Pop | # Step: check other node consistency
|
3619 | 5bfac263 | Iustin Pop | self.proc.LogStep(2, steps_total, "check peer consistency") |
3620 | cff90b79 | Iustin Pop | for dev in instance.disks: |
3621 | cff90b79 | Iustin Pop | if not dev.iv_name in self.op.disks: |
3622 | cff90b79 | Iustin Pop | continue
|
3623 | cff90b79 | Iustin Pop | info("checking %s consistency on %s" % (dev.iv_name, oth_node))
|
3624 | cff90b79 | Iustin Pop | if not _CheckDiskConsistency(self.cfg, dev, oth_node, |
3625 | cff90b79 | Iustin Pop | oth_node==instance.primary_node): |
3626 | cff90b79 | Iustin Pop | raise errors.OpExecError("Peer node (%s) has degraded storage, unsafe" |
3627 | cff90b79 | Iustin Pop | " to replace disks on this node (%s)" %
|
3628 | cff90b79 | Iustin Pop | (oth_node, tgt_node)) |
3629 | cff90b79 | Iustin Pop | |
3630 | cff90b79 | Iustin Pop | # Step: create new storage
|
3631 | 5bfac263 | Iustin Pop | self.proc.LogStep(3, steps_total, "allocate new storage") |
3632 | a9e0c397 | Iustin Pop | for dev in instance.disks: |
3633 | a9e0c397 | Iustin Pop | if not dev.iv_name in self.op.disks: |
3634 | a9e0c397 | Iustin Pop | continue
|
3635 | a9e0c397 | Iustin Pop | size = dev.size |
3636 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, tgt_node) |
3637 | a9e0c397 | Iustin Pop | lv_names = [".%s_%s" % (dev.iv_name, suf) for suf in ["data", "meta"]] |
3638 | a9e0c397 | Iustin Pop | names = _GenerateUniqueNames(cfg, lv_names) |
3639 | a9e0c397 | Iustin Pop | lv_data = objects.Disk(dev_type=constants.LD_LV, size=size, |
3640 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[0]))
|
3641 | a9e0c397 | Iustin Pop | lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
|
3642 | a9e0c397 | Iustin Pop | logical_id=(vgname, names[1]))
|
3643 | a9e0c397 | Iustin Pop | new_lvs = [lv_data, lv_meta] |
3644 | a9e0c397 | Iustin Pop | old_lvs = dev.children |
3645 | a9e0c397 | Iustin Pop | iv_names[dev.iv_name] = (dev, old_lvs, new_lvs) |
3646 | cff90b79 | Iustin Pop | info("creating new local storage on %s for %s" %
|
3647 | cff90b79 | Iustin Pop | (tgt_node, dev.iv_name)) |
3648 | a9e0c397 | Iustin Pop | # since we *always* want to create this LV, we use the
|
3649 | a9e0c397 | Iustin Pop | # _Create...OnPrimary (which forces the creation), even if we
|
3650 | a9e0c397 | Iustin Pop | # are talking about the secondary node
|
3651 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
3652 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnPrimary(cfg, tgt_node, instance, new_lv, |
3653 | a9e0c397 | Iustin Pop | _GetInstanceInfoText(instance)): |
3654 | a9e0c397 | Iustin Pop | raise errors.OpExecError("Failed to create new LV named '%s' on" |
3655 | a9e0c397 | Iustin Pop | " node '%s'" %
|
3656 | a9e0c397 | Iustin Pop | (new_lv.logical_id[1], tgt_node))
|
3657 | a9e0c397 | Iustin Pop | |
3658 | cff90b79 | Iustin Pop | # Step: for each lv, detach+rename*2+attach
|
3659 | 5bfac263 | Iustin Pop | self.proc.LogStep(4, steps_total, "change drbd configuration") |
3660 | cff90b79 | Iustin Pop | for dev, old_lvs, new_lvs in iv_names.itervalues(): |
3661 | cff90b79 | Iustin Pop | info("detaching %s drbd from local storage" % dev.iv_name)
|
3662 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_removechildren(tgt_node, dev, old_lvs): |
3663 | a9e0c397 | Iustin Pop | raise errors.OpExecError("Can't detach drbd from local storage on node" |
3664 | a9e0c397 | Iustin Pop | " %s for device %s" % (tgt_node, dev.iv_name))
|
3665 | cff90b79 | Iustin Pop | #dev.children = []
|
3666 | cff90b79 | Iustin Pop | #cfg.Update(instance)
|
3667 | a9e0c397 | Iustin Pop | |
3668 | a9e0c397 | Iustin Pop | # ok, we created the new LVs, so now we know we have the needed
|
3669 | a9e0c397 | Iustin Pop | # storage; as such, we proceed on the target node to rename
|
3670 | a9e0c397 | Iustin Pop | # old_lv to _old, and new_lv to old_lv; note that we rename LVs
|
3671 | c99a3cc0 | Manuel Franceschini | # using the assumption that logical_id == physical_id (which in
|
3672 | a9e0c397 | Iustin Pop | # turn is the unique_id on that node)
|
3673 | cff90b79 | Iustin Pop | |
3674 | cff90b79 | Iustin Pop | # FIXME(iustin): use a better name for the replaced LVs
|
3675 | a9e0c397 | Iustin Pop | temp_suffix = int(time.time())
|
3676 | a9e0c397 | Iustin Pop | ren_fn = lambda d, suff: (d.physical_id[0], |
3677 | a9e0c397 | Iustin Pop | d.physical_id[1] + "_replaced-%s" % suff) |
3678 | cff90b79 | Iustin Pop | # build the rename list based on what LVs exist on the node
|
3679 | cff90b79 | Iustin Pop | rlist = [] |
3680 | cff90b79 | Iustin Pop | for to_ren in old_lvs: |
3681 | cff90b79 | Iustin Pop | find_res = rpc.call_blockdev_find(tgt_node, to_ren) |
3682 | cff90b79 | Iustin Pop | if find_res is not None: # device exists |
3683 | cff90b79 | Iustin Pop | rlist.append((to_ren, ren_fn(to_ren, temp_suffix))) |
3684 | cff90b79 | Iustin Pop | |
3685 | cff90b79 | Iustin Pop | info("renaming the old LVs on the target node")
|
3686 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_rename(tgt_node, rlist): |
3687 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't rename old LVs on node %s" % tgt_node) |
3688 | a9e0c397 | Iustin Pop | # now we rename the new LVs to the old LVs
|
3689 | cff90b79 | Iustin Pop | info("renaming the new LVs on the target node")
|
3690 | a9e0c397 | Iustin Pop | rlist = [(new, old.physical_id) for old, new in zip(old_lvs, new_lvs)] |
3691 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_rename(tgt_node, rlist): |
3692 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't rename new LVs on node %s" % tgt_node) |
3693 | cff90b79 | Iustin Pop | |
3694 | cff90b79 | Iustin Pop | for old, new in zip(old_lvs, new_lvs): |
3695 | cff90b79 | Iustin Pop | new.logical_id = old.logical_id |
3696 | cff90b79 | Iustin Pop | cfg.SetDiskID(new, tgt_node) |
3697 | a9e0c397 | Iustin Pop | |
3698 | cff90b79 | Iustin Pop | for disk in old_lvs: |
3699 | cff90b79 | Iustin Pop | disk.logical_id = ren_fn(disk, temp_suffix) |
3700 | cff90b79 | Iustin Pop | cfg.SetDiskID(disk, tgt_node) |
3701 | a9e0c397 | Iustin Pop | |
3702 | a9e0c397 | Iustin Pop | # now that the new lvs have the old name, we can add them to the device
|
3703 | cff90b79 | Iustin Pop | info("adding new mirror component on %s" % tgt_node)
|
3704 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_addchildren(tgt_node, dev, new_lvs): |
3705 | a9e0c397 | Iustin Pop | for new_lv in new_lvs: |
3706 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_remove(tgt_node, new_lv): |
3707 | 79caa9ed | Guido Trotter | warning("Can't rollback device %s", hint="manually cleanup unused" |
3708 | cff90b79 | Iustin Pop | " logical volumes")
|
3709 | cff90b79 | Iustin Pop | raise errors.OpExecError("Can't add local storage to drbd") |
3710 | a9e0c397 | Iustin Pop | |
3711 | a9e0c397 | Iustin Pop | dev.children = new_lvs |
3712 | a9e0c397 | Iustin Pop | cfg.Update(instance) |
3713 | a9e0c397 | Iustin Pop | |
3714 | cff90b79 | Iustin Pop | # Step: wait for sync
|
3715 | a9e0c397 | Iustin Pop | |
3716 | a9e0c397 | Iustin Pop | # this can fail as the old devices are degraded and _WaitForSync
|
3717 | a9e0c397 | Iustin Pop | # does a combined result over all disks, so we don't check its
|
3718 | a9e0c397 | Iustin Pop | # return value
|
3719 | 5bfac263 | Iustin Pop | self.proc.LogStep(5, steps_total, "sync devices") |
3720 | 5bfac263 | Iustin Pop | _WaitForSync(cfg, instance, self.proc, unlock=True) |
3721 | a9e0c397 | Iustin Pop | |
3722 | a9e0c397 | Iustin Pop | # so check manually all the devices
|
3723 | a9e0c397 | Iustin Pop | for name, (dev, old_lvs, new_lvs) in iv_names.iteritems(): |
3724 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, instance.primary_node) |
3725 | a9e0c397 | Iustin Pop | is_degr = rpc.call_blockdev_find(instance.primary_node, dev)[5]
|
3726 | a9e0c397 | Iustin Pop | if is_degr:
|
3727 | a9e0c397 | Iustin Pop | raise errors.OpExecError("DRBD device %s is degraded!" % name) |
3728 | a9e0c397 | Iustin Pop | |
3729 | cff90b79 | Iustin Pop | # Step: remove old storage
|
3730 | 5bfac263 | Iustin Pop | self.proc.LogStep(6, steps_total, "removing old storage") |
3731 | a9e0c397 | Iustin Pop | for name, (dev, old_lvs, new_lvs) in iv_names.iteritems(): |
3732 | cff90b79 | Iustin Pop | info("remove logical volumes for %s" % name)
|
3733 | a9e0c397 | Iustin Pop | for lv in old_lvs: |
3734 | a9e0c397 | Iustin Pop | cfg.SetDiskID(lv, tgt_node) |
3735 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_remove(tgt_node, lv): |
3736 | 79caa9ed | Guido Trotter | warning("Can't remove old LV", hint="manually remove unused LVs") |
3737 | a9e0c397 | Iustin Pop | continue
|
3738 | a9e0c397 | Iustin Pop | |
3739 | a9e0c397 | Iustin Pop | def _ExecD8Secondary(self, feedback_fn): |
3740 | a9e0c397 | Iustin Pop | """Replace the secondary node for drbd8.
|
3741 | a9e0c397 | Iustin Pop |
|
3742 | a9e0c397 | Iustin Pop | The algorithm for replace is quite complicated:
|
3743 | a9e0c397 | Iustin Pop | - for all disks of the instance:
|
3744 | a9e0c397 | Iustin Pop | - create new LVs on the new node with same names
|
3745 | a9e0c397 | Iustin Pop | - shutdown the drbd device on the old secondary
|
3746 | a9e0c397 | Iustin Pop | - disconnect the drbd network on the primary
|
3747 | a9e0c397 | Iustin Pop | - create the drbd device on the new secondary
|
3748 | a9e0c397 | Iustin Pop | - network attach the drbd on the primary, using an artifice:
|
3749 | a9e0c397 | Iustin Pop | the drbd code for Attach() will connect to the network if it
|
3750 | a9e0c397 | Iustin Pop | finds a device which is connected to the good local disks but
|
3751 | a9e0c397 | Iustin Pop | not network enabled
|
3752 | a9e0c397 | Iustin Pop | - wait for sync across all devices
|
3753 | a9e0c397 | Iustin Pop | - remove all disks from the old secondary
|
3754 | a9e0c397 | Iustin Pop |
|
3755 | a9e0c397 | Iustin Pop | Failures are not very well handled.
|
3756 | 0834c866 | Iustin Pop |
|
3757 | a9e0c397 | Iustin Pop | """
|
3758 | 0834c866 | Iustin Pop | steps_total = 6
|
3759 | 5bfac263 | Iustin Pop | warning, info = (self.proc.LogWarning, self.proc.LogInfo) |
3760 | a9e0c397 | Iustin Pop | instance = self.instance
|
3761 | a9e0c397 | Iustin Pop | iv_names = {} |
3762 | a9e0c397 | Iustin Pop | vgname = self.cfg.GetVGName()
|
3763 | a9e0c397 | Iustin Pop | # start of work
|
3764 | a9e0c397 | Iustin Pop | cfg = self.cfg
|
3765 | a9e0c397 | Iustin Pop | old_node = self.tgt_node
|
3766 | a9e0c397 | Iustin Pop | new_node = self.new_node
|
3767 | a9e0c397 | Iustin Pop | pri_node = instance.primary_node |
3768 | 0834c866 | Iustin Pop | |
3769 | 0834c866 | Iustin Pop | # Step: check device activation
|
3770 | 5bfac263 | Iustin Pop | self.proc.LogStep(1, steps_total, "check device existence") |
3771 | 0834c866 | Iustin Pop | info("checking volume groups")
|
3772 | 0834c866 | Iustin Pop | my_vg = cfg.GetVGName() |
3773 | 0834c866 | Iustin Pop | results = rpc.call_vg_list([pri_node, new_node]) |
3774 | 0834c866 | Iustin Pop | if not results: |
3775 | 0834c866 | Iustin Pop | raise errors.OpExecError("Can't list volume groups on the nodes") |
3776 | 0834c866 | Iustin Pop | for node in pri_node, new_node: |
3777 | 0834c866 | Iustin Pop | res = results.get(node, False)
|
3778 | 0834c866 | Iustin Pop | if not res or my_vg not in res: |
3779 | 0834c866 | Iustin Pop | raise errors.OpExecError("Volume group '%s' not found on %s" % |
3780 | 0834c866 | Iustin Pop | (my_vg, node)) |
3781 | 0834c866 | Iustin Pop | for dev in instance.disks: |
3782 | 0834c866 | Iustin Pop | if not dev.iv_name in self.op.disks: |
3783 | 0834c866 | Iustin Pop | continue
|
3784 | 0834c866 | Iustin Pop | info("checking %s on %s" % (dev.iv_name, pri_node))
|
3785 | 0834c866 | Iustin Pop | cfg.SetDiskID(dev, pri_node) |
3786 | 0834c866 | Iustin Pop | if not rpc.call_blockdev_find(pri_node, dev): |
3787 | 0834c866 | Iustin Pop | raise errors.OpExecError("Can't find device %s on node %s" % |
3788 | 0834c866 | Iustin Pop | (dev.iv_name, pri_node)) |
3789 | 0834c866 | Iustin Pop | |
3790 | 0834c866 | Iustin Pop | # Step: check other node consistency
|
3791 | 5bfac263 | Iustin Pop | self.proc.LogStep(2, steps_total, "check peer consistency") |
3792 | 0834c866 | Iustin Pop | for dev in instance.disks: |
3793 | 0834c866 | Iustin Pop | if not dev.iv_name in self.op.disks: |
3794 | 0834c866 | Iustin Pop | continue
|
3795 | 0834c866 | Iustin Pop | info("checking %s consistency on %s" % (dev.iv_name, pri_node))
|
3796 | 0834c866 | Iustin Pop | if not _CheckDiskConsistency(self.cfg, dev, pri_node, True, ldisk=True): |
3797 | 0834c866 | Iustin Pop | raise errors.OpExecError("Primary node (%s) has degraded storage," |
3798 | 0834c866 | Iustin Pop | " unsafe to replace the secondary" %
|
3799 | 0834c866 | Iustin Pop | pri_node) |
3800 | 0834c866 | Iustin Pop | |
3801 | 0834c866 | Iustin Pop | # Step: create new storage
|
3802 | 5bfac263 | Iustin Pop | self.proc.LogStep(3, steps_total, "allocate new storage") |
3803 | a9e0c397 | Iustin Pop | for dev in instance.disks: |
3804 | a9e0c397 | Iustin Pop | size = dev.size |
3805 | 0834c866 | Iustin Pop | info("adding new local storage on %s for %s" % (new_node, dev.iv_name))
|
3806 | a9e0c397 | Iustin Pop | # since we *always* want to create this LV, we use the
|
3807 | a9e0c397 | Iustin Pop | # _Create...OnPrimary (which forces the creation), even if we
|
3808 | a9e0c397 | Iustin Pop | # are talking about the secondary node
|
3809 | a9e0c397 | Iustin Pop | for new_lv in dev.children: |
3810 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnPrimary(cfg, new_node, instance, new_lv, |
3811 | a9e0c397 | Iustin Pop | _GetInstanceInfoText(instance)): |
3812 | a9e0c397 | Iustin Pop | raise errors.OpExecError("Failed to create new LV named '%s' on" |
3813 | a9e0c397 | Iustin Pop | " node '%s'" %
|
3814 | a9e0c397 | Iustin Pop | (new_lv.logical_id[1], new_node))
|
3815 | a9e0c397 | Iustin Pop | |
3816 | 0834c866 | Iustin Pop | iv_names[dev.iv_name] = (dev, dev.children) |
3817 | 0834c866 | Iustin Pop | |
3818 | 5bfac263 | Iustin Pop | self.proc.LogStep(4, steps_total, "changing drbd configuration") |
3819 | 0834c866 | Iustin Pop | for dev in instance.disks: |
3820 | 0834c866 | Iustin Pop | size = dev.size |
3821 | 0834c866 | Iustin Pop | info("activating a new drbd on %s for %s" % (new_node, dev.iv_name))
|
3822 | a9e0c397 | Iustin Pop | # create new devices on new_node
|
3823 | a9e0c397 | Iustin Pop | new_drbd = objects.Disk(dev_type=constants.LD_DRBD8, |
3824 | a9e0c397 | Iustin Pop | logical_id=(pri_node, new_node, |
3825 | a9e0c397 | Iustin Pop | dev.logical_id[2]),
|
3826 | a9e0c397 | Iustin Pop | children=dev.children) |
3827 | 3f78eef2 | Iustin Pop | if not _CreateBlockDevOnSecondary(cfg, new_node, instance, |
3828 | 3f78eef2 | Iustin Pop | new_drbd, False,
|
3829 | a9e0c397 | Iustin Pop | _GetInstanceInfoText(instance)): |
3830 | a9e0c397 | Iustin Pop | raise errors.OpExecError("Failed to create new DRBD on" |
3831 | a9e0c397 | Iustin Pop | " node '%s'" % new_node)
|
3832 | a9e0c397 | Iustin Pop | |
3833 | 0834c866 | Iustin Pop | for dev in instance.disks: |
3834 | a9e0c397 | Iustin Pop | # we have new devices, shutdown the drbd on the old secondary
|
3835 | 0834c866 | Iustin Pop | info("shutting down drbd for %s on old node" % dev.iv_name)
|
3836 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, old_node) |
3837 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_shutdown(old_node, dev): |
3838 | 0834c866 | Iustin Pop | warning("Failed to shutdown drbd for %s on old node" % dev.iv_name,
|
3839 | 79caa9ed | Guido Trotter | hint="Please cleanup this device manually as soon as possible")
|
3840 | a9e0c397 | Iustin Pop | |
3841 | 642445d9 | Iustin Pop | info("detaching primary drbds from the network (=> standalone)")
|
3842 | 642445d9 | Iustin Pop | done = 0
|
3843 | 642445d9 | Iustin Pop | for dev in instance.disks: |
3844 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, pri_node) |
3845 | 642445d9 | Iustin Pop | # set the physical (unique in bdev terms) id to None, meaning
|
3846 | 642445d9 | Iustin Pop | # detach from network
|
3847 | 642445d9 | Iustin Pop | dev.physical_id = (None,) * len(dev.physical_id) |
3848 | 642445d9 | Iustin Pop | # and 'find' the device, which will 'fix' it to match the
|
3849 | 642445d9 | Iustin Pop | # standalone state
|
3850 | 642445d9 | Iustin Pop | if rpc.call_blockdev_find(pri_node, dev):
|
3851 | 642445d9 | Iustin Pop | done += 1
|
3852 | 642445d9 | Iustin Pop | else:
|
3853 | 642445d9 | Iustin Pop | warning("Failed to detach drbd %s from network, unusual case" %
|
3854 | 642445d9 | Iustin Pop | dev.iv_name) |
3855 | 642445d9 | Iustin Pop | |
3856 | 642445d9 | Iustin Pop | if not done: |
3857 | 642445d9 | Iustin Pop | # no detaches succeeded (very unlikely)
|
3858 | 642445d9 | Iustin Pop | raise errors.OpExecError("Can't detach at least one DRBD from old node") |
3859 | 642445d9 | Iustin Pop | |
3860 | 642445d9 | Iustin Pop | # if we managed to detach at least one, we update all the disks of
|
3861 | 642445d9 | Iustin Pop | # the instance to point to the new secondary
|
3862 | 642445d9 | Iustin Pop | info("updating instance configuration")
|
3863 | 642445d9 | Iustin Pop | for dev in instance.disks: |
3864 | 642445d9 | Iustin Pop | dev.logical_id = (pri_node, new_node) + dev.logical_id[2:]
|
3865 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, pri_node) |
3866 | 642445d9 | Iustin Pop | cfg.Update(instance) |
3867 | a9e0c397 | Iustin Pop | |
3868 | 642445d9 | Iustin Pop | # and now perform the drbd attach
|
3869 | 642445d9 | Iustin Pop | info("attaching primary drbds to new secondary (standalone => connected)")
|
3870 | 642445d9 | Iustin Pop | failures = [] |
3871 | 642445d9 | Iustin Pop | for dev in instance.disks: |
3872 | 642445d9 | Iustin Pop | info("attaching primary drbd for %s to new secondary node" % dev.iv_name)
|
3873 | 642445d9 | Iustin Pop | # since the attach is smart, it's enough to 'find' the device,
|
3874 | 642445d9 | Iustin Pop | # it will automatically activate the network, if the physical_id
|
3875 | 642445d9 | Iustin Pop | # is correct
|
3876 | 642445d9 | Iustin Pop | cfg.SetDiskID(dev, pri_node) |
3877 | 642445d9 | Iustin Pop | if not rpc.call_blockdev_find(pri_node, dev): |
3878 | 642445d9 | Iustin Pop | warning("can't attach drbd %s to new secondary!" % dev.iv_name,
|
3879 | 642445d9 | Iustin Pop | "please do a gnt-instance info to see the status of disks")
|
3880 | a9e0c397 | Iustin Pop | |
3881 | a9e0c397 | Iustin Pop | # this can fail as the old devices are degraded and _WaitForSync
|
3882 | a9e0c397 | Iustin Pop | # does a combined result over all disks, so we don't check its
|
3883 | a9e0c397 | Iustin Pop | # return value
|
3884 | 5bfac263 | Iustin Pop | self.proc.LogStep(5, steps_total, "sync devices") |
3885 | 5bfac263 | Iustin Pop | _WaitForSync(cfg, instance, self.proc, unlock=True) |
3886 | a9e0c397 | Iustin Pop | |
3887 | a9e0c397 | Iustin Pop | # so check manually all the devices
|
3888 | a9e0c397 | Iustin Pop | for name, (dev, old_lvs) in iv_names.iteritems(): |
3889 | a9e0c397 | Iustin Pop | cfg.SetDiskID(dev, pri_node) |
3890 | a9e0c397 | Iustin Pop | is_degr = rpc.call_blockdev_find(pri_node, dev)[5]
|
3891 | a9e0c397 | Iustin Pop | if is_degr:
|
3892 | a9e0c397 | Iustin Pop | raise errors.OpExecError("DRBD device %s is degraded!" % name) |
3893 | a9e0c397 | Iustin Pop | |
3894 | 5bfac263 | Iustin Pop | self.proc.LogStep(6, steps_total, "removing old storage") |
3895 | a9e0c397 | Iustin Pop | for name, (dev, old_lvs) in iv_names.iteritems(): |
3896 | 0834c866 | Iustin Pop | info("remove logical volumes for %s" % name)
|
3897 | a9e0c397 | Iustin Pop | for lv in old_lvs: |
3898 | a9e0c397 | Iustin Pop | cfg.SetDiskID(lv, old_node) |
3899 | a9e0c397 | Iustin Pop | if not rpc.call_blockdev_remove(old_node, lv): |
3900 | 0834c866 | Iustin Pop | warning("Can't remove LV on old secondary",
|
3901 | 79caa9ed | Guido Trotter | hint="Cleanup stale volumes by hand")
|
3902 | a9e0c397 | Iustin Pop | |
3903 | a9e0c397 | Iustin Pop | def Exec(self, feedback_fn): |
3904 | a9e0c397 | Iustin Pop | """Execute disk replacement.
|
3905 | a9e0c397 | Iustin Pop |
|
3906 | a9e0c397 | Iustin Pop | This dispatches the disk replacement to the appropriate handler.
|
3907 | a9e0c397 | Iustin Pop |
|
3908 | a9e0c397 | Iustin Pop | """
|
3909 | a9e0c397 | Iustin Pop | instance = self.instance
|
3910 | abdf0113 | Iustin Pop | if instance.disk_template == constants.DT_DRBD8:
|
3911 | a9e0c397 | Iustin Pop | if self.op.remote_node is None: |
3912 | a9e0c397 | Iustin Pop | fn = self._ExecD8DiskOnly
|
3913 | a9e0c397 | Iustin Pop | else:
|
3914 | a9e0c397 | Iustin Pop | fn = self._ExecD8Secondary
|
3915 | a9e0c397 | Iustin Pop | else:
|
3916 | a9e0c397 | Iustin Pop | raise errors.ProgrammerError("Unhandled disk replacement case") |
3917 | a9e0c397 | Iustin Pop | return fn(feedback_fn)
|
3918 | a9e0c397 | Iustin Pop | |
3919 | a8083063 | Iustin Pop | |
3920 | a8083063 | Iustin Pop | class LUQueryInstanceData(NoHooksLU): |
3921 | a8083063 | Iustin Pop | """Query runtime instance data.
|
3922 | a8083063 | Iustin Pop |
|
3923 | a8083063 | Iustin Pop | """
|
3924 | a8083063 | Iustin Pop | _OP_REQP = ["instances"]
|
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 only checks the optional instance list against the existing names.
|
3930 | a8083063 | Iustin Pop |
|
3931 | a8083063 | Iustin Pop | """
|
3932 | a8083063 | Iustin Pop | if not isinstance(self.op.instances, list): |
3933 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid argument type 'instances'") |
3934 | a8083063 | Iustin Pop | if self.op.instances: |
3935 | a8083063 | Iustin Pop | self.wanted_instances = []
|
3936 | a8083063 | Iustin Pop | names = self.op.instances
|
3937 | a8083063 | Iustin Pop | for name in names: |
3938 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(self.cfg.ExpandInstanceName(name)) |
3939 | a8083063 | Iustin Pop | if instance is None: |
3940 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No such instance name '%s'" % name) |
3941 | 515207af | Guido Trotter | self.wanted_instances.append(instance)
|
3942 | a8083063 | Iustin Pop | else:
|
3943 | a8083063 | Iustin Pop | self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name |
3944 | a8083063 | Iustin Pop | in self.cfg.GetInstanceList()] |
3945 | a8083063 | Iustin Pop | return
|
3946 | a8083063 | Iustin Pop | |
3947 | a8083063 | Iustin Pop | |
3948 | a8083063 | Iustin Pop | def _ComputeDiskStatus(self, instance, snode, dev): |
3949 | a8083063 | Iustin Pop | """Compute block device status.
|
3950 | a8083063 | Iustin Pop |
|
3951 | a8083063 | Iustin Pop | """
|
3952 | a8083063 | Iustin Pop | self.cfg.SetDiskID(dev, instance.primary_node)
|
3953 | a8083063 | Iustin Pop | dev_pstatus = rpc.call_blockdev_find(instance.primary_node, dev) |
3954 | a1f445d3 | Iustin Pop | if dev.dev_type in constants.LDS_DRBD: |
3955 | a8083063 | Iustin Pop | # we change the snode then (otherwise we use the one passed in)
|
3956 | a8083063 | Iustin Pop | if dev.logical_id[0] == instance.primary_node: |
3957 | a8083063 | Iustin Pop | snode = dev.logical_id[1]
|
3958 | a8083063 | Iustin Pop | else:
|
3959 | a8083063 | Iustin Pop | snode = dev.logical_id[0]
|
3960 | a8083063 | Iustin Pop | |
3961 | a8083063 | Iustin Pop | if snode:
|
3962 | a8083063 | Iustin Pop | self.cfg.SetDiskID(dev, snode)
|
3963 | a8083063 | Iustin Pop | dev_sstatus = rpc.call_blockdev_find(snode, dev) |
3964 | a8083063 | Iustin Pop | else:
|
3965 | a8083063 | Iustin Pop | dev_sstatus = None
|
3966 | a8083063 | Iustin Pop | |
3967 | a8083063 | Iustin Pop | if dev.children:
|
3968 | a8083063 | Iustin Pop | dev_children = [self._ComputeDiskStatus(instance, snode, child)
|
3969 | a8083063 | Iustin Pop | for child in dev.children] |
3970 | a8083063 | Iustin Pop | else:
|
3971 | a8083063 | Iustin Pop | dev_children = [] |
3972 | a8083063 | Iustin Pop | |
3973 | a8083063 | Iustin Pop | data = { |
3974 | a8083063 | Iustin Pop | "iv_name": dev.iv_name,
|
3975 | a8083063 | Iustin Pop | "dev_type": dev.dev_type,
|
3976 | a8083063 | Iustin Pop | "logical_id": dev.logical_id,
|
3977 | a8083063 | Iustin Pop | "physical_id": dev.physical_id,
|
3978 | a8083063 | Iustin Pop | "pstatus": dev_pstatus,
|
3979 | a8083063 | Iustin Pop | "sstatus": dev_sstatus,
|
3980 | a8083063 | Iustin Pop | "children": dev_children,
|
3981 | a8083063 | Iustin Pop | } |
3982 | a8083063 | Iustin Pop | |
3983 | a8083063 | Iustin Pop | return data
|
3984 | a8083063 | Iustin Pop | |
3985 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
3986 | a8083063 | Iustin Pop | """Gather and return data"""
|
3987 | a8083063 | Iustin Pop | result = {} |
3988 | a8083063 | Iustin Pop | for instance in self.wanted_instances: |
3989 | a8083063 | Iustin Pop | remote_info = rpc.call_instance_info(instance.primary_node, |
3990 | a8083063 | Iustin Pop | instance.name) |
3991 | a8083063 | Iustin Pop | if remote_info and "state" in remote_info: |
3992 | a8083063 | Iustin Pop | remote_state = "up"
|
3993 | a8083063 | Iustin Pop | else:
|
3994 | a8083063 | Iustin Pop | remote_state = "down"
|
3995 | a8083063 | Iustin Pop | if instance.status == "down": |
3996 | a8083063 | Iustin Pop | config_state = "down"
|
3997 | a8083063 | Iustin Pop | else:
|
3998 | a8083063 | Iustin Pop | config_state = "up"
|
3999 | a8083063 | Iustin Pop | |
4000 | a8083063 | Iustin Pop | disks = [self._ComputeDiskStatus(instance, None, device) |
4001 | a8083063 | Iustin Pop | for device in instance.disks] |
4002 | a8083063 | Iustin Pop | |
4003 | a8083063 | Iustin Pop | idict = { |
4004 | a8083063 | Iustin Pop | "name": instance.name,
|
4005 | a8083063 | Iustin Pop | "config_state": config_state,
|
4006 | a8083063 | Iustin Pop | "run_state": remote_state,
|
4007 | a8083063 | Iustin Pop | "pnode": instance.primary_node,
|
4008 | a8083063 | Iustin Pop | "snodes": instance.secondary_nodes,
|
4009 | a8083063 | Iustin Pop | "os": instance.os,
|
4010 | a8083063 | Iustin Pop | "memory": instance.memory,
|
4011 | a8083063 | Iustin Pop | "nics": [(nic.mac, nic.ip, nic.bridge) for nic in instance.nics], |
4012 | a8083063 | Iustin Pop | "disks": disks,
|
4013 | f55ff7ec | Iustin Pop | "vcpus": instance.vcpus,
|
4014 | a8083063 | Iustin Pop | } |
4015 | a8083063 | Iustin Pop | |
4016 | a8340917 | Iustin Pop | htkind = self.sstore.GetHypervisorType()
|
4017 | a8340917 | Iustin Pop | if htkind == constants.HT_XEN_PVM30:
|
4018 | a8340917 | Iustin Pop | idict["kernel_path"] = instance.kernel_path
|
4019 | a8340917 | Iustin Pop | idict["initrd_path"] = instance.initrd_path
|
4020 | a8340917 | Iustin Pop | |
4021 | a8340917 | Iustin Pop | if htkind == constants.HT_XEN_HVM31:
|
4022 | a8340917 | Iustin Pop | idict["hvm_boot_order"] = instance.hvm_boot_order
|
4023 | a8340917 | Iustin Pop | idict["hvm_acpi"] = instance.hvm_acpi
|
4024 | a8340917 | Iustin Pop | idict["hvm_pae"] = instance.hvm_pae
|
4025 | a8340917 | Iustin Pop | idict["hvm_cdrom_image_path"] = instance.hvm_cdrom_image_path
|
4026 | a8340917 | Iustin Pop | |
4027 | a8340917 | Iustin Pop | if htkind in constants.HTS_REQ_PORT: |
4028 | a8340917 | Iustin Pop | idict["vnc_bind_address"] = instance.vnc_bind_address
|
4029 | a8340917 | Iustin Pop | idict["network_port"] = instance.network_port
|
4030 | a8340917 | Iustin Pop | |
4031 | a8083063 | Iustin Pop | result[instance.name] = idict |
4032 | a8083063 | Iustin Pop | |
4033 | a8083063 | Iustin Pop | return result
|
4034 | a8083063 | Iustin Pop | |
4035 | a8083063 | Iustin Pop | |
4036 | 7767bbf5 | Manuel Franceschini | class LUSetInstanceParams(LogicalUnit): |
4037 | a8083063 | Iustin Pop | """Modifies an instances's parameters.
|
4038 | a8083063 | Iustin Pop |
|
4039 | a8083063 | Iustin Pop | """
|
4040 | a8083063 | Iustin Pop | HPATH = "instance-modify"
|
4041 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4042 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name"]
|
4043 | a8083063 | Iustin Pop | |
4044 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4045 | a8083063 | Iustin Pop | """Build hooks env.
|
4046 | a8083063 | Iustin Pop |
|
4047 | a8083063 | Iustin Pop | This runs on the master, primary and secondaries.
|
4048 | a8083063 | Iustin Pop |
|
4049 | a8083063 | Iustin Pop | """
|
4050 | 396e1b78 | Michael Hanselmann | args = dict()
|
4051 | a8083063 | Iustin Pop | if self.mem: |
4052 | 396e1b78 | Michael Hanselmann | args['memory'] = self.mem |
4053 | a8083063 | Iustin Pop | if self.vcpus: |
4054 | 396e1b78 | Michael Hanselmann | args['vcpus'] = self.vcpus |
4055 | ef756965 | Iustin Pop | if self.do_ip or self.do_bridge or self.mac: |
4056 | 396e1b78 | Michael Hanselmann | if self.do_ip: |
4057 | 396e1b78 | Michael Hanselmann | ip = self.ip
|
4058 | 396e1b78 | Michael Hanselmann | else:
|
4059 | 396e1b78 | Michael Hanselmann | ip = self.instance.nics[0].ip |
4060 | 396e1b78 | Michael Hanselmann | if self.bridge: |
4061 | 396e1b78 | Michael Hanselmann | bridge = self.bridge
|
4062 | 396e1b78 | Michael Hanselmann | else:
|
4063 | 396e1b78 | Michael Hanselmann | bridge = self.instance.nics[0].bridge |
4064 | ef756965 | Iustin Pop | if self.mac: |
4065 | ef756965 | Iustin Pop | mac = self.mac
|
4066 | ef756965 | Iustin Pop | else:
|
4067 | ef756965 | Iustin Pop | mac = self.instance.nics[0].mac |
4068 | ef756965 | Iustin Pop | args['nics'] = [(ip, bridge, mac)]
|
4069 | 396e1b78 | Michael Hanselmann | env = _BuildInstanceHookEnvByObject(self.instance, override=args)
|
4070 | 880478f8 | Iustin Pop | nl = [self.sstore.GetMasterNode(),
|
4071 | a8083063 | Iustin Pop | self.instance.primary_node] + list(self.instance.secondary_nodes) |
4072 | a8083063 | Iustin Pop | return env, nl, nl
|
4073 | a8083063 | Iustin Pop | |
4074 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4075 | a8083063 | Iustin Pop | """Check prerequisites.
|
4076 | a8083063 | Iustin Pop |
|
4077 | a8083063 | Iustin Pop | This only checks the instance list against the existing names.
|
4078 | a8083063 | Iustin Pop |
|
4079 | a8083063 | Iustin Pop | """
|
4080 | a8083063 | Iustin Pop | self.mem = getattr(self.op, "mem", None) |
4081 | a8083063 | Iustin Pop | self.vcpus = getattr(self.op, "vcpus", None) |
4082 | a8083063 | Iustin Pop | self.ip = getattr(self.op, "ip", None) |
4083 | 1862d460 | Alexander Schreiber | self.mac = getattr(self.op, "mac", None) |
4084 | a8083063 | Iustin Pop | self.bridge = getattr(self.op, "bridge", None) |
4085 | 973d7867 | Iustin Pop | self.kernel_path = getattr(self.op, "kernel_path", None) |
4086 | 973d7867 | Iustin Pop | self.initrd_path = getattr(self.op, "initrd_path", None) |
4087 | 25c5878d | Alexander Schreiber | self.hvm_boot_order = getattr(self.op, "hvm_boot_order", None) |
4088 | 31a853d2 | Iustin Pop | self.hvm_acpi = getattr(self.op, "hvm_acpi", None) |
4089 | 31a853d2 | Iustin Pop | self.hvm_pae = getattr(self.op, "hvm_pae", None) |
4090 | 31a853d2 | Iustin Pop | self.hvm_cdrom_image_path = getattr(self.op, "hvm_cdrom_image_path", None) |
4091 | 31a853d2 | Iustin Pop | self.vnc_bind_address = getattr(self.op, "vnc_bind_address", None) |
4092 | 31a853d2 | Iustin Pop | all_parms = [self.mem, self.vcpus, self.ip, self.bridge, self.mac, |
4093 | 31a853d2 | Iustin Pop | self.kernel_path, self.initrd_path, self.hvm_boot_order, |
4094 | 31a853d2 | Iustin Pop | self.hvm_acpi, self.hvm_pae, self.hvm_cdrom_image_path, |
4095 | 31a853d2 | Iustin Pop | self.vnc_bind_address]
|
4096 | 31a853d2 | Iustin Pop | if all_parms.count(None) == len(all_parms): |
4097 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No changes submitted") |
4098 | a8083063 | Iustin Pop | if self.mem is not None: |
4099 | a8083063 | Iustin Pop | try:
|
4100 | a8083063 | Iustin Pop | self.mem = int(self.mem) |
4101 | a8083063 | Iustin Pop | except ValueError, err: |
4102 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid memory size: %s" % str(err)) |
4103 | a8083063 | Iustin Pop | if self.vcpus is not None: |
4104 | a8083063 | Iustin Pop | try:
|
4105 | a8083063 | Iustin Pop | self.vcpus = int(self.vcpus) |
4106 | a8083063 | Iustin Pop | except ValueError, err: |
4107 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid vcpus number: %s" % str(err)) |
4108 | a8083063 | Iustin Pop | if self.ip is not None: |
4109 | a8083063 | Iustin Pop | self.do_ip = True |
4110 | a8083063 | Iustin Pop | if self.ip.lower() == "none": |
4111 | a8083063 | Iustin Pop | self.ip = None |
4112 | a8083063 | Iustin Pop | else:
|
4113 | a8083063 | Iustin Pop | if not utils.IsValidIP(self.ip): |
4114 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid IP address '%s'." % self.ip) |
4115 | a8083063 | Iustin Pop | else:
|
4116 | a8083063 | Iustin Pop | self.do_ip = False |
4117 | ecb215b5 | Michael Hanselmann | self.do_bridge = (self.bridge is not None) |
4118 | 1862d460 | Alexander Schreiber | if self.mac is not None: |
4119 | 1862d460 | Alexander Schreiber | if self.cfg.IsMacInUse(self.mac): |
4120 | 1862d460 | Alexander Schreiber | raise errors.OpPrereqError('MAC address %s already in use in cluster' % |
4121 | 1862d460 | Alexander Schreiber | self.mac)
|
4122 | 1862d460 | Alexander Schreiber | if not utils.IsValidMac(self.mac): |
4123 | 1862d460 | Alexander Schreiber | raise errors.OpPrereqError('Invalid MAC address %s' % self.mac) |
4124 | a8083063 | Iustin Pop | |
4125 | 973d7867 | Iustin Pop | if self.kernel_path is not None: |
4126 | 973d7867 | Iustin Pop | self.do_kernel_path = True |
4127 | 973d7867 | Iustin Pop | if self.kernel_path == constants.VALUE_NONE: |
4128 | 973d7867 | Iustin Pop | raise errors.OpPrereqError("Can't set instance to no kernel") |
4129 | 973d7867 | Iustin Pop | |
4130 | 973d7867 | Iustin Pop | if self.kernel_path != constants.VALUE_DEFAULT: |
4131 | 973d7867 | Iustin Pop | if not os.path.isabs(self.kernel_path): |
4132 | ba4b62cf | Iustin Pop | raise errors.OpPrereqError("The kernel path must be an absolute" |
4133 | 973d7867 | Iustin Pop | " filename")
|
4134 | 8cafeb26 | Iustin Pop | else:
|
4135 | 8cafeb26 | Iustin Pop | self.do_kernel_path = False |
4136 | 973d7867 | Iustin Pop | |
4137 | 973d7867 | Iustin Pop | if self.initrd_path is not None: |
4138 | 973d7867 | Iustin Pop | self.do_initrd_path = True |
4139 | 973d7867 | Iustin Pop | if self.initrd_path not in (constants.VALUE_NONE, |
4140 | 973d7867 | Iustin Pop | constants.VALUE_DEFAULT): |
4141 | 2bc22872 | Iustin Pop | if not os.path.isabs(self.initrd_path): |
4142 | ba4b62cf | Iustin Pop | raise errors.OpPrereqError("The initrd path must be an absolute" |
4143 | 973d7867 | Iustin Pop | " filename")
|
4144 | 8cafeb26 | Iustin Pop | else:
|
4145 | 8cafeb26 | Iustin Pop | self.do_initrd_path = False |
4146 | 973d7867 | Iustin Pop | |
4147 | 25c5878d | Alexander Schreiber | # boot order verification
|
4148 | 25c5878d | Alexander Schreiber | if self.hvm_boot_order is not None: |
4149 | 25c5878d | Alexander Schreiber | if self.hvm_boot_order != constants.VALUE_DEFAULT: |
4150 | 25c5878d | Alexander Schreiber | if len(self.hvm_boot_order.strip("acdn")) != 0: |
4151 | 25c5878d | Alexander Schreiber | raise errors.OpPrereqError("invalid boot order specified," |
4152 | 25c5878d | Alexander Schreiber | " must be one or more of [acdn]"
|
4153 | 25c5878d | Alexander Schreiber | " or 'default'")
|
4154 | 25c5878d | Alexander Schreiber | |
4155 | 31a853d2 | Iustin Pop | # hvm_cdrom_image_path verification
|
4156 | 31a853d2 | Iustin Pop | if self.op.hvm_cdrom_image_path is not None: |
4157 | 31a853d2 | Iustin Pop | if not os.path.isabs(self.op.hvm_cdrom_image_path): |
4158 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("The path to the HVM CDROM image must" |
4159 | 31a853d2 | Iustin Pop | " be an absolute path or None, not %s" %
|
4160 | 31a853d2 | Iustin Pop | self.op.hvm_cdrom_image_path)
|
4161 | 31a853d2 | Iustin Pop | if not os.path.isfile(self.op.hvm_cdrom_image_path): |
4162 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("The HVM CDROM image must either be a" |
4163 | 31a853d2 | Iustin Pop | " regular file or a symlink pointing to"
|
4164 | 31a853d2 | Iustin Pop | " an existing regular file, not %s" %
|
4165 | 31a853d2 | Iustin Pop | self.op.hvm_cdrom_image_path)
|
4166 | 31a853d2 | Iustin Pop | |
4167 | 31a853d2 | Iustin Pop | # vnc_bind_address verification
|
4168 | 31a853d2 | Iustin Pop | if self.op.vnc_bind_address is not None: |
4169 | 31a853d2 | Iustin Pop | if not utils.IsValidIP(self.op.vnc_bind_address): |
4170 | 31a853d2 | Iustin Pop | raise errors.OpPrereqError("given VNC bind address '%s' doesn't look" |
4171 | 31a853d2 | Iustin Pop | " like a valid IP address" %
|
4172 | 31a853d2 | Iustin Pop | self.op.vnc_bind_address)
|
4173 | 31a853d2 | Iustin Pop | |
4174 | a8083063 | Iustin Pop | instance = self.cfg.GetInstanceInfo(
|
4175 | a8083063 | Iustin Pop | self.cfg.ExpandInstanceName(self.op.instance_name)) |
4176 | a8083063 | Iustin Pop | if instance is None: |
4177 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("No such instance name '%s'" % |
4178 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
4179 | a8083063 | Iustin Pop | self.op.instance_name = instance.name
|
4180 | a8083063 | Iustin Pop | self.instance = instance
|
4181 | a8083063 | Iustin Pop | return
|
4182 | a8083063 | Iustin Pop | |
4183 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4184 | a8083063 | Iustin Pop | """Modifies an instance.
|
4185 | a8083063 | Iustin Pop |
|
4186 | a8083063 | Iustin Pop | All parameters take effect only at the next restart of the instance.
|
4187 | a8083063 | Iustin Pop | """
|
4188 | a8083063 | Iustin Pop | result = [] |
4189 | a8083063 | Iustin Pop | instance = self.instance
|
4190 | a8083063 | Iustin Pop | if self.mem: |
4191 | a8083063 | Iustin Pop | instance.memory = self.mem
|
4192 | a8083063 | Iustin Pop | result.append(("mem", self.mem)) |
4193 | a8083063 | Iustin Pop | if self.vcpus: |
4194 | a8083063 | Iustin Pop | instance.vcpus = self.vcpus
|
4195 | a8083063 | Iustin Pop | result.append(("vcpus", self.vcpus)) |
4196 | a8083063 | Iustin Pop | if self.do_ip: |
4197 | a8083063 | Iustin Pop | instance.nics[0].ip = self.ip |
4198 | a8083063 | Iustin Pop | result.append(("ip", self.ip)) |
4199 | a8083063 | Iustin Pop | if self.bridge: |
4200 | a8083063 | Iustin Pop | instance.nics[0].bridge = self.bridge |
4201 | a8083063 | Iustin Pop | result.append(("bridge", self.bridge)) |
4202 | 1862d460 | Alexander Schreiber | if self.mac: |
4203 | 1862d460 | Alexander Schreiber | instance.nics[0].mac = self.mac |
4204 | 1862d460 | Alexander Schreiber | result.append(("mac", self.mac)) |
4205 | 973d7867 | Iustin Pop | if self.do_kernel_path: |
4206 | 973d7867 | Iustin Pop | instance.kernel_path = self.kernel_path
|
4207 | 973d7867 | Iustin Pop | result.append(("kernel_path", self.kernel_path)) |
4208 | 973d7867 | Iustin Pop | if self.do_initrd_path: |
4209 | 973d7867 | Iustin Pop | instance.initrd_path = self.initrd_path
|
4210 | 973d7867 | Iustin Pop | result.append(("initrd_path", self.initrd_path)) |
4211 | 25c5878d | Alexander Schreiber | if self.hvm_boot_order: |
4212 | 25c5878d | Alexander Schreiber | if self.hvm_boot_order == constants.VALUE_DEFAULT: |
4213 | 25c5878d | Alexander Schreiber | instance.hvm_boot_order = None
|
4214 | 25c5878d | Alexander Schreiber | else:
|
4215 | 25c5878d | Alexander Schreiber | instance.hvm_boot_order = self.hvm_boot_order
|
4216 | 25c5878d | Alexander Schreiber | result.append(("hvm_boot_order", self.hvm_boot_order)) |
4217 | 31a853d2 | Iustin Pop | if self.hvm_acpi: |
4218 | ec1ba002 | Iustin Pop | instance.hvm_acpi = self.hvm_acpi
|
4219 | 31a853d2 | Iustin Pop | result.append(("hvm_acpi", self.hvm_acpi)) |
4220 | 31a853d2 | Iustin Pop | if self.hvm_pae: |
4221 | ec1ba002 | Iustin Pop | instance.hvm_pae = self.hvm_pae
|
4222 | 31a853d2 | Iustin Pop | result.append(("hvm_pae", self.hvm_pae)) |
4223 | 31a853d2 | Iustin Pop | if self.hvm_cdrom_image_path: |
4224 | ec1ba002 | Iustin Pop | instance.hvm_cdrom_image_path = self.hvm_cdrom_image_path
|
4225 | 31a853d2 | Iustin Pop | result.append(("hvm_cdrom_image_path", self.hvm_cdrom_image_path)) |
4226 | 31a853d2 | Iustin Pop | if self.vnc_bind_address: |
4227 | 31a853d2 | Iustin Pop | instance.vnc_bind_address = self.vnc_bind_address
|
4228 | 31a853d2 | Iustin Pop | result.append(("vnc_bind_address", self.vnc_bind_address)) |
4229 | a8083063 | Iustin Pop | |
4230 | a8083063 | Iustin Pop | self.cfg.AddInstance(instance)
|
4231 | a8083063 | Iustin Pop | |
4232 | a8083063 | Iustin Pop | return result
|
4233 | a8083063 | Iustin Pop | |
4234 | a8083063 | Iustin Pop | |
4235 | a8083063 | Iustin Pop | class LUQueryExports(NoHooksLU): |
4236 | a8083063 | Iustin Pop | """Query the exports list
|
4237 | a8083063 | Iustin Pop |
|
4238 | a8083063 | Iustin Pop | """
|
4239 | a8083063 | Iustin Pop | _OP_REQP = [] |
4240 | a8083063 | Iustin Pop | |
4241 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4242 | a8083063 | Iustin Pop | """Check that the nodelist contains only existing nodes.
|
4243 | a8083063 | Iustin Pop |
|
4244 | a8083063 | Iustin Pop | """
|
4245 | dcb93971 | Michael Hanselmann | self.nodes = _GetWantedNodes(self, getattr(self.op, "nodes", None)) |
4246 | a8083063 | Iustin Pop | |
4247 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4248 | a8083063 | Iustin Pop | """Compute the list of all the exported system images.
|
4249 | a8083063 | Iustin Pop |
|
4250 | a8083063 | Iustin Pop | Returns:
|
4251 | a8083063 | Iustin Pop | a dictionary with the structure node->(export-list)
|
4252 | a8083063 | Iustin Pop | where export-list is a list of the instances exported on
|
4253 | a8083063 | Iustin Pop | that node.
|
4254 | a8083063 | Iustin Pop |
|
4255 | a8083063 | Iustin Pop | """
|
4256 | a7ba5e53 | Iustin Pop | return rpc.call_export_list(self.nodes) |
4257 | a8083063 | Iustin Pop | |
4258 | a8083063 | Iustin Pop | |
4259 | a8083063 | Iustin Pop | class LUExportInstance(LogicalUnit): |
4260 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
4261 | a8083063 | Iustin Pop |
|
4262 | a8083063 | Iustin Pop | """
|
4263 | a8083063 | Iustin Pop | HPATH = "instance-export"
|
4264 | a8083063 | Iustin Pop | HTYPE = constants.HTYPE_INSTANCE |
4265 | a8083063 | Iustin Pop | _OP_REQP = ["instance_name", "target_node", "shutdown"] |
4266 | a8083063 | Iustin Pop | |
4267 | a8083063 | Iustin Pop | def BuildHooksEnv(self): |
4268 | a8083063 | Iustin Pop | """Build hooks env.
|
4269 | a8083063 | Iustin Pop |
|
4270 | a8083063 | Iustin Pop | This will run on the master, primary node and target node.
|
4271 | a8083063 | Iustin Pop |
|
4272 | a8083063 | Iustin Pop | """
|
4273 | a8083063 | Iustin Pop | env = { |
4274 | a8083063 | Iustin Pop | "EXPORT_NODE": self.op.target_node, |
4275 | a8083063 | Iustin Pop | "EXPORT_DO_SHUTDOWN": self.op.shutdown, |
4276 | a8083063 | Iustin Pop | } |
4277 | 396e1b78 | Michael Hanselmann | env.update(_BuildInstanceHookEnvByObject(self.instance))
|
4278 | 880478f8 | Iustin Pop | nl = [self.sstore.GetMasterNode(), self.instance.primary_node, |
4279 | a8083063 | Iustin Pop | self.op.target_node]
|
4280 | a8083063 | Iustin Pop | return env, nl, nl
|
4281 | a8083063 | Iustin Pop | |
4282 | a8083063 | Iustin Pop | def CheckPrereq(self): |
4283 | a8083063 | Iustin Pop | """Check prerequisites.
|
4284 | a8083063 | Iustin Pop |
|
4285 | 9ac99fda | Guido Trotter | This checks that the instance and node names are valid.
|
4286 | a8083063 | Iustin Pop |
|
4287 | a8083063 | Iustin Pop | """
|
4288 | a8083063 | Iustin Pop | instance_name = self.cfg.ExpandInstanceName(self.op.instance_name) |
4289 | a8083063 | Iustin Pop | self.instance = self.cfg.GetInstanceInfo(instance_name) |
4290 | a8083063 | Iustin Pop | if self.instance is None: |
4291 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not found" % |
4292 | 3ecf6786 | Iustin Pop | self.op.instance_name)
|
4293 | a8083063 | Iustin Pop | |
4294 | a8083063 | Iustin Pop | # node verification
|
4295 | a8083063 | Iustin Pop | dst_node_short = self.cfg.ExpandNodeName(self.op.target_node) |
4296 | a8083063 | Iustin Pop | self.dst_node = self.cfg.GetNodeInfo(dst_node_short) |
4297 | a8083063 | Iustin Pop | |
4298 | a8083063 | Iustin Pop | if self.dst_node is None: |
4299 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Destination node '%s' is unknown." % |
4300 | 3ecf6786 | Iustin Pop | self.op.target_node)
|
4301 | a8083063 | Iustin Pop | self.op.target_node = self.dst_node.name |
4302 | a8083063 | Iustin Pop | |
4303 | b6023d6c | Manuel Franceschini | # instance disk type verification
|
4304 | b6023d6c | Manuel Franceschini | for disk in self.instance.disks: |
4305 | b6023d6c | Manuel Franceschini | if disk.dev_type == constants.LD_FILE:
|
4306 | b6023d6c | Manuel Franceschini | raise errors.OpPrereqError("Export not supported for instances with" |
4307 | b6023d6c | Manuel Franceschini | " file-based disks")
|
4308 | b6023d6c | Manuel Franceschini | |
4309 | a8083063 | Iustin Pop | def Exec(self, feedback_fn): |
4310 | a8083063 | Iustin Pop | """Export an instance to an image in the cluster.
|
4311 | a8083063 | Iustin Pop |
|
4312 | a8083063 | Iustin Pop | """
|
4313 | a8083063 | Iustin Pop | instance = self.instance
|
4314 | a8083063 | Iustin Pop | dst_node = self.dst_node
|
4315 | a8083063 | Iustin Pop | src_node = instance.primary_node |
4316 | a8083063 | Iustin Pop | if self.op.shutdown: |
4317 | fb300fb7 | Guido Trotter | # shutdown the instance, but not the disks
|
4318 | fb300fb7 | Guido Trotter | if not rpc.call_instance_shutdown(src_node, instance): |
4319 | fb300fb7 | Guido Trotter | raise errors.OpExecError("Could not shutdown instance %s on node %s" % |
4320 | b4de68a9 | Iustin Pop | (instance.name, src_node)) |
4321 | a8083063 | Iustin Pop | |
4322 | a8083063 | Iustin Pop | vgname = self.cfg.GetVGName()
|
4323 | a8083063 | Iustin Pop | |
4324 | a8083063 | Iustin Pop | snap_disks = [] |
4325 | a8083063 | Iustin Pop | |
4326 | a8083063 | Iustin Pop | try:
|
4327 | a8083063 | Iustin Pop | for disk in instance.disks: |
4328 | a8083063 | Iustin Pop | if disk.iv_name == "sda": |
4329 | a8083063 | Iustin Pop | # new_dev_name will be a snapshot of an lvm leaf of the one we passed
|
4330 | a8083063 | Iustin Pop | new_dev_name = rpc.call_blockdev_snapshot(src_node, disk) |
4331 | a8083063 | Iustin Pop | |
4332 | a8083063 | Iustin Pop | if not new_dev_name: |
4333 | a8083063 | Iustin Pop | logger.Error("could not snapshot block device %s on node %s" %
|
4334 | a8083063 | Iustin Pop | (disk.logical_id[1], src_node))
|
4335 | a8083063 | Iustin Pop | else:
|
4336 | fe96220b | Iustin Pop | new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size, |
4337 | a8083063 | Iustin Pop | logical_id=(vgname, new_dev_name), |
4338 | a8083063 | Iustin Pop | physical_id=(vgname, new_dev_name), |
4339 | a8083063 | Iustin Pop | iv_name=disk.iv_name) |
4340 | a8083063 | Iustin Pop | snap_disks.append(new_dev) |
4341 | a8083063 | Iustin Pop | |
4342 | a8083063 | Iustin Pop | finally:
|
4343 | fb300fb7 | Guido Trotter | if self.op.shutdown and instance.status == "up": |
4344 | fb300fb7 | Guido Trotter | if not rpc.call_instance_start(src_node, instance, None): |
4345 | fb300fb7 | Guido Trotter | _ShutdownInstanceDisks(instance, self.cfg)
|
4346 | fb300fb7 | Guido Trotter | raise errors.OpExecError("Could not start instance") |
4347 | a8083063 | Iustin Pop | |
4348 | a8083063 | Iustin Pop | # TODO: check for size
|
4349 | a8083063 | Iustin Pop | |
4350 | a8083063 | Iustin Pop | for dev in snap_disks: |
4351 | 16687b98 | Manuel Franceschini | if not rpc.call_snapshot_export(src_node, dev, dst_node.name, instance): |
4352 | 16687b98 | Manuel Franceschini | logger.Error("could not export block device %s from node %s to node %s"
|
4353 | 16687b98 | Manuel Franceschini | % (dev.logical_id[1], src_node, dst_node.name))
|
4354 | a8083063 | Iustin Pop | if not rpc.call_blockdev_remove(src_node, dev): |
4355 | 16687b98 | Manuel Franceschini | logger.Error("could not remove snapshot block device %s from node %s" %
|
4356 | 16687b98 | Manuel Franceschini | (dev.logical_id[1], src_node))
|
4357 | a8083063 | Iustin Pop | |
4358 | a8083063 | Iustin Pop | if not rpc.call_finalize_export(dst_node.name, instance, snap_disks): |
4359 | a8083063 | Iustin Pop | logger.Error("could not finalize export for instance %s on node %s" %
|
4360 | a8083063 | Iustin Pop | (instance.name, dst_node.name)) |
4361 | a8083063 | Iustin Pop | |
4362 | a8083063 | Iustin Pop | nodelist = self.cfg.GetNodeList()
|
4363 | a8083063 | Iustin Pop | nodelist.remove(dst_node.name) |
4364 | a8083063 | Iustin Pop | |
4365 | a8083063 | Iustin Pop | # on one-node clusters nodelist will be empty after the removal
|
4366 | a8083063 | Iustin Pop | # if we proceed the backup would be removed because OpQueryExports
|
4367 | a8083063 | Iustin Pop | # substitutes an empty list with the full cluster node list.
|
4368 | a8083063 | Iustin Pop | if nodelist:
|
4369 | a8083063 | Iustin Pop | op = opcodes.OpQueryExports(nodes=nodelist) |
4370 | 5bfac263 | Iustin Pop | exportlist = self.proc.ChainOpCode(op)
|
4371 | a8083063 | Iustin Pop | for node in exportlist: |
4372 | a8083063 | Iustin Pop | if instance.name in exportlist[node]: |
4373 | a8083063 | Iustin Pop | if not rpc.call_export_remove(node, instance.name): |
4374 | a8083063 | Iustin Pop | logger.Error("could not remove older export for instance %s"
|
4375 | a8083063 | Iustin Pop | " on node %s" % (instance.name, node))
|
4376 | 5c947f38 | Iustin Pop | |
4377 | 5c947f38 | Iustin Pop | |
4378 | 9ac99fda | Guido Trotter | class LURemoveExport(NoHooksLU): |
4379 | 9ac99fda | Guido Trotter | """Remove exports related to the named instance.
|
4380 | 9ac99fda | Guido Trotter |
|
4381 | 9ac99fda | Guido Trotter | """
|
4382 | 9ac99fda | Guido Trotter | _OP_REQP = ["instance_name"]
|
4383 | 9ac99fda | Guido Trotter | |
4384 | 9ac99fda | Guido Trotter | def CheckPrereq(self): |
4385 | 9ac99fda | Guido Trotter | """Check prerequisites.
|
4386 | 9ac99fda | Guido Trotter | """
|
4387 | 9ac99fda | Guido Trotter | pass
|
4388 | 9ac99fda | Guido Trotter | |
4389 | 9ac99fda | Guido Trotter | def Exec(self, feedback_fn): |
4390 | 9ac99fda | Guido Trotter | """Remove any export.
|
4391 | 9ac99fda | Guido Trotter |
|
4392 | 9ac99fda | Guido Trotter | """
|
4393 | 9ac99fda | Guido Trotter | instance_name = self.cfg.ExpandInstanceName(self.op.instance_name) |
4394 | 9ac99fda | Guido Trotter | # If the instance was not found we'll try with the name that was passed in.
|
4395 | 9ac99fda | Guido Trotter | # This will only work if it was an FQDN, though.
|
4396 | 9ac99fda | Guido Trotter | fqdn_warn = False
|
4397 | 9ac99fda | Guido Trotter | if not instance_name: |
4398 | 9ac99fda | Guido Trotter | fqdn_warn = True
|
4399 | 9ac99fda | Guido Trotter | instance_name = self.op.instance_name
|
4400 | 9ac99fda | Guido Trotter | |
4401 | 9ac99fda | Guido Trotter | op = opcodes.OpQueryExports(nodes=[]) |
4402 | 9ac99fda | Guido Trotter | exportlist = self.proc.ChainOpCode(op)
|
4403 | 9ac99fda | Guido Trotter | found = False
|
4404 | 9ac99fda | Guido Trotter | for node in exportlist: |
4405 | 9ac99fda | Guido Trotter | if instance_name in exportlist[node]: |
4406 | 9ac99fda | Guido Trotter | found = True
|
4407 | 9ac99fda | Guido Trotter | if not rpc.call_export_remove(node, instance_name): |
4408 | 9ac99fda | Guido Trotter | logger.Error("could not remove export for instance %s"
|
4409 | 9ac99fda | Guido Trotter | " on node %s" % (instance_name, node))
|
4410 | 9ac99fda | Guido Trotter | |
4411 | 9ac99fda | Guido Trotter | if fqdn_warn and not found: |
4412 | 9ac99fda | Guido Trotter | feedback_fn("Export not found. If trying to remove an export belonging"
|
4413 | 9ac99fda | Guido Trotter | " to a deleted instance please use its Fully Qualified"
|
4414 | 9ac99fda | Guido Trotter | " Domain Name.")
|
4415 | 9ac99fda | Guido Trotter | |
4416 | 9ac99fda | Guido Trotter | |
4417 | 5c947f38 | Iustin Pop | class TagsLU(NoHooksLU): |
4418 | 5c947f38 | Iustin Pop | """Generic tags LU.
|
4419 | 5c947f38 | Iustin Pop |
|
4420 | 5c947f38 | Iustin Pop | This is an abstract class which is the parent of all the other tags LUs.
|
4421 | 5c947f38 | Iustin Pop |
|
4422 | 5c947f38 | Iustin Pop | """
|
4423 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
4424 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
4425 | 5c947f38 | Iustin Pop |
|
4426 | 5c947f38 | Iustin Pop | """
|
4427 | 5c947f38 | Iustin Pop | if self.op.kind == constants.TAG_CLUSTER: |
4428 | 5c947f38 | Iustin Pop | self.target = self.cfg.GetClusterInfo() |
4429 | 5c947f38 | Iustin Pop | elif self.op.kind == constants.TAG_NODE: |
4430 | 5c947f38 | Iustin Pop | name = self.cfg.ExpandNodeName(self.op.name) |
4431 | 5c947f38 | Iustin Pop | if name is None: |
4432 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid node name (%s)" % |
4433 | 3ecf6786 | Iustin Pop | (self.op.name,))
|
4434 | 5c947f38 | Iustin Pop | self.op.name = name
|
4435 | 5c947f38 | Iustin Pop | self.target = self.cfg.GetNodeInfo(name) |
4436 | 5c947f38 | Iustin Pop | elif self.op.kind == constants.TAG_INSTANCE: |
4437 | 8f684e16 | Iustin Pop | name = self.cfg.ExpandInstanceName(self.op.name) |
4438 | 5c947f38 | Iustin Pop | if name is None: |
4439 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Invalid instance name (%s)" % |
4440 | 3ecf6786 | Iustin Pop | (self.op.name,))
|
4441 | 5c947f38 | Iustin Pop | self.op.name = name
|
4442 | 5c947f38 | Iustin Pop | self.target = self.cfg.GetInstanceInfo(name) |
4443 | 5c947f38 | Iustin Pop | else:
|
4444 | 3ecf6786 | Iustin Pop | raise errors.OpPrereqError("Wrong tag type requested (%s)" % |
4445 | 3ecf6786 | Iustin Pop | str(self.op.kind)) |
4446 | 5c947f38 | Iustin Pop | |
4447 | 5c947f38 | Iustin Pop | |
4448 | 5c947f38 | Iustin Pop | class LUGetTags(TagsLU): |
4449 | 5c947f38 | Iustin Pop | """Returns the tags of a given object.
|
4450 | 5c947f38 | Iustin Pop |
|
4451 | 5c947f38 | Iustin Pop | """
|
4452 | 5c947f38 | Iustin Pop | _OP_REQP = ["kind", "name"] |
4453 | 5c947f38 | Iustin Pop | |
4454 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
4455 | 5c947f38 | Iustin Pop | """Returns the tag list.
|
4456 | 5c947f38 | Iustin Pop |
|
4457 | 5c947f38 | Iustin Pop | """
|
4458 | 5c947f38 | Iustin Pop | return self.target.GetTags() |
4459 | 5c947f38 | Iustin Pop | |
4460 | 5c947f38 | Iustin Pop | |
4461 | 73415719 | Iustin Pop | class LUSearchTags(NoHooksLU): |
4462 | 73415719 | Iustin Pop | """Searches the tags for a given pattern.
|
4463 | 73415719 | Iustin Pop |
|
4464 | 73415719 | Iustin Pop | """
|
4465 | 73415719 | Iustin Pop | _OP_REQP = ["pattern"]
|
4466 | 73415719 | Iustin Pop | |
4467 | 73415719 | Iustin Pop | def CheckPrereq(self): |
4468 | 73415719 | Iustin Pop | """Check prerequisites.
|
4469 | 73415719 | Iustin Pop |
|
4470 | 73415719 | Iustin Pop | This checks the pattern passed for validity by compiling it.
|
4471 | 73415719 | Iustin Pop |
|
4472 | 73415719 | Iustin Pop | """
|
4473 | 73415719 | Iustin Pop | try:
|
4474 | 73415719 | Iustin Pop | self.re = re.compile(self.op.pattern) |
4475 | 73415719 | Iustin Pop | except re.error, err:
|
4476 | 73415719 | Iustin Pop | raise errors.OpPrereqError("Invalid search pattern '%s': %s" % |
4477 | 73415719 | Iustin Pop | (self.op.pattern, err))
|
4478 | 73415719 | Iustin Pop | |
4479 | 73415719 | Iustin Pop | def Exec(self, feedback_fn): |
4480 | 73415719 | Iustin Pop | """Returns the tag list.
|
4481 | 73415719 | Iustin Pop |
|
4482 | 73415719 | Iustin Pop | """
|
4483 | 73415719 | Iustin Pop | cfg = self.cfg
|
4484 | 73415719 | Iustin Pop | tgts = [("/cluster", cfg.GetClusterInfo())]
|
4485 | 73415719 | Iustin Pop | ilist = [cfg.GetInstanceInfo(name) for name in cfg.GetInstanceList()] |
4486 | 73415719 | Iustin Pop | tgts.extend([("/instances/%s" % i.name, i) for i in ilist]) |
4487 | 73415719 | Iustin Pop | nlist = [cfg.GetNodeInfo(name) for name in cfg.GetNodeList()] |
4488 | 73415719 | Iustin Pop | tgts.extend([("/nodes/%s" % n.name, n) for n in nlist]) |
4489 | 73415719 | Iustin Pop | results = [] |
4490 | 73415719 | Iustin Pop | for path, target in tgts: |
4491 | 73415719 | Iustin Pop | for tag in target.GetTags(): |
4492 | 73415719 | Iustin Pop | if self.re.search(tag): |
4493 | 73415719 | Iustin Pop | results.append((path, tag)) |
4494 | 73415719 | Iustin Pop | return results
|
4495 | 73415719 | Iustin Pop | |
4496 | 73415719 | Iustin Pop | |
4497 | f27302fa | Iustin Pop | class LUAddTags(TagsLU): |
4498 | 5c947f38 | Iustin Pop | """Sets a tag on a given object.
|
4499 | 5c947f38 | Iustin Pop |
|
4500 | 5c947f38 | Iustin Pop | """
|
4501 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
4502 | 5c947f38 | Iustin Pop | |
4503 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
4504 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
4505 | 5c947f38 | Iustin Pop |
|
4506 | 5c947f38 | Iustin Pop | This checks the type and length of the tag name and value.
|
4507 | 5c947f38 | Iustin Pop |
|
4508 | 5c947f38 | Iustin Pop | """
|
4509 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
4510 | f27302fa | Iustin Pop | for tag in self.op.tags: |
4511 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
4512 | 5c947f38 | Iustin Pop | |
4513 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
4514 | 5c947f38 | Iustin Pop | """Sets the tag.
|
4515 | 5c947f38 | Iustin Pop |
|
4516 | 5c947f38 | Iustin Pop | """
|
4517 | 5c947f38 | Iustin Pop | try:
|
4518 | f27302fa | Iustin Pop | for tag in self.op.tags: |
4519 | f27302fa | Iustin Pop | self.target.AddTag(tag)
|
4520 | 5c947f38 | Iustin Pop | except errors.TagError, err:
|
4521 | 3ecf6786 | Iustin Pop | raise errors.OpExecError("Error while setting tag: %s" % str(err)) |
4522 | 5c947f38 | Iustin Pop | try:
|
4523 | 5c947f38 | Iustin Pop | self.cfg.Update(self.target) |
4524 | 5c947f38 | Iustin Pop | except errors.ConfigurationError:
|
4525 | 3ecf6786 | Iustin Pop | raise errors.OpRetryError("There has been a modification to the" |
4526 | 3ecf6786 | Iustin Pop | " config file and the operation has been"
|
4527 | 3ecf6786 | Iustin Pop | " aborted. Please retry.")
|
4528 | 5c947f38 | Iustin Pop | |
4529 | 5c947f38 | Iustin Pop | |
4530 | f27302fa | Iustin Pop | class LUDelTags(TagsLU): |
4531 | f27302fa | Iustin Pop | """Delete a list of tags from a given object.
|
4532 | 5c947f38 | Iustin Pop |
|
4533 | 5c947f38 | Iustin Pop | """
|
4534 | f27302fa | Iustin Pop | _OP_REQP = ["kind", "name", "tags"] |
4535 | 5c947f38 | Iustin Pop | |
4536 | 5c947f38 | Iustin Pop | def CheckPrereq(self): |
4537 | 5c947f38 | Iustin Pop | """Check prerequisites.
|
4538 | 5c947f38 | Iustin Pop |
|
4539 | 5c947f38 | Iustin Pop | This checks that we have the given tag.
|
4540 | 5c947f38 | Iustin Pop |
|
4541 | 5c947f38 | Iustin Pop | """
|
4542 | 5c947f38 | Iustin Pop | TagsLU.CheckPrereq(self)
|
4543 | f27302fa | Iustin Pop | for tag in self.op.tags: |
4544 | f27302fa | Iustin Pop | objects.TaggableObject.ValidateTag(tag) |
4545 | f27302fa | Iustin Pop | del_tags = frozenset(self.op.tags) |
4546 | f27302fa | Iustin Pop | cur_tags = self.target.GetTags()
|
4547 | f27302fa | Iustin Pop | if not del_tags <= cur_tags: |
4548 | f27302fa | Iustin Pop | diff_tags = del_tags - cur_tags |
4549 | f27302fa | Iustin Pop | diff_names = ["'%s'" % tag for tag in diff_tags] |
4550 | f27302fa | Iustin Pop | diff_names.sort() |
4551 | f27302fa | Iustin Pop | raise errors.OpPrereqError("Tag(s) %s not found" % |
4552 | f27302fa | Iustin Pop | (",".join(diff_names)))
|
4553 | 5c947f38 | Iustin Pop | |
4554 | 5c947f38 | Iustin Pop | def Exec(self, feedback_fn): |
4555 | 5c947f38 | Iustin Pop | """Remove the tag from the object.
|
4556 | 5c947f38 | Iustin Pop |
|
4557 | 5c947f38 | Iustin Pop | """
|
4558 | f27302fa | Iustin Pop | for tag in self.op.tags: |
4559 | f27302fa | Iustin Pop | self.target.RemoveTag(tag)
|
4560 | 5c947f38 | Iustin Pop | try:
|
4561 | 5c947f38 | Iustin Pop | self.cfg.Update(self.target) |
4562 | 5c947f38 | Iustin Pop | except errors.ConfigurationError:
|
4563 | 3ecf6786 | Iustin Pop | raise errors.OpRetryError("There has been a modification to the" |
4564 | 3ecf6786 | Iustin Pop | " config file and the operation has been"
|
4565 | 3ecf6786 | Iustin Pop | " aborted. Please retry.")
|
4566 | 06009e27 | Iustin Pop | |
4567 | 06009e27 | Iustin Pop | class LUTestDelay(NoHooksLU): |
4568 | 06009e27 | Iustin Pop | """Sleep for a specified amount of time.
|
4569 | 06009e27 | Iustin Pop |
|
4570 | 06009e27 | Iustin Pop | This LU sleeps on the master and/or nodes for a specified amoutn of
|
4571 | 06009e27 | Iustin Pop | time.
|
4572 | 06009e27 | Iustin Pop |
|
4573 | 06009e27 | Iustin Pop | """
|
4574 | 06009e27 | Iustin Pop | _OP_REQP = ["duration", "on_master", "on_nodes"] |
4575 | 06009e27 | Iustin Pop | |
4576 | 06009e27 | Iustin Pop | def CheckPrereq(self): |
4577 | 06009e27 | Iustin Pop | """Check prerequisites.
|
4578 | 06009e27 | Iustin Pop |
|
4579 | 06009e27 | Iustin Pop | This checks that we have a good list of nodes and/or the duration
|
4580 | 06009e27 | Iustin Pop | is valid.
|
4581 | 06009e27 | Iustin Pop |
|
4582 | 06009e27 | Iustin Pop | """
|
4583 | 06009e27 | Iustin Pop | |
4584 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
4585 | 06009e27 | Iustin Pop | self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes) |
4586 | 06009e27 | Iustin Pop | |
4587 | 06009e27 | Iustin Pop | def Exec(self, feedback_fn): |
4588 | 06009e27 | Iustin Pop | """Do the actual sleep.
|
4589 | 06009e27 | Iustin Pop |
|
4590 | 06009e27 | Iustin Pop | """
|
4591 | 06009e27 | Iustin Pop | if self.op.on_master: |
4592 | 06009e27 | Iustin Pop | if not utils.TestDelay(self.op.duration): |
4593 | 06009e27 | Iustin Pop | raise errors.OpExecError("Error during master delay test") |
4594 | 06009e27 | Iustin Pop | if self.op.on_nodes: |
4595 | 06009e27 | Iustin Pop | result = rpc.call_test_delay(self.op.on_nodes, self.op.duration) |
4596 | 06009e27 | Iustin Pop | if not result: |
4597 | 06009e27 | Iustin Pop | raise errors.OpExecError("Complete failure from rpc call") |
4598 | 06009e27 | Iustin Pop | for node, node_result in result.items(): |
4599 | 06009e27 | Iustin Pop | if not node_result: |
4600 | 06009e27 | Iustin Pop | raise errors.OpExecError("Failure during rpc call to node %s," |
4601 | 06009e27 | Iustin Pop | " result: %s" % (node, node_result))
|
4602 | d61df03e | Iustin Pop | |
4603 | d61df03e | Iustin Pop | |
4604 | d1c2dd75 | Iustin Pop | class IAllocator(object): |
4605 | d1c2dd75 | Iustin Pop | """IAllocator framework.
|
4606 | d61df03e | Iustin Pop |
|
4607 | d1c2dd75 | Iustin Pop | An IAllocator instance has three sets of attributes:
|
4608 | d1c2dd75 | Iustin Pop | - cfg/sstore that are needed to query the cluster
|
4609 | d1c2dd75 | Iustin Pop | - input data (all members of the _KEYS class attribute are required)
|
4610 | d1c2dd75 | Iustin Pop | - four buffer attributes (in|out_data|text), that represent the
|
4611 | d1c2dd75 | Iustin Pop | input (to the external script) in text and data structure format,
|
4612 | d1c2dd75 | Iustin Pop | and the output from it, again in two formats
|
4613 | d1c2dd75 | Iustin Pop | - the result variables from the script (success, info, nodes) for
|
4614 | d1c2dd75 | Iustin Pop | easy usage
|
4615 | d61df03e | Iustin Pop |
|
4616 | d61df03e | Iustin Pop | """
|
4617 | 29859cb7 | Iustin Pop | _ALLO_KEYS = [ |
4618 | d1c2dd75 | Iustin Pop | "mem_size", "disks", "disk_template", |
4619 | d1c2dd75 | Iustin Pop | "os", "tags", "nics", "vcpus", |
4620 | d1c2dd75 | Iustin Pop | ] |
4621 | 29859cb7 | Iustin Pop | _RELO_KEYS = [ |
4622 | 29859cb7 | Iustin Pop | "relocate_from",
|
4623 | 29859cb7 | Iustin Pop | ] |
4624 | d1c2dd75 | Iustin Pop | |
4625 | 29859cb7 | Iustin Pop | def __init__(self, cfg, sstore, mode, name, **kwargs): |
4626 | d1c2dd75 | Iustin Pop | self.cfg = cfg
|
4627 | d1c2dd75 | Iustin Pop | self.sstore = sstore
|
4628 | d1c2dd75 | Iustin Pop | # init buffer variables
|
4629 | d1c2dd75 | Iustin Pop | self.in_text = self.out_text = self.in_data = self.out_data = None |
4630 | d1c2dd75 | Iustin Pop | # init all input fields so that pylint is happy
|
4631 | 29859cb7 | Iustin Pop | self.mode = mode
|
4632 | 29859cb7 | Iustin Pop | self.name = name
|
4633 | d1c2dd75 | Iustin Pop | self.mem_size = self.disks = self.disk_template = None |
4634 | d1c2dd75 | Iustin Pop | self.os = self.tags = self.nics = self.vcpus = None |
4635 | 29859cb7 | Iustin Pop | self.relocate_from = None |
4636 | 27579978 | Iustin Pop | # computed fields
|
4637 | 27579978 | Iustin Pop | self.required_nodes = None |
4638 | d1c2dd75 | Iustin Pop | # init result fields
|
4639 | d1c2dd75 | Iustin Pop | self.success = self.info = self.nodes = None |
4640 | 29859cb7 | Iustin Pop | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
4641 | 29859cb7 | Iustin Pop | keyset = self._ALLO_KEYS
|
4642 | 29859cb7 | Iustin Pop | elif self.mode == constants.IALLOCATOR_MODE_RELOC: |
4643 | 29859cb7 | Iustin Pop | keyset = self._RELO_KEYS
|
4644 | 29859cb7 | Iustin Pop | else:
|
4645 | 29859cb7 | Iustin Pop | raise errors.ProgrammerError("Unknown mode '%s' passed to the" |
4646 | 29859cb7 | Iustin Pop | " IAllocator" % self.mode) |
4647 | d1c2dd75 | Iustin Pop | for key in kwargs: |
4648 | 29859cb7 | Iustin Pop | if key not in keyset: |
4649 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Invalid input parameter '%s' to" |
4650 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
4651 | d1c2dd75 | Iustin Pop | setattr(self, key, kwargs[key]) |
4652 | 29859cb7 | Iustin Pop | for key in keyset: |
4653 | d1c2dd75 | Iustin Pop | if key not in kwargs: |
4654 | d1c2dd75 | Iustin Pop | raise errors.ProgrammerError("Missing input parameter '%s' to" |
4655 | d1c2dd75 | Iustin Pop | " IAllocator" % key)
|
4656 | d1c2dd75 | Iustin Pop | self._BuildInputData()
|
4657 | d1c2dd75 | Iustin Pop | |
4658 | d1c2dd75 | Iustin Pop | def _ComputeClusterData(self): |
4659 | d1c2dd75 | Iustin Pop | """Compute the generic allocator input data.
|
4660 | d1c2dd75 | Iustin Pop |
|
4661 | d1c2dd75 | Iustin Pop | This is the data that is independent of the actual operation.
|
4662 | d1c2dd75 | Iustin Pop |
|
4663 | d1c2dd75 | Iustin Pop | """
|
4664 | d1c2dd75 | Iustin Pop | cfg = self.cfg
|
4665 | d1c2dd75 | Iustin Pop | # cluster data
|
4666 | d1c2dd75 | Iustin Pop | data = { |
4667 | d1c2dd75 | Iustin Pop | "version": 1, |
4668 | d1c2dd75 | Iustin Pop | "cluster_name": self.sstore.GetClusterName(), |
4669 | d1c2dd75 | Iustin Pop | "cluster_tags": list(cfg.GetClusterInfo().GetTags()), |
4670 | 6286519f | Iustin Pop | "hypervisor_type": self.sstore.GetHypervisorType(), |
4671 | d1c2dd75 | Iustin Pop | # we don't have job IDs
|
4672 | d61df03e | Iustin Pop | } |
4673 | d61df03e | Iustin Pop | |
4674 | 6286519f | Iustin Pop | i_list = [cfg.GetInstanceInfo(iname) for iname in cfg.GetInstanceList()] |
4675 | 6286519f | Iustin Pop | |
4676 | d1c2dd75 | Iustin Pop | # node data
|
4677 | d1c2dd75 | Iustin Pop | node_results = {} |
4678 | d1c2dd75 | Iustin Pop | node_list = cfg.GetNodeList() |
4679 | d1c2dd75 | Iustin Pop | node_data = rpc.call_node_info(node_list, cfg.GetVGName()) |
4680 | d1c2dd75 | Iustin Pop | for nname in node_list: |
4681 | d1c2dd75 | Iustin Pop | ninfo = cfg.GetNodeInfo(nname) |
4682 | d1c2dd75 | Iustin Pop | if nname not in node_data or not isinstance(node_data[nname], dict): |
4683 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't get data for node %s" % nname) |
4684 | d1c2dd75 | Iustin Pop | remote_info = node_data[nname] |
4685 | b2662e7f | Iustin Pop | for attr in ['memory_total', 'memory_free', 'memory_dom0', |
4686 | 4337cf1b | Iustin Pop | 'vg_size', 'vg_free', 'cpu_total']: |
4687 | d1c2dd75 | Iustin Pop | if attr not in remote_info: |
4688 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Node '%s' didn't return attribute '%s'" % |
4689 | d1c2dd75 | Iustin Pop | (nname, attr)) |
4690 | d1c2dd75 | Iustin Pop | try:
|
4691 | b2662e7f | Iustin Pop | remote_info[attr] = int(remote_info[attr])
|
4692 | d1c2dd75 | Iustin Pop | except ValueError, err: |
4693 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Node '%s' returned invalid value for '%s':" |
4694 | d1c2dd75 | Iustin Pop | " %s" % (nname, attr, str(err))) |
4695 | 6286519f | Iustin Pop | # compute memory used by primary instances
|
4696 | 6286519f | Iustin Pop | i_p_mem = i_p_up_mem = 0
|
4697 | 6286519f | Iustin Pop | for iinfo in i_list: |
4698 | 6286519f | Iustin Pop | if iinfo.primary_node == nname:
|
4699 | 6286519f | Iustin Pop | i_p_mem += iinfo.memory |
4700 | 6286519f | Iustin Pop | if iinfo.status == "up": |
4701 | 6286519f | Iustin Pop | i_p_up_mem += iinfo.memory |
4702 | 6286519f | Iustin Pop | |
4703 | b2662e7f | Iustin Pop | # compute memory used by instances
|
4704 | d1c2dd75 | Iustin Pop | pnr = { |
4705 | d1c2dd75 | Iustin Pop | "tags": list(ninfo.GetTags()), |
4706 | b2662e7f | Iustin Pop | "total_memory": remote_info['memory_total'], |
4707 | b2662e7f | Iustin Pop | "reserved_memory": remote_info['memory_dom0'], |
4708 | b2662e7f | Iustin Pop | "free_memory": remote_info['memory_free'], |
4709 | 6286519f | Iustin Pop | "i_pri_memory": i_p_mem,
|
4710 | 6286519f | Iustin Pop | "i_pri_up_memory": i_p_up_mem,
|
4711 | b2662e7f | Iustin Pop | "total_disk": remote_info['vg_size'], |
4712 | b2662e7f | Iustin Pop | "free_disk": remote_info['vg_free'], |
4713 | d1c2dd75 | Iustin Pop | "primary_ip": ninfo.primary_ip,
|
4714 | d1c2dd75 | Iustin Pop | "secondary_ip": ninfo.secondary_ip,
|
4715 | 4337cf1b | Iustin Pop | "total_cpus": remote_info['cpu_total'], |
4716 | d1c2dd75 | Iustin Pop | } |
4717 | d1c2dd75 | Iustin Pop | node_results[nname] = pnr |
4718 | d1c2dd75 | Iustin Pop | data["nodes"] = node_results
|
4719 | d1c2dd75 | Iustin Pop | |
4720 | d1c2dd75 | Iustin Pop | # instance data
|
4721 | d1c2dd75 | Iustin Pop | instance_data = {} |
4722 | 6286519f | Iustin Pop | for iinfo in i_list: |
4723 | d1c2dd75 | Iustin Pop | nic_data = [{"mac": n.mac, "ip": n.ip, "bridge": n.bridge} |
4724 | d1c2dd75 | Iustin Pop | for n in iinfo.nics] |
4725 | d1c2dd75 | Iustin Pop | pir = { |
4726 | d1c2dd75 | Iustin Pop | "tags": list(iinfo.GetTags()), |
4727 | d1c2dd75 | Iustin Pop | "should_run": iinfo.status == "up", |
4728 | d1c2dd75 | Iustin Pop | "vcpus": iinfo.vcpus,
|
4729 | d1c2dd75 | Iustin Pop | "memory": iinfo.memory,
|
4730 | d1c2dd75 | Iustin Pop | "os": iinfo.os,
|
4731 | d1c2dd75 | Iustin Pop | "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes), |
4732 | d1c2dd75 | Iustin Pop | "nics": nic_data,
|
4733 | d1c2dd75 | Iustin Pop | "disks": [{"size": dsk.size, "mode": "w"} for dsk in iinfo.disks], |
4734 | d1c2dd75 | Iustin Pop | "disk_template": iinfo.disk_template,
|
4735 | d1c2dd75 | Iustin Pop | } |
4736 | 768f0a80 | Iustin Pop | instance_data[iinfo.name] = pir |
4737 | d61df03e | Iustin Pop | |
4738 | d1c2dd75 | Iustin Pop | data["instances"] = instance_data
|
4739 | d61df03e | Iustin Pop | |
4740 | d1c2dd75 | Iustin Pop | self.in_data = data
|
4741 | d61df03e | Iustin Pop | |
4742 | d1c2dd75 | Iustin Pop | def _AddNewInstance(self): |
4743 | d1c2dd75 | Iustin Pop | """Add new instance data to allocator structure.
|
4744 | d61df03e | Iustin Pop |
|
4745 | d1c2dd75 | Iustin Pop | This in combination with _AllocatorGetClusterData will create the
|
4746 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
4747 | d61df03e | Iustin Pop |
|
4748 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
4749 | d1c2dd75 | Iustin Pop | done.
|
4750 | d61df03e | Iustin Pop |
|
4751 | d1c2dd75 | Iustin Pop | """
|
4752 | d1c2dd75 | Iustin Pop | data = self.in_data
|
4753 | d1c2dd75 | Iustin Pop | if len(self.disks) != 2: |
4754 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Only two-disk configurations supported") |
4755 | d1c2dd75 | Iustin Pop | |
4756 | d1c2dd75 | Iustin Pop | disk_space = _ComputeDiskSize(self.disk_template,
|
4757 | d1c2dd75 | Iustin Pop | self.disks[0]["size"], self.disks[1]["size"]) |
4758 | d1c2dd75 | Iustin Pop | |
4759 | 27579978 | Iustin Pop | if self.disk_template in constants.DTS_NET_MIRROR: |
4760 | 27579978 | Iustin Pop | self.required_nodes = 2 |
4761 | 27579978 | Iustin Pop | else:
|
4762 | 27579978 | Iustin Pop | self.required_nodes = 1 |
4763 | d1c2dd75 | Iustin Pop | request = { |
4764 | d1c2dd75 | Iustin Pop | "type": "allocate", |
4765 | d1c2dd75 | Iustin Pop | "name": self.name, |
4766 | d1c2dd75 | Iustin Pop | "disk_template": self.disk_template, |
4767 | d1c2dd75 | Iustin Pop | "tags": self.tags, |
4768 | d1c2dd75 | Iustin Pop | "os": self.os, |
4769 | d1c2dd75 | Iustin Pop | "vcpus": self.vcpus, |
4770 | d1c2dd75 | Iustin Pop | "memory": self.mem_size, |
4771 | d1c2dd75 | Iustin Pop | "disks": self.disks, |
4772 | d1c2dd75 | Iustin Pop | "disk_space_total": disk_space,
|
4773 | d1c2dd75 | Iustin Pop | "nics": self.nics, |
4774 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
4775 | d1c2dd75 | Iustin Pop | } |
4776 | d1c2dd75 | Iustin Pop | data["request"] = request
|
4777 | 298fe380 | Iustin Pop | |
4778 | d1c2dd75 | Iustin Pop | def _AddRelocateInstance(self): |
4779 | d1c2dd75 | Iustin Pop | """Add relocate instance data to allocator structure.
|
4780 | 298fe380 | Iustin Pop |
|
4781 | d1c2dd75 | Iustin Pop | This in combination with _IAllocatorGetClusterData will create the
|
4782 | d1c2dd75 | Iustin Pop | correct structure needed as input for the allocator.
|
4783 | d61df03e | Iustin Pop |
|
4784 | d1c2dd75 | Iustin Pop | The checks for the completeness of the opcode must have already been
|
4785 | d1c2dd75 | Iustin Pop | done.
|
4786 | d61df03e | Iustin Pop |
|
4787 | d1c2dd75 | Iustin Pop | """
|
4788 | 27579978 | Iustin Pop | instance = self.cfg.GetInstanceInfo(self.name) |
4789 | 27579978 | Iustin Pop | if instance is None: |
4790 | 27579978 | Iustin Pop | raise errors.ProgrammerError("Unknown instance '%s' passed to" |
4791 | 27579978 | Iustin Pop | " IAllocator" % self.name) |
4792 | 27579978 | Iustin Pop | |
4793 | 27579978 | Iustin Pop | if instance.disk_template not in constants.DTS_NET_MIRROR: |
4794 | 27579978 | Iustin Pop | raise errors.OpPrereqError("Can't relocate non-mirrored instances") |
4795 | 27579978 | Iustin Pop | |
4796 | 2a139bb0 | Iustin Pop | if len(instance.secondary_nodes) != 1: |
4797 | 2a139bb0 | Iustin Pop | raise errors.OpPrereqError("Instance has not exactly one secondary node") |
4798 | 2a139bb0 | Iustin Pop | |
4799 | 27579978 | Iustin Pop | self.required_nodes = 1 |
4800 | 27579978 | Iustin Pop | |
4801 | 27579978 | Iustin Pop | disk_space = _ComputeDiskSize(instance.disk_template, |
4802 | 27579978 | Iustin Pop | instance.disks[0].size,
|
4803 | 27579978 | Iustin Pop | instance.disks[1].size)
|
4804 | 27579978 | Iustin Pop | |
4805 | d1c2dd75 | Iustin Pop | request = { |
4806 | 2a139bb0 | Iustin Pop | "type": "relocate", |
4807 | d1c2dd75 | Iustin Pop | "name": self.name, |
4808 | 27579978 | Iustin Pop | "disk_space_total": disk_space,
|
4809 | 27579978 | Iustin Pop | "required_nodes": self.required_nodes, |
4810 | 29859cb7 | Iustin Pop | "relocate_from": self.relocate_from, |
4811 | d1c2dd75 | Iustin Pop | } |
4812 | 27579978 | Iustin Pop | self.in_data["request"] = request |
4813 | d61df03e | Iustin Pop | |
4814 | d1c2dd75 | Iustin Pop | def _BuildInputData(self): |
4815 | d1c2dd75 | Iustin Pop | """Build input data structures.
|
4816 | d61df03e | Iustin Pop |
|
4817 | d1c2dd75 | Iustin Pop | """
|
4818 | d1c2dd75 | Iustin Pop | self._ComputeClusterData()
|
4819 | d61df03e | Iustin Pop | |
4820 | d1c2dd75 | Iustin Pop | if self.mode == constants.IALLOCATOR_MODE_ALLOC: |
4821 | d1c2dd75 | Iustin Pop | self._AddNewInstance()
|
4822 | d1c2dd75 | Iustin Pop | else:
|
4823 | d1c2dd75 | Iustin Pop | self._AddRelocateInstance()
|
4824 | d61df03e | Iustin Pop | |
4825 | d1c2dd75 | Iustin Pop | self.in_text = serializer.Dump(self.in_data) |
4826 | d61df03e | Iustin Pop | |
4827 | 8d528b7c | Iustin Pop | def Run(self, name, validate=True, call_fn=rpc.call_iallocator_runner): |
4828 | d1c2dd75 | Iustin Pop | """Run an instance allocator and return the results.
|
4829 | 298fe380 | Iustin Pop |
|
4830 | d1c2dd75 | Iustin Pop | """
|
4831 | d1c2dd75 | Iustin Pop | data = self.in_text
|
4832 | 298fe380 | Iustin Pop | |
4833 | 8d528b7c | Iustin Pop | result = call_fn(self.sstore.GetMasterNode(), name, self.in_text) |
4834 | 298fe380 | Iustin Pop | |
4835 | 8d528b7c | Iustin Pop | if not isinstance(result, tuple) or len(result) != 4: |
4836 | 8d528b7c | Iustin Pop | raise errors.OpExecError("Invalid result from master iallocator runner") |
4837 | 8d528b7c | Iustin Pop | |
4838 | 8d528b7c | Iustin Pop | rcode, stdout, stderr, fail = result |
4839 | 8d528b7c | Iustin Pop | |
4840 | 8d528b7c | Iustin Pop | if rcode == constants.IARUN_NOTFOUND:
|
4841 | 8d528b7c | Iustin Pop | raise errors.OpExecError("Can't find allocator '%s'" % name) |
4842 | 8d528b7c | Iustin Pop | elif rcode == constants.IARUN_FAILURE:
|
4843 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Instance allocator call failed: %s," |
4844 | d1c2dd75 | Iustin Pop | " output: %s" %
|
4845 | 8d528b7c | Iustin Pop | (fail, stdout+stderr)) |
4846 | 8d528b7c | Iustin Pop | self.out_text = stdout
|
4847 | d1c2dd75 | Iustin Pop | if validate:
|
4848 | d1c2dd75 | Iustin Pop | self._ValidateResult()
|
4849 | 298fe380 | Iustin Pop | |
4850 | d1c2dd75 | Iustin Pop | def _ValidateResult(self): |
4851 | d1c2dd75 | Iustin Pop | """Process the allocator results.
|
4852 | 538475ca | Iustin Pop |
|
4853 | d1c2dd75 | Iustin Pop | This will process and if successful save the result in
|
4854 | d1c2dd75 | Iustin Pop | self.out_data and the other parameters.
|
4855 | 538475ca | Iustin Pop |
|
4856 | d1c2dd75 | Iustin Pop | """
|
4857 | d1c2dd75 | Iustin Pop | try:
|
4858 | d1c2dd75 | Iustin Pop | rdict = serializer.Load(self.out_text)
|
4859 | d1c2dd75 | Iustin Pop | except Exception, err: |
4860 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: %s" % str(err)) |
4861 | d1c2dd75 | Iustin Pop | |
4862 | d1c2dd75 | Iustin Pop | if not isinstance(rdict, dict): |
4863 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: not a dict") |
4864 | 538475ca | Iustin Pop | |
4865 | d1c2dd75 | Iustin Pop | for key in "success", "info", "nodes": |
4866 | d1c2dd75 | Iustin Pop | if key not in rdict: |
4867 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results:" |
4868 | d1c2dd75 | Iustin Pop | " missing key '%s'" % key)
|
4869 | d1c2dd75 | Iustin Pop | setattr(self, key, rdict[key]) |
4870 | 538475ca | Iustin Pop | |
4871 | d1c2dd75 | Iustin Pop | if not isinstance(rdict["nodes"], list): |
4872 | d1c2dd75 | Iustin Pop | raise errors.OpExecError("Can't parse iallocator results: 'nodes' key" |
4873 | d1c2dd75 | Iustin Pop | " is not a list")
|
4874 | d1c2dd75 | Iustin Pop | self.out_data = rdict
|
4875 | 538475ca | Iustin Pop | |
4876 | 538475ca | Iustin Pop | |
4877 | d61df03e | Iustin Pop | class LUTestAllocator(NoHooksLU): |
4878 | d61df03e | Iustin Pop | """Run allocator tests.
|
4879 | d61df03e | Iustin Pop |
|
4880 | d61df03e | Iustin Pop | This LU runs the allocator tests
|
4881 | d61df03e | Iustin Pop |
|
4882 | d61df03e | Iustin Pop | """
|
4883 | d61df03e | Iustin Pop | _OP_REQP = ["direction", "mode", "name"] |
4884 | d61df03e | Iustin Pop | |
4885 | d61df03e | Iustin Pop | def CheckPrereq(self): |
4886 | d61df03e | Iustin Pop | """Check prerequisites.
|
4887 | d61df03e | Iustin Pop |
|
4888 | d61df03e | Iustin Pop | This checks the opcode parameters depending on the director and mode test.
|
4889 | d61df03e | Iustin Pop |
|
4890 | d61df03e | Iustin Pop | """
|
4891 | 298fe380 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
4892 | d61df03e | Iustin Pop | for attr in ["name", "mem_size", "disks", "disk_template", |
4893 | d61df03e | Iustin Pop | "os", "tags", "nics", "vcpus"]: |
4894 | d61df03e | Iustin Pop | if not hasattr(self.op, attr): |
4895 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing attribute '%s' on opcode input" % |
4896 | d61df03e | Iustin Pop | attr) |
4897 | d61df03e | Iustin Pop | iname = self.cfg.ExpandInstanceName(self.op.name) |
4898 | d61df03e | Iustin Pop | if iname is not None: |
4899 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Instance '%s' already in the cluster" % |
4900 | d61df03e | Iustin Pop | iname) |
4901 | d61df03e | Iustin Pop | if not isinstance(self.op.nics, list): |
4902 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'nics'") |
4903 | d61df03e | Iustin Pop | for row in self.op.nics: |
4904 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
4905 | d61df03e | Iustin Pop | "mac" not in row or |
4906 | d61df03e | Iustin Pop | "ip" not in row or |
4907 | d61df03e | Iustin Pop | "bridge" not in row): |
4908 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the" |
4909 | d61df03e | Iustin Pop | " 'nics' parameter")
|
4910 | d61df03e | Iustin Pop | if not isinstance(self.op.disks, list): |
4911 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid parameter 'disks'") |
4912 | 298fe380 | Iustin Pop | if len(self.op.disks) != 2: |
4913 | 298fe380 | Iustin Pop | raise errors.OpPrereqError("Only two-disk configurations supported") |
4914 | d61df03e | Iustin Pop | for row in self.op.disks: |
4915 | d61df03e | Iustin Pop | if (not isinstance(row, dict) or |
4916 | d61df03e | Iustin Pop | "size" not in row or |
4917 | d61df03e | Iustin Pop | not isinstance(row["size"], int) or |
4918 | d61df03e | Iustin Pop | "mode" not in row or |
4919 | d61df03e | Iustin Pop | row["mode"] not in ['r', 'w']): |
4920 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid contents of the" |
4921 | d61df03e | Iustin Pop | " 'disks' parameter")
|
4922 | 298fe380 | Iustin Pop | elif self.op.mode == constants.IALLOCATOR_MODE_RELOC: |
4923 | d61df03e | Iustin Pop | if not hasattr(self.op, "name"): |
4924 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing attribute 'name' on opcode input") |
4925 | d61df03e | Iustin Pop | fname = self.cfg.ExpandInstanceName(self.op.name) |
4926 | d61df03e | Iustin Pop | if fname is None: |
4927 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Instance '%s' not found for relocation" % |
4928 | d61df03e | Iustin Pop | self.op.name)
|
4929 | d61df03e | Iustin Pop | self.op.name = fname
|
4930 | 29859cb7 | Iustin Pop | self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes |
4931 | d61df03e | Iustin Pop | else:
|
4932 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Invalid test allocator mode '%s'" % |
4933 | d61df03e | Iustin Pop | self.op.mode)
|
4934 | d61df03e | Iustin Pop | |
4935 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_OUT: |
4936 | 298fe380 | Iustin Pop | if not hasattr(self.op, "allocator") or self.op.allocator is None: |
4937 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Missing allocator name") |
4938 | 298fe380 | Iustin Pop | elif self.op.direction != constants.IALLOCATOR_DIR_IN: |
4939 | d61df03e | Iustin Pop | raise errors.OpPrereqError("Wrong allocator test '%s'" % |
4940 | d61df03e | Iustin Pop | self.op.direction)
|
4941 | d61df03e | Iustin Pop | |
4942 | d61df03e | Iustin Pop | def Exec(self, feedback_fn): |
4943 | d61df03e | Iustin Pop | """Run the allocator test.
|
4944 | d61df03e | Iustin Pop |
|
4945 | d61df03e | Iustin Pop | """
|
4946 | 29859cb7 | Iustin Pop | if self.op.mode == constants.IALLOCATOR_MODE_ALLOC: |
4947 | 29859cb7 | Iustin Pop | ial = IAllocator(self.cfg, self.sstore, |
4948 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
4949 | 29859cb7 | Iustin Pop | name=self.op.name,
|
4950 | 29859cb7 | Iustin Pop | mem_size=self.op.mem_size,
|
4951 | 29859cb7 | Iustin Pop | disks=self.op.disks,
|
4952 | 29859cb7 | Iustin Pop | disk_template=self.op.disk_template,
|
4953 | 29859cb7 | Iustin Pop | os=self.op.os,
|
4954 | 29859cb7 | Iustin Pop | tags=self.op.tags,
|
4955 | 29859cb7 | Iustin Pop | nics=self.op.nics,
|
4956 | 29859cb7 | Iustin Pop | vcpus=self.op.vcpus,
|
4957 | 29859cb7 | Iustin Pop | ) |
4958 | 29859cb7 | Iustin Pop | else:
|
4959 | 29859cb7 | Iustin Pop | ial = IAllocator(self.cfg, self.sstore, |
4960 | 29859cb7 | Iustin Pop | mode=self.op.mode,
|
4961 | 29859cb7 | Iustin Pop | name=self.op.name,
|
4962 | 29859cb7 | Iustin Pop | relocate_from=list(self.relocate_from), |
4963 | 29859cb7 | Iustin Pop | ) |
4964 | d61df03e | Iustin Pop | |
4965 | 298fe380 | Iustin Pop | if self.op.direction == constants.IALLOCATOR_DIR_IN: |
4966 | d1c2dd75 | Iustin Pop | result = ial.in_text |
4967 | 298fe380 | Iustin Pop | else:
|
4968 | d1c2dd75 | Iustin Pop | ial.Run(self.op.allocator, validate=False) |
4969 | d1c2dd75 | Iustin Pop | result = ial.out_text |
4970 | 298fe380 | Iustin Pop | return result |