From 83414466c814fc4ebf9899e8d2f9c6f3d384751f Mon Sep 17 00:00:00 2001 From: Ryan Toler Date: Mon, 2 Mar 2026 06:30:49 +0000 Subject: [PATCH] init --- .gitingnore | 1 + bootstrap.sh | 65 +++++++++++ firstsetup.sh | 128 +++++++++++++++++++++ playbooks/downloader/docker-compose.env | 135 +++++++++++++++++++++++ playbooks/downloader/docker-compose.yaml | 61 ++++++++++ 5 files changed, 390 insertions(+) create mode 100644 .gitingnore create mode 100644 bootstrap.sh create mode 100644 firstsetup.sh create mode 100644 playbooks/downloader/docker-compose.env create mode 100644 playbooks/downloader/docker-compose.yaml diff --git a/.gitingnore b/.gitingnore new file mode 100644 index 0000000..b130a1b --- /dev/null +++ b/.gitingnore @@ -0,0 +1 @@ +firstrun \ No newline at end of file diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..06d23f8 --- /dev/null +++ b/bootstrap.sh @@ -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 \ No newline at end of file diff --git a/firstsetup.sh b/firstsetup.sh new file mode 100644 index 0000000..9b0df69 --- /dev/null +++ b/firstsetup.sh @@ -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 <"${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 diff --git a/playbooks/downloader/docker-compose.env b/playbooks/downloader/docker-compose.env new file mode 100644 index 0000000..b1f9999 --- /dev/null +++ b/playbooks/downloader/docker-compose.env @@ -0,0 +1,135 @@ +######################################################################## +######################################################################## +######################################################################## +## +## Docker Compose Environment Variable file for Jellyfin Media Stack +## +## Update any of the environment variables below as required. +## +## It is highly recommended Linux users set up a "docker" +## user, so the applications can access the local filesystem +## with this user's access privileges. Use PUID / PGID to map +## user access between the Docker apps and local filesystem. +## +######################################################################## +######################################################################## +######################################################################## + +#Name of the project in Docker +COMPOSE_PROJECT_NAME=media-stack + +# This is the network subnet which will be used inside the docker "media_network", change as required. +# LOCAL_SUBNET is your home network and is needed so the VPN client allows access to your home computers. +DOCKER_SUBNET=172.28.10.0/24 +DOCKER_GATEWAY=172.28.10.1 +LOCAL_SUBNET=192.168.0.0/24 +LOCAL_DOCKER_IP=192.168.0.57 + +# Each of the "*ARR" applications have been configured so the theme can be changed to your needs. +# Refer to Theme Park for more info / options: https://docs.theme-park.dev/theme-options/aquamarine/ +TP_DISABLE_THEME=false +TP_THEME=nord + +# These are the folders on your local host computer / NAS running docker, they MUST exist +# and have correct permissions for PUID and PGUI prior to running the docker-compose. +# +# Use the commands in the Guide to create all the sub-folders in each of these folders. + +# Host Data Folders - Will accept Linux, Windows, NAS folders +FOLDER_FOR_CONFIGS=/home/grail/docker +FOLDER_FOR_MEDIA=/media + +# File access, date and time details for the containers / applications to use. +# Run "sudo id docker" on host computer to find PUID / PGID and update these to suit. +PUID=1000 +PGID=1000 +UMASK=0002 +TIMEZONE=America/Chicago + +# Update your own Internet VPN provide details below +VPN_TYPE=openvpn +VPN_SERVICE_PROVIDER=airvpn +VPN_USERNAME= +VPN_PASSWORD= + + +SERVER_COUNTRIES= +SERVER_REGION= +SERVER_CITIES="Chicago Illinois" +SERVER_HOSTNAMES= + +# Fill in this item ONLY if you're using a custom OpenVPN configuration +# Should be inside gluetun data folder - Example: /gluetun/custom-openvpn.conf +# You can then edit it inside the FOLDER_FOR_CONFIGS location for gluetun. +OPENVPN_CUSTOM_CONFIG=/gluetun/ipvanish.openvpn.conf +#OPENVPN_CUSTOM_CONFIG= + +QBIT_PORT_TCP=44172 +QBIT_PORT_UDP=44172 +FLARESOLVERR_PORT=8191 +TDARR_SERVER_PORT=8266 +WEBUI_PORT_TDARR=8265 +RUTORRENT_PORT=8200 + + +# SWAG is configured for Reverse Proxy. Set your Internet gateway to redirect incoming ports 80 and 443 +# to the ports used below (using Docker IP Address), and they will be translated back to 80 and 443 by SWAG. +# Change these port numbers if you have conflicting services running on the Docker host computer. + +REVERSE_PROXY_PORT_HTTP=5080 +REVERSE_PROXY_PORT_HTTPS=5443 + +# SWAG REVERSE PROXY SETTINGS: +URL=your-domain-name-goes-here.com +SUBDOMAINS=wildcard +VALIDATION=dns +DNSPLUGIN=cloudflare +CERTPROVIDER= +PROPAGATION= +DUCKDNSTOKEN= +EMAIL= +ONLY_SUBDOMAINS=false +EXTRA_DOMAINS= +STAGING=false +HEALTH_VPN_DURATION_INITIAL=45s +HEALTH_VPN_DURATION_ADDITION=30s +UPDATER_PERIOD=8h +TZ=America/Chicago +PUID=1000 +PGID=1000 + +WAN_IP_CMD=false + +AUTH_DELAY=0s +MEMORY_LIMIT=256M +UPLOAD_MAX_SIZE=16M +OPCACHE_MEM_SIZE=128 +MAX_FILE_UPLOADS=50 +REAL_IP_FROM=0.0.0.0/32 +REAL_IP_HEADER=X-Forwarded-For +LOG_IP_VAR=remote_addr +LOG_ACCESS=true + +XMLRPC_AUTHBASIC_STRING=rTorrent XMLRPC restricted access +RUTORRENT_AUTHBASIC_STRING=ruTorrent restricted access +WEBDAV_AUTHBASIC_STRING=WebDAV restricted access + +RT_LOG_LEVEL=info +RT_LOG_EXECUTE=false +RT_LOG_XMLRPC=false + +RU_HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.0; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 +RU_HTTP_TIME_OUT=30 +RU_HTTP_USE_GZIP=true +RU_RPC_TIME_OUT=5 +RU_LOG_RPC_CALLS=false +RU_LOG_RPC_FAULTS=true +RU_PHP_USE_GZIP=false +RU_PHP_GZIP_LEVEL=2 +RU_SCHEDULE_RAND=10 +RU_LOG_FILE=/data/rutorrent/rutorrent.log +RU_DO_DIAGNOSTIC=true +RU_SAVE_UPLOADED_TORRENTS=true +RU_OVERWRITE_UPLOADED_TORRENTS=false +RU_FORBID_USER_SETTINGS=false +RU_LOCALE=UTF8 \ No newline at end of file diff --git a/playbooks/downloader/docker-compose.yaml b/playbooks/downloader/docker-compose.yaml new file mode 100644 index 0000000..dc552f9 --- /dev/null +++ b/playbooks/downloader/docker-compose.yaml @@ -0,0 +1,61 @@ +services: + gluetun: + image: qmcgaw/gluetun + # container_name: gluetun + restart: always + cap_add: + - NET_ADMIN + devices: + - /dev/net/tun + volumes: + - ./data/gluetun:/gluetun + ports: + - 11000:6881/udp #rutorrent + - 11001:11001 #rutorrent + - 11002:8080 #rutorrent + - 11003:9000 #rutorremt + - 4733:4733 #rutorrent + environment: + - PUID=1000 + - PGID=1000 + - TZ=America/Chicago + - VPN_SERVICE_PROVIDER=airvpn + - VPN_TYPE=openvpn # change it to your protocol + - OPENVPN_CUSTOM_CONFIG=/gluetun/ipvanish.openvpn.conf + - VPN_INTERFACE=tun0 + - FIREWALL_OUTBOUND_SUBNETS=192.168.0.0/24 + - SERVER_CITIES="Chicago Illinois" + - FIREWALL_VPN_INPUT_PORTS=4733 + + rutorrent: + image: crazymax/rtorrent-rutorrent:latest + # container_name: rutorrent + environment: + - PUID=1000 + - PGID=1000 + - TZ=America/Chicago + - XMLRPC_PORT=11001 #the default port is 8000 but mine was already in use. if yours is free then you don't need this line and you can change this line '- 11001:11001' in ports to '- 11001:8000' + - RT_INC_PORT=4733 + depends_on: + - gluetun + volumes: + - ./data/rutorrent/data:/data + - /data:/downloads + - ./data/rutorrent/password:/passwd + network_mode: service:gluetun + security_opt: + - no-new-privileges:true + restart: always + + tinyfilemanager: + image: tinyfilemanager/tinyfilemanager:master + container_name: tinyfilemanager + restart: unless-stopped + ports: + - 80:80 + environment: + - PUID=1000 + - PGID=1000 + volumes: + - /data/:/var/www/html/data/ + - ./data/tinyfilemanager/:/var/www/html/ \ No newline at end of file