Statistics
| Branch: | Tag: | Revision:

root / lib / mcpu.py @ 243cdbcc

History | View | Annotate | Download (10.7 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 a8083063 Iustin Pop
32 a8083063 Iustin Pop
from ganeti import opcodes
33 a8083063 Iustin Pop
from ganeti import constants
34 a8083063 Iustin Pop
from ganeti import errors
35 a8083063 Iustin Pop
from ganeti import rpc
36 a8083063 Iustin Pop
from ganeti import cmdlib
37 a8083063 Iustin Pop
from ganeti import config
38 a8083063 Iustin Pop
from ganeti import ssconf
39 0fbbf897 Iustin Pop
from ganeti import logger
40 a8083063 Iustin Pop
41 7c0d6283 Michael Hanselmann
42 a8083063 Iustin Pop
class Processor(object):
43 a8083063 Iustin Pop
  """Object which runs OpCodes"""
44 a8083063 Iustin Pop
  DISPATCH_TABLE = {
45 a8083063 Iustin Pop
    # Cluster
46 a8083063 Iustin Pop
    opcodes.OpInitCluster: cmdlib.LUInitCluster,
47 a8083063 Iustin Pop
    opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
48 a8083063 Iustin Pop
    opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
49 a8083063 Iustin Pop
    opcodes.OpClusterCopyFile: cmdlib.LUClusterCopyFile,
50 a8083063 Iustin Pop
    opcodes.OpRunClusterCommand: cmdlib.LURunClusterCommand,
51 a8083063 Iustin Pop
    opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
52 a8083063 Iustin Pop
    opcodes.OpMasterFailover: cmdlib.LUMasterFailover,
53 a8083063 Iustin Pop
    opcodes.OpDumpClusterConfig: cmdlib.LUDumpClusterConfig,
54 07bd8a51 Iustin Pop
    opcodes.OpRenameCluster: cmdlib.LURenameCluster,
55 f4d4e184 Iustin Pop
    opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
56 0cc05d44 Manuel Franceschini
    opcodes.OpSetClusterParams: cmdlib.LUSetClusterParams,
57 a8083063 Iustin Pop
    # node lu
58 a8083063 Iustin Pop
    opcodes.OpAddNode: cmdlib.LUAddNode,
59 a8083063 Iustin Pop
    opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
60 dcb93971 Michael Hanselmann
    opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
61 a8083063 Iustin Pop
    opcodes.OpRemoveNode: cmdlib.LURemoveNode,
62 a8083063 Iustin Pop
    # instance lu
63 a8083063 Iustin Pop
    opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
64 fe7b0351 Michael Hanselmann
    opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
65 a8083063 Iustin Pop
    opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
66 decd5f45 Iustin Pop
    opcodes.OpRenameInstance: cmdlib.LURenameInstance,
67 a8083063 Iustin Pop
    opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
68 a8083063 Iustin Pop
    opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
69 a8083063 Iustin Pop
    opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
70 bf6929a2 Alexander Schreiber
    opcodes.OpRebootInstance: cmdlib.LURebootInstance,
71 a8083063 Iustin Pop
    opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
72 a8083063 Iustin Pop
    opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
73 a8083063 Iustin Pop
    opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
74 a8083063 Iustin Pop
    opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
75 a8083063 Iustin Pop
    opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
76 a8083063 Iustin Pop
    opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
77 7767bbf5 Manuel Franceschini
    opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
78 a8083063 Iustin Pop
    # os lu
79 a8083063 Iustin Pop
    opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
80 a8083063 Iustin Pop
    # exports lu
81 a8083063 Iustin Pop
    opcodes.OpQueryExports: cmdlib.LUQueryExports,
82 a8083063 Iustin Pop
    opcodes.OpExportInstance: cmdlib.LUExportInstance,
83 9ac99fda Guido Trotter
    opcodes.OpRemoveExport: cmdlib.LURemoveExport,
84 5c947f38 Iustin Pop
    # tags lu
85 5c947f38 Iustin Pop
    opcodes.OpGetTags: cmdlib.LUGetTags,
86 73415719 Iustin Pop
    opcodes.OpSearchTags: cmdlib.LUSearchTags,
87 f27302fa Iustin Pop
    opcodes.OpAddTags: cmdlib.LUAddTags,
88 f27302fa Iustin Pop
    opcodes.OpDelTags: cmdlib.LUDelTags,
89 06009e27 Iustin Pop
    # test lu
90 06009e27 Iustin Pop
    opcodes.OpTestDelay: cmdlib.LUTestDelay,
91 d61df03e Iustin Pop
    opcodes.OpTestAllocator: cmdlib.LUTestAllocator,
92 a8083063 Iustin Pop
    }
93 a8083063 Iustin Pop
94 1a8c0ce1 Iustin Pop
  def __init__(self, feedback=None):
95 a8083063 Iustin Pop
    """Constructor for Processor
96 a8083063 Iustin Pop

97 1a8c0ce1 Iustin Pop
    Args:
98 1a8c0ce1 Iustin Pop
     - feedback_fn: the feedback function (taking one string) to be run when
99 1a8c0ce1 Iustin Pop
                    interesting events are happening
100 a8083063 Iustin Pop
    """
101 a8083063 Iustin Pop
    self.cfg = None
102 a8083063 Iustin Pop
    self.sstore = None
103 1a8c0ce1 Iustin Pop
    self._feedback_fn = feedback
104 a8083063 Iustin Pop
105 1a8c0ce1 Iustin Pop
  def ExecOpCode(self, op):
106 a8083063 Iustin Pop
    """Execute an opcode.
107 a8083063 Iustin Pop

108 a8083063 Iustin Pop
    Args:
109 c99a3cc0 Manuel Franceschini
      op: the opcode to be executed
110 a8083063 Iustin Pop

111 a8083063 Iustin Pop
    """
112 a8083063 Iustin Pop
    if not isinstance(op, opcodes.OpCode):
113 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Non-opcode instance passed"
114 3ecf6786 Iustin Pop
                                   " to ExecOpcode")
