Statistics
| Branch: | Tag: | Revision:

root / lib / mcpu.py @ 9a39f854

History | View | Annotate | Download (10 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 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.OpInitCluster: cmdlib.LUInitCluster,
46 a8083063 Iustin Pop
    opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
47 a8083063 Iustin Pop
    opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
48 a8083063 Iustin Pop
    opcodes.OpClusterCopyFile: cmdlib.LUClusterCopyFile,
49 a8083063 Iustin Pop
    opcodes.OpRunClusterCommand: cmdlib.LURunClusterCommand,
50 a8083063 Iustin Pop
    opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
51 a8083063 Iustin Pop
    opcodes.OpMasterFailover: cmdlib.LUMasterFailover,
52 a8083063 Iustin Pop
    opcodes.OpDumpClusterConfig: cmdlib.LUDumpClusterConfig,
53 07bd8a51 Iustin Pop
    opcodes.OpRenameCluster: cmdlib.LURenameCluster,
54 f4d4e184 Iustin Pop
    opcodes.OpVerifyDisks: cmdlib.LUVerifyDisks,
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 a8083063 Iustin Pop
    opcodes.OpRemoveNode: cmdlib.LURemoveNode,
60 a8083063 Iustin Pop
    # instance lu
61 a8083063 Iustin Pop
    opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
62 fe7b0351 Michael Hanselmann
    opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance,
63 a8083063 Iustin Pop
    opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
64 decd5f45 Iustin Pop
    opcodes.OpRenameInstance: cmdlib.LURenameInstance,
65 a8083063 Iustin Pop
    opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
66 a8083063 Iustin Pop
    opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
67 a8083063 Iustin Pop
    opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
68 bf6929a2 Alexander Schreiber
    opcodes.OpRebootInstance: cmdlib.LURebootInstance,
69 a8083063 Iustin Pop
    opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
70 a8083063 Iustin Pop
    opcodes.OpAddMDDRBDComponent: cmdlib.LUAddMDDRBDComponent,
71 a8083063 Iustin Pop
    opcodes.OpRemoveMDDRBDComponent: cmdlib.LURemoveMDDRBDComponent,
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 a8083063 Iustin Pop
    opcodes.OpSetInstanceParms: cmdlib.LUSetInstanceParms,
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 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 a8083063 Iustin Pop
    }
91 a8083063 Iustin Pop
92 1a8c0ce1 Iustin Pop
  def __init__(self, feedback=None):
93 a8083063 Iustin Pop
    """Constructor for Processor
94 a8083063 Iustin Pop

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

106 a8083063 Iustin Pop
    Args:
107 c99a3cc0 Manuel Franceschini
      op: the opcode to be executed
108 a8083063 Iustin Pop

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

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

145 a8083063 Iustin Pop
    Args:
146 a8083063 Iustin Pop
     - opcode: the opcode to be executed
147 a8083063 Iustin Pop

148 a8083063 Iustin Pop
    """
149 a8083063 Iustin Pop
    if not isinstance(op, opcodes.OpCode):
150 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Non-opcode instance passed"
151 3ecf6786 Iustin Pop
                                   " to ExecOpcode")
152 a8083063 Iustin Pop
153 a8083063 Iustin Pop
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
154 a8083063 Iustin Pop
    if lu_class is None:
155 3ecf6786 Iustin Pop
      raise errors.OpCodeUnknown("Unknown opcode")
156 a8083063 Iustin Pop
157 a8083063 Iustin Pop
    if lu_class.REQ_CLUSTER and self.cfg is None:
158 a8083063 Iustin Pop
      self.cfg = config.ConfigWriter()
159 a8083063 Iustin Pop
      self.sstore = ssconf.SimpleStore()
160 f97a6b10 Iustin Pop
    #do_hooks = lu_class.HPATH is not None
161 a8083063 Iustin Pop
    lu = lu_class(self, op, self.cfg, self.sstore)
162 a8083063 Iustin Pop
    lu.CheckPrereq()
163 a8083063 Iustin Pop
    #if do_hooks:
164 2395c322 Iustin Pop
    #  hm = HooksMaster(rpc.call_hooks_runner, self, lu)
165 a8083063 Iustin Pop
    #  hm.RunPhase(constants.HOOKS_PHASE_PRE)
166 1a8c0ce1 Iustin Pop
    result = lu.Exec(self._feedback_fn)
167 a8083063 Iustin Pop
    #if do_hooks:
168 a8083063 Iustin Pop
    #  hm.RunPhase(constants.HOOKS_PHASE_POST)
