65 lines
2.0 KiB
Bash
65 lines
2.0 KiB
Bash
#!/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 |