Merge "Add O2 IMS compliance automation test scripts."
[it/test.git] / XTesting / kubespray / scripts / download_hash.py
1 #!/usr/bin/env python3
2
3 # After a new version of Kubernetes has been released,
4 # run this script to update roles/download/defaults/main.yml
5 # with new hashes.
6
7 import hashlib
8 import sys
9
10 import requests
11 from ruamel.yaml import YAML
12
13 MAIN_YML = "../roles/download/defaults/main.yml"
14
15 def open_main_yaml():
16     yaml = YAML()
17     yaml.explicit_start = True
18     yaml.preserve_quotes = True
19     yaml.width = 4096
20
21     with open(MAIN_YML, "r") as main_yml:
22         data = yaml.load(main_yml)
23
24     return data, yaml
25
26
27 def download_hash(versions):
28     architectures = ["arm", "arm64", "amd64", "ppc64le"]
29     downloads = ["kubelet", "kubectl", "kubeadm"]
30
31     data, yaml = open_main_yaml()
32
33     for download in downloads:
34         checksum_name = f"{download}_checksums"
35         for arch in architectures:
36             for version in versions:
37                 if not version.startswith("v"):
38                     version = f"v{version}"
39                 url = f"https://storage.googleapis.com/kubernetes-release/release/{version}/bin/linux/{arch}/{download}"
40                 download_file = requests.get(url, allow_redirects=True)
41                 download_file.raise_for_status()
42                 sha256sum = hashlib.sha256(download_file.content).hexdigest()
43                 data[checksum_name][arch][version] = sha256sum
44
45     with open(MAIN_YML, "w") as main_yml:
46         yaml.dump(data, main_yml)
47         print(f"\n\nUpdated {MAIN_YML}\n")
48
49
50 def usage():
51     print(f"USAGE:\n    {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]")
52
53
54 def main(argv=None):
55     if not argv:
56         argv = sys.argv[1:]
57     if not argv:
58         usage()
59         return 1
60     download_hash(argv)
61     return 0
62
63
64 if __name__ == "__main__":
65     sys.exit(main())