From bc1f23a47b6086cc8b1ab2d83a682dad5d76293b Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Wed, 26 Jun 2024 20:57:47 -0500 Subject: [PATCH] Better cache file naming --- cache.sh | 32 -------------------------- flake.nix | 10 +-------- nix/cache.nix | 41 ++++++++++++++++++++++++++++++++++ sources/hyperv-iso.pkr.hcl | 4 ++-- sources/qemu.pkr.hcl | 4 ++-- sources/virtualbox-iso.pkr.hcl | 4 ++-- 6 files changed, 48 insertions(+), 47 deletions(-) delete mode 100755 cache.sh create mode 100644 nix/cache.nix diff --git a/cache.sh b/cache.sh deleted file mode 100755 index ae64694..0000000 --- a/cache.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail - -files=() - -# These files are always out of date -rm -f cache/*latest* cache/*Rawhide* cache/openSUSE*Current* - -# shellcheck disable=SC2044 -for vars in $(find distros/ -name '*.pkrvars.hcl'); do - file=$(echo var.iso.url | packer console -var-file="${vars}" -config-type=hcl2 sources/) - base="$(basename "${file}")" - if [ -e "cache/${base}" ]; then - echo "Found cache/${base} already" - else - files+=("--retry" "3" "--retry-all-errors" "-O" "${file}") - fi -done - -mkdir -p cache -pushd cache -echo "Downloading ${files[*]}" -curl --parallel -L "${files[@]}" -popd - -ls -lh cache/ - -mc alias set storage "$STORAGE_URL" root "$MINIO_SECRET" - -for f in cache/*; do - mc put "$f" "storage/isos/$f" -done diff --git a/flake.nix b/flake.nix index 5cb00e5..e2ebdf8 100644 --- a/flake.nix +++ b/flake.nix @@ -108,15 +108,7 @@ (builtins.replaceStrings ["QEMU_MATRIX" "VBOX_MATRIX" "HYPERV_MATRIX"] [qemu_list vbox_list hyperv_list] (builtins.readFile ./.gitlab-ci-build.yml.in) ); - cache = pkgs.writeShellApplication { - name = "cache-isos"; - runtimeInputs = with pkgs; [ - curl - minio-client - packer - ]; - text = builtins.readFile ./cache.sh; - }; + cache = pkgs.callPackage (import ./nix/cache.nix) { inherit active distroFile; }; cache-restore = pkgs.writeShellApplication { name = "cache-restore"; runtimeInputs = with pkgs; [ diff --git a/nix/cache.nix b/nix/cache.nix new file mode 100644 index 0000000..33ea53f --- /dev/null +++ b/nix/cache.nix @@ -0,0 +1,41 @@ +{ + writeShellApplication, + active, + distroFile, + + curl, + minio-client, + packer, + + ... +}: + +let + inherit (builtins) concatStringsSep map; +in writeShellApplication { + name = "cache-isos"; + runtimeInputs = [ + curl + minio-client + packer + ]; + text = (concatStringsSep "\n" ( + map (d: '' + url=$(echo var.iso.url | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) + sha=$(echo var.iso.checksum | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) + if [ ! -e "cache/$sha.iso" ]; then + curl --retry 3 --retry-all-errors -o "cache/$sha.iso" "$url" + else + echo "Skipping ${d.distro} - cache/$sha.iso" + fi + '') active + )) + '' + ls -lh cache/ + + mc alias set storage "$STORAGE_URL" root "$MINIO_SECRET" + + for f in cache/*; do + mc put "$f" "storage/isos/$f" + done + ''; +} diff --git a/sources/hyperv-iso.pkr.hcl b/sources/hyperv-iso.pkr.hcl index fd4ba7e..0ce8ff9 100644 --- a/sources/hyperv-iso.pkr.hcl +++ b/sources/hyperv-iso.pkr.hcl @@ -1,6 +1,6 @@ source "hyperv-iso" "amd64" { - iso_urls = [ "s3::${var.storage_url}/isos/cache/${basename(var.iso.url)}?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] - iso_target_path = "cache/${basename(var.iso.url)}" + iso_urls = [ "s3::${var.storage_url}/isos/cache/${var.iso.checksum}.iso?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] + iso_target_path = "cache/${var.iso.checksum}.iso" iso_checksum = var.iso.checksum output_directory = "output/hyperv/${local.name}" diff --git a/sources/qemu.pkr.hcl b/sources/qemu.pkr.hcl index c579021..dffafc8 100644 --- a/sources/qemu.pkr.hcl +++ b/sources/qemu.pkr.hcl @@ -4,8 +4,8 @@ variable "qemu_accelerator" { } source "qemu" "amd64" { - iso_urls = [ "s3::${var.storage_url}/isos/cache/${basename(var.iso.url)}?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] - iso_target_path = "cache/${basename(var.iso.url)}" + iso_urls = [ "s3::${var.storage_url}/isos/cache/${var.iso.checksum}.iso?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] + iso_target_path = "cache/${var.iso.checksum}.iso" iso_checksum = var.iso.checksum output_directory = "output/libvirt/${local.name}" diff --git a/sources/virtualbox-iso.pkr.hcl b/sources/virtualbox-iso.pkr.hcl index 541e207..e2c9a56 100644 --- a/sources/virtualbox-iso.pkr.hcl +++ b/sources/virtualbox-iso.pkr.hcl @@ -1,6 +1,6 @@ source "virtualbox-iso" "amd64" { - iso_urls = [ "s3::${var.storage_url}/isos/cache/${basename(var.iso.url)}?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] - iso_target_path = "cache/${basename(var.iso.url)}" + iso_urls = [ "s3::${var.storage_url}/isos/cache/${var.iso.checksum}.iso?aws_access_key_id=root&aws_access_key_secret=${var.minio_secret}" ] + iso_target_path = "cache/${var.iso.checksum}.iso" iso_checksum = var.iso.checksum output_directory = "output/virtualbox/${local.name}"