diff options
Diffstat (limited to '')
| -rwxr-xr-x | scripts/build.sh | 100 | ||||
| -rwxr-xr-x | scripts/check.sh | 41 | ||||
| -rw-r--r-- | scripts/check_get_tests.nix | 11 | ||||
| -rwxr-xr-x | scripts/get_dns.sh | 55 | ||||
| -rwxr-xr-x | scripts/ping_hosts.sh | 12 | ||||
| -rwxr-xr-x | scripts/system_info.sh | 28 | ||||
| -rwxr-xr-x | scripts/test.sh | 14 | ||||
| -rwxr-xr-x | scripts/test_build.sh | 13 | ||||
| -rwxr-xr-x | scripts/test_interactive.sh | 4 | ||||
| -rwxr-xr-x | scripts/unflake.sh | 9 | ||||
| -rwxr-xr-x | scripts/update_hosts.sh | 29 |
11 files changed, 315 insertions, 1 deletions
diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..6c3d246 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,100 @@ +#! /usr/bin/env sh +set -eu + +build_system() { + host="$1" + + show_trace_arg="" + if [ "$show_trace" = true ]; then + show_trace_arg="--show-trace" + fi + + _val="$(nix build ".#nixosConfigurations.$host.config.system.build.toplevel" --print-out-paths --no-link --option max-jobs 1 $show_trace_arg)" + exit_val="$?" + + if [ "$exit_val" -ne 0 ]; then + echo "ERROR: Failed to build you system config for host: '$host'" 1>&2 + printf 1 + else + printf "%s" "$_val" + fi +} +remove_symlink() { + link="$1" + if [ -L "$link" ]; then + rm "$link" + elif ! [ -e "$link" ]; then + : "Not existing file is also okay" + else + echo "ERROR: '$link' is not a symlink. Not removing it." 1>&2 + exit 1 + fi +} + +root="$(git rev-parse --show-toplevel)" +use_dirty=true +show_trace=false + +while [ "$#" -ne 0 ]; do + case "$1" in + --dirty) + use_dirty=true + ;; + --clean) + use_dirty=false + ;; + --show-trace) + show_trace=true + ;; + --* | -*) + echo "arg '$1' is not known." + exit 2 + ;; + *) + # We reached the hosts + break + ;; + esac + shift 1 +done + +if [ "$#" -eq 0 ]; then + # shellcheck disable=SC2046 + set -- $(ls "$root/hosts/by-name") +fi + +if [ "$use_dirty" = "false" ]; then + tmp="$(mktemp -d -t "server_build_script_XXXXX")" + trap 'rm -r "$tmp"' EXIT + + git archive --format=tar.gz HEAD | { + cd "$tmp" + tar -xz + } + + cd "$tmp" +fi + +for host in "$@"; do + system="$(build_system "$host")" + [ "$system" = "1" ] && exit 1 + + result_link="$root/result-$host" + remove_symlink "$result_link" + nix-store --add-root "$result_link" --realise "$(readlink -f "$system")" + + # Try to also provide an easy link for potential home manager outputs. + fd "home-manager-\w*.service" "$result_link/etc/systemd/system" | while read -r hm; do + home_generation_link="$(grep ExecStart= "$hm" | awk '{print $2}')" + user_name="$(basename "$hm" | sed 's/home-manager-\(.*\)\.service/\1/')" + home_link="$root/result-$host-hm-$user_name" + + remove_symlink "$home_link" + ln --symbolic "$home_generation_link" "$home_link" + done + + if [ "$host" = "$(hostname)" ]; then + nvd diff /run/current-system "$result_link" + fi +done +# vim: ft=sh diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..6732dc1 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,41 @@ +#! /usr/bin/env sh + +git_root="$(git rev-parse --show-toplevel)" + +fmt_check() { + echo ".#checks.x86_64-linux.$1" +} + +failed="$(mktemp -t server_check_XXXXX)" +trap 'rm -f "$failed"' EXIT +trap 'exit 4' HUP INT QUIT TERM + +# We want word-splitting for the arg +# shellcheck disable=SC2046 +set -- $(nix eval --file "$git_root/scripts/check_get_tests.nix" --raw) + +while [ "$#" -ne 0 ]; do + test="$1" + shift 1 + + echo "Building test '$test'..." + fmt_test="$(fmt_check "$test")" + + if ! nix build \ + --option max-jobs 1 \ + --print-out-paths \ + --no-link \ + "$fmt_test"; then + echo "$test" >>"$failed" + fi +done + +if [ "$(cat "$failed" | wc -l)" -ne 0 ]; then + echo "Failed tests:" + while read -r test; do + echo " - $test" + done <"$failed" + exit 1 +else + exit 0 +fi diff --git a/scripts/check_get_tests.nix b/scripts/check_get_tests.nix new file mode 100644 index 0000000..545c000 --- /dev/null +++ b/scripts/check_get_tests.nix @@ -0,0 +1,11 @@ +let + sources = import ../unflake.nix; + flake-compat = import sources.flake-compat {src = ../.;}; + pkgs = import sources.nixpkgs-unstable {}; + inherit (pkgs) lib; + + inherit (flake-compat) outputs; +in + lib.strings.concatStringsSep "\n" + (builtins.attrNames + outputs.checks.x86_64-linux) diff --git a/scripts/get_dns.sh b/scripts/get_dns.sh new file mode 100755 index 0000000..2d82925 --- /dev/null +++ b/scripts/get_dns.sh @@ -0,0 +1,55 @@ +#! /usr/bin/env nix-shell +#! nix-shell -p dig -p dash -i dash --impure +# shellcheck shell=dash + +get_dns_types() { + cat <<EOF + A + AAAA + CAA + CNAME + DNAME + MX + NS + SOA + SRV + TXT + PTR + DNSKEY + DS + SSHFP + TLSA + OPENPGPKEY + SVCB + HTTPS +EOF +} + +check_type() { + domain="$1" + type="$2" + + if [ "$(dig +short -t "$type" "$domain" | wc -c)" -ne 0 ]; then + dig +short -t "$type" "$domain" | while IFS="$(printf "\n")" read -r output; do + printf "(%s) %s [%s]\n" "$type" "$output" "$domain" + done + else + printf "(%s) <Not set> [%s]\n" "$type" "$domain" + fi +} + +get_dns() { + original_domain="$1" + + get_dns_types | while read -r type; do + check_type "$original_domain" "$type" + done + + # DKIM + check_type "mail._domainkey.$original_domain" "TXT" + + # DMARC + check_type "_dmarc.$original_domain" "TXT" +} + +get_dns "$1" diff --git a/scripts/ping_hosts.sh b/scripts/ping_hosts.sh new file mode 100755 index 0000000..fba2490 --- /dev/null +++ b/scripts/ping_hosts.sh @@ -0,0 +1,12 @@ +#! /usr/bin/env sh + +user="${1-$USER}" +hosts="${2-server2 server3}" + +for host in $hosts; do + echo "Checking status of '$user@$host.vhack.eu' ..." + + ssh "$user@$host.vhack.eu" "set -x; systemctl --failed" +done + +# vim: ft=sh diff --git a/scripts/system_info.sh b/scripts/system_info.sh new file mode 100755 index 0000000..f0de42b --- /dev/null +++ b/scripts/system_info.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env sh + +root="$(git rev-parse --show-toplevel)" + +# Take a host name and return the nix store path to the host's system info. +# Type +# _system_info :: String -> Path +_system_info() { + nix --option warn-dirty false build .#nixosConfigurations."$1".config.vhack.system-info.markdown --print-out-paths --no-link +} + +_glow() { + if command -v glow >/dev/null; then + glow --width 0 + else + cat + fi +} + +# The expression is not meant to be expanded by the shell +# shellcheck disable=SC2016 +fd . "$root/hosts/by-name" --type directory --max-depth 1 | while read -r host_path; do + host="$(basename "$host_path")" + echo "# $host" | _glow + _glow <"$(_system_info "$host")" +done + +# vim: ft=sh diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..5a75517 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +test_target="$1" + +[ -z "$test_target" ] && { + echo "You need to select a test target!" 1>&2 + echo "Usage: test_interactive TEST_TARGET" 1>&2 + exit 1 +} + +shift 1 + +nix build --log-format multiline-with-logs .#checks.x86_64-linux."$test_target" "$@" +# vim: ft=sh diff --git a/scripts/test_build.sh b/scripts/test_build.sh new file mode 100755 index 0000000..eeb8572 --- /dev/null +++ b/scripts/test_build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +[ "$#" -ne 2 ] && { + echo "Usage: test_build <TEST_TARGET> <BUILD_NODE>" 1>&2 + exit 2 +} + +test_target="$1" +build_node="$2" + +nix build .#checks.x86_64-linux."$test_target".nodes."$build_node".system.build.toplevel + +# vim: ft=sh diff --git a/scripts/test_interactive.sh b/scripts/test_interactive.sh index 3b3fe0d..230f5a0 100755 --- a/scripts/test_interactive.sh +++ b/scripts/test_interactive.sh @@ -8,7 +8,9 @@ test_target="$1" exit 1 } -nix build .#checks.x86_64-linux."$test_target".driver +nix build .#checks.x86_64-linux."$test_target".driverInteractive || { + exit 1 +} ./result/bin/nixos-test-driver --interactive diff --git a/scripts/unflake.sh b/scripts/unflake.sh new file mode 100755 index 0000000..faaa533 --- /dev/null +++ b/scripts/unflake.sh @@ -0,0 +1,9 @@ +#! /usr/bin/env sh + +set -e + +NIX_CONFIG="${NIX_CONFIG-pure-eval = false}" +export NIX_CONFIG +nix run -f https://codeberg.org/goldstein/unflake/archive/main.tar.gz unflake -- "$@" + +# vim: ft=sh diff --git a/scripts/update_hosts.sh b/scripts/update_hosts.sh new file mode 100755 index 0000000..73900cc --- /dev/null +++ b/scripts/update_hosts.sh @@ -0,0 +1,29 @@ +#! /usr/bin/env sh +set -e + +check=true +if [ "$1" = "--no-check" ]; then + check=false + shift 1 +fi + +if [ "$#" -eq 0 ]; then + set -- server2 server3 +fi + +set -x + +[ "$check" = true ] && check.sh + +deploy --skip-checks --boot --interactive-sudo true +for host in "$@"; do + ssh -t "$USER@$host.vhack.eu" 'set -x; sudo reboot' +done + +set +x + +# Give the servers some time to reboot +sleep 5 +ping_hosts.sh "$USER" "$@" + +# vim: ft=sh |
