From 7e45f16948a802a3810d915b315f810f83fe8900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Tue, 11 Oct 2022 13:15:50 +0200 Subject: hosts.sh: support not only devices and use screen to detach switch --- common.sh | 3 +- devices.sh | 107 ------------------------------------------ hosts.sh | 92 ++++++++++++++++++++++++++++++++++++ nixos/modules/generic.nix | 5 ++ nixos/modules/nixos-system.sh | 27 +++++++++++ 5 files changed, 125 insertions(+), 109 deletions(-) delete mode 100755 devices.sh create mode 100755 hosts.sh create mode 100644 nixos/modules/nixos-system.sh diff --git a/common.sh b/common.sh index 1af840d..7cc262a 100644 --- a/common.sh +++ b/common.sh @@ -161,8 +161,7 @@ setenv() { local _store _switchop printf -v _store '%q' "$store" printf -v _switchop '%q' "$switchop" - _tssh "$device" \ - "sudo nix-env --profile /nix/var/nix/profiles/system --set '$_store' && sudo /nix/var/nix/profiles/system/bin/switch-to-configuration '$_switchop'" + _tssh "$device" "sudo '$_store/bin/nixos-system' -s '$_switchop'" else warning "The latest system might have been already set." fi diff --git a/devices.sh b/devices.sh deleted file mode 100755 index 6c1f30b..0000000 --- a/devices.sh +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/env bash -source "${0%/*}/common.sh" -declare -a devices -################################################################################ -## aarch64 -# Mox -devices+=( "dean" "spt-mox" "spt-mox2" ) -# Raspberry Pi -devices+=( "adm-mpd" ) - -## armv7 -# Omnia -devices+=( "spt-omnia" "adm-omnia" "adm-omnia2" ) -# Raspberry Pi -devices+=( "spt-mpd" ) -################################################################################ - -valid_device() { - local check="$1" - for dev in "${devices[@]}"; do - [ "$dev" != "$check" ] \ - || return 0 - done - return 1 -} - -for_devices() { - for device in "${selected_devices[@]}"; do - for op in "$@"; do - if ! "$op" "$device"; then - error "Operation '$op' failed for: $device" >&2 - break - fi - done - done -} - -################################################################################ -operation="${1:-}" -[ $# -gt 0 ] && shift - -declare -a selected_devices -if [ $# -gt 0 ]; then - for device in "$@"; do - if valid_device "$device"; then - selected_devices+=("$device") - else - asdev="$(sshhost "$device")" - if valid_device "$asdev"; then - selected_devices+=("$asdev") - else - error "No such device: $device" >&2 - exit 2 - fi - fi - done -else - selected_devices=("${devices[@]}") -fi - - -case "$operation" in - help|h) - cat <<-EOF - Usage $0 operation [device]... - Local system builder and updater for remote devices. - - Operations: - build: build device system - copy: copy built system to the device - boot: set built system to be boot default on the device - switch: switch to the built system on the target device - test: test the built system on the target device - EOF - ;; - build|b|"") - for_devices build - ;; - copy|c) - for_devices copy - ;; - boot) - for_devices boot - ;; - switch|s) - for_devices switch - ;; - test|t) - for_devices switch_test - ;; - build-copy|bc) - for_devices build copy - ;; - build-switch|bs) - for_devices build copy switch - ;; - build-test|bt) - for_devices build copy switch_test - ;; - build-boot|bb) - for_devices build copy boot - ;; - default) - echo "Unknown operation: $operation" >&2 - exit 2 - ;; -esac diff --git a/hosts.sh b/hosts.sh new file mode 100755 index 0000000..dfd54da --- /dev/null +++ b/hosts.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +source "${0%/*}/common.sh" +declare -a default_hosts +################################################################################ +## x86_64 +# VPSFree +default_hosts+=( "lipwig" "mrpump" ) + +## aarch64 +# Mox +default_hosts+=( "dean" "spt-mox" "spt-mox2" ) +# Raspberry Pi +default_hosts+=( "adm-mpd" ) + +## armv7 +# Omnia +default_hosts+=( "spt-omnia" "adm-omnia" "adm-omnia2" ) +# Raspberry Pi +default_hosts+=( "spt-mpd" ) + +################################################################################ +operation="${1:-}" +[ $# -gt 0 ] && shift + +declare -a selected_hosts +if [ $# -gt 0 ]; then + for host in "$@"; do + selected_hosts+=("$(sshhost "$host")") + done +else + selected_hosts=("${default_hosts[@]}") +fi + + +for_hosts() { + for host in "${selected_hosts[@]}"; do + for op in "$@"; do + if ! "$op" "$host"; then + error "Operation '$op' failed for: $host" >&2 + break + fi + done + done +} + + +case "$operation" in + help|h) + cat <<-EOF + Usage $0 operation [host]... + Local system builder and updater for remote hosts. + + Operations: + build: build host system + copy: copy built system to the host + boot: set built system to be boot default on the host + switch: switch to the built system on the target host + test: test the built system on the target host + EOF + ;; + build|b|"") + for_hosts build + ;; + copy|c) + for_hosts copy + ;; + boot) + for_hosts boot + ;; + switch|s) + for_hosts switch + ;; + test|t) + for_hosts switch_test + ;; + build-copy|bc) + for_hosts build copy + ;; + build-switch|bs) + for_hosts build copy switch + ;; + build-test|bt) + for_hosts build copy switch_test + ;; + build-boot|bb) + for_hosts build copy boot + ;; + default) + echo "Unknown operation: $operation" >&2 + exit 2 + ;; +esac diff --git a/nixos/modules/generic.nix b/nixos/modules/generic.nix index ff635cf..fb2879c 100644 --- a/nixos/modules/generic.nix +++ b/nixos/modules/generic.nix @@ -131,6 +131,11 @@ in { ''; }) ]; + + system.extraSystemBuilderCmds = '' + substituteAll ${./nixos-system.sh} $out/bin/nixos-system + chmod +x $out/bin/nixos-system + ''; }; } diff --git a/nixos/modules/nixos-system.sh b/nixos/modules/nixos-system.sh new file mode 100644 index 0000000..7a220bb --- /dev/null +++ b/nixos/modules/nixos-system.sh @@ -0,0 +1,27 @@ +#!@shell@ +# Simple script handy to be used for activation + +while getopts "s" opt; do + case "$opt" in + s) + if [ ! -v NIXOS_SYSTEM_GNU_SCREEN ]; then + export NIXOS_SYSTEM_GNU_SCREEN=1 + exec @out@/sw/bin/screen "$0" "$@" + fi + ;; + *) + echo "Invalid argument: $1" >&2 + exit 1 + ;; + esac +done +shift $((OPTIND - 1)) + + +@out@/sw/bin/nix-env --profile /nix/var/nix/profiles/system --set '@out@' + +@out@/bin/switch-to-configuration "$@" || { + echo "Switch failed!" >&2 + read -r _ + exit 1 +} -- cgit v1.2.3