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