2023-11-14 22:53:04 +00:00
|
|
|
#!/usr/bin/env bash
|
2024-02-29 22:07:51 +00:00
|
|
|
|
2023-11-15 10:41:45 +00:00
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
|
|
|
servername="${1-}"
|
|
|
|
|
2023-11-25 20:00:21 +00:00
|
|
|
hostname="${2-}"
|
|
|
|
|
|
|
|
if [ -z "$servername" ] || [ -z "$hostname" ]
|
2023-11-15 10:41:45 +00:00
|
|
|
then
|
2023-11-25 20:00:21 +00:00
|
|
|
echo "Usage: $0 SERVERNAME HOSTNAME"
|
2023-11-15 10:41:45 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
confirmation="Yes, wipe ${servername}."
|
|
|
|
|
|
|
|
echo "⚠️ This will wipe ${servername} completely! ⚠️"
|
|
|
|
echo "Confirm by typing: \"${confirmation}\""
|
|
|
|
read response
|
|
|
|
|
|
|
|
if [ "$response" != "$confirmation" ]; then
|
|
|
|
echo "Aborting."
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-11-14 22:53:04 +00:00
|
|
|
|
|
|
|
# Create a temporary directory
|
|
|
|
temp=$(mktemp -d)
|
|
|
|
|
|
|
|
# Function to cleanup temporary directory on exit
|
|
|
|
cleanup() {
|
|
|
|
rm -rf "$temp"
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2023-11-15 10:41:45 +00:00
|
|
|
# Create directory where age key will go.
|
|
|
|
# Nixos-anwhere creates a kind of overlay and retains this structure on the final file system.
|
2024-01-08 19:47:12 +00:00
|
|
|
mkdir "$temp/etc"
|
2023-11-14 22:53:04 +00:00
|
|
|
|
2024-01-08 19:47:12 +00:00
|
|
|
secret-tool lookup age-identity "$servername" > "$temp/etc/age_ed25519"
|
2023-11-14 22:53:04 +00:00
|
|
|
|
2023-11-15 10:41:45 +00:00
|
|
|
# Set the correct permissions
|
2024-01-08 19:47:12 +00:00
|
|
|
chmod 600 "$temp/etc/age_ed25519"
|
2023-11-14 22:53:04 +00:00
|
|
|
|
2023-11-15 10:41:45 +00:00
|
|
|
# Install NixOS to the host system with our age identity
|
2024-02-29 22:07:51 +00:00
|
|
|
nixos-anywhere --extra-files "$temp" --flake ".#${servername}" "root@${hostname}"
|