|
| 1 | +#!/bin/sh |
| 2 | +# Check for available software updates and notify on login if one exists. |
| 3 | +# Called by the scheduler. |
| 4 | + |
| 5 | +NOTIFY_FILE=/run/os-update |
| 6 | +TAG=os-update |
| 7 | + |
| 8 | +# Source os-release for VERSION and IMAGE_ID |
| 9 | +if [ ! -f /etc/os-release ]; then |
| 10 | + logger -t "$TAG" "ERROR: /etc/os-release not found" |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | +. /etc/os-release |
| 14 | + |
| 15 | +# Dev/dirty builds have no comparable semver — always show the latest release |
| 16 | +IS_RELEASE=true |
| 17 | +if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+'; then |
| 18 | + IS_RELEASE=false |
| 19 | +fi |
| 20 | + |
| 21 | +# Read configured update-url from running config, fall back to upstream |
| 22 | +UPDATE_URL=$(copy running-config \ |
| 23 | + -x '/ietf-system:system/infix-system:software/check-update/update-url' \ |
| 24 | + 2>/dev/null \ |
| 25 | + | jq -r '.. | objects | ."update-url"? // empty') |
| 26 | +UPDATE_URL=${UPDATE_URL:-"https://github.com/kernelkit/infix"} |
| 27 | + |
| 28 | +# Derive API URL from the configured update URL. |
| 29 | +# Default (github.com): https://github.com/org/repo → https://api.github.com/repos/org/repo |
| 30 | +REPO=$(echo "$UPDATE_URL" | sed 's|https://github.com/||; s|/*$||') |
| 31 | +API_URL="https://api.github.com/repos/${REPO}/releases/latest" |
| 32 | + |
| 33 | +LATEST_TAG=$(curl -sSL --max-time 10 "$API_URL" 2>/dev/null \ |
| 34 | + | jq -r '.tag_name // empty') |
| 35 | +if [ -z "$LATEST_TAG" ]; then |
| 36 | + logger -p daemon.info -t "$TAG" "Update check skipped: could not reach ${API_URL}" |
| 37 | + exit 0 |
| 38 | +fi |
| 39 | +LATEST=${LATEST_TAG#v} |
| 40 | + |
| 41 | +# Compare: is $1 strictly newer than $2? |
| 42 | +newer() { |
| 43 | + [ "$1" = "$2" ] && return 1 |
| 44 | + [ "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -1)" = "$1" ] |
| 45 | +} |
| 46 | + |
| 47 | +if [ "$IS_RELEASE" = false ] || newer "$LATEST" "$VERSION"; then |
| 48 | + RELEASE_URL="${UPDATE_URL}/releases/${LATEST_TAG}" |
| 49 | + MSG="Software update available: ${LATEST_TAG}, running ${VERSION} (see ${RELEASE_URL})" |
| 50 | + logger -t "$TAG" "$MSG" |
| 51 | + printf '%s\n' "$MSG" > "$NOTIFY_FILE" |
| 52 | +else |
| 53 | + logger -p daemon.debug -t "$TAG" "No update available (current: $VERSION, latest: $LATEST)" |
| 54 | + printf '' > "$NOTIFY_FILE" |
| 55 | +fi |
0 commit comments