forked from gitea/gitea
1
0
Fork 0

Fix `contrib/upgrade.sh` (#19222)

* fix idempotency of script (eg when aborting the downloads)
* improve readability (user facing variables first, definitions next, statements last)
* improve dependency checks
* fix ignored $giteaversion variable
* more logging
* print usage string on incorrect usage
This commit is contained in:
Norwin 2022-03-27 19:48:08 +02:00 committed by GitHub
parent d2ca021df0
commit 42fe076084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 42 deletions

View File

@ -3,26 +3,32 @@
# from dl.gitea.io on linux as systemd service. It performs a backup and updates # from dl.gitea.io on linux as systemd service. It performs a backup and updates
# Gitea in place. # Gitea in place.
# NOTE: This adds the GPG Signing Key of the Gitea maintainers to the keyring. # NOTE: This adds the GPG Signing Key of the Gitea maintainers to the keyring.
# Depends on: bash, curl, xz, sha256sum, gpg. optionally jq. # Depends on: bash, curl, xz, sha256sum. optionally jq, gpg
# Usage: [environment vars] upgrade.sh [version]
# See section below for available environment vars. # See section below for available environment vars.
# When no version is specified, updates to the latest release. # When no version is specified, updates to the latest release.
# Examples: # Examples:
# upgrade.sh 1.15.10 # upgrade.sh 1.15.10
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh # giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
while true; do # apply variables from environment
case "$1" in : "${giteabin:="/usr/local/bin/gitea"}"
-v | --version ) ver="$2"; shift 2 ;; : "${giteahome:="/var/lib/gitea"}"
-y | --yes ) no_confirm="yes"; shift ;; : "${giteaconf:="/etc/gitea/app.ini"}"
--ignore-gpg) ignore_gpg="yes"; shift ;; : "${giteauser:="git"}"
-- ) shift; break ;; : "${sudocmd:="sudo"}"
* ) break ;; : "${arch:="linux-amd64"}"
esac : "${service_start:="$sudocmd systemctl start gitea"}"
done : "${service_stop:="$sudocmd systemctl stop gitea"}"
: "${service_status:="$sudocmd systemctl status gitea"}"
set -euo pipefail : "${backupopts:=""}" # see `gitea dump --help` for available options
function giteacmd {
if [[ $sudocmd = "su" ]]; then
"$sudocmd" - "$giteauser" -c "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
else
"$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
fi
}
function require { function require {
for exe in "$@"; do for exe in "$@"; do
@ -30,8 +36,19 @@ function require {
done done
} }
# parse command line arguments
while true; do
case "$1" in
-v | --version ) giteaversion="$2"; shift 2 ;;
-y | --yes ) no_confirm="yes"; shift ;;
--ignore-gpg) ignore_gpg="yes"; shift ;;
"" | -- ) shift; break ;;
* ) echo "Usage: [<environment vars>] upgrade.sh [-v <version>] [-y] [--ignore-gpg]"; exit 1;;
esac
done
require curl xz sha256sum gpg # exit once any command fails. this means that each step should be idempotent!
set -euo pipefail
if [[ -f /etc/os-release ]]; then if [[ -f /etc/os-release ]]; then
os_release=$(cat /etc/os-release) os_release=$(cat /etc/os-release)
@ -46,38 +63,17 @@ if [[ -f /etc/os-release ]]; then
fi fi
fi fi
require curl xz sha256sum "$sudocmd"
# apply variables from environment
: "${giteabin:="/usr/local/bin/gitea"}"
: "${giteahome:="/var/lib/gitea"}"
: "${giteaconf:="/etc/gitea/app.ini"}"
: "${giteauser:="git"}"
: "${sudocmd:="sudo"}"
: "${arch:="linux-amd64"}"
: "${service_start:="$sudocmd systemctl start gitea"}"
: "${service_stop:="$sudocmd systemctl stop gitea"}"
: "${service_status:="$sudocmd systemctl status gitea"}"
: "${backupopts:=""}" # see `gitea dump --help` for available options
function giteacmd {
if [[ $sudocmd = "su" ]]; then
"$sudocmd" - "$giteauser" -c "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
else
"$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
fi
}
# select version to install # select version to install
if [[ -z "${ver:-}" ]]; then if [[ -z "${giteaversion:-}" ]]; then
require jq require jq
giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version) giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version)
else echo "Latest available version is $giteaversion"
giteaversion="$ver"
fi fi
# confirm update # confirm update
echo "Checking currently installed version..."
current=$(giteacmd --version | cut -d ' ' -f 3) current=$(giteacmd --version | cut -d ' ' -f 3)
[[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1 [[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1
if [[ -z "${no_confirm:-}" ]]; then if [[ -z "${no_confirm:-}" ]]; then
@ -98,22 +94,24 @@ binurl="https://dl.gitea.io/gitea/${giteaversion}/${binname}.xz"
echo "Downloading $binurl..." echo "Downloading $binurl..."
curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}" curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}"
# validate checksum & gpg signature (exit script if error) # validate checksum & gpg signature
sha256sum -c "${binname}.xz.sha256" sha256sum -c "${binname}.xz.sha256"
if [[ -z "${ignore_gpg:-}" ]]; then if [[ -z "${ignore_gpg:-}" ]]; then
require gpg
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2 gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; } gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; }
fi fi
rm "${binname}".xz.{sha256,asc} rm "${binname}".xz.{sha256,asc}
# unpack binary + make executable # unpack binary + make executable
xz --decompress "${binname}.xz" xz --decompress --force "${binname}.xz"
chown "$giteauser" "$binname" chown "$giteauser" "$binname"
chmod +x "$binname" chmod +x "$binname"
# stop gitea, create backup, replace binary, restart gitea # stop gitea, create backup, replace binary, restart gitea
echo "Stopping gitea at $(date)" echo "Flushing gitea queues at $(date)"
giteacmd manager flush-queues giteacmd manager flush-queues
echo "Stopping gitea at $(date)"
$service_stop $service_stop
echo "Creating backup in $giteahome" echo "Creating backup in $giteahome"
giteacmd dump $backupopts giteacmd dump $backupopts