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