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