Skip to content

Commit 42fe076

Browse files
authored
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
1 parent d2ca021 commit 42fe076

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

contrib/upgrade.sh

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,52 @@
33
# from dl.gitea.io on linux as systemd service. It performs a backup and updates
44
# Gitea in place.
55
# NOTE: This adds the GPG Signing Key of the Gitea maintainers to the keyring.
6-
# Depends on: bash, curl, xz, sha256sum, gpg. optionally jq.
7-
# Usage: [environment vars] upgrade.sh [version]
6+
# Depends on: bash, curl, xz, sha256sum. optionally jq, gpg
87
# See section below for available environment vars.
98
# When no version is specified, updates to the latest release.
109
# Examples:
1110
# upgrade.sh 1.15.10
1211
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
1312

14-
while true; do
15-
case "$1" in
16-
-v | --version ) ver="$2"; shift 2 ;;
17-
-y | --yes ) no_confirm="yes"; shift ;;
18-
--ignore-gpg) ignore_gpg="yes"; shift ;;
19-
-- ) shift; break ;;
20-
* ) break ;;
21-
esac
22-
done
23-
24-
set -euo pipefail
13+
# apply variables from environment
14+
: "${giteabin:="/usr/local/bin/gitea"}"
15+
: "${giteahome:="/var/lib/gitea"}"
16+
: "${giteaconf:="/etc/gitea/app.ini"}"
17+
: "${giteauser:="git"}"
18+
: "${sudocmd:="sudo"}"
19+
: "${arch:="linux-amd64"}"
20+
: "${service_start:="$sudocmd systemctl start gitea"}"
21+
: "${service_stop:="$sudocmd systemctl stop gitea"}"
22+
: "${service_status:="$sudocmd systemctl status gitea"}"
23+
: "${backupopts:=""}" # see `gitea dump --help` for available options
2524

25+
function giteacmd {
26+
if [[ $sudocmd = "su" ]]; then
27+
"$sudocmd" - "$giteauser" -c "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
28+
else
29+
"$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
30+
fi
31+
}
2632

2733
function require {
2834
for exe in "$@"; do
2935
command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1)
3036
done
3137
}
3238

39+
# parse command line arguments
40+
while true; do
41+
case "$1" in
42+
-v | --version ) giteaversion="$2"; shift 2 ;;
43+
-y | --yes ) no_confirm="yes"; shift ;;
44+
--ignore-gpg) ignore_gpg="yes"; shift ;;
45+
"" | -- ) shift; break ;;
46+
* ) echo "Usage: [<environment vars>] upgrade.sh [-v <version>] [-y] [--ignore-gpg]"; exit 1;;
47+
esac
48+
done
3349

34-
require curl xz sha256sum gpg
50+
# exit once any command fails. this means that each step should be idempotent!
51+
set -euo pipefail
3552

3653
if [[ -f /etc/os-release ]]; then
3754
os_release=$(cat /etc/os-release)
@@ -46,38 +63,17 @@ if [[ -f /etc/os-release ]]; then
4663
fi
4764
fi
4865

49-
50-
# apply variables from environment
51-
: "${giteabin:="/usr/local/bin/gitea"}"
52-
: "${giteahome:="/var/lib/gitea"}"
53-
: "${giteaconf:="/etc/gitea/app.ini"}"
54-
: "${giteauser:="git"}"
55-
: "${sudocmd:="sudo"}"
56-
: "${arch:="linux-amd64"}"
57-
: "${service_start:="$sudocmd systemctl start gitea"}"
58-
: "${service_stop:="$sudocmd systemctl stop gitea"}"
59-
: "${service_status:="$sudocmd systemctl status gitea"}"
60-
: "${backupopts:=""}" # see `gitea dump --help` for available options
61-
62-
63-
function giteacmd {
64-
if [[ $sudocmd = "su" ]]; then
65-
"$sudocmd" - "$giteauser" -c "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
66-
else
67-
"$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
68-
fi
69-
}
66+
require curl xz sha256sum "$sudocmd"
7067

7168
# select version to install
72-
if [[ -z "${ver:-}" ]]; then
69+
if [[ -z "${giteaversion:-}" ]]; then
7370
require jq
7471
giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version)
75-
else
76-
giteaversion="$ver"
72+
echo "Latest available version is $giteaversion"
7773
fi
7874

79-
8075
# confirm update
76+
echo "Checking currently installed version..."
8177
current=$(giteacmd --version | cut -d ' ' -f 3)
8278
[[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1
8379
if [[ -z "${no_confirm:-}" ]]; then
@@ -98,22 +94,24 @@ binurl="https://dl.gitea.io/gitea/${giteaversion}/${binname}.xz"
9894
echo "Downloading $binurl..."
9995
curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}"
10096

101-
# validate checksum & gpg signature (exit script if error)
97+
# validate checksum & gpg signature
10298
sha256sum -c "${binname}.xz.sha256"
10399
if [[ -z "${ignore_gpg:-}" ]]; then
100+
require gpg
104101
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
105102
gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; }
106103
fi
107104
rm "${binname}".xz.{sha256,asc}
108105

109106
# unpack binary + make executable
110-
xz --decompress "${binname}.xz"
107+
xz --decompress --force "${binname}.xz"
111108
chown "$giteauser" "$binname"
112109
chmod +x "$binname"
113110

114111
# stop gitea, create backup, replace binary, restart gitea
115-
echo "Stopping gitea at $(date)"
112+
echo "Flushing gitea queues at $(date)"
116113
giteacmd manager flush-queues
114+
echo "Stopping gitea at $(date)"
117115
$service_stop
118116
echo "Creating backup in $giteahome"
119117
giteacmd dump $backupopts

0 commit comments

Comments
 (0)