130 lines
3.5 KiB
Bash
Executable File
130 lines
3.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" "$@"
|
|
}
|
|
|
|
playground() {
|
|
docker compose -f "$ROOT_DIR/playground/docker-compose.yml" "$@"
|
|
}
|
|
|
|
sync_env_values() {
|
|
local mongo_pass mongo_db
|
|
mongo_pass=$(grep -E '^MONGO_INITDB_ROOT_PASSWORD=' "$ENV_FILE" | cut -d '=' -f2-)
|
|
mongo_db=$(grep -E '^MONGODB_DB_NAME=' "$ENV_FILE" | cut -d '=' -f2-)
|
|
|
|
[[ -n "$mongo_db" ]] || mongo_db="theplug"
|
|
|
|
if [[ -n "$mongo_pass" ]]; then
|
|
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$mongo_pass@localhost:27017/$mongo_db?authSource=admin|" "$ENV_FILE"
|
|
fi
|
|
}
|
|
|
|
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
|
|
sync_env_values
|
|
info "Building and starting backend, frontend and MongoDB..."
|
|
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)"
|
|
}
|
|
|
|
start_deps() {
|
|
ensure_env
|
|
sync_env_values
|
|
info "Starting MongoDB for host-backend testing..."
|
|
compose up -d mongodb
|
|
info "Now run these on the host:"
|
|
info " npm run dev:backend"
|
|
info " npm run dev:frontend"
|
|
}
|
|
|
|
start_playground() {
|
|
info "Starting local playground upstream service..."
|
|
playground up -d
|
|
info "Playground service: http://127.0.0.1:9081"
|
|
}
|
|
|
|
if [[ "${1:-}" == "service" ]]; then
|
|
ensure_env
|
|
install_service
|
|
systemctl --user start theplug
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "${1:-}" == "deps" ]]; then
|
|
start_deps
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "${1:-}" == "playground" ]]; then
|
|
start_playground
|
|
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
|