Files
Kao/install.sh
Spencer Grimes 36aecb1fc9 Add git-based auto-update for Linux deployment
- install.sh now clones from git instead of copying files
- Service runs git pull on restart for automatic updates
- Support config.local.json for production settings (gitignored)
- kao.py prefers config.local.json when present

To update production: push changes, then 'sudo systemctl restart kao'

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

136 lines
3.9 KiB
Bash

#!/bin/bash
# Kao Installation Script
# Clones from git to /opt/kao and sets up systemd service
# Updates automatically via git pull on service restart
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 ""
# 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 for git
if ! command -v git &> /dev/null; then
echo "Error: git not found"
echo "Install with: sudo apt install git"
exit 1
fi
# Get git remote URL from source repo
if [ -d "$SCRIPT_DIR/.git" ]; then
REPO_URL=$(git -C "$SCRIPT_DIR" remote get-url origin 2>/dev/null || echo "")
fi
if [ -z "$REPO_URL" ]; then
echo "Error: Could not detect git remote URL"
echo "Run this script from within the Kao git repository"
exit 1
fi
echo "Git remote: $REPO_URL"
echo ""
# Remove existing installation if present
if [ -d "$INSTALL_DIR" ]; then
echo "[1/6] Removing existing installation..."
systemctl stop kao.service 2>/dev/null || true
rm -rf "$INSTALL_DIR"
else
echo "[1/6] No existing installation found"
fi
# Clone repository (as root since /opt requires elevated permissions)
echo "[2/6] Cloning repository..."
git clone "$REPO_URL" "$INSTALL_DIR"
# Set ownership to target user (so they can git pull, create venv, etc.)
chown -R "$CURRENT_USER:$CURRENT_GROUP" "$INSTALL_DIR"
# Create local config for production customization
echo "[3/6] Creating local config..."
if [ ! -f "$INSTALL_DIR/config.local.json" ]; then
cp "$INSTALL_DIR/config.json" "$INSTALL_DIR/config.local.json"
echo "Created config.local.json - edit this for production settings"
fi
# Create virtual environment as target user
echo "[4/6] 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 with git pull on start
echo "[5/6] 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
ExecStartPre=/usr/bin/git -C $INSTALL_DIR pull --ff-only
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 "[6/6] Enabling and starting service..."
systemctl daemon-reload
systemctl enable kao.service
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 (auto-pulls latest code)"
echo " sudo systemctl stop kao # Stop"
echo " sudo journalctl -u kao -f # View logs"
echo ""
echo "Config: $INSTALL_DIR/config.local.json"
echo "To update: push changes to git, then 'sudo systemctl restart kao'"
echo ""