download_hash: argument handling with argparse

Allow the script to be called with a list of components, to only
download new versions checksums for those.
By default, we get new versions checksums for all supported (by the
script) components.
pull/11513/head
Max Gautier 2024-09-05 15:58:36 +02:00
parent da0e445d69
commit 2710e984c8
No known key found for this signature in database
1 changed files with 16 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import sys
from itertools import count, groupby from itertools import count, groupby
from collections import defaultdict from collections import defaultdict
import argparse
import requests import requests
from ruamel.yaml import YAML from ruamel.yaml import YAML
from packaging.version import Version from packaging.version import Version
@ -28,14 +29,15 @@ def open_checksums_yaml():
def version_compare(version): def version_compare(version):
return Version(version.removeprefix("v")) return Version(version.removeprefix("v"))
def download_hash(minors): downloads = {
downloads = { "containerd_archive": "https://github.com/containerd/containerd/releases/download/v{version}/containerd-{version}-{os}-{arch}.tar.gz.sha256sum",
"containerd_archive": "https://github.com/containerd/containerd/releases/download/v{version}/containerd-{version}-{os}-{arch}.tar.gz.sha256sum", "kubeadm": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubeadm.sha256",
"kubeadm": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubeadm.sha256", "kubectl": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubectl.sha256",
"kubectl": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubectl.sha256", "kubelet": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubelet.sha256",
"kubelet": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubelet.sha256", "runc": "https://github.com/opencontainers/runc/releases/download/{version}/runc.sha256sum",
"runc": "https://github.com/opencontainers/runc/releases/download/{version}/runc.sha256sum", }
}
def download_hash(only_downloads: [str]) -> None:
# Handle hashes not directly in one url per hash. Return dict of hashs indexed by arch # Handle hashes not directly in one url per hash. Return dict of hashs indexed by arch
download_hash_extract = { download_hash_extract = {
"runc": lambda hashes : { "runc": lambda hashes : {
@ -47,7 +49,8 @@ def download_hash(minors):
data, yaml = open_checksums_yaml() data, yaml = open_checksums_yaml()
for download, url in downloads.items(): for download, url in (downloads if only_downloads == []
else {k:downloads[k] for k in downloads.keys() & only_downloads}).items():
checksum_name = f"{download}_checksums" checksum_name = f"{download}_checksums"
for arch, versions in data[checksum_name].items(): for arch, versions in data[checksum_name].items():
for minor, patches in groupby(versions.copy().keys(), lambda v : '.'.join(v.split('.')[:-1])): for minor, patches in groupby(versions.copy().keys(), lambda v : '.'.join(v.split('.')[:-1])):
@ -88,15 +91,8 @@ def download_hash(minors):
yaml.dump(data, checksums_yml) yaml.dump(data, checksums_yml)
print(f"\n\nUpdated {CHECKSUMS_YML}\n") print(f"\n\nUpdated {CHECKSUMS_YML}\n")
parser = argparse.ArgumentParser(description=f"Add new patch versions hashes in {CHECKSUMS_YML}")
parser.add_argument('binaries', nargs='*', choices=downloads.keys())
def usage(): args = parser.parse_args()
print(f"USAGE:\n {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]") download_hash(args.binaries)
def main(argv=None):
download_hash(sys.argv[1:])
return 0
if __name__ == "__main__":
sys.exit(main())