Files
Kao/install.sh
Spencer Grimes 81c23308cc Fix Linux installer ownership and path issues
- Use SCRIPT_DIR to find source files from any directory
- Create venv as target user (sudo -u) for correct ownership
- Add python3 existence check
- Add source file existence check
- Add group to systemd service
- Fix uninstall.sh root check

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 23:34:08 -06:00

112 lines
3.0 KiB
Bash

#!/bin/bash
# Kao Installation Script
# Installs to /opt/kao and sets up systemd service
set -e
INSTALL_DIR="/opt/kao"
SERVICE_FILE="/etc/systemd/system/kao.service"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Use SUDO_USER if running via sudo, otherwise current user
CURRENT_USER="${SUDO_USER:-$(whoami)}"
CURRENT_GROUP="$(id -gn "$CURRENT_USER")"
echo "===================================="
echo " Kao Installer"
echo "===================================="
echo "Install directory: $INSTALL_DIR"
echo "Running as user: $CURRENT_USER"
echo "Source: $SCRIPT_DIR"
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Error: Must run as root"
echo "Run with: sudo $0"
exit 1
fi
if [ -z "$CURRENT_USER" ] || [ "$CURRENT_USER" = "root" ]; then
echo "Error: Could not determine target user"
echo "Run with: sudo $0"
exit 1
fi
# Check for python3
if ! command -v python3 &> /dev/null; then
echo "Error: python3 not found"
echo "Install with: sudo apt install python3 python3-venv"
exit 1
fi
# Check source files exist
if [ ! -f "$SCRIPT_DIR/kao.py" ]; then
echo "Error: Source files not found in $SCRIPT_DIR"
echo "Run this script from the Kao repository directory"
exit 1
fi
# Create install directory
echo "[1/5] Creating install directory..."
mkdir -p "$INSTALL_DIR"
cp "$SCRIPT_DIR/aggregator.py" "$INSTALL_DIR/"
cp "$SCRIPT_DIR/kao.py" "$INSTALL_DIR/"
cp "$SCRIPT_DIR/index.html" "$INSTALL_DIR/"
cp "$SCRIPT_DIR/config.json" "$INSTALL_DIR/"
cp "$SCRIPT_DIR/requirements.txt" "$INSTALL_DIR/"
cp -r "$SCRIPT_DIR/detectors" "$INSTALL_DIR/"
# Set ownership before venv creation
chown -R "$CURRENT_USER:$CURRENT_GROUP" "$INSTALL_DIR"
# Create virtual environment as target user
echo "[2/5] Creating virtual environment..."
sudo -u "$CURRENT_USER" python3 -m venv "$INSTALL_DIR/venv"
sudo -u "$CURRENT_USER" "$INSTALL_DIR/venv/bin/pip" install --upgrade pip -q
sudo -u "$CURRENT_USER" "$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt" -q
# Create systemd service
echo "[3/5] Creating systemd service..."
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Kao - System Status Monitor
After=network.target
[Service]
Type=simple
User=$CURRENT_USER
Group=$CURRENT_GROUP
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/venv/bin/python $INSTALL_DIR/kao.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and enable service
echo "[4/5] Enabling service..."
systemctl daemon-reload
systemctl enable kao.service
# Start service
echo "[5/5] Starting Kao..."
systemctl start kao.service
echo ""
echo "===================================="
echo " Installation complete!"
echo "===================================="
echo ""
echo "Kao is now running at http://$(hostname -I | awk '{print $1}'):5100"
echo ""
echo "Commands:"
echo " sudo systemctl status kao # Check status"
echo " sudo systemctl restart kao # Restart"
echo " sudo systemctl stop kao # Stop"
echo " sudo journalctl -u kao -f # View logs"
echo ""
echo "Config file: $INSTALL_DIR/config.json"