-
Notifications
You must be signed in to change notification settings - Fork 19
Add support for unattended updates #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
saba8814
wants to merge
4
commits into
kernelkit:main
Choose a base branch
from
saba8814:feat/unattended-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Shared helpers for check-update and unattended-update. Sourced, not run; | ||
| # the caller sets TAG first. | ||
|
|
||
| # Read the shared update-url from running-config, fall back to upstream. | ||
| update_read_url() { | ||
| url=$(copy running-config \ | ||
| -x '/ietf-system:system/infix-system:software/update-url' \ | ||
| 2>/dev/null \ | ||
| | jq -r '.. | objects | ."update-url"? // empty') | ||
| [ -n "$url" ] && printf '%s' "$url" || printf 'https://github.com/kernelkit/infix' | ||
| } | ||
|
|
||
| # Is $1 strictly newer than $2? | ||
| newer() { | ||
| [ "$1" = "$2" ] && return 1 | ||
| [ "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -1)" = "$1" ] | ||
| } | ||
|
|
||
| # Gather running and latest version info. | ||
| # Returns: 0 ok, 1 fatal (no os-release), 2 release API unreachable. | ||
| update_probe() { | ||
| if [ ! -f /etc/os-release ]; then | ||
| logger -t "$TAG" "ERROR: /etc/os-release not found" | ||
| return 1 | ||
| fi | ||
| . /etc/os-release | ||
|
|
||
| # Dev/dirty builds have no comparable semver -- always treat as upgradable. | ||
| IS_RELEASE=true | ||
| if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+'; then | ||
| IS_RELEASE=false | ||
| fi | ||
|
|
||
| UPDATE_URL=$(update_read_url) | ||
|
|
||
| # Derive the GitHub release API URL from the configured update URL. | ||
| # https://github.com/org/repo -> https://api.github.com/repos/org/repo | ||
| REPO=$(echo "$UPDATE_URL" | sed 's|https://github.com/||; s|/*$||') | ||
| API_URL="https://api.github.com/repos/${REPO}/releases/latest" | ||
|
|
||
| RELEASE_JSON=$(curl -sSL --max-time 10 "$API_URL" 2>/dev/null) | ||
| LATEST_TAG=$(printf '%s' "$RELEASE_JSON" | jq -r '.tag_name // empty') | ||
| if [ -z "$LATEST_TAG" ]; then | ||
| return 2 | ||
| fi | ||
|
saba8814 marked this conversation as resolved.
Outdated
|
||
| LATEST=${LATEST_TAG#v} | ||
| return 0 | ||
| } | ||
|
|
||
| # Should the latest release be applied over the running version? | ||
| # Requires update_probe() to have run. Returns 0 if an update is available. | ||
| update_available() { | ||
| [ "$IS_RELEASE" = false ] && return 0 | ||
| newer "$LATEST" "$VERSION" | ||
| } | ||
|
|
||
| # Print the download URL of this platform's release tarball, or nothing if it | ||
| # is missing. Releases ship no standalone bundle; the RAUC .pkg is packed in | ||
| # a per-platform tarball named "<IMAGE_ID>-<version>.tar.gz", e.g. | ||
| # "infix-x86_64-26.06.0.tar.gz". | ||
| update_tarball_url() { | ||
| name="${IMAGE_ID}-${LATEST}.tar.gz" | ||
| printf '%s' "$RELEASE_JSON" \ | ||
| | jq -r --arg n "$name" \ | ||
| '.assets[]? | select(.name == $n) | .browser_download_url' \ | ||
| | head -1 | ||
| } | ||
|
|
||
| # Print the path of the RAUC bundle inside that tarball. The archive unpacks | ||
| # to a "<IMAGE_ID>-<version>/" directory holding "<IMAGE_ID>-v<version>.pkg" | ||
| # -- note the 'v' on the bundle name but not on the directory. | ||
| update_tarball_member() { | ||
| printf '%s-%s/%s-v%s.pkg' "$IMAGE_ID" "$LATEST" "$IMAGE_ID" "$LATEST" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #!/bin/sh | ||
| # Download and install a newer release, unattended. Called by the scheduler. | ||
| # | ||
| # Installs to the inactive slot like a manual 'upgrade': RAUC flips the | ||
| # boot-order to activate on next reboot, leaving the old slot as fallback. | ||
| # The 'reboot' config policy decides whether that reboot is automatic. | ||
|
|
||
| TAG=unattended-update | ||
| # Scratch dir is used for the lock and the download bundle | ||
| SCRATCH=/var/lib/misc/unattended-update | ||
| LOCKFILE=$SCRATCH/lock | ||
|
|
||
| . /usr/libexec/infix/update-common | ||
|
|
||
| # Read the reboot policy (manual|immediate) from running-config; default manual. | ||
| read_reboot_policy() { | ||
| policy=$(copy running-config \ | ||
| -x '/ietf-system:system/infix-system:software/unattended-update/reboot' \ | ||
| 2>/dev/null \ | ||
| | jq -r '.. | objects | .reboot? // empty') | ||
| [ -n "$policy" ] && printf '%s' "$policy" || printf 'manual' | ||
| } | ||
|
|
||
| # Single-instance guard -- also avoids racing a manual 'upgrade' or an | ||
| # overlapping tick if a previous run is still installing. | ||
| exec 9>"$LOCKFILE" | ||
| if ! flock -n 9; then | ||
| logger -t "$TAG" "An update is already in progress, skipping" | ||
| exit 0 | ||
| fi | ||
|
saba8814 marked this conversation as resolved.
|
||
|
|
||
| update_probe | ||
| rc=$? | ||
| if [ $rc -eq 1 ]; then | ||
| exit 1 | ||
| fi | ||
| if [ $rc -eq 2 ]; then | ||
| logger -p daemon.info -t "$TAG" "Skipped: could not reach ${API_URL}" | ||
|
saba8814 marked this conversation as resolved.
Outdated
|
||
| exit 0 | ||
| fi | ||
|
|
||
| if ! update_available; then | ||
| logger -p daemon.debug -t "$TAG" "No update available (current: $VERSION, latest: $LATEST)" | ||
| exit 0 | ||
| fi | ||
|
|
||
| TARBALL_URL=$(update_tarball_url) | ||
| if [ -z "$TARBALL_URL" ]; then | ||
| logger -t "$TAG" "Update ${LATEST_TAG} found, but no tarball '${IMAGE_ID}-${LATEST}.tar.gz' in release; skipping" | ||
| exit 1 | ||
| fi | ||
| MEMBER=$(update_tarball_member) | ||
|
|
||
| # Stage the bundle next to the lock. Clean up on any exit. | ||
| PKG=$SCRATCH/bundle.pkg | ||
| trap 'rm -f "$PKG"' EXIT | ||
|
|
||
| # The release ships the .pkg inside a tarball, so stream it through tar and | ||
| # write out only the bundle member -- we never store the full archive. | ||
| logger -t "$TAG" "Downloading ${LATEST_TAG} bundle from ${TARBALL_URL}" | ||
| set -o pipefail | ||
|
saba8814 marked this conversation as resolved.
Outdated
|
||
| if ! curl -sSfL --max-time 1800 "$TARBALL_URL" | tar -xzOf - "$MEMBER" > "$PKG"; then | ||
| logger -t "$TAG" "ERROR: failed to download or extract '${MEMBER}' from ${TARBALL_URL}" | ||
| exit 1 | ||
| fi | ||
| if [ ! -s "$PKG" ]; then | ||
| logger -t "$TAG" "ERROR: extracted bundle is empty; '${MEMBER}' not found in tarball?" | ||
| exit 1 | ||
| fi | ||
|
|
||
| logger -t "$TAG" "Installing ${LATEST_TAG} (running ${VERSION})" | ||
| if ! rauc install "$PKG"; then | ||
| logger -t "$TAG" "ERROR: installation of ${LATEST_TAG} failed" | ||
| exit 1 | ||
| fi | ||
| rm -f "$PKG" | ||
|
|
||
| POLICY=$(read_reboot_policy) | ||
| if [ "$POLICY" = immediate ]; then | ||
| logger -t "$TAG" "Installed ${LATEST_TAG}; reboot policy 'immediate', rebooting to activate" | ||
| sleep 2 | ||
| /usr/sbin/reboot | ||
| else | ||
| logger -t "$TAG" "Installed ${LATEST_TAG}; reboot to activate the new image" | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.