diff --git a/.forgejo/workflows/example-lxc-systemd.yml b/.forgejo/workflows/example-lxc-systemd.yml index 6b8ce89..e2feebd 100644 --- a/.forgejo/workflows/example-lxc-systemd.yml +++ b/.forgejo/workflows/example-lxc-systemd.yml @@ -29,6 +29,27 @@ jobs: VERBOSE=true ./forgejo-runner-service.sh dependencies lxc-ls + - name: forgejo-runner-service.sh upgrade + run: | + set -x + + bin=/usr/local/bin + scripts="lxc-helpers.sh lxc-helpers-lib.sh forgejo-runner-service.sh" + + # make the existing scripts different, as if originating from a previous release + rm -f $bin/*.backup + for script in $scripts; do + echo '# something' >> $bin/$script + done + + cd examples/lxc-systemd + VERBOSE=true ./forgejo-runner-service.sh upgrade 1.2.3 $(pwd)/forgejo-runner-service.sh + + for script in $scripts; do + ! grep --quiet something $bin/$script + grep --quiet something $bin/$script.backup + done + - id: forgejo uses: https://data.forgejo.org/actions/setup-forgejo@v2.0.7 with: diff --git a/examples/lxc-systemd/README.md b/examples/lxc-systemd/README.md index b039ee8..4c68ffd 100644 --- a/examples/lxc-systemd/README.md +++ b/examples/lxc-systemd/README.md @@ -13,8 +13,23 @@ forgejo-runner-service.sh installs a [Forgejo runner](https://forgejo.org/docs/n ## Installation or upgrade +### Installation + - `sudo wget -O /usr/local/bin/forgejo-runner-service.sh https://code.forgejo.org/forgejo/runner/raw/branch/main/examples/lxc-systemd/forgejo-runner-service.sh && sudo chmod +x /usr/local/bin/forgejo-runner-service.sh` +### Upgrade + +> **Warning** runners will not be upgraded immediately, the upgrade will happen when they restart (at `$INPUTS_LIFETIME` intervals). + +The following will be upgraded: + +- `forgejo-runner-service.sh` will replace itself with the version found at `https://code.forgejo.org/forgejo/runner/src/tag/vX.Y.Z/examples/lxc-systemd/forgejo-runner-service.sh` +- `lxc-helpers*.sh` will be replaced with the version pinned in `forgejo-runner-service.sh` + +Upgrade to the version X.Y.Z (e.g 6.2.1): + +- `forgejo-runner-service.sh upgrade X.Y.Z` + ## Description - Each runner is assigned a unique serial number (`$INPUTS_SERIAL`) diff --git a/examples/lxc-systemd/forgejo-runner-service.sh b/examples/lxc-systemd/forgejo-runner-service.sh index b80d7e6..e50e38a 100755 --- a/examples/lxc-systemd/forgejo-runner-service.sh +++ b/examples/lxc-systemd/forgejo-runner-service.sh @@ -20,6 +20,7 @@ trap "rm -fr $TMPDIR" EXIT : ${INPUTS_TOKEN:=} : ${INPUTS_FORGEJO:=https://code.forgejo.org} : ${INPUTS_LIFETIME:=7d} +: ${INPUTS_LXC_HELPERS_VERSION:=1.0.3} : ${INPUTS_RUNNER_VERSION:=6.2.1} : ${KILL_AFTER:=21600} # 6h == 21600 @@ -59,26 +60,50 @@ function config_inotify() { $SUDO sysctl -p } +function install_or_update_lxc_helpers() { + for lxc_helper in lxc-helpers.sh lxc-helpers-lib.sh; do + local new=$TMPDIR/$lxc_helper + local existing=/usr/local/bin/$lxc_helper + curl --fail -sS -o $new https://code.forgejo.org/forgejo/lxc-helpers/raw/tag/v${INPUTS_LXC_HELPERS_VERSION}/$lxc_helper + if ! test -f $existing || ! cmp --quiet $existing $new; then + if test -f $existing; then + $SUDO mv $existing $existing.backup + fi + $SUDO mv $new $existing + $SUDO chmod +x $existing + fi + done +} + +function install_or_update_self() { + local bin=/usr/local/bin/$SELF_FILENAME + + if ! cmp --quiet $SELF $bin; then + if test -f $bin; then + $SUDO mv $bin $bin.backup + fi + $SUDO cp -a $SELF $bin + fi +} + +function install_self() { + install_or_update_self +} + function dependencies() { if ! which curl jq retry >/dev/null; then export DEBIAN_FRONTEND=noninteractive $SUDO apt-get update -qq $SUDO apt-get install -y -qq curl jq retry fi - if ! which lxc-helpers.sh >/dev/null; then - $SUDO curl --fail -sS -o /usr/local/bin/lxc-helpers-lib.sh https://code.forgejo.org/forgejo/lxc-helpers/raw/branch/main/lxc-helpers-lib.sh - $SUDO curl --fail -sS -o /usr/local/bin/lxc-helpers.sh https://code.forgejo.org/forgejo/lxc-helpers/raw/branch/main/lxc-helpers.sh - $SUDO chmod +x /usr/local/bin/lxc-helpers*.sh - fi - if ! which lxc-ls >/dev/null; then - $SUDO lxc-helpers.sh lxc_install_lxc_inside $LXC_IPV4_PREFIX $LXC_IPV6_PREFIX - fi if ! which yq >/dev/null; then $SUDO curl -L --fail -sS -o /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_arm64 $SUDO chmod +x /usr/local/bin/yq fi - if ! cmp $SELF /usr/local/bin/$SELF_FILENAME; then - cp -a $SELF /usr/local/bin/$SELF_FILENAME + install_self + install_or_update_lxc_helpers + if ! which lxc-ls >/dev/null; then + $SUDO lxc-helpers.sh lxc_install_lxc_inside $LXC_IPV4_PREFIX $LXC_IPV6_PREFIX fi } @@ -325,6 +350,22 @@ function main() { inside ensure_configuration_and_registration } +function upgrade() { + run_in_copy upgrade_safely "$@" +} + +function upgrade_safely() { + local version="${1:-$INPUTS_RUNNER_VERSION}" + local upgrade="${2:-$TMPDIR/$SELF_FILENAME}" + + if ! test -f $upgrade; then + curl --fail -sS -o $upgrade https://code.forgejo.org/forgejo/runner/raw/tag/v$version/examples/lxc-systemd/forgejo-runner-service.sh + fi + chmod +x $upgrade + $upgrade install_or_update_lxc_helpers + $upgrade install_or_update_self +} + # # ensure an update of the current script does not break a long # running function (such as `start`) by running from a copy instead