From 5cfb4ee1841ae0259ca7a39a6af75ab284394d73 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 12 Jul 2024 00:39:14 +0200 Subject: [PATCH] Wait for internet connection before starting the setup --- scripts/Arch/OS/setup.fish | 5 ++- scripts/Common/Scripts/wait-network.fish | 50 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 scripts/Common/Scripts/wait-network.fish diff --git a/scripts/Arch/OS/setup.fish b/scripts/Arch/OS/setup.fish index 75535736..2939c7f2 100644 --- a/scripts/Arch/OS/setup.fish +++ b/scripts/Arch/OS/setup.fish @@ -3,6 +3,7 @@ begin set -l dir (status dirname) source "$dir/../../Common/Scripts/config.fish" source "$dir/../../Common/Scripts/hooks.fish" + source "$dir/../../Common/Scripts/wait-network.fish" set -q CONFIG_MODULE || set -l CONFIG_MODULE "$dir/config.nix" @@ -11,7 +12,9 @@ begin set -q USER_NAME || set -l USER_NAME manuel set -q USER_DISPLAYNAME set -q USER_GROUPS || set -l USER_GROUPS "" - begin + + waitNetwork + and begin set -l projectRoot (realpath "$dir/../../..") set -l projectName (basename "$projectRoot") set -l relativeDir (realpath --relative-to "$projectRoot" "$dir") diff --git a/scripts/Common/Scripts/wait-network.fish b/scripts/Common/Scripts/wait-network.fish new file mode 100644 index 00000000..8b2b1e9a --- /dev/null +++ b/scripts/Common/Scripts/wait-network.fish @@ -0,0 +1,50 @@ +#!/bin/env fish +function waitNetwork -a witness + set -l timeout (math 10 \* 60) + set -l codeFile (mktemp) + + if [ -z "$witness" ] + tmux new-session -d 'cat /etc/motd; echo "$(tput bold)Please establish an internet connection…$(tput sgr0)"; $SHELL' + + if not systemctl --quiet is-active network-online.target + fish (status filename) true & + set -l pid "$last_pid" + + function witnessHandler -V codeFile --on-process-exit "$pid" -a event pid code + echo "$code" > "$codeFile" + + if [ "$code" -gt 0 ] + echo "Unable to connect to the internet!" + end + end + + tmux attach + wait "$pid" + return "$(cat "$codeFile")" + end + else + set -l x 0 + + while true + set x (math $x + 1) + + if systemctl --quiet is-active network-online.target + tmux kill-session &> /dev/null + break + else + not tmux list-sessions &> /dev/null + or test "$x" -gt 10 + and begin + tmux kill-session &> /dev/null + exit 1 + end + end + + sleep 1 + end + end +end + +if [ -n "$argv" ] + waitNetwork $argv +end