nixos/scripts/infra-status.nix

85 lines
2.4 KiB
Nix
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{ 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)"
'';
}