feat: add upgrade to forgejo-runner-service.sh (#475)

for the purpose of indirectly upgrading lxc-helpers if a newer version is needed. Otherwise it has to be done manually and can easily be overlooked.

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/475
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
pull/478/head
Earl Warren 2025-01-30 19:37:17 +00:00 committed by earl-warren
parent 624b47bc2f
commit 5df67e66fe
No known key found for this signature in database
GPG Key ID: F128CBE6AB3A7201
3 changed files with 87 additions and 10 deletions

View File

@ -29,6 +29,27 @@ jobs:
VERBOSE=true ./forgejo-runner-service.sh dependencies VERBOSE=true ./forgejo-runner-service.sh dependencies
lxc-ls 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 - id: forgejo
uses: https://data.forgejo.org/actions/setup-forgejo@v2.0.7 uses: https://data.forgejo.org/actions/setup-forgejo@v2.0.7
with: with:

View File

@ -13,8 +13,23 @@ forgejo-runner-service.sh installs a [Forgejo runner](https://forgejo.org/docs/n
## Installation or upgrade ## 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` - `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 ## Description
- Each runner is assigned a unique serial number (`$INPUTS_SERIAL`) - Each runner is assigned a unique serial number (`$INPUTS_SERIAL`)

View File

@ -20,6 +20,7 @@ trap "rm -fr $TMPDIR" EXIT
: ${INPUTS_TOKEN:=} : ${INPUTS_TOKEN:=}
: ${INPUTS_FORGEJO:=https://code.forgejo.org} : ${INPUTS_FORGEJO:=https://code.forgejo.org}
: ${INPUTS_LIFETIME:=7d} : ${INPUTS_LIFETIME:=7d}
: ${INPUTS_LXC_HELPERS_VERSION:=1.0.3}
: ${INPUTS_RUNNER_VERSION:=6.2.1} : ${INPUTS_RUNNER_VERSION:=6.2.1}
: ${KILL_AFTER:=21600} # 6h == 21600 : ${KILL_AFTER:=21600} # 6h == 21600
@ -59,26 +60,50 @@ function config_inotify() {
$SUDO sysctl -p $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() { function dependencies() {
if ! which curl jq retry >/dev/null; then if ! which curl jq retry >/dev/null; then
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
$SUDO apt-get update -qq $SUDO apt-get update -qq
$SUDO apt-get install -y -qq curl jq retry $SUDO apt-get install -y -qq curl jq retry
fi 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 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 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 $SUDO chmod +x /usr/local/bin/yq
fi fi
if ! cmp $SELF /usr/local/bin/$SELF_FILENAME; then install_self
cp -a $SELF /usr/local/bin/$SELF_FILENAME 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 fi
} }
@ -325,6 +350,22 @@ function main() {
inside ensure_configuration_and_registration 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 # ensure an update of the current script does not break a long
# running function (such as `start`) by running from a copy instead # running function (such as `start`) by running from a copy instead