115 a8083063 Iustin Pop
116 a8083063 Iustin Pop
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
117 a8083063 Iustin Pop
    if lu_class is None:
118 3ecf6786 Iustin Pop
      raise errors.OpCodeUnknown("Unknown opcode")
119 a8083063 Iustin Pop
120 a8083063 Iustin Pop
    if lu_class.REQ_CLUSTER and self.cfg is None:
121 a8083063 Iustin Pop
      self.cfg = config.ConfigWriter()
122 a8083063 Iustin Pop
      self.sstore = ssconf.SimpleStore()
123 6a4aa7c1 Iustin Pop
    if self.cfg is not None:
124 6a4aa7c1 Iustin Pop
      write_count = self.cfg.write_count
125 6a4aa7c1 Iustin Pop
    else:
126 6a4aa7c1 Iustin Pop
      write_count = 0
127 a8083063 Iustin Pop
    lu = lu_class(self, op, self.cfg, self.sstore)
128 a8083063 Iustin Pop
    lu.CheckPrereq()
129 2395c322 Iustin Pop
    hm = HooksMaster(rpc.call_hooks_runner, self, lu)
130 1fce5219 Guido Trotter
    h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
131 1fce5219 Guido Trotter
    lu.HooksCallBack(constants.HOOKS_PHASE_PRE,
132 1fce5219 Guido Trotter
                     h_results, self._feedback_fn, None)
133 fe482621 Iustin Pop
    try:
134 fe482621 Iustin Pop
      result = lu.Exec(self._feedback_fn)
135 1fce5219 Guido Trotter
      h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
136 1fce5219 Guido Trotter
      result = lu.HooksCallBack(constants.HOOKS_PHASE_POST,
137 1fce5219 Guido Trotter
                       h_results, self._feedback_fn, result)
138 fe482621 Iustin Pop
    finally:
139 fe482621 Iustin Pop
      if lu.cfg is not None:
140 fe482621 Iustin Pop
        # we use lu.cfg and not self.cfg as for init cluster, self.cfg
141 fe482621 Iustin Pop
        # is None but lu.cfg has been recently initialized in the
