Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ npm-debug.log
/dist/
.DS_Store
/.playwright-mcp/
/REVISION
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The dashboard opens at the same `http://localhost:4100` address. If a screenshot
./bin/install-launch-agent
```

The installer copies the app to `~/Library/Application Support/LocalWebMonitorex`, prepares production assets there, installs a user LaunchAgent, and starts it. This lets the background process run outside protected project folders such as Documents. It starts again at login. Re-run the installer after pulling project updates. To restart it after saving a new port or watched range in **Settings**:
The installer builds the current commit into `~/Library/Application Support/LocalWebMonitorex-versions/<commit>`, points `~/Library/Application Support/LocalWebMonitorex` at it, installs a user LaunchAgent, and starts it. Running outside the project folder lets the background process work even when the checkout is in a protected folder such as Documents. It starts again at login. To restart it after saving a new port or watched range in **Settings**:

```bash
launchctl kickstart -k gui/$(id -u)/com.localwebmonitorex
Expand All @@ -104,7 +104,27 @@ The log is at `~/Library/Logs/LocalWebMonitorex.log`. To stop and remove automat
./bin/uninstall-launch-agent
```

Removing the LaunchAgent keeps the installed copy and your saved preferences.
Removing the LaunchAgents keeps the installed copies and your saved preferences.

#### Automatic updates

The installer also adds `com.localwebmonitorex.updater`, which runs at login and then about once an hour. It never changes your source checkout. Each run:

1. Fetches `main` from the checkout's `origin` (read over HTTPS) into a private repository in `~/Library/Application Support/LocalWebMonitorex-updater`.
2. Compares it with the installed commit. If they match, it records that the copy is up to date.
3. Exports the new commit into its own version directory and runs `bin/build-installation` there.
4. Starts that build on a spare loopback port with throwaway settings and an empty watched range, and waits for it to serve its own commit.
5. Points the installation at the new version and restarts `com.localwebmonitorex`. If the dashboard does not serve the new commit on your saved port within three minutes, it switches back and restarts the previous version.

A failed fetch, build, smoke test, or restart keeps the current version running and is retried on the next run. The running version and the one before it are kept; older version directories are removed. If the installed commit is not on `main`, for example after installing from an unmerged branch, updates pause until you install from `main` again. A change to the LaunchAgent files themselves applies when you re-run the installer.

**Settings → Updates** shows the installed commit, the last check, and whether the copy is up to date, behind `main` by some commits, or why the last update failed. Details are in `~/Library/Logs/LocalWebMonitorex-updater.log`. To check now:

```bash
launchctl kickstart gui/$(id -u)/com.localwebmonitorex.updater
```

Installations made before the updater existed keep their files in place: re-run `./bin/install-launch-agent` from an up-to-date `main` checkout once. The installer keeps the old copy as the previous version and turns on the hourly updater.

## Configuration

Expand Down
11 changes: 11 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ a { color:inherit; text-decoration:none; }
.settings-range-fields > span { color:#87948a; padding-bottom:11px; }
.settings-range-save { width:100%; height:41px; margin-top:13px; border:0; border-radius:5px; background:#20382f; color:#f4f8ea; font-size:12px; font-weight:700; }
.settings-range-save:hover { background:#335943; }
.settings-updates { border-top:1px solid #d9e1d7; margin-top:38px; padding-top:29px; }
.settings-updates h3 { margin:0 0 8px; font-size:18px; letter-spacing:-.045em; }
.settings-updates-summary { margin:0; color:#355c41; font-size:12px; font-weight:700; line-height:1.5; }
.settings-updates-summary.failed { color:#9b3b2c; }
.settings-updates dl { display:grid; grid-template-columns:auto minmax(0,1fr); gap:6px 14px; margin:14px 0 0; font-size:12px; }
.settings-updates dt { color:#6d7b70; }
.settings-updates dd { margin:0; min-width:0; overflow-wrap:anywhere; }
.settings-error,.settings-success { margin:11px 0 0; font-size:12px; line-height:1.5; }
.settings-error { color:#a54334; }
.settings-success { color:#357344; }
Expand All @@ -252,6 +259,10 @@ html[data-theme="dark"] .app-notice.error { background:#432924; border-color:#8b
html[data-theme="dark"] .brand-mark span { box-shadow:0 0 0 4px #0b1812; }
html[data-theme="dark"] .eyebrow,html[data-theme="dark"] .section-index { color:#a2c5a4; }
html[data-theme="dark"] .page-head p,html[data-theme="dark"] .settings-panel > p,html[data-theme="dark"] .settings-theme p,html[data-theme="dark"] .settings-range > p { color:#adbfaf; }
html[data-theme="dark"] .settings-updates { border-color:#3d5443; }
html[data-theme="dark"] .settings-updates dt { color:#adbfaf; }
html[data-theme="dark"] .settings-updates-summary { color:#b7ddb1; }
html[data-theme="dark"] .settings-updates-summary.failed { color:#ffb4a5; }
html[data-theme="dark"] .scan-status { color:#b7ddb1; }
html[data-theme="dark"] .pulse-dot { box-shadow:0 0 0 4px #24402a; }
html[data-theme="dark"] .refresh-button,html[data-theme="dark"] .settings-shortcut { background:#1c2c22; border-color:#476050; color:#dcebdc; }
Expand Down
12 changes: 12 additions & 0 deletions bin/build-installation
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Prepares this project copy to run with `MIX_ENV=prod mix phx.server`.
# The installer and the updater both run it inside the copy they are building.
set -euo pipefail

cd "$(dirname "${BASH_SOURCE[0]}")/.."

mix deps.get
npm ci --prefix assets
./assets/node_modules/.bin/playwright install webkit
MIX_ENV=prod mix assets.deploy
MIX_ENV=prod mix compile
139 changes: 107 additions & 32 deletions bin/install-launch-agent
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,52 @@ if [[ "$(uname -s)" != "Darwin" ]]; then
fi

project_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
install_dir="$HOME/Library/Application Support/LocalWebMonitorex"
support_dir="$HOME/Library/Application Support"
install_link="$support_dir/LocalWebMonitorex"
versions_dir="$support_dir/LocalWebMonitorex-versions"
update_dir="$support_dir/LocalWebMonitorex-updater"
agent_label="com.localwebmonitorex"
updater_label="com.localwebmonitorex.updater"
agent_file="$HOME/Library/LaunchAgents/$agent_label.plist"
updater_file="$HOME/Library/LaunchAgents/$updater_label.plist"
log_file="$HOME/Library/Logs/LocalWebMonitorex.log"
updater_log="$HOME/Library/Logs/LocalWebMonitorex-updater.log"
default_remote="https://github.com/sallaumen/LocalWebMonitorex.git"

mkdir -p "$install_dir"
if [[ "$project_dir" != "$install_dir" ]]; then
rsync -a --delete --exclude '/.git/' --exclude '/_build/' "$project_dir/" "$install_dir/"
case "$project_dir/" in
"$install_link/"* | "$versions_dir/"*)
echo "Run this installer from your source checkout, not from the installed copy." >&2
exit 1
;;
esac

if ! revision="$(git -C "$project_dir" rev-parse --verify HEAD 2>/dev/null)"; then
echo "Run this installer from a git checkout so the installed commit is known." >&2
exit 1
fi

# The updater fetches anonymously, so an SSH GitHub remote is read over HTTPS.
remote="$(git -C "$project_dir" remote get-url origin 2>/dev/null || echo "$default_remote")"
case "$remote" in
git@github.com:*) remote="https://github.com/${remote#git@github.com:}" ;;
ssh://git@github.com/*) remote="https://github.com/${remote#ssh://git@github.com/}" ;;
esac

version_name="$revision"
if [[ -n "$(git -C "$project_dir" status --porcelain 2>/dev/null)" ]]; then
version_name="$revision-local"
fi
version_dir="$versions_dir/$version_name"

cd "$install_dir"
mix deps.get
npm ci --prefix assets
./assets/node_modules/.bin/playwright install webkit
MIX_ENV=prod mix assets.deploy
mkdir -p "$version_dir" "$update_dir" "$HOME/Library/LaunchAgents" "$HOME/Library/Logs"
rsync -a --delete --exclude '/.git' --exclude '/_build/' --exclude '/REVISION' \
"$project_dir/" "$version_dir/"
printf '%s\n' "$revision" > "$version_dir/REVISION"
"$version_dir/bin/build-installation"

mkdir -p "$HOME/Library/LaunchAgents" "$HOME/Library/Logs"
PROJECT_DIR="$install_dir" AGENT_FILE="$agent_file" LOG_FILE="$log_file" python3 - <<'PY'
PROJECT_DIR="$install_link" UPDATE_DIR="$update_dir" VERSIONS_DIR="$versions_dir" \
REMOTE="$remote" AGENT_FILE="$agent_file" UPDATER_FILE="$updater_file" \
LOG_FILE="$log_file" UPDATER_LOG="$updater_log" python3 - <<'PY'
import os
import plistlib
import shutil
Expand All @@ -35,40 +63,87 @@ node = shutil.which("node")
runtime_dirs = [os.path.dirname(tool) for tool in (mix, asdf, node) if tool]
runtime_dirs += ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"]
runtime_path = os.pathsep.join(dict.fromkeys(runtime_dirs))
status_file = os.path.join(os.environ["UPDATE_DIR"], "status")

config = {
service = {
"Label": "com.localwebmonitorex",
"ProgramArguments": [mix, "phx.server"],
"WorkingDirectory": os.environ["PROJECT_DIR"],
"EnvironmentVariables": {"MIX_ENV": "prod", "PHX_SERVER": "true", "PATH": runtime_path},
"EnvironmentVariables": {
"MIX_ENV": "prod",
"PHX_SERVER": "true",
"PATH": runtime_path,
"LOCALWEBMONITOREX_UPDATE_STATUS": status_file,
},
"RunAtLoad": True,
"KeepAlive": True,
"StandardOutPath": os.environ["LOG_FILE"],
"StandardErrorPath": os.environ["LOG_FILE"],
}

with open(os.environ["AGENT_FILE"], "wb") as output:
plistlib.dump(config, output)
updater = {
"Label": "com.localwebmonitorex.updater",
"ProgramArguments": ["/bin/bash", os.path.join(os.environ["UPDATE_DIR"], "update-from-main")],
"EnvironmentVariables": {
"PATH": runtime_path,
"LOCALWEBMONITOREX_INSTALL_DIR": os.environ["PROJECT_DIR"],
"LOCALWEBMONITOREX_VERSIONS_DIR": os.environ["VERSIONS_DIR"],
"LOCALWEBMONITOREX_UPDATE_DIR": os.environ["UPDATE_DIR"],
"LOCALWEBMONITOREX_UPDATE_REMOTE": os.environ["REMOTE"],
},
"StartInterval": 3600,
"RunAtLoad": True,
"Nice": 10,
"StandardOutPath": os.environ["UPDATER_LOG"],
"StandardErrorPath": os.environ["UPDATER_LOG"],
}

for path, config in ((os.environ["AGENT_FILE"], service), (os.environ["UPDATER_FILE"], updater)):
with open(path, "wb") as output:
plistlib.dump(config, output)
PY

agent_target="gui/$(id -u)/$agent_label"
launchctl bootout "$agent_target" >/dev/null 2>&1 || true
bootstrap_error=""
registered=false
for attempt in 1 2 3 4 5; do
if bootstrap_error="$(launchctl bootstrap "gui/$(id -u)" "$agent_file" 2>&1)"; then
registered=true
break
fi
sleep 1
domain="gui/$(id -u)"
launchctl bootout "$domain/$updater_label" >/dev/null 2>&1 || true
launchctl bootout "$domain/$agent_label" >/dev/null 2>&1 || true

# Earlier installers kept a real directory here; keep it as the previous version.
previous_version=""
if [[ -L "$install_link" ]]; then
previous_version="$(readlink "$install_link")"
elif [[ -d "$install_link" ]]; then
previous_version="$versions_dir/legacy-$(date +%s)"
mv "$install_link" "$previous_version"
fi
ln -sfn "$version_dir" "$install_link"

for directory in "$versions_dir"/*; do
[[ -d "$directory" && ! -L "$directory" ]] || continue
[[ "$directory" == "$version_dir" || "$directory" == "$previous_version" ]] && continue
rm -rf "$directory"
done
if [[ "$registered" != true ]]; then

install -m 755 "$version_dir/bin/update-from-main" "$update_dir/update-from-main"

bootstrap_agent() {
local file=$1 bootstrap_error=""
for _ in 1 2 3 4 5; do
if bootstrap_error="$(launchctl bootstrap "$domain" "$file" 2>&1)"; then
return 0
fi
sleep 1
done
echo "$bootstrap_error" >&2
exit 1
fi
launchctl enable "$agent_target"
launchctl kickstart -k "$agent_target"
return 1
}

bootstrap_agent "$agent_file"
launchctl enable "$domain/$agent_label"
launchctl kickstart -k "$domain/$agent_label"
bootstrap_agent "$updater_file"
launchctl enable "$domain/$updater_label"

echo "LocalWebMonitorex starts at login. Open http://localhost:4100 (or your saved port)."
echo "Installed at: $install_dir"
echo "Log: $log_file"
echo "Installed commit: $revision ($version_dir)"
echo "It checks $remote main about once an hour and updates itself."
echo "Logs: $log_file and $updater_log"
14 changes: 7 additions & 7 deletions bin/uninstall-launch-agent
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

agent_label="com.localwebmonitorex"
agent_target="gui/$(id -u)/$agent_label"
agent_file="$HOME/Library/LaunchAgents/$agent_label.plist"

launchctl bootout "$agent_target" >/dev/null 2>&1 || true
rm -f "$agent_file"
echo "LocalWebMonitorex was removed from login startup. Your port preference was kept."
domain="gui/$(id -u)"
for agent_label in com.localwebmonitorex.updater com.localwebmonitorex; do
launchctl bootout "$domain/$agent_label" >/dev/null 2>&1 || true
rm -f "$HOME/Library/LaunchAgents/$agent_label.plist"
done
echo "LocalWebMonitorex and its updater were removed from login startup."
echo "Your port preference and installed copies under ~/Library/Application Support were kept."
Loading
Loading