switch-prebuilt: make latest slightly... better

This commit is contained in:
Luke Granger-Brown 2024-01-07 17:57:27 +00:00
parent d6638eb663
commit f7a8e1c6c9

View file

@ -1,22 +1,43 @@
# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
# SPDX-FileCopyrightText: 2024 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0
{ depot, system, pkgs, ... }:
pkgs.writeShellScriptBin "switch-prebuilt" ''
set -ue
pkgs.writeShellApplication {
name = "switch-prebuilt";
export AWS_ACCESS_KEY_ID="$(${pkgs.vault}/bin/vault kv get --address=unix:///run/tokend/sock -field=cacheAccessKeyID kv/apps/nix-daemon)"
export AWS_SECRET_ACCESS_KEY="$(${pkgs.vault}/bin/vault kv get --address=unix:///run/tokend/sock -field=cacheSecretAccessKey kv/apps/nix-daemon)"
runtimeInputs = with pkgs; [ curl unzip jq ];
text = ''
set -ue
system="''${1}"
if [[ "$system" == "latest" ]]; then
tmpdir="$(mktemp -d)"
trap '{ rm -rf -- "$tmpdir"; }' EXIT
${pkgs.curl}/bin/curl -so "$tmpdir/archive.zip" 'https://hg.lukegb.com/api/v4/projects/lukegb%2Fdepot/jobs/artifacts/branch%2Fdefault/download?job=nixCache-${system}'
${pkgs.unzip}/bin/unzip -d "$tmpdir" -q -o "$tmpdir/archive.zip"
system="$(${pkgs.jq}/bin/jq -r ".\"$(hostname)\"" "$tmpdir/systems.json")"
COMMIT_DATA="$(curl -s 'https://hg.lukegb.com/api/v4/projects/lukegb%2Fdepot/repository/commits/branch%2Fdefault')"
echo "Commit: $(echo "$COMMIT_DATA" | jq -r '.id') $(echo "$COMMIT_DATA" | jq -r '.title')"
if [[ "$(echo "$COMMIT_DATA" | jq -r '.last_pipeline.id')" == null ]]; then
echo "...but there's no CI pipeline for it"
exit 1
fi
PARENT_PIPELINE_BRIDGES="$(curl -s "https://hg.lukegb.com/api/v4/projects/lukegb%2Fdepot/pipelines/$(echo "$COMMIT_DATA" | jq -r '.last_pipeline.id')/bridges")"
if [[ "$(echo "$PARENT_PIPELINE_BRIDGES" | jq -r '. | length')" == 0 ]]; then
echo "...but there's no downstream pipeline for it"
exit 1
fi
CHILD_PIPELINE_ID="$(echo "$PARENT_PIPELINE_BRIDGES" | jq -r '.[].downstream_pipeline.id')"
CHILD_PIPELINE_JOBS="$(curl -s "https://hg.lukegb.com/api/v4/projects/lukegb%2Fdepot/pipelines/$CHILD_PIPELINE_ID/jobs?per_page=100")"
CACHE_JOB_ID="$(echo "$CHILD_PIPELINE_JOBS" | jq -r '.[] | select(.name == "nixCache-linux") | .id')"
if [[ "$CACHE_JOB_ID" == "" ]]; then
echo "...but there's no data for the cache job"
exit 1
fi
curl -so "$tmpdir/systems.json" "https://hg.lukegb.com/api/v4/projects/lukegb%2Fdepot/jobs/$CACHE_JOB_ID/artifacts/systems.json"
system="$(jq -r ".\"$(hostname)\"" "$tmpdir/systems.json")"
echo "Switching to system ''$system"
fi
@ -35,4 +56,5 @@ pkgs.writeShellScriptBin "switch-prebuilt" ''
nix-env -p /nix/var/nix/profiles/system --set "$system"
"$system/bin/switch-to-configuration" switch
''
'';
}