142 fe482621 Iustin Pop
        # lu.Exec method
143 fe482621 Iustin Pop
        if write_count != lu.cfg.write_count:
144 fe482621 Iustin Pop
          hm.RunConfigUpdate()
145 6a4aa7c1 Iustin Pop
146 a8083063 Iustin Pop
    return result
147 a8083063 Iustin Pop
148 1a8c0ce1 Iustin Pop
  def ChainOpCode(self, op):
149 a8083063 Iustin Pop
    """Chain and execute an opcode.
150 a8083063 Iustin Pop

151 a8083063 Iustin Pop
    This is used by LUs when they need to execute a child LU.
152 a8083063 Iustin Pop

153 a8083063 Iustin Pop
    Args:
154 a8083063 Iustin Pop
     - opcode: the opcode to be executed
155 a8083063 Iustin Pop

156 a8083063 Iustin Pop
    """
157 a8083063 Iustin Pop
    if not isinstance(op, opcodes.OpCode):
158 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Non-opcode instance passed"
159 3ecf6786 Iustin Pop
                                   " to ExecOpcode")
160 a8083063 Iustin Pop
161 a8083063 Iustin Pop
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
162 a8083063 Iustin Pop
    if lu_class is None:
163 3ecf6786 Iustin Pop
      raise errors.OpCodeUnknown("Unknown opcode")
164 a8083063 Iustin Pop
165 a8083063 Iustin Pop
    if lu_class.REQ_CLUSTER and self.cfg is None:
166 a8083063 Iustin Pop
      self.cfg = config.ConfigWriter()
167 a8083063 Iustin Pop
      self.sstore = ssconf.SimpleStore()
168 f97a6b10 Iustin Pop
    #do_hooks = lu_class.HPATH is not None
169 a8083063 Iustin Pop
    lu = lu_class(self, op, self.cfg, self.sstore)
170 a8083063 Iustin Pop
    lu.CheckPrereq()
171 a8083063 Iustin Pop
    #if do_hooks:
172 2395c322 Iustin Pop
    #  hm = HooksMaster(rpc.call_hooks_runner, self, lu)
173 1fce5219 Guido Trotter
    #  h_results = hm.RunPhase(constants.HOOKS_PHASE_PRE)
174 1fce5219 Guido Trotter
    #  lu.HooksCallBack(constants.HOOKS_PHASE_PRE,
175 1fce5219 Guido Trotter
    #                   h_results, self._feedback_fn, None)
176 1a8c0ce1 Iustin Pop
    result = lu.Exec(self._feedback_fn)
177 a8083063 Iustin Pop
    #if do_hooks:
178 1fce5219 Guido Trotter
    #  h_results = hm.RunPhase(constants.HOOKS_PHASE_POST)
179 1fce5219 Guido Trotter
    #  result = lu.HooksCallBack(constants.HOOKS_PHASE_POST,
180 1fce5219 Guido Trotter
    #                   h_results, self._feedback_fn, result)
181 a8083063 Iustin Pop
    return result
182 a8083063 Iustin Pop
183 0fbbf897 Iustin Pop
  def LogStep(self, current, total, message):
184 0fbbf897 Iustin Pop
    """Log a change in LU execution progress.
185 0fbbf897 Iustin Pop

186 0fbbf897 Iustin Pop
    """
187 0fbbf897 Iustin Pop
    logger.Debug("Step %d/%d %s" % (current, total, message))
188 0fbbf897 Iustin Pop
    self._feedback_fn("STEP %d/%d %s" % (current, total, message))
189 0fbbf897 Iustin Pop
190 5bfac263 Iustin Pop
  def LogWarning(self, message, hint=None):
191 0fbbf897 Iustin Pop
    """Log a warning to the logs and the user.
192 0fbbf897 Iustin Pop

193 0fbbf897 Iustin Pop
    """
194 0fbbf897 Iustin Pop
    logger.Error(message)
195 0fbbf897 Iustin Pop
    self._feedback_fn(" - WARNING: %s" % message)
196 5bfac263 Iustin Pop
    if hint:
