Skip to main content

02.04 – MeshMonitor Auto-Updates

MeshMonitor is under active development. Automated updates reduce maintenance burden while protecting local changes. I did this because I could never get the Proxmox LXC release to work on my stack - thats another story. Plus, I don't use Docker (yeah yeah). So native installation it is for me, and with the constant updates and improvements (fantastic product by the way, highly suggest you use it!) I needed a way to quickly get up to speed, and keep my install up-to-date.

Design Goals

  • Only update clean, fast-forwardable git trees
  • Stop service during rebuild
  • Restart automatically
  • Abort if local changes exist

Update Script

Path: /usr/local/sbin/updatemesh.sh

This script refuses to update if the repo has diverged. That’s intentional.

#!/usr/bin/env bash
set -euo pipefail

TARGET_DIR="/root/meshmonitor"
SERVICE="meshmonitor"

log() { echo "[$(date '+%Y-%m-%d %H:%M:%S %Z')] $*"; }

log "=========================================="
log "MeshMonitor Update Check"
log "=========================================="

if [[ ! -d "$TARGET_DIR/.git" ]]; then
  log "ERROR: $TARGET_DIR is missing or not a git repo."
  exit 1
fi

cd "$TARGET_DIR"
log "Directory: $TARGET_DIR"

# Make sure we have an upstream branch configured
UPSTREAM="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || true)"
if [[ -z "$UPSTREAM" ]]; then
  log "ERROR: No upstream configured. Run:"
  log "  git branch --set-upstream-to=origin/main main"
  exit 1
fi

log "Fetching remote updates..."
git fetch --quiet

LOCAL="$(git rev-parse HEAD)"
REMOTE="$(git rev-parse @{u})"
BASE="$(git merge-base HEAD @{u})"

if [[ "$LOCAL" == "$REMOTE" ]]; then
  log "You are up to date. (HEAD = ${LOCAL:0:7})"
  exit 0
fi

if [[ "$LOCAL" != "$BASE" ]]; then
  log "WARNING: Local branch has diverged from $UPSTREAM."
  log "Local:  ${LOCAL:0:7}"
  log "Remote: ${REMOTE:0:7}"
  log "Aborting (not auto-updating a diverged tree)."
  exit 2
fi

log "Update available! Local ${LOCAL:0:7} -> Remote ${REMOTE:0:7}"
log "Stopping service: $SERVICE"
systemctl stop "$SERVICE"

# If anything fails after stopping, try to bring it back up.
trap 'log "ERROR: Update failed. Attempting to start service..."; systemctl start "'"$SERVICE"'" || true' ERR

log "Pulling changes..."
git pull --ff-only

log "Installing NPM dependencies..."
npm install

log "Building frontend..."
npm run build

log "Building server..."
npm run build:server

log "Starting service: $SERVICE"
systemctl start "$SERVICE"

trap - ERR
log "Update completed successfully! Refresh the dashboard."

systemd Service

/etc/systemd/system/updatemesh.service

[Unit]
Description=MeshMonitor updater (check then update)
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/updatemesh.sh

systemd Timer

/etc/systemd/system/updatemesh.timer

[Unit]
Description=Run MeshMonitor updater daily

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=15m

[Install]
WantedBy=timers.target

Enable:

systemctl daemon-reload
systemctl enable --now updatemesh.timer