Skip to content

Latest commit

 

History

History
130 lines (108 loc) · 6.92 KB

updating.md

File metadata and controls

130 lines (108 loc) · 6.92 KB

Self-Updating

The mfi-cli, mfi-mqtt-client, and mfi-rest-server tools can update themselves from this repository's GitHub Releases, replacing their own binary in place. This mirrors — and is compatible with — the shell-based updater in the mfi-env project.

How it works

  1. The tool resolves the latest release for its own name via the GitHub API: git/matching-refs/tags/<tool>/, then picks the highest semantic version (a true MAJOR.MINOR.PATCH comparison, so v1.10.0 beats v1.9.0).
  2. If that version is newer than the running one (from the tool's own --version), it reads the release's asset named exactly <tool> and its browser_download_url.
  3. It shells out to the device's wget (or curl) to download the asset — so TLS, proxying, and certificate handling are the firmware's, not ours. No TLS library is linked into the binaries.
  4. The download is staged in a uniquely named file beside the target and validated (regular file, non-empty, ELF magic). The applying thread atomically rename()s it over the running binary and the process re-execvs itself. Because the target lives in persistent storage (/var/etc/persistent/bin by default), the update survives reboots.
  • Long-running tools (mfi-mqtt-client, mfi-rest-server) check periodically from their main loop (default: once every 24 hours). The first check is deferred one full interval, since the boot downloader already fetched the latest at startup.
  • MQTT preparation runs on one background worker. Metadata checks, downloads, and validation do not stop hardware polling, MQTT servicing, or relay commands. Up-to-date, failed, and timed-out checks do not change MQTT availability. Only a prepared update initiates the main-thread offline/disconnect/apply sequence. The completed worker is joined before an update is offered for apply.
  • REST retains its synchronous periodic updater. The CLI's one-shot updater is synchronous too; neither consumer has been moved onto an MQTT-style worker.
  • mfi-cli is short-lived, so it exposes an explicit update subcommand instead of a periodic check.

Deadlines and termination

Preparation gets one 120-second total deadline, not a fresh timeout for each metadata request or download. A cancelled/timed-out downloader gets up to 5 additional seconds for termination escalation and child reaping. Pipe reads and child waits are nonblocking/cancellation-aware; cancellation does not wait out the remaining 120 seconds. Partial and discarded staged files are removed.

MQTT keeps servicing its normal loop during preparation. SIGTERM/SIGINT is recorded by a signal-safe handler; the owning loop requests worker cancellation and does not start more work or apply a late result. Cleanup/join is separate from the MQTT offline acknowledgement allowance: up to 5 seconds to obtain the offline PUBACK before disconnect, with acknowledgement failure/timeout reported rather than treated as delivered.

After preparation, only the MQTT-owning thread may apply, after its bounded offline shutdown. Cancellation before replacement leaves the target unchanged. If replacement happened but termination prevents restart (or exec fails), the outcome explicitly says replaced but not restarted. A returning apply failure may reconnect only when termination was not requested, using a new connection epoch and fresh hardware samples. A successfully restarted process likewise starts without inherited measurement caches.

The final rename/exec transition checks cancellation and pending SIGTERM/SIGINT with termination signals blocked on the applying thread, then commits with their default dispositions and unblocks them before exec. A racing signal terminates the old or new image instead of being lost when exec resets signal handlers. An update that has already crossed that final irreversible commit point cannot be revoked by an ordinary later cancellation request.

Configuration

All tools accept the same knobs; values resolve with the precedence CLI option > environment (http_proxy/https_proxy) > built-in default.

Setting Default mqtt-client / rest-server flag mfi-cli update flag
Enable on --update / --no-update (the subcommand itself)
Interval (s) 86400 --update-interval n/a (one-shot)
Repo owner/name shmuelie/mfi-custom-code --update-repo --repo
Proxy host:port (env or none) --update-proxy --proxy
Skip TLS verify on --update-insecure / --update-check-cert --no-check-cert
Binary directory /var/etc/persistent/bin (auto: /proc/self/exe) --bin-dir

TLS verification is skipped by default because the device's CA store is limited (the same reason mfi-env passes --no-check-certificate).

Examples

# Check for an update without applying it
mfi-cli update --check

# Update mfi-cli now, through a proxy
mfi-cli update --proxy proxy.local:8888

# Run the MQTT client with self-update disabled
mfi-mqtt-client --config /var/etc/persistent/mqtt.ini --no-update

# Update from a fork/mirror
mfi-rest-server --update-repo myfork/mfi-custom-code

Requirements & caveats

  • The device must have a wget (preferred) or curl that can reach api.github.com over HTTPS. If none is found, the tool logs once and keeps running — it never fails because of updates.
  • Downloader stderr is suppressed so failed requests cannot leak URLs/proxy credentials. Outcomes use short, credential-free descriptions. Captured metadata is capped at 1 MiB.
  • Child cleanup targets only the downloader's owned process group/direct child. As with any userspace timeout, uninterruptible kernel I/O or stalled filesystem operations cannot be given a hard real-time bound. Failure to reap after SIGKILL within the cleanup allowance is reported explicitly and disables further background updates; kernel intervention may still be needed.
  • ELF magic validation detects obvious bad downloads; it is not a signature, checksum, architecture, or compatibility verification.
  • Updates run unauthenticated against the GitHub API, which is rate-limited to 60 requests/hour per IP; the conservative default interval stays well under it.
  • The self-update replaces the running binary in persistent storage. If you deploy via the mfi-env boot downloader, both paths target the same /var/etc/persistent/bin/<tool>, so they stay consistent.

Relationship to mfi-env

mfi-env drives updates externally: a boot rc.poststart.d/downloader fetches the latest release at startup, and a host update.sh checks/updates over SSH. The in-binary self-update described here uses the same API endpoints, asset naming, and /var/etc/persistent/bin path, so it complements those scripts rather than conflicting with them — it just lets a device keep itself current between reboots without an external trigger.