freeleaps-ops/apps/gitea-webhook-ambassador-python/start.sh

154 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# Gitea Webhook Ambassador Python Start Script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICE_NAME="gitea-webhook-ambassador-python"
LOG_FILE="$SCRIPT_DIR/logs/service.log"
PID_FILE="$SCRIPT_DIR/service.pid"
# Create logs directory
mkdir -p "$SCRIPT_DIR/logs"
# Activate virtual environment
source "$SCRIPT_DIR/venv/bin/activate"
# Function: Start service
start_service() {
echo "🚀 Starting $SERVICE_NAME..."
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
echo "❌ Service is already running (PID: $PID)"
return 1
else
echo "⚠️ Found stale PID file, cleaning up..."
rm -f "$PID_FILE"
fi
fi
# Start service in background
nohup python -m uvicorn app.main_demo:app --host 0.0.0.0 --port 8000 > "$LOG_FILE" 2>&1 &
PID=$!
echo $PID > "$PID_FILE"
# Wait for service to start
sleep 3
if ps -p $PID > /dev/null 2>&1; then
echo "✅ Service started successfully (PID: $PID)"
echo "📝 Log file: $LOG_FILE"
echo "🌐 Access: http://localhost:8000"
echo "🔑 Demo keys: demo_admin_key, demo_user_key"
else
echo "❌ Service failed to start"
rm -f "$PID_FILE"
return 1
fi
}
# Function: Stop service
stop_service() {
echo "🛑 Stopping $SERVICE_NAME..."
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
kill $PID
echo "✅ Service stopped (PID: $PID)"
else
echo "⚠️ Service not running"
fi
rm -f "$PID_FILE"
else
echo "⚠️ PID file does not exist"
fi
}
# Function: Restart service
restart_service() {
echo "🔄 Restarting $SERVICE_NAME..."
stop_service
sleep 2
start_service
}
# Function: Show status
status_service() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
echo "$SERVICE_NAME is running (PID: $PID)"
echo "📝 Log file: $LOG_FILE"
echo "🌐 Access: http://localhost:8000"
else
echo "$SERVICE_NAME is not running (PID file exists but process not found)"
rm -f "$PID_FILE"
fi
else
echo "$SERVICE_NAME is not running"
fi
}
# Function: Show logs
show_logs() {
if [ -f "$LOG_FILE" ]; then
echo "📝 Showing latest logs (last 50 lines):"
echo "----------------------------------------"
tail -n 50 "$LOG_FILE"
echo "----------------------------------------"
echo "Full log file: $LOG_FILE"
else
echo "❌ Log file does not exist"
fi
}
# Function: Follow logs
follow_logs() {
if [ -f "$LOG_FILE" ]; then
echo "📝 Real-time logs (Ctrl+C to exit):"
tail -f "$LOG_FILE"
else
echo "❌ Log file does not exist"
fi
}
# Main logic
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
restart)
restart_service
;;
status)
status_service
;;
logs)
show_logs
;;
follow)
follow_logs
;;
*)
echo "Usage: $0 {start|stop|restart|status|logs|follow}"
echo ""
echo "Command description:"
echo " start - Start service"
echo " stop - Stop service"
echo " restart - Restart service"
echo " status - Show service status"
echo " logs - Show latest logs"
echo " follow - Show real-time logs"
echo ""
echo "Examples:"
echo " $0 start # Start service"
echo " $0 status # Show status"
echo " $0 logs # Show logs"
exit 1
;;
esac