Statistics
| Branch: | Tag: | Revision:

root / lib / mcpu.py @ 73086975

History | View | Annotate | Download (13.8 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 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 a8083063 Iustin Pop
"""Module implementing the logic behind the cluster operations
23 a8083063 Iustin Pop

24 a8083063 Iustin Pop
This module implements the logic for doing operations in the cluster. There
25 a8083063 Iustin Pop
are two kinds of classes defined:
26 a8083063 Iustin Pop
  - logical units, which know how to deal with their specific opcode only
27 a8083063 Iustin Pop
  - the processor, which dispatches the opcodes to their logical units
28 a8083063 Iustin Pop

29 a8083063 Iustin Pop
"""
30 a8083063 Iustin Pop
31 a5eb7789 Iustin Pop
import logging
32 a8083063 Iustin Pop
33 a8083063 Iustin Pop
from ganeti import opcodes
34 a8083063 Iustin Pop
from ganeti import constants
35 a8083063 Iustin Pop
from ganeti import errors
36 a8083063 Iustin Pop
from ganeti import rpc
37 a8083063 Iustin Pop
from ganeti import cmdlib
38 04864530 Guido Trotter
from ganeti import locking
39 a8083063 Iustin Pop
40 7c0d6283 Michael Hanselmann
41 a8083063 Iustin Pop
class Processor(object):
42 a8083063 Iustin Pop
  """Object which runs OpCodes"""
43 a8083063 Iustin Pop
  DISPATCH_TABLE = {
44 a8083063 Iustin Pop
    # Cluster
45 b5f5fae9 Luca Bigliardi
    opcodes.OpPostInitCluster: cmdlib.LUPostInitCluster,
46 a8083063 Iustin Pop
    opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
47 a8083063 Iustin Pop
    opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
48 a8083063 Iustin Pop
    opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
49 ae5849b5 Michael Hanselmann
    opcodes.OpQueryConfigValues: cmdlib.LUQueryConfigValues,
50 07bd8a51 Iustin Pop
    opcodes.OpRenameCluster: cmdlib.LURenameCluster,
51 f4d4e184 Iustin Pop
    opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
52 0cc05d44 Manuel Franceschini
    opcodes.OpSetClusterParams: cmdlib.LUSetClusterParams,
53 afee0879 Iustin Pop
    opcodes.OpRedistributeConfig: cmdlib.LURedistributeConfig,
54 60975797 Iustin Pop
    opcodes.OpRepairDiskSizes: cmdlib.LURepairDiskSizes,
55 a8083063 Iustin Pop
    # node lu
56 a8083063 Iustin Pop
    opcodes.OpAddNode: cmdlib.LUAddNode,
57 a8083063 Iustin Pop
    opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
58 dcb93971 Michael Hanselmann
    opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
59 9e5442ce Michael Hanselmann
    opcodes.OpQueryNodeStorage: cmdlib.LUQueryNodeStorage,
60 efb8da02 Michael Hanselmann
    opcodes.OpModifyNodeStorage: cmdlib.LUModifyNodeStorage,
61 a8083063 Iustin Pop
    opcodes.OpRemoveNode: cmdlib.LURemoveNode,
62 b31c8676 Iustin Pop
    opcodes.OpSetNodeParams: cmdlib.LUSetNodeParams,
63 f5118ade Iustin Pop
    opcodes.OpPowercycleNode: cmdlib.LUPowercycleNode,
64 7ffc5a86 Michael Hanselmann
    opcodes.OpEvacuateNode: cmdlib.LUEvacuateNode,
65 80cb875c Michael Hanselmann
    opcodes.OpMigrateNode: cmdlib.LUMigrateNode,
66 a8083063 Iustin Pop
    # instance lu
67 a8083063 Iustin Pop
    opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
68 fe7b0351 Michael Hanselmann
    opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
69 a8083063 Iustin Pop
    opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
70 decd5f45 Iustin Pop
    opcodes.OpRenameInstance: cmdlib.LURenameInstance,
71 a8083063 Iustin Pop
    opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
72 a8083063 Iustin Pop
    opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
73 a8083063 Iustin Pop
    opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
74 bf6929a2 Alexander Schreiber
    opcodes.OpRebootInstance: cmdlib.LURebootInstance,
75 a8083063 Iustin Pop
    opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
76 a8083063 Iustin Pop
    opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
77 a8083063 Iustin Pop
    opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
78 53c776b5 Iustin Pop
    opcodes.OpMigrateInstance: cmdlib.LUMigrateInstance,
79 a8083063 Iustin Pop
    opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
80 a8083063 Iustin Pop
    opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
81 a8083063 Iustin Pop
    opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
82 7767bbf5 Manuel Franceschini
    opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
83 8729e0d7 Iustin Pop
    opcodes.OpGrowDisk: cmdlib.LUGrowDisk,
84 a8083063 Iustin Pop
    # os lu
85 a8083063 Iustin Pop
    opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
86 a8083063 Iustin Pop
    # exports lu
87 a8083063 Iustin Pop
    opcodes.OpQueryExports: cmdlib.LUQueryExports,
88 a8083063 Iustin Pop
    opcodes.OpExportInstance: cmdlib.LUExportInstance,
89 9ac99fda Guido Trotter
    opcodes.OpRemoveExport: cmdlib.LURemoveExport,
90 5c947f38 Iustin Pop
    # tags lu
91 5c947f38 Iustin Pop
    opcodes.OpGetTags: cmdlib.LUGetTags,
92 73415719 Iustin Pop
    opcodes.OpSearchTags: cmdlib.LUSearchTags,
93 f27302fa Iustin Pop
    opcodes.OpAddTags: cmdlib.LUAddTags,
94 f27302fa Iustin Pop
    opcodes.OpDelTags: cmdlib.LUDelTags,
95 06009e27 Iustin Pop
    # test lu
96 06009e27 Iustin Pop
    opcodes.OpTestDelay: cmdlib.LUTestDelay,
97 d61df03e Iustin Pop
    opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
98 a8083063 Iustin Pop
    }
99 a8083063 Iustin Pop
100 f1048938 Iustin Pop
  def __init__(self, context):
101 a8083063 Iustin Pop
    """Constructor for Processor
102 a8083063 Iustin Pop

103 1a8c0ce1 Iustin Pop
    Args:
104 1a8c0ce1 Iustin Pop
     - feedback_fn: the feedback function (taking one string) to be run when
105 1a8c0ce1 Iustin Pop
                    interesting events are happening
106 a8083063 Iustin Pop
    """
107 1c901d13 Guido Trotter
    self.context = context
108 f1048938 Iustin Pop
    self._feedback_fn = None
109 04864530 Guido Trotter
    self.exclusive_BGL = False
110 72737a7f Iustin Pop
    self.rpc = rpc.RpcRunner(context.cfg)
111 a8083063 Iustin Pop
112 36c381d7 Guido Trotter
  def _ExecLU(self, lu):
113 36c381d7 Guido Trotter
    """Logical Unit execution sequence.
114 36c381d7 Guido Trotter

115 36c381d7 Guido Trotter
    """
116 36c381d7 Guido Trotter
    write_count = self.context.cfg.write_count
117 36c381d7 Guido Trotter
    lu.CheckPrereq()
118 72737a7f Iustin Pop
    hm = HooksMaster(self.rpc.call_hooks_runner, self, lu)
119 36c381d7 Guido Trotter
    h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
120 36c381d7 Guido Trotter
    lu.HooksCallBack(constants.HOOKS_PHASE_PRE, h_results,
121 36c381d7 Guido Trotter
                     self._feedback_fn, None)
122 20777413 Iustin Pop
123 20777413 Iustin Pop
    if getattr(lu.op, "dry_run", False):
124 20777413 Iustin Pop
      # in this mode, no post-hooks are run, and the config is not
125 20777413 Iustin Pop
      # written (as it might have been modified by another LU, and we
126 20777413 Iustin Pop
      # shouldn't do writeout on behalf of other threads
127 20777413 Iustin Pop
      self.LogInfo("dry-run mode requested, not actually executing"
128 20777413 Iustin Pop
                   " the operation")
129 20777413 Iustin Pop
      return lu.dry_run_result
130 20777413 Iustin Pop
131 36c381d7 Guido Trotter
    try:
132 36c381d7 Guido Trotter
      result = lu.Exec(self._feedback_fn)
133 36c381d7 Guido Trotter
      h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
134 36c381d7 Guido Trotter
      result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results,
135 36c381d7 Guido Trotter
                                self._feedback_fn, result)
