diff --git a/backend/mpc-system/scripts/deploy.sh b/backend/mpc-system/scripts/deploy.sh index b4407242..b3995d4b 100644 --- a/backend/mpc-system/scripts/deploy.sh +++ b/backend/mpc-system/scripts/deploy.sh @@ -145,23 +145,42 @@ setup_directories() { configure_postgres() { log_info "Configuring PostgreSQL..." - # Load environment variables + # Load environment variables - use MPC_ prefix variables (same as Go code uses) source "$CONFIG_DIR/mpc.env" 2>/dev/null || true - local DB_USER="${POSTGRES_USER:-mpc_user}" - local DB_PASS="${POSTGRES_PASSWORD:-mpc_secret_password}" - local DB_NAME="mpc_system" + local DB_USER="${MPC_DATABASE_USER:-mpc_user}" + local DB_PASS="${MPC_DATABASE_PASSWORD:-your_secure_postgres_password_here}" + local DB_NAME="${MPC_DATABASE_DBNAME:-mpc_system}" + + # Configure pg_hba.conf to allow password authentication for local connections + local PG_HBA="/etc/postgresql/*/main/pg_hba.conf" + for hba_file in $PG_HBA; do + if [ -f "$hba_file" ]; then + # Backup original + cp "$hba_file" "${hba_file}.bak" 2>/dev/null || true + # Change 'peer' to 'md5' for local connections to allow password auth + sed -i 's/local all all peer/local all all md5/' "$hba_file" + sed -i 's/host all all 127.0.0.1\/32 scram-sha-256/host all all 127.0.0.1\/32 md5/' "$hba_file" + sed -i 's/host all all ::1\/128 scram-sha-256/host all all ::1\/128 md5/' "$hba_file" + log_info "Updated pg_hba.conf at $hba_file" + fi + done + + # Reload PostgreSQL to apply pg_hba.conf changes + systemctl reload postgresql 2>/dev/null || systemctl restart postgresql # Create database and user - sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" 2>/dev/null || true + sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" 2>/dev/null || \ + sudo -u postgres psql -c "ALTER USER $DB_USER WITH PASSWORD '$DB_PASS';" sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" 2>/dev/null || true sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" 2>/dev/null || true + sudo -u postgres psql -d "$DB_NAME" -c "GRANT ALL ON SCHEMA public TO $DB_USER;" 2>/dev/null || true # Run migrations log_info "Running database migrations..." - PGPASSWORD="$DB_PASS" psql -h localhost -U "$DB_USER" -d "$DB_NAME" -f "$MPC_HOME/migrations/001_init_schema.up.sql" 2>/dev/null || log_warn "Migration may have already been applied" + PGPASSWORD="$DB_PASS" psql -h 127.0.0.1 -U "$DB_USER" -d "$DB_NAME" -f "$MPC_HOME/migrations/001_init_schema.up.sql" 2>/dev/null || log_warn "Migration may have already been applied" - log_info "PostgreSQL configured" + log_info "PostgreSQL configured with user '$DB_USER' and database '$DB_NAME'" } configure_redis() { @@ -507,6 +526,22 @@ build() { log_info "Start services with: $0 start" } +# ============================================ +# Reconfigure Command (fix existing installation) +# ============================================ +reconfigure() { + check_root + + log_info "Reconfiguring MPC-System infrastructure..." + + configure_postgres + configure_redis + configure_rabbitmq + + log_info "Reconfiguration complete!" + log_info "Restart services with: $0 restart" +} + # ============================================ # Uninstall Command # ============================================ @@ -550,6 +585,9 @@ case "${1:-}" in build) build ;; + reconfigure) + reconfigure + ;; start) start_services ;; @@ -571,17 +609,18 @@ case "${1:-}" in *) echo "MPC-System Deployment Script" echo "" - echo "Usage: $0 {install|build|start|stop|restart|status|logs|uninstall}" + echo "Usage: $0 {install|build|reconfigure|start|stop|restart|status|logs|uninstall}" echo "" echo "Commands:" - echo " install - Install system dependencies (Go, PostgreSQL, Redis, RabbitMQ)" - echo " build - Build services and configure infrastructure" - echo " start - Start all MPC services" - echo " stop - Stop all MPC services" - echo " restart - Restart all MPC services" - echo " status - Show service status" - echo " logs - View service logs (use: $0 logs [service-name])" - echo " uninstall - Remove MPC services" + echo " install - Install system dependencies (Go, PostgreSQL, Redis, RabbitMQ)" + echo " build - Build services and configure infrastructure" + echo " reconfigure - Reconfigure PostgreSQL/Redis/RabbitMQ (fix auth issues)" + echo " start - Start all MPC services" + echo " stop - Stop all MPC services" + echo " restart - Restart all MPC services" + echo " status - Show service status" + echo " logs - View service logs (use: $0 logs [service-name])" + echo " uninstall - Remove MPC services" echo "" echo "Example:" echo " $0 install # First time setup" @@ -590,6 +629,10 @@ case "${1:-}" in echo " $0 start # Start services" echo " $0 status # Check status" echo "" + echo "Troubleshooting:" + echo " $0 reconfigure # Fix database authentication issues" + echo " $0 restart # Then restart services" + echo "" exit 1 ;; esac