disk_availability.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # pylint: disable=missing-docstring
  2. from openshift_checks import OpenShiftCheck, OpenShiftCheckException, get_var
  3. from openshift_checks.mixins import NotContainerizedMixin
  4. class DiskAvailability(NotContainerizedMixin, OpenShiftCheck):
  5. """Check that recommended disk space is available before a first-time install."""
  6. name = "disk_availability"
  7. tags = ["preflight"]
  8. # all values are base-10 as they are taken, as is, from
  9. # the latest requirements for an OpenShift installation
  10. # https://docs.openshift.org/latest/install_config/install/prerequisites.html#system-requirements
  11. recommended_diskspace = {
  12. "nodes": 15 * 10 ** 9,
  13. "masters": 40 * 10 ** 9,
  14. "etcd": 20 * 10 ** 9,
  15. }
  16. def run(self, tmp, task_vars):
  17. ansible_mounts = get_var(task_vars, "ansible_mounts")
  18. self.recommended_diskspace["nodes"] = get_var(task_vars,
  19. "min_recommended_diskspace_node",
  20. default=self.recommended_diskspace["nodes"])
  21. self.recommended_diskspace["masters"] = get_var(task_vars,
  22. "min_recommended_diskspace_master",
  23. default=self.recommended_diskspace["masters"])
  24. self.recommended_diskspace["etcd"] = get_var(task_vars,
  25. "min_recommended_diskspace_etcd",
  26. default=self.recommended_diskspace["etcd"])
  27. failed, msg = self.volume_check(ansible_mounts, task_vars)
  28. return {"failed": failed, "msg": msg}
  29. def volume_check(self, ansible_mounts, task_vars):
  30. group_names = get_var(task_vars, "group_names", default=[])
  31. if not set(self.recommended_diskspace).intersection(group_names):
  32. msg = "Unable to determine recommended volume size for group_name {group_name}"
  33. raise OpenShiftCheckException(msg.format(group_name=group_names))
  34. recommended_diskspace_bytes = max(self.recommended_diskspace.get(group, 0) for group in group_names)
  35. openshift_diskfree_bytes = self.get_openshift_disk_availability(ansible_mounts)
  36. if openshift_diskfree_bytes < recommended_diskspace_bytes:
  37. msg = ("Available disk space ({diskfree} GB) for the volume containing \"/var\" is "
  38. "below recommended storage. Minimum required disk space: {recommended} GB")
  39. return True, msg.format(diskfree=self.to_gigabytes(openshift_diskfree_bytes),
  40. recommended=self.to_gigabytes(recommended_diskspace_bytes))
  41. return False, ""
  42. @staticmethod
  43. def get_openshift_disk_availability(ansible_mounts):
  44. """Iterates through a map of mounted volumes to determine space remaining on the OpenShift volume"""
  45. if not ansible_mounts:
  46. msg = "Unable to determine existing volume mounts from ansible_mounts"
  47. raise OpenShiftCheckException(msg)
  48. # priority list in descending order
  49. supported_mnt_paths = ["/var", "/"]
  50. available_mnts = {mnt.get("mount"): mnt for mnt in ansible_mounts}
  51. for path in supported_mnt_paths:
  52. if path in available_mnts:
  53. return available_mnts[path].get("size_available")
  54. return 0
  55. @staticmethod
  56. def to_gigabytes(total_bytes):
  57. return total_bytes / 10**9