ploughshares/install.sh

71 lines
2.6 KiB
Bash

#!/bin/bash
# Non-interactive installer for Ubuntu hosts.
# - Installs docker and docker compose plugin if missing
# - Installs repo under /opt/ploughshares (uses current repo copy)
# - Ensures .env exists (creates from .env.example if missing)
# - Installs systemd unit and timer for stack management and daily restart
# - Enables and starts services
set -euo pipefail
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
echo "This install.sh must be run as root (sudo)."
exit 1
fi
SRC_DIR="$(cd "$(dirname "$0")" && pwd)"
DEST_DIR="/opt/ploughshares"
SYSTEMD_DIR="/etc/systemd/system"
echo "=== Installing prerequisites (docker, compose) ==="
if ! command -v docker >/dev/null 2>&1; then
apt-get update
apt-get install -y ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
fi
echo "=== Deploying repo to $DEST_DIR ==="
mkdir -p "$DEST_DIR"
rsync -a --delete "$SRC_DIR/" "$DEST_DIR/"
echo "=== Ensuring .env exists ==="
if [ ! -f "$DEST_DIR/.env" ]; then
if [ -f "$DEST_DIR/.env.example" ]; then
cp "$DEST_DIR/.env.example" "$DEST_DIR/.env"
echo "Created $DEST_DIR/.env from example. Update secrets as needed."
else
touch "$DEST_DIR/.env"
echo "Created empty $DEST_DIR/.env."
fi
fi
echo "=== Installing systemd unit and timer ==="
install -m 0644 "$DEST_DIR/systemd/ploughshares-compose.service" "$SYSTEMD_DIR/ploughshares-compose.service"
install -m 0644 "$DEST_DIR/systemd/ploughshares-daily-restart.service" "$SYSTEMD_DIR/ploughshares-daily-restart.service"
install -m 0644 "$DEST_DIR/systemd/ploughshares-daily-restart.timer" "$SYSTEMD_DIR/ploughshares-daily-restart.timer"
systemctl daemon-reload
systemctl enable ploughshares-compose.service
systemctl start ploughshares-compose.service
systemctl enable ploughshares-daily-restart.timer
systemctl start ploughshares-daily-restart.timer
echo "=== Verifying services ==="
systemctl --no-pager status ploughshares-compose.service | sed -n '1,20p'
systemctl --no-pager status ploughshares-daily-restart.timer | sed -n '1,20p'
echo "Install complete. Repo at $DEST_DIR. Edit $DEST_DIR/.env to set secrets."