diff --git a/scripts/provision_node.sh b/scripts/provision_node.sh index 43eb891..6667b7d 100755 --- a/scripts/provision_node.sh +++ b/scripts/provision_node.sh @@ -28,21 +28,22 @@ PROJECT_ROOT=$(cd "$BACKEND_ROOT/.." && pwd) CONFIGS_DIR="$PROJECT_ROOT/configs" FRONTEND_DIR="$PROJECT_ROOT/web2-client" -if [[ ! -d "$CONFIGS_DIR" ]]; then - echo "Expected configs directory at $CONFIGS_DIR." >&2 - exit 1 -fi -if [[ ! -f "$CONFIGS_DIR/docker-compose.yml" ]]; then - echo "Missing docker-compose.yml in $CONFIGS_DIR." >&2 - exit 1 -fi -if [[ ! -d "$FRONTEND_DIR" ]]; then - echo "Warning: web2-client directory not found at $FRONTEND_DIR (frontend build will fail)." >&2 -fi - ENV_FILE="$CONFIGS_DIR/.env" ENV_EXAMPLE="$BACKEND_ROOT/env.example" +DEFAULT_BACKEND_REMOTE="https://git.projscale.dev/my-dev/uploader-bot" +if [[ -d "$BACKEND_ROOT/.git" ]]; then + DEFAULT_BACKEND_REMOTE=$(git -C "$BACKEND_ROOT" config --get remote.origin.url 2>/dev/null || echo "$DEFAULT_BACKEND_REMOTE") +fi +DEFAULT_CONFIGS_REMOTE="https://git.projscale.dev/my-dev/configs" +if [[ -d "$PROJECT_ROOT/configs/.git" ]]; then + DEFAULT_CONFIGS_REMOTE=$(git -C "$PROJECT_ROOT/configs" config --get remote.origin.url 2>/dev/null || echo "$DEFAULT_CONFIGS_REMOTE") +fi +DEFAULT_WEB2_REMOTE="https://git.projscale.dev/my-dev/web2-client" +if [[ -d "$PROJECT_ROOT/web2-client/.git" ]]; then + DEFAULT_WEB2_REMOTE=$(git -C "$PROJECT_ROOT/web2-client" config --get remote.origin.url 2>/dev/null || echo "$DEFAULT_WEB2_REMOTE") +fi + trim() { local val="$1" val="${val#${val%%[![:space:]]*}}" @@ -69,6 +70,41 @@ update_env() { echo "${key}=${value}" >> "$ENV_FILE" } +RUN_USER=${SUDO_USER:-$(logname 2>/dev/null || echo root)} +if ! id -u "$RUN_USER" >/dev/null 2>&1; then + RUN_USER=root +fi +RUN_GROUP=$(id -gn "$RUN_USER" 2>/dev/null || echo $RUN_USER) + +ensure_repo() { + local dir="$1" + local default_url="$2" + local label="$3" + + if [[ -d "$dir/.git" ]]; then + return + fi + if [[ -d "$dir" && ! -d "$dir/.git" ]]; then + echo "Found existing $label directory at $dir without git metadata." >&2 + read -r -p "Use the existing directory as-is? [y/N]: " reuse || true + reuse=${reuse:-N} + if [[ ! $reuse =~ ^[Yy]$ ]]; then + echo "Please remove or move $dir and rerun the script." >&2 + exit 1 + fi + return + fi + + read -r -p "Git URL for $label repository [$default_url]: " repo_url || true + repo_url=${repo_url:-$default_url} + mkdir -p "$(dirname "$dir")" + if ! git clone "$repo_url" "$dir"; then + echo "Failed to clone $label repository from $repo_url" >&2 + exit 1 + fi + chown -R "$RUN_USER:$RUN_GROUP" "$dir" || true +} + prompt_required() { local var="$1" local label="$2" @@ -105,6 +141,14 @@ prompt_optional() { printf -v "$var" '%s' "$value" } +# Install base dependencies +apt-get update -y +apt-get install -y ca-certificates curl gnupg apt-transport-https software-properties-common git make nginx certbot python3-certbot-nginx python3 jq openssl + +# Ensure repository checkouts +ensure_repo "$CONFIGS_DIR" "$DEFAULT_CONFIGS_REMOTE" "configs" +ensure_repo "$FRONTEND_DIR" "$DEFAULT_WEB2_REMOTE" "web2-client" + # Prepare environment file if [[ ! -f "$ENV_FILE" ]]; then echo "Creating $ENV_FILE from example template." >&2 @@ -117,10 +161,7 @@ if [[ ! -f "$ENV_FILE" ]]; then fi fi fi - -# Install base dependencies -apt-get update -y -apt-get install -y ca-certificates curl gnupg apt-transport-https software-properties-common git make nginx certbot python3-certbot-nginx python3 jq openssl +chown "$RUN_USER:$RUN_GROUP" "$ENV_FILE" || true # Docker repository setup install -m 0755 -d /etc/apt/keyrings