282 lines
8.4 KiB
Bash
Executable File
282 lines
8.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# CyberHybridHub Web App Service Manager
|
|
# Usage: ./cyberhybridhub-web.sh [up|down]
|
|
|
|
set -e
|
|
|
|
# Configuration
|
|
APP_DIR="/home/chh/cyberhybridhub.com/publish"
|
|
APP_NAME="cyberhybridhub-web"
|
|
PID_FILE="$APP_DIR/.cyberhybridhub-web.pid"
|
|
LOG_FILE="$APP_DIR/.cyberhybridhub-web.log"
|
|
SERVICE_FILE="/etc/systemd/system/cyberhybridhub-web.service"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to print colored output
|
|
print_status() {
|
|
local color=$1
|
|
local message=$2
|
|
echo -e "${color}${message}${NC}"
|
|
}
|
|
|
|
# Function to check if the service is running
|
|
is_running() {
|
|
if [[ -f "$PID_FILE" ]]; then
|
|
local pid=$(cat "$PID_FILE")
|
|
if ps -p "$pid" > /dev/null 2>&1; then
|
|
return 0
|
|
else
|
|
# PID file exists but process is not running
|
|
rm -f "$PID_FILE"
|
|
return 1
|
|
fi
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# Function to kill any process running on port 8080
|
|
kill_port_8080() {
|
|
local pid_on_port=$(lsof -ti:8080 2>/dev/null || true)
|
|
if [[ -n "$pid_on_port" ]]; then
|
|
print_status "$YELLOW" "Killing process running on port 8080 (PID: $pid_on_port)..."
|
|
kill "$pid_on_port" 2>/dev/null || true
|
|
|
|
# Wait a moment and force kill if still running
|
|
sleep 1
|
|
if lsof -ti:8080 >/dev/null 2>&1; then
|
|
print_status "$YELLOW" "Force killing process on port 8080..."
|
|
kill -9 "$pid_on_port" 2>/dev/null || true
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Function to load environment variables
|
|
load_env_vars() {
|
|
if [ -f ".env" ]; then
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
# Skip empty lines and comments
|
|
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
|
[[ -z "${line// }" ]] && continue
|
|
|
|
# Export the variable properly handling quoted values
|
|
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
|
|
export "${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
|
|
fi
|
|
done < ".env"
|
|
print_status "$GREEN" "Environment variables loaded from .env"
|
|
else
|
|
print_status "$YELLOW" "Warning: .env file not found"
|
|
fi
|
|
}
|
|
|
|
# Function to create systemd service for auto-start
|
|
create_systemd_service() {
|
|
cat << EOF | sudo tee "$SERVICE_FILE" > /dev/null
|
|
[Unit]
|
|
Description=CyberHybridHub Web Service
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=chh
|
|
WorkingDirectory=$APP_DIR
|
|
ExecStart=/bin/bash -c 'set -a && source $APP_DIR/.env 2>/dev/null || true && set +a && exec $APP_DIR/server -A'
|
|
Restart=always
|
|
RestartSec=10
|
|
StandardOutput=append:$APP_DIR/.cyberhybridhub-web.log
|
|
StandardError=append:$APP_DIR/.cyberhybridhub-web.log
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable cyberhybridhub-web.service
|
|
print_status "$GREEN" "Systemd service created and enabled for auto-start on reboot"
|
|
}
|
|
|
|
# Function to remove systemd service
|
|
remove_systemd_service() {
|
|
if [[ -f "$SERVICE_FILE" ]]; then
|
|
sudo systemctl stop cyberhybridhub-web.service 2>/dev/null || true
|
|
sudo systemctl disable cyberhybridhub-web.service 2>/dev/null || true
|
|
sudo rm -f "$SERVICE_FILE"
|
|
sudo systemctl daemon-reload
|
|
print_status "$GREEN" "Systemd service removed and auto-start disabled"
|
|
fi
|
|
}
|
|
|
|
# Function to start the service
|
|
start_service() {
|
|
if is_running; then
|
|
print_status "$YELLOW" "CyberHybridHub web service is already running (PID: $(cat "$PID_FILE"))"
|
|
return 0
|
|
fi
|
|
|
|
print_status "$GREEN" "Starting CyberHybridHub web service..."
|
|
|
|
# Load environment variables
|
|
load_env_vars
|
|
|
|
# Kill any process running on port 8080 first
|
|
kill_port_8080
|
|
|
|
# Check if the app directory exists
|
|
if [[ ! -d "$APP_DIR" ]]; then
|
|
print_status "$RED" "Error: Application directory '$APP_DIR' does not exist"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if the server binary exists
|
|
if [[ ! -f "$APP_DIR/server" ]]; then
|
|
print_status "$RED" "Error: Server binary '$APP_DIR/server' not found"
|
|
print_status "$YELLOW" "Please ensure the server binary is built and available"
|
|
exit 1
|
|
fi
|
|
|
|
# Create systemd service for auto-start
|
|
create_systemd_service
|
|
|
|
# Start the service using systemctl
|
|
sudo systemctl start cyberhybridhub-web.service
|
|
|
|
# Wait a moment to check if the service started successfully
|
|
sleep 3
|
|
if sudo systemctl is-active --quiet cyberhybridhub-web.service; then
|
|
print_status "$GREEN" "CyberHybridHub web service started successfully with auto-start enabled"
|
|
print_status "$GREEN" "Log file: $LOG_FILE"
|
|
else
|
|
print_status "$RED" "Failed to start CyberHybridHub web service"
|
|
print_status "$YELLOW" "Check the log file: $LOG_FILE"
|
|
remove_systemd_service
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to stop the service
|
|
stop_service() {
|
|
if ! is_running; then
|
|
print_status "$YELLOW" "CyberHybridHub web service is not running"
|
|
else
|
|
local pid=$(cat "$PID_FILE")
|
|
print_status "$GREEN" "Stopping CyberHybridHub web service (PID: $pid)..."
|
|
|
|
# Send SIGTERM signal for graceful shutdown
|
|
kill "$pid" 2>/dev/null || true
|
|
|
|
# Wait for graceful shutdown
|
|
local count=0
|
|
while is_running && [[ $count -lt 10 ]]; do
|
|
sleep 1
|
|
count=$((count + 1))
|
|
done
|
|
|
|
# If still running, force kill
|
|
if is_running; then
|
|
print_status "$YELLOW" "Service didn't stop gracefully, forcing shutdown..."
|
|
kill -9 "$pid" 2>/dev/null || true
|
|
sleep 1
|
|
fi
|
|
|
|
# Clean up PID file
|
|
rm -f "$PID_FILE"
|
|
|
|
if ! is_running; then
|
|
print_status "$GREEN" "CyberHybridHub web service stopped successfully"
|
|
else
|
|
print_status "$RED" "Failed to stop CyberHybridHub web service"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Remove systemd service and disable auto-start
|
|
remove_systemd_service
|
|
}
|
|
|
|
# Function to reset the service
|
|
reset_service() {
|
|
print_status "$YELLOW" "Resetting CyberHybridHub web service..."
|
|
|
|
# Stop the service and remove systemd service
|
|
stop_service
|
|
|
|
# Kill any process on port 8080
|
|
kill_port_8080
|
|
|
|
# Clean up any remaining files
|
|
rm -f "$PID_FILE"
|
|
|
|
print_status "$GREEN" "CyberHybridHub web service has been reset"
|
|
}
|
|
|
|
# Function to show service status
|
|
show_status() {
|
|
print_status "$GREEN" "=== CyberHybridHub Web Service Status ==="
|
|
|
|
# Show systemctl status first
|
|
if systemctl list-unit-files | grep -q "cyberhybridhub-web.service"; then
|
|
print_status "$GREEN" "Systemd service status:"
|
|
sudo systemctl status cyberhybridhub-web.service --no-pager -l
|
|
echo ""
|
|
else
|
|
print_status "$YELLOW" "Systemd service not installed"
|
|
fi
|
|
|
|
# Show custom status
|
|
if sudo systemctl is-active --quiet cyberhybridhub-web.service 2>/dev/null; then
|
|
print_status "$GREEN" "Service is running (systemd managed)"
|
|
print_status "$GREEN" "Auto-start on reboot: ENABLED"
|
|
print_status "$GREEN" "Log file: $LOG_FILE"
|
|
elif is_running; then
|
|
local pid=$(cat "$PID_FILE")
|
|
print_status "$YELLOW" "Service is running (manual, PID: $pid)"
|
|
print_status "$YELLOW" "Auto-start on reboot: DISABLED"
|
|
print_status "$GREEN" "Log file: $LOG_FILE"
|
|
else
|
|
print_status "$YELLOW" "Service is not running"
|
|
if [[ -f "$SERVICE_FILE" ]]; then
|
|
print_status "$YELLOW" "Auto-start on reboot: ENABLED (service installed)"
|
|
else
|
|
print_status "$YELLOW" "Auto-start on reboot: DISABLED"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Main script logic
|
|
case "${1:-}" in
|
|
"up")
|
|
start_service
|
|
;;
|
|
"down")
|
|
stop_service
|
|
;;
|
|
"reset")
|
|
reset_service
|
|
;;
|
|
"status")
|
|
show_status
|
|
;;
|
|
"restart")
|
|
stop_service
|
|
sleep 2
|
|
start_service
|
|
;;
|
|
*)
|
|
echo "CyberHybridHub Web App Service Manager"
|
|
echo "Usage: $0 {up|down|reset|status|restart}"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " up - Start the web service and enable auto-start on reboot"
|
|
echo " down - Stop the web service and disable auto-start on reboot"
|
|
echo " reset - Completely reset the service (stop + cleanup)"
|
|
echo " status - Show service status and auto-start configuration"
|
|
echo " restart - Restart the web service"
|
|
exit 1
|
|
;;
|
|
esac
|