#!/bin/bash # Remote deployment script for the AI service # Usage: ./remote_deploy.sh [server_ip] [user] [port] [remote_dir] # Default values SERVER_IP=${1:-"104.225.217.215"} # IP address of the server where OpenWebUI is installed SERVER_USER=${2:-"root"} # SSH username for the server SERVER_PORT=${3:-"22"} # SSH port for the server REMOTE_DIR=${4:-"/root/openwebui"} # Directory where OpenWebUI is installed LOCAL_DIR="." echo "Deploying to server: $SERVER_IP" echo "Remote directory: $REMOTE_DIR" # Check if the server is reachable echo "Checking if server is reachable..." ssh -q -o BatchMode=yes -o ConnectTimeout=5 -p $SERVER_PORT $SERVER_USER@$SERVER_IP exit if [ $? -ne 0 ]; then echo "Error: Cannot connect to server $SERVER_IP" exit 1 fi # Create a subdirectory for our AI service in the OpenWebUI directory echo "Creating AI service directory in OpenWebUI..." ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "mkdir -p $REMOTE_DIR/ai_service_app" # Sync files to the server echo "Syncing files to server..." rsync -avz -e "ssh -p $SERVER_PORT" --exclude 'venv' --exclude '__pycache__' --exclude '*.pyc' --exclude '.git' \ $LOCAL_DIR/ $SERVER_USER@$SERVER_IP:$REMOTE_DIR/ai_service_app/ # Install dependencies on the server echo "Installing dependencies on the server..." ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "cd $REMOTE_DIR/ai_service_app && \ python3 -m venv venv || true && \ source venv/bin/activate && \ pip install --upgrade pip && \ pip install -r requirements.txt" # Stop any existing service echo "Stopping any existing service..." ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "pkill -f 'uvicorn ai_service.api:app' || true" # Set up environment file if it doesn't exist echo "Setting up environment file..." ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "cd $REMOTE_DIR/ai_service_app && \ if [ ! -f ai_service/.env ]; then \ cp ai_service/.env.example ai_service/.env; \ echo 'Created .env file from .env.example'; \ else \ echo '.env file already exists'; \ fi" # Start the service echo "Starting the service..." ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "cd $REMOTE_DIR/ai_service_app && \ source venv/bin/activate && \ bash ai_service/deploy.sh" # Check if the service is running echo "Checking if the service is running..." sleep 5 ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "ps aux | grep 'uvicorn ai_service.api:app' | grep -v grep" if [ $? -eq 0 ]; then echo "Service is running!" echo "You can access the API at: http://$SERVER_IP:5252" echo "Check logs with: ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP 'tail -f $REMOTE_DIR/ai_service_app/logs/ai_service.log'" else echo "Error: Service failed to start. Check logs on the server." exit 1 fi echo "Deployment completed successfully!"