init
This commit is contained in:
128
firstsetup.sh
Normal file
128
firstsetup.sh
Normal file
@@ -0,0 +1,128 @@
|
||||
#!/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/${HOSTNAME}"
|
||||
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
|
||||
|
||||
# ---------- 4. Run Ansible playbook (Docker setup) ----------
|
||||
ANSIBLE_PLAYBOOK="${APP_DIR}/ansible/playbooks/setup-docker.yml"
|
||||
|
||||
if [[ ! -f "${ANSIBLE_PLAYBOOK}" ]]; then
|
||||
error_exit "Ansible playbook not found: ${ANSIBLE_PLAYBOOK}"
|
||||
fi
|
||||
|
||||
log "Running Ansible Docker‑setup playbook"
|
||||
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
|
||||
-i localhost, \
|
||||
--connection=local \
|
||||
"${ANSIBLE_PLAYBOOK}" 2>&1 | tee -a "${LOG_FILE}"
|
||||
|
||||
# ---------- 5. Data restore placeholder ----------
|
||||
if [[ ! -d "${DATA_DIR}" || "$(ls -A ${DATA_DIR} 2>/dev/null)" == "" ]]; then
|
||||
log "Data directory empty or missing – running Restic restore (placeholder)"
|
||||
echo "Restic restore would run here" | tee -a "${LOG_FILE}"
|
||||
else
|
||||
log "Data directory already populated – skipping restore."
|
||||
fi
|
||||
|
||||
# ---------- 6. Create docker‑compose systemd service ----------
|
||||
SERVICE_FILE="/etc/systemd/system/docker-compose-${HOSTNAME}.service"
|
||||
|
||||
cat <<EOF >"${SERVICE_FILE}"
|
||||
[Unit]
|
||||
Description=Docker Compose for ${HOSTNAME}
|
||||
Requires=docker.service
|
||||
After=docker.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
RemainAfterExit=yes
|
||||
WorkingDirectory=${APP_DIR}
|
||||
ExecStart=/usr/bin/docker compose -f ${APP_DIR}/docker-compose.yaml up -d
|
||||
ExecStop=/usr/bin/docker compose -f ${APP_DIR}/docker-compose.yaml down
|
||||
Restart=no
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
log "Reloading systemd daemon and enabling docker‑compose service"
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now "docker-compose-${HOSTNAME}.service" 2>&1 | tee -a "${LOG_FILE}"
|
||||
|
||||
# ---------- 7. Discord webhook placeholder ----------
|
||||
if [[ -n "$DISCORD_WEBHOOK" ]]; then
|
||||
log "Sending success notification to Discord"
|
||||
curl -s -X POST "$DISCORD_WEBHOOK" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"content\": \"Firstrun succeeded on ${HOSTNAME}.\"}" 2>&1 | tee -a "${LOG_FILE}"
|
||||
else
|
||||
log "Discord webhook URL not set – skipping notification."
|
||||
fi
|
||||
|
||||
# ---------- 8. Mark completion ----------
|
||||
log "Creating marker file to prevent re‑execution"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S')" > "${MARKER_FILE}"
|
||||
|
||||
log "=== firstrun completed successfully ==="
|
||||
exit 0
|
||||
Reference in New Issue
Block a user