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.

65 lines
1.7 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 hashlib
  6. import sys
  7. import requests
  8. from ruamel.yaml import YAML
  9. MAIN_YML = "../roles/kubespray-defaults/defaults/main/download.yml"
  10. def open_main_yaml():
  11. yaml = YAML()
  12. yaml.explicit_start = True
  13. yaml.preserve_quotes = True
  14. yaml.width = 4096
  15. with open(MAIN_YML, "r") as main_yml:
  16. data = yaml.load(main_yml)
  17. return data, yaml
  18. def download_hash(versions):
  19. architectures = ["arm", "arm64", "amd64", "ppc64le"]
  20. downloads = ["kubelet", "kubectl", "kubeadm"]
  21. data, yaml = open_main_yaml()
  22. for download in downloads:
  23. checksum_name = f"{download}_checksums"
  24. for arch in architectures:
  25. for version in versions:
  26. if not version.startswith("v"):
  27. version = f"v{version}"
  28. url = f"https://dl.k8s.io/release/{version}/bin/linux/{arch}/{download}"
  29. download_file = requests.get(url, allow_redirects=True)
  30. download_file.raise_for_status()
  31. sha256sum = hashlib.sha256(download_file.content).hexdigest()
  32. data[checksum_name][arch][version] = sha256sum
  33. with open(MAIN_YML, "w") as main_yml:
  34. yaml.dump(data, main_yml)
  35. print(f"\n\nUpdated {MAIN_YML}\n")
  36. def usage():
  37. print(f"USAGE:\n {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]")
  38. def main(argv=None):
  39. if not argv:
  40. argv = sys.argv[1:]
  41. if not argv:
  42. usage()
  43. return 1
  44. download_hash(argv)
  45. return 0
  46. if __name__ == "__main__":
  47. sys.exit(main())