197 5bfac263 Iustin Pop
      self._feedback_fn("      Hint: %s" % hint)
198 0fbbf897 Iustin Pop
199 0fbbf897 Iustin Pop
  def LogInfo(self, message):
200 0fbbf897 Iustin Pop
    """Log an informational message to the logs and the user.
201 0fbbf897 Iustin Pop

202 0fbbf897 Iustin Pop
    """
203 0fbbf897 Iustin Pop
    logger.Info(message)
204 0fbbf897 Iustin Pop
    self._feedback_fn(" - INFO: %s" % message)
205 0fbbf897 Iustin Pop
206 a8083063 Iustin Pop
207 a8083063 Iustin Pop
class HooksMaster(object):
208 a8083063 Iustin Pop
  """Hooks master.
209 a8083063 Iustin Pop

210 a8083063 Iustin Pop
  This class distributes the run commands to the nodes based on the
211 a8083063 Iustin Pop
  specific LU class.
212 a8083063 Iustin Pop

213 a8083063 Iustin Pop
  In order to remove the direct dependency on the rpc module, the
214 a8083063 Iustin Pop
  constructor needs a function which actually does the remote
215 a8083063 Iustin Pop
  call. This will usually be rpc.call_hooks_runner, but any function
216 a8083063 Iustin Pop
  which behaves the same works.
217 a8083063 Iustin Pop

218 a8083063 Iustin Pop
  """
219 2395c322 Iustin Pop
  def __init__(self, callfn, proc, lu):
220 a8083063 Iustin Pop
    self.callfn = callfn
221 2395c322 Iustin Pop
    self.proc = proc
222 a8083063 Iustin Pop
    self.lu = lu
223 a8083063 Iustin Pop
    self.op = lu.op
224 a8083063 Iustin Pop
    self.env, node_list_pre, node_list_post = self._BuildEnv()
225 a8083063 Iustin Pop
    self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
226 a8083063 Iustin Pop
                      constants.HOOKS_PHASE_POST: node_list_post}
227 a8083063 Iustin Pop
228 a8083063 Iustin Pop
  def _BuildEnv(self):
229 a8083063 Iustin Pop
    """Compute the environment and the target nodes.
230 a8083063 Iustin Pop

231 a8083063 Iustin Pop
    Based on the opcode and the current node list, this builds the
232 a8083063 Iustin Pop
    environment for the hooks and the target node list for the run.
233 a8083063 Iustin Pop

234 a8083063 Iustin Pop
    """
235 a8083063 Iustin Pop
    env = {
236 a8083063 Iustin Pop
      "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
237 a8083063 Iustin Pop
      "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
238 a8083063 Iustin Pop
      "GANETI_OP_CODE": self.op.OP_ID,
239 a8083063 Iustin Pop
      "GANETI_OBJECT_TYPE": self.lu.HTYPE,
240 6a4aa7c1 Iustin Pop
      "GANETI_DATA_DIR": constants.DATA_DIR,
241 a8083063 Iustin Pop
      }
242 a8083063 Iustin Pop
243 9a395a76 Iustin Pop
    if self.lu.HPATH is not None:
244 9a395a76 Iustin Pop
      lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
245 9a395a76 Iustin Pop
      if lu_env:
246 9a395a76 Iustin Pop
        for key in lu_env:
247 9a395a76 Iustin Pop
          env["GANETI_" + key] = lu_env[key]
248 9a395a76 Iustin Pop
    else:
249 9a395a76 Iustin Pop
      lu_nodes_pre = lu_nodes_post = []
250 a8083063 Iustin Pop
251 4167825b Iustin Pop
    return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
252 4167825b Iustin Pop
253 4167825b Iustin Pop
  def _RunWrapper(self, node_list, hpath, phase):
254 4167825b Iustin Pop
    """Simple wrapper over self.callfn.
255 4167825b Iustin Pop

256 4167825b Iustin Pop
    This method fixes the environment before doing the rpc call.
257 4167825b Iustin Pop

258 4167825b Iustin Pop
    """
259 4167825b Iustin Pop
    env = self.env.copy()
