root / lib / mcpu.py @ 4167825b
History | View | Annotate | Download (8.4 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 | a8083063 | Iustin Pop | |
40 | a8083063 | Iustin Pop | class Processor(object): |
41 | a8083063 | Iustin Pop | """Object which runs OpCodes"""
|
42 | a8083063 | Iustin Pop | DISPATCH_TABLE = { |
43 | a8083063 | Iustin Pop | # Cluster
|
44 | a8083063 | Iustin Pop | opcodes.OpInitCluster: cmdlib.LUInitCluster, |
45 | a8083063 | Iustin Pop | opcodes.OpDestroyCluster: cmdlib.LUDestroyCluster, |
46 | a8083063 | Iustin Pop | opcodes.OpQueryClusterInfo: cmdlib.LUQueryClusterInfo, |
47 | a8083063 | Iustin Pop | opcodes.OpClusterCopyFile: cmdlib.LUClusterCopyFile, |
48 | a8083063 | Iustin Pop | opcodes.OpRunClusterCommand: cmdlib.LURunClusterCommand, |
49 | a8083063 | Iustin Pop | opcodes.OpVerifyCluster: cmdlib.LUVerifyCluster, |
50 | a8083063 | Iustin Pop | opcodes.OpMasterFailover: cmdlib.LUMasterFailover, |
51 | a8083063 | Iustin Pop | opcodes.OpDumpClusterConfig: cmdlib.LUDumpClusterConfig, |
52 | 07bd8a51 | Iustin Pop | opcodes.OpRenameCluster: cmdlib.LURenameCluster, |
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 | a8083063 | Iustin Pop | # instance lu
|
59 | a8083063 | Iustin Pop | opcodes.OpCreateInstance: cmdlib.LUCreateInstance, |
60 | fe7b0351 | Michael Hanselmann | opcodes.OpReinstallInstance: cmdlib.LUReinstallInstance, |
61 | a8083063 | Iustin Pop | opcodes.OpRemoveInstance: cmdlib.LURemoveInstance, |
62 | decd5f45 | Iustin Pop | opcodes.OpRenameInstance: cmdlib.LURenameInstance, |
63 | a8083063 | Iustin Pop | opcodes.OpActivateInstanceDisks: cmdlib.LUActivateInstanceDisks, |
64 | a8083063 | Iustin Pop | opcodes.OpShutdownInstance: cmdlib.LUShutdownInstance, |
65 | a8083063 | Iustin Pop | opcodes.OpStartupInstance: cmdlib.LUStartupInstance, |
66 | a8083063 | Iustin Pop | opcodes.OpDeactivateInstanceDisks: cmdlib.LUDeactivateInstanceDisks, |
67 | a8083063 | Iustin Pop | opcodes.OpAddMDDRBDComponent: cmdlib.LUAddMDDRBDComponent, |
68 | a8083063 | Iustin Pop | opcodes.OpRemoveMDDRBDComponent: cmdlib.LURemoveMDDRBDComponent, |
69 | a8083063 | Iustin Pop | opcodes.OpReplaceDisks: cmdlib.LUReplaceDisks, |
70 | a8083063 | Iustin Pop | opcodes.OpFailoverInstance: cmdlib.LUFailoverInstance, |
71 | a8083063 | Iustin Pop | opcodes.OpConnectConsole: cmdlib.LUConnectConsole, |
72 | a8083063 | Iustin Pop | opcodes.OpQueryInstances: cmdlib.LUQueryInstances, |
73 | a8083063 | Iustin Pop | opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData, |
74 | a8083063 | Iustin Pop | opcodes.OpSetInstanceParms: cmdlib.LUSetInstanceParms, |
75 | a8083063 | Iustin Pop | # os lu
|
76 | a8083063 | Iustin Pop | opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS, |
77 | a8083063 | Iustin Pop | # exports lu
|
78 | a8083063 | Iustin Pop | opcodes.OpQueryExports: cmdlib.LUQueryExports, |
79 | a8083063 | Iustin Pop | opcodes.OpExportInstance: cmdlib.LUExportInstance, |
80 | 5c947f38 | Iustin Pop | # tags lu
|
81 | 5c947f38 | Iustin Pop | opcodes.OpGetTags: cmdlib.LUGetTags, |
82 | f27302fa | Iustin Pop | opcodes.OpAddTags: cmdlib.LUAddTags, |
83 | f27302fa | Iustin Pop | opcodes.OpDelTags: cmdlib.LUDelTags, |
84 | a8083063 | Iustin Pop | } |
85 | a8083063 | Iustin Pop | |
86 | a8083063 | Iustin Pop | |
87 | a8083063 | Iustin Pop | def __init__(self): |
88 | a8083063 | Iustin Pop | """Constructor for Processor
|
89 | a8083063 | Iustin Pop |
|
90 | a8083063 | Iustin Pop | """
|
91 | a8083063 | Iustin Pop | self.cfg = None |
92 | a8083063 | Iustin Pop | self.sstore = None |
93 | a8083063 | Iustin Pop | |
94 | a8083063 | Iustin Pop | def ExecOpCode(self, op, feedback_fn): |
95 | a8083063 | Iustin Pop | """Execute an opcode.
|
96 | a8083063 | Iustin Pop |
|
97 | a8083063 | Iustin Pop | Args:
|
98 | a8083063 | Iustin Pop | - cfg: the configuration in which we execute this opcode
|
99 | a8083063 | Iustin Pop | - opcode: the opcode to be executed
|
100 | a8083063 | Iustin Pop | - feedback_fn: the feedback function (taking one string) to be run when
|
101 | a8083063 | Iustin Pop | interesting events are happening
|
102 | a8083063 | Iustin Pop |
|
103 | a8083063 | Iustin Pop | """
|
104 | a8083063 | Iustin Pop | if not isinstance(op, opcodes.OpCode): |
105 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Non-opcode instance passed" |
106 | 3ecf6786 | Iustin Pop | " to ExecOpcode")
|
107 | a8083063 | Iustin Pop | |
108 | a8083063 | Iustin Pop | lu_class = self.DISPATCH_TABLE.get(op.__class__, None) |
109 | a8083063 | Iustin Pop | if lu_class is None: |
110 | 3ecf6786 | Iustin Pop | raise errors.OpCodeUnknown("Unknown opcode") |
111 | a8083063 | Iustin Pop | |
112 | a8083063 | Iustin Pop | if lu_class.REQ_CLUSTER and self.cfg is None: |
113 | a8083063 | Iustin Pop | self.cfg = config.ConfigWriter()
|
114 | a8083063 | Iustin Pop | self.sstore = ssconf.SimpleStore()
|
115 | a8083063 | Iustin Pop | lu = lu_class(self, op, self.cfg, self.sstore) |
116 | a8083063 | Iustin Pop | lu.CheckPrereq() |
117 | 9a395a76 | Iustin Pop | hm = HooksMaster(rpc.call_hooks_runner, lu) |
118 | 9a395a76 | Iustin Pop | hm.RunPhase(constants.HOOKS_PHASE_PRE) |
119 | a8083063 | Iustin Pop | result = lu.Exec(feedback_fn) |
120 | 9a395a76 | Iustin Pop | hm.RunPhase(constants.HOOKS_PHASE_POST) |
121 | a8083063 | Iustin Pop | return result
|
122 | a8083063 | Iustin Pop | |
123 | a8083063 | Iustin Pop | def ChainOpCode(self, op, feedback_fn): |
124 | a8083063 | Iustin Pop | """Chain and execute an opcode.
|
125 | a8083063 | Iustin Pop |
|
126 | a8083063 | Iustin Pop | This is used by LUs when they need to execute a child LU.
|
127 | a8083063 | Iustin Pop |
|
128 | a8083063 | Iustin Pop | Args:
|
129 | a8083063 | Iustin Pop | - opcode: the opcode to be executed
|
130 | a8083063 | Iustin Pop | - feedback_fn: the feedback function (taking one string) to be run when
|
131 | a8083063 | Iustin Pop | interesting events are happening
|
132 | a8083063 | Iustin Pop |
|
133 | a8083063 | Iustin Pop | """
|
134 | a8083063 | Iustin Pop | if not isinstance(op, opcodes.OpCode): |
135 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Non-opcode instance passed" |
136 | 3ecf6786 | Iustin Pop | " to ExecOpcode")
|
137 | a8083063 | Iustin Pop | |
138 | a8083063 | Iustin Pop | lu_class = self.DISPATCH_TABLE.get(op.__class__, None) |
139 | a8083063 | Iustin Pop | if lu_class is None: |
140 | 3ecf6786 | Iustin Pop | raise errors.OpCodeUnknown("Unknown opcode") |
141 | a8083063 | Iustin Pop | |
142 | a8083063 | Iustin Pop | if lu_class.REQ_CLUSTER and self.cfg is None: |
143 | a8083063 | Iustin Pop | self.cfg = config.ConfigWriter()
|
144 | a8083063 | Iustin Pop | self.sstore = ssconf.SimpleStore()
|
145 | f97a6b10 | Iustin Pop | #do_hooks = lu_class.HPATH is not None
|
146 | a8083063 | Iustin Pop | lu = lu_class(self, op, self.cfg, self.sstore) |
147 | a8083063 | Iustin Pop | lu.CheckPrereq() |
148 | a8083063 | Iustin Pop | #if do_hooks:
|
149 | f97a6b10 | Iustin Pop | # hm = HooksMaster(rpc.call_hooks_runner, lu)
|
150 | a8083063 | Iustin Pop | # hm.RunPhase(constants.HOOKS_PHASE_PRE)
|
151 | a8083063 | Iustin Pop | result = lu.Exec(feedback_fn) |
152 | a8083063 | Iustin Pop | #if do_hooks:
|
153 | a8083063 | Iustin Pop | # hm.RunPhase(constants.HOOKS_PHASE_POST)
|
154 | a8083063 | Iustin Pop | return result
|
155 | a8083063 | Iustin Pop | |
156 | a8083063 | Iustin Pop | |
157 | a8083063 | Iustin Pop | class HooksMaster(object): |
158 | a8083063 | Iustin Pop | """Hooks master.
|
159 | a8083063 | Iustin Pop |
|
160 | a8083063 | Iustin Pop | This class distributes the run commands to the nodes based on the
|
161 | a8083063 | Iustin Pop | specific LU class.
|
162 | a8083063 | Iustin Pop |
|
163 | a8083063 | Iustin Pop | In order to remove the direct dependency on the rpc module, the
|
164 | a8083063 | Iustin Pop | constructor needs a function which actually does the remote
|
165 | a8083063 | Iustin Pop | call. This will usually be rpc.call_hooks_runner, but any function
|
166 | a8083063 | Iustin Pop | which behaves the same works.
|
167 | a8083063 | Iustin Pop |
|
168 | a8083063 | Iustin Pop | """
|
169 | f97a6b10 | Iustin Pop | def __init__(self, callfn, lu): |
170 | a8083063 | Iustin Pop | self.callfn = callfn
|
171 | a8083063 | Iustin Pop | self.lu = lu
|
172 | a8083063 | Iustin Pop | self.op = lu.op
|
173 | a8083063 | Iustin Pop | self.env, node_list_pre, node_list_post = self._BuildEnv() |
174 | a8083063 | Iustin Pop | self.node_list = {constants.HOOKS_PHASE_PRE: node_list_pre,
|
175 | a8083063 | Iustin Pop | constants.HOOKS_PHASE_POST: node_list_post} |
176 | a8083063 | Iustin Pop | |
177 | a8083063 | Iustin Pop | def _BuildEnv(self): |
178 | a8083063 | Iustin Pop | """Compute the environment and the target nodes.
|
179 | a8083063 | Iustin Pop |
|
180 | a8083063 | Iustin Pop | Based on the opcode and the current node list, this builds the
|
181 | a8083063 | Iustin Pop | environment for the hooks and the target node list for the run.
|
182 | a8083063 | Iustin Pop |
|
183 | a8083063 | Iustin Pop | """
|
184 | a8083063 | Iustin Pop | env = { |
185 | a8083063 | Iustin Pop | "PATH": "/sbin:/bin:/usr/sbin:/usr/bin", |
186 | a8083063 | Iustin Pop | "GANETI_HOOKS_VERSION": constants.HOOKS_VERSION,
|
187 | a8083063 | Iustin Pop | "GANETI_OP_CODE": self.op.OP_ID, |
188 | a8083063 | Iustin Pop | "GANETI_OBJECT_TYPE": self.lu.HTYPE, |
189 | a8083063 | Iustin Pop | } |
190 | a8083063 | Iustin Pop | |
191 | 9a395a76 | Iustin Pop | if self.lu.HPATH is not None: |
192 | 9a395a76 | Iustin Pop | lu_env, lu_nodes_pre, lu_nodes_post = self.lu.BuildHooksEnv()
|
193 | 9a395a76 | Iustin Pop | if lu_env:
|
194 | 9a395a76 | Iustin Pop | for key in lu_env: |
195 | 9a395a76 | Iustin Pop | env["GANETI_" + key] = lu_env[key]
|
196 | 9a395a76 | Iustin Pop | else:
|
197 | 9a395a76 | Iustin Pop | lu_nodes_pre = lu_nodes_post = [] |
198 | a8083063 | Iustin Pop | |
199 | 4167825b | Iustin Pop | return env, frozenset(lu_nodes_pre), frozenset(lu_nodes_post) |
200 | 4167825b | Iustin Pop | |
201 | 4167825b | Iustin Pop | def _RunWrapper(self, node_list, hpath, phase): |
202 | 4167825b | Iustin Pop | """Simple wrapper over self.callfn.
|
203 | 4167825b | Iustin Pop |
|
204 | 4167825b | Iustin Pop | This method fixes the environment before doing the rpc call.
|
205 | 4167825b | Iustin Pop |
|
206 | 4167825b | Iustin Pop | """
|
207 | 4167825b | Iustin Pop | env = self.env.copy()
|
208 | 4167825b | Iustin Pop | env["GANETI_HOOKS_PHASE"] = phase
|
209 | 4167825b | Iustin Pop | env["GANETI_HOOKS_PATH"] = hpath
|
210 | f97a6b10 | Iustin Pop | if self.lu.sstore is not None: |
211 | f97a6b10 | Iustin Pop | env["GANETI_CLUSTER"] = self.lu.sstore.GetClusterName() |
212 | f97a6b10 | Iustin Pop | env["GANETI_MASTER"] = self.lu.sstore.GetMasterNode() |
213 | a8083063 | Iustin Pop | |
214 | 4167825b | Iustin Pop | env = dict([(str(key), str(val)) for key, val in env.iteritems()]) |
215 | a8083063 | Iustin Pop | |
216 | 4167825b | Iustin Pop | return self.callfn(node_list, hpath, phase, env) |
217 | a8083063 | Iustin Pop | |
218 | a8083063 | Iustin Pop | def RunPhase(self, phase): |
219 | a8083063 | Iustin Pop | """Run all the scripts for a phase.
|
220 | a8083063 | Iustin Pop |
|
221 | a8083063 | Iustin Pop | This is the main function of the HookMaster.
|
222 | a8083063 | Iustin Pop |
|
223 | a8083063 | Iustin Pop | """
|
224 | a8083063 | Iustin Pop | if not self.node_list[phase]: |
225 | 9a395a76 | Iustin Pop | # empty node list, we should not attempt to run this as either
|
226 | 9a395a76 | Iustin Pop | # we're in the cluster init phase and the rpc client part can't
|
227 | 9a395a76 | Iustin Pop | # even attempt to run, or this LU doesn't do hooks at all
|
228 | a8083063 | Iustin Pop | return
|
229 | 4167825b | Iustin Pop | hpath = self.lu.HPATH
|
230 | 4167825b | Iustin Pop | results = self._RunWrapper(self.node_list[phase], hpath, phase) |
231 | a8083063 | Iustin Pop | if phase == constants.HOOKS_PHASE_PRE:
|
232 | a8083063 | Iustin Pop | errs = [] |
233 | a8083063 | Iustin Pop | if not results: |
234 | 3ecf6786 | Iustin Pop | raise errors.HooksFailure("Communication failure") |
235 | a8083063 | Iustin Pop | for node_name in results: |
236 | a8083063 | Iustin Pop | res = results[node_name] |
237 | a8083063 | Iustin Pop | if res is False or not isinstance(res, list): |
238 | 3ecf6786 | Iustin Pop | raise errors.HooksFailure("Communication failure to node %s" % |
239 | 3ecf6786 | Iustin Pop | node_name) |
240 | a8083063 | Iustin Pop | for script, hkr, output in res: |
241 | a8083063 | Iustin Pop | if hkr == constants.HKR_FAIL:
|
242 | a8083063 | Iustin Pop | output = output.strip().encode("string_escape")
|
243 | a8083063 | Iustin Pop | errs.append((node_name, script, output)) |
244 | a8083063 | Iustin Pop | if errs:
|
245 | 3ecf6786 | Iustin Pop | raise errors.HooksAbort(errs) |