136 36c381d7 Guido Trotter
    finally:
137 36c381d7 Guido Trotter
      # FIXME: This needs locks if not lu_class.REQ_BGL
138 36c381d7 Guido Trotter
      if write_count != self.context.cfg.write_count:
139 36c381d7 Guido Trotter
        hm.RunConfigUpdate()
140 36c381d7 Guido Trotter
141 36c381d7 Guido Trotter
    return result
142 36c381d7 Guido Trotter
143 68adfdb2 Guido Trotter
  def _LockAndExecLU(self, lu, level):
144 68adfdb2 Guido Trotter
    """Execute a Logical Unit, with the needed locks.
145 68adfdb2 Guido Trotter

146 68adfdb2 Guido Trotter
    This is a recursive function that starts locking the given level, and
147 68adfdb2 Guido Trotter
    proceeds up, till there are no more locks to acquire. Then it executes the
148 68adfdb2 Guido Trotter
    given LU and its opcodes.
149 68adfdb2 Guido Trotter

150 68adfdb2 Guido Trotter
    """
151 ca2a79e1 Guido Trotter
    adding_locks = level in lu.add_locks
152 ca2a79e1 Guido Trotter
    acquiring_locks = level in lu.needed_locks
153 8a2941c4 Guido Trotter
    if level not in locking.LEVELS:
