116 lines
4.2 KiB
Bash
116 lines
4.2 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
REAL_BASE_DIR=$( dirname $( realpath "$0" ) )
|
||
|
|
||
|
# Wrapper for nixos-rebuild operations, which act remotely.
|
||
|
# Gets the necessary configuration to do so.
|
||
|
|
||
|
# Takes the following arguments supplied as environment variables.
|
||
|
# HOST: The host as defined in nixosConfigurations and the
|
||
|
# deployment_configuration.json.
|
||
|
# OPERATION: The nixos-rebuild operation to execute. Can be one of:
|
||
|
# switch, boot, test, reboot
|
||
|
# All operations are as defined in the nixos-rebuild man page except
|
||
|
# for reboot, which runs boot, but then also reboots the host.
|
||
|
|
||
|
if [ -z $HOST ]; then
|
||
|
$REAL_BASE_DIR/../helper/msg_error.sh "\
|
||
|
Internal Error: No host given.
|
||
|
A host needs to be provided via the HOST environment variable."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if [ -z $OPERATION ]; then
|
||
|
$REAL_BASE_DIR/../helper/msg_error.sh "\
|
||
|
Internal Error: No operation given.
|
||
|
An operation needs to be provided via the OPERATION environment variable."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
ACTUAL_OPERATION=""
|
||
|
case $OPERATION in
|
||
|
switch|boot|test)
|
||
|
ACTUAL_OPERATION="$OPERATION"
|
||
|
;;
|
||
|
reboot)
|
||
|
ACTUAL_OPERATION="boot"
|
||
|
;;
|
||
|
*)
|
||
|
$REAL_BASE_DIR/../helper/msg_error.sh "\
|
||
|
Internal Error: No valid operation given.
|
||
|
The operation provided via the OPERTION environment variable needs to be one of:
|
||
|
switch, boot, test, reboot."
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
|
||
|
TARGET_HOSTNAME=""
|
||
|
TARGET_USER=""
|
||
|
TARGET_PORT=""
|
||
|
|
||
|
DEPLOYMENT_CONFIGURATION_EXISTS=true
|
||
|
if ! [ -f deployment_configuration.json ]; then
|
||
|
$REAL_BASE_DIR/../helper/msg_warning.sh "\
|
||
|
Warning: No deployment_configuration.json exists and therefore it can't be used to retrieve configuration values."
|
||
|
DEPLOYMENT_CONFIGURATION_EXISTS=false
|
||
|
fi
|
||
|
|
||
|
if $DEPLOYMENT_CONFIGURATION_EXISTS && ! cat deployment_configuration.json | jq . >/dev/null 2>&1; then
|
||
|
$REAL_BASE_DIR/../helper/msg_warning.sh "\
|
||
|
Warning: jq can't parse the deployment_configuration.json and therefore it can't be used to retrieve configuration values."
|
||
|
fi
|
||
|
|
||
|
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_HOSTNAME="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetHostname 2>/dev/null)"; then
|
||
|
TARGET_HOSTNAME=$CONFIG_TARGET_HOSTNAME
|
||
|
elif NIX_CONFIG_FQDN="$(nix eval --raw .\#nixosConfigurations.$HOST.config.networking.fqdn 2>/dev/null)"; then
|
||
|
TARGET_HOSTNAME=$NIX_CONFIG_FQDN
|
||
|
else
|
||
|
$REAL_BASE_DIR/../helper/msg_error.sh "\
|
||
|
Error: Couldn't determine target hostname for $HOST.
|
||
|
You either need to set targetHostname for this host in the deployment_configuration.json or have an FQDN available in the NixOS configuration of this host, which then gets used for the target hostname."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_USER="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetUser 2>/dev/null)"; then
|
||
|
TARGET_USER=$CONFIG_TARGET_USER
|
||
|
elif $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_DEFAULT_TARGET_USER="$(cat deployment_configuration.json | jq -re '.default.targetUser' 2>/dev/null)"; then
|
||
|
TARGET_USER=$CONFIG_DEFAULT_TARGET_USER
|
||
|
fi
|
||
|
|
||
|
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_PORT="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetPort 2>/dev/null)"; then
|
||
|
TARGET_PORT=$CONFIG_TARGET_PORT
|
||
|
elif $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_DEFAULT_TARGET_PORT="$(cat deployment_configuration.json | jq -re '.default.targetPort' 2>/dev/null)"; then
|
||
|
TARGET_PORT=$CONFIG_DEFAULT_TARGET_PORT
|
||
|
fi
|
||
|
|
||
|
|
||
|
TARGET_HOST="$TARGET_HOSTNAME"
|
||
|
if [ -n "$TARGET_USER" ]; then
|
||
|
TARGET_HOST="$TARGET_USER@$TARGET_HOST"
|
||
|
fi
|
||
|
|
||
|
SSHOPTS=""
|
||
|
if [ -n "$TARGET_PORT" ]; then
|
||
|
SSHOPTS="-o Port=$TARGET_PORT"
|
||
|
fi
|
||
|
|
||
|
|
||
|
set -e
|
||
|
|
||
|
if [ -n "$TARGET_PORT" ]; then
|
||
|
$REAL_BASE_DIR/../helper/msg_info.sh "\
|
||
|
Running nixos-rebuild $ACTUAL_OPERATION for $HOST on $TARGET_HOST:$TARGET_PORT..."
|
||
|
else
|
||
|
$REAL_BASE_DIR/../helper/msg_info.sh "\
|
||
|
Running nixos-rebuild $ACTUAL_OPERATION for $HOST on $TARGET_HOST..."
|
||
|
fi
|
||
|
|
||
|
env NIX_SSHOPTS="$SSHOPTS" nixos-rebuild "$ACTUAL_OPERATION" --flake ".#$HOST" --target-host "$TARGET_HOST" --use-substitutes --use-remote-sudo
|
||
|
|
||
|
if [ "$OPERATION" = "reboot" ]; then
|
||
|
$REAL_BASE_DIR/../helper/msg_info.sh "\
|
||
|
Rebooting $TARGET_HOSTNAME..."
|
||
|
ssh $SSH_OPTS "$TARGET_HOST" sudo systemctl reboot
|
||
|
fi
|