Skip to content

Commit

Permalink
download_hash: argument handling with argparse
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VannTen committed Sep 8, 2024
1 parent da0e445 commit 2710e98
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions scripts/download_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from itertools import count, groupby
from collections import defaultdict
import argparse
import requests
from ruamel.yaml import YAML
from packaging.version import Version
Expand All @@ -28,14 +29,15 @@ def open_checksums_yaml():
def version_compare(version):
return Version(version.removeprefix("v"))

def download_hash(minors):
downloads = {
"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",
"kubectl": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubectl.sha256",
"kubelet": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubelet.sha256",
"runc": "https://github.com/opencontainers/runc/releases/download/{version}/runc.sha256sum",
}
downloads = {
"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",
"kubectl": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubectl.sha256",
"kubelet": "https://dl.k8s.io/release/{version}/bin/linux/{arch}/kubelet.sha256",
"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
download_hash_extract = {
"runc": lambda hashes : {
Expand All @@ -47,7 +49,8 @@ def download_hash(minors):

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"
for arch, versions in data[checksum_name].items():
for minor, patches in groupby(versions.copy().keys(), lambda v : '.'.join(v.split('.')[:-1])):
Expand Down Expand Up @@ -88,15 +91,8 @@ def download_hash(minors):
yaml.dump(data, checksums_yml)
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():
print(f"USAGE:\n {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]")


def main(argv=None):
download_hash(sys.argv[1:])
return 0


if __name__ == "__main__":
sys.exit(main())
args = parser.parse_args()
download_hash(args.binaries)

0 comments on commit 2710e98

Please sign in to comment.