From dd7d2ff347f4bb18bfe0d9276f3a789adcd66035 Mon Sep 17 00:00:00 2001 From: karim mdmirajul Date: Wed, 10 Apr 2024 16:30:20 +0300 Subject: [PATCH] Inital nixcache test Signed-off-by: karim mdmirajul --- terraform/tests/nix_cache.sh | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 terraform/tests/nix_cache.sh diff --git a/terraform/tests/nix_cache.sh b/terraform/tests/nix_cache.sh new file mode 100644 index 00000000..12de3912 --- /dev/null +++ b/terraform/tests/nix_cache.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -e + +URL=${URL:-"https://ghaf-binary-cache-dev.northeurope.cloudapp.azure.com/nix-cache-info"} + +function check_cache() { + output=$(curl -Ls "$URL") + if [ "$output" != "StoreDir: /nix/store" ]; then + echo "NixOS binary cache is not available or changed." + exit 1 + fi +} + +function extract_hash() { + if [ ! -z "$1" ]; then + hash=$1 + else + full_path=$(ls -la result | awk '{print $NF}') + hash=${full_path##*/} + hash=${hash%-nixos-disk-image} + fi + + if [[ ! "$hash" ]]; then + echo "Failed to extract hash." + exit 1 + fi + + echo $hash +} + +function get_narinfo() { + narinfo_url="https://ghaf-binary-cache-dev.northeurope.cloudapp.azure.com/$1.narinfo" + curl_output=$(curl -L "$narinfo_url" -s | grep -v "References") + + if [[ ! "$curl_output" ]]; then + echo "Failed to retrieve or process narinfo." + exit 1 + fi + + echo "$curl_output" | grep "URL:" | awk '{print $2}' +} + +function download_nar() { + nar_url="https://ghaf-binary-cache-dev.northeurope.cloudapp.azure.com/$1" + curl -L "$nar_url" -o nixos.img.nar.zst +} + +function decompress_nar() { + zstd --decompress nixos.img.nar.zst -o nixos.img.nar +} + +function restore_nar() { + cat nixos.img.nar | nix-store --restore imagedir +} + +check_cache +hash=$(extract_hash "$1") +nar_file_name=$(get_narinfo "$hash") +download_nar "$nar_file_name" +decompress_nar +restore_nar +ls -la imagedir \ No newline at end of file