154 e92376d7 Iustin Pop
      if callable(self._run_notifier):
155 e92376d7 Iustin Pop
        self._run_notifier()
156 8a2941c4 Guido Trotter
      result = self._ExecLU(lu)
157 ca2a79e1 Guido Trotter
    elif adding_locks and acquiring_locks:
158 ca2a79e1 Guido Trotter
      # We could both acquire and add locks at the same level, but for now we
159 ca2a79e1 Guido Trotter
      # don't need this, so we'll avoid the complicated code needed.
160 ca2a79e1 Guido Trotter
      raise NotImplementedError(
161 ca2a79e1 Guido Trotter
        "Can't declare locks to acquire when adding others")
162 ca2a79e1 Guido Trotter
    elif adding_locks or acquiring_locks:
163 fb8dcb62 Guido Trotter
      lu.DeclareLocks(level)
164 3977a4c1 Guido Trotter
      share = lu.share_locks[level]
165 ca2a79e1 Guido Trotter
      if acquiring_locks:
166 ca2a79e1 Guido Trotter
        needed_locks = lu.needed_locks[level]
167 ca2a79e1 Guido Trotter
        lu.acquired_locks[level] = self.context.glm.acquire(level,
168 ca2a79e1 Guido Trotter
                                                            needed_locks,
169 ca2a79e1 Guido Trotter
                                                            shared=share)
170 ca2a79e1 Guido Trotter
      else: # adding_locks
171 ca2a79e1 Guido Trotter
        add_locks = lu.add_locks[level]
172 ca2a79e1 Guido Trotter
        lu.remove_locks[level] = add_locks
173 ca2a79e1 Guido Trotter
        try:
174 ca2a79e1 Guido Trotter
          self.context.glm.add(level, add_locks, acquired=1, shared=share)
175 ca2a79e1 Guido Trotter
        except errors.LockError:
176 ca2a79e1 Guido Trotter
          raise errors.OpPrereqError(
177 5bbd3f7f Michael Hanselmann
            "Couldn't add locks (%s), probably because of a race condition"
178 ca2a79e1 Guido Trotter
            " with another job, who added them first" % add_locks)
179 68adfdb2 Guido Trotter
      try:
180 ca2a79e1 Guido Trotter
        try:
181 ca2a79e1 Guido Trotter
          if adding_locks:
182 ca2a79e1 Guido Trotter
            lu.acquired_locks[level] = add_locks
183 ca2a79e1 Guido Trotter
          result = self._LockAndExecLU(lu, level + 1)
184 ca2a79e1 Guido Trotter
        finally:
185 ca2a79e1 Guido Trotter
          if level in lu.remove_locks:
186 ca2a79e1 Guido Trotter
            self.context.glm.remove(level, lu.remove_locks[level])
187 68adfdb2 Guido Trotter
      finally:
188 80ee04a4 Guido Trotter
        if self.context.glm.is_owned(level):
189 68adfdb2 Guido Trotter
          self.context.glm.release(level)
190 68adfdb2 Guido Trotter
    else:
191 8a2941c4 Guido Trotter
      result = self._LockAndExecLU(lu, level + 1)
192 68adfdb2 Guido Trotter
193 68adfdb2 Guido Trotter
    return result
194 68adfdb2 Guido Trotter
195 e92376d7 Iustin Pop
  def ExecOpCode(self, op, feedback_fn, run_notifier):
