You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.8 KiB

  1. #!/usr/bin/env python3
  2. # After a new version of Kubernetes has been released,
  3. # run this script to update roles/kubespray-defaults/defaults/main/download.yml
  4. # with new hashes.
  5. import sys
  6. from itertools import count
  7. from collections import defaultdict
  8. import requests
  9. from ruamel.yaml import YAML
  10. from packaging.version import Version
  11. CHECKSUMS_YML = "../roles/kubespray-defaults/defaults/main/checksums.yml"
  12. def open_checksums_yaml():
  13. yaml = YAML()
  14. yaml.explicit_start = True
  15. yaml.preserve_quotes = True
  16. yaml.width = 4096
  17. with open(CHECKSUMS_YML, "r") as checksums_yml:
  18. data = yaml.load(checksums_yml)
  19. return data, yaml
  20. def download_hash(minors):
  21. architectures = ["arm", "arm64", "amd64", "ppc64le"]
  22. downloads = ["kubelet", "kubectl", "kubeadm"]
  23. data, yaml = open_checksums_yaml()
  24. if not minors:
  25. minors = {'.'.join(minor.split('.')[:-1]) for minor in data["kubelet_checksums"]["amd64"].keys()}
  26. for download in downloads:
  27. checksum_name = f"{download}_checksums"
  28. data[checksum_name] = defaultdict(dict, data[checksum_name])
  29. for arch in architectures:
  30. for minor in minors:
  31. if not minor.startswith("v"):
  32. minor = f"v{minor}"
  33. for release in (f"{minor}.{patch}" for patch in count(start=0, step=1)):
  34. if release in data[checksum_name][arch]:
  35. continue
  36. hash_file = requests.get(f"https://dl.k8s.io/release/{release}/bin/linux/{arch}/{download}.sha256", allow_redirects=True)
  37. if hash_file.status_code == 404:
  38. print(f"Unable to find {download} hash file for release {release} (arch: {arch})")
  39. break
  40. hash_file.raise_for_status()
  41. sha256sum = hash_file.content.decode().strip()
  42. if len(sha256sum) != 64:
  43. raise Exception(f"Checksum has an unexpected length: {len(sha256sum)} (binary: {download}, arch: {arch}, release: 1.{minor}.{patch})")
  44. data[checksum_name][arch][release] = sha256sum
  45. data[checksum_name] = {arch : {r : releases[r] for r in sorted(releases.keys(),
  46. key=lambda v : Version(v[1:]),
  47. reverse=True)}
  48. for arch, releases in data[checksum_name].items()}
  49. with open(CHECKSUMS_YML, "w") as checksums_yml:
  50. yaml.dump(data, checksums_yml)
  51. print(f"\n\nUpdated {CHECKSUMS_YML}\n")
  52. def usage():
  53. print(f"USAGE:\n {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]")
  54. def main(argv=None):
  55. download_hash(sys.argv[1:])
  56. return 0
  57. if __name__ == "__main__":
  58. sys.exit(main())