unos-repository

APK repository for unos
git clone git://git.finwo.net/misc/unos-repository
Log | Files | Refs | README

check-revision.sh (4134B)


      1 #!/bin/sh
      2 # mk/check-revision.sh - refuse to build if version-rrev already published.
      3 #
      4 #   ./mk/check-revision.sh <pkg> [arch] [--repo <repo-url>]
      5 #
      6 # Checks whether <pkg>-<version>-r<revision>.apk already exists in the
      7 # published repo. If it does, the template must bump revision before it can
      8 # be built/published. This enforces the "revision bump required" policy.
      9 #
     10 # Repo resolution: --repo flag, else local build/repo/<arch>, else
     11 # https://repo.unos.finwo.net/<arch> (if reachable).
     12 #
     13 # Used by mk/build.sh (gate) and by the conductor (pre-dispatch check).
     14 set -eu
     15 
     16 HERE=$(cd "$(dirname "$0")" && pwd)
     17 ROOT=$(cd "${HERE}/.." && pwd)
     18 
     19 # shellcheck disable=SC1091
     20 . "${HERE}/channels.inc"
     21 
     22 PKG=""
     23 ARCH="x86_64"
     24 REPO=""
     25 PKGVER=""
     26 CHANNEL="${UNOS_CHANNEL:-lts}"
     27 
     28 while [ $# -gt 0 ]; do
     29   case "$1" in
     30     --arch) ARCH="$2"; shift 2 ;;
     31     --arch=*) ARCH="${1#--arch=}"; shift ;;
     32     --repo) REPO="$2"; shift 2 ;;
     33     --repo=*) REPO="${1#--repo=}"; shift ;;
     34     --channel) CHANNEL="$2"; shift 2 ;;
     35     --channel=*) CHANNEL="${1#--channel=}"; shift ;;
     36     --pkgver) PKGVER="$2"; shift 2 ;;
     37     --pkgver=*) PKGVER="${1#--pkgver=}"; shift ;;
     38     -h|--help) sed -n '1,25p' "$0"; exit 0 ;;
     39     -*) echo "check-revision.sh: unknown option: $1" >&2; exit 1 ;;
     40     *) if [ -z "${PKG}" ]; then PKG="$1"; else
     41          case "$1" in x86_64|aarch64|arm64) ARCH="$1";; *) REPO="$1";; esac
     42        fi; shift ;;
     43   esac
     44 done
     45 
     46 [ -n "${PKG}" ] || { echo "usage: check-revision.sh <pkg> [arch] [--repo <url>]" >&2; exit 1; }
     47 
     48 # Already published: nothing to build. The marker tells the conductor to
     49 # report the task as skipped rather than as a pass.
     50 published() {
     51   echo "check-revision.sh: $1" >&2
     52   echo "  bump ${PKG}'s revision in channel ${CHANNEL} to rebuild" >&2
     53   echo "::set-state skipped"
     54   exit 1
     55 }
     56 
     57 ARCH=$(chan_arch "${ARCH}")
     58 case "${ARCH}" in x86_64|aarch64) ;; *) echo "check-revision.sh: unsupported ARCH ${ARCH}" >&2; exit 1 ;; esac
     59 
     60 if [ -z "${PKGVER}" ]; then
     61   TEMPLATE="$(chan_pkgdir "${CHANNEL}")/${PKG}/template"
     62   [ -f "${TEMPLATE}" ] || { echo "check-revision.sh: no template: ${TEMPLATE}" >&2; exit 1; }
     63 
     64   # Read version/revision from template (same subshell pattern as deps.sh)
     65   VERSION=; REVISION=0
     66   eval "$( (
     67     version=; revision=0
     68     SYSROOT=; DESTDIR=; WRKSRC=; FILESDIR=; WORK=; SRCDEST=
     69     UNOS_MKDIR="${HERE}"
     70     die() { :; } ; msg() { :; } ; vinstall() { :; } ; vmkdir() { :; }
     71     . "${TEMPLATE}" >/dev/null 2>&1 || true
     72     printf 'VERSION=%s\nREVISION=%s\n' "${version}" "${revision}"
     73   ) )"
     74 
     75   [ -n "${VERSION}" ] || { echo "check-revision.sh: cannot read version from ${TEMPLATE}" >&2; exit 2; }
     76   PKGVER="${VERSION}-r${REVISION}"
     77 fi
     78 
     79 APK_NAME="${PKG}-${PKGVER}.apk"
     80 
     81 LOCALREPO=$(chan_repodir "${CHANNEL}" "${ARCH}")
     82 
     83 # Check local repo first
     84 if [ -z "${REPO}" ]; then
     85   if [ -f "${LOCALREPO}/${APK_NAME}" ]; then
     86     published "${APK_NAME} already exists in ${LOCALREPO}/"
     87   fi
     88   REPO="${LOCALREPO}"
     89 fi
     90 
     91 # --repo takes the bucket base; the channel and arch segments are this
     92 # script's to append, same as every other consumer of chan_url.
     93 case "${REPO}" in
     94   http://*|https://*)
     95     url=$(chan_url "${REPO}" "${CHANNEL}" "${ARCH}" "${APK_NAME}")
     96 
     97     if ! command -v curl >/dev/null 2>&1 && command -v apt-get >/dev/null 2>&1; then
     98       apt-get update -qq >/dev/null 2>&1 || true
     99       apt-get install -y -qq --no-install-recommends curl ca-certificates >/dev/null 2>&1 || true
    100     fi
    101     command -v curl >/dev/null 2>&1 || {
    102       echo "check-revision.sh: curl unavailable; cannot determine if ${APK_NAME} is published" >&2
    103       exit 2
    104     }
    105 
    106     code=$(curl -sSL -o /dev/null -w '%{http_code}' --head "${url}" 2>/dev/null) || code=000
    107     case "${code}" in
    108       2*)
    109         published "${APK_NAME} already published at ${url}"
    110         ;;
    111       404) ;;
    112       *)
    113         echo "check-revision.sh: cannot determine if ${APK_NAME} is published (HTTP ${code} for ${url})" >&2
    114         exit 2
    115         ;;
    116     esac
    117     ;;
    118   *)
    119     if [ -f "${REPO}/${APK_NAME}" ]; then
    120       published "${APK_NAME} already exists in ${REPO}/"
    121     fi
    122     ;;
    123 esac
    124 
    125 echo "check-revision.sh: OK ${APK_NAME} not yet published"