Files
ds-fire-fighter/server_deploy.sh
T
OwusuBlessing 60d2368be9 added
2025-06-18 17:15:19 +01:00

237 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# Configuration
REPO_URL="http://owusu:890eccfcea010beb94a0adba246aaf9258330b70@23.29.118.76:3000/owusu/ds-fire-fighter.git"
APP_DIR="/home/ec2-user/ds-fire-fighter"
BRANCH="dev"
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 yum update -y
sudo yum groupinstall -y "Development Tools"
sudo yum install -y openssl-devel bzip2-devel libffi-devel xz-devel
# Install Python 3.11 from source
cd /tmp
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar xzf Python-3.11.0.tgz
cd Python-3.11.0
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall
# Create symlink for python3.11
sudo ln -sf /usr/local/bin/python3.11 /usr/bin/python3.11
sudo ln -sf /usr/local/bin/pip3.11 /usr/bin/pip3.11
}
# 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
python3.11 -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 = SEGMIND_API_KEY="SG_28b00087ea064e64"
IMAGEKIT_PRIVATE_KEY="private_BXnyXGjdhBpBs/sU7avdyLwPx1o="
IMAGEKIT_PUBLIC_KEY="public_hb1iwGN/UWT+aYg9mMkUQNeFh40="
IMAGEKIT_URL_ENDPOINT="6pxd8st0ugi"
DEEPAI_API_KEY = "67768bbb-b946-400d-8188-ca21b0506ed8"
HEDRA_API_KEY ="sk_hedra_0MeJTqGmZnyt_jAXboOvXUGACTeeGAFuqpjuDU7ZVy5WOkSsM6B9kDe_vI_Dhcnn"
API_KEY_ACCESS = "face_forge_sk_test_51NxYz8KJh2LmPqR3vW4tU5w6X7y8Z9A0B1C2D3E4F5G6H7I8J9K0L1M2N3O4P5Q6R7S8T9U0V1W2X3Y4Z"
PORT=5000
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=ec2-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 python3.11; 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