Make packer build script interactive

Prompts for config values on first run, saves to server.pkrvars.hcl.
On subsequent runs, shows existing config and asks whether to reuse or
enter new values. Secret keys are hidden when displaying existing config.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Nelis Volschenk 2026-05-04 13:11:46 +00:00
parent 06580f9db6
commit 865b5e0ea0

View file

@ -4,12 +4,62 @@ set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
if [ ! -f server.pkrvars.hcl ]; then VARS_FILE="server.pkrvars.hcl"
echo "Error: server.pkrvars.hcl not found."
echo "Copy server.pkrvars.hcl.example to server.pkrvars.hcl and fill in your values." collect_values() {
exit 1 echo "=== Server Configuration ==="
echo ""
read -p "Base domain: " BASE_DOMAIN
read -p "SSH public key path [~/.ssh/id_ed25519.pub]: " PUBKEY_PATH
PUBKEY_PATH="${PUBKEY_PATH:-$HOME/.ssh/id_ed25519.pub}"
if [ ! -f "$PUBKEY_PATH" ]; then
echo "Warning: $PUBKEY_PATH not found, leaving SSH key empty"
SSH_PUBKEY=""
else
SSH_PUBKEY=$(cat "$PUBKEY_PATH")
fi fi
echo ""
echo "--- JuiceFS / Nextcloud Storage ---"
read -p "S3 endpoint (e.g. https://s3.amazonaws.com): " JUICEFS_S3_ENDPOINT
read -p "S3 bucket name: " JUICEFS_S3_BUCKET
read -p "S3 access key: " JUICEFS_S3_ACCESS_KEY
read -s -p "S3 secret key: " JUICEFS_S3_SECRET_KEY
echo
read -p "JuiceFS local cache size [50G]: " JUICEFS_CACHE_SIZE
JUICEFS_CACHE_SIZE="${JUICEFS_CACHE_SIZE:-50G}"
cat > "$VARS_FILE" <<EOF
base_domain = "$BASE_DOMAIN"
ssh_pubkey = "$SSH_PUBKEY"
juicefs_s3_endpoint = "$JUICEFS_S3_ENDPOINT"
juicefs_s3_bucket = "$JUICEFS_S3_BUCKET"
juicefs_s3_access_key = "$JUICEFS_S3_ACCESS_KEY"
juicefs_s3_secret_key = "$JUICEFS_S3_SECRET_KEY"
juicefs_cache_size = "$JUICEFS_CACHE_SIZE"
EOF
echo ""
echo "Configuration saved to $VARS_FILE"
}
if [ -f "$VARS_FILE" ]; then
echo "Existing configuration found ($VARS_FILE):"
echo ""
grep -v "secret_key" "$VARS_FILE"
echo " (secret keys hidden)"
echo ""
read -p "Use existing config? [Y/n]: " USE_EXISTING
if [[ "$USE_EXISTING" =~ ^[Nn] ]]; then
collect_values
fi
else
collect_values
fi
echo ""
echo "=== Initializing Packer plugins ===" echo "=== Initializing Packer plugins ==="
packer init ubuntu-server.pkr.hcl packer init ubuntu-server.pkr.hcl
@ -18,7 +68,7 @@ echo "=== Building image ==="
echo "This will take 15-30 minutes depending on your machine." echo "This will take 15-30 minutes depending on your machine."
echo "" echo ""
packer build -var-file=server.pkrvars.hcl ubuntu-server.pkr.hcl packer build -var-file="$VARS_FILE" ubuntu-server.pkr.hcl
echo "" echo ""
echo "===========================================" echo "==========================================="