135 lines
4.7 KiB
Bash
135 lines
4.7 KiB
Bash
#!/bin/bash
|
|
# install_service.sh
|
|
# This script must be run with sudo on the target machine.
|
|
# It installs the Wilcon Agent as a systemd service.
|
|
|
|
set -e # Exit immediately if a command exits with a non-zero status.
|
|
|
|
echo "--- Wilcon Agent Service Installer ---"
|
|
|
|
# Check if running as root
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Please run this script with sudo: sudo ./install_service.sh"
|
|
exit 1
|
|
fi
|
|
|
|
# --- Configuration ---
|
|
AGENT_USER="agent_user"
|
|
SERVICE_NAME="wilcon-agent"
|
|
BACKEND_SCRIPT="wilcon.py"
|
|
FRONTEND_FILE="index.html"
|
|
|
|
# Determine the script's own directory to find project files
|
|
# This makes the script location-independent.
|
|
INSTALL_PATH=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
|
|
echo "Installation path detected: $INSTALL_PATH"
|
|
|
|
# Determine the primary IP address of the machine for configuration
|
|
# This is needed for CORS and the frontend API URL.
|
|
TARGET_IP=$(hostname -I | awk '{print $1}')
|
|
if [ -z "$TARGET_IP" ]; then
|
|
echo "Error: Could not determine the IP address of this machine."
|
|
exit 1
|
|
fi
|
|
echo "Machine IP address detected: $TARGET_IP"
|
|
BACKEND_PORT="8080" # This should match the port in wilcon.py
|
|
|
|
LOG_DIR="/var/log/wilcon-agent"
|
|
|
|
# --- Installation Steps ---
|
|
|
|
# 1. Create log directory
|
|
echo " Creating log directory at $LOG_DIR..."
|
|
mkdir -p "$LOG_DIR"
|
|
|
|
# 2. Create a non-login system user to run the service
|
|
if ! id -u "$AGENT_USER" >/dev/null 2>&1; then
|
|
echo " Creating system user '$AGENT_USER'..."
|
|
useradd -r -s /bin/false "$AGENT_USER"
|
|
else
|
|
echo " System user '$AGENT_USER' already exists."
|
|
fi
|
|
|
|
# Set ownership of the log directory now that the user is guaranteed to exist
|
|
chown "${AGENT_USER}:${AGENT_USER}" "$LOG_DIR"
|
|
|
|
# 3. Check for Python and install if necessary
|
|
echo " Checking for Python 3.10..."
|
|
# On Debian/Ubuntu, python3-venv is a separate package needed for creating virtual environments.
|
|
# We check for both the python command and the venv package.
|
|
if ! command -v python3.10 &>/dev/null || ! dpkg -s python3.10-venv &>/dev/null; then
|
|
echo " Python 3.10 and/or python3.10-venv not found. Attempting to install..."
|
|
apt update && apt install -y python3.10 python3.10-venv
|
|
if [ $? -ne 0 ]; then
|
|
echo " Error: Failed to install Python 3.10 and/or python3.10-venv. Please install them manually."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# 4. Create virtual environment and install dependencies
|
|
echo " Creating Python virtual environment in $INSTALL_PATH/venv..."
|
|
echo " Ensuring clean state for virtual environment..."
|
|
rm -rf "$INSTALL_PATH/venv"
|
|
python3.10 -m venv "$INSTALL_PATH/venv"
|
|
|
|
echo " Installing Python dependencies..."
|
|
# Activate venv and install
|
|
"$INSTALL_PATH/venv/bin/pip" install -r "$INSTALL_PATH/requirements.txt"
|
|
if [ $? -ne 0 ]; then
|
|
echo " Error: Failed to install Python dependencies. Check requirements.txt."
|
|
exit 1
|
|
fi
|
|
|
|
# 5. Configure application files
|
|
echo " Configuring frontend API URL in $FRONTEND_FILE..."
|
|
sed -i "s|const API_BASE_URL = \".*\";|const API_BASE_URL = \"http://${TARGET_IP}:${BACKEND_PORT}\";|" "$INSTALL_PATH/$FRONTEND_FILE"
|
|
|
|
echo " Modifying $BACKEND_SCRIPT for network access..."
|
|
# This sed command inserts the new origin after the line containing "http://127.0.0.1:8080","
|
|
sed -i "/\"http:\/\/127.0.0.1:8080\",/a \ \ \ \ \"http://${TARGET_IP}:${BACKEND_PORT}\"," "$INSTALL_PATH/$BACKEND_SCRIPT"
|
|
|
|
# 6. Set final ownership for the application directory
|
|
echo " Setting final ownership for '$AGENT_USER'..."
|
|
chown -R "${AGENT_USER}:${AGENT_USER}" "$INSTALL_PATH"
|
|
|
|
# 7. Create systemd service file
|
|
echo " Creating systemd service file..."
|
|
SERVICE_FILE_PATH="/etc/systemd/system/${SERVICE_NAME}.service"
|
|
tee "$SERVICE_FILE_PATH" >/dev/null <<EOF_SERVICE
|
|
[Unit]
|
|
Description=Wilcon Client Dashboard Agent
|
|
After=network.target
|
|
|
|
[Service]
|
|
User=${AGENT_USER}
|
|
Group=${AGENT_USER}
|
|
WorkingDirectory=${INSTALL_PATH}
|
|
ExecStart=${INSTALL_PATH}/venv/bin/uvicorn wilcon:app --host 0.0.0.0 --port 8080
|
|
Restart=always
|
|
Environment="LOG_DIR=${LOG_DIR}"
|
|
RestartSec=5
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF_SERVICE
|
|
|
|
# 8. Reload systemd, enable and start the service
|
|
echo " Reloading systemd, enabling and starting service..."
|
|
systemctl daemon-reload
|
|
systemctl enable "$SERVICE_NAME.service"
|
|
systemctl restart "$SERVICE_NAME.service"
|
|
echo " Service status:"
|
|
systemctl status "$SERVICE_NAME.service" --no-pager
|
|
|
|
echo ""
|
|
echo "--- Installation Complete! ---"
|
|
echo "The Wilcon Client Dashboard is now running as a service."
|
|
echo "You can access the dashboard from any browser on your network at:"
|
|
echo " => http://$TARGET_IP:$BACKEND_PORT/$FRONTEND_FILE"
|
|
echo ""
|
|
echo "To manage the service, you can use these commands:"
|
|
echo " sudo systemctl status $SERVICE_NAME"
|
|
echo " sudo systemctl stop $SERVICE_NAME"
|
|
echo " sudo systemctl start $SERVICE_NAME"
|
|
echo " sudo journalctl -u $SERVICE_NAME -f (to view logs)"
|