root / lib / backend.py @ 3361ab37
History | View | Annotate | Download (129.7 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | 45bc4635 | Iustin Pop | # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 | b459a848 | Andrea Spadaccini | # pylint: disable=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 | cde49218 | Helga Velroyen | from ganeti.storage import bdev |
58 | cde49218 | Helga Velroyen | from ganeti.storage import drbd |
59 | a8083063 | Iustin Pop | from ganeti import objects |
60 | 880478f8 | Iustin Pop | from ganeti import ssconf |
61 | 1651d116 | Michael Hanselmann | from ganeti import serializer |
62 | a744b676 | Manuel Franceschini | from ganeti import netutils |
63 | 82b22e19 | René Nussbaumer | from ganeti import runtime |
64 | 3ccd3243 | Andrea Spadaccini | from ganeti import compat |
65 | 710f30ec | Michael Hanselmann | from ganeti import pathutils |
66 | cffbbae7 | Michael Hanselmann | from ganeti import vcluster |
67 | 99e222b1 | Michael Hanselmann | from ganeti import ht |
68 | cde49218 | Helga Velroyen | from ganeti.storage.base import BlockDev |
69 | cde49218 | Helga Velroyen | from ganeti.storage.drbd import DRBD8 |
70 | 68d95757 | Guido Trotter | from ganeti import hooksmaster |
71 | a8083063 | Iustin Pop | |
72 | a8083063 | Iustin Pop | |
73 | 13998ef2 | Michael Hanselmann | _BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
|
74 | b8028dcf | Michael Hanselmann | _ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([ |
75 | 710f30ec | Michael Hanselmann | pathutils.DATA_DIR, |
76 | 710f30ec | Michael Hanselmann | pathutils.JOB_QUEUE_ARCHIVE_DIR, |
77 | 710f30ec | Michael Hanselmann | pathutils.QUEUE_DIR, |
78 | 710f30ec | Michael Hanselmann | pathutils.CRYPTO_KEYS_DIR, |
79 | 714ea7ca | Iustin Pop | ]) |
80 | f942a838 | Michael Hanselmann | _MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60 |
81 | f942a838 | Michael Hanselmann | _X509_KEY_FILE = "key"
|
82 | f942a838 | Michael Hanselmann | _X509_CERT_FILE = "cert"
|
83 | 1651d116 | Michael Hanselmann | _IES_STATUS_FILE = "status"
|
84 | 1651d116 | Michael Hanselmann | _IES_PID_FILE = "pid"
|
85 | 1651d116 | Michael Hanselmann | _IES_CA_FILE = "ca"
|
86 | 13998ef2 | Michael Hanselmann | |
87 | 0b5303da | Iustin Pop | #: Valid LVS output line regex
|
88 | a1f38213 | Iustin Pop | _LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
|
89 | 0b5303da | Iustin Pop | |
90 | 702eff21 | Andrea Spadaccini | # Actions for the master setup script
|
91 | 702eff21 | Andrea Spadaccini | _MASTER_START = "start"
|
92 | 702eff21 | Andrea Spadaccini | _MASTER_STOP = "stop"
|
93 | 702eff21 | Andrea Spadaccini | |
94 | 45bc4635 | Iustin Pop | #: Maximum file permissions for restricted command directory and executables
|
95 | 1a2eb2dc | Michael Hanselmann | _RCMD_MAX_MODE = (stat.S_IRWXU | |
96 | 1a2eb2dc | Michael Hanselmann | stat.S_IRGRP | stat.S_IXGRP | |
97 | 1a2eb2dc | Michael Hanselmann | stat.S_IROTH | stat.S_IXOTH) |
98 | 1a2eb2dc | Michael Hanselmann | |
99 | 45bc4635 | Iustin Pop | #: Delay before returning an error for restricted commands
|
100 | 1a2eb2dc | Michael Hanselmann | _RCMD_INVALID_DELAY = 10
|
101 | 1a2eb2dc | Michael Hanselmann | |
102 | 45bc4635 | Iustin Pop | #: How long to wait to acquire lock for restricted commands (shorter than
|
103 | 1a2eb2dc | Michael Hanselmann | #: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
|
104 | 1a2eb2dc | Michael Hanselmann | #: command requests arrive
|
105 | 1a2eb2dc | Michael Hanselmann | _RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
|
106 | 1a2eb2dc | Michael Hanselmann | |
107 | 13998ef2 | Michael Hanselmann | |
108 | 2cc6781a | Iustin Pop | class RPCFail(Exception): |
109 | 2cc6781a | Iustin Pop | """Class denoting RPC failure.
|
110 | 2cc6781a | Iustin Pop |
|
111 | 2cc6781a | Iustin Pop | Its argument is the error message.
|
112 | 2cc6781a | Iustin Pop |
|
113 | 2cc6781a | Iustin Pop | """
|
114 | 2cc6781a | Iustin Pop | |
115 | 13998ef2 | Michael Hanselmann | |
116 | 584ea340 | Michele Tartara | def _GetInstReasonFilename(instance_name): |
117 | a59d5fa1 | Michele Tartara | """Path of the file containing the reason of the instance status change.
|
118 | a59d5fa1 | Michele Tartara |
|
119 | a59d5fa1 | Michele Tartara | @type instance_name: string
|
120 | a59d5fa1 | Michele Tartara | @param instance_name: The name of the instance
|
121 | a59d5fa1 | Michele Tartara | @rtype: string
|
122 | a59d5fa1 | Michele Tartara | @return: The path of the file
|
123 | a59d5fa1 | Michele Tartara |
|
124 | a59d5fa1 | Michele Tartara | """
|
125 | a59d5fa1 | Michele Tartara | return utils.PathJoin(pathutils.INSTANCE_REASON_DIR, instance_name)
|
126 | a59d5fa1 | Michele Tartara | |
127 | a59d5fa1 | Michele Tartara | |
128 | 584ea340 | Michele Tartara | def _StoreInstReasonTrail(instance_name, trail): |
129 | 584ea340 | Michele Tartara | """Serialize a reason trail related to an instance change of state to file.
|
130 | 584ea340 | Michele Tartara |
|
131 | 584ea340 | Michele Tartara | The exact location of the file depends on the name of the instance and on
|
132 | 584ea340 | Michele Tartara | the configuration of the Ganeti cluster defined at deploy time.
|
133 | 584ea340 | Michele Tartara |
|
134 | 584ea340 | Michele Tartara | @type instance_name: string
|
135 | 584ea340 | Michele Tartara | @param instance_name: The name of the instance
|
136 | 584ea340 | Michele Tartara | @rtype: None
|
137 | 584ea340 | Michele Tartara |
|
138 | 584ea340 | Michele Tartara | """
|
139 | 584ea340 | Michele Tartara | json = serializer.DumpJson(trail) |
140 | 584ea340 | Michele Tartara | filename = _GetInstReasonFilename(instance_name) |
141 | 584ea340 | Michele Tartara | utils.WriteFile(filename, data=json) |
142 | 584ea340 | Michele Tartara | |
143 | 584ea340 | Michele Tartara | |
144 | 2cc6781a | Iustin Pop | def _Fail(msg, *args, **kwargs): |
145 | 2cc6781a | Iustin Pop | """Log an error and the raise an RPCFail exception.
|
146 | 2cc6781a | Iustin Pop |
|
147 | 2cc6781a | Iustin Pop | This exception is then handled specially in the ganeti daemon and
|
148 | 2cc6781a | Iustin Pop | turned into a 'failed' return type. As such, this function is a
|
149 | 2cc6781a | Iustin Pop | useful shortcut for logging the error and returning it to the master
|
150 | 2cc6781a | Iustin Pop | daemon.
|
151 | 2cc6781a | Iustin Pop |
|
152 | 2cc6781a | Iustin Pop | @type msg: string
|
153 | 2cc6781a | Iustin Pop | @param msg: the text of the exception
|
154 | 2cc6781a | Iustin Pop | @raise RPCFail
|
155 | 2cc6781a | Iustin Pop |
|
156 | 2cc6781a | Iustin Pop | """
|
157 | 2cc6781a | Iustin Pop | if args:
|
158 | 2cc6781a | Iustin Pop | msg = msg % args |
159 | afdc3985 | Iustin Pop | if "log" not in kwargs or kwargs["log"]: # if we should log this error |
160 | afdc3985 | Iustin Pop | if "exc" in kwargs and kwargs["exc"]: |
161 | afdc3985 | Iustin Pop | logging.exception(msg) |
162 | afdc3985 | Iustin Pop | else:
|
163 | afdc3985 | Iustin Pop | logging.error(msg) |
164 | 2cc6781a | Iustin Pop | raise RPCFail(msg)
|
165 | 2cc6781a | Iustin Pop | |
166 | 2cc6781a | Iustin Pop | |
167 | c657dcc9 | Michael Hanselmann | def _GetConfig(): |
168 | 93384844 | Iustin Pop | """Simple wrapper to return a SimpleStore.
|
169 | 10c2650b | Iustin Pop |
|
170 | 93384844 | Iustin Pop | @rtype: L{ssconf.SimpleStore}
|
171 | 93384844 | Iustin Pop | @return: a SimpleStore instance
|
172 | 10c2650b | Iustin Pop |
|
173 | 10c2650b | Iustin Pop | """
|
174 | 93384844 | Iustin Pop | return ssconf.SimpleStore()
|
175 | c657dcc9 | Michael Hanselmann | |
176 | c657dcc9 | Michael Hanselmann | |
177 | 62c9ec92 | Iustin Pop | def _GetSshRunner(cluster_name): |
178 | 10c2650b | Iustin Pop | """Simple wrapper to return an SshRunner.
|
179 | 10c2650b | Iustin Pop |
|
180 | 10c2650b | Iustin Pop | @type cluster_name: str
|
181 | 10c2650b | Iustin Pop | @param cluster_name: the cluster name, which is needed
|
182 | 10c2650b | Iustin Pop | by the SshRunner constructor
|
183 | 10c2650b | Iustin Pop | @rtype: L{ssh.SshRunner}
|
184 | 10c2650b | Iustin Pop | @return: an SshRunner instance
|
185 | 10c2650b | Iustin Pop |
|
186 | 10c2650b | Iustin Pop | """
|
187 | 62c9ec92 | Iustin Pop | return ssh.SshRunner(cluster_name)
|
188 | c92b310a | Michael Hanselmann | |
189 | c92b310a | Michael Hanselmann | |
190 | 12bce260 | Michael Hanselmann | def _Decompress(data): |
191 | 12bce260 | Michael Hanselmann | """Unpacks data compressed by the RPC client.
|
192 | 12bce260 | Michael Hanselmann |
|
193 | 12bce260 | Michael Hanselmann | @type data: list or tuple
|
194 | 12bce260 | Michael Hanselmann | @param data: Data sent by RPC client
|
195 | 12bce260 | Michael Hanselmann | @rtype: str
|
196 | 12bce260 | Michael Hanselmann | @return: Decompressed data
|
197 | 12bce260 | Michael Hanselmann |
|
198 | 12bce260 | Michael Hanselmann | """
|
199 | 52e2f66e | Michael Hanselmann | assert isinstance(data, (list, tuple)) |
200 | 12bce260 | Michael Hanselmann | assert len(data) == 2 |
201 | 12bce260 | Michael Hanselmann | (encoding, content) = data |
202 | 12bce260 | Michael Hanselmann | if encoding == constants.RPC_ENCODING_NONE:
|
203 | 12bce260 | Michael Hanselmann | return content
|
204 | 12bce260 | Michael Hanselmann | elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
|
205 | 12bce260 | Michael Hanselmann | return zlib.decompress(base64.b64decode(content))
|
206 | 12bce260 | Michael Hanselmann | else:
|
207 | 12bce260 | Michael Hanselmann | raise AssertionError("Unknown data encoding") |
208 | 12bce260 | Michael Hanselmann | |
209 | 12bce260 | Michael Hanselmann | |
210 | 3bc6be5c | Iustin Pop | def _CleanDirectory(path, exclude=None): |
211 | 76ab5558 | Michael Hanselmann | """Removes all regular files in a directory.
|
212 | 76ab5558 | Michael Hanselmann |
|
213 | 10c2650b | Iustin Pop | @type path: str
|
214 | 10c2650b | Iustin Pop | @param path: the directory to clean
|
215 | 76ab5558 | Michael Hanselmann | @type exclude: list
|
216 | 10c2650b | Iustin Pop | @param exclude: list of files to be excluded, defaults
|
217 | 10c2650b | Iustin Pop | to the empty list
|
218 | 76ab5558 | Michael Hanselmann |
|
219 | 76ab5558 | Michael Hanselmann | """
|
220 | 714ea7ca | Iustin Pop | if path not in _ALLOWED_CLEAN_DIRS: |
221 | 714ea7ca | Iustin Pop | _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
|
222 | 714ea7ca | Iustin Pop | path) |
223 | 714ea7ca | Iustin Pop | |
224 | 3956cee1 | Michael Hanselmann | if not os.path.isdir(path): |
225 | 3956cee1 | Michael Hanselmann | return
|
226 | 3bc6be5c | Iustin Pop | if exclude is None: |
227 | 3bc6be5c | Iustin Pop | exclude = [] |
228 | 3bc6be5c | Iustin Pop | else:
|
229 | 3bc6be5c | Iustin Pop | # Normalize excluded paths
|
230 | 3bc6be5c | Iustin Pop | exclude = [os.path.normpath(i) for i in exclude] |
231 | 76ab5558 | Michael Hanselmann | |
232 | 3956cee1 | Michael Hanselmann | for rel_name in utils.ListVisibleFiles(path): |
233 | c4feafe8 | Iustin Pop | full_name = utils.PathJoin(path, rel_name) |
234 | 76ab5558 | Michael Hanselmann | if full_name in exclude: |
235 | 76ab5558 | Michael Hanselmann | continue
|
236 | 3956cee1 | Michael Hanselmann | if os.path.isfile(full_name) and not os.path.islink(full_name): |
237 | 3956cee1 | Michael Hanselmann | utils.RemoveFile(full_name) |
238 | 3956cee1 | Michael Hanselmann | |
239 | 3956cee1 | Michael Hanselmann | |
240 | 360b0dc2 | Iustin Pop | def _BuildUploadFileList(): |
241 | 360b0dc2 | Iustin Pop | """Build the list of allowed upload files.
|
242 | 360b0dc2 | Iustin Pop |
|
243 | 360b0dc2 | Iustin Pop | This is abstracted so that it's built only once at module import time.
|
244 | 360b0dc2 | Iustin Pop |
|
245 | 360b0dc2 | Iustin Pop | """
|
246 | b397a7d2 | Iustin Pop | allowed_files = set([
|
247 | 710f30ec | Michael Hanselmann | pathutils.CLUSTER_CONF_FILE, |
248 | ee045466 | Michael Hanselmann | pathutils.ETC_HOSTS, |
249 | 710f30ec | Michael Hanselmann | pathutils.SSH_KNOWN_HOSTS_FILE, |
250 | 710f30ec | Michael Hanselmann | pathutils.VNC_PASSWORD_FILE, |
251 | 710f30ec | Michael Hanselmann | pathutils.RAPI_CERT_FILE, |
252 | 710f30ec | Michael Hanselmann | pathutils.SPICE_CERT_FILE, |
253 | 710f30ec | Michael Hanselmann | pathutils.SPICE_CACERT_FILE, |
254 | 710f30ec | Michael Hanselmann | pathutils.RAPI_USERS_FILE, |
255 | 710f30ec | Michael Hanselmann | pathutils.CONFD_HMAC_KEY, |
256 | 710f30ec | Michael Hanselmann | pathutils.CLUSTER_DOMAIN_SECRET_FILE, |
257 | b397a7d2 | Iustin Pop | ]) |
258 | b397a7d2 | Iustin Pop | |
259 | b397a7d2 | Iustin Pop | for hv_name in constants.HYPER_TYPES: |
260 | e5a45a16 | Iustin Pop | hv_class = hypervisor.GetHypervisorClass(hv_name) |
261 | 69ab2e12 | Guido Trotter | allowed_files.update(hv_class.GetAncillaryFiles()[0])
|
262 | b397a7d2 | Iustin Pop | |
263 | 3439fd6b | Michael Hanselmann | assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \ |
264 | 3439fd6b | Michael Hanselmann | "Allowed file storage paths should never be uploaded via RPC"
|
265 | 3439fd6b | Michael Hanselmann | |
266 | b397a7d2 | Iustin Pop | return frozenset(allowed_files) |
267 | 360b0dc2 | Iustin Pop | |
268 | 360b0dc2 | Iustin Pop | |
269 | 360b0dc2 | Iustin Pop | _ALLOWED_UPLOAD_FILES = _BuildUploadFileList() |
270 | 360b0dc2 | Iustin Pop | |
271 | 360b0dc2 | Iustin Pop | |
272 | 1bc59f76 | Michael Hanselmann | def JobQueuePurge(): |
273 | 10c2650b | Iustin Pop | """Removes job queue files and archived jobs.
|
274 | 10c2650b | Iustin Pop |
|
275 | c8457ce7 | Iustin Pop | @rtype: tuple
|
276 | c8457ce7 | Iustin Pop | @return: True, None
|
277 | 24fc781f | Michael Hanselmann |
|
278 | 24fc781f | Michael Hanselmann | """
|
279 | 710f30ec | Michael Hanselmann | _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE]) |
280 | 710f30ec | Michael Hanselmann | _CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR) |
281 | 24fc781f | Michael Hanselmann | |
282 | 24fc781f | Michael Hanselmann | |
283 | bd1e4562 | Iustin Pop | def GetMasterInfo(): |
284 | bd1e4562 | Iustin Pop | """Returns master information.
|
285 | bd1e4562 | Iustin Pop |
|
286 | bd1e4562 | Iustin Pop | This is an utility function to compute master information, either
|
287 | bd1e4562 | Iustin Pop | for consumption here or from the node daemon.
|
288 | bd1e4562 | Iustin Pop |
|
289 | bd1e4562 | Iustin Pop | @rtype: tuple
|
290 | 909b3a0e | Andrea Spadaccini | @return: master_netdev, master_ip, master_name, primary_ip_family,
|
291 | 909b3a0e | Andrea Spadaccini | master_netmask
|
292 | 2a52a064 | Iustin Pop | @raise RPCFail: in case of errors
|
293 | b1b6ea87 | Iustin Pop |
|
294 | b1b6ea87 | Iustin Pop | """
|
295 | b1b6ea87 | Iustin Pop | try:
|
296 | c657dcc9 | Michael Hanselmann | cfg = _GetConfig() |
297 | c657dcc9 | Michael Hanselmann | master_netdev = cfg.GetMasterNetdev() |
298 | c657dcc9 | Michael Hanselmann | master_ip = cfg.GetMasterIP() |
299 | 5a8648eb | Andrea Spadaccini | master_netmask = cfg.GetMasterNetmask() |
300 | c657dcc9 | Michael Hanselmann | master_node = cfg.GetMasterNode() |
301 | d8e0caa6 | Manuel Franceschini | primary_ip_family = cfg.GetPrimaryIPFamily() |
302 | b1b6ea87 | Iustin Pop | except errors.ConfigurationError, err:
|
303 | 29921401 | Iustin Pop | _Fail("Cluster configuration incomplete: %s", err, exc=True) |
304 | 909b3a0e | Andrea Spadaccini | return (master_netdev, master_ip, master_node, primary_ip_family,
|
305 | 5ae4945a | Iustin Pop | master_netmask) |
306 | b1b6ea87 | Iustin Pop | |
307 | b1b6ea87 | Iustin Pop | |
308 | 0fa481f5 | Andrea Spadaccini | def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn): |
309 | 0fa481f5 | Andrea Spadaccini | """Decorator that runs hooks before and after the decorated function.
|
310 | 0fa481f5 | Andrea Spadaccini |
|
311 | 0fa481f5 | Andrea Spadaccini | @type hook_opcode: string
|
312 | 0fa481f5 | Andrea Spadaccini | @param hook_opcode: opcode of the hook
|
313 | 0fa481f5 | Andrea Spadaccini | @type hooks_path: string
|
314 | 0fa481f5 | Andrea Spadaccini | @param hooks_path: path of the hooks
|
315 | 0fa481f5 | Andrea Spadaccini | @type env_builder_fn: function
|
316 | 0fa481f5 | Andrea Spadaccini | @param env_builder_fn: function that returns a dictionary containing the
|
317 | 3ccd3243 | Andrea Spadaccini | environment variables for the hooks. Will get all the parameters of the
|
318 | 3ccd3243 | Andrea Spadaccini | decorated function.
|
319 | 0fa481f5 | Andrea Spadaccini | @raise RPCFail: in case of pre-hook failure
|
320 | 0fa481f5 | Andrea Spadaccini |
|
321 | 0fa481f5 | Andrea Spadaccini | """
|
322 | 0fa481f5 | Andrea Spadaccini | def decorator(fn): |
323 | 0fa481f5 | Andrea Spadaccini | def wrapper(*args, **kwargs): |
324 | 0fa481f5 | Andrea Spadaccini | _, myself = ssconf.GetMasterAndMyself() |
325 | 0fa481f5 | Andrea Spadaccini | nodes = ([myself], [myself]) # these hooks run locally
|
326 | 0fa481f5 | Andrea Spadaccini | |
327 | 3ccd3243 | Andrea Spadaccini | env_fn = compat.partial(env_builder_fn, *args, **kwargs) |
328 | 3ccd3243 | Andrea Spadaccini | |
329 | 0fa481f5 | Andrea Spadaccini | cfg = _GetConfig() |
330 | 0fa481f5 | Andrea Spadaccini | hr = HooksRunner() |
331 | 68d95757 | Guido Trotter | hm = hooksmaster.HooksMaster(hook_opcode, hooks_path, nodes, |
332 | 68d95757 | Guido Trotter | hr.RunLocalHooks, None, env_fn,
|
333 | 68d95757 | Guido Trotter | logging.warning, cfg.GetClusterName(), |
334 | 68d95757 | Guido Trotter | cfg.GetMasterNode()) |
335 | 0fa481f5 | Andrea Spadaccini | hm.RunPhase(constants.HOOKS_PHASE_PRE) |
336 | 0fa481f5 | Andrea Spadaccini | result = fn(*args, **kwargs) |
337 | 0fa481f5 | Andrea Spadaccini | hm.RunPhase(constants.HOOKS_PHASE_POST) |
338 | 0fa481f5 | Andrea Spadaccini | |
339 | 0fa481f5 | Andrea Spadaccini | return result
|
340 | 0fa481f5 | Andrea Spadaccini | return wrapper
|
341 | 0fa481f5 | Andrea Spadaccini | return decorator
|
342 | 0fa481f5 | Andrea Spadaccini | |
343 | 0fa481f5 | Andrea Spadaccini | |
344 | 57c7bc57 | Andrea Spadaccini | def _BuildMasterIpEnv(master_params, use_external_mip_script=None): |
345 | 2d88fdd3 | Andrea Spadaccini | """Builds environment variables for master IP hooks.
|
346 | 2d88fdd3 | Andrea Spadaccini |
|
347 | 3ccd3243 | Andrea Spadaccini | @type master_params: L{objects.MasterNetworkParameters}
|
348 | 3ccd3243 | Andrea Spadaccini | @param master_params: network parameters of the master
|
349 | 57c7bc57 | Andrea Spadaccini | @type use_external_mip_script: boolean
|
350 | 57c7bc57 | Andrea Spadaccini | @param use_external_mip_script: whether to use an external master IP
|
351 | 57c7bc57 | Andrea Spadaccini | address setup script (unused, but necessary per the implementation of the
|
352 | 57c7bc57 | Andrea Spadaccini | _RunLocalHooks decorator)
|
353 | 3ccd3243 | Andrea Spadaccini |
|
354 | 2d88fdd3 | Andrea Spadaccini | """
|
355 | 57c7bc57 | Andrea Spadaccini | # pylint: disable=W0613
|
356 | 3ccd3243 | Andrea Spadaccini | ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family) |
357 | 2d88fdd3 | Andrea Spadaccini | env = { |
358 | 3ccd3243 | Andrea Spadaccini | "MASTER_NETDEV": master_params.netdev,
|
359 | 3ccd3243 | Andrea Spadaccini | "MASTER_IP": master_params.ip,
|
360 | 702eff21 | Andrea Spadaccini | "MASTER_NETMASK": str(master_params.netmask), |
361 | 3ccd3243 | Andrea Spadaccini | "CLUSTER_IP_VERSION": str(ver), |
362 | 2d88fdd3 | Andrea Spadaccini | } |
363 | 2d88fdd3 | Andrea Spadaccini | |
364 | 2d88fdd3 | Andrea Spadaccini | return env
|
365 | 2d88fdd3 | Andrea Spadaccini | |
366 | 2d88fdd3 | Andrea Spadaccini | |
367 | 702eff21 | Andrea Spadaccini | def _RunMasterSetupScript(master_params, action, use_external_mip_script): |
368 | 702eff21 | Andrea Spadaccini | """Execute the master IP address setup script.
|
369 | 702eff21 | Andrea Spadaccini |
|
370 | 702eff21 | Andrea Spadaccini | @type master_params: L{objects.MasterNetworkParameters}
|
371 | 702eff21 | Andrea Spadaccini | @param master_params: network parameters of the master
|
372 | 702eff21 | Andrea Spadaccini | @type action: string
|
373 | 702eff21 | Andrea Spadaccini | @param action: action to pass to the script. Must be one of
|
374 | 702eff21 | Andrea Spadaccini | L{backend._MASTER_START} or L{backend._MASTER_STOP}
|
375 | 702eff21 | Andrea Spadaccini | @type use_external_mip_script: boolean
|
376 | 702eff21 | Andrea Spadaccini | @param use_external_mip_script: whether to use an external master IP
|
377 | 702eff21 | Andrea Spadaccini | address setup script
|
378 | 702eff21 | Andrea Spadaccini | @raise backend.RPCFail: if there are errors during the execution of the
|
379 | 702eff21 | Andrea Spadaccini | script
|
380 | 702eff21 | Andrea Spadaccini |
|
381 | 702eff21 | Andrea Spadaccini | """
|
382 | 702eff21 | Andrea Spadaccini | env = _BuildMasterIpEnv(master_params) |
383 | 702eff21 | Andrea Spadaccini | |
384 | 702eff21 | Andrea Spadaccini | if use_external_mip_script:
|
385 | 710f30ec | Michael Hanselmann | setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT |
386 | 702eff21 | Andrea Spadaccini | else:
|
387 | 710f30ec | Michael Hanselmann | setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT |
388 | 702eff21 | Andrea Spadaccini | |
389 | 702eff21 | Andrea Spadaccini | result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
|
390 | 702eff21 | Andrea Spadaccini | |
391 | 702eff21 | Andrea Spadaccini | if result.failed:
|
392 | 19e1b715 | Iustin Pop | _Fail("Failed to %s the master IP. Script return value: %s, output: '%s'" %
|
393 | 19e1b715 | Iustin Pop | (action, result.exit_code, result.output), log=True)
|
394 | 702eff21 | Andrea Spadaccini | |
395 | 702eff21 | Andrea Spadaccini | |
396 | 2d88fdd3 | Andrea Spadaccini | @RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup", |
397 | 3a3e4f1e | Andrea Spadaccini | _BuildMasterIpEnv) |
398 | 57c7bc57 | Andrea Spadaccini | def ActivateMasterIp(master_params, use_external_mip_script): |
399 | fb460cf7 | Andrea Spadaccini | """Activate the IP address of the master daemon.
|
400 | fb460cf7 | Andrea Spadaccini |
|
401 | c79198a0 | Andrea Spadaccini | @type master_params: L{objects.MasterNetworkParameters}
|
402 | c79198a0 | Andrea Spadaccini | @param master_params: network parameters of the master
|
403 | 57c7bc57 | Andrea Spadaccini | @type use_external_mip_script: boolean
|
404 | 57c7bc57 | Andrea Spadaccini | @param use_external_mip_script: whether to use an external master IP
|
405 | 57c7bc57 | Andrea Spadaccini | address setup script
|
406 | 702eff21 | Andrea Spadaccini | @raise RPCFail: in case of errors during the IP startup
|
407 | 8da2bd43 | Andrea Spadaccini |
|
408 | fb460cf7 | Andrea Spadaccini | """
|
409 | 702eff21 | Andrea Spadaccini | _RunMasterSetupScript(master_params, _MASTER_START, |
410 | 702eff21 | Andrea Spadaccini | use_external_mip_script) |
411 | fb460cf7 | Andrea Spadaccini | |
412 | fb460cf7 | Andrea Spadaccini | |
413 | fb460cf7 | Andrea Spadaccini | def StartMasterDaemons(no_voting): |
414 | a8083063 | Iustin Pop | """Activate local node as master node.
|
415 | a8083063 | Iustin Pop |
|
416 | fb460cf7 | Andrea Spadaccini | The function will start the master daemons (ganeti-masterd and ganeti-rapi).
|
417 | 10c2650b | Iustin Pop |
|
418 | 3583908a | Guido Trotter | @type no_voting: boolean
|
419 | 3583908a | Guido Trotter | @param no_voting: whether to start ganeti-masterd without a node vote
|
420 | fb460cf7 | Andrea Spadaccini | but still non-interactively
|
421 | 10c2650b | Iustin Pop | @rtype: None
|
422 | a8083063 | Iustin Pop |
|
423 | a8083063 | Iustin Pop | """
|
424 | a8083063 | Iustin Pop | |
425 | fb460cf7 | Andrea Spadaccini | if no_voting:
|
426 | fb460cf7 | Andrea Spadaccini | masterd_args = "--no-voting --yes-do-it"
|
427 | fb460cf7 | Andrea Spadaccini | else:
|
428 | fb460cf7 | Andrea Spadaccini | masterd_args = ""
|
429 | f154a7a3 | Michael Hanselmann | |
430 | fb460cf7 | Andrea Spadaccini | env = { |
431 | fb460cf7 | Andrea Spadaccini | "EXTRA_MASTERD_ARGS": masterd_args,
|
432 | fb460cf7 | Andrea Spadaccini | } |
433 | fb460cf7 | Andrea Spadaccini | |
434 | 710f30ec | Michael Hanselmann | result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
|
435 | fb460cf7 | Andrea Spadaccini | if result.failed:
|
436 | fb460cf7 | Andrea Spadaccini | msg = "Can't start Ganeti master: %s" % result.output
|
437 | fb460cf7 | Andrea Spadaccini | logging.error(msg) |
438 | fb460cf7 | Andrea Spadaccini | _Fail(msg) |
439 | f154a7a3 | Michael Hanselmann | |
440 | fb460cf7 | Andrea Spadaccini | |
441 | 2d88fdd3 | Andrea Spadaccini | @RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown", |
442 | 3a3e4f1e | Andrea Spadaccini | _BuildMasterIpEnv) |
443 | 57c7bc57 | Andrea Spadaccini | def DeactivateMasterIp(master_params, use_external_mip_script): |
444 | fb460cf7 | Andrea Spadaccini | """Deactivate the master IP on this node.
|
445 | a8083063 | Iustin Pop |
|
446 | c79198a0 | Andrea Spadaccini | @type master_params: L{objects.MasterNetworkParameters}
|
447 | c79198a0 | Andrea Spadaccini | @param master_params: network parameters of the master
|
448 | 57c7bc57 | Andrea Spadaccini | @type use_external_mip_script: boolean
|
449 | 57c7bc57 | Andrea Spadaccini | @param use_external_mip_script: whether to use an external master IP
|
450 | 57c7bc57 | Andrea Spadaccini | address setup script
|
451 | 702eff21 | Andrea Spadaccini | @raise RPCFail: in case of errors during the IP turndown
|
452 | 96e0d5cc | Andrea Spadaccini |
|
453 | a8083063 | Iustin Pop | """
|
454 | 702eff21 | Andrea Spadaccini | _RunMasterSetupScript(master_params, _MASTER_STOP, |
455 | 702eff21 | Andrea Spadaccini | use_external_mip_script) |
456 | b1b6ea87 | Iustin Pop | |
457 | fb460cf7 | Andrea Spadaccini | |
458 | fb460cf7 | Andrea Spadaccini | def StopMasterDaemons(): |
459 | fb460cf7 | Andrea Spadaccini | """Stop the master daemons on this node.
|
460 | fb460cf7 | Andrea Spadaccini |
|
461 | fb460cf7 | Andrea Spadaccini | Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
|
462 | fb460cf7 | Andrea Spadaccini |
|
463 | fb460cf7 | Andrea Spadaccini | @rtype: None
|
464 | fb460cf7 | Andrea Spadaccini |
|
465 | fb460cf7 | Andrea Spadaccini | """
|
466 | fb460cf7 | Andrea Spadaccini | # TODO: log and report back to the caller the error failures; we
|
467 | fb460cf7 | Andrea Spadaccini | # need to decide in which case we fail the RPC for this
|
468 | fb460cf7 | Andrea Spadaccini | |
469 | 710f30ec | Michael Hanselmann | result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
|
470 | fb460cf7 | Andrea Spadaccini | if result.failed:
|
471 | fb460cf7 | Andrea Spadaccini | logging.error("Could not stop Ganeti master, command %s had exitcode %s"
|
472 | fb460cf7 | Andrea Spadaccini | " and error %s",
|
473 | fb460cf7 | Andrea Spadaccini | result.cmd, result.exit_code, result.output) |
474 | a8083063 | Iustin Pop | |
475 | a8083063 | Iustin Pop | |
476 | 41e079ce | Andrea Spadaccini | def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev): |
477 | 5a8648eb | Andrea Spadaccini | """Change the netmask of the master IP.
|
478 | 5a8648eb | Andrea Spadaccini |
|
479 | 41e079ce | Andrea Spadaccini | @param old_netmask: the old value of the netmask
|
480 | 41e079ce | Andrea Spadaccini | @param netmask: the new value of the netmask
|
481 | 41e079ce | Andrea Spadaccini | @param master_ip: the master IP
|
482 | 41e079ce | Andrea Spadaccini | @param master_netdev: the master network device
|
483 | 41e079ce | Andrea Spadaccini |
|
484 | 5a8648eb | Andrea Spadaccini | """
|
485 | 5a8648eb | Andrea Spadaccini | if old_netmask == netmask:
|
486 | 5a8648eb | Andrea Spadaccini | return
|
487 | 5a8648eb | Andrea Spadaccini | |
488 | 9e6014b9 | Andrea Spadaccini | if not netutils.IPAddress.Own(master_ip): |
489 | 9e6014b9 | Andrea Spadaccini | _Fail("The master IP address is not up, not attempting to change its"
|
490 | 9e6014b9 | Andrea Spadaccini | " netmask")
|
491 | 9e6014b9 | Andrea Spadaccini | |
492 | 5a8648eb | Andrea Spadaccini | result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add", |
493 | 5a8648eb | Andrea Spadaccini | "%s/%s" % (master_ip, netmask),
|
494 | 5a8648eb | Andrea Spadaccini | "dev", master_netdev, "label", |
495 | 5a8648eb | Andrea Spadaccini | "%s:0" % master_netdev])
|
496 | 5a8648eb | Andrea Spadaccini | if result.failed:
|
497 | 9e6014b9 | Andrea Spadaccini | _Fail("Could not set the new netmask on the master IP address")
|
498 | 5a8648eb | Andrea Spadaccini | |
499 | 5a8648eb | Andrea Spadaccini | result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del", |
500 | 5a8648eb | Andrea Spadaccini | "%s/%s" % (master_ip, old_netmask),
|
501 | 5a8648eb | Andrea Spadaccini | "dev", master_netdev, "label", |
502 | 5a8648eb | Andrea Spadaccini | "%s:0" % master_netdev])
|
503 | 5a8648eb | Andrea Spadaccini | if result.failed:
|
504 | 9e6014b9 | Andrea Spadaccini | _Fail("Could not bring down the master IP address with the old netmask")
|
505 | 5a8648eb | Andrea Spadaccini | |
506 | 5a8648eb | Andrea Spadaccini | |
507 | 19ddc57a | René Nussbaumer | def EtcHostsModify(mode, host, ip): |
508 | 19ddc57a | René Nussbaumer | """Modify a host entry in /etc/hosts.
|
509 | 19ddc57a | René Nussbaumer |
|
510 | 19ddc57a | René Nussbaumer | @param mode: The mode to operate. Either add or remove entry
|
511 | 19ddc57a | René Nussbaumer | @param host: The host to operate on
|
512 | 19ddc57a | René Nussbaumer | @param ip: The ip associated with the entry
|
513 | 19ddc57a | René Nussbaumer |
|
514 | 19ddc57a | René Nussbaumer | """
|
515 | 19ddc57a | René Nussbaumer | if mode == constants.ETC_HOSTS_ADD:
|
516 | 19ddc57a | René Nussbaumer | if not ip: |
517 | 19ddc57a | René Nussbaumer | RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
|
518 | 19ddc57a | René Nussbaumer | " present")
|
519 | 19ddc57a | René Nussbaumer | utils.AddHostToEtcHosts(host, ip) |
520 | 19ddc57a | René Nussbaumer | elif mode == constants.ETC_HOSTS_REMOVE:
|
521 | 19ddc57a | René Nussbaumer | if ip:
|
522 | 19ddc57a | René Nussbaumer | RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
|
523 | 19ddc57a | René Nussbaumer | " parameter is present")
|
524 | 19ddc57a | René Nussbaumer | utils.RemoveHostFromEtcHosts(host) |
525 | 19ddc57a | René Nussbaumer | else:
|
526 | 19ddc57a | René Nussbaumer | RPCFail("Mode not supported")
|
527 | 19ddc57a | René Nussbaumer | |
528 | 19ddc57a | René Nussbaumer | |
529 | b989b9d9 | Ken Wehr | def LeaveCluster(modify_ssh_setup): |
530 | 10c2650b | Iustin Pop | """Cleans up and remove the current node.
|
531 | 10c2650b | Iustin Pop |
|
532 | 10c2650b | Iustin Pop | This function cleans up and prepares the current node to be removed
|
533 | 10c2650b | Iustin Pop | from the cluster.
|
534 | 10c2650b | Iustin Pop |
|
535 | 10c2650b | Iustin Pop | If processing is successful, then it raises an
|
536 | c41eea6e | Iustin Pop | L{errors.QuitGanetiException} which is used as a special case to
|
537 | 10c2650b | Iustin Pop | shutdown the node daemon.
|
538 | a8083063 | Iustin Pop |
|
539 | b989b9d9 | Ken Wehr | @param modify_ssh_setup: boolean
|
540 | b989b9d9 | Ken Wehr |
|
541 | a8083063 | Iustin Pop | """
|
542 | 710f30ec | Michael Hanselmann | _CleanDirectory(pathutils.DATA_DIR) |
543 | 710f30ec | Michael Hanselmann | _CleanDirectory(pathutils.CRYPTO_KEYS_DIR) |
544 | 1bc59f76 | Michael Hanselmann | JobQueuePurge() |
545 | f78346f5 | Michael Hanselmann | |
546 | b989b9d9 | Ken Wehr | if modify_ssh_setup:
|
547 | b989b9d9 | Ken Wehr | try:
|
548 | 052783ff | Michael Hanselmann | priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER) |
549 | 7900ed01 | Iustin Pop | |
550 | b989b9d9 | Ken Wehr | utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key)) |
551 | a8083063 | Iustin Pop | |
552 | b989b9d9 | Ken Wehr | utils.RemoveFile(priv_key) |
553 | b989b9d9 | Ken Wehr | utils.RemoveFile(pub_key) |
554 | b989b9d9 | Ken Wehr | except errors.OpExecError:
|
555 | b989b9d9 | Ken Wehr | logging.exception("Error while processing ssh files")
|
556 | a8083063 | Iustin Pop | |
557 | ed008420 | Guido Trotter | try:
|
558 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.CONFD_HMAC_KEY) |
559 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.RAPI_CERT_FILE) |
560 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.SPICE_CERT_FILE) |
561 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.SPICE_CACERT_FILE) |
562 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.NODED_CERT_FILE) |
563 | b459a848 | Andrea Spadaccini | except: # pylint: disable=W0702 |
564 | ed008420 | Guido Trotter | logging.exception("Error while removing cluster secrets")
|
565 | ed008420 | Guido Trotter | |
566 | 710f30ec | Michael Hanselmann | result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
|
567 | f154a7a3 | Michael Hanselmann | if result.failed:
|
568 | f154a7a3 | Michael Hanselmann | logging.error("Command %s failed with exitcode %s and error %s",
|
569 | f154a7a3 | Michael Hanselmann | result.cmd, result.exit_code, result.output) |
570 | ed008420 | Guido Trotter | |
571 | 0623d351 | Iustin Pop | # Raise a custom exception (handled in ganeti-noded)
|
572 | d0c8c01d | Iustin Pop | raise errors.QuitGanetiException(True, "Shutdown scheduled") |
573 | 6d8b6238 | Guido Trotter | |
574 | a8083063 | Iustin Pop | |
575 | 1a3c5d4e | Bernardo Dal Seno | def _GetVgInfo(name, excl_stor): |
576 | 78519c10 | Michael Hanselmann | """Retrieves information about a LVM volume group.
|
577 | 78519c10 | Michael Hanselmann |
|
578 | 78519c10 | Michael Hanselmann | """
|
579 | 78519c10 | Michael Hanselmann | # TODO: GetVGInfo supports returning information for multiple VGs at once
|
580 | 1a3c5d4e | Bernardo Dal Seno | vginfo = bdev.LogicalVolume.GetVGInfo([name], excl_stor) |
581 | 78519c10 | Michael Hanselmann | if vginfo:
|
582 | 78519c10 | Michael Hanselmann | vg_free = int(round(vginfo[0][0], 0)) |
583 | 78519c10 | Michael Hanselmann | vg_size = int(round(vginfo[0][1], 0)) |
584 | 78519c10 | Michael Hanselmann | else:
|
585 | 78519c10 | Michael Hanselmann | vg_free = None
|
586 | 78519c10 | Michael Hanselmann | vg_size = None
|
587 | 78519c10 | Michael Hanselmann | |
588 | 78519c10 | Michael Hanselmann | return {
|
589 | 78519c10 | Michael Hanselmann | "name": name,
|
590 | 1e89a135 | Michael Hanselmann | "vg_free": vg_free,
|
591 | 1e89a135 | Michael Hanselmann | "vg_size": vg_size,
|
592 | 78519c10 | Michael Hanselmann | } |
593 | 78519c10 | Michael Hanselmann | |
594 | 78519c10 | Michael Hanselmann | |
595 | a1860404 | Bernardo Dal Seno | def _GetVgSpindlesInfo(name, excl_stor): |
596 | a1860404 | Bernardo Dal Seno | """Retrieves information about spindles in an LVM volume group.
|
597 | a1860404 | Bernardo Dal Seno |
|
598 | a1860404 | Bernardo Dal Seno | @type name: string
|
599 | a1860404 | Bernardo Dal Seno | @param name: VG name
|
600 | a1860404 | Bernardo Dal Seno | @type excl_stor: bool
|
601 | a1860404 | Bernardo Dal Seno | @param excl_stor: exclusive storage
|
602 | a1860404 | Bernardo Dal Seno | @rtype: dict
|
603 | a1860404 | Bernardo Dal Seno | @return: dictionary whose keys are "name", "vg_free", "vg_size" for VG name,
|
604 | a1860404 | Bernardo Dal Seno | free spindles, total spindles respectively
|
605 | a1860404 | Bernardo Dal Seno |
|
606 | a1860404 | Bernardo Dal Seno | """
|
607 | a1860404 | Bernardo Dal Seno | if excl_stor:
|
608 | a1860404 | Bernardo Dal Seno | (vg_free, vg_size) = bdev.LogicalVolume.GetVgSpindlesInfo(name) |
609 | a1860404 | Bernardo Dal Seno | else:
|
610 | a1860404 | Bernardo Dal Seno | vg_free = 0
|
611 | a1860404 | Bernardo Dal Seno | vg_size = 0
|
612 | a1860404 | Bernardo Dal Seno | return {
|
613 | a1860404 | Bernardo Dal Seno | "name": name,
|
614 | a1860404 | Bernardo Dal Seno | "vg_free": vg_free,
|
615 | a1860404 | Bernardo Dal Seno | "vg_size": vg_size,
|
616 | a1860404 | Bernardo Dal Seno | } |
617 | a1860404 | Bernardo Dal Seno | |
618 | a1860404 | Bernardo Dal Seno | |
619 | 78519c10 | Michael Hanselmann | def _GetHvInfo(name): |
620 | 78519c10 | Michael Hanselmann | """Retrieves node information from a hypervisor.
|
621 | 78519c10 | Michael Hanselmann |
|
622 | 78519c10 | Michael Hanselmann | The information returned depends on the hypervisor. Common items:
|
623 | 78519c10 | Michael Hanselmann |
|
624 | 78519c10 | Michael Hanselmann | - vg_size is the size of the configured volume group in MiB
|
625 | 78519c10 | Michael Hanselmann | - vg_free is the free size of the volume group in MiB
|
626 | 78519c10 | Michael Hanselmann | - memory_dom0 is the memory allocated for domain0 in MiB
|
627 | 78519c10 | Michael Hanselmann | - memory_free is the currently available (free) ram in MiB
|
628 | 78519c10 | Michael Hanselmann | - memory_total is the total number of ram in MiB
|
629 | 78519c10 | Michael Hanselmann | - hv_version: the hypervisor version, if available
|
630 | 78519c10 | Michael Hanselmann |
|
631 | 78519c10 | Michael Hanselmann | """
|
632 | 78519c10 | Michael Hanselmann | return hypervisor.GetHypervisor(name).GetNodeInfo()
|
633 | 78519c10 | Michael Hanselmann | |
634 | 78519c10 | Michael Hanselmann | |
635 | 78519c10 | Michael Hanselmann | def _GetNamedNodeInfo(names, fn): |
636 | 78519c10 | Michael Hanselmann | """Calls C{fn} for all names in C{names} and returns a dictionary.
|
637 | 78519c10 | Michael Hanselmann |
|
638 | 78519c10 | Michael Hanselmann | @rtype: None or dict
|
639 | 78519c10 | Michael Hanselmann |
|
640 | 78519c10 | Michael Hanselmann | """
|
641 | 78519c10 | Michael Hanselmann | if names is None: |
642 | 78519c10 | Michael Hanselmann | return None |
643 | 78519c10 | Michael Hanselmann | else:
|
644 | ff3be305 | Michael Hanselmann | return map(fn, names) |
645 | 78519c10 | Michael Hanselmann | |
646 | 78519c10 | Michael Hanselmann | |
647 | 4b92e992 | Helga Velroyen | def GetNodeInfo(storage_units, hv_names, excl_stor): |
648 | 5bbd3f7f | Michael Hanselmann | """Gives back a hash with different information about the node.
|
649 | a8083063 | Iustin Pop |
|
650 | 4b92e992 | Helga Velroyen | @type storage_units: list of pairs (string, string)
|
651 | 4b92e992 | Helga Velroyen | @param storage_units: List of pairs (storage unit, identifier) to ask for disk
|
652 | 4b92e992 | Helga Velroyen | space information. In case of lvm-vg, the identifier is
|
653 | 4b92e992 | Helga Velroyen | the VG name.
|
654 | 78519c10 | Michael Hanselmann | @type hv_names: list of string
|
655 | 78519c10 | Michael Hanselmann | @param hv_names: Names of the hypervisors to ask for node information
|
656 | 1a3c5d4e | Bernardo Dal Seno | @type excl_stor: boolean
|
657 | 1a3c5d4e | Bernardo Dal Seno | @param excl_stor: Whether exclusive_storage is active
|
658 | 78519c10 | Michael Hanselmann | @rtype: tuple; (string, None/dict, None/dict)
|
659 | 78519c10 | Michael Hanselmann | @return: Tuple containing boot ID, volume group information and hypervisor
|
660 | 78519c10 | Michael Hanselmann | information
|
661 | a8083063 | Iustin Pop |
|
662 | 098c0958 | Michael Hanselmann | """
|
663 | 78519c10 | Michael Hanselmann | bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n") |
664 | 4b92e992 | Helga Velroyen | storage_info = _GetNamedNodeInfo( |
665 | 4b92e992 | Helga Velroyen | storage_units, |
666 | 4b92e992 | Helga Velroyen | (lambda storage_unit: _ApplyStorageInfoFunction(storage_unit[0], |
667 | 4b92e992 | Helga Velroyen | storage_unit[1],
|
668 | 4b92e992 | Helga Velroyen | excl_stor))) |
669 | 78519c10 | Michael Hanselmann | hv_info = _GetNamedNodeInfo(hv_names, _GetHvInfo) |
670 | 78519c10 | Michael Hanselmann | |
671 | 4b92e992 | Helga Velroyen | return (bootid, storage_info, hv_info)
|
672 | 4b92e992 | Helga Velroyen | |
673 | 4b92e992 | Helga Velroyen | |
674 | 4b92e992 | Helga Velroyen | # FIXME: implement storage reporting for all missing storage types.
|
675 | 4b92e992 | Helga Velroyen | _STORAGE_TYPE_INFO_FN = { |
676 | 4b92e992 | Helga Velroyen | constants.ST_BLOCK: None,
|
677 | 4b92e992 | Helga Velroyen | constants.ST_DISKLESS: None,
|
678 | 4b92e992 | Helga Velroyen | constants.ST_EXT: None,
|
679 | 4b92e992 | Helga Velroyen | constants.ST_FILE: None,
|
680 | a1860404 | Bernardo Dal Seno | constants.ST_LVM_PV: _GetVgSpindlesInfo, |
681 | 4b92e992 | Helga Velroyen | constants.ST_LVM_VG: _GetVgInfo, |
682 | 4b92e992 | Helga Velroyen | constants.ST_RADOS: None,
|
683 | 4b92e992 | Helga Velroyen | } |
684 | 4b92e992 | Helga Velroyen | |
685 | 4b92e992 | Helga Velroyen | |
686 | 4b92e992 | Helga Velroyen | def _ApplyStorageInfoFunction(storage_type, storage_key, *args): |
687 | 4b92e992 | Helga Velroyen | """Looks up and applies the correct function to calculate free and total
|
688 | 4b92e992 | Helga Velroyen | storage for the given storage type.
|
689 | 4b92e992 | Helga Velroyen |
|
690 | 4b92e992 | Helga Velroyen | @type storage_type: string
|
691 | 4b92e992 | Helga Velroyen | @param storage_type: the storage type for which the storage shall be reported.
|
692 | 4b92e992 | Helga Velroyen | @type storage_key: string
|
693 | 4b92e992 | Helga Velroyen | @param storage_key: identifier of a storage unit, e.g. the volume group name
|
694 | 4b92e992 | Helga Velroyen | of an LVM storage unit
|
695 | 4b92e992 | Helga Velroyen | @type args: any
|
696 | 4b92e992 | Helga Velroyen | @param args: various parameters that can be used for storage reporting. These
|
697 | 4b92e992 | Helga Velroyen | parameters and their semantics vary from storage type to storage type and
|
698 | 4b92e992 | Helga Velroyen | are just propagated in this function.
|
699 | 4b92e992 | Helga Velroyen | @return: the results of the application of the storage space function (see
|
700 | 4b92e992 | Helga Velroyen | _STORAGE_TYPE_INFO_FN) if storage space reporting is implemented for that
|
701 | 4b92e992 | Helga Velroyen | storage type
|
702 | 4b92e992 | Helga Velroyen | @raises NotImplementedError: for storage types who don't support space
|
703 | 4b92e992 | Helga Velroyen | reporting yet
|
704 | 4b92e992 | Helga Velroyen | """
|
705 | 4b92e992 | Helga Velroyen | fn = _STORAGE_TYPE_INFO_FN[storage_type] |
706 | 4b92e992 | Helga Velroyen | if fn is not None: |
707 | 4b92e992 | Helga Velroyen | return fn(storage_key, *args)
|
708 | 4b92e992 | Helga Velroyen | else:
|
709 | 4b92e992 | Helga Velroyen | raise NotImplementedError |
710 | a8083063 | Iustin Pop | |
711 | a8083063 | Iustin Pop | |
712 | d5a690cb | Bernardo Dal Seno | def _CheckExclusivePvs(pvi_list): |
713 | d5a690cb | Bernardo Dal Seno | """Check that PVs are not shared among LVs
|
714 | d5a690cb | Bernardo Dal Seno |
|
715 | d5a690cb | Bernardo Dal Seno | @type pvi_list: list of L{objects.LvmPvInfo} objects
|
716 | d5a690cb | Bernardo Dal Seno | @param pvi_list: information about the PVs
|
717 | d5a690cb | Bernardo Dal Seno |
|
718 | d5a690cb | Bernardo Dal Seno | @rtype: list of tuples (string, list of strings)
|
719 | d5a690cb | Bernardo Dal Seno | @return: offending volumes, as tuples: (pv_name, [lv1_name, lv2_name...])
|
720 | d5a690cb | Bernardo Dal Seno |
|
721 | d5a690cb | Bernardo Dal Seno | """
|
722 | d5a690cb | Bernardo Dal Seno | res = [] |
723 | d5a690cb | Bernardo Dal Seno | for pvi in pvi_list: |
724 | d5a690cb | Bernardo Dal Seno | if len(pvi.lv_list) > 1: |
725 | d5a690cb | Bernardo Dal Seno | res.append((pvi.name, pvi.lv_list)) |
726 | d5a690cb | Bernardo Dal Seno | return res
|
727 | d5a690cb | Bernardo Dal Seno | |
728 | d5a690cb | Bernardo Dal Seno | |
729 | 62c9ec92 | Iustin Pop | def VerifyNode(what, cluster_name): |
730 | a8083063 | Iustin Pop | """Verify the status of the local node.
|
731 | a8083063 | Iustin Pop |
|
732 | e69d05fd | Iustin Pop | Based on the input L{what} parameter, various checks are done on the
|
733 | e69d05fd | Iustin Pop | local node.
|
734 | e69d05fd | Iustin Pop |
|
735 | e69d05fd | Iustin Pop | If the I{filelist} key is present, this list of
|
736 | e69d05fd | Iustin Pop | files is checksummed and the file/checksum pairs are returned.
|
737 | e69d05fd | Iustin Pop |
|
738 | e69d05fd | Iustin Pop | If the I{nodelist} key is present, we check that we have
|
739 | e69d05fd | Iustin Pop | connectivity via ssh with the target nodes (and check the hostname
|
740 | e69d05fd | Iustin Pop | report).
|
741 | a8083063 | Iustin Pop |
|
742 | e69d05fd | Iustin Pop | If the I{node-net-test} key is present, we check that we have
|
743 | e69d05fd | Iustin Pop | connectivity to the given nodes via both primary IP and, if
|
744 | e69d05fd | Iustin Pop | applicable, secondary IPs.
|
745 | e69d05fd | Iustin Pop |
|
746 | e69d05fd | Iustin Pop | @type what: C{dict}
|
747 | e69d05fd | Iustin Pop | @param what: a dictionary of things to check:
|
748 | e69d05fd | Iustin Pop | - filelist: list of files for which to compute checksums
|
749 | e69d05fd | Iustin Pop | - nodelist: list of nodes we should check ssh communication with
|
750 | e69d05fd | Iustin Pop | - node-net-test: list of nodes we should check node daemon port
|
751 | e69d05fd | Iustin Pop | connectivity with
|
752 | e69d05fd | Iustin Pop | - hypervisor: list with hypervisors to run the verify for
|
753 | 10c2650b | Iustin Pop | @rtype: dict
|
754 | 10c2650b | Iustin Pop | @return: a dictionary with the same keys as the input dict, and
|
755 | 10c2650b | Iustin Pop | values representing the result of the checks
|
756 | a8083063 | Iustin Pop |
|
757 | a8083063 | Iustin Pop | """
|
758 | a8083063 | Iustin Pop | result = {} |
759 | b705c7a6 | Manuel Franceschini | my_name = netutils.Hostname.GetSysName() |
760 | a744b676 | Manuel Franceschini | port = netutils.GetDaemonPort(constants.NODED) |
761 | 8964ee14 | Iustin Pop | vm_capable = my_name not in what.get(constants.NV_VMNODES, []) |
762 | a8083063 | Iustin Pop | |
763 | 8964ee14 | Iustin Pop | if constants.NV_HYPERVISOR in what and vm_capable: |
764 | 25361b9a | Iustin Pop | result[constants.NV_HYPERVISOR] = tmp = {} |
765 | 25361b9a | Iustin Pop | for hv_name in what[constants.NV_HYPERVISOR]: |
766 | 0cf5e7f5 | Iustin Pop | try:
|
767 | 0cf5e7f5 | Iustin Pop | val = hypervisor.GetHypervisor(hv_name).Verify() |
768 | 0cf5e7f5 | Iustin Pop | except errors.HypervisorError, err:
|
769 | 0cf5e7f5 | Iustin Pop | val = "Error while checking hypervisor: %s" % str(err) |
770 | 0cf5e7f5 | Iustin Pop | tmp[hv_name] = val |
771 | 25361b9a | Iustin Pop | |
772 | 58a59652 | Iustin Pop | if constants.NV_HVPARAMS in what and vm_capable: |
773 | 58a59652 | Iustin Pop | result[constants.NV_HVPARAMS] = tmp = [] |
774 | 58a59652 | Iustin Pop | for source, hv_name, hvparms in what[constants.NV_HVPARAMS]: |
775 | 58a59652 | Iustin Pop | try:
|
776 | 58a59652 | Iustin Pop | logging.info("Validating hv %s, %s", hv_name, hvparms)
|
777 | 58a59652 | Iustin Pop | hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms) |
778 | 58a59652 | Iustin Pop | except errors.HypervisorError, err:
|
779 | 58a59652 | Iustin Pop | tmp.append((source, hv_name, str(err)))
|
780 | 58a59652 | Iustin Pop | |
781 | 25361b9a | Iustin Pop | if constants.NV_FILELIST in what: |
782 | 47130d50 | Michael Hanselmann | fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
|
783 | 47130d50 | Michael Hanselmann | what[constants.NV_FILELIST])) |
784 | 47130d50 | Michael Hanselmann | result[constants.NV_FILELIST] = \ |
785 | 47130d50 | Michael Hanselmann | dict((vcluster.MakeVirtualPath(key), value)
|
786 | 47130d50 | Michael Hanselmann | for (key, value) in fingerprints.items()) |
787 | 25361b9a | Iustin Pop | |
788 | 25361b9a | Iustin Pop | if constants.NV_NODELIST in what: |
789 | 64c7b383 | Michael Hanselmann | (nodes, bynode) = what[constants.NV_NODELIST] |
790 | 64c7b383 | Michael Hanselmann | |
791 | 64c7b383 | Michael Hanselmann | # Add nodes from other groups (different for each node)
|
792 | 64c7b383 | Michael Hanselmann | try:
|
793 | 64c7b383 | Michael Hanselmann | nodes.extend(bynode[my_name]) |
794 | 64c7b383 | Michael Hanselmann | except KeyError: |
795 | 64c7b383 | Michael Hanselmann | pass
|
796 | 64c7b383 | Michael Hanselmann | |
797 | 64c7b383 | Michael Hanselmann | # Use a random order
|
798 | 64c7b383 | Michael Hanselmann | random.shuffle(nodes) |
799 | 64c7b383 | Michael Hanselmann | |
800 | 64c7b383 | Michael Hanselmann | # Try to contact all nodes
|
801 | 64c7b383 | Michael Hanselmann | val = {} |
802 | 64c7b383 | Michael Hanselmann | for node in nodes: |
803 | 62c9ec92 | Iustin Pop | success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node) |
804 | a8083063 | Iustin Pop | if not success: |
805 | 64c7b383 | Michael Hanselmann | val[node] = message |
806 | 64c7b383 | Michael Hanselmann | |
807 | 64c7b383 | Michael Hanselmann | result[constants.NV_NODELIST] = val |
808 | 25361b9a | Iustin Pop | |
809 | 25361b9a | Iustin Pop | if constants.NV_NODENETTEST in what: |
810 | 25361b9a | Iustin Pop | result[constants.NV_NODENETTEST] = tmp = {} |
811 | 9d4bfc96 | Iustin Pop | my_pip = my_sip = None
|
812 | 25361b9a | Iustin Pop | for name, pip, sip in what[constants.NV_NODENETTEST]: |
813 | 9d4bfc96 | Iustin Pop | if name == my_name:
|
814 | 9d4bfc96 | Iustin Pop | my_pip = pip |
815 | 9d4bfc96 | Iustin Pop | my_sip = sip |
816 | 9d4bfc96 | Iustin Pop | break
|
817 | 9d4bfc96 | Iustin Pop | if not my_pip: |
818 | 25361b9a | Iustin Pop | tmp[my_name] = ("Can't find my own primary/secondary IP"
|
819 | 25361b9a | Iustin Pop | " in the node list")
|
820 | 9d4bfc96 | Iustin Pop | else:
|
821 | 25361b9a | Iustin Pop | for name, pip, sip in what[constants.NV_NODENETTEST]: |
822 | 9d4bfc96 | Iustin Pop | fail = [] |
823 | a744b676 | Manuel Franceschini | if not netutils.TcpPing(pip, port, source=my_pip): |
824 | 9d4bfc96 | Iustin Pop | fail.append("primary")
|
825 | 9d4bfc96 | Iustin Pop | if sip != pip:
|
826 | a744b676 | Manuel Franceschini | if not netutils.TcpPing(sip, port, source=my_sip): |
827 | 9d4bfc96 | Iustin Pop | fail.append("secondary")
|
828 | 9d4bfc96 | Iustin Pop | if fail:
|
829 | 25361b9a | Iustin Pop | tmp[name] = ("failure using the %s interface(s)" %
|
830 | 25361b9a | Iustin Pop | " and ".join(fail))
|
831 | 25361b9a | Iustin Pop | |
832 | a3a5f850 | Iustin Pop | if constants.NV_MASTERIP in what: |
833 | a3a5f850 | Iustin Pop | # FIXME: add checks on incoming data structures (here and in the
|
834 | a3a5f850 | Iustin Pop | # rest of the function)
|
835 | a3a5f850 | Iustin Pop | master_name, master_ip = what[constants.NV_MASTERIP] |
836 | a3a5f850 | Iustin Pop | if master_name == my_name:
|
837 | 9769bb78 | Manuel Franceschini | source = constants.IP4_ADDRESS_LOCALHOST |
838 | a3a5f850 | Iustin Pop | else:
|
839 | a3a5f850 | Iustin Pop | source = None
|
840 | a744b676 | Manuel Franceschini | result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port, |
841 | 5ae4945a | Iustin Pop | source=source) |
842 | a3a5f850 | Iustin Pop | |
843 | 17b0b812 | Andrea Spadaccini | if constants.NV_USERSCRIPTS in what: |
844 | 17b0b812 | Andrea Spadaccini | result[constants.NV_USERSCRIPTS] = \ |
845 | 17b0b812 | Andrea Spadaccini | [script for script in what[constants.NV_USERSCRIPTS] |
846 | 10b86782 | Michael Hanselmann | if not utils.IsExecutable(script)] |
847 | 17b0b812 | Andrea Spadaccini | |
848 | 16f41f24 | René Nussbaumer | if constants.NV_OOB_PATHS in what: |
849 | 16f41f24 | René Nussbaumer | result[constants.NV_OOB_PATHS] = tmp = [] |
850 | 16f41f24 | René Nussbaumer | for path in what[constants.NV_OOB_PATHS]: |
851 | 16f41f24 | René Nussbaumer | try:
|
852 | 16f41f24 | René Nussbaumer | st = os.stat(path) |
853 | 16f41f24 | René Nussbaumer | except OSError, err: |
854 | 16f41f24 | René Nussbaumer | tmp.append("error stating out of band helper: %s" % err)
|
855 | 16f41f24 | René Nussbaumer | else:
|
856 | 16f41f24 | René Nussbaumer | if stat.S_ISREG(st.st_mode):
|
857 | 16f41f24 | René Nussbaumer | if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
|
858 | 16f41f24 | René Nussbaumer | tmp.append(None)
|
859 | 16f41f24 | René Nussbaumer | else:
|
860 | 16f41f24 | René Nussbaumer | tmp.append("out of band helper %s is not executable" % path)
|
861 | 16f41f24 | René Nussbaumer | else:
|
862 | 16f41f24 | René Nussbaumer | tmp.append("out of band helper %s is not a file" % path)
|
863 | 16f41f24 | René Nussbaumer | |
864 | 8964ee14 | Iustin Pop | if constants.NV_LVLIST in what and vm_capable: |
865 | ed904904 | Iustin Pop | try:
|
866 | 84d7e26b | Dmitry Chernyak | val = GetVolumeList(utils.ListVolumeGroups().keys()) |
867 | ed904904 | Iustin Pop | except RPCFail, err:
|
868 | ed904904 | Iustin Pop | val = str(err)
|
869 | ed904904 | Iustin Pop | result[constants.NV_LVLIST] = val |
870 | 25361b9a | Iustin Pop | |
871 | 8964ee14 | Iustin Pop | if constants.NV_INSTANCELIST in what and vm_capable: |
872 | 0cf5e7f5 | Iustin Pop | # GetInstanceList can fail
|
873 | 0cf5e7f5 | Iustin Pop | try:
|
874 | 0cf5e7f5 | Iustin Pop | val = GetInstanceList(what[constants.NV_INSTANCELIST]) |
875 | 0cf5e7f5 | Iustin Pop | except RPCFail, err:
|
876 | 0cf5e7f5 | Iustin Pop | val = str(err)
|
877 | 0cf5e7f5 | Iustin Pop | result[constants.NV_INSTANCELIST] = val |
878 | 25361b9a | Iustin Pop | |
879 | 8964ee14 | Iustin Pop | if constants.NV_VGLIST in what and vm_capable: |
880 | e480923b | Iustin Pop | result[constants.NV_VGLIST] = utils.ListVolumeGroups() |
881 | 25361b9a | Iustin Pop | |
882 | 8964ee14 | Iustin Pop | if constants.NV_PVLIST in what and vm_capable: |
883 | d5a690cb | Bernardo Dal Seno | check_exclusive_pvs = constants.NV_EXCLUSIVEPVS in what
|
884 | 59726e15 | Bernardo Dal Seno | val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST], |
885 | d5a690cb | Bernardo Dal Seno | filter_allocatable=False,
|
886 | d5a690cb | Bernardo Dal Seno | include_lvs=check_exclusive_pvs) |
887 | d5a690cb | Bernardo Dal Seno | if check_exclusive_pvs:
|
888 | d5a690cb | Bernardo Dal Seno | result[constants.NV_EXCLUSIVEPVS] = _CheckExclusivePvs(val) |
889 | d5a690cb | Bernardo Dal Seno | for pvi in val: |
890 | d5a690cb | Bernardo Dal Seno | # Avoid sending useless data on the wire
|
891 | d5a690cb | Bernardo Dal Seno | pvi.lv_list = [] |
892 | 59726e15 | Bernardo Dal Seno | result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
|
893 | d091393e | Iustin Pop | |
894 | 25361b9a | Iustin Pop | if constants.NV_VERSION in what: |
895 | e9ce0a64 | Iustin Pop | result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION, |
896 | e9ce0a64 | Iustin Pop | constants.RELEASE_VERSION) |
897 | 25361b9a | Iustin Pop | |
898 | 8964ee14 | Iustin Pop | if constants.NV_HVINFO in what and vm_capable: |
899 | 25361b9a | Iustin Pop | hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO]) |
900 | 25361b9a | Iustin Pop | result[constants.NV_HVINFO] = hyper.GetNodeInfo() |
901 | 9d4bfc96 | Iustin Pop | |
902 | 5bb0a1cb | Thomas Thrainer | if constants.NV_DRBDVERSION in what and vm_capable: |
903 | 5bb0a1cb | Thomas Thrainer | try:
|
904 | 47e0abee | Thomas Thrainer | drbd_version = DRBD8.GetProcInfo().GetVersionString() |
905 | 5bb0a1cb | Thomas Thrainer | except errors.BlockDeviceError, err:
|
906 | 5bb0a1cb | Thomas Thrainer | logging.warning("Can't get DRBD version", exc_info=True) |
907 | 5bb0a1cb | Thomas Thrainer | drbd_version = str(err)
|
908 | 5bb0a1cb | Thomas Thrainer | result[constants.NV_DRBDVERSION] = drbd_version |
909 | 5bb0a1cb | Thomas Thrainer | |
910 | 8964ee14 | Iustin Pop | if constants.NV_DRBDLIST in what and vm_capable: |
911 | 6d2e83d5 | Iustin Pop | try:
|
912 | 47e0abee | Thomas Thrainer | used_minors = drbd.DRBD8.GetUsedDevs() |
913 | f6eaed12 | Iustin Pop | except errors.BlockDeviceError, err:
|
914 | 6d2e83d5 | Iustin Pop | logging.warning("Can't get used minors list", exc_info=True) |
915 | f6eaed12 | Iustin Pop | used_minors = str(err)
|
916 | 6d2e83d5 | Iustin Pop | result[constants.NV_DRBDLIST] = used_minors |
917 | 6d2e83d5 | Iustin Pop | |
918 | 8964ee14 | Iustin Pop | if constants.NV_DRBDHELPER in what and vm_capable: |
919 | 7ef40fbe | Luca Bigliardi | status = True
|
920 | 7ef40fbe | Luca Bigliardi | try:
|
921 | 47e0abee | Thomas Thrainer | payload = drbd.DRBD8.GetUsermodeHelper() |
922 | 7ef40fbe | Luca Bigliardi | except errors.BlockDeviceError, err:
|
923 | 7ef40fbe | Luca Bigliardi | logging.error("Can't get DRBD usermode helper: %s", str(err)) |
924 | 7ef40fbe | Luca Bigliardi | status = False
|
925 | 7ef40fbe | Luca Bigliardi | payload = str(err)
|
926 | 7ef40fbe | Luca Bigliardi | result[constants.NV_DRBDHELPER] = (status, payload) |
927 | 7ef40fbe | Luca Bigliardi | |
928 | 7c0aa8e9 | Iustin Pop | if constants.NV_NODESETUP in what: |
929 | 7c0aa8e9 | Iustin Pop | result[constants.NV_NODESETUP] = tmpr = [] |
930 | 7c0aa8e9 | Iustin Pop | if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"): |
931 | 7c0aa8e9 | Iustin Pop | tmpr.append("The sysfs filesytem doesn't seem to be mounted"
|
932 | 7c0aa8e9 | Iustin Pop | " under /sys, missing required directories /sys/block"
|
933 | 7c0aa8e9 | Iustin Pop | " and /sys/class/net")
|
934 | 7c0aa8e9 | Iustin Pop | if (not os.path.isdir("/proc/sys") or |
935 | 7c0aa8e9 | Iustin Pop | not os.path.isfile("/proc/sysrq-trigger")): |
936 | 7c0aa8e9 | Iustin Pop | tmpr.append("The procfs filesystem doesn't seem to be mounted"
|
937 | 7c0aa8e9 | Iustin Pop | " under /proc, missing required directory /proc/sys and"
|
938 | 7c0aa8e9 | Iustin Pop | " the file /proc/sysrq-trigger")
|
939 | 313b2dd4 | Michael Hanselmann | |
940 | 313b2dd4 | Michael Hanselmann | if constants.NV_TIME in what: |
941 | 313b2dd4 | Michael Hanselmann | result[constants.NV_TIME] = utils.SplitTime(time.time()) |
942 | 313b2dd4 | Michael Hanselmann | |
943 | 8964ee14 | Iustin Pop | if constants.NV_OSLIST in what and vm_capable: |
944 | b0d85178 | Iustin Pop | result[constants.NV_OSLIST] = DiagnoseOS() |
945 | b0d85178 | Iustin Pop | |
946 | 20d317d4 | Iustin Pop | if constants.NV_BRIDGES in what and vm_capable: |
947 | 20d317d4 | Iustin Pop | result[constants.NV_BRIDGES] = [bridge |
948 | 20d317d4 | Iustin Pop | for bridge in what[constants.NV_BRIDGES] |
949 | 20d317d4 | Iustin Pop | if not utils.BridgeExists(bridge)] |
950 | 23e3c9b7 | Michael Hanselmann | |
951 | 72b35807 | Michael Hanselmann | if what.get(constants.NV_FILE_STORAGE_PATHS) == my_name:
|
952 | 72b35807 | Michael Hanselmann | result[constants.NV_FILE_STORAGE_PATHS] = \ |
953 | 72b35807 | Michael Hanselmann | bdev.ComputeWrongFileStoragePaths() |
954 | 72b35807 | Michael Hanselmann | |
955 | c26a6bd2 | Iustin Pop | return result
|
956 | a8083063 | Iustin Pop | |
957 | a8083063 | Iustin Pop | |
958 | 2be7273c | Apollon Oikonomopoulos | def GetBlockDevSizes(devices): |
959 | 2be7273c | Apollon Oikonomopoulos | """Return the size of the given block devices
|
960 | 2be7273c | Apollon Oikonomopoulos |
|
961 | 2be7273c | Apollon Oikonomopoulos | @type devices: list
|
962 | 2be7273c | Apollon Oikonomopoulos | @param devices: list of block device nodes to query
|
963 | 2be7273c | Apollon Oikonomopoulos | @rtype: dict
|
964 | 2be7273c | Apollon Oikonomopoulos | @return:
|
965 | 2be7273c | Apollon Oikonomopoulos | dictionary of all block devices under /dev (key). The value is their
|
966 | 2be7273c | Apollon Oikonomopoulos | size in MiB.
|
967 | 2be7273c | Apollon Oikonomopoulos |
|
968 | 2be7273c | Apollon Oikonomopoulos | {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
|
969 | 2be7273c | Apollon Oikonomopoulos |
|
970 | 2be7273c | Apollon Oikonomopoulos | """
|
971 | 2be7273c | Apollon Oikonomopoulos | DEV_PREFIX = "/dev/"
|
972 | 2be7273c | Apollon Oikonomopoulos | blockdevs = {} |
973 | 2be7273c | Apollon Oikonomopoulos | |
974 | 2be7273c | Apollon Oikonomopoulos | for devpath in devices: |
975 | cf00dba0 | René Nussbaumer | if not utils.IsBelowDir(DEV_PREFIX, devpath): |
976 | 2be7273c | Apollon Oikonomopoulos | continue
|
977 | 2be7273c | Apollon Oikonomopoulos | |
978 | 2be7273c | Apollon Oikonomopoulos | try:
|
979 | 2be7273c | Apollon Oikonomopoulos | st = os.stat(devpath) |
980 | 2be7273c | Apollon Oikonomopoulos | except EnvironmentError, err: |
981 | 2be7273c | Apollon Oikonomopoulos | logging.warning("Error stat()'ing device %s: %s", devpath, str(err)) |
982 | 2be7273c | Apollon Oikonomopoulos | continue
|
983 | 2be7273c | Apollon Oikonomopoulos | |
984 | 2be7273c | Apollon Oikonomopoulos | if stat.S_ISBLK(st.st_mode):
|
985 | 2be7273c | Apollon Oikonomopoulos | result = utils.RunCmd(["blockdev", "--getsize64", devpath]) |
986 | 2be7273c | Apollon Oikonomopoulos | if result.failed:
|
987 | 2be7273c | Apollon Oikonomopoulos | # We don't want to fail, just do not list this device as available
|
988 | 2be7273c | Apollon Oikonomopoulos | logging.warning("Cannot get size for block device %s", devpath)
|
989 | 2be7273c | Apollon Oikonomopoulos | continue
|
990 | 2be7273c | Apollon Oikonomopoulos | |
991 | 2be7273c | Apollon Oikonomopoulos | size = int(result.stdout) / (1024 * 1024) |
992 | 2be7273c | Apollon Oikonomopoulos | blockdevs[devpath] = size |
993 | 2be7273c | Apollon Oikonomopoulos | return blockdevs
|
994 | 2be7273c | Apollon Oikonomopoulos | |
995 | 2be7273c | Apollon Oikonomopoulos | |
996 | 84d7e26b | Dmitry Chernyak | def GetVolumeList(vg_names): |
997 | a8083063 | Iustin Pop | """Compute list of logical volumes and their size.
|
998 | a8083063 | Iustin Pop |
|
999 | 84d7e26b | Dmitry Chernyak | @type vg_names: list
|
1000 | 397693d3 | Iustin Pop | @param vg_names: the volume groups whose LVs we should list, or
|
1001 | 397693d3 | Iustin Pop | empty for all volume groups
|
1002 | 10c2650b | Iustin Pop | @rtype: dict
|
1003 | 10c2650b | Iustin Pop | @return:
|
1004 | 10c2650b | Iustin Pop | dictionary of all partions (key) with value being a tuple of
|
1005 | 10c2650b | Iustin Pop | their size (in MiB), inactive and online status::
|
1006 | 10c2650b | Iustin Pop |
|
1007 | 84d7e26b | Dmitry Chernyak | {'xenvg/test1': ('20.06', True, True)}
|
1008 | 10c2650b | Iustin Pop |
|
1009 | 10c2650b | Iustin Pop | in case of errors, a string is returned with the error
|
1010 | 10c2650b | Iustin Pop | details.
|
1011 | a8083063 | Iustin Pop |
|
1012 | a8083063 | Iustin Pop | """
|
1013 | cb2037a2 | Iustin Pop | lvs = {} |
1014 | d0c8c01d | Iustin Pop | sep = "|"
|
1015 | 397693d3 | Iustin Pop | if not vg_names: |
1016 | 397693d3 | Iustin Pop | vg_names = [] |
1017 | cb2037a2 | Iustin Pop | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
1018 | cb2037a2 | Iustin Pop | "--separator=%s" % sep,
|
1019 | 84d7e26b | Dmitry Chernyak | "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
|
1020 | a8083063 | Iustin Pop | if result.failed:
|
1021 | 29d376ec | Iustin Pop | _Fail("Failed to list logical volumes, lvs output: %s", result.output)
|
1022 | cb2037a2 | Iustin Pop | |
1023 | cb2037a2 | Iustin Pop | for line in result.stdout.splitlines(): |
1024 | df4c2628 | Iustin Pop | line = line.strip() |
1025 | 0b5303da | Iustin Pop | match = _LVSLINE_REGEX.match(line) |
1026 | df4c2628 | Iustin Pop | if not match: |
1027 | 18682bca | Iustin Pop | logging.error("Invalid line returned from lvs output: '%s'", line)
|
1028 | df4c2628 | Iustin Pop | continue
|
1029 | 84d7e26b | Dmitry Chernyak | vg_name, name, size, attr = match.groups() |
1030 | d0c8c01d | Iustin Pop | inactive = attr[4] == "-" |
1031 | d0c8c01d | Iustin Pop | online = attr[5] == "o" |
1032 | d0c8c01d | Iustin Pop | virtual = attr[0] == "v" |
1033 | 33f2a81a | Iustin Pop | if virtual:
|
1034 | 33f2a81a | Iustin Pop | # we don't want to report such volumes as existing, since they
|
1035 | 33f2a81a | Iustin Pop | # don't really hold data
|
1036 | 33f2a81a | Iustin Pop | continue
|
1037 | e687ec01 | Michael Hanselmann | lvs[vg_name + "/" + name] = (size, inactive, online)
|
1038 | cb2037a2 | Iustin Pop | |
1039 | cb2037a2 | Iustin Pop | return lvs
|
1040 | a8083063 | Iustin Pop | |
1041 | a8083063 | Iustin Pop | |
1042 | a8083063 | Iustin Pop | def ListVolumeGroups(): |
1043 | 2f8598a5 | Alexander Schreiber | """List the volume groups and their size.
|
1044 | a8083063 | Iustin Pop |
|
1045 | 10c2650b | Iustin Pop | @rtype: dict
|
1046 | 10c2650b | Iustin Pop | @return: dictionary with keys volume name and values the
|
1047 | 10c2650b | Iustin Pop | size of the volume
|
1048 | a8083063 | Iustin Pop |
|
1049 | a8083063 | Iustin Pop | """
|
1050 | c26a6bd2 | Iustin Pop | return utils.ListVolumeGroups()
|
1051 | a8083063 | Iustin Pop | |
1052 | a8083063 | Iustin Pop | |
1053 | dcb93971 | Michael Hanselmann | def NodeVolumes(): |
1054 | dcb93971 | Michael Hanselmann | """List all volumes on this node.
|
1055 | dcb93971 | Michael Hanselmann |
|
1056 | 10c2650b | Iustin Pop | @rtype: list
|
1057 | 10c2650b | Iustin Pop | @return:
|
1058 | 10c2650b | Iustin Pop | A list of dictionaries, each having four keys:
|
1059 | 10c2650b | Iustin Pop | - name: the logical volume name,
|
1060 | 10c2650b | Iustin Pop | - size: the size of the logical volume
|
1061 | 10c2650b | Iustin Pop | - dev: the physical device on which the LV lives
|
1062 | 10c2650b | Iustin Pop | - vg: the volume group to which it belongs
|
1063 | 10c2650b | Iustin Pop |
|
1064 | 10c2650b | Iustin Pop | In case of errors, we return an empty list and log the
|
1065 | 10c2650b | Iustin Pop | error.
|
1066 | 10c2650b | Iustin Pop |
|
1067 | 10c2650b | Iustin Pop | Note that since a logical volume can live on multiple physical
|
1068 | 10c2650b | Iustin Pop | volumes, the resulting list might include a logical volume
|
1069 | 10c2650b | Iustin Pop | multiple times.
|
1070 | 10c2650b | Iustin Pop |
|
1071 | dcb93971 | Michael Hanselmann | """
|
1072 | dcb93971 | Michael Hanselmann | result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix", |
1073 | dcb93971 | Michael Hanselmann | "--separator=|",
|
1074 | dcb93971 | Michael Hanselmann | "--options=lv_name,lv_size,devices,vg_name"])
|
1075 | dcb93971 | Michael Hanselmann | if result.failed:
|
1076 | 10bfe6cb | Iustin Pop | _Fail("Failed to list logical volumes, lvs output: %s",
|
1077 | 10bfe6cb | Iustin Pop | result.output) |
1078 | dcb93971 | Michael Hanselmann | |
1079 | dcb93971 | Michael Hanselmann | def parse_dev(dev): |
1080 | d0c8c01d | Iustin Pop | return dev.split("(")[0] |
1081 | 89e5ab02 | Iustin Pop | |
1082 | 89e5ab02 | Iustin Pop | def handle_dev(dev): |
1083 | 89e5ab02 | Iustin Pop | return [parse_dev(x) for x in dev.split(",")] |
1084 | dcb93971 | Michael Hanselmann | |
1085 | dcb93971 | Michael Hanselmann | def map_line(line): |
1086 | 89e5ab02 | Iustin Pop | line = [v.strip() for v in line] |
1087 | d0c8c01d | Iustin Pop | return [{"name": line[0], "size": line[1], |
1088 | d0c8c01d | Iustin Pop | "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])] |
1089 | 89e5ab02 | Iustin Pop | |
1090 | 89e5ab02 | Iustin Pop | all_devs = [] |
1091 | 89e5ab02 | Iustin Pop | for line in result.stdout.splitlines(): |
1092 | d0c8c01d | Iustin Pop | if line.count("|") >= 3: |
1093 | d0c8c01d | Iustin Pop | all_devs.extend(map_line(line.split("|")))
|
1094 | 89e5ab02 | Iustin Pop | else:
|
1095 | 89e5ab02 | Iustin Pop | logging.warning("Strange line in the output from lvs: '%s'", line)
|
1096 | 89e5ab02 | Iustin Pop | return all_devs
|
1097 | dcb93971 | Michael Hanselmann | |
1098 | dcb93971 | Michael Hanselmann | |
1099 | a8083063 | Iustin Pop | def BridgesExist(bridges_list): |
1100 | 2f8598a5 | Alexander Schreiber | """Check if a list of bridges exist on the current node.
|
1101 | a8083063 | Iustin Pop |
|
1102 | b1206984 | Iustin Pop | @rtype: boolean
|
1103 | b1206984 | Iustin Pop | @return: C{True} if all of them exist, C{False} otherwise
|
1104 | a8083063 | Iustin Pop |
|
1105 | a8083063 | Iustin Pop | """
|
1106 | 35c0c8da | Iustin Pop | missing = [] |
1107 | a8083063 | Iustin Pop | for bridge in bridges_list: |
1108 | a8083063 | Iustin Pop | if not utils.BridgeExists(bridge): |
1109 | 35c0c8da | Iustin Pop | missing.append(bridge) |
1110 | a8083063 | Iustin Pop | |
1111 | 35c0c8da | Iustin Pop | if missing:
|
1112 | 1f864b60 | Iustin Pop | _Fail("Missing bridges %s", utils.CommaJoin(missing))
|
1113 | 35c0c8da | Iustin Pop | |
1114 | a8083063 | Iustin Pop | |
1115 | 2bff1928 | Helga Velroyen | def GetInstanceListForHypervisor(hname, hvparams=None, |
1116 | 2bff1928 | Helga Velroyen | get_hv_fn=hypervisor.GetHypervisor): |
1117 | 2bff1928 | Helga Velroyen | """Provides a list of instances of the given hypervisor.
|
1118 | 2bff1928 | Helga Velroyen |
|
1119 | 2bff1928 | Helga Velroyen | @type hname: string
|
1120 | 2bff1928 | Helga Velroyen | @param hname: name of the hypervisor
|
1121 | 2bff1928 | Helga Velroyen | @type hvparams: dict of strings
|
1122 | 2bff1928 | Helga Velroyen | @param hvparams: hypervisor parameters for the given hypervisor
|
1123 | 2bff1928 | Helga Velroyen | @type get_hv_fn: function
|
1124 | 2bff1928 | Helga Velroyen | @param get_hv_fn: function that returns a hypervisor for the given hypervisor
|
1125 | 2bff1928 | Helga Velroyen | name; optional parameter to increase testability
|
1126 | 2bff1928 | Helga Velroyen |
|
1127 | 2bff1928 | Helga Velroyen | @rtype: list
|
1128 | 2bff1928 | Helga Velroyen | @return: a list of all running instances on the current node
|
1129 | 2bff1928 | Helga Velroyen | - instance1.example.com
|
1130 | 2bff1928 | Helga Velroyen | - instance2.example.com
|
1131 | 2bff1928 | Helga Velroyen |
|
1132 | 2bff1928 | Helga Velroyen | """
|
1133 | 2bff1928 | Helga Velroyen | results = [] |
1134 | 2bff1928 | Helga Velroyen | try:
|
1135 | 2bff1928 | Helga Velroyen | hv = get_hv_fn(hname) |
1136 | 2bff1928 | Helga Velroyen | names = hv.ListInstances(hvparams) |
1137 | 2bff1928 | Helga Velroyen | results.extend(names) |
1138 | 2bff1928 | Helga Velroyen | except errors.HypervisorError, err:
|
1139 | 2bff1928 | Helga Velroyen | _Fail("Error enumerating instances (hypervisor %s): %s",
|
1140 | 2bff1928 | Helga Velroyen | hname, err, exc=True)
|
1141 | 2bff1928 | Helga Velroyen | return results
|
1142 | 2bff1928 | Helga Velroyen | |
1143 | 2bff1928 | Helga Velroyen | |
1144 | fac83f8a | Helga Velroyen | def GetInstanceList(hypervisor_list, all_hvparams=None, |
1145 | fac83f8a | Helga Velroyen | get_hv_fn=hypervisor.GetHypervisor): |
1146 | 2f8598a5 | Alexander Schreiber | """Provides a list of instances.
|
1147 | a8083063 | Iustin Pop |
|
1148 | e69d05fd | Iustin Pop | @type hypervisor_list: list
|
1149 | e69d05fd | Iustin Pop | @param hypervisor_list: the list of hypervisors to query information
|
1150 | fac83f8a | Helga Velroyen | @type all_hvparams: dict of dict of strings
|
1151 | fac83f8a | Helga Velroyen | @param all_hvparams: a dictionary mapping hypervisor types to respective
|
1152 | fac83f8a | Helga Velroyen | cluster-wide hypervisor parameters
|
1153 | fac83f8a | Helga Velroyen | @type get_hv_fn: function
|
1154 | fac83f8a | Helga Velroyen | @param get_hv_fn: function that returns a hypervisor for the given hypervisor
|
1155 | fac83f8a | Helga Velroyen | name; optional parameter to increase testability
|
1156 | e69d05fd | Iustin Pop |
|
1157 | e69d05fd | Iustin Pop | @rtype: list
|
1158 | e69d05fd | Iustin Pop | @return: a list of all running instances on the current node
|
1159 | 10c2650b | Iustin Pop | - instance1.example.com
|
1160 | 10c2650b | Iustin Pop | - instance2.example.com
|
1161 | a8083063 | Iustin Pop |
|
1162 | 098c0958 | Michael Hanselmann | """
|
1163 | e69d05fd | Iustin Pop | results = [] |
1164 | e69d05fd | Iustin Pop | for hname in hypervisor_list: |
1165 | 2bff1928 | Helga Velroyen | hvparams = None
|
1166 | 2bff1928 | Helga Velroyen | if all_hvparams is not None: |
1167 | 2bff1928 | Helga Velroyen | hvparams = all_hvparams[hname] |
1168 | 2bff1928 | Helga Velroyen | results.extend(GetInstanceListForHypervisor(hname, hvparams, |
1169 | 2bff1928 | Helga Velroyen | get_hv_fn=get_hv_fn)) |
1170 | e69d05fd | Iustin Pop | return results
|
1171 | a8083063 | Iustin Pop | |
1172 | a8083063 | Iustin Pop | |
1173 | e69d05fd | Iustin Pop | def GetInstanceInfo(instance, hname): |
1174 | 5bbd3f7f | Michael Hanselmann | """Gives back the information about an instance as a dictionary.
|
1175 | a8083063 | Iustin Pop |
|
1176 | e69d05fd | Iustin Pop | @type instance: string
|
1177 | e69d05fd | Iustin Pop | @param instance: the instance name
|
1178 | e69d05fd | Iustin Pop | @type hname: string
|
1179 | e69d05fd | Iustin Pop | @param hname: the hypervisor type of the instance
|
1180 | a8083063 | Iustin Pop |
|
1181 | e69d05fd | Iustin Pop | @rtype: dict
|
1182 | e69d05fd | Iustin Pop | @return: dictionary with the following keys:
|
1183 | e69d05fd | Iustin Pop | - memory: memory size of instance (int)
|
1184 | e69d05fd | Iustin Pop | - state: xen state of instance (string)
|
1185 | e69d05fd | Iustin Pop | - time: cpu time of instance (float)
|
1186 | 1cb97324 | Agata Murawska | - vcpus: the number of vcpus (int)
|
1187 | a8083063 | Iustin Pop |
|
1188 | 098c0958 | Michael Hanselmann | """
|
1189 | a8083063 | Iustin Pop | output = {} |
1190 | a8083063 | Iustin Pop | |
1191 | e69d05fd | Iustin Pop | iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance) |
1192 | a8083063 | Iustin Pop | if iinfo is not None: |
1193 | d0c8c01d | Iustin Pop | output["memory"] = iinfo[2] |
1194 | 1cb97324 | Agata Murawska | output["vcpus"] = iinfo[3] |
1195 | d0c8c01d | Iustin Pop | output["state"] = iinfo[4] |
1196 | d0c8c01d | Iustin Pop | output["time"] = iinfo[5] |
1197 | a8083063 | Iustin Pop | |
1198 | c26a6bd2 | Iustin Pop | return output
|
1199 | a8083063 | Iustin Pop | |
1200 | a8083063 | Iustin Pop | |
1201 | 56e7640c | Iustin Pop | def GetInstanceMigratable(instance): |
1202 | 3361ab37 | Helga Velroyen | """Computes whether an instance can be migrated.
|
1203 | 56e7640c | Iustin Pop |
|
1204 | 56e7640c | Iustin Pop | @type instance: L{objects.Instance}
|
1205 | 56e7640c | Iustin Pop | @param instance: object representing the instance to be checked.
|
1206 | 56e7640c | Iustin Pop |
|
1207 | 56e7640c | Iustin Pop | @rtype: tuple
|
1208 | 56e7640c | Iustin Pop | @return: tuple of (result, description) where:
|
1209 | 56e7640c | Iustin Pop | - result: whether the instance can be migrated or not
|
1210 | 56e7640c | Iustin Pop | - description: a description of the issue, if relevant
|
1211 | 56e7640c | Iustin Pop |
|
1212 | 56e7640c | Iustin Pop | """
|
1213 | 56e7640c | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1214 | afdc3985 | Iustin Pop | iname = instance.name |
1215 | 3361ab37 | Helga Velroyen | if iname not in hyper.ListInstances(instance.hvparams): |
1216 | afdc3985 | Iustin Pop | _Fail("Instance %s is not running", iname)
|
1217 | 56e7640c | Iustin Pop | |
1218 | 56e7640c | Iustin Pop | for idx in range(len(instance.disks)): |
1219 | afdc3985 | Iustin Pop | link_name = _GetBlockDevSymlinkPath(iname, idx) |
1220 | 56e7640c | Iustin Pop | if not os.path.islink(link_name): |
1221 | b8ebd37b | Iustin Pop | logging.warning("Instance %s is missing symlink %s for disk %d",
|
1222 | b8ebd37b | Iustin Pop | iname, link_name, idx) |
1223 | 56e7640c | Iustin Pop | |
1224 | 56e7640c | Iustin Pop | |
1225 | e69d05fd | Iustin Pop | def GetAllInstancesInfo(hypervisor_list): |
1226 | a8083063 | Iustin Pop | """Gather data about all instances.
|
1227 | a8083063 | Iustin Pop |
|
1228 | 10c2650b | Iustin Pop | This is the equivalent of L{GetInstanceInfo}, except that it
|
1229 | a8083063 | Iustin Pop | computes data for all instances at once, thus being faster if one
|
1230 | a8083063 | Iustin Pop | needs data about more than one instance.
|
1231 | a8083063 | Iustin Pop |
|
1232 | e69d05fd | Iustin Pop | @type hypervisor_list: list
|
1233 | e69d05fd | Iustin Pop | @param hypervisor_list: list of hypervisors to query for instance data
|
1234 | e69d05fd | Iustin Pop |
|
1235 | 955db481 | Guido Trotter | @rtype: dict
|
1236 | e69d05fd | Iustin Pop | @return: dictionary of instance: data, with data having the following keys:
|
1237 | e69d05fd | Iustin Pop | - memory: memory size of instance (int)
|
1238 | e69d05fd | Iustin Pop | - state: xen state of instance (string)
|
1239 | e69d05fd | Iustin Pop | - time: cpu time of instance (float)
|
1240 | 10c2650b | Iustin Pop | - vcpus: the number of vcpus
|
1241 | a8083063 | Iustin Pop |
|
1242 | 098c0958 | Michael Hanselmann | """
|
1243 | a8083063 | Iustin Pop | output = {} |
1244 | a8083063 | Iustin Pop | |
1245 | e69d05fd | Iustin Pop | for hname in hypervisor_list: |
1246 | e69d05fd | Iustin Pop | iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo() |
1247 | e69d05fd | Iustin Pop | if iinfo:
|
1248 | 29921401 | Iustin Pop | for name, _, memory, vcpus, state, times in iinfo: |
1249 | f23b5ae8 | Iustin Pop | value = { |
1250 | d0c8c01d | Iustin Pop | "memory": memory,
|
1251 | d0c8c01d | Iustin Pop | "vcpus": vcpus,
|
1252 | d0c8c01d | Iustin Pop | "state": state,
|
1253 | d0c8c01d | Iustin Pop | "time": times,
|
1254 | e69d05fd | Iustin Pop | } |
1255 | b33b6f55 | Iustin Pop | if name in output: |
1256 | b33b6f55 | Iustin Pop | # we only check static parameters, like memory and vcpus,
|
1257 | b33b6f55 | Iustin Pop | # and not state and time which can change between the
|
1258 | b33b6f55 | Iustin Pop | # invocations of the different hypervisors
|
1259 | d0c8c01d | Iustin Pop | for key in "memory", "vcpus": |
1260 | b33b6f55 | Iustin Pop | if value[key] != output[name][key]:
|
1261 | 2fa74ef4 | Iustin Pop | _Fail("Instance %s is running twice"
|
1262 | 2fa74ef4 | Iustin Pop | " with different parameters", name)
|
1263 | f23b5ae8 | Iustin Pop | output[name] = value |
1264 | a8083063 | Iustin Pop | |
1265 | c26a6bd2 | Iustin Pop | return output
|
1266 | a8083063 | Iustin Pop | |
1267 | a8083063 | Iustin Pop | |
1268 | 6aa7a354 | Iustin Pop | def _InstanceLogName(kind, os_name, instance, component): |
1269 | 81a3406c | Iustin Pop | """Compute the OS log filename for a given instance and operation.
|
1270 | 81a3406c | Iustin Pop |
|
1271 | 81a3406c | Iustin Pop | The instance name and os name are passed in as strings since not all
|
1272 | 81a3406c | Iustin Pop | operations have these as part of an instance object.
|
1273 | 81a3406c | Iustin Pop |
|
1274 | 81a3406c | Iustin Pop | @type kind: string
|
1275 | 81a3406c | Iustin Pop | @param kind: the operation type (e.g. add, import, etc.)
|
1276 | 81a3406c | Iustin Pop | @type os_name: string
|
1277 | 81a3406c | Iustin Pop | @param os_name: the os name
|
1278 | 81a3406c | Iustin Pop | @type instance: string
|
1279 | 81a3406c | Iustin Pop | @param instance: the name of the instance being imported/added/etc.
|
1280 | 6aa7a354 | Iustin Pop | @type component: string or None
|
1281 | 6aa7a354 | Iustin Pop | @param component: the name of the component of the instance being
|
1282 | 6aa7a354 | Iustin Pop | transferred
|
1283 | 81a3406c | Iustin Pop |
|
1284 | 81a3406c | Iustin Pop | """
|
1285 | 1651d116 | Michael Hanselmann | # TODO: Use tempfile.mkstemp to create unique filename
|
1286 | 6aa7a354 | Iustin Pop | if component:
|
1287 | 6aa7a354 | Iustin Pop | assert "/" not in component |
1288 | 6aa7a354 | Iustin Pop | c_msg = "-%s" % component
|
1289 | 6aa7a354 | Iustin Pop | else:
|
1290 | 6aa7a354 | Iustin Pop | c_msg = ""
|
1291 | 6aa7a354 | Iustin Pop | base = ("%s-%s-%s%s-%s.log" %
|
1292 | 6aa7a354 | Iustin Pop | (kind, os_name, instance, c_msg, utils.TimestampForFilename())) |
1293 | 710f30ec | Michael Hanselmann | return utils.PathJoin(pathutils.LOG_OS_DIR, base)
|
1294 | 81a3406c | Iustin Pop | |
1295 | 81a3406c | Iustin Pop | |
1296 | 4a0e011f | Iustin Pop | def InstanceOsAdd(instance, reinstall, debug): |
1297 | 2f8598a5 | Alexander Schreiber | """Add an OS to an instance.
|
1298 | a8083063 | Iustin Pop |
|
1299 | d15a9ad3 | Guido Trotter | @type instance: L{objects.Instance}
|
1300 | d15a9ad3 | Guido Trotter | @param instance: Instance whose OS is to be installed
|
1301 | e557bae9 | Guido Trotter | @type reinstall: boolean
|
1302 | e557bae9 | Guido Trotter | @param reinstall: whether this is an instance reinstall
|
1303 | 4a0e011f | Iustin Pop | @type debug: integer
|
1304 | 4a0e011f | Iustin Pop | @param debug: debug level, passed to the OS scripts
|
1305 | c26a6bd2 | Iustin Pop | @rtype: None
|
1306 | a8083063 | Iustin Pop |
|
1307 | a8083063 | Iustin Pop | """
|
1308 | 255dcebd | Iustin Pop | inst_os = OSFromDisk(instance.os) |
1309 | 255dcebd | Iustin Pop | |
1310 | 4a0e011f | Iustin Pop | create_env = OSEnvironment(instance, inst_os, debug) |
1311 | e557bae9 | Guido Trotter | if reinstall:
|
1312 | d0c8c01d | Iustin Pop | create_env["INSTANCE_REINSTALL"] = "1" |
1313 | a8083063 | Iustin Pop | |
1314 | 6aa7a354 | Iustin Pop | logfile = _InstanceLogName("add", instance.os, instance.name, None) |
1315 | decd5f45 | Iustin Pop | |
1316 | d868edb4 | Iustin Pop | result = utils.RunCmd([inst_os.create_script], env=create_env, |
1317 | 896a03f6 | Iustin Pop | cwd=inst_os.path, output=logfile, reset_env=True)
|
1318 | decd5f45 | Iustin Pop | if result.failed:
|
1319 | 18682bca | Iustin Pop | logging.error("os create command '%s' returned error: %s, logfile: %s,"
|
1320 | d868edb4 | Iustin Pop | " output: %s", result.cmd, result.fail_reason, logfile,
|
1321 | 18682bca | Iustin Pop | result.output) |
1322 | 26f15862 | Iustin Pop | lines = [utils.SafeEncode(val) |
1323 | 20e01edd | Iustin Pop | for val in utils.TailFile(logfile, lines=20)] |
1324 | afdc3985 | Iustin Pop | _Fail("OS create script failed (%s), last lines in the"
|
1325 | afdc3985 | Iustin Pop | " log file:\n%s", result.fail_reason, "\n".join(lines), log=False) |
1326 | decd5f45 | Iustin Pop | |
1327 | decd5f45 | Iustin Pop | |
1328 | 4a0e011f | Iustin Pop | def RunRenameInstance(instance, old_name, debug): |
1329 | decd5f45 | Iustin Pop | """Run the OS rename script for an instance.
|
1330 | decd5f45 | Iustin Pop |
|
1331 | b1206984 | Iustin Pop | @type instance: L{objects.Instance}
|
1332 | d15a9ad3 | Guido Trotter | @param instance: Instance whose OS is to be installed
|
1333 | d15a9ad3 | Guido Trotter | @type old_name: string
|
1334 | d15a9ad3 | Guido Trotter | @param old_name: previous instance name
|
1335 | 4a0e011f | Iustin Pop | @type debug: integer
|
1336 | 4a0e011f | Iustin Pop | @param debug: debug level, passed to the OS scripts
|
1337 | 10c2650b | Iustin Pop | @rtype: boolean
|
1338 | 10c2650b | Iustin Pop | @return: the success of the operation
|
1339 | decd5f45 | Iustin Pop |
|
1340 | decd5f45 | Iustin Pop | """
|
1341 | decd5f45 | Iustin Pop | inst_os = OSFromDisk(instance.os) |
1342 | decd5f45 | Iustin Pop | |
1343 | 4a0e011f | Iustin Pop | rename_env = OSEnvironment(instance, inst_os, debug) |
1344 | d0c8c01d | Iustin Pop | rename_env["OLD_INSTANCE_NAME"] = old_name
|
1345 | decd5f45 | Iustin Pop | |
1346 | 81a3406c | Iustin Pop | logfile = _InstanceLogName("rename", instance.os,
|
1347 | 6aa7a354 | Iustin Pop | "%s-%s" % (old_name, instance.name), None) |
1348 | a8083063 | Iustin Pop | |
1349 | d868edb4 | Iustin Pop | result = utils.RunCmd([inst_os.rename_script], env=rename_env, |
1350 | 896a03f6 | Iustin Pop | cwd=inst_os.path, output=logfile, reset_env=True)
|
1351 | a8083063 | Iustin Pop | |
1352 | a8083063 | Iustin Pop | if result.failed:
|
1353 | 18682bca | Iustin Pop | logging.error("os create command '%s' returned error: %s output: %s",
|
1354 | d868edb4 | Iustin Pop | result.cmd, result.fail_reason, result.output) |
1355 | 26f15862 | Iustin Pop | lines = [utils.SafeEncode(val) |
1356 | 96841384 | Iustin Pop | for val in utils.TailFile(logfile, lines=20)] |
1357 | afdc3985 | Iustin Pop | _Fail("OS rename script failed (%s), last lines in the"
|
1358 | afdc3985 | Iustin Pop | " log file:\n%s", result.fail_reason, "\n".join(lines), log=False) |
1359 | a8083063 | Iustin Pop | |
1360 | a8083063 | Iustin Pop | |
1361 | 3b721842 | Michael Hanselmann | def _GetBlockDevSymlinkPath(instance_name, idx, _dir=None): |
1362 | 3b721842 | Michael Hanselmann | """Returns symlink path for block device.
|
1363 | 3b721842 | Michael Hanselmann |
|
1364 | 3b721842 | Michael Hanselmann | """
|
1365 | 3b721842 | Michael Hanselmann | if _dir is None: |
1366 | 3b721842 | Michael Hanselmann | _dir = pathutils.DISK_LINKS_DIR |
1367 | 3b721842 | Michael Hanselmann | |
1368 | 3b721842 | Michael Hanselmann | return utils.PathJoin(_dir,
|
1369 | 3b721842 | Michael Hanselmann | ("%s%s%s" %
|
1370 | 3b721842 | Michael Hanselmann | (instance_name, constants.DISK_SEPARATOR, idx))) |
1371 | 5282084b | Iustin Pop | |
1372 | 5282084b | Iustin Pop | |
1373 | 5282084b | Iustin Pop | def _SymlinkBlockDev(instance_name, device_path, idx): |
1374 | 9332fd8a | Iustin Pop | """Set up symlinks to a instance's block device.
|
1375 | 9332fd8a | Iustin Pop |
|
1376 | 9332fd8a | Iustin Pop | This is an auxiliary function run when an instance is start (on the primary
|
1377 | 9332fd8a | Iustin Pop | node) or when an instance is migrated (on the target node).
|
1378 | 9332fd8a | Iustin Pop |
|
1379 | 9332fd8a | Iustin Pop |
|
1380 | 5282084b | Iustin Pop | @param instance_name: the name of the target instance
|
1381 | 5282084b | Iustin Pop | @param device_path: path of the physical block device, on the node
|
1382 | 5282084b | Iustin Pop | @param idx: the disk index
|
1383 | 5282084b | Iustin Pop | @return: absolute path to the disk's symlink
|
1384 | 9332fd8a | Iustin Pop |
|
1385 | 9332fd8a | Iustin Pop | """
|
1386 | 5282084b | Iustin Pop | link_name = _GetBlockDevSymlinkPath(instance_name, idx) |
1387 | 9332fd8a | Iustin Pop | try:
|
1388 | 9332fd8a | Iustin Pop | os.symlink(device_path, link_name) |
1389 | 5282084b | Iustin Pop | except OSError, err: |
1390 | 5282084b | Iustin Pop | if err.errno == errno.EEXIST:
|
1391 | 9332fd8a | Iustin Pop | if (not os.path.islink(link_name) or |
1392 | 9332fd8a | Iustin Pop | os.readlink(link_name) != device_path): |
1393 | 9332fd8a | Iustin Pop | os.remove(link_name) |
1394 | 9332fd8a | Iustin Pop | os.symlink(device_path, link_name) |
1395 | 9332fd8a | Iustin Pop | else:
|
1396 | 9332fd8a | Iustin Pop | raise
|
1397 | 9332fd8a | Iustin Pop | |
1398 | 9332fd8a | Iustin Pop | return link_name
|
1399 | 9332fd8a | Iustin Pop | |
1400 | 9332fd8a | Iustin Pop | |
1401 | 5282084b | Iustin Pop | def _RemoveBlockDevLinks(instance_name, disks): |
1402 | 3c9c571d | Iustin Pop | """Remove the block device symlinks belonging to the given instance.
|
1403 | 3c9c571d | Iustin Pop |
|
1404 | 3c9c571d | Iustin Pop | """
|
1405 | 29921401 | Iustin Pop | for idx, _ in enumerate(disks): |
1406 | 5282084b | Iustin Pop | link_name = _GetBlockDevSymlinkPath(instance_name, idx) |
1407 | 5282084b | Iustin Pop | if os.path.islink(link_name):
|
1408 | 3c9c571d | Iustin Pop | try:
|
1409 | 03dfa658 | Iustin Pop | os.remove(link_name) |
1410 | 03dfa658 | Iustin Pop | except OSError: |
1411 | 03dfa658 | Iustin Pop | logging.exception("Can't remove symlink '%s'", link_name)
|
1412 | 3c9c571d | Iustin Pop | |
1413 | 3c9c571d | Iustin Pop | |
1414 | 9332fd8a | Iustin Pop | def _GatherAndLinkBlockDevs(instance): |
1415 | a8083063 | Iustin Pop | """Set up an instance's block device(s).
|
1416 | a8083063 | Iustin Pop |
|
1417 | a8083063 | Iustin Pop | This is run on the primary node at instance startup. The block
|
1418 | a8083063 | Iustin Pop | devices must be already assembled.
|
1419 | a8083063 | Iustin Pop |
|
1420 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1421 | 10c2650b | Iustin Pop | @param instance: the instance whose disks we shoul assemble
|
1422 | 069cfbf1 | Iustin Pop | @rtype: list
|
1423 | 069cfbf1 | Iustin Pop | @return: list of (disk_object, device_path)
|
1424 | 10c2650b | Iustin Pop |
|
1425 | a8083063 | Iustin Pop | """
|
1426 | a8083063 | Iustin Pop | block_devices = [] |
1427 | 9332fd8a | Iustin Pop | for idx, disk in enumerate(instance.disks): |
1428 | a8083063 | Iustin Pop | device = _RecursiveFindBD(disk) |
1429 | a8083063 | Iustin Pop | if device is None: |
1430 | a8083063 | Iustin Pop | raise errors.BlockDeviceError("Block device '%s' is not set up." % |
1431 | a8083063 | Iustin Pop | str(disk))
|
1432 | a8083063 | Iustin Pop | device.Open() |
1433 | 9332fd8a | Iustin Pop | try:
|
1434 | 5282084b | Iustin Pop | link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx) |
1435 | 9332fd8a | Iustin Pop | except OSError, e: |
1436 | 9332fd8a | Iustin Pop | raise errors.BlockDeviceError("Cannot create block device symlink: %s" % |
1437 | 9332fd8a | Iustin Pop | e.strerror) |
1438 | 9332fd8a | Iustin Pop | |
1439 | 9332fd8a | Iustin Pop | block_devices.append((disk, link_name)) |
1440 | 9332fd8a | Iustin Pop | |
1441 | a8083063 | Iustin Pop | return block_devices
|
1442 | a8083063 | Iustin Pop | |
1443 | a8083063 | Iustin Pop | |
1444 | 1fa6fcba | Michele Tartara | def StartInstance(instance, startup_paused, reason, store_reason=True): |
1445 | a8083063 | Iustin Pop | """Start an instance.
|
1446 | a8083063 | Iustin Pop |
|
1447 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1448 | e69d05fd | Iustin Pop | @param instance: the instance object
|
1449 | 323f9095 | Stephen Shirley | @type startup_paused: bool
|
1450 | 323f9095 | Stephen Shirley | @param instance: pause instance at startup?
|
1451 | 1fa6fcba | Michele Tartara | @type reason: list of reasons
|
1452 | 1fa6fcba | Michele Tartara | @param reason: the reason trail for this startup
|
1453 | 1fa6fcba | Michele Tartara | @type store_reason: boolean
|
1454 | 1fa6fcba | Michele Tartara | @param store_reason: whether to store the shutdown reason trail on file
|
1455 | c26a6bd2 | Iustin Pop | @rtype: None
|
1456 | a8083063 | Iustin Pop |
|
1457 | 098c0958 | Michael Hanselmann | """
|
1458 | 3361ab37 | Helga Velroyen | running_instances = GetInstanceListForHypervisor(instance.hypervisor, |
1459 | 3361ab37 | Helga Velroyen | instance.hvparams) |
1460 | a8083063 | Iustin Pop | |
1461 | a8083063 | Iustin Pop | if instance.name in running_instances: |
1462 | c26a6bd2 | Iustin Pop | logging.info("Instance %s already running, not starting", instance.name)
|
1463 | c26a6bd2 | Iustin Pop | return
|
1464 | a8083063 | Iustin Pop | |
1465 | a8083063 | Iustin Pop | try:
|
1466 | ec596c24 | Iustin Pop | block_devices = _GatherAndLinkBlockDevs(instance) |
1467 | ec596c24 | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1468 | 323f9095 | Stephen Shirley | hyper.StartInstance(instance, block_devices, startup_paused) |
1469 | 1fa6fcba | Michele Tartara | if store_reason:
|
1470 | 1fa6fcba | Michele Tartara | _StoreInstReasonTrail(instance.name, reason) |
1471 | ec596c24 | Iustin Pop | except errors.BlockDeviceError, err:
|
1472 | 2cc6781a | Iustin Pop | _Fail("Block device error: %s", err, exc=True) |
1473 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
1474 | 5282084b | Iustin Pop | _RemoveBlockDevLinks(instance.name, instance.disks) |
1475 | 2cc6781a | Iustin Pop | _Fail("Hypervisor error: %s", err, exc=True) |
1476 | a8083063 | Iustin Pop | |
1477 | a8083063 | Iustin Pop | |
1478 | 1f350e0f | Michele Tartara | def InstanceShutdown(instance, timeout, reason, store_reason=True): |
1479 | a8083063 | Iustin Pop | """Shut an instance down.
|
1480 | a8083063 | Iustin Pop |
|
1481 | 10c2650b | Iustin Pop | @note: this functions uses polling with a hardcoded timeout.
|
1482 | 10c2650b | Iustin Pop |
|
1483 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1484 | e69d05fd | Iustin Pop | @param instance: the instance object
|
1485 | 6263189c | Guido Trotter | @type timeout: integer
|
1486 | 6263189c | Guido Trotter | @param timeout: maximum timeout for soft shutdown
|
1487 | 1f350e0f | Michele Tartara | @type reason: list of reasons
|
1488 | 1f350e0f | Michele Tartara | @param reason: the reason trail for this shutdown
|
1489 | 1f350e0f | Michele Tartara | @type store_reason: boolean
|
1490 | 1f350e0f | Michele Tartara | @param store_reason: whether to store the shutdown reason trail on file
|
1491 | c26a6bd2 | Iustin Pop | @rtype: None
|
1492 | a8083063 | Iustin Pop |
|
1493 | 098c0958 | Michael Hanselmann | """
|
1494 | e69d05fd | Iustin Pop | hv_name = instance.hypervisor |
1495 | e4e9b806 | Guido Trotter | hyper = hypervisor.GetHypervisor(hv_name) |
1496 | c26a6bd2 | Iustin Pop | iname = instance.name |
1497 | a8083063 | Iustin Pop | |
1498 | 3361ab37 | Helga Velroyen | if instance.name not in hyper.ListInstances(instance.hvparams): |
1499 | c26a6bd2 | Iustin Pop | logging.info("Instance %s not running, doing nothing", iname)
|
1500 | c26a6bd2 | Iustin Pop | return
|
1501 | a8083063 | Iustin Pop | |
1502 | 3c0cdc83 | Michael Hanselmann | class _TryShutdown: |
1503 | 3c0cdc83 | Michael Hanselmann | def __init__(self): |
1504 | 3c0cdc83 | Michael Hanselmann | self.tried_once = False |
1505 | a8083063 | Iustin Pop | |
1506 | 3c0cdc83 | Michael Hanselmann | def __call__(self): |
1507 | 3361ab37 | Helga Velroyen | if iname not in hyper.ListInstances(instance.hvparams): |
1508 | 3c0cdc83 | Michael Hanselmann | return
|
1509 | 3c0cdc83 | Michael Hanselmann | |
1510 | 3c0cdc83 | Michael Hanselmann | try:
|
1511 | 3c0cdc83 | Michael Hanselmann | hyper.StopInstance(instance, retry=self.tried_once)
|
1512 | 1f350e0f | Michele Tartara | if store_reason:
|
1513 | 1f350e0f | Michele Tartara | _StoreInstReasonTrail(instance.name, reason) |
1514 | 3c0cdc83 | Michael Hanselmann | except errors.HypervisorError, err:
|
1515 | 3361ab37 | Helga Velroyen | if iname not in hyper.ListInstances(instance.hvparams): |
1516 | 3c0cdc83 | Michael Hanselmann | # if the instance is no longer existing, consider this a
|
1517 | 3c0cdc83 | Michael Hanselmann | # success and go to cleanup
|
1518 | 3c0cdc83 | Michael Hanselmann | return
|
1519 | 3c0cdc83 | Michael Hanselmann | |
1520 | 3c0cdc83 | Michael Hanselmann | _Fail("Failed to stop instance %s: %s", iname, err)
|
1521 | 3c0cdc83 | Michael Hanselmann | |
1522 | 3c0cdc83 | Michael Hanselmann | self.tried_once = True |
1523 | 3c0cdc83 | Michael Hanselmann | |
1524 | 3c0cdc83 | Michael Hanselmann | raise utils.RetryAgain()
|
1525 | 3c0cdc83 | Michael Hanselmann | |
1526 | 3c0cdc83 | Michael Hanselmann | try:
|
1527 | 3c0cdc83 | Michael Hanselmann | utils.Retry(_TryShutdown(), 5, timeout)
|
1528 | 3c0cdc83 | Michael Hanselmann | except utils.RetryTimeout:
|
1529 | a8083063 | Iustin Pop | # the shutdown did not succeed
|
1530 | e4e9b806 | Guido Trotter | logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
|
1531 | a8083063 | Iustin Pop | |
1532 | a8083063 | Iustin Pop | try:
|
1533 | a8083063 | Iustin Pop | hyper.StopInstance(instance, force=True)
|
1534 | a8083063 | Iustin Pop | except errors.HypervisorError, err:
|
1535 | 3361ab37 | Helga Velroyen | if iname in hyper.ListInstances(instance.hvparams): |
1536 | 3782acd7 | Iustin Pop | # only raise an error if the instance still exists, otherwise
|
1537 | 3782acd7 | Iustin Pop | # the error could simply be "instance ... unknown"!
|
1538 | 3782acd7 | Iustin Pop | _Fail("Failed to force stop instance %s: %s", iname, err)
|
1539 | a8083063 | Iustin Pop | |
1540 | a8083063 | Iustin Pop | time.sleep(1)
|
1541 | 3c0cdc83 | Michael Hanselmann | |
1542 | 3361ab37 | Helga Velroyen | if iname in hyper.ListInstances(instance.hvparams): |
1543 | c26a6bd2 | Iustin Pop | _Fail("Could not shutdown instance %s even by destroy", iname)
|
1544 | 3c9c571d | Iustin Pop | |
1545 | f28ec899 | Guido Trotter | try:
|
1546 | f28ec899 | Guido Trotter | hyper.CleanupInstance(instance.name) |
1547 | f28ec899 | Guido Trotter | except errors.HypervisorError, err:
|
1548 | f28ec899 | Guido Trotter | logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
|
1549 | f28ec899 | Guido Trotter | |
1550 | c26a6bd2 | Iustin Pop | _RemoveBlockDevLinks(iname, instance.disks) |
1551 | a8083063 | Iustin Pop | |
1552 | a8083063 | Iustin Pop | |
1553 | 55cec070 | Michele Tartara | def InstanceReboot(instance, reboot_type, shutdown_timeout, reason): |
1554 | 007a2f3e | Alexander Schreiber | """Reboot an instance.
|
1555 | 007a2f3e | Alexander Schreiber |
|
1556 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
1557 | 10c2650b | Iustin Pop | @param instance: the instance object to reboot
|
1558 | 10c2650b | Iustin Pop | @type reboot_type: str
|
1559 | 10c2650b | Iustin Pop | @param reboot_type: the type of reboot, one the following
|
1560 | 10c2650b | Iustin Pop | constants:
|
1561 | 10c2650b | Iustin Pop | - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
|
1562 | 10c2650b | Iustin Pop | instance OS, do not recreate the VM
|
1563 | 10c2650b | Iustin Pop | - L{constants.INSTANCE_REBOOT_HARD}: tear down and
|
1564 | 10c2650b | Iustin Pop | restart the VM (at the hypervisor level)
|
1565 | 73e5a4f4 | Iustin Pop | - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
|
1566 | 73e5a4f4 | Iustin Pop | not accepted here, since that mode is handled differently, in
|
1567 | 73e5a4f4 | Iustin Pop | cmdlib, and translates into full stop and start of the
|
1568 | 73e5a4f4 | Iustin Pop | instance (instead of a call_instance_reboot RPC)
|
1569 | 23057d29 | Michael Hanselmann | @type shutdown_timeout: integer
|
1570 | 23057d29 | Michael Hanselmann | @param shutdown_timeout: maximum timeout for soft shutdown
|
1571 | 55cec070 | Michele Tartara | @type reason: list of reasons
|
1572 | 55cec070 | Michele Tartara | @param reason: the reason trail for this reboot
|
1573 | c26a6bd2 | Iustin Pop | @rtype: None
|
1574 | 007a2f3e | Alexander Schreiber |
|
1575 | 007a2f3e | Alexander Schreiber | """
|
1576 | 3361ab37 | Helga Velroyen | running_instances = GetInstanceListForHypervisor(instance.hypervisor, |
1577 | 3361ab37 | Helga Velroyen | instance.hvparams) |
1578 | 007a2f3e | Alexander Schreiber | |
1579 | 007a2f3e | Alexander Schreiber | if instance.name not in running_instances: |
1580 | 2cc6781a | Iustin Pop | _Fail("Cannot reboot instance %s that is not running", instance.name)
|
1581 | 007a2f3e | Alexander Schreiber | |
1582 | e69d05fd | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1583 | 007a2f3e | Alexander Schreiber | if reboot_type == constants.INSTANCE_REBOOT_SOFT:
|
1584 | 007a2f3e | Alexander Schreiber | try:
|
1585 | 007a2f3e | Alexander Schreiber | hyper.RebootInstance(instance) |
1586 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
1587 | 2cc6781a | Iustin Pop | _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
|
1588 | 007a2f3e | Alexander Schreiber | elif reboot_type == constants.INSTANCE_REBOOT_HARD:
|
1589 | 007a2f3e | Alexander Schreiber | try:
|
1590 | 1f350e0f | Michele Tartara | InstanceShutdown(instance, shutdown_timeout, reason, store_reason=False)
|
1591 | 1fa6fcba | Michele Tartara | result = StartInstance(instance, False, reason, store_reason=False) |
1592 | 55cec070 | Michele Tartara | _StoreInstReasonTrail(instance.name, reason) |
1593 | 4a90bd4f | Michele Tartara | return result
|
1594 | 007a2f3e | Alexander Schreiber | except errors.HypervisorError, err:
|
1595 | 2cc6781a | Iustin Pop | _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
|
1596 | 007a2f3e | Alexander Schreiber | else:
|
1597 | 2cc6781a | Iustin Pop | _Fail("Invalid reboot_type received: %s", reboot_type)
|
1598 | 007a2f3e | Alexander Schreiber | |
1599 | 007a2f3e | Alexander Schreiber | |
1600 | ebe466d8 | Guido Trotter | def InstanceBalloonMemory(instance, memory): |
1601 | ebe466d8 | Guido Trotter | """Resize an instance's memory.
|
1602 | ebe466d8 | Guido Trotter |
|
1603 | ebe466d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1604 | ebe466d8 | Guido Trotter | @param instance: the instance object
|
1605 | ebe466d8 | Guido Trotter | @type memory: int
|
1606 | ebe466d8 | Guido Trotter | @param memory: new memory amount in MB
|
1607 | ebe466d8 | Guido Trotter | @rtype: None
|
1608 | ebe466d8 | Guido Trotter |
|
1609 | ebe466d8 | Guido Trotter | """
|
1610 | ebe466d8 | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1611 | 3361ab37 | Helga Velroyen | running = hyper.ListInstances(instance.hvparams) |
1612 | ebe466d8 | Guido Trotter | if instance.name not in running: |
1613 | ebe466d8 | Guido Trotter | logging.info("Instance %s is not running, cannot balloon", instance.name)
|
1614 | ebe466d8 | Guido Trotter | return
|
1615 | ebe466d8 | Guido Trotter | try:
|
1616 | ebe466d8 | Guido Trotter | hyper.BalloonInstanceMemory(instance, memory) |
1617 | ebe466d8 | Guido Trotter | except errors.HypervisorError, err:
|
1618 | ebe466d8 | Guido Trotter | _Fail("Failed to balloon instance memory: %s", err, exc=True) |
1619 | ebe466d8 | Guido Trotter | |
1620 | ebe466d8 | Guido Trotter | |
1621 | 6906a9d8 | Guido Trotter | def MigrationInfo(instance): |
1622 | 6906a9d8 | Guido Trotter | """Gather information about an instance to be migrated.
|
1623 | 6906a9d8 | Guido Trotter |
|
1624 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1625 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1626 | 6906a9d8 | Guido Trotter |
|
1627 | 6906a9d8 | Guido Trotter | """
|
1628 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1629 | cd42d0ad | Guido Trotter | try:
|
1630 | cd42d0ad | Guido Trotter | info = hyper.MigrationInfo(instance) |
1631 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1632 | 2cc6781a | Iustin Pop | _Fail("Failed to fetch migration information: %s", err, exc=True) |
1633 | c26a6bd2 | Iustin Pop | return info
|
1634 | 6906a9d8 | Guido Trotter | |
1635 | 6906a9d8 | Guido Trotter | |
1636 | 6906a9d8 | Guido Trotter | def AcceptInstance(instance, info, target): |
1637 | 6906a9d8 | Guido Trotter | """Prepare the node to accept an instance.
|
1638 | 6906a9d8 | Guido Trotter |
|
1639 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1640 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1641 | 6906a9d8 | Guido Trotter | @type info: string/data (opaque)
|
1642 | 6906a9d8 | Guido Trotter | @param info: migration information, from the source node
|
1643 | 6906a9d8 | Guido Trotter | @type target: string
|
1644 | 6906a9d8 | Guido Trotter | @param target: target host (usually ip), on this node
|
1645 | 6906a9d8 | Guido Trotter |
|
1646 | 6906a9d8 | Guido Trotter | """
|
1647 | 77fcff4a | Apollon Oikonomopoulos | # TODO: why is this required only for DTS_EXT_MIRROR?
|
1648 | 77fcff4a | Apollon Oikonomopoulos | if instance.disk_template in constants.DTS_EXT_MIRROR: |
1649 | 77fcff4a | Apollon Oikonomopoulos | # Create the symlinks, as the disks are not active
|
1650 | 77fcff4a | Apollon Oikonomopoulos | # in any way
|
1651 | 77fcff4a | Apollon Oikonomopoulos | try:
|
1652 | 77fcff4a | Apollon Oikonomopoulos | _GatherAndLinkBlockDevs(instance) |
1653 | 77fcff4a | Apollon Oikonomopoulos | except errors.BlockDeviceError, err:
|
1654 | 77fcff4a | Apollon Oikonomopoulos | _Fail("Block device error: %s", err, exc=True) |
1655 | 77fcff4a | Apollon Oikonomopoulos | |
1656 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1657 | cd42d0ad | Guido Trotter | try:
|
1658 | cd42d0ad | Guido Trotter | hyper.AcceptInstance(instance, info, target) |
1659 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1660 | 77fcff4a | Apollon Oikonomopoulos | if instance.disk_template in constants.DTS_EXT_MIRROR: |
1661 | 77fcff4a | Apollon Oikonomopoulos | _RemoveBlockDevLinks(instance.name, instance.disks) |
1662 | 2cc6781a | Iustin Pop | _Fail("Failed to accept instance: %s", err, exc=True) |
1663 | 6906a9d8 | Guido Trotter | |
1664 | 6906a9d8 | Guido Trotter | |
1665 | 6a1434d7 | Andrea Spadaccini | def FinalizeMigrationDst(instance, info, success): |
1666 | 6906a9d8 | Guido Trotter | """Finalize any preparation to accept an instance.
|
1667 | 6906a9d8 | Guido Trotter |
|
1668 | 6906a9d8 | Guido Trotter | @type instance: L{objects.Instance}
|
1669 | 6906a9d8 | Guido Trotter | @param instance: the instance definition
|
1670 | 6906a9d8 | Guido Trotter | @type info: string/data (opaque)
|
1671 | 6906a9d8 | Guido Trotter | @param info: migration information, from the source node
|
1672 | 6906a9d8 | Guido Trotter | @type success: boolean
|
1673 | 6906a9d8 | Guido Trotter | @param success: whether the migration was a success or a failure
|
1674 | 6906a9d8 | Guido Trotter |
|
1675 | 6906a9d8 | Guido Trotter | """
|
1676 | cd42d0ad | Guido Trotter | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1677 | cd42d0ad | Guido Trotter | try:
|
1678 | 6a1434d7 | Andrea Spadaccini | hyper.FinalizeMigrationDst(instance, info, success) |
1679 | cd42d0ad | Guido Trotter | except errors.HypervisorError, err:
|
1680 | 6a1434d7 | Andrea Spadaccini | _Fail("Failed to finalize migration on the target node: %s", err, exc=True) |
1681 | 6906a9d8 | Guido Trotter | |
1682 | 6906a9d8 | Guido Trotter | |
1683 | 2a10865c | Iustin Pop | def MigrateInstance(instance, target, live): |
1684 | 2a10865c | Iustin Pop | """Migrates an instance to another node.
|
1685 | 2a10865c | Iustin Pop |
|
1686 | b1206984 | Iustin Pop | @type instance: L{objects.Instance}
|
1687 | 9f0e6b37 | Iustin Pop | @param instance: the instance definition
|
1688 | 9f0e6b37 | Iustin Pop | @type target: string
|
1689 | 9f0e6b37 | Iustin Pop | @param target: the target node name
|
1690 | 9f0e6b37 | Iustin Pop | @type live: boolean
|
1691 | 9f0e6b37 | Iustin Pop | @param live: whether the migration should be done live or not (the
|
1692 | 9f0e6b37 | Iustin Pop | interpretation of this parameter is left to the hypervisor)
|
1693 | c03fe62b | Andrea Spadaccini | @raise RPCFail: if migration fails for some reason
|
1694 | 9f0e6b37 | Iustin Pop |
|
1695 | 2a10865c | Iustin Pop | """
|
1696 | 53c776b5 | Iustin Pop | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1697 | 2a10865c | Iustin Pop | |
1698 | 2a10865c | Iustin Pop | try:
|
1699 | 58d38b02 | Iustin Pop | hyper.MigrateInstance(instance, target, live) |
1700 | 2a10865c | Iustin Pop | except errors.HypervisorError, err:
|
1701 | 2cc6781a | Iustin Pop | _Fail("Failed to migrate instance: %s", err, exc=True) |
1702 | 2a10865c | Iustin Pop | |
1703 | 2a10865c | Iustin Pop | |
1704 | 6a1434d7 | Andrea Spadaccini | def FinalizeMigrationSource(instance, success, live): |
1705 | 6a1434d7 | Andrea Spadaccini | """Finalize the instance migration on the source node.
|
1706 | 6a1434d7 | Andrea Spadaccini |
|
1707 | 6a1434d7 | Andrea Spadaccini | @type instance: L{objects.Instance}
|
1708 | 6a1434d7 | Andrea Spadaccini | @param instance: the instance definition of the migrated instance
|
1709 | 6a1434d7 | Andrea Spadaccini | @type success: bool
|
1710 | 6a1434d7 | Andrea Spadaccini | @param success: whether the migration succeeded or not
|
1711 | 6a1434d7 | Andrea Spadaccini | @type live: bool
|
1712 | 6a1434d7 | Andrea Spadaccini | @param live: whether the user requested a live migration or not
|
1713 | 6a1434d7 | Andrea Spadaccini | @raise RPCFail: If the execution fails for some reason
|
1714 | 6a1434d7 | Andrea Spadaccini |
|
1715 | 6a1434d7 | Andrea Spadaccini | """
|
1716 | 6a1434d7 | Andrea Spadaccini | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1717 | 6a1434d7 | Andrea Spadaccini | |
1718 | 6a1434d7 | Andrea Spadaccini | try:
|
1719 | 6a1434d7 | Andrea Spadaccini | hyper.FinalizeMigrationSource(instance, success, live) |
1720 | 6a1434d7 | Andrea Spadaccini | except Exception, err: # pylint: disable=W0703 |
1721 | 6a1434d7 | Andrea Spadaccini | _Fail("Failed to finalize the migration on the source node: %s", err,
|
1722 | 6a1434d7 | Andrea Spadaccini | exc=True)
|
1723 | 6a1434d7 | Andrea Spadaccini | |
1724 | 6a1434d7 | Andrea Spadaccini | |
1725 | 6a1434d7 | Andrea Spadaccini | def GetMigrationStatus(instance): |
1726 | 6a1434d7 | Andrea Spadaccini | """Get the migration status
|
1727 | 6a1434d7 | Andrea Spadaccini |
|
1728 | 6a1434d7 | Andrea Spadaccini | @type instance: L{objects.Instance}
|
1729 | 6a1434d7 | Andrea Spadaccini | @param instance: the instance that is being migrated
|
1730 | 6a1434d7 | Andrea Spadaccini | @rtype: L{objects.MigrationStatus}
|
1731 | 6a1434d7 | Andrea Spadaccini | @return: the status of the current migration (one of
|
1732 | 6a1434d7 | Andrea Spadaccini | L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
|
1733 | 6a1434d7 | Andrea Spadaccini | progress info that can be retrieved from the hypervisor
|
1734 | 6a1434d7 | Andrea Spadaccini | @raise RPCFail: If the migration status cannot be retrieved
|
1735 | 6a1434d7 | Andrea Spadaccini |
|
1736 | 6a1434d7 | Andrea Spadaccini | """
|
1737 | 6a1434d7 | Andrea Spadaccini | hyper = hypervisor.GetHypervisor(instance.hypervisor) |
1738 | 6a1434d7 | Andrea Spadaccini | try:
|
1739 | 6a1434d7 | Andrea Spadaccini | return hyper.GetMigrationStatus(instance)
|
1740 | 6a1434d7 | Andrea Spadaccini | except Exception, err: # pylint: disable=W0703 |
1741 | 6a1434d7 | Andrea Spadaccini | _Fail("Failed to get migration status: %s", err, exc=True) |
1742 | 6a1434d7 | Andrea Spadaccini | |
1743 | 6a1434d7 | Andrea Spadaccini | |
1744 | ee1478e5 | Bernardo Dal Seno | def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor): |
1745 | a8083063 | Iustin Pop | """Creates a block device for an instance.
|
1746 | a8083063 | Iustin Pop |
|
1747 | b1206984 | Iustin Pop | @type disk: L{objects.Disk}
|
1748 | b1206984 | Iustin Pop | @param disk: the object describing the disk we should create
|
1749 | b1206984 | Iustin Pop | @type size: int
|
1750 | b1206984 | Iustin Pop | @param size: the size of the physical underlying device, in MiB
|
1751 | b1206984 | Iustin Pop | @type owner: str
|
1752 | b1206984 | Iustin Pop | @param owner: the name of the instance for which disk is created,
|
1753 | b1206984 | Iustin Pop | used for device cache data
|
1754 | b1206984 | Iustin Pop | @type on_primary: boolean
|
1755 | b1206984 | Iustin Pop | @param on_primary: indicates if it is the primary node or not
|
1756 | b1206984 | Iustin Pop | @type info: string
|
1757 | b1206984 | Iustin Pop | @param info: string that will be sent to the physical device
|
1758 | b1206984 | Iustin Pop | creation, used for example to set (LVM) tags on LVs
|
1759 | ee1478e5 | Bernardo Dal Seno | @type excl_stor: boolean
|
1760 | ee1478e5 | Bernardo Dal Seno | @param excl_stor: Whether exclusive_storage is active
|
1761 | b1206984 | Iustin Pop |
|
1762 | b1206984 | Iustin Pop | @return: the new unique_id of the device (this can sometime be
|
1763 | b1206984 | Iustin Pop | computed only after creation), or None. On secondary nodes,
|
1764 | b1206984 | Iustin Pop | it's not required to return anything.
|
1765 | a8083063 | Iustin Pop |
|
1766 | a8083063 | Iustin Pop | """
|
1767 | d0c8c01d | Iustin Pop | # TODO: remove the obsolete "size" argument
|
1768 | b459a848 | Andrea Spadaccini | # pylint: disable=W0613
|
1769 | a8083063 | Iustin Pop | clist = [] |
1770 | a8083063 | Iustin Pop | if disk.children:
|
1771 | a8083063 | Iustin Pop | for child in disk.children: |
1772 | 1063abd1 | Iustin Pop | try:
|
1773 | 1063abd1 | Iustin Pop | crdev = _RecursiveAssembleBD(child, owner, on_primary) |
1774 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1775 | 2cc6781a | Iustin Pop | _Fail("Can't assemble device %s: %s", child, err)
|
1776 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
1777 | a8083063 | Iustin Pop | # we need the children open in case the device itself has to
|
1778 | a8083063 | Iustin Pop | # be assembled
|
1779 | 1063abd1 | Iustin Pop | try:
|
1780 | b459a848 | Andrea Spadaccini | # pylint: disable=E1103
|
1781 | 1063abd1 | Iustin Pop | crdev.Open() |
1782 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1783 | 2cc6781a | Iustin Pop | _Fail("Can't make child '%s' read-write: %s", child, err)
|
1784 | a8083063 | Iustin Pop | clist.append(crdev) |
1785 | a8083063 | Iustin Pop | |
1786 | dab69e97 | Iustin Pop | try:
|
1787 | ee1478e5 | Bernardo Dal Seno | device = bdev.Create(disk, clist, excl_stor) |
1788 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1789 | 2cc6781a | Iustin Pop | _Fail("Can't create block device: %s", err)
|
1790 | 6c626518 | Iustin Pop | |
1791 | a8083063 | Iustin Pop | if on_primary or disk.AssembleOnSecondary(): |
1792 | 1063abd1 | Iustin Pop | try:
|
1793 | 1063abd1 | Iustin Pop | device.Assemble() |
1794 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1795 | 2cc6781a | Iustin Pop | _Fail("Can't assemble device after creation, unusual event: %s", err)
|
1796 | a8083063 | Iustin Pop | if on_primary or disk.OpenOnSecondary(): |
1797 | 1063abd1 | Iustin Pop | try:
|
1798 | 1063abd1 | Iustin Pop | device.Open(force=True)
|
1799 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1800 | 2cc6781a | Iustin Pop | _Fail("Can't make device r/w after creation, unusual event: %s", err)
|
1801 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(device.dev_path, owner, |
1802 | 3f78eef2 | Iustin Pop | on_primary, disk.iv_name) |
1803 | a0c3fea1 | Michael Hanselmann | |
1804 | a0c3fea1 | Michael Hanselmann | device.SetInfo(info) |
1805 | a0c3fea1 | Michael Hanselmann | |
1806 | c26a6bd2 | Iustin Pop | return device.unique_id
|
1807 | a8083063 | Iustin Pop | |
1808 | a8083063 | Iustin Pop | |
1809 | da63bb4e | René Nussbaumer | def _WipeDevice(path, offset, size): |
1810 | 69dd363f | René Nussbaumer | """This function actually wipes the device.
|
1811 | 69dd363f | René Nussbaumer |
|
1812 | 69dd363f | René Nussbaumer | @param path: The path to the device to wipe
|
1813 | da63bb4e | René Nussbaumer | @param offset: The offset in MiB in the file
|
1814 | da63bb4e | René Nussbaumer | @param size: The size in MiB to write
|
1815 | 69dd363f | René Nussbaumer |
|
1816 | 69dd363f | René Nussbaumer | """
|
1817 | 0188611b | Michael Hanselmann | # Internal sizes are always in Mebibytes; if the following "dd" command
|
1818 | 0188611b | Michael Hanselmann | # should use a different block size the offset and size given to this
|
1819 | 0188611b | Michael Hanselmann | # function must be adjusted accordingly before being passed to "dd".
|
1820 | 0188611b | Michael Hanselmann | block_size = 1024 * 1024 |
1821 | 0188611b | Michael Hanselmann | |
1822 | da63bb4e | René Nussbaumer | cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset, |
1823 | 0188611b | Michael Hanselmann | "bs=%s" % block_size, "oflag=direct", "of=%s" % path, |
1824 | da63bb4e | René Nussbaumer | "count=%d" % size]
|
1825 | da63bb4e | René Nussbaumer | result = utils.RunCmd(cmd) |
1826 | 69dd363f | René Nussbaumer | |
1827 | 69dd363f | René Nussbaumer | if result.failed:
|
1828 | 69dd363f | René Nussbaumer | _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
|
1829 | 69dd363f | René Nussbaumer | result.fail_reason, result.output) |
1830 | 69dd363f | René Nussbaumer | |
1831 | 69dd363f | René Nussbaumer | |
1832 | da63bb4e | René Nussbaumer | def BlockdevWipe(disk, offset, size): |
1833 | 69dd363f | René Nussbaumer | """Wipes a block device.
|
1834 | 69dd363f | René Nussbaumer |
|
1835 | 69dd363f | René Nussbaumer | @type disk: L{objects.Disk}
|
1836 | 69dd363f | René Nussbaumer | @param disk: the disk object we want to wipe
|
1837 | da63bb4e | René Nussbaumer | @type offset: int
|
1838 | da63bb4e | René Nussbaumer | @param offset: The offset in MiB in the file
|
1839 | da63bb4e | René Nussbaumer | @type size: int
|
1840 | da63bb4e | René Nussbaumer | @param size: The size in MiB to write
|
1841 | 69dd363f | René Nussbaumer |
|
1842 | 69dd363f | René Nussbaumer | """
|
1843 | 69dd363f | René Nussbaumer | try:
|
1844 | 69dd363f | René Nussbaumer | rdev = _RecursiveFindBD(disk) |
1845 | da63bb4e | René Nussbaumer | except errors.BlockDeviceError:
|
1846 | da63bb4e | René Nussbaumer | rdev = None
|
1847 | da63bb4e | René Nussbaumer | |
1848 | da63bb4e | René Nussbaumer | if not rdev: |
1849 | da63bb4e | René Nussbaumer | _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
|
1850 | da63bb4e | René Nussbaumer | |
1851 | da63bb4e | René Nussbaumer | # Do cross verify some of the parameters
|
1852 | 0188611b | Michael Hanselmann | if offset < 0: |
1853 | 0188611b | Michael Hanselmann | _Fail("Negative offset")
|
1854 | 0188611b | Michael Hanselmann | if size < 0: |
1855 | 0188611b | Michael Hanselmann | _Fail("Negative size")
|
1856 | da63bb4e | René Nussbaumer | if offset > rdev.size:
|
1857 | da63bb4e | René Nussbaumer | _Fail("Offset is bigger than device size")
|
1858 | da63bb4e | René Nussbaumer | if (offset + size) > rdev.size:
|
1859 | da63bb4e | René Nussbaumer | _Fail("The provided offset and size to wipe is bigger than device size")
|
1860 | 69dd363f | René Nussbaumer | |
1861 | da63bb4e | René Nussbaumer | _WipeDevice(rdev.dev_path, offset, size) |
1862 | 69dd363f | René Nussbaumer | |
1863 | 69dd363f | René Nussbaumer | |
1864 | 5119c79e | René Nussbaumer | def BlockdevPauseResumeSync(disks, pause): |
1865 | 5119c79e | René Nussbaumer | """Pause or resume the sync of the block device.
|
1866 | 5119c79e | René Nussbaumer |
|
1867 | 0f39886a | René Nussbaumer | @type disks: list of L{objects.Disk}
|
1868 | 0f39886a | René Nussbaumer | @param disks: the disks object we want to pause/resume
|
1869 | 5119c79e | René Nussbaumer | @type pause: bool
|
1870 | 5119c79e | René Nussbaumer | @param pause: Wheater to pause or resume
|
1871 | 5119c79e | René Nussbaumer |
|
1872 | 5119c79e | René Nussbaumer | """
|
1873 | 5119c79e | René Nussbaumer | success = [] |
1874 | 5119c79e | René Nussbaumer | for disk in disks: |
1875 | 5119c79e | René Nussbaumer | try:
|
1876 | 5119c79e | René Nussbaumer | rdev = _RecursiveFindBD(disk) |
1877 | 5119c79e | René Nussbaumer | except errors.BlockDeviceError:
|
1878 | 5119c79e | René Nussbaumer | rdev = None
|
1879 | 5119c79e | René Nussbaumer | |
1880 | 5119c79e | René Nussbaumer | if not rdev: |
1881 | 5119c79e | René Nussbaumer | success.append((False, ("Cannot change sync for device %s:" |
1882 | 5119c79e | René Nussbaumer | " device not found" % disk.iv_name)))
|
1883 | 5119c79e | René Nussbaumer | continue
|
1884 | 5119c79e | René Nussbaumer | |
1885 | 5119c79e | René Nussbaumer | result = rdev.PauseResumeSync(pause) |
1886 | 5119c79e | René Nussbaumer | |
1887 | 5119c79e | René Nussbaumer | if result:
|
1888 | 5119c79e | René Nussbaumer | success.append((result, None))
|
1889 | 5119c79e | René Nussbaumer | else:
|
1890 | 5119c79e | René Nussbaumer | if pause:
|
1891 | 5119c79e | René Nussbaumer | msg = "Pause"
|
1892 | 5119c79e | René Nussbaumer | else:
|
1893 | 5119c79e | René Nussbaumer | msg = "Resume"
|
1894 | 5119c79e | René Nussbaumer | success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
|
1895 | 5119c79e | René Nussbaumer | |
1896 | 5119c79e | René Nussbaumer | return success
|
1897 | 5119c79e | René Nussbaumer | |
1898 | 5119c79e | René Nussbaumer | |
1899 | 821d1bd1 | Iustin Pop | def BlockdevRemove(disk): |
1900 | a8083063 | Iustin Pop | """Remove a block device.
|
1901 | a8083063 | Iustin Pop |
|
1902 | 10c2650b | Iustin Pop | @note: This is intended to be called recursively.
|
1903 | 10c2650b | Iustin Pop |
|
1904 | c41eea6e | Iustin Pop | @type disk: L{objects.Disk}
|
1905 | 10c2650b | Iustin Pop | @param disk: the disk object we should remove
|
1906 | 10c2650b | Iustin Pop | @rtype: boolean
|
1907 | 10c2650b | Iustin Pop | @return: the success of the operation
|
1908 | a8083063 | Iustin Pop |
|
1909 | a8083063 | Iustin Pop | """
|
1910 | e1bc0878 | Iustin Pop | msgs = [] |
1911 | a8083063 | Iustin Pop | try:
|
1912 | bca2e7f4 | Iustin Pop | rdev = _RecursiveFindBD(disk) |
1913 | a8083063 | Iustin Pop | except errors.BlockDeviceError, err:
|
1914 | a8083063 | Iustin Pop | # probably can't attach
|
1915 | 18682bca | Iustin Pop | logging.info("Can't attach to device %s in remove", disk)
|
1916 | a8083063 | Iustin Pop | rdev = None
|
1917 | a8083063 | Iustin Pop | if rdev is not None: |
1918 | 3f78eef2 | Iustin Pop | r_path = rdev.dev_path |
1919 | e1bc0878 | Iustin Pop | try:
|
1920 | 0c6c04ec | Iustin Pop | rdev.Remove() |
1921 | e1bc0878 | Iustin Pop | except errors.BlockDeviceError, err:
|
1922 | e1bc0878 | Iustin Pop | msgs.append(str(err))
|
1923 | c26a6bd2 | Iustin Pop | if not msgs: |
1924 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
1925 | e1bc0878 | Iustin Pop | |
1926 | a8083063 | Iustin Pop | if disk.children:
|
1927 | a8083063 | Iustin Pop | for child in disk.children: |
1928 | c26a6bd2 | Iustin Pop | try:
|
1929 | c26a6bd2 | Iustin Pop | BlockdevRemove(child) |
1930 | c26a6bd2 | Iustin Pop | except RPCFail, err:
|
1931 | c26a6bd2 | Iustin Pop | msgs.append(str(err))
|
1932 | e1bc0878 | Iustin Pop | |
1933 | c26a6bd2 | Iustin Pop | if msgs:
|
1934 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
1935 | afdc3985 | Iustin Pop | |
1936 | a8083063 | Iustin Pop | |
1937 | 3f78eef2 | Iustin Pop | def _RecursiveAssembleBD(disk, owner, as_primary): |
1938 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
1939 | a8083063 | Iustin Pop |
|
1940 | a8083063 | Iustin Pop | This is run on the primary and secondary nodes for an instance.
|
1941 | a8083063 | Iustin Pop |
|
1942 | 10c2650b | Iustin Pop | @note: this function is called recursively.
|
1943 | a8083063 | Iustin Pop |
|
1944 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
1945 | 10c2650b | Iustin Pop | @param disk: the disk we try to assemble
|
1946 | 10c2650b | Iustin Pop | @type owner: str
|
1947 | 10c2650b | Iustin Pop | @param owner: the name of the instance which owns the disk
|
1948 | 10c2650b | Iustin Pop | @type as_primary: boolean
|
1949 | 10c2650b | Iustin Pop | @param as_primary: if we should make the block device
|
1950 | 10c2650b | Iustin Pop | read/write
|
1951 | a8083063 | Iustin Pop |
|
1952 | 10c2650b | Iustin Pop | @return: the assembled device or None (in case no device
|
1953 | 10c2650b | Iustin Pop | was assembled)
|
1954 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: in case there is an error
|
1955 | 10c2650b | Iustin Pop | during the activation of the children or the device
|
1956 | 10c2650b | Iustin Pop | itself
|
1957 | a8083063 | Iustin Pop |
|
1958 | a8083063 | Iustin Pop | """
|
1959 | a8083063 | Iustin Pop | children = [] |
1960 | a8083063 | Iustin Pop | if disk.children:
|
1961 | fc1dc9d7 | Iustin Pop | mcn = disk.ChildrenNeeded() |
1962 | fc1dc9d7 | Iustin Pop | if mcn == -1: |
1963 | fc1dc9d7 | Iustin Pop | mcn = 0 # max number of Nones allowed |
1964 | fc1dc9d7 | Iustin Pop | else:
|
1965 | fc1dc9d7 | Iustin Pop | mcn = len(disk.children) - mcn # max number of Nones |
1966 | a8083063 | Iustin Pop | for chld_disk in disk.children: |
1967 | fc1dc9d7 | Iustin Pop | try:
|
1968 | fc1dc9d7 | Iustin Pop | cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary) |
1969 | fc1dc9d7 | Iustin Pop | except errors.BlockDeviceError, err:
|
1970 | 7803d4d3 | Iustin Pop | if children.count(None) >= mcn: |
1971 | fc1dc9d7 | Iustin Pop | raise
|
1972 | fc1dc9d7 | Iustin Pop | cdev = None
|
1973 | 1063abd1 | Iustin Pop | logging.error("Error in child activation (but continuing): %s",
|
1974 | 1063abd1 | Iustin Pop | str(err))
|
1975 | fc1dc9d7 | Iustin Pop | children.append(cdev) |
1976 | a8083063 | Iustin Pop | |
1977 | a8083063 | Iustin Pop | if as_primary or disk.AssembleOnSecondary(): |
1978 | 94dcbdb0 | Andrea Spadaccini | r_dev = bdev.Assemble(disk, children) |
1979 | a8083063 | Iustin Pop | result = r_dev |
1980 | a8083063 | Iustin Pop | if as_primary or disk.OpenOnSecondary(): |
1981 | a8083063 | Iustin Pop | r_dev.Open() |
1982 | 3f78eef2 | Iustin Pop | DevCacheManager.UpdateCache(r_dev.dev_path, owner, |
1983 | 3f78eef2 | Iustin Pop | as_primary, disk.iv_name) |
1984 | 3f78eef2 | Iustin Pop | |
1985 | a8083063 | Iustin Pop | else:
|
1986 | a8083063 | Iustin Pop | result = True
|
1987 | a8083063 | Iustin Pop | return result
|
1988 | a8083063 | Iustin Pop | |
1989 | a8083063 | Iustin Pop | |
1990 | c417e115 | Iustin Pop | def BlockdevAssemble(disk, owner, as_primary, idx): |
1991 | a8083063 | Iustin Pop | """Activate a block device for an instance.
|
1992 | a8083063 | Iustin Pop |
|
1993 | a8083063 | Iustin Pop | This is a wrapper over _RecursiveAssembleBD.
|
1994 | a8083063 | Iustin Pop |
|
1995 | b1206984 | Iustin Pop | @rtype: str or boolean
|
1996 | b1206984 | Iustin Pop | @return: a C{/dev/...} path for primary nodes, and
|
1997 | b1206984 | Iustin Pop | C{True} for secondary nodes
|
1998 | a8083063 | Iustin Pop |
|
1999 | a8083063 | Iustin Pop | """
|
2000 | 53c14ef1 | Iustin Pop | try:
|
2001 | 53c14ef1 | Iustin Pop | result = _RecursiveAssembleBD(disk, owner, as_primary) |
2002 | 89ff748d | Thomas Thrainer | if isinstance(result, BlockDev): |
2003 | b459a848 | Andrea Spadaccini | # pylint: disable=E1103
|
2004 | 53c14ef1 | Iustin Pop | result = result.dev_path |
2005 | c417e115 | Iustin Pop | if as_primary:
|
2006 | c417e115 | Iustin Pop | _SymlinkBlockDev(owner, result, idx) |
2007 | 53c14ef1 | Iustin Pop | except errors.BlockDeviceError, err:
|
2008 | afdc3985 | Iustin Pop | _Fail("Error while assembling disk: %s", err, exc=True) |
2009 | c417e115 | Iustin Pop | except OSError, err: |
2010 | c417e115 | Iustin Pop | _Fail("Error while symlinking disk: %s", err, exc=True) |
2011 | afdc3985 | Iustin Pop | |
2012 | c26a6bd2 | Iustin Pop | return result
|
2013 | a8083063 | Iustin Pop | |
2014 | a8083063 | Iustin Pop | |
2015 | 821d1bd1 | Iustin Pop | def BlockdevShutdown(disk): |
2016 | a8083063 | Iustin Pop | """Shut down a block device.
|
2017 | a8083063 | Iustin Pop |
|
2018 | 5bbd3f7f | Michael Hanselmann | First, if the device is assembled (Attach() is successful), then
|
2019 | c41eea6e | Iustin Pop | the device is shutdown. Then the children of the device are
|
2020 | c41eea6e | Iustin Pop | shutdown.
|
2021 | a8083063 | Iustin Pop |
|
2022 | a8083063 | Iustin Pop | This function is called recursively. Note that we don't cache the
|
2023 | a8083063 | Iustin Pop | children or such, as oppossed to assemble, shutdown of different
|
2024 | a8083063 | Iustin Pop | devices doesn't require that the upper device was active.
|
2025 | a8083063 | Iustin Pop |
|
2026 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
2027 | 10c2650b | Iustin Pop | @param disk: the description of the disk we should
|
2028 | 10c2650b | Iustin Pop | shutdown
|
2029 | c26a6bd2 | Iustin Pop | @rtype: None
|
2030 | 10c2650b | Iustin Pop |
|
2031 | a8083063 | Iustin Pop | """
|
2032 | cacfd1fd | Iustin Pop | msgs = [] |
2033 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2034 | a8083063 | Iustin Pop | if r_dev is not None: |
2035 | 3f78eef2 | Iustin Pop | r_path = r_dev.dev_path |
2036 | cacfd1fd | Iustin Pop | try:
|
2037 | 746f7476 | Iustin Pop | r_dev.Shutdown() |
2038 | 746f7476 | Iustin Pop | DevCacheManager.RemoveCache(r_path) |
2039 | cacfd1fd | Iustin Pop | except errors.BlockDeviceError, err:
|
2040 | cacfd1fd | Iustin Pop | msgs.append(str(err))
|
2041 | 746f7476 | Iustin Pop | |
2042 | a8083063 | Iustin Pop | if disk.children:
|
2043 | a8083063 | Iustin Pop | for child in disk.children: |
2044 | c26a6bd2 | Iustin Pop | try:
|
2045 | c26a6bd2 | Iustin Pop | BlockdevShutdown(child) |
2046 | c26a6bd2 | Iustin Pop | except RPCFail, err:
|
2047 | c26a6bd2 | Iustin Pop | msgs.append(str(err))
|
2048 | 746f7476 | Iustin Pop | |
2049 | c26a6bd2 | Iustin Pop | if msgs:
|
2050 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
2051 | a8083063 | Iustin Pop | |
2052 | a8083063 | Iustin Pop | |
2053 | 821d1bd1 | Iustin Pop | def BlockdevAddchildren(parent_cdev, new_cdevs): |
2054 | 153d9724 | Iustin Pop | """Extend a mirrored block device.
|
2055 | a8083063 | Iustin Pop |
|
2056 | 10c2650b | Iustin Pop | @type parent_cdev: L{objects.Disk}
|
2057 | 10c2650b | Iustin Pop | @param parent_cdev: the disk to which we should add children
|
2058 | 10c2650b | Iustin Pop | @type new_cdevs: list of L{objects.Disk}
|
2059 | 10c2650b | Iustin Pop | @param new_cdevs: the list of children which we should add
|
2060 | c26a6bd2 | Iustin Pop | @rtype: None
|
2061 | 10c2650b | Iustin Pop |
|
2062 | a8083063 | Iustin Pop | """
|
2063 | bca2e7f4 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev) |
2064 | 153d9724 | Iustin Pop | if parent_bdev is None: |
2065 | 2cc6781a | Iustin Pop | _Fail("Can't find parent device '%s' in add children", parent_cdev)
|
2066 | 153d9724 | Iustin Pop | new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs] |
2067 | 153d9724 | Iustin Pop | if new_bdevs.count(None) > 0: |
2068 | 2cc6781a | Iustin Pop | _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
|
2069 | 153d9724 | Iustin Pop | parent_bdev.AddChildren(new_bdevs) |
2070 | a8083063 | Iustin Pop | |
2071 | a8083063 | Iustin Pop | |
2072 | 821d1bd1 | Iustin Pop | def BlockdevRemovechildren(parent_cdev, new_cdevs): |
2073 | 153d9724 | Iustin Pop | """Shrink a mirrored block device.
|
2074 | a8083063 | Iustin Pop |
|
2075 | 10c2650b | Iustin Pop | @type parent_cdev: L{objects.Disk}
|
2076 | 10c2650b | Iustin Pop | @param parent_cdev: the disk from which we should remove children
|
2077 | 10c2650b | Iustin Pop | @type new_cdevs: list of L{objects.Disk}
|
2078 | 10c2650b | Iustin Pop | @param new_cdevs: the list of children which we should remove
|
2079 | c26a6bd2 | Iustin Pop | @rtype: None
|
2080 | 10c2650b | Iustin Pop |
|
2081 | a8083063 | Iustin Pop | """
|
2082 | 153d9724 | Iustin Pop | parent_bdev = _RecursiveFindBD(parent_cdev) |
2083 | 153d9724 | Iustin Pop | if parent_bdev is None: |
2084 | 2cc6781a | Iustin Pop | _Fail("Can't find parent device '%s' in remove children", parent_cdev)
|
2085 | e739bd57 | Iustin Pop | devs = [] |
2086 | e739bd57 | Iustin Pop | for disk in new_cdevs: |
2087 | e739bd57 | Iustin Pop | rpath = disk.StaticDevPath() |
2088 | e739bd57 | Iustin Pop | if rpath is None: |
2089 | e739bd57 | Iustin Pop | bd = _RecursiveFindBD(disk) |
2090 | e739bd57 | Iustin Pop | if bd is None: |
2091 | 2cc6781a | Iustin Pop | _Fail("Can't find device %s while removing children", disk)
|
2092 | e739bd57 | Iustin Pop | else:
|
2093 | e739bd57 | Iustin Pop | devs.append(bd.dev_path) |
2094 | e739bd57 | Iustin Pop | else:
|
2095 | e51db2a6 | Iustin Pop | if not utils.IsNormAbsPath(rpath): |
2096 | e51db2a6 | Iustin Pop | _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
|
2097 | e739bd57 | Iustin Pop | devs.append(rpath) |
2098 | e739bd57 | Iustin Pop | parent_bdev.RemoveChildren(devs) |
2099 | a8083063 | Iustin Pop | |
2100 | a8083063 | Iustin Pop | |
2101 | 821d1bd1 | Iustin Pop | def BlockdevGetmirrorstatus(disks): |
2102 | a8083063 | Iustin Pop | """Get the mirroring status of a list of devices.
|
2103 | a8083063 | Iustin Pop |
|
2104 | 10c2650b | Iustin Pop | @type disks: list of L{objects.Disk}
|
2105 | 10c2650b | Iustin Pop | @param disks: the list of disks which we should query
|
2106 | 10c2650b | Iustin Pop | @rtype: disk
|
2107 | c6a9dffa | Michael Hanselmann | @return: List of L{objects.BlockDevStatus}, one for each disk
|
2108 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: if any of the disks cannot be
|
2109 | 10c2650b | Iustin Pop | found
|
2110 | a8083063 | Iustin Pop |
|
2111 | a8083063 | Iustin Pop | """
|
2112 | a8083063 | Iustin Pop | stats = [] |
2113 | a8083063 | Iustin Pop | for dsk in disks: |
2114 | a8083063 | Iustin Pop | rbd = _RecursiveFindBD(dsk) |
2115 | a8083063 | Iustin Pop | if rbd is None: |
2116 | 3efa9051 | Iustin Pop | _Fail("Can't find device %s", dsk)
|
2117 | 96acbc09 | Michael Hanselmann | |
2118 | 36145b12 | Michael Hanselmann | stats.append(rbd.CombinedSyncStatus()) |
2119 | 96acbc09 | Michael Hanselmann | |
2120 | c26a6bd2 | Iustin Pop | return stats
|
2121 | a8083063 | Iustin Pop | |
2122 | a8083063 | Iustin Pop | |
2123 | c6a9dffa | Michael Hanselmann | def BlockdevGetmirrorstatusMulti(disks): |
2124 | c6a9dffa | Michael Hanselmann | """Get the mirroring status of a list of devices.
|
2125 | c6a9dffa | Michael Hanselmann |
|
2126 | c6a9dffa | Michael Hanselmann | @type disks: list of L{objects.Disk}
|
2127 | c6a9dffa | Michael Hanselmann | @param disks: the list of disks which we should query
|
2128 | c6a9dffa | Michael Hanselmann | @rtype: disk
|
2129 | c6a9dffa | Michael Hanselmann | @return: List of tuples, (bool, status), one for each disk; bool denotes
|
2130 | c6a9dffa | Michael Hanselmann | success/failure, status is L{objects.BlockDevStatus} on success, string
|
2131 | c6a9dffa | Michael Hanselmann | otherwise
|
2132 | c6a9dffa | Michael Hanselmann |
|
2133 | c6a9dffa | Michael Hanselmann | """
|
2134 | c6a9dffa | Michael Hanselmann | result = [] |
2135 | c6a9dffa | Michael Hanselmann | for disk in disks: |
2136 | c6a9dffa | Michael Hanselmann | try:
|
2137 | c6a9dffa | Michael Hanselmann | rbd = _RecursiveFindBD(disk) |
2138 | c6a9dffa | Michael Hanselmann | if rbd is None: |
2139 | c6a9dffa | Michael Hanselmann | result.append((False, "Can't find device %s" % disk)) |
2140 | c6a9dffa | Michael Hanselmann | continue
|
2141 | c6a9dffa | Michael Hanselmann | |
2142 | c6a9dffa | Michael Hanselmann | status = rbd.CombinedSyncStatus() |
2143 | c6a9dffa | Michael Hanselmann | except errors.BlockDeviceError, err:
|
2144 | c6a9dffa | Michael Hanselmann | logging.exception("Error while getting disk status")
|
2145 | c6a9dffa | Michael Hanselmann | result.append((False, str(err))) |
2146 | c6a9dffa | Michael Hanselmann | else:
|
2147 | c6a9dffa | Michael Hanselmann | result.append((True, status))
|
2148 | c6a9dffa | Michael Hanselmann | |
2149 | c6a9dffa | Michael Hanselmann | assert len(disks) == len(result) |
2150 | c6a9dffa | Michael Hanselmann | |
2151 | c6a9dffa | Michael Hanselmann | return result
|
2152 | c6a9dffa | Michael Hanselmann | |
2153 | c6a9dffa | Michael Hanselmann | |
2154 | bca2e7f4 | Iustin Pop | def _RecursiveFindBD(disk): |
2155 | a8083063 | Iustin Pop | """Check if a device is activated.
|
2156 | a8083063 | Iustin Pop |
|
2157 | 5bbd3f7f | Michael Hanselmann | If so, return information about the real device.
|
2158 | a8083063 | Iustin Pop |
|
2159 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
2160 | 10c2650b | Iustin Pop | @param disk: the disk object we need to find
|
2161 | a8083063 | Iustin Pop |
|
2162 | 10c2650b | Iustin Pop | @return: None if the device can't be found,
|
2163 | 10c2650b | Iustin Pop | otherwise the device instance
|
2164 | a8083063 | Iustin Pop |
|
2165 | a8083063 | Iustin Pop | """
|
2166 | a8083063 | Iustin Pop | children = [] |
2167 | a8083063 | Iustin Pop | if disk.children:
|
2168 | a8083063 | Iustin Pop | for chdisk in disk.children: |
2169 | a8083063 | Iustin Pop | children.append(_RecursiveFindBD(chdisk)) |
2170 | a8083063 | Iustin Pop | |
2171 | 94dcbdb0 | Andrea Spadaccini | return bdev.FindDevice(disk, children)
|
2172 | a8083063 | Iustin Pop | |
2173 | a8083063 | Iustin Pop | |
2174 | f2e07bb4 | Michael Hanselmann | def _OpenRealBD(disk): |
2175 | f2e07bb4 | Michael Hanselmann | """Opens the underlying block device of a disk.
|
2176 | f2e07bb4 | Michael Hanselmann |
|
2177 | f2e07bb4 | Michael Hanselmann | @type disk: L{objects.Disk}
|
2178 | f2e07bb4 | Michael Hanselmann | @param disk: the disk object we want to open
|
2179 | f2e07bb4 | Michael Hanselmann |
|
2180 | f2e07bb4 | Michael Hanselmann | """
|
2181 | f2e07bb4 | Michael Hanselmann | real_disk = _RecursiveFindBD(disk) |
2182 | f2e07bb4 | Michael Hanselmann | if real_disk is None: |
2183 | f2e07bb4 | Michael Hanselmann | _Fail("Block device '%s' is not set up", disk)
|
2184 | f2e07bb4 | Michael Hanselmann | |
2185 | f2e07bb4 | Michael Hanselmann | real_disk.Open() |
2186 | f2e07bb4 | Michael Hanselmann | |
2187 | f2e07bb4 | Michael Hanselmann | return real_disk
|
2188 | f2e07bb4 | Michael Hanselmann | |
2189 | f2e07bb4 | Michael Hanselmann | |
2190 | 821d1bd1 | Iustin Pop | def BlockdevFind(disk): |
2191 | a8083063 | Iustin Pop | """Check if a device is activated.
|
2192 | a8083063 | Iustin Pop |
|
2193 | 5bbd3f7f | Michael Hanselmann | If it is, return information about the real device.
|
2194 | a8083063 | Iustin Pop |
|
2195 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
2196 | 10c2650b | Iustin Pop | @param disk: the disk to find
|
2197 | 96acbc09 | Michael Hanselmann | @rtype: None or objects.BlockDevStatus
|
2198 | 96acbc09 | Michael Hanselmann | @return: None if the disk cannot be found, otherwise a the current
|
2199 | 96acbc09 | Michael Hanselmann | information
|
2200 | a8083063 | Iustin Pop |
|
2201 | a8083063 | Iustin Pop | """
|
2202 | 23829f6f | Iustin Pop | try:
|
2203 | 23829f6f | Iustin Pop | rbd = _RecursiveFindBD(disk) |
2204 | 23829f6f | Iustin Pop | except errors.BlockDeviceError, err:
|
2205 | 2cc6781a | Iustin Pop | _Fail("Failed to find device: %s", err, exc=True) |
2206 | 96acbc09 | Michael Hanselmann | |
2207 | a8083063 | Iustin Pop | if rbd is None: |
2208 | c26a6bd2 | Iustin Pop | return None |
2209 | 96acbc09 | Michael Hanselmann | |
2210 | 96acbc09 | Michael Hanselmann | return rbd.GetSyncStatus()
|
2211 | a8083063 | Iustin Pop | |
2212 | a8083063 | Iustin Pop | |
2213 | 6ef8077e | Bernardo Dal Seno | def BlockdevGetdimensions(disks): |
2214 | 968a7623 | Iustin Pop | """Computes the size of the given disks.
|
2215 | 968a7623 | Iustin Pop |
|
2216 | 968a7623 | Iustin Pop | If a disk is not found, returns None instead.
|
2217 | 968a7623 | Iustin Pop |
|
2218 | 968a7623 | Iustin Pop | @type disks: list of L{objects.Disk}
|
2219 | 968a7623 | Iustin Pop | @param disks: the list of disk to compute the size for
|
2220 | 968a7623 | Iustin Pop | @rtype: list
|
2221 | 968a7623 | Iustin Pop | @return: list with elements None if the disk cannot be found,
|
2222 | 6ef8077e | Bernardo Dal Seno | otherwise the pair (size, spindles), where spindles is None if the
|
2223 | 6ef8077e | Bernardo Dal Seno | device doesn't support that
|
2224 | 968a7623 | Iustin Pop |
|
2225 | 968a7623 | Iustin Pop | """
|
2226 | 968a7623 | Iustin Pop | result = [] |
2227 | 968a7623 | Iustin Pop | for cf in disks: |
2228 | 968a7623 | Iustin Pop | try:
|
2229 | 968a7623 | Iustin Pop | rbd = _RecursiveFindBD(cf) |
2230 | 1122eb25 | Iustin Pop | except errors.BlockDeviceError:
|
2231 | 968a7623 | Iustin Pop | result.append(None)
|
2232 | 968a7623 | Iustin Pop | continue
|
2233 | 968a7623 | Iustin Pop | if rbd is None: |
2234 | 968a7623 | Iustin Pop | result.append(None)
|
2235 | 968a7623 | Iustin Pop | else:
|
2236 | 6ef8077e | Bernardo Dal Seno | result.append(rbd.GetActualDimensions()) |
2237 | 968a7623 | Iustin Pop | return result
|
2238 | 968a7623 | Iustin Pop | |
2239 | 968a7623 | Iustin Pop | |
2240 | 858f3d18 | Iustin Pop | def BlockdevExport(disk, dest_node, dest_path, cluster_name): |
2241 | 858f3d18 | Iustin Pop | """Export a block device to a remote node.
|
2242 | 858f3d18 | Iustin Pop |
|
2243 | 858f3d18 | Iustin Pop | @type disk: L{objects.Disk}
|
2244 | 858f3d18 | Iustin Pop | @param disk: the description of the disk to export
|
2245 | 858f3d18 | Iustin Pop | @type dest_node: str
|
2246 | 858f3d18 | Iustin Pop | @param dest_node: the destination node to export to
|
2247 | 858f3d18 | Iustin Pop | @type dest_path: str
|
2248 | 858f3d18 | Iustin Pop | @param dest_path: the destination path on the target node
|
2249 | 858f3d18 | Iustin Pop | @type cluster_name: str
|
2250 | 858f3d18 | Iustin Pop | @param cluster_name: the cluster name, needed for SSH hostalias
|
2251 | 858f3d18 | Iustin Pop | @rtype: None
|
2252 | 858f3d18 | Iustin Pop |
|
2253 | 858f3d18 | Iustin Pop | """
|
2254 | f2e07bb4 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
2255 | 858f3d18 | Iustin Pop | |
2256 | 858f3d18 | Iustin Pop | # the block size on the read dd is 1MiB to match our units
|
2257 | 858f3d18 | Iustin Pop | expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
|
2258 | 858f3d18 | Iustin Pop | "dd if=%s bs=1048576 count=%s",
|
2259 | 858f3d18 | Iustin Pop | real_disk.dev_path, str(disk.size))
|
2260 | 858f3d18 | Iustin Pop | |
2261 | 858f3d18 | Iustin Pop | # we set here a smaller block size as, due to ssh buffering, more
|
2262 | 858f3d18 | Iustin Pop | # than 64-128k will mostly ignored; we use nocreat to fail if the
|
2263 | 858f3d18 | Iustin Pop | # device is not already there or we pass a wrong path; we use
|
2264 | 858f3d18 | Iustin Pop | # notrunc to no attempt truncate on an LV device; we use oflag=dsync
|
2265 | 858f3d18 | Iustin Pop | # to not buffer too much memory; this means that at best, we flush
|
2266 | 858f3d18 | Iustin Pop | # every 64k, which will not be very fast
|
2267 | 858f3d18 | Iustin Pop | destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
|
2268 | 858f3d18 | Iustin Pop | " oflag=dsync", dest_path)
|
2269 | 858f3d18 | Iustin Pop | |
2270 | 858f3d18 | Iustin Pop | remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node, |
2271 | 052783ff | Michael Hanselmann | constants.SSH_LOGIN_USER, |
2272 | 858f3d18 | Iustin Pop | destcmd) |
2273 | 858f3d18 | Iustin Pop | |
2274 | 858f3d18 | Iustin Pop | # all commands have been checked, so we're safe to combine them
|
2275 | d0c8c01d | Iustin Pop | command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
|
2276 | 858f3d18 | Iustin Pop | |
2277 | 858f3d18 | Iustin Pop | result = utils.RunCmd(["bash", "-c", command]) |
2278 | 858f3d18 | Iustin Pop | |
2279 | 858f3d18 | Iustin Pop | if result.failed:
|
2280 | 858f3d18 | Iustin Pop | _Fail("Disk copy command '%s' returned error: %s"
|
2281 | 858f3d18 | Iustin Pop | " output: %s", command, result.fail_reason, result.output)
|
2282 | 858f3d18 | Iustin Pop | |
2283 | 858f3d18 | Iustin Pop | |
2284 | a8083063 | Iustin Pop | def UploadFile(file_name, data, mode, uid, gid, atime, mtime): |
2285 | a8083063 | Iustin Pop | """Write a file to the filesystem.
|
2286 | a8083063 | Iustin Pop |
|
2287 | a8083063 | Iustin Pop | This allows the master to overwrite(!) a file. It will only perform
|
2288 | a8083063 | Iustin Pop | the operation if the file belongs to a list of configuration files.
|
2289 | a8083063 | Iustin Pop |
|
2290 | 10c2650b | Iustin Pop | @type file_name: str
|
2291 | 10c2650b | Iustin Pop | @param file_name: the target file name
|
2292 | 10c2650b | Iustin Pop | @type data: str
|
2293 | 10c2650b | Iustin Pop | @param data: the new contents of the file
|
2294 | 10c2650b | Iustin Pop | @type mode: int
|
2295 | 10c2650b | Iustin Pop | @param mode: the mode to give the file (can be None)
|
2296 | 9a914f7a | René Nussbaumer | @type uid: string
|
2297 | 9a914f7a | René Nussbaumer | @param uid: the owner of the file
|
2298 | 9a914f7a | René Nussbaumer | @type gid: string
|
2299 | 9a914f7a | René Nussbaumer | @param gid: the group of the file
|
2300 | 10c2650b | Iustin Pop | @type atime: float
|
2301 | 10c2650b | Iustin Pop | @param atime: the atime to set on the file (can be None)
|
2302 | 10c2650b | Iustin Pop | @type mtime: float
|
2303 | 10c2650b | Iustin Pop | @param mtime: the mtime to set on the file (can be None)
|
2304 | c26a6bd2 | Iustin Pop | @rtype: None
|
2305 | 10c2650b | Iustin Pop |
|
2306 | a8083063 | Iustin Pop | """
|
2307 | cffbbae7 | Michael Hanselmann | file_name = vcluster.LocalizeVirtualPath(file_name) |
2308 | cffbbae7 | Michael Hanselmann | |
2309 | a8083063 | Iustin Pop | if not os.path.isabs(file_name): |
2310 | 2cc6781a | Iustin Pop | _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
|
2311 | a8083063 | Iustin Pop | |
2312 | 360b0dc2 | Iustin Pop | if file_name not in _ALLOWED_UPLOAD_FILES: |
2313 | 2cc6781a | Iustin Pop | _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
|
2314 | 2cc6781a | Iustin Pop | file_name) |
2315 | a8083063 | Iustin Pop | |
2316 | 12bce260 | Michael Hanselmann | raw_data = _Decompress(data) |
2317 | 12bce260 | Michael Hanselmann | |
2318 | 9a914f7a | René Nussbaumer | if not (isinstance(uid, basestring) and isinstance(gid, basestring)): |
2319 | 9a914f7a | René Nussbaumer | _Fail("Invalid username/groupname type")
|
2320 | 9a914f7a | René Nussbaumer | |
2321 | 9a914f7a | René Nussbaumer | getents = runtime.GetEnts() |
2322 | 9a914f7a | René Nussbaumer | uid = getents.LookupUser(uid) |
2323 | 9a914f7a | René Nussbaumer | gid = getents.LookupGroup(gid) |
2324 | 9a914f7a | René Nussbaumer | |
2325 | 8f065ae2 | Iustin Pop | utils.SafeWriteFile(file_name, None,
|
2326 | 8f065ae2 | Iustin Pop | data=raw_data, mode=mode, uid=uid, gid=gid, |
2327 | 8f065ae2 | Iustin Pop | atime=atime, mtime=mtime) |
2328 | a8083063 | Iustin Pop | |
2329 | 386b57af | Iustin Pop | |
2330 | b2f29800 | René Nussbaumer | def RunOob(oob_program, command, node, timeout): |
2331 | b2f29800 | René Nussbaumer | """Executes oob_program with given command on given node.
|
2332 | b2f29800 | René Nussbaumer |
|
2333 | b2f29800 | René Nussbaumer | @param oob_program: The path to the executable oob_program
|
2334 | b2f29800 | René Nussbaumer | @param command: The command to invoke on oob_program
|
2335 | b2f29800 | René Nussbaumer | @param node: The node given as an argument to the program
|
2336 | b2f29800 | René Nussbaumer | @param timeout: Timeout after which we kill the oob program
|
2337 | b2f29800 | René Nussbaumer |
|
2338 | b2f29800 | René Nussbaumer | @return: stdout
|
2339 | b2f29800 | René Nussbaumer | @raise RPCFail: If execution fails for some reason
|
2340 | b2f29800 | René Nussbaumer |
|
2341 | b2f29800 | René Nussbaumer | """
|
2342 | b2f29800 | René Nussbaumer | result = utils.RunCmd([oob_program, command, node], timeout=timeout) |
2343 | b2f29800 | René Nussbaumer | |
2344 | b2f29800 | René Nussbaumer | if result.failed:
|
2345 | b2f29800 | René Nussbaumer | _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
|
2346 | b2f29800 | René Nussbaumer | result.fail_reason, result.output) |
2347 | b2f29800 | René Nussbaumer | |
2348 | b2f29800 | René Nussbaumer | return result.stdout
|
2349 | b2f29800 | René Nussbaumer | |
2350 | b2f29800 | René Nussbaumer | |
2351 | c19f9810 | Iustin Pop | def _OSOndiskAPIVersion(os_dir): |
2352 | 2f8598a5 | Alexander Schreiber | """Compute and return the API version of a given OS.
|
2353 | a8083063 | Iustin Pop |
|
2354 | c19f9810 | Iustin Pop | This function will try to read the API version of the OS residing in
|
2355 | c19f9810 | Iustin Pop | the 'os_dir' directory.
|
2356 | 7c3d51d4 | Guido Trotter |
|
2357 | 10c2650b | Iustin Pop | @type os_dir: str
|
2358 | c19f9810 | Iustin Pop | @param os_dir: the directory in which we should look for the OS
|
2359 | 8e70b181 | Iustin Pop | @rtype: tuple
|
2360 | 8e70b181 | Iustin Pop | @return: tuple (status, data) with status denoting the validity and
|
2361 | 8e70b181 | Iustin Pop | data holding either the vaid versions or an error message
|
2362 | a8083063 | Iustin Pop |
|
2363 | a8083063 | Iustin Pop | """
|
2364 | e02b9114 | Iustin Pop | api_file = utils.PathJoin(os_dir, constants.OS_API_FILE) |
2365 | a8083063 | Iustin Pop | |
2366 | a8083063 | Iustin Pop | try:
|
2367 | a8083063 | Iustin Pop | st = os.stat(api_file) |
2368 | a8083063 | Iustin Pop | except EnvironmentError, err: |
2369 | b6b45e0d | Guido Trotter | return False, ("Required file '%s' not found under path %s: %s" % |
2370 | eb93b673 | Guido Trotter | (constants.OS_API_FILE, os_dir, utils.ErrnoOrStr(err))) |
2371 | a8083063 | Iustin Pop | |
2372 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
2373 | b6b45e0d | Guido Trotter | return False, ("File '%s' in %s is not a regular file" % |
2374 | b6b45e0d | Guido Trotter | (constants.OS_API_FILE, os_dir)) |
2375 | a8083063 | Iustin Pop | |
2376 | a8083063 | Iustin Pop | try:
|
2377 | 3374afa9 | Guido Trotter | api_versions = utils.ReadFile(api_file).splitlines() |
2378 | a8083063 | Iustin Pop | except EnvironmentError, err: |
2379 | 255dcebd | Iustin Pop | return False, ("Error while reading the API version file at %s: %s" % |
2380 | eb93b673 | Guido Trotter | (api_file, utils.ErrnoOrStr(err))) |
2381 | a8083063 | Iustin Pop | |
2382 | a8083063 | Iustin Pop | try:
|
2383 | 63b9b186 | Guido Trotter | api_versions = [int(version.strip()) for version in api_versions] |
2384 | a8083063 | Iustin Pop | except (TypeError, ValueError), err: |
2385 | 255dcebd | Iustin Pop | return False, ("API version(s) can't be converted to integer: %s" % |
2386 | 255dcebd | Iustin Pop | str(err))
|
2387 | a8083063 | Iustin Pop | |
2388 | 255dcebd | Iustin Pop | return True, api_versions |
2389 | a8083063 | Iustin Pop | |
2390 | 386b57af | Iustin Pop | |
2391 | 7c3d51d4 | Guido Trotter | def DiagnoseOS(top_dirs=None): |
2392 | a8083063 | Iustin Pop | """Compute the validity for all OSes.
|
2393 | a8083063 | Iustin Pop |
|
2394 | 10c2650b | Iustin Pop | @type top_dirs: list
|
2395 | 10c2650b | Iustin Pop | @param top_dirs: the list of directories in which to
|
2396 | 10c2650b | Iustin Pop | search (if not given defaults to
|
2397 | 3329f4de | Michael Hanselmann | L{pathutils.OS_SEARCH_PATH})
|
2398 | 10c2650b | Iustin Pop | @rtype: list of L{objects.OS}
|
2399 | bad78e66 | Iustin Pop | @return: a list of tuples (name, path, status, diagnose, variants,
|
2400 | bad78e66 | Iustin Pop | parameters, api_version) for all (potential) OSes under all
|
2401 | bad78e66 | Iustin Pop | search paths, where:
|
2402 | 255dcebd | Iustin Pop | - name is the (potential) OS name
|
2403 | 255dcebd | Iustin Pop | - path is the full path to the OS
|
2404 | 255dcebd | Iustin Pop | - status True/False is the validity of the OS
|
2405 | 255dcebd | Iustin Pop | - diagnose is the error message for an invalid OS, otherwise empty
|
2406 | ba00557a | Guido Trotter | - variants is a list of supported OS variants, if any
|
2407 | c7d04a6b | Iustin Pop | - parameters is a list of (name, help) parameters, if any
|
2408 | bad78e66 | Iustin Pop | - api_version is a list of support OS API versions
|
2409 | a8083063 | Iustin Pop |
|
2410 | a8083063 | Iustin Pop | """
|
2411 | 7c3d51d4 | Guido Trotter | if top_dirs is None: |
2412 | 710f30ec | Michael Hanselmann | top_dirs = pathutils.OS_SEARCH_PATH |
2413 | a8083063 | Iustin Pop | |
2414 | a8083063 | Iustin Pop | result = [] |
2415 | 65fe4693 | Iustin Pop | for dir_name in top_dirs: |
2416 | 65fe4693 | Iustin Pop | if os.path.isdir(dir_name):
|
2417 | 7c3d51d4 | Guido Trotter | try:
|
2418 | 65fe4693 | Iustin Pop | f_names = utils.ListVisibleFiles(dir_name) |
2419 | 7c3d51d4 | Guido Trotter | except EnvironmentError, err: |
2420 | 29921401 | Iustin Pop | logging.exception("Can't list the OS directory %s: %s", dir_name, err)
|
2421 | 7c3d51d4 | Guido Trotter | break
|
2422 | 7c3d51d4 | Guido Trotter | for name in f_names: |
2423 | e02b9114 | Iustin Pop | os_path = utils.PathJoin(dir_name, name) |
2424 | 255dcebd | Iustin Pop | status, os_inst = _TryOSFromDisk(name, base_dir=dir_name) |
2425 | 255dcebd | Iustin Pop | if status:
|
2426 | 255dcebd | Iustin Pop | diagnose = ""
|
2427 | ba00557a | Guido Trotter | variants = os_inst.supported_variants |
2428 | c7d04a6b | Iustin Pop | parameters = os_inst.supported_parameters |
2429 | bad78e66 | Iustin Pop | api_versions = os_inst.api_versions |
2430 | 255dcebd | Iustin Pop | else:
|
2431 | 255dcebd | Iustin Pop | diagnose = os_inst |
2432 | bad78e66 | Iustin Pop | variants = parameters = api_versions = [] |
2433 | bad78e66 | Iustin Pop | result.append((name, os_path, status, diagnose, variants, |
2434 | bad78e66 | Iustin Pop | parameters, api_versions)) |
2435 | a8083063 | Iustin Pop | |
2436 | c26a6bd2 | Iustin Pop | return result
|
2437 | a8083063 | Iustin Pop | |
2438 | a8083063 | Iustin Pop | |
2439 | 255dcebd | Iustin Pop | def _TryOSFromDisk(name, base_dir=None): |
2440 | a8083063 | Iustin Pop | """Create an OS instance from disk.
|
2441 | a8083063 | Iustin Pop |
|
2442 | a8083063 | Iustin Pop | This function will return an OS instance if the given name is a
|
2443 | 8e70b181 | Iustin Pop | valid OS name.
|
2444 | a8083063 | Iustin Pop |
|
2445 | 8ee4dc80 | Guido Trotter | @type base_dir: string
|
2446 | 8ee4dc80 | Guido Trotter | @keyword base_dir: Base directory containing OS installations.
|
2447 | 8ee4dc80 | Guido Trotter | Defaults to a search in all the OS_SEARCH_PATH dirs.
|
2448 | 255dcebd | Iustin Pop | @rtype: tuple
|
2449 | 255dcebd | Iustin Pop | @return: success and either the OS instance if we find a valid one,
|
2450 | 255dcebd | Iustin Pop | or error message
|
2451 | 7c3d51d4 | Guido Trotter |
|
2452 | a8083063 | Iustin Pop | """
|
2453 | 56bcd3f4 | Guido Trotter | if base_dir is None: |
2454 | 710f30ec | Michael Hanselmann | os_dir = utils.FindFile(name, pathutils.OS_SEARCH_PATH, os.path.isdir) |
2455 | c34c0cfd | Iustin Pop | else:
|
2456 | f95c81bf | Iustin Pop | os_dir = utils.FindFile(name, [base_dir], os.path.isdir) |
2457 | f95c81bf | Iustin Pop | |
2458 | f95c81bf | Iustin Pop | if os_dir is None: |
2459 | 5c0433d6 | Iustin Pop | return False, "Directory for OS %s not found in search path" % name |
2460 | a8083063 | Iustin Pop | |
2461 | c19f9810 | Iustin Pop | status, api_versions = _OSOndiskAPIVersion(os_dir) |
2462 | 255dcebd | Iustin Pop | if not status: |
2463 | 255dcebd | Iustin Pop | # push the error up
|
2464 | 255dcebd | Iustin Pop | return status, api_versions
|
2465 | a8083063 | Iustin Pop | |
2466 | d1a7d66f | Guido Trotter | if not constants.OS_API_VERSIONS.intersection(api_versions): |
2467 | 255dcebd | Iustin Pop | return False, ("API version mismatch for path '%s': found %s, want %s." % |
2468 | d1a7d66f | Guido Trotter | (os_dir, api_versions, constants.OS_API_VERSIONS)) |
2469 | a8083063 | Iustin Pop | |
2470 | 35007011 | Iustin Pop | # OS Files dictionary, we will populate it with the absolute path
|
2471 | 35007011 | Iustin Pop | # names; if the value is True, then it is a required file, otherwise
|
2472 | 35007011 | Iustin Pop | # an optional one
|
2473 | 35007011 | Iustin Pop | os_files = dict.fromkeys(constants.OS_SCRIPTS, True) |
2474 | a8083063 | Iustin Pop | |
2475 | 95075fba | Guido Trotter | if max(api_versions) >= constants.OS_API_V15: |
2476 | 35007011 | Iustin Pop | os_files[constants.OS_VARIANTS_FILE] = False
|
2477 | 95075fba | Guido Trotter | |
2478 | c7d04a6b | Iustin Pop | if max(api_versions) >= constants.OS_API_V20: |
2479 | 35007011 | Iustin Pop | os_files[constants.OS_PARAMETERS_FILE] = True
|
2480 | c7d04a6b | Iustin Pop | else:
|
2481 | c7d04a6b | Iustin Pop | del os_files[constants.OS_SCRIPT_VERIFY]
|
2482 | c7d04a6b | Iustin Pop | |
2483 | 35007011 | Iustin Pop | for (filename, required) in os_files.items(): |
2484 | e02b9114 | Iustin Pop | os_files[filename] = utils.PathJoin(os_dir, filename) |
2485 | a8083063 | Iustin Pop | |
2486 | a8083063 | Iustin Pop | try:
|
2487 | ea79fc15 | Michael Hanselmann | st = os.stat(os_files[filename]) |
2488 | a8083063 | Iustin Pop | except EnvironmentError, err: |
2489 | 35007011 | Iustin Pop | if err.errno == errno.ENOENT and not required: |
2490 | 35007011 | Iustin Pop | del os_files[filename]
|
2491 | 35007011 | Iustin Pop | continue
|
2492 | 41ba4061 | Guido Trotter | return False, ("File '%s' under path '%s' is missing (%s)" % |
2493 | eb93b673 | Guido Trotter | (filename, os_dir, utils.ErrnoOrStr(err))) |
2494 | a8083063 | Iustin Pop | |
2495 | a8083063 | Iustin Pop | if not stat.S_ISREG(stat.S_IFMT(st.st_mode)): |
2496 | 41ba4061 | Guido Trotter | return False, ("File '%s' under path '%s' is not a regular file" % |
2497 | ea79fc15 | Michael Hanselmann | (filename, os_dir)) |
2498 | 255dcebd | Iustin Pop | |
2499 | ea79fc15 | Michael Hanselmann | if filename in constants.OS_SCRIPTS: |
2500 | 0757c107 | Guido Trotter | if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
|
2501 | 0757c107 | Guido Trotter | return False, ("File '%s' under path '%s' is not executable" % |
2502 | ea79fc15 | Michael Hanselmann | (filename, os_dir)) |
2503 | 0757c107 | Guido Trotter | |
2504 | 845da3e8 | Iustin Pop | variants = [] |
2505 | 95075fba | Guido Trotter | if constants.OS_VARIANTS_FILE in os_files: |
2506 | 95075fba | Guido Trotter | variants_file = os_files[constants.OS_VARIANTS_FILE] |
2507 | 95075fba | Guido Trotter | try:
|
2508 | 5a7cb9d3 | Iustin Pop | variants = \ |
2509 | 5a7cb9d3 | Iustin Pop | utils.FilterEmptyLinesAndComments(utils.ReadFile(variants_file)) |
2510 | 95075fba | Guido Trotter | except EnvironmentError, err: |
2511 | 35007011 | Iustin Pop | # we accept missing files, but not other errors
|
2512 | 35007011 | Iustin Pop | if err.errno != errno.ENOENT:
|
2513 | 35007011 | Iustin Pop | return False, ("Error while reading the OS variants file at %s: %s" % |
2514 | eb93b673 | Guido Trotter | (variants_file, utils.ErrnoOrStr(err))) |
2515 | 0757c107 | Guido Trotter | |
2516 | c7d04a6b | Iustin Pop | parameters = [] |
2517 | c7d04a6b | Iustin Pop | if constants.OS_PARAMETERS_FILE in os_files: |
2518 | c7d04a6b | Iustin Pop | parameters_file = os_files[constants.OS_PARAMETERS_FILE] |
2519 | c7d04a6b | Iustin Pop | try:
|
2520 | c7d04a6b | Iustin Pop | parameters = utils.ReadFile(parameters_file).splitlines() |
2521 | c7d04a6b | Iustin Pop | except EnvironmentError, err: |
2522 | c7d04a6b | Iustin Pop | return False, ("Error while reading the OS parameters file at %s: %s" % |
2523 | eb93b673 | Guido Trotter | (parameters_file, utils.ErrnoOrStr(err))) |
2524 | c7d04a6b | Iustin Pop | parameters = [v.split(None, 1) for v in parameters] |
2525 | c7d04a6b | Iustin Pop | |
2526 | 8e70b181 | Iustin Pop | os_obj = objects.OS(name=name, path=os_dir, |
2527 | 41ba4061 | Guido Trotter | create_script=os_files[constants.OS_SCRIPT_CREATE], |
2528 | 41ba4061 | Guido Trotter | export_script=os_files[constants.OS_SCRIPT_EXPORT], |
2529 | 41ba4061 | Guido Trotter | import_script=os_files[constants.OS_SCRIPT_IMPORT], |
2530 | 41ba4061 | Guido Trotter | rename_script=os_files[constants.OS_SCRIPT_RENAME], |
2531 | 40684c3a | Iustin Pop | verify_script=os_files.get(constants.OS_SCRIPT_VERIFY, |
2532 | 40684c3a | Iustin Pop | None),
|
2533 | 95075fba | Guido Trotter | supported_variants=variants, |
2534 | c7d04a6b | Iustin Pop | supported_parameters=parameters, |
2535 | 255dcebd | Iustin Pop | api_versions=api_versions) |
2536 | 255dcebd | Iustin Pop | return True, os_obj |
2537 | 255dcebd | Iustin Pop | |
2538 | 255dcebd | Iustin Pop | |
2539 | 255dcebd | Iustin Pop | def OSFromDisk(name, base_dir=None): |
2540 | 255dcebd | Iustin Pop | """Create an OS instance from disk.
|
2541 | 255dcebd | Iustin Pop |
|
2542 | 255dcebd | Iustin Pop | This function will return an OS instance if the given name is a
|
2543 | 255dcebd | Iustin Pop | valid OS name. Otherwise, it will raise an appropriate
|
2544 | 255dcebd | Iustin Pop | L{RPCFail} exception, detailing why this is not a valid OS.
|
2545 | 255dcebd | Iustin Pop |
|
2546 | 255dcebd | Iustin Pop | This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
|
2547 | 255dcebd | Iustin Pop | an exception but returns true/false status data.
|
2548 | 255dcebd | Iustin Pop |
|
2549 | 255dcebd | Iustin Pop | @type base_dir: string
|
2550 | 255dcebd | Iustin Pop | @keyword base_dir: Base directory containing OS installations.
|
2551 | 255dcebd | Iustin Pop | Defaults to a search in all the OS_SEARCH_PATH dirs.
|
2552 | 255dcebd | Iustin Pop | @rtype: L{objects.OS}
|
2553 | 255dcebd | Iustin Pop | @return: the OS instance if we find a valid one
|
2554 | 255dcebd | Iustin Pop | @raise RPCFail: if we don't find a valid OS
|
2555 | 255dcebd | Iustin Pop |
|
2556 | 255dcebd | Iustin Pop | """
|
2557 | 870dc44c | Iustin Pop | name_only = objects.OS.GetName(name) |
2558 | 6ee7102a | Guido Trotter | status, payload = _TryOSFromDisk(name_only, base_dir) |
2559 | 255dcebd | Iustin Pop | |
2560 | 255dcebd | Iustin Pop | if not status: |
2561 | 255dcebd | Iustin Pop | _Fail(payload) |
2562 | a8083063 | Iustin Pop | |
2563 | 255dcebd | Iustin Pop | return payload
|
2564 | a8083063 | Iustin Pop | |
2565 | a8083063 | Iustin Pop | |
2566 | a025e535 | Vitaly Kuznetsov | def OSCoreEnv(os_name, inst_os, os_params, debug=0): |
2567 | efaa9b06 | Iustin Pop | """Calculate the basic environment for an os script.
|
2568 | 2266edb2 | Guido Trotter |
|
2569 | a025e535 | Vitaly Kuznetsov | @type os_name: str
|
2570 | a025e535 | Vitaly Kuznetsov | @param os_name: full operating system name (including variant)
|
2571 | 099c52ad | Iustin Pop | @type inst_os: L{objects.OS}
|
2572 | 099c52ad | Iustin Pop | @param inst_os: operating system for which the environment is being built
|
2573 | 1bdcbbab | Iustin Pop | @type os_params: dict
|
2574 | 1bdcbbab | Iustin Pop | @param os_params: the OS parameters
|
2575 | 2266edb2 | Guido Trotter | @type debug: integer
|
2576 | 10c2650b | Iustin Pop | @param debug: debug level (0 or 1, for OS Api 10)
|
2577 | 2266edb2 | Guido Trotter | @rtype: dict
|
2578 | 2266edb2 | Guido Trotter | @return: dict of environment variables
|
2579 | 10c2650b | Iustin Pop | @raise errors.BlockDeviceError: if the block device
|
2580 | 10c2650b | Iustin Pop | cannot be found
|
2581 | 2266edb2 | Guido Trotter |
|
2582 | 2266edb2 | Guido Trotter | """
|
2583 | 2266edb2 | Guido Trotter | result = {} |
2584 | 099c52ad | Iustin Pop | api_version = \ |
2585 | 099c52ad | Iustin Pop | max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
|
2586 | d0c8c01d | Iustin Pop | result["OS_API_VERSION"] = "%d" % api_version |
2587 | d0c8c01d | Iustin Pop | result["OS_NAME"] = inst_os.name
|
2588 | d0c8c01d | Iustin Pop | result["DEBUG_LEVEL"] = "%d" % debug |
2589 | efaa9b06 | Iustin Pop | |
2590 | efaa9b06 | Iustin Pop | # OS variants
|
2591 | 35007011 | Iustin Pop | if api_version >= constants.OS_API_V15 and inst_os.supported_variants: |
2592 | 870dc44c | Iustin Pop | variant = objects.OS.GetVariant(os_name) |
2593 | 870dc44c | Iustin Pop | if not variant: |
2594 | 099c52ad | Iustin Pop | variant = inst_os.supported_variants[0]
|
2595 | 35007011 | Iustin Pop | else:
|
2596 | 35007011 | Iustin Pop | variant = ""
|
2597 | 35007011 | Iustin Pop | result["OS_VARIANT"] = variant
|
2598 | efaa9b06 | Iustin Pop | |
2599 | 1bdcbbab | Iustin Pop | # OS params
|
2600 | 1bdcbbab | Iustin Pop | for pname, pvalue in os_params.items(): |
2601 | d0c8c01d | Iustin Pop | result["OSP_%s" % pname.upper()] = pvalue
|
2602 | 1bdcbbab | Iustin Pop | |
2603 | 9a6ade06 | Iustin Pop | # Set a default path otherwise programs called by OS scripts (or
|
2604 | 9a6ade06 | Iustin Pop | # even hooks called from OS scripts) might break, and we don't want
|
2605 | 9a6ade06 | Iustin Pop | # to have each script require setting a PATH variable
|
2606 | 9a6ade06 | Iustin Pop | result["PATH"] = constants.HOOKS_PATH
|
2607 | 9a6ade06 | Iustin Pop | |
2608 | efaa9b06 | Iustin Pop | return result
|
2609 | efaa9b06 | Iustin Pop | |
2610 | efaa9b06 | Iustin Pop | |
2611 | efaa9b06 | Iustin Pop | def OSEnvironment(instance, inst_os, debug=0): |
2612 | efaa9b06 | Iustin Pop | """Calculate the environment for an os script.
|
2613 | efaa9b06 | Iustin Pop |
|
2614 | efaa9b06 | Iustin Pop | @type instance: L{objects.Instance}
|
2615 | efaa9b06 | Iustin Pop | @param instance: target instance for the os script run
|
2616 | efaa9b06 | Iustin Pop | @type inst_os: L{objects.OS}
|
2617 | efaa9b06 | Iustin Pop | @param inst_os: operating system for which the environment is being built
|
2618 | efaa9b06 | Iustin Pop | @type debug: integer
|
2619 | efaa9b06 | Iustin Pop | @param debug: debug level (0 or 1, for OS Api 10)
|
2620 | efaa9b06 | Iustin Pop | @rtype: dict
|
2621 | efaa9b06 | Iustin Pop | @return: dict of environment variables
|
2622 | efaa9b06 | Iustin Pop | @raise errors.BlockDeviceError: if the block device
|
2623 | efaa9b06 | Iustin Pop | cannot be found
|
2624 | efaa9b06 | Iustin Pop |
|
2625 | efaa9b06 | Iustin Pop | """
|
2626 | a025e535 | Vitaly Kuznetsov | result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug) |
2627 | efaa9b06 | Iustin Pop | |
2628 | 519719fd | Marco Casavecchia | for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]: |
2629 | f2165b8a | Iustin Pop | result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr)) |
2630 | f2165b8a | Iustin Pop | |
2631 | d0c8c01d | Iustin Pop | result["HYPERVISOR"] = instance.hypervisor
|
2632 | d0c8c01d | Iustin Pop | result["DISK_COUNT"] = "%d" % len(instance.disks) |
2633 | d0c8c01d | Iustin Pop | result["NIC_COUNT"] = "%d" % len(instance.nics) |
2634 | d0c8c01d | Iustin Pop | result["INSTANCE_SECONDARY_NODES"] = \
|
2635 | d0c8c01d | Iustin Pop | ("%s" % " ".join(instance.secondary_nodes)) |
2636 | efaa9b06 | Iustin Pop | |
2637 | efaa9b06 | Iustin Pop | # Disks
|
2638 | 2266edb2 | Guido Trotter | for idx, disk in enumerate(instance.disks): |
2639 | f2e07bb4 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
2640 | d0c8c01d | Iustin Pop | result["DISK_%d_PATH" % idx] = real_disk.dev_path
|
2641 | d0c8c01d | Iustin Pop | result["DISK_%d_ACCESS" % idx] = disk.mode
|
2642 | 2266edb2 | Guido Trotter | if constants.HV_DISK_TYPE in instance.hvparams: |
2643 | d0c8c01d | Iustin Pop | result["DISK_%d_FRONTEND_TYPE" % idx] = \
|
2644 | 2266edb2 | Guido Trotter | instance.hvparams[constants.HV_DISK_TYPE] |
2645 | 2266edb2 | Guido Trotter | if disk.dev_type in constants.LDS_BLOCK: |
2646 | d0c8c01d | Iustin Pop | result["DISK_%d_BACKEND_TYPE" % idx] = "block" |
2647 | 2266edb2 | Guido Trotter | elif disk.dev_type == constants.LD_FILE:
|
2648 | d0c8c01d | Iustin Pop | result["DISK_%d_BACKEND_TYPE" % idx] = \
|
2649 | d0c8c01d | Iustin Pop | "file:%s" % disk.physical_id[0] |
2650 | efaa9b06 | Iustin Pop | |
2651 | efaa9b06 | Iustin Pop | # NICs
|
2652 | 2266edb2 | Guido Trotter | for idx, nic in enumerate(instance.nics): |
2653 | d0c8c01d | Iustin Pop | result["NIC_%d_MAC" % idx] = nic.mac
|
2654 | 2266edb2 | Guido Trotter | if nic.ip:
|
2655 | d0c8c01d | Iustin Pop | result["NIC_%d_IP" % idx] = nic.ip
|
2656 | d0c8c01d | Iustin Pop | result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
|
2657 | 1ba9227f | Guido Trotter | if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
|
2658 | d0c8c01d | Iustin Pop | result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
|
2659 | 1ba9227f | Guido Trotter | if nic.nicparams[constants.NIC_LINK]:
|
2660 | d0c8c01d | Iustin Pop | result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
|
2661 | d89168ff | Guido Trotter | if nic.netinfo:
|
2662 | d89168ff | Guido Trotter | nobj = objects.Network.FromDict(nic.netinfo) |
2663 | d89168ff | Guido Trotter | result.update(nobj.HooksDict("NIC_%d_" % idx))
|
2664 | 2266edb2 | Guido Trotter | if constants.HV_NIC_TYPE in instance.hvparams: |
2665 | d0c8c01d | Iustin Pop | result["NIC_%d_FRONTEND_TYPE" % idx] = \
|
2666 | 2266edb2 | Guido Trotter | instance.hvparams[constants.HV_NIC_TYPE] |
2667 | 2266edb2 | Guido Trotter | |
2668 | efaa9b06 | Iustin Pop | # HV/BE params
|
2669 | 67fc3042 | Iustin Pop | for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]: |
2670 | 67fc3042 | Iustin Pop | for key, value in source.items(): |
2671 | 030b218a | Iustin Pop | result["INSTANCE_%s_%s" % (kind, key)] = str(value) |
2672 | 67fc3042 | Iustin Pop | |
2673 | 2266edb2 | Guido Trotter | return result
|
2674 | a8083063 | Iustin Pop | |
2675 | f2e07bb4 | Michael Hanselmann | |
2676 | b954f097 | Constantinos Venetsanopoulos | def DiagnoseExtStorage(top_dirs=None): |
2677 | b954f097 | Constantinos Venetsanopoulos | """Compute the validity for all ExtStorage Providers.
|
2678 | b954f097 | Constantinos Venetsanopoulos |
|
2679 | b954f097 | Constantinos Venetsanopoulos | @type top_dirs: list
|
2680 | b954f097 | Constantinos Venetsanopoulos | @param top_dirs: the list of directories in which to
|
2681 | b954f097 | Constantinos Venetsanopoulos | search (if not given defaults to
|
2682 | b954f097 | Constantinos Venetsanopoulos | L{pathutils.ES_SEARCH_PATH})
|
2683 | b954f097 | Constantinos Venetsanopoulos | @rtype: list of L{objects.ExtStorage}
|
2684 | b954f097 | Constantinos Venetsanopoulos | @return: a list of tuples (name, path, status, diagnose, parameters)
|
2685 | b954f097 | Constantinos Venetsanopoulos | for all (potential) ExtStorage Providers under all
|
2686 | b954f097 | Constantinos Venetsanopoulos | search paths, where:
|
2687 | b954f097 | Constantinos Venetsanopoulos | - name is the (potential) ExtStorage Provider
|
2688 | b954f097 | Constantinos Venetsanopoulos | - path is the full path to the ExtStorage Provider
|
2689 | b954f097 | Constantinos Venetsanopoulos | - status True/False is the validity of the ExtStorage Provider
|
2690 | b954f097 | Constantinos Venetsanopoulos | - diagnose is the error message for an invalid ExtStorage Provider,
|
2691 | b954f097 | Constantinos Venetsanopoulos | otherwise empty
|
2692 | b954f097 | Constantinos Venetsanopoulos | - parameters is a list of (name, help) parameters, if any
|
2693 | b954f097 | Constantinos Venetsanopoulos |
|
2694 | b954f097 | Constantinos Venetsanopoulos | """
|
2695 | b954f097 | Constantinos Venetsanopoulos | if top_dirs is None: |
2696 | b954f097 | Constantinos Venetsanopoulos | top_dirs = pathutils.ES_SEARCH_PATH |
2697 | b954f097 | Constantinos Venetsanopoulos | |
2698 | b954f097 | Constantinos Venetsanopoulos | result = [] |
2699 | b954f097 | Constantinos Venetsanopoulos | for dir_name in top_dirs: |
2700 | b954f097 | Constantinos Venetsanopoulos | if os.path.isdir(dir_name):
|
2701 | b954f097 | Constantinos Venetsanopoulos | try:
|
2702 | b954f097 | Constantinos Venetsanopoulos | f_names = utils.ListVisibleFiles(dir_name) |
2703 | b954f097 | Constantinos Venetsanopoulos | except EnvironmentError, err: |
2704 | b954f097 | Constantinos Venetsanopoulos | logging.exception("Can't list the ExtStorage directory %s: %s",
|
2705 | b954f097 | Constantinos Venetsanopoulos | dir_name, err) |
2706 | b954f097 | Constantinos Venetsanopoulos | break
|
2707 | b954f097 | Constantinos Venetsanopoulos | for name in f_names: |
2708 | b954f097 | Constantinos Venetsanopoulos | es_path = utils.PathJoin(dir_name, name) |
2709 | b954f097 | Constantinos Venetsanopoulos | status, es_inst = bdev.ExtStorageFromDisk(name, base_dir=dir_name) |
2710 | b954f097 | Constantinos Venetsanopoulos | if status:
|
2711 | b954f097 | Constantinos Venetsanopoulos | diagnose = ""
|
2712 | b954f097 | Constantinos Venetsanopoulos | parameters = es_inst.supported_parameters |
2713 | b954f097 | Constantinos Venetsanopoulos | else:
|
2714 | b954f097 | Constantinos Venetsanopoulos | diagnose = es_inst |
2715 | b954f097 | Constantinos Venetsanopoulos | parameters = [] |
2716 | b954f097 | Constantinos Venetsanopoulos | result.append((name, es_path, status, diagnose, parameters)) |
2717 | b954f097 | Constantinos Venetsanopoulos | |
2718 | b954f097 | Constantinos Venetsanopoulos | return result
|
2719 | b954f097 | Constantinos Venetsanopoulos | |
2720 | b954f097 | Constantinos Venetsanopoulos | |
2721 | cad0723b | Iustin Pop | def BlockdevGrow(disk, amount, dryrun, backingstore): |
2722 | 594609c0 | Iustin Pop | """Grow a stack of block devices.
|
2723 | 594609c0 | Iustin Pop |
|
2724 | 594609c0 | Iustin Pop | This function is called recursively, with the childrens being the
|
2725 | 10c2650b | Iustin Pop | first ones to resize.
|
2726 | 594609c0 | Iustin Pop |
|
2727 | 10c2650b | Iustin Pop | @type disk: L{objects.Disk}
|
2728 | 10c2650b | Iustin Pop | @param disk: the disk to be grown
|
2729 | a59faf4b | Iustin Pop | @type amount: integer
|
2730 | a59faf4b | Iustin Pop | @param amount: the amount (in mebibytes) to grow with
|
2731 | a59faf4b | Iustin Pop | @type dryrun: boolean
|
2732 | a59faf4b | Iustin Pop | @param dryrun: whether to execute the operation in simulation mode
|
2733 | a59faf4b | Iustin Pop | only, without actually increasing the size
|
2734 | cad0723b | Iustin Pop | @param backingstore: whether to execute the operation on backing storage
|
2735 | cad0723b | Iustin Pop | only, or on "logical" storage only; e.g. DRBD is logical storage,
|
2736 | cad0723b | Iustin Pop | whereas LVM, file, RBD are backing storage
|
2737 | 10c2650b | Iustin Pop | @rtype: (status, result)
|
2738 | a59faf4b | Iustin Pop | @return: a tuple with the status of the operation (True/False), and
|
2739 | a59faf4b | Iustin Pop | the errors message if status is False
|
2740 | 594609c0 | Iustin Pop |
|
2741 | 594609c0 | Iustin Pop | """
|
2742 | 594609c0 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2743 | 594609c0 | Iustin Pop | if r_dev is None: |
2744 | afdc3985 | Iustin Pop | _Fail("Cannot find block device %s", disk)
|
2745 | 594609c0 | Iustin Pop | |
2746 | 594609c0 | Iustin Pop | try:
|
2747 | cad0723b | Iustin Pop | r_dev.Grow(amount, dryrun, backingstore) |
2748 | 594609c0 | Iustin Pop | except errors.BlockDeviceError, err:
|
2749 | 2cc6781a | Iustin Pop | _Fail("Failed to grow block device: %s", err, exc=True) |
2750 | 594609c0 | Iustin Pop | |
2751 | 594609c0 | Iustin Pop | |
2752 | 821d1bd1 | Iustin Pop | def BlockdevSnapshot(disk): |
2753 | a8083063 | Iustin Pop | """Create a snapshot copy of a block device.
|
2754 | a8083063 | Iustin Pop |
|
2755 | a8083063 | Iustin Pop | This function is called recursively, and the snapshot is actually created
|
2756 | a8083063 | Iustin Pop | just for the leaf lvm backend device.
|
2757 | a8083063 | Iustin Pop |
|
2758 | e9e9263d | Guido Trotter | @type disk: L{objects.Disk}
|
2759 | e9e9263d | Guido Trotter | @param disk: the disk to be snapshotted
|
2760 | e9e9263d | Guido Trotter | @rtype: string
|
2761 | 800ac399 | Iustin Pop | @return: snapshot disk ID as (vg, lv)
|
2762 | a8083063 | Iustin Pop |
|
2763 | 098c0958 | Michael Hanselmann | """
|
2764 | 433c63aa | Iustin Pop | if disk.dev_type == constants.LD_DRBD8:
|
2765 | 433c63aa | Iustin Pop | if not disk.children: |
2766 | 433c63aa | Iustin Pop | _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
|
2767 | 433c63aa | Iustin Pop | disk.unique_id) |
2768 | 433c63aa | Iustin Pop | return BlockdevSnapshot(disk.children[0]) |
2769 | fe96220b | Iustin Pop | elif disk.dev_type == constants.LD_LV:
|
2770 | a8083063 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2771 | a8083063 | Iustin Pop | if r_dev is not None: |
2772 | 433c63aa | Iustin Pop | # FIXME: choose a saner value for the snapshot size
|
2773 | a8083063 | Iustin Pop | # let's stay on the safe side and ask for the full size, for now
|
2774 | c26a6bd2 | Iustin Pop | return r_dev.Snapshot(disk.size)
|
2775 | a8083063 | Iustin Pop | else:
|
2776 | 87812fd3 | Iustin Pop | _Fail("Cannot find block device %s", disk)
|
2777 | a8083063 | Iustin Pop | else:
|
2778 | 87812fd3 | Iustin Pop | _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
|
2779 | 87812fd3 | Iustin Pop | disk.unique_id, disk.dev_type) |
2780 | a8083063 | Iustin Pop | |
2781 | a8083063 | Iustin Pop | |
2782 | 48e175a2 | Iustin Pop | def BlockdevSetInfo(disk, info): |
2783 | 48e175a2 | Iustin Pop | """Sets 'metadata' information on block devices.
|
2784 | 48e175a2 | Iustin Pop |
|
2785 | 48e175a2 | Iustin Pop | This function sets 'info' metadata on block devices. Initial
|
2786 | 48e175a2 | Iustin Pop | information is set at device creation; this function should be used
|
2787 | 48e175a2 | Iustin Pop | for example after renames.
|
2788 | 48e175a2 | Iustin Pop |
|
2789 | 48e175a2 | Iustin Pop | @type disk: L{objects.Disk}
|
2790 | 48e175a2 | Iustin Pop | @param disk: the disk to be grown
|
2791 | 48e175a2 | Iustin Pop | @type info: string
|
2792 | 48e175a2 | Iustin Pop | @param info: new 'info' metadata
|
2793 | 48e175a2 | Iustin Pop | @rtype: (status, result)
|
2794 | 48e175a2 | Iustin Pop | @return: a tuple with the status of the operation (True/False), and
|
2795 | 48e175a2 | Iustin Pop | the errors message if status is False
|
2796 | 48e175a2 | Iustin Pop |
|
2797 | 48e175a2 | Iustin Pop | """
|
2798 | 48e175a2 | Iustin Pop | r_dev = _RecursiveFindBD(disk) |
2799 | 48e175a2 | Iustin Pop | if r_dev is None: |
2800 | 48e175a2 | Iustin Pop | _Fail("Cannot find block device %s", disk)
|
2801 | 48e175a2 | Iustin Pop | |
2802 | 48e175a2 | Iustin Pop | try:
|
2803 | 48e175a2 | Iustin Pop | r_dev.SetInfo(info) |
2804 | 48e175a2 | Iustin Pop | except errors.BlockDeviceError, err:
|
2805 | 48e175a2 | Iustin Pop | _Fail("Failed to set information on block device: %s", err, exc=True) |
2806 | 48e175a2 | Iustin Pop | |
2807 | 48e175a2 | Iustin Pop | |
2808 | a8083063 | Iustin Pop | def FinalizeExport(instance, snap_disks): |
2809 | a8083063 | Iustin Pop | """Write out the export configuration information.
|
2810 | a8083063 | Iustin Pop |
|
2811 | 10c2650b | Iustin Pop | @type instance: L{objects.Instance}
|
2812 | 10c2650b | Iustin Pop | @param instance: the instance which we export, used for
|
2813 | 10c2650b | Iustin Pop | saving configuration
|
2814 | 10c2650b | Iustin Pop | @type snap_disks: list of L{objects.Disk}
|
2815 | 10c2650b | Iustin Pop | @param snap_disks: list of snapshot block devices, which
|
2816 | 10c2650b | Iustin Pop | will be used to get the actual name of the dump file
|
2817 | a8083063 | Iustin Pop |
|
2818 | c26a6bd2 | Iustin Pop | @rtype: None
|
2819 | a8083063 | Iustin Pop |
|
2820 | 098c0958 | Michael Hanselmann | """
|
2821 | 710f30ec | Michael Hanselmann | destdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name + ".new")
|
2822 | 710f30ec | Michael Hanselmann | finaldestdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name) |
2823 | a8083063 | Iustin Pop | |
2824 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
2825 | a8083063 | Iustin Pop | |
2826 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_EXP) |
2827 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_EXP, "version", "0") |
2828 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time())) |
2829 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_EXP, "source", instance.primary_node)
|
2830 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_EXP, "os", instance.os)
|
2831 | 775b8743 | Michael Hanselmann | config.set(constants.INISECT_EXP, "compression", "none") |
2832 | a8083063 | Iustin Pop | |
2833 | a8083063 | Iustin Pop | config.add_section(constants.INISECT_INS) |
2834 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "name", instance.name)
|
2835 | 1db993d5 | Guido Trotter | config.set(constants.INISECT_INS, "maxmem", "%d" % |
2836 | 1db993d5 | Guido Trotter | instance.beparams[constants.BE_MAXMEM]) |
2837 | 1db993d5 | Guido Trotter | config.set(constants.INISECT_INS, "minmem", "%d" % |
2838 | 1db993d5 | Guido Trotter | instance.beparams[constants.BE_MINMEM]) |
2839 | 1db993d5 | Guido Trotter | # "memory" is deprecated, but useful for exporting to old ganeti versions
|
2840 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "memory", "%d" % |
2841 | 1db993d5 | Guido Trotter | instance.beparams[constants.BE_MAXMEM]) |
2842 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "vcpus", "%d" % |
2843 | 51de46bf | Iustin Pop | instance.beparams[constants.BE_VCPUS]) |
2844 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
|
2845 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
|
2846 | fbb2c636 | Michael Hanselmann | config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags())) |
2847 | 66f93869 | Manuel Franceschini | |
2848 | 95268cc3 | Iustin Pop | nic_total = 0
|
2849 | a8083063 | Iustin Pop | for nic_count, nic in enumerate(instance.nics): |
2850 | 95268cc3 | Iustin Pop | nic_total += 1
|
2851 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "nic%d_mac" %
|
2852 | d0c8c01d | Iustin Pop | nic_count, "%s" % nic.mac)
|
2853 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip) |
2854 | 7a476bb5 | Dimitris Aragiorgis | config.set(constants.INISECT_INS, "nic%d_network" % nic_count,
|
2855 | 7a476bb5 | Dimitris Aragiorgis | "%s" % nic.network)
|
2856 | 6801eb5c | Iustin Pop | for param in constants.NICS_PARAMETER_TYPES: |
2857 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
|
2858 | d0c8c01d | Iustin Pop | "%s" % nic.nicparams.get(param, None)) |
2859 | a8083063 | Iustin Pop | # TODO: redundant: on load can read nics until it doesn't exist
|
2860 | e687ec01 | Michael Hanselmann | config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total) |
2861 | a8083063 | Iustin Pop | |
2862 | 726d7d68 | Iustin Pop | disk_total = 0
|
2863 | a8083063 | Iustin Pop | for disk_count, disk in enumerate(snap_disks): |
2864 | 19d7f90a | Guido Trotter | if disk:
|
2865 | 726d7d68 | Iustin Pop | disk_total += 1
|
2866 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
|
2867 | d0c8c01d | Iustin Pop | ("%s" % disk.iv_name))
|
2868 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
|
2869 | d0c8c01d | Iustin Pop | ("%s" % disk.physical_id[1])) |
2870 | d0c8c01d | Iustin Pop | config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
|
2871 | d0c8c01d | Iustin Pop | ("%d" % disk.size))
|
2872 | d0c8c01d | Iustin Pop | |
2873 | e687ec01 | Michael Hanselmann | config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total) |
2874 | a8083063 | Iustin Pop | |
2875 | 3c8954ad | Iustin Pop | # New-style hypervisor/backend parameters
|
2876 | 3c8954ad | Iustin Pop | |
2877 | 3c8954ad | Iustin Pop | config.add_section(constants.INISECT_HYP) |
2878 | 3c8954ad | Iustin Pop | for name, value in instance.hvparams.items(): |
2879 | 3c8954ad | Iustin Pop | if name not in constants.HVC_GLOBALS: |
2880 | 3c8954ad | Iustin Pop | config.set(constants.INISECT_HYP, name, str(value))
|
2881 | 3c8954ad | Iustin Pop | |
2882 | 3c8954ad | Iustin Pop | config.add_section(constants.INISECT_BEP) |
2883 | 3c8954ad | Iustin Pop | for name, value in instance.beparams.items(): |
2884 | 3c8954ad | Iustin Pop | config.set(constants.INISECT_BEP, name, str(value))
|
2885 | 3c8954ad | Iustin Pop | |
2886 | 535b49cb | Iustin Pop | config.add_section(constants.INISECT_OSP) |
2887 | 535b49cb | Iustin Pop | for name, value in instance.osparams.items(): |
2888 | 535b49cb | Iustin Pop | config.set(constants.INISECT_OSP, name, str(value))
|
2889 | 535b49cb | Iustin Pop | |
2890 | c4feafe8 | Iustin Pop | utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE), |
2891 | 726d7d68 | Iustin Pop | data=config.Dumps()) |
2892 | 56569f4e | Michael Hanselmann | shutil.rmtree(finaldestdir, ignore_errors=True)
|
2893 | a8083063 | Iustin Pop | shutil.move(destdir, finaldestdir) |
2894 | a8083063 | Iustin Pop | |
2895 | a8083063 | Iustin Pop | |
2896 | a8083063 | Iustin Pop | def ExportInfo(dest): |
2897 | a8083063 | Iustin Pop | """Get export configuration information.
|
2898 | a8083063 | Iustin Pop |
|
2899 | 10c2650b | Iustin Pop | @type dest: str
|
2900 | 10c2650b | Iustin Pop | @param dest: directory containing the export
|
2901 | a8083063 | Iustin Pop |
|
2902 | 10c2650b | Iustin Pop | @rtype: L{objects.SerializableConfigParser}
|
2903 | 10c2650b | Iustin Pop | @return: a serializable config file containing the
|
2904 | 10c2650b | Iustin Pop | export info
|
2905 | a8083063 | Iustin Pop |
|
2906 | a8083063 | Iustin Pop | """
|
2907 | c4feafe8 | Iustin Pop | cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE) |
2908 | a8083063 | Iustin Pop | |
2909 | a8083063 | Iustin Pop | config = objects.SerializableConfigParser() |
2910 | a8083063 | Iustin Pop | config.read(cff) |
2911 | a8083063 | Iustin Pop | |
2912 | a8083063 | Iustin Pop | if (not config.has_section(constants.INISECT_EXP) or |
2913 | a8083063 | Iustin Pop | not config.has_section(constants.INISECT_INS)):
|
2914 | 3eccac06 | Iustin Pop | _Fail("Export info file doesn't have the required fields")
|
2915 | a8083063 | Iustin Pop | |
2916 | c26a6bd2 | Iustin Pop | return config.Dumps()
|
2917 | a8083063 | Iustin Pop | |
2918 | a8083063 | Iustin Pop | |
2919 | a8083063 | Iustin Pop | def ListExports(): |
2920 | a8083063 | Iustin Pop | """Return a list of exports currently available on this machine.
|
2921 | 098c0958 | Michael Hanselmann |
|
2922 | 10c2650b | Iustin Pop | @rtype: list
|
2923 | 10c2650b | Iustin Pop | @return: list of the exports
|
2924 | 10c2650b | Iustin Pop |
|
2925 | a8083063 | Iustin Pop | """
|
2926 | 710f30ec | Michael Hanselmann | if os.path.isdir(pathutils.EXPORT_DIR):
|
2927 | 710f30ec | Michael Hanselmann | return sorted(utils.ListVisibleFiles(pathutils.EXPORT_DIR)) |
2928 | a8083063 | Iustin Pop | else:
|
2929 | afdc3985 | Iustin Pop | _Fail("No exports directory")
|
2930 | a8083063 | Iustin Pop | |
2931 | a8083063 | Iustin Pop | |
2932 | a8083063 | Iustin Pop | def RemoveExport(export): |
2933 | a8083063 | Iustin Pop | """Remove an existing export from the node.
|
2934 | a8083063 | Iustin Pop |
|
2935 | 10c2650b | Iustin Pop | @type export: str
|
2936 | 10c2650b | Iustin Pop | @param export: the name of the export to remove
|
2937 | c26a6bd2 | Iustin Pop | @rtype: None
|
2938 | a8083063 | Iustin Pop |
|
2939 | 098c0958 | Michael Hanselmann | """
|
2940 | 710f30ec | Michael Hanselmann | target = utils.PathJoin(pathutils.EXPORT_DIR, export) |
2941 | a8083063 | Iustin Pop | |
2942 | 35fbcd11 | Iustin Pop | try:
|
2943 | 35fbcd11 | Iustin Pop | shutil.rmtree(target) |
2944 | 35fbcd11 | Iustin Pop | except EnvironmentError, err: |
2945 | 35fbcd11 | Iustin Pop | _Fail("Error while removing the export: %s", err, exc=True) |
2946 | a8083063 | Iustin Pop | |
2947 | a8083063 | Iustin Pop | |
2948 | 821d1bd1 | Iustin Pop | def BlockdevRename(devlist): |
2949 | f3e513ad | Iustin Pop | """Rename a list of block devices.
|
2950 | f3e513ad | Iustin Pop |
|
2951 | 10c2650b | Iustin Pop | @type devlist: list of tuples
|
2952 | 10c2650b | Iustin Pop | @param devlist: list of tuples of the form (disk,
|
2953 | 10c2650b | Iustin Pop | new_logical_id, new_physical_id); disk is an
|
2954 | 10c2650b | Iustin Pop | L{objects.Disk} object describing the current disk,
|
2955 | 10c2650b | Iustin Pop | and new logical_id/physical_id is the name we
|
2956 | 10c2650b | Iustin Pop | rename it to
|
2957 | 10c2650b | Iustin Pop | @rtype: boolean
|
2958 | 10c2650b | Iustin Pop | @return: True if all renames succeeded, False otherwise
|
2959 | f3e513ad | Iustin Pop |
|
2960 | f3e513ad | Iustin Pop | """
|
2961 | 6b5e3f70 | Iustin Pop | msgs = [] |
2962 | f3e513ad | Iustin Pop | result = True
|
2963 | f3e513ad | Iustin Pop | for disk, unique_id in devlist: |
2964 | f3e513ad | Iustin Pop | dev = _RecursiveFindBD(disk) |
2965 | f3e513ad | Iustin Pop | if dev is None: |
2966 | 6b5e3f70 | Iustin Pop | msgs.append("Can't find device %s in rename" % str(disk)) |
2967 | f3e513ad | Iustin Pop | result = False
|
2968 | f3e513ad | Iustin Pop | continue
|
2969 | f3e513ad | Iustin Pop | try:
|
2970 | 3f78eef2 | Iustin Pop | old_rpath = dev.dev_path |
2971 | f3e513ad | Iustin Pop | dev.Rename(unique_id) |
2972 | 3f78eef2 | Iustin Pop | new_rpath = dev.dev_path |
2973 | 3f78eef2 | Iustin Pop | if old_rpath != new_rpath:
|
2974 | 3f78eef2 | Iustin Pop | DevCacheManager.RemoveCache(old_rpath) |
2975 | 3f78eef2 | Iustin Pop | # FIXME: we should add the new cache information here, like:
|
2976 | 3f78eef2 | Iustin Pop | # DevCacheManager.UpdateCache(new_rpath, owner, ...)
|
2977 | 3f78eef2 | Iustin Pop | # but we don't have the owner here - maybe parse from existing
|
2978 | 3f78eef2 | Iustin Pop | # cache? for now, we only lose lvm data when we rename, which
|
2979 | 3f78eef2 | Iustin Pop | # is less critical than DRBD or MD
|
2980 | f3e513ad | Iustin Pop | except errors.BlockDeviceError, err:
|
2981 | 6b5e3f70 | Iustin Pop | msgs.append("Can't rename device '%s' to '%s': %s" %
|
2982 | 6b5e3f70 | Iustin Pop | (dev, unique_id, err)) |
2983 | 18682bca | Iustin Pop | logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
|
2984 | f3e513ad | Iustin Pop | result = False
|
2985 | afdc3985 | Iustin Pop | if not result: |
2986 | afdc3985 | Iustin Pop | _Fail("; ".join(msgs))
|
2987 | f3e513ad | Iustin Pop | |
2988 | f3e513ad | Iustin Pop | |
2989 | 4b97f902 | Apollon Oikonomopoulos | def _TransformFileStorageDir(fs_dir): |
2990 | 778b75bb | Manuel Franceschini | """Checks whether given file_storage_dir is valid.
|
2991 | 778b75bb | Manuel Franceschini |
|
2992 | 4b97f902 | Apollon Oikonomopoulos | Checks wheter the given fs_dir is within the cluster-wide default
|
2993 | 4b97f902 | Apollon Oikonomopoulos | file_storage_dir or the shared_file_storage_dir, which are stored in
|
2994 | 4b97f902 | Apollon Oikonomopoulos | SimpleStore. Only paths under those directories are allowed.
|
2995 | 778b75bb | Manuel Franceschini |
|
2996 | 4b97f902 | Apollon Oikonomopoulos | @type fs_dir: str
|
2997 | 4b97f902 | Apollon Oikonomopoulos | @param fs_dir: the path to check
|
2998 | d61cbe76 | Iustin Pop |
|
2999 | b1206984 | Iustin Pop | @return: the normalized path if valid, None otherwise
|
3000 | 778b75bb | Manuel Franceschini |
|
3001 | 778b75bb | Manuel Franceschini | """
|
3002 | 63a3d8f7 | Michael Hanselmann | if not (constants.ENABLE_FILE_STORAGE or |
3003 | 63a3d8f7 | Michael Hanselmann | constants.ENABLE_SHARED_FILE_STORAGE): |
3004 | cb7c0198 | Iustin Pop | _Fail("File storage disabled at configure time")
|
3005 | 5e09a309 | Michael Hanselmann | |
3006 | 5e09a309 | Michael Hanselmann | bdev.CheckFileStoragePath(fs_dir) |
3007 | 5e09a309 | Michael Hanselmann | |
3008 | 5e09a309 | Michael Hanselmann | return os.path.normpath(fs_dir)
|
3009 | 778b75bb | Manuel Franceschini | |
3010 | 778b75bb | Manuel Franceschini | |
3011 | 778b75bb | Manuel Franceschini | def CreateFileStorageDir(file_storage_dir): |
3012 | 778b75bb | Manuel Franceschini | """Create file storage directory.
|
3013 | 778b75bb | Manuel Franceschini |
|
3014 | b1206984 | Iustin Pop | @type file_storage_dir: str
|
3015 | b1206984 | Iustin Pop | @param file_storage_dir: directory to create
|
3016 | 778b75bb | Manuel Franceschini |
|
3017 | b1206984 | Iustin Pop | @rtype: tuple
|
3018 | b1206984 | Iustin Pop | @return: tuple with first element a boolean indicating wheter dir
|
3019 | b1206984 | Iustin Pop | creation was successful or not
|
3020 | 778b75bb | Manuel Franceschini |
|
3021 | 778b75bb | Manuel Franceschini | """
|
3022 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
3023 | b2b8bcce | Iustin Pop | if os.path.exists(file_storage_dir):
|
3024 | b2b8bcce | Iustin Pop | if not os.path.isdir(file_storage_dir): |
3025 | b2b8bcce | Iustin Pop | _Fail("Specified storage dir '%s' is not a directory",
|
3026 | b2b8bcce | Iustin Pop | file_storage_dir) |
3027 | 778b75bb | Manuel Franceschini | else:
|
3028 | b2b8bcce | Iustin Pop | try:
|
3029 | b2b8bcce | Iustin Pop | os.makedirs(file_storage_dir, 0750)
|
3030 | b2b8bcce | Iustin Pop | except OSError, err: |
3031 | b2b8bcce | Iustin Pop | _Fail("Cannot create file storage directory '%s': %s",
|
3032 | b2b8bcce | Iustin Pop | file_storage_dir, err, exc=True)
|
3033 | 778b75bb | Manuel Franceschini | |
3034 | 778b75bb | Manuel Franceschini | |
3035 | 778b75bb | Manuel Franceschini | def RemoveFileStorageDir(file_storage_dir): |
3036 | 778b75bb | Manuel Franceschini | """Remove file storage directory.
|
3037 | 778b75bb | Manuel Franceschini |
|
3038 | 778b75bb | Manuel Franceschini | Remove it only if it's empty. If not log an error and return.
|
3039 | 778b75bb | Manuel Franceschini |
|
3040 | 10c2650b | Iustin Pop | @type file_storage_dir: str
|
3041 | 10c2650b | Iustin Pop | @param file_storage_dir: the directory we should cleanup
|
3042 | 10c2650b | Iustin Pop | @rtype: tuple (success,)
|
3043 | 10c2650b | Iustin Pop | @return: tuple of one element, C{success}, denoting
|
3044 | 5bbd3f7f | Michael Hanselmann | whether the operation was successful
|
3045 | 778b75bb | Manuel Franceschini |
|
3046 | 778b75bb | Manuel Franceschini | """
|
3047 | 778b75bb | Manuel Franceschini | file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
3048 | b2b8bcce | Iustin Pop | if os.path.exists(file_storage_dir):
|
3049 | b2b8bcce | Iustin Pop | if not os.path.isdir(file_storage_dir): |
3050 | b2b8bcce | Iustin Pop | _Fail("Specified Storage directory '%s' is not a directory",
|
3051 | b2b8bcce | Iustin Pop | file_storage_dir) |
3052 | afdc3985 | Iustin Pop | # deletes dir only if empty, otherwise we want to fail the rpc call
|
3053 | b2b8bcce | Iustin Pop | try:
|
3054 | b2b8bcce | Iustin Pop | os.rmdir(file_storage_dir) |
3055 | b2b8bcce | Iustin Pop | except OSError, err: |
3056 | b2b8bcce | Iustin Pop | _Fail("Cannot remove file storage directory '%s': %s",
|
3057 | b2b8bcce | Iustin Pop | file_storage_dir, err) |
3058 | b2b8bcce | Iustin Pop | |
3059 | 778b75bb | Manuel Franceschini | |
3060 | 778b75bb | Manuel Franceschini | def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir): |
3061 | 778b75bb | Manuel Franceschini | """Rename the file storage directory.
|
3062 | 778b75bb | Manuel Franceschini |
|
3063 | 10c2650b | Iustin Pop | @type old_file_storage_dir: str
|
3064 | 10c2650b | Iustin Pop | @param old_file_storage_dir: the current path
|
3065 | 10c2650b | Iustin Pop | @type new_file_storage_dir: str
|
3066 | 10c2650b | Iustin Pop | @param new_file_storage_dir: the name we should rename to
|
3067 | 10c2650b | Iustin Pop | @rtype: tuple (success,)
|
3068 | 10c2650b | Iustin Pop | @return: tuple of one element, C{success}, denoting
|
3069 | 10c2650b | Iustin Pop | whether the operation was successful
|
3070 | 778b75bb | Manuel Franceschini |
|
3071 | 778b75bb | Manuel Franceschini | """
|
3072 | 778b75bb | Manuel Franceschini | old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir) |
3073 | 778b75bb | Manuel Franceschini | new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir) |
3074 | b2b8bcce | Iustin Pop | if not os.path.exists(new_file_storage_dir): |
3075 | b2b8bcce | Iustin Pop | if os.path.isdir(old_file_storage_dir):
|
3076 | b2b8bcce | Iustin Pop | try:
|
3077 | b2b8bcce | Iustin Pop | os.rename(old_file_storage_dir, new_file_storage_dir) |
3078 | b2b8bcce | Iustin Pop | except OSError, err: |
3079 | b2b8bcce | Iustin Pop | _Fail("Cannot rename '%s' to '%s': %s",
|
3080 | b2b8bcce | Iustin Pop | old_file_storage_dir, new_file_storage_dir, err) |
3081 | 778b75bb | Manuel Franceschini | else:
|
3082 | b2b8bcce | Iustin Pop | _Fail("Specified storage dir '%s' is not a directory",
|
3083 | b2b8bcce | Iustin Pop | old_file_storage_dir) |
3084 | b2b8bcce | Iustin Pop | else:
|
3085 | b2b8bcce | Iustin Pop | if os.path.exists(old_file_storage_dir):
|
3086 | b2b8bcce | Iustin Pop | _Fail("Cannot rename '%s' to '%s': both locations exist",
|
3087 | b2b8bcce | Iustin Pop | old_file_storage_dir, new_file_storage_dir) |
3088 | 778b75bb | Manuel Franceschini | |
3089 | 778b75bb | Manuel Franceschini | |
3090 | c8457ce7 | Iustin Pop | def _EnsureJobQueueFile(file_name): |
3091 | dc31eae3 | Michael Hanselmann | """Checks whether the given filename is in the queue directory.
|
3092 | ca52cdeb | Michael Hanselmann |
|
3093 | 10c2650b | Iustin Pop | @type file_name: str
|
3094 | 10c2650b | Iustin Pop | @param file_name: the file name we should check
|
3095 | c8457ce7 | Iustin Pop | @rtype: None
|
3096 | c8457ce7 | Iustin Pop | @raises RPCFail: if the file is not valid
|
3097 | 10c2650b | Iustin Pop |
|
3098 | ca52cdeb | Michael Hanselmann | """
|
3099 | b3589802 | Michael Hanselmann | if not utils.IsBelowDir(pathutils.QUEUE_DIR, file_name): |
3100 | c8457ce7 | Iustin Pop | _Fail("Passed job queue file '%s' does not belong to"
|
3101 | b3589802 | Michael Hanselmann | " the queue directory '%s'", file_name, pathutils.QUEUE_DIR)
|
3102 | dc31eae3 | Michael Hanselmann | |
3103 | dc31eae3 | Michael Hanselmann | |
3104 | dc31eae3 | Michael Hanselmann | def JobQueueUpdate(file_name, content): |
3105 | dc31eae3 | Michael Hanselmann | """Updates a file in the queue directory.
|
3106 | dc31eae3 | Michael Hanselmann |
|
3107 | 3865ca48 | Michael Hanselmann | This is just a wrapper over L{utils.io.WriteFile}, with proper
|
3108 | 10c2650b | Iustin Pop | checking.
|
3109 | 10c2650b | Iustin Pop |
|
3110 | 10c2650b | Iustin Pop | @type file_name: str
|
3111 | 10c2650b | Iustin Pop | @param file_name: the job file name
|
3112 | 10c2650b | Iustin Pop | @type content: str
|
3113 | 10c2650b | Iustin Pop | @param content: the new job contents
|
3114 | 10c2650b | Iustin Pop | @rtype: boolean
|
3115 | 10c2650b | Iustin Pop | @return: the success of the operation
|
3116 | 10c2650b | Iustin Pop |
|
3117 | dc31eae3 | Michael Hanselmann | """
|
3118 | cffbbae7 | Michael Hanselmann | file_name = vcluster.LocalizeVirtualPath(file_name) |
3119 | cffbbae7 | Michael Hanselmann | |
3120 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(file_name) |
3121 | 82b22e19 | René Nussbaumer | getents = runtime.GetEnts() |
3122 | ca52cdeb | Michael Hanselmann | |
3123 | ca52cdeb | Michael Hanselmann | # Write and replace the file atomically
|
3124 | 82b22e19 | René Nussbaumer | utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid, |
3125 | fe05a931 | Michele Tartara | gid=getents.daemons_gid, mode=constants.JOB_QUEUE_FILES_PERMS) |
3126 | ca52cdeb | Michael Hanselmann | |
3127 | ca52cdeb | Michael Hanselmann | |
3128 | af5ebcb1 | Michael Hanselmann | def JobQueueRename(old, new): |
3129 | af5ebcb1 | Michael Hanselmann | """Renames a job queue file.
|
3130 | af5ebcb1 | Michael Hanselmann |
|
3131 | c41eea6e | Iustin Pop | This is just a wrapper over os.rename with proper checking.
|
3132 | 10c2650b | Iustin Pop |
|
3133 | 10c2650b | Iustin Pop | @type old: str
|
3134 | 10c2650b | Iustin Pop | @param old: the old (actual) file name
|
3135 | 10c2650b | Iustin Pop | @type new: str
|
3136 | 10c2650b | Iustin Pop | @param new: the desired file name
|
3137 | c8457ce7 | Iustin Pop | @rtype: tuple
|
3138 | c8457ce7 | Iustin Pop | @return: the success of the operation and payload
|
3139 | 10c2650b | Iustin Pop |
|
3140 | af5ebcb1 | Michael Hanselmann | """
|
3141 | cffbbae7 | Michael Hanselmann | old = vcluster.LocalizeVirtualPath(old) |
3142 | cffbbae7 | Michael Hanselmann | new = vcluster.LocalizeVirtualPath(new) |
3143 | cffbbae7 | Michael Hanselmann | |
3144 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(old) |
3145 | c8457ce7 | Iustin Pop | _EnsureJobQueueFile(new) |
3146 | af5ebcb1 | Michael Hanselmann | |
3147 | 8e5a705d | René Nussbaumer | getents = runtime.GetEnts() |
3148 | 8e5a705d | René Nussbaumer | |
3149 | fe05a931 | Michele Tartara | utils.RenameFile(old, new, mkdir=True, mkdir_mode=0750, |
3150 | fe05a931 | Michele Tartara | dir_uid=getents.masterd_uid, dir_gid=getents.daemons_gid) |
3151 | af5ebcb1 | Michael Hanselmann | |
3152 | af5ebcb1 | Michael Hanselmann | |
3153 | 821d1bd1 | Iustin Pop | def BlockdevClose(instance_name, disks): |
3154 | d61cbe76 | Iustin Pop | """Closes the given block devices.
|
3155 | d61cbe76 | Iustin Pop |
|
3156 | 10c2650b | Iustin Pop | This means they will be switched to secondary mode (in case of
|
3157 | 10c2650b | Iustin Pop | DRBD).
|
3158 | 10c2650b | Iustin Pop |
|
3159 | b2e7666a | Iustin Pop | @param instance_name: if the argument is not empty, the symlinks
|
3160 | b2e7666a | Iustin Pop | of this instance will be removed
|
3161 | 10c2650b | Iustin Pop | @type disks: list of L{objects.Disk}
|
3162 | 10c2650b | Iustin Pop | @param disks: the list of disks to be closed
|
3163 | 10c2650b | Iustin Pop | @rtype: tuple (success, message)
|
3164 | 10c2650b | Iustin Pop | @return: a tuple of success and message, where success
|
3165 | 10c2650b | Iustin Pop | indicates the succes of the operation, and message
|
3166 | 10c2650b | Iustin Pop | which will contain the error details in case we
|
3167 | 10c2650b | Iustin Pop | failed
|
3168 | d61cbe76 | Iustin Pop |
|
3169 | d61cbe76 | Iustin Pop | """
|
3170 | d61cbe76 | Iustin Pop | bdevs = [] |
3171 | d61cbe76 | Iustin Pop | for cf in disks: |
3172 | d61cbe76 | Iustin Pop | rd = _RecursiveFindBD(cf) |
3173 | d61cbe76 | Iustin Pop | if rd is None: |
3174 | 2cc6781a | Iustin Pop | _Fail("Can't find device %s", cf)
|
3175 | d61cbe76 | Iustin Pop | bdevs.append(rd) |
3176 | d61cbe76 | Iustin Pop | |
3177 | d61cbe76 | Iustin Pop | msg = [] |
3178 | d61cbe76 | Iustin Pop | for rd in bdevs: |
3179 | d61cbe76 | Iustin Pop | try:
|
3180 | d61cbe76 | Iustin Pop | rd.Close() |
3181 | d61cbe76 | Iustin Pop | except errors.BlockDeviceError, err:
|
3182 | d61cbe76 | Iustin Pop | msg.append(str(err))
|
3183 | d61cbe76 | Iustin Pop | if msg:
|
3184 | afdc3985 | Iustin Pop | _Fail("Can't make devices secondary: %s", ",".join(msg)) |
3185 | d61cbe76 | Iustin Pop | else:
|
3186 | b2e7666a | Iustin Pop | if instance_name:
|
3187 | 5282084b | Iustin Pop | _RemoveBlockDevLinks(instance_name, disks) |
3188 | d61cbe76 | Iustin Pop | |
3189 | d61cbe76 | Iustin Pop | |
3190 | 6217e295 | Iustin Pop | def ValidateHVParams(hvname, hvparams): |
3191 | 6217e295 | Iustin Pop | """Validates the given hypervisor parameters.
|
3192 | 6217e295 | Iustin Pop |
|
3193 | 6217e295 | Iustin Pop | @type hvname: string
|
3194 | 6217e295 | Iustin Pop | @param hvname: the hypervisor name
|
3195 | 6217e295 | Iustin Pop | @type hvparams: dict
|
3196 | 6217e295 | Iustin Pop | @param hvparams: the hypervisor parameters to be validated
|
3197 | c26a6bd2 | Iustin Pop | @rtype: None
|
3198 | 6217e295 | Iustin Pop |
|
3199 | 6217e295 | Iustin Pop | """
|
3200 | 6217e295 | Iustin Pop | try:
|
3201 | 6217e295 | Iustin Pop | hv_type = hypervisor.GetHypervisor(hvname) |
3202 | 6217e295 | Iustin Pop | hv_type.ValidateParameters(hvparams) |
3203 | 6217e295 | Iustin Pop | except errors.HypervisorError, err:
|
3204 | afdc3985 | Iustin Pop | _Fail(str(err), log=False) |
3205 | 6217e295 | Iustin Pop | |
3206 | 6217e295 | Iustin Pop | |
3207 | acd9ff9e | Iustin Pop | def _CheckOSPList(os_obj, parameters): |
3208 | acd9ff9e | Iustin Pop | """Check whether a list of parameters is supported by the OS.
|
3209 | acd9ff9e | Iustin Pop |
|
3210 | acd9ff9e | Iustin Pop | @type os_obj: L{objects.OS}
|
3211 | acd9ff9e | Iustin Pop | @param os_obj: OS object to check
|
3212 | acd9ff9e | Iustin Pop | @type parameters: list
|
3213 | acd9ff9e | Iustin Pop | @param parameters: the list of parameters to check
|
3214 | acd9ff9e | Iustin Pop |
|
3215 | acd9ff9e | Iustin Pop | """
|
3216 | acd9ff9e | Iustin Pop | supported = [v[0] for v in os_obj.supported_parameters] |
3217 | acd9ff9e | Iustin Pop | delta = frozenset(parameters).difference(supported)
|
3218 | acd9ff9e | Iustin Pop | if delta:
|
3219 | acd9ff9e | Iustin Pop | _Fail("The following parameters are not supported"
|
3220 | acd9ff9e | Iustin Pop | " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
|
3221 | acd9ff9e | Iustin Pop | |
3222 | acd9ff9e | Iustin Pop | |
3223 | acd9ff9e | Iustin Pop | def ValidateOS(required, osname, checks, osparams): |
3224 | acd9ff9e | Iustin Pop | """Validate the given OS' parameters.
|
3225 | acd9ff9e | Iustin Pop |
|
3226 | acd9ff9e | Iustin Pop | @type required: boolean
|
3227 | acd9ff9e | Iustin Pop | @param required: whether absence of the OS should translate into
|
3228 | acd9ff9e | Iustin Pop | failure or not
|
3229 | acd9ff9e | Iustin Pop | @type osname: string
|
3230 | acd9ff9e | Iustin Pop | @param osname: the OS to be validated
|
3231 | acd9ff9e | Iustin Pop | @type checks: list
|
3232 | acd9ff9e | Iustin Pop | @param checks: list of the checks to run (currently only 'parameters')
|
3233 | acd9ff9e | Iustin Pop | @type osparams: dict
|
3234 | acd9ff9e | Iustin Pop | @param osparams: dictionary with OS parameters
|
3235 | acd9ff9e | Iustin Pop | @rtype: boolean
|
3236 | acd9ff9e | Iustin Pop | @return: True if the validation passed, or False if the OS was not
|
3237 | acd9ff9e | Iustin Pop | found and L{required} was false
|
3238 | acd9ff9e | Iustin Pop |
|
3239 | acd9ff9e | Iustin Pop | """
|
3240 | acd9ff9e | Iustin Pop | if not constants.OS_VALIDATE_CALLS.issuperset(checks): |
3241 | acd9ff9e | Iustin Pop | _Fail("Unknown checks required for OS %s: %s", osname,
|
3242 | acd9ff9e | Iustin Pop | set(checks).difference(constants.OS_VALIDATE_CALLS))
|
3243 | acd9ff9e | Iustin Pop | |
3244 | 870dc44c | Iustin Pop | name_only = objects.OS.GetName(osname) |
3245 | acd9ff9e | Iustin Pop | status, tbv = _TryOSFromDisk(name_only, None)
|
3246 | acd9ff9e | Iustin Pop | |
3247 | acd9ff9e | Iustin Pop | if not status: |
3248 | acd9ff9e | Iustin Pop | if required:
|
3249 | acd9ff9e | Iustin Pop | _Fail(tbv) |
3250 | acd9ff9e | Iustin Pop | else:
|
3251 | acd9ff9e | Iustin Pop | return False |
3252 | acd9ff9e | Iustin Pop | |
3253 | 72db3fd7 | Iustin Pop | if max(tbv.api_versions) < constants.OS_API_V20: |
3254 | 72db3fd7 | Iustin Pop | return True |
3255 | 72db3fd7 | Iustin Pop | |
3256 | acd9ff9e | Iustin Pop | if constants.OS_VALIDATE_PARAMETERS in checks: |
3257 | acd9ff9e | Iustin Pop | _CheckOSPList(tbv, osparams.keys()) |
3258 | acd9ff9e | Iustin Pop | |
3259 | a025e535 | Vitaly Kuznetsov | validate_env = OSCoreEnv(osname, tbv, osparams) |
3260 | acd9ff9e | Iustin Pop | result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env, |
3261 | 896a03f6 | Iustin Pop | cwd=tbv.path, reset_env=True)
|
3262 | acd9ff9e | Iustin Pop | if result.failed:
|
3263 | acd9ff9e | Iustin Pop | logging.error("os validate command '%s' returned error: %s output: %s",
|
3264 | acd9ff9e | Iustin Pop | result.cmd, result.fail_reason, result.output) |
3265 | acd9ff9e | Iustin Pop | _Fail("OS validation script failed (%s), output: %s",
|
3266 | acd9ff9e | Iustin Pop | result.fail_reason, result.output, log=False)
|
3267 | acd9ff9e | Iustin Pop | |
3268 | acd9ff9e | Iustin Pop | return True |
3269 | acd9ff9e | Iustin Pop | |
3270 | acd9ff9e | Iustin Pop | |
3271 | 56aa9fd5 | Iustin Pop | def DemoteFromMC(): |
3272 | 56aa9fd5 | Iustin Pop | """Demotes the current node from master candidate role.
|
3273 | 56aa9fd5 | Iustin Pop |
|
3274 | 56aa9fd5 | Iustin Pop | """
|
3275 | 56aa9fd5 | Iustin Pop | # try to ensure we're not the master by mistake
|
3276 | 56aa9fd5 | Iustin Pop | master, myself = ssconf.GetMasterAndMyself() |
3277 | 56aa9fd5 | Iustin Pop | if master == myself:
|
3278 | afdc3985 | Iustin Pop | _Fail("ssconf status shows I'm the master node, will not demote")
|
3279 | f154a7a3 | Michael Hanselmann | |
3280 | 710f30ec | Michael Hanselmann | result = utils.RunCmd([pathutils.DAEMON_UTIL, "check", constants.MASTERD])
|
3281 | f154a7a3 | Michael Hanselmann | if not result.failed: |
3282 | afdc3985 | Iustin Pop | _Fail("The master daemon is running, will not demote")
|
3283 | f154a7a3 | Michael Hanselmann | |
3284 | 56aa9fd5 | Iustin Pop | try:
|
3285 | 710f30ec | Michael Hanselmann | if os.path.isfile(pathutils.CLUSTER_CONF_FILE):
|
3286 | 710f30ec | Michael Hanselmann | utils.CreateBackup(pathutils.CLUSTER_CONF_FILE) |
3287 | 56aa9fd5 | Iustin Pop | except EnvironmentError, err: |
3288 | 56aa9fd5 | Iustin Pop | if err.errno != errno.ENOENT:
|
3289 | afdc3985 | Iustin Pop | _Fail("Error while backing up cluster file: %s", err, exc=True) |
3290 | f154a7a3 | Michael Hanselmann | |
3291 | 710f30ec | Michael Hanselmann | utils.RemoveFile(pathutils.CLUSTER_CONF_FILE) |
3292 | 56aa9fd5 | Iustin Pop | |
3293 | 56aa9fd5 | Iustin Pop | |
3294 | f942a838 | Michael Hanselmann | def _GetX509Filenames(cryptodir, name): |
3295 | f942a838 | Michael Hanselmann | """Returns the full paths for the private key and certificate.
|
3296 | f942a838 | Michael Hanselmann |
|
3297 | f942a838 | Michael Hanselmann | """
|
3298 | f942a838 | Michael Hanselmann | return (utils.PathJoin(cryptodir, name),
|
3299 | f942a838 | Michael Hanselmann | utils.PathJoin(cryptodir, name, _X509_KEY_FILE), |
3300 | f942a838 | Michael Hanselmann | utils.PathJoin(cryptodir, name, _X509_CERT_FILE)) |
3301 | f942a838 | Michael Hanselmann | |
3302 | f942a838 | Michael Hanselmann | |
3303 | 710f30ec | Michael Hanselmann | def CreateX509Certificate(validity, cryptodir=pathutils.CRYPTO_KEYS_DIR): |
3304 | f942a838 | Michael Hanselmann | """Creates a new X509 certificate for SSL/TLS.
|
3305 | f942a838 | Michael Hanselmann |
|
3306 | f942a838 | Michael Hanselmann | @type validity: int
|
3307 | f942a838 | Michael Hanselmann | @param validity: Validity in seconds
|
3308 | f942a838 | Michael Hanselmann | @rtype: tuple; (string, string)
|
3309 | f942a838 | Michael Hanselmann | @return: Certificate name and public part
|
3310 | f942a838 | Michael Hanselmann |
|
3311 | f942a838 | Michael Hanselmann | """
|
3312 | f942a838 | Michael Hanselmann | (key_pem, cert_pem) = \ |
3313 | b705c7a6 | Manuel Franceschini | utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(), |
3314 | f942a838 | Michael Hanselmann | min(validity, _MAX_SSL_CERT_VALIDITY))
|
3315 | f942a838 | Michael Hanselmann | |
3316 | f942a838 | Michael Hanselmann | cert_dir = tempfile.mkdtemp(dir=cryptodir, |
3317 | f942a838 | Michael Hanselmann | prefix="x509-%s-" % utils.TimestampForFilename())
|
3318 | f942a838 | Michael Hanselmann | try:
|
3319 | f942a838 | Michael Hanselmann | name = os.path.basename(cert_dir) |
3320 | f942a838 | Michael Hanselmann | assert len(name) > 5 |
3321 | f942a838 | Michael Hanselmann | |
3322 | f942a838 | Michael Hanselmann | (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name) |
3323 | f942a838 | Michael Hanselmann | |
3324 | f942a838 | Michael Hanselmann | utils.WriteFile(key_file, mode=0400, data=key_pem)
|
3325 | f942a838 | Michael Hanselmann | utils.WriteFile(cert_file, mode=0400, data=cert_pem)
|
3326 | f942a838 | Michael Hanselmann | |
3327 | f942a838 | Michael Hanselmann | # Never return private key as it shouldn't leave the node
|
3328 | f942a838 | Michael Hanselmann | return (name, cert_pem)
|
3329 | f942a838 | Michael Hanselmann | except Exception: |
3330 | f942a838 | Michael Hanselmann | shutil.rmtree(cert_dir, ignore_errors=True)
|
3331 | f942a838 | Michael Hanselmann | raise
|
3332 | f942a838 | Michael Hanselmann | |
3333 | f942a838 | Michael Hanselmann | |
3334 | 710f30ec | Michael Hanselmann | def RemoveX509Certificate(name, cryptodir=pathutils.CRYPTO_KEYS_DIR): |
3335 | f942a838 | Michael Hanselmann | """Removes a X509 certificate.
|
3336 | f942a838 | Michael Hanselmann |
|
3337 | f942a838 | Michael Hanselmann | @type name: string
|
3338 | f942a838 | Michael Hanselmann | @param name: Certificate name
|
3339 | f942a838 | Michael Hanselmann |
|
3340 | f942a838 | Michael Hanselmann | """
|
3341 | f942a838 | Michael Hanselmann | (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name) |
3342 | f942a838 | Michael Hanselmann | |
3343 | f942a838 | Michael Hanselmann | utils.RemoveFile(key_file) |
3344 | f942a838 | Michael Hanselmann | utils.RemoveFile(cert_file) |
3345 | f942a838 | Michael Hanselmann | |
3346 | f942a838 | Michael Hanselmann | try:
|
3347 | f942a838 | Michael Hanselmann | os.rmdir(cert_dir) |
3348 | f942a838 | Michael Hanselmann | except EnvironmentError, err: |
3349 | f942a838 | Michael Hanselmann | _Fail("Cannot remove certificate directory '%s': %s",
|
3350 | f942a838 | Michael Hanselmann | cert_dir, err) |
3351 | f942a838 | Michael Hanselmann | |
3352 | f942a838 | Michael Hanselmann | |
3353 | 1651d116 | Michael Hanselmann | def _GetImportExportIoCommand(instance, mode, ieio, ieargs): |
3354 | 1651d116 | Michael Hanselmann | """Returns the command for the requested input/output.
|
3355 | 1651d116 | Michael Hanselmann |
|
3356 | 1651d116 | Michael Hanselmann | @type instance: L{objects.Instance}
|
3357 | 1651d116 | Michael Hanselmann | @param instance: The instance object
|
3358 | 1651d116 | Michael Hanselmann | @param mode: Import/export mode
|
3359 | 1651d116 | Michael Hanselmann | @param ieio: Input/output type
|
3360 | 1651d116 | Michael Hanselmann | @param ieargs: Input/output arguments
|
3361 | 1651d116 | Michael Hanselmann |
|
3362 | 1651d116 | Michael Hanselmann | """
|
3363 | 1651d116 | Michael Hanselmann | assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT) |
3364 | 1651d116 | Michael Hanselmann | |
3365 | 1651d116 | Michael Hanselmann | env = None
|
3366 | 1651d116 | Michael Hanselmann | prefix = None
|
3367 | 1651d116 | Michael Hanselmann | suffix = None
|
3368 | 2ad5550d | Michael Hanselmann | exp_size = None
|
3369 | 1651d116 | Michael Hanselmann | |
3370 | 1651d116 | Michael Hanselmann | if ieio == constants.IEIO_FILE:
|
3371 | 1651d116 | Michael Hanselmann | (filename, ) = ieargs |
3372 | 1651d116 | Michael Hanselmann | |
3373 | 1651d116 | Michael Hanselmann | if not utils.IsNormAbsPath(filename): |
3374 | 1651d116 | Michael Hanselmann | _Fail("Path '%s' is not normalized or absolute", filename)
|
3375 | 1651d116 | Michael Hanselmann | |
3376 | 748c9884 | René Nussbaumer | real_filename = os.path.realpath(filename) |
3377 | 748c9884 | René Nussbaumer | directory = os.path.dirname(real_filename) |
3378 | 1651d116 | Michael Hanselmann | |
3379 | 710f30ec | Michael Hanselmann | if not utils.IsBelowDir(pathutils.EXPORT_DIR, real_filename): |
3380 | 748c9884 | René Nussbaumer | _Fail("File '%s' is not under exports directory '%s': %s",
|
3381 | 710f30ec | Michael Hanselmann | filename, pathutils.EXPORT_DIR, real_filename) |
3382 | 1651d116 | Michael Hanselmann | |
3383 | 1651d116 | Michael Hanselmann | # Create directory
|
3384 | 1651d116 | Michael Hanselmann | utils.Makedirs(directory, mode=0750)
|
3385 | 1651d116 | Michael Hanselmann | |
3386 | 1651d116 | Michael Hanselmann | quoted_filename = utils.ShellQuote(filename) |
3387 | 1651d116 | Michael Hanselmann | |
3388 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
3389 | 1651d116 | Michael Hanselmann | suffix = "> %s" % quoted_filename
|
3390 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
3391 | 1651d116 | Michael Hanselmann | suffix = "< %s" % quoted_filename
|
3392 | 1651d116 | Michael Hanselmann | |
3393 | 2ad5550d | Michael Hanselmann | # Retrieve file size
|
3394 | 2ad5550d | Michael Hanselmann | try:
|
3395 | 2ad5550d | Michael Hanselmann | st = os.stat(filename) |
3396 | 2ad5550d | Michael Hanselmann | except EnvironmentError, err: |
3397 | 2ad5550d | Michael Hanselmann | logging.error("Can't stat(2) %s: %s", filename, err)
|
3398 | 2ad5550d | Michael Hanselmann | else:
|
3399 | 2ad5550d | Michael Hanselmann | exp_size = utils.BytesToMebibyte(st.st_size) |
3400 | 2ad5550d | Michael Hanselmann | |
3401 | 1651d116 | Michael Hanselmann | elif ieio == constants.IEIO_RAW_DISK:
|
3402 | 1651d116 | Michael Hanselmann | (disk, ) = ieargs |
3403 | 1651d116 | Michael Hanselmann | |
3404 | 1651d116 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
3405 | 1651d116 | Michael Hanselmann | |
3406 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
3407 | 1651d116 | Michael Hanselmann | # we set here a smaller block size as, due to transport buffering, more
|
3408 | 1651d116 | Michael Hanselmann | # than 64-128k will mostly ignored; we use nocreat to fail if the device
|
3409 | 1651d116 | Michael Hanselmann | # is not already there or we pass a wrong path; we use notrunc to no
|
3410 | 1651d116 | Michael Hanselmann | # attempt truncate on an LV device; we use oflag=dsync to not buffer too
|
3411 | 1651d116 | Michael Hanselmann | # much memory; this means that at best, we flush every 64k, which will
|
3412 | 1651d116 | Michael Hanselmann | # not be very fast
|
3413 | 1651d116 | Michael Hanselmann | suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
|
3414 | 1651d116 | Michael Hanselmann | " bs=%s oflag=dsync"),
|
3415 | 1651d116 | Michael Hanselmann | real_disk.dev_path, |
3416 | 1651d116 | Michael Hanselmann | str(64 * 1024)) |
3417 | 1651d116 | Michael Hanselmann | |
3418 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
3419 | 1651d116 | Michael Hanselmann | # the block size on the read dd is 1MiB to match our units
|
3420 | 1651d116 | Michael Hanselmann | prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
|
3421 | 1651d116 | Michael Hanselmann | real_disk.dev_path, |
3422 | 1651d116 | Michael Hanselmann | str(1024 * 1024), # 1 MB |
3423 | 1651d116 | Michael Hanselmann | str(disk.size))
|
3424 | 2ad5550d | Michael Hanselmann | exp_size = disk.size |
3425 | 1651d116 | Michael Hanselmann | |
3426 | 1651d116 | Michael Hanselmann | elif ieio == constants.IEIO_SCRIPT:
|
3427 | 1651d116 | Michael Hanselmann | (disk, disk_index, ) = ieargs |
3428 | 1651d116 | Michael Hanselmann | |
3429 | 1651d116 | Michael Hanselmann | assert isinstance(disk_index, (int, long)) |
3430 | 1651d116 | Michael Hanselmann | |
3431 | 1651d116 | Michael Hanselmann | real_disk = _OpenRealBD(disk) |
3432 | 1651d116 | Michael Hanselmann | |
3433 | 1651d116 | Michael Hanselmann | inst_os = OSFromDisk(instance.os) |
3434 | 1651d116 | Michael Hanselmann | env = OSEnvironment(instance, inst_os) |
3435 | 1651d116 | Michael Hanselmann | |
3436 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
3437 | 1651d116 | Michael Hanselmann | env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index] |
3438 | 1651d116 | Michael Hanselmann | env["IMPORT_INDEX"] = str(disk_index) |
3439 | 1651d116 | Michael Hanselmann | script = inst_os.import_script |
3440 | 1651d116 | Michael Hanselmann | |
3441 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
3442 | 1651d116 | Michael Hanselmann | env["EXPORT_DEVICE"] = real_disk.dev_path
|
3443 | 1651d116 | Michael Hanselmann | env["EXPORT_INDEX"] = str(disk_index) |
3444 | 1651d116 | Michael Hanselmann | script = inst_os.export_script |
3445 | 1651d116 | Michael Hanselmann | |
3446 | 1651d116 | Michael Hanselmann | # TODO: Pass special environment only to script
|
3447 | 1651d116 | Michael Hanselmann | script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
|
3448 | 1651d116 | Michael Hanselmann | |
3449 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
3450 | 1651d116 | Michael Hanselmann | suffix = "| %s" % script_cmd
|
3451 | 1651d116 | Michael Hanselmann | |
3452 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
3453 | 1651d116 | Michael Hanselmann | prefix = "%s |" % script_cmd
|
3454 | 1651d116 | Michael Hanselmann | |
3455 | 2ad5550d | Michael Hanselmann | # Let script predict size
|
3456 | 2ad5550d | Michael Hanselmann | exp_size = constants.IE_CUSTOM_SIZE |
3457 | 2ad5550d | Michael Hanselmann | |
3458 | 1651d116 | Michael Hanselmann | else:
|
3459 | 1651d116 | Michael Hanselmann | _Fail("Invalid %s I/O mode %r", mode, ieio)
|
3460 | 1651d116 | Michael Hanselmann | |
3461 | 2ad5550d | Michael Hanselmann | return (env, prefix, suffix, exp_size)
|
3462 | 1651d116 | Michael Hanselmann | |
3463 | 1651d116 | Michael Hanselmann | |
3464 | 1651d116 | Michael Hanselmann | def _CreateImportExportStatusDir(prefix): |
3465 | 1651d116 | Michael Hanselmann | """Creates status directory for import/export.
|
3466 | 1651d116 | Michael Hanselmann |
|
3467 | 1651d116 | Michael Hanselmann | """
|
3468 | 710f30ec | Michael Hanselmann | return tempfile.mkdtemp(dir=pathutils.IMPORT_EXPORT_DIR,
|
3469 | 1651d116 | Michael Hanselmann | prefix=("%s-%s-" %
|
3470 | 1651d116 | Michael Hanselmann | (prefix, utils.TimestampForFilename()))) |
3471 | 1651d116 | Michael Hanselmann | |
3472 | 1651d116 | Michael Hanselmann | |
3473 | 6613661a | Iustin Pop | def StartImportExportDaemon(mode, opts, host, port, instance, component, |
3474 | 6613661a | Iustin Pop | ieio, ieioargs): |
3475 | 1651d116 | Michael Hanselmann | """Starts an import or export daemon.
|
3476 | 1651d116 | Michael Hanselmann |
|
3477 | 1651d116 | Michael Hanselmann | @param mode: Import/output mode
|
3478 | eb630f50 | Michael Hanselmann | @type opts: L{objects.ImportExportOptions}
|
3479 | eb630f50 | Michael Hanselmann | @param opts: Daemon options
|
3480 | 1651d116 | Michael Hanselmann | @type host: string
|
3481 | 1651d116 | Michael Hanselmann | @param host: Remote host for export (None for import)
|
3482 | 1651d116 | Michael Hanselmann | @type port: int
|
3483 | 1651d116 | Michael Hanselmann | @param port: Remote port for export (None for import)
|
3484 | 1651d116 | Michael Hanselmann | @type instance: L{objects.Instance}
|
3485 | 1651d116 | Michael Hanselmann | @param instance: Instance object
|
3486 | 6613661a | Iustin Pop | @type component: string
|
3487 | 6613661a | Iustin Pop | @param component: which part of the instance is transferred now,
|
3488 | 6613661a | Iustin Pop | e.g. 'disk/0'
|
3489 | 1651d116 | Michael Hanselmann | @param ieio: Input/output type
|
3490 | 1651d116 | Michael Hanselmann | @param ieioargs: Input/output arguments
|
3491 | 1651d116 | Michael Hanselmann |
|
3492 | 1651d116 | Michael Hanselmann | """
|
3493 | 1651d116 | Michael Hanselmann | if mode == constants.IEM_IMPORT:
|
3494 | 1651d116 | Michael Hanselmann | prefix = "import"
|
3495 | 1651d116 | Michael Hanselmann | |
3496 | 1651d116 | Michael Hanselmann | if not (host is None and port is None): |
3497 | 1651d116 | Michael Hanselmann | _Fail("Can not specify host or port on import")
|
3498 | 1651d116 | Michael Hanselmann | |
3499 | 1651d116 | Michael Hanselmann | elif mode == constants.IEM_EXPORT:
|
3500 | 1651d116 | Michael Hanselmann | prefix = "export"
|
3501 | 1651d116 | Michael Hanselmann | |
3502 | 1651d116 | Michael Hanselmann | if host is None or port is None: |
3503 | 1651d116 | Michael Hanselmann | _Fail("Host and port must be specified for an export")
|
3504 | 1651d116 | Michael Hanselmann | |
3505 | 1651d116 | Michael Hanselmann | else:
|
3506 | 1651d116 | Michael Hanselmann | _Fail("Invalid mode %r", mode)
|
3507 | 1651d116 | Michael Hanselmann | |
3508 | eb630f50 | Michael Hanselmann | if (opts.key_name is None) ^ (opts.ca_pem is None): |
3509 | 1651d116 | Michael Hanselmann | _Fail("Cluster certificate can only be used for both key and CA")
|
3510 | 1651d116 | Michael Hanselmann | |
3511 | 2ad5550d | Michael Hanselmann | (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \ |
3512 | 1651d116 | Michael Hanselmann | _GetImportExportIoCommand(instance, mode, ieio, ieioargs) |
3513 | 1651d116 | Michael Hanselmann | |
3514 | eb630f50 | Michael Hanselmann | if opts.key_name is None: |
3515 | 1651d116 | Michael Hanselmann | # Use server.pem
|
3516 | 710f30ec | Michael Hanselmann | key_path = pathutils.NODED_CERT_FILE |
3517 | 710f30ec | Michael Hanselmann | cert_path = pathutils.NODED_CERT_FILE |
3518 | eb630f50 | Michael Hanselmann | assert opts.ca_pem is None |
3519 | 1651d116 | Michael Hanselmann | else:
|
3520 | 710f30ec | Michael Hanselmann | (_, key_path, cert_path) = _GetX509Filenames(pathutils.CRYPTO_KEYS_DIR, |
3521 | eb630f50 | Michael Hanselmann | opts.key_name) |
3522 | eb630f50 | Michael Hanselmann | assert opts.ca_pem is not None |
3523 | 1651d116 | Michael Hanselmann | |
3524 | 63bcea2a | Michael Hanselmann | for i in [key_path, cert_path]: |
3525 | dcaabc4f | Michael Hanselmann | if not os.path.exists(i): |
3526 | 63bcea2a | Michael Hanselmann | _Fail("File '%s' does not exist" % i)
|
3527 | 63bcea2a | Michael Hanselmann | |
3528 | 6613661a | Iustin Pop | status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
|
3529 | 1651d116 | Michael Hanselmann | try:
|
3530 | 1651d116 | Michael Hanselmann | status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE) |
3531 | 1651d116 | Michael Hanselmann | pid_file = utils.PathJoin(status_dir, _IES_PID_FILE) |
3532 | 63bcea2a | Michael Hanselmann | ca_file = utils.PathJoin(status_dir, _IES_CA_FILE) |
3533 | 1651d116 | Michael Hanselmann | |
3534 | eb630f50 | Michael Hanselmann | if opts.ca_pem is None: |
3535 | 1651d116 | Michael Hanselmann | # Use server.pem
|
3536 | 710f30ec | Michael Hanselmann | ca = utils.ReadFile(pathutils.NODED_CERT_FILE) |
3537 | eb630f50 | Michael Hanselmann | else:
|
3538 | eb630f50 | Michael Hanselmann | ca = opts.ca_pem |
3539 | 63bcea2a | Michael Hanselmann | |
3540 | eb630f50 | Michael Hanselmann | # Write CA file
|
3541 | 63bcea2a | Michael Hanselmann | utils.WriteFile(ca_file, data=ca, mode=0400)
|
3542 | 1651d116 | Michael Hanselmann | |
3543 | 1651d116 | Michael Hanselmann | cmd = [ |
3544 | 710f30ec | Michael Hanselmann | pathutils.IMPORT_EXPORT_DAEMON, |
3545 | 1651d116 | Michael Hanselmann | status_file, mode, |
3546 | 1651d116 | Michael Hanselmann | "--key=%s" % key_path,
|
3547 | 1651d116 | Michael Hanselmann | "--cert=%s" % cert_path,
|
3548 | 63bcea2a | Michael Hanselmann | "--ca=%s" % ca_file,
|
3549 | 1651d116 | Michael Hanselmann | ] |
3550 | 1651d116 | Michael Hanselmann | |
3551 | 1651d116 | Michael Hanselmann | if host:
|
3552 | 1651d116 | Michael Hanselmann | cmd.append("--host=%s" % host)
|
3553 | 1651d116 | Michael Hanselmann | |
3554 | 1651d116 | Michael Hanselmann | if port:
|
3555 | 1651d116 | Michael Hanselmann | cmd.append("--port=%s" % port)
|
3556 | 1651d116 | Michael Hanselmann | |
3557 | 855d2fc7 | Michael Hanselmann | if opts.ipv6:
|
3558 | 855d2fc7 | Michael Hanselmann | cmd.append("--ipv6")
|
3559 | 855d2fc7 | Michael Hanselmann | else:
|
3560 | 855d2fc7 | Michael Hanselmann | cmd.append("--ipv4")
|
3561 | 855d2fc7 | Michael Hanselmann | |
3562 | a5310c2a | Michael Hanselmann | if opts.compress:
|
3563 | a5310c2a | Michael Hanselmann | cmd.append("--compress=%s" % opts.compress)
|
3564 | a5310c2a | Michael Hanselmann | |
3565 | af1d39b1 | Michael Hanselmann | if opts.magic:
|
3566 | af1d39b1 | Michael Hanselmann | cmd.append("--magic=%s" % opts.magic)
|
3567 | af1d39b1 | Michael Hanselmann | |
3568 | 2ad5550d | Michael Hanselmann | if exp_size is not None: |
3569 | 2ad5550d | Michael Hanselmann | cmd.append("--expected-size=%s" % exp_size)
|
3570 | 2ad5550d | Michael Hanselmann | |
3571 | 1651d116 | Michael Hanselmann | if cmd_prefix:
|
3572 | 1651d116 | Michael Hanselmann | cmd.append("--cmd-prefix=%s" % cmd_prefix)
|
3573 | 1651d116 | Michael Hanselmann | |
3574 | 1651d116 | Michael Hanselmann | if cmd_suffix:
|
3575 | 1651d116 | Michael Hanselmann | cmd.append("--cmd-suffix=%s" % cmd_suffix)
|
3576 | 1651d116 | Michael Hanselmann | |
3577 | 4478301b | Michael Hanselmann | if mode == constants.IEM_EXPORT:
|
3578 | 4478301b | Michael Hanselmann | # Retry connection a few times when connecting to remote peer
|
3579 | 4478301b | Michael Hanselmann | cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
|
3580 | 4478301b | Michael Hanselmann | cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
|
3581 | 4478301b | Michael Hanselmann | elif opts.connect_timeout is not None: |
3582 | 4478301b | Michael Hanselmann | assert mode == constants.IEM_IMPORT
|
3583 | 4478301b | Michael Hanselmann | # Overall timeout for establishing connection while listening
|
3584 | 4478301b | Michael Hanselmann | cmd.append("--connect-timeout=%s" % opts.connect_timeout)
|
3585 | 4478301b | Michael Hanselmann | |
3586 | 6aa7a354 | Iustin Pop | logfile = _InstanceLogName(prefix, instance.os, instance.name, component) |
3587 | 1651d116 | Michael Hanselmann | |
3588 | 1651d116 | Michael Hanselmann | # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
|
3589 | 1651d116 | Michael Hanselmann | # support for receiving a file descriptor for output
|
3590 | 1651d116 | Michael Hanselmann | utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file, |
3591 | 1651d116 | Michael Hanselmann | output=logfile) |
3592 | 1651d116 | Michael Hanselmann | |
3593 | 1651d116 | Michael Hanselmann | # The import/export name is simply the status directory name
|
3594 | 1651d116 | Michael Hanselmann | return os.path.basename(status_dir)
|
3595 | 1651d116 | Michael Hanselmann | |
3596 | 1651d116 | Michael Hanselmann | except Exception: |
3597 | 1651d116 | Michael Hanselmann | shutil.rmtree(status_dir, ignore_errors=True)
|
3598 | 1651d116 | Michael Hanselmann | raise
|
3599 | 1651d116 | Michael Hanselmann | |
3600 | 1651d116 | Michael Hanselmann | |
3601 | 1651d116 | Michael Hanselmann | def GetImportExportStatus(names): |
3602 | 1651d116 | Michael Hanselmann | """Returns import/export daemon status.
|
3603 | 1651d116 | Michael Hanselmann |
|
3604 | 1651d116 | Michael Hanselmann | @type names: sequence
|
3605 | 1651d116 | Michael Hanselmann | @param names: List of names
|
3606 | 1651d116 | Michael Hanselmann | @rtype: List of dicts
|
3607 | 1651d116 | Michael Hanselmann | @return: Returns a list of the state of each named import/export or None if a
|
3608 | 1651d116 | Michael Hanselmann | status couldn't be read
|
3609 | 1651d116 | Michael Hanselmann |
|
3610 | 1651d116 | Michael Hanselmann | """
|
3611 | 1651d116 | Michael Hanselmann | result = [] |
3612 | 1651d116 | Michael Hanselmann | |
3613 | 1651d116 | Michael Hanselmann | for name in names: |
3614 | 710f30ec | Michael Hanselmann | status_file = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name, |
3615 | 1651d116 | Michael Hanselmann | _IES_STATUS_FILE) |
3616 | 1651d116 | Michael Hanselmann | |
3617 | 1651d116 | Michael Hanselmann | try:
|
3618 | 1651d116 | Michael Hanselmann | data = utils.ReadFile(status_file) |
3619 | 1651d116 | Michael Hanselmann | except EnvironmentError, err: |
3620 | 1651d116 | Michael Hanselmann | if err.errno != errno.ENOENT:
|
3621 | 1651d116 | Michael Hanselmann | raise
|
3622 | 1651d116 | Michael Hanselmann | data = None
|
3623 | 1651d116 | Michael Hanselmann | |
3624 | 1651d116 | Michael Hanselmann | if not data: |
3625 | 1651d116 | Michael Hanselmann | result.append(None)
|
3626 | 1651d116 | Michael Hanselmann | continue
|
3627 | 1651d116 | Michael Hanselmann | |
3628 | 1651d116 | Michael Hanselmann | result.append(serializer.LoadJson(data)) |
3629 | 1651d116 | Michael Hanselmann | |
3630 | 1651d116 | Michael Hanselmann | return result
|
3631 | 1651d116 | Michael Hanselmann | |
3632 | 1651d116 | Michael Hanselmann | |
3633 | f81c4737 | Michael Hanselmann | def AbortImportExport(name): |
3634 | f81c4737 | Michael Hanselmann | """Sends SIGTERM to a running import/export daemon.
|
3635 | f81c4737 | Michael Hanselmann |
|
3636 | f81c4737 | Michael Hanselmann | """
|
3637 | f81c4737 | Michael Hanselmann | logging.info("Abort import/export %s", name)
|
3638 | f81c4737 | Michael Hanselmann | |
3639 | 710f30ec | Michael Hanselmann | status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name) |
3640 | f81c4737 | Michael Hanselmann | pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE)) |
3641 | f81c4737 | Michael Hanselmann | |
3642 | f81c4737 | Michael Hanselmann | if pid:
|
3643 | f81c4737 | Michael Hanselmann | logging.info("Import/export %s is running with PID %s, sending SIGTERM",
|
3644 | f81c4737 | Michael Hanselmann | name, pid) |
3645 | 560cbec1 | Michael Hanselmann | utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM) |
3646 | f81c4737 | Michael Hanselmann | |
3647 | f81c4737 | Michael Hanselmann | |
3648 | 1651d116 | Michael Hanselmann | def CleanupImportExport(name): |
3649 | 1651d116 | Michael Hanselmann | """Cleanup after an import or export.
|
3650 | 1651d116 | Michael Hanselmann |
|
3651 | 1651d116 | Michael Hanselmann | If the import/export daemon is still running it's killed. Afterwards the
|
3652 | 1651d116 | Michael Hanselmann | whole status directory is removed.
|
3653 | 1651d116 | Michael Hanselmann |
|
3654 | 1651d116 | Michael Hanselmann | """
|
3655 | 1651d116 | Michael Hanselmann | logging.info("Finalizing import/export %s", name)
|
3656 | 1651d116 | Michael Hanselmann | |
3657 | 710f30ec | Michael Hanselmann | status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name) |
3658 | 1651d116 | Michael Hanselmann | |
3659 | debed9ae | Michael Hanselmann | pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE)) |
3660 | 1651d116 | Michael Hanselmann | |
3661 | 1651d116 | Michael Hanselmann | if pid:
|
3662 | 1651d116 | Michael Hanselmann | logging.info("Import/export %s is still running with PID %s",
|
3663 | 1651d116 | Michael Hanselmann | name, pid) |
3664 | 1651d116 | Michael Hanselmann | utils.KillProcess(pid, waitpid=False)
|
3665 | 1651d116 | Michael Hanselmann | |
3666 | 1651d116 | Michael Hanselmann | shutil.rmtree(status_dir, ignore_errors=True)
|
3667 | 1651d116 | Michael Hanselmann | |
3668 | 1651d116 | Michael Hanselmann | |
3669 | 6b93ec9d | Iustin Pop | def _FindDisks(nodes_ip, disks): |
3670 | 6b93ec9d | Iustin Pop | """Sets the physical ID on disks and returns the block devices.
|
3671 | 6b93ec9d | Iustin Pop |
|
3672 | 6b93ec9d | Iustin Pop | """
|
3673 | 6b93ec9d | Iustin Pop | # set the correct physical ID
|
3674 | b705c7a6 | Manuel Franceschini | my_name = netutils.Hostname.GetSysName() |
3675 | 6b93ec9d | Iustin Pop | for cf in disks: |
3676 | 6b93ec9d | Iustin Pop | cf.SetPhysicalID(my_name, nodes_ip) |
3677 | 6b93ec9d | Iustin Pop | |
3678 | 6b93ec9d | Iustin Pop | bdevs = [] |
3679 | 6b93ec9d | Iustin Pop | |
3680 | 6b93ec9d | Iustin Pop | for cf in disks: |
3681 | 6b93ec9d | Iustin Pop | rd = _RecursiveFindBD(cf) |
3682 | 6b93ec9d | Iustin Pop | if rd is None: |
3683 | 5a533f8a | Iustin Pop | _Fail("Can't find device %s", cf)
|
3684 | 6b93ec9d | Iustin Pop | bdevs.append(rd) |
3685 | 5a533f8a | Iustin Pop | return bdevs
|
3686 | 6b93ec9d | Iustin Pop | |
3687 | 6b93ec9d | Iustin Pop | |
3688 | 6b93ec9d | Iustin Pop | def DrbdDisconnectNet(nodes_ip, disks): |
3689 | 6b93ec9d | Iustin Pop | """Disconnects the network on a list of drbd devices.
|
3690 | 6b93ec9d | Iustin Pop |
|
3691 | 6b93ec9d | Iustin Pop | """
|
3692 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
3693 | 6b93ec9d | Iustin Pop | |
3694 | 6b93ec9d | Iustin Pop | # disconnect disks
|
3695 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3696 | 6b93ec9d | Iustin Pop | try:
|
3697 | 6b93ec9d | Iustin Pop | rd.DisconnectNet() |
3698 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
3699 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration to standalone mode: %s",
|
3700 | 2cc6781a | Iustin Pop | err, exc=True)
|
3701 | 6b93ec9d | Iustin Pop | |
3702 | 6b93ec9d | Iustin Pop | |
3703 | 6b93ec9d | Iustin Pop | def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster): |
3704 | 6b93ec9d | Iustin Pop | """Attaches the network on a list of drbd devices.
|
3705 | 6b93ec9d | Iustin Pop |
|
3706 | 6b93ec9d | Iustin Pop | """
|
3707 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
3708 | 6b93ec9d | Iustin Pop | |
3709 | 6b93ec9d | Iustin Pop | if multimaster:
|
3710 | 53c776b5 | Iustin Pop | for idx, rd in enumerate(bdevs): |
3711 | 6b93ec9d | Iustin Pop | try:
|
3712 | 53c776b5 | Iustin Pop | _SymlinkBlockDev(instance_name, rd.dev_path, idx) |
3713 | 6b93ec9d | Iustin Pop | except EnvironmentError, err: |
3714 | 2cc6781a | Iustin Pop | _Fail("Can't create symlink: %s", err)
|
3715 | 6b93ec9d | Iustin Pop | # reconnect disks, switch to new master configuration and if
|
3716 | 6b93ec9d | Iustin Pop | # needed primary mode
|
3717 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3718 | 6b93ec9d | Iustin Pop | try:
|
3719 | 6b93ec9d | Iustin Pop | rd.AttachNet(multimaster) |
3720 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
3721 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration: %s", err)
|
3722 | 3c0cdc83 | Michael Hanselmann | |
3723 | 6b93ec9d | Iustin Pop | # wait until the disks are connected; we need to retry the re-attach
|
3724 | 6b93ec9d | Iustin Pop | # if the device becomes standalone, as this might happen if the one
|
3725 | 6b93ec9d | Iustin Pop | # node disconnects and reconnects in a different mode before the
|
3726 | 6b93ec9d | Iustin Pop | # other node reconnects; in this case, one or both of the nodes will
|
3727 | 6b93ec9d | Iustin Pop | # decide it has wrong configuration and switch to standalone
|
3728 | 3c0cdc83 | Michael Hanselmann | |
3729 | 3c0cdc83 | Michael Hanselmann | def _Attach(): |
3730 | 6b93ec9d | Iustin Pop | all_connected = True
|
3731 | 3c0cdc83 | Michael Hanselmann | |
3732 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3733 | 6b93ec9d | Iustin Pop | stats = rd.GetProcStatus() |
3734 | 3c0cdc83 | Michael Hanselmann | |
3735 | 3c0cdc83 | Michael Hanselmann | all_connected = (all_connected and
|
3736 | 3c0cdc83 | Michael Hanselmann | (stats.is_connected or stats.is_in_resync))
|
3737 | 3c0cdc83 | Michael Hanselmann | |
3738 | 6b93ec9d | Iustin Pop | if stats.is_standalone:
|
3739 | 6b93ec9d | Iustin Pop | # peer had different config info and this node became
|
3740 | 6b93ec9d | Iustin Pop | # standalone, even though this should not happen with the
|
3741 | 6b93ec9d | Iustin Pop | # new staged way of changing disk configs
|
3742 | 6b93ec9d | Iustin Pop | try:
|
3743 | c738375b | Iustin Pop | rd.AttachNet(multimaster) |
3744 | 6b93ec9d | Iustin Pop | except errors.BlockDeviceError, err:
|
3745 | 2cc6781a | Iustin Pop | _Fail("Can't change network configuration: %s", err)
|
3746 | 3c0cdc83 | Michael Hanselmann | |
3747 | 3c0cdc83 | Michael Hanselmann | if not all_connected: |
3748 | 3c0cdc83 | Michael Hanselmann | raise utils.RetryAgain()
|
3749 | 3c0cdc83 | Michael Hanselmann | |
3750 | 3c0cdc83 | Michael Hanselmann | try:
|
3751 | 3c0cdc83 | Michael Hanselmann | # Start with a delay of 100 miliseconds and go up to 5 seconds
|
3752 | 3c0cdc83 | Michael Hanselmann | utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60) |
3753 | 3c0cdc83 | Michael Hanselmann | except utils.RetryTimeout:
|
3754 | afdc3985 | Iustin Pop | _Fail("Timeout in disk reconnecting")
|
3755 | 3c0cdc83 | Michael Hanselmann | |
3756 | 6b93ec9d | Iustin Pop | if multimaster:
|
3757 | 6b93ec9d | Iustin Pop | # change to primary mode
|
3758 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3759 | d3da87b8 | Iustin Pop | try:
|
3760 | d3da87b8 | Iustin Pop | rd.Open() |
3761 | d3da87b8 | Iustin Pop | except errors.BlockDeviceError, err:
|
3762 | 2cc6781a | Iustin Pop | _Fail("Can't change to primary mode: %s", err)
|
3763 | 6b93ec9d | Iustin Pop | |
3764 | 6b93ec9d | Iustin Pop | |
3765 | 6b93ec9d | Iustin Pop | def DrbdWaitSync(nodes_ip, disks): |
3766 | 6b93ec9d | Iustin Pop | """Wait until DRBDs have synchronized.
|
3767 | 6b93ec9d | Iustin Pop |
|
3768 | 6b93ec9d | Iustin Pop | """
|
3769 | db8667b7 | Iustin Pop | def _helper(rd): |
3770 | db8667b7 | Iustin Pop | stats = rd.GetProcStatus() |
3771 | db8667b7 | Iustin Pop | if not (stats.is_connected or stats.is_in_resync): |
3772 | db8667b7 | Iustin Pop | raise utils.RetryAgain()
|
3773 | db8667b7 | Iustin Pop | return stats
|
3774 | db8667b7 | Iustin Pop | |
3775 | 5a533f8a | Iustin Pop | bdevs = _FindDisks(nodes_ip, disks) |
3776 | 6b93ec9d | Iustin Pop | |
3777 | 6b93ec9d | Iustin Pop | min_resync = 100
|
3778 | 6b93ec9d | Iustin Pop | alldone = True
|
3779 | 6b93ec9d | Iustin Pop | for rd in bdevs: |
3780 | db8667b7 | Iustin Pop | try:
|
3781 | db8667b7 | Iustin Pop | # poll each second for 15 seconds
|
3782 | db8667b7 | Iustin Pop | stats = utils.Retry(_helper, 1, 15, args=[rd]) |
3783 | db8667b7 | Iustin Pop | except utils.RetryTimeout:
|
3784 | db8667b7 | Iustin Pop | stats = rd.GetProcStatus() |
3785 | db8667b7 | Iustin Pop | # last check
|
3786 | db8667b7 | Iustin Pop | if not (stats.is_connected or stats.is_in_resync): |
3787 | db8667b7 | Iustin Pop | _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
|
3788 | 6b93ec9d | Iustin Pop | alldone = alldone and (not stats.is_in_resync) |
3789 | 6b93ec9d | Iustin Pop | if stats.sync_percent is not None: |
3790 | 6b93ec9d | Iustin Pop | min_resync = min(min_resync, stats.sync_percent)
|
3791 | afdc3985 | Iustin Pop | |
3792 | c26a6bd2 | Iustin Pop | return (alldone, min_resync)
|
3793 | 6b93ec9d | Iustin Pop | |
3794 | 6b93ec9d | Iustin Pop | |
3795 | c46b9782 | Luca Bigliardi | def GetDrbdUsermodeHelper(): |
3796 | c46b9782 | Luca Bigliardi | """Returns DRBD usermode helper currently configured.
|
3797 | c46b9782 | Luca Bigliardi |
|
3798 | c46b9782 | Luca Bigliardi | """
|
3799 | c46b9782 | Luca Bigliardi | try:
|
3800 | 47e0abee | Thomas Thrainer | return drbd.DRBD8.GetUsermodeHelper()
|
3801 | c46b9782 | Luca Bigliardi | except errors.BlockDeviceError, err:
|
3802 | c46b9782 | Luca Bigliardi | _Fail(str(err))
|
3803 | c46b9782 | Luca Bigliardi | |
3804 | c46b9782 | Luca Bigliardi | |
3805 | f5118ade | Iustin Pop | def PowercycleNode(hypervisor_type): |
3806 | f5118ade | Iustin Pop | """Hard-powercycle the node.
|
3807 | f5118ade | Iustin Pop |
|
3808 | f5118ade | Iustin Pop | Because we need to return first, and schedule the powercycle in the
|
3809 | f5118ade | Iustin Pop | background, we won't be able to report failures nicely.
|
3810 | f5118ade | Iustin Pop |
|
3811 | f5118ade | Iustin Pop | """
|
3812 | f5118ade | Iustin Pop | hyper = hypervisor.GetHypervisor(hypervisor_type) |
3813 | f5118ade | Iustin Pop | try:
|
3814 | f5118ade | Iustin Pop | pid = os.fork() |
3815 | 29921401 | Iustin Pop | except OSError: |
3816 | f5118ade | Iustin Pop | # if we can't fork, we'll pretend that we're in the child process
|
3817 | f5118ade | Iustin Pop | pid = 0
|
3818 | f5118ade | Iustin Pop | if pid > 0: |
3819 | c26a6bd2 | Iustin Pop | return "Reboot scheduled in 5 seconds" |
3820 | 1af6ac0f | Luca Bigliardi | # ensure the child is running on ram
|
3821 | 1af6ac0f | Luca Bigliardi | try:
|
3822 | 1af6ac0f | Luca Bigliardi | utils.Mlockall() |
3823 | b459a848 | Andrea Spadaccini | except Exception: # pylint: disable=W0703 |
3824 | 1af6ac0f | Luca Bigliardi | pass
|
3825 | f5118ade | Iustin Pop | time.sleep(5)
|
3826 | f5118ade | Iustin Pop | hyper.PowercycleNode() |
3827 | f5118ade | Iustin Pop | |
3828 | f5118ade | Iustin Pop | |
3829 | 405bffe2 | Michael Hanselmann | def _VerifyRestrictedCmdName(cmd): |
3830 | 45bc4635 | Iustin Pop | """Verifies a restricted command name.
|
3831 | 1a2eb2dc | Michael Hanselmann |
|
3832 | 1a2eb2dc | Michael Hanselmann | @type cmd: string
|
3833 | 1a2eb2dc | Michael Hanselmann | @param cmd: Command name
|
3834 | 1a2eb2dc | Michael Hanselmann | @rtype: tuple; (boolean, string or None)
|
3835 | 1a2eb2dc | Michael Hanselmann | @return: The tuple's first element is the status; if C{False}, the second
|
3836 | 1a2eb2dc | Michael Hanselmann | element is an error message string, otherwise it's C{None}
|
3837 | 1a2eb2dc | Michael Hanselmann |
|
3838 | 1a2eb2dc | Michael Hanselmann | """
|
3839 | 1a2eb2dc | Michael Hanselmann | if not cmd.strip(): |
3840 | 1a2eb2dc | Michael Hanselmann | return (False, "Missing command name") |
3841 | 1a2eb2dc | Michael Hanselmann | |
3842 | 1a2eb2dc | Michael Hanselmann | if os.path.basename(cmd) != cmd:
|
3843 | 1a2eb2dc | Michael Hanselmann | return (False, "Invalid command name") |
3844 | 1a2eb2dc | Michael Hanselmann | |
3845 | 1a2eb2dc | Michael Hanselmann | if not constants.EXT_PLUGIN_MASK.match(cmd): |
3846 | 1a2eb2dc | Michael Hanselmann | return (False, "Command name contains forbidden characters") |
3847 | 1a2eb2dc | Michael Hanselmann | |
3848 | 1a2eb2dc | Michael Hanselmann | return (True, None) |
3849 | 1a2eb2dc | Michael Hanselmann | |
3850 | 1a2eb2dc | Michael Hanselmann | |
3851 | 405bffe2 | Michael Hanselmann | def _CommonRestrictedCmdCheck(path, owner): |
3852 | 45bc4635 | Iustin Pop | """Common checks for restricted command file system directories and files.
|
3853 | 1a2eb2dc | Michael Hanselmann |
|
3854 | 1a2eb2dc | Michael Hanselmann | @type path: string
|
3855 | 1a2eb2dc | Michael Hanselmann | @param path: Path to check
|
3856 | 1a2eb2dc | Michael Hanselmann | @param owner: C{None} or tuple containing UID and GID
|
3857 | 1a2eb2dc | Michael Hanselmann | @rtype: tuple; (boolean, string or C{os.stat} result)
|
3858 | 1a2eb2dc | Michael Hanselmann | @return: The tuple's first element is the status; if C{False}, the second
|
3859 | 1a2eb2dc | Michael Hanselmann | element is an error message string, otherwise it's the result of C{os.stat}
|
3860 | 1a2eb2dc | Michael Hanselmann |
|
3861 | 1a2eb2dc | Michael Hanselmann | """
|
3862 | 1a2eb2dc | Michael Hanselmann | if owner is None: |
3863 | 1a2eb2dc | Michael Hanselmann | # Default to root as owner
|
3864 | 1a2eb2dc | Michael Hanselmann | owner = (0, 0) |
3865 | 1a2eb2dc | Michael Hanselmann | |
3866 | 1a2eb2dc | Michael Hanselmann | try:
|
3867 | 1a2eb2dc | Michael Hanselmann | st = os.stat(path) |
3868 | 1a2eb2dc | Michael Hanselmann | except EnvironmentError, err: |
3869 | 1a2eb2dc | Michael Hanselmann | return (False, "Can't stat(2) '%s': %s" % (path, err)) |
3870 | 1a2eb2dc | Michael Hanselmann | |
3871 | 1a2eb2dc | Michael Hanselmann | if stat.S_IMODE(st.st_mode) & (~_RCMD_MAX_MODE):
|
3872 | 1a2eb2dc | Michael Hanselmann | return (False, "Permissions on '%s' are too permissive" % path) |
3873 | 1a2eb2dc | Michael Hanselmann | |
3874 | 1a2eb2dc | Michael Hanselmann | if (st.st_uid, st.st_gid) != owner:
|
3875 | 1a2eb2dc | Michael Hanselmann | (owner_uid, owner_gid) = owner |
3876 | 1a2eb2dc | Michael Hanselmann | return (False, "'%s' is not owned by %s:%s" % (path, owner_uid, owner_gid)) |
3877 | 1a2eb2dc | Michael Hanselmann | |
3878 | 1a2eb2dc | Michael Hanselmann | return (True, st) |
3879 | 1a2eb2dc | Michael Hanselmann | |
3880 | 1a2eb2dc | Michael Hanselmann | |
3881 | 405bffe2 | Michael Hanselmann | def _VerifyRestrictedCmdDirectory(path, _owner=None): |
3882 | 45bc4635 | Iustin Pop | """Verifies restricted command directory.
|
3883 | 1a2eb2dc | Michael Hanselmann |
|
3884 | 1a2eb2dc | Michael Hanselmann | @type path: string
|
3885 | 1a2eb2dc | Michael Hanselmann | @param path: Path to check
|
3886 | 1a2eb2dc | Michael Hanselmann | @rtype: tuple; (boolean, string or None)
|
3887 | 1a2eb2dc | Michael Hanselmann | @return: The tuple's first element is the status; if C{False}, the second
|
3888 | 1a2eb2dc | Michael Hanselmann | element is an error message string, otherwise it's C{None}
|
3889 | 1a2eb2dc | Michael Hanselmann |
|
3890 | 1a2eb2dc | Michael Hanselmann | """
|
3891 | 405bffe2 | Michael Hanselmann | (status, value) = _CommonRestrictedCmdCheck(path, _owner) |
3892 | 1a2eb2dc | Michael Hanselmann | |
3893 | 1a2eb2dc | Michael Hanselmann | if not status: |
3894 | 1a2eb2dc | Michael Hanselmann | return (False, value) |
3895 | 1a2eb2dc | Michael Hanselmann | |
3896 | 1a2eb2dc | Michael Hanselmann | if not stat.S_ISDIR(value.st_mode): |
3897 | 1a2eb2dc | Michael Hanselmann | return (False, "Path '%s' is not a directory" % path) |
3898 | 1a2eb2dc | Michael Hanselmann | |
3899 | 1a2eb2dc | Michael Hanselmann | return (True, None) |
3900 | 1a2eb2dc | Michael Hanselmann | |
3901 | 1a2eb2dc | Michael Hanselmann | |
3902 | 405bffe2 | Michael Hanselmann | def _VerifyRestrictedCmd(path, cmd, _owner=None): |
3903 | 45bc4635 | Iustin Pop | """Verifies a whole restricted command and returns its executable filename.
|
3904 | 1a2eb2dc | Michael Hanselmann |
|
3905 | 1a2eb2dc | Michael Hanselmann | @type path: string
|
3906 | 45bc4635 | Iustin Pop | @param path: Directory containing restricted commands
|
3907 | 1a2eb2dc | Michael Hanselmann | @type cmd: string
|
3908 | 1a2eb2dc | Michael Hanselmann | @param cmd: Command name
|
3909 | 1a2eb2dc | Michael Hanselmann | @rtype: tuple; (boolean, string)
|
3910 | 1a2eb2dc | Michael Hanselmann | @return: The tuple's first element is the status; if C{False}, the second
|
3911 | 1a2eb2dc | Michael Hanselmann | element is an error message string, otherwise the second element is the
|
3912 | 1a2eb2dc | Michael Hanselmann | absolute path to the executable
|
3913 | 1a2eb2dc | Michael Hanselmann |
|
3914 | 1a2eb2dc | Michael Hanselmann | """
|
3915 | 1a2eb2dc | Michael Hanselmann | executable = utils.PathJoin(path, cmd) |
3916 | 1a2eb2dc | Michael Hanselmann | |
3917 | 405bffe2 | Michael Hanselmann | (status, msg) = _CommonRestrictedCmdCheck(executable, _owner) |
3918 | 1a2eb2dc | Michael Hanselmann | |
3919 | 1a2eb2dc | Michael Hanselmann | if not status: |
3920 | 1a2eb2dc | Michael Hanselmann | return (False, msg) |
3921 | 1a2eb2dc | Michael Hanselmann | |
3922 | 1a2eb2dc | Michael Hanselmann | if not utils.IsExecutable(executable): |
3923 | 1a2eb2dc | Michael Hanselmann | return (False, "access(2) thinks '%s' can't be executed" % executable) |
3924 | 1a2eb2dc | Michael Hanselmann | |
3925 | 1a2eb2dc | Michael Hanselmann | return (True, executable) |
3926 | 1a2eb2dc | Michael Hanselmann | |
3927 | 1a2eb2dc | Michael Hanselmann | |
3928 | 405bffe2 | Michael Hanselmann | def _PrepareRestrictedCmd(path, cmd, |
3929 | 405bffe2 | Michael Hanselmann | _verify_dir=_VerifyRestrictedCmdDirectory, |
3930 | 405bffe2 | Michael Hanselmann | _verify_name=_VerifyRestrictedCmdName, |
3931 | 405bffe2 | Michael Hanselmann | _verify_cmd=_VerifyRestrictedCmd): |
3932 | 45bc4635 | Iustin Pop | """Performs a number of tests on a restricted command.
|
3933 | 1a2eb2dc | Michael Hanselmann |
|
3934 | 1a2eb2dc | Michael Hanselmann | @type path: string
|
3935 | 45bc4635 | Iustin Pop | @param path: Directory containing restricted commands
|
3936 | 1a2eb2dc | Michael Hanselmann | @type cmd: string
|
3937 | 1a2eb2dc | Michael Hanselmann | @param cmd: Command name
|
3938 | 405bffe2 | Michael Hanselmann | @return: Same as L{_VerifyRestrictedCmd}
|
3939 | 1a2eb2dc | Michael Hanselmann |
|
3940 | 1a2eb2dc | Michael Hanselmann | """
|
3941 | 1a2eb2dc | Michael Hanselmann | # Verify the directory first
|
3942 | 1a2eb2dc | Michael Hanselmann | (status, msg) = _verify_dir(path) |
3943 | 1a2eb2dc | Michael Hanselmann | if status:
|
3944 | 1a2eb2dc | Michael Hanselmann | # Check command if everything was alright
|
3945 | 1a2eb2dc | Michael Hanselmann | (status, msg) = _verify_name(cmd) |
3946 | 1a2eb2dc | Michael Hanselmann | |
3947 | 1a2eb2dc | Michael Hanselmann | if not status: |
3948 | 1a2eb2dc | Michael Hanselmann | return (False, msg) |
3949 | 1a2eb2dc | Michael Hanselmann | |
3950 | 1a2eb2dc | Michael Hanselmann | # Check actual executable
|
3951 | 1a2eb2dc | Michael Hanselmann | return _verify_cmd(path, cmd)
|
3952 | 1a2eb2dc | Michael Hanselmann | |
3953 | 1a2eb2dc | Michael Hanselmann | |
3954 | 42bd26e8 | Michael Hanselmann | def RunRestrictedCmd(cmd, |
3955 | 1a2eb2dc | Michael Hanselmann | _lock_timeout=_RCMD_LOCK_TIMEOUT, |
3956 | 878c42ae | Michael Hanselmann | _lock_file=pathutils.RESTRICTED_COMMANDS_LOCK_FILE, |
3957 | 878c42ae | Michael Hanselmann | _path=pathutils.RESTRICTED_COMMANDS_DIR, |
3958 | 1a2eb2dc | Michael Hanselmann | _sleep_fn=time.sleep, |
3959 | 405bffe2 | Michael Hanselmann | _prepare_fn=_PrepareRestrictedCmd, |
3960 | 1a2eb2dc | Michael Hanselmann | _runcmd_fn=utils.RunCmd, |
3961 | 1fdeb284 | Michael Hanselmann | _enabled=constants.ENABLE_RESTRICTED_COMMANDS): |
3962 | 45bc4635 | Iustin Pop | """Executes a restricted command after performing strict tests.
|
3963 | 1a2eb2dc | Michael Hanselmann |
|
3964 | 1a2eb2dc | Michael Hanselmann | @type cmd: string
|
3965 | 1a2eb2dc | Michael Hanselmann | @param cmd: Command name
|
3966 | 1a2eb2dc | Michael Hanselmann | @rtype: string
|
3967 | 1a2eb2dc | Michael Hanselmann | @return: Command output
|
3968 | 1a2eb2dc | Michael Hanselmann | @raise RPCFail: In case of an error
|
3969 | 1a2eb2dc | Michael Hanselmann |
|
3970 | 1a2eb2dc | Michael Hanselmann | """
|
3971 | 45bc4635 | Iustin Pop | logging.info("Preparing to run restricted command '%s'", cmd)
|
3972 | 1a2eb2dc | Michael Hanselmann | |
3973 | 1a2eb2dc | Michael Hanselmann | if not _enabled: |
3974 | 45bc4635 | Iustin Pop | _Fail("Restricted commands disabled at configure time")
|
3975 | 1a2eb2dc | Michael Hanselmann | |
3976 | 1a2eb2dc | Michael Hanselmann | lock = None
|
3977 | 1a2eb2dc | Michael Hanselmann | try:
|
3978 | 1a2eb2dc | Michael Hanselmann | cmdresult = None
|
3979 | 1a2eb2dc | Michael Hanselmann | try:
|
3980 | 1a2eb2dc | Michael Hanselmann | lock = utils.FileLock.Open(_lock_file) |
3981 | 1a2eb2dc | Michael Hanselmann | lock.Exclusive(blocking=True, timeout=_lock_timeout)
|
3982 | 1a2eb2dc | Michael Hanselmann | |
3983 | 1a2eb2dc | Michael Hanselmann | (status, value) = _prepare_fn(_path, cmd) |
3984 | 1a2eb2dc | Michael Hanselmann | |
3985 | 1a2eb2dc | Michael Hanselmann | if status:
|
3986 | 1a2eb2dc | Michael Hanselmann | cmdresult = _runcmd_fn([value], env={}, reset_env=True,
|
3987 | 1a2eb2dc | Michael Hanselmann | postfork_fn=lambda _: lock.Unlock())
|
3988 | 1a2eb2dc | Michael Hanselmann | else:
|
3989 | 1a2eb2dc | Michael Hanselmann | logging.error(value) |
3990 | 1a2eb2dc | Michael Hanselmann | except Exception: # pylint: disable=W0703 |
3991 | 1a2eb2dc | Michael Hanselmann | # Keep original error in log
|
3992 | 1a2eb2dc | Michael Hanselmann | logging.exception("Caught exception")
|
3993 | 1a2eb2dc | Michael Hanselmann | |
3994 | 1a2eb2dc | Michael Hanselmann | if cmdresult is None: |
3995 | 1a2eb2dc | Michael Hanselmann | logging.info("Sleeping for %0.1f seconds before returning",
|
3996 | 1a2eb2dc | Michael Hanselmann | _RCMD_INVALID_DELAY) |
3997 | 1a2eb2dc | Michael Hanselmann | _sleep_fn(_RCMD_INVALID_DELAY) |
3998 | 1a2eb2dc | Michael Hanselmann | |
3999 | 1a2eb2dc | Michael Hanselmann | # Do not include original error message in returned error
|
4000 | 1a2eb2dc | Michael Hanselmann | _Fail("Executing command '%s' failed" % cmd)
|
4001 | 1a2eb2dc | Michael Hanselmann | elif cmdresult.failed or cmdresult.fail_reason: |
4002 | 45bc4635 | Iustin Pop | _Fail("Restricted command '%s' failed: %s; output: %s",
|
4003 | 1a2eb2dc | Michael Hanselmann | cmd, cmdresult.fail_reason, cmdresult.output) |
4004 | 1a2eb2dc | Michael Hanselmann | else:
|
4005 | 1a2eb2dc | Michael Hanselmann | return cmdresult.output
|
4006 | 1a2eb2dc | Michael Hanselmann | finally:
|
4007 | 1a2eb2dc | Michael Hanselmann | if lock is not None: |
4008 | 1a2eb2dc | Michael Hanselmann | # Release lock at last
|
4009 | 1a2eb2dc | Michael Hanselmann | lock.Close() |
4010 | 1a2eb2dc | Michael Hanselmann | lock = None
|
4011 | 1a2eb2dc | Michael Hanselmann | |
4012 | 1a2eb2dc | Michael Hanselmann | |
4013 | 99e222b1 | Michael Hanselmann | def SetWatcherPause(until, _filename=pathutils.WATCHER_PAUSEFILE): |
4014 | 99e222b1 | Michael Hanselmann | """Creates or removes the watcher pause file.
|
4015 | 99e222b1 | Michael Hanselmann |
|
4016 | 99e222b1 | Michael Hanselmann | @type until: None or number
|
4017 | 99e222b1 | Michael Hanselmann | @param until: Unix timestamp saying until when the watcher shouldn't run
|
4018 | 99e222b1 | Michael Hanselmann |
|
4019 | 99e222b1 | Michael Hanselmann | """
|
4020 | 99e222b1 | Michael Hanselmann | if until is None: |
4021 | 99e222b1 | Michael Hanselmann | logging.info("Received request to no longer pause watcher")
|
4022 | 99e222b1 | Michael Hanselmann | utils.RemoveFile(_filename) |
4023 | 99e222b1 | Michael Hanselmann | else:
|
4024 | 99e222b1 | Michael Hanselmann | logging.info("Received request to pause watcher until %s", until)
|
4025 | 99e222b1 | Michael Hanselmann | |
4026 | 99e222b1 | Michael Hanselmann | if not ht.TNumber(until): |
4027 | 99e222b1 | Michael Hanselmann | _Fail("Duration must be numeric")
|
4028 | 99e222b1 | Michael Hanselmann | |
4029 | 99e222b1 | Michael Hanselmann | utils.WriteFile(_filename, data="%d\n" % (until, ), mode=0644) |
4030 | 99e222b1 | Michael Hanselmann | |
4031 | 99e222b1 | Michael Hanselmann | |
4032 | a8083063 | Iustin Pop | class HooksRunner(object): |
4033 | a8083063 | Iustin Pop | """Hook runner.
|
4034 | a8083063 | Iustin Pop |
|
4035 | 10c2650b | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not
|
4036 | 10c2650b | Iustin Pop | on the master side.
|
4037 | a8083063 | Iustin Pop |
|
4038 | a8083063 | Iustin Pop | """
|
4039 | a8083063 | Iustin Pop | def __init__(self, hooks_base_dir=None): |
4040 | a8083063 | Iustin Pop | """Constructor for hooks runner.
|
4041 | a8083063 | Iustin Pop |
|
4042 | 10c2650b | Iustin Pop | @type hooks_base_dir: str or None
|
4043 | 10c2650b | Iustin Pop | @param hooks_base_dir: if not None, this overrides the
|
4044 | 3329f4de | Michael Hanselmann | L{pathutils.HOOKS_BASE_DIR} (useful for unittests)
|
4045 | a8083063 | Iustin Pop |
|
4046 | a8083063 | Iustin Pop | """
|
4047 | a8083063 | Iustin Pop | if hooks_base_dir is None: |
4048 | 710f30ec | Michael Hanselmann | hooks_base_dir = pathutils.HOOKS_BASE_DIR |
4049 | fe267188 | Iustin Pop | # yeah, _BASE_DIR is not valid for attributes, we use it like a
|
4050 | fe267188 | Iustin Pop | # constant
|
4051 | b459a848 | Andrea Spadaccini | self._BASE_DIR = hooks_base_dir # pylint: disable=C0103 |
4052 | a8083063 | Iustin Pop | |
4053 | 0fa481f5 | Andrea Spadaccini | def RunLocalHooks(self, node_list, hpath, phase, env): |
4054 | 0fa481f5 | Andrea Spadaccini | """Check that the hooks will be run only locally and then run them.
|
4055 | 0fa481f5 | Andrea Spadaccini |
|
4056 | 0fa481f5 | Andrea Spadaccini | """
|
4057 | 0fa481f5 | Andrea Spadaccini | assert len(node_list) == 1 |
4058 | 0fa481f5 | Andrea Spadaccini | node = node_list[0]
|
4059 | 0fa481f5 | Andrea Spadaccini | _, myself = ssconf.GetMasterAndMyself() |
4060 | 0fa481f5 | Andrea Spadaccini | assert node == myself
|
4061 | 0fa481f5 | Andrea Spadaccini | |
4062 | 0fa481f5 | Andrea Spadaccini | results = self.RunHooks(hpath, phase, env)
|
4063 | 0fa481f5 | Andrea Spadaccini | |
4064 | 0fa481f5 | Andrea Spadaccini | # Return values in the form expected by HooksMaster
|
4065 | 0fa481f5 | Andrea Spadaccini | return {node: (None, False, results)} |
4066 | 0fa481f5 | Andrea Spadaccini | |
4067 | a8083063 | Iustin Pop | def RunHooks(self, hpath, phase, env): |
4068 | a8083063 | Iustin Pop | """Run the scripts in the hooks directory.
|
4069 | a8083063 | Iustin Pop |
|
4070 | 10c2650b | Iustin Pop | @type hpath: str
|
4071 | 10c2650b | Iustin Pop | @param hpath: the path to the hooks directory which
|
4072 | 10c2650b | Iustin Pop | holds the scripts
|
4073 | 10c2650b | Iustin Pop | @type phase: str
|
4074 | 10c2650b | Iustin Pop | @param phase: either L{constants.HOOKS_PHASE_PRE} or
|
4075 | 10c2650b | Iustin Pop | L{constants.HOOKS_PHASE_POST}
|
4076 | 10c2650b | Iustin Pop | @type env: dict
|
4077 | 10c2650b | Iustin Pop | @param env: dictionary with the environment for the hook
|
4078 | 10c2650b | Iustin Pop | @rtype: list
|
4079 | 10c2650b | Iustin Pop | @return: list of 3-element tuples:
|
4080 | 10c2650b | Iustin Pop | - script path
|
4081 | 10c2650b | Iustin Pop | - script result, either L{constants.HKR_SUCCESS} or
|
4082 | 10c2650b | Iustin Pop | L{constants.HKR_FAIL}
|
4083 | 10c2650b | Iustin Pop | - output of the script
|
4084 | 10c2650b | Iustin Pop |
|
4085 | 10c2650b | Iustin Pop | @raise errors.ProgrammerError: for invalid input
|
4086 | 10c2650b | Iustin Pop | parameters
|
4087 | a8083063 | Iustin Pop |
|
4088 | a8083063 | Iustin Pop | """
|
4089 | a8083063 | Iustin Pop | if phase == constants.HOOKS_PHASE_PRE:
|
4090 | a8083063 | Iustin Pop | suffix = "pre"
|
4091 | a8083063 | Iustin Pop | elif phase == constants.HOOKS_PHASE_POST:
|
4092 | a8083063 | Iustin Pop | suffix = "post"
|
4093 | a8083063 | Iustin Pop | else:
|
4094 | 3fb4f740 | Iustin Pop | _Fail("Unknown hooks phase '%s'", phase)
|
4095 | 3fb4f740 | Iustin Pop | |
4096 | a8083063 | Iustin Pop | subdir = "%s-%s.d" % (hpath, suffix)
|
4097 | 0411c011 | Iustin Pop | dir_name = utils.PathJoin(self._BASE_DIR, subdir)
|
4098 | 6bb65e3a | Guido Trotter | |
4099 | 6bb65e3a | Guido Trotter | results = [] |
4100 | a9b7e346 | Iustin Pop | |
4101 | a9b7e346 | Iustin Pop | if not os.path.isdir(dir_name): |
4102 | a9b7e346 | Iustin Pop | # for non-existing/non-dirs, we simply exit instead of logging a
|
4103 | a9b7e346 | Iustin Pop | # warning at every operation
|
4104 | a9b7e346 | Iustin Pop | return results
|
4105 | a9b7e346 | Iustin Pop | |
4106 | a9b7e346 | Iustin Pop | runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
|
4107 | a9b7e346 | Iustin Pop | |
4108 | 5ae4945a | Iustin Pop | for (relname, relstatus, runresult) in runparts_results: |
4109 | 6bb65e3a | Guido Trotter | if relstatus == constants.RUNPARTS_SKIP:
|
4110 | a8083063 | Iustin Pop | rrval = constants.HKR_SKIP |
4111 | a8083063 | Iustin Pop | output = ""
|
4112 | 6bb65e3a | Guido Trotter | elif relstatus == constants.RUNPARTS_ERR:
|
4113 | 6bb65e3a | Guido Trotter | rrval = constants.HKR_FAIL |
4114 | 6bb65e3a | Guido Trotter | output = "Hook script execution error: %s" % runresult
|
4115 | 6bb65e3a | Guido Trotter | elif relstatus == constants.RUNPARTS_RUN:
|
4116 | 6bb65e3a | Guido Trotter | if runresult.failed:
|
4117 | a8083063 | Iustin Pop | rrval = constants.HKR_FAIL |
4118 | a8083063 | Iustin Pop | else:
|
4119 | 6bb65e3a | Guido Trotter | rrval = constants.HKR_SUCCESS |
4120 | 6bb65e3a | Guido Trotter | output = utils.SafeEncode(runresult.output.strip()) |
4121 | 6bb65e3a | Guido Trotter | results.append(("%s/%s" % (subdir, relname), rrval, output))
|
4122 | 6bb65e3a | Guido Trotter | |
4123 | 6bb65e3a | Guido Trotter | return results
|
4124 | 3f78eef2 | Iustin Pop | |
4125 | 3f78eef2 | Iustin Pop | |
4126 | 8d528b7c | Iustin Pop | class IAllocatorRunner(object): |
4127 | 8d528b7c | Iustin Pop | """IAllocator runner.
|
4128 | 8d528b7c | Iustin Pop |
|
4129 | 8d528b7c | Iustin Pop | This class is instantiated on the node side (ganeti-noded) and not on
|
4130 | 8d528b7c | Iustin Pop | the master side.
|
4131 | 8d528b7c | Iustin Pop |
|
4132 | 8d528b7c | Iustin Pop | """
|
4133 | 7e950d31 | Iustin Pop | @staticmethod
|
4134 | 7e950d31 | Iustin Pop | def Run(name, idata): |
4135 | 8d528b7c | Iustin Pop | """Run an iallocator script.
|
4136 | 8d528b7c | Iustin Pop |
|
4137 | 10c2650b | Iustin Pop | @type name: str
|
4138 | 10c2650b | Iustin Pop | @param name: the iallocator script name
|
4139 | 10c2650b | Iustin Pop | @type idata: str
|
4140 | 10c2650b | Iustin Pop | @param idata: the allocator input data
|
4141 | 10c2650b | Iustin Pop |
|
4142 | 10c2650b | Iustin Pop | @rtype: tuple
|
4143 | 87f5c298 | Iustin Pop | @return: two element tuple of:
|
4144 | 87f5c298 | Iustin Pop | - status
|
4145 | 87f5c298 | Iustin Pop | - either error message or stdout of allocator (for success)
|
4146 | 8d528b7c | Iustin Pop |
|
4147 | 8d528b7c | Iustin Pop | """
|
4148 | 8d528b7c | Iustin Pop | alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH, |
4149 | 8d528b7c | Iustin Pop | os.path.isfile) |
4150 | 8d528b7c | Iustin Pop | if alloc_script is None: |
4151 | 87f5c298 | Iustin Pop | _Fail("iallocator module '%s' not found in the search path", name)
|
4152 | 8d528b7c | Iustin Pop | |
4153 | 8d528b7c | Iustin Pop | fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
|
4154 | 8d528b7c | Iustin Pop | try:
|
4155 | 8d528b7c | Iustin Pop | os.write(fd, idata) |
4156 | 8d528b7c | Iustin Pop | os.close(fd) |
4157 | 8d528b7c | Iustin Pop | result = utils.RunCmd([alloc_script, fin_name]) |
4158 | 8d528b7c | Iustin Pop | if result.failed:
|
4159 | 87f5c298 | Iustin Pop | _Fail("iallocator module '%s' failed: %s, output '%s'",
|
4160 | 87f5c298 | Iustin Pop | name, result.fail_reason, result.output) |
4161 | 8d528b7c | Iustin Pop | finally:
|
4162 | 8d528b7c | Iustin Pop | os.unlink(fin_name) |
4163 | 8d528b7c | Iustin Pop | |
4164 | c26a6bd2 | Iustin Pop | return result.stdout
|
4165 | 8d528b7c | Iustin Pop | |
4166 | 8d528b7c | Iustin Pop | |
4167 | 3f78eef2 | Iustin Pop | class DevCacheManager(object): |
4168 | c99a3cc0 | Manuel Franceschini | """Simple class for managing a cache of block device information.
|
4169 | 3f78eef2 | Iustin Pop |
|
4170 | 3f78eef2 | Iustin Pop | """
|
4171 | 3f78eef2 | Iustin Pop | _DEV_PREFIX = "/dev/"
|
4172 | 710f30ec | Michael Hanselmann | _ROOT_DIR = pathutils.BDEV_CACHE_DIR |
4173 | 3f78eef2 | Iustin Pop | |
4174 | 3f78eef2 | Iustin Pop | @classmethod
|
4175 | 3f78eef2 | Iustin Pop | def _ConvertPath(cls, dev_path): |
4176 | 3f78eef2 | Iustin Pop | """Converts a /dev/name path to the cache file name.
|
4177 | 3f78eef2 | Iustin Pop |
|
4178 | 3f78eef2 | Iustin Pop | This replaces slashes with underscores and strips the /dev
|
4179 | 10c2650b | Iustin Pop | prefix. It then returns the full path to the cache file.
|
4180 | 10c2650b | Iustin Pop |
|
4181 | 10c2650b | Iustin Pop | @type dev_path: str
|
4182 | 10c2650b | Iustin Pop | @param dev_path: the C{/dev/} path name
|
4183 | 10c2650b | Iustin Pop | @rtype: str
|
4184 | 10c2650b | Iustin Pop | @return: the converted path name
|
4185 | 3f78eef2 | Iustin Pop |
|
4186 | 3f78eef2 | Iustin Pop | """
|
4187 | 3f78eef2 | Iustin Pop | if dev_path.startswith(cls._DEV_PREFIX):
|
4188 | 3f78eef2 | Iustin Pop | dev_path = dev_path[len(cls._DEV_PREFIX):]
|
4189 | 3f78eef2 | Iustin Pop | dev_path = dev_path.replace("/", "_") |
4190 | 0411c011 | Iustin Pop | fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
|
4191 | 3f78eef2 | Iustin Pop | return fpath
|
4192 | 3f78eef2 | Iustin Pop | |
4193 | 3f78eef2 | Iustin Pop | @classmethod
|
4194 | 3f78eef2 | Iustin Pop | def UpdateCache(cls, dev_path, owner, on_primary, iv_name): |
4195 | 3f78eef2 | Iustin Pop | """Updates the cache information for a given device.
|
4196 | 3f78eef2 | Iustin Pop |
|
4197 | 10c2650b | Iustin Pop | @type dev_path: str
|
4198 | 10c2650b | Iustin Pop | @param dev_path: the pathname of the device
|
4199 | 10c2650b | Iustin Pop | @type owner: str
|
4200 | 10c2650b | Iustin Pop | @param owner: the owner (instance name) of the device
|
4201 | 10c2650b | Iustin Pop | @type on_primary: bool
|
4202 | 10c2650b | Iustin Pop | @param on_primary: whether this is the primary
|
4203 | 10c2650b | Iustin Pop | node nor not
|
4204 | 10c2650b | Iustin Pop | @type iv_name: str
|
4205 | 10c2650b | Iustin Pop | @param iv_name: the instance-visible name of the
|
4206 | c41eea6e | Iustin Pop | device, as in objects.Disk.iv_name
|
4207 | 10c2650b | Iustin Pop |
|
4208 | 10c2650b | Iustin Pop | @rtype: None
|
4209 | 10c2650b | Iustin Pop |
|
4210 | 3f78eef2 | Iustin Pop | """
|
4211 | cf5a8306 | Iustin Pop | if dev_path is None: |
4212 | 18682bca | Iustin Pop | logging.error("DevCacheManager.UpdateCache got a None dev_path")
|
4213 | cf5a8306 | Iustin Pop | return
|
4214 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
4215 | 3f78eef2 | Iustin Pop | if on_primary:
|
4216 | 3f78eef2 | Iustin Pop | state = "primary"
|
4217 | 3f78eef2 | Iustin Pop | else:
|
4218 | 3f78eef2 | Iustin Pop | state = "secondary"
|
4219 | 3f78eef2 | Iustin Pop | if iv_name is None: |
4220 | 3f78eef2 | Iustin Pop | iv_name = "not_visible"
|
4221 | 3f78eef2 | Iustin Pop | fdata = "%s %s %s\n" % (str(owner), state, iv_name) |
4222 | 3f78eef2 | Iustin Pop | try:
|
4223 | 3f78eef2 | Iustin Pop | utils.WriteFile(fpath, data=fdata) |
4224 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
4225 | 29921401 | Iustin Pop | logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
|
4226 | 3f78eef2 | Iustin Pop | |
4227 | 3f78eef2 | Iustin Pop | @classmethod
|
4228 | 3f78eef2 | Iustin Pop | def RemoveCache(cls, dev_path): |
4229 | 3f78eef2 | Iustin Pop | """Remove data for a dev_path.
|
4230 | 3f78eef2 | Iustin Pop |
|
4231 | 3865ca48 | Michael Hanselmann | This is just a wrapper over L{utils.io.RemoveFile} with a converted
|
4232 | 10c2650b | Iustin Pop | path name and logging.
|
4233 | 10c2650b | Iustin Pop |
|
4234 | 10c2650b | Iustin Pop | @type dev_path: str
|
4235 | 10c2650b | Iustin Pop | @param dev_path: the pathname of the device
|
4236 | 10c2650b | Iustin Pop |
|
4237 | 10c2650b | Iustin Pop | @rtype: None
|
4238 | 10c2650b | Iustin Pop |
|
4239 | 3f78eef2 | Iustin Pop | """
|
4240 | cf5a8306 | Iustin Pop | if dev_path is None: |
4241 | 18682bca | Iustin Pop | logging.error("DevCacheManager.RemoveCache got a None dev_path")
|
4242 | cf5a8306 | Iustin Pop | return
|
4243 | 3f78eef2 | Iustin Pop | fpath = cls._ConvertPath(dev_path) |
4244 | 3f78eef2 | Iustin Pop | try:
|
4245 | 3f78eef2 | Iustin Pop | utils.RemoveFile(fpath) |
4246 | 3f78eef2 | Iustin Pop | except EnvironmentError, err: |
4247 | 29921401 | Iustin Pop | logging.exception("Can't update bdev cache for %s: %s", dev_path, err) |