recordding firstrun.sh and setup-compse.yaml
This commit is contained in:
8
firstrun.sh
Normal file
8
firstrun.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# This script is stored in the template and is executed the first time a new container boots. |
|
||||
# Setup-lxc should contain all basic setup requirements needed
|
||||
# ie
|
||||
# docker setup, docker-compose install
|
||||
# restic restore from misamisa
|
||||
ansible-pull --clean --vault-password-file ./vault.pass --private-key ~/.ssh/github_ro -U git@github.com:tolerryan/Watari-Ansible.git playbooks/setup-lxc.yaml
|
||||
systemctl disable firstrun.service
|
||||
119
firstsetup.sh
119
firstsetup.sh
@@ -1,119 +0,0 @@
|
||||
#!/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} ==="
|
||||
|
||||
|
||||
|
||||
# ---------- 4. Run Ansible playbook (lxc setup) ----------
|
||||
ANSIBLE_PLAYBOOK="${APP_DIR}/playbooks/setup-lxc.yaml"
|
||||
|
||||
if [[ ! -f "${ANSIBLE_PLAYBOOK}" ]]; then
|
||||
error_exit "Ansible playbook not found: ${ANSIBLE_PLAYBOOK}"
|
||||
fi
|
||||
|
||||
log "Running Ansible lxc‑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
|
||||
51
playbooks/setup-compose.yaml
Normal file
51
playbooks/setup-compose.yaml
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
- name: Pull repo, copy compose and restart service if needed
|
||||
hosts: localhost # run on the pull‑host itself
|
||||
gather_facts: true
|
||||
|
||||
vars:
|
||||
repo_url: "git@github.com:tolerryan/Watari-Ansible.git"
|
||||
git_branch: "main" # or a tag / commit SHA
|
||||
src_dir_in_repo: "playbooks"
|
||||
local_clone_path: "/opt/Watari-Ansible"
|
||||
apps_root: "/apps"
|
||||
|
||||
tasks:
|
||||
|
||||
- name: Pull the latest repo
|
||||
ansible.builtin.git:
|
||||
repo: "{{ repo_url }}"
|
||||
dest: "{{ local_clone_path }}"
|
||||
version: "{{ git_branch }}"
|
||||
update: true # pull instead of clone on subsequent runs
|
||||
register: git_result
|
||||
|
||||
- name: Set path to this host's compose file in the repo
|
||||
set_fact:
|
||||
compose_src: "{{ local_clone_path }}/{{ src_dir_in_repo }}/{{ inventory_hostname }}/docker-compose.yaml"
|
||||
|
||||
- name: Ensure destination directory exists
|
||||
ansible.builtin.file:
|
||||
path: "{{ apps_root }}/{{ inventory_hostname }}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Copy compose file (notify if changed)
|
||||
ansible.builtin.copy:
|
||||
src: "{{ compose_src }}"
|
||||
dest: "{{ apps_root }}/{{ inventory_hostname }}/docker-compose.yaml"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: restart docker‑compose service
|
||||
register: copy_result
|
||||
|
||||
# ------------------------------------------
|
||||
# Handlers – only run if the copy task reported a change
|
||||
# ------------------------------------------
|
||||
|
||||
handlers:
|
||||
- name: restart docker‑compose service
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ inventory_hostname }}.service"
|
||||
state: restarted
|
||||
Reference in New Issue
Block a user