This commit is contained in:
Ryan Toler
2026-03-02 06:30:49 +00:00
parent ddbcf0b9a3
commit 83414466c8
5 changed files with 390 additions and 0 deletions

65
bootstrap.sh Normal file
View File

@@ -0,0 +1,65 @@
#!/usr/bin/env bash
# ------------------------------------------------------------
# firstrun.sh – One‑shot VM bootstrap script.
#
# Run this via a systemd unit on first boot. The script will:
# • Check /apps/firstrun (marker file) to skip if already ran
# • Clone a repo into /apps/$HOSTNAME
# • Execute an Ansible playbook that installs Docker
# • Restore data from /data if it is empty/missing
# • Create a systemd service that runs docker‑compose
# • Log everything to /apps/firstrun.log
# • Optionally send a Discord webhook (placeholder)
#
# ------------------------------------------------------------
set -euo pipefail
# ---------- CONFIG ----------
REPO_URL="https://github.com/tolerryan/Watari-Ansible.git" # <-- change this!
DISCORD_WEBHOOK="" # <-- empty for now
LOG_FILE="/apps/firstrun.log"
MARKER_FILE="/apps/firstrun"
HOSTNAME="$(hostname -s)"
APP_DIR="/apps/"
DATA_DIR="/data"
# ---------- HELPER ----------
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "${LOG_FILE}"
}
error_exit() {
log "ERROR: $*"
# Placeholder for Discord failure webhook
if [[ -n "$DISCORD_WEBHOOK" ]]; then
curl -s -X POST "$DISCORD_WEBHOOK" \
-H 'Content-Type: application/json' \
-d "{\"content\": \"Firstrun failed on ${HOSTNAME}: $*\"}"
fi
exit 1
}
# ---------- 1. Marker check ----------
if [[ -f "${MARKER_FILE}" && -s "${MARKER_FILE}" ]]; then
log "Marker file exists – script has already run. Exiting."
exit 0
fi
# ---------- 2. Prepare logging ----------
mkdir -p "$(dirname "${LOG_FILE}")"
touch "${LOG_FILE}"
chmod 640 "${LOG_FILE}"
log "=== Starting firstrun on ${HOSTNAME} ==="
# ---------- 3. Clone the repo ----------
if [[ ! -d "${APP_DIR}" ]]; then
log "Cloning repository to ${APP_DIR}"
git clone --depth 1 "${REPO_URL}" "${APP_DIR}" 2>&1 | tee -a "${LOG_FILE}"
else
log "App directory already exists – pulling latest changes"
cd "${APP_DIR}" && git pull origin main 2>&1 | tee -a "${LOG_FILE}"
fi