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>
This commit is contained in:
80
install.sh
80
install.sh
@@ -1,6 +1,7 @@
|
||||
#!/bin/bash
|
||||
# Kao Installation Script
|
||||
# Installs to /opt/kao and sets up systemd service
|
||||
# Clones from git to /opt/kao and sets up systemd service
|
||||
# Updates automatically via git pull on service restart
|
||||
|
||||
set -e
|
||||
|
||||
@@ -17,7 +18,6 @@ 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
|
||||
@@ -40,34 +40,58 @@ if ! command -v python3 &> /dev/null; then
|
||||
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"
|
||||
# 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
|
||||
|
||||
# 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/"
|
||||
# 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
|
||||
|
||||
# Set ownership before venv creation
|
||||
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 "[2/5] Creating virtual environment..."
|
||||
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
|
||||
echo "[3/5] Creating systemd service..."
|
||||
# Create systemd service with git pull on start
|
||||
echo "[5/6] Creating systemd service..."
|
||||
cat > "$SERVICE_FILE" << EOF
|
||||
[Unit]
|
||||
Description=Kao - System Status Monitor
|
||||
@@ -78,6 +102,7 @@ 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
|
||||
@@ -87,12 +112,9 @@ WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# Reload systemd and enable service
|
||||
echo "[4/5] Enabling service..."
|
||||
echo "[6/6] Enabling and starting service..."
|
||||
systemctl daemon-reload
|
||||
systemctl enable kao.service
|
||||
|
||||
# Start service
|
||||
echo "[5/5] Starting Kao..."
|
||||
systemctl start kao.service
|
||||
|
||||
echo ""
|
||||
@@ -103,9 +125,11 @@ 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 " 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 ""
|
||||
echo "Config file: $INSTALL_DIR/config.json"
|
||||
|
||||
Reference in New Issue
Block a user