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