root / lib / cmdlib / instance_operation.py @ 4e7f986e
History | View | Annotate | Download (17.5 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Logical units dealing with instance operations (start/stop/...).
|
23 |
|
24 |
Those operations have in common that they affect the operating system in a
|
25 |
running instance directly.
|
26 |
|
27 |
"""
|
28 |
|
29 |
import logging |
30 |
|
31 |
from ganeti import constants |
32 |
from ganeti import errors |
33 |
from ganeti import hypervisor |
34 |
from ganeti import locking |
35 |
from ganeti import objects |
36 |
from ganeti import utils |
37 |
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU |
38 |
from ganeti.cmdlib.common import INSTANCE_ONLINE, INSTANCE_DOWN, \ |
39 |
CheckHVParams, CheckInstanceState, CheckNodeOnline, GetUpdatedParams, \ |
40 |
CheckOSParams, ShareAll |
41 |
from ganeti.cmdlib.instance_storage import StartInstanceDisks, \ |
42 |
ShutdownInstanceDisks
|
43 |
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \ |
44 |
CheckInstanceBridgesExist, CheckNodeFreeMemory, CheckNodeHasOS |
45 |
from ganeti.hypervisor import hv_base |
46 |
|
47 |
|
48 |
class LUInstanceStartup(LogicalUnit): |
49 |
"""Starts an instance.
|
50 |
|
51 |
"""
|
52 |
HPATH = "instance-start"
|
53 |
HTYPE = constants.HTYPE_INSTANCE |
54 |
REQ_BGL = False
|
55 |
|
56 |
def CheckArguments(self): |
57 |
# extra beparams
|
58 |
if self.op.beparams: |
59 |
# fill the beparams dict
|
60 |
objects.UpgradeBeParams(self.op.beparams)
|
61 |
utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
|
62 |
|
63 |
def ExpandNames(self): |
64 |
self._ExpandAndLockInstance()
|
65 |
self.recalculate_locks[locking.LEVEL_NODE_RES] = constants.LOCKS_REPLACE
|
66 |
|
67 |
def DeclareLocks(self, level): |
68 |
if level == locking.LEVEL_NODE_RES:
|
69 |
self._LockInstancesNodes(primary_only=True, level=locking.LEVEL_NODE_RES) |
70 |
|
71 |
def BuildHooksEnv(self): |
72 |
"""Build hooks env.
|
73 |
|
74 |
This runs on master, primary and secondary nodes of the instance.
|
75 |
|
76 |
"""
|
77 |
env = { |
78 |
"FORCE": self.op.force, |
79 |
} |
80 |
|
81 |
env.update(BuildInstanceHookEnvByObject(self, self.instance)) |
82 |
|
83 |
return env
|
84 |
|
85 |
def BuildHooksNodes(self): |
86 |
"""Build hooks nodes.
|
87 |
|
88 |
"""
|
89 |
nl = [self.cfg.GetMasterNode()] + \
|
90 |
list(self.cfg.GetInstanceNodes(self.instance)) |
91 |
return (nl, nl)
|
92 |
|
93 |
def CheckPrereq(self): |
94 |
"""Check prerequisites.
|
95 |
|
96 |
This checks that the instance is in the cluster.
|
97 |
|
98 |
"""
|
99 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
100 |
assert self.instance is not None, \ |
101 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
102 |
|
103 |
cluster = self.cfg.GetClusterInfo()
|
104 |
# extra hvparams
|
105 |
if self.op.hvparams: |
106 |
# check hypervisor parameter syntax (locally)
|
107 |
utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
|
108 |
filled_hvp = cluster.FillHV(self.instance)
|
109 |
filled_hvp.update(self.op.hvparams)
|
110 |
hv_type = hypervisor.GetHypervisorClass(self.instance.hypervisor)
|
111 |
hv_type.CheckParameterSyntax(filled_hvp) |
112 |
CheckHVParams(self, self.cfg.GetInstanceNodes(self.instance), |
113 |
self.instance.hypervisor, filled_hvp)
|
114 |
|
115 |
CheckInstanceState(self, self.instance, INSTANCE_ONLINE) |
116 |
|
117 |
self.primary_offline = \
|
118 |
self.cfg.GetNodeInfo(self.instance.primary_node).offline |
119 |
|
120 |
if self.primary_offline and self.op.ignore_offline_nodes: |
121 |
self.LogWarning("Ignoring offline primary node") |
122 |
|
123 |
if self.op.hvparams or self.op.beparams: |
124 |
self.LogWarning("Overridden parameters are ignored") |
125 |
else:
|
126 |
CheckNodeOnline(self, self.instance.primary_node) |
127 |
|
128 |
bep = self.cfg.GetClusterInfo().FillBE(self.instance) |
129 |
bep.update(self.op.beparams)
|
130 |
|
131 |
# check bridges existence
|
132 |
CheckInstanceBridgesExist(self, self.instance) |
133 |
|
134 |
remote_info = self.rpc.call_instance_info(
|
135 |
self.instance.primary_node, self.instance.name, |
136 |
self.instance.hypervisor, cluster.hvparams[self.instance.hypervisor]) |
137 |
remote_info.Raise("Error checking node %s" %
|
138 |
self.cfg.GetNodeName(self.instance.primary_node), |
139 |
prereq=True, ecode=errors.ECODE_ENVIRON)
|
140 |
if remote_info.payload:
|
141 |
if hv_base.HvInstanceState.IsShutdown(remote_info.payload["state"]): |
142 |
raise errors.OpPrereqError("Instance '%s' was shutdown by the user," |
143 |
" please shutdown the instance before"
|
144 |
" starting it again" % self.instance.name, |
145 |
errors.ECODE_INVAL) |
146 |
else: # not running already |
147 |
CheckNodeFreeMemory( |
148 |
self, self.instance.primary_node, |
149 |
"starting instance %s" % self.instance.name, |
150 |
bep[constants.BE_MINMEM], self.instance.hypervisor,
|
151 |
self.cfg.GetClusterInfo().hvparams[self.instance.hypervisor]) |
152 |
|
153 |
def Exec(self, feedback_fn): |
154 |
"""Start the instance.
|
155 |
|
156 |
"""
|
157 |
if not self.op.no_remember: |
158 |
self.cfg.MarkInstanceUp(self.instance.uuid) |
159 |
|
160 |
if self.primary_offline: |
161 |
assert self.op.ignore_offline_nodes |
162 |
self.LogInfo("Primary node offline, marked instance as started") |
163 |
else:
|
164 |
StartInstanceDisks(self, self.instance, self.op.force) |
165 |
|
166 |
result = \ |
167 |
self.rpc.call_instance_start(self.instance.primary_node, |
168 |
(self.instance, self.op.hvparams, |
169 |
self.op.beparams),
|
170 |
self.op.startup_paused, self.op.reason) |
171 |
msg = result.fail_msg |
172 |
if msg:
|
173 |
ShutdownInstanceDisks(self, self.instance) |
174 |
raise errors.OpExecError("Could not start instance: %s" % msg) |
175 |
|
176 |
|
177 |
class LUInstanceShutdown(LogicalUnit): |
178 |
"""Shutdown an instance.
|
179 |
|
180 |
"""
|
181 |
HPATH = "instance-stop"
|
182 |
HTYPE = constants.HTYPE_INSTANCE |
183 |
REQ_BGL = False
|
184 |
|
185 |
def ExpandNames(self): |
186 |
self._ExpandAndLockInstance()
|
187 |
|
188 |
def BuildHooksEnv(self): |
189 |
"""Build hooks env.
|
190 |
|
191 |
This runs on master, primary and secondary nodes of the instance.
|
192 |
|
193 |
"""
|
194 |
env = BuildInstanceHookEnvByObject(self, self.instance) |
195 |
env["TIMEOUT"] = self.op.timeout |
196 |
return env
|
197 |
|
198 |
def BuildHooksNodes(self): |
199 |
"""Build hooks nodes.
|
200 |
|
201 |
"""
|
202 |
nl = [self.cfg.GetMasterNode()] + \
|
203 |
list(self.cfg.GetInstanceNodes(self.instance)) |
204 |
return (nl, nl)
|
205 |
|
206 |
def CheckPrereq(self): |
207 |
"""Check prerequisites.
|
208 |
|
209 |
This checks that the instance is in the cluster.
|
210 |
|
211 |
"""
|
212 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
213 |
assert self.instance is not None, \ |
214 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
215 |
|
216 |
if not self.op.force: |
217 |
CheckInstanceState(self, self.instance, INSTANCE_ONLINE) |
218 |
else:
|
219 |
self.LogWarning("Ignoring offline instance check") |
220 |
|
221 |
self.primary_offline = \
|
222 |
self.cfg.GetNodeInfo(self.instance.primary_node).offline |
223 |
|
224 |
if self.primary_offline and self.op.ignore_offline_nodes: |
225 |
self.LogWarning("Ignoring offline primary node") |
226 |
else:
|
227 |
CheckNodeOnline(self, self.instance.primary_node) |
228 |
|
229 |
def Exec(self, feedback_fn): |
230 |
"""Shutdown the instance.
|
231 |
|
232 |
"""
|
233 |
# If the instance is offline we shouldn't mark it as down, as that
|
234 |
# resets the offline flag.
|
235 |
if not self.op.no_remember and self.instance.admin_state in INSTANCE_ONLINE: |
236 |
self.cfg.MarkInstanceDown(self.instance.uuid) |
237 |
|
238 |
if self.primary_offline: |
239 |
assert self.op.ignore_offline_nodes |
240 |
self.LogInfo("Primary node offline, marked instance as stopped") |
241 |
else:
|
242 |
result = self.rpc.call_instance_shutdown(
|
243 |
self.instance.primary_node,
|
244 |
self.instance,
|
245 |
self.op.timeout, self.op.reason) |
246 |
msg = result.fail_msg |
247 |
if msg:
|
248 |
self.LogWarning("Could not shutdown instance: %s", msg) |
249 |
|
250 |
ShutdownInstanceDisks(self, self.instance) |
251 |
|
252 |
|
253 |
class LUInstanceReinstall(LogicalUnit): |
254 |
"""Reinstall an instance.
|
255 |
|
256 |
"""
|
257 |
HPATH = "instance-reinstall"
|
258 |
HTYPE = constants.HTYPE_INSTANCE |
259 |
REQ_BGL = False
|
260 |
|
261 |
def ExpandNames(self): |
262 |
self._ExpandAndLockInstance()
|
263 |
|
264 |
def BuildHooksEnv(self): |
265 |
"""Build hooks env.
|
266 |
|
267 |
This runs on master, primary and secondary nodes of the instance.
|
268 |
|
269 |
"""
|
270 |
return BuildInstanceHookEnvByObject(self, self.instance) |
271 |
|
272 |
def BuildHooksNodes(self): |
273 |
"""Build hooks nodes.
|
274 |
|
275 |
"""
|
276 |
nl = [self.cfg.GetMasterNode()] + \
|
277 |
list(self.cfg.GetInstanceNodes(self.instance)) |
278 |
return (nl, nl)
|
279 |
|
280 |
def CheckPrereq(self): |
281 |
"""Check prerequisites.
|
282 |
|
283 |
This checks that the instance is in the cluster and is not running.
|
284 |
|
285 |
"""
|
286 |
instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
287 |
assert instance is not None, \ |
288 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
289 |
CheckNodeOnline(self, instance.primary_node, "Instance primary node" |
290 |
" offline, cannot reinstall")
|
291 |
|
292 |
if instance.disk_template == constants.DT_DISKLESS:
|
293 |
raise errors.OpPrereqError("Instance '%s' has no disks" % |
294 |
self.op.instance_name,
|
295 |
errors.ECODE_INVAL) |
296 |
CheckInstanceState(self, instance, INSTANCE_DOWN, msg="cannot reinstall") |
297 |
|
298 |
# Handle OS parameters
|
299 |
self._MergeValidateOsParams(instance)
|
300 |
|
301 |
self.instance = instance
|
302 |
|
303 |
def _MergeValidateOsParams(self, instance): |
304 |
"Handle the OS parameter merging and validation for the target instance."
|
305 |
|
306 |
if self.op.os_type is not None: |
307 |
# OS verification
|
308 |
CheckNodeHasOS(self, instance.primary_node, self.op.os_type, |
309 |
self.op.force_variant)
|
310 |
instance_os = self.op.os_type
|
311 |
else:
|
312 |
instance_os = instance.os |
313 |
|
314 |
node_uuids = list(self.cfg.GetInstanceNodes(instance)) |
315 |
|
316 |
self.op.osparams = self.op.osparams or {} |
317 |
self.op.osparams_private = self.op.osparams_private or {} |
318 |
self.op.osparams_secret = self.op.osparams_secret or {} |
319 |
|
320 |
# Handle the use of 'default' values.
|
321 |
params_public = GetUpdatedParams(instance.osparams, self.op.osparams)
|
322 |
params_private = GetUpdatedParams(instance.osparams_private, |
323 |
self.op.osparams_private)
|
324 |
params_secret = self.op.osparams_secret
|
325 |
|
326 |
cluster = self.cfg.GetClusterInfo()
|
327 |
self.os_inst = cluster.SimpleFillOS(
|
328 |
instance_os, |
329 |
params_public, |
330 |
os_params_private=params_private, |
331 |
os_params_secret=params_secret |
332 |
) |
333 |
|
334 |
CheckOSParams(self, True, node_uuids, instance_os, self.os_inst) |
335 |
|
336 |
def Exec(self, feedback_fn): |
337 |
"""Reinstall the instance.
|
338 |
|
339 |
"""
|
340 |
if self.op.os_type is not None: |
341 |
feedback_fn("Changing OS to '%s'..." % self.op.os_type) |
342 |
self.instance.os = self.op.os_type |
343 |
# Write to configuration
|
344 |
self.cfg.Update(self.instance, feedback_fn) |
345 |
|
346 |
StartInstanceDisks(self, self.instance, None) |
347 |
try:
|
348 |
feedback_fn("Running the instance OS create scripts...")
|
349 |
# FIXME: pass debug option from opcode to backend
|
350 |
result = self.rpc.call_instance_os_add(self.instance.primary_node, |
351 |
(self.instance, self.os_inst), |
352 |
True, self.op.debug_level) |
353 |
result.Raise("Could not install OS for instance %s on node %s" %
|
354 |
(self.instance.name,
|
355 |
self.cfg.GetNodeName(self.instance.primary_node))) |
356 |
finally:
|
357 |
ShutdownInstanceDisks(self, self.instance) |
358 |
|
359 |
|
360 |
class LUInstanceReboot(LogicalUnit): |
361 |
"""Reboot an instance.
|
362 |
|
363 |
"""
|
364 |
HPATH = "instance-reboot"
|
365 |
HTYPE = constants.HTYPE_INSTANCE |
366 |
REQ_BGL = False
|
367 |
|
368 |
def ExpandNames(self): |
369 |
self._ExpandAndLockInstance()
|
370 |
|
371 |
def BuildHooksEnv(self): |
372 |
"""Build hooks env.
|
373 |
|
374 |
This runs on master, primary and secondary nodes of the instance.
|
375 |
|
376 |
"""
|
377 |
env = { |
378 |
"IGNORE_SECONDARIES": self.op.ignore_secondaries, |
379 |
"REBOOT_TYPE": self.op.reboot_type, |
380 |
"SHUTDOWN_TIMEOUT": self.op.shutdown_timeout, |
381 |
} |
382 |
|
383 |
env.update(BuildInstanceHookEnvByObject(self, self.instance)) |
384 |
|
385 |
return env
|
386 |
|
387 |
def BuildHooksNodes(self): |
388 |
"""Build hooks nodes.
|
389 |
|
390 |
"""
|
391 |
nl = [self.cfg.GetMasterNode()] + \
|
392 |
list(self.cfg.GetInstanceNodes(self.instance)) |
393 |
return (nl, nl)
|
394 |
|
395 |
def CheckPrereq(self): |
396 |
"""Check prerequisites.
|
397 |
|
398 |
This checks that the instance is in the cluster.
|
399 |
|
400 |
"""
|
401 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
402 |
assert self.instance is not None, \ |
403 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
404 |
CheckInstanceState(self, self.instance, INSTANCE_ONLINE) |
405 |
CheckNodeOnline(self, self.instance.primary_node) |
406 |
|
407 |
# check bridges existence
|
408 |
CheckInstanceBridgesExist(self, self.instance) |
409 |
|
410 |
def Exec(self, feedback_fn): |
411 |
"""Reboot the instance.
|
412 |
|
413 |
"""
|
414 |
cluster = self.cfg.GetClusterInfo()
|
415 |
remote_info = self.rpc.call_instance_info(
|
416 |
self.instance.primary_node, self.instance.name, |
417 |
self.instance.hypervisor, cluster.hvparams[self.instance.hypervisor]) |
418 |
remote_info.Raise("Error checking node %s" %
|
419 |
self.cfg.GetNodeName(self.instance.primary_node)) |
420 |
instance_running = bool(remote_info.payload)
|
421 |
|
422 |
current_node_uuid = self.instance.primary_node
|
423 |
|
424 |
if instance_running and \ |
425 |
self.op.reboot_type in [constants.INSTANCE_REBOOT_SOFT, |
426 |
constants.INSTANCE_REBOOT_HARD]: |
427 |
result = self.rpc.call_instance_reboot(current_node_uuid, self.instance, |
428 |
self.op.reboot_type,
|
429 |
self.op.shutdown_timeout,
|
430 |
self.op.reason)
|
431 |
result.Raise("Could not reboot instance")
|
432 |
else:
|
433 |
if instance_running:
|
434 |
result = self.rpc.call_instance_shutdown(current_node_uuid,
|
435 |
self.instance,
|
436 |
self.op.shutdown_timeout,
|
437 |
self.op.reason)
|
438 |
result.Raise("Could not shutdown instance for full reboot")
|
439 |
ShutdownInstanceDisks(self, self.instance) |
440 |
else:
|
441 |
self.LogInfo("Instance %s was already stopped, starting now", |
442 |
self.instance.name)
|
443 |
StartInstanceDisks(self, self.instance, self.op.ignore_secondaries) |
444 |
result = self.rpc.call_instance_start(current_node_uuid,
|
445 |
(self.instance, None, None), False, |
446 |
self.op.reason)
|
447 |
msg = result.fail_msg |
448 |
if msg:
|
449 |
ShutdownInstanceDisks(self, self.instance) |
450 |
raise errors.OpExecError("Could not start instance for" |
451 |
" full reboot: %s" % msg)
|
452 |
|
453 |
self.cfg.MarkInstanceUp(self.instance.uuid) |
454 |
|
455 |
|
456 |
def GetInstanceConsole(cluster, instance, primary_node, node_group): |
457 |
"""Returns console information for an instance.
|
458 |
|
459 |
@type cluster: L{objects.Cluster}
|
460 |
@type instance: L{objects.Instance}
|
461 |
@type primary_node: L{objects.Node}
|
462 |
@type node_group: L{objects.NodeGroup}
|
463 |
@rtype: dict
|
464 |
|
465 |
"""
|
466 |
hyper = hypervisor.GetHypervisorClass(instance.hypervisor) |
467 |
# beparams and hvparams are passed separately, to avoid editing the
|
468 |
# instance and then saving the defaults in the instance itself.
|
469 |
hvparams = cluster.FillHV(instance) |
470 |
beparams = cluster.FillBE(instance) |
471 |
console = hyper.GetInstanceConsole(instance, primary_node, node_group, |
472 |
hvparams, beparams) |
473 |
|
474 |
assert console.instance == instance.name
|
475 |
assert console.Validate()
|
476 |
|
477 |
return console.ToDict()
|
478 |
|
479 |
|
480 |
class LUInstanceConsole(NoHooksLU): |
481 |
"""Connect to an instance's console.
|
482 |
|
483 |
This is somewhat special in that it returns the command line that
|
484 |
you need to run on the master node in order to connect to the
|
485 |
console.
|
486 |
|
487 |
"""
|
488 |
REQ_BGL = False
|
489 |
|
490 |
def ExpandNames(self): |
491 |
self.share_locks = ShareAll()
|
492 |
self._ExpandAndLockInstance()
|
493 |
|
494 |
def CheckPrereq(self): |
495 |
"""Check prerequisites.
|
496 |
|
497 |
This checks that the instance is in the cluster.
|
498 |
|
499 |
"""
|
500 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
501 |
assert self.instance is not None, \ |
502 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
503 |
CheckNodeOnline(self, self.instance.primary_node) |
504 |
|
505 |
def Exec(self, feedback_fn): |
506 |
"""Connect to the console of an instance
|
507 |
|
508 |
"""
|
509 |
node_uuid = self.instance.primary_node
|
510 |
|
511 |
cluster_hvparams = self.cfg.GetClusterInfo().hvparams
|
512 |
node_insts = self.rpc.call_instance_list(
|
513 |
[node_uuid], [self.instance.hypervisor],
|
514 |
cluster_hvparams)[node_uuid] |
515 |
node_insts.Raise("Can't get node information from %s" %
|
516 |
self.cfg.GetNodeName(node_uuid))
|
517 |
|
518 |
if self.instance.name not in node_insts.payload: |
519 |
if self.instance.admin_state == constants.ADMINST_UP: |
520 |
state = constants.INSTST_ERRORDOWN |
521 |
elif self.instance.admin_state == constants.ADMINST_DOWN: |
522 |
state = constants.INSTST_ADMINDOWN |
523 |
else:
|
524 |
state = constants.INSTST_ADMINOFFLINE |
525 |
raise errors.OpExecError("Instance %s is not running (state %s)" % |
526 |
(self.instance.name, state))
|
527 |
|
528 |
logging.debug("Connecting to console of %s on %s", self.instance.name, |
529 |
self.cfg.GetNodeName(node_uuid))
|
530 |
|
531 |
node = self.cfg.GetNodeInfo(self.instance.primary_node) |
532 |
group = self.cfg.GetNodeGroup(node.group)
|
533 |
return GetInstanceConsole(self.cfg.GetClusterInfo(), |
534 |
self.instance, node, group)
|