#!/bin/bash # Define variables for easier modifications and readability GIT_REPO="https://git.maxprint.io/MaxApex/ticket_ai_flask_app.git" APP_DIR="/opt/ticket-ai" VENV_DIR="$APP_DIR/venv" SERVICE_FILE="/etc/systemd/system/ticket-ai.service" PYTHON_BIN="$VENV_DIR/bin/python3" ACTIVATE_SCRIPT="$VENV_DIR/bin/activate" IPTABLES_RULES=("83.136.253.122") # Add IPs as needed # Ensure the script is run as root if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi # Installing git yum install -y git || { echo "Failed to install git. Exiting."; exit 1; } # Clone the repository if it doesn't exist if [ ! -d "$APP_DIR" ]; then git clone $GIT_REPO $APP_DIR || { echo "Failed to clone repository. Exiting."; exit 1; } else echo "$APP_DIR already exists. Skipping clone." fi # Navigate to the repo directory cd $APP_DIR || { echo "Failed to navigate to $APP_DIR. Exiting."; exit 1; } # Create virtual environment if it doesn't exist if [ ! -d "$VENV_DIR" ]; then python3 -m venv $VENV_DIR else echo "$VENV_DIR already exists. Skipping virtual environment creation." fi # Activate virtual environment and install dependencies source $ACTIVATE_SCRIPT pip install Flask flask_jwt_extended || { echo "Failed to install Flask or flask_jwt_extended. Exiting."; exit 1; } # Create systemd service file cat < $SERVICE_FILE [Unit] Description=Ticket AI [Service] Type=simple ExecStart=$PYTHON_BIN $APP_DIR/src/app.py [Install] WantedBy=multi-user.target EOF # Reload systemd to recognize the new service and start it systemctl daemon-reload systemctl start ticket-ai.service || { echo "Failed to start ticket-ai.service. Exiting."; exit 1; } # Add firewall rules to accept traffic for IP in "${IPTABLES_RULES[@]}"; do iptables -A INPUT -p tcp -m tcp -s $IP --dport 5000 -j ACCEPT done # Save iptables rules and restart the service to apply changes service iptables save service iptables restart || { echo "Failed to restart iptables. Exiting."; exit 1; } echo "Setup completed successfully."