260 4167825b Iustin Pop
    env["GANETI_HOOKS_PHASE"] = phase
261 4167825b Iustin Pop
    env["GANETI_HOOKS_PATH"] = hpath
262 f97a6b10 Iustin Pop
    if self.lu.sstore is not None:
263 f97a6b10 Iustin Pop
      env["GANETI_CLUSTER"] = self.lu.sstore.GetClusterName()
264 f97a6b10 Iustin Pop
      env["GANETI_MASTER"] = self.lu.sstore.GetMasterNode()
265 a8083063 Iustin Pop
266 4167825b Iustin Pop
    env = dict([(str(key), str(val)) for key, val in env.iteritems()])
267 a8083063 Iustin Pop
268 4167825b Iustin Pop
    return self.callfn(node_list, hpath, phase, env)
269 a8083063 Iustin Pop
270 a8083063 Iustin Pop
  def RunPhase(self, phase):
271 a8083063 Iustin Pop
    """Run all the scripts for a phase.
272 a8083063 Iustin Pop

273 a8083063 Iustin Pop
    This is the main function of the HookMaster.
274 a8083063 Iustin Pop

275 b07a6922 Guido Trotter
    Args:
276 b07a6922 Guido Trotter
      phase: the hooks phase to run
277 b07a6922 Guido Trotter

278 b07a6922 Guido Trotter
    Returns:
279 b07a6922 Guido Trotter
      the result of the hooks multi-node rpc call
280 b07a6922 Guido Trotter

281 a8083063 Iustin Pop
    """
282 a8083063 Iustin Pop
    if not self.node_list[phase]:
283 9a395a76 Iustin Pop
      # empty node list, we should not attempt to run this as either
284 9a395a76 Iustin Pop
      # we're in the cluster init phase and the rpc client part can't
285 9a395a76 Iustin Pop
      # even attempt to run, or this LU doesn't do hooks at all
286 a8083063 Iustin Pop
      return
287 4167825b Iustin Pop
    hpath = self.lu.HPATH
288 4167825b Iustin Pop
    results = self._RunWrapper(self.node_list[phase], hpath, phase)
289 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
290 a8083063 Iustin Pop
      errs = []
291 a8083063 Iustin Pop
      if not results:
292 3ecf6786 Iustin Pop
        raise errors.HooksFailure("Communication failure")
293 a8083063 Iustin Pop
      for node_name in results:
294 a8083063 Iustin Pop
        res = results[node_name]
295 a8083063 Iustin Pop
        if res is False or not isinstance(res, list):
296 2395c322 Iustin Pop
          self.proc.LogWarning("Communication failure to node %s" % node_name)
297 2395c322 Iustin Pop
          continue
298 a8083063 Iustin Pop
        for script, hkr, output in res:
299 a8083063 Iustin Pop
          if hkr == constants.HKR_FAIL:
300 a8083063 Iustin Pop
            output = output.strip().encode("string_escape")
301 a8083063 Iustin Pop
            errs.append((node_name, script, output))
302 a8083063 Iustin Pop
      if errs:
303 3ecf6786 Iustin Pop
        raise errors.HooksAbort(errs)
304 b07a6922 Guido Trotter
    return results
305 6a4aa7c1 Iustin Pop
306 6a4aa7c1 Iustin Pop
  def RunConfigUpdate(self):
307 6a4aa7c1 Iustin Pop
    """Run the special configuration update hook
308 6a4aa7c1 Iustin Pop

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

312 6a4aa7c1 Iustin Pop
    """
313 6a4aa7c1 Iustin Pop
    phase = constants.HOOKS_PHASE_POST
314 6a4aa7c1 Iustin Pop
    hpath = constants.HOOKS_NAME_CFGUPDATE
315 6a4aa7c1 Iustin Pop
    if self.lu.sstore is None:
316 6a4aa7c1 Iustin Pop
      raise errors.ProgrammerError("Null sstore on config update hook")
317 6a4aa7c1 Iustin Pop
    nodes = [self.lu.sstore.GetMasterNode()]
318 6a4aa7c1 Iustin Pop
    results = self._RunWrapper(nodes, hpath, phase)