#!/bin/bash set -e ############################################################################## # H&M EMS Product Review Tool - Deployment Script # # This script is idempotent and can be run for both initial deployment # and subsequent updates. # # Usage: sudo ./deploy.sh ############################################################################## # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration APP_DIR="/opt/hm_ems_report" DATA_DIR="/opt/hm-ems-data" WEB_DIR="/var/www/html/hm-ems-report" BACKUP_DIR="${DATA_DIR}/backups" SERVICE_NAME="hm-ems" VENV_DIR="${APP_DIR}/venv" LOG_DIR="/var/log/hm-ems" USER="vadym.samoilenko" GROUP="vadym.samoilenko" # Logging functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running as root if [ "$EUID" -ne 0 ]; then log_error "Please run as root (use sudo)" exit 1 fi log_info "Starting H&M EMS deployment..." ############################################################################## # 1. Check user exists and create directories ############################################################################## log_info "Checking user ${USER}..." if ! id "${USER}" &>/dev/null; then log_error "User ${USER} does not exist. Please create it first or update USER variable in this script." exit 1 fi log_info "Creating directories..." mkdir -p "${DATA_DIR}/Master_Json" mkdir -p "${DATA_DIR}/campaign_images" mkdir -p "${BACKUP_DIR}" mkdir -p "${WEB_DIR}" mkdir -p "${LOG_DIR}" log_info "Setting ownership to ${USER}:${GROUP}..." chown -R ${USER}:${GROUP} "${DATA_DIR}" chown -R ${USER}:${GROUP} "${WEB_DIR}" chown -R ${USER}:${GROUP} "${LOG_DIR}" chown -R ${USER}:${GROUP} "${APP_DIR}" # Set proper permissions chmod -R 755 "${DATA_DIR}" chmod -R 755 "${WEB_DIR}" chmod -R 755 "${APP_DIR}" ############################################################################## # 2. Backup existing data ############################################################################## if [ -d "${DATA_DIR}/Master_Json" ] && [ "$(ls -A ${DATA_DIR}/Master_Json)" ]; then TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="${BACKUP_DIR}/backup_${TIMESTAMP}.tar.gz" log_info "Creating backup: ${BACKUP_FILE}" tar -czf "${BACKUP_FILE}" -C "${DATA_DIR}" Master_Json 2>/dev/null || true # Keep only last 10 backups cd "${BACKUP_DIR}" ls -t backup_*.tar.gz 2>/dev/null | tail -n +11 | xargs rm -f 2>/dev/null || true cd - > /dev/null fi ############################################################################## # 3. Verify git repository ############################################################################## cd "${APP_DIR}" if [ ! -d .git ]; then log_error "Not a git repository. Please clone the repo to ${APP_DIR} first" exit 1 fi # Note: Git pull should be done manually before running this script # Example: cd /opt/hm_ems_report && git pull log_info "Using current code version (run 'git pull' manually before deploy if needed)" ############################################################################## # 4. Setup Python virtual environment ############################################################################## log_info "Setting up Python virtual environment..." if [ ! -d "${VENV_DIR}" ]; then log_info "Creating new virtual environment..." python3 -m venv "${VENV_DIR}" fi # Activate venv and install dependencies source "${VENV_DIR}/bin/activate" pip install --upgrade pip pip install -r requirements.txt log_info "Python dependencies installed" ############################################################################## # 5. Deploy frontend files ############################################################################## log_info "Deploying frontend files..." # Delete old files from web directory rm -rf "${WEB_DIR}"/* # Copy static files cp -r static/* "${WEB_DIR}/" # Set permissions chown -R ${USER}:${GROUP} "${WEB_DIR}" chmod -R 755 "${WEB_DIR}" log_info "Frontend files deployed to ${WEB_DIR}" ############################################################################## # 6. Install and configure systemd service ############################################################################## log_info "Configuring systemd service..." if [ -f "systemd/${SERVICE_NAME}.service" ]; then cp "systemd/${SERVICE_NAME}.service" "/etc/systemd/system/${SERVICE_NAME}.service" systemctl daemon-reload systemctl enable ${SERVICE_NAME} log_info "Systemd service configured" else log_warn "Systemd service file not found, skipping" fi ############################################################################## # 7. Restart service ############################################################################## log_info "Restarting application service..." if systemctl is-active --quiet ${SERVICE_NAME}; then systemctl restart ${SERVICE_NAME} log_info "Service restarted" else systemctl start ${SERVICE_NAME} log_info "Service started" fi # Wait a moment for service to start sleep 2 ############################################################################## # 8. Health check ############################################################################## log_info "Running health check..." if systemctl is-active --quiet ${SERVICE_NAME}; then log_info "✓ Service is running" # Check if port is listening if ss -tlnp | grep -q ":5000"; then log_info "✓ Port 5000 is listening" # Check API endpoint if curl -sf http://localhost:5000/api/files > /dev/null 2>&1; then log_info "✓ API endpoint responding" else log_warn "API endpoint not responding yet (may need more time to start)" fi else log_warn "Port 5000 not listening yet" fi else log_error "Service failed to start!" systemctl status ${SERVICE_NAME} --no-pager exit 1 fi ############################################################################## # 9. Summary ############################################################################## echo "" log_info "=========================================" log_info "Deployment completed successfully!" log_info "=========================================" echo "" echo "Application URL: https://ai-sandbox.oliver.solutions/hm-ems-report/" echo "API endpoint: https://ai-sandbox.oliver.solutions/hm-ems-report/api/files" echo "" echo "Useful commands:" echo " - Check status: sudo systemctl status ${SERVICE_NAME}" echo " - View logs: sudo journalctl -u ${SERVICE_NAME} -f" echo " - Restart: sudo systemctl restart ${SERVICE_NAME}" echo " - Stop: sudo systemctl stop ${SERVICE_NAME}" echo "" echo "Data location: ${DATA_DIR}" echo "Web files: ${WEB_DIR}" echo "Backups: ${BACKUP_DIR}" echo ""