root / lib / backend.py @ a9369c6e
History | View | Annotate | Download (49.3 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 | """Functions used by the node daemon"""
|
23 | a8083063 | Iustin Pop | |
24 | a8083063 | Iustin Pop | |
25 | a8083063 | Iustin Pop | import os |
26 | a8083063 | Iustin Pop | import os.path |
27 | a8083063 | Iustin Pop | import shutil |
28 | a8083063 | Iustin Pop | import time |
29 | a8083063 | Iustin Pop | import stat |
30 | a8083063 | Iustin Pop | import errno |
31 | a8083063 | Iustin Pop | import re |
32 | a8083063 | Iustin Pop | import subprocess |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | from ganeti import logger |
35 | a8083063 | Iustin Pop | from ganeti import errors |
36 | a8083063 | Iustin Pop | from ganeti import utils |
37 | a8083063 | Iustin Pop | from ganeti import ssh |
38 | a8083063 | Iustin Pop | from ganeti import hypervisor |
39 | a8083063 | Iustin Pop | from ganeti import constants |
40 | a8083063 | Iustin Pop | from ganeti import bdev |
41 | a8083063 | Iustin Pop | from ganeti import objects |
42 | 880478f8 | Iustin Pop | from ganeti import ssconf |
43 | a8083063 | Iustin Pop | |
44 | a8083063 | Iustin Pop | |
45 | c92b310a | Michael Hanselmann | def _GetSshRunner(): |
46 | c92b310a | Michael Hanselmann | return ssh.SshRunner()
|
47 | c92b310a | Michael Hanselmann | |
48 | c92b310a | Michael Hanselmann | |
49 | a8083063 | Iustin Pop | def StartMaster(): |
50 | a8083063 | Iustin Pop | """Activate local node as master node.
|
51 | a8083063 | Iustin Pop |
|
52 | a8083063 | Iustin Pop | There are two needed steps for this:
|
53 | 880478f8 | Iustin Pop | - run the master script
|
54 | a8083063 | Iustin Pop | - register the cron script
|
55 | a8083063 | Iustin Pop |
|
56 | a8083063 | Iustin Pop | """
|
57 | 880478f8 | Iustin Pop | result = utils.RunCmd([constants.MASTER_SCRIPT, "-d", "start"]) |
58 | a8083063 | Iustin Pop | |
59 | a8083063 | Iustin Pop | if result.failed:
|
60 | a8083063 | Iustin Pop | logger.Error("could not activate cluster interface with command %s,"
|
61 | 880478f8 | Iustin Pop | " error: '%s'" % (result.cmd, result.output))
|
62 | a8083063 | Iustin Pop | return False |
63 | a8083063 | Iustin Pop | |
64 | a8083063 | Iustin Pop | return True |
65 | a8083063 | Iustin Pop | |
66 | a8083063 | Iustin Pop | |
67 | a8083063 | Iustin Pop | def StopMaster(): |
68 | a8083063 | Iustin Pop | """Deactivate this node as master.
|
69 | a8083063 | Iustin Pop |
|
70 | c9064964 | Iustin Pop | This runs the master stop script.
|
71 | a8083063 | Iustin Pop |
|
72 | a8083063 | Iustin Pop | """
|
73 | 880478f8 | Iustin Pop | result = utils.RunCmd([constants.MASTER_SCRIPT, "-d", "stop"]) |
74 | a8083063 | Iustin Pop | |
75 | a8083063 | Iustin Pop | if result.failed:
|
76 | a8083063 | Iustin Pop | logger.Error("could not deactivate cluster interface with command %s,"
|
77 | 880478f8 | Iustin Pop | " error: '%s'" % (result.cmd, result.output))
|
78 | a8083063 | Iustin Pop | return False |
79 | a8083063 | Iustin Pop | |
80 | a8083063 | Iustin Pop | return True |
81 | a8083063 | Iustin Pop | |
82 | a8083063 | Iustin Pop | |
83 | 9716fdce | Iustin Pop | def AddNode(dsa, dsapub, rsa, rsapub, sshkey, sshpub): |
84 | 7900ed01 | Iustin Pop | """Joins this node to the cluster.
|
85 | a8083063 | Iustin Pop |
|
86 | 7900ed01 | Iustin Pop | This does the following:
|
87 | 7900ed01 | Iustin Pop | - updates the hostkeys of the machine (rsa and dsa)
|
88 | 7900ed01 | Iustin Pop | - adds the ssh private key to the user
|
89 | 7900ed01 | Iustin Pop | - adds the ssh public key to the users' authorized_keys file
|
90 | a8083063 | Iustin Pop |
|
91 | 7900ed01 | Iustin Pop | """
|
92 | 70d9e3d8 | Iustin Pop | sshd_keys = [(constants.SSH_HOST_RSA_PRIV, rsa, 0600),
|
93 | 70d9e3d8 | Iustin Pop | (constants.SSH_HOST_RSA_PUB, rsapub, 0644),
|
94 | 70d9e3d8 | Iustin Pop | (constants.SSH_HOST_DSA_PRIV, dsa, 0600),
|
95 | 70d9e3d8 | Iustin Pop | (constants.SSH_HOST_DSA_PUB, dsapub, 0644)]
|
96 | 7900ed01 | Iustin Pop | for name, content, mode in sshd_keys: |
97 | 70d9e3d8 | Iustin Pop | utils.WriteFile(name, data=content, mode=mode) |
98 | a8083063 | Iustin Pop | |
99 | 70d9e3d8 | Iustin Pop | try:
|
100 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS, |
101 | 70d9e3d8 | Iustin Pop | mkdir=True)
|
102 | 70d9e3d8 | Iustin Pop | except errors.OpExecError, err:
|
103 | 70d9e3d8 | Iustin Pop | logger.Error("Error while processing user ssh files: %s" % err)
|
104 | 70d9e3d8 | Iustin Pop | return False |
105 | a8083063 | Iustin Pop | |
106 | 70d9e3d8 | Iustin Pop | for name, content in [(priv_key, sshkey), (pub_key, sshpub)]: |
107 | 70d9e3d8 | Iustin Pop | utils.WriteFile(name, data=content, mode=0600)
|
108 | a8083063 | Iustin Pop | |
109 | 70d9e3d8 | Iustin Pop | utils.AddAuthorizedKey(auth_keys, sshpub) |
110 | a8083063 | Iustin Pop | |
111 | f491c3a8 | Michael Hanselmann | utils.RunCmd([constants.SSH_INITD_SCRIPT, "restart"])
|
112 | a8083063 | Iustin Pop | |
113 | a8083063 | Iustin Pop | return True |
114 | a8083063 | Iustin Pop | |
115 | a8083063 | Iustin Pop | |
116 | a8083063 | Iustin Pop | def LeaveCluster(): |
117 | a8083063 | Iustin Pop | """Cleans up the current node and prepares it to be removed from the cluster.
|
118 | a8083063 | Iustin Pop |
|
119 | a8083063 | Iustin Pop | """
|
120 | 71eca7c3 | Iustin Pop | if os.path.isdir(constants.DATA_DIR):
|
121 | 71eca7c3 | Iustin Pop | for rel_name in utils.ListVisibleFiles(constants.DATA_DIR): |
122 | 71eca7c3 | Iustin Pop | full_name = os.path.join(constants.DATA_DIR, rel_name) |
123 | 71eca7c3 | Iustin Pop | if os.path.isfile(full_name) and not os.path.islink(full_name): |
124 | 71eca7c3 | Iustin Pop | utils.RemoveFile(full_name) |
125 | a8083063 | Iustin Pop | |
126 | 70d9e3d8 | Iustin Pop | try:
|
127 | 70d9e3d8 | Iustin Pop | priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS) |
128 | 70d9e3d8 | Iustin Pop | except errors.OpExecError, err:
|
129 | 70d9e3d8 | Iustin Pop | logger.Error("Error while processing ssh files: %s" % err)
|
130 | 7900ed01 | Iustin Pop | return
|
131 | 7900ed01 | Iustin Pop | |
132 | 70d9e3d8 | Iustin Pop | f = open(pub_key, 'r') |
133 | a8083063 | Iustin Pop | try:
|
134 | 70d9e3d8 | Iustin Pop | utils.RemoveAuthorizedKey(auth_keys, f.read(8192))
|
135 | a8083063 | Iustin Pop | finally:
|
136 | a8083063 | Iustin Pop | f.close() |
137 | a8083063 | Iustin Pop | |
138 | 70d9e3d8 | Iustin Pop | utils.RemoveFile(priv_key) |
139 | 70d9e3d8 | Iustin Pop | utils.RemoveFile(pub_key) |
140 | a8083063 | Iustin Pop | |
141 | a8083063 | Iustin Pop | |
142 | a8083063 | Iustin Pop | def GetNodeInfo(vgname): |
143 | 2f8598a5 | Alexander Schreiber | """Gives back a hash with different informations about the node.
|
144 | a8083063 | Iustin Pop |
|
145 | a8083063 | Iustin Pop | Returns:
|
146 | a8083063 | Iustin Pop | { 'vg_size' : xxx, 'vg_free' : xxx, 'memory_domain0': xxx,
|
147 | a8083063 | Iustin Pop | 'memory_free' : xxx, 'memory_total' : xxx }
|
148 | a8083063 | Iustin Pop | where
|
149 | a8083063 | Iustin Pop | vg_size is the size of the configured volume group in MiB
|
150 | a8083063 | Iustin Pop | vg_free is the free size of the volume group in MiB
|
151 | a8083063 | Iustin Pop | memory_dom0 is the memory allocated for domain0 in MiB
|
152 | a8083063 | Iustin Pop | memory_free is the currently available (free) ram in MiB
|
153 | a8083063 | Iustin Pop | memory_total is the total number of ram in MiB
|
154 | a8083063 | Iustin Pop |
|
155 | 098c0958 | Michael Hanselmann | """
|
156 | a8083063 | Iustin Pop | outputarray = {} |
157 | a8083063 | Iustin Pop | vginfo = _GetVGInfo(vgname) |
158 | a8083063 | Iustin Pop | outputarray['vg_size'] = vginfo['vg_size'] |
159 | a8083063 | Iustin Pop | outputarray['vg_free'] = vginfo['vg_free'] |
160 | a8083063 | Iustin Pop | |
161 | a8083063 | Iustin Pop | hyper = hypervisor.GetHypervisor() |
162 | a8083063 | Iustin Pop | hyp_info = hyper.GetNodeInfo() |
163 | a8083063 | Iustin Pop | if hyp_info is not None: |
164 | a8083063 | Iustin Pop | outputarray.update(hyp_info) |
165 | a8083063 | Iustin Pop | |
166 | 3ef10550 | Michael Hanselmann | f = open("/proc/sys/kernel/random/boot_id", 'r') |
167 | 3ef10550 | Michael Hanselmann | try:
|
168 | 3ef10550 | Michael Hanselmann | outputarray["bootid"] = f.read(128).rstrip("\n") |
169 | 3ef10550 | Michael Hanselmann | finally:
|
170 | 3ef10550 | Michael Hanselmann | f.close() |
171 | 3ef10550 | Michael Hanselmann | |
172 | a8083063 | Iustin Pop | return outputarray
|
173 | a8083063 | Iustin Pop | |
174 | a8083063 | Iustin Pop | |
175 | a8083063 | Iustin Pop | def VerifyNode(what): |
176 | a8083063 | Iustin Pop | """Verify the status of the local node.
|
177 | a8083063 | Iustin Pop |
|
178 | a8083063 | Iustin Pop | Args:
|
179 | a8083063 | Iustin Pop | what - a dictionary of things to check:
|
180 | a8083063 | Iustin Pop | 'filelist' : list of files for which to compute checksums
|
181 | a8083063 | Iustin Pop | 'nodelist' : list of nodes we should check communication with
|
182 | a8083063 | Iustin Pop | 'hypervisor': run the hypervisor-specific verify
|
183 | a8083063 | Iustin Pop |
|
184 | a8083063 | Iustin Pop | Requested files on local node are checksummed and the result returned.
|
185 | a8083063 | Iustin Pop |
|
186 | a8083063 | Iustin Pop | The nodelist is traversed, with the following checks being made
|
187 | a8083063 | Iustin Pop | for each node:
|
188 | a8083063 | Iustin Pop | - known_hosts key correct
|
189 | a8083063 | Iustin Pop | - correct resolving of node name (target node returns its own hostname
|
190 | a8083063 | Iustin Pop | by ssh-execution of 'hostname', result compared against name in list.
|
191 | a8083063 | Iustin Pop |
|
192 | a8083063 | Iustin Pop | """
|
193 | a8083063 | Iustin Pop | result = {} |
194 | a8083063 | Iustin Pop | |
195 | a8083063 | Iustin Pop | if 'hypervisor' in what: |
196 | a8083063 | Iustin Pop | result['hypervisor'] = hypervisor.GetHypervisor().Verify()
|
197 | a8083063 | Iustin Pop | |
198 | a8083063 | Iustin Pop | if 'filelist' in what: |
199 | a8083063 | Iustin Pop | result['filelist'] = utils.FingerprintFiles(what['filelist']) |
200 | a8083063 | Iustin Pop | |
201 | a8083063 | Iustin Pop | if 'nodelist' in what: |
202 | a8083063 | Iustin Pop | result['nodelist'] = {}
|
203 | a8083063 | Iustin Pop | for node in what['nodelist']: |
204 | c92b310a | Michael Hanselmann | success, message = _GetSshRunner().VerifyNodeHostname(node) |
205 | a8083063 | Iustin Pop | if not success: |
206 | a8083063 | Iustin Pop | result['nodelist'][node] = message
|
207 | a8083063 | Iustin Pop | return result
|
208 | a8083063 | Iustin Pop | |
209 | a8083063 | Iustin Pop | |
210 | a8083063 | Iustin Pop | def GetVolumeList(vg_name): |
211 | a8083063 | Iustin Pop | """Compute list of logical volumes and their size.
|
212 | a8083063 | Iustin Pop |
|
213 | a8083063 | Iustin Pop | Returns:
|
214 | cb2037a2 | Iustin Pop | dictionary of all partions (key) with their size (in MiB), inactive
|
215 | cb2037a2 | Iustin Pop | and online status:
|
216 | cb2037a2 | Iustin Pop | {'test1': ('20.06', True, True)}
|
217 | a8083063 | Iustin Pop |
|
218 | a8083063 | Iustin Pop | """
|
219 | cb2037a2 | Iustin Pop | lvs = {} |
220 | cb2037a2 | Iustin Pop | sep = '|'
|
221 | cb2037a2 | Iustin Pop | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
222 | cb2037a2 | Iustin Pop | "--separator=%s" % sep,
|
223 | cb2037a2 | Iustin Pop | "-olv_name,lv_size,lv_attr", vg_name])
|
224 | a8083063 | Iustin Pop | if result.failed:
|
225 | a8083063 | Iustin Pop | logger.Error("Failed to list logical volumes, lvs output: %s" %
|
226 | a8083063 | Iustin Pop | result.output) |
227 | b63ed789 | Iustin Pop | return result.output
|
228 | cb2037a2 | Iustin Pop | |
229 | cb2037a2 | Iustin Pop | for line in result.stdout.splitlines(): |
230 | cb2037a2 | Iustin Pop | line = line.strip().rstrip(sep) |
231 | cb2037a2 | Iustin Pop | name, size, attr = line.split(sep) |
232 | cb2037a2 | Iustin Pop | if len(attr) != 6: |
233 | cb2037a2 | Iustin Pop | attr = '------'
|
234 | cb2037a2 | Iustin Pop | inactive = attr[4] == '-' |
235 | cb2037a2 | Iustin Pop | online = attr[5] == 'o' |
236 | cb2037a2 | Iustin Pop | lvs[name] = (size, inactive, online) |
237 | cb2037a2 | Iustin Pop | |
238 | cb2037a2 | Iustin Pop | return lvs
|
239 | a8083063 | Iustin Pop | |
240 | a8083063 | Iustin Pop | |
241 | a8083063 | Iustin Pop | def ListVolumeGroups(): |
242 | 2f8598a5 | Alexander Schreiber | """List the volume groups and their size.
|
243 | a8083063 | Iustin Pop |
|
244 | a8083063 | Iustin Pop | Returns:
|
245 | a8083063 | Iustin Pop | Dictionary with keys volume name and values the size of the volume
|
246 | a8083063 | Iustin Pop |
|
247 | a8083063 | Iustin Pop | """
|
248 | a8083063 | Iustin Pop | return utils.ListVolumeGroups()
|
249 | a8083063 | Iustin Pop | |
250 | a8083063 | Iustin Pop | |
251 | dcb93971 | Michael Hanselmann | def NodeVolumes(): |
252 | dcb93971 | Michael Hanselmann | """List all volumes on this node.
|
253 | dcb93971 | Michael Hanselmann |
|
254 | dcb93971 | Michael Hanselmann | """
|
255 | dcb93971 | Michael Hanselmann | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
256 | dcb93971 | Michael Hanselmann | "--separator=|",
|
257 | dcb93971 | Michael Hanselmann | "--options=lv_name,lv_size,devices,vg_name"])
|
258 | dcb93971 | Michael Hanselmann | if result.failed:
|
259 | dcb93971 | Michael Hanselmann | logger.Error("Failed to list logical volumes, lvs output: %s" %
|
260 | dcb93971 | Michael Hanselmann | result.output) |
261 | dcb93971 | Michael Hanselmann | return {}
|
262 | dcb93971 | Michael Hanselmann | |
263 | dcb93971 | Michael Hanselmann | def parse_dev(dev): |
264 | dcb93971 | Michael Hanselmann | if '(' in dev: |
265 | dcb93971 | Michael Hanselmann | return dev.split('(')[0] |
266 | dcb93971 | Michael Hanselmann | else:
|
267 | dcb93971 | Michael Hanselmann | return dev
|
268 | dcb93971 | Michael Hanselmann | |
269 | dcb93971 | Michael Hanselmann | def map_line(line): |
270 | dcb93971 | Michael Hanselmann | return {
|
271 | dcb93971 | Michael Hanselmann | 'name': line[0].strip(), |
272 | dcb93971 | Michael Hanselmann | 'size': line[1].strip(), |
273 | dcb93971 | Michael Hanselmann | 'dev': parse_dev(line[2].strip()), |
274 | dcb93971 | Michael Hanselmann | 'vg': line[3].strip(), |
275 | dcb93971 | Michael Hanselmann | } |
276 | dcb93971 | Michael Hanselmann | |
277 | f675d4aa | Alexander Schreiber | return [map_line(line.split('|')) for line in result.stdout.splitlines()] |
278 | dcb93971 | Michael Hanselmann | |
279 | dcb93971 | Michael Hanselmann | |
280 | a8083063 | Iustin Pop | def BridgesExist(bridges_list): |
281 | 2f8598a5 | Alexander Schreiber | """Check if a list of bridges exist on the current node.
|
282 | a8083063 | Iustin Pop |
|
283 | a8083063 | Iustin Pop | Returns:
|
284 | a8083063 | Iustin Pop | True if all of them exist, false otherwise
|
285 | a8083063 | Iustin Pop |
|
286 | a8083063 | Iustin Pop | """
|
287 | a8083063 | Iustin Pop | for bridge in bridges_list: |
288 | a8083063 | Iustin Pop | if not utils.BridgeExists(bridge): |
289 | a8083063 | Iustin Pop | return False |
290 | a8083063 | Iustin Pop | |
291 | a8083063 | Iustin Pop | return True |
292 | a8083063 | Iustin Pop | |
293 | a8083063 | Iustin Pop | |
294 | a8083063 | Iustin Pop | def GetInstanceList(): |
295 | 2f8598a5 | Alexander Schreiber | """Provides a list of instances.
|
296 | a8083063 | Iustin Pop |
|
297 | a8083063 | Iustin Pop | Returns:
|
298 | a8083063 | Iustin Pop | A list of all running instances on the current node
|
299 | a8083063 | Iustin Pop | - instance1.example.com
|
300 | a8083063 | Iustin Pop | - instance2.example.com
|
301 | a8083063 | Iustin Pop |
|
302 | 098c0958 | Michael Hanselmann | """
|
303 | a8083063 | Iustin Pop | try:
|
304 | a8083063 | Iustin Pop | names = hypervisor.GetHypervisor().ListInstances() |
305 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
306 | a8083063 | Iustin Pop | logger.Error("error enumerating instances: %s" % str(err)) |
307 | a8083063 | Iustin Pop | raise
|
308 | a8083063 | Iustin Pop | |
309 | a8083063 | Iustin Pop | return names
|
310 | a8083063 | Iustin Pop | |
311 | a8083063 | Iustin Pop | |
312 | a8083063 | Iustin Pop | def GetInstanceInfo(instance): |
313 | 2f8598a5 | Alexander Schreiber | """Gives back the informations about an instance as a dictionary.
|
314 | a8083063 | Iustin Pop |
|
315 | a8083063 | Iustin Pop | Args:
|
316 | a8083063 | Iustin Pop | instance: name of the instance (ex. instance1.example.com)
|
317 | a8083063 | Iustin Pop |
|
318 | a8083063 | Iustin Pop | Returns:
|
319 | a8083063 | Iustin Pop | { 'memory' : 511, 'state' : '-b---', 'time' : 3188.8, }
|
320 | a8083063 | Iustin Pop | where
|
321 | a8083063 | Iustin Pop | memory: memory size of instance (int)
|
322 | a8083063 | Iustin Pop | state: xen state of instance (string)
|
323 | a8083063 | Iustin Pop | time: cpu time of instance (float)
|
324 | a8083063 | Iustin Pop |
|
325 | 098c0958 | Michael Hanselmann | """
|
326 | a8083063 | Iustin Pop | output = {} |
327 | a8083063 | Iustin Pop | |
328 | a8083063 | Iustin Pop | iinfo = hypervisor.GetHypervisor().GetInstanceInfo(instance) |
329 | a8083063 | Iustin Pop | if iinfo is not None: |
330 | a8083063 | Iustin Pop | output['memory'] = iinfo[2] |
331 | a8083063 | Iustin Pop | output['state'] = iinfo[4] |
332 | a8083063 | Iustin Pop | output['time'] = iinfo[5] |
333 | a8083063 | Iustin Pop | |
334 | a8083063 | Iustin Pop | return output
|
335 | a8083063 | Iustin Pop | |
336 | a8083063 | Iustin Pop | |
337 | a8083063 | Iustin Pop | def GetAllInstancesInfo(): |
338 | a8083063 | Iustin Pop | """Gather data about all instances.
|
339 | a8083063 | Iustin Pop |
|
340 | a8083063 | Iustin Pop | This is the equivalent of `GetInstanceInfo()`, except that it
|
341 | a8083063 | Iustin Pop | computes data for all instances at once, thus being faster if one
|
342 | a8083063 | Iustin Pop | needs data about more than one instance.
|
343 | a8083063 | Iustin Pop |
|
344 | a8083063 | Iustin Pop | Returns: a dictionary of dictionaries, keys being the instance name,
|
345 | a8083063 | Iustin Pop | and with values:
|
346 | a8083063 | Iustin Pop | { 'memory' : 511, 'state' : '-b---', 'time' : 3188.8, }
|
347 | a8083063 | Iustin Pop | where
|
348 | a8083063 | Iustin Pop | memory: memory size of instance (int)
|
349 | a8083063 | Iustin Pop | state: xen state of instance (string)
|
350 | a8083063 | Iustin Pop | time: cpu time of instance (float)
|
351 | a8083063 | Iustin Pop | vcpus: the number of cpus
|
352 | a8083063 | Iustin Pop |
|
353 | 098c0958 | Michael Hanselmann | """
|
354 | a8083063 | Iustin Pop | output = {} |
355 | a8083063 | Iustin Pop | |
356 | a8083063 | Iustin Pop | iinfo = hypervisor.GetHypervisor().GetAllInstancesInfo() |
357 | a8083063 | Iustin Pop | if iinfo:
|
358 | 3ecf6786 | Iustin Pop | for name, inst_id, memory, vcpus, state, times in iinfo: |
359 | a8083063 | Iustin Pop | output[name] = { |
360 | a8083063 | Iustin Pop | 'memory': memory,
|
361 | a8083063 | Iustin Pop | 'vcpus': vcpus,
|
362 | a8083063 | Iustin Pop | 'state': state,
|
363 | a8083063 | Iustin Pop | 'time': times,
|
364 | a8083063 | Iustin Pop | } |
365 | a8083063 | Iustin Pop | |
366 | a8083063 | Iustin Pop | return output
|
367 | a8083063 | Iustin Pop | |
368 | a8083063 | Iustin Pop | |
369 | a8083063 | Iustin Pop | def AddOSToInstance(instance, os_disk, swap_disk): |
370 | 2f8598a5 | Alexander Schreiber | """Add an OS to an instance.
|
371 | a8083063 | Iustin Pop |
|
372 | a8083063 | Iustin Pop | Args:
|
373 | a8083063 | Iustin Pop | instance: the instance object
|
374 | a8083063 | Iustin Pop | os_disk: the instance-visible name of the os device
|
375 | a8083063 | Iustin Pop | swap_disk: the instance-visible name of the swap device
|
376 | a8083063 | Iustin Pop |
|
377 | a8083063 | Iustin Pop | """
|
378 | a8083063 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
379 | a8083063 | Iustin Pop | |
380 | a8083063 | Iustin Pop | create_script = inst_os.create_script |
381 | a8083063 | Iustin Pop | |
382 | 9716fdce | Iustin Pop | os_device = instance.FindDisk(os_disk) |
383 | 9716fdce | Iustin Pop | if os_device is None: |
384 | a8083063 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % os_disk)
|
385 | a8083063 | Iustin Pop | return False |
386 | a8083063 | Iustin Pop | |
387 | 9716fdce | Iustin Pop | swap_device = instance.FindDisk(swap_disk) |
388 | 9716fdce | Iustin Pop | if swap_device is None: |
389 | a8083063 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % swap_disk)
|
390 | a8083063 | Iustin Pop | return False |
391 | a8083063 | Iustin Pop | |
392 | a8083063 | Iustin Pop | real_os_dev = _RecursiveFindBD(os_device) |
393 | a8083063 | Iustin Pop | if real_os_dev is None: |
394 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
395 | a8083063 | Iustin Pop | str(os_device))
|
396 | a8083063 | Iustin Pop | real_os_dev.Open() |
397 | a8083063 | Iustin Pop | |
398 | a8083063 | Iustin Pop | real_swap_dev = _RecursiveFindBD(swap_device) |
399 | a8083063 | Iustin Pop | if real_swap_dev is None: |
400 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
401 | a8083063 | Iustin Pop | str(swap_device))
|
402 | a8083063 | Iustin Pop | real_swap_dev.Open() |
403 | a8083063 | Iustin Pop | |
404 | a8083063 | Iustin Pop | logfile = "%s/add-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
|
405 | a8083063 | Iustin Pop | instance.name, int(time.time()))
|
406 | a8083063 | Iustin Pop | if not os.path.exists(constants.LOG_OS_DIR): |
407 | a8083063 | Iustin Pop | os.mkdir(constants.LOG_OS_DIR, 0750)
|
408 | a8083063 | Iustin Pop | |
409 | c20494cd | Iustin Pop | command = utils.BuildShellCmd("cd %s && %s -i %s -b %s -s %s &>%s",
|
410 | a8083063 | Iustin Pop | inst_os.path, create_script, instance.name, |
411 | a8083063 | Iustin Pop | real_os_dev.dev_path, real_swap_dev.dev_path, |
412 | a8083063 | Iustin Pop | logfile) |
413 | decd5f45 | Iustin Pop | |
414 | decd5f45 | Iustin Pop | result = utils.RunCmd(command) |
415 | decd5f45 | Iustin Pop | if result.failed:
|
416 | ff73280e | Michael Hanselmann | logger.Error("os create command '%s' returned error: %s, logfile: %s,"
|
417 | decd5f45 | Iustin Pop | " output: %s" %
|
418 | ff73280e | Michael Hanselmann | (command, result.fail_reason, logfile, result.output)) |
419 | decd5f45 | Iustin Pop | return False |
420 | decd5f45 | Iustin Pop | |
421 | decd5f45 | Iustin Pop | return True |
422 | decd5f45 | Iustin Pop | |
423 | decd5f45 | Iustin Pop | |
424 | decd5f45 | Iustin Pop | def RunRenameInstance(instance, old_name, os_disk, swap_disk): |
425 | decd5f45 | Iustin Pop | """Run the OS rename script for an instance.
|
426 | decd5f45 | Iustin Pop |
|
427 | decd5f45 | Iustin Pop | Args:
|
428 | decd5f45 | Iustin Pop | instance: the instance object
|
429 | decd5f45 | Iustin Pop | old_name: the old name of the instance
|
430 | decd5f45 | Iustin Pop | os_disk: the instance-visible name of the os device
|
431 | decd5f45 | Iustin Pop | swap_disk: the instance-visible name of the swap device
|
432 | decd5f45 | Iustin Pop |
|
433 | decd5f45 | Iustin Pop | """
|
434 | decd5f45 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
435 | decd5f45 | Iustin Pop | |
436 | decd5f45 | Iustin Pop | script = inst_os.rename_script |
437 | decd5f45 | Iustin Pop | |
438 | decd5f45 | Iustin Pop | os_device = instance.FindDisk(os_disk) |
439 | decd5f45 | Iustin Pop | if os_device is None: |
440 | decd5f45 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % os_disk)
|
441 | decd5f45 | Iustin Pop | return False |
442 | decd5f45 | Iustin Pop | |
443 | decd5f45 | Iustin Pop | swap_device = instance.FindDisk(swap_disk) |
444 | decd5f45 | Iustin Pop | if swap_device is None: |
445 | decd5f45 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % swap_disk)
|
446 | decd5f45 | Iustin Pop | return False |
447 | decd5f45 | Iustin Pop | |
448 | decd5f45 | Iustin Pop | real_os_dev = _RecursiveFindBD(os_device) |
449 | decd5f45 | Iustin Pop | if real_os_dev is None: |
450 | decd5f45 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
451 | decd5f45 | Iustin Pop | str(os_device))
|
452 | decd5f45 | Iustin Pop | real_os_dev.Open() |
453 | decd5f45 | Iustin Pop | |
454 | decd5f45 | Iustin Pop | real_swap_dev = _RecursiveFindBD(swap_device) |
455 | decd5f45 | Iustin Pop | if real_swap_dev is None: |
456 | decd5f45 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
457 | decd5f45 | Iustin Pop | str(swap_device))
|
458 | decd5f45 | Iustin Pop | real_swap_dev.Open() |
459 | decd5f45 | Iustin Pop | |
460 | decd5f45 | Iustin Pop | logfile = "%s/rename-%s-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
|
461 | decd5f45 | Iustin Pop | old_name, |
462 | decd5f45 | Iustin Pop | instance.name, int(time.time()))
|
463 | decd5f45 | Iustin Pop | if not os.path.exists(constants.LOG_OS_DIR): |
464 | decd5f45 | Iustin Pop | os.mkdir(constants.LOG_OS_DIR, 0750)
|
465 | decd5f45 | Iustin Pop | |
466 | decd5f45 | Iustin Pop | command = utils.BuildShellCmd("cd %s && %s -o %s -n %s -b %s -s %s &>%s",
|
467 | decd5f45 | Iustin Pop | inst_os.path, script, old_name, instance.name, |
468 | decd5f45 | Iustin Pop | real_os_dev.dev_path, real_swap_dev.dev_path, |
469 | decd5f45 | Iustin Pop | logfile) |
470 | a8083063 | Iustin Pop | |
471 | a8083063 | Iustin Pop | result = utils.RunCmd(command) |
472 | a8083063 | Iustin Pop | |
473 | a8083063 | Iustin Pop | if result.failed:
|
474 | a8083063 | Iustin Pop | logger.Error("os create command '%s' returned error: %s"
|
475 | a8083063 | Iustin Pop | " output: %s" %
|
476 | a8083063 | Iustin Pop | (command, result.fail_reason, result.output)) |
477 | a8083063 | Iustin Pop | return False |
478 | a8083063 | Iustin Pop | |
479 | a8083063 | Iustin Pop | return True |
480 | a8083063 | Iustin Pop | |
481 | a8083063 | Iustin Pop | |
482 | a8083063 | Iustin Pop | def _GetVGInfo(vg_name): |
483 | a8083063 | Iustin Pop | """Get informations about the volume group.
|
484 | a8083063 | Iustin Pop |
|
485 | a8083063 | Iustin Pop | Args:
|
486 | a8083063 | Iustin Pop | vg_name: the volume group
|
487 | a8083063 | Iustin Pop |
|
488 | a8083063 | Iustin Pop | Returns:
|
489 | a8083063 | Iustin Pop | { 'vg_size' : xxx, 'vg_free' : xxx, 'pv_count' : xxx }
|
490 | a8083063 | Iustin Pop | where
|
491 | a8083063 | Iustin Pop | vg_size is the total size of the volume group in MiB
|
492 | a8083063 | Iustin Pop | vg_free is the free size of the volume group in MiB
|
493 | a8083063 | Iustin Pop | pv_count are the number of physical disks in that vg
|
494 | a8083063 | Iustin Pop |
|
495 | f4d377e7 | Iustin Pop | If an error occurs during gathering of data, we return the same dict
|
496 | f4d377e7 | Iustin Pop | with keys all set to None.
|
497 | f4d377e7 | Iustin Pop |
|
498 | a8083063 | Iustin Pop | """
|
499 | f4d377e7 | Iustin Pop | retdic = dict.fromkeys(["vg_size", "vg_free", "pv_count"]) |
500 | f4d377e7 | Iustin Pop | |
501 | a8083063 | Iustin Pop | retval = utils.RunCmd(["vgs", "-ovg_size,vg_free,pv_count", "--noheadings", |
502 | a8083063 | Iustin Pop | "--nosuffix", "--units=m", "--separator=:", vg_name]) |
503 | a8083063 | Iustin Pop | |
504 | a8083063 | Iustin Pop | if retval.failed:
|
505 | a8083063 | Iustin Pop | errmsg = "volume group %s not present" % vg_name
|
506 | a8083063 | Iustin Pop | logger.Error(errmsg) |
507 | f4d377e7 | Iustin Pop | return retdic
|
508 | d87ae7d2 | Iustin Pop | valarr = retval.stdout.strip().rstrip(':').split(':') |
509 | f4d377e7 | Iustin Pop | if len(valarr) == 3: |
510 | f4d377e7 | Iustin Pop | try:
|
511 | f4d377e7 | Iustin Pop | retdic = { |
512 | f4d377e7 | Iustin Pop | "vg_size": int(round(float(valarr[0]), 0)), |
513 | f4d377e7 | Iustin Pop | "vg_free": int(round(float(valarr[1]), 0)), |
514 | f4d377e7 | Iustin Pop | "pv_count": int(valarr[2]), |
515 | f4d377e7 | Iustin Pop | } |
516 | f4d377e7 | Iustin Pop | except ValueError, err: |
517 | f4d377e7 | Iustin Pop | logger.Error("Fail to parse vgs output: %s" % str(err)) |
518 | f4d377e7 | Iustin Pop | else:
|
519 | f4d377e7 | Iustin Pop | logger.Error("vgs output has the wrong number of fields (expected"
|
520 | f4d377e7 | Iustin Pop | " three): %s" % str(valarr)) |
521 | a8083063 | Iustin Pop | return retdic
|
522 | a8083063 | Iustin Pop | |
523 | a8083063 | Iustin Pop | |
524 | a8083063 | Iustin Pop | def _GatherBlockDevs(instance): |
525 | a8083063 | Iustin Pop | """Set up an instance's block device(s).
|
526 | a8083063 | Iustin Pop |
|
527 | a8083063 | Iustin Pop | This is run on the primary node at instance startup. The block
|
528 | a8083063 | Iustin Pop | devices must be already assembled.
|
529 | a8083063 | Iustin Pop |
|
530 | a8083063 | Iustin Pop | """
|
531 | a8083063 | Iustin Pop | block_devices = [] |
532 | a8083063 | Iustin Pop | for disk in instance.disks: |
533 | a8083063 | Iustin Pop | device = _RecursiveFindBD(disk) |
534 | a8083063 | Iustin Pop | if device is None: |
535 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up." % |
536 | a8083063 | Iustin Pop | str(disk))
|
537 | a8083063 | Iustin Pop | device.Open() |
538 | a8083063 | Iustin Pop | block_devices.append((disk, device)) |
539 | a8083063 | Iustin Pop | return block_devices
|
540 | a8083063 | Iustin Pop | |
541 | a8083063 | Iustin Pop | |
542 | a8083063 | Iustin Pop | def StartInstance(instance, extra_args): |
543 | a8083063 | Iustin Pop | """Start an instance.
|
544 | a8083063 | Iustin Pop |
|
545 | a8083063 | Iustin Pop | Args:
|
546 | a8083063 | Iustin Pop | instance - name of instance to start.
|
547 | a8083063 | Iustin Pop |
|
548 | 098c0958 | Michael Hanselmann | """
|
549 | a8083063 | Iustin Pop | running_instances = GetInstanceList() |
550 | a8083063 | Iustin Pop | |
551 | a8083063 | Iustin Pop | if instance.name in running_instances: |
552 | a8083063 | Iustin Pop | return True |
553 | a8083063 | Iustin Pop | |
554 | a8083063 | Iustin Pop | block_devices = _GatherBlockDevs(instance) |
555 | a8083063 | Iustin Pop | hyper = hypervisor.GetHypervisor() |
556 | a8083063 | Iustin Pop | |
557 | a8083063 | Iustin Pop | try:
|
558 | a8083063 | Iustin Pop | hyper.StartInstance(instance, block_devices, extra_args) |
559 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
560 | a8083063 | Iustin Pop | logger.Error("Failed to start instance: %s" % err)
|
561 | a8083063 | Iustin Pop | return False |
562 | a8083063 | Iustin Pop | |
563 | a8083063 | Iustin Pop | return True |
564 | a8083063 | Iustin Pop | |
565 | a8083063 | Iustin Pop | |
566 | a8083063 | Iustin Pop | def ShutdownInstance(instance): |
567 | a8083063 | Iustin Pop | """Shut an instance down.
|
568 | a8083063 | Iustin Pop |
|
569 | a8083063 | Iustin Pop | Args:
|
570 | a8083063 | Iustin Pop | instance - name of instance to shutdown.
|
571 | a8083063 | Iustin Pop |
|
572 | 098c0958 | Michael Hanselmann | """
|
573 | a8083063 | Iustin Pop | running_instances = GetInstanceList() |
574 | a8083063 | Iustin Pop | |
575 | a8083063 | Iustin Pop | if instance.name not in running_instances: |
576 | a8083063 | Iustin Pop | return True |
577 | a8083063 | Iustin Pop | |
578 | a8083063 | Iustin Pop | hyper = hypervisor.GetHypervisor() |
579 | a8083063 | Iustin Pop | try:
|
580 | a8083063 | Iustin Pop | hyper.StopInstance(instance) |
581 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
582 | a8083063 | Iustin Pop | logger.Error("Failed to stop instance: %s" % err)
|
583 | a8083063 | Iustin Pop | return False |
584 | a8083063 | Iustin Pop | |
585 | a8083063 | Iustin Pop | # test every 10secs for 2min
|
586 | a8083063 | Iustin Pop | shutdown_ok = False
|
587 | a8083063 | Iustin Pop | |
588 | a8083063 | Iustin Pop | time.sleep(1)
|
589 | a8083063 | Iustin Pop | for dummy in range(11): |
590 | a8083063 | Iustin Pop | if instance.name not in GetInstanceList(): |
591 | a8083063 | Iustin Pop | break
|
592 | a8083063 | Iustin Pop | time.sleep(10)
|
593 | a8083063 | Iustin Pop | else:
|
594 | a8083063 | Iustin Pop | # the shutdown did not succeed
|
595 | a8083063 | Iustin Pop | logger.Error("shutdown of '%s' unsuccessful, using destroy" % instance)
|
596 | a8083063 | Iustin Pop | |
597 | a8083063 | Iustin Pop | try:
|
598 | a8083063 | Iustin Pop | hyper.StopInstance(instance, force=True)
|
599 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
600 | a8083063 | Iustin Pop | logger.Error("Failed to stop instance: %s" % err)
|
601 | a8083063 | Iustin Pop | return False |
602 | a8083063 | Iustin Pop | |
603 | a8083063 | Iustin Pop | time.sleep(1)
|
604 | a8083063 | Iustin Pop | if instance.name in GetInstanceList(): |
605 | a8083063 | Iustin Pop | logger.Error("could not shutdown instance '%s' even by destroy")
|
606 | a8083063 | Iustin Pop | return False |
607 | a8083063 | Iustin Pop | |
608 | a8083063 | Iustin Pop | return True |
609 | a8083063 | Iustin Pop | |
610 | a8083063 | Iustin Pop | |
611 | 007a2f3e | Alexander Schreiber | def RebootInstance(instance, reboot_type, extra_args): |
612 | 007a2f3e | Alexander Schreiber | """Reboot an instance.
|
613 | 007a2f3e | Alexander Schreiber |
|
614 | 007a2f3e | Alexander Schreiber | Args:
|
615 | 007a2f3e | Alexander Schreiber | instance - name of instance to reboot
|
616 | 007a2f3e | Alexander Schreiber | reboot_type - how to reboot [soft,hard,full]
|
617 | 007a2f3e | Alexander Schreiber |
|
618 | 007a2f3e | Alexander Schreiber | """
|
619 | 007a2f3e | Alexander Schreiber | running_instances = GetInstanceList() |
620 | 007a2f3e | Alexander Schreiber | |
621 | 007a2f3e | Alexander Schreiber | if instance.name not in running_instances: |
622 | 007a2f3e | Alexander Schreiber | logger.Error("Cannot reboot instance that is not running")
|
623 | 007a2f3e | Alexander Schreiber | return False |
624 | 007a2f3e | Alexander Schreiber | |
625 | 007a2f3e | Alexander Schreiber | hyper = hypervisor.GetHypervisor() |
626 | 007a2f3e | Alexander Schreiber | if reboot_type == constants.INSTANCE_REBOOT_SOFT:
|
627 | 007a2f3e | Alexander Schreiber | try:
|
628 | 007a2f3e | Alexander Schreiber | hyper.RebootInstance(instance) |
629 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
630 | 007a2f3e | Alexander Schreiber | logger.Error("Failed to soft reboot instance: %s" % err)
|
631 | 007a2f3e | Alexander Schreiber | return False |
632 | 007a2f3e | Alexander Schreiber | elif reboot_type == constants.INSTANCE_REBOOT_HARD:
|
633 | 007a2f3e | Alexander Schreiber | try:
|
634 | 007a2f3e | Alexander Schreiber | ShutdownInstance(instance) |
635 | 007a2f3e | Alexander Schreiber | StartInstance(instance, extra_args) |
636 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
637 | 007a2f3e | Alexander Schreiber | logger.Error("Failed to hard reboot instance: %s" % err)
|
638 | 007a2f3e | Alexander Schreiber | return False |
639 | 007a2f3e | Alexander Schreiber | else:
|
640 | 007a2f3e | Alexander Schreiber | raise errors.ParameterError("reboot_type invalid") |
641 | 007a2f3e | Alexander Schreiber | |
642 | 007a2f3e | Alexander Schreiber | |
643 | 007a2f3e | Alexander Schreiber | return True |
644 | 007a2f3e | Alexander Schreiber | |
645 | 007a2f3e | Alexander Schreiber | |
646 | 3f78eef2 | Iustin Pop | def CreateBlockDevice(disk, size, owner, on_primary, info): |
647 | a8083063 | Iustin Pop | """Creates a block device for an instance.
|
648 | a8083063 | Iustin Pop |
|
649 | a8083063 | Iustin Pop | Args:
|
650 | c99a3cc0 | Manuel Franceschini | disk: a ganeti.objects.Disk object
|
651 | c99a3cc0 | Manuel Franceschini | size: the size of the physical underlying device
|
652 | c99a3cc0 | Manuel Franceschini | owner: a string with the name of the instance
|
653 | 6c8af3d0 | Manuel Franceschini | on_primary: a boolean indicating if it is the primary node or not
|
654 | 6c8af3d0 | Manuel Franceschini | info: string that will be sent to the physical device creation
|
655 | a8083063 | Iustin Pop |
|
656 | a8083063 | Iustin Pop | Returns:
|
657 | a8083063 | Iustin Pop | the new unique_id of the device (this can sometime be
|
658 | a8083063 | Iustin Pop | computed only after creation), or None. On secondary nodes,
|
659 | a8083063 | Iustin Pop | it's not required to return anything.
|
660 | a8083063 | Iustin Pop |
|
661 | a8083063 | Iustin Pop | """
|
662 | a8083063 | Iustin Pop | clist = [] |
663 | a8083063 | Iustin Pop | if disk.children:
|
664 | a8083063 | Iustin Pop | for child in disk.children: |
665 | 3f78eef2 | Iustin Pop | crdev = _RecursiveAssembleBD(child, owner, on_primary) |
666 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
667 | a8083063 | Iustin Pop | # we need the children open in case the device itself has to
|
668 | a8083063 | Iustin Pop | # be assembled
|
669 | a8083063 | Iustin Pop | crdev.Open() |
670 | a8083063 | Iustin Pop | clist.append(crdev) |
671 | a8083063 | Iustin Pop | try:
|
672 | a8083063 | Iustin Pop | device = bdev.FindDevice(disk.dev_type, disk.physical_id, clist) |
673 | a8083063 | Iustin Pop | if device is not None: |
674 | a8083063 | Iustin Pop | logger.Info("removing existing device %s" % disk)
|
675 | a8083063 | Iustin Pop | device.Remove() |
676 | a8083063 | Iustin Pop | except errors.BlockDeviceError, err:
|
677 | a8083063 | Iustin Pop | pass
|
678 | a8083063 | Iustin Pop | |
679 | a8083063 | Iustin Pop | device = bdev.Create(disk.dev_type, disk.physical_id, |
680 | a8083063 | Iustin Pop | clist, size) |
681 | a8083063 | Iustin Pop | if device is None: |
682 | a8083063 | Iustin Pop | raise ValueError("Can't create child device for %s, %s" % |
683 | a8083063 | Iustin Pop | (disk, size)) |
684 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
685 | cf5a8306 | Iustin Pop | if not device.Assemble(): |
686 | 20a0c9ef | Guido Trotter | errorstring = "Can't assemble device after creation"
|
687 | 20a0c9ef | Guido Trotter | logger.Error(errorstring) |
688 | 20a0c9ef | Guido Trotter | raise errors.BlockDeviceError("%s, very unusual event - check the node" |
689 | 20a0c9ef | Guido Trotter | " daemon logs" % errorstring)
|
690 | e31c43f7 | Michael Hanselmann | device.SetSyncSpeed(constants.SYNC_SPEED) |
691 | a8083063 | Iustin Pop | if on_primary or disk.OpenOnSecondary(): |
692 | a8083063 | Iustin Pop | device.Open(force=True)
|
693 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(device.dev_path, owner, |
694 | 3f78eef2 | Iustin Pop | on_primary, disk.iv_name) |
695 | a0c3fea1 | Michael Hanselmann | |
696 | a0c3fea1 | Michael Hanselmann | device.SetInfo(info) |
697 | a0c3fea1 | Michael Hanselmann | |
698 | a8083063 | Iustin Pop | physical_id = device.unique_id |
699 | a8083063 | Iustin Pop | return physical_id
|
700 | a8083063 | Iustin Pop | |
701 | a8083063 | Iustin Pop | |
702 | a8083063 | Iustin Pop | def RemoveBlockDevice(disk): |
703 | a8083063 | Iustin Pop | """Remove a block device.
|
704 | a8083063 | Iustin Pop |
|
705 | a8083063 | Iustin Pop | This is intended to be called recursively.
|
706 | a8083063 | Iustin Pop |
|
707 | a8083063 | Iustin Pop | """
|
708 | a8083063 | Iustin Pop | try:
|
709 | a8083063 | Iustin Pop | # since we are removing the device, allow a partial match
|
710 | a8083063 | Iustin Pop | # this allows removal of broken mirrors
|
711 | a8083063 | Iustin Pop | rdev = _RecursiveFindBD(disk, allow_partial=True)
|
712 | a8083063 | Iustin Pop | except errors.BlockDeviceError, err:
|
713 | a8083063 | Iustin Pop | # probably can't attach
|
714 | a8083063 | Iustin Pop | logger.Info("Can't attach to device %s in remove" % disk)
|
715 | a8083063 | Iustin Pop | rdev = None
|
716 | a8083063 | Iustin Pop | if rdev is not None: |
717 | 3f78eef2 | Iustin Pop | r_path = rdev.dev_path |
718 | a8083063 | Iustin Pop | result = rdev.Remove() |
719 | 3f78eef2 | Iustin Pop | if result:
|
720 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
721 | a8083063 | Iustin Pop | else:
|
722 | a8083063 | Iustin Pop | result = True
|
723 | a8083063 | Iustin Pop | if disk.children:
|
724 | a8083063 | Iustin Pop | for child in disk.children: |
725 | a8083063 | Iustin Pop | result = result and RemoveBlockDevice(child)
|
726 | a8083063 | Iustin Pop | return result
|
727 | a8083063 | Iustin Pop | |
728 | a8083063 | Iustin Pop | |
729 | 3f78eef2 | Iustin Pop | def _RecursiveAssembleBD(disk, owner, as_primary): |
730 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
731 | a8083063 | Iustin Pop |
|
732 | a8083063 | Iustin Pop | This is run on the primary and secondary nodes for an instance.
|
733 | a8083063 | Iustin Pop |
|
734 | a8083063 | Iustin Pop | This function is called recursively.
|
735 | a8083063 | Iustin Pop |
|
736 | a8083063 | Iustin Pop | Args:
|
737 | a8083063 | Iustin Pop | disk: a objects.Disk object
|
738 | a8083063 | Iustin Pop | as_primary: if we should make the block device read/write
|
739 | a8083063 | Iustin Pop |
|
740 | a8083063 | Iustin Pop | Returns:
|
741 | a8083063 | Iustin Pop | the assembled device or None (in case no device was assembled)
|
742 | a8083063 | Iustin Pop |
|
743 | a8083063 | Iustin Pop | If the assembly is not successful, an exception is raised.
|
744 | a8083063 | Iustin Pop |
|
745 | a8083063 | Iustin Pop | """
|
746 | a8083063 | Iustin Pop | children = [] |
747 | a8083063 | Iustin Pop | if disk.children:
|
748 | fc1dc9d7 | Iustin Pop | mcn = disk.ChildrenNeeded() |
749 | fc1dc9d7 | Iustin Pop | if mcn == -1: |
750 | fc1dc9d7 | Iustin Pop | mcn = 0 # max number of Nones allowed |
751 | fc1dc9d7 | Iustin Pop | else:
|
752 | fc1dc9d7 | Iustin Pop | mcn = len(disk.children) - mcn # max number of Nones |
753 | a8083063 | Iustin Pop | for chld_disk in disk.children: |
754 | fc1dc9d7 | Iustin Pop | try:
|
755 | fc1dc9d7 | Iustin Pop | cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary) |
756 | fc1dc9d7 | Iustin Pop | except errors.BlockDeviceError, err:
|
757 | 7803d4d3 | Iustin Pop | if children.count(None) >= mcn: |
758 | fc1dc9d7 | Iustin Pop | raise
|
759 | fc1dc9d7 | Iustin Pop | cdev = None
|
760 | fc1dc9d7 | Iustin Pop | logger.Debug("Error in child activation: %s" % str(err)) |
761 | fc1dc9d7 | Iustin Pop | children.append(cdev) |
762 | a8083063 | Iustin Pop | |
763 | a8083063 | Iustin Pop | if as_primary or disk.AssembleOnSecondary(): |
764 | a8083063 | Iustin Pop | r_dev = bdev.AttachOrAssemble(disk.dev_type, disk.physical_id, children) |
765 | e31c43f7 | Michael Hanselmann | r_dev.SetSyncSpeed(constants.SYNC_SPEED) |
766 | a8083063 | Iustin Pop | result = r_dev |
767 | a8083063 | Iustin Pop | if as_primary or disk.OpenOnSecondary(): |
768 | a8083063 | Iustin Pop | r_dev.Open() |
769 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(r_dev.dev_path, owner, |
770 | 3f78eef2 | Iustin Pop | as_primary, disk.iv_name) |
771 | 3f78eef2 | Iustin Pop | |
772 | a8083063 | Iustin Pop | else:
|
773 | a8083063 | Iustin Pop | result = True
|
774 | a8083063 | Iustin Pop | return result
|
775 | a8083063 | Iustin Pop | |
776 | a8083063 | Iustin Pop | |
777 | 3f78eef2 | Iustin Pop | def AssembleBlockDevice(disk, owner, as_primary): |
778 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
779 | a8083063 | Iustin Pop |
|
780 | a8083063 | Iustin Pop | This is a wrapper over _RecursiveAssembleBD.
|
781 | a8083063 | Iustin Pop |
|
782 | a8083063 | Iustin Pop | Returns:
|
783 | a8083063 | Iustin Pop | a /dev path for primary nodes
|
784 | a8083063 | Iustin Pop | True for secondary nodes
|
785 | a8083063 | Iustin Pop |
|
786 | a8083063 | Iustin Pop | """
|
787 | 3f78eef2 | Iustin Pop | result = _RecursiveAssembleBD(disk, owner, as_primary) |
788 | a8083063 | Iustin Pop | if isinstance(result, bdev.BlockDev): |
789 | a8083063 | Iustin Pop | result = result.dev_path |
790 | a8083063 | Iustin Pop | return result
|
791 | a8083063 | Iustin Pop | |
792 | a8083063 | Iustin Pop | |
793 | a8083063 | Iustin Pop | def ShutdownBlockDevice(disk): |
794 | a8083063 | Iustin Pop | """Shut down a block device.
|
795 | a8083063 | Iustin Pop |
|
796 | a8083063 | Iustin Pop | First, if the device is assembled (can `Attach()`), then the device
|
797 | a8083063 | Iustin Pop | is shutdown. Then the children of the device are shutdown.
|
798 | a8083063 | Iustin Pop |
|
799 | a8083063 | Iustin Pop | This function is called recursively. Note that we don't cache the
|
800 | a8083063 | Iustin Pop | children or such, as oppossed to assemble, shutdown of different
|
801 | a8083063 | Iustin Pop | devices doesn't require that the upper device was active.
|
802 | a8083063 | Iustin Pop |
|
803 | a8083063 | Iustin Pop | """
|
804 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
805 | a8083063 | Iustin Pop | if r_dev is not None: |
806 | 3f78eef2 | Iustin Pop | r_path = r_dev.dev_path |
807 | a8083063 | Iustin Pop | result = r_dev.Shutdown() |
808 | 3f78eef2 | Iustin Pop | if result:
|
809 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
810 | a8083063 | Iustin Pop | else:
|
811 | a8083063 | Iustin Pop | result = True
|
812 | a8083063 | Iustin Pop | if disk.children:
|
813 | a8083063 | Iustin Pop | for child in disk.children: |
814 | a8083063 | Iustin Pop | result = result and ShutdownBlockDevice(child)
|
815 | a8083063 | Iustin Pop | return result
|
816 | a8083063 | Iustin Pop | |
817 | a8083063 | Iustin Pop | |
818 | 153d9724 | Iustin Pop | def MirrorAddChildren(parent_cdev, new_cdevs): |
819 | 153d9724 | Iustin Pop | """Extend a mirrored block device.
|
820 | a8083063 | Iustin Pop |
|
821 | a8083063 | Iustin Pop | """
|
822 | 153d9724 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev, allow_partial=True)
|
823 | 153d9724 | Iustin Pop | if parent_bdev is None: |
824 | 153d9724 | Iustin Pop | logger.Error("Can't find parent device")
|
825 | a8083063 | Iustin Pop | return False |
826 | 153d9724 | Iustin Pop | new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs] |
827 | 153d9724 | Iustin Pop | if new_bdevs.count(None) > 0: |
828 | a9e0c397 | Iustin Pop | logger.Error("Can't find new device(s) to add: %s:%s" %
|
829 | a9e0c397 | Iustin Pop | (new_bdevs, new_cdevs)) |
830 | a8083063 | Iustin Pop | return False |
831 | 153d9724 | Iustin Pop | parent_bdev.AddChildren(new_bdevs) |
832 | a8083063 | Iustin Pop | return True |
833 | a8083063 | Iustin Pop | |
834 | a8083063 | Iustin Pop | |
835 | 153d9724 | Iustin Pop | def MirrorRemoveChildren(parent_cdev, new_cdevs): |
836 | 153d9724 | Iustin Pop | """Shrink a mirrored block device.
|
837 | a8083063 | Iustin Pop |
|
838 | a8083063 | Iustin Pop | """
|
839 | 153d9724 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev) |
840 | 153d9724 | Iustin Pop | if parent_bdev is None: |
841 | a9e0c397 | Iustin Pop | logger.Error("Can't find parent in remove children: %s" % parent_cdev)
|
842 | a8083063 | Iustin Pop | return False |
843 | e739bd57 | Iustin Pop | devs = [] |
844 | e739bd57 | Iustin Pop | for disk in new_cdevs: |
845 | e739bd57 | Iustin Pop | rpath = disk.StaticDevPath() |
846 | e739bd57 | Iustin Pop | if rpath is None: |
847 | e739bd57 | Iustin Pop | bd = _RecursiveFindBD(disk) |
848 | e739bd57 | Iustin Pop | if bd is None: |
849 | e739bd57 | Iustin Pop | logger.Error("Can't find dynamic device %s while removing children" %
|
850 | e739bd57 | Iustin Pop | disk) |
851 | e739bd57 | Iustin Pop | return False |
852 | e739bd57 | Iustin Pop | else:
|
853 | e739bd57 | Iustin Pop | devs.append(bd.dev_path) |
854 | e739bd57 | Iustin Pop | else:
|
855 | e739bd57 | Iustin Pop | devs.append(rpath) |
856 | e739bd57 | Iustin Pop | parent_bdev.RemoveChildren(devs) |
857 | a8083063 | Iustin Pop | return True |
858 | a8083063 | Iustin Pop | |
859 | a8083063 | Iustin Pop | |
860 | a8083063 | Iustin Pop | def GetMirrorStatus(disks): |
861 | a8083063 | Iustin Pop | """Get the mirroring status of a list of devices.
|
862 | a8083063 | Iustin Pop |
|
863 | a8083063 | Iustin Pop | Args:
|
864 | a8083063 | Iustin Pop | disks: list of `objects.Disk`
|
865 | a8083063 | Iustin Pop |
|
866 | a8083063 | Iustin Pop | Returns:
|
867 | a8083063 | Iustin Pop | list of (mirror_done, estimated_time) tuples, which
|
868 | a8083063 | Iustin Pop | are the result of bdev.BlockDevice.CombinedSyncStatus()
|
869 | a8083063 | Iustin Pop |
|
870 | a8083063 | Iustin Pop | """
|
871 | a8083063 | Iustin Pop | stats = [] |
872 | a8083063 | Iustin Pop | for dsk in disks: |
873 | a8083063 | Iustin Pop | rbd = _RecursiveFindBD(dsk) |
874 | a8083063 | Iustin Pop | if rbd is None: |
875 | 3ecf6786 | Iustin Pop | raise errors.BlockDeviceError("Can't find device %s" % str(dsk)) |
876 | a8083063 | Iustin Pop | stats.append(rbd.CombinedSyncStatus()) |
877 | a8083063 | Iustin Pop | return stats
|
878 | a8083063 | Iustin Pop | |
879 | a8083063 | Iustin Pop | |
880 | a8083063 | Iustin Pop | def _RecursiveFindBD(disk, allow_partial=False): |
881 | a8083063 | Iustin Pop | """Check if a device is activated.
|
882 | a8083063 | Iustin Pop |
|
883 | a8083063 | Iustin Pop | If so, return informations about the real device.
|
884 | a8083063 | Iustin Pop |
|
885 | a8083063 | Iustin Pop | Args:
|
886 | a8083063 | Iustin Pop | disk: the objects.Disk instance
|
887 | a8083063 | Iustin Pop | allow_partial: don't abort the find if a child of the
|
888 | a8083063 | Iustin Pop | device can't be found; this is intended to be
|
889 | a8083063 | Iustin Pop | used when repairing mirrors
|
890 | a8083063 | Iustin Pop |
|
891 | a8083063 | Iustin Pop | Returns:
|
892 | a8083063 | Iustin Pop | None if the device can't be found
|
893 | a8083063 | Iustin Pop | otherwise the device instance
|
894 | a8083063 | Iustin Pop |
|
895 | a8083063 | Iustin Pop | """
|
896 | a8083063 | Iustin Pop | children = [] |
897 | a8083063 | Iustin Pop | if disk.children:
|
898 | a8083063 | Iustin Pop | for chdisk in disk.children: |
899 | a8083063 | Iustin Pop | children.append(_RecursiveFindBD(chdisk)) |
900 | a8083063 | Iustin Pop | |
901 | a8083063 | Iustin Pop | return bdev.FindDevice(disk.dev_type, disk.physical_id, children)
|
902 | a8083063 | Iustin Pop | |
903 | a8083063 | Iustin Pop | |
904 | a8083063 | Iustin Pop | def FindBlockDevice(disk): |
905 | a8083063 | Iustin Pop | """Check if a device is activated.
|
906 | a8083063 | Iustin Pop |
|
907 | a8083063 | Iustin Pop | If so, return informations about the real device.
|
908 | a8083063 | Iustin Pop |
|
909 | a8083063 | Iustin Pop | Args:
|
910 | a8083063 | Iustin Pop | disk: the objects.Disk instance
|
911 | a8083063 | Iustin Pop | Returns:
|
912 | a8083063 | Iustin Pop | None if the device can't be found
|
913 | a8083063 | Iustin Pop | (device_path, major, minor, sync_percent, estimated_time, is_degraded)
|
914 | a8083063 | Iustin Pop |
|
915 | a8083063 | Iustin Pop | """
|
916 | a8083063 | Iustin Pop | rbd = _RecursiveFindBD(disk) |
917 | a8083063 | Iustin Pop | if rbd is None: |
918 | a8083063 | Iustin Pop | return rbd
|
919 | 0834c866 | Iustin Pop | return (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus()
|
920 | a8083063 | Iustin Pop | |
921 | a8083063 | Iustin Pop | |
922 | a8083063 | Iustin Pop | def UploadFile(file_name, data, mode, uid, gid, atime, mtime): |
923 | a8083063 | Iustin Pop | """Write a file to the filesystem.
|
924 | a8083063 | Iustin Pop |
|
925 | a8083063 | Iustin Pop | This allows the master to overwrite(!) a file. It will only perform
|
926 | a8083063 | Iustin Pop | the operation if the file belongs to a list of configuration files.
|
927 | a8083063 | Iustin Pop |
|
928 | a8083063 | Iustin Pop | """
|
929 | a8083063 | Iustin Pop | if not os.path.isabs(file_name): |
930 | a8083063 | Iustin Pop | logger.Error("Filename passed to UploadFile is not absolute: '%s'" %
|
931 | a8083063 | Iustin Pop | file_name) |
932 | a8083063 | Iustin Pop | return False |
933 | a8083063 | Iustin Pop | |
934 | 97628462 | Iustin Pop | allowed_files = [ |
935 | 97628462 | Iustin Pop | constants.CLUSTER_CONF_FILE, |
936 | 97628462 | Iustin Pop | constants.ETC_HOSTS, |
937 | 97628462 | Iustin Pop | constants.SSH_KNOWN_HOSTS_FILE, |
938 | 97628462 | Iustin Pop | ] |
939 | 880478f8 | Iustin Pop | allowed_files.extend(ssconf.SimpleStore().GetFileList()) |
940 | 880478f8 | Iustin Pop | if file_name not in allowed_files: |
941 | a8083063 | Iustin Pop | logger.Error("Filename passed to UploadFile not in allowed"
|
942 | a8083063 | Iustin Pop | " upload targets: '%s'" % file_name)
|
943 | a8083063 | Iustin Pop | return False |
944 | a8083063 | Iustin Pop | |
945 | 41a57aab | Michael Hanselmann | utils.WriteFile(file_name, data=data, mode=mode, uid=uid, gid=gid, |
946 | 41a57aab | Michael Hanselmann | atime=atime, mtime=mtime) |
947 | a8083063 | Iustin Pop | return True |
948 | a8083063 | Iustin Pop | |
949 | 386b57af | Iustin Pop | |
950 | a8083063 | Iustin Pop | def _ErrnoOrStr(err): |
951 | a8083063 | Iustin Pop | """Format an EnvironmentError exception.
|
952 | a8083063 | Iustin Pop |
|
953 | a8083063 | Iustin Pop | If the `err` argument has an errno attribute, it will be looked up
|
954 | a8083063 | Iustin Pop | and converted into a textual EXXXX description. Otherwise the string
|
955 | a8083063 | Iustin Pop | representation of the error will be returned.
|
956 | a8083063 | Iustin Pop |
|
957 | a8083063 | Iustin Pop | """
|
958 | a8083063 | Iustin Pop | if hasattr(err, 'errno'): |
959 | a8083063 | Iustin Pop | detail = errno.errorcode[err.errno] |
960 | a8083063 | Iustin Pop | else:
|
961 | a8083063 | Iustin Pop | detail = str(err)
|
962 | a8083063 | Iustin Pop | return detail
|
963 | a8083063 | Iustin Pop | |
964 | 5d0fe286 | Iustin Pop | |
965 | c26dabd7 | Guido Trotter | def _OSOndiskVersion(name, os_dir): |
966 | 2f8598a5 | Alexander Schreiber | """Compute and return the API version of a given OS.
|
967 | a8083063 | Iustin Pop |
|
968 | 2f8598a5 | Alexander Schreiber | This function will try to read the API version of the os given by
|
969 | 7c3d51d4 | Guido Trotter | the 'name' parameter and residing in the 'os_dir' directory.
|
970 | 7c3d51d4 | Guido Trotter |
|
971 | 7c3d51d4 | Guido Trotter | Return value will be either an integer denoting the version or None in the
|
972 | 7c3d51d4 | Guido Trotter | case when this is not a valid OS name.
|
973 | a8083063 | Iustin Pop |
|
974 | a8083063 | Iustin Pop | """
|
975 | a8083063 | Iustin Pop | api_file = os.path.sep.join([os_dir, "ganeti_api_version"])
|
976 | a8083063 | Iustin Pop | |
977 | a8083063 | Iustin Pop | try:
|
978 | a8083063 | Iustin Pop | st = os.stat(api_file) |
979 | a8083063 | Iustin Pop | except EnvironmentError, err: |
980 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file not" |
981 | 3ecf6786 | Iustin Pop | " found (%s)" % _ErrnoOrStr(err))
|
982 | a8083063 | Iustin Pop | |
983 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
984 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file is not" |
985 | 3ecf6786 | Iustin Pop | " a regular file")
|
986 | a8083063 | Iustin Pop | |
987 | a8083063 | Iustin Pop | try:
|
988 | a8083063 | Iustin Pop | f = open(api_file)
|
989 | a8083063 | Iustin Pop | try:
|
990 | a8083063 | Iustin Pop | api_version = f.read(256)
|
991 | a8083063 | Iustin Pop | finally:
|
992 | a8083063 | Iustin Pop | f.close() |
993 | a8083063 | Iustin Pop | except EnvironmentError, err: |
994 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "error while reading the" |
995 | 3ecf6786 | Iustin Pop | " API version (%s)" % _ErrnoOrStr(err))
|
996 | a8083063 | Iustin Pop | |
997 | a8083063 | Iustin Pop | api_version = api_version.strip() |
998 | a8083063 | Iustin Pop | try:
|
999 | a8083063 | Iustin Pop | api_version = int(api_version)
|
1000 | a8083063 | Iustin Pop | except (TypeError, ValueError), err: |
1001 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir,
|
1002 | 305a7297 | Guido Trotter | "API version is not integer (%s)" % str(err)) |
1003 | a8083063 | Iustin Pop | |
1004 | a8083063 | Iustin Pop | return api_version
|
1005 | a8083063 | Iustin Pop | |
1006 | 386b57af | Iustin Pop | |
1007 | 7c3d51d4 | Guido Trotter | def DiagnoseOS(top_dirs=None): |
1008 | a8083063 | Iustin Pop | """Compute the validity for all OSes.
|
1009 | a8083063 | Iustin Pop |
|
1010 | 8fa42c7c | Guido Trotter | Returns an OS object for each name in all the given top directories
|
1011 | 8fa42c7c | Guido Trotter | (if not given defaults to constants.OS_SEARCH_PATH)
|
1012 | a8083063 | Iustin Pop |
|
1013 | a8083063 | Iustin Pop | Returns:
|
1014 | 8fa42c7c | Guido Trotter | list of OS objects
|
1015 | a8083063 | Iustin Pop |
|
1016 | a8083063 | Iustin Pop | """
|
1017 | 7c3d51d4 | Guido Trotter | if top_dirs is None: |
1018 | 7c3d51d4 | Guido Trotter | top_dirs = constants.OS_SEARCH_PATH |
1019 | a8083063 | Iustin Pop | |
1020 | a8083063 | Iustin Pop | result = [] |
1021 | 65fe4693 | Iustin Pop | for dir_name in top_dirs: |
1022 | 65fe4693 | Iustin Pop | if os.path.isdir(dir_name):
|
1023 | 7c3d51d4 | Guido Trotter | try:
|
1024 | 65fe4693 | Iustin Pop | f_names = utils.ListVisibleFiles(dir_name) |
1025 | 7c3d51d4 | Guido Trotter | except EnvironmentError, err: |
1026 | 65fe4693 | Iustin Pop | logger.Error("Can't list the OS directory %s: %s" %
|
1027 | 65fe4693 | Iustin Pop | (dir_name, str(err)))
|
1028 | 7c3d51d4 | Guido Trotter | break
|
1029 | 7c3d51d4 | Guido Trotter | for name in f_names: |
1030 | 7c3d51d4 | Guido Trotter | try:
|
1031 | 65fe4693 | Iustin Pop | os_inst = OSFromDisk(name, base_dir=dir_name) |
1032 | 7c3d51d4 | Guido Trotter | result.append(os_inst) |
1033 | 7c3d51d4 | Guido Trotter | except errors.InvalidOS, err:
|
1034 | 8fa42c7c | Guido Trotter | result.append(objects.OS.FromInvalidOS(err)) |
1035 | a8083063 | Iustin Pop | |
1036 | a8083063 | Iustin Pop | return result
|
1037 | a8083063 | Iustin Pop | |
1038 | a8083063 | Iustin Pop | |
1039 | 56bcd3f4 | Guido Trotter | def OSFromDisk(name, base_dir=None): |
1040 | a8083063 | Iustin Pop | """Create an OS instance from disk.
|
1041 | a8083063 | Iustin Pop |
|
1042 | a8083063 | Iustin Pop | This function will return an OS instance if the given name is a
|
1043 | a8083063 | Iustin Pop | valid OS name. Otherwise, it will raise an appropriate
|
1044 | a8083063 | Iustin Pop | `errors.InvalidOS` exception, detailing why this is not a valid
|
1045 | a8083063 | Iustin Pop | OS.
|
1046 | a8083063 | Iustin Pop |
|
1047 | 7c3d51d4 | Guido Trotter | Args:
|
1048 | 7c3d51d4 | Guido Trotter | os_dir: Directory containing the OS scripts. Defaults to a search
|
1049 | 7c3d51d4 | Guido Trotter | in all the OS_SEARCH_PATH directories.
|
1050 | 7c3d51d4 | Guido Trotter |
|
1051 | a8083063 | Iustin Pop | """
|
1052 | 7c3d51d4 | Guido Trotter | |
1053 | 56bcd3f4 | Guido Trotter | if base_dir is None: |
1054 | 57c177af | Iustin Pop | os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir) |
1055 | c34c0cfd | Iustin Pop | if os_dir is None: |
1056 | c34c0cfd | Iustin Pop | raise errors.InvalidOS(name, None, "OS dir not found in search path") |
1057 | c34c0cfd | Iustin Pop | else:
|
1058 | c34c0cfd | Iustin Pop | os_dir = os.path.sep.join([base_dir, name]) |
1059 | a8083063 | Iustin Pop | |
1060 | c26dabd7 | Guido Trotter | api_version = _OSOndiskVersion(name, os_dir) |
1061 | a8083063 | Iustin Pop | |
1062 | a8083063 | Iustin Pop | if api_version != constants.OS_API_VERSION:
|
1063 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "API version mismatch" |
1064 | 305a7297 | Guido Trotter | " (found %s want %s)"
|
1065 | 3ecf6786 | Iustin Pop | % (api_version, constants.OS_API_VERSION)) |
1066 | a8083063 | Iustin Pop | |
1067 | a8083063 | Iustin Pop | # OS Scripts dictionary, we will populate it with the actual script names
|
1068 | 386b57af | Iustin Pop | os_scripts = {'create': '', 'export': '', 'import': '', 'rename': ''} |
1069 | a8083063 | Iustin Pop | |
1070 | a8083063 | Iustin Pop | for script in os_scripts: |
1071 | a8083063 | Iustin Pop | os_scripts[script] = os.path.sep.join([os_dir, script]) |
1072 | a8083063 | Iustin Pop | |
1073 | a8083063 | Iustin Pop | try:
|
1074 | a8083063 | Iustin Pop | st = os.stat(os_scripts[script]) |
1075 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1076 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "'%s' script missing (%s)" % |
1077 | 3ecf6786 | Iustin Pop | (script, _ErrnoOrStr(err))) |
1078 | a8083063 | Iustin Pop | |
1079 | a8083063 | Iustin Pop | if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
|
1080 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "'%s' script not executable" % |
1081 | 305a7297 | Guido Trotter | script) |
1082 | a8083063 | Iustin Pop | |
1083 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
1084 | 305a7297 | Guido Trotter | raise errors.InvalidOS(name, os_dir, "'%s' is not a regular file" % |
1085 | 305a7297 | Guido Trotter | script) |
1086 | a8083063 | Iustin Pop | |
1087 | a8083063 | Iustin Pop | |
1088 | 8fa42c7c | Guido Trotter | return objects.OS(name=name, path=os_dir, status=constants.OS_VALID_STATUS,
|
1089 | a8083063 | Iustin Pop | create_script=os_scripts['create'],
|
1090 | a8083063 | Iustin Pop | export_script=os_scripts['export'],
|
1091 | a8083063 | Iustin Pop | import_script=os_scripts['import'],
|
1092 | 386b57af | Iustin Pop | rename_script=os_scripts['rename'],
|
1093 | a8083063 | Iustin Pop | api_version=api_version) |
1094 | a8083063 | Iustin Pop | |
1095 | a8083063 | Iustin Pop | |
1096 | a8083063 | Iustin Pop | def SnapshotBlockDevice(disk): |
1097 | a8083063 | Iustin Pop | """Create a snapshot copy of a block device.
|
1098 | a8083063 | Iustin Pop |
|
1099 | a8083063 | Iustin Pop | This function is called recursively, and the snapshot is actually created
|
1100 | a8083063 | Iustin Pop | just for the leaf lvm backend device.
|
1101 | a8083063 | Iustin Pop |
|
1102 | a8083063 | Iustin Pop | Args:
|
1103 | a8083063 | Iustin Pop | disk: the disk to be snapshotted
|
1104 | a8083063 | Iustin Pop |
|
1105 | a8083063 | Iustin Pop | Returns:
|
1106 | a8083063 | Iustin Pop | a config entry for the actual lvm device snapshotted.
|
1107 | a8083063 | Iustin Pop |
|
1108 | 098c0958 | Michael Hanselmann | """
|
1109 | a8083063 | Iustin Pop | if disk.children:
|
1110 | a8083063 | Iustin Pop | if len(disk.children) == 1: |
1111 | a8083063 | Iustin Pop | # only one child, let's recurse on it
|
1112 | a8083063 | Iustin Pop | return SnapshotBlockDevice(disk.children[0]) |
1113 | a8083063 | Iustin Pop | else:
|
1114 | a8083063 | Iustin Pop | # more than one child, choose one that matches
|
1115 | a8083063 | Iustin Pop | for child in disk.children: |
1116 | a8083063 | Iustin Pop | if child.size == disk.size:
|
1117 | a8083063 | Iustin Pop | # return implies breaking the loop
|
1118 | a8083063 | Iustin Pop | return SnapshotBlockDevice(child)
|
1119 | fe96220b | Iustin Pop | elif disk.dev_type == constants.LD_LV:
|
1120 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
1121 | a8083063 | Iustin Pop | if r_dev is not None: |
1122 | a8083063 | Iustin Pop | # let's stay on the safe side and ask for the full size, for now
|
1123 | a8083063 | Iustin Pop | return r_dev.Snapshot(disk.size)
|
1124 | a8083063 | Iustin Pop | else:
|
1125 | a8083063 | Iustin Pop | return None |
1126 | a8083063 | Iustin Pop | else:
|
1127 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Cannot snapshot non-lvm block device" |
1128 | f4bc1f2c | Michael Hanselmann | " '%s' of type '%s'" %
|
1129 | 3ecf6786 | Iustin Pop | (disk.unique_id, disk.dev_type)) |
1130 | a8083063 | Iustin Pop | |
1131 | a8083063 | Iustin Pop | |
1132 | a8083063 | Iustin Pop | def ExportSnapshot(disk, dest_node, instance): |
1133 | a8083063 | Iustin Pop | """Export a block device snapshot to a remote node.
|
1134 | a8083063 | Iustin Pop |
|
1135 | a8083063 | Iustin Pop | Args:
|
1136 | a8083063 | Iustin Pop | disk: the snapshot block device
|
1137 | a8083063 | Iustin Pop | dest_node: the node to send the image to
|
1138 | a8083063 | Iustin Pop | instance: instance being exported
|
1139 | a8083063 | Iustin Pop |
|
1140 | a8083063 | Iustin Pop | Returns:
|
1141 | a8083063 | Iustin Pop | True if successful, False otherwise.
|
1142 | a8083063 | Iustin Pop |
|
1143 | 098c0958 | Michael Hanselmann | """
|
1144 | a8083063 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
1145 | a8083063 | Iustin Pop | export_script = inst_os.export_script |
1146 | a8083063 | Iustin Pop | |
1147 | a8083063 | Iustin Pop | logfile = "%s/exp-%s-%s-%s.log" % (constants.LOG_OS_DIR, inst_os.name,
|
1148 | a8083063 | Iustin Pop | instance.name, int(time.time()))
|
1149 | a8083063 | Iustin Pop | if not os.path.exists(constants.LOG_OS_DIR): |
1150 | a8083063 | Iustin Pop | os.mkdir(constants.LOG_OS_DIR, 0750)
|
1151 | a8083063 | Iustin Pop | |
1152 | a8083063 | Iustin Pop | real_os_dev = _RecursiveFindBD(disk) |
1153 | a8083063 | Iustin Pop | if real_os_dev is None: |
1154 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
1155 | a8083063 | Iustin Pop | str(disk))
|
1156 | a8083063 | Iustin Pop | real_os_dev.Open() |
1157 | a8083063 | Iustin Pop | |
1158 | a8083063 | Iustin Pop | destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
|
1159 | a8083063 | Iustin Pop | destfile = disk.physical_id[1]
|
1160 | a8083063 | Iustin Pop | |
1161 | a8083063 | Iustin Pop | # the target command is built out of three individual commands,
|
1162 | a8083063 | Iustin Pop | # which are joined by pipes; we check each individual command for
|
1163 | a8083063 | Iustin Pop | # valid parameters
|
1164 | a8083063 | Iustin Pop | |
1165 | a8083063 | Iustin Pop | expcmd = utils.BuildShellCmd("cd %s; %s -i %s -b %s 2>%s", inst_os.path,
|
1166 | a8083063 | Iustin Pop | export_script, instance.name, |
1167 | a8083063 | Iustin Pop | real_os_dev.dev_path, logfile) |
1168 | a8083063 | Iustin Pop | |
1169 | a8083063 | Iustin Pop | comprcmd = "gzip"
|
1170 | a8083063 | Iustin Pop | |
1171 | 72f0f7fd | Iustin Pop | destcmd = utils.BuildShellCmd("mkdir -p %s && cat > %s/%s",
|
1172 | 00003458 | Guido Trotter | destdir, destdir, destfile) |
1173 | c92b310a | Michael Hanselmann | remotecmd = _GetSshRunner().BuildCmd(dest_node, constants.GANETI_RUNAS, |
1174 | c92b310a | Michael Hanselmann | destcmd) |
1175 | a8083063 | Iustin Pop | |
1176 | a8083063 | Iustin Pop | # all commands have been checked, so we're safe to combine them
|
1177 | 72f0f7fd | Iustin Pop | command = '|'.join([expcmd, comprcmd, utils.ShellQuoteArgs(remotecmd)])
|
1178 | a8083063 | Iustin Pop | |
1179 | a8083063 | Iustin Pop | result = utils.RunCmd(command) |
1180 | a8083063 | Iustin Pop | |
1181 | a8083063 | Iustin Pop | if result.failed:
|
1182 | a8083063 | Iustin Pop | logger.Error("os snapshot export command '%s' returned error: %s"
|
1183 | a8083063 | Iustin Pop | " output: %s" %
|
1184 | a8083063 | Iustin Pop | (command, result.fail_reason, result.output)) |
1185 | a8083063 | Iustin Pop | return False |
1186 | a8083063 | Iustin Pop | |
1187 | a8083063 | Iustin Pop | return True |
1188 | a8083063 | Iustin Pop | |
1189 | a8083063 | Iustin Pop | |
1190 | a8083063 | Iustin Pop | def FinalizeExport(instance, snap_disks): |
1191 | a8083063 | Iustin Pop | """Write out the export configuration information.
|
1192 | a8083063 | Iustin Pop |
|
1193 | a8083063 | Iustin Pop | Args:
|
1194 | a8083063 | Iustin Pop | instance: instance configuration
|
1195 | a8083063 | Iustin Pop | snap_disks: snapshot block devices
|
1196 | a8083063 | Iustin Pop |
|
1197 | a8083063 | Iustin Pop | Returns:
|
1198 | a8083063 | Iustin Pop | False in case of error, True otherwise.
|
1199 | a8083063 | Iustin Pop |
|
1200 | 098c0958 | Michael Hanselmann | """
|
1201 | a8083063 | Iustin Pop | destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
|
1202 | a8083063 | Iustin Pop | finaldestdir = os.path.join(constants.EXPORT_DIR, instance.name) |
1203 | a8083063 | Iustin Pop | |
1204 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
1205 | a8083063 | Iustin Pop | |
1206 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_EXP) |
1207 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'version', '0') |
1208 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'timestamp', '%d' % int(time.time())) |
1209 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'source', instance.primary_node)
|
1210 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'os', instance.os)
|
1211 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'compression', 'gzip') |
1212 | a8083063 | Iustin Pop | |
1213 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_INS) |
1214 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'name', instance.name)
|
1215 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'memory', '%d' % instance.memory) |
1216 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'vcpus', '%d' % instance.vcpus) |
1217 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk_template', instance.disk_template)
|
1218 | 66f93869 | Manuel Franceschini | |
1219 | 66f93869 | Manuel Franceschini | nic_count = 0
|
1220 | a8083063 | Iustin Pop | for nic_count, nic in enumerate(instance.nics): |
1221 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'nic%d_mac' %
|
1222 | a8083063 | Iustin Pop | nic_count, '%s' % nic.mac)
|
1223 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'nic%d_ip' % nic_count, '%s' % nic.ip) |
1224 | 1cafd236 | Guido Trotter | config.set(constants.INISECT_INS, 'nic%d_bridge' % nic_count, '%s' % nic.bridge) |
1225 | a8083063 | Iustin Pop | # TODO: redundant: on load can read nics until it doesn't exist
|
1226 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'nic_count' , '%d' % nic_count) |
1227 | a8083063 | Iustin Pop | |
1228 | 66f93869 | Manuel Franceschini | disk_count = 0
|
1229 | a8083063 | Iustin Pop | for disk_count, disk in enumerate(snap_disks): |
1230 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk%d_ivname' % disk_count,
|
1231 | a8083063 | Iustin Pop | ('%s' % disk.iv_name))
|
1232 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk%d_dump' % disk_count,
|
1233 | a8083063 | Iustin Pop | ('%s' % disk.physical_id[1])) |
1234 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk%d_size' % disk_count,
|
1235 | a8083063 | Iustin Pop | ('%d' % disk.size))
|
1236 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk_count' , '%d' % disk_count) |
1237 | a8083063 | Iustin Pop | |
1238 | a8083063 | Iustin Pop | cff = os.path.join(destdir, constants.EXPORT_CONF_FILE) |
1239 | a8083063 | Iustin Pop | cfo = open(cff, 'w') |
1240 | a8083063 | Iustin Pop | try:
|
1241 | a8083063 | Iustin Pop | config.write(cfo) |
1242 | a8083063 | Iustin Pop | finally:
|
1243 | a8083063 | Iustin Pop | cfo.close() |
1244 | a8083063 | Iustin Pop | |
1245 | a8083063 | Iustin Pop | shutil.rmtree(finaldestdir, True)
|
1246 | a8083063 | Iustin Pop | shutil.move(destdir, finaldestdir) |
1247 | a8083063 | Iustin Pop | |
1248 | a8083063 | Iustin Pop | return True |
1249 | a8083063 | Iustin Pop | |
1250 | a8083063 | Iustin Pop | |
1251 | a8083063 | Iustin Pop | def ExportInfo(dest): |
1252 | a8083063 | Iustin Pop | """Get export configuration information.
|
1253 | a8083063 | Iustin Pop |
|
1254 | a8083063 | Iustin Pop | Args:
|
1255 | a8083063 | Iustin Pop | dest: directory containing the export
|
1256 | a8083063 | Iustin Pop |
|
1257 | a8083063 | Iustin Pop | Returns:
|
1258 | a8083063 | Iustin Pop | A serializable config file containing the export info.
|
1259 | a8083063 | Iustin Pop |
|
1260 | a8083063 | Iustin Pop | """
|
1261 | a8083063 | Iustin Pop | cff = os.path.join(dest, constants.EXPORT_CONF_FILE) |
1262 | a8083063 | Iustin Pop | |
1263 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
1264 | a8083063 | Iustin Pop | config.read(cff) |
1265 | a8083063 | Iustin Pop | |
1266 | a8083063 | Iustin Pop | if (not config.has_section(constants.INISECT_EXP) or |
1267 | a8083063 | Iustin Pop | not config.has_section(constants.INISECT_INS)):
|
1268 | a8083063 | Iustin Pop | return None |
1269 | a8083063 | Iustin Pop | |
1270 | a8083063 | Iustin Pop | return config
|
1271 | a8083063 | Iustin Pop | |
1272 | a8083063 | Iustin Pop | |
1273 | a8083063 | Iustin Pop | def ImportOSIntoInstance(instance, os_disk, swap_disk, src_node, src_image): |
1274 | a8083063 | Iustin Pop | """Import an os image into an instance.
|
1275 | a8083063 | Iustin Pop |
|
1276 | a8083063 | Iustin Pop | Args:
|
1277 | a8083063 | Iustin Pop | instance: the instance object
|
1278 | a8083063 | Iustin Pop | os_disk: the instance-visible name of the os device
|
1279 | a8083063 | Iustin Pop | swap_disk: the instance-visible name of the swap device
|
1280 | a8083063 | Iustin Pop | src_node: node holding the source image
|
1281 | a8083063 | Iustin Pop | src_image: path to the source image on src_node
|
1282 | a8083063 | Iustin Pop |
|
1283 | a8083063 | Iustin Pop | Returns:
|
1284 | a8083063 | Iustin Pop | False in case of error, True otherwise.
|
1285 | a8083063 | Iustin Pop |
|
1286 | a8083063 | Iustin Pop | """
|
1287 | a8083063 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
1288 | a8083063 | Iustin Pop | import_script = inst_os.import_script |
1289 | a8083063 | Iustin Pop | |
1290 | 9716fdce | Iustin Pop | os_device = instance.FindDisk(os_disk) |
1291 | 9716fdce | Iustin Pop | if os_device is None: |
1292 | a8083063 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % os_disk)
|
1293 | a8083063 | Iustin Pop | return False |
1294 | a8083063 | Iustin Pop | |
1295 | 9716fdce | Iustin Pop | swap_device = instance.FindDisk(swap_disk) |
1296 | 9716fdce | Iustin Pop | if swap_device is None: |
1297 | a8083063 | Iustin Pop | logger.Error("Can't find this device-visible name '%s'" % swap_disk)
|
1298 | a8083063 | Iustin Pop | return False |
1299 | a8083063 | Iustin Pop | |
1300 | a8083063 | Iustin Pop | real_os_dev = _RecursiveFindBD(os_device) |
1301 | a8083063 | Iustin Pop | if real_os_dev is None: |
1302 | 3ecf6786 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
1303 | 3ecf6786 | Iustin Pop | str(os_device))
|
1304 | a8083063 | Iustin Pop | real_os_dev.Open() |
1305 | a8083063 | Iustin Pop | |
1306 | a8083063 | Iustin Pop | real_swap_dev = _RecursiveFindBD(swap_device) |
1307 | a8083063 | Iustin Pop | if real_swap_dev is None: |
1308 | 3ecf6786 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up" % |
1309 | 3ecf6786 | Iustin Pop | str(swap_device))
|
1310 | a8083063 | Iustin Pop | real_swap_dev.Open() |
1311 | a8083063 | Iustin Pop | |
1312 | a8083063 | Iustin Pop | logfile = "%s/import-%s-%s-%s.log" % (constants.LOG_OS_DIR, instance.os,
|
1313 | a8083063 | Iustin Pop | instance.name, int(time.time()))
|
1314 | a8083063 | Iustin Pop | if not os.path.exists(constants.LOG_OS_DIR): |
1315 | a8083063 | Iustin Pop | os.mkdir(constants.LOG_OS_DIR, 0750)
|
1316 | a8083063 | Iustin Pop | |
1317 | 00003458 | Guido Trotter | destcmd = utils.BuildShellCmd('cat %s', src_image)
|
1318 | c92b310a | Michael Hanselmann | remotecmd = _GetSshRunner().BuildCmd(src_node, constants.GANETI_RUNAS, |
1319 | c92b310a | Michael Hanselmann | destcmd) |
1320 | a8083063 | Iustin Pop | |
1321 | a8083063 | Iustin Pop | comprcmd = "gunzip"
|
1322 | a8083063 | Iustin Pop | impcmd = utils.BuildShellCmd("(cd %s; %s -i %s -b %s -s %s &>%s)",
|
1323 | a8083063 | Iustin Pop | inst_os.path, import_script, instance.name, |
1324 | a8083063 | Iustin Pop | real_os_dev.dev_path, real_swap_dev.dev_path, |
1325 | a8083063 | Iustin Pop | logfile) |
1326 | a8083063 | Iustin Pop | |
1327 | 72f0f7fd | Iustin Pop | command = '|'.join([utils.ShellQuoteArgs(remotecmd), comprcmd, impcmd])
|
1328 | a8083063 | Iustin Pop | |
1329 | a8083063 | Iustin Pop | result = utils.RunCmd(command) |
1330 | a8083063 | Iustin Pop | |
1331 | a8083063 | Iustin Pop | if result.failed:
|
1332 | a8083063 | Iustin Pop | logger.Error("os import command '%s' returned error: %s"
|
1333 | a8083063 | Iustin Pop | " output: %s" %
|
1334 | a8083063 | Iustin Pop | (command, result.fail_reason, result.output)) |
1335 | a8083063 | Iustin Pop | return False |
1336 | a8083063 | Iustin Pop | |
1337 | a8083063 | Iustin Pop | return True |
1338 | a8083063 | Iustin Pop | |
1339 | a8083063 | Iustin Pop | |
1340 | a8083063 | Iustin Pop | def ListExports(): |
1341 | a8083063 | Iustin Pop | """Return a list of exports currently available on this machine.
|
1342 | 098c0958 | Michael Hanselmann |
|
1343 | a8083063 | Iustin Pop | """
|
1344 | a8083063 | Iustin Pop | if os.path.isdir(constants.EXPORT_DIR):
|
1345 | eedbda4b | Michael Hanselmann | return utils.ListVisibleFiles(constants.EXPORT_DIR)
|
1346 | a8083063 | Iustin Pop | else:
|
1347 | a8083063 | Iustin Pop | return []
|
1348 | a8083063 | Iustin Pop | |
1349 | a8083063 | Iustin Pop | |
1350 | a8083063 | Iustin Pop | def RemoveExport(export): |
1351 | a8083063 | Iustin Pop | """Remove an existing export from the node.
|
1352 | a8083063 | Iustin Pop |
|
1353 | a8083063 | Iustin Pop | Args:
|
1354 | a8083063 | Iustin Pop | export: the name of the export to remove
|
1355 | a8083063 | Iustin Pop |
|
1356 | a8083063 | Iustin Pop | Returns:
|
1357 | a8083063 | Iustin Pop | False in case of error, True otherwise.
|
1358 | a8083063 | Iustin Pop |
|
1359 | 098c0958 | Michael Hanselmann | """
|
1360 | a8083063 | Iustin Pop | target = os.path.join(constants.EXPORT_DIR, export) |
1361 | a8083063 | Iustin Pop | |
1362 | a8083063 | Iustin Pop | shutil.rmtree(target) |
1363 | a8083063 | Iustin Pop | # TODO: catch some of the relevant exceptions and provide a pretty
|
1364 | a8083063 | Iustin Pop | # error message if rmtree fails.
|
1365 | a8083063 | Iustin Pop | |
1366 | a8083063 | Iustin Pop | return True |
1367 | a8083063 | Iustin Pop | |
1368 | a8083063 | Iustin Pop | |
1369 | f3e513ad | Iustin Pop | def RenameBlockDevices(devlist): |
1370 | f3e513ad | Iustin Pop | """Rename a list of block devices.
|
1371 | f3e513ad | Iustin Pop |
|
1372 | f3e513ad | Iustin Pop | The devlist argument is a list of tuples (disk, new_logical,
|
1373 | f3e513ad | Iustin Pop | new_physical). The return value will be a combined boolean result
|
1374 | f3e513ad | Iustin Pop | (True only if all renames succeeded).
|
1375 | f3e513ad | Iustin Pop |
|
1376 | f3e513ad | Iustin Pop | """
|
1377 | f3e513ad | Iustin Pop | result = True
|
1378 | f3e513ad | Iustin Pop | for disk, unique_id in devlist: |
1379 | f3e513ad | Iustin Pop | dev = _RecursiveFindBD(disk) |
1380 | f3e513ad | Iustin Pop | if dev is None: |
1381 | f3e513ad | Iustin Pop | result = False
|
1382 | f3e513ad | Iustin Pop | continue
|
1383 | f3e513ad | Iustin Pop | try:
|
1384 | 3f78eef2 | Iustin Pop | old_rpath = dev.dev_path |
1385 | f3e513ad | Iustin Pop | dev.Rename(unique_id) |
1386 | 3f78eef2 | Iustin Pop | new_rpath = dev.dev_path |
1387 | 3f78eef2 | Iustin Pop | if old_rpath != new_rpath:
|
1388 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(old_rpath) |
1389 | 3f78eef2 | Iustin Pop | # FIXME: we should add the new cache information here, like:
|
1390 | 3f78eef2 | Iustin Pop | # DevCacheManager.UpdateCache(new_rpath, owner, ...)
|
1391 | 3f78eef2 | Iustin Pop | # but we don't have the owner here - maybe parse from existing
|
1392 | 3f78eef2 | Iustin Pop | # cache? for now, we only lose lvm data when we rename, which
|
1393 | 3f78eef2 | Iustin Pop | # is less critical than DRBD or MD
|
1394 | f3e513ad | Iustin Pop | except errors.BlockDeviceError, err:
|
1395 | f3e513ad | Iustin Pop | logger.Error("Can't rename device '%s' to '%s': %s" %
|
1396 | f3e513ad | Iustin Pop | (dev, unique_id, err)) |
1397 | f3e513ad | Iustin Pop | result = False
|
1398 | f3e513ad | Iustin Pop | return result
|
1399 | f3e513ad | Iustin Pop | |
1400 | f3e513ad | Iustin Pop | |
1401 | 778b75bb | Manuel Franceschini | def _TransformFileStorageDir(file_storage_dir): |
1402 | 778b75bb | Manuel Franceschini | """Checks whether given file_storage_dir is valid.
|
1403 | 778b75bb | Manuel Franceschini |
|
1404 | 778b75bb | Manuel Franceschini | Checks wheter the given file_storage_dir is within the cluster-wide
|
1405 | 778b75bb | Manuel Franceschini | default file_storage_dir stored in SimpleStore. Only paths under that
|
1406 | 778b75bb | Manuel Franceschini | directory are allowed.
|
1407 | 778b75bb | Manuel Franceschini |
|
1408 | 778b75bb | Manuel Franceschini | Args:
|
1409 | 778b75bb | Manuel Franceschini | file_storage_dir: string with path
|
1410 | 778b75bb | Manuel Franceschini |
|
1411 | 778b75bb | Manuel Franceschini | Returns:
|
1412 | 778b75bb | Manuel Franceschini | normalized file_storage_dir (string) if valid, None otherwise
|
1413 | 778b75bb | Manuel Franceschini |
|
1414 | 778b75bb | Manuel Franceschini | """
|
1415 | 778b75bb | Manuel Franceschini | file_storage_dir = os.path.normpath(file_storage_dir) |
1416 | 778b75bb | Manuel Franceschini | base_file_storage_dir = ssconf.SimpleStore().GetFileStorageDir() |
1417 | 778b75bb | Manuel Franceschini | if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) == |
1418 | 778b75bb | Manuel Franceschini | base_file_storage_dir): |
1419 | 778b75bb | Manuel Franceschini | logger.Error("file storage directory '%s' is not under base file"
|
1420 | 778b75bb | Manuel Franceschini | " storage directory '%s'" %
|
1421 | 778b75bb | Manuel Franceschini | (file_storage_dir, base_file_storage_dir)) |
1422 | 778b75bb | Manuel Franceschini | return None |
1423 | 778b75bb | Manuel Franceschini | return file_storage_dir
|
1424 | 778b75bb | Manuel Franceschini | |
1425 | 778b75bb | Manuel Franceschini | |
1426 | 778b75bb | Manuel Franceschini | def CreateFileStorageDir(file_storage_dir): |
1427 | 778b75bb | Manuel Franceschini | """Create file storage directory.
|
1428 | 778b75bb | Manuel Franceschini |
|
1429 | 778b75bb | Manuel Franceschini | Args:
|
1430 | 778b75bb | Manuel Franceschini | file_storage_dir: string containing the path
|
1431 | 778b75bb | Manuel Franceschini |
|
1432 | 778b75bb | Manuel Franceschini | Returns:
|
1433 | 778b75bb | Manuel Franceschini | tuple with first element a boolean indicating wheter dir
|
1434 | 778b75bb | Manuel Franceschini | creation was successful or not
|
1435 | 778b75bb | Manuel Franceschini |
|
1436 | 778b75bb | Manuel Franceschini | """
|
1437 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
1438 | 778b75bb | Manuel Franceschini | result = True,
|
1439 | 778b75bb | Manuel Franceschini | if not file_storage_dir: |
1440 | 778b75bb | Manuel Franceschini | result = False,
|
1441 | 778b75bb | Manuel Franceschini | else:
|
1442 | 778b75bb | Manuel Franceschini | if os.path.exists(file_storage_dir):
|
1443 | 778b75bb | Manuel Franceschini | if not os.path.isdir(file_storage_dir): |
1444 | 778b75bb | Manuel Franceschini | logger.Error("'%s' is not a directory" % file_storage_dir)
|
1445 | 778b75bb | Manuel Franceschini | result = False,
|
1446 | 778b75bb | Manuel Franceschini | else:
|
1447 | 778b75bb | Manuel Franceschini | try:
|
1448 | 778b75bb | Manuel Franceschini | os.makedirs(file_storage_dir, 0750)
|
1449 | 778b75bb | Manuel Franceschini | except OSError, err: |
1450 | 778b75bb | Manuel Franceschini | logger.Error("Cannot create file storage directory '%s': %s" %
|
1451 | 778b75bb | Manuel Franceschini | (file_storage_dir, err)) |
1452 | 778b75bb | Manuel Franceschini | result = False,
|
1453 | 778b75bb | Manuel Franceschini | return result
|
1454 | 778b75bb | Manuel Franceschini | |
1455 | 778b75bb | Manuel Franceschini | |
1456 | 778b75bb | Manuel Franceschini | def RemoveFileStorageDir(file_storage_dir): |
1457 | 778b75bb | Manuel Franceschini | """Remove file storage directory.
|
1458 | 778b75bb | Manuel Franceschini |
|
1459 | 778b75bb | Manuel Franceschini | Remove it only if it's empty. If not log an error and return.
|
1460 | 778b75bb | Manuel Franceschini |
|
1461 | 778b75bb | Manuel Franceschini | Args:
|
1462 | 778b75bb | Manuel Franceschini | file_storage_dir: string containing the path
|
1463 | 778b75bb | Manuel Franceschini |
|
1464 | 778b75bb | Manuel Franceschini | Returns:
|
1465 | 778b75bb | Manuel Franceschini | tuple with first element a boolean indicating wheter dir
|
1466 | 778b75bb | Manuel Franceschini | removal was successful or not
|
1467 | 778b75bb | Manuel Franceschini |
|
1468 | 778b75bb | Manuel Franceschini | """
|
1469 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
1470 | 778b75bb | Manuel Franceschini | result = True,
|
1471 | 778b75bb | Manuel Franceschini | if not file_storage_dir: |
1472 | 778b75bb | Manuel Franceschini | result = False,
|
1473 | 778b75bb | Manuel Franceschini | else:
|
1474 | 778b75bb | Manuel Franceschini | if os.path.exists(file_storage_dir):
|
1475 | 778b75bb | Manuel Franceschini | if not os.path.isdir(file_storage_dir): |
1476 | 778b75bb | Manuel Franceschini | logger.Error("'%s' is not a directory" % file_storage_dir)
|
1477 | 778b75bb | Manuel Franceschini | result = False,
|
1478 | 778b75bb | Manuel Franceschini | # deletes dir only if empty, otherwise we want to return False
|
1479 | 778b75bb | Manuel Franceschini | try:
|
1480 | 778b75bb | Manuel Franceschini | os.rmdir(file_storage_dir) |
1481 | 778b75bb | Manuel Franceschini | except OSError, err: |
1482 | 778b75bb | Manuel Franceschini | logger.Error("Cannot remove file storage directory '%s': %s" %
|
1483 | 778b75bb | Manuel Franceschini | (file_storage_dir, err)) |
1484 | 778b75bb | Manuel Franceschini | result = False,
|
1485 | 778b75bb | Manuel Franceschini | return result
|
1486 | 778b75bb | Manuel Franceschini | |
1487 | 778b75bb | Manuel Franceschini | |
1488 | 778b75bb | Manuel Franceschini | def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir): |
1489 | 778b75bb | Manuel Franceschini | """Rename the file storage directory.
|
1490 | 778b75bb | Manuel Franceschini |
|
1491 | 778b75bb | Manuel Franceschini | Args:
|
1492 | 778b75bb | Manuel Franceschini | old_file_storage_dir: string containing the old path
|
1493 | 778b75bb | Manuel Franceschini | new_file_storage_dir: string containing the new path
|
1494 | 778b75bb | Manuel Franceschini |
|
1495 | 778b75bb | Manuel Franceschini | Returns:
|
1496 | 778b75bb | Manuel Franceschini | tuple with first element a boolean indicating wheter dir
|
1497 | 778b75bb | Manuel Franceschini | rename was successful or not
|
1498 | 778b75bb | Manuel Franceschini |
|
1499 | 778b75bb | Manuel Franceschini | """
|
1500 | 778b75bb | Manuel Franceschini | old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir) |
1501 | 778b75bb | Manuel Franceschini | new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir) |
1502 | 778b75bb | Manuel Franceschini | result = True,
|
1503 | 778b75bb | Manuel Franceschini | if not old_file_storage_dir or not new_file_storage_dir: |
1504 | 778b75bb | Manuel Franceschini | result = False,
|
1505 | 778b75bb | Manuel Franceschini | else:
|
1506 | 778b75bb | Manuel Franceschini | if not os.path.exists(new_file_storage_dir): |
1507 | 778b75bb | Manuel Franceschini | if os.path.isdir(old_file_storage_dir):
|
1508 | 778b75bb | Manuel Franceschini | try:
|
1509 | 778b75bb | Manuel Franceschini | os.rename(old_file_storage_dir, new_file_storage_dir) |
1510 | 778b75bb | Manuel Franceschini | except OSError, err: |
1511 | 778b75bb | Manuel Franceschini | logger.Error("Cannot rename '%s' to '%s': %s"
|
1512 | 778b75bb | Manuel Franceschini | % (old_file_storage_dir, new_file_storage_dir, err)) |
1513 | 778b75bb | Manuel Franceschini | result = False,
|
1514 | 778b75bb | Manuel Franceschini | else:
|
1515 | 778b75bb | Manuel Franceschini | logger.Error("'%s' is not a directory" % old_file_storage_dir)
|
1516 | 778b75bb | Manuel Franceschini | result = False,
|
1517 | 778b75bb | Manuel Franceschini | else:
|
1518 | 778b75bb | Manuel Franceschini | if os.path.exists(old_file_storage_dir):
|
1519 | 778b75bb | Manuel Franceschini | logger.Error("Cannot rename '%s' to '%s'. Both locations exist." %
|
1520 | 778b75bb | Manuel Franceschini | old_file_storage_dir, new_file_storage_dir) |
1521 | 778b75bb | Manuel Franceschini | result = False,
|
1522 | 778b75bb | Manuel Franceschini | return result
|
1523 | 778b75bb | Manuel Franceschini | |
1524 | 778b75bb | Manuel Franceschini | |
1525 | a8083063 | Iustin Pop | class HooksRunner(object): |
1526 | a8083063 | Iustin Pop | """Hook runner.
|
1527 | a8083063 | Iustin Pop |
|
1528 | a8083063 | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not on
|
1529 | a8083063 | Iustin Pop | the master side.
|
1530 | a8083063 | Iustin Pop |
|
1531 | a8083063 | Iustin Pop | """
|
1532 | a8083063 | Iustin Pop | RE_MASK = re.compile("^[a-zA-Z0-9_-]+$")
|
1533 | a8083063 | Iustin Pop | |
1534 | a8083063 | Iustin Pop | def __init__(self, hooks_base_dir=None): |
1535 | a8083063 | Iustin Pop | """Constructor for hooks runner.
|
1536 | a8083063 | Iustin Pop |
|
1537 | a8083063 | Iustin Pop | Args:
|
1538 | a8083063 | Iustin Pop | - hooks_base_dir: if not None, this overrides the
|
1539 | a8083063 | Iustin Pop | constants.HOOKS_BASE_DIR (useful for unittests)
|
1540 | a8083063 | Iustin Pop |
|
1541 | a8083063 | Iustin Pop | """
|
1542 | a8083063 | Iustin Pop | if hooks_base_dir is None: |
1543 | a8083063 | Iustin Pop | hooks_base_dir = constants.HOOKS_BASE_DIR |
1544 | a8083063 | Iustin Pop | self._BASE_DIR = hooks_base_dir
|
1545 | a8083063 | Iustin Pop | |
1546 | a8083063 | Iustin Pop | @staticmethod
|
1547 | a8083063 | Iustin Pop | def ExecHook(script, env): |
1548 | a8083063 | Iustin Pop | """Exec one hook script.
|
1549 | a8083063 | Iustin Pop |
|
1550 | a8083063 | Iustin Pop | Args:
|
1551 | a8083063 | Iustin Pop | - script: the full path to the script
|
1552 | a8083063 | Iustin Pop | - env: the environment with which to exec the script
|
1553 | a8083063 | Iustin Pop |
|
1554 | a8083063 | Iustin Pop | """
|
1555 | a8083063 | Iustin Pop | # exec the process using subprocess and log the output
|
1556 | a8083063 | Iustin Pop | fdstdin = None
|
1557 | a8083063 | Iustin Pop | try:
|
1558 | a8083063 | Iustin Pop | fdstdin = open("/dev/null", "r") |
1559 | a8083063 | Iustin Pop | child = subprocess.Popen([script], stdin=fdstdin, stdout=subprocess.PIPE, |
1560 | a8083063 | Iustin Pop | stderr=subprocess.STDOUT, close_fds=True,
|
1561 | 147af04d | Iustin Pop | shell=False, cwd="/", env=env) |
1562 | a8083063 | Iustin Pop | output = ""
|
1563 | a8083063 | Iustin Pop | try:
|
1564 | a8083063 | Iustin Pop | output = child.stdout.read(4096)
|
1565 | a8083063 | Iustin Pop | child.stdout.close() |
1566 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1567 | a8083063 | Iustin Pop | output += "Hook script error: %s" % str(err) |
1568 | a8083063 | Iustin Pop | |
1569 | a8083063 | Iustin Pop | while True: |
1570 | a8083063 | Iustin Pop | try:
|
1571 | a8083063 | Iustin Pop | result = child.wait() |
1572 | a8083063 | Iustin Pop | break
|
1573 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1574 | a8083063 | Iustin Pop | if err.errno == errno.EINTR:
|
1575 | a8083063 | Iustin Pop | continue
|
1576 | a8083063 | Iustin Pop | raise
|
1577 | a8083063 | Iustin Pop | finally:
|
1578 | a8083063 | Iustin Pop | # try not to leak fds
|
1579 | a8083063 | Iustin Pop | for fd in (fdstdin, ): |
1580 | a8083063 | Iustin Pop | if fd is not None: |
1581 | a8083063 | Iustin Pop | try:
|
1582 | a8083063 | Iustin Pop | fd.close() |
1583 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1584 | a8083063 | Iustin Pop | # just log the error
|
1585 | a8083063 | Iustin Pop | #logger.Error("While closing fd %s: %s" % (fd, err))
|
1586 | a8083063 | Iustin Pop | pass
|
1587 | a8083063 | Iustin Pop | |
1588 | a8083063 | Iustin Pop | return result == 0, output |
1589 | a8083063 | Iustin Pop | |
1590 | a8083063 | Iustin Pop | def RunHooks(self, hpath, phase, env): |
1591 | a8083063 | Iustin Pop | """Run the scripts in the hooks directory.
|
1592 | a8083063 | Iustin Pop |
|
1593 | a8083063 | Iustin Pop | This method will not be usually overriden by child opcodes.
|
1594 | a8083063 | Iustin Pop |
|
1595 | a8083063 | Iustin Pop | """
|
1596 | a8083063 | Iustin Pop | if phase == constants.HOOKS_PHASE_PRE:
|
1597 | a8083063 | Iustin Pop | suffix = "pre"
|
1598 | a8083063 | Iustin Pop | elif phase == constants.HOOKS_PHASE_POST:
|
1599 | a8083063 | Iustin Pop | suffix = "post"
|
1600 | a8083063 | Iustin Pop | else:
|
1601 | 3ecf6786 | Iustin Pop | raise errors.ProgrammerError("Unknown hooks phase: '%s'" % phase) |
1602 | a8083063 | Iustin Pop | rr = [] |
1603 | a8083063 | Iustin Pop | |
1604 | a8083063 | Iustin Pop | subdir = "%s-%s.d" % (hpath, suffix)
|
1605 | a8083063 | Iustin Pop | dir_name = "%s/%s" % (self._BASE_DIR, subdir) |
1606 | a8083063 | Iustin Pop | try:
|
1607 | eedbda4b | Michael Hanselmann | dir_contents = utils.ListVisibleFiles(dir_name) |
1608 | a8083063 | Iustin Pop | except OSError, err: |
1609 | a8083063 | Iustin Pop | # must log
|
1610 | a8083063 | Iustin Pop | return rr
|
1611 | a8083063 | Iustin Pop | |
1612 | a8083063 | Iustin Pop | # we use the standard python sort order,
|
1613 | a8083063 | Iustin Pop | # so 00name is the recommended naming scheme
|
1614 | a8083063 | Iustin Pop | dir_contents.sort() |
1615 | a8083063 | Iustin Pop | for relname in dir_contents: |
1616 | a8083063 | Iustin Pop | fname = os.path.join(dir_name, relname) |
1617 | a8083063 | Iustin Pop | if not (os.path.isfile(fname) and os.access(fname, os.X_OK) and |
1618 | a8083063 | Iustin Pop | self.RE_MASK.match(relname) is not None): |
1619 | a8083063 | Iustin Pop | rrval = constants.HKR_SKIP |
1620 | a8083063 | Iustin Pop | output = ""
|
1621 | a8083063 | Iustin Pop | else:
|
1622 | a8083063 | Iustin Pop | result, output = self.ExecHook(fname, env)
|
1623 | a8083063 | Iustin Pop | if not result: |
1624 | a8083063 | Iustin Pop | rrval = constants.HKR_FAIL |
1625 | a8083063 | Iustin Pop | else:
|
1626 | a8083063 | Iustin Pop | rrval = constants.HKR_SUCCESS |
1627 | a8083063 | Iustin Pop | rr.append(("%s/%s" % (subdir, relname), rrval, output))
|
1628 | a8083063 | Iustin Pop | |
1629 | a8083063 | Iustin Pop | return rr
|
1630 | 3f78eef2 | Iustin Pop | |
1631 | 3f78eef2 | Iustin Pop | |
1632 | 8d528b7c | Iustin Pop | class IAllocatorRunner(object): |
1633 | 8d528b7c | Iustin Pop | """IAllocator runner.
|
1634 | 8d528b7c | Iustin Pop |
|
1635 | 8d528b7c | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not on
|
1636 | 8d528b7c | Iustin Pop | the master side.
|
1637 | 8d528b7c | Iustin Pop |
|
1638 | 8d528b7c | Iustin Pop | """
|
1639 | 8d528b7c | Iustin Pop | def Run(self, name, idata): |
1640 | 8d528b7c | Iustin Pop | """Run an iallocator script.
|
1641 | 8d528b7c | Iustin Pop |
|
1642 | 8d528b7c | Iustin Pop | Return value: tuple of:
|
1643 | 8d528b7c | Iustin Pop | - run status (one of the IARUN_ constants)
|
1644 | 8d528b7c | Iustin Pop | - stdout
|
1645 | 8d528b7c | Iustin Pop | - stderr
|
1646 | 8d528b7c | Iustin Pop | - fail reason (as from utils.RunResult)
|
1647 | 8d528b7c | Iustin Pop |
|
1648 | 8d528b7c | Iustin Pop | """
|
1649 | 8d528b7c | Iustin Pop | alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH, |
1650 | 8d528b7c | Iustin Pop | os.path.isfile) |
1651 | 8d528b7c | Iustin Pop | if alloc_script is None: |
1652 | 8d528b7c | Iustin Pop | return (constants.IARUN_NOTFOUND, None, None, None) |
1653 | 8d528b7c | Iustin Pop | |
1654 | 8d528b7c | Iustin Pop | fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
|
1655 | 8d528b7c | Iustin Pop | try:
|
1656 | 8d528b7c | Iustin Pop | os.write(fd, idata) |
1657 | 8d528b7c | Iustin Pop | os.close(fd) |
1658 | 8d528b7c | Iustin Pop | result = utils.RunCmd([alloc_script, fin_name]) |
1659 | 8d528b7c | Iustin Pop | if result.failed:
|
1660 | 8d528b7c | Iustin Pop | return (constants.IARUN_FAILURE, result.stdout, result.stderr,
|
1661 | 8d528b7c | Iustin Pop | result.fail_reason) |
1662 | 8d528b7c | Iustin Pop | finally:
|
1663 | 8d528b7c | Iustin Pop | os.unlink(fin_name) |
1664 | 8d528b7c | Iustin Pop | |
1665 | 8d528b7c | Iustin Pop | return (constants.IARUN_SUCCESS, result.stdout, result.stderr, None) |
1666 | 8d528b7c | Iustin Pop | |
1667 | 8d528b7c | Iustin Pop | |
1668 | 3f78eef2 | Iustin Pop | class DevCacheManager(object): |
1669 | c99a3cc0 | Manuel Franceschini | """Simple class for managing a cache of block device information.
|
1670 | 3f78eef2 | Iustin Pop |
|
1671 | 3f78eef2 | Iustin Pop | """
|
1672 | 3f78eef2 | Iustin Pop | _DEV_PREFIX = "/dev/"
|
1673 | 3f78eef2 | Iustin Pop | _ROOT_DIR = constants.BDEV_CACHE_DIR |
1674 | 3f78eef2 | Iustin Pop | |
1675 | 3f78eef2 | Iustin Pop | @classmethod
|
1676 | 3f78eef2 | Iustin Pop | def _ConvertPath(cls, dev_path): |
1677 | 3f78eef2 | Iustin Pop | """Converts a /dev/name path to the cache file name.
|
1678 | 3f78eef2 | Iustin Pop |
|
1679 | 3f78eef2 | Iustin Pop | This replaces slashes with underscores and strips the /dev
|
1680 | 3f78eef2 | Iustin Pop | prefix. It then returns the full path to the cache file
|
1681 | 3f78eef2 | Iustin Pop |
|
1682 | 3f78eef2 | Iustin Pop | """
|
1683 | 3f78eef2 | Iustin Pop | if dev_path.startswith(cls._DEV_PREFIX):
|
1684 | 3f78eef2 | Iustin Pop | dev_path = dev_path[len(cls._DEV_PREFIX):]
|
1685 | 3f78eef2 | Iustin Pop | dev_path = dev_path.replace("/", "_") |
1686 | 3f78eef2 | Iustin Pop | fpath = "%s/bdev_%s" % (cls._ROOT_DIR, dev_path)
|
1687 | 3f78eef2 | Iustin Pop | return fpath
|
1688 | 3f78eef2 | Iustin Pop | |
1689 | 3f78eef2 | Iustin Pop | @classmethod
|
1690 | 3f78eef2 | Iustin Pop | def UpdateCache(cls, dev_path, owner, on_primary, iv_name): |
1691 | 3f78eef2 | Iustin Pop | """Updates the cache information for a given device.
|
1692 | 3f78eef2 | Iustin Pop |
|
1693 | 3f78eef2 | Iustin Pop | """
|
1694 | cf5a8306 | Iustin Pop | if dev_path is None: |
1695 | cf5a8306 | Iustin Pop | logger.Error("DevCacheManager.UpdateCache got a None dev_path")
|
1696 | cf5a8306 | Iustin Pop | return
|
1697 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
1698 | 3f78eef2 | Iustin Pop | if on_primary:
|
1699 | 3f78eef2 | Iustin Pop | state = "primary"
|
1700 | 3f78eef2 | Iustin Pop | else:
|
1701 | 3f78eef2 | Iustin Pop | state = "secondary"
|
1702 | 3f78eef2 | Iustin Pop | if iv_name is None: |
1703 | 3f78eef2 | Iustin Pop | iv_name = "not_visible"
|
1704 | 3f78eef2 | Iustin Pop | fdata = "%s %s %s\n" % (str(owner), state, iv_name) |
1705 | 3f78eef2 | Iustin Pop | try:
|
1706 | 3f78eef2 | Iustin Pop | utils.WriteFile(fpath, data=fdata) |
1707 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
1708 | 3f78eef2 | Iustin Pop | logger.Error("Can't update bdev cache for %s, error %s" %
|
1709 | 3f78eef2 | Iustin Pop | (dev_path, str(err)))
|
1710 | 3f78eef2 | Iustin Pop | |
1711 | 3f78eef2 | Iustin Pop | @classmethod
|
1712 | 3f78eef2 | Iustin Pop | def RemoveCache(cls, dev_path): |
1713 | 3f78eef2 | Iustin Pop | """Remove data for a dev_path.
|
1714 | 3f78eef2 | Iustin Pop |
|
1715 | 3f78eef2 | Iustin Pop | """
|
1716 | cf5a8306 | Iustin Pop | if dev_path is None: |
1717 | cf5a8306 | Iustin Pop | logger.Error("DevCacheManager.RemoveCache got a None dev_path")
|
1718 | cf5a8306 | Iustin Pop | return
|
1719 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
1720 | 3f78eef2 | Iustin Pop | try:
|
1721 | 3f78eef2 | Iustin Pop | utils.RemoveFile(fpath) |
1722 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
1723 | 3f78eef2 | Iustin Pop | logger.Error("Can't update bdev cache for %s, error %s" %
|
1724 | 3f78eef2 | Iustin Pop | (dev_path, str(err))) |