196 a8083063 Iustin Pop
    """Execute an opcode.
197 a8083063 Iustin Pop

198 e92376d7 Iustin Pop
    @type op: an OpCode instance
199 e92376d7 Iustin Pop
    @param op: the opcode to be executed
200 e92376d7 Iustin Pop
    @type feedback_fn: a function that takes a single argument
201 e92376d7 Iustin Pop
    @param feedback_fn: this function will be used as feedback from the LU
202 e92376d7 Iustin Pop
                        code to the end-user
203 e92376d7 Iustin Pop
    @type run_notifier: callable (no arguments) or None
204 e92376d7 Iustin Pop
    @param run_notifier:  this function (if callable) will be called when
205 e92376d7 Iustin Pop
                          we are about to call the lu's Exec() method, that
206 5bbd3f7f Michael Hanselmann
                          is, after we have acquired all locks
207 a8083063 Iustin Pop

208 a8083063 Iustin Pop
    """
209 a8083063 Iustin Pop
    if not isinstance(op, opcodes.OpCode):
210 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Non-opcode instance passed"
211 3ecf6786 Iustin Pop
                                   " to ExecOpcode")
212 a8083063 Iustin Pop
213 f1048938 Iustin Pop
    self._feedback_fn = feedback_fn
214 e92376d7 Iustin Pop
    self._run_notifier = run_notifier
215 a8083063 Iustin Pop
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
216 a8083063 Iustin Pop
    if lu_class is None:
217 3ecf6786 Iustin Pop
      raise errors.OpCodeUnknown("Unknown opcode")
218 a8083063 Iustin Pop
219 04864530 Guido Trotter
    # Acquire the Big Ganeti Lock exclusively if this LU requires it, and in a
220 04864530 Guido Trotter
    # shared fashion otherwise (to prevent concurrent run with an exclusive LU.
221 984f7c32 Guido Trotter
    self.context.glm.acquire(locking.LEVEL_CLUSTER, [locking.BGL],
222 04864530 Guido Trotter
                             shared=not lu_class.REQ_BGL)
223 fe482621 Iustin Pop
    try:
224 04864530 Guido Trotter
      self.exclusive_BGL = lu_class.REQ_BGL
225 72737a7f Iustin Pop
      lu = lu_class(self, op, self.context, self.rpc)
226 d465bdc8 Guido Trotter
      lu.ExpandNames()
227 68adfdb2 Guido Trotter
      assert lu.needed_locks is not None, "needed_locks not set by LU"
228 04e1bfaf Guido Trotter
      result = self._LockAndExecLU(lu, locking.LEVEL_INSTANCE)
229 04864530 Guido Trotter
    finally:
230 984f7c32 Guido Trotter
      self.context.glm.release(locking.LEVEL_CLUSTER)
231 04864530 Guido Trotter
      self.exclusive_BGL = False
232 6a4aa7c1 Iustin Pop
233 a8083063 Iustin Pop
    return result
234 a8083063 Iustin Pop
235 0fbbf897 Iustin Pop
  def LogStep(self, current, total, message):
236 0fbbf897 Iustin Pop
    """Log a change in LU execution progress.
237 0fbbf897 Iustin Pop

238 0fbbf897 Iustin Pop
    """
239 a5eb7789 Iustin Pop
    logging.debug("Step %d/%d %s", current, total, message)
240 0fbbf897 Iustin Pop
    self._feedback_fn("STEP %d/%d %s" % (current, total, message))
241 0fbbf897 Iustin Pop
242 c0088fb9 Iustin Pop
  def LogWarning(self, message, *args, **kwargs):
243 0fbbf897 Iustin Pop
    """Log a warning to the logs and the user.
244 0fbbf897 Iustin Pop

245 c0088fb9 Iustin Pop
    The optional keyword argument is 'hint' and can be used to show a
246 c0088fb9 Iustin Pop
    hint to the user (presumably related to the warning). If the
247 c0088fb9 Iustin Pop
    message is empty, it will not be printed at all, allowing one to
248 c0088fb9 Iustin Pop
    show only a hint.
249 0fbbf897 Iustin Pop

250 c0088fb9 Iustin Pop
    """
251 c0088fb9 Iustin Pop
    assert not kwargs or (len(kwargs) == 1 and "hint" in kwargs), \
252 c0088fb9 Iustin Pop
           "Invalid keyword arguments for LogWarning (%s)" % str(kwargs)
253 c0088fb9 Iustin Pop
    if args:
