Skip to content

Commit ddf7f13

Browse files
mokeyish6543Gustedzeripathtechknowlogick
authored
Fix script compatiable with OpenWrt (#19000)
* Fix compatibility of upgrade.sh in openwrt and add service init script for openwrt. * Apply suggestions from code review Co-authored-by: Gusted <[email protected]> Co-authored-by: zeripath <[email protected]> * Update contrib/upgrade.sh Co-authored-by: Norwin <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: Gusted <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Norwin <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent eceab9e commit ddf7f13

File tree

2 files changed

+103
-24
lines changed

2 files changed

+103
-24
lines changed

contrib/init/openwrt/gitea

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh /etc/rc.common
2+
3+
USE_PROCD=1
4+
5+
# PROCD_DEBUG=1
6+
7+
START=90
8+
STOP=10
9+
10+
PROG=/opt/gitea/gitea
11+
GITEA_WORK_DIR=/opt/gitea
12+
CONF_FILE=$GITEA_WORK_DIR/app.ini
13+
14+
start_service(){
15+
procd_open_instance gitea
16+
procd_set_param env GITEA_WORK_DIR=$GITEA_WORK_DIR
17+
procd_set_param env HOME=$GITEA_WORK_DIR
18+
procd_set_param command $PROG web -c $CONF_FILE
19+
procd_set_param file $CONF_FILE
20+
procd_set_param user git
21+
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} # respawn automatically if something died, be careful if you have an alternative process supervisor
22+
procd_close_instance
23+
}
24+
25+
start(){
26+
service_start $PROG
27+
}
28+
29+
stop(){
30+
service_stop $PROG
31+
}
32+
33+
reload(){
34+
service_reload $PROG
35+
}

contrib/upgrade.sh

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env bash
2-
set -euo pipefail
3-
42
# This is an update script for gitea installed via the binary distribution
53
# from dl.gitea.io on linux as systemd service. It performs a backup and updates
64
# Gitea in place.
@@ -13,41 +11,83 @@ set -euo pipefail
1311
# upgrade.sh 1.15.10
1412
# giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh
1513

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
25+
26+
27+
function require {
28+
for exe in "$@"; do
29+
command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1)
30+
done
31+
}
32+
33+
34+
require curl xz sha256sum gpg
35+
36+
if [[ -f /etc/os-release ]]; then
37+
os_release=$(cat /etc/os-release)
38+
39+
if [[ "$os_release" =~ "OpenWrt" ]]; then
40+
sudocmd="su"
41+
service_start="/etc/init.d/gitea start"
42+
service_stop="/etc/init.d/gitea stop"
43+
service_status="/etc/init.d/gitea status"
44+
else
45+
require systemctl
46+
fi
47+
fi
48+
49+
1650
# apply variables from environment
1751
: "${giteabin:="/usr/local/bin/gitea"}"
1852
: "${giteahome:="/var/lib/gitea"}"
1953
: "${giteaconf:="/etc/gitea/app.ini"}"
2054
: "${giteauser:="git"}"
2155
: "${sudocmd:="sudo"}"
2256
: "${arch:="linux-amd64"}"
57+
: "${service_start:="$sudocmd systemctl start gitea"}"
58+
: "${service_stop:="$sudocmd systemctl stop gitea"}"
59+
: "${service_status:="$sudocmd systemctl status gitea"}"
2360
: "${backupopts:=""}" # see `gitea dump --help` for available options
2461

25-
function giteacmd {
26-
"$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@"
27-
}
2862

29-
function require {
30-
for exe in "$@"; do
31-
command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1)
32-
done
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
3369
}
34-
require systemctl curl xz sha256sum gpg "$sudocmd"
3570

3671
# select version to install
37-
if [[ -z "${1:-}" ]]; then
72+
if [[ -z "${ver:-}" ]]; then
3873
require jq
3974
giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version)
4075
else
41-
giteaversion="$1"
76+
giteaversion="$ver"
4277
fi
4378

79+
4480
# confirm update
45-
current=$(giteacmd --version | cut --delimiter=' ' --fields=3)
81+
current=$(giteacmd --version | cut -d ' ' -f 3)
4682
[[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1
47-
echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md"
48-
echo "Are you ready to update Gitea from ${current} to ${giteaversion}? (y/N)"
49-
read -r confirm
50-
[[ "$confirm" == "y" ]] || [[ "$confirm" == "Y" ]] || exit 1
83+
if [[ -z "${no_confirm:-}" ]]; then
84+
echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md"
85+
echo "Are you ready to update Gitea from ${current} to ${giteaversion}? (y/N)"
86+
read -r confirm
87+
[[ "$confirm" == "y" ]] || [[ "$confirm" == "Y" ]] || exit 1
88+
fi
89+
90+
echo "Upgrading gitea from $current to $giteaversion ..."
5191

5292
pushd "$(pwd)" &>/dev/null
5393
cd "$giteahome" # needed for gitea dump later
@@ -59,9 +99,11 @@ echo "Downloading $binurl..."
5999
curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}"
60100

61101
# validate checksum & gpg signature (exit script if error)
62-
sha256sum --check "${binname}.xz.sha256"
63-
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
64-
gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; }
102+
sha256sum -c "${binname}.xz.sha256"
103+
if [[ -z "${ignore_gpg:-}" ]]; then
104+
gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2
105+
gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; }
106+
fi
65107
rm "${binname}".xz.{sha256,asc}
66108

67109
# unpack binary + make executable
@@ -72,12 +114,14 @@ chmod +x "$binname"
72114
# stop gitea, create backup, replace binary, restart gitea
73115
echo "Stopping gitea at $(date)"
74116
giteacmd manager flush-queues
75-
$sudocmd systemctl stop gitea
117+
$service_stop
76118
echo "Creating backup in $giteahome"
77119
giteacmd dump $backupopts
78120
echo "Updating binary at $giteabin"
79-
mv --force --backup "$binname" "$giteabin"
80-
$sudocmd systemctl start gitea
81-
$sudocmd systemctl status gitea
121+
cp -f "$giteabin" "$giteabin.bak" && mv -f "$binname" "$giteabin"
122+
$service_start
123+
$service_status
124+
125+
echo "Upgrade to $giteaversion successful!"
82126

83127
popd

0 commit comments

Comments
 (0)