Statistics
| Branch: | Tag: | Revision:

root / lib / mcpu.py @ 9afb67fe

History | View | Annotate | Download (13.1 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 a8083063 Iustin Pop
    opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
46 a8083063 Iustin Pop
    opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
47 a8083063 Iustin Pop
    opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
48 ae5849b5 Michael Hanselmann
    opcodes.OpQueryConfigValues: cmdlib.LUQueryConfigValues,
49 07bd8a51 Iustin Pop
    opcodes.OpRenameCluster: cmdlib.LURenameCluster,
50 f4d4e184 Iustin Pop
    opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
51 0cc05d44 Manuel Franceschini
    opcodes.OpSetClusterParams: cmdlib.LUSetClusterParams,
52 afee0879 Iustin Pop
    opcodes.OpRedistributeConfig: cmdlib.LURedistributeConfig,
53 a8083063 Iustin Pop
    # node lu
54 a8083063 Iustin Pop
    opcodes.OpAddNode: cmdlib.LUAddNode,
55 a8083063 Iustin Pop
    opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
56 dcb93971 Michael Hanselmann
    opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
57 a8083063 Iustin Pop
    opcodes.OpRemoveNode: cmdlib.LURemoveNode,
58 b31c8676 Iustin Pop
    opcodes.OpSetNodeParams: cmdlib.LUSetNodeParams,
59 a8083063 Iustin Pop
    # instance lu
60 a8083063 Iustin Pop
    opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
61 fe7b0351 Michael Hanselmann
    opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
62 a8083063 Iustin Pop
    opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
63 decd5f45 Iustin Pop
    opcodes.OpRenameInstance: cmdlib.LURenameInstance,
64 a8083063 Iustin Pop
    opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
65 a8083063 Iustin Pop
    opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
66 a8083063 Iustin Pop
    opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
67 bf6929a2 Alexander Schreiber
    opcodes.OpRebootInstance: cmdlib.LURebootInstance,
68 a8083063 Iustin Pop
    opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
69 a8083063 Iustin Pop
    opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
70 a8083063 Iustin Pop
    opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
71 53c776b5 Iustin Pop
    opcodes.OpMigrateInstance: cmdlib.LUMigrateInstance,
72 a8083063 Iustin Pop
    opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
73 a8083063 Iustin Pop
    opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
74 a8083063 Iustin Pop
    opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
75 7767bbf5 Manuel Franceschini
    opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
76 8729e0d7 Iustin Pop
    opcodes.OpGrowDisk: cmdlib.LUGrowDisk,
77 a8083063 Iustin Pop
    # os lu
78 a8083063 Iustin Pop
    opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
79 a8083063 Iustin Pop
    # exports lu
80 a8083063 Iustin Pop
    opcodes.OpQueryExports: cmdlib.LUQueryExports,
81 a8083063 Iustin Pop
    opcodes.OpExportInstance: cmdlib.LUExportInstance,
82 9ac99fda Guido Trotter
    opcodes.OpRemoveExport: cmdlib.LURemoveExport,
83 5c947f38 Iustin Pop
    # tags lu
84 5c947f38 Iustin Pop
    opcodes.OpGetTags: cmdlib.LUGetTags,
85 73415719 Iustin Pop
    opcodes.OpSearchTags: cmdlib.LUSearchTags,
86 f27302fa Iustin Pop
    opcodes.OpAddTags: cmdlib.LUAddTags,
87 f27302fa Iustin Pop
    opcodes.OpDelTags: cmdlib.LUDelTags,
88 06009e27 Iustin Pop
    # test lu
89 06009e27 Iustin Pop
    opcodes.OpTestDelay: cmdlib.LUTestDelay,
90 d61df03e Iustin Pop
    opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
91 a8083063 Iustin Pop
    }
92 a8083063 Iustin Pop
93 f1048938 Iustin Pop
  def __init__(self, context):