254 c0088fb9 Iustin Pop
      message = message % tuple(args)
255 c0088fb9 Iustin Pop
    if message:
256 c0088fb9 Iustin Pop
      logging.warning(message)
257 c0088fb9 Iustin Pop
      self._feedback_fn(" - WARNING: %s" % message)
258 c0088fb9 Iustin Pop
    if "hint" in kwargs:
259 c0088fb9 Iustin Pop
      self._feedback_fn("      Hint: %s" % kwargs["hint"])
260 c0088fb9 Iustin Pop
261 c0088fb9 Iustin Pop
  def LogInfo(self, message, *args):
262 0fbbf897 Iustin Pop
    """Log an informational message to the logs and the user.
263 0fbbf897 Iustin Pop

264 0fbbf897 Iustin Pop
    """
265 c0088fb9 Iustin Pop
    if args:
266 c0088fb9 Iustin Pop
      message = message % tuple(args)
267 a5eb7789 Iustin Pop
    logging.info(message)
268 0fbbf897 Iustin Pop
    self._feedback_fn(" - INFO: %s" % message)
269 0fbbf897 Iustin Pop
270 a8083063 Iustin Pop
271 a8083063 Iustin Pop
class HooksMaster(object):
272 a8083063 Iustin Pop
  """Hooks master.
273 a8083063 Iustin Pop

274 a8083063 Iustin Pop
  This class distributes the run commands to the nodes based on the
275 a8083063 Iustin Pop
  specific LU class.
276 a8083063 Iustin Pop

277 a8083063 Iustin Pop
  In order to remove the direct dependency on the rpc module, the
278 a8083063 Iustin Pop
  constructor needs a function which actually does the remote
279 a8083063 Iustin Pop
  call. This will usually be rpc.call_hooks_runner, but any function
280 a8083063 Iustin Pop
  which behaves the same works.
281 a8083063 Iustin Pop

282 a8083063 Iustin Pop
  """
283 2395c322 Iustin Pop
  def __init__(self, callfn, proc, lu):
284 a8083063 Iustin Pop
    self.callfn = callfn
285 2395c322 Iustin Pop
    self.proc = proc
286 a8083063 Iustin Pop
    self.lu = lu
287 a8083063 Iustin Pop
    self.op = lu.op
288 a8083063 Iustin Pop
    self.env, node_list_pre, node_list_post = self._BuildEnv()
289 a8083063 Iustin Pop
    self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
290 a8083063 Iustin Pop
                      constants.HOOKS_PHASE_POST: node_list_post}
291 a8083063 Iustin Pop
292 a8083063 Iustin Pop
  def _BuildEnv(self):
293 a8083063 Iustin Pop
    """Compute the environment and the target nodes.
294 a8083063 Iustin Pop

295 a8083063 Iustin Pop
    Based on the opcode and the current node list, this builds the
296 a8083063 Iustin Pop
    environment for the hooks and the target node list for the run.
297 a8083063 Iustin Pop

298 a8083063 Iustin Pop
    """
299 a8083063 Iustin Pop
    env = {
300 a8083063 Iustin Pop
      "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
301 a8083063 Iustin Pop
      "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
302 a8083063 Iustin Pop
      "GANETI_OP_CODE": self.op.OP_ID,
303 a8083063 Iustin Pop
      "GANETI_OBJECT_TYPE": self.lu.HTYPE,
304 6a4aa7c1 Iustin Pop
      "GANETI_DATA_DIR": constants.DATA_DIR,
305 a8083063 Iustin Pop
      }
306 a8083063 Iustin Pop
307 9a395a76 Iustin Pop
    if self.lu.HPATH is not None:
308 9a395a76 Iustin Pop
      lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
309 9a395a76 Iustin Pop
      if lu_env:
310 9a395a76 Iustin Pop
        for key in lu_env:
311 9a395a76 Iustin Pop
          env["GANETI_" + key] = lu_env[key]
312 9a395a76 Iustin Pop
    else:
313 9a395a76 Iustin Pop
      lu_nodes_pre = lu_nodes_post = []
314 a8083063 Iustin Pop
315 4167825b Iustin Pop
    return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
316 4167825b Iustin Pop
317 4167825b Iustin Pop
  def _RunWrapper(self, node_list, hpath, phase):
318 4167825b Iustin Pop
    """Simple wrapper over self.callfn.
319 4167825b Iustin Pop

320 4167825b Iustin Pop
    This method fixes the environment before doing the rpc call.
321 4167825b Iustin Pop

322 4167825b Iustin Pop
    """
