4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
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.
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.
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
22 """Functions used by the node daemon
24 @var _ALLOWED_UPLOAD_FILES: denotes which files are accepted in
25 the L{UploadFile} function
26 @var _ALLOWED_CLEAN_DIRS: denotes which directories are accepted
27 in the L{_CleanDirectory} function
31 # pylint: disable=E1103
33 # E1103: %s %r has no %r member (but some types could not be
34 # inferred), because the _TryOSFromDisk returns either (True, os_obj)
35 # or (False, "string") which confuses pylint
52 from ganeti import errors
53 from ganeti import utils
54 from ganeti import ssh
55 from ganeti import hypervisor
56 from ganeti import constants
57 from ganeti import bdev
58 from ganeti import objects
59 from ganeti import ssconf
60 from ganeti import serializer
61 from ganeti import netutils
62 from ganeti import runtime
65 _BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
66 _ALLOWED_CLEAN_DIRS = frozenset([
68 constants.JOB_QUEUE_ARCHIVE_DIR,
70 constants.CRYPTO_KEYS_DIR,
72 _MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
73 _X509_KEY_FILE = "key"
74 _X509_CERT_FILE = "cert"
75 _IES_STATUS_FILE = "status"
79 #: Valid LVS output line regex
80 _LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
83 class RPCFail(Exception):
84 """Class denoting RPC failure.
86 Its argument is the error message.
91 def _Fail(msg, *args, **kwargs):
92 """Log an error and the raise an RPCFail exception.
94 This exception is then handled specially in the ganeti daemon and
95 turned into a 'failed' return type. As such, this function is a
96 useful shortcut for logging the error and returning it to the master
100 @param msg: the text of the exception
106 if "log" not in kwargs or kwargs["log"]: # if we should log this error
107 if "exc" in kwargs and kwargs["exc"]:
108 logging.exception(msg)
115 """Simple wrapper to return a SimpleStore.
117 @rtype: L{ssconf.SimpleStore}
118 @return: a SimpleStore instance
121 return ssconf.SimpleStore()
124 def _GetSshRunner(cluster_name):
125 """Simple wrapper to return an SshRunner.
127 @type cluster_name: str
128 @param cluster_name: the cluster name, which is needed
129 by the SshRunner constructor
130 @rtype: L{ssh.SshRunner}
131 @return: an SshRunner instance
134 return ssh.SshRunner(cluster_name)
137 def _Decompress(data):
138 """Unpacks data compressed by the RPC client.
140 @type data: list or tuple
141 @param data: Data sent by RPC client
143 @return: Decompressed data
146 assert isinstance(data, (list, tuple))
147 assert len(data) == 2
148 (encoding, content) = data
149 if encoding == constants.RPC_ENCODING_NONE:
151 elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
152 return zlib.decompress(base64.b64decode(content))
154 raise AssertionError("Unknown data encoding")
157 def _CleanDirectory(path, exclude=None):
158 """Removes all regular files in a directory.
161 @param path: the directory to clean
163 @param exclude: list of files to be excluded, defaults
167 if path not in _ALLOWED_CLEAN_DIRS:
168 _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
171 if not os.path.isdir(path):
176 # Normalize excluded paths
177 exclude = [os.path.normpath(i) for i in exclude]
179 for rel_name in utils.ListVisibleFiles(path):
180 full_name = utils.PathJoin(path, rel_name)
181 if full_name in exclude:
183 if os.path.isfile(full_name) and not os.path.islink(full_name):
184 utils.RemoveFile(full_name)
187 def _BuildUploadFileList():
188 """Build the list of allowed upload files.
190 This is abstracted so that it's built only once at module import time.
193 allowed_files = set([
194 constants.CLUSTER_CONF_FILE,
196 constants.SSH_KNOWN_HOSTS_FILE,
197 constants.VNC_PASSWORD_FILE,
198 constants.RAPI_CERT_FILE,
199 constants.RAPI_USERS_FILE,
200 constants.CONFD_HMAC_KEY,
201 constants.CLUSTER_DOMAIN_SECRET_FILE,
204 for hv_name in constants.HYPER_TYPES:
205 hv_class = hypervisor.GetHypervisorClass(hv_name)
206 allowed_files.update(hv_class.GetAncillaryFiles()[0])
208 return frozenset(allowed_files)
211 _ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
215 """Removes job queue files and archived jobs.
221 _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
222 _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
226 """Returns master information.
228 This is an utility function to compute master information, either
229 for consumption here or from the node daemon.
232 @return: master_netdev, master_ip, master_name, primary_ip_family
233 @raise RPCFail: in case of errors
238 master_netdev = cfg.GetMasterNetdev()
239 master_ip = cfg.GetMasterIP()
240 master_node = cfg.GetMasterNode()
241 primary_ip_family = cfg.GetPrimaryIPFamily()
242 except errors.ConfigurationError, err:
243 _Fail("Cluster configuration incomplete: %s", err, exc=True)
244 return (master_netdev, master_ip, master_node, primary_ip_family)
247 def ActivateMasterIp():
248 """Activate the IP address of the master daemon.
251 # GetMasterInfo will raise an exception if not able to return data
252 master_netdev, master_ip, _, family = GetMasterInfo()
255 if netutils.TcpPing(master_ip, constants.DEFAULT_NODED_PORT):
256 if netutils.IPAddress.Own(master_ip):
257 # we already have the ip:
258 logging.debug("Master IP already configured, doing nothing")
260 err_msg = "Someone else has the master ip, not activating"
261 logging.error(err_msg)
263 ipcls = netutils.IP4Address
264 if family == netutils.IP6Address.family:
265 ipcls = netutils.IP6Address
267 result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
268 "%s/%d" % (master_ip, ipcls.iplen),
269 "dev", master_netdev, "label",
270 "%s:0" % master_netdev])
272 err_msg = "Can't activate master IP: %s" % result.output
273 logging.error(err_msg)
275 # we ignore the exit code of the following cmds
276 if ipcls == netutils.IP4Address:
277 utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev, "-s",
278 master_ip, master_ip])
279 elif ipcls == netutils.IP6Address:
281 utils.RunCmd(["ndisc6", "-q", "-r 3", master_ip, master_netdev])
282 except errors.OpExecError:
283 # TODO: Better error reporting
284 logging.warning("Can't execute ndisc6, please install if missing")
290 def StartMasterDaemons(no_voting):
291 """Activate local node as master node.
293 The function will start the master daemons (ganeti-masterd and ganeti-rapi).
295 @type no_voting: boolean
296 @param no_voting: whether to start ganeti-masterd without a node vote
297 but still non-interactively
303 masterd_args = "--no-voting --yes-do-it"
308 "EXTRA_MASTERD_ARGS": masterd_args,
311 result = utils.RunCmd([constants.DAEMON_UTIL, "start-master"], env=env)
313 msg = "Can't start Ganeti master: %s" % result.output
318 def DeactivateMasterIp():
319 """Deactivate the master IP on this node.
322 # TODO: log and report back to the caller the error failures; we
323 # need to decide in which case we fail the RPC for this
325 # GetMasterInfo will raise an exception if not able to return data
326 master_netdev, master_ip, _, family = GetMasterInfo()
328 ipcls = netutils.IP4Address
329 if family == netutils.IP6Address.family:
330 ipcls = netutils.IP6Address
332 result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
333 "%s/%d" % (master_ip, ipcls.iplen),
334 "dev", master_netdev])
336 logging.error("Can't remove the master IP, error: %s", result.output)
337 # but otherwise ignore the failure
340 def StopMasterDaemons():
341 """Stop the master daemons on this node.
343 Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
348 # TODO: log and report back to the caller the error failures; we
349 # need to decide in which case we fail the RPC for this
351 result = utils.RunCmd([constants.DAEMON_UTIL, "stop-master"])
353 logging.error("Could not stop Ganeti master, command %s had exitcode %s"
355 result.cmd, result.exit_code, result.output)
358 def EtcHostsModify(mode, host, ip):
359 """Modify a host entry in /etc/hosts.
361 @param mode: The mode to operate. Either add or remove entry
362 @param host: The host to operate on
363 @param ip: The ip associated with the entry
366 if mode == constants.ETC_HOSTS_ADD:
368 RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
370 utils.AddHostToEtcHosts(host, ip)
371 elif mode == constants.ETC_HOSTS_REMOVE:
373 RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
374 " parameter is present")
375 utils.RemoveHostFromEtcHosts(host)
377 RPCFail("Mode not supported")
380 def LeaveCluster(modify_ssh_setup):
381 """Cleans up and remove the current node.
383 This function cleans up and prepares the current node to be removed
386 If processing is successful, then it raises an
387 L{errors.QuitGanetiException} which is used as a special case to
388 shutdown the node daemon.
390 @param modify_ssh_setup: boolean
393 _CleanDirectory(constants.DATA_DIR)
394 _CleanDirectory(constants.CRYPTO_KEYS_DIR)
399 priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
401 utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
403 utils.RemoveFile(priv_key)
404 utils.RemoveFile(pub_key)
405 except errors.OpExecError:
406 logging.exception("Error while processing ssh files")
409 utils.RemoveFile(constants.CONFD_HMAC_KEY)
410 utils.RemoveFile(constants.RAPI_CERT_FILE)
411 utils.RemoveFile(constants.NODED_CERT_FILE)
412 except: # pylint: disable=W0702
413 logging.exception("Error while removing cluster secrets")
415 result = utils.RunCmd([constants.DAEMON_UTIL, "stop", constants.CONFD])
417 logging.error("Command %s failed with exitcode %s and error %s",
418 result.cmd, result.exit_code, result.output)
420 # Raise a custom exception (handled in ganeti-noded)
421 raise errors.QuitGanetiException(True, "Shutdown scheduled")
424 def GetNodeInfo(vgname, hypervisor_type):
425 """Gives back a hash with different information about the node.
427 @type vgname: C{string}
428 @param vgname: the name of the volume group to ask for disk space information
429 @type hypervisor_type: C{str}
430 @param hypervisor_type: the name of the hypervisor to ask for
433 @return: dictionary with the following keys:
434 - vg_size is the size of the configured volume group in MiB
435 - vg_free is the free size of the volume group in MiB
436 - memory_dom0 is the memory allocated for domain0 in MiB
437 - memory_free is the currently available (free) ram in MiB
438 - memory_total is the total number of ram in MiB
439 - hv_version: the hypervisor version, if available
444 if vgname is not None:
445 vginfo = bdev.LogicalVolume.GetVGInfo([vgname])
446 vg_free = vg_size = None
448 vg_free = int(round(vginfo[0][0], 0))
449 vg_size = int(round(vginfo[0][1], 0))
450 outputarray["vg_size"] = vg_size
451 outputarray["vg_free"] = vg_free
453 if hypervisor_type is not None:
454 hyper = hypervisor.GetHypervisor(hypervisor_type)
455 hyp_info = hyper.GetNodeInfo()
456 if hyp_info is not None:
457 outputarray.update(hyp_info)
459 outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
464 def VerifyNode(what, cluster_name):
465 """Verify the status of the local node.
467 Based on the input L{what} parameter, various checks are done on the
470 If the I{filelist} key is present, this list of
471 files is checksummed and the file/checksum pairs are returned.
473 If the I{nodelist} key is present, we check that we have
474 connectivity via ssh with the target nodes (and check the hostname
477 If the I{node-net-test} key is present, we check that we have
478 connectivity to the given nodes via both primary IP and, if
479 applicable, secondary IPs.
482 @param what: a dictionary of things to check:
483 - filelist: list of files for which to compute checksums
484 - nodelist: list of nodes we should check ssh communication with
485 - node-net-test: list of nodes we should check node daemon port
487 - hypervisor: list with hypervisors to run the verify for
489 @return: a dictionary with the same keys as the input dict, and
490 values representing the result of the checks
494 my_name = netutils.Hostname.GetSysName()
495 port = netutils.GetDaemonPort(constants.NODED)
496 vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
498 if constants.NV_HYPERVISOR in what and vm_capable:
499 result[constants.NV_HYPERVISOR] = tmp = {}
500 for hv_name in what[constants.NV_HYPERVISOR]:
502 val = hypervisor.GetHypervisor(hv_name).Verify()
503 except errors.HypervisorError, err:
504 val = "Error while checking hypervisor: %s" % str(err)
507 if constants.NV_HVPARAMS in what and vm_capable:
508 result[constants.NV_HVPARAMS] = tmp = []
509 for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
511 logging.info("Validating hv %s, %s", hv_name, hvparms)
512 hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms)
513 except errors.HypervisorError, err:
514 tmp.append((source, hv_name, str(err)))
516 if constants.NV_FILELIST in what:
517 result[constants.NV_FILELIST] = utils.FingerprintFiles(
518 what[constants.NV_FILELIST])
520 if constants.NV_NODELIST in what:
521 (nodes, bynode) = what[constants.NV_NODELIST]
523 # Add nodes from other groups (different for each node)
525 nodes.extend(bynode[my_name])
530 random.shuffle(nodes)
532 # Try to contact all nodes
535 success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
539 result[constants.NV_NODELIST] = val
541 if constants.NV_NODENETTEST in what:
542 result[constants.NV_NODENETTEST] = tmp = {}
543 my_pip = my_sip = None
544 for name, pip, sip in what[constants.NV_NODENETTEST]:
550 tmp[my_name] = ("Can't find my own primary/secondary IP"
553 for name, pip, sip in what[constants.NV_NODENETTEST]:
555 if not netutils.TcpPing(pip, port, source=my_pip):
556 fail.append("primary")
558 if not netutils.TcpPing(sip, port, source=my_sip):
559 fail.append("secondary")
561 tmp[name] = ("failure using the %s interface(s)" %
564 if constants.NV_MASTERIP in what:
565 # FIXME: add checks on incoming data structures (here and in the
566 # rest of the function)
567 master_name, master_ip = what[constants.NV_MASTERIP]
568 if master_name == my_name:
569 source = constants.IP4_ADDRESS_LOCALHOST
572 result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
575 if constants.NV_OOB_PATHS in what:
576 result[constants.NV_OOB_PATHS] = tmp = []
577 for path in what[constants.NV_OOB_PATHS]:
581 tmp.append("error stating out of band helper: %s" % err)
583 if stat.S_ISREG(st.st_mode):
584 if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
587 tmp.append("out of band helper %s is not executable" % path)
589 tmp.append("out of band helper %s is not a file" % path)
591 if constants.NV_LVLIST in what and vm_capable:
593 val = GetVolumeList(utils.ListVolumeGroups().keys())
596 result[constants.NV_LVLIST] = val
598 if constants.NV_INSTANCELIST in what and vm_capable:
599 # GetInstanceList can fail
601 val = GetInstanceList(what[constants.NV_INSTANCELIST])
604 result[constants.NV_INSTANCELIST] = val
606 if constants.NV_VGLIST in what and vm_capable:
607 result[constants.NV_VGLIST] = utils.ListVolumeGroups()
609 if constants.NV_PVLIST in what and vm_capable:
610 result[constants.NV_PVLIST] = \
611 bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
612 filter_allocatable=False)
614 if constants.NV_VERSION in what:
615 result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
616 constants.RELEASE_VERSION)
618 if constants.NV_HVINFO in what and vm_capable:
619 hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
620 result[constants.NV_HVINFO] = hyper.GetNodeInfo()
622 if constants.NV_DRBDLIST in what and vm_capable:
624 used_minors = bdev.DRBD8.GetUsedDevs().keys()
625 except errors.BlockDeviceError, err:
626 logging.warning("Can't get used minors list", exc_info=True)
627 used_minors = str(err)
628 result[constants.NV_DRBDLIST] = used_minors
630 if constants.NV_DRBDHELPER in what and vm_capable:
633 payload = bdev.BaseDRBD.GetUsermodeHelper()
634 except errors.BlockDeviceError, err:
635 logging.error("Can't get DRBD usermode helper: %s", str(err))
638 result[constants.NV_DRBDHELPER] = (status, payload)
640 if constants.NV_NODESETUP in what:
641 result[constants.NV_NODESETUP] = tmpr = []
642 if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
643 tmpr.append("The sysfs filesytem doesn't seem to be mounted"
644 " under /sys, missing required directories /sys/block"
645 " and /sys/class/net")
646 if (not os.path.isdir("/proc/sys") or
647 not os.path.isfile("/proc/sysrq-trigger")):
648 tmpr.append("The procfs filesystem doesn't seem to be mounted"
649 " under /proc, missing required directory /proc/sys and"
650 " the file /proc/sysrq-trigger")
652 if constants.NV_TIME in what:
653 result[constants.NV_TIME] = utils.SplitTime(time.time())
655 if constants.NV_OSLIST in what and vm_capable:
656 result[constants.NV_OSLIST] = DiagnoseOS()
658 if constants.NV_BRIDGES in what and vm_capable:
659 result[constants.NV_BRIDGES] = [bridge
660 for bridge in what[constants.NV_BRIDGES]
661 if not utils.BridgeExists(bridge)]
665 def GetBlockDevSizes(devices):
666 """Return the size of the given block devices
669 @param devices: list of block device nodes to query
672 dictionary of all block devices under /dev (key). The value is their
675 {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
681 for devpath in devices:
682 if not utils.IsBelowDir(DEV_PREFIX, devpath):
686 st = os.stat(devpath)
687 except EnvironmentError, err:
688 logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
691 if stat.S_ISBLK(st.st_mode):
692 result = utils.RunCmd(["blockdev", "--getsize64", devpath])
694 # We don't want to fail, just do not list this device as available
695 logging.warning("Cannot get size for block device %s", devpath)
698 size = int(result.stdout) / (1024 * 1024)
699 blockdevs[devpath] = size
703 def GetVolumeList(vg_names):
704 """Compute list of logical volumes and their size.
707 @param vg_names: the volume groups whose LVs we should list, or
708 empty for all volume groups
711 dictionary of all partions (key) with value being a tuple of
712 their size (in MiB), inactive and online status::
714 {'xenvg/test1': ('20.06', True, True)}
716 in case of errors, a string is returned with the error
724 result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
725 "--separator=%s" % sep,
726 "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
728 _Fail("Failed to list logical volumes, lvs output: %s", result.output)
730 for line in result.stdout.splitlines():
732 match = _LVSLINE_REGEX.match(line)
734 logging.error("Invalid line returned from lvs output: '%s'", line)
736 vg_name, name, size, attr = match.groups()
737 inactive = attr[4] == "-"
738 online = attr[5] == "o"
739 virtual = attr[0] == "v"
741 # we don't want to report such volumes as existing, since they
742 # don't really hold data
744 lvs[vg_name + "/" + name] = (size, inactive, online)
749 def ListVolumeGroups():
750 """List the volume groups and their size.
753 @return: dictionary with keys volume name and values the
757 return utils.ListVolumeGroups()
761 """List all volumes on this node.
765 A list of dictionaries, each having four keys:
766 - name: the logical volume name,
767 - size: the size of the logical volume
768 - dev: the physical device on which the LV lives
769 - vg: the volume group to which it belongs
771 In case of errors, we return an empty list and log the
774 Note that since a logical volume can live on multiple physical
775 volumes, the resulting list might include a logical volume
779 result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
781 "--options=lv_name,lv_size,devices,vg_name"])
783 _Fail("Failed to list logical volumes, lvs output: %s",
787 return dev.split("(")[0]
790 return [parse_dev(x) for x in dev.split(",")]
793 line = [v.strip() for v in line]
794 return [{"name": line[0], "size": line[1],
795 "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
798 for line in result.stdout.splitlines():
799 if line.count("|") >= 3:
800 all_devs.extend(map_line(line.split("|")))
802 logging.warning("Strange line in the output from lvs: '%s'", line)
806 def BridgesExist(bridges_list):
807 """Check if a list of bridges exist on the current node.
810 @return: C{True} if all of them exist, C{False} otherwise
814 for bridge in bridges_list:
815 if not utils.BridgeExists(bridge):
816 missing.append(bridge)
819 _Fail("Missing bridges %s", utils.CommaJoin(missing))
822 def GetInstanceList(hypervisor_list):
823 """Provides a list of instances.
825 @type hypervisor_list: list
826 @param hypervisor_list: the list of hypervisors to query information
829 @return: a list of all running instances on the current node
830 - instance1.example.com
831 - instance2.example.com
835 for hname in hypervisor_list:
837 names = hypervisor.GetHypervisor(hname).ListInstances()
838 results.extend(names)
839 except errors.HypervisorError, err:
840 _Fail("Error enumerating instances (hypervisor %s): %s",
841 hname, err, exc=True)
846 def GetInstanceInfo(instance, hname):
847 """Gives back the information about an instance as a dictionary.
849 @type instance: string
850 @param instance: the instance name
852 @param hname: the hypervisor type of the instance
855 @return: dictionary with the following keys:
856 - memory: memory size of instance (int)
857 - state: xen state of instance (string)
858 - time: cpu time of instance (float)
863 iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
864 if iinfo is not None:
865 output["memory"] = iinfo[2]
866 output["state"] = iinfo[4]
867 output["time"] = iinfo[5]
872 def GetInstanceMigratable(instance):
873 """Gives whether an instance can be migrated.
875 @type instance: L{objects.Instance}
876 @param instance: object representing the instance to be checked.
879 @return: tuple of (result, description) where:
880 - result: whether the instance can be migrated or not
881 - description: a description of the issue, if relevant
884 hyper = hypervisor.GetHypervisor(instance.hypervisor)
885 iname = instance.name
886 if iname not in hyper.ListInstances():
887 _Fail("Instance %s is not running", iname)
889 for idx in range(len(instance.disks)):
890 link_name = _GetBlockDevSymlinkPath(iname, idx)
891 if not os.path.islink(link_name):
892 logging.warning("Instance %s is missing symlink %s for disk %d",
893 iname, link_name, idx)
896 def GetAllInstancesInfo(hypervisor_list):
897 """Gather data about all instances.
899 This is the equivalent of L{GetInstanceInfo}, except that it
900 computes data for all instances at once, thus being faster if one
901 needs data about more than one instance.
903 @type hypervisor_list: list
904 @param hypervisor_list: list of hypervisors to query for instance data
907 @return: dictionary of instance: data, with data having the following keys:
908 - memory: memory size of instance (int)
909 - state: xen state of instance (string)
910 - time: cpu time of instance (float)
911 - vcpus: the number of vcpus
916 for hname in hypervisor_list:
917 iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
919 for name, _, memory, vcpus, state, times in iinfo:
927 # we only check static parameters, like memory and vcpus,
928 # and not state and time which can change between the
929 # invocations of the different hypervisors
930 for key in "memory", "vcpus":
931 if value[key] != output[name][key]:
932 _Fail("Instance %s is running twice"
933 " with different parameters", name)
939 def _InstanceLogName(kind, os_name, instance, component):
940 """Compute the OS log filename for a given instance and operation.
942 The instance name and os name are passed in as strings since not all
943 operations have these as part of an instance object.
946 @param kind: the operation type (e.g. add, import, etc.)
947 @type os_name: string
948 @param os_name: the os name
949 @type instance: string
950 @param instance: the name of the instance being imported/added/etc.
951 @type component: string or None
952 @param component: the name of the component of the instance being
956 # TODO: Use tempfile.mkstemp to create unique filename
958 assert "/" not in component
959 c_msg = "-%s" % component
962 base = ("%s-%s-%s%s-%s.log" %
963 (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
964 return utils.PathJoin(constants.LOG_OS_DIR, base)
967 def InstanceOsAdd(instance, reinstall, debug):
968 """Add an OS to an instance.
970 @type instance: L{objects.Instance}
971 @param instance: Instance whose OS is to be installed
972 @type reinstall: boolean
973 @param reinstall: whether this is an instance reinstall
975 @param debug: debug level, passed to the OS scripts
979 inst_os = OSFromDisk(instance.os)
981 create_env = OSEnvironment(instance, inst_os, debug)
983 create_env["INSTANCE_REINSTALL"] = "1"
985 logfile = _InstanceLogName("add", instance.os, instance.name, None)
987 result = utils.RunCmd([inst_os.create_script], env=create_env,
988 cwd=inst_os.path, output=logfile, reset_env=True)
990 logging.error("os create command '%s' returned error: %s, logfile: %s,"
991 " output: %s", result.cmd, result.fail_reason, logfile,
993 lines = [utils.SafeEncode(val)
994 for val in utils.TailFile(logfile, lines=20)]
995 _Fail("OS create script failed (%s), last lines in the"
996 " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
999 def RunRenameInstance(instance, old_name, debug):
1000 """Run the OS rename script for an instance.
1002 @type instance: L{objects.Instance}
1003 @param instance: Instance whose OS is to be installed
1004 @type old_name: string
1005 @param old_name: previous instance name
1006 @type debug: integer
1007 @param debug: debug level, passed to the OS scripts
1009 @return: the success of the operation
1012 inst_os = OSFromDisk(instance.os)
1014 rename_env = OSEnvironment(instance, inst_os, debug)
1015 rename_env["OLD_INSTANCE_NAME"] = old_name
1017 logfile = _InstanceLogName("rename", instance.os,
1018 "%s-%s" % (old_name, instance.name), None)
1020 result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1021 cwd=inst_os.path, output=logfile, reset_env=True)
1024 logging.error("os create command '%s' returned error: %s output: %s",
1025 result.cmd, result.fail_reason, result.output)
1026 lines = [utils.SafeEncode(val)
1027 for val in utils.TailFile(logfile, lines=20)]
1028 _Fail("OS rename script failed (%s), last lines in the"
1029 " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1032 def _GetBlockDevSymlinkPath(instance_name, idx):
1033 return utils.PathJoin(constants.DISK_LINKS_DIR, "%s%s%d" %
1034 (instance_name, constants.DISK_SEPARATOR, idx))
1037 def _SymlinkBlockDev(instance_name, device_path, idx):
1038 """Set up symlinks to a instance's block device.
1040 This is an auxiliary function run when an instance is start (on the primary
1041 node) or when an instance is migrated (on the target node).
1044 @param instance_name: the name of the target instance
1045 @param device_path: path of the physical block device, on the node
1046 @param idx: the disk index
1047 @return: absolute path to the disk's symlink
1050 link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1052 os.symlink(device_path, link_name)
1053 except OSError, err:
1054 if err.errno == errno.EEXIST:
1055 if (not os.path.islink(link_name) or
1056 os.readlink(link_name) != device_path):
1057 os.remove(link_name)
1058 os.symlink(device_path, link_name)
1065 def _RemoveBlockDevLinks(instance_name, disks):
1066 """Remove the block device symlinks belonging to the given instance.
1069 for idx, _ in enumerate(disks):
1070 link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1071 if os.path.islink(link_name):
1073 os.remove(link_name)
1075 logging.exception("Can't remove symlink '%s'", link_name)
1078 def _GatherAndLinkBlockDevs(instance):
1079 """Set up an instance's block device(s).
1081 This is run on the primary node at instance startup. The block
1082 devices must be already assembled.
1084 @type instance: L{objects.Instance}
1085 @param instance: the instance whose disks we shoul assemble
1087 @return: list of (disk_object, device_path)
1091 for idx, disk in enumerate(instance.disks):
1092 device = _RecursiveFindBD(disk)
1094 raise errors.BlockDeviceError("Block device '%s' is not set up." %
1098 link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1100 raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1103 block_devices.append((disk, link_name))
1105 return block_devices
1108 def StartInstance(instance, startup_paused):
1109 """Start an instance.
1111 @type instance: L{objects.Instance}
1112 @param instance: the instance object
1113 @type startup_paused: bool
1114 @param instance: pause instance at startup?
1118 running_instances = GetInstanceList([instance.hypervisor])
1120 if instance.name in running_instances:
1121 logging.info("Instance %s already running, not starting", instance.name)
1125 block_devices = _GatherAndLinkBlockDevs(instance)
1126 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1127 hyper.StartInstance(instance, block_devices, startup_paused)
1128 except errors.BlockDeviceError, err:
1129 _Fail("Block device error: %s", err, exc=True)
1130 except errors.HypervisorError, err:
1131 _RemoveBlockDevLinks(instance.name, instance.disks)
1132 _Fail("Hypervisor error: %s", err, exc=True)
1135 def InstanceShutdown(instance, timeout):
1136 """Shut an instance down.
1138 @note: this functions uses polling with a hardcoded timeout.
1140 @type instance: L{objects.Instance}
1141 @param instance: the instance object
1142 @type timeout: integer
1143 @param timeout: maximum timeout for soft shutdown
1147 hv_name = instance.hypervisor
1148 hyper = hypervisor.GetHypervisor(hv_name)
1149 iname = instance.name
1151 if instance.name not in hyper.ListInstances():
1152 logging.info("Instance %s not running, doing nothing", iname)
1157 self.tried_once = False
1160 if iname not in hyper.ListInstances():
1164 hyper.StopInstance(instance, retry=self.tried_once)
1165 except errors.HypervisorError, err:
1166 if iname not in hyper.ListInstances():
1167 # if the instance is no longer existing, consider this a
1168 # success and go to cleanup
1171 _Fail("Failed to stop instance %s: %s", iname, err)
1173 self.tried_once = True
1175 raise utils.RetryAgain()
1178 utils.Retry(_TryShutdown(), 5, timeout)
1179 except utils.RetryTimeout:
1180 # the shutdown did not succeed
1181 logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1184 hyper.StopInstance(instance, force=True)
1185 except errors.HypervisorError, err:
1186 if iname in hyper.ListInstances():
1187 # only raise an error if the instance still exists, otherwise
1188 # the error could simply be "instance ... unknown"!
1189 _Fail("Failed to force stop instance %s: %s", iname, err)
1193 if iname in hyper.ListInstances():
1194 _Fail("Could not shutdown instance %s even by destroy", iname)
1197 hyper.CleanupInstance(instance.name)
1198 except errors.HypervisorError, err:
1199 logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1201 _RemoveBlockDevLinks(iname, instance.disks)
1204 def InstanceReboot(instance, reboot_type, shutdown_timeout):
1205 """Reboot an instance.
1207 @type instance: L{objects.Instance}
1208 @param instance: the instance object to reboot
1209 @type reboot_type: str
1210 @param reboot_type: the type of reboot, one the following
1212 - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1213 instance OS, do not recreate the VM
1214 - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1215 restart the VM (at the hypervisor level)
1216 - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1217 not accepted here, since that mode is handled differently, in
1218 cmdlib, and translates into full stop and start of the
1219 instance (instead of a call_instance_reboot RPC)
1220 @type shutdown_timeout: integer
1221 @param shutdown_timeout: maximum timeout for soft shutdown
1225 running_instances = GetInstanceList([instance.hypervisor])
1227 if instance.name not in running_instances:
1228 _Fail("Cannot reboot instance %s that is not running", instance.name)
1230 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1231 if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1233 hyper.RebootInstance(instance)
1234 except errors.HypervisorError, err:
1235 _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1236 elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1238 InstanceShutdown(instance, shutdown_timeout)
1239 return StartInstance(instance, False)
1240 except errors.HypervisorError, err:
1241 _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1243 _Fail("Invalid reboot_type received: %s", reboot_type)
1246 def MigrationInfo(instance):
1247 """Gather information about an instance to be migrated.
1249 @type instance: L{objects.Instance}
1250 @param instance: the instance definition
1253 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1255 info = hyper.MigrationInfo(instance)
1256 except errors.HypervisorError, err:
1257 _Fail("Failed to fetch migration information: %s", err, exc=True)
1261 def AcceptInstance(instance, info, target):
1262 """Prepare the node to accept an instance.
1264 @type instance: L{objects.Instance}
1265 @param instance: the instance definition
1266 @type info: string/data (opaque)
1267 @param info: migration information, from the source node
1268 @type target: string
1269 @param target: target host (usually ip), on this node
1272 # TODO: why is this required only for DTS_EXT_MIRROR?
1273 if instance.disk_template in constants.DTS_EXT_MIRROR:
1274 # Create the symlinks, as the disks are not active
1277 _GatherAndLinkBlockDevs(instance)
1278 except errors.BlockDeviceError, err:
1279 _Fail("Block device error: %s", err, exc=True)
1281 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1283 hyper.AcceptInstance(instance, info, target)
1284 except errors.HypervisorError, err:
1285 if instance.disk_template in constants.DTS_EXT_MIRROR:
1286 _RemoveBlockDevLinks(instance.name, instance.disks)
1287 _Fail("Failed to accept instance: %s", err, exc=True)
1290 def FinalizeMigration(instance, info, success):
1291 """Finalize any preparation to accept an instance.
1293 @type instance: L{objects.Instance}
1294 @param instance: the instance definition
1295 @type info: string/data (opaque)
1296 @param info: migration information, from the source node
1297 @type success: boolean
1298 @param success: whether the migration was a success or a failure
1301 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1303 hyper.FinalizeMigration(instance, info, success)
1304 except errors.HypervisorError, err:
1305 _Fail("Failed to finalize migration: %s", err, exc=True)
1308 def MigrateInstance(instance, target, live):
1309 """Migrates an instance to another node.
1311 @type instance: L{objects.Instance}
1312 @param instance: the instance definition
1313 @type target: string
1314 @param target: the target node name
1316 @param live: whether the migration should be done live or not (the
1317 interpretation of this parameter is left to the hypervisor)
1319 @return: a tuple of (success, msg) where:
1320 - succes is a boolean denoting the success/failure of the operation
1321 - msg is a string with details in case of failure
1324 hyper = hypervisor.GetHypervisor(instance.hypervisor)
1327 hyper.MigrateInstance(instance, target, live)
1328 except errors.HypervisorError, err:
1329 _Fail("Failed to migrate instance: %s", err, exc=True)
1332 def BlockdevCreate(disk, size, owner, on_primary, info):
1333 """Creates a block device for an instance.
1335 @type disk: L{objects.Disk}
1336 @param disk: the object describing the disk we should create
1338 @param size: the size of the physical underlying device, in MiB
1340 @param owner: the name of the instance for which disk is created,
1341 used for device cache data
1342 @type on_primary: boolean
1343 @param on_primary: indicates if it is the primary node or not
1345 @param info: string that will be sent to the physical device
1346 creation, used for example to set (LVM) tags on LVs
1348 @return: the new unique_id of the device (this can sometime be
1349 computed only after creation), or None. On secondary nodes,
1350 it's not required to return anything.
1353 # TODO: remove the obsolete "size" argument
1354 # pylint: disable=W0613
1357 for child in disk.children:
1359 crdev = _RecursiveAssembleBD(child, owner, on_primary)
1360 except errors.BlockDeviceError, err:
1361 _Fail("Can't assemble device %s: %s", child, err)
1362 if on_primary or disk.AssembleOnSecondary():
1363 # we need the children open in case the device itself has to
1366 # pylint: disable=E1103
1368 except errors.BlockDeviceError, err:
1369 _Fail("Can't make child '%s' read-write: %s", child, err)
1373 device = bdev.Create(disk.dev_type, disk.physical_id, clist, disk.size)
1374 except errors.BlockDeviceError, err:
1375 _Fail("Can't create block device: %s", err)
1377 if on_primary or disk.AssembleOnSecondary():
1380 except errors.BlockDeviceError, err:
1381 _Fail("Can't assemble device after creation, unusual event: %s", err)
1382 device.SetSyncSpeed(constants.SYNC_SPEED)
1383 if on_primary or disk.OpenOnSecondary():
1385 device.Open(force=True)
1386 except errors.BlockDeviceError, err:
1387 _Fail("Can't make device r/w after creation, unusual event: %s", err)
1388 DevCacheManager.UpdateCache(device.dev_path, owner,
1389 on_primary, disk.iv_name)
1391 device.SetInfo(info)
1393 return device.unique_id
1396 def _WipeDevice(path, offset, size):
1397 """This function actually wipes the device.
1399 @param path: The path to the device to wipe
1400 @param offset: The offset in MiB in the file
1401 @param size: The size in MiB to write
1404 cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1405 "bs=%d" % constants.WIPE_BLOCK_SIZE, "oflag=direct", "of=%s" % path,
1407 result = utils.RunCmd(cmd)
1410 _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1411 result.fail_reason, result.output)
1414 def BlockdevWipe(disk, offset, size):
1415 """Wipes a block device.
1417 @type disk: L{objects.Disk}
1418 @param disk: the disk object we want to wipe
1420 @param offset: The offset in MiB in the file
1422 @param size: The size in MiB to write
1426 rdev = _RecursiveFindBD(disk)
1427 except errors.BlockDeviceError:
1431 _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1433 # Do cross verify some of the parameters
1434 if offset > rdev.size:
1435 _Fail("Offset is bigger than device size")
1436 if (offset + size) > rdev.size:
1437 _Fail("The provided offset and size to wipe is bigger than device size")
1439 _WipeDevice(rdev.dev_path, offset, size)
1442 def BlockdevPauseResumeSync(disks, pause):
1443 """Pause or resume the sync of the block device.
1445 @type disks: list of L{objects.Disk}
1446 @param disks: the disks object we want to pause/resume
1448 @param pause: Wheater to pause or resume
1454 rdev = _RecursiveFindBD(disk)
1455 except errors.BlockDeviceError:
1459 success.append((False, ("Cannot change sync for device %s:"
1460 " device not found" % disk.iv_name)))
1463 result = rdev.PauseResumeSync(pause)
1466 success.append((result, None))
1472 success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1477 def BlockdevRemove(disk):
1478 """Remove a block device.
1480 @note: This is intended to be called recursively.
1482 @type disk: L{objects.Disk}
1483 @param disk: the disk object we should remove
1485 @return: the success of the operation
1490 rdev = _RecursiveFindBD(disk)
1491 except errors.BlockDeviceError, err:
1492 # probably can't attach
1493 logging.info("Can't attach to device %s in remove", disk)
1495 if rdev is not None:
1496 r_path = rdev.dev_path
1499 except errors.BlockDeviceError, err:
1500 msgs.append(str(err))
1502 DevCacheManager.RemoveCache(r_path)
1505 for child in disk.children:
1507 BlockdevRemove(child)
1508 except RPCFail, err:
1509 msgs.append(str(err))
1512 _Fail("; ".join(msgs))
1515 def _RecursiveAssembleBD(disk, owner, as_primary):
1516 """Activate a block device for an instance.
1518 This is run on the primary and secondary nodes for an instance.
1520 @note: this function is called recursively.
1522 @type disk: L{objects.Disk}
1523 @param disk: the disk we try to assemble
1525 @param owner: the name of the instance which owns the disk
1526 @type as_primary: boolean
1527 @param as_primary: if we should make the block device
1530 @return: the assembled device or None (in case no device
1532 @raise errors.BlockDeviceError: in case there is an error
1533 during the activation of the children or the device
1539 mcn = disk.ChildrenNeeded()
1541 mcn = 0 # max number of Nones allowed
1543 mcn = len(disk.children) - mcn # max number of Nones
1544 for chld_disk in disk.children:
1546 cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1547 except errors.BlockDeviceError, err:
1548 if children.count(None) >= mcn:
1551 logging.error("Error in child activation (but continuing): %s",
1553 children.append(cdev)
1555 if as_primary or disk.AssembleOnSecondary():
1556 r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children, disk.size)
1557 r_dev.SetSyncSpeed(constants.SYNC_SPEED)
1559 if as_primary or disk.OpenOnSecondary():
1561 DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1562 as_primary, disk.iv_name)
1569 def BlockdevAssemble(disk, owner, as_primary, idx):
1570 """Activate a block device for an instance.
1572 This is a wrapper over _RecursiveAssembleBD.
1574 @rtype: str or boolean
1575 @return: a C{/dev/...} path for primary nodes, and
1576 C{True} for secondary nodes
1580 result = _RecursiveAssembleBD(disk, owner, as_primary)
1581 if isinstance(result, bdev.BlockDev):
1582 # pylint: disable=E1103
1583 result = result.dev_path
1585 _SymlinkBlockDev(owner, result, idx)
1586 except errors.BlockDeviceError, err:
1587 _Fail("Error while assembling disk: %s", err, exc=True)
1588 except OSError, err:
1589 _Fail("Error while symlinking disk: %s", err, exc=True)
1594 def BlockdevShutdown(disk):
1595 """Shut down a block device.
1597 First, if the device is assembled (Attach() is successful), then
1598 the device is shutdown. Then the children of the device are
1601 This function is called recursively. Note that we don't cache the
1602 children or such, as oppossed to assemble, shutdown of different
1603 devices doesn't require that the upper device was active.
1605 @type disk: L{objects.Disk}
1606 @param disk: the description of the disk we should
1612 r_dev = _RecursiveFindBD(disk)
1613 if r_dev is not None:
1614 r_path = r_dev.dev_path
1617 DevCacheManager.RemoveCache(r_path)
1618 except errors.BlockDeviceError, err:
1619 msgs.append(str(err))
1622 for child in disk.children:
1624 BlockdevShutdown(child)
1625 except RPCFail, err:
1626 msgs.append(str(err))
1629 _Fail("; ".join(msgs))
1632 def BlockdevAddchildren(parent_cdev, new_cdevs):
1633 """Extend a mirrored block device.
1635 @type parent_cdev: L{objects.Disk}
1636 @param parent_cdev: the disk to which we should add children
1637 @type new_cdevs: list of L{objects.Disk}
1638 @param new_cdevs: the list of children which we should add
1642 parent_bdev = _RecursiveFindBD(parent_cdev)
1643 if parent_bdev is None:
1644 _Fail("Can't find parent device '%s' in add children", parent_cdev)
1645 new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1646 if new_bdevs.count(None) > 0:
1647 _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1648 parent_bdev.AddChildren(new_bdevs)
1651 def BlockdevRemovechildren(parent_cdev, new_cdevs):
1652 """Shrink a mirrored block device.
1654 @type parent_cdev: L{objects.Disk}
1655 @param parent_cdev: the disk from which we should remove children
1656 @type new_cdevs: list of L{objects.Disk}
1657 @param new_cdevs: the list of children which we should remove
1661 parent_bdev = _RecursiveFindBD(parent_cdev)
1662 if parent_bdev is None:
1663 _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1665 for disk in new_cdevs:
1666 rpath = disk.StaticDevPath()
1668 bd = _RecursiveFindBD(disk)
1670 _Fail("Can't find device %s while removing children", disk)
1672 devs.append(bd.dev_path)
1674 if not utils.IsNormAbsPath(rpath):
1675 _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
1677 parent_bdev.RemoveChildren(devs)
1680 def BlockdevGetmirrorstatus(disks):
1681 """Get the mirroring status of a list of devices.
1683 @type disks: list of L{objects.Disk}
1684 @param disks: the list of disks which we should query
1686 @return: List of L{objects.BlockDevStatus}, one for each disk
1687 @raise errors.BlockDeviceError: if any of the disks cannot be
1693 rbd = _RecursiveFindBD(dsk)
1695 _Fail("Can't find device %s", dsk)
1697 stats.append(rbd.CombinedSyncStatus())
1702 def BlockdevGetmirrorstatusMulti(disks):
1703 """Get the mirroring status of a list of devices.
1705 @type disks: list of L{objects.Disk}
1706 @param disks: the list of disks which we should query
1708 @return: List of tuples, (bool, status), one for each disk; bool denotes
1709 success/failure, status is L{objects.BlockDevStatus} on success, string
1716 rbd = _RecursiveFindBD(disk)
1718 result.append((False, "Can't find device %s" % disk))
1721 status = rbd.CombinedSyncStatus()
1722 except errors.BlockDeviceError, err:
1723 logging.exception("Error while getting disk status")
1724 result.append((False, str(err)))
1726 result.append((True, status))
1728 assert len(disks) == len(result)
1733 def _RecursiveFindBD(disk):
1734 """Check if a device is activated.
1736 If so, return information about the real device.
1738 @type disk: L{objects.Disk}
1739 @param disk: the disk object we need to find
1741 @return: None if the device can't be found,
1742 otherwise the device instance
1747 for chdisk in disk.children:
1748 children.append(_RecursiveFindBD(chdisk))
1750 return bdev.FindDevice(disk.dev_type, disk.physical_id, children, disk.size)
1753 def _OpenRealBD(disk):
1754 """Opens the underlying block device of a disk.
1756 @type disk: L{objects.Disk}
1757 @param disk: the disk object we want to open
1760 real_disk = _RecursiveFindBD(disk)
1761 if real_disk is None:
1762 _Fail("Block device '%s' is not set up", disk)
1769 def BlockdevFind(disk):
1770 """Check if a device is activated.
1772 If it is, return information about the real device.
1774 @type disk: L{objects.Disk}
1775 @param disk: the disk to find
1776 @rtype: None or objects.BlockDevStatus
1777 @return: None if the disk cannot be found, otherwise a the current
1782 rbd = _RecursiveFindBD(disk)
1783 except errors.BlockDeviceError, err:
1784 _Fail("Failed to find device: %s", err, exc=True)
1789 return rbd.GetSyncStatus()
1792 def BlockdevGetsize(disks):
1793 """Computes the size of the given disks.
1795 If a disk is not found, returns None instead.
1797 @type disks: list of L{objects.Disk}
1798 @param disks: the list of disk to compute the size for
1800 @return: list with elements None if the disk cannot be found,
1807 rbd = _RecursiveFindBD(cf)
1808 except errors.BlockDeviceError:
1814 result.append(rbd.GetActualSize())
1818 def BlockdevExport(disk, dest_node, dest_path, cluster_name):
1819 """Export a block device to a remote node.
1821 @type disk: L{objects.Disk}
1822 @param disk: the description of the disk to export
1823 @type dest_node: str
1824 @param dest_node: the destination node to export to
1825 @type dest_path: str
1826 @param dest_path: the destination path on the target node
1827 @type cluster_name: str
1828 @param cluster_name: the cluster name, needed for SSH hostalias
1832 real_disk = _OpenRealBD(disk)
1834 # the block size on the read dd is 1MiB to match our units
1835 expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
1836 "dd if=%s bs=1048576 count=%s",
1837 real_disk.dev_path, str(disk.size))
1839 # we set here a smaller block size as, due to ssh buffering, more
1840 # than 64-128k will mostly ignored; we use nocreat to fail if the
1841 # device is not already there or we pass a wrong path; we use
1842 # notrunc to no attempt truncate on an LV device; we use oflag=dsync
1843 # to not buffer too much memory; this means that at best, we flush
1844 # every 64k, which will not be very fast
1845 destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
1846 " oflag=dsync", dest_path)
1848 remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
1849 constants.GANETI_RUNAS,
1852 # all commands have been checked, so we're safe to combine them
1853 command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
1855 result = utils.RunCmd(["bash", "-c", command])
1858 _Fail("Disk copy command '%s' returned error: %s"
1859 " output: %s", command, result.fail_reason, result.output)
1862 def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
1863 """Write a file to the filesystem.
1865 This allows the master to overwrite(!) a file. It will only perform
1866 the operation if the file belongs to a list of configuration files.
1868 @type file_name: str
1869 @param file_name: the target file name
1871 @param data: the new contents of the file
1873 @param mode: the mode to give the file (can be None)
1875 @param uid: the owner of the file
1877 @param gid: the group of the file
1879 @param atime: the atime to set on the file (can be None)
1881 @param mtime: the mtime to set on the file (can be None)
1885 if not os.path.isabs(file_name):
1886 _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
1888 if file_name not in _ALLOWED_UPLOAD_FILES:
1889 _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
1892 raw_data = _Decompress(data)
1894 if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
1895 _Fail("Invalid username/groupname type")
1897 getents = runtime.GetEnts()
1898 uid = getents.LookupUser(uid)
1899 gid = getents.LookupGroup(gid)
1901 utils.SafeWriteFile(file_name, None,
1902 data=raw_data, mode=mode, uid=uid, gid=gid,
1903 atime=atime, mtime=mtime)
1906 def RunOob(oob_program, command, node, timeout):
1907 """Executes oob_program with given command on given node.
1909 @param oob_program: The path to the executable oob_program
1910 @param command: The command to invoke on oob_program
1911 @param node: The node given as an argument to the program
1912 @param timeout: Timeout after which we kill the oob program
1915 @raise RPCFail: If execution fails for some reason
1918 result = utils.RunCmd([oob_program, command, node], timeout=timeout)
1921 _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
1922 result.fail_reason, result.output)
1924 return result.stdout
1927 def WriteSsconfFiles(values):
1928 """Update all ssconf files.
1930 Wrapper around the SimpleStore.WriteFiles.
1933 ssconf.SimpleStore().WriteFiles(values)
1936 def _ErrnoOrStr(err):
1937 """Format an EnvironmentError exception.
1939 If the L{err} argument has an errno attribute, it will be looked up
1940 and converted into a textual C{E...} description. Otherwise the
1941 string representation of the error will be returned.
1943 @type err: L{EnvironmentError}
1944 @param err: the exception to format
1947 if hasattr(err, "errno"):
1948 detail = errno.errorcode[err.errno]
1954 def _OSOndiskAPIVersion(os_dir):
1955 """Compute and return the API version of a given OS.
1957 This function will try to read the API version of the OS residing in
1958 the 'os_dir' directory.
1961 @param os_dir: the directory in which we should look for the OS
1963 @return: tuple (status, data) with status denoting the validity and
1964 data holding either the vaid versions or an error message
1967 api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
1970 st = os.stat(api_file)
1971 except EnvironmentError, err:
1972 return False, ("Required file '%s' not found under path %s: %s" %
1973 (constants.OS_API_FILE, os_dir, _ErrnoOrStr(err)))
1975 if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1976 return False, ("File '%s' in %s is not a regular file" %
1977 (constants.OS_API_FILE, os_dir))
1980 api_versions = utils.ReadFile(api_file).splitlines()
1981 except EnvironmentError, err:
1982 return False, ("Error while reading the API version file at %s: %s" %
1983 (api_file, _ErrnoOrStr(err)))
1986 api_versions = [int(version.strip()) for version in api_versions]
1987 except (TypeError, ValueError), err:
1988 return False, ("API version(s) can't be converted to integer: %s" %
1991 return True, api_versions
1994 def DiagnoseOS(top_dirs=None):
1995 """Compute the validity for all OSes.
1997 @type top_dirs: list
1998 @param top_dirs: the list of directories in which to
1999 search (if not given defaults to
2000 L{constants.OS_SEARCH_PATH})
2001 @rtype: list of L{objects.OS}
2002 @return: a list of tuples (name, path, status, diagnose, variants,
2003 parameters, api_version) for all (potential) OSes under all
2004 search paths, where:
2005 - name is the (potential) OS name
2006 - path is the full path to the OS
2007 - status True/False is the validity of the OS
2008 - diagnose is the error message for an invalid OS, otherwise empty
2009 - variants is a list of supported OS variants, if any
2010 - parameters is a list of (name, help) parameters, if any
2011 - api_version is a list of support OS API versions
2014 if top_dirs is None:
2015 top_dirs = constants.OS_SEARCH_PATH
2018 for dir_name in top_dirs:
2019 if os.path.isdir(dir_name):
2021 f_names = utils.ListVisibleFiles(dir_name)
2022 except EnvironmentError, err:
2023 logging.exception("Can't list the OS directory %s: %s", dir_name, err)
2025 for name in f_names:
2026 os_path = utils.PathJoin(dir_name, name)
2027 status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
2030 variants = os_inst.supported_variants
2031 parameters = os_inst.supported_parameters
2032 api_versions = os_inst.api_versions
2035 variants = parameters = api_versions = []
2036 result.append((name, os_path, status, diagnose, variants,
2037 parameters, api_versions))
2042 def _TryOSFromDisk(name, base_dir=None):
2043 """Create an OS instance from disk.
2045 This function will return an OS instance if the given name is a
2048 @type base_dir: string
2049 @keyword base_dir: Base directory containing OS installations.
2050 Defaults to a search in all the OS_SEARCH_PATH dirs.
2052 @return: success and either the OS instance if we find a valid one,
2056 if base_dir is None:
2057 os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir)
2059 os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
2062 return False, "Directory for OS %s not found in search path" % name
2064 status, api_versions = _OSOndiskAPIVersion(os_dir)
2067 return status, api_versions
2069 if not constants.OS_API_VERSIONS.intersection(api_versions):
2070 return False, ("API version mismatch for path '%s': found %s, want %s." %
2071 (os_dir, api_versions, constants.OS_API_VERSIONS))
2073 # OS Files dictionary, we will populate it with the absolute path
2074 # names; if the value is True, then it is a required file, otherwise
2076 os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
2078 if max(api_versions) >= constants.OS_API_V15:
2079 os_files[constants.OS_VARIANTS_FILE] = False
2081 if max(api_versions) >= constants.OS_API_V20:
2082 os_files[constants.OS_PARAMETERS_FILE] = True
2084 del os_files[constants.OS_SCRIPT_VERIFY]
2086 for (filename, required) in os_files.items():
2087 os_files[filename] = utils.PathJoin(os_dir, filename)
2090 st = os.stat(os_files[filename])
2091 except EnvironmentError, err:
2092 if err.errno == errno.ENOENT and not required:
2093 del os_files[filename]
2095 return False, ("File '%s' under path '%s' is missing (%s)" %
2096 (filename, os_dir, _ErrnoOrStr(err)))
2098 if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2099 return False, ("File '%s' under path '%s' is not a regular file" %
2102 if filename in constants.OS_SCRIPTS:
2103 if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
2104 return False, ("File '%s' under path '%s' is not executable" %
2108 if constants.OS_VARIANTS_FILE in os_files:
2109 variants_file = os_files[constants.OS_VARIANTS_FILE]
2111 variants = utils.ReadFile(variants_file).splitlines()
2112 except EnvironmentError, err:
2113 # we accept missing files, but not other errors
2114 if err.errno != errno.ENOENT:
2115 return False, ("Error while reading the OS variants file at %s: %s" %
2116 (variants_file, _ErrnoOrStr(err)))
2119 if constants.OS_PARAMETERS_FILE in os_files:
2120 parameters_file = os_files[constants.OS_PARAMETERS_FILE]
2122 parameters = utils.ReadFile(parameters_file).splitlines()
2123 except EnvironmentError, err:
2124 return False, ("Error while reading the OS parameters file at %s: %s" %
2125 (parameters_file, _ErrnoOrStr(err)))
2126 parameters = [v.split(None, 1) for v in parameters]
2128 os_obj = objects.OS(name=name, path=os_dir,
2129 create_script=os_files[constants.OS_SCRIPT_CREATE],
2130 export_script=os_files[constants.OS_SCRIPT_EXPORT],
2131 import_script=os_files[constants.OS_SCRIPT_IMPORT],
2132 rename_script=os_files[constants.OS_SCRIPT_RENAME],
2133 verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
2135 supported_variants=variants,
2136 supported_parameters=parameters,
2137 api_versions=api_versions)
2141 def OSFromDisk(name, base_dir=None):
2142 """Create an OS instance from disk.
2144 This function will return an OS instance if the given name is a
2145 valid OS name. Otherwise, it will raise an appropriate
2146 L{RPCFail} exception, detailing why this is not a valid OS.
2148 This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
2149 an exception but returns true/false status data.
2151 @type base_dir: string
2152 @keyword base_dir: Base directory containing OS installations.
2153 Defaults to a search in all the OS_SEARCH_PATH dirs.
2154 @rtype: L{objects.OS}
2155 @return: the OS instance if we find a valid one
2156 @raise RPCFail: if we don't find a valid OS
2159 name_only = objects.OS.GetName(name)
2160 status, payload = _TryOSFromDisk(name_only, base_dir)
2168 def OSCoreEnv(os_name, inst_os, os_params, debug=0):
2169 """Calculate the basic environment for an os script.
2172 @param os_name: full operating system name (including variant)
2173 @type inst_os: L{objects.OS}
2174 @param inst_os: operating system for which the environment is being built
2175 @type os_params: dict
2176 @param os_params: the OS parameters
2177 @type debug: integer
2178 @param debug: debug level (0 or 1, for OS Api 10)
2180 @return: dict of environment variables
2181 @raise errors.BlockDeviceError: if the block device
2187 max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
2188 result["OS_API_VERSION"] = "%d" % api_version
2189 result["OS_NAME"] = inst_os.name
2190 result["DEBUG_LEVEL"] = "%d" % debug
2193 if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
2194 variant = objects.OS.GetVariant(os_name)
2196 variant = inst_os.supported_variants[0]
2199 result["OS_VARIANT"] = variant
2202 for pname, pvalue in os_params.items():
2203 result["OSP_%s" % pname.upper()] = pvalue
2208 def OSEnvironment(instance, inst_os, debug=0):
2209 """Calculate the environment for an os script.
2211 @type instance: L{objects.Instance}
2212 @param instance: target instance for the os script run
2213 @type inst_os: L{objects.OS}
2214 @param inst_os: operating system for which the environment is being built
2215 @type debug: integer
2216 @param debug: debug level (0 or 1, for OS Api 10)
2218 @return: dict of environment variables
2219 @raise errors.BlockDeviceError: if the block device
2223 result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug)
2225 for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]:
2226 result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr))
2228 result["HYPERVISOR"] = instance.hypervisor
2229 result["DISK_COUNT"] = "%d" % len(instance.disks)
2230 result["NIC_COUNT"] = "%d" % len(instance.nics)
2231 result["INSTANCE_SECONDARY_NODES"] = \
2232 ("%s" % " ".join(instance.secondary_nodes))
2235 for idx, disk in enumerate(instance.disks):
2236 real_disk = _OpenRealBD(disk)
2237 result["DISK_%d_PATH" % idx] = real_disk.dev_path
2238 result["DISK_%d_ACCESS" % idx] = disk.mode
2239 if constants.HV_DISK_TYPE in instance.hvparams:
2240 result["DISK_%d_FRONTEND_TYPE" % idx] = \
2241 instance.hvparams[constants.HV_DISK_TYPE]
2242 if disk.dev_type in constants.LDS_BLOCK:
2243 result["DISK_%d_BACKEND_TYPE" % idx] = "block"
2244 elif disk.dev_type == constants.LD_FILE:
2245 result["DISK_%d_BACKEND_TYPE" % idx] = \
2246 "file:%s" % disk.physical_id[0]
2249 for idx, nic in enumerate(instance.nics):
2250 result["NIC_%d_MAC" % idx] = nic.mac
2252 result["NIC_%d_IP" % idx] = nic.ip
2253 result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
2254 if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2255 result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
2256 if nic.nicparams[constants.NIC_LINK]:
2257 result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
2258 if constants.HV_NIC_TYPE in instance.hvparams:
2259 result["NIC_%d_FRONTEND_TYPE" % idx] = \
2260 instance.hvparams[constants.HV_NIC_TYPE]
2263 for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
2264 for key, value in source.items():
2265 result["INSTANCE_%s_%s" % (kind, key)] = str(value)
2270 def BlockdevGrow(disk, amount, dryrun):
2271 """Grow a stack of block devices.
2273 This function is called recursively, with the childrens being the
2274 first ones to resize.
2276 @type disk: L{objects.Disk}
2277 @param disk: the disk to be grown
2278 @type amount: integer
2279 @param amount: the amount (in mebibytes) to grow with
2280 @type dryrun: boolean
2281 @param dryrun: whether to execute the operation in simulation mode
2282 only, without actually increasing the size
2283 @rtype: (status, result)
2284 @return: a tuple with the status of the operation (True/False), and
2285 the errors message if status is False
2288 r_dev = _RecursiveFindBD(disk)
2290 _Fail("Cannot find block device %s", disk)
2293 r_dev.Grow(amount, dryrun)
2294 except errors.BlockDeviceError, err:
2295 _Fail("Failed to grow block device: %s", err, exc=True)
2298 def BlockdevSnapshot(disk):
2299 """Create a snapshot copy of a block device.
2301 This function is called recursively, and the snapshot is actually created
2302 just for the leaf lvm backend device.
2304 @type disk: L{objects.Disk}
2305 @param disk: the disk to be snapshotted
2307 @return: snapshot disk ID as (vg, lv)
2310 if disk.dev_type == constants.LD_DRBD8:
2311 if not disk.children:
2312 _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
2314 return BlockdevSnapshot(disk.children[0])
2315 elif disk.dev_type == constants.LD_LV:
2316 r_dev = _RecursiveFindBD(disk)
2317 if r_dev is not None:
2318 # FIXME: choose a saner value for the snapshot size
2319 # let's stay on the safe side and ask for the full size, for now
2320 return r_dev.Snapshot(disk.size)
2322 _Fail("Cannot find block device %s", disk)
2324 _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
2325 disk.unique_id, disk.dev_type)
2328 def FinalizeExport(instance, snap_disks):
2329 """Write out the export configuration information.
2331 @type instance: L{objects.Instance}
2332 @param instance: the instance which we export, used for
2333 saving configuration
2334 @type snap_disks: list of L{objects.Disk}
2335 @param snap_disks: list of snapshot block devices, which
2336 will be used to get the actual name of the dump file
2341 destdir = utils.PathJoin(constants.EXPORT_DIR, instance.name + ".new")
2342 finaldestdir = utils.PathJoin(constants.EXPORT_DIR, instance.name)
2344 config = objects.SerializableConfigParser()
2346 config.add_section(constants.INISECT_EXP)
2347 config.set(constants.INISECT_EXP, "version", "0")
2348 config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
2349 config.set(constants.INISECT_EXP, "source", instance.primary_node)
2350 config.set(constants.INISECT_EXP, "os", instance.os)
2351 config.set(constants.INISECT_EXP, "compression", "none")
2353 config.add_section(constants.INISECT_INS)
2354 config.set(constants.INISECT_INS, "name", instance.name)
2355 config.set(constants.INISECT_INS, "memory", "%d" %
2356 instance.beparams[constants.BE_MEMORY])
2357 config.set(constants.INISECT_INS, "vcpus", "%d" %
2358 instance.beparams[constants.BE_VCPUS])
2359 config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
2360 config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
2361 config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
2364 for nic_count, nic in enumerate(instance.nics):
2366 config.set(constants.INISECT_INS, "nic%d_mac" %
2367 nic_count, "%s" % nic.mac)
2368 config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
2369 for param in constants.NICS_PARAMETER_TYPES:
2370 config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
2371 "%s" % nic.nicparams.get(param, None))
2372 # TODO: redundant: on load can read nics until it doesn't exist
2373 config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
2376 for disk_count, disk in enumerate(snap_disks):
2379 config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
2380 ("%s" % disk.iv_name))
2381 config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
2382 ("%s" % disk.physical_id[1]))
2383 config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
2386 config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
2388 # New-style hypervisor/backend parameters
2390 config.add_section(constants.INISECT_HYP)
2391 for name, value in instance.hvparams.items():
2392 if name not in constants.HVC_GLOBALS:
2393 config.set(constants.INISECT_HYP, name, str(value))
2395 config.add_section(constants.INISECT_BEP)
2396 for name, value in instance.beparams.items():
2397 config.set(constants.INISECT_BEP, name, str(value))
2399 config.add_section(constants.INISECT_OSP)
2400 for name, value in instance.osparams.items():
2401 config.set(constants.INISECT_OSP, name, str(value))
2403 utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
2404 data=config.Dumps())
2405 shutil.rmtree(finaldestdir, ignore_errors=True)
2406 shutil.move(destdir, finaldestdir)
2409 def ExportInfo(dest):
2410 """Get export configuration information.
2413 @param dest: directory containing the export
2415 @rtype: L{objects.SerializableConfigParser}
2416 @return: a serializable config file containing the
2420 cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
2422 config = objects.SerializableConfigParser()
2425 if (not config.has_section(constants.INISECT_EXP) or
2426 not config.has_section(constants.INISECT_INS)):
2427 _Fail("Export info file doesn't have the required fields")
2429 return config.Dumps()
2433 """Return a list of exports currently available on this machine.
2436 @return: list of the exports
2439 if os.path.isdir(constants.EXPORT_DIR):
2440 return sorted(utils.ListVisibleFiles(constants.EXPORT_DIR))
2442 _Fail("No exports directory")
2445 def RemoveExport(export):
2446 """Remove an existing export from the node.
2449 @param export: the name of the export to remove
2453 target = utils.PathJoin(constants.EXPORT_DIR, export)
2456 shutil.rmtree(target)
2457 except EnvironmentError, err:
2458 _Fail("Error while removing the export: %s", err, exc=True)
2461 def BlockdevRename(devlist):
2462 """Rename a list of block devices.
2464 @type devlist: list of tuples
2465 @param devlist: list of tuples of the form (disk,
2466 new_logical_id, new_physical_id); disk is an
2467 L{objects.Disk} object describing the current disk,
2468 and new logical_id/physical_id is the name we
2471 @return: True if all renames succeeded, False otherwise
2476 for disk, unique_id in devlist:
2477 dev = _RecursiveFindBD(disk)
2479 msgs.append("Can't find device %s in rename" % str(disk))
2483 old_rpath = dev.dev_path
2484 dev.Rename(unique_id)
2485 new_rpath = dev.dev_path
2486 if old_rpath != new_rpath:
2487 DevCacheManager.RemoveCache(old_rpath)
2488 # FIXME: we should add the new cache information here, like:
2489 # DevCacheManager.UpdateCache(new_rpath, owner, ...)
2490 # but we don't have the owner here - maybe parse from existing
2491 # cache? for now, we only lose lvm data when we rename, which
2492 # is less critical than DRBD or MD
2493 except errors.BlockDeviceError, err:
2494 msgs.append("Can't rename device '%s' to '%s': %s" %
2495 (dev, unique_id, err))
2496 logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2499 _Fail("; ".join(msgs))
2502 def _TransformFileStorageDir(fs_dir):
2503 """Checks whether given file_storage_dir is valid.
2505 Checks wheter the given fs_dir is within the cluster-wide default
2506 file_storage_dir or the shared_file_storage_dir, which are stored in
2507 SimpleStore. Only paths under those directories are allowed.
2510 @param fs_dir: the path to check
2512 @return: the normalized path if valid, None otherwise
2515 if not constants.ENABLE_FILE_STORAGE:
2516 _Fail("File storage disabled at configure time")
2518 fs_dir = os.path.normpath(fs_dir)
2519 base_fstore = cfg.GetFileStorageDir()
2520 base_shared = cfg.GetSharedFileStorageDir()
2521 if not (utils.IsBelowDir(base_fstore, fs_dir) or
2522 utils.IsBelowDir(base_shared, fs_dir)):
2523 _Fail("File storage directory '%s' is not under base file"
2524 " storage directory '%s' or shared storage directory '%s'",
2525 fs_dir, base_fstore, base_shared)
2529 def CreateFileStorageDir(file_storage_dir):
2530 """Create file storage directory.
2532 @type file_storage_dir: str
2533 @param file_storage_dir: directory to create
2536 @return: tuple with first element a boolean indicating wheter dir
2537 creation was successful or not
2540 file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2541 if os.path.exists(file_storage_dir):
2542 if not os.path.isdir(file_storage_dir):
2543 _Fail("Specified storage dir '%s' is not a directory",
2547 os.makedirs(file_storage_dir, 0750)
2548 except OSError, err:
2549 _Fail("Cannot create file storage directory '%s': %s",
2550 file_storage_dir, err, exc=True)
2553 def RemoveFileStorageDir(file_storage_dir):
2554 """Remove file storage directory.
2556 Remove it only if it's empty. If not log an error and return.
2558 @type file_storage_dir: str
2559 @param file_storage_dir: the directory we should cleanup
2560 @rtype: tuple (success,)
2561 @return: tuple of one element, C{success}, denoting
2562 whether the operation was successful
2565 file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2566 if os.path.exists(file_storage_dir):
2567 if not os.path.isdir(file_storage_dir):
2568 _Fail("Specified Storage directory '%s' is not a directory",
2570 # deletes dir only if empty, otherwise we want to fail the rpc call
2572 os.rmdir(file_storage_dir)
2573 except OSError, err:
2574 _Fail("Cannot remove file storage directory '%s': %s",
2575 file_storage_dir, err)
2578 def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2579 """Rename the file storage directory.
2581 @type old_file_storage_dir: str
2582 @param old_file_storage_dir: the current path
2583 @type new_file_storage_dir: str
2584 @param new_file_storage_dir: the name we should rename to
2585 @rtype: tuple (success,)
2586 @return: tuple of one element, C{success}, denoting
2587 whether the operation was successful
2590 old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2591 new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2592 if not os.path.exists(new_file_storage_dir):
2593 if os.path.isdir(old_file_storage_dir):
2595 os.rename(old_file_storage_dir, new_file_storage_dir)
2596 except OSError, err:
2597 _Fail("Cannot rename '%s' to '%s': %s",
2598 old_file_storage_dir, new_file_storage_dir, err)
2600 _Fail("Specified storage dir '%s' is not a directory",
2601 old_file_storage_dir)
2603 if os.path.exists(old_file_storage_dir):
2604 _Fail("Cannot rename '%s' to '%s': both locations exist",
2605 old_file_storage_dir, new_file_storage_dir)
2608 def _EnsureJobQueueFile(file_name):
2609 """Checks whether the given filename is in the queue directory.
2611 @type file_name: str
2612 @param file_name: the file name we should check
2614 @raises RPCFail: if the file is not valid
2617 queue_dir = os.path.normpath(constants.QUEUE_DIR)
2618 result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir)
2621 _Fail("Passed job queue file '%s' does not belong to"
2622 " the queue directory '%s'", file_name, queue_dir)
2625 def JobQueueUpdate(file_name, content):
2626 """Updates a file in the queue directory.
2628 This is just a wrapper over L{utils.io.WriteFile}, with proper
2631 @type file_name: str
2632 @param file_name: the job file name
2634 @param content: the new job contents
2636 @return: the success of the operation
2639 _EnsureJobQueueFile(file_name)
2640 getents = runtime.GetEnts()
2642 # Write and replace the file atomically
2643 utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
2644 gid=getents.masterd_gid)
2647 def JobQueueRename(old, new):
2648 """Renames a job queue file.
2650 This is just a wrapper over os.rename with proper checking.
2653 @param old: the old (actual) file name
2655 @param new: the desired file name
2657 @return: the success of the operation and payload
2660 _EnsureJobQueueFile(old)
2661 _EnsureJobQueueFile(new)
2663 utils.RenameFile(old, new, mkdir=True)
2666 def BlockdevClose(instance_name, disks):
2667 """Closes the given block devices.
2669 This means they will be switched to secondary mode (in case of
2672 @param instance_name: if the argument is not empty, the symlinks
2673 of this instance will be removed
2674 @type disks: list of L{objects.Disk}
2675 @param disks: the list of disks to be closed
2676 @rtype: tuple (success, message)
2677 @return: a tuple of success and message, where success
2678 indicates the succes of the operation, and message
2679 which will contain the error details in case we
2685 rd = _RecursiveFindBD(cf)
2687 _Fail("Can't find device %s", cf)
2694 except errors.BlockDeviceError, err:
2695 msg.append(str(err))
2697 _Fail("Can't make devices secondary: %s", ",".join(msg))
2700 _RemoveBlockDevLinks(instance_name, disks)
2703 def ValidateHVParams(hvname, hvparams):
2704 """Validates the given hypervisor parameters.
2706 @type hvname: string
2707 @param hvname: the hypervisor name
2708 @type hvparams: dict
2709 @param hvparams: the hypervisor parameters to be validated
2714 hv_type = hypervisor.GetHypervisor(hvname)
2715 hv_type.ValidateParameters(hvparams)
2716 except errors.HypervisorError, err:
2717 _Fail(str(err), log=False)
2720 def _CheckOSPList(os_obj, parameters):
2721 """Check whether a list of parameters is supported by the OS.
2723 @type os_obj: L{objects.OS}
2724 @param os_obj: OS object to check
2725 @type parameters: list
2726 @param parameters: the list of parameters to check
2729 supported = [v[0] for v in os_obj.supported_parameters]
2730 delta = frozenset(parameters).difference(supported)
2732 _Fail("The following parameters are not supported"
2733 " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
2736 def ValidateOS(required, osname, checks, osparams):
2737 """Validate the given OS' parameters.
2739 @type required: boolean
2740 @param required: whether absence of the OS should translate into
2742 @type osname: string
2743 @param osname: the OS to be validated
2745 @param checks: list of the checks to run (currently only 'parameters')
2746 @type osparams: dict
2747 @param osparams: dictionary with OS parameters
2749 @return: True if the validation passed, or False if the OS was not
2750 found and L{required} was false
2753 if not constants.OS_VALIDATE_CALLS.issuperset(checks):
2754 _Fail("Unknown checks required for OS %s: %s", osname,
2755 set(checks).difference(constants.OS_VALIDATE_CALLS))
2757 name_only = objects.OS.GetName(osname)
2758 status, tbv = _TryOSFromDisk(name_only, None)
2766 if max(tbv.api_versions) < constants.OS_API_V20:
2769 if constants.OS_VALIDATE_PARAMETERS in checks:
2770 _CheckOSPList(tbv, osparams.keys())
2772 validate_env = OSCoreEnv(osname, tbv, osparams)
2773 result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
2774 cwd=tbv.path, reset_env=True)
2776 logging.error("os validate command '%s' returned error: %s output: %s",
2777 result.cmd, result.fail_reason, result.output)
2778 _Fail("OS validation script failed (%s), output: %s",
2779 result.fail_reason, result.output, log=False)
2785 """Demotes the current node from master candidate role.
2788 # try to ensure we're not the master by mistake
2789 master, myself = ssconf.GetMasterAndMyself()
2790 if master == myself:
2791 _Fail("ssconf status shows I'm the master node, will not demote")
2793 result = utils.RunCmd([constants.DAEMON_UTIL, "check", constants.MASTERD])
2794 if not result.failed:
2795 _Fail("The master daemon is running, will not demote")
2798 if os.path.isfile(constants.CLUSTER_CONF_FILE):
2799 utils.CreateBackup(constants.CLUSTER_CONF_FILE)
2800 except EnvironmentError, err:
2801 if err.errno != errno.ENOENT:
2802 _Fail("Error while backing up cluster file: %s", err, exc=True)
2804 utils.RemoveFile(constants.CLUSTER_CONF_FILE)
2807 def _GetX509Filenames(cryptodir, name):
2808 """Returns the full paths for the private key and certificate.
2811 return (utils.PathJoin(cryptodir, name),
2812 utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
2813 utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
2816 def CreateX509Certificate(validity, cryptodir=constants.CRYPTO_KEYS_DIR):
2817 """Creates a new X509 certificate for SSL/TLS.
2820 @param validity: Validity in seconds
2821 @rtype: tuple; (string, string)
2822 @return: Certificate name and public part
2825 (key_pem, cert_pem) = \
2826 utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
2827 min(validity, _MAX_SSL_CERT_VALIDITY))
2829 cert_dir = tempfile.mkdtemp(dir=cryptodir,
2830 prefix="x509-%s-" % utils.TimestampForFilename())
2832 name = os.path.basename(cert_dir)
2833 assert len(name) > 5
2835 (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
2837 utils.WriteFile(key_file, mode=0400, data=key_pem)
2838 utils.WriteFile(cert_file, mode=0400, data=cert_pem)
2840 # Never return private key as it shouldn't leave the node
2841 return (name, cert_pem)
2843 shutil.rmtree(cert_dir, ignore_errors=True)
2847 def RemoveX509Certificate(name, cryptodir=constants.CRYPTO_KEYS_DIR):
2848 """Removes a X509 certificate.
2851 @param name: Certificate name
2854 (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
2856 utils.RemoveFile(key_file)
2857 utils.RemoveFile(cert_file)
2861 except EnvironmentError, err:
2862 _Fail("Cannot remove certificate directory '%s': %s",
2866 def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
2867 """Returns the command for the requested input/output.
2869 @type instance: L{objects.Instance}
2870 @param instance: The instance object
2871 @param mode: Import/export mode
2872 @param ieio: Input/output type
2873 @param ieargs: Input/output arguments
2876 assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
2883 if ieio == constants.IEIO_FILE:
2884 (filename, ) = ieargs
2886 if not utils.IsNormAbsPath(filename):
2887 _Fail("Path '%s' is not normalized or absolute", filename)
2889 real_filename = os.path.realpath(filename)
2890 directory = os.path.dirname(real_filename)
2892 if not utils.IsBelowDir(constants.EXPORT_DIR, real_filename):
2893 _Fail("File '%s' is not under exports directory '%s': %s",
2894 filename, constants.EXPORT_DIR, real_filename)
2897 utils.Makedirs(directory, mode=0750)
2899 quoted_filename = utils.ShellQuote(filename)
2901 if mode == constants.IEM_IMPORT:
2902 suffix = "> %s" % quoted_filename
2903 elif mode == constants.IEM_EXPORT:
2904 suffix = "< %s" % quoted_filename
2906 # Retrieve file size
2908 st = os.stat(filename)
2909 except EnvironmentError, err:
2910 logging.error("Can't stat(2) %s: %s", filename, err)
2912 exp_size = utils.BytesToMebibyte(st.st_size)
2914 elif ieio == constants.IEIO_RAW_DISK:
2917 real_disk = _OpenRealBD(disk)
2919 if mode == constants.IEM_IMPORT:
2920 # we set here a smaller block size as, due to transport buffering, more
2921 # than 64-128k will mostly ignored; we use nocreat to fail if the device
2922 # is not already there or we pass a wrong path; we use notrunc to no
2923 # attempt truncate on an LV device; we use oflag=dsync to not buffer too
2924 # much memory; this means that at best, we flush every 64k, which will
2926 suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
2927 " bs=%s oflag=dsync"),
2931 elif mode == constants.IEM_EXPORT:
2932 # the block size on the read dd is 1MiB to match our units
2933 prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
2935 str(1024 * 1024), # 1 MB
2937 exp_size = disk.size
2939 elif ieio == constants.IEIO_SCRIPT:
2940 (disk, disk_index, ) = ieargs
2942 assert isinstance(disk_index, (int, long))
2944 real_disk = _OpenRealBD(disk)
2946 inst_os = OSFromDisk(instance.os)
2947 env = OSEnvironment(instance, inst_os)
2949 if mode == constants.IEM_IMPORT:
2950 env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
2951 env["IMPORT_INDEX"] = str(disk_index)
2952 script = inst_os.import_script
2954 elif mode == constants.IEM_EXPORT:
2955 env["EXPORT_DEVICE"] = real_disk.dev_path
2956 env["EXPORT_INDEX"] = str(disk_index)
2957 script = inst_os.export_script
2959 # TODO: Pass special environment only to script
2960 script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
2962 if mode == constants.IEM_IMPORT:
2963 suffix = "| %s" % script_cmd
2965 elif mode == constants.IEM_EXPORT:
2966 prefix = "%s |" % script_cmd
2968 # Let script predict size
2969 exp_size = constants.IE_CUSTOM_SIZE
2972 _Fail("Invalid %s I/O mode %r", mode, ieio)
2974 return (env, prefix, suffix, exp_size)
2977 def _CreateImportExportStatusDir(prefix):
2978 """Creates status directory for import/export.
2981 return tempfile.mkdtemp(dir=constants.IMPORT_EXPORT_DIR,
2983 (prefix, utils.TimestampForFilename())))
2986 def StartImportExportDaemon(mode, opts, host, port, instance, component,
2988 """Starts an import or export daemon.
2990 @param mode: Import/output mode
2991 @type opts: L{objects.ImportExportOptions}
2992 @param opts: Daemon options
2994 @param host: Remote host for export (None for import)
2996 @param port: Remote port for export (None for import)
2997 @type instance: L{objects.Instance}
2998 @param instance: Instance object
2999 @type component: string
3000 @param component: which part of the instance is transferred now,
3002 @param ieio: Input/output type
3003 @param ieioargs: Input/output arguments
3006 if mode == constants.IEM_IMPORT:
3009 if not (host is None and port is None):
3010 _Fail("Can not specify host or port on import")
3012 elif mode == constants.IEM_EXPORT:
3015 if host is None or port is None:
3016 _Fail("Host and port must be specified for an export")
3019 _Fail("Invalid mode %r", mode)
3021 if (opts.key_name is None) ^ (opts.ca_pem is None):
3022 _Fail("Cluster certificate can only be used for both key and CA")
3024 (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
3025 _GetImportExportIoCommand(instance, mode, ieio, ieioargs)
3027 if opts.key_name is None:
3029 key_path = constants.NODED_CERT_FILE
3030 cert_path = constants.NODED_CERT_FILE
3031 assert opts.ca_pem is None
3033 (_, key_path, cert_path) = _GetX509Filenames(constants.CRYPTO_KEYS_DIR,
3035 assert opts.ca_pem is not None
3037 for i in [key_path, cert_path]:
3038 if not os.path.exists(i):
3039 _Fail("File '%s' does not exist" % i)
3041 status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
3043 status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
3044 pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
3045 ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
3047 if opts.ca_pem is None:
3049 ca = utils.ReadFile(constants.NODED_CERT_FILE)
3054 utils.WriteFile(ca_file, data=ca, mode=0400)
3057 constants.IMPORT_EXPORT_DAEMON,
3059 "--key=%s" % key_path,
3060 "--cert=%s" % cert_path,
3061 "--ca=%s" % ca_file,
3065 cmd.append("--host=%s" % host)
3068 cmd.append("--port=%s" % port)
3071 cmd.append("--ipv6")
3073 cmd.append("--ipv4")
3076 cmd.append("--compress=%s" % opts.compress)
3079 cmd.append("--magic=%s" % opts.magic)
3081 if exp_size is not None:
3082 cmd.append("--expected-size=%s" % exp_size)
3085 cmd.append("--cmd-prefix=%s" % cmd_prefix)
3088 cmd.append("--cmd-suffix=%s" % cmd_suffix)
3090 if mode == constants.IEM_EXPORT:
3091 # Retry connection a few times when connecting to remote peer
3092 cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
3093 cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
3094 elif opts.connect_timeout is not None:
3095 assert mode == constants.IEM_IMPORT
3096 # Overall timeout for establishing connection while listening
3097 cmd.append("--connect-timeout=%s" % opts.connect_timeout)
3099 logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
3101 # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
3102 # support for receiving a file descriptor for output
3103 utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
3106 # The import/export name is simply the status directory name
3107 return os.path.basename(status_dir)
3110 shutil.rmtree(status_dir, ignore_errors=True)
3114 def GetImportExportStatus(names):
3115 """Returns import/export daemon status.
3117 @type names: sequence
3118 @param names: List of names
3119 @rtype: List of dicts
3120 @return: Returns a list of the state of each named import/export or None if a
3121 status couldn't be read
3127 status_file = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name,
3131 data = utils.ReadFile(status_file)
3132 except EnvironmentError, err:
3133 if err.errno != errno.ENOENT:
3141 result.append(serializer.LoadJson(data))
3146 def AbortImportExport(name):
3147 """Sends SIGTERM to a running import/export daemon.
3150 logging.info("Abort import/export %s", name)
3152 status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3153 pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3156 logging.info("Import/export %s is running with PID %s, sending SIGTERM",
3158 utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
3161 def CleanupImportExport(name):
3162 """Cleanup after an import or export.
3164 If the import/export daemon is still running it's killed. Afterwards the
3165 whole status directory is removed.
3168 logging.info("Finalizing import/export %s", name)
3170 status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3172 pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3175 logging.info("Import/export %s is still running with PID %s",
3177 utils.KillProcess(pid, waitpid=False)
3179 shutil.rmtree(status_dir, ignore_errors=True)
3182 def _FindDisks(nodes_ip, disks):
3183 """Sets the physical ID on disks and returns the block devices.
3186 # set the correct physical ID
3187 my_name = netutils.Hostname.GetSysName()
3189 cf.SetPhysicalID(my_name, nodes_ip)
3194 rd = _RecursiveFindBD(cf)
3196 _Fail("Can't find device %s", cf)
3201 def DrbdDisconnectNet(nodes_ip, disks):
3202 """Disconnects the network on a list of drbd devices.
3205 bdevs = _FindDisks(nodes_ip, disks)
3211 except errors.BlockDeviceError, err:
3212 _Fail("Can't change network configuration to standalone mode: %s",
3216 def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
3217 """Attaches the network on a list of drbd devices.
3220 bdevs = _FindDisks(nodes_ip, disks)
3223 for idx, rd in enumerate(bdevs):
3225 _SymlinkBlockDev(instance_name, rd.dev_path, idx)
3226 except EnvironmentError, err:
3227 _Fail("Can't create symlink: %s", err)
3228 # reconnect disks, switch to new master configuration and if
3229 # needed primary mode
3232 rd.AttachNet(multimaster)
3233 except errors.BlockDeviceError, err:
3234 _Fail("Can't change network configuration: %s", err)
3236 # wait until the disks are connected; we need to retry the re-attach
3237 # if the device becomes standalone, as this might happen if the one
3238 # node disconnects and reconnects in a different mode before the
3239 # other node reconnects; in this case, one or both of the nodes will
3240 # decide it has wrong configuration and switch to standalone
3243 all_connected = True
3246 stats = rd.GetProcStatus()
3248 all_connected = (all_connected and
3249 (stats.is_connected or stats.is_in_resync))
3251 if stats.is_standalone:
3252 # peer had different config info and this node became
3253 # standalone, even though this should not happen with the
3254 # new staged way of changing disk configs
3256 rd.AttachNet(multimaster)
3257 except errors.BlockDeviceError, err:
3258 _Fail("Can't change network configuration: %s", err)
3260 if not all_connected:
3261 raise utils.RetryAgain()
3264 # Start with a delay of 100 miliseconds and go up to 5 seconds
3265 utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
3266 except utils.RetryTimeout:
3267 _Fail("Timeout in disk reconnecting")
3270 # change to primary mode
3274 except errors.BlockDeviceError, err:
3275 _Fail("Can't change to primary mode: %s", err)
3278 def DrbdWaitSync(nodes_ip, disks):
3279 """Wait until DRBDs have synchronized.
3283 stats = rd.GetProcStatus()
3284 if not (stats.is_connected or stats.is_in_resync):
3285 raise utils.RetryAgain()
3288 bdevs = _FindDisks(nodes_ip, disks)
3294 # poll each second for 15 seconds
3295 stats = utils.Retry(_helper, 1, 15, args=[rd])
3296 except utils.RetryTimeout:
3297 stats = rd.GetProcStatus()
3299 if not (stats.is_connected or stats.is_in_resync):
3300 _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
3301 alldone = alldone and (not stats.is_in_resync)
3302 if stats.sync_percent is not None:
3303 min_resync = min(min_resync, stats.sync_percent)
3305 return (alldone, min_resync)
3308 def GetDrbdUsermodeHelper():
3309 """Returns DRBD usermode helper currently configured.
3313 return bdev.BaseDRBD.GetUsermodeHelper()
3314 except errors.BlockDeviceError, err:
3318 def PowercycleNode(hypervisor_type):
3319 """Hard-powercycle the node.
3321 Because we need to return first, and schedule the powercycle in the
3322 background, we won't be able to report failures nicely.
3325 hyper = hypervisor.GetHypervisor(hypervisor_type)
3329 # if we can't fork, we'll pretend that we're in the child process
3332 return "Reboot scheduled in 5 seconds"
3333 # ensure the child is running on ram
3336 except Exception: # pylint: disable=W0703
3339 hyper.PowercycleNode()
3342 class HooksRunner(object):
3345 This class is instantiated on the node side (ganeti-noded) and not
3349 def __init__(self, hooks_base_dir=None):
3350 """Constructor for hooks runner.
3352 @type hooks_base_dir: str or None
3353 @param hooks_base_dir: if not None, this overrides the
3354 L{constants.HOOKS_BASE_DIR} (useful for unittests)
3357 if hooks_base_dir is None:
3358 hooks_base_dir = constants.HOOKS_BASE_DIR
3359 # yeah, _BASE_DIR is not valid for attributes, we use it like a
3361 self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3363 def RunHooks(self, hpath, phase, env):
3364 """Run the scripts in the hooks directory.
3367 @param hpath: the path to the hooks directory which
3370 @param phase: either L{constants.HOOKS_PHASE_PRE} or
3371 L{constants.HOOKS_PHASE_POST}
3373 @param env: dictionary with the environment for the hook
3375 @return: list of 3-element tuples:
3377 - script result, either L{constants.HKR_SUCCESS} or
3378 L{constants.HKR_FAIL}
3379 - output of the script
3381 @raise errors.ProgrammerError: for invalid input
3385 if phase == constants.HOOKS_PHASE_PRE:
3387 elif phase == constants.HOOKS_PHASE_POST:
3390 _Fail("Unknown hooks phase '%s'", phase)
3392 subdir = "%s-%s.d" % (hpath, suffix)
3393 dir_name = utils.PathJoin(self._BASE_DIR, subdir)
3397 if not os.path.isdir(dir_name):
3398 # for non-existing/non-dirs, we simply exit instead of logging a
3399 # warning at every operation
3402 runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
3404 for (relname, relstatus, runresult) in runparts_results:
3405 if relstatus == constants.RUNPARTS_SKIP:
3406 rrval = constants.HKR_SKIP
3408 elif relstatus == constants.RUNPARTS_ERR:
3409 rrval = constants.HKR_FAIL
3410 output = "Hook script execution error: %s" % runresult
3411 elif relstatus == constants.RUNPARTS_RUN:
3412 if runresult.failed:
3413 rrval = constants.HKR_FAIL
3415 rrval = constants.HKR_SUCCESS
3416 output = utils.SafeEncode(runresult.output.strip())
3417 results.append(("%s/%s" % (subdir, relname), rrval, output))
3422 class IAllocatorRunner(object):
3423 """IAllocator runner.
3425 This class is instantiated on the node side (ganeti-noded) and not on
3430 def Run(name, idata):
3431 """Run an iallocator script.
3434 @param name: the iallocator script name
3436 @param idata: the allocator input data
3439 @return: two element tuple of:
3441 - either error message or stdout of allocator (for success)
3444 alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
3446 if alloc_script is None:
3447 _Fail("iallocator module '%s' not found in the search path", name)
3449 fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
3453 result = utils.RunCmd([alloc_script, fin_name])
3455 _Fail("iallocator module '%s' failed: %s, output '%s'",
3456 name, result.fail_reason, result.output)
3460 return result.stdout
3463 class DevCacheManager(object):
3464 """Simple class for managing a cache of block device information.
3467 _DEV_PREFIX = "/dev/"
3468 _ROOT_DIR = constants.BDEV_CACHE_DIR
3471 def _ConvertPath(cls, dev_path):
3472 """Converts a /dev/name path to the cache file name.
3474 This replaces slashes with underscores and strips the /dev
3475 prefix. It then returns the full path to the cache file.
3478 @param dev_path: the C{/dev/} path name
3480 @return: the converted path name
3483 if dev_path.startswith(cls._DEV_PREFIX):
3484 dev_path = dev_path[len(cls._DEV_PREFIX):]
3485 dev_path = dev_path.replace("/", "_")
3486 fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
3490 def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
3491 """Updates the cache information for a given device.
3494 @param dev_path: the pathname of the device
3496 @param owner: the owner (instance name) of the device
3497 @type on_primary: bool
3498 @param on_primary: whether this is the primary
3501 @param iv_name: the instance-visible name of the
3502 device, as in objects.Disk.iv_name
3507 if dev_path is None:
3508 logging.error("DevCacheManager.UpdateCache got a None dev_path")
3510 fpath = cls._ConvertPath(dev_path)
3516 iv_name = "not_visible"
3517 fdata = "%s %s %s\n" % (str(owner), state, iv_name)
3519 utils.WriteFile(fpath, data=fdata)
3520 except EnvironmentError, err:
3521 logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
3524 def RemoveCache(cls, dev_path):
3525 """Remove data for a dev_path.
3527 This is just a wrapper over L{utils.io.RemoveFile} with a converted
3528 path name and logging.
3531 @param dev_path: the pathname of the device
3536 if dev_path is None:
3537 logging.error("DevCacheManager.RemoveCache got a None dev_path")
3539 fpath = cls._ConvertPath(dev_path)
3541 utils.RemoveFile(fpath)
3542 except EnvironmentError, err:
3543 logging.exception("Can't update bdev cache for %s: %s", dev_path, err)