94 a8083063 Iustin Pop
    """Constructor for Processor
95 a8083063 Iustin Pop

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

108 36c381d7 Guido Trotter
    """
109 36c381d7 Guido Trotter
    write_count = self.context.cfg.write_count
110 36c381d7 Guido Trotter
    lu.CheckPrereq()
111 72737a7f Iustin Pop
    hm = HooksMaster(self.rpc.call_hooks_runner, self, lu)
112 36c381d7 Guido Trotter
    h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
113 36c381d7 Guido Trotter
    lu.HooksCallBack(constants.HOOKS_PHASE_PRE, h_results,
114 36c381d7 Guido Trotter
                     self._feedback_fn, None)
115 36c381d7 Guido Trotter
    try:
116 36c381d7 Guido Trotter
      result = lu.Exec(self._feedback_fn)
117 36c381d7 Guido Trotter
      h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
118 36c381d7 Guido Trotter
      result = lu.HooksCallBack(constants.HOOKS_PHASE_POST, h_results,
119 36c381d7 Guido Trotter
                                self._feedback_fn, result)
120 36c381d7 Guido Trotter
    finally:
121 36c381d7 Guido Trotter
      # FIXME: This needs locks if not lu_class.REQ_BGL
122 36c381d7 Guido Trotter
      if write_count != self.context.cfg.write_count:
123 36c381d7 Guido Trotter
        hm.RunConfigUpdate()
124 36c381d7 Guido Trotter
125 36c381d7 Guido Trotter
    return result
126 36c381d7 Guido Trotter
127 68adfdb2 Guido Trotter
  def _LockAndExecLU(self, lu, level):
128 68adfdb2 Guido Trotter
    """Execute a Logical Unit, with the needed locks.
129 68adfdb2 Guido Trotter

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

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

182 e92376d7 Iustin Pop
    @type op: an OpCode instance
183 e92376d7 Iustin Pop
    @param op: the opcode to be executed
184 e92376d7 Iustin Pop
    @type feedback_fn: a function that takes a single argument
185 e92376d7 Iustin Pop
    @param feedback_fn: this function will be used as feedback from the LU
186 e92376d7 Iustin Pop
                        code to the end-user
187 e92376d7 Iustin Pop
    @type run_notifier: callable (no arguments) or None
188 e92376d7 Iustin Pop
    @param run_notifier:  this function (if callable) will be called when
189 e92376d7 Iustin Pop
                          we are about to call the lu's Exec() method, that
190 e92376d7 Iustin Pop
                          is, after we have aquired all locks
191 a8083063 Iustin Pop

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

222 0fbbf897 Iustin Pop
    """
223 a5eb7789 Iustin Pop
    logging.debug("Step %d/%d %s", current, total, message)
224 0fbbf897 Iustin Pop
    self._feedback_fn("STEP %d/%d %s" % (current, total, message))
225 0fbbf897 Iustin Pop
226 c0088fb9 Iustin Pop
  def LogWarning(self, message, *args, **kwargs):
227 0fbbf897 Iustin Pop
    """Log a warning to the logs and the user.
228 0fbbf897 Iustin Pop

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

234 c0088fb9 Iustin Pop
    """
235 c0088fb9 Iustin Pop
    assert not kwargs or (len(kwargs) == 1 and "hint" in kwargs), \
236 c0088fb9 Iustin Pop
           "Invalid keyword arguments for LogWarning (%s)" % str(kwargs)
237 c0088fb9 Iustin Pop
    if args:
238 c0088fb9 Iustin Pop
      message = message % tuple(args)
239 c0088fb9 Iustin Pop
    if message:
240 c0088fb9 Iustin Pop
      logging.warning(message)
241 c0088fb9 Iustin Pop
      self._feedback_fn(" - WARNING: %s" % message)
242 c0088fb9 Iustin Pop
    if "hint" in kwargs:
243 c0088fb9 Iustin Pop
      self._feedback_fn("      Hint: %s" % kwargs["hint"])