169 a8083063 Iustin Pop
    return result
170 a8083063 Iustin Pop
171 0fbbf897 Iustin Pop
  def LogStep(self, current, total, message):
172 0fbbf897 Iustin Pop
    """Log a change in LU execution progress.
173 0fbbf897 Iustin Pop

174 0fbbf897 Iustin Pop
    """
175 0fbbf897 Iustin Pop
    logger.Debug("Step %d/%d %s" % (current, total, message))
176 0fbbf897 Iustin Pop
    self._feedback_fn("STEP %d/%d %s" % (current, total, message))
177 0fbbf897 Iustin Pop
178 5bfac263 Iustin Pop
  def LogWarning(self, message, hint=None):
179 0fbbf897 Iustin Pop
    """Log a warning to the logs and the user.
180 0fbbf897 Iustin Pop

181 0fbbf897 Iustin Pop
    """
182 0fbbf897 Iustin Pop
    logger.Error(message)
183 0fbbf897 Iustin Pop
    self._feedback_fn(" - WARNING: %s" % message)
184 5bfac263 Iustin Pop
    if hint:
185 5bfac263 Iustin Pop
      self._feedback_fn("      Hint: %s" % hint)
186 0fbbf897 Iustin Pop
187 0fbbf897 Iustin Pop
  def LogInfo(self, message):
188 0fbbf897 Iustin Pop
    """Log an informational message to the logs and the user.
189 0fbbf897 Iustin Pop

190 0fbbf897 Iustin Pop
    """
191 0fbbf897 Iustin Pop
    logger.Info(message)
192 0fbbf897 Iustin Pop
    self._feedback_fn(" - INFO: %s" % message)
193 0fbbf897 Iustin Pop
194 a8083063 Iustin Pop
195 a8083063 Iustin Pop
class HooksMaster(object):
196 a8083063 Iustin Pop
  """Hooks master.
197 a8083063 Iustin Pop

198 a8083063 Iustin Pop
  This class distributes the run commands to the nodes based on the
199 a8083063 Iustin Pop
  specific LU class.
200 a8083063 Iustin Pop

201 a8083063 Iustin Pop
  In order to remove the direct dependency on the rpc module, the
202 a8083063 Iustin Pop
  constructor needs a function which actually does the remote
203 a8083063 Iustin Pop
  call. This will usually be rpc.call_hooks_runner, but any function
204 a8083063 Iustin Pop
  which behaves the same works.
205 a8083063 Iustin Pop

206 a8083063 Iustin Pop
  """
207 2395c322 Iustin Pop
  def __init__(self, callfn, proc, lu):
208 a8083063 Iustin Pop
    self.callfn = callfn
209 2395c322 Iustin Pop
    self.proc = proc
210 a8083063 Iustin Pop
    self.lu = lu
211 a8083063 Iustin Pop
    self.op = lu.op
212 a8083063 Iustin Pop
    self.env, node_list_pre, node_list_post = self._BuildEnv()
213 a8083063 Iustin Pop
    self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
214 a8083063 Iustin Pop
                      constants.HOOKS_PHASE_POST: node_list_post}
215 a8083063 Iustin Pop
216 a8083063 Iustin Pop
  def _BuildEnv(self):
217 a8083063 Iustin Pop
    """Compute the environment and the target nodes.
218 a8083063 Iustin Pop

219 a8083063 Iustin Pop
    Based on the opcode and the current node list, this builds the
220 a8083063 Iustin Pop
    environment for the hooks and the target node list for the run.
221 a8083063 Iustin Pop

222 a8083063 Iustin Pop
    """
223 a8083063 Iustin Pop
    env = {
224 a8083063 Iustin Pop
      "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
225 a8083063 Iustin Pop
      "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
226 a8083063 Iustin Pop
      "GANETI_OP_CODE": self.op.OP_ID,
227 a8083063 Iustin Pop
      "GANETI_OBJECT_TYPE": self.lu.HTYPE,
228 6a4aa7c1 Iustin Pop
      "GANETI_DATA_DIR": constants.DATA_DIR,
229 a8083063 Iustin Pop
      }
230 a8083063 Iustin Pop
231 9a395a76 Iustin Pop
    if self.lu.HPATH is not None:
232 9a395a76 Iustin Pop
      lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
233 9a395a76 Iustin Pop
      if lu_env:
234 9a395a76 Iustin Pop
        for key in lu_env:
