installer / updates

This commit is contained in:
legop3
2026-11-16 23:08:30 -05:00
parent 91c9194856
commit 9626389dbe
16 changed files with 156 additions and 116 deletions
+28 -94
View File
@@ -1,98 +1,32 @@
#!/usr/bin/env bash
#
# Fixed-purpose self-update helper for Raspberry Pi rover hosts.
#
# roverd itself runs as the unprivileged "roverd" service user, but updating the
# installed agent requires root because the installer writes to /usr/local/bin,
# /etc, /opt, /var/lib/roverd, and systemd unit locations. This helper is the
# narrow privilege boundary: sudoers allows roverd to run this exact file with no
# arguments, and this file decides the complete update sequence internally.
# A separate systemd job survives replacement/restart of the roverd service.
set -euo pipefail
ENV_FILE="/etc/roverd-update.env"
LOCK_FILE="/var/lock/roverd-self-update.lock"
LOG_FILE="/var/log/roverd-self-update.log"
log() {
# Log to stdout so systemd/journal captures the message when launched by
# roverd, and also append to a stable file so an admin can inspect the last
# update attempt after the roverd service restarts.
local message
message="[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"
echo "$message"
echo "$message" >> "$LOG_FILE"
[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; }
if [[ ${ROVERD_UPDATE_JOB:-} != 1 ]]; then
exec systemd-run --unit=roverd-self-update --collect --wait \
--property=Type=exec --setenv=ROVERD_UPDATE_JOB=1 "$0"
fi
exec 9>/var/lock/roverd-self-update.lock
flock -n 9 || exit 1
install -d /var/lib/roverd
status() {
echo "$2"
python3 - "$1" "$2" <<'PY'
import json, os, sys, time
path = '/var/lib/roverd/update-status.json'
with open(path + '.tmp', 'w') as f:
json.dump(dict(state=sys.argv[1], message=sys.argv[2], timestamp=int(time.time()*1000)), f)
os.replace(path + '.tmp', path)
PY
}
if [[ "${EUID}" -ne 0 ]]; then
echo "roverd-self-update must run as root" >&2
exit 1
fi
if [[ "${ROVERD_SELF_UPDATE_SYSTEMD:-}" != "1" ]] && command -v systemd-run >/dev/null 2>&1; then
exec systemd-run \
--unit=roverd-self-update \
--collect \
--property=Type=exec \
--setenv=ROVERD_SELF_UPDATE_SYSTEMD=1 \
"$0"
fi
if [[ ! -f "$ENV_FILE" ]]; then
echo "Missing $ENV_FILE; run rover/install_roverd.sh once to register the repository path" >&2
exit 1
fi
# shellcheck disable=SC1090
source "$ENV_FILE"
if [[ -z "${ROVERD_REPO_DIR:-}" ]]; then
echo "ROVERD_REPO_DIR is not set in $ENV_FILE" >&2
exit 1
fi
if [[ ! -d "$ROVERD_REPO_DIR/.git" ]]; then
echo "ROVERD_REPO_DIR does not point at a git checkout: $ROVERD_REPO_DIR" >&2
exit 1
fi
if [[ ! -x "$ROVERD_REPO_DIR/rover/install_roverd.sh" ]]; then
echo "Installer is missing or not executable: $ROVERD_REPO_DIR/rover/install_roverd.sh" >&2
exit 1
fi
mkdir -p "$(dirname "$LOCK_FILE")" "$(dirname "$LOG_FILE")"
touch "$LOG_FILE"
chmod 0644 "$LOG_FILE"
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Another roverd self-update is already running; refusing to start a second one"
exit 1
fi
trap 'result=$?; if (( result != 0 )); then status failed "Update failed (exit $result). See journalctl -u roverd-self-update."; fi' EXIT
source /etc/roverd-update.env
cd "$ROVERD_REPO_DIR"
log "Starting roverd self-update in $ROVERD_REPO_DIR"
repo_uid="$(stat -c '%u' "$ROVERD_REPO_DIR")"
# Fetch through the normal git remote instead of downloading just one artifact.
# That keeps the binary, installer, scripts, systemd units, and any other rover
# files from this repository on the same commit.
if [[ "$repo_uid" -eq 0 ]]; then
git pull --ff-only
else
# The helper runs as root only for installation privileges. The git checkout
# usually belongs to the human deploy user, so running the pull as that owner
# avoids Git's dubious-ownership protection and preserves whatever SSH/HTTPS
# credentials that user normally uses for this repository.
sudo -H -u "#${repo_uid}" git -C "$ROVERD_REPO_DIR" pull --ff-only
fi
log "Repository fast-forward pull complete"
# The installer already owns the full desired rover host state. Reusing it here
# prevents this update helper from becoming a second, partial installer that can
# drift away from the normal manual install path.
"$ROVERD_REPO_DIR/rover/install_roverd.sh"
log "Installer completed successfully"
systemctl reboot
status running 'Updating repository'
# This is a dedicated root-owned checkout; local configuration lives outside it.
git pull --ff-only
status running 'Installing rover software'
bash rover/install_roverd.sh
status running 'Restarting roverd'
systemctl restart roverd
status succeeded 'Update installed and roverd service restarted'
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Download this file and run it with sudo; no existing checkout is needed.
set -euo pipefail
[[ $EUID == 0 ]] || { echo 'Run with sudo bash bootstrap.sh' >&2; exit 1; }
read -r -p 'Rover name: ' rover_name </dev/tty
read -r -p 'Server address (ws://host:port/rover or wss://host/rover): ' server_address </dev/tty
[[ -n "$rover_name" && -n "$server_address" ]] || { echo 'Name and address are required' >&2; exit 1; }
apt-get update
apt-get install -y git ca-certificates python3
if [[ ! -e /opt/roverd ]]; then
git clone https://github.com/legop3/MultiRoombaRover.git /opt/roverd
fi
[[ -d /opt/roverd/.git ]] || { echo '/opt/roverd must be a repository checkout' >&2; exit 1; }
export ROVERD_INITIAL_NAME="$rover_name" ROVERD_INITIAL_SERVER="$server_address"
bash /opt/roverd/rover/install_roverd.sh
systemctl restart roverd
+3 -1
View File
@@ -5,6 +5,8 @@ install_self_update_support() {
printf -v quoted_repo_root '%q' "$REPO_ROOT"
printf 'ROVERD_REPO_DIR=%s\n' "$quoted_repo_root" > /etc/roverd-update.env
chmod 0600 /etc/roverd-update.env
install -D -o root -g root -m 0755 rover/bin/roverd-self-update.sh /usr/local/sbin/roverd-self-update
install -D -o root -g root -m 0755 rover/bin/roverd-self-update.sh /usr/local/sbin/roverd-self-update.new
# Keep the running updater on its original inode until it exits.
mv -f /usr/local/sbin/roverd-self-update.new /usr/local/sbin/roverd-self-update
log "Registered update checkout at $REPO_ROOT"
}
+23 -6
View File
@@ -1,18 +1,35 @@
#!/usr/bin/env bash
# Installs from an existing checkout. Standalone bootstrap is a later stage.
# Shared installation path for bootstrap and self-update.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
cd "$REPO_ROOT"
[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; }
case "$(dpkg --print-architecture)" in
amd64) arch=amd64 ;; arm64) arch=arm64 ;; armhf) arch=armv6 ;;
amd64) arch=amd64 ;; arm64) arch=arm64 ;;
armhf) case "$(uname -m)" in armv6*) arch=armv6 ;; *) arch=armv7 ;; esac ;;
*) echo 'Unsupported OS architecture' >&2; exit 1 ;;
esac
install -m 0755 "dist/roverd-linux-$arch" /usr/local/bin/roverd
# Rename avoids overwriting an executable that the running service has mapped.
install -m 0755 "dist/roverd-linux-$arch" /usr/local/bin/roverd.new
mv -f /usr/local/bin/roverd.new /usr/local/bin/roverd
apt-get update
apt-get install -y git ca-certificates python3 util-linux
install -d /etc/roverd/profiles /var/lib/roverd
if [[ ! -f /etc/roverd.json ]]; then
/usr/local/bin/roverd -print-default-config > /etc/roverd.json
chmod 600 /etc/roverd.json
/usr/local/bin/roverd -print-default-config > /etc/roverd.json.new
chmod 600 /etc/roverd.json.new
python3 - <<'PYCONFIG'
import json, os
path = '/etc/roverd.json.new'
with open(path) as f:
config = json.load(f)
config['name'] = os.environ.get('ROVERD_INITIAL_NAME', config['name'])
config['serverUrl'] = os.environ.get('ROVERD_INITIAL_SERVER', config['serverUrl'])
with open(path, 'w') as f:
json.dump(config, f, indent=2)
PYCONFIG
/usr/local/bin/roverd -config /etc/roverd.json.new -check-config
mv /etc/roverd.json.new /etc/roverd.json
echo 'Set name, serverUrl, devices, and enabled profiles in /etc/roverd.json before starting roverd.'
fi
install -m 0644 rover/systemd/roverd.service /etc/systemd/system/roverd.service
@@ -26,4 +43,4 @@ source rover/install/self_update.sh
install_self_update_support
systemctl daemon-reload
systemctl enable roverd.service
echo 'Installed. Run systemctl restart roverd after configuring this rover.'
echo 'Installation complete; caller can now restart roverd.'
+35 -2
View File
@@ -1,11 +1,13 @@
package roverd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"sync"
"time"
@@ -81,7 +83,7 @@ func (m *ManagementClient) connect(parent context.Context) error {
conn.SetReadLimit(1024 * 1024)
if err := writeJSON(ctx, conn, map[string]any{
"type": "hello", "name": m.cfg.Name, "config": m.cfg,
"schema": ConfigurationSchema(), "profiles": m.profileCatalog(), "mediaStatus": m.mediaStatus(),
"schema": ConfigurationSchema(), "profiles": m.profileCatalog(), "mediaStatus": m.mediaStatus(), "updateStatus": readUpdateStatus(),
}); err != nil {
return err
}
@@ -102,6 +104,7 @@ func (m *ManagementClient) connect(parent context.Context) error {
}
}()
go m.forwardHostStats(ctx, conn)
go m.forwardUpdateStatus(ctx, conn)
select {
case <-ctx.Done():
return ctx.Err()
@@ -198,7 +201,7 @@ func (m *ManagementClient) execute(ctx context.Context, command managementComman
return m.restart, nil
}
// The updater remains a fixed-purpose helper; browser input cannot select
// executable paths or shell arguments. Its implementation is a later stage.
// executable paths or shell arguments. The separate systemd job survives restart.
path, args := "systemctl", []string{"reboot"}
if command.Type == "update" {
path, args = "/usr/local/sbin/roverd-self-update", nil
@@ -304,3 +307,33 @@ func (m *ManagementClient) mediaStatus() map[string]string {
}
return m.media.Status()
}
// Persist only the latest update result, so reconnects do not depend on an
// in-memory event surviving the service restart.
func readUpdateStatus() json.RawMessage {
data, err := os.ReadFile("/var/lib/roverd/update-status.json")
if err != nil || !json.Valid(data) {
return nil
}
return data
}
func (m *ManagementClient) forwardUpdateStatus(ctx context.Context, conn *websocket.Conn) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var previous json.RawMessage
for {
current := readUpdateStatus()
if current != nil && !bytes.Equal(current, previous) {
if err := writeJSON(ctx, conn, map[string]any{"type": "updateStatus", "status": current}); err != nil {
return
}
previous = current
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}