244 c0088fb9 Iustin Pop
245 c0088fb9 Iustin Pop
  def LogInfo(self, message, *args):
246 0fbbf897 Iustin Pop
    """Log an informational message to the logs and the user.
247 0fbbf897 Iustin Pop

248 0fbbf897 Iustin Pop
    """
249 c0088fb9 Iustin Pop
    if args:
250 c0088fb9 Iustin Pop
      message = message % tuple(args)
251 a5eb7789 Iustin Pop
    logging.info(message)
252 0fbbf897 Iustin Pop
    self._feedback_fn(" - INFO: %s" % message)
253 0fbbf897 Iustin Pop
254 a8083063 Iustin Pop
255 a8083063 Iustin Pop
class HooksMaster(object):
256 a8083063 Iustin Pop
  """Hooks master.
257 a8083063 Iustin Pop

258 a8083063 Iustin Pop
  This class distributes the run commands to the nodes based on the
259 a8083063 Iustin Pop
  specific LU class.
260 a8083063 Iustin Pop

261 a8083063 Iustin Pop
  In order to remove the direct dependency on the rpc module, the
262 a8083063 Iustin Pop
  constructor needs a function which actually does the remote
263 a8083063 Iustin Pop
  call. This will usually be rpc.call_hooks_runner, but any function
264 a8083063 Iustin Pop
  which behaves the same works.
265 a8083063 Iustin Pop

266 a8083063 Iustin Pop
  """
267 2395c322 Iustin Pop
  def __init__(self, callfn, proc, lu):
268 a8083063 Iustin Pop
    self.callfn = callfn
269 2395c322 Iustin Pop
    self.proc = proc
270 a8083063 Iustin Pop
    self.lu = lu
271 a8083063 Iustin Pop
    self.op = lu.op
272 a8083063 Iustin Pop
    self.env, node_list_pre, node_list_post = self._BuildEnv()
273 a8083063 Iustin Pop
    self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
274 a8083063 Iustin Pop
                      constants.HOOKS_PHASE_POST: node_list_post}
275 a8083063 Iustin Pop
276 a8083063 Iustin Pop
  def _BuildEnv(self):
277 a8083063 Iustin Pop
    """Compute the environment and the target nodes.
278 a8083063 Iustin Pop

279 a8083063 Iustin Pop
    Based on the opcode and the current node list, this builds the
280 a8083063 Iustin Pop
    environment for the hooks and the target node list for the run.
281 a8083063 Iustin Pop

282 a8083063 Iustin Pop
    """
283 a8083063 Iustin Pop
    env = {
284 a8083063 Iustin Pop
      "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
285 a8083063 Iustin Pop
      "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
286 a8083063 Iustin Pop
      "GANETI_OP_CODE": self.op.OP_ID,
287 a8083063 Iustin Pop
      "GANETI_OBJECT_TYPE": self.lu.HTYPE,
288 6a4aa7c1 Iustin Pop
      "GANETI_DATA_DIR": constants.DATA_DIR,
289 a8083063 Iustin Pop
      }
290 a8083063 Iustin Pop
291 9a395a76 Iustin Pop
    if self.lu.HPATH is not None:
292 9a395a76 Iustin Pop
      lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
293 9a395a76 Iustin Pop
      if lu_env:
294 9a395a76 Iustin Pop
        for key in lu_env:
295 9a395a76 Iustin Pop
          env["GANETI_" + key] = lu_env[key]
296 9a395a76 Iustin Pop
    else:
297 9a395a76 Iustin Pop
      lu_nodes_pre = lu_nodes_post = []
298 a8083063 Iustin Pop
299 4167825b Iustin Pop
    return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
300 4167825b Iustin Pop
301 4167825b Iustin Pop
  def _RunWrapper(self, node_list, hpath, phase):
