215 lines
5.0 KiB
Bash
215 lines
5.0 KiB
Bash
#!/bin/bash
|
|
|
|
# Configuration
|
|
REPO_URL="http://owusu:890eccfcea010beb94a0adba246aaf9258330b70@23.29.118.76:3000/owusu/ds-fire-fighter.git"
|
|
APP_DIR="/home/owusu/ds-fire-fighter"
|
|
BRANCH="main"
|
|
PYTHON_VERSION="3.11"
|
|
WORKERS=4
|
|
THREADS=2
|
|
TIMEOUT=120
|
|
MAX_REQUESTS=1000
|
|
MAX_REQUESTS_JITTER=50
|
|
DEBUG_MODE=true
|
|
|
|
# Colors for output
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Logging function
|
|
log() {
|
|
local level=$1
|
|
local message=$2
|
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
|
|
|
case $level in
|
|
"INFO")
|
|
echo -e "${BLUE}[$timestamp] INFO: $message${NC}"
|
|
;;
|
|
"SUCCESS")
|
|
echo -e "${GREEN}[$timestamp] SUCCESS: $message${NC}"
|
|
;;
|
|
"WARNING")
|
|
echo -e "${YELLOW}[$timestamp] WARNING: $message${NC}"
|
|
;;
|
|
"ERROR")
|
|
echo -e "${RED}[$timestamp] ERROR: $message${NC}"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Debug logging function
|
|
debug_log() {
|
|
if [ "$DEBUG_MODE" = true ]; then
|
|
echo -e "${YELLOW}[DEBUG] $1${NC}"
|
|
fi
|
|
}
|
|
|
|
# Error handling
|
|
set -e
|
|
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
|
|
trap 'if [ $? -ne 0 ]; then log "ERROR" "Command failed: $last_command"; exit 1; fi' EXIT
|
|
|
|
# Function to check if a command exists
|
|
command_exists() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
# Function to install Python 3.11
|
|
install_python() {
|
|
log "INFO" "Installing Python ${PYTHON_VERSION}..."
|
|
sudo apt-get update
|
|
sudo apt-get install -y software-properties-common
|
|
sudo add-apt-repository -y ppa:deadsnakes/ppa
|
|
sudo apt-get update
|
|
sudo apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-venv python${PYTHON_VERSION}-dev
|
|
}
|
|
|
|
# Function to setup git repository
|
|
setup_repo() {
|
|
if [ ! -d "$APP_DIR" ]; then
|
|
log "INFO" "Cloning repository..."
|
|
git clone $REPO_URL $APP_DIR
|
|
else
|
|
log "INFO" "Updating repository..."
|
|
cd $APP_DIR
|
|
git fetch origin
|
|
git reset --hard origin/$BRANCH
|
|
git clean -fd
|
|
fi
|
|
}
|
|
|
|
# Function to setup virtual environment
|
|
setup_venv() {
|
|
log "INFO" "Setting up virtual environment..."
|
|
if [ ! -d "$APP_DIR/venv" ]; then
|
|
python${PYTHON_VERSION} -m venv $APP_DIR/venv
|
|
fi
|
|
source $APP_DIR/venv/bin/activate
|
|
pip install --upgrade pip
|
|
pip install -r $APP_DIR/requirements.txt
|
|
}
|
|
|
|
# Function to create gunicorn config
|
|
create_gunicorn_config() {
|
|
log "INFO" "Creating Gunicorn configuration..."
|
|
cat > $APP_DIR/gunicorn_config.py << EOL
|
|
import multiprocessing
|
|
import os
|
|
|
|
# Server socket
|
|
bind = "0.0.0.0:5042"
|
|
backlog = 2048
|
|
|
|
# Worker processes
|
|
workers = ${WORKERS}
|
|
worker_class = "uvicorn.workers.UvicornWorker"
|
|
worker_connections = 1000
|
|
timeout = ${TIMEOUT}
|
|
keepalive = 2
|
|
|
|
# Process naming
|
|
proc_name = "firefighter"
|
|
pythonpath = "."
|
|
|
|
# Logging
|
|
accesslog = "logs/access.log"
|
|
errorlog = "logs/error.log"
|
|
loglevel = "info"
|
|
|
|
# Server mechanics
|
|
daemon = False
|
|
pidfile = "gunicorn.pid"
|
|
umask = 0
|
|
user = None
|
|
group = None
|
|
tmp_upload_dir = None
|
|
|
|
# Worker lifecycle
|
|
max_requests = ${MAX_REQUESTS}
|
|
max_requests_jitter = ${MAX_REQUESTS_JITTER}
|
|
graceful_timeout = 30
|
|
preload_app = True
|
|
|
|
# Debug
|
|
reload = False
|
|
reload_engine = "auto"
|
|
spew = False
|
|
|
|
# Server mechanics
|
|
check_config = False
|
|
preload_app = True
|
|
EOL
|
|
}
|
|
|
|
# Function to setup systemd service
|
|
setup_service() {
|
|
log "INFO" "Setting up systemd service..."
|
|
sudo tee /etc/systemd/system/firefighter.service << EOL
|
|
[Unit]
|
|
Description=Fire Fighter Interview API
|
|
After=network.target
|
|
|
|
[Service]
|
|
User=$USER
|
|
WorkingDirectory=$APP_DIR
|
|
Environment="PATH=$APP_DIR/venv/bin"
|
|
Environment="PYTHONPATH=$APP_DIR"
|
|
ExecStart=$APP_DIR/start.sh
|
|
Restart=always
|
|
RestartSec=5
|
|
StartLimitInterval=0
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOL
|
|
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable firefighter
|
|
}
|
|
|
|
# Main deployment process
|
|
main() {
|
|
log "INFO" "Starting deployment process..."
|
|
|
|
# Check and install Python if needed
|
|
if ! command_exists python${PYTHON_VERSION}; then
|
|
install_python
|
|
fi
|
|
|
|
# Setup repository
|
|
setup_repo
|
|
|
|
# Create logs directory
|
|
mkdir -p $APP_DIR/logs
|
|
|
|
# Setup virtual environment and install dependencies
|
|
setup_venv
|
|
|
|
# Create gunicorn config
|
|
create_gunicorn_config
|
|
|
|
# Setup and start service
|
|
setup_service
|
|
sudo systemctl restart firefighter
|
|
|
|
log "SUCCESS" "Deployment completed!"
|
|
log "INFO" "Your application should now be running at http://localhost:5042"
|
|
|
|
# Print helpful commands
|
|
echo -e "\n${BLUE}Useful commands:${NC}"
|
|
echo "View service status: sudo systemctl status firefighter"
|
|
echo "View logs: sudo journalctl -u firefighter -f"
|
|
echo "View application logs: tail -f $APP_DIR/logs/access.log"
|
|
echo "View error logs: tail -f $APP_DIR/logs/error.log"
|
|
echo "Restart service: sudo systemctl restart firefighter"
|
|
echo "Stop service: sudo systemctl stop firefighter"
|
|
echo "Start service: sudo systemctl start firefighter"
|
|
echo "Deploy new version: ./server_deploy.sh"
|
|
}
|
|
|
|
# Run main function
|
|
main |