aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xscripts/build.sh43
-rwxr-xr-xscripts/check.sh38
-rw-r--r--scripts/check_get_tests.nix16
-rwxr-xr-xscripts/ping_hosts.sh12
-rwxr-xr-xscripts/system_info.sh25
-rwxr-xr-xscripts/test.sh12
-rw-r--r--scripts/update_hosts.remote48
-rwxr-xr-xscripts/update_hosts.sh86
8 files changed, 280 insertions, 0 deletions
diff --git a/scripts/build.sh b/scripts/build.sh
new file mode 100755
index 0000000..f1ff620
--- /dev/null
+++ b/scripts/build.sh
@@ -0,0 +1,43 @@
+#! /usr/bin/env sh
+
+set -eu
+
+wrong_arg=false
+use_dirty=true
+
+for arg in "$@"; do
+ case "$arg" in
+ --dirty)
+ use_dirty=true
+ ;;
+ --clean)
+ use_dirty=false
+ ;;
+ *)
+ echo "arg '$arg' is not known."
+ wrong_arg=true
+ ;;
+ esac
+done
+
+if [ "$wrong_arg" = true ]; then
+ exit 2
+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 "server2" "server3"; do
+ nix build ".#nixosConfigurations.$host.config.system.build.toplevel" --print-out-paths --no-link --option max-jobs 1
+done
+
+# vim: ft=sh
diff --git a/scripts/check.sh b/scripts/check.sh
new file mode 100755
index 0000000..dbfa67d
--- /dev/null
+++ b/scripts/check.sh
@@ -0,0 +1,38 @@
+#! /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"
+fi
diff --git a/scripts/check_get_tests.nix b/scripts/check_get_tests.nix
new file mode 100644
index 0000000..a39d300
--- /dev/null
+++ b/scripts/check_get_tests.nix
@@ -0,0 +1,16 @@
+let
+ get = input: (builtins.fetchTree input.locked).outPath;
+
+ lock = (builtins.fromJSON (builtins.readFile ../flake.lock)).nodes;
+
+ flake-compat = import (get lock.flake-compat) {src = ../.;};
+ pkgs = import (get lock.nixpkgs) {};
+ inherit (pkgs) lib;
+
+ inherit (flake-compat) outputs;
+in
+ lib.strings.concatStringsSep "\n"
+ (builtins.attrNames
+ (lib.filterAttrs
+ (name: test: test.meta.broken == false)
+ outputs.checks.x86_64-linux))
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..940406a
--- /dev/null
+++ b/scripts/system_info.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env sh
+
+# 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
+nix eval --expr '"${builtins.concatStringsSep "\n" (builtins.attrNames (builtins.fromTOML (builtins.readFile ./hosts/host-names.toml)))}\n"' --impure --raw | while read -r host; do
+ 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..58c3343
--- /dev/null
+++ b/scripts/test.sh
@@ -0,0 +1,12 @@
+#!/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
+}
+
+nix build --log-format multiline-with-logs .#checks.x86_64-linux."$test_target"
+# vim: ft=sh
diff --git a/scripts/update_hosts.remote b/scripts/update_hosts.remote
new file mode 100644
index 0000000..7385129
--- /dev/null
+++ b/scripts/update_hosts.remote
@@ -0,0 +1,48 @@
+#! /usr/bin/env sh
+
+# This is the remote side of `update_hosts.sh`, it will be copied to the remote host
+# and is responsible for performing the update.
+
+set -e
+
+PATH_add() {
+ nix_expr="$1"
+ what="$(nix build "nixpkgs#$nix_expr.out" --print-out-paths --no-link)"
+
+ printf "Adding '%s' (%s/bin) to PATH..\n" "$nix_expr" "$what"
+
+ PATH="$what/bin:$PATH"
+ export PATH
+}
+
+branch="$1"
+flake_path="${2-""}"
+
+# We don't have access to git by default, so evaluate it here
+PATH_add git
+
+# By-default these systems use cppnix, which can't build our config. So let's switch to
+# lix.
+PATH_add lixPackageSets.latest.lix
+
+# We might or might not have python, and we need it, because we use the unwrapped
+# `nixos-update`.
+PATH_add python3
+PATH_add nixos-rebuild
+
+if [ -z "$flake_path" ]; then
+ set -x
+ cd /etc/nixos
+
+ sudo git fetch --all --prune
+ sudo git switch "$branch"
+ sudo git pull --rebase
+
+ flake_path="git+file:/etc/nixos/"
+fi
+
+# we use the unwrapped one, to make sure that we are really using lix (otherwise it might
+# be wrapped with cppnix).
+PYTHONNOUSERSITE='true' sudo --preserve-env=PATH --preserve-env=PYTHONNOUSERSITE ".nixos-rebuild-wrapped" --flake "$flake_path" --no-reexec boot
+
+sudo reboot
diff --git a/scripts/update_hosts.sh b/scripts/update_hosts.sh
new file mode 100755
index 0000000..4f32722
--- /dev/null
+++ b/scripts/update_hosts.sh
@@ -0,0 +1,86 @@
+#! /usr/bin/env sh
+set -e
+
+base_dir="$(git rev-parse --show-toplevel)"
+
+user="$USER"
+set_hosts=""
+branch="main"
+direct=false
+
+while [ "$#" -ne 0 ]; do
+ case "$1" in
+ --user)
+ shift 1
+ user="$1"
+ ;;
+ --host)
+ shift 1
+ set_hosts="$set_hosts $1"
+ ;;
+ --hosts)
+ shift 1
+ if [ -n "$set_hosts" ]; then
+ echo "Warning: --hosts overrides --host"
+ fi
+ set_hosts="$1"
+ ;;
+ --branch)
+ shift 1
+ branch="$1"
+ ;;
+ --direct)
+ direct=true
+ ;;
+ *)
+ echo "Arg '$1' not known."
+ exit 2
+ ;;
+ esac
+ shift 1
+done
+
+hosts="${set_hosts-"server2 server3"}"
+
+echo "update_hosts.sh --user '$user' --hosts '$hosts' --branch '$branch' --direct '$direct'"
+
+for host in $hosts; do
+ echo "Updating '$user@$host.vhack.eu' ..."
+
+ printf "Deploying remote side script ..\n"
+ scp "$base_dir/scripts/update_hosts.remote" "$user@$host.vhack.eu:update_host.remote"
+
+ flake_path=""
+ if [ "$direct" = true ]; then
+ printf "Deploying git config ..\n"
+
+ flake_path="path:$(git archive --format=tar.gz HEAD |
+ ssh "$user@$host.vhack.eu" \
+ 'flake_path=$(mktemp -d -t update_hosts_XXXXX); cd $flake_path && tar -xz; echo $flake_path')"
+
+ fi
+
+ tmp="$(mktemp -d -t "server_build_script_XXXXX")"
+ trap 'rm -r "$tmp"' EXIT
+ if [ "$direct" = true ]; then
+ git archive --format=tar.gz HEAD | {
+ cd "$tmp"
+ tar -xz
+ }
+ else
+ git clone "git.foss-syndicate.org" --branch="$branch" "$tmp"
+ fi
+ cd "$tmp"
+
+ printf "Copying closure ..\n"
+ nix copy --substitute-on-destination --to "ssh://$user@$host.vhack.eu" ".#nixosConfigurations.$host.config.system.build.toplevel"
+
+ printf "Executing remote side script ..\n"
+ ssh -t "$user@$host.vhack.eu" "chmod +x update_host.remote; ./update_host.remote '$branch' '$flake_path'"
+done
+
+# Give the servers some time to reboot
+sleep 5
+ping_hosts.sh "$user" "$hosts"
+
+# vim: ft=sh