323 4167825b Iustin Pop
    env = self.env.copy()
324 4167825b Iustin Pop
    env["GANETI_HOOKS_PHASE"] = phase
325 4167825b Iustin Pop
    env["GANETI_HOOKS_PATH"] = hpath
326 437138c9 Michael Hanselmann
    if self.lu.cfg is not None:
327 437138c9 Michael Hanselmann
      env["GANETI_CLUSTER"] = self.lu.cfg.GetClusterName()
328 437138c9 Michael Hanselmann
      env["GANETI_MASTER"] = self.lu.cfg.GetMasterNode()
329 a8083063 Iustin Pop
330 4167825b Iustin Pop
    env = dict([(str(key), str(val)) for key, val in env.iteritems()])
331 a8083063 Iustin Pop
332 4167825b Iustin Pop
    return self.callfn(node_list, hpath, phase, env)
333 a8083063 Iustin Pop
334 a8083063 Iustin Pop
  def RunPhase(self, phase):
335 a8083063 Iustin Pop
    """Run all the scripts for a phase.
336 a8083063 Iustin Pop

337 a8083063 Iustin Pop
    This is the main function of the HookMaster.
338 a8083063 Iustin Pop

339 8dca23a3 Iustin Pop
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
340 8dca23a3 Iustin Pop
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
341 8dca23a3 Iustin Pop
    @return: the processed results of the hooks multi-node rpc call
342 8dca23a3 Iustin Pop
    @raise errors.HooksFailure: on communication failure to the nodes
343 b07a6922 Guido Trotter

344 a8083063 Iustin Pop
    """
345 a8083063 Iustin Pop
    if not self.node_list[phase]:
346 9a395a76 Iustin Pop
      # empty node list, we should not attempt to run this as either
347 9a395a76 Iustin Pop
      # we're in the cluster init phase and the rpc client part can't
348 9a395a76 Iustin Pop
      # even attempt to run, or this LU doesn't do hooks at all
349 a8083063 Iustin Pop
      return
350 4167825b Iustin Pop
    hpath = self.lu.HPATH
351 4167825b Iustin Pop
    results = self._RunWrapper(self.node_list[phase], hpath, phase)
352 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
353 a8083063 Iustin Pop
      errs = []
354 a8083063 Iustin Pop
      if not results:
355 3ecf6786 Iustin Pop
        raise errors.HooksFailure("Communication failure")
356 a8083063 Iustin Pop
      for node_name in results:
357 a8083063 Iustin Pop
        res = results[node_name]
358 3fb4f740 Iustin Pop
        if res.offline:
359 2395c322 Iustin Pop
          continue
360 3fb4f740 Iustin Pop
        msg = res.RemoteFailMsg()
361 3fb4f740 Iustin Pop
        if msg:
362 3fb4f740 Iustin Pop
          self.proc.LogWarning("Communication failure to node %s: %s",
363 3fb4f740 Iustin Pop
                               node_name, msg)
364 3fb4f740 Iustin Pop
          continue
365 3fb4f740 Iustin Pop
        for script, hkr, output in res.payload:
366 a8083063 Iustin Pop
          if hkr == constants.HKR_FAIL:
367 a8083063 Iustin Pop
            errs.append((node_name, script, output))
368 a8083063 Iustin Pop
      if errs:
369 3ecf6786 Iustin Pop
        raise errors.HooksAbort(errs)
370 b07a6922 Guido Trotter
    return results
371 6a4aa7c1 Iustin Pop
372 6a4aa7c1 Iustin Pop
  def RunConfigUpdate(self):
373 6a4aa7c1 Iustin Pop
    """Run the special configuration update hook
374 6a4aa7c1 Iustin Pop

375 6a4aa7c1 Iustin Pop
    This is a special hook that runs only on the master after each
376 6a4aa7c1 Iustin Pop
    top-level LI if the configuration has been updated.
377 6a4aa7c1 Iustin Pop

378 6a4aa7c1 Iustin Pop
    """
379 6a4aa7c1 Iustin Pop
    phase = constants.HOOKS_PHASE_POST
380 6a4aa7c1 Iustin Pop
    hpath = constants.HOOKS_NAME_CFGUPDATE
381 437138c9 Michael Hanselmann
    nodes = [self.lu.cfg.GetMasterNode()]
382 29921401 Iustin Pop
    self._RunWrapper(nodes, hpath, phase)