diff --git a/install.sh b/install.sh index a30a172..4772a76 100644 --- a/install.sh +++ b/install.sh @@ -6,34 +6,65 @@ set -e INSTALL_DIR="/opt/kao" SERVICE_FILE="/etc/systemd/system/kao.service" -CURRENT_USER=$(whoami) +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 with appropriate permissions -if [ ! -w "/opt" ]; then - echo "Error: Need write access to /opt" +# 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 -r aggregator.py kao.py index.html config.json requirements.txt detectors "$INSTALL_DIR/" -chown -R "$CURRENT_USER:$CURRENT_USER" "$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/" -# Create virtual environment +# 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..." -cd "$INSTALL_DIR" -python3 -m venv venv -./venv/bin/pip install --upgrade pip -q -./venv/bin/pip install -r requirements.txt -q +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..." @@ -45,6 +76,7 @@ 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 diff --git a/uninstall.sh b/uninstall.sh index 5af93aa..aa44b09 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -10,9 +10,9 @@ echo "====================================" echo " Kao Uninstaller" echo "====================================" -# Check permissions -if [ ! -w "/opt" ]; then - echo "Error: Need write access" +# Check if running as root +if [ "$EUID" -ne 0 ]; then + echo "Error: Must run as root" echo "Run with: sudo $0" exit 1 fi