116 lines
3.4 KiB
Bash
116 lines
3.4 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# --- Default values (can be overridden by environment variables) ---
|
|
REPO_URL="${REPO_URL:-}"
|
|
REPO_DIR="${REPO_DIR:-/etc/nixos-infra}"
|
|
BRANCH="${BRANCH:-main}"
|
|
DRY_RUN="${DRY_RUN:-false}"
|
|
|
|
# --- Usage ---
|
|
usage() {
|
|
cat <<EOF
|
|
Deploy NixOS configuration from the nixos-infra repository.
|
|
|
|
Usage:
|
|
$0 [options]
|
|
|
|
Options:
|
|
-u, --repo-url URL Git repository URL
|
|
[default: ${REPO_URL}]
|
|
-d, --repo-dir DIR Local directory for the repository
|
|
[default: ${REPO_DIR}]
|
|
-b, --branch BRANCH Git branch to deploy
|
|
[default: ${BRANCH}]
|
|
-n, --dry-run Simulate deployment without making changes.
|
|
-h, --help Show this help message.
|
|
|
|
Environment variables:
|
|
REPO_URL, REPO_DIR, BRANCH, DRY_RUN (same as options above).
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
# --- Parse arguments ---
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-u|--repo-url) REPO_URL="$2"; shift 2 ;;
|
|
-d|--repo-dir) REPO_DIR="$2"; shift 2 ;;
|
|
-b|--branch) BRANCH="$2"; shift 2 ;;
|
|
-n|--dry-run) DRY_RUN="true"; shift ;;
|
|
-h|--help) usage ;;
|
|
*) echo "❌ Unknown option: $1" >&2; usage ;;
|
|
esac
|
|
done
|
|
|
|
HOSTNAME=$(hostname)
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "❌ This script must be run as root." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# --- Dry run mode ---
|
|
if [ "$DRY_RUN" = "true" ]; then
|
|
echo "🧪 Dry run mode:"
|
|
echo " - Repository URL: $REPO_URL"
|
|
echo " - Repository dir: $REPO_DIR"
|
|
echo " - Branch: $BRANCH"
|
|
echo " - Hostname: $HOSTNAME"
|
|
echo " - Expected config: $REPO_DIR/environments/<env>/hosts/servers/$HOSTNAME/configuration.nix"
|
|
echo ""
|
|
echo " Would execute:"
|
|
echo " git clone --branch $BRANCH $REPO_URL $REPO_DIR"
|
|
echo " nixos-rebuild switch -I nixos-config=...$HOSTNAME/configuration.nix"
|
|
exit 0
|
|
fi
|
|
|
|
# --- Clone or update the repository ---
|
|
if [ -d "$REPO_DIR/.git" ]; then
|
|
echo "🔄 Mise à jour du dépôt dans $REPO_DIR..."
|
|
cd "$REPO_DIR"
|
|
git fetch origin
|
|
git checkout "$BRANCH"
|
|
git pull origin "$BRANCH"
|
|
else
|
|
echo "📥 Clonage du dépôt dans $REPO_DIR..."
|
|
mkdir -p "$REPO_DIR"
|
|
git clone --branch "$BRANCH" "$REPO_URL" "$REPO_DIR"
|
|
fi
|
|
|
|
# --- Find the configuration for this machine ---
|
|
ENVIRONMENT="${ENVIRONMENT:-}"
|
|
if [ -z "$ENVIRONMENT" ]; then
|
|
# Try common environment names
|
|
for env in production prod staging stage dev development; do
|
|
if [ -d "$REPO_DIR/environments/$env" ]; then
|
|
ENVIRONMENT="$env"
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
if [ -z "$ENVIRONMENT" ]; then
|
|
echo "❌ Error: No environment specified and none detected." >&2
|
|
echo " Set ENVIRONMENT environment variable or use --environment flag." >&2
|
|
exit 1
|
|
fi
|
|
|
|
CONFIG_PATH="$REPO_DIR/environments/$ENVIRONMENT/hosts/servers/$HOSTNAME/configuration.nix"
|
|
if [ ! -f "$CONFIG_PATH" ]; then
|
|
CONFIG_PATH="$REPO_DIR/environments/$ENVIRONMENT/hosts/workstations/$HOSTNAME/configuration.nix"
|
|
fi
|
|
|
|
if [ ! -f "$CONFIG_PATH" ]; then
|
|
echo "❌ Error : No configuration found for $HOSTNAME in environment '$ENVIRONMENT'" >&2
|
|
echo " Checked paths :" >&2
|
|
echo " - $REPO_DIR/environments/$ENVIRONMENT/hosts/servers/$HOSTNAME/configuration.nix" >&2
|
|
echo " - $REPO_DIR/environments/$ENVIRONMENT/hosts/workstations/$HOSTNAME/configuration.nix" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# --- Apply the configuration ---
|
|
echo "🚀 Deploying the configuration for $HOSTNAME..."
|
|
nixos-rebuild switch -I nixos-config="$CONFIG_PATH"
|
|
|
|
echo "✅ Deployment was successful !" |