Statistics
| Branch: | Tag: | Revision:

root / lib / mcpu.py @ dcb93971

History | View | Annotate | Download (8.2 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 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
"""Module implementing the logic behind the cluster operations
23

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

29
"""
30

    
31

    
32
import os
33
import os.path
34
import time
35

    
36
from ganeti import opcodes
37
from ganeti import logger
38
from ganeti import constants
39
from ganeti import utils
40
from ganeti import errors
41
from ganeti import rpc
42
from ganeti import cmdlib
43
from ganeti import config
44
from ganeti import ssconf
45

    
46
class Processor(object):
47
  """Object which runs OpCodes"""
48
  DISPATCH_TABLE = {
49
    # Cluster
50
    opcodes.OpInitCluster: cmdlib.LUInitCluster,
51
    opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster,
52
    opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo,
53
    opcodes.OpClusterCopyFile: cmdlib.LUClusterCopyFile,
54
    opcodes.OpRunClusterCommand: cmdlib.LURunClusterCommand,
55
    opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster,
56
    opcodes.OpMasterFailover: cmdlib.LUMasterFailover,
57
    opcodes.OpDumpClusterConfig: cmdlib.LUDumpClusterConfig,
58
    # node lu
59
    opcodes.OpAddNode: cmdlib.LUAddNode,
60
    opcodes.OpQueryNodes: cmdlib.LUQueryNodes,
61
    opcodes.OpQueryNodeData: cmdlib.LUQueryNodeData,
62
    opcodes.OpQueryNodeVolumes: cmdlib.LUQueryNodeVolumes,
63
    opcodes.OpRemoveNode: cmdlib.LURemoveNode,
64
    # instance lu
65
    opcodes.OpCreateInstance: cmdlib.LUCreateInstance,
66
    opcodes.OpRemoveInstance: cmdlib.LURemoveInstance,
67
    opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks,
68
    opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance,
69
    opcodes.OpStartupInstance: cmdlib.LUStartupInstance,
70
    opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks,
71
    opcodes.OpAddMDDRBDComponent: cmdlib.LUAddMDDRBDComponent,
72
    opcodes.OpRemoveMDDRBDComponent: cmdlib.LURemoveMDDRBDComponent,
73
    opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks,
74
    opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance,
75
    opcodes.OpConnectConsole: cmdlib.LUConnectConsole,
76
    opcodes.OpQueryInstances: cmdlib.LUQueryInstances,
77
    opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
78
    opcodes.OpSetInstanceParms: cmdlib.LUSetInstanceParms,
79
    # os lu
80
    opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
81
    # exports lu
82
    opcodes.OpQueryExports: cmdlib.LUQueryExports,
83
    opcodes.OpExportInstance: cmdlib.LUExportInstance,
84
    }
85

    
86

    
87
  def __init__(self):
88
    """Constructor for Processor
89

90
    """
91
    self.cfg = None
92
    self.sstore = None
93

    
94
  def ExecOpCode(self, op, feedback_fn):
95
    """Execute an opcode.
96

97
    Args:
98
     - cfg: the configuration in which we execute this opcode
99
     - opcode: the opcode to be executed
100
     - feedback_fn: the feedback function (taking one string) to be run when
101
                    interesting events are happening
102

103
    """
104
    if not isinstance(op, opcodes.OpCode):
105
      raise errors.ProgrammerError, ("Non-opcode instance passed"
106
                                     " to ExecOpcode")
107

    
108
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
109
    if lu_class is None:
110
      raise errors.OpCodeUnknown, "Unknown opcode"
111

    
112
    if lu_class.REQ_CLUSTER and self.cfg is None:
113
      self.cfg = config.ConfigWriter()
114
      self.sstore = ssconf.SimpleStore()
115
    lu = lu_class(self, op, self.cfg, self.sstore)
116
    lu.CheckPrereq()
117
    do_hooks = lu_class.HPATH is not None
118
    if do_hooks:
119
      hm = HooksMaster(rpc.call_hooks_runner, self.cfg, self.sstore, lu)
120
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
121
    result = lu.Exec(feedback_fn)
122
    if do_hooks:
123
      hm.RunPhase(constants.HOOKS_PHASE_POST)
124
    return result
125

    
126
  def ChainOpCode(self, op, feedback_fn):
127
    """Chain and execute an opcode.
128

129
    This is used by LUs when they need to execute a child LU.
130

131
    Args:
132
     - opcode: the opcode to be executed
133
     - feedback_fn: the feedback function (taking one string) to be run when
134
                    interesting events are happening
135

136
    """
137
    if not isinstance(op, opcodes.OpCode):
138
      raise errors.ProgrammerError, ("Non-opcode instance passed"
139
                                     " to ExecOpcode")
140

    
141
    lu_class = self.DISPATCH_TABLE.get(op.__class__, None)
142
    if lu_class is None:
143
      raise errors.OpCodeUnknown, "Unknown opcode"
144

    
145
    if lu_class.REQ_CLUSTER and self.cfg is None:
146
      self.cfg = config.ConfigWriter()
147
      self.sstore = ssconf.SimpleStore()
148
    do_hooks = lu_class.HPATH is not None
149
    lu = lu_class(self, op, self.cfg, self.sstore)
150
    lu.CheckPrereq()
151
    #if do_hooks:
152
    #  hm = HooksMaster(rpc.call_hooks_runner, self.cfg, self.sstore, lu)
153
    #  hm.RunPhase(constants.HOOKS_PHASE_PRE)
154
    result = lu.Exec(feedback_fn)
155
    #if do_hooks:
156
    #  hm.RunPhase(constants.HOOKS_PHASE_POST)
157
    return result
158

    
159

    
160
class HooksMaster(object):
161
  """Hooks master.
162

163
  This class distributes the run commands to the nodes based on the
164
  specific LU class.
165

166
  In order to remove the direct dependency on the rpc module, the
167
  constructor needs a function which actually does the remote
168
  call. This will usually be rpc.call_hooks_runner, but any function
169
  which behaves the same works.
170

171
  """
172
  def __init__(self, callfn, cfg, sstore, lu):
173
    self.callfn = callfn
174
    self.cfg = cfg
175
    self.sstore = sstore
176
    self.lu = lu
177
    self.op = lu.op
178
    self.hpath = self.lu.HPATH
179
    self.env, node_list_pre, node_list_post = self._BuildEnv()
180

    
181
    self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
182
                      constants.HOOKS_PHASE_POST: node_list_post}
