From b9170040fef72ab359c9c7c459a2cbdbbe5a068c Mon Sep 17 00:00:00 2001 From: Pyjacpp Date: Thu, 22 Oct 2026 21:39:14 +0200 Subject: [PATCH] Ajout script infra-status --- flake.nix | 13 +++++++ scripts/infra-status.nix | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 scripts/infra-status.nix diff --git a/flake.nix b/flake.nix index 46ee8d6..f00085f 100644 --- a/flake.nix +++ b/flake.nix @@ -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)) + ); }; }; } diff --git a/scripts/infra-status.nix b/scripts/infra-status.nix new file mode 100644 index 0000000..08bdcf7 --- /dev/null +++ b/scripts/infra-status.nix @@ -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)" + ''; +}