Add nightly auto-update via systemd timer

Add scripts/update.sh that fetches origin/main, resets if changed,
reinstalls dependencies when requirements.txt differs, and restarts
the service. Deploy script now installs a systemd timer that triggers
the update nightly at 03:00 with Persistent=true for catch-up on
missed runs.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Tim Hadwen
2026-01-26 16:29:35 +10:00
parent dc2fe2a8a0
commit b48a0ffe93
2 changed files with 72 additions and 4 deletions

View File

@@ -19,12 +19,12 @@ if [[ ! -f /proc/device-tree/model ]] || ! grep -q "Raspberry Pi" /proc/device-t
fi fi
# Step 1: Install system dependencies # Step 1: Install system dependencies
echo "[1/4] Installing system dependencies..." echo "[1/6] Installing system dependencies..."
sudo apt update sudo apt update
sudo apt install -y python3 python3-pip python3-venv libusb-1.0-0-dev sudo apt install -y python3 python3-pip python3-venv libusb-1.0-0-dev
# Step 2: Install uv if not present # Step 2: Install uv if not present
echo "[2/4] Setting up Python package manager..." echo "[2/6] Setting up Python package manager..."
if ! command -v uv &> /dev/null; then if ! command -v uv &> /dev/null; then
echo "Installing uv..." echo "Installing uv..."
curl -LsSf https://astral.sh/uv/install.sh | sh curl -LsSf https://astral.sh/uv/install.sh | sh
@@ -32,13 +32,13 @@ if ! command -v uv &> /dev/null; then
fi fi
# Step 3: Setup Python environment # Step 3: Setup Python environment
echo "[3/4] Installing Python dependencies..." echo "[3/6] Installing Python dependencies..."
cd "$PROJECT_DIR" cd "$PROJECT_DIR"
uv venv uv venv
uv pip install -r requirements.txt uv pip install -r requirements.txt
# Step 4: Setup udev rules # Step 4: Setup udev rules
echo "[4/4] Configuring USB permissions..." echo "[4/6] Configuring USB permissions..."
if [[ ! -f /etc/udev/rules.d/99-brother-ql.rules ]]; then if [[ ! -f /etc/udev/rules.d/99-brother-ql.rules ]]; then
sudo tee /etc/udev/rules.d/99-brother-ql.rules > /dev/null << 'EOF' sudo tee /etc/udev/rules.d/99-brother-ql.rules > /dev/null << 'EOF'
# Brother QL label printers # Brother QL label printers
@@ -73,6 +73,37 @@ EOF
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable brother-ql-bridge sudo systemctl enable brother-ql-bridge
# Step 6: Install nightly update timer
echo "[5/6] Installing nightly update timer..."
chmod +x "$PROJECT_DIR/scripts/update.sh"
sudo tee /etc/systemd/system/brother-ql-bridge-update.service > /dev/null << EOF
[Unit]
Description=Update Brother QL Bridge from GitHub
[Service]
Type=oneshot
User=$USER
WorkingDirectory=$PROJECT_DIR
ExecStart=$PROJECT_DIR/scripts/update.sh
EOF
sudo tee /etc/systemd/system/brother-ql-bridge-update.timer > /dev/null << 'EOF'
[Unit]
Description=Nightly update for Brother QL Bridge
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
EOF
echo "[6/6] Enabling update timer..."
sudo systemctl daemon-reload
sudo systemctl enable --now brother-ql-bridge-update.timer
echo "" echo ""
echo "=== Deployment Complete ===" echo "=== Deployment Complete ==="
echo "" echo ""

37
scripts/update.sh Normal file
View File

@@ -0,0 +1,37 @@
#!/bin/bash
# Brother QL Bridge - Nightly Update Script
# Pulls latest code from GitHub and restarts the service if anything changed.
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_DIR"
echo "Fetching latest changes from origin/main..."
git fetch origin main
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/main)
if [ "$LOCAL" = "$REMOTE" ]; then
echo "Already up to date."
exit 0
fi
echo "Updating from $LOCAL to $REMOTE..."
# Check if requirements.txt will change
REQUIREMENTS_CHANGED=$(git diff HEAD origin/main --name-only -- requirements.txt)
git reset --hard origin/main
if [ -n "$REQUIREMENTS_CHANGED" ]; then
echo "requirements.txt changed, reinstalling dependencies..."
uv pip install -r requirements.txt
fi
echo "Restarting brother-ql-bridge service..."
sudo systemctl restart brother-ql-bridge
echo "Update complete."