Files
thePlug/setup.sh
T
2026-05-04 23:59:48 +02:00

87 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[+]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
die() { echo -e "${RED}[x] FATAL:${NC} $*" >&2; exit 1; }
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$ROOT_DIR/backend/.env"
compose() {
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/docker-compose.yml" "$@"
}
ensure_env() {
if [[ -f "$ENV_FILE" ]]; then
return
fi
warn "backend/.env not found, creating it from backend/.env.example"
cp "$ROOT_DIR/backend/.env.example" "$ENV_FILE"
local mongo_pass jwt_secret auth_pass
mongo_pass=$(openssl rand -hex 8)
jwt_secret=$(openssl rand -hex 20)
auth_pass=$(openssl rand -hex 10)
sed -i "s|^MONGO_INITDB_ROOT_PASSWORD=.*|MONGO_INITDB_ROOT_PASSWORD=$mongo_pass|" "$ENV_FILE"
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$mongo_pass@localhost:27017/theplug?authSource=admin|" "$ENV_FILE"
sed -i "s|^JWT_SECRET_KEY=.*|JWT_SECRET_KEY=$jwt_secret|" "$ENV_FILE"
sed -i "s|^AUTH_PASSWORD=.*|AUTH_PASSWORD=$auth_pass|" "$ENV_FILE"
info "Generated credentials in $ENV_FILE"
info "AUTH_USERNAME: admin"
info "AUTH_PASSWORD: $auth_pass"
}
install_service() {
mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/theplug.service <<EOF
[Unit]
Description=thePlug stack
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=$ROOT_DIR
ExecStart=/usr/bin/docker compose --env-file $ENV_FILE -f $ROOT_DIR/docker-compose.yml up -d
ExecStop=/usr/bin/docker compose --env-file $ENV_FILE -f $ROOT_DIR/docker-compose.yml down
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable theplug
info "systemd user service installed"
}
start_stack() {
ensure_env
info "Building and starting backend, frontend, MongoDB and Redis..."
compose up -d --build
info "Backend: http://localhost:$(grep -E '^API_PORT=' "$ENV_FILE" | cut -d '=' -f2 || echo 3671)"
info "Frontend: http://localhost:$(grep -E '^FRONTEND_PORT=' "$ENV_FILE" | cut -d '=' -f2 || echo 5173)"
}
if [[ "${1:-}" == "service" ]]; then
ensure_env
install_service
systemctl --user start theplug
exit 0
fi
if [[ -d "/srv/theplug" && "$ROOT_DIR" != "/srv/theplug" ]]; then
info "thePlug found in /srv/theplug, switching there"
cd /srv/theplug
exec ./setup.sh "$@"
fi
start_stack