235 9a395a76 Iustin Pop
          env["GANETI_" + key] = lu_env[key]
236 9a395a76 Iustin Pop
    else:
237 9a395a76 Iustin Pop
      lu_nodes_pre = lu_nodes_post = []
238 a8083063 Iustin Pop
239 4167825b Iustin Pop
    return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
240 4167825b Iustin Pop
241 4167825b Iustin Pop
  def _RunWrapper(self, node_list, hpath, phase):
242 4167825b Iustin Pop
    """Simple wrapper over self.callfn.
243 4167825b Iustin Pop

244 4167825b Iustin Pop
    This method fixes the environment before doing the rpc call.
245 4167825b Iustin Pop

246 4167825b Iustin Pop
    """
247 4167825b Iustin Pop
    env = self.env.copy()
248 4167825b Iustin Pop
    env["GANETI_HOOKS_PHASE"] = phase
249 4167825b Iustin Pop
    env["GANETI_HOOKS_PATH"] = hpath
250 f97a6b10 Iustin Pop
    if self.lu.sstore is not None:
251 f97a6b10 Iustin Pop
      env["GANETI_CLUSTER"] = self.lu.sstore.GetClusterName()
252 f97a6b10 Iustin Pop
      env["GANETI_MASTER"] = self.lu.sstore.GetMasterNode()
253 a8083063 Iustin Pop
254 4167825b Iustin Pop
    env = dict([(str(key), str(val)) for key, val in env.iteritems()])
255 a8083063 Iustin Pop
256 4167825b Iustin Pop
    return self.callfn(node_list, hpath, phase, env)
257 a8083063 Iustin Pop
258 a8083063 Iustin Pop
  def RunPhase(self, phase):
259 a8083063 Iustin Pop
    """Run all the scripts for a phase.
260 a8083063 Iustin Pop

261 a8083063 Iustin Pop
    This is the main function of the HookMaster.
262 a8083063 Iustin Pop

263 a8083063 Iustin Pop
    """
264 a8083063 Iustin Pop
    if not self.node_list[phase]:
265 9a395a76 Iustin Pop
      # empty node list, we should not attempt to run this as either
266 9a395a76 Iustin Pop
      # we're in the cluster init phase and the rpc client part can't
267 9a395a76 Iustin Pop
      # even attempt to run, or this LU doesn't do hooks at all
268 a8083063 Iustin Pop
      return
269 4167825b Iustin Pop
    hpath = self.lu.HPATH
270 4167825b Iustin Pop
    results = self._RunWrapper(self.node_list[phase], hpath, phase)
271 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
272 a8083063 Iustin Pop
      errs = []
273 a8083063 Iustin Pop
      if not results:
274 3ecf6786 Iustin Pop
        raise errors.HooksFailure("Communication failure")
275 a8083063 Iustin Pop
      for node_name in results:
276 a8083063 Iustin Pop
        res = results[node_name]
277 a8083063 Iustin Pop
        if res is False or not isinstance(res, list):
278 2395c322 Iustin Pop
          self.proc.LogWarning("Communication failure to node %s" % node_name)
279 2395c322 Iustin Pop
          continue
280 a8083063 Iustin Pop
        for script, hkr, output in res:
281 a8083063 Iustin Pop
          if hkr == constants.HKR_FAIL:
282 a8083063 Iustin Pop
            output = output.strip().encode("string_escape")
283 a8083063 Iustin Pop
            errs.append((node_name, script, output))
284 a8083063 Iustin Pop
      if errs:
285 3ecf6786 Iustin Pop
        raise errors.HooksAbort(errs)
286 6a4aa7c1 Iustin Pop
287 6a4aa7c1 Iustin Pop
  def RunConfigUpdate(self):
288 6a4aa7c1 Iustin Pop
    """Run the special configuration update hook
289 6a4aa7c1 Iustin Pop

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

293 6a4aa7c1 Iustin Pop
    """
294 6a4aa7c1 Iustin Pop
    phase = constants.HOOKS_PHASE_POST
295 6a4aa7c1 Iustin Pop
    hpath = constants.HOOKS_NAME_CFGUPDATE
296 6a4aa7c1 Iustin Pop
    if self.lu.sstore is None:
297 6a4aa7c1 Iustin Pop
      raise errors.ProgrammerError("Null sstore on config update hook")
298 6a4aa7c1 Iustin Pop
    nodes = [self.lu.sstore.GetMasterNode()]
299 6a4aa7c1 Iustin Pop
    results = self._RunWrapper(nodes, hpath, phase)