root / lib / backend.py @ 26d3fd2f
History | View | Annotate | Download (97.8 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a025e535 | Vitaly Kuznetsov | # Copyright (C) 2006, 2007, 2008, 2009, 2010 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 | 360b0dc2 | Iustin Pop | """Functions used by the node daemon
|
23 | 360b0dc2 | Iustin Pop |
|
24 | 360b0dc2 | Iustin Pop | @var _ALLOWED_UPLOAD_FILES: denotes which files are accepted in
|
25 | 360b0dc2 | Iustin Pop | the L{UploadFile} function
|
26 | 714ea7ca | Iustin Pop | @var _ALLOWED_CLEAN_DIRS: denotes which directories are accepted
|
27 | 714ea7ca | Iustin Pop | in the L{_CleanDirectory} function
|
28 | 360b0dc2 | Iustin Pop |
|
29 | 360b0dc2 | Iustin Pop | """
|
30 | a8083063 | Iustin Pop | |
31 | 6c881c52 | Iustin Pop | # pylint: disable-msg=E1103
|
32 | 6c881c52 | Iustin Pop | |
33 | 6c881c52 | Iustin Pop | # E1103: %s %r has no %r member (but some types could not be
|
34 | 6c881c52 | Iustin Pop | # inferred), because the _TryOSFromDisk returns either (True, os_obj)
|
35 | 6c881c52 | Iustin Pop | # or (False, "string") which confuses pylint
|
36 | 6c881c52 | Iustin Pop | |
37 | a8083063 | Iustin Pop | |
38 | a8083063 | Iustin Pop | import os |
39 | a8083063 | Iustin Pop | import os.path |
40 | a8083063 | Iustin Pop | import shutil |
41 | a8083063 | Iustin Pop | import time |
42 | a8083063 | Iustin Pop | import stat |
43 | a8083063 | Iustin Pop | import errno |
44 | a8083063 | Iustin Pop | import re |
45 | b544cfe0 | Iustin Pop | import random |
46 | 18682bca | Iustin Pop | import logging |
47 | 3b9e6a30 | Iustin Pop | import tempfile |
48 | 12bce260 | Michael Hanselmann | import zlib |
49 | 12bce260 | Michael Hanselmann | import base64 |
50 | f81c4737 | Michael Hanselmann | import signal |
51 | a8083063 | Iustin Pop | |
52 | a8083063 | Iustin Pop | from ganeti import errors |
53 | a8083063 | Iustin Pop | from ganeti import utils |
54 | a8083063 | Iustin Pop | from ganeti import ssh |
55 | a8083063 | Iustin Pop | from ganeti import hypervisor |
56 | a8083063 | Iustin Pop | from ganeti import constants |
57 | a8083063 | Iustin Pop | from ganeti import bdev |
58 | a8083063 | Iustin Pop | from ganeti import objects |
59 | 880478f8 | Iustin Pop | from ganeti import ssconf |
60 | 1651d116 | Michael Hanselmann | from ganeti import serializer |
61 | a744b676 | Manuel Franceschini | from ganeti import netutils |
62 | 82b22e19 | René Nussbaumer | from ganeti import runtime |
63 | a8083063 | Iustin Pop | |
64 | a8083063 | Iustin Pop | |
65 | 13998ef2 | Michael Hanselmann | _BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
|
66 | 714ea7ca | Iustin Pop | _ALLOWED_CLEAN_DIRS = frozenset([
|
67 | 714ea7ca | Iustin Pop | constants.DATA_DIR, |
68 | 714ea7ca | Iustin Pop | constants.JOB_QUEUE_ARCHIVE_DIR, |
69 | 714ea7ca | Iustin Pop | constants.QUEUE_DIR, |
70 | f942a838 | Michael Hanselmann | constants.CRYPTO_KEYS_DIR, |
71 | 714ea7ca | Iustin Pop | ]) |
72 | f942a838 | Michael Hanselmann | _MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60 |
73 | f942a838 | Michael Hanselmann | _X509_KEY_FILE = "key"
|
74 | f942a838 | Michael Hanselmann | _X509_CERT_FILE = "cert"
|
75 | 1651d116 | Michael Hanselmann | _IES_STATUS_FILE = "status"
|
76 | 1651d116 | Michael Hanselmann | _IES_PID_FILE = "pid"
|
77 | 1651d116 | Michael Hanselmann | _IES_CA_FILE = "ca"
|
78 | 13998ef2 | Michael Hanselmann | |
79 | 13998ef2 | Michael Hanselmann | |
80 | 2cc6781a | Iustin Pop | class RPCFail(Exception): |
81 | 2cc6781a | Iustin Pop | """Class denoting RPC failure.
|
82 | 2cc6781a | Iustin Pop |
|
83 | 2cc6781a | Iustin Pop | Its argument is the error message.
|
84 | 2cc6781a | Iustin Pop |
|
85 | 2cc6781a | Iustin Pop | """
|
86 | 2cc6781a | Iustin Pop | |
87 | 13998ef2 | Michael Hanselmann | |
88 | 2cc6781a | Iustin Pop | def _Fail(msg, *args, **kwargs): |
89 | 2cc6781a | Iustin Pop | """Log an error and the raise an RPCFail exception.
|
90 | 2cc6781a | Iustin Pop |
|
91 | 2cc6781a | Iustin Pop | This exception is then handled specially in the ganeti daemon and
|
92 | 2cc6781a | Iustin Pop | turned into a 'failed' return type. As such, this function is a
|
93 | 2cc6781a | Iustin Pop | useful shortcut for logging the error and returning it to the master
|
94 | 2cc6781a | Iustin Pop | daemon.
|
95 | 2cc6781a | Iustin Pop |
|
96 | 2cc6781a | Iustin Pop | @type msg: string
|
97 | 2cc6781a | Iustin Pop | @param msg: the text of the exception
|
98 | 2cc6781a | Iustin Pop | @raise RPCFail
|
99 | 2cc6781a | Iustin Pop |
|
100 | 2cc6781a | Iustin Pop | """
|
101 | 2cc6781a | Iustin Pop | if args:
|
102 | 2cc6781a | Iustin Pop | msg = msg % args |
103 | afdc3985 | Iustin Pop | if "log" not in kwargs or kwargs["log"]: # if we should log this error |
104 | afdc3985 | Iustin Pop | if "exc" in kwargs and kwargs["exc"]: |
105 | afdc3985 | Iustin Pop | logging.exception(msg) |
106 | afdc3985 | Iustin Pop | else:
|
107 | afdc3985 | Iustin Pop | logging.error(msg) |
108 | 2cc6781a | Iustin Pop | raise RPCFail(msg)
|
109 | 2cc6781a | Iustin Pop | |
110 | 2cc6781a | Iustin Pop | |
111 | c657dcc9 | Michael Hanselmann | def _GetConfig(): |
112 | 93384844 | Iustin Pop | """Simple wrapper to return a SimpleStore.
|
113 | 10c2650b | Iustin Pop |
|
114 | 93384844 | Iustin Pop | @rtype: L{ssconf.SimpleStore}
|
115 | 93384844 | Iustin Pop | @return: a SimpleStore instance
|
116 | 10c2650b | Iustin Pop |
|
117 | 10c2650b | Iustin Pop | """
|
118 | 93384844 | Iustin Pop | return ssconf.SimpleStore()
|
119 | c657dcc9 | Michael Hanselmann | |
120 | c657dcc9 | Michael Hanselmann | |
121 | 62c9ec92 | Iustin Pop | def _GetSshRunner(cluster_name): |
122 | 10c2650b | Iustin Pop | """Simple wrapper to return an SshRunner.
|
123 | 10c2650b | Iustin Pop |
|
124 | 10c2650b | Iustin Pop | @type cluster_name: str
|
125 | 10c2650b | Iustin Pop | @param cluster_name: the cluster name, which is needed
|
126 | 10c2650b | Iustin Pop | by the SshRunner constructor
|
127 | 10c2650b | Iustin Pop | @rtype: L{ssh.SshRunner}
|
128 | 10c2650b | Iustin Pop | @return: an SshRunner instance
|
129 | 10c2650b | Iustin Pop |
|
130 | 10c2650b | Iustin Pop | """
|
131 | 62c9ec92 | Iustin Pop | return ssh.SshRunner(cluster_name)
|
132 | c92b310a | Michael Hanselmann | |
133 | c92b310a | Michael Hanselmann | |
134 | 12bce260 | Michael Hanselmann | def _Decompress(data): |
135 | 12bce260 | Michael Hanselmann | """Unpacks data compressed by the RPC client.
|
136 | 12bce260 | Michael Hanselmann |
|
137 | 12bce260 | Michael Hanselmann | @type data: list or tuple
|
138 | 12bce260 | Michael Hanselmann | @param data: Data sent by RPC client
|
139 | 12bce260 | Michael Hanselmann | @rtype: str
|
140 | 12bce260 | Michael Hanselmann | @return: Decompressed data
|
141 | 12bce260 | Michael Hanselmann |
|
142 | 12bce260 | Michael Hanselmann | """
|
143 | 52e2f66e | Michael Hanselmann | assert isinstance(data, (list, tuple)) |
144 | 12bce260 | Michael Hanselmann | assert len(data) == 2 |
145 | 12bce260 | Michael Hanselmann | (encoding, content) = data |
146 | 12bce260 | Michael Hanselmann | if encoding == constants.RPC_ENCODING_NONE:
|
147 | 12bce260 | Michael Hanselmann | return content
|
148 | 12bce260 | Michael Hanselmann | elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
|
149 | 12bce260 | Michael Hanselmann | return zlib.decompress(base64.b64decode(content))
|
150 | 12bce260 | Michael Hanselmann | else:
|
151 | 12bce260 | Michael Hanselmann | raise AssertionError("Unknown data encoding") |
152 | 12bce260 | Michael Hanselmann | |
153 | 12bce260 | Michael Hanselmann | |
154 | 3bc6be5c | Iustin Pop | def _CleanDirectory(path, exclude=None): |
155 | 76ab5558 | Michael Hanselmann | """Removes all regular files in a directory.
|
156 | 76ab5558 | Michael Hanselmann |
|
157 | 10c2650b | Iustin Pop | @type path: str
|
158 | 10c2650b | Iustin Pop | @param path: the directory to clean
|
159 | 76ab5558 | Michael Hanselmann | @type exclude: list
|
160 | 10c2650b | Iustin Pop | @param exclude: list of files to be excluded, defaults
|
161 | 10c2650b | Iustin Pop | to the empty list
|
162 | 76ab5558 | Michael Hanselmann |
|
163 | 76ab5558 | Michael Hanselmann | """
|
164 | 714ea7ca | Iustin Pop | if path not in _ALLOWED_CLEAN_DIRS: |
165 | 714ea7ca | Iustin Pop | _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
|
166 | 714ea7ca | Iustin Pop | path) |
167 | 714ea7ca | Iustin Pop | |
168 | 3956cee1 | Michael Hanselmann | if not os.path.isdir(path): |
169 | 3956cee1 | Michael Hanselmann | return
|
170 | 3bc6be5c | Iustin Pop | if exclude is None: |
171 | 3bc6be5c | Iustin Pop | exclude = [] |
172 | 3bc6be5c | Iustin Pop | else:
|
173 | 3bc6be5c | Iustin Pop | # Normalize excluded paths
|
174 | 3bc6be5c | Iustin Pop | exclude = [os.path.normpath(i) for i in exclude] |
175 | 76ab5558 | Michael Hanselmann | |
176 | 3956cee1 | Michael Hanselmann | for rel_name in utils.ListVisibleFiles(path): |
177 | c4feafe8 | Iustin Pop | full_name = utils.PathJoin(path, rel_name) |
178 | 76ab5558 | Michael Hanselmann | if full_name in exclude: |
179 | 76ab5558 | Michael Hanselmann | continue
|
180 | 3956cee1 | Michael Hanselmann | if os.path.isfile(full_name) and not os.path.islink(full_name): |
181 | 3956cee1 | Michael Hanselmann | utils.RemoveFile(full_name) |
182 | 3956cee1 | Michael Hanselmann | |
183 | 3956cee1 | Michael Hanselmann | |
184 | 360b0dc2 | Iustin Pop | def _BuildUploadFileList(): |
185 | 360b0dc2 | Iustin Pop | """Build the list of allowed upload files.
|
186 | 360b0dc2 | Iustin Pop |
|
187 | 360b0dc2 | Iustin Pop | This is abstracted so that it's built only once at module import time.
|
188 | 360b0dc2 | Iustin Pop |
|
189 | 360b0dc2 | Iustin Pop | """
|
190 | b397a7d2 | Iustin Pop | allowed_files = set([
|
191 | b397a7d2 | Iustin Pop | constants.CLUSTER_CONF_FILE, |
192 | b397a7d2 | Iustin Pop | constants.ETC_HOSTS, |
193 | b397a7d2 | Iustin Pop | constants.SSH_KNOWN_HOSTS_FILE, |
194 | b397a7d2 | Iustin Pop | constants.VNC_PASSWORD_FILE, |
195 | b397a7d2 | Iustin Pop | constants.RAPI_CERT_FILE, |
196 | b397a7d2 | Iustin Pop | constants.RAPI_USERS_FILE, |
197 | 6b7d5878 | Michael Hanselmann | constants.CONFD_HMAC_KEY, |
198 | ff89a747 | Michael Hanselmann | constants.CLUSTER_DOMAIN_SECRET_FILE, |
199 | b397a7d2 | Iustin Pop | ]) |
200 | b397a7d2 | Iustin Pop | |
201 | b397a7d2 | Iustin Pop | for hv_name in constants.HYPER_TYPES: |
202 | e5a45a16 | Iustin Pop | hv_class = hypervisor.GetHypervisorClass(hv_name) |
203 | b397a7d2 | Iustin Pop | allowed_files.update(hv_class.GetAncillaryFiles()) |
204 | b397a7d2 | Iustin Pop | |
205 | b397a7d2 | Iustin Pop | return frozenset(allowed_files) |
206 | 360b0dc2 | Iustin Pop | |
207 | 360b0dc2 | Iustin Pop | |
208 | 360b0dc2 | Iustin Pop | _ALLOWED_UPLOAD_FILES = _BuildUploadFileList() |
209 | 360b0dc2 | Iustin Pop | |
210 | 360b0dc2 | Iustin Pop | |
211 | 1bc59f76 | Michael Hanselmann | def JobQueuePurge(): |
212 | 10c2650b | Iustin Pop | """Removes job queue files and archived jobs.
|
213 | 10c2650b | Iustin Pop |
|
214 | c8457ce7 | Iustin Pop | @rtype: tuple
|
215 | c8457ce7 | Iustin Pop | @return: True, None
|
216 | 24fc781f | Michael Hanselmann |
|
217 | 24fc781f | Michael Hanselmann | """
|
218 | 1bc59f76 | Michael Hanselmann | _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE]) |
219 | 24fc781f | Michael Hanselmann | _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR) |
220 | 24fc781f | Michael Hanselmann | |
221 | 24fc781f | Michael Hanselmann | |
222 | bd1e4562 | Iustin Pop | def GetMasterInfo(): |
223 | bd1e4562 | Iustin Pop | """Returns master information.
|
224 | bd1e4562 | Iustin Pop |
|
225 | bd1e4562 | Iustin Pop | This is an utility function to compute master information, either
|
226 | bd1e4562 | Iustin Pop | for consumption here or from the node daemon.
|
227 | bd1e4562 | Iustin Pop |
|
228 | bd1e4562 | Iustin Pop | @rtype: tuple
|
229 | d8e0caa6 | Manuel Franceschini | @return: master_netdev, master_ip, master_name, primary_ip_family
|
230 | 2a52a064 | Iustin Pop | @raise RPCFail: in case of errors
|
231 | b1b6ea87 | Iustin Pop |
|
232 | b1b6ea87 | Iustin Pop | """
|
233 | b1b6ea87 | Iustin Pop | try:
|
234 | c657dcc9 | Michael Hanselmann | cfg = _GetConfig() |
235 | c657dcc9 | Michael Hanselmann | master_netdev = cfg.GetMasterNetdev() |
236 | c657dcc9 | Michael Hanselmann | master_ip = cfg.GetMasterIP() |
237 | c657dcc9 | Michael Hanselmann | master_node = cfg.GetMasterNode() |
238 | d8e0caa6 | Manuel Franceschini | primary_ip_family = cfg.GetPrimaryIPFamily() |
239 | b1b6ea87 | Iustin Pop | except errors.ConfigurationError, err:
|
240 | 29921401 | Iustin Pop | _Fail("Cluster configuration incomplete: %s", err, exc=True) |
241 | d8e0caa6 | Manuel Franceschini | return (master_netdev, master_ip, master_node, primary_ip_family)
|
242 | b1b6ea87 | Iustin Pop | |
243 | b1b6ea87 | Iustin Pop | |
244 | 3583908a | Guido Trotter | def StartMaster(start_daemons, no_voting): |
245 | a8083063 | Iustin Pop | """Activate local node as master node.
|
246 | a8083063 | Iustin Pop |
|
247 | 91492e57 | Iustin Pop | The function will either try activate the IP address of the master
|
248 | 91492e57 | Iustin Pop | (unless someone else has it) or also start the master daemons, based
|
249 | 91492e57 | Iustin Pop | on the start_daemons parameter.
|
250 | 10c2650b | Iustin Pop |
|
251 | 10c2650b | Iustin Pop | @type start_daemons: boolean
|
252 | 91492e57 | Iustin Pop | @param start_daemons: whether to start the master daemons
|
253 | 91492e57 | Iustin Pop | (ganeti-masterd and ganeti-rapi), or (if false) activate the
|
254 | 91492e57 | Iustin Pop | master ip
|
255 | 3583908a | Guido Trotter | @type no_voting: boolean
|
256 | 3583908a | Guido Trotter | @param no_voting: whether to start ganeti-masterd without a node vote
|
257 | 3583908a | Guido Trotter | (if start_daemons is True), but still non-interactively
|
258 | 10c2650b | Iustin Pop | @rtype: None
|
259 | a8083063 | Iustin Pop |
|
260 | a8083063 | Iustin Pop | """
|
261 | 2a52a064 | Iustin Pop | # GetMasterInfo will raise an exception if not able to return data
|
262 | d8e0caa6 | Manuel Franceschini | master_netdev, master_ip, _, family = GetMasterInfo() |
263 | a8083063 | Iustin Pop | |
264 | 396b5733 | Iustin Pop | err_msgs = [] |
265 | 91492e57 | Iustin Pop | # either start the master and rapi daemons
|
266 | b1b6ea87 | Iustin Pop | if start_daemons:
|
267 | 3583908a | Guido Trotter | if no_voting:
|
268 | f154a7a3 | Michael Hanselmann | masterd_args = "--no-voting --yes-do-it"
|
269 | f154a7a3 | Michael Hanselmann | else:
|
270 | f154a7a3 | Michael Hanselmann | masterd_args = ""
|
271 | f154a7a3 | Michael Hanselmann | |
272 | f154a7a3 | Michael Hanselmann | env = { |
273 | f154a7a3 | Michael Hanselmann | "EXTRA_MASTERD_ARGS": masterd_args,
|
274 | f154a7a3 | Michael Hanselmann | } |
275 | f154a7a3 | Michael Hanselmann | |
276 | f154a7a3 | Michael Hanselmann | result = utils.RunCmd([constants.DAEMON_UTIL, "start-master"], env=env)
|
277 | f154a7a3 | Michael Hanselmann | if result.failed:
|
278 | f154a7a3 | Michael Hanselmann | msg = "Can't start Ganeti master: %s" % result.output
|
279 | f154a7a3 | Michael Hanselmann | logging.error(msg) |
280 | f154a7a3 | Michael Hanselmann | err_msgs.append(msg) |
281 | 91492e57 | Iustin Pop | # or activate the IP
|
282 | 91492e57 | Iustin Pop | else:
|
283 | 91492e57 | Iustin Pop | if netutils.TcpPing(master_ip, constants.DEFAULT_NODED_PORT):
|
284 | 8b312c1d | Manuel Franceschini | if netutils.IPAddress.Own(master_ip):
|
285 | 91492e57 | Iustin Pop | # we already have the ip:
|
286 | 91492e57 | Iustin Pop | logging.debug("Master IP already configured, doing nothing")
|
287 | 91492e57 | Iustin Pop | else:
|
288 | 91492e57 | Iustin Pop | msg = "Someone else has the master ip, not activating"
|
289 | 91492e57 | Iustin Pop | logging.error(msg) |
290 | 91492e57 | Iustin Pop | err_msgs.append(msg) |
291 | 91492e57 | Iustin Pop | else:
|
292 | d8e0caa6 | Manuel Franceschini | ipcls = netutils.IP4Address |
293 | d8e0caa6 | Manuel Franceschini | if family == netutils.IP6Address.family:
|
294 | d8e0caa6 | Manuel Franceschini | ipcls = netutils.IP6Address |
295 | e7323b5e | Manuel Franceschini | |
296 | e7323b5e | Manuel Franceschini | result = utils.RunCmd(["ip", "address", "add", |
297 | d8e0caa6 | Manuel Franceschini | "%s/%d" % (master_ip, ipcls.iplen),
|
298 | 91492e57 | Iustin Pop | "dev", master_netdev, "label", |
299 | 91492e57 | Iustin Pop | "%s:0" % master_netdev])
|
300 | 91492e57 | Iustin Pop | if result.failed:
|
301 | 91492e57 | Iustin Pop | msg = "Can't activate master IP: %s" % result.output
|
302 | 91492e57 | Iustin Pop | logging.error(msg) |
303 | 91492e57 | Iustin Pop | err_msgs.append(msg) |
304 | 91492e57 | Iustin Pop | |
305 | e7323b5e | Manuel Franceschini | # we ignore the exit code of the following cmds
|
306 | d8e0caa6 | Manuel Franceschini | if ipcls == netutils.IP4Address:
|
307 | e7323b5e | Manuel Franceschini | utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev, "-s", |
308 | e7323b5e | Manuel Franceschini | master_ip, master_ip]) |
309 | d8e0caa6 | Manuel Franceschini | elif ipcls == netutils.IP6Address:
|
310 | 2dc1237c | Manuel Franceschini | try:
|
311 | 2dc1237c | Manuel Franceschini | utils.RunCmd(["ndisc6", "-q", "-r 3", master_ip, master_netdev]) |
312 | 2dc1237c | Manuel Franceschini | except errors.OpExecError:
|
313 | 2dc1237c | Manuel Franceschini | # TODO: Better error reporting
|
314 | 2dc1237c | Manuel Franceschini | logging.warning("Can't execute ndisc6, please install if missing")
|
315 | b726aff0 | Iustin Pop | |
316 | 396b5733 | Iustin Pop | if err_msgs:
|
317 | 396b5733 | Iustin Pop | _Fail("; ".join(err_msgs))
|
318 | afdc3985 | Iustin Pop | |
319 | a8083063 | Iustin Pop | |
320 | 1c65840b | Iustin Pop | def StopMaster(stop_daemons): |
321 | a8083063 | Iustin Pop | """Deactivate this node as master.
|
322 | a8083063 | Iustin Pop |
|
323 | 1c65840b | Iustin Pop | The function will always try to deactivate the IP address of the
|
324 | 10c2650b | Iustin Pop | master. It will also stop the master daemons depending on the
|
325 | 10c2650b | Iustin Pop | stop_daemons parameter.
|
326 | 10c2650b | Iustin Pop |
|
327 | 10c2650b | Iustin Pop | @type stop_daemons: boolean
|
328 | 10c2650b | Iustin Pop | @param stop_daemons: whether to also stop the master daemons
|
329 | 10c2650b | Iustin Pop | (ganeti-masterd and ganeti-rapi)
|
330 | 10c2650b | Iustin Pop | @rtype: None
|
331 | a8083063 | Iustin Pop |
|
332 | a8083063 | Iustin Pop | """
|
333 | 6c00d19a | Iustin Pop | # TODO: log and report back to the caller the error failures; we
|
334 | 6c00d19a | Iustin Pop | # need to decide in which case we fail the RPC for this
|
335 | 2a52a064 | Iustin Pop | |
336 | 2a52a064 | Iustin Pop | # GetMasterInfo will raise an exception if not able to return data
|
337 | d8e0caa6 | Manuel Franceschini | master_netdev, master_ip, _, family = GetMasterInfo() |
338 | a8083063 | Iustin Pop | |
339 | d8e0caa6 | Manuel Franceschini | ipcls = netutils.IP4Address |
340 | d8e0caa6 | Manuel Franceschini | if family == netutils.IP6Address.family:
|
341 | d8e0caa6 | Manuel Franceschini | ipcls = netutils.IP6Address |
342 | e7323b5e | Manuel Franceschini | |
343 | e7323b5e | Manuel Franceschini | result = utils.RunCmd(["ip", "address", "del", |
344 | d8e0caa6 | Manuel Franceschini | "%s/%d" % (master_ip, ipcls.iplen),
|
345 | b1b6ea87 | Iustin Pop | "dev", master_netdev])
|
346 | a8083063 | Iustin Pop | if result.failed:
|
347 | 3b9e6a30 | Iustin Pop | logging.error("Can't remove the master IP, error: %s", result.output)
|
348 | b1b6ea87 | Iustin Pop | # but otherwise ignore the failure
|
349 | b1b6ea87 | Iustin Pop | |
350 | b1b6ea87 | Iustin Pop | if stop_daemons:
|
351 | f154a7a3 | Michael Hanselmann | result = utils.RunCmd([constants.DAEMON_UTIL, "stop-master"])
|
352 | f154a7a3 | Michael Hanselmann | if result.failed:
|
353 | f154a7a3 | Michael Hanselmann | logging.error("Could not stop Ganeti master, command %s had exitcode %s"
|
354 | f154a7a3 | Michael Hanselmann | " and error %s",
|
355 | f154a7a3 | Michael Hanselmann | result.cmd, result.exit_code, result.output) |
356 | a8083063 | Iustin Pop | |
357 | a8083063 | Iustin Pop | |
358 | 19ddc57a | René Nussbaumer | def EtcHostsModify(mode, host, ip): |
359 | 19ddc57a | René Nussbaumer | """Modify a host entry in /etc/hosts.
|
360 | 19ddc57a | René Nussbaumer |
|
361 | 19ddc57a | René Nussbaumer | @param mode: The mode to operate. Either add or remove entry
|
362 | 19ddc57a | René Nussbaumer | @param host: The host to operate on
|
363 | 19ddc57a | René Nussbaumer | @param ip: The ip associated with the entry
|
364 | 19ddc57a | René Nussbaumer |
|
365 | 19ddc57a | René Nussbaumer | """
|
366 | 19ddc57a | René Nussbaumer | if mode == constants.ETC_HOSTS_ADD:
|
367 | 19ddc57a | René Nussbaumer | if not ip: |
368 | 19ddc57a | René Nussbaumer | RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
|
369 | 19ddc57a | René Nussbaumer | " present")
|
370 | 19ddc57a | René Nussbaumer | utils.AddHostToEtcHosts(host, ip) |
371 | 19ddc57a | René Nussbaumer | elif mode == constants.ETC_HOSTS_REMOVE:
|
372 | 19ddc57a | René Nussbaumer | if ip:
|
373 | 19ddc57a | René Nussbaumer | RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
|
374 | 19ddc57a | René Nussbaumer | " parameter is present")
|
375 | 19ddc57a | René Nussbaumer | utils.RemoveHostFromEtcHosts(host) |
376 | 19ddc57a | René Nussbaumer | else:
|
377 | 19ddc57a | René Nussbaumer | RPCFail("Mode not supported")
|
378 | 19ddc57a | René Nussbaumer | |
379 | 19ddc57a | René Nussbaumer | |
380 | b989b9d9 | Ken Wehr | def LeaveCluster(modify_ssh_setup): |
381 | 10c2650b | Iustin Pop | """Cleans up and remove the current node.
|
382 | 10c2650b | Iustin Pop |
|
383 | 10c2650b | Iustin Pop | This function cleans up and prepares the current node to be removed
|
384 | 10c2650b | Iustin Pop | from the cluster.
|
385 | 10c2650b | Iustin Pop |
|
386 | 10c2650b | Iustin Pop | If processing is successful, then it raises an
|
387 | c41eea6e | Iustin Pop | L{errors.QuitGanetiException} which is used as a special case to
|
388 | 10c2650b | Iustin Pop | shutdown the node daemon.
|
389 | a8083063 | Iustin Pop |
|
390 | b989b9d9 | Ken Wehr | @param modify_ssh_setup: boolean
|
391 | b989b9d9 | Ken Wehr |
|
392 | a8083063 | Iustin Pop | """
|
393 | f78346f5 | Michael Hanselmann | _CleanDirectory(constants.DATA_DIR) |
394 | f942a838 | Michael Hanselmann | _CleanDirectory(constants.CRYPTO_KEYS_DIR) |
395 | 1bc59f76 | Michael Hanselmann | JobQueuePurge() |
396 | f78346f5 | Michael Hanselmann | |
397 | b989b9d9 | Ken Wehr | if modify_ssh_setup:
|
398 | b989b9d9 | Ken Wehr | try:
|
399 | b989b9d9 | Ken Wehr | priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS) |
400 | 7900ed01 | Iustin Pop | |
401 | b989b9d9 | Ken Wehr | utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key)) |
402 | a8083063 | Iustin Pop | |
403 | b989b9d9 | Ken Wehr | utils.RemoveFile(priv_key) |
404 | b989b9d9 | Ken Wehr | utils.RemoveFile(pub_key) |
405 | b989b9d9 | Ken Wehr | except errors.OpExecError:
|
406 | b989b9d9 | Ken Wehr | logging.exception("Error while processing ssh files")
|
407 | a8083063 | Iustin Pop | |
408 | ed008420 | Guido Trotter | try:
|
409 | 6b7d5878 | Michael Hanselmann | utils.RemoveFile(constants.CONFD_HMAC_KEY) |
410 | ed008420 | Guido Trotter | utils.RemoveFile(constants.RAPI_CERT_FILE) |
411 | 168c1de2 | Michael Hanselmann | utils.RemoveFile(constants.NODED_CERT_FILE) |
412 | 7260cfbe | Iustin Pop | except: # pylint: disable-msg=W0702 |
413 | ed008420 | Guido Trotter | logging.exception("Error while removing cluster secrets")
|
414 | ed008420 | Guido Trotter | |
415 | f154a7a3 | Michael Hanselmann | result = utils.RunCmd([constants.DAEMON_UTIL, "stop", constants.CONFD])
|
416 | f154a7a3 | Michael Hanselmann | if result.failed:
|
417 | f154a7a3 | Michael Hanselmann | logging.error("Command %s failed with exitcode %s and error %s",
|
418 | f154a7a3 | Michael Hanselmann | result.cmd, result.exit_code, result.output) |
419 | ed008420 | Guido Trotter | |
420 | 0623d351 | Iustin Pop | # Raise a custom exception (handled in ganeti-noded)
|
421 | 0623d351 | Iustin Pop | raise errors.QuitGanetiException(True, 'Shutdown scheduled') |
422 | 6d8b6238 | Guido Trotter | |
423 | a8083063 | Iustin Pop | |
424 | e69d05fd | Iustin Pop | def GetNodeInfo(vgname, hypervisor_type): |
425 | 5bbd3f7f | Michael Hanselmann | """Gives back a hash with different information about the node.
|
426 | a8083063 | Iustin Pop |
|
427 | e69d05fd | Iustin Pop | @type vgname: C{string}
|
428 | e69d05fd | Iustin Pop | @param vgname: the name of the volume group to ask for disk space information
|
429 | e69d05fd | Iustin Pop | @type hypervisor_type: C{str}
|
430 | e69d05fd | Iustin Pop | @param hypervisor_type: the name of the hypervisor to ask for
|
431 | e69d05fd | Iustin Pop | memory information
|
432 | e69d05fd | Iustin Pop | @rtype: C{dict}
|
433 | e69d05fd | Iustin Pop | @return: dictionary with the following keys:
|
434 | e69d05fd | Iustin Pop | - vg_size is the size of the configured volume group in MiB
|
435 | e69d05fd | Iustin Pop | - vg_free is the free size of the volume group in MiB
|
436 | e69d05fd | Iustin Pop | - memory_dom0 is the memory allocated for domain0 in MiB
|
437 | e69d05fd | Iustin Pop | - memory_free is the currently available (free) ram in MiB
|
438 | e69d05fd | Iustin Pop | - memory_total is the total number of ram in MiB
|
439 | a8083063 | Iustin Pop |
|
440 | 098c0958 | Michael Hanselmann | """
|
441 | a8083063 | Iustin Pop | outputarray = {} |
442 | 673cd9c4 | René Nussbaumer | |
443 | 673cd9c4 | René Nussbaumer | vginfo = bdev.LogicalVolume.GetVGInfo([vgname]) |
444 | 673cd9c4 | René Nussbaumer | vg_free = vg_size = None
|
445 | 673cd9c4 | René Nussbaumer | if vginfo:
|
446 | 673cd9c4 | René Nussbaumer | vg_free = int(round(vginfo[0][0], 0)) |
447 | 673cd9c4 | René Nussbaumer | vg_size = int(round(vginfo[0][1], 0)) |
448 | 673cd9c4 | René Nussbaumer | |
449 | 673cd9c4 | René Nussbaumer | outputarray['vg_size'] = vg_size
|
450 | 673cd9c4 | René Nussbaumer | outputarray['vg_free'] = vg_free
|
451 | a8083063 | Iustin Pop | |
452 | e69d05fd | Iustin Pop | hyper = hypervisor.GetHypervisor(hypervisor_type) |
453 | a8083063 | Iustin Pop | hyp_info = hyper.GetNodeInfo() |
454 | a8083063 | Iustin Pop | if hyp_info is not None: |
455 | a8083063 | Iustin Pop | outputarray.update(hyp_info) |
456 | a8083063 | Iustin Pop | |
457 | 13998ef2 | Michael Hanselmann | outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n") |
458 | 3ef10550 | Michael Hanselmann | |
459 | c26a6bd2 | Iustin Pop | return outputarray
|
460 | a8083063 | Iustin Pop | |
461 | a8083063 | Iustin Pop | |
462 | 62c9ec92 | Iustin Pop | def VerifyNode(what, cluster_name): |
463 | a8083063 | Iustin Pop | """Verify the status of the local node.
|
464 | a8083063 | Iustin Pop |
|
465 | e69d05fd | Iustin Pop | Based on the input L{what} parameter, various checks are done on the
|
466 | e69d05fd | Iustin Pop | local node.
|
467 | e69d05fd | Iustin Pop |
|
468 | e69d05fd | Iustin Pop | If the I{filelist} key is present, this list of
|
469 | e69d05fd | Iustin Pop | files is checksummed and the file/checksum pairs are returned.
|
470 | e69d05fd | Iustin Pop |
|
471 | e69d05fd | Iustin Pop | If the I{nodelist} key is present, we check that we have
|
472 | e69d05fd | Iustin Pop | connectivity via ssh with the target nodes (and check the hostname
|
473 | e69d05fd | Iustin Pop | report).
|
474 | a8083063 | Iustin Pop |
|
475 | e69d05fd | Iustin Pop | If the I{node-net-test} key is present, we check that we have
|
476 | e69d05fd | Iustin Pop | connectivity to the given nodes via both primary IP and, if
|
477 | e69d05fd | Iustin Pop | applicable, secondary IPs.
|
478 | e69d05fd | Iustin Pop |
|
479 | e69d05fd | Iustin Pop | @type what: C{dict}
|
480 | e69d05fd | Iustin Pop | @param what: a dictionary of things to check:
|
481 | e69d05fd | Iustin Pop | - filelist: list of files for which to compute checksums
|
482 | e69d05fd | Iustin Pop | - nodelist: list of nodes we should check ssh communication with
|
483 | e69d05fd | Iustin Pop | - node-net-test: list of nodes we should check node daemon port
|
484 | e69d05fd | Iustin Pop | connectivity with
|
485 | e69d05fd | Iustin Pop | - hypervisor: list with hypervisors to run the verify for
|
486 | 10c2650b | Iustin Pop | @rtype: dict
|
487 | 10c2650b | Iustin Pop | @return: a dictionary with the same keys as the input dict, and
|
488 | 10c2650b | Iustin Pop | values representing the result of the checks
|
489 | a8083063 | Iustin Pop |
|
490 | a8083063 | Iustin Pop | """
|
491 | a8083063 | Iustin Pop | result = {} |
492 | b705c7a6 | Manuel Franceschini | my_name = netutils.Hostname.GetSysName() |
493 | a744b676 | Manuel Franceschini | port = netutils.GetDaemonPort(constants.NODED) |
494 | a8083063 | Iustin Pop | |
495 | 25361b9a | Iustin Pop | if constants.NV_HYPERVISOR in what: |
496 | 25361b9a | Iustin Pop | result[constants.NV_HYPERVISOR] = tmp = {} |
497 | 25361b9a | Iustin Pop | for hv_name in what[constants.NV_HYPERVISOR]: |
498 | 0cf5e7f5 | Iustin Pop | try:
|
499 | 0cf5e7f5 | Iustin Pop | val = hypervisor.GetHypervisor(hv_name).Verify() |
500 | 0cf5e7f5 | Iustin Pop | except errors.HypervisorError, err:
|
501 | 0cf5e7f5 | Iustin Pop | val = "Error while checking hypervisor: %s" % str(err) |
502 | 0cf5e7f5 | Iustin Pop | tmp[hv_name] = val |
503 | 25361b9a | Iustin Pop | |
504 | 25361b9a | Iustin Pop | if constants.NV_FILELIST in what: |
505 | 25361b9a | Iustin Pop | result[constants.NV_FILELIST] = utils.FingerprintFiles( |
506 | 25361b9a | Iustin Pop | what[constants.NV_FILELIST]) |
507 | 25361b9a | Iustin Pop | |
508 | 25361b9a | Iustin Pop | if constants.NV_NODELIST in what: |
509 | 25361b9a | Iustin Pop | result[constants.NV_NODELIST] = tmp = {} |
510 | 25361b9a | Iustin Pop | random.shuffle(what[constants.NV_NODELIST]) |
511 | 25361b9a | Iustin Pop | for node in what[constants.NV_NODELIST]: |
512 | 62c9ec92 | Iustin Pop | success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node) |
513 | a8083063 | Iustin Pop | if not success: |
514 | 25361b9a | Iustin Pop | tmp[node] = message |
515 | 25361b9a | Iustin Pop | |
516 | 25361b9a | Iustin Pop | if constants.NV_NODENETTEST in what: |
517 | 25361b9a | Iustin Pop | result[constants.NV_NODENETTEST] = tmp = {} |
518 | 9d4bfc96 | Iustin Pop | my_pip = my_sip = None
|
519 | 25361b9a | Iustin Pop | for name, pip, sip in what[constants.NV_NODENETTEST]: |
520 | 9d4bfc96 | Iustin Pop | if name == my_name:
|
521 | 9d4bfc96 | Iustin Pop | my_pip = pip |
522 | 9d4bfc96 | Iustin Pop | my_sip = sip |
523 | 9d4bfc96 | Iustin Pop | break
|
524 | 9d4bfc96 | Iustin Pop | if not my_pip: |
525 | 25361b9a | Iustin Pop | tmp[my_name] = ("Can't find my own primary/secondary IP"
|
526 | 25361b9a | Iustin Pop | " in the node list")
|
527 | 9d4bfc96 | Iustin Pop | else:
|
528 | 25361b9a | Iustin Pop | for name, pip, sip in what[constants.NV_NODENETTEST]: |
529 | 9d4bfc96 | Iustin Pop | fail = [] |
530 | a744b676 | Manuel Franceschini | if not netutils.TcpPing(pip, port, source=my_pip): |
531 | 9d4bfc96 | Iustin Pop | fail.append("primary")
|
532 | 9d4bfc96 | Iustin Pop | if sip != pip:
|
533 | a744b676 | Manuel Franceschini | if not netutils.TcpPing(sip, port, source=my_sip): |
534 | 9d4bfc96 | Iustin Pop | fail.append("secondary")
|
535 | 9d4bfc96 | Iustin Pop | if fail:
|
536 | 25361b9a | Iustin Pop | tmp[name] = ("failure using the %s interface(s)" %
|
537 | 25361b9a | Iustin Pop | " and ".join(fail))
|
538 | 25361b9a | Iustin Pop | |
539 | a3a5f850 | Iustin Pop | if constants.NV_MASTERIP in what: |
540 | a3a5f850 | Iustin Pop | # FIXME: add checks on incoming data structures (here and in the
|
541 | a3a5f850 | Iustin Pop | # rest of the function)
|
542 | a3a5f850 | Iustin Pop | master_name, master_ip = what[constants.NV_MASTERIP] |
543 | a3a5f850 | Iustin Pop | if master_name == my_name:
|
544 | 9769bb78 | Manuel Franceschini | source = constants.IP4_ADDRESS_LOCALHOST |
545 | a3a5f850 | Iustin Pop | else:
|
546 | a3a5f850 | Iustin Pop | source = None
|
547 | a744b676 | Manuel Franceschini | result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port, |
548 | a3a5f850 | Iustin Pop | source=source) |
549 | a3a5f850 | Iustin Pop | |
550 | 25361b9a | Iustin Pop | if constants.NV_LVLIST in what: |
551 | ed904904 | Iustin Pop | try:
|
552 | ed904904 | Iustin Pop | val = GetVolumeList(what[constants.NV_LVLIST]) |
553 | ed904904 | Iustin Pop | except RPCFail, err:
|
554 | ed904904 | Iustin Pop | val = str(err)
|
555 | ed904904 | Iustin Pop | result[constants.NV_LVLIST] = val |
556 | 25361b9a | Iustin Pop | |
557 | 25361b9a | Iustin Pop | if constants.NV_INSTANCELIST in what: |
558 | 0cf5e7f5 | Iustin Pop | # GetInstanceList can fail
|
559 | 0cf5e7f5 | Iustin Pop | try:
|
560 | 0cf5e7f5 | Iustin Pop | val = GetInstanceList(what[constants.NV_INSTANCELIST]) |
561 | 0cf5e7f5 | Iustin Pop | except RPCFail, err:
|
562 | 0cf5e7f5 | Iustin Pop | val = str(err)
|
563 | 0cf5e7f5 | Iustin Pop | result[constants.NV_INSTANCELIST] = val |
564 | 25361b9a | Iustin Pop | |
565 | 25361b9a | Iustin Pop | if constants.NV_VGLIST in what: |
566 | e480923b | Iustin Pop | result[constants.NV_VGLIST] = utils.ListVolumeGroups() |
567 | 25361b9a | Iustin Pop | |
568 | d091393e | Iustin Pop | if constants.NV_PVLIST in what: |
569 | d091393e | Iustin Pop | result[constants.NV_PVLIST] = \ |
570 | d091393e | Iustin Pop | bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST], |
571 | d091393e | Iustin Pop | filter_allocatable=False)
|
572 | d091393e | Iustin Pop | |
573 | 25361b9a | Iustin Pop | if constants.NV_VERSION in what: |
574 | e9ce0a64 | Iustin Pop | result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION, |
575 | e9ce0a64 | Iustin Pop | constants.RELEASE_VERSION) |
576 | 25361b9a | Iustin Pop | |
577 | 25361b9a | Iustin Pop | if constants.NV_HVINFO in what: |
578 | 25361b9a | Iustin Pop | hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO]) |
579 | 25361b9a | Iustin Pop | result[constants.NV_HVINFO] = hyper.GetNodeInfo() |
580 | 9d4bfc96 | Iustin Pop | |
581 | 6d2e83d5 | Iustin Pop | if constants.NV_DRBDLIST in what: |
582 | 6d2e83d5 | Iustin Pop | try:
|
583 | 6d2e83d5 | Iustin Pop | used_minors = bdev.DRBD8.GetUsedDevs().keys() |
584 | f6eaed12 | Iustin Pop | except errors.BlockDeviceError, err:
|
585 | 6d2e83d5 | Iustin Pop | logging.warning("Can't get used minors list", exc_info=True) |
586 | f6eaed12 | Iustin Pop | used_minors = str(err)
|
587 | 6d2e83d5 | Iustin Pop | result[constants.NV_DRBDLIST] = used_minors |
588 | 6d2e83d5 | Iustin Pop | |
589 | 7ef40fbe | Luca Bigliardi | if constants.NV_DRBDHELPER in what: |
590 | 7ef40fbe | Luca Bigliardi | status = True
|
591 | 7ef40fbe | Luca Bigliardi | try:
|
592 | 7ef40fbe | Luca Bigliardi | payload = bdev.BaseDRBD.GetUsermodeHelper() |
593 | 7ef40fbe | Luca Bigliardi | except errors.BlockDeviceError, err:
|
594 | 7ef40fbe | Luca Bigliardi | logging.error("Can't get DRBD usermode helper: %s", str(err)) |
595 | 7ef40fbe | Luca Bigliardi | status = False
|
596 | 7ef40fbe | Luca Bigliardi | payload = str(err)
|
597 | 7ef40fbe | Luca Bigliardi | result[constants.NV_DRBDHELPER] = (status, payload) |
598 | 7ef40fbe | Luca Bigliardi | |
599 | 7c0aa8e9 | Iustin Pop | if constants.NV_NODESETUP in what: |
600 | 7c0aa8e9 | Iustin Pop | result[constants.NV_NODESETUP] = tmpr = [] |
601 | 7c0aa8e9 | Iustin Pop | if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"): |
602 | 7c0aa8e9 | Iustin Pop | tmpr.append("The sysfs filesytem doesn't seem to be mounted"
|
603 | 7c0aa8e9 | Iustin Pop | " under /sys, missing required directories /sys/block"
|
604 | 7c0aa8e9 | Iustin Pop | " and /sys/class/net")
|
605 | 7c0aa8e9 | Iustin Pop | if (not os.path.isdir("/proc/sys") or |
606 | 7c0aa8e9 | Iustin Pop | not os.path.isfile("/proc/sysrq-trigger")): |
607 | 7c0aa8e9 | Iustin Pop | tmpr.append("The procfs filesystem doesn't seem to be mounted"
|
608 | 7c0aa8e9 | Iustin Pop | " under /proc, missing required directory /proc/sys and"
|
609 | 7c0aa8e9 | Iustin Pop | " the file /proc/sysrq-trigger")
|
610 | 313b2dd4 | Michael Hanselmann | |
611 | 313b2dd4 | Michael Hanselmann | if constants.NV_TIME in what: |
612 | 313b2dd4 | Michael Hanselmann | result[constants.NV_TIME] = utils.SplitTime(time.time()) |
613 | 313b2dd4 | Michael Hanselmann | |
614 | b0d85178 | Iustin Pop | if constants.NV_OSLIST in what: |
615 | b0d85178 | Iustin Pop | result[constants.NV_OSLIST] = DiagnoseOS() |
616 | b0d85178 | Iustin Pop | |
617 | c26a6bd2 | Iustin Pop | return result
|
618 | a8083063 | Iustin Pop | |
619 | a8083063 | Iustin Pop | |
620 | a8083063 | Iustin Pop | def GetVolumeList(vg_name): |
621 | a8083063 | Iustin Pop | """Compute list of logical volumes and their size.
|
622 | a8083063 | Iustin Pop |
|
623 | 10c2650b | Iustin Pop | @type vg_name: str
|
624 | 10c2650b | Iustin Pop | @param vg_name: the volume group whose LVs we should list
|
625 | 10c2650b | Iustin Pop | @rtype: dict
|
626 | 10c2650b | Iustin Pop | @return:
|
627 | 10c2650b | Iustin Pop | dictionary of all partions (key) with value being a tuple of
|
628 | 10c2650b | Iustin Pop | their size (in MiB), inactive and online status::
|
629 | 10c2650b | Iustin Pop |
|
630 | 10c2650b | Iustin Pop | {'test1': ('20.06', True, True)}
|
631 | 10c2650b | Iustin Pop |
|
632 | 10c2650b | Iustin Pop | in case of errors, a string is returned with the error
|
633 | 10c2650b | Iustin Pop | details.
|
634 | a8083063 | Iustin Pop |
|
635 | a8083063 | Iustin Pop | """
|
636 | cb2037a2 | Iustin Pop | lvs = {} |
637 | cb2037a2 | Iustin Pop | sep = '|'
|
638 | cb2037a2 | Iustin Pop | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
639 | cb2037a2 | Iustin Pop | "--separator=%s" % sep,
|
640 | cb2037a2 | Iustin Pop | "-olv_name,lv_size,lv_attr", vg_name])
|
641 | a8083063 | Iustin Pop | if result.failed:
|
642 | 29d376ec | Iustin Pop | _Fail("Failed to list logical volumes, lvs output: %s", result.output)
|
643 | cb2037a2 | Iustin Pop | |
644 | df4c2628 | Iustin Pop | valid_line_re = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
|
645 | cb2037a2 | Iustin Pop | for line in result.stdout.splitlines(): |
646 | df4c2628 | Iustin Pop | line = line.strip() |
647 | df4c2628 | Iustin Pop | match = valid_line_re.match(line) |
648 | df4c2628 | Iustin Pop | if not match: |
649 | 18682bca | Iustin Pop | logging.error("Invalid line returned from lvs output: '%s'", line)
|
650 | df4c2628 | Iustin Pop | continue
|
651 | df4c2628 | Iustin Pop | name, size, attr = match.groups() |
652 | cb2037a2 | Iustin Pop | inactive = attr[4] == '-' |
653 | cb2037a2 | Iustin Pop | online = attr[5] == 'o' |
654 | 33f2a81a | Iustin Pop | virtual = attr[0] == 'v' |
655 | 33f2a81a | Iustin Pop | if virtual:
|
656 | 33f2a81a | Iustin Pop | # we don't want to report such volumes as existing, since they
|
657 | 33f2a81a | Iustin Pop | # don't really hold data
|
658 | 33f2a81a | Iustin Pop | continue
|
659 | cb2037a2 | Iustin Pop | lvs[name] = (size, inactive, online) |
660 | cb2037a2 | Iustin Pop | |
661 | cb2037a2 | Iustin Pop | return lvs
|
662 | a8083063 | Iustin Pop | |
663 | a8083063 | Iustin Pop | |
664 | a8083063 | Iustin Pop | def ListVolumeGroups(): |
665 | 2f8598a5 | Alexander Schreiber | """List the volume groups and their size.
|
666 | a8083063 | Iustin Pop |
|
667 | 10c2650b | Iustin Pop | @rtype: dict
|
668 | 10c2650b | Iustin Pop | @return: dictionary with keys volume name and values the
|
669 | 10c2650b | Iustin Pop | size of the volume
|
670 | a8083063 | Iustin Pop |
|
671 | a8083063 | Iustin Pop | """
|
672 | c26a6bd2 | Iustin Pop | return utils.ListVolumeGroups()
|
673 | a8083063 | Iustin Pop | |
674 | a8083063 | Iustin Pop | |
675 | dcb93971 | Michael Hanselmann | def NodeVolumes(): |
676 | dcb93971 | Michael Hanselmann | """List all volumes on this node.
|
677 | dcb93971 | Michael Hanselmann |
|
678 | 10c2650b | Iustin Pop | @rtype: list
|
679 | 10c2650b | Iustin Pop | @return:
|
680 | 10c2650b | Iustin Pop | A list of dictionaries, each having four keys:
|
681 | 10c2650b | Iustin Pop | - name: the logical volume name,
|
682 | 10c2650b | Iustin Pop | - size: the size of the logical volume
|
683 | 10c2650b | Iustin Pop | - dev: the physical device on which the LV lives
|
684 | 10c2650b | Iustin Pop | - vg: the volume group to which it belongs
|
685 | 10c2650b | Iustin Pop |
|
686 | 10c2650b | Iustin Pop | In case of errors, we return an empty list and log the
|
687 | 10c2650b | Iustin Pop | error.
|
688 | 10c2650b | Iustin Pop |
|
689 | 10c2650b | Iustin Pop | Note that since a logical volume can live on multiple physical
|
690 | 10c2650b | Iustin Pop | volumes, the resulting list might include a logical volume
|
691 | 10c2650b | Iustin Pop | multiple times.
|
692 | 10c2650b | Iustin Pop |
|
693 | dcb93971 | Michael Hanselmann | """
|
694 | dcb93971 | Michael Hanselmann | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
695 | dcb93971 | Michael Hanselmann | "--separator=|",
|
696 | dcb93971 | Michael Hanselmann | "--options=lv_name,lv_size,devices,vg_name"])
|
697 | dcb93971 | Michael Hanselmann | if result.failed:
|
698 | 10bfe6cb | Iustin Pop | _Fail("Failed to list logical volumes, lvs output: %s",
|
699 | 10bfe6cb | Iustin Pop | result.output) |
700 | dcb93971 | Michael Hanselmann | |
701 | dcb93971 | Michael Hanselmann | def parse_dev(dev): |
702 | 89e5ab02 | Iustin Pop | return dev.split('(')[0] |
703 | 89e5ab02 | Iustin Pop | |
704 | 89e5ab02 | Iustin Pop | def handle_dev(dev): |
705 | 89e5ab02 | Iustin Pop | return [parse_dev(x) for x in dev.split(",")] |
706 | dcb93971 | Michael Hanselmann | |
707 | dcb93971 | Michael Hanselmann | def map_line(line): |
708 | 89e5ab02 | Iustin Pop | line = [v.strip() for v in line] |
709 | 89e5ab02 | Iustin Pop | return [{'name': line[0], 'size': line[1], |
710 | 89e5ab02 | Iustin Pop | 'dev': dev, 'vg': line[3]} for dev in handle_dev(line[2])] |
711 | 89e5ab02 | Iustin Pop | |
712 | 89e5ab02 | Iustin Pop | all_devs = [] |
713 | 89e5ab02 | Iustin Pop | for line in result.stdout.splitlines(): |
714 | 89e5ab02 | Iustin Pop | if line.count('|') >= 3: |
715 | 89e5ab02 | Iustin Pop | all_devs.extend(map_line(line.split('|')))
|
716 | 89e5ab02 | Iustin Pop | else:
|
717 | 89e5ab02 | Iustin Pop | logging.warning("Strange line in the output from lvs: '%s'", line)
|
718 | 89e5ab02 | Iustin Pop | return all_devs
|
719 | dcb93971 | Michael Hanselmann | |
720 | dcb93971 | Michael Hanselmann | |
721 | a8083063 | Iustin Pop | def BridgesExist(bridges_list): |
722 | 2f8598a5 | Alexander Schreiber | """Check if a list of bridges exist on the current node.
|
723 | a8083063 | Iustin Pop |
|
724 | b1206984 | Iustin Pop | @rtype: boolean
|
725 | b1206984 | Iustin Pop | @return: C{True} if all of them exist, C{False} otherwise
|
726 | a8083063 | Iustin Pop |
|
727 | a8083063 | Iustin Pop | """
|
728 | 35c0c8da | Iustin Pop | missing = [] |
729 | a8083063 | Iustin Pop | for bridge in bridges_list: |
730 | a8083063 | Iustin Pop | if not utils.BridgeExists(bridge): |
731 | 35c0c8da | Iustin Pop | missing.append(bridge) |
732 | a8083063 | Iustin Pop | |
733 | 35c0c8da | Iustin Pop | if missing:
|
734 | 1f864b60 | Iustin Pop | _Fail("Missing bridges %s", utils.CommaJoin(missing))
|
735 | 35c0c8da | Iustin Pop | |
736 | a8083063 | Iustin Pop | |
737 | e69d05fd | Iustin Pop | def GetInstanceList(hypervisor_list): |
738 | 2f8598a5 | Alexander Schreiber | """Provides a list of instances.
|
739 | a8083063 | Iustin Pop |
|
740 | e69d05fd | Iustin Pop | @type hypervisor_list: list
|
741 | e69d05fd | Iustin Pop | @param hypervisor_list: the list of hypervisors to query information
|
742 | e69d05fd | Iustin Pop |
|
743 | e69d05fd | Iustin Pop | @rtype: list
|
744 | e69d05fd | Iustin Pop | @return: a list of all running instances on the current node
|
745 | 10c2650b | Iustin Pop | - instance1.example.com
|
746 | 10c2650b | Iustin Pop | - instance2.example.com
|
747 | a8083063 | Iustin Pop |
|
748 | 098c0958 | Michael Hanselmann | """
|
749 | e69d05fd | Iustin Pop | results = [] |
750 | e69d05fd | Iustin Pop | for hname in hypervisor_list: |
751 | e69d05fd | Iustin Pop | try:
|
752 | e69d05fd | Iustin Pop | names = hypervisor.GetHypervisor(hname).ListInstances() |
753 | e69d05fd | Iustin Pop | results.extend(names) |
754 | e69d05fd | Iustin Pop | except errors.HypervisorError, err:
|
755 | aca13712 | Iustin Pop | _Fail("Error enumerating instances (hypervisor %s): %s",
|
756 | aca13712 | Iustin Pop | hname, err, exc=True)
|
757 | a8083063 | Iustin Pop | |
758 | e69d05fd | Iustin Pop | return results
|
759 | a8083063 | Iustin Pop | |
760 | a8083063 | Iustin Pop | |
761 | e69d05fd | Iustin Pop | def GetInstanceInfo(instance, hname): |
762 | 5bbd3f7f | Michael Hanselmann | """Gives back the information about an instance as a dictionary.
|
763 | a8083063 | Iustin Pop |
|
764 | e69d05fd | Iustin Pop | @type instance: string
|
765 | e69d05fd | Iustin Pop | @param instance: the instance name
|
766 | e69d05fd | Iustin Pop | @type hname: string
|
767 | e69d05fd | Iustin Pop | @param hname: the hypervisor type of the instance
|
768 | a8083063 | Iustin Pop |
|
769 | e69d05fd | Iustin Pop | @rtype: dict
|
770 | e69d05fd | Iustin Pop | @return: dictionary with the following keys:
|
771 | e69d05fd | Iustin Pop | - memory: memory size of instance (int)
|
772 | e69d05fd | Iustin Pop | - state: xen state of instance (string)
|
773 | e69d05fd | Iustin Pop | - time: cpu time of instance (float)
|
774 | a8083063 | Iustin Pop |
|
775 | 098c0958 | Michael Hanselmann | """
|
776 | a8083063 | Iustin Pop | output = {} |
777 | a8083063 | Iustin Pop | |
778 | e69d05fd | Iustin Pop | iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance) |
779 | a8083063 | Iustin Pop | if iinfo is not None: |
780 | a8083063 | Iustin Pop | output['memory'] = iinfo[2] |
781 | a8083063 | Iustin Pop | output['state'] = iinfo[4] |
782 | a8083063 | Iustin Pop | output['time'] = iinfo[5] |
783 | a8083063 | Iustin Pop | |
784 | c26a6bd2 | Iustin Pop | return output
|
785 | a8083063 | Iustin Pop | |
786 | a8083063 | Iustin Pop | |
787 | 56e7640c | Iustin Pop | def GetInstanceMigratable(instance): |
788 | 56e7640c | Iustin Pop | """Gives whether an instance can be migrated.
|
789 | 56e7640c | Iustin Pop |
|
790 | 56e7640c | Iustin Pop | @type instance: L{objects.Instance}
|
791 | 56e7640c | Iustin Pop | @param instance: object representing the instance to be checked.
|
792 | 56e7640c | Iustin Pop |
|
793 | 56e7640c | Iustin Pop | @rtype: tuple
|
794 | 56e7640c | Iustin Pop | @return: tuple of (result, description) where:
|
795 | 56e7640c | Iustin Pop | - result: whether the instance can be migrated or not
|
796 | 56e7640c | Iustin Pop | - description: a description of the issue, if relevant
|
797 | 56e7640c | Iustin Pop |
|
798 | 56e7640c | Iustin Pop | """
|
799 | 56e7640c | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
800 | afdc3985 | Iustin Pop | iname = instance.name |
801 | afdc3985 | Iustin Pop | if iname not in hyper.ListInstances(): |
802 | afdc3985 | Iustin Pop | _Fail("Instance %s is not running", iname)
|
803 | 56e7640c | Iustin Pop | |
804 | 56e7640c | Iustin Pop | for idx in range(len(instance.disks)): |
805 | afdc3985 | Iustin Pop | link_name = _GetBlockDevSymlinkPath(iname, idx) |
806 | 56e7640c | Iustin Pop | if not os.path.islink(link_name): |
807 | b8ebd37b | Iustin Pop | logging.warning("Instance %s is missing symlink %s for disk %d",
|
808 | b8ebd37b | Iustin Pop | iname, link_name, idx) |
809 | 56e7640c | Iustin Pop | |
810 | 56e7640c | Iustin Pop | |
811 | e69d05fd | Iustin Pop | def GetAllInstancesInfo(hypervisor_list): |
812 | a8083063 | Iustin Pop | """Gather data about all instances.
|
813 | a8083063 | Iustin Pop |
|
814 | 10c2650b | Iustin Pop | This is the equivalent of L{GetInstanceInfo}, except that it
|
815 | a8083063 | Iustin Pop | computes data for all instances at once, thus being faster if one
|
816 | a8083063 | Iustin Pop | needs data about more than one instance.
|
817 | a8083063 | Iustin Pop |
|
818 | e69d05fd | Iustin Pop | @type hypervisor_list: list
|
819 | e69d05fd | Iustin Pop | @param hypervisor_list: list of hypervisors to query for instance data
|
820 | e69d05fd | Iustin Pop |
|
821 | 955db481 | Guido Trotter | @rtype: dict
|
822 | e69d05fd | Iustin Pop | @return: dictionary of instance: data, with data having the following keys:
|
823 | e69d05fd | Iustin Pop | - memory: memory size of instance (int)
|
824 | e69d05fd | Iustin Pop | - state: xen state of instance (string)
|
825 | e69d05fd | Iustin Pop | - time: cpu time of instance (float)
|
826 | 10c2650b | Iustin Pop | - vcpus: the number of vcpus
|
827 | a8083063 | Iustin Pop |
|
828 | 098c0958 | Michael Hanselmann | """
|
829 | a8083063 | Iustin Pop | output = {} |
830 | a8083063 | Iustin Pop | |
831 | e69d05fd | Iustin Pop | for hname in hypervisor_list: |
832 | e69d05fd | Iustin Pop | iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo() |
833 | e69d05fd | Iustin Pop | if iinfo:
|
834 | 29921401 | Iustin Pop | for name, _, memory, vcpus, state, times in iinfo: |
835 | f23b5ae8 | Iustin Pop | value = { |
836 | e69d05fd | Iustin Pop | 'memory': memory,
|
837 | e69d05fd | Iustin Pop | 'vcpus': vcpus,
|
838 | e69d05fd | Iustin Pop | 'state': state,
|
839 | e69d05fd | Iustin Pop | 'time': times,
|
840 | e69d05fd | Iustin Pop | } |
841 | b33b6f55 | Iustin Pop | if name in output: |
842 | b33b6f55 | Iustin Pop | # we only check static parameters, like memory and vcpus,
|
843 | b33b6f55 | Iustin Pop | # and not state and time which can change between the
|
844 | b33b6f55 | Iustin Pop | # invocations of the different hypervisors
|
845 | b33b6f55 | Iustin Pop | for key in 'memory', 'vcpus': |
846 | b33b6f55 | Iustin Pop | if value[key] != output[name][key]:
|
847 | 2fa74ef4 | Iustin Pop | _Fail("Instance %s is running twice"
|
848 | 2fa74ef4 | Iustin Pop | " with different parameters", name)
|
849 | f23b5ae8 | Iustin Pop | output[name] = value |
850 | a8083063 | Iustin Pop | |
851 | c26a6bd2 | Iustin Pop | return output
|
852 | a8083063 | Iustin Pop | |
853 | a8083063 | Iustin Pop | |
854 | 81a3406c | Iustin Pop | def _InstanceLogName(kind, os_name, instance): |
855 | 81a3406c | Iustin Pop | """Compute the OS log filename for a given instance and operation.
|
856 | 81a3406c | Iustin Pop |
|
857 | 81a3406c | Iustin Pop | The instance name and os name are passed in as strings since not all
|
858 | 81a3406c | Iustin Pop | operations have these as part of an instance object.
|
859 | 81a3406c | Iustin Pop |
|
860 | 81a3406c | Iustin Pop | @type kind: string
|
861 | 81a3406c | Iustin Pop | @param kind: the operation type (e.g. add, import, etc.)
|
862 | 81a3406c | Iustin Pop | @type os_name: string
|
863 | 81a3406c | Iustin Pop | @param os_name: the os name
|
864 | 81a3406c | Iustin Pop | @type instance: string
|
865 | 81a3406c | Iustin Pop | @param instance: the name of the instance being imported/added/etc.
|
866 | 81a3406c | Iustin Pop |
|
867 | 81a3406c | Iustin Pop | """
|
868 | 1651d116 | Michael Hanselmann | # TODO: Use tempfile.mkstemp to create unique filename
|
869 | 1d466a4f | Michael Hanselmann | base = ("%s-%s-%s-%s.log" %
|
870 | 1d466a4f | Michael Hanselmann | (kind, os_name, instance, utils.TimestampForFilename())) |
871 | 81a3406c | Iustin Pop | return utils.PathJoin(constants.LOG_OS_DIR, base)
|
872 | 81a3406c | Iustin Pop | |
873 | 81a3406c | Iustin Pop | |
874 | 4a0e011f | Iustin Pop | def InstanceOsAdd(instance, reinstall, debug): |
875 | 2f8598a5 | Alexander Schreiber | """Add an OS to an instance.
|
876 | a8083063 | Iustin Pop |
|
877 | d15a9ad3 | Guido Trotter | @type instance: L{objects.Instance}
|
878 | d15a9ad3 | Guido Trotter | @param instance: Instance whose OS is to be installed
|
879 | e557bae9 | Guido Trotter | @type reinstall: boolean
|
880 | e557bae9 | Guido Trotter | @param reinstall: whether this is an instance reinstall
|
881 | 4a0e011f | Iustin Pop | @type debug: integer
|
882 | 4a0e011f | Iustin Pop | @param debug: debug level, passed to the OS scripts
|
883 | c26a6bd2 | Iustin Pop | @rtype: None
|
884 | a8083063 | Iustin Pop |
|
885 | a8083063 | Iustin Pop | """
|
886 | 255dcebd | Iustin Pop | inst_os = OSFromDisk(instance.os) |
887 | 255dcebd | Iustin Pop | |
888 | 4a0e011f | Iustin Pop | create_env = OSEnvironment(instance, inst_os, debug) |
889 | e557bae9 | Guido Trotter | if reinstall:
|
890 | e557bae9 | Guido Trotter | create_env['INSTANCE_REINSTALL'] = "1" |
891 | a8083063 | Iustin Pop | |
892 | 81a3406c | Iustin Pop | logfile = _InstanceLogName("add", instance.os, instance.name)
|
893 | decd5f45 | Iustin Pop | |
894 | d868edb4 | Iustin Pop | result = utils.RunCmd([inst_os.create_script], env=create_env, |
895 | d868edb4 | Iustin Pop | cwd=inst_os.path, output=logfile,) |
896 | decd5f45 | Iustin Pop | if result.failed:
|
897 | 18682bca | Iustin Pop | logging.error("os create command '%s' returned error: %s, logfile: %s,"
|
898 | d868edb4 | Iustin Pop | " output: %s", result.cmd, result.fail_reason, logfile,
|
899 | 18682bca | Iustin Pop | result.output) |
900 | 26f15862 | Iustin Pop | lines = [utils.SafeEncode(val) |
901 | 20e01edd | Iustin Pop | for val in utils.TailFile(logfile, lines=20)] |
902 | afdc3985 | Iustin Pop | _Fail("OS create script failed (%s), last lines in the"
|
903 | afdc3985 | Iustin Pop | " log file:\n%s", result.fail_reason, "\n".join(lines), log=False) |
904 | decd5f45 | Iustin Pop | |
905 | decd5f45 | Iustin Pop | |
906 | 4a0e011f | Iustin Pop | def RunRenameInstance(instance, old_name, debug): |
907 | decd5f45 | Iustin Pop | """Run the OS rename script for an instance.
|
908 | decd5f45 | Iustin Pop |
|
909 | b1206984 | Iustin Pop | @type instance: L{objects.Instance}
|
910 | d15a9ad3 | Guido Trotter | @param instance: Instance whose OS is to be installed
|
911 | d15a9ad3 | Guido Trotter | @type old_name: string
|
912 | d15a9ad3 | Guido Trotter | @param old_name: previous instance name
|
913 | 4a0e011f | Iustin Pop | @type debug: integer
|
914 | 4a0e011f | Iustin Pop | @param debug: debug level, passed to the OS scripts
|
915 | 10c2650b | Iustin Pop | @rtype: boolean
|
916 | 10c2650b | Iustin Pop | @return: the success of the operation
|
917 | decd5f45 | Iustin Pop |
|
918 | decd5f45 | Iustin Pop | """
|
919 | decd5f45 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
920 | decd5f45 | Iustin Pop | |
921 | 4a0e011f | Iustin Pop | rename_env = OSEnvironment(instance, inst_os, debug) |
922 | ff38b6c0 | Guido Trotter | rename_env['OLD_INSTANCE_NAME'] = old_name
|
923 | decd5f45 | Iustin Pop | |
924 | 81a3406c | Iustin Pop | logfile = _InstanceLogName("rename", instance.os,
|
925 | 81a3406c | Iustin Pop | "%s-%s" % (old_name, instance.name))
|
926 | a8083063 | Iustin Pop | |
927 | d868edb4 | Iustin Pop | result = utils.RunCmd([inst_os.rename_script], env=rename_env, |
928 | d868edb4 | Iustin Pop | cwd=inst_os.path, output=logfile) |
929 | a8083063 | Iustin Pop | |
930 | a8083063 | Iustin Pop | if result.failed:
|
931 | 18682bca | Iustin Pop | logging.error("os create command '%s' returned error: %s output: %s",
|
932 | d868edb4 | Iustin Pop | result.cmd, result.fail_reason, result.output) |
933 | 26f15862 | Iustin Pop | lines = [utils.SafeEncode(val) |
934 | 96841384 | Iustin Pop | for val in utils.TailFile(logfile, lines=20)] |
935 | afdc3985 | Iustin Pop | _Fail("OS rename script failed (%s), last lines in the"
|
936 | afdc3985 | Iustin Pop | " log file:\n%s", result.fail_reason, "\n".join(lines), log=False) |
937 | a8083063 | Iustin Pop | |
938 | a8083063 | Iustin Pop | |
939 | 5282084b | Iustin Pop | def _GetBlockDevSymlinkPath(instance_name, idx): |
940 | c4feafe8 | Iustin Pop | return utils.PathJoin(constants.DISK_LINKS_DIR,
|
941 | c4feafe8 | Iustin Pop | "%s:%d" % (instance_name, idx))
|
942 | 5282084b | Iustin Pop | |
943 | 5282084b | Iustin Pop | |
944 | 5282084b | Iustin Pop | def _SymlinkBlockDev(instance_name, device_path, idx): |
945 | 9332fd8a | Iustin Pop | """Set up symlinks to a instance's block device.
|
946 | 9332fd8a | Iustin Pop |
|
947 | 9332fd8a | Iustin Pop | This is an auxiliary function run when an instance is start (on the primary
|
948 | 9332fd8a | Iustin Pop | node) or when an instance is migrated (on the target node).
|
949 | 9332fd8a | Iustin Pop |
|
950 | 9332fd8a | Iustin Pop |
|
951 | 5282084b | Iustin Pop | @param instance_name: the name of the target instance
|
952 | 5282084b | Iustin Pop | @param device_path: path of the physical block device, on the node
|
953 | 5282084b | Iustin Pop | @param idx: the disk index
|
954 | 5282084b | Iustin Pop | @return: absolute path to the disk's symlink
|
955 | 9332fd8a | Iustin Pop |
|
956 | 9332fd8a | Iustin Pop | """
|
957 | 5282084b | Iustin Pop | link_name = _GetBlockDevSymlinkPath(instance_name, idx) |
958 | 9332fd8a | Iustin Pop | try:
|
959 | 9332fd8a | Iustin Pop | os.symlink(device_path, link_name) |
960 | 5282084b | Iustin Pop | except OSError, err: |
961 | 5282084b | Iustin Pop | if err.errno == errno.EEXIST:
|
962 | 9332fd8a | Iustin Pop | if (not os.path.islink(link_name) or |
963 | 9332fd8a | Iustin Pop | os.readlink(link_name) != device_path): |
964 | 9332fd8a | Iustin Pop | os.remove(link_name) |
965 | 9332fd8a | Iustin Pop | os.symlink(device_path, link_name) |
966 | 9332fd8a | Iustin Pop | else:
|
967 | 9332fd8a | Iustin Pop | raise
|
968 | 9332fd8a | Iustin Pop | |
969 | 9332fd8a | Iustin Pop | return link_name
|
970 | 9332fd8a | Iustin Pop | |
971 | 9332fd8a | Iustin Pop | |
972 | 5282084b | Iustin Pop | def _RemoveBlockDevLinks(instance_name, disks): |
973 | 3c9c571d | Iustin Pop | """Remove the block device symlinks belonging to the given instance.
|
974 | 3c9c571d | Iustin Pop |
|
975 | 3c9c571d | Iustin Pop | """
|
976 | 29921401 | Iustin Pop | for idx, _ in enumerate(disks): |
977 | 5282084b | Iustin Pop | link_name = _GetBlockDevSymlinkPath(instance_name, idx) |
978 | 5282084b | Iustin Pop | if os.path.islink(link_name):
|
979 | 3c9c571d | Iustin Pop | try:
|
980 | 03dfa658 | Iustin Pop | os.remove(link_name) |
981 | 03dfa658 | Iustin Pop | except OSError: |
982 | 03dfa658 | Iustin Pop | logging.exception("Can't remove symlink '%s'", link_name)
|
983 | 3c9c571d | Iustin Pop | |
984 | 3c9c571d | Iustin Pop | |
985 | 9332fd8a | Iustin Pop | def _GatherAndLinkBlockDevs(instance): |
986 | a8083063 | Iustin Pop | """Set up an instance's block device(s).
|
987 | a8083063 | Iustin Pop |
|
988 | a8083063 | Iustin Pop | This is run on the primary node at instance startup. The block
|
989 | a8083063 | Iustin Pop | devices must be already assembled.
|
990 | a8083063 | Iustin Pop |
|
991 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
992 | 10c2650b | Iustin Pop | @param instance: the instance whose disks we shoul assemble
|
993 | 069cfbf1 | Iustin Pop | @rtype: list
|
994 | 069cfbf1 | Iustin Pop | @return: list of (disk_object, device_path)
|
995 | 10c2650b | Iustin Pop |
|
996 | a8083063 | Iustin Pop | """
|
997 | a8083063 | Iustin Pop | block_devices = [] |
998 | 9332fd8a | Iustin Pop | for idx, disk in enumerate(instance.disks): |
999 | a8083063 | Iustin Pop | device = _RecursiveFindBD(disk) |
1000 | a8083063 | Iustin Pop | if device is None: |
1001 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up." % |
1002 | a8083063 | Iustin Pop | str(disk))
|
1003 | a8083063 | Iustin Pop | device.Open() |
1004 | 9332fd8a | Iustin Pop | try:
|
1005 | 5282084b | Iustin Pop | link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx) |
1006 | 9332fd8a | Iustin Pop | except OSError, e: |
1007 | 9332fd8a | Iustin Pop | raise errors.BlockDeviceError("Cannot create block device symlink: %s" % |
1008 | 9332fd8a | Iustin Pop | e.strerror) |
1009 | 9332fd8a | Iustin Pop | |
1010 | 9332fd8a | Iustin Pop | block_devices.append((disk, link_name)) |
1011 | 9332fd8a | Iustin Pop | |
1012 | a8083063 | Iustin Pop | return block_devices
|
1013 | a8083063 | Iustin Pop | |
1014 | a8083063 | Iustin Pop | |
1015 | 07813a9e | Iustin Pop | def StartInstance(instance): |
1016 | a8083063 | Iustin Pop | """Start an instance.
|
1017 | a8083063 | Iustin Pop |
|
1018 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1019 | e69d05fd | Iustin Pop | @param instance: the instance object
|
1020 | c26a6bd2 | Iustin Pop | @rtype: None
|
1021 | a8083063 | Iustin Pop |
|
1022 | 098c0958 | Michael Hanselmann | """
|
1023 | e69d05fd | Iustin Pop | running_instances = GetInstanceList([instance.hypervisor]) |
1024 | a8083063 | Iustin Pop | |
1025 | a8083063 | Iustin Pop | if instance.name in running_instances: |
1026 | c26a6bd2 | Iustin Pop | logging.info("Instance %s already running, not starting", instance.name)
|
1027 | c26a6bd2 | Iustin Pop | return
|
1028 | a8083063 | Iustin Pop | |
1029 | a8083063 | Iustin Pop | try:
|
1030 | ec596c24 | Iustin Pop | block_devices = _GatherAndLinkBlockDevs(instance) |
1031 | ec596c24 | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1032 | 07813a9e | Iustin Pop | hyper.StartInstance(instance, block_devices) |
1033 | ec596c24 | Iustin Pop | except errors.BlockDeviceError, err:
|
1034 | 2cc6781a | Iustin Pop | _Fail("Block device error: %s", err, exc=True) |
1035 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
1036 | 5282084b | Iustin Pop | _RemoveBlockDevLinks(instance.name, instance.disks) |
1037 | 2cc6781a | Iustin Pop | _Fail("Hypervisor error: %s", err, exc=True) |
1038 | a8083063 | Iustin Pop | |
1039 | a8083063 | Iustin Pop | |
1040 | 6263189c | Guido Trotter | def InstanceShutdown(instance, timeout): |
1041 | a8083063 | Iustin Pop | """Shut an instance down.
|
1042 | a8083063 | Iustin Pop |
|
1043 | 10c2650b | Iustin Pop | @note: this functions uses polling with a hardcoded timeout.
|
1044 | 10c2650b | Iustin Pop |
|
1045 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1046 | e69d05fd | Iustin Pop | @param instance: the instance object
|
1047 | 6263189c | Guido Trotter | @type timeout: integer
|
1048 | 6263189c | Guido Trotter | @param timeout: maximum timeout for soft shutdown
|
1049 | c26a6bd2 | Iustin Pop | @rtype: None
|
1050 | a8083063 | Iustin Pop |
|
1051 | 098c0958 | Michael Hanselmann | """
|
1052 | e69d05fd | Iustin Pop | hv_name = instance.hypervisor |
1053 | e4e9b806 | Guido Trotter | hyper = hypervisor.GetHypervisor(hv_name) |
1054 | c26a6bd2 | Iustin Pop | iname = instance.name |
1055 | a8083063 | Iustin Pop | |
1056 | 3c0cdc83 | Michael Hanselmann | if instance.name not in hyper.ListInstances(): |
1057 | c26a6bd2 | Iustin Pop | logging.info("Instance %s not running, doing nothing", iname)
|
1058 | c26a6bd2 | Iustin Pop | return
|
1059 | a8083063 | Iustin Pop | |
1060 | 3c0cdc83 | Michael Hanselmann | class _TryShutdown: |
1061 | 3c0cdc83 | Michael Hanselmann | def __init__(self): |
1062 | 3c0cdc83 | Michael Hanselmann | self.tried_once = False |
1063 | a8083063 | Iustin Pop | |
1064 | 3c0cdc83 | Michael Hanselmann | def __call__(self): |
1065 | 3c0cdc83 | Michael Hanselmann | if iname not in hyper.ListInstances(): |
1066 | 3c0cdc83 | Michael Hanselmann | return
|
1067 | 3c0cdc83 | Michael Hanselmann | |
1068 | 3c0cdc83 | Michael Hanselmann | try:
|
1069 | 3c0cdc83 | Michael Hanselmann | hyper.StopInstance(instance, retry=self.tried_once)
|
1070 | 3c0cdc83 | Michael Hanselmann | except errors.HypervisorError, err:
|
1071 | 3c0cdc83 | Michael Hanselmann | if iname not in hyper.ListInstances(): |
1072 | 3c0cdc83 | Michael Hanselmann | # if the instance is no longer existing, consider this a
|
1073 | 3c0cdc83 | Michael Hanselmann | # success and go to cleanup
|
1074 | 3c0cdc83 | Michael Hanselmann | return
|
1075 | 3c0cdc83 | Michael Hanselmann | |
1076 | 3c0cdc83 | Michael Hanselmann | _Fail("Failed to stop instance %s: %s", iname, err)
|
1077 | 3c0cdc83 | Michael Hanselmann | |
1078 | 3c0cdc83 | Michael Hanselmann | self.tried_once = True |
1079 | 3c0cdc83 | Michael Hanselmann | |
1080 | 3c0cdc83 | Michael Hanselmann | raise utils.RetryAgain()
|
1081 | 3c0cdc83 | Michael Hanselmann | |
1082 | 3c0cdc83 | Michael Hanselmann | try:
|
1083 | 3c0cdc83 | Michael Hanselmann | utils.Retry(_TryShutdown(), 5, timeout)
|
1084 | 3c0cdc83 | Michael Hanselmann | except utils.RetryTimeout:
|
1085 | a8083063 | Iustin Pop | # the shutdown did not succeed
|
1086 | e4e9b806 | Guido Trotter | logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
|
1087 | a8083063 | Iustin Pop | |
1088 | a8083063 | Iustin Pop | try:
|
1089 | a8083063 | Iustin Pop | hyper.StopInstance(instance, force=True)
|
1090 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
1091 | 3c0cdc83 | Michael Hanselmann | if iname in hyper.ListInstances(): |
1092 | 3782acd7 | Iustin Pop | # only raise an error if the instance still exists, otherwise
|
1093 | 3782acd7 | Iustin Pop | # the error could simply be "instance ... unknown"!
|
1094 | 3782acd7 | Iustin Pop | _Fail("Failed to force stop instance %s: %s", iname, err)
|
1095 | a8083063 | Iustin Pop | |
1096 | a8083063 | Iustin Pop | time.sleep(1)
|
1097 | 3c0cdc83 | Michael Hanselmann | |
1098 | 3c0cdc83 | Michael Hanselmann | if iname in hyper.ListInstances(): |
1099 | c26a6bd2 | Iustin Pop | _Fail("Could not shutdown instance %s even by destroy", iname)
|
1100 | 3c9c571d | Iustin Pop | |
1101 | f28ec899 | Guido Trotter | try:
|
1102 | f28ec899 | Guido Trotter | hyper.CleanupInstance(instance.name) |
1103 | f28ec899 | Guido Trotter | except errors.HypervisorError, err:
|
1104 | f28ec899 | Guido Trotter | logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
|
1105 | f28ec899 | Guido Trotter | |
1106 | c26a6bd2 | Iustin Pop | _RemoveBlockDevLinks(iname, instance.disks) |
1107 | a8083063 | Iustin Pop | |
1108 | a8083063 | Iustin Pop | |
1109 | 17c3f802 | Guido Trotter | def InstanceReboot(instance, reboot_type, shutdown_timeout): |
1110 | 007a2f3e | Alexander Schreiber | """Reboot an instance.
|
1111 | 007a2f3e | Alexander Schreiber |
|
1112 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1113 | 10c2650b | Iustin Pop | @param instance: the instance object to reboot
|
1114 | 10c2650b | Iustin Pop | @type reboot_type: str
|
1115 | 10c2650b | Iustin Pop | @param reboot_type: the type of reboot, one the following
|
1116 | 10c2650b | Iustin Pop | constants:
|
1117 | 10c2650b | Iustin Pop | - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
|
1118 | 10c2650b | Iustin Pop | instance OS, do not recreate the VM
|
1119 | 10c2650b | Iustin Pop | - L{constants.INSTANCE_REBOOT_HARD}: tear down and
|
1120 | 10c2650b | Iustin Pop | restart the VM (at the hypervisor level)
|
1121 | 73e5a4f4 | Iustin Pop | - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
|
1122 | 73e5a4f4 | Iustin Pop | not accepted here, since that mode is handled differently, in
|
1123 | 73e5a4f4 | Iustin Pop | cmdlib, and translates into full stop and start of the
|
1124 | 73e5a4f4 | Iustin Pop | instance (instead of a call_instance_reboot RPC)
|
1125 | 23057d29 | Michael Hanselmann | @type shutdown_timeout: integer
|
1126 | 23057d29 | Michael Hanselmann | @param shutdown_timeout: maximum timeout for soft shutdown
|
1127 | c26a6bd2 | Iustin Pop | @rtype: None
|
1128 | 007a2f3e | Alexander Schreiber |
|
1129 | 007a2f3e | Alexander Schreiber | """
|
1130 | e69d05fd | Iustin Pop | running_instances = GetInstanceList([instance.hypervisor]) |
1131 | 007a2f3e | Alexander Schreiber | |
1132 | 007a2f3e | Alexander Schreiber | if instance.name not in running_instances: |
1133 | 2cc6781a | Iustin Pop | _Fail("Cannot reboot instance %s that is not running", instance.name)
|
1134 | 007a2f3e | Alexander Schreiber | |
1135 | e69d05fd | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1136 | 007a2f3e | Alexander Schreiber | if reboot_type == constants.INSTANCE_REBOOT_SOFT:
|
1137 | 007a2f3e | Alexander Schreiber | try:
|
1138 | 007a2f3e | Alexander Schreiber | hyper.RebootInstance(instance) |
1139 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
1140 | 2cc6781a | Iustin Pop | _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
|
1141 | 007a2f3e | Alexander Schreiber | elif reboot_type == constants.INSTANCE_REBOOT_HARD:
|
1142 | 007a2f3e | Alexander Schreiber | try:
|
1143 | 17c3f802 | Guido Trotter | InstanceShutdown(instance, shutdown_timeout) |
1144 | 07813a9e | Iustin Pop | return StartInstance(instance)
|
1145 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
1146 | 2cc6781a | Iustin Pop | _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
|
1147 | 007a2f3e | Alexander Schreiber | else:
|
1148 | 2cc6781a | Iustin Pop | _Fail("Invalid reboot_type received: %s", reboot_type)
|
1149 | 007a2f3e | Alexander Schreiber | |
1150 | 007a2f3e | Alexander Schreiber | |
1151 | 6906a9d8 | Guido Trotter | def MigrationInfo(instance): |
1152 | 6906a9d8 | Guido Trotter | """Gather information about an instance to be migrated.
|
1153 | 6906a9d8 | Guido Trotter |
|
1154 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1155 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1156 | 6906a9d8 | Guido Trotter |
|
1157 | 6906a9d8 | Guido Trotter | """
|
1158 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1159 | cd42d0ad | Guido Trotter | try:
|
1160 | cd42d0ad | Guido Trotter | info = hyper.MigrationInfo(instance) |
1161 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1162 | 2cc6781a | Iustin Pop | _Fail("Failed to fetch migration information: %s", err, exc=True) |
1163 | c26a6bd2 | Iustin Pop | return info
|
1164 | 6906a9d8 | Guido Trotter | |
1165 | 6906a9d8 | Guido Trotter | |
1166 | 6906a9d8 | Guido Trotter | def AcceptInstance(instance, info, target): |
1167 | 6906a9d8 | Guido Trotter | """Prepare the node to accept an instance.
|
1168 | 6906a9d8 | Guido Trotter |
|
1169 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1170 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1171 | 6906a9d8 | Guido Trotter | @type info: string/data (opaque)
|
1172 | 6906a9d8 | Guido Trotter | @param info: migration information, from the source node
|
1173 | 6906a9d8 | Guido Trotter | @type target: string
|
1174 | 6906a9d8 | Guido Trotter | @param target: target host (usually ip), on this node
|
1175 | 6906a9d8 | Guido Trotter |
|
1176 | 6906a9d8 | Guido Trotter | """
|
1177 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1178 | cd42d0ad | Guido Trotter | try:
|
1179 | cd42d0ad | Guido Trotter | hyper.AcceptInstance(instance, info, target) |
1180 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1181 | 2cc6781a | Iustin Pop | _Fail("Failed to accept instance: %s", err, exc=True) |
1182 | 6906a9d8 | Guido Trotter | |
1183 | 6906a9d8 | Guido Trotter | |
1184 | 6906a9d8 | Guido Trotter | def FinalizeMigration(instance, info, success): |
1185 | 6906a9d8 | Guido Trotter | """Finalize any preparation to accept an instance.
|
1186 | 6906a9d8 | Guido Trotter |
|
1187 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1188 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1189 | 6906a9d8 | Guido Trotter | @type info: string/data (opaque)
|
1190 | 6906a9d8 | Guido Trotter | @param info: migration information, from the source node
|
1191 | 6906a9d8 | Guido Trotter | @type success: boolean
|
1192 | 6906a9d8 | Guido Trotter | @param success: whether the migration was a success or a failure
|
1193 | 6906a9d8 | Guido Trotter |
|
1194 | 6906a9d8 | Guido Trotter | """
|
1195 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1196 | cd42d0ad | Guido Trotter | try:
|
1197 | cd42d0ad | Guido Trotter | hyper.FinalizeMigration(instance, info, success) |
1198 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1199 | 2cc6781a | Iustin Pop | _Fail("Failed to finalize migration: %s", err, exc=True) |
1200 | 6906a9d8 | Guido Trotter | |
1201 | 6906a9d8 | Guido Trotter | |
1202 | 2a10865c | Iustin Pop | def MigrateInstance(instance, target, live): |
1203 | 2a10865c | Iustin Pop | """Migrates an instance to another node.
|
1204 | 2a10865c | Iustin Pop |
|
1205 | b1206984 | Iustin Pop | @type instance: L{objects.Instance}
|
1206 | 9f0e6b37 | Iustin Pop | @param instance: the instance definition
|
1207 | 9f0e6b37 | Iustin Pop | @type target: string
|
1208 | 9f0e6b37 | Iustin Pop | @param target: the target node name
|
1209 | 9f0e6b37 | Iustin Pop | @type live: boolean
|
1210 | 9f0e6b37 | Iustin Pop | @param live: whether the migration should be done live or not (the
|
1211 | 9f0e6b37 | Iustin Pop | interpretation of this parameter is left to the hypervisor)
|
1212 | 9f0e6b37 | Iustin Pop | @rtype: tuple
|
1213 | 9f0e6b37 | Iustin Pop | @return: a tuple of (success, msg) where:
|
1214 | 9f0e6b37 | Iustin Pop | - succes is a boolean denoting the success/failure of the operation
|
1215 | 9f0e6b37 | Iustin Pop | - msg is a string with details in case of failure
|
1216 | 9f0e6b37 | Iustin Pop |
|
1217 | 2a10865c | Iustin Pop | """
|
1218 | 53c776b5 | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1219 | 2a10865c | Iustin Pop | |
1220 | 2a10865c | Iustin Pop | try:
|
1221 | 58d38b02 | Iustin Pop | hyper.MigrateInstance(instance, target, live) |
1222 | 2a10865c | Iustin Pop | except errors.HypervisorError, err:
|
1223 | 2cc6781a | Iustin Pop | _Fail("Failed to migrate instance: %s", err, exc=True) |
1224 | 2a10865c | Iustin Pop | |
1225 | 2a10865c | Iustin Pop | |
1226 | 821d1bd1 | Iustin Pop | def BlockdevCreate(disk, size, owner, on_primary, info): |
1227 | a8083063 | Iustin Pop | """Creates a block device for an instance.
|
1228 | a8083063 | Iustin Pop |
|
1229 | b1206984 | Iustin Pop | @type disk: L{objects.Disk}
|
1230 | b1206984 | Iustin Pop | @param disk: the object describing the disk we should create
|
1231 | b1206984 | Iustin Pop | @type size: int
|
1232 | b1206984 | Iustin Pop | @param size: the size of the physical underlying device, in MiB
|
1233 | b1206984 | Iustin Pop | @type owner: str
|
1234 | b1206984 | Iustin Pop | @param owner: the name of the instance for which disk is created,
|
1235 | b1206984 | Iustin Pop | used for device cache data
|
1236 | b1206984 | Iustin Pop | @type on_primary: boolean
|
1237 | b1206984 | Iustin Pop | @param on_primary: indicates if it is the primary node or not
|
1238 | b1206984 | Iustin Pop | @type info: string
|
1239 | b1206984 | Iustin Pop | @param info: string that will be sent to the physical device
|
1240 | b1206984 | Iustin Pop | creation, used for example to set (LVM) tags on LVs
|
1241 | b1206984 | Iustin Pop |
|
1242 | b1206984 | Iustin Pop | @return: the new unique_id of the device (this can sometime be
|
1243 | b1206984 | Iustin Pop | computed only after creation), or None. On secondary nodes,
|
1244 | b1206984 | Iustin Pop | it's not required to return anything.
|
1245 | a8083063 | Iustin Pop |
|
1246 | a8083063 | Iustin Pop | """
|
1247 | 7260cfbe | Iustin Pop | # TODO: remove the obsolete 'size' argument
|
1248 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0613
|
1249 | a8083063 | Iustin Pop | clist = [] |
1250 | a8083063 | Iustin Pop | if disk.children:
|
1251 | a8083063 | Iustin Pop | for child in disk.children: |
1252 | 1063abd1 | Iustin Pop | try:
|
1253 | 1063abd1 | Iustin Pop | crdev = _RecursiveAssembleBD(child, owner, on_primary) |
1254 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1255 | 2cc6781a | Iustin Pop | _Fail("Can't assemble device %s: %s", child, err)
|
1256 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
1257 | a8083063 | Iustin Pop | # we need the children open in case the device itself has to
|
1258 | a8083063 | Iustin Pop | # be assembled
|
1259 | 1063abd1 | Iustin Pop | try:
|
1260 | fe267188 | Iustin Pop | # pylint: disable-msg=E1103
|
1261 | 1063abd1 | Iustin Pop | crdev.Open() |
1262 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1263 | 2cc6781a | Iustin Pop | _Fail("Can't make child '%s' read-write: %s", child, err)
|
1264 | a8083063 | Iustin Pop | clist.append(crdev) |
1265 | a8083063 | Iustin Pop | |
1266 | dab69e97 | Iustin Pop | try:
|
1267 | 464f8daf | Iustin Pop | device = bdev.Create(disk.dev_type, disk.physical_id, clist, disk.size) |
1268 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1269 | 2cc6781a | Iustin Pop | _Fail("Can't create block device: %s", err)
|
1270 | 6c626518 | Iustin Pop | |
1271 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
1272 | 1063abd1 | Iustin Pop | try:
|
1273 | 1063abd1 | Iustin Pop | device.Assemble() |
1274 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1275 | 2cc6781a | Iustin Pop | _Fail("Can't assemble device after creation, unusual event: %s", err)
|
1276 | e31c43f7 | Michael Hanselmann | device.SetSyncSpeed(constants.SYNC_SPEED) |
1277 | a8083063 | Iustin Pop | if on_primary or disk.OpenOnSecondary(): |
1278 | 1063abd1 | Iustin Pop | try:
|
1279 | 1063abd1 | Iustin Pop | device.Open(force=True)
|
1280 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1281 | 2cc6781a | Iustin Pop | _Fail("Can't make device r/w after creation, unusual event: %s", err)
|
1282 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(device.dev_path, owner, |
1283 | 3f78eef2 | Iustin Pop | on_primary, disk.iv_name) |
1284 | a0c3fea1 | Michael Hanselmann | |
1285 | a0c3fea1 | Michael Hanselmann | device.SetInfo(info) |
1286 | a0c3fea1 | Michael Hanselmann | |
1287 | c26a6bd2 | Iustin Pop | return device.unique_id
|
1288 | a8083063 | Iustin Pop | |
1289 | a8083063 | Iustin Pop | |
1290 | 821d1bd1 | Iustin Pop | def BlockdevRemove(disk): |
1291 | a8083063 | Iustin Pop | """Remove a block device.
|
1292 | a8083063 | Iustin Pop |
|
1293 | 10c2650b | Iustin Pop | @note: This is intended to be called recursively.
|
1294 | 10c2650b | Iustin Pop |
|
1295 | c41eea6e | Iustin Pop | @type disk: L{objects.Disk}
|
1296 | 10c2650b | Iustin Pop | @param disk: the disk object we should remove
|
1297 | 10c2650b | Iustin Pop | @rtype: boolean
|
1298 | 10c2650b | Iustin Pop | @return: the success of the operation
|
1299 | a8083063 | Iustin Pop |
|
1300 | a8083063 | Iustin Pop | """
|
1301 | e1bc0878 | Iustin Pop | msgs = [] |
1302 | a8083063 | Iustin Pop | try:
|
1303 | bca2e7f4 | Iustin Pop | rdev = _RecursiveFindBD(disk) |
1304 | a8083063 | Iustin Pop | except errors.BlockDeviceError, err:
|
1305 | a8083063 | Iustin Pop | # probably can't attach
|
1306 | 18682bca | Iustin Pop | logging.info("Can't attach to device %s in remove", disk)
|
1307 | a8083063 | Iustin Pop | rdev = None
|
1308 | a8083063 | Iustin Pop | if rdev is not None: |
1309 | 3f78eef2 | Iustin Pop | r_path = rdev.dev_path |
1310 | e1bc0878 | Iustin Pop | try:
|
1311 | 0c6c04ec | Iustin Pop | rdev.Remove() |
1312 | e1bc0878 | Iustin Pop | except errors.BlockDeviceError, err:
|
1313 | e1bc0878 | Iustin Pop | msgs.append(str(err))
|
1314 | c26a6bd2 | Iustin Pop | if not msgs: |
1315 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
1316 | e1bc0878 | Iustin Pop | |
1317 | a8083063 | Iustin Pop | if disk.children:
|
1318 | a8083063 | Iustin Pop | for child in disk.children: |
1319 | c26a6bd2 | Iustin Pop | try:
|
1320 | c26a6bd2 | Iustin Pop | BlockdevRemove(child) |
1321 | c26a6bd2 | Iustin Pop | except RPCFail, err:
|
1322 | c26a6bd2 | Iustin Pop | msgs.append(str(err))
|
1323 | e1bc0878 | Iustin Pop | |
1324 | c26a6bd2 | Iustin Pop | if msgs:
|
1325 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
1326 | afdc3985 | Iustin Pop | |
1327 | a8083063 | Iustin Pop | |
1328 | 3f78eef2 | Iustin Pop | def _RecursiveAssembleBD(disk, owner, as_primary): |
1329 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
1330 | a8083063 | Iustin Pop |
|
1331 | a8083063 | Iustin Pop | This is run on the primary and secondary nodes for an instance.
|
1332 | a8083063 | Iustin Pop |
|
1333 | 10c2650b | Iustin Pop | @note: this function is called recursively.
|
1334 | a8083063 | Iustin Pop |
|
1335 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
1336 | 10c2650b | Iustin Pop | @param disk: the disk we try to assemble
|
1337 | 10c2650b | Iustin Pop | @type owner: str
|
1338 | 10c2650b | Iustin Pop | @param owner: the name of the instance which owns the disk
|
1339 | 10c2650b | Iustin Pop | @type as_primary: boolean
|
1340 | 10c2650b | Iustin Pop | @param as_primary: if we should make the block device
|
1341 | 10c2650b | Iustin Pop | read/write
|
1342 | a8083063 | Iustin Pop |
|
1343 | 10c2650b | Iustin Pop | @return: the assembled device or None (in case no device
|
1344 | 10c2650b | Iustin Pop | was assembled)
|
1345 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: in case there is an error
|
1346 | 10c2650b | Iustin Pop | during the activation of the children or the device
|
1347 | 10c2650b | Iustin Pop | itself
|
1348 | a8083063 | Iustin Pop |
|
1349 | a8083063 | Iustin Pop | """
|
1350 | a8083063 | Iustin Pop | children = [] |
1351 | a8083063 | Iustin Pop | if disk.children:
|
1352 | fc1dc9d7 | Iustin Pop | mcn = disk.ChildrenNeeded() |
1353 | fc1dc9d7 | Iustin Pop | if mcn == -1: |
1354 | fc1dc9d7 | Iustin Pop | mcn = 0 # max number of Nones allowed |
1355 | fc1dc9d7 | Iustin Pop | else:
|
1356 | fc1dc9d7 | Iustin Pop | mcn = len(disk.children) - mcn # max number of Nones |
1357 | a8083063 | Iustin Pop | for chld_disk in disk.children: |
1358 | fc1dc9d7 | Iustin Pop | try:
|
1359 | fc1dc9d7 | Iustin Pop | cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary) |
1360 | fc1dc9d7 | Iustin Pop | except errors.BlockDeviceError, err:
|
1361 | 7803d4d3 | Iustin Pop | if children.count(None) >= mcn: |
1362 | fc1dc9d7 | Iustin Pop | raise
|
1363 | fc1dc9d7 | Iustin Pop | cdev = None
|
1364 | 1063abd1 | Iustin Pop | logging.error("Error in child activation (but continuing): %s",
|
1365 | 1063abd1 | Iustin Pop | str(err))
|
1366 | fc1dc9d7 | Iustin Pop | children.append(cdev) |
1367 | a8083063 | Iustin Pop | |
1368 | a8083063 | Iustin Pop | if as_primary or disk.AssembleOnSecondary(): |
1369 | 464f8daf | Iustin Pop | r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children, disk.size) |
1370 | e31c43f7 | Michael Hanselmann | r_dev.SetSyncSpeed(constants.SYNC_SPEED) |
1371 | a8083063 | Iustin Pop | result = r_dev |
1372 | a8083063 | Iustin Pop | if as_primary or disk.OpenOnSecondary(): |
1373 | a8083063 | Iustin Pop | r_dev.Open() |
1374 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(r_dev.dev_path, owner, |
1375 | 3f78eef2 | Iustin Pop | as_primary, disk.iv_name) |
1376 | 3f78eef2 | Iustin Pop | |
1377 | a8083063 | Iustin Pop | else:
|
1378 | a8083063 | Iustin Pop | result = True
|
1379 | a8083063 | Iustin Pop | return result
|
1380 | a8083063 | Iustin Pop | |
1381 | a8083063 | Iustin Pop | |
1382 | 821d1bd1 | Iustin Pop | def BlockdevAssemble(disk, owner, as_primary): |
1383 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
1384 | a8083063 | Iustin Pop |
|
1385 | a8083063 | Iustin Pop | This is a wrapper over _RecursiveAssembleBD.
|
1386 | a8083063 | Iustin Pop |
|
1387 | b1206984 | Iustin Pop | @rtype: str or boolean
|
1388 | b1206984 | Iustin Pop | @return: a C{/dev/...} path for primary nodes, and
|
1389 | b1206984 | Iustin Pop | C{True} for secondary nodes
|
1390 | a8083063 | Iustin Pop |
|
1391 | a8083063 | Iustin Pop | """
|
1392 | 53c14ef1 | Iustin Pop | try:
|
1393 | 53c14ef1 | Iustin Pop | result = _RecursiveAssembleBD(disk, owner, as_primary) |
1394 | 53c14ef1 | Iustin Pop | if isinstance(result, bdev.BlockDev): |
1395 | fe267188 | Iustin Pop | # pylint: disable-msg=E1103
|
1396 | 53c14ef1 | Iustin Pop | result = result.dev_path |
1397 | 53c14ef1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1398 | afdc3985 | Iustin Pop | _Fail("Error while assembling disk: %s", err, exc=True) |
1399 | afdc3985 | Iustin Pop | |
1400 | c26a6bd2 | Iustin Pop | return result
|
1401 | a8083063 | Iustin Pop | |
1402 | a8083063 | Iustin Pop | |
1403 | 821d1bd1 | Iustin Pop | def BlockdevShutdown(disk): |
1404 | a8083063 | Iustin Pop | """Shut down a block device.
|
1405 | a8083063 | Iustin Pop |
|
1406 | 5bbd3f7f | Michael Hanselmann | First, if the device is assembled (Attach() is successful), then
|
1407 | c41eea6e | Iustin Pop | the device is shutdown. Then the children of the device are
|
1408 | c41eea6e | Iustin Pop | shutdown.
|
1409 | a8083063 | Iustin Pop |
|
1410 | a8083063 | Iustin Pop | This function is called recursively. Note that we don't cache the
|
1411 | a8083063 | Iustin Pop | children or such, as oppossed to assemble, shutdown of different
|
1412 | a8083063 | Iustin Pop | devices doesn't require that the upper device was active.
|
1413 | a8083063 | Iustin Pop |
|
1414 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
1415 | 10c2650b | Iustin Pop | @param disk: the description of the disk we should
|
1416 | 10c2650b | Iustin Pop | shutdown
|
1417 | c26a6bd2 | Iustin Pop | @rtype: None
|
1418 | 10c2650b | Iustin Pop |
|
1419 | a8083063 | Iustin Pop | """
|
1420 | cacfd1fd | Iustin Pop | msgs = [] |
1421 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
1422 | a8083063 | Iustin Pop | if r_dev is not None: |
1423 | 3f78eef2 | Iustin Pop | r_path = r_dev.dev_path |
1424 | cacfd1fd | Iustin Pop | try:
|
1425 | 746f7476 | Iustin Pop | r_dev.Shutdown() |
1426 | 746f7476 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
1427 | cacfd1fd | Iustin Pop | except errors.BlockDeviceError, err:
|
1428 | cacfd1fd | Iustin Pop | msgs.append(str(err))
|
1429 | 746f7476 | Iustin Pop | |
1430 | a8083063 | Iustin Pop | if disk.children:
|
1431 | a8083063 | Iustin Pop | for child in disk.children: |
1432 | c26a6bd2 | Iustin Pop | try:
|
1433 | c26a6bd2 | Iustin Pop | BlockdevShutdown(child) |
1434 | c26a6bd2 | Iustin Pop | except RPCFail, err:
|
1435 | c26a6bd2 | Iustin Pop | msgs.append(str(err))
|
1436 | 746f7476 | Iustin Pop | |
1437 | c26a6bd2 | Iustin Pop | if msgs:
|
1438 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
1439 | a8083063 | Iustin Pop | |
1440 | a8083063 | Iustin Pop | |
1441 | 821d1bd1 | Iustin Pop | def BlockdevAddchildren(parent_cdev, new_cdevs): |
1442 | 153d9724 | Iustin Pop | """Extend a mirrored block device.
|
1443 | a8083063 | Iustin Pop |
|
1444 | 10c2650b | Iustin Pop | @type parent_cdev: L{objects.Disk}
|
1445 | 10c2650b | Iustin Pop | @param parent_cdev: the disk to which we should add children
|
1446 | 10c2650b | Iustin Pop | @type new_cdevs: list of L{objects.Disk}
|
1447 | 10c2650b | Iustin Pop | @param new_cdevs: the list of children which we should add
|
1448 | c26a6bd2 | Iustin Pop | @rtype: None
|
1449 | 10c2650b | Iustin Pop |
|
1450 | a8083063 | Iustin Pop | """
|
1451 | bca2e7f4 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev) |
1452 | 153d9724 | Iustin Pop | if parent_bdev is None: |
1453 | 2cc6781a | Iustin Pop | _Fail("Can't find parent device '%s' in add children", parent_cdev)
|
1454 | 153d9724 | Iustin Pop | new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs] |
1455 | 153d9724 | Iustin Pop | if new_bdevs.count(None) > 0: |
1456 | 2cc6781a | Iustin Pop | _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
|
1457 | 153d9724 | Iustin Pop | parent_bdev.AddChildren(new_bdevs) |
1458 | a8083063 | Iustin Pop | |
1459 | a8083063 | Iustin Pop | |
1460 | 821d1bd1 | Iustin Pop | def BlockdevRemovechildren(parent_cdev, new_cdevs): |
1461 | 153d9724 | Iustin Pop | """Shrink a mirrored block device.
|
1462 | a8083063 | Iustin Pop |
|
1463 | 10c2650b | Iustin Pop | @type parent_cdev: L{objects.Disk}
|
1464 | 10c2650b | Iustin Pop | @param parent_cdev: the disk from which we should remove children
|
1465 | 10c2650b | Iustin Pop | @type new_cdevs: list of L{objects.Disk}
|
1466 | 10c2650b | Iustin Pop | @param new_cdevs: the list of children which we should remove
|
1467 | c26a6bd2 | Iustin Pop | @rtype: None
|
1468 | 10c2650b | Iustin Pop |
|
1469 | a8083063 | Iustin Pop | """
|
1470 | 153d9724 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev) |
1471 | 153d9724 | Iustin Pop | if parent_bdev is None: |
1472 | 2cc6781a | Iustin Pop | _Fail("Can't find parent device '%s' in remove children", parent_cdev)
|
1473 | e739bd57 | Iustin Pop | devs = [] |
1474 | e739bd57 | Iustin Pop | for disk in new_cdevs: |
1475 | e739bd57 | Iustin Pop | rpath = disk.StaticDevPath() |
1476 | e739bd57 | Iustin Pop | if rpath is None: |
1477 | e739bd57 | Iustin Pop | bd = _RecursiveFindBD(disk) |
1478 | e739bd57 | Iustin Pop | if bd is None: |
1479 | 2cc6781a | Iustin Pop | _Fail("Can't find device %s while removing children", disk)
|
1480 | e739bd57 | Iustin Pop | else:
|
1481 | e739bd57 | Iustin Pop | devs.append(bd.dev_path) |
1482 | e739bd57 | Iustin Pop | else:
|
1483 | e51db2a6 | Iustin Pop | if not utils.IsNormAbsPath(rpath): |
1484 | e51db2a6 | Iustin Pop | _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
|
1485 | e739bd57 | Iustin Pop | devs.append(rpath) |
1486 | e739bd57 | Iustin Pop | parent_bdev.RemoveChildren(devs) |
1487 | a8083063 | Iustin Pop | |
1488 | a8083063 | Iustin Pop | |
1489 | 821d1bd1 | Iustin Pop | def BlockdevGetmirrorstatus(disks): |
1490 | a8083063 | Iustin Pop | """Get the mirroring status of a list of devices.
|
1491 | a8083063 | Iustin Pop |
|
1492 | 10c2650b | Iustin Pop | @type disks: list of L{objects.Disk}
|
1493 | 10c2650b | Iustin Pop | @param disks: the list of disks which we should query
|
1494 | 10c2650b | Iustin Pop | @rtype: disk
|
1495 | 10c2650b | Iustin Pop | @return:
|
1496 | 10c2650b | Iustin Pop | a list of (mirror_done, estimated_time) tuples, which
|
1497 | c41eea6e | Iustin Pop | are the result of L{bdev.BlockDev.CombinedSyncStatus}
|
1498 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: if any of the disks cannot be
|
1499 | 10c2650b | Iustin Pop | found
|
1500 | a8083063 | Iustin Pop |
|
1501 | a8083063 | Iustin Pop | """
|
1502 | a8083063 | Iustin Pop | stats = [] |
1503 | a8083063 | Iustin Pop | for dsk in disks: |
1504 | a8083063 | Iustin Pop | rbd = _RecursiveFindBD(dsk) |
1505 | a8083063 | Iustin Pop | if rbd is None: |
1506 | 3efa9051 | Iustin Pop | _Fail("Can't find device %s", dsk)
|
1507 | 96acbc09 | Michael Hanselmann | |
1508 | 36145b12 | Michael Hanselmann | stats.append(rbd.CombinedSyncStatus()) |
1509 | 96acbc09 | Michael Hanselmann | |
1510 | c26a6bd2 | Iustin Pop | return stats
|
1511 | a8083063 | Iustin Pop | |
1512 | a8083063 | Iustin Pop | |
1513 | bca2e7f4 | Iustin Pop | def _RecursiveFindBD(disk): |
1514 | a8083063 | Iustin Pop | """Check if a device is activated.
|
1515 | a8083063 | Iustin Pop |
|
1516 | 5bbd3f7f | Michael Hanselmann | If so, return information about the real device.
|
1517 | a8083063 | Iustin Pop |
|
1518 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
1519 | 10c2650b | Iustin Pop | @param disk: the disk object we need to find
|
1520 | a8083063 | Iustin Pop |
|
1521 | 10c2650b | Iustin Pop | @return: None if the device can't be found,
|
1522 | 10c2650b | Iustin Pop | otherwise the device instance
|
1523 | a8083063 | Iustin Pop |
|
1524 | a8083063 | Iustin Pop | """
|
1525 | a8083063 | Iustin Pop | children = [] |
1526 | a8083063 | Iustin Pop | if disk.children:
|
1527 | a8083063 | Iustin Pop | for chdisk in disk.children: |
1528 | a8083063 | Iustin Pop | children.append(_RecursiveFindBD(chdisk)) |
1529 | a8083063 | Iustin Pop | |
1530 | 464f8daf | Iustin Pop | return bdev.FindDevice(disk.dev_type, disk.physical_id, children, disk.size)
|
1531 | a8083063 | Iustin Pop | |
1532 | a8083063 | Iustin Pop | |
1533 | f2e07bb4 | Michael Hanselmann | def _OpenRealBD(disk): |
1534 | f2e07bb4 | Michael Hanselmann | """Opens the underlying block device of a disk.
|
1535 | f2e07bb4 | Michael Hanselmann |
|
1536 | f2e07bb4 | Michael Hanselmann | @type disk: L{objects.Disk}
|
1537 | f2e07bb4 | Michael Hanselmann | @param disk: the disk object we want to open
|
1538 | f2e07bb4 | Michael Hanselmann |
|
1539 | f2e07bb4 | Michael Hanselmann | """
|
1540 | f2e07bb4 | Michael Hanselmann | real_disk = _RecursiveFindBD(disk) |
1541 | f2e07bb4 | Michael Hanselmann | if real_disk is None: |
1542 | f2e07bb4 | Michael Hanselmann | _Fail("Block device '%s' is not set up", disk)
|
1543 | f2e07bb4 | Michael Hanselmann | |
1544 | f2e07bb4 | Michael Hanselmann | real_disk.Open() |
1545 | f2e07bb4 | Michael Hanselmann | |
1546 | f2e07bb4 | Michael Hanselmann | return real_disk
|
1547 | f2e07bb4 | Michael Hanselmann | |
1548 | f2e07bb4 | Michael Hanselmann | |
1549 | 821d1bd1 | Iustin Pop | def BlockdevFind(disk): |
1550 | a8083063 | Iustin Pop | """Check if a device is activated.
|
1551 | a8083063 | Iustin Pop |
|
1552 | 5bbd3f7f | Michael Hanselmann | If it is, return information about the real device.
|
1553 | a8083063 | Iustin Pop |
|
1554 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
1555 | 10c2650b | Iustin Pop | @param disk: the disk to find
|
1556 | 96acbc09 | Michael Hanselmann | @rtype: None or objects.BlockDevStatus
|
1557 | 96acbc09 | Michael Hanselmann | @return: None if the disk cannot be found, otherwise a the current
|
1558 | 96acbc09 | Michael Hanselmann | information
|
1559 | a8083063 | Iustin Pop |
|
1560 | a8083063 | Iustin Pop | """
|
1561 | 23829f6f | Iustin Pop | try:
|
1562 | 23829f6f | Iustin Pop | rbd = _RecursiveFindBD(disk) |
1563 | 23829f6f | Iustin Pop | except errors.BlockDeviceError, err:
|
1564 | 2cc6781a | Iustin Pop | _Fail("Failed to find device: %s", err, exc=True) |
1565 | 96acbc09 | Michael Hanselmann | |
1566 | a8083063 | Iustin Pop | if rbd is None: |
1567 | c26a6bd2 | Iustin Pop | return None |
1568 | 96acbc09 | Michael Hanselmann | |
1569 | 96acbc09 | Michael Hanselmann | return rbd.GetSyncStatus()
|
1570 | a8083063 | Iustin Pop | |
1571 | a8083063 | Iustin Pop | |
1572 | 968a7623 | Iustin Pop | def BlockdevGetsize(disks): |
1573 | 968a7623 | Iustin Pop | """Computes the size of the given disks.
|
1574 | 968a7623 | Iustin Pop |
|
1575 | 968a7623 | Iustin Pop | If a disk is not found, returns None instead.
|
1576 | 968a7623 | Iustin Pop |
|
1577 | 968a7623 | Iustin Pop | @type disks: list of L{objects.Disk}
|
1578 | 968a7623 | Iustin Pop | @param disks: the list of disk to compute the size for
|
1579 | 968a7623 | Iustin Pop | @rtype: list
|
1580 | 968a7623 | Iustin Pop | @return: list with elements None if the disk cannot be found,
|
1581 | 968a7623 | Iustin Pop | otherwise the size
|
1582 | 968a7623 | Iustin Pop |
|
1583 | 968a7623 | Iustin Pop | """
|
1584 | 968a7623 | Iustin Pop | result = [] |
1585 | 968a7623 | Iustin Pop | for cf in disks: |
1586 | 968a7623 | Iustin Pop | try:
|
1587 | 968a7623 | Iustin Pop | rbd = _RecursiveFindBD(cf) |
1588 | 1122eb25 | Iustin Pop | except errors.BlockDeviceError:
|
1589 | 968a7623 | Iustin Pop | result.append(None)
|
1590 | 968a7623 | Iustin Pop | continue
|
1591 | 968a7623 | Iustin Pop | if rbd is None: |
1592 | 968a7623 | Iustin Pop | result.append(None)
|
1593 | 968a7623 | Iustin Pop | else:
|
1594 | 968a7623 | Iustin Pop | result.append(rbd.GetActualSize()) |
1595 | 968a7623 | Iustin Pop | return result
|
1596 | 968a7623 | Iustin Pop | |
1597 | 968a7623 | Iustin Pop | |
1598 | 858f3d18 | Iustin Pop | def BlockdevExport(disk, dest_node, dest_path, cluster_name): |
1599 | 858f3d18 | Iustin Pop | """Export a block device to a remote node.
|
1600 | 858f3d18 | Iustin Pop |
|
1601 | 858f3d18 | Iustin Pop | @type disk: L{objects.Disk}
|
1602 | 858f3d18 | Iustin Pop | @param disk: the description of the disk to export
|
1603 | 858f3d18 | Iustin Pop | @type dest_node: str
|
1604 | 858f3d18 | Iustin Pop | @param dest_node: the destination node to export to
|
1605 | 858f3d18 | Iustin Pop | @type dest_path: str
|
1606 | 858f3d18 | Iustin Pop | @param dest_path: the destination path on the target node
|
1607 | 858f3d18 | Iustin Pop | @type cluster_name: str
|
1608 | 858f3d18 | Iustin Pop | @param cluster_name: the cluster name, needed for SSH hostalias
|
1609 | 858f3d18 | Iustin Pop | @rtype: None
|
1610 | 858f3d18 | Iustin Pop |
|
1611 | 858f3d18 | Iustin Pop | """
|
1612 | f2e07bb4 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
1613 | 858f3d18 | Iustin Pop | |
1614 | 858f3d18 | Iustin Pop | # the block size on the read dd is 1MiB to match our units
|
1615 | 858f3d18 | Iustin Pop | expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
|
1616 | 858f3d18 | Iustin Pop | "dd if=%s bs=1048576 count=%s",
|
1617 | 858f3d18 | Iustin Pop | real_disk.dev_path, str(disk.size))
|
1618 | 858f3d18 | Iustin Pop | |
1619 | 858f3d18 | Iustin Pop | # we set here a smaller block size as, due to ssh buffering, more
|
1620 | 858f3d18 | Iustin Pop | # than 64-128k will mostly ignored; we use nocreat to fail if the
|
1621 | 858f3d18 | Iustin Pop | # device is not already there or we pass a wrong path; we use
|
1622 | 858f3d18 | Iustin Pop | # notrunc to no attempt truncate on an LV device; we use oflag=dsync
|
1623 | 858f3d18 | Iustin Pop | # to not buffer too much memory; this means that at best, we flush
|
1624 | 858f3d18 | Iustin Pop | # every 64k, which will not be very fast
|
1625 | 858f3d18 | Iustin Pop | destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
|
1626 | 858f3d18 | Iustin Pop | " oflag=dsync", dest_path)
|
1627 | 858f3d18 | Iustin Pop | |
1628 | 858f3d18 | Iustin Pop | remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node, |
1629 | 858f3d18 | Iustin Pop | constants.GANETI_RUNAS, |
1630 | 858f3d18 | Iustin Pop | destcmd) |
1631 | 858f3d18 | Iustin Pop | |
1632 | 858f3d18 | Iustin Pop | # all commands have been checked, so we're safe to combine them
|
1633 | 858f3d18 | Iustin Pop | command = '|'.join([expcmd, utils.ShellQuoteArgs(remotecmd)])
|
1634 | 858f3d18 | Iustin Pop | |
1635 | 858f3d18 | Iustin Pop | result = utils.RunCmd(["bash", "-c", command]) |
1636 | 858f3d18 | Iustin Pop | |
1637 | 858f3d18 | Iustin Pop | if result.failed:
|
1638 | 858f3d18 | Iustin Pop | _Fail("Disk copy command '%s' returned error: %s"
|
1639 | 858f3d18 | Iustin Pop | " output: %s", command, result.fail_reason, result.output)
|
1640 | 858f3d18 | Iustin Pop | |
1641 | 858f3d18 | Iustin Pop | |
1642 | a8083063 | Iustin Pop | def UploadFile(file_name, data, mode, uid, gid, atime, mtime): |
1643 | a8083063 | Iustin Pop | """Write a file to the filesystem.
|
1644 | a8083063 | Iustin Pop |
|
1645 | a8083063 | Iustin Pop | This allows the master to overwrite(!) a file. It will only perform
|
1646 | a8083063 | Iustin Pop | the operation if the file belongs to a list of configuration files.
|
1647 | a8083063 | Iustin Pop |
|
1648 | 10c2650b | Iustin Pop | @type file_name: str
|
1649 | 10c2650b | Iustin Pop | @param file_name: the target file name
|
1650 | 10c2650b | Iustin Pop | @type data: str
|
1651 | 10c2650b | Iustin Pop | @param data: the new contents of the file
|
1652 | 10c2650b | Iustin Pop | @type mode: int
|
1653 | 10c2650b | Iustin Pop | @param mode: the mode to give the file (can be None)
|
1654 | 10c2650b | Iustin Pop | @type uid: int
|
1655 | 10c2650b | Iustin Pop | @param uid: the owner of the file (can be -1 for default)
|
1656 | 10c2650b | Iustin Pop | @type gid: int
|
1657 | 10c2650b | Iustin Pop | @param gid: the group of the file (can be -1 for default)
|
1658 | 10c2650b | Iustin Pop | @type atime: float
|
1659 | 10c2650b | Iustin Pop | @param atime: the atime to set on the file (can be None)
|
1660 | 10c2650b | Iustin Pop | @type mtime: float
|
1661 | 10c2650b | Iustin Pop | @param mtime: the mtime to set on the file (can be None)
|
1662 | c26a6bd2 | Iustin Pop | @rtype: None
|
1663 | 10c2650b | Iustin Pop |
|
1664 | a8083063 | Iustin Pop | """
|
1665 | a8083063 | Iustin Pop | if not os.path.isabs(file_name): |
1666 | 2cc6781a | Iustin Pop | _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
|
1667 | a8083063 | Iustin Pop | |
1668 | 360b0dc2 | Iustin Pop | if file_name not in _ALLOWED_UPLOAD_FILES: |
1669 | 2cc6781a | Iustin Pop | _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
|
1670 | 2cc6781a | Iustin Pop | file_name) |
1671 | a8083063 | Iustin Pop | |
1672 | 12bce260 | Michael Hanselmann | raw_data = _Decompress(data) |
1673 | 12bce260 | Michael Hanselmann | |
1674 | 12bce260 | Michael Hanselmann | utils.WriteFile(file_name, data=raw_data, mode=mode, uid=uid, gid=gid, |
1675 | 41a57aab | Michael Hanselmann | atime=atime, mtime=mtime) |
1676 | a8083063 | Iustin Pop | |
1677 | 386b57af | Iustin Pop | |
1678 | 03d1dba2 | Michael Hanselmann | def WriteSsconfFiles(values): |
1679 | 89b14f05 | Iustin Pop | """Update all ssconf files.
|
1680 | 89b14f05 | Iustin Pop |
|
1681 | 89b14f05 | Iustin Pop | Wrapper around the SimpleStore.WriteFiles.
|
1682 | 89b14f05 | Iustin Pop |
|
1683 | 89b14f05 | Iustin Pop | """
|
1684 | 89b14f05 | Iustin Pop | ssconf.SimpleStore().WriteFiles(values) |
1685 | 6ddc95ec | Michael Hanselmann | |
1686 | 6ddc95ec | Michael Hanselmann | |
1687 | a8083063 | Iustin Pop | def _ErrnoOrStr(err): |
1688 | a8083063 | Iustin Pop | """Format an EnvironmentError exception.
|
1689 | a8083063 | Iustin Pop |
|
1690 | 10c2650b | Iustin Pop | If the L{err} argument has an errno attribute, it will be looked up
|
1691 | 10c2650b | Iustin Pop | and converted into a textual C{E...} description. Otherwise the
|
1692 | 10c2650b | Iustin Pop | string representation of the error will be returned.
|
1693 | 10c2650b | Iustin Pop |
|
1694 | 10c2650b | Iustin Pop | @type err: L{EnvironmentError}
|
1695 | 10c2650b | Iustin Pop | @param err: the exception to format
|
1696 | a8083063 | Iustin Pop |
|
1697 | a8083063 | Iustin Pop | """
|
1698 | a8083063 | Iustin Pop | if hasattr(err, 'errno'): |
1699 | a8083063 | Iustin Pop | detail = errno.errorcode[err.errno] |
1700 | a8083063 | Iustin Pop | else:
|
1701 | a8083063 | Iustin Pop | detail = str(err)
|
1702 | a8083063 | Iustin Pop | return detail
|
1703 | a8083063 | Iustin Pop | |
1704 | 5d0fe286 | Iustin Pop | |
1705 | c19f9810 | Iustin Pop | def _OSOndiskAPIVersion(os_dir): |
1706 | 2f8598a5 | Alexander Schreiber | """Compute and return the API version of a given OS.
|
1707 | a8083063 | Iustin Pop |
|
1708 | c19f9810 | Iustin Pop | This function will try to read the API version of the OS residing in
|
1709 | c19f9810 | Iustin Pop | the 'os_dir' directory.
|
1710 | 7c3d51d4 | Guido Trotter |
|
1711 | 10c2650b | Iustin Pop | @type os_dir: str
|
1712 | c19f9810 | Iustin Pop | @param os_dir: the directory in which we should look for the OS
|
1713 | 8e70b181 | Iustin Pop | @rtype: tuple
|
1714 | 8e70b181 | Iustin Pop | @return: tuple (status, data) with status denoting the validity and
|
1715 | 8e70b181 | Iustin Pop | data holding either the vaid versions or an error message
|
1716 | a8083063 | Iustin Pop |
|
1717 | a8083063 | Iustin Pop | """
|
1718 | e02b9114 | Iustin Pop | api_file = utils.PathJoin(os_dir, constants.OS_API_FILE) |
1719 | a8083063 | Iustin Pop | |
1720 | a8083063 | Iustin Pop | try:
|
1721 | a8083063 | Iustin Pop | st = os.stat(api_file) |
1722 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1723 | b6b45e0d | Guido Trotter | return False, ("Required file '%s' not found under path %s: %s" % |
1724 | b6b45e0d | Guido Trotter | (constants.OS_API_FILE, os_dir, _ErrnoOrStr(err))) |
1725 | a8083063 | Iustin Pop | |
1726 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
1727 | b6b45e0d | Guido Trotter | return False, ("File '%s' in %s is not a regular file" % |
1728 | b6b45e0d | Guido Trotter | (constants.OS_API_FILE, os_dir)) |
1729 | a8083063 | Iustin Pop | |
1730 | a8083063 | Iustin Pop | try:
|
1731 | 3374afa9 | Guido Trotter | api_versions = utils.ReadFile(api_file).splitlines() |
1732 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1733 | 255dcebd | Iustin Pop | return False, ("Error while reading the API version file at %s: %s" % |
1734 | 255dcebd | Iustin Pop | (api_file, _ErrnoOrStr(err))) |
1735 | a8083063 | Iustin Pop | |
1736 | a8083063 | Iustin Pop | try:
|
1737 | 63b9b186 | Guido Trotter | api_versions = [int(version.strip()) for version in api_versions] |
1738 | a8083063 | Iustin Pop | except (TypeError, ValueError), err: |
1739 | 255dcebd | Iustin Pop | return False, ("API version(s) can't be converted to integer: %s" % |
1740 | 255dcebd | Iustin Pop | str(err))
|
1741 | a8083063 | Iustin Pop | |
1742 | 255dcebd | Iustin Pop | return True, api_versions |
1743 | a8083063 | Iustin Pop | |
1744 | 386b57af | Iustin Pop | |
1745 | 7c3d51d4 | Guido Trotter | def DiagnoseOS(top_dirs=None): |
1746 | a8083063 | Iustin Pop | """Compute the validity for all OSes.
|
1747 | a8083063 | Iustin Pop |
|
1748 | 10c2650b | Iustin Pop | @type top_dirs: list
|
1749 | 10c2650b | Iustin Pop | @param top_dirs: the list of directories in which to
|
1750 | 10c2650b | Iustin Pop | search (if not given defaults to
|
1751 | 10c2650b | Iustin Pop | L{constants.OS_SEARCH_PATH})
|
1752 | 10c2650b | Iustin Pop | @rtype: list of L{objects.OS}
|
1753 | bad78e66 | Iustin Pop | @return: a list of tuples (name, path, status, diagnose, variants,
|
1754 | bad78e66 | Iustin Pop | parameters, api_version) for all (potential) OSes under all
|
1755 | bad78e66 | Iustin Pop | search paths, where:
|
1756 | 255dcebd | Iustin Pop | - name is the (potential) OS name
|
1757 | 255dcebd | Iustin Pop | - path is the full path to the OS
|
1758 | 255dcebd | Iustin Pop | - status True/False is the validity of the OS
|
1759 | 255dcebd | Iustin Pop | - diagnose is the error message for an invalid OS, otherwise empty
|
1760 | ba00557a | Guido Trotter | - variants is a list of supported OS variants, if any
|
1761 | c7d04a6b | Iustin Pop | - parameters is a list of (name, help) parameters, if any
|
1762 | bad78e66 | Iustin Pop | - api_version is a list of support OS API versions
|
1763 | a8083063 | Iustin Pop |
|
1764 | a8083063 | Iustin Pop | """
|
1765 | 7c3d51d4 | Guido Trotter | if top_dirs is None: |
1766 | 7c3d51d4 | Guido Trotter | top_dirs = constants.OS_SEARCH_PATH |
1767 | a8083063 | Iustin Pop | |
1768 | a8083063 | Iustin Pop | result = [] |
1769 | 65fe4693 | Iustin Pop | for dir_name in top_dirs: |
1770 | 65fe4693 | Iustin Pop | if os.path.isdir(dir_name):
|
1771 | 7c3d51d4 | Guido Trotter | try:
|
1772 | 65fe4693 | Iustin Pop | f_names = utils.ListVisibleFiles(dir_name) |
1773 | 7c3d51d4 | Guido Trotter | except EnvironmentError, err: |
1774 | 29921401 | Iustin Pop | logging.exception("Can't list the OS directory %s: %s", dir_name, err)
|
1775 | 7c3d51d4 | Guido Trotter | break
|
1776 | 7c3d51d4 | Guido Trotter | for name in f_names: |
1777 | e02b9114 | Iustin Pop | os_path = utils.PathJoin(dir_name, name) |
1778 | 255dcebd | Iustin Pop | status, os_inst = _TryOSFromDisk(name, base_dir=dir_name) |
1779 | 255dcebd | Iustin Pop | if status:
|
1780 | 255dcebd | Iustin Pop | diagnose = ""
|
1781 | ba00557a | Guido Trotter | variants = os_inst.supported_variants |
1782 | c7d04a6b | Iustin Pop | parameters = os_inst.supported_parameters |
1783 | bad78e66 | Iustin Pop | api_versions = os_inst.api_versions |
1784 | 255dcebd | Iustin Pop | else:
|
1785 | 255dcebd | Iustin Pop | diagnose = os_inst |
1786 | bad78e66 | Iustin Pop | variants = parameters = api_versions = [] |
1787 | bad78e66 | Iustin Pop | result.append((name, os_path, status, diagnose, variants, |
1788 | bad78e66 | Iustin Pop | parameters, api_versions)) |
1789 | a8083063 | Iustin Pop | |
1790 | c26a6bd2 | Iustin Pop | return result
|
1791 | a8083063 | Iustin Pop | |
1792 | a8083063 | Iustin Pop | |
1793 | 255dcebd | Iustin Pop | def _TryOSFromDisk(name, base_dir=None): |
1794 | a8083063 | Iustin Pop | """Create an OS instance from disk.
|
1795 | a8083063 | Iustin Pop |
|
1796 | a8083063 | Iustin Pop | This function will return an OS instance if the given name is a
|
1797 | 8e70b181 | Iustin Pop | valid OS name.
|
1798 | a8083063 | Iustin Pop |
|
1799 | 8ee4dc80 | Guido Trotter | @type base_dir: string
|
1800 | 8ee4dc80 | Guido Trotter | @keyword base_dir: Base directory containing OS installations.
|
1801 | 8ee4dc80 | Guido Trotter | Defaults to a search in all the OS_SEARCH_PATH dirs.
|
1802 | 255dcebd | Iustin Pop | @rtype: tuple
|
1803 | 255dcebd | Iustin Pop | @return: success and either the OS instance if we find a valid one,
|
1804 | 255dcebd | Iustin Pop | or error message
|
1805 | 7c3d51d4 | Guido Trotter |
|
1806 | a8083063 | Iustin Pop | """
|
1807 | 56bcd3f4 | Guido Trotter | if base_dir is None: |
1808 | 57c177af | Iustin Pop | os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir) |
1809 | c34c0cfd | Iustin Pop | else:
|
1810 | f95c81bf | Iustin Pop | os_dir = utils.FindFile(name, [base_dir], os.path.isdir) |
1811 | f95c81bf | Iustin Pop | |
1812 | f95c81bf | Iustin Pop | if os_dir is None: |
1813 | 5c0433d6 | Iustin Pop | return False, "Directory for OS %s not found in search path" % name |
1814 | a8083063 | Iustin Pop | |
1815 | c19f9810 | Iustin Pop | status, api_versions = _OSOndiskAPIVersion(os_dir) |
1816 | 255dcebd | Iustin Pop | if not status: |
1817 | 255dcebd | Iustin Pop | # push the error up
|
1818 | 255dcebd | Iustin Pop | return status, api_versions
|
1819 | a8083063 | Iustin Pop | |
1820 | d1a7d66f | Guido Trotter | if not constants.OS_API_VERSIONS.intersection(api_versions): |
1821 | 255dcebd | Iustin Pop | return False, ("API version mismatch for path '%s': found %s, want %s." % |
1822 | d1a7d66f | Guido Trotter | (os_dir, api_versions, constants.OS_API_VERSIONS)) |
1823 | a8083063 | Iustin Pop | |
1824 | 41ba4061 | Guido Trotter | # OS Files dictionary, we will populate it with the absolute path names
|
1825 | 41ba4061 | Guido Trotter | os_files = dict.fromkeys(constants.OS_SCRIPTS)
|
1826 | a8083063 | Iustin Pop | |
1827 | 95075fba | Guido Trotter | if max(api_versions) >= constants.OS_API_V15: |
1828 | 95075fba | Guido Trotter | os_files[constants.OS_VARIANTS_FILE] = ''
|
1829 | 95075fba | Guido Trotter | |
1830 | c7d04a6b | Iustin Pop | if max(api_versions) >= constants.OS_API_V20: |
1831 | c7d04a6b | Iustin Pop | os_files[constants.OS_PARAMETERS_FILE] = ''
|
1832 | c7d04a6b | Iustin Pop | else:
|
1833 | c7d04a6b | Iustin Pop | del os_files[constants.OS_SCRIPT_VERIFY]
|
1834 | c7d04a6b | Iustin Pop | |
1835 | ea79fc15 | Michael Hanselmann | for filename in os_files: |
1836 | e02b9114 | Iustin Pop | os_files[filename] = utils.PathJoin(os_dir, filename) |
1837 | a8083063 | Iustin Pop | |
1838 | a8083063 | Iustin Pop | try:
|
1839 | ea79fc15 | Michael Hanselmann | st = os.stat(os_files[filename]) |
1840 | a8083063 | Iustin Pop | except EnvironmentError, err: |
1841 | 41ba4061 | Guido Trotter | return False, ("File '%s' under path '%s' is missing (%s)" % |
1842 | ea79fc15 | Michael Hanselmann | (filename, os_dir, _ErrnoOrStr(err))) |
1843 | a8083063 | Iustin Pop | |
1844 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
1845 | 41ba4061 | Guido Trotter | return False, ("File '%s' under path '%s' is not a regular file" % |
1846 | ea79fc15 | Michael Hanselmann | (filename, os_dir)) |
1847 | 255dcebd | Iustin Pop | |
1848 | ea79fc15 | Michael Hanselmann | if filename in constants.OS_SCRIPTS: |
1849 | 0757c107 | Guido Trotter | if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
|
1850 | 0757c107 | Guido Trotter | return False, ("File '%s' under path '%s' is not executable" % |
1851 | ea79fc15 | Michael Hanselmann | (filename, os_dir)) |
1852 | 0757c107 | Guido Trotter | |
1853 | 845da3e8 | Iustin Pop | variants = [] |
1854 | 95075fba | Guido Trotter | if constants.OS_VARIANTS_FILE in os_files: |
1855 | 95075fba | Guido Trotter | variants_file = os_files[constants.OS_VARIANTS_FILE] |
1856 | 95075fba | Guido Trotter | try:
|
1857 | 95075fba | Guido Trotter | variants = utils.ReadFile(variants_file).splitlines() |
1858 | 95075fba | Guido Trotter | except EnvironmentError, err: |
1859 | 95075fba | Guido Trotter | return False, ("Error while reading the OS variants file at %s: %s" % |
1860 | 95075fba | Guido Trotter | (variants_file, _ErrnoOrStr(err))) |
1861 | 95075fba | Guido Trotter | if not variants: |
1862 | 95075fba | Guido Trotter | return False, ("No supported os variant found") |
1863 | 0757c107 | Guido Trotter | |
1864 | c7d04a6b | Iustin Pop | parameters = [] |
1865 | c7d04a6b | Iustin Pop | if constants.OS_PARAMETERS_FILE in os_files: |
1866 | c7d04a6b | Iustin Pop | parameters_file = os_files[constants.OS_PARAMETERS_FILE] |
1867 | c7d04a6b | Iustin Pop | try:
|
1868 | c7d04a6b | Iustin Pop | parameters = utils.ReadFile(parameters_file).splitlines() |
1869 | c7d04a6b | Iustin Pop | except EnvironmentError, err: |
1870 | c7d04a6b | Iustin Pop | return False, ("Error while reading the OS parameters file at %s: %s" % |
1871 | c7d04a6b | Iustin Pop | (parameters_file, _ErrnoOrStr(err))) |
1872 | c7d04a6b | Iustin Pop | parameters = [v.split(None, 1) for v in parameters] |
1873 | c7d04a6b | Iustin Pop | |
1874 | 8e70b181 | Iustin Pop | os_obj = objects.OS(name=name, path=os_dir, |
1875 | 41ba4061 | Guido Trotter | create_script=os_files[constants.OS_SCRIPT_CREATE], |
1876 | 41ba4061 | Guido Trotter | export_script=os_files[constants.OS_SCRIPT_EXPORT], |
1877 | 41ba4061 | Guido Trotter | import_script=os_files[constants.OS_SCRIPT_IMPORT], |
1878 | 41ba4061 | Guido Trotter | rename_script=os_files[constants.OS_SCRIPT_RENAME], |
1879 | 40684c3a | Iustin Pop | verify_script=os_files.get(constants.OS_SCRIPT_VERIFY, |
1880 | 40684c3a | Iustin Pop | None),
|
1881 | 95075fba | Guido Trotter | supported_variants=variants, |
1882 | c7d04a6b | Iustin Pop | supported_parameters=parameters, |
1883 | 255dcebd | Iustin Pop | api_versions=api_versions) |
1884 | 255dcebd | Iustin Pop | return True, os_obj |
1885 | 255dcebd | Iustin Pop | |
1886 | 255dcebd | Iustin Pop | |
1887 | 255dcebd | Iustin Pop | def OSFromDisk(name, base_dir=None): |
1888 | 255dcebd | Iustin Pop | """Create an OS instance from disk.
|
1889 | 255dcebd | Iustin Pop |
|
1890 | 255dcebd | Iustin Pop | This function will return an OS instance if the given name is a
|
1891 | 255dcebd | Iustin Pop | valid OS name. Otherwise, it will raise an appropriate
|
1892 | 255dcebd | Iustin Pop | L{RPCFail} exception, detailing why this is not a valid OS.
|
1893 | 255dcebd | Iustin Pop |
|
1894 | 255dcebd | Iustin Pop | This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
|
1895 | 255dcebd | Iustin Pop | an exception but returns true/false status data.
|
1896 | 255dcebd | Iustin Pop |
|
1897 | 255dcebd | Iustin Pop | @type base_dir: string
|
1898 | 255dcebd | Iustin Pop | @keyword base_dir: Base directory containing OS installations.
|
1899 | 255dcebd | Iustin Pop | Defaults to a search in all the OS_SEARCH_PATH dirs.
|
1900 | 255dcebd | Iustin Pop | @rtype: L{objects.OS}
|
1901 | 255dcebd | Iustin Pop | @return: the OS instance if we find a valid one
|
1902 | 255dcebd | Iustin Pop | @raise RPCFail: if we don't find a valid OS
|
1903 | 255dcebd | Iustin Pop |
|
1904 | 255dcebd | Iustin Pop | """
|
1905 | 69b99987 | Michael Hanselmann | name_only = name.split("+", 1)[0] |
1906 | 6ee7102a | Guido Trotter | status, payload = _TryOSFromDisk(name_only, base_dir) |
1907 | 255dcebd | Iustin Pop | |
1908 | 255dcebd | Iustin Pop | if not status: |
1909 | 255dcebd | Iustin Pop | _Fail(payload) |
1910 | a8083063 | Iustin Pop | |
1911 | 255dcebd | Iustin Pop | return payload
|
1912 | a8083063 | Iustin Pop | |
1913 | a8083063 | Iustin Pop | |
1914 | a025e535 | Vitaly Kuznetsov | def OSCoreEnv(os_name, inst_os, os_params, debug=0): |
1915 | efaa9b06 | Iustin Pop | """Calculate the basic environment for an os script.
|
1916 | 2266edb2 | Guido Trotter |
|
1917 | a025e535 | Vitaly Kuznetsov | @type os_name: str
|
1918 | a025e535 | Vitaly Kuznetsov | @param os_name: full operating system name (including variant)
|
1919 | 099c52ad | Iustin Pop | @type inst_os: L{objects.OS}
|
1920 | 099c52ad | Iustin Pop | @param inst_os: operating system for which the environment is being built
|
1921 | 1bdcbbab | Iustin Pop | @type os_params: dict
|
1922 | 1bdcbbab | Iustin Pop | @param os_params: the OS parameters
|
1923 | 2266edb2 | Guido Trotter | @type debug: integer
|
1924 | 10c2650b | Iustin Pop | @param debug: debug level (0 or 1, for OS Api 10)
|
1925 | 2266edb2 | Guido Trotter | @rtype: dict
|
1926 | 2266edb2 | Guido Trotter | @return: dict of environment variables
|
1927 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: if the block device
|
1928 | 10c2650b | Iustin Pop | cannot be found
|
1929 | 2266edb2 | Guido Trotter |
|
1930 | 2266edb2 | Guido Trotter | """
|
1931 | 2266edb2 | Guido Trotter | result = {} |
1932 | 099c52ad | Iustin Pop | api_version = \ |
1933 | 099c52ad | Iustin Pop | max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
|
1934 | d1a7d66f | Guido Trotter | result['OS_API_VERSION'] = '%d' % api_version |
1935 | efaa9b06 | Iustin Pop | result['OS_NAME'] = inst_os.name
|
1936 | 2266edb2 | Guido Trotter | result['DEBUG_LEVEL'] = '%d' % debug |
1937 | efaa9b06 | Iustin Pop | |
1938 | efaa9b06 | Iustin Pop | # OS variants
|
1939 | f11280b5 | Guido Trotter | if api_version >= constants.OS_API_V15:
|
1940 | f11280b5 | Guido Trotter | try:
|
1941 | a025e535 | Vitaly Kuznetsov | variant = os_name.split('+', 1)[1] |
1942 | f11280b5 | Guido Trotter | except IndexError: |
1943 | 099c52ad | Iustin Pop | variant = inst_os.supported_variants[0]
|
1944 | f11280b5 | Guido Trotter | result['OS_VARIANT'] = variant
|
1945 | efaa9b06 | Iustin Pop | |
1946 | 1bdcbbab | Iustin Pop | # OS params
|
1947 | 1bdcbbab | Iustin Pop | for pname, pvalue in os_params.items(): |
1948 | 1bdcbbab | Iustin Pop | result['OSP_%s' % pname.upper()] = pvalue
|
1949 | 1bdcbbab | Iustin Pop | |
1950 | efaa9b06 | Iustin Pop | return result
|
1951 | efaa9b06 | Iustin Pop | |
1952 | efaa9b06 | Iustin Pop | |
1953 | efaa9b06 | Iustin Pop | def OSEnvironment(instance, inst_os, debug=0): |
1954 | efaa9b06 | Iustin Pop | """Calculate the environment for an os script.
|
1955 | efaa9b06 | Iustin Pop |
|
1956 | efaa9b06 | Iustin Pop | @type instance: L{objects.Instance}
|
1957 | efaa9b06 | Iustin Pop | @param instance: target instance for the os script run
|
1958 | efaa9b06 | Iustin Pop | @type inst_os: L{objects.OS}
|
1959 | efaa9b06 | Iustin Pop | @param inst_os: operating system for which the environment is being built
|
1960 | efaa9b06 | Iustin Pop | @type debug: integer
|
1961 | efaa9b06 | Iustin Pop | @param debug: debug level (0 or 1, for OS Api 10)
|
1962 | efaa9b06 | Iustin Pop | @rtype: dict
|
1963 | efaa9b06 | Iustin Pop | @return: dict of environment variables
|
1964 | efaa9b06 | Iustin Pop | @raise errors.BlockDeviceError: if the block device
|
1965 | efaa9b06 | Iustin Pop | cannot be found
|
1966 | efaa9b06 | Iustin Pop |
|
1967 | efaa9b06 | Iustin Pop | """
|
1968 | a025e535 | Vitaly Kuznetsov | result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug) |
1969 | efaa9b06 | Iustin Pop | |
1970 | efaa9b06 | Iustin Pop | result['INSTANCE_NAME'] = instance.name
|
1971 | efaa9b06 | Iustin Pop | result['INSTANCE_OS'] = instance.os
|
1972 | efaa9b06 | Iustin Pop | result['HYPERVISOR'] = instance.hypervisor
|
1973 | efaa9b06 | Iustin Pop | result['DISK_COUNT'] = '%d' % len(instance.disks) |
1974 | efaa9b06 | Iustin Pop | result['NIC_COUNT'] = '%d' % len(instance.nics) |
1975 | efaa9b06 | Iustin Pop | |
1976 | efaa9b06 | Iustin Pop | # Disks
|
1977 | 2266edb2 | Guido Trotter | for idx, disk in enumerate(instance.disks): |
1978 | f2e07bb4 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
1979 | 2266edb2 | Guido Trotter | result['DISK_%d_PATH' % idx] = real_disk.dev_path
|
1980 | 15552312 | Iustin Pop | result['DISK_%d_ACCESS' % idx] = disk.mode
|
1981 | 2266edb2 | Guido Trotter | if constants.HV_DISK_TYPE in instance.hvparams: |
1982 | 2266edb2 | Guido Trotter | result['DISK_%d_FRONTEND_TYPE' % idx] = \
|
1983 | 2266edb2 | Guido Trotter | instance.hvparams[constants.HV_DISK_TYPE] |
1984 | 2266edb2 | Guido Trotter | if disk.dev_type in constants.LDS_BLOCK: |
1985 | 2266edb2 | Guido Trotter | result['DISK_%d_BACKEND_TYPE' % idx] = 'block' |
1986 | 2266edb2 | Guido Trotter | elif disk.dev_type == constants.LD_FILE:
|
1987 | 2266edb2 | Guido Trotter | result['DISK_%d_BACKEND_TYPE' % idx] = \
|
1988 | 2266edb2 | Guido Trotter | 'file:%s' % disk.physical_id[0] |
1989 | efaa9b06 | Iustin Pop | |
1990 | efaa9b06 | Iustin Pop | # NICs
|
1991 | 2266edb2 | Guido Trotter | for idx, nic in enumerate(instance.nics): |
1992 | 2266edb2 | Guido Trotter | result['NIC_%d_MAC' % idx] = nic.mac
|
1993 | 2266edb2 | Guido Trotter | if nic.ip:
|
1994 | 2266edb2 | Guido Trotter | result['NIC_%d_IP' % idx] = nic.ip
|
1995 | 1ba9227f | Guido Trotter | result['NIC_%d_MODE' % idx] = nic.nicparams[constants.NIC_MODE]
|
1996 | 1ba9227f | Guido Trotter | if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
1997 | 1ba9227f | Guido Trotter | result['NIC_%d_BRIDGE' % idx] = nic.nicparams[constants.NIC_LINK]
|
1998 | 1ba9227f | Guido Trotter | if nic.nicparams[constants.NIC_LINK]:
|
1999 | 1ba9227f | Guido Trotter | result['NIC_%d_LINK' % idx] = nic.nicparams[constants.NIC_LINK]
|
2000 | 2266edb2 | Guido Trotter | if constants.HV_NIC_TYPE in instance.hvparams: |
2001 | 2266edb2 | Guido Trotter | result['NIC_%d_FRONTEND_TYPE' % idx] = \
|
2002 | 2266edb2 | Guido Trotter | instance.hvparams[constants.HV_NIC_TYPE] |
2003 | 2266edb2 | Guido Trotter | |
2004 | efaa9b06 | Iustin Pop | # HV/BE params
|
2005 | 67fc3042 | Iustin Pop | for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]: |
2006 | 67fc3042 | Iustin Pop | for key, value in source.items(): |
2007 | 030b218a | Iustin Pop | result["INSTANCE_%s_%s" % (kind, key)] = str(value) |
2008 | 67fc3042 | Iustin Pop | |
2009 | 2266edb2 | Guido Trotter | return result
|
2010 | a8083063 | Iustin Pop | |
2011 | f2e07bb4 | Michael Hanselmann | |
2012 | 821d1bd1 | Iustin Pop | def BlockdevGrow(disk, amount): |
2013 | 594609c0 | Iustin Pop | """Grow a stack of block devices.
|
2014 | 594609c0 | Iustin Pop |
|
2015 | 594609c0 | Iustin Pop | This function is called recursively, with the childrens being the
|
2016 | 10c2650b | Iustin Pop | first ones to resize.
|
2017 | 594609c0 | Iustin Pop |
|
2018 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
2019 | 10c2650b | Iustin Pop | @param disk: the disk to be grown
|
2020 | 10c2650b | Iustin Pop | @rtype: (status, result)
|
2021 | 10c2650b | Iustin Pop | @return: a tuple with the status of the operation
|
2022 | 10c2650b | Iustin Pop | (True/False), and the errors message if status
|
2023 | 10c2650b | Iustin Pop | is False
|
2024 | 594609c0 | Iustin Pop |
|
2025 | 594609c0 | Iustin Pop | """
|
2026 | 594609c0 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2027 | 594609c0 | Iustin Pop | if r_dev is None: |
2028 | afdc3985 | Iustin Pop | _Fail("Cannot find block device %s", disk)
|
2029 | 594609c0 | Iustin Pop | |
2030 | 594609c0 | Iustin Pop | try:
|
2031 | 594609c0 | Iustin Pop | r_dev.Grow(amount) |
2032 | 594609c0 | Iustin Pop | except errors.BlockDeviceError, err:
|
2033 | 2cc6781a | Iustin Pop | _Fail("Failed to grow block device: %s", err, exc=True) |
2034 | 594609c0 | Iustin Pop | |
2035 | 594609c0 | Iustin Pop | |
2036 | 821d1bd1 | Iustin Pop | def BlockdevSnapshot(disk): |
2037 | a8083063 | Iustin Pop | """Create a snapshot copy of a block device.
|
2038 | a8083063 | Iustin Pop |
|
2039 | a8083063 | Iustin Pop | This function is called recursively, and the snapshot is actually created
|
2040 | a8083063 | Iustin Pop | just for the leaf lvm backend device.
|
2041 | a8083063 | Iustin Pop |
|
2042 | e9e9263d | Guido Trotter | @type disk: L{objects.Disk}
|
2043 | e9e9263d | Guido Trotter | @param disk: the disk to be snapshotted
|
2044 | e9e9263d | Guido Trotter | @rtype: string
|
2045 | e9e9263d | Guido Trotter | @return: snapshot disk path
|
2046 | a8083063 | Iustin Pop |
|
2047 | 098c0958 | Michael Hanselmann | """
|
2048 | 433c63aa | Iustin Pop | if disk.dev_type == constants.LD_DRBD8:
|
2049 | 433c63aa | Iustin Pop | if not disk.children: |
2050 | 433c63aa | Iustin Pop | _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
|
2051 | 433c63aa | Iustin Pop | disk.unique_id) |
2052 | 433c63aa | Iustin Pop | return BlockdevSnapshot(disk.children[0]) |
2053 | fe96220b | Iustin Pop | elif disk.dev_type == constants.LD_LV:
|
2054 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2055 | a8083063 | Iustin Pop | if r_dev is not None: |
2056 | 433c63aa | Iustin Pop | # FIXME: choose a saner value for the snapshot size
|
2057 | a8083063 | Iustin Pop | # let's stay on the safe side and ask for the full size, for now
|
2058 | c26a6bd2 | Iustin Pop | return r_dev.Snapshot(disk.size)
|
2059 | a8083063 | Iustin Pop | else:
|
2060 | 87812fd3 | Iustin Pop | _Fail("Cannot find block device %s", disk)
|
2061 | a8083063 | Iustin Pop | else:
|
2062 | 87812fd3 | Iustin Pop | _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
|
2063 | 87812fd3 | Iustin Pop | disk.unique_id, disk.dev_type) |
2064 | a8083063 | Iustin Pop | |
2065 | a8083063 | Iustin Pop | |
2066 | a8083063 | Iustin Pop | def FinalizeExport(instance, snap_disks): |
2067 | a8083063 | Iustin Pop | """Write out the export configuration information.
|
2068 | a8083063 | Iustin Pop |
|
2069 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
2070 | 10c2650b | Iustin Pop | @param instance: the instance which we export, used for
|
2071 | 10c2650b | Iustin Pop | saving configuration
|
2072 | 10c2650b | Iustin Pop | @type snap_disks: list of L{objects.Disk}
|
2073 | 10c2650b | Iustin Pop | @param snap_disks: list of snapshot block devices, which
|
2074 | 10c2650b | Iustin Pop | will be used to get the actual name of the dump file
|
2075 | a8083063 | Iustin Pop |
|
2076 | c26a6bd2 | Iustin Pop | @rtype: None
|
2077 | a8083063 | Iustin Pop |
|
2078 | 098c0958 | Michael Hanselmann | """
|
2079 | c4feafe8 | Iustin Pop | destdir = utils.PathJoin(constants.EXPORT_DIR, instance.name + ".new")
|
2080 | c4feafe8 | Iustin Pop | finaldestdir = utils.PathJoin(constants.EXPORT_DIR, instance.name) |
2081 | a8083063 | Iustin Pop | |
2082 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
2083 | a8083063 | Iustin Pop | |
2084 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_EXP) |
2085 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'version', '0') |
2086 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'timestamp', '%d' % int(time.time())) |
2087 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'source', instance.primary_node)
|
2088 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'os', instance.os)
|
2089 | a8083063 | Iustin Pop | config.set(constants.INISECT_EXP, 'compression', 'gzip') |
2090 | a8083063 | Iustin Pop | |
2091 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_INS) |
2092 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'name', instance.name)
|
2093 | 51de46bf | Iustin Pop | config.set(constants.INISECT_INS, 'memory', '%d' % |
2094 | 51de46bf | Iustin Pop | instance.beparams[constants.BE_MEMORY]) |
2095 | 51de46bf | Iustin Pop | config.set(constants.INISECT_INS, 'vcpus', '%d' % |
2096 | 51de46bf | Iustin Pop | instance.beparams[constants.BE_VCPUS]) |
2097 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'disk_template', instance.disk_template)
|
2098 | 3c8954ad | Iustin Pop | config.set(constants.INISECT_INS, 'hypervisor', instance.hypervisor)
|
2099 | 66f93869 | Manuel Franceschini | |
2100 | 95268cc3 | Iustin Pop | nic_total = 0
|
2101 | a8083063 | Iustin Pop | for nic_count, nic in enumerate(instance.nics): |
2102 | 95268cc3 | Iustin Pop | nic_total += 1
|
2103 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'nic%d_mac' %
|
2104 | a8083063 | Iustin Pop | nic_count, '%s' % nic.mac)
|
2105 | a8083063 | Iustin Pop | config.set(constants.INISECT_INS, 'nic%d_ip' % nic_count, '%s' % nic.ip) |
2106 | 6801eb5c | Iustin Pop | for param in constants.NICS_PARAMETER_TYPES: |
2107 | 6801eb5c | Iustin Pop | config.set(constants.INISECT_INS, 'nic%d_%s' % (nic_count, param),
|
2108 | 6801eb5c | Iustin Pop | '%s' % nic.nicparams.get(param, None)) |
2109 | a8083063 | Iustin Pop | # TODO: redundant: on load can read nics until it doesn't exist
|
2110 | 95268cc3 | Iustin Pop | config.set(constants.INISECT_INS, 'nic_count' , '%d' % nic_total) |
2111 | a8083063 | Iustin Pop | |
2112 | 726d7d68 | Iustin Pop | disk_total = 0
|
2113 | a8083063 | Iustin Pop | for disk_count, disk in enumerate(snap_disks): |
2114 | 19d7f90a | Guido Trotter | if disk:
|
2115 | 726d7d68 | Iustin Pop | disk_total += 1
|
2116 | 19d7f90a | Guido Trotter | config.set(constants.INISECT_INS, 'disk%d_ivname' % disk_count,
|
2117 | 19d7f90a | Guido Trotter | ('%s' % disk.iv_name))
|
2118 | 19d7f90a | Guido Trotter | config.set(constants.INISECT_INS, 'disk%d_dump' % disk_count,
|
2119 | 19d7f90a | Guido Trotter | ('%s' % disk.physical_id[1])) |
2120 | 19d7f90a | Guido Trotter | config.set(constants.INISECT_INS, 'disk%d_size' % disk_count,
|
2121 | 19d7f90a | Guido Trotter | ('%d' % disk.size))
|
2122 | a8083063 | Iustin Pop | |
2123 | 726d7d68 | Iustin Pop | config.set(constants.INISECT_INS, 'disk_count' , '%d' % disk_total) |
2124 | a8083063 | Iustin Pop | |
2125 | 3c8954ad | Iustin Pop | # New-style hypervisor/backend parameters
|
2126 | 3c8954ad | Iustin Pop | |
2127 | 3c8954ad | Iustin Pop | config.add_section(constants.INISECT_HYP) |
2128 | 3c8954ad | Iustin Pop | for name, value in instance.hvparams.items(): |
2129 | 3c8954ad | Iustin Pop | if name not in constants.HVC_GLOBALS: |
2130 | 3c8954ad | Iustin Pop | config.set(constants.INISECT_HYP, name, str(value))
|
2131 | 3c8954ad | Iustin Pop | |
2132 | 3c8954ad | Iustin Pop | config.add_section(constants.INISECT_BEP) |
2133 | 3c8954ad | Iustin Pop | for name, value in instance.beparams.items(): |
2134 | 3c8954ad | Iustin Pop | config.set(constants.INISECT_BEP, name, str(value))
|
2135 | 3c8954ad | Iustin Pop | |
2136 | 535b49cb | Iustin Pop | config.add_section(constants.INISECT_OSP) |
2137 | 535b49cb | Iustin Pop | for name, value in instance.osparams.items(): |
2138 | 535b49cb | Iustin Pop | config.set(constants.INISECT_OSP, name, str(value))
|
2139 | 535b49cb | Iustin Pop | |
2140 | c4feafe8 | Iustin Pop | utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE), |
2141 | 726d7d68 | Iustin Pop | data=config.Dumps()) |
2142 | 56569f4e | Michael Hanselmann | shutil.rmtree(finaldestdir, ignore_errors=True)
|
2143 | a8083063 | Iustin Pop | shutil.move(destdir, finaldestdir) |
2144 | a8083063 | Iustin Pop | |
2145 | a8083063 | Iustin Pop | |
2146 | a8083063 | Iustin Pop | def ExportInfo(dest): |
2147 | a8083063 | Iustin Pop | """Get export configuration information.
|
2148 | a8083063 | Iustin Pop |
|
2149 | 10c2650b | Iustin Pop | @type dest: str
|
2150 | 10c2650b | Iustin Pop | @param dest: directory containing the export
|
2151 | a8083063 | Iustin Pop |
|
2152 | 10c2650b | Iustin Pop | @rtype: L{objects.SerializableConfigParser}
|
2153 | 10c2650b | Iustin Pop | @return: a serializable config file containing the
|
2154 | 10c2650b | Iustin Pop | export info
|
2155 | a8083063 | Iustin Pop |
|
2156 | a8083063 | Iustin Pop | """
|
2157 | c4feafe8 | Iustin Pop | cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE) |
2158 | a8083063 | Iustin Pop | |
2159 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
2160 | a8083063 | Iustin Pop | config.read(cff) |
2161 | a8083063 | Iustin Pop | |
2162 | a8083063 | Iustin Pop | if (not config.has_section(constants.INISECT_EXP) or |
2163 | a8083063 | Iustin Pop | not config.has_section(constants.INISECT_INS)):
|
2164 | 3eccac06 | Iustin Pop | _Fail("Export info file doesn't have the required fields")
|
2165 | a8083063 | Iustin Pop | |
2166 | c26a6bd2 | Iustin Pop | return config.Dumps()
|
2167 | a8083063 | Iustin Pop | |
2168 | a8083063 | Iustin Pop | |
2169 | a8083063 | Iustin Pop | def ListExports(): |
2170 | a8083063 | Iustin Pop | """Return a list of exports currently available on this machine.
|
2171 | 098c0958 | Michael Hanselmann |
|
2172 | 10c2650b | Iustin Pop | @rtype: list
|
2173 | 10c2650b | Iustin Pop | @return: list of the exports
|
2174 | 10c2650b | Iustin Pop |
|
2175 | a8083063 | Iustin Pop | """
|
2176 | a8083063 | Iustin Pop | if os.path.isdir(constants.EXPORT_DIR):
|
2177 | b5b8309d | Guido Trotter | return sorted(utils.ListVisibleFiles(constants.EXPORT_DIR)) |
2178 | a8083063 | Iustin Pop | else:
|
2179 | afdc3985 | Iustin Pop | _Fail("No exports directory")
|
2180 | a8083063 | Iustin Pop | |
2181 | a8083063 | Iustin Pop | |
2182 | a8083063 | Iustin Pop | def RemoveExport(export): |
2183 | a8083063 | Iustin Pop | """Remove an existing export from the node.
|
2184 | a8083063 | Iustin Pop |
|
2185 | 10c2650b | Iustin Pop | @type export: str
|
2186 | 10c2650b | Iustin Pop | @param export: the name of the export to remove
|
2187 | c26a6bd2 | Iustin Pop | @rtype: None
|
2188 | a8083063 | Iustin Pop |
|
2189 | 098c0958 | Michael Hanselmann | """
|
2190 | c4feafe8 | Iustin Pop | target = utils.PathJoin(constants.EXPORT_DIR, export) |
2191 | a8083063 | Iustin Pop | |
2192 | 35fbcd11 | Iustin Pop | try:
|
2193 | 35fbcd11 | Iustin Pop | shutil.rmtree(target) |
2194 | 35fbcd11 | Iustin Pop | except EnvironmentError, err: |
2195 | 35fbcd11 | Iustin Pop | _Fail("Error while removing the export: %s", err, exc=True) |
2196 | a8083063 | Iustin Pop | |
2197 | a8083063 | Iustin Pop | |
2198 | 821d1bd1 | Iustin Pop | def BlockdevRename(devlist): |
2199 | f3e513ad | Iustin Pop | """Rename a list of block devices.
|
2200 | f3e513ad | Iustin Pop |
|
2201 | 10c2650b | Iustin Pop | @type devlist: list of tuples
|
2202 | 10c2650b | Iustin Pop | @param devlist: list of tuples of the form (disk,
|
2203 | 10c2650b | Iustin Pop | new_logical_id, new_physical_id); disk is an
|
2204 | 10c2650b | Iustin Pop | L{objects.Disk} object describing the current disk,
|
2205 | 10c2650b | Iustin Pop | and new logical_id/physical_id is the name we
|
2206 | 10c2650b | Iustin Pop | rename it to
|
2207 | 10c2650b | Iustin Pop | @rtype: boolean
|
2208 | 10c2650b | Iustin Pop | @return: True if all renames succeeded, False otherwise
|
2209 | f3e513ad | Iustin Pop |
|
2210 | f3e513ad | Iustin Pop | """
|
2211 | 6b5e3f70 | Iustin Pop | msgs = [] |
2212 | f3e513ad | Iustin Pop | result = True
|
2213 | f3e513ad | Iustin Pop | for disk, unique_id in devlist: |
2214 | f3e513ad | Iustin Pop | dev = _RecursiveFindBD(disk) |
2215 | f3e513ad | Iustin Pop | if dev is None: |
2216 | 6b5e3f70 | Iustin Pop | msgs.append("Can't find device %s in rename" % str(disk)) |
2217 | f3e513ad | Iustin Pop | result = False
|
2218 | f3e513ad | Iustin Pop | continue
|
2219 | f3e513ad | Iustin Pop | try:
|
2220 | 3f78eef2 | Iustin Pop | old_rpath = dev.dev_path |
2221 | f3e513ad | Iustin Pop | dev.Rename(unique_id) |
2222 | 3f78eef2 | Iustin Pop | new_rpath = dev.dev_path |
2223 | 3f78eef2 | Iustin Pop | if old_rpath != new_rpath:
|
2224 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(old_rpath) |
2225 | 3f78eef2 | Iustin Pop | # FIXME: we should add the new cache information here, like:
|
2226 | 3f78eef2 | Iustin Pop | # DevCacheManager.UpdateCache(new_rpath, owner, ...)
|
2227 | 3f78eef2 | Iustin Pop | # but we don't have the owner here - maybe parse from existing
|
2228 | 3f78eef2 | Iustin Pop | # cache? for now, we only lose lvm data when we rename, which
|
2229 | 3f78eef2 | Iustin Pop | # is less critical than DRBD or MD
|
2230 | f3e513ad | Iustin Pop | except errors.BlockDeviceError, err:
|
2231 | 6b5e3f70 | Iustin Pop | msgs.append("Can't rename device '%s' to '%s': %s" %
|
2232 | 6b5e3f70 | Iustin Pop | (dev, unique_id, err)) |
2233 | 18682bca | Iustin Pop | logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
|
2234 | f3e513ad | Iustin Pop | result = False
|
2235 | afdc3985 | Iustin Pop | if not result: |
2236 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
2237 | f3e513ad | Iustin Pop | |
2238 | f3e513ad | Iustin Pop | |
2239 | 778b75bb | Manuel Franceschini | def _TransformFileStorageDir(file_storage_dir): |
2240 | 778b75bb | Manuel Franceschini | """Checks whether given file_storage_dir is valid.
|
2241 | 778b75bb | Manuel Franceschini |
|
2242 | 778b75bb | Manuel Franceschini | Checks wheter the given file_storage_dir is within the cluster-wide
|
2243 | 778b75bb | Manuel Franceschini | default file_storage_dir stored in SimpleStore. Only paths under that
|
2244 | 778b75bb | Manuel Franceschini | directory are allowed.
|
2245 | 778b75bb | Manuel Franceschini |
|
2246 | b1206984 | Iustin Pop | @type file_storage_dir: str
|
2247 | b1206984 | Iustin Pop | @param file_storage_dir: the path to check
|
2248 | d61cbe76 | Iustin Pop |
|
2249 | b1206984 | Iustin Pop | @return: the normalized path if valid, None otherwise
|
2250 | 778b75bb | Manuel Franceschini |
|
2251 | 778b75bb | Manuel Franceschini | """
|
2252 | cb7c0198 | Iustin Pop | if not constants.ENABLE_FILE_STORAGE: |
2253 | cb7c0198 | Iustin Pop | _Fail("File storage disabled at configure time")
|
2254 | c657dcc9 | Michael Hanselmann | cfg = _GetConfig() |
2255 | 778b75bb | Manuel Franceschini | file_storage_dir = os.path.normpath(file_storage_dir) |
2256 | c657dcc9 | Michael Hanselmann | base_file_storage_dir = cfg.GetFileStorageDir() |
2257 | 56569f4e | Michael Hanselmann | if (os.path.commonprefix([file_storage_dir, base_file_storage_dir]) !=
|
2258 | 778b75bb | Manuel Franceschini | base_file_storage_dir): |
2259 | b2b8bcce | Iustin Pop | _Fail("File storage directory '%s' is not under base file"
|
2260 | b2b8bcce | Iustin Pop | " storage directory '%s'", file_storage_dir, base_file_storage_dir)
|
2261 | 778b75bb | Manuel Franceschini | return file_storage_dir
|
2262 | 778b75bb | Manuel Franceschini | |
2263 | 778b75bb | Manuel Franceschini | |
2264 | 778b75bb | Manuel Franceschini | def CreateFileStorageDir(file_storage_dir): |
2265 | 778b75bb | Manuel Franceschini | """Create file storage directory.
|
2266 | 778b75bb | Manuel Franceschini |
|
2267 | b1206984 | Iustin Pop | @type file_storage_dir: str
|
2268 | b1206984 | Iustin Pop | @param file_storage_dir: directory to create
|
2269 | 778b75bb | Manuel Franceschini |
|
2270 | b1206984 | Iustin Pop | @rtype: tuple
|
2271 | b1206984 | Iustin Pop | @return: tuple with first element a boolean indicating wheter dir
|
2272 | b1206984 | Iustin Pop | creation was successful or not
|
2273 | 778b75bb | Manuel Franceschini |
|
2274 | 778b75bb | Manuel Franceschini | """
|
2275 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
2276 | b2b8bcce | Iustin Pop | if os.path.exists(file_storage_dir):
|
2277 | b2b8bcce | Iustin Pop | if not os.path.isdir(file_storage_dir): |
2278 | b2b8bcce | Iustin Pop | _Fail("Specified storage dir '%s' is not a directory",
|
2279 | b2b8bcce | Iustin Pop | file_storage_dir) |
2280 | 778b75bb | Manuel Franceschini | else:
|
2281 | b2b8bcce | Iustin Pop | try:
|
2282 | b2b8bcce | Iustin Pop | os.makedirs(file_storage_dir, 0750)
|
2283 | b2b8bcce | Iustin Pop | except OSError, err: |
2284 | b2b8bcce | Iustin Pop | _Fail("Cannot create file storage directory '%s': %s",
|
2285 | b2b8bcce | Iustin Pop | file_storage_dir, err, exc=True)
|
2286 | 778b75bb | Manuel Franceschini | |
2287 | 778b75bb | Manuel Franceschini | |
2288 | 778b75bb | Manuel Franceschini | def RemoveFileStorageDir(file_storage_dir): |
2289 | 778b75bb | Manuel Franceschini | """Remove file storage directory.
|
2290 | 778b75bb | Manuel Franceschini |
|
2291 | 778b75bb | Manuel Franceschini | Remove it only if it's empty. If not log an error and return.
|
2292 | 778b75bb | Manuel Franceschini |
|
2293 | 10c2650b | Iustin Pop | @type file_storage_dir: str
|
2294 | 10c2650b | Iustin Pop | @param file_storage_dir: the directory we should cleanup
|
2295 | 10c2650b | Iustin Pop | @rtype: tuple (success,)
|
2296 | 10c2650b | Iustin Pop | @return: tuple of one element, C{success}, denoting
|
2297 | 5bbd3f7f | Michael Hanselmann | whether the operation was successful
|
2298 | 778b75bb | Manuel Franceschini |
|
2299 | 778b75bb | Manuel Franceschini | """
|
2300 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
2301 | b2b8bcce | Iustin Pop | if os.path.exists(file_storage_dir):
|
2302 | b2b8bcce | Iustin Pop | if not os.path.isdir(file_storage_dir): |
2303 | b2b8bcce | Iustin Pop | _Fail("Specified Storage directory '%s' is not a directory",
|
2304 | b2b8bcce | Iustin Pop | file_storage_dir) |
2305 | afdc3985 | Iustin Pop | # deletes dir only if empty, otherwise we want to fail the rpc call
|
2306 | b2b8bcce | Iustin Pop | try:
|
2307 | b2b8bcce | Iustin Pop | os.rmdir(file_storage_dir) |
2308 | b2b8bcce | Iustin Pop | except OSError, err: |
2309 | b2b8bcce | Iustin Pop | _Fail("Cannot remove file storage directory '%s': %s",
|
2310 | b2b8bcce | Iustin Pop | file_storage_dir, err) |
2311 | b2b8bcce | Iustin Pop | |
2312 | 778b75bb | Manuel Franceschini | |
2313 | 778b75bb | Manuel Franceschini | def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir): |
2314 | 778b75bb | Manuel Franceschini | """Rename the file storage directory.
|
2315 | 778b75bb | Manuel Franceschini |
|
2316 | 10c2650b | Iustin Pop | @type old_file_storage_dir: str
|
2317 | 10c2650b | Iustin Pop | @param old_file_storage_dir: the current path
|
2318 | 10c2650b | Iustin Pop | @type new_file_storage_dir: str
|
2319 | 10c2650b | Iustin Pop | @param new_file_storage_dir: the name we should rename to
|
2320 | 10c2650b | Iustin Pop | @rtype: tuple (success,)
|
2321 | 10c2650b | Iustin Pop | @return: tuple of one element, C{success}, denoting
|
2322 | 10c2650b | Iustin Pop | whether the operation was successful
|
2323 | 778b75bb | Manuel Franceschini |
|
2324 | 778b75bb | Manuel Franceschini | """
|
2325 | 778b75bb | Manuel Franceschini | old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir) |
2326 | 778b75bb | Manuel Franceschini | new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir) |
2327 | b2b8bcce | Iustin Pop | if not os.path.exists(new_file_storage_dir): |
2328 | b2b8bcce | Iustin Pop | if os.path.isdir(old_file_storage_dir):
|
2329 | b2b8bcce | Iustin Pop | try:
|
2330 | b2b8bcce | Iustin Pop | os.rename(old_file_storage_dir, new_file_storage_dir) |
2331 | b2b8bcce | Iustin Pop | except OSError, err: |
2332 | b2b8bcce | Iustin Pop | _Fail("Cannot rename '%s' to '%s': %s",
|
2333 | b2b8bcce | Iustin Pop | old_file_storage_dir, new_file_storage_dir, err) |
2334 | 778b75bb | Manuel Franceschini | else:
|
2335 | b2b8bcce | Iustin Pop | _Fail("Specified storage dir '%s' is not a directory",
|
2336 | b2b8bcce | Iustin Pop | old_file_storage_dir) |
2337 | b2b8bcce | Iustin Pop | else:
|
2338 | b2b8bcce | Iustin Pop | if os.path.exists(old_file_storage_dir):
|
2339 | b2b8bcce | Iustin Pop | _Fail("Cannot rename '%s' to '%s': both locations exist",
|
2340 | b2b8bcce | Iustin Pop | old_file_storage_dir, new_file_storage_dir) |
2341 | 778b75bb | Manuel Franceschini | |
2342 | 778b75bb | Manuel Franceschini | |
2343 | c8457ce7 | Iustin Pop | def _EnsureJobQueueFile(file_name): |
2344 | dc31eae3 | Michael Hanselmann | """Checks whether the given filename is in the queue directory.
|
2345 | ca52cdeb | Michael Hanselmann |
|
2346 | 10c2650b | Iustin Pop | @type file_name: str
|
2347 | 10c2650b | Iustin Pop | @param file_name: the file name we should check
|
2348 | c8457ce7 | Iustin Pop | @rtype: None
|
2349 | c8457ce7 | Iustin Pop | @raises RPCFail: if the file is not valid
|
2350 | 10c2650b | Iustin Pop |
|
2351 | ca52cdeb | Michael Hanselmann | """
|
2352 | ca52cdeb | Michael Hanselmann | queue_dir = os.path.normpath(constants.QUEUE_DIR) |
2353 | dc31eae3 | Michael Hanselmann | result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir) |
2354 | dc31eae3 | Michael Hanselmann | |
2355 | dc31eae3 | Michael Hanselmann | if not result: |
2356 | c8457ce7 | Iustin Pop | _Fail("Passed job queue file '%s' does not belong to"
|
2357 | c8457ce7 | Iustin Pop | " the queue directory '%s'", file_name, queue_dir)
|
2358 | dc31eae3 | Michael Hanselmann | |
2359 | dc31eae3 | Michael Hanselmann | |
2360 | dc31eae3 | Michael Hanselmann | def JobQueueUpdate(file_name, content): |
2361 | dc31eae3 | Michael Hanselmann | """Updates a file in the queue directory.
|
2362 | dc31eae3 | Michael Hanselmann |
|
2363 | 10c2650b | Iustin Pop | This is just a wrapper over L{utils.WriteFile}, with proper
|
2364 | 10c2650b | Iustin Pop | checking.
|
2365 | 10c2650b | Iustin Pop |
|
2366 | 10c2650b | Iustin Pop | @type file_name: str
|
2367 | 10c2650b | Iustin Pop | @param file_name: the job file name
|
2368 | 10c2650b | Iustin Pop | @type content: str
|
2369 | 10c2650b | Iustin Pop | @param content: the new job contents
|
2370 | 10c2650b | Iustin Pop | @rtype: boolean
|
2371 | 10c2650b | Iustin Pop | @return: the success of the operation
|
2372 | 10c2650b | Iustin Pop |
|
2373 | dc31eae3 | Michael Hanselmann | """
|
2374 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(file_name) |
2375 | 82b22e19 | René Nussbaumer | getents = runtime.GetEnts() |
2376 | ca52cdeb | Michael Hanselmann | |
2377 | ca52cdeb | Michael Hanselmann | # Write and replace the file atomically
|
2378 | 82b22e19 | René Nussbaumer | utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid, |
2379 | 82b22e19 | René Nussbaumer | gid=getents.masterd_gid) |
2380 | ca52cdeb | Michael Hanselmann | |
2381 | ca52cdeb | Michael Hanselmann | |
2382 | af5ebcb1 | Michael Hanselmann | def JobQueueRename(old, new): |
2383 | af5ebcb1 | Michael Hanselmann | """Renames a job queue file.
|
2384 | af5ebcb1 | Michael Hanselmann |
|
2385 | c41eea6e | Iustin Pop | This is just a wrapper over os.rename with proper checking.
|
2386 | 10c2650b | Iustin Pop |
|
2387 | 10c2650b | Iustin Pop | @type old: str
|
2388 | 10c2650b | Iustin Pop | @param old: the old (actual) file name
|
2389 | 10c2650b | Iustin Pop | @type new: str
|
2390 | 10c2650b | Iustin Pop | @param new: the desired file name
|
2391 | c8457ce7 | Iustin Pop | @rtype: tuple
|
2392 | c8457ce7 | Iustin Pop | @return: the success of the operation and payload
|
2393 | 10c2650b | Iustin Pop |
|
2394 | af5ebcb1 | Michael Hanselmann | """
|
2395 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(old) |
2396 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(new) |
2397 | af5ebcb1 | Michael Hanselmann | |
2398 | 58b22b6e | Michael Hanselmann | utils.RenameFile(old, new, mkdir=True)
|
2399 | af5ebcb1 | Michael Hanselmann | |
2400 | af5ebcb1 | Michael Hanselmann | |
2401 | 821d1bd1 | Iustin Pop | def BlockdevClose(instance_name, disks): |
2402 | d61cbe76 | Iustin Pop | """Closes the given block devices.
|
2403 | d61cbe76 | Iustin Pop |
|
2404 | 10c2650b | Iustin Pop | This means they will be switched to secondary mode (in case of
|
2405 | 10c2650b | Iustin Pop | DRBD).
|
2406 | 10c2650b | Iustin Pop |
|
2407 | b2e7666a | Iustin Pop | @param instance_name: if the argument is not empty, the symlinks
|
2408 | b2e7666a | Iustin Pop | of this instance will be removed
|
2409 | 10c2650b | Iustin Pop | @type disks: list of L{objects.Disk}
|
2410 | 10c2650b | Iustin Pop | @param disks: the list of disks to be closed
|
2411 | 10c2650b | Iustin Pop | @rtype: tuple (success, message)
|
2412 | 10c2650b | Iustin Pop | @return: a tuple of success and message, where success
|
2413 | 10c2650b | Iustin Pop | indicates the succes of the operation, and message
|
2414 | 10c2650b | Iustin Pop | which will contain the error details in case we
|
2415 | 10c2650b | Iustin Pop | failed
|
2416 | d61cbe76 | Iustin Pop |
|
2417 | d61cbe76 | Iustin Pop | """
|
2418 | d61cbe76 | Iustin Pop | bdevs = [] |
2419 | d61cbe76 | Iustin Pop | for cf in disks: |
2420 | d61cbe76 | Iustin Pop | rd = _RecursiveFindBD(cf) |
2421 | d61cbe76 | Iustin Pop | if rd is None: |
2422 | 2cc6781a | Iustin Pop | _Fail("Can't find device %s", cf)
|
2423 | d61cbe76 | Iustin Pop | bdevs.append(rd) |
2424 | d61cbe76 | Iustin Pop | |
2425 | d61cbe76 | Iustin Pop | msg = [] |
2426 | d61cbe76 | Iustin Pop | for rd in bdevs: |
2427 | d61cbe76 | Iustin Pop | try:
|
2428 | d61cbe76 | Iustin Pop | rd.Close() |
2429 | d61cbe76 | Iustin Pop | except errors.BlockDeviceError, err:
|
2430 | d61cbe76 | Iustin Pop | msg.append(str(err))
|
2431 | d61cbe76 | Iustin Pop | if msg:
|
2432 | afdc3985 | Iustin Pop | _Fail("Can't make devices secondary: %s", ",".join(msg)) |
2433 | d61cbe76 | Iustin Pop | else:
|
2434 | b2e7666a | Iustin Pop | if instance_name:
|
2435 | 5282084b | Iustin Pop | _RemoveBlockDevLinks(instance_name, disks) |
2436 | d61cbe76 | Iustin Pop | |
2437 | d61cbe76 | Iustin Pop | |
2438 | 6217e295 | Iustin Pop | def ValidateHVParams(hvname, hvparams): |
2439 | 6217e295 | Iustin Pop | """Validates the given hypervisor parameters.
|
2440 | 6217e295 | Iustin Pop |
|
2441 | 6217e295 | Iustin Pop | @type hvname: string
|
2442 | 6217e295 | Iustin Pop | @param hvname: the hypervisor name
|
2443 | 6217e295 | Iustin Pop | @type hvparams: dict
|
2444 | 6217e295 | Iustin Pop | @param hvparams: the hypervisor parameters to be validated
|
2445 | c26a6bd2 | Iustin Pop | @rtype: None
|
2446 | 6217e295 | Iustin Pop |
|
2447 | 6217e295 | Iustin Pop | """
|
2448 | 6217e295 | Iustin Pop | try:
|
2449 | 6217e295 | Iustin Pop | hv_type = hypervisor.GetHypervisor(hvname) |
2450 | 6217e295 | Iustin Pop | hv_type.ValidateParameters(hvparams) |
2451 | 6217e295 | Iustin Pop | except errors.HypervisorError, err:
|
2452 | afdc3985 | Iustin Pop | _Fail(str(err), log=False) |
2453 | 6217e295 | Iustin Pop | |
2454 | 6217e295 | Iustin Pop | |
2455 | acd9ff9e | Iustin Pop | def _CheckOSPList(os_obj, parameters): |
2456 | acd9ff9e | Iustin Pop | """Check whether a list of parameters is supported by the OS.
|
2457 | acd9ff9e | Iustin Pop |
|
2458 | acd9ff9e | Iustin Pop | @type os_obj: L{objects.OS}
|
2459 | acd9ff9e | Iustin Pop | @param os_obj: OS object to check
|
2460 | acd9ff9e | Iustin Pop | @type parameters: list
|
2461 | acd9ff9e | Iustin Pop | @param parameters: the list of parameters to check
|
2462 | acd9ff9e | Iustin Pop |
|
2463 | acd9ff9e | Iustin Pop | """
|
2464 | acd9ff9e | Iustin Pop | supported = [v[0] for v in os_obj.supported_parameters] |
2465 | acd9ff9e | Iustin Pop | delta = frozenset(parameters).difference(supported)
|
2466 | acd9ff9e | Iustin Pop | if delta:
|
2467 | acd9ff9e | Iustin Pop | _Fail("The following parameters are not supported"
|
2468 | acd9ff9e | Iustin Pop | " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
|
2469 | acd9ff9e | Iustin Pop | |
2470 | acd9ff9e | Iustin Pop | |
2471 | acd9ff9e | Iustin Pop | def ValidateOS(required, osname, checks, osparams): |
2472 | acd9ff9e | Iustin Pop | """Validate the given OS' parameters.
|
2473 | acd9ff9e | Iustin Pop |
|
2474 | acd9ff9e | Iustin Pop | @type required: boolean
|
2475 | acd9ff9e | Iustin Pop | @param required: whether absence of the OS should translate into
|
2476 | acd9ff9e | Iustin Pop | failure or not
|
2477 | acd9ff9e | Iustin Pop | @type osname: string
|
2478 | acd9ff9e | Iustin Pop | @param osname: the OS to be validated
|
2479 | acd9ff9e | Iustin Pop | @type checks: list
|
2480 | acd9ff9e | Iustin Pop | @param checks: list of the checks to run (currently only 'parameters')
|
2481 | acd9ff9e | Iustin Pop | @type osparams: dict
|
2482 | acd9ff9e | Iustin Pop | @param osparams: dictionary with OS parameters
|
2483 | acd9ff9e | Iustin Pop | @rtype: boolean
|
2484 | acd9ff9e | Iustin Pop | @return: True if the validation passed, or False if the OS was not
|
2485 | acd9ff9e | Iustin Pop | found and L{required} was false
|
2486 | acd9ff9e | Iustin Pop |
|
2487 | acd9ff9e | Iustin Pop | """
|
2488 | acd9ff9e | Iustin Pop | if not constants.OS_VALIDATE_CALLS.issuperset(checks): |
2489 | acd9ff9e | Iustin Pop | _Fail("Unknown checks required for OS %s: %s", osname,
|
2490 | acd9ff9e | Iustin Pop | set(checks).difference(constants.OS_VALIDATE_CALLS))
|
2491 | acd9ff9e | Iustin Pop | |
2492 | acd9ff9e | Iustin Pop | name_only = osname.split("+", 1)[0] |
2493 | acd9ff9e | Iustin Pop | status, tbv = _TryOSFromDisk(name_only, None)
|
2494 | acd9ff9e | Iustin Pop | |
2495 | acd9ff9e | Iustin Pop | if not status: |
2496 | acd9ff9e | Iustin Pop | if required:
|
2497 | acd9ff9e | Iustin Pop | _Fail(tbv) |
2498 | acd9ff9e | Iustin Pop | else:
|
2499 | acd9ff9e | Iustin Pop | return False |
2500 | acd9ff9e | Iustin Pop | |
2501 | 72db3fd7 | Iustin Pop | if max(tbv.api_versions) < constants.OS_API_V20: |
2502 | 72db3fd7 | Iustin Pop | return True |
2503 | 72db3fd7 | Iustin Pop | |
2504 | acd9ff9e | Iustin Pop | if constants.OS_VALIDATE_PARAMETERS in checks: |
2505 | acd9ff9e | Iustin Pop | _CheckOSPList(tbv, osparams.keys()) |
2506 | acd9ff9e | Iustin Pop | |
2507 | a025e535 | Vitaly Kuznetsov | validate_env = OSCoreEnv(osname, tbv, osparams) |
2508 | acd9ff9e | Iustin Pop | result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env, |
2509 | acd9ff9e | Iustin Pop | cwd=tbv.path) |
2510 | acd9ff9e | Iustin Pop | if result.failed:
|
2511 | acd9ff9e | Iustin Pop | logging.error("os validate command '%s' returned error: %s output: %s",
|
2512 | acd9ff9e | Iustin Pop | result.cmd, result.fail_reason, result.output) |
2513 | acd9ff9e | Iustin Pop | _Fail("OS validation script failed (%s), output: %s",
|
2514 | acd9ff9e | Iustin Pop | result.fail_reason, result.output, log=False)
|
2515 | acd9ff9e | Iustin Pop | |
2516 | acd9ff9e | Iustin Pop | return True |
2517 | acd9ff9e | Iustin Pop | |
2518 | acd9ff9e | Iustin Pop | |
2519 | 56aa9fd5 | Iustin Pop | def DemoteFromMC(): |
2520 | 56aa9fd5 | Iustin Pop | """Demotes the current node from master candidate role.
|
2521 | 56aa9fd5 | Iustin Pop |
|
2522 | 56aa9fd5 | Iustin Pop | """
|
2523 | 56aa9fd5 | Iustin Pop | # try to ensure we're not the master by mistake
|
2524 | 56aa9fd5 | Iustin Pop | master, myself = ssconf.GetMasterAndMyself() |
2525 | 56aa9fd5 | Iustin Pop | if master == myself:
|
2526 | afdc3985 | Iustin Pop | _Fail("ssconf status shows I'm the master node, will not demote")
|
2527 | f154a7a3 | Michael Hanselmann | |
2528 | f154a7a3 | Michael Hanselmann | result = utils.RunCmd([constants.DAEMON_UTIL, "check", constants.MASTERD])
|
2529 | f154a7a3 | Michael Hanselmann | if not result.failed: |
2530 | afdc3985 | Iustin Pop | _Fail("The master daemon is running, will not demote")
|
2531 | f154a7a3 | Michael Hanselmann | |
2532 | 56aa9fd5 | Iustin Pop | try:
|
2533 | 9a5cb537 | Iustin Pop | if os.path.isfile(constants.CLUSTER_CONF_FILE):
|
2534 | 9a5cb537 | Iustin Pop | utils.CreateBackup(constants.CLUSTER_CONF_FILE) |
2535 | 56aa9fd5 | Iustin Pop | except EnvironmentError, err: |
2536 | 56aa9fd5 | Iustin Pop | if err.errno != errno.ENOENT:
|
2537 | afdc3985 | Iustin Pop | _Fail("Error while backing up cluster file: %s", err, exc=True) |
2538 | f154a7a3 | Michael Hanselmann | |
2539 | 56aa9fd5 | Iustin Pop | utils.RemoveFile(constants.CLUSTER_CONF_FILE) |
2540 | 56aa9fd5 | Iustin Pop | |
2541 | 56aa9fd5 | Iustin Pop | |
2542 | f942a838 | Michael Hanselmann | def _GetX509Filenames(cryptodir, name): |
2543 | f942a838 | Michael Hanselmann | """Returns the full paths for the private key and certificate.
|
2544 | f942a838 | Michael Hanselmann |
|
2545 | f942a838 | Michael Hanselmann | """
|
2546 | f942a838 | Michael Hanselmann | return (utils.PathJoin(cryptodir, name),
|
2547 | f942a838 | Michael Hanselmann | utils.PathJoin(cryptodir, name, _X509_KEY_FILE), |
2548 | f942a838 | Michael Hanselmann | utils.PathJoin(cryptodir, name, _X509_CERT_FILE)) |
2549 | f942a838 | Michael Hanselmann | |
2550 | f942a838 | Michael Hanselmann | |
2551 | f942a838 | Michael Hanselmann | def CreateX509Certificate(validity, cryptodir=constants.CRYPTO_KEYS_DIR): |
2552 | f942a838 | Michael Hanselmann | """Creates a new X509 certificate for SSL/TLS.
|
2553 | f942a838 | Michael Hanselmann |
|
2554 | f942a838 | Michael Hanselmann | @type validity: int
|
2555 | f942a838 | Michael Hanselmann | @param validity: Validity in seconds
|
2556 | f942a838 | Michael Hanselmann | @rtype: tuple; (string, string)
|
2557 | f942a838 | Michael Hanselmann | @return: Certificate name and public part
|
2558 | f942a838 | Michael Hanselmann |
|
2559 | f942a838 | Michael Hanselmann | """
|
2560 | f942a838 | Michael Hanselmann | (key_pem, cert_pem) = \ |
2561 | b705c7a6 | Manuel Franceschini | utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(), |
2562 | f942a838 | Michael Hanselmann | min(validity, _MAX_SSL_CERT_VALIDITY))
|
2563 | f942a838 | Michael Hanselmann | |
2564 | f942a838 | Michael Hanselmann | cert_dir = tempfile.mkdtemp(dir=cryptodir, |
2565 | f942a838 | Michael Hanselmann | prefix="x509-%s-" % utils.TimestampForFilename())
|
2566 | f942a838 | Michael Hanselmann | try:
|
2567 | f942a838 | Michael Hanselmann | name = os.path.basename(cert_dir) |
2568 | f942a838 | Michael Hanselmann | assert len(name) > 5 |
2569 | f942a838 | Michael Hanselmann | |
2570 | f942a838 | Michael Hanselmann | (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name) |
2571 | f942a838 | Michael Hanselmann | |
2572 | f942a838 | Michael Hanselmann | utils.WriteFile(key_file, mode=0400, data=key_pem)
|
2573 | f942a838 | Michael Hanselmann | utils.WriteFile(cert_file, mode=0400, data=cert_pem)
|
2574 | f942a838 | Michael Hanselmann | |
2575 | f942a838 | Michael Hanselmann | # Never return private key as it shouldn't leave the node
|
2576 | f942a838 | Michael Hanselmann | return (name, cert_pem)
|
2577 | f942a838 | Michael Hanselmann | except Exception: |
2578 | f942a838 | Michael Hanselmann | shutil.rmtree(cert_dir, ignore_errors=True)
|
2579 | f942a838 | Michael Hanselmann | raise
|
2580 | f942a838 | Michael Hanselmann | |
2581 | f942a838 | Michael Hanselmann | |
2582 | f942a838 | Michael Hanselmann | def RemoveX509Certificate(name, cryptodir=constants.CRYPTO_KEYS_DIR): |
2583 | f942a838 | Michael Hanselmann | """Removes a X509 certificate.
|
2584 | f942a838 | Michael Hanselmann |
|
2585 | f942a838 | Michael Hanselmann | @type name: string
|
2586 | f942a838 | Michael Hanselmann | @param name: Certificate name
|
2587 | f942a838 | Michael Hanselmann |
|
2588 | f942a838 | Michael Hanselmann | """
|
2589 | f942a838 | Michael Hanselmann | (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name) |
2590 | f942a838 | Michael Hanselmann | |
2591 | f942a838 | Michael Hanselmann | utils.RemoveFile(key_file) |
2592 | f942a838 | Michael Hanselmann | utils.RemoveFile(cert_file) |
2593 | f942a838 | Michael Hanselmann | |
2594 | f942a838 | Michael Hanselmann | try:
|
2595 | f942a838 | Michael Hanselmann | os.rmdir(cert_dir) |
2596 | f942a838 | Michael Hanselmann | except EnvironmentError, err: |
2597 | f942a838 | Michael Hanselmann | _Fail("Cannot remove certificate directory '%s': %s",
|
2598 | f942a838 | Michael Hanselmann | cert_dir, err) |
2599 | f942a838 | Michael Hanselmann | |
2600 | f942a838 | Michael Hanselmann | |
2601 | 1651d116 | Michael Hanselmann | def _GetImportExportIoCommand(instance, mode, ieio, ieargs): |
2602 | 1651d116 | Michael Hanselmann | """Returns the command for the requested input/output.
|
2603 | 1651d116 | Michael Hanselmann |
|
2604 | 1651d116 | Michael Hanselmann | @type instance: L{objects.Instance}
|
2605 | 1651d116 | Michael Hanselmann | @param instance: The instance object
|
2606 | 1651d116 | Michael Hanselmann | @param mode: Import/export mode
|
2607 | 1651d116 | Michael Hanselmann | @param ieio: Input/output type
|
2608 | 1651d116 | Michael Hanselmann | @param ieargs: Input/output arguments
|
2609 | 1651d116 | Michael Hanselmann |
|
2610 | 1651d116 | Michael Hanselmann | """
|
2611 | 1651d116 | Michael Hanselmann | assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT) |
2612 | 1651d116 | Michael Hanselmann | |
2613 | 1651d116 | Michael Hanselmann | env = None
|
2614 | 1651d116 | Michael Hanselmann | prefix = None
|
2615 | 1651d116 | Michael Hanselmann | suffix = None
|
2616 | 2ad5550d | Michael Hanselmann | exp_size = None
|
2617 | 1651d116 | Michael Hanselmann | |
2618 | 1651d116 | Michael Hanselmann | if ieio == constants.IEIO_FILE:
|
2619 | 1651d116 | Michael Hanselmann | (filename, ) = ieargs |
2620 | 1651d116 | Michael Hanselmann | |
2621 | 1651d116 | Michael Hanselmann | if not utils.IsNormAbsPath(filename): |
2622 | 1651d116 | Michael Hanselmann | _Fail("Path '%s' is not normalized or absolute", filename)
|
2623 | 1651d116 | Michael Hanselmann | |
2624 | 1651d116 | Michael Hanselmann | directory = os.path.normpath(os.path.dirname(filename)) |
2625 | 1651d116 | Michael Hanselmann | |
2626 | 1651d116 | Michael Hanselmann | if (os.path.commonprefix([constants.EXPORT_DIR, directory]) !=
|
2627 | 1651d116 | Michael Hanselmann | constants.EXPORT_DIR): |
2628 | 1651d116 | Michael Hanselmann | _Fail("File '%s' is not under exports directory '%s'",
|
2629 | 1651d116 | Michael Hanselmann | filename, constants.EXPORT_DIR) |
2630 | 1651d116 | Michael Hanselmann | |
2631 | 1651d116 | Michael Hanselmann | # Create directory
|
2632 | 1651d116 | Michael Hanselmann | utils.Makedirs(directory, mode=0750)
|
2633 | 1651d116 | Michael Hanselmann | |
2634 | 1651d116 | Michael Hanselmann | quoted_filename = utils.ShellQuote(filename) |
2635 | 1651d116 | Michael Hanselmann | |
2636 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
2637 | 1651d116 | Michael Hanselmann | suffix = "> %s" % quoted_filename
|
2638 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
2639 | 1651d116 | Michael Hanselmann | suffix = "< %s" % quoted_filename
|
2640 | 1651d116 | Michael Hanselmann | |
2641 | 2ad5550d | Michael Hanselmann | # Retrieve file size
|
2642 | 2ad5550d | Michael Hanselmann | try:
|
2643 | 2ad5550d | Michael Hanselmann | st = os.stat(filename) |
2644 | 2ad5550d | Michael Hanselmann | except EnvironmentError, err: |
2645 | 2ad5550d | Michael Hanselmann | logging.error("Can't stat(2) %s: %s", filename, err)
|
2646 | 2ad5550d | Michael Hanselmann | else:
|
2647 | 2ad5550d | Michael Hanselmann | exp_size = utils.BytesToMebibyte(st.st_size) |
2648 | 2ad5550d | Michael Hanselmann | |
2649 | 1651d116 | Michael Hanselmann | elif ieio == constants.IEIO_RAW_DISK:
|
2650 | 1651d116 | Michael Hanselmann | (disk, ) = ieargs |
2651 | 1651d116 | Michael Hanselmann | |
2652 | 1651d116 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
2653 | 1651d116 | Michael Hanselmann | |
2654 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
2655 | 1651d116 | Michael Hanselmann | # we set here a smaller block size as, due to transport buffering, more
|
2656 | 1651d116 | Michael Hanselmann | # than 64-128k will mostly ignored; we use nocreat to fail if the device
|
2657 | 1651d116 | Michael Hanselmann | # is not already there or we pass a wrong path; we use notrunc to no
|
2658 | 1651d116 | Michael Hanselmann | # attempt truncate on an LV device; we use oflag=dsync to not buffer too
|
2659 | 1651d116 | Michael Hanselmann | # much memory; this means that at best, we flush every 64k, which will
|
2660 | 1651d116 | Michael Hanselmann | # not be very fast
|
2661 | 1651d116 | Michael Hanselmann | suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
|
2662 | 1651d116 | Michael Hanselmann | " bs=%s oflag=dsync"),
|
2663 | 1651d116 | Michael Hanselmann | real_disk.dev_path, |
2664 | 1651d116 | Michael Hanselmann | str(64 * 1024)) |
2665 | 1651d116 | Michael Hanselmann | |
2666 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
2667 | 1651d116 | Michael Hanselmann | # the block size on the read dd is 1MiB to match our units
|
2668 | 1651d116 | Michael Hanselmann | prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
|
2669 | 1651d116 | Michael Hanselmann | real_disk.dev_path, |
2670 | 1651d116 | Michael Hanselmann | str(1024 * 1024), # 1 MB |
2671 | 1651d116 | Michael Hanselmann | str(disk.size))
|
2672 | 2ad5550d | Michael Hanselmann | exp_size = disk.size |
2673 | 1651d116 | Michael Hanselmann | |
2674 | 1651d116 | Michael Hanselmann | elif ieio == constants.IEIO_SCRIPT:
|
2675 | 1651d116 | Michael Hanselmann | (disk, disk_index, ) = ieargs |
2676 | 1651d116 | Michael Hanselmann | |
2677 | 1651d116 | Michael Hanselmann | assert isinstance(disk_index, (int, long)) |
2678 | 1651d116 | Michael Hanselmann | |
2679 | 1651d116 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
2680 | 1651d116 | Michael Hanselmann | |
2681 | 1651d116 | Michael Hanselmann | inst_os = OSFromDisk(instance.os) |
2682 | 1651d116 | Michael Hanselmann | env = OSEnvironment(instance, inst_os) |
2683 | 1651d116 | Michael Hanselmann | |
2684 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
2685 | 1651d116 | Michael Hanselmann | env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index] |
2686 | 1651d116 | Michael Hanselmann | env["IMPORT_INDEX"] = str(disk_index) |
2687 | 1651d116 | Michael Hanselmann | script = inst_os.import_script |
2688 | 1651d116 | Michael Hanselmann | |
2689 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
2690 | 1651d116 | Michael Hanselmann | env["EXPORT_DEVICE"] = real_disk.dev_path
|
2691 | 1651d116 | Michael Hanselmann | env["EXPORT_INDEX"] = str(disk_index) |
2692 | 1651d116 | Michael Hanselmann | script = inst_os.export_script |
2693 | 1651d116 | Michael Hanselmann | |
2694 | 1651d116 | Michael Hanselmann | # TODO: Pass special environment only to script
|
2695 | 1651d116 | Michael Hanselmann | script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
|
2696 | 1651d116 | Michael Hanselmann | |
2697 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
2698 | 1651d116 | Michael Hanselmann | suffix = "| %s" % script_cmd
|
2699 | 1651d116 | Michael Hanselmann | |
2700 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
2701 | 1651d116 | Michael Hanselmann | prefix = "%s |" % script_cmd
|
2702 | 1651d116 | Michael Hanselmann | |
2703 | 2ad5550d | Michael Hanselmann | # Let script predict size
|
2704 | 2ad5550d | Michael Hanselmann | exp_size = constants.IE_CUSTOM_SIZE |
2705 | 2ad5550d | Michael Hanselmann | |
2706 | 1651d116 | Michael Hanselmann | else:
|
2707 | 1651d116 | Michael Hanselmann | _Fail("Invalid %s I/O mode %r", mode, ieio)
|
2708 | 1651d116 | Michael Hanselmann | |
2709 | 2ad5550d | Michael Hanselmann | return (env, prefix, suffix, exp_size)
|
2710 | 1651d116 | Michael Hanselmann | |
2711 | 1651d116 | Michael Hanselmann | |
2712 | 1651d116 | Michael Hanselmann | def _CreateImportExportStatusDir(prefix): |
2713 | 1651d116 | Michael Hanselmann | """Creates status directory for import/export.
|
2714 | 1651d116 | Michael Hanselmann |
|
2715 | 1651d116 | Michael Hanselmann | """
|
2716 | 1651d116 | Michael Hanselmann | return tempfile.mkdtemp(dir=constants.IMPORT_EXPORT_DIR,
|
2717 | 1651d116 | Michael Hanselmann | prefix=("%s-%s-" %
|
2718 | 1651d116 | Michael Hanselmann | (prefix, utils.TimestampForFilename()))) |
2719 | 1651d116 | Michael Hanselmann | |
2720 | 1651d116 | Michael Hanselmann | |
2721 | eb630f50 | Michael Hanselmann | def StartImportExportDaemon(mode, opts, host, port, instance, ieio, ieioargs): |
2722 | 1651d116 | Michael Hanselmann | """Starts an import or export daemon.
|
2723 | 1651d116 | Michael Hanselmann |
|
2724 | 1651d116 | Michael Hanselmann | @param mode: Import/output mode
|
2725 | eb630f50 | Michael Hanselmann | @type opts: L{objects.ImportExportOptions}
|
2726 | eb630f50 | Michael Hanselmann | @param opts: Daemon options
|
2727 | 1651d116 | Michael Hanselmann | @type host: string
|
2728 | 1651d116 | Michael Hanselmann | @param host: Remote host for export (None for import)
|
2729 | 1651d116 | Michael Hanselmann | @type port: int
|
2730 | 1651d116 | Michael Hanselmann | @param port: Remote port for export (None for import)
|
2731 | 1651d116 | Michael Hanselmann | @type instance: L{objects.Instance}
|
2732 | 1651d116 | Michael Hanselmann | @param instance: Instance object
|
2733 | 1651d116 | Michael Hanselmann | @param ieio: Input/output type
|
2734 | 1651d116 | Michael Hanselmann | @param ieioargs: Input/output arguments
|
2735 | 1651d116 | Michael Hanselmann |
|
2736 | 1651d116 | Michael Hanselmann | """
|
2737 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
2738 | 1651d116 | Michael Hanselmann | prefix = "import"
|
2739 | 1651d116 | Michael Hanselmann | |
2740 | 1651d116 | Michael Hanselmann | if not (host is None and port is None): |
2741 | 1651d116 | Michael Hanselmann | _Fail("Can not specify host or port on import")
|
2742 | 1651d116 | Michael Hanselmann | |
2743 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
2744 | 1651d116 | Michael Hanselmann | prefix = "export"
|
2745 | 1651d116 | Michael Hanselmann | |
2746 | 1651d116 | Michael Hanselmann | if host is None or port is None: |
2747 | 1651d116 | Michael Hanselmann | _Fail("Host and port must be specified for an export")
|
2748 | 1651d116 | Michael Hanselmann | |
2749 | 1651d116 | Michael Hanselmann | else:
|
2750 | 1651d116 | Michael Hanselmann | _Fail("Invalid mode %r", mode)
|
2751 | 1651d116 | Michael Hanselmann | |
2752 | eb630f50 | Michael Hanselmann | if (opts.key_name is None) ^ (opts.ca_pem is None): |
2753 | 1651d116 | Michael Hanselmann | _Fail("Cluster certificate can only be used for both key and CA")
|
2754 | 1651d116 | Michael Hanselmann | |
2755 | 2ad5550d | Michael Hanselmann | (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \ |
2756 | 1651d116 | Michael Hanselmann | _GetImportExportIoCommand(instance, mode, ieio, ieioargs) |
2757 | 1651d116 | Michael Hanselmann | |
2758 | eb630f50 | Michael Hanselmann | if opts.key_name is None: |
2759 | 1651d116 | Michael Hanselmann | # Use server.pem
|
2760 | 1651d116 | Michael Hanselmann | key_path = constants.NODED_CERT_FILE |
2761 | 1651d116 | Michael Hanselmann | cert_path = constants.NODED_CERT_FILE |
2762 | eb630f50 | Michael Hanselmann | assert opts.ca_pem is None |
2763 | 1651d116 | Michael Hanselmann | else:
|
2764 | 1651d116 | Michael Hanselmann | (_, key_path, cert_path) = _GetX509Filenames(constants.CRYPTO_KEYS_DIR, |
2765 | eb630f50 | Michael Hanselmann | opts.key_name) |
2766 | eb630f50 | Michael Hanselmann | assert opts.ca_pem is not None |
2767 | 1651d116 | Michael Hanselmann | |
2768 | 63bcea2a | Michael Hanselmann | for i in [key_path, cert_path]: |
2769 | dcaabc4f | Michael Hanselmann | if not os.path.exists(i): |
2770 | 63bcea2a | Michael Hanselmann | _Fail("File '%s' does not exist" % i)
|
2771 | 63bcea2a | Michael Hanselmann | |
2772 | 1651d116 | Michael Hanselmann | status_dir = _CreateImportExportStatusDir(prefix) |
2773 | 1651d116 | Michael Hanselmann | try:
|
2774 | 1651d116 | Michael Hanselmann | status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE) |
2775 | 1651d116 | Michael Hanselmann | pid_file = utils.PathJoin(status_dir, _IES_PID_FILE) |
2776 | 63bcea2a | Michael Hanselmann | ca_file = utils.PathJoin(status_dir, _IES_CA_FILE) |
2777 | 1651d116 | Michael Hanselmann | |
2778 | eb630f50 | Michael Hanselmann | if opts.ca_pem is None: |
2779 | 1651d116 | Michael Hanselmann | # Use server.pem
|
2780 | 63bcea2a | Michael Hanselmann | ca = utils.ReadFile(constants.NODED_CERT_FILE) |
2781 | eb630f50 | Michael Hanselmann | else:
|
2782 | eb630f50 | Michael Hanselmann | ca = opts.ca_pem |
2783 | 63bcea2a | Michael Hanselmann | |
2784 | eb630f50 | Michael Hanselmann | # Write CA file
|
2785 | 63bcea2a | Michael Hanselmann | utils.WriteFile(ca_file, data=ca, mode=0400)
|
2786 | 1651d116 | Michael Hanselmann | |
2787 | 1651d116 | Michael Hanselmann | cmd = [ |
2788 | 1651d116 | Michael Hanselmann | constants.IMPORT_EXPORT_DAEMON, |
2789 | 1651d116 | Michael Hanselmann | status_file, mode, |
2790 | 1651d116 | Michael Hanselmann | "--key=%s" % key_path,
|
2791 | 1651d116 | Michael Hanselmann | "--cert=%s" % cert_path,
|
2792 | 63bcea2a | Michael Hanselmann | "--ca=%s" % ca_file,
|
2793 | 1651d116 | Michael Hanselmann | ] |
2794 | 1651d116 | Michael Hanselmann | |
2795 | 1651d116 | Michael Hanselmann | if host:
|
2796 | 1651d116 | Michael Hanselmann | cmd.append("--host=%s" % host)
|
2797 | 1651d116 | Michael Hanselmann | |
2798 | 1651d116 | Michael Hanselmann | if port:
|
2799 | 1651d116 | Michael Hanselmann | cmd.append("--port=%s" % port)
|
2800 | 1651d116 | Michael Hanselmann | |
2801 | a5310c2a | Michael Hanselmann | if opts.compress:
|
2802 | a5310c2a | Michael Hanselmann | cmd.append("--compress=%s" % opts.compress)
|
2803 | a5310c2a | Michael Hanselmann | |
2804 | af1d39b1 | Michael Hanselmann | if opts.magic:
|
2805 | af1d39b1 | Michael Hanselmann | cmd.append("--magic=%s" % opts.magic)
|
2806 | af1d39b1 | Michael Hanselmann | |
2807 | 2ad5550d | Michael Hanselmann | if exp_size is not None: |
2808 | 2ad5550d | Michael Hanselmann | cmd.append("--expected-size=%s" % exp_size)
|
2809 | 2ad5550d | Michael Hanselmann | |
2810 | 1651d116 | Michael Hanselmann | if cmd_prefix:
|
2811 | 1651d116 | Michael Hanselmann | cmd.append("--cmd-prefix=%s" % cmd_prefix)
|
2812 | 1651d116 | Michael Hanselmann | |
2813 | 1651d116 | Michael Hanselmann | if cmd_suffix:
|
2814 | 1651d116 | Michael Hanselmann | cmd.append("--cmd-suffix=%s" % cmd_suffix)
|
2815 | 1651d116 | Michael Hanselmann | |
2816 | 1651d116 | Michael Hanselmann | logfile = _InstanceLogName(prefix, instance.os, instance.name) |
2817 | 1651d116 | Michael Hanselmann | |
2818 | 1651d116 | Michael Hanselmann | # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
|
2819 | 1651d116 | Michael Hanselmann | # support for receiving a file descriptor for output
|
2820 | 1651d116 | Michael Hanselmann | utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file, |
2821 | 1651d116 | Michael Hanselmann | output=logfile) |
2822 | 1651d116 | Michael Hanselmann | |
2823 | 1651d116 | Michael Hanselmann | # The import/export name is simply the status directory name
|
2824 | 1651d116 | Michael Hanselmann | return os.path.basename(status_dir)
|
2825 | 1651d116 | Michael Hanselmann | |
2826 | 1651d116 | Michael Hanselmann | except Exception: |
2827 | 1651d116 | Michael Hanselmann | shutil.rmtree(status_dir, ignore_errors=True)
|
2828 | 1651d116 | Michael Hanselmann | raise
|
2829 | 1651d116 | Michael Hanselmann | |
2830 | 1651d116 | Michael Hanselmann | |
2831 | 1651d116 | Michael Hanselmann | def GetImportExportStatus(names): |
2832 | 1651d116 | Michael Hanselmann | """Returns import/export daemon status.
|
2833 | 1651d116 | Michael Hanselmann |
|
2834 | 1651d116 | Michael Hanselmann | @type names: sequence
|
2835 | 1651d116 | Michael Hanselmann | @param names: List of names
|
2836 | 1651d116 | Michael Hanselmann | @rtype: List of dicts
|
2837 | 1651d116 | Michael Hanselmann | @return: Returns a list of the state of each named import/export or None if a
|
2838 | 1651d116 | Michael Hanselmann | status couldn't be read
|
2839 | 1651d116 | Michael Hanselmann |
|
2840 | 1651d116 | Michael Hanselmann | """
|
2841 | 1651d116 | Michael Hanselmann | result = [] |
2842 | 1651d116 | Michael Hanselmann | |
2843 | 1651d116 | Michael Hanselmann | for name in names: |
2844 | 1651d116 | Michael Hanselmann | status_file = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name, |
2845 | 1651d116 | Michael Hanselmann | _IES_STATUS_FILE) |
2846 | 1651d116 | Michael Hanselmann | |
2847 | 1651d116 | Michael Hanselmann | try:
|
2848 | 1651d116 | Michael Hanselmann | data = utils.ReadFile(status_file) |
2849 | 1651d116 | Michael Hanselmann | except EnvironmentError, err: |
2850 | 1651d116 | Michael Hanselmann | if err.errno != errno.ENOENT:
|
2851 | 1651d116 | Michael Hanselmann | raise
|
2852 | 1651d116 | Michael Hanselmann | data = None
|
2853 | 1651d116 | Michael Hanselmann | |
2854 | 1651d116 | Michael Hanselmann | if not data: |
2855 | 1651d116 | Michael Hanselmann | result.append(None)
|
2856 | 1651d116 | Michael Hanselmann | continue
|
2857 | 1651d116 | Michael Hanselmann | |
2858 | 1651d116 | Michael Hanselmann | result.append(serializer.LoadJson(data)) |
2859 | 1651d116 | Michael Hanselmann | |
2860 | 1651d116 | Michael Hanselmann | return result
|
2861 | 1651d116 | Michael Hanselmann | |
2862 | 1651d116 | Michael Hanselmann | |
2863 | f81c4737 | Michael Hanselmann | def AbortImportExport(name): |
2864 | f81c4737 | Michael Hanselmann | """Sends SIGTERM to a running import/export daemon.
|
2865 | f81c4737 | Michael Hanselmann |
|
2866 | f81c4737 | Michael Hanselmann | """
|
2867 | f81c4737 | Michael Hanselmann | logging.info("Abort import/export %s", name)
|
2868 | f81c4737 | Michael Hanselmann | |
2869 | f81c4737 | Michael Hanselmann | status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name) |
2870 | f81c4737 | Michael Hanselmann | pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE)) |
2871 | f81c4737 | Michael Hanselmann | |
2872 | f81c4737 | Michael Hanselmann | if pid:
|
2873 | f81c4737 | Michael Hanselmann | logging.info("Import/export %s is running with PID %s, sending SIGTERM",
|
2874 | f81c4737 | Michael Hanselmann | name, pid) |
2875 | 560cbec1 | Michael Hanselmann | utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM) |
2876 | f81c4737 | Michael Hanselmann | |
2877 | f81c4737 | Michael Hanselmann | |
2878 | 1651d116 | Michael Hanselmann | def CleanupImportExport(name): |
2879 | 1651d116 | Michael Hanselmann | """Cleanup after an import or export.
|
2880 | 1651d116 | Michael Hanselmann |
|
2881 | 1651d116 | Michael Hanselmann | If the import/export daemon is still running it's killed. Afterwards the
|
2882 | 1651d116 | Michael Hanselmann | whole status directory is removed.
|
2883 | 1651d116 | Michael Hanselmann |
|
2884 | 1651d116 | Michael Hanselmann | """
|
2885 | 1651d116 | Michael Hanselmann | logging.info("Finalizing import/export %s", name)
|
2886 | 1651d116 | Michael Hanselmann | |
2887 | 1651d116 | Michael Hanselmann | status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name) |
2888 | 1651d116 | Michael Hanselmann | |
2889 | debed9ae | Michael Hanselmann | pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE)) |
2890 | 1651d116 | Michael Hanselmann | |
2891 | 1651d116 | Michael Hanselmann | if pid:
|
2892 | 1651d116 | Michael Hanselmann | logging.info("Import/export %s is still running with PID %s",
|
2893 | 1651d116 | Michael Hanselmann | name, pid) |
2894 | 1651d116 | Michael Hanselmann | utils.KillProcess(pid, waitpid=False)
|
2895 | 1651d116 | Michael Hanselmann | |
2896 | 1651d116 | Michael Hanselmann | shutil.rmtree(status_dir, ignore_errors=True)
|
2897 | 1651d116 | Michael Hanselmann | |
2898 | 1651d116 | Michael Hanselmann | |
2899 | 6b93ec9d | Iustin Pop | def _FindDisks(nodes_ip, disks): |
2900 | 6b93ec9d | Iustin Pop | """Sets the physical ID on disks and returns the block devices.
|
2901 | 6b93ec9d | Iustin Pop |
|
2902 | 6b93ec9d | Iustin Pop | """
|
2903 | 6b93ec9d | Iustin Pop | # set the correct physical ID
|
2904 | b705c7a6 | Manuel Franceschini | my_name = netutils.Hostname.GetSysName() |
2905 | 6b93ec9d | Iustin Pop | for cf in disks: |
2906 | 6b93ec9d | Iustin Pop | cf.SetPhysicalID(my_name, nodes_ip) |
2907 | 6b93ec9d | Iustin Pop | |
2908 | 6b93ec9d | Iustin Pop | bdevs = [] |
2909 | 6b93ec9d | Iustin Pop | |
2910 | 6b93ec9d | Iustin Pop | for cf in disks: |
2911 | 6b93ec9d | Iustin Pop | rd = _RecursiveFindBD(cf) |
2912 | 6b93ec9d | Iustin Pop | if rd is None: |
2913 | 5a533f8a | Iustin Pop | _Fail("Can't find device %s", cf)
|
2914 | 6b93ec9d | Iustin Pop | bdevs.append(rd) |
2915 | 5a533f8a | Iustin Pop | return bdevs
|
2916 | 6b93ec9d | Iustin Pop | |
2917 | 6b93ec9d | Iustin Pop | |
2918 | 6b93ec9d | Iustin Pop | def DrbdDisconnectNet(nodes_ip, disks): |
2919 | 6b93ec9d | Iustin Pop | """Disconnects the network on a list of drbd devices.
|
2920 | 6b93ec9d | Iustin Pop |
|
2921 | 6b93ec9d | Iustin Pop | """
|
2922 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
2923 | 6b93ec9d | Iustin Pop | |
2924 | 6b93ec9d | Iustin Pop | # disconnect disks
|
2925 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
2926 | 6b93ec9d | Iustin Pop | try:
|
2927 | 6b93ec9d | Iustin Pop | rd.DisconnectNet() |
2928 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
2929 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration to standalone mode: %s",
|
2930 | 2cc6781a | Iustin Pop | err, exc=True)
|
2931 | 6b93ec9d | Iustin Pop | |
2932 | 6b93ec9d | Iustin Pop | |
2933 | 6b93ec9d | Iustin Pop | def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster): |
2934 | 6b93ec9d | Iustin Pop | """Attaches the network on a list of drbd devices.
|
2935 | 6b93ec9d | Iustin Pop |
|
2936 | 6b93ec9d | Iustin Pop | """
|
2937 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
2938 | 6b93ec9d | Iustin Pop | |
2939 | 6b93ec9d | Iustin Pop | if multimaster:
|
2940 | 53c776b5 | Iustin Pop | for idx, rd in enumerate(bdevs): |
2941 | 6b93ec9d | Iustin Pop | try:
|
2942 | 53c776b5 | Iustin Pop | _SymlinkBlockDev(instance_name, rd.dev_path, idx) |
2943 | 6b93ec9d | Iustin Pop | except EnvironmentError, err: |
2944 | 2cc6781a | Iustin Pop | _Fail("Can't create symlink: %s", err)
|
2945 | 6b93ec9d | Iustin Pop | # reconnect disks, switch to new master configuration and if
|
2946 | 6b93ec9d | Iustin Pop | # needed primary mode
|
2947 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
2948 | 6b93ec9d | Iustin Pop | try:
|
2949 | 6b93ec9d | Iustin Pop | rd.AttachNet(multimaster) |
2950 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
2951 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration: %s", err)
|
2952 | 3c0cdc83 | Michael Hanselmann | |
2953 | 6b93ec9d | Iustin Pop | # wait until the disks are connected; we need to retry the re-attach
|
2954 | 6b93ec9d | Iustin Pop | # if the device becomes standalone, as this might happen if the one
|
2955 | 6b93ec9d | Iustin Pop | # node disconnects and reconnects in a different mode before the
|
2956 | 6b93ec9d | Iustin Pop | # other node reconnects; in this case, one or both of the nodes will
|
2957 | 6b93ec9d | Iustin Pop | # decide it has wrong configuration and switch to standalone
|
2958 | 3c0cdc83 | Michael Hanselmann | |
2959 | 3c0cdc83 | Michael Hanselmann | def _Attach(): |
2960 | 6b93ec9d | Iustin Pop | all_connected = True
|
2961 | 3c0cdc83 | Michael Hanselmann | |
2962 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
2963 | 6b93ec9d | Iustin Pop | stats = rd.GetProcStatus() |
2964 | 3c0cdc83 | Michael Hanselmann | |
2965 | 3c0cdc83 | Michael Hanselmann | all_connected = (all_connected and
|
2966 | 3c0cdc83 | Michael Hanselmann | (stats.is_connected or stats.is_in_resync))
|
2967 | 3c0cdc83 | Michael Hanselmann | |
2968 | 6b93ec9d | Iustin Pop | if stats.is_standalone:
|
2969 | 6b93ec9d | Iustin Pop | # peer had different config info and this node became
|
2970 | 6b93ec9d | Iustin Pop | # standalone, even though this should not happen with the
|
2971 | 6b93ec9d | Iustin Pop | # new staged way of changing disk configs
|
2972 | 6b93ec9d | Iustin Pop | try:
|
2973 | c738375b | Iustin Pop | rd.AttachNet(multimaster) |
2974 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
2975 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration: %s", err)
|
2976 | 3c0cdc83 | Michael Hanselmann | |
2977 | 3c0cdc83 | Michael Hanselmann | if not all_connected: |
2978 | 3c0cdc83 | Michael Hanselmann | raise utils.RetryAgain()
|
2979 | 3c0cdc83 | Michael Hanselmann | |
2980 | 3c0cdc83 | Michael Hanselmann | try:
|
2981 | 3c0cdc83 | Michael Hanselmann | # Start with a delay of 100 miliseconds and go up to 5 seconds
|
2982 | 3c0cdc83 | Michael Hanselmann | utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60) |
2983 | 3c0cdc83 | Michael Hanselmann | except utils.RetryTimeout:
|
2984 | afdc3985 | Iustin Pop | _Fail("Timeout in disk reconnecting")
|
2985 | 3c0cdc83 | Michael Hanselmann | |
2986 | 6b93ec9d | Iustin Pop | if multimaster:
|
2987 | 6b93ec9d | Iustin Pop | # change to primary mode
|
2988 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
2989 | d3da87b8 | Iustin Pop | try:
|
2990 | d3da87b8 | Iustin Pop | rd.Open() |
2991 | d3da87b8 | Iustin Pop | except errors.BlockDeviceError, err:
|
2992 | 2cc6781a | Iustin Pop | _Fail("Can't change to primary mode: %s", err)
|
2993 | 6b93ec9d | Iustin Pop | |
2994 | 6b93ec9d | Iustin Pop | |
2995 | 6b93ec9d | Iustin Pop | def DrbdWaitSync(nodes_ip, disks): |
2996 | 6b93ec9d | Iustin Pop | """Wait until DRBDs have synchronized.
|
2997 | 6b93ec9d | Iustin Pop |
|
2998 | 6b93ec9d | Iustin Pop | """
|
2999 | db8667b7 | Iustin Pop | def _helper(rd): |
3000 | db8667b7 | Iustin Pop | stats = rd.GetProcStatus() |
3001 | db8667b7 | Iustin Pop | if not (stats.is_connected or stats.is_in_resync): |
3002 | db8667b7 | Iustin Pop | raise utils.RetryAgain()
|
3003 | db8667b7 | Iustin Pop | return stats
|
3004 | db8667b7 | Iustin Pop | |
3005 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
3006 | 6b93ec9d | Iustin Pop | |
3007 | 6b93ec9d | Iustin Pop | min_resync = 100
|
3008 | 6b93ec9d | Iustin Pop | alldone = True
|
3009 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3010 | db8667b7 | Iustin Pop | try:
|
3011 | db8667b7 | Iustin Pop | # poll each second for 15 seconds
|
3012 | db8667b7 | Iustin Pop | stats = utils.Retry(_helper, 1, 15, args=[rd]) |
3013 | db8667b7 | Iustin Pop | except utils.RetryTimeout:
|
3014 | db8667b7 | Iustin Pop | stats = rd.GetProcStatus() |
3015 | db8667b7 | Iustin Pop | # last check
|
3016 | db8667b7 | Iustin Pop | if not (stats.is_connected or stats.is_in_resync): |
3017 | db8667b7 | Iustin Pop | _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
|
3018 | 6b93ec9d | Iustin Pop | alldone = alldone and (not stats.is_in_resync) |
3019 | 6b93ec9d | Iustin Pop | if stats.sync_percent is not None: |
3020 | 6b93ec9d | Iustin Pop | min_resync = min(min_resync, stats.sync_percent)
|
3021 | afdc3985 | Iustin Pop | |
3022 | c26a6bd2 | Iustin Pop | return (alldone, min_resync)
|
3023 | 6b93ec9d | Iustin Pop | |
3024 | 6b93ec9d | Iustin Pop | |
3025 | c46b9782 | Luca Bigliardi | def GetDrbdUsermodeHelper(): |
3026 | c46b9782 | Luca Bigliardi | """Returns DRBD usermode helper currently configured.
|
3027 | c46b9782 | Luca Bigliardi |
|
3028 | c46b9782 | Luca Bigliardi | """
|
3029 | c46b9782 | Luca Bigliardi | try:
|
3030 | c46b9782 | Luca Bigliardi | return bdev.BaseDRBD.GetUsermodeHelper()
|
3031 | c46b9782 | Luca Bigliardi | except errors.BlockDeviceError, err:
|
3032 | c46b9782 | Luca Bigliardi | _Fail(str(err))
|
3033 | c46b9782 | Luca Bigliardi | |
3034 | c46b9782 | Luca Bigliardi | |
3035 | f5118ade | Iustin Pop | def PowercycleNode(hypervisor_type): |
3036 | f5118ade | Iustin Pop | """Hard-powercycle the node.
|
3037 | f5118ade | Iustin Pop |
|
3038 | f5118ade | Iustin Pop | Because we need to return first, and schedule the powercycle in the
|
3039 | f5118ade | Iustin Pop | background, we won't be able to report failures nicely.
|
3040 | f5118ade | Iustin Pop |
|
3041 | f5118ade | Iustin Pop | """
|
3042 | f5118ade | Iustin Pop | hyper = hypervisor.GetHypervisor(hypervisor_type) |
3043 | f5118ade | Iustin Pop | try:
|
3044 | f5118ade | Iustin Pop | pid = os.fork() |
3045 | 29921401 | Iustin Pop | except OSError: |
3046 | f5118ade | Iustin Pop | # if we can't fork, we'll pretend that we're in the child process
|
3047 | f5118ade | Iustin Pop | pid = 0
|
3048 | f5118ade | Iustin Pop | if pid > 0: |
3049 | c26a6bd2 | Iustin Pop | return "Reboot scheduled in 5 seconds" |
3050 | 1af6ac0f | Luca Bigliardi | # ensure the child is running on ram
|
3051 | 1af6ac0f | Luca Bigliardi | try:
|
3052 | 1af6ac0f | Luca Bigliardi | utils.Mlockall() |
3053 | 20601361 | Luca Bigliardi | except Exception: # pylint: disable-msg=W0703 |
3054 | 1af6ac0f | Luca Bigliardi | pass
|
3055 | f5118ade | Iustin Pop | time.sleep(5)
|
3056 | f5118ade | Iustin Pop | hyper.PowercycleNode() |
3057 | f5118ade | Iustin Pop | |
3058 | f5118ade | Iustin Pop | |
3059 | a8083063 | Iustin Pop | class HooksRunner(object): |
3060 | a8083063 | Iustin Pop | """Hook runner.
|
3061 | a8083063 | Iustin Pop |
|
3062 | 10c2650b | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not
|
3063 | 10c2650b | Iustin Pop | on the master side.
|
3064 | a8083063 | Iustin Pop |
|
3065 | a8083063 | Iustin Pop | """
|
3066 | a8083063 | Iustin Pop | def __init__(self, hooks_base_dir=None): |
3067 | a8083063 | Iustin Pop | """Constructor for hooks runner.
|
3068 | a8083063 | Iustin Pop |
|
3069 | 10c2650b | Iustin Pop | @type hooks_base_dir: str or None
|
3070 | 10c2650b | Iustin Pop | @param hooks_base_dir: if not None, this overrides the
|
3071 | 10c2650b | Iustin Pop | L{constants.HOOKS_BASE_DIR} (useful for unittests)
|
3072 | a8083063 | Iustin Pop |
|
3073 | a8083063 | Iustin Pop | """
|
3074 | a8083063 | Iustin Pop | if hooks_base_dir is None: |
3075 | a8083063 | Iustin Pop | hooks_base_dir = constants.HOOKS_BASE_DIR |
3076 | fe267188 | Iustin Pop | # yeah, _BASE_DIR is not valid for attributes, we use it like a
|
3077 | fe267188 | Iustin Pop | # constant
|
3078 | fe267188 | Iustin Pop | self._BASE_DIR = hooks_base_dir # pylint: disable-msg=C0103 |
3079 | a8083063 | Iustin Pop | |
3080 | a8083063 | Iustin Pop | def RunHooks(self, hpath, phase, env): |
3081 | a8083063 | Iustin Pop | """Run the scripts in the hooks directory.
|
3082 | a8083063 | Iustin Pop |
|
3083 | 10c2650b | Iustin Pop | @type hpath: str
|
3084 | 10c2650b | Iustin Pop | @param hpath: the path to the hooks directory which
|
3085 | 10c2650b | Iustin Pop | holds the scripts
|
3086 | 10c2650b | Iustin Pop | @type phase: str
|
3087 | 10c2650b | Iustin Pop | @param phase: either L{constants.HOOKS_PHASE_PRE} or
|
3088 | 10c2650b | Iustin Pop | L{constants.HOOKS_PHASE_POST}
|
3089 | 10c2650b | Iustin Pop | @type env: dict
|
3090 | 10c2650b | Iustin Pop | @param env: dictionary with the environment for the hook
|
3091 | 10c2650b | Iustin Pop | @rtype: list
|
3092 | 10c2650b | Iustin Pop | @return: list of 3-element tuples:
|
3093 | 10c2650b | Iustin Pop | - script path
|
3094 | 10c2650b | Iustin Pop | - script result, either L{constants.HKR_SUCCESS} or
|
3095 | 10c2650b | Iustin Pop | L{constants.HKR_FAIL}
|
3096 | 10c2650b | Iustin Pop | - output of the script
|
3097 | 10c2650b | Iustin Pop |
|
3098 | 10c2650b | Iustin Pop | @raise errors.ProgrammerError: for invalid input
|
3099 | 10c2650b | Iustin Pop | parameters
|
3100 | a8083063 | Iustin Pop |
|
3101 | a8083063 | Iustin Pop | """
|
3102 | a8083063 | Iustin Pop | if phase == constants.HOOKS_PHASE_PRE:
|
3103 | a8083063 | Iustin Pop | suffix = "pre"
|
3104 | a8083063 | Iustin Pop | elif phase == constants.HOOKS_PHASE_POST:
|
3105 | a8083063 | Iustin Pop | suffix = "post"
|
3106 | a8083063 | Iustin Pop | else:
|
3107 | 3fb4f740 | Iustin Pop | _Fail("Unknown hooks phase '%s'", phase)
|
3108 | 3fb4f740 | Iustin Pop | |
3109 | a8083063 | Iustin Pop | |
3110 | a8083063 | Iustin Pop | subdir = "%s-%s.d" % (hpath, suffix)
|
3111 | 0411c011 | Iustin Pop | dir_name = utils.PathJoin(self._BASE_DIR, subdir)
|
3112 | 6bb65e3a | Guido Trotter | |
3113 | 6bb65e3a | Guido Trotter | results = [] |
3114 | a9b7e346 | Iustin Pop | |
3115 | a9b7e346 | Iustin Pop | if not os.path.isdir(dir_name): |
3116 | a9b7e346 | Iustin Pop | # for non-existing/non-dirs, we simply exit instead of logging a
|
3117 | a9b7e346 | Iustin Pop | # warning at every operation
|
3118 | a9b7e346 | Iustin Pop | return results
|
3119 | a9b7e346 | Iustin Pop | |
3120 | a9b7e346 | Iustin Pop | runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
|
3121 | a9b7e346 | Iustin Pop | |
3122 | 6bb65e3a | Guido Trotter | for (relname, relstatus, runresult) in runparts_results: |
3123 | 6bb65e3a | Guido Trotter | if relstatus == constants.RUNPARTS_SKIP:
|
3124 | a8083063 | Iustin Pop | rrval = constants.HKR_SKIP |
3125 | a8083063 | Iustin Pop | output = ""
|
3126 | 6bb65e3a | Guido Trotter | elif relstatus == constants.RUNPARTS_ERR:
|
3127 | 6bb65e3a | Guido Trotter | rrval = constants.HKR_FAIL |
3128 | 6bb65e3a | Guido Trotter | output = "Hook script execution error: %s" % runresult
|
3129 | 6bb65e3a | Guido Trotter | elif relstatus == constants.RUNPARTS_RUN:
|
3130 | 6bb65e3a | Guido Trotter | if runresult.failed:
|
3131 | a8083063 | Iustin Pop | rrval = constants.HKR_FAIL |
3132 | a8083063 | Iustin Pop | else:
|
3133 | 6bb65e3a | Guido Trotter | rrval = constants.HKR_SUCCESS |
3134 | 6bb65e3a | Guido Trotter | output = utils.SafeEncode(runresult.output.strip()) |
3135 | 6bb65e3a | Guido Trotter | results.append(("%s/%s" % (subdir, relname), rrval, output))
|
3136 | 6bb65e3a | Guido Trotter | |
3137 | 6bb65e3a | Guido Trotter | return results
|
3138 | 3f78eef2 | Iustin Pop | |
3139 | 3f78eef2 | Iustin Pop | |
3140 | 8d528b7c | Iustin Pop | class IAllocatorRunner(object): |
3141 | 8d528b7c | Iustin Pop | """IAllocator runner.
|
3142 | 8d528b7c | Iustin Pop |
|
3143 | 8d528b7c | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not on
|
3144 | 8d528b7c | Iustin Pop | the master side.
|
3145 | 8d528b7c | Iustin Pop |
|
3146 | 8d528b7c | Iustin Pop | """
|
3147 | 7e950d31 | Iustin Pop | @staticmethod
|
3148 | 7e950d31 | Iustin Pop | def Run(name, idata): |
3149 | 8d528b7c | Iustin Pop | """Run an iallocator script.
|
3150 | 8d528b7c | Iustin Pop |
|
3151 | 10c2650b | Iustin Pop | @type name: str
|
3152 | 10c2650b | Iustin Pop | @param name: the iallocator script name
|
3153 | 10c2650b | Iustin Pop | @type idata: str
|
3154 | 10c2650b | Iustin Pop | @param idata: the allocator input data
|
3155 | 10c2650b | Iustin Pop |
|
3156 | 10c2650b | Iustin Pop | @rtype: tuple
|
3157 | 87f5c298 | Iustin Pop | @return: two element tuple of:
|
3158 | 87f5c298 | Iustin Pop | - status
|
3159 | 87f5c298 | Iustin Pop | - either error message or stdout of allocator (for success)
|
3160 | 8d528b7c | Iustin Pop |
|
3161 | 8d528b7c | Iustin Pop | """
|
3162 | 8d528b7c | Iustin Pop | alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH, |
3163 | 8d528b7c | Iustin Pop | os.path.isfile) |
3164 | 8d528b7c | Iustin Pop | if alloc_script is None: |
3165 | 87f5c298 | Iustin Pop | _Fail("iallocator module '%s' not found in the search path", name)
|
3166 | 8d528b7c | Iustin Pop | |
3167 | 8d528b7c | Iustin Pop | fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
|
3168 | 8d528b7c | Iustin Pop | try:
|
3169 | 8d528b7c | Iustin Pop | os.write(fd, idata) |
3170 | 8d528b7c | Iustin Pop | os.close(fd) |
3171 | 8d528b7c | Iustin Pop | result = utils.RunCmd([alloc_script, fin_name]) |
3172 | 8d528b7c | Iustin Pop | if result.failed:
|
3173 | 87f5c298 | Iustin Pop | _Fail("iallocator module '%s' failed: %s, output '%s'",
|
3174 | 87f5c298 | Iustin Pop | name, result.fail_reason, result.output) |
3175 | 8d528b7c | Iustin Pop | finally:
|
3176 | 8d528b7c | Iustin Pop | os.unlink(fin_name) |
3177 | 8d528b7c | Iustin Pop | |
3178 | c26a6bd2 | Iustin Pop | return result.stdout
|
3179 | 8d528b7c | Iustin Pop | |
3180 | 8d528b7c | Iustin Pop | |
3181 | 3f78eef2 | Iustin Pop | class DevCacheManager(object): |
3182 | c99a3cc0 | Manuel Franceschini | """Simple class for managing a cache of block device information.
|
3183 | 3f78eef2 | Iustin Pop |
|
3184 | 3f78eef2 | Iustin Pop | """
|
3185 | 3f78eef2 | Iustin Pop | _DEV_PREFIX = "/dev/"
|
3186 | 3f78eef2 | Iustin Pop | _ROOT_DIR = constants.BDEV_CACHE_DIR |
3187 | 3f78eef2 | Iustin Pop | |
3188 | 3f78eef2 | Iustin Pop | @classmethod
|
3189 | 3f78eef2 | Iustin Pop | def _ConvertPath(cls, dev_path): |
3190 | 3f78eef2 | Iustin Pop | """Converts a /dev/name path to the cache file name.
|
3191 | 3f78eef2 | Iustin Pop |
|
3192 | 3f78eef2 | Iustin Pop | This replaces slashes with underscores and strips the /dev
|
3193 | 10c2650b | Iustin Pop | prefix. It then returns the full path to the cache file.
|
3194 | 10c2650b | Iustin Pop |
|
3195 | 10c2650b | Iustin Pop | @type dev_path: str
|
3196 | 10c2650b | Iustin Pop | @param dev_path: the C{/dev/} path name
|
3197 | 10c2650b | Iustin Pop | @rtype: str
|
3198 | 10c2650b | Iustin Pop | @return: the converted path name
|
3199 | 3f78eef2 | Iustin Pop |
|
3200 | 3f78eef2 | Iustin Pop | """
|
3201 | 3f78eef2 | Iustin Pop | if dev_path.startswith(cls._DEV_PREFIX):
|
3202 | 3f78eef2 | Iustin Pop | dev_path = dev_path[len(cls._DEV_PREFIX):]
|
3203 | 3f78eef2 | Iustin Pop | dev_path = dev_path.replace("/", "_") |
3204 | 0411c011 | Iustin Pop | fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
|
3205 | 3f78eef2 | Iustin Pop | return fpath
|
3206 | 3f78eef2 | Iustin Pop | |
3207 | 3f78eef2 | Iustin Pop | @classmethod
|
3208 | 3f78eef2 | Iustin Pop | def UpdateCache(cls, dev_path, owner, on_primary, iv_name): |
3209 | 3f78eef2 | Iustin Pop | """Updates the cache information for a given device.
|
3210 | 3f78eef2 | Iustin Pop |
|
3211 | 10c2650b | Iustin Pop | @type dev_path: str
|
3212 | 10c2650b | Iustin Pop | @param dev_path: the pathname of the device
|
3213 | 10c2650b | Iustin Pop | @type owner: str
|
3214 | 10c2650b | Iustin Pop | @param owner: the owner (instance name) of the device
|
3215 | 10c2650b | Iustin Pop | @type on_primary: bool
|
3216 | 10c2650b | Iustin Pop | @param on_primary: whether this is the primary
|
3217 | 10c2650b | Iustin Pop | node nor not
|
3218 | 10c2650b | Iustin Pop | @type iv_name: str
|
3219 | 10c2650b | Iustin Pop | @param iv_name: the instance-visible name of the
|
3220 | c41eea6e | Iustin Pop | device, as in objects.Disk.iv_name
|
3221 | 10c2650b | Iustin Pop |
|
3222 | 10c2650b | Iustin Pop | @rtype: None
|
3223 | 10c2650b | Iustin Pop |
|
3224 | 3f78eef2 | Iustin Pop | """
|
3225 | cf5a8306 | Iustin Pop | if dev_path is None: |
3226 | 18682bca | Iustin Pop | logging.error("DevCacheManager.UpdateCache got a None dev_path")
|
3227 | cf5a8306 | Iustin Pop | return
|
3228 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
3229 | 3f78eef2 | Iustin Pop | if on_primary:
|
3230 | 3f78eef2 | Iustin Pop | state = "primary"
|
3231 | 3f78eef2 | Iustin Pop | else:
|
3232 | 3f78eef2 | Iustin Pop | state = "secondary"
|
3233 | 3f78eef2 | Iustin Pop | if iv_name is None: |
3234 | 3f78eef2 | Iustin Pop | iv_name = "not_visible"
|
3235 | 3f78eef2 | Iustin Pop | fdata = "%s %s %s\n" % (str(owner), state, iv_name) |
3236 | 3f78eef2 | Iustin Pop | try:
|
3237 | 3f78eef2 | Iustin Pop | utils.WriteFile(fpath, data=fdata) |
3238 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
3239 | 29921401 | Iustin Pop | logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
|
3240 | 3f78eef2 | Iustin Pop | |
3241 | 3f78eef2 | Iustin Pop | @classmethod
|
3242 | 3f78eef2 | Iustin Pop | def RemoveCache(cls, dev_path): |
3243 | 3f78eef2 | Iustin Pop | """Remove data for a dev_path.
|
3244 | 3f78eef2 | Iustin Pop |
|
3245 | 10c2650b | Iustin Pop | This is just a wrapper over L{utils.RemoveFile} with a converted
|
3246 | 10c2650b | Iustin Pop | path name and logging.
|
3247 | 10c2650b | Iustin Pop |
|
3248 | 10c2650b | Iustin Pop | @type dev_path: str
|
3249 | 10c2650b | Iustin Pop | @param dev_path: the pathname of the device
|
3250 | 10c2650b | Iustin Pop |
|
3251 | 10c2650b | Iustin Pop | @rtype: None
|
3252 | 10c2650b | Iustin Pop |
|
3253 | 3f78eef2 | Iustin Pop | """
|
3254 | cf5a8306 | Iustin Pop | if dev_path is None: |
3255 | 18682bca | Iustin Pop | logging.error("DevCacheManager.RemoveCache got a None dev_path")
|
3256 | cf5a8306 | Iustin Pop | return
|
3257 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
3258 | 3f78eef2 | Iustin Pop | try:
|
3259 | 3f78eef2 | Iustin Pop | utils.RemoveFile(fpath) |
3260 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
3261 | 29921401 | Iustin Pop | logging.exception("Can't update bdev cache for %s: %s", dev_path, err) |