302 4167825b Iustin Pop
    """Simple wrapper over self.callfn.
303 4167825b Iustin Pop

304 4167825b Iustin Pop
    This method fixes the environment before doing the rpc call.
305 4167825b Iustin Pop

306 4167825b Iustin Pop
    """
307 4167825b Iustin Pop
    env = self.env.copy()
308 4167825b Iustin Pop
    env["GANETI_HOOKS_PHASE"] = phase
309 4167825b Iustin Pop
    env["GANETI_HOOKS_PATH"] = hpath
310 437138c9 Michael Hanselmann
    if self.lu.cfg is not None:
311 437138c9 Michael Hanselmann
      env["GANETI_CLUSTER"] = self.lu.cfg.GetClusterName()
312 437138c9 Michael Hanselmann
      env["GANETI_MASTER"] = self.lu.cfg.GetMasterNode()
313 a8083063 Iustin Pop
314 4167825b Iustin Pop
    env = dict([(str(key), str(val)) for key, val in env.iteritems()])
315 a8083063 Iustin Pop
316 4167825b Iustin Pop
    return self.callfn(node_list, hpath, phase, env)
317 a8083063 Iustin Pop
318 a8083063 Iustin Pop
  def RunPhase(self, phase):
319 a8083063 Iustin Pop
    """Run all the scripts for a phase.
320 a8083063 Iustin Pop

321 a8083063 Iustin Pop
    This is the main function of the HookMaster.
322 a8083063 Iustin Pop

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

328 a8083063 Iustin Pop
    """
329 a8083063 Iustin Pop
    if not self.node_list[phase]:
330 9a395a76 Iustin Pop
      # empty node list, we should not attempt to run this as either
331 9a395a76 Iustin Pop
      # we're in the cluster init phase and the rpc client part can't
332 9a395a76 Iustin Pop
      # even attempt to run, or this LU doesn't do hooks at all
333 a8083063 Iustin Pop
      return
334 4167825b Iustin Pop
    hpath = self.lu.HPATH
335 4167825b Iustin Pop
    results = self._RunWrapper(self.node_list[phase], hpath, phase)
336 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
337 a8083063 Iustin Pop
      errs = []
338 a8083063 Iustin Pop
      if not results:
339 3ecf6786 Iustin Pop
        raise errors.HooksFailure("Communication failure")
340 a8083063 Iustin Pop
      for node_name in results:
341 a8083063 Iustin Pop
        res = results[node_name]
342 781de953 Iustin Pop
        if res.failed or res.data is False or not isinstance(res.data, list):
343 0a66c968 Iustin Pop
          if not res.offline:
344 0a66c968 Iustin Pop
            self.proc.LogWarning("Communication failure to node %s" %
345 0a66c968 Iustin Pop
                                 node_name)
346 2395c322 Iustin Pop
          continue
347 781de953 Iustin Pop
        for script, hkr, output in res.data:
348 a8083063 Iustin Pop
          if hkr == constants.HKR_FAIL:
349 a8083063 Iustin Pop
            errs.append((node_name, script, output))
350 a8083063 Iustin Pop
      if errs:
351 3ecf6786 Iustin Pop
        raise errors.HooksAbort(errs)
352 b07a6922 Guido Trotter
    return results
353 6a4aa7c1 Iustin Pop
354 6a4aa7c1 Iustin Pop
  def RunConfigUpdate(self):
355 6a4aa7c1 Iustin Pop
    """Run the special configuration update hook
356 6a4aa7c1 Iustin Pop

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

360 6a4aa7c1 Iustin Pop
    """
361 6a4aa7c1 Iustin Pop
    phase = constants.HOOKS_PHASE_POST
362 6a4aa7c1 Iustin Pop
    hpath = constants.HOOKS_NAME_CFGUPDATE
363 437138c9 Michael Hanselmann
    nodes = [self.lu.cfg.GetMasterNode()]
364 6a4aa7c1 Iustin Pop
    results = self._RunWrapper(nodes, hpath, phase)