Files
vms/nix/cache.nix
T

48 lines
1.2 KiB
Nix
Raw Normal View History

2024-06-26 20:57:47 -05:00
{
2024-11-18 21:22:32 -06:00
writeShellApplication,
active,
distroFile,
2024-06-26 20:57:47 -05:00
2024-11-18 21:22:32 -06:00
curl,
minio-client,
packer,
2024-06-26 20:57:47 -05:00
2024-11-18 21:22:32 -06:00
...
2024-06-26 20:57:47 -05:00
}:
let
2024-11-18 21:22:32 -06:00
inherit (builtins) concatStringsSep map;
in
writeShellApplication {
name = "cache-isos";
runtimeInputs = [
curl
minio-client
packer
];
text =
''
push="''${1:-yes}"
if [ "$push" == "yes" ]; then
mc alias set storage "$STORAGE_URL" root "$MINIO_SECRET"
fi
mkdir -p cache
''
+ (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/)
distro=$(echo var.distro | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/)
# shellcheck disable=SC2143
if [ -z "$(mc ls "storage/isos/cache/$distro" | grep "$sha")" ]; then
curl -L --retry 3 --retry-all-errors -o "cache/$sha.iso" "$url"
if [ "$push" == "yes" ]; then
mc put "cache/$sha.iso" "storage/isos/cache/$distro/$sha.iso"
fi
else
echo "Skipping ${d.distro} - cache/$sha.iso"
fi
'') active
));
2024-06-26 20:57:47 -05:00
}