Skip to content

Commit

Permalink
Inital nixcache test
Browse files Browse the repository at this point in the history
Signed-off-by: karim mdmirajul <karim.mdmirajul@unikie.com>
  • Loading branch information
karim20230 committed Apr 10, 2024
1 parent 8149653 commit dd7d2ff
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions terraform/tests/nix_cache.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dd7d2ff

Please sign in to comment.