From c46bbd3c2838da194793621127e82ca639378f69 Mon Sep 17 00:00:00 2001 From: Rox0z Date: Mon, 9 Jun 2025 09:02:28 +0100 Subject: [PATCH] scripts --- setup.sh | 157 ++++++++++++++++++++++++++++++++++++++++++++- start_dev.sh | 17 ++++- troubleshooting.md | 9 +++ 3 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 troubleshooting.md diff --git a/setup.sh b/setup.sh index 9d769898..c3e6f268 100755 --- a/setup.sh +++ b/setup.sh @@ -81,7 +81,7 @@ poetry add setuptools || { } } -# Install PostgreSQL adapter +# Install PostgreSQL adapter (inside backend directory) echo "Installing PostgreSQL adapter (psycopg2-binary)..." poetry add psycopg2-binary || { echo "Failed to install psycopg2-binary through Poetry." @@ -110,6 +110,161 @@ echo cd "${ROOT_DIR}" || exit +# Database setup function +setup_postgres() { + echo + echo "=== PostgreSQL Database Setup ===" + + # Check if psql is installed + if ! command -v psql &> /dev/null; then + echo "Error: PostgreSQL client (psql) is not installed or not in PATH" + echo "Please install PostgreSQL: https://www.postgresql.org/download/" + return 1 + fi + + echo "PostgreSQL client found: $(which psql)" + + # Check if PostgreSQL server is running + if ! pg_isready &> /dev/null; then + echo "Error: PostgreSQL server is not running" + echo "Please start your PostgreSQL server" + return 1 + fi + + echo "PostgreSQL server is running" + + # Default values + local PG_USER="doccano_admin" + local PG_PASS="doccano_pass" + local PG_DB="doccano" + local PG_HOST="localhost" + local PG_PORT="5432" + + echo "Setting up PostgreSQL database with the following configuration:" + echo " Host: ${PG_HOST}:${PG_PORT}" + echo " Database: ${PG_DB}" + echo " User: ${PG_USER}" + echo " Password: ${PG_PASS}" + echo + + # Ask for confirmation + read -p "Continue with these settings? (y/n): " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Database setup cancelled" + return 1 + fi + + # Create user if it doesn't exist + echo "Creating user ${PG_USER} if it doesn't exist..." + psql -h ${PG_HOST} -p ${PG_PORT} -c "SELECT 1 FROM pg_roles WHERE rolname='${PG_USER}'" | grep -q 1 + if [ $? -ne 0 ]; then + psql -h ${PG_HOST} -p ${PG_PORT} -c "CREATE USER ${PG_USER} WITH PASSWORD '${PG_PASS}';" + if [ $? -ne 0 ]; then + echo "Error: Failed to create user ${PG_USER}" + return 1 + fi + echo "User ${PG_USER} created successfully" + else + echo "User ${PG_USER} already exists" + fi + + # Grant privileges to user + echo "Granting necessary privileges to ${PG_USER}..." + psql -h ${PG_HOST} -p ${PG_PORT} -c "ALTER USER ${PG_USER} WITH CREATEDB;" || { + echo "Error: Failed to grant CREATEDB privilege to ${PG_USER}" + return 1 + } + + # Create database if it doesn't exist + echo "Creating database ${PG_DB} if it doesn't exist..." + psql -h ${PG_HOST} -p ${PG_PORT} -c "SELECT 1 FROM pg_database WHERE datname='${PG_DB}'" | grep -q 1 + if [ $? -ne 0 ]; then + psql -h ${PG_HOST} -p ${PG_PORT} -c "CREATE DATABASE ${PG_DB} OWNER ${PG_USER};" + if [ $? -ne 0 ]; then + echo "Error: Failed to create database ${PG_DB}" + return 1 + fi + echo "Database ${PG_DB} created successfully" + else + echo "Database ${PG_DB} already exists" + + # Ensure the existing database is owned by our user + psql -h ${PG_HOST} -p ${PG_PORT} -c "ALTER DATABASE ${PG_DB} OWNER TO ${PG_USER};" || { + echo "Warning: Failed to change ownership of existing database ${PG_DB}" + } + fi + + # Grant privileges on database + echo "Granting all privileges on database ${PG_DB} to ${PG_USER}..." + psql -h ${PG_HOST} -p ${PG_PORT} -c "GRANT ALL PRIVILEGES ON DATABASE ${PG_DB} TO ${PG_USER};" || { + echo "Error: Failed to grant privileges on ${PG_DB} to ${PG_USER}" + return 1 + } + + echo "PostgreSQL database setup completed successfully" + echo "Connection URL: postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" + echo + + # Store the config in a .env file + echo "DATABASE_URL=postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" > "${ROOT_DIR}/.env" + echo "Database configuration saved to ${ROOT_DIR}/.env" + + # Create a .env file also in the backend directory for direct access + echo "DATABASE_URL=postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" > "${BACKEND_DIR}/.env" + echo "Database configuration also saved to ${BACKEND_DIR}/.env" + + return 0 +} + +# Docker PostgreSQL setup function +show_docker_command() { + echo + echo "=== PostgreSQL Docker Setup Command ===" + echo "To set up PostgreSQL in a Docker container, run:" + echo + echo "docker run -d \\" + echo " --name doccano-postgres \\" + echo " -e POSTGRES_USER=doccano_admin \\" + echo " -e POSTGRES_PASSWORD=doccano_pass \\" + echo " -e POSTGRES_DB=doccano \\" + echo " -v doccano-db:/var/lib/postgresql/data \\" + echo " -p 5432:5432 \\" + echo " postgres:13.8-alpine" + echo + echo "After running the container, press any key to continue setup..." + echo + read -n 1 -s -r + # After showing the command and waiting for user to run it, save the connection info + echo "DATABASE_URL=postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" > "${ROOT_DIR}/.env" + echo "DATABASE_URL=postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" > "${BACKEND_DIR}/.env" + echo "Database configuration saved to ${ROOT_DIR}/.env and ${BACKEND_DIR}/.env" +} + +# Ask about database setup +echo +echo "=== Database Configuration ===" +echo "How would you like to set up the PostgreSQL database?" +echo "1. Set up local PostgreSQL database with psql (requires PostgreSQL server)" +echo "2. Show Docker command to run PostgreSQL container" +echo "3. Skip database setup (use existing database or configure manually)" + +read -p "Select an option (1-3): " db_option +case $db_option in + 1) + setup_postgres + ;; + 2) + show_docker_command + ;; + 3) + echo "Skipping database setup" + ;; + *) + echo "Invalid option, skipping database setup" + ;; +esac + echo "=== Setup Complete ===" echo "All dependencies have been installed successfully." echo "Python version for virtual environment: $($PYTHON_CMD --version)" diff --git a/start_dev.sh b/start_dev.sh index 1e30a358..e7d1110e 100755 --- a/start_dev.sh +++ b/start_dev.sh @@ -24,6 +24,15 @@ echo "Frontend directory: ${FRONTEND_DIR}" echo "Backend directory: ${BACKEND_DIR}" echo +# Load environment variables from .env file if it exists +if [ -f "${ROOT_DIR}/.env" ]; then + echo "Loading environment variables from ${ROOT_DIR}/.env" + export $(grep -v '^#' "${ROOT_DIR}/.env" | xargs -0) +elif [ -f "${BACKEND_DIR}/.env" ]; then + echo "Loading environment variables from ${BACKEND_DIR}/.env" + export $(grep -v '^#' "${BACKEND_DIR}/.env" | xargs -0) +fi + # Start backend first echo "Starting backend services..." cd "${BACKEND_DIR}" || { echo "Error: Backend directory not found"; exit 1; } @@ -59,9 +68,11 @@ if ! poetry run python -c "import pkg_resources" 2>/dev/null; then fi fi -# Set database environment variable for PostgreSQL -echo "Configuring PostgreSQL database connection..." -export DATABASE_URL="postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" +# Set database environment variable for PostgreSQL if not already set +if [ -z "$DATABASE_URL" ]; then + echo "DATABASE_URL not found in environment. Setting default..." + export DATABASE_URL="postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" +fi echo "DATABASE_URL set to: ${DATABASE_URL}" # Ensure the database is migrated before starting the server diff --git a/troubleshooting.md b/troubleshooting.md new file mode 100644 index 00000000..fbe3fcfe --- /dev/null +++ b/troubleshooting.md @@ -0,0 +1,9 @@ +# Doccano Troubleshooting Guide + +This document provides solutions for common issues you might encounter when setting up and using Doccano. + +## Connection Issues + +### Proxy Error: ECONNREFUSED + +**Error message:**