183

    
184
  def _BuildEnv(self):
185
    """Compute the environment and the target nodes.
186

187
    Based on the opcode and the current node list, this builds the
188
    environment for the hooks and the target node list for the run.
189

190
    """
191
    env = {
192
      "PATH": "/sbin:/bin:/usr/sbin:/usr/bin",
193
      "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
194
      "GANETI_OP_CODE": self.op.OP_ID,
195
      "GANETI_OBJECT_TYPE": self.lu.HTYPE,
196
      }
197

    
198
    lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
199
    if lu_env:
200
      for key in lu_env:
201
        env["GANETI_" + key] = lu_env[key]
202

    
203
    if self.cfg is not None:
204
      env["GANETI_CLUSTER"] = self.cfg.GetClusterName()
205
    if self.sstore is not None:
206
      env["GANETI_MASTER"] = self.sstore.GetMasterNode()
207

    
208
    for key in env:
209
      if not isinstance(env[key], str):
210
        env[key] = str(env[key])
211

    
212
    return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post)
213

    
214
  def RunPhase(self, phase):
215
    """Run all the scripts for a phase.
216

217
    This is the main function of the HookMaster.
218

219
    """
220
    if not self.node_list[phase]:
221
      # empty node list, we should not attempt to run this
222
      # as most probably we're in the cluster init phase and the rpc client
223
      # part can't even attempt to run
224
      return
225
    self.env["GANETI_HOOKS_PHASE"] = str(phase)
226
    results = self.callfn(self.node_list[phase], self.hpath, phase, self.env)
227
    if phase == constants.HOOKS_PHASE_PRE:
228
      errs = []
229
      if not results:
230
        raise errors.HooksFailure, "Communication failure"
231
      for node_name in results:
232
        res = results[node_name]
233
        if res is False or not isinstance(res, list):
234
          raise errors.HooksFailure, ("Communication failure to node %s" %
235
                                      node_name)
236
        for script, hkr, output in res:
237
          if hkr == constants.HKR_FAIL:
238
            output = output.strip().encode("string_escape")
239
            errs.append((node_name, script, output))
240
      if errs:
241
        raise errors.HooksAbort, errs