Ajout script infra-status

iso
Pyjacpp 2026-10-22 21:39:14 +02:00
parent b4e5078180
commit b9170040fe
2 changed files with 97 additions and 0 deletions

View File

@ -68,6 +68,19 @@
devShells = {
default = pkgs.callPackage ./devshells/default.nix { inherit (inputs) agenix; };
};
apps =
with builtins;
listToAttrs (
map (
name:
lib.nameValuePair (lib.strings.removeSuffix ".nix" name) (
{
type = "app";
}
// import (./scripts + "/${name}") { inherit pkgs lib; }
)
) (attrNames (readDir ./scripts))
);
};
};
}

View File

@ -0,0 +1,84 @@
{ pkgs, lib, ... }:
let
hosts = builtins.attrNames (
lib.filterAttrs (_: info: !(info.isDebian or false)) lib.crans.info.attrs_all_info
);
in
{
meta.description = ''
Ce script donne un aperçu de l’infrastructure NixOs. Il affiche les commits
déployés et leur propreté. Il compare automatiquement ces versions par
rapport à la branche main et la tête du git.
Utilisation :
nix run .#infra-status [host1] [host2] …
'';
program = pkgs.writeShellScriptBin "infra-status" ''
shopt -s extglob;
# Extend patterns
COLOR_RESET="\e[0m"
COLOR_RED="\e[31m"
COLOR_GREEN="\e[32m"
COLOR_YELLOW="\e[33m"
COLOR_BLUE="\e[34m"
COLOR_CYAN="\e[36m"
function query_host() {
host=$1
raw_rev=$(ssh $host.adm.crans.org nixos-version --configuration-revision 2> /dev/null)
ssh_status=$?
full_rev=''\${raw_rev%-dirty}
printf "$COLOR_BLUE%-18s$COLOR_RESET " "$host"
if [[ $tssh_status -ne 0 ]] then
printf "''\${COLOR_RED}DOWN$COLOR_RESET\n"
return
fi
case "$raw_rev" in
*-dirty) type=dirty;;
+([0-9a-f])) type=ok;;
*) printf "''\${COLOR_RED}INVALID$COLOR_RESET\n"
return ;;
esac
pretty_rev=$(git log -1 --pretty='%h %cs' "$full_rev" 2> /dev/null)
if [[ $? -ne 0 ]] then
printf "%-18s ''\${COLOR_YELLOW}NOT FOUND$COLOR_RESET\n" $(echo "$full_rev" | cut -c -7)
return
fi
printf "%-18s " "$pretty_rev"
if [[ "$type" == dirty ]] then
printf "''\${COLOR_RED}DIRTY!$COLOR_RESET "
fi
if [[ "$git_head" == "$full_rev" ]] then
printf "''\${COLOR_CYAN}UP TO DATE$COLOR_RESET\n"
elif git merge-base --is-ancestor "$full_rev" HEAD 2> /dev/null; then
printf "''\${COLOR_GREEN}UPDATABLE$COLOR_RESET\n"
elif git merge-base --is-ancestor "$full_rev" main 2> /dev/null; then
printf "''\${COLOR_YELLOW}LATER$COLOR_RESET on main\n"
else
printf "''\${COLOR_YELLOW}NOT FOLLOWING main$COLOR_RESET\n"
fi
}
git_head=$(git rev-parse HEAD)
if [[ $# -gt 0 ]] then
while [[ $# -gt 0 ]] do
query_host $1
shift
done
else
for host in ${lib.join " " hosts}; do
query_host $host
done
fi
printf "\nHEAD : %s %s\n" "$(git rev-parse --abbrev-ref HEAD)" "$(git rev-parse --short HEAD)"
'';
}