From ddf7f1319f70e124acfd1e8e86dff4ff87290f51 Mon Sep 17 00:00:00 2001 From: YISH Date: Wed, 9 Mar 2022 06:58:14 +0800 Subject: [PATCH] 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 Co-authored-by: zeripath * Update contrib/upgrade.sh Co-authored-by: Norwin Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Gusted Co-authored-by: zeripath Co-authored-by: techknowlogick Co-authored-by: Norwin Co-authored-by: techknowlogick --- contrib/init/openwrt/gitea | 35 +++++++++++++++ contrib/upgrade.sh | 92 ++++++++++++++++++++++++++++---------- 2 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 contrib/init/openwrt/gitea diff --git a/contrib/init/openwrt/gitea b/contrib/init/openwrt/gitea new file mode 100644 index 0000000000..67b4495ff8 --- /dev/null +++ b/contrib/init/openwrt/gitea @@ -0,0 +1,35 @@ +#!/bin/sh /etc/rc.common + +USE_PROCD=1 + +# PROCD_DEBUG=1 + +START=90 +STOP=10 + +PROG=/opt/gitea/gitea +GITEA_WORK_DIR=/opt/gitea +CONF_FILE=$GITEA_WORK_DIR/app.ini + +start_service(){ + procd_open_instance gitea + procd_set_param env GITEA_WORK_DIR=$GITEA_WORK_DIR + procd_set_param env HOME=$GITEA_WORK_DIR + procd_set_param command $PROG web -c $CONF_FILE + procd_set_param file $CONF_FILE + procd_set_param user git + 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 + procd_close_instance +} + +start(){ + service_start $PROG +} + +stop(){ + service_stop $PROG +} + +reload(){ + service_reload $PROG +} diff --git a/contrib/upgrade.sh b/contrib/upgrade.sh index 2f4f32bfab..171d9617c1 100755 --- a/contrib/upgrade.sh +++ b/contrib/upgrade.sh @@ -1,6 +1,4 @@ #!/usr/bin/env bash -set -euo pipefail - # This is an update script for gitea installed via the binary distribution # from dl.gitea.io on linux as systemd service. It performs a backup and updates # Gitea in place. @@ -13,6 +11,42 @@ set -euo pipefail # upgrade.sh 1.15.10 # giteahome=/opt/gitea giteaconf=$giteahome/app.ini upgrade.sh +while true; do + case "$1" in + -v | --version ) ver="$2"; shift 2 ;; + -y | --yes ) no_confirm="yes"; shift ;; + --ignore-gpg) ignore_gpg="yes"; shift ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +set -euo pipefail + + +function require { + for exe in "$@"; do + command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1) + done +} + + +require curl xz sha256sum gpg + +if [[ -f /etc/os-release ]]; then + os_release=$(cat /etc/os-release) + + if [[ "$os_release" =~ "OpenWrt" ]]; then + sudocmd="su" + service_start="/etc/init.d/gitea start" + service_stop="/etc/init.d/gitea stop" + service_status="/etc/init.d/gitea status" + else + require systemctl + fi +fi + + # apply variables from environment : "${giteabin:="/usr/local/bin/gitea"}" : "${giteahome:="/var/lib/gitea"}" @@ -20,34 +54,40 @@ set -euo pipefail : "${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 { - "$sudocmd" --user "$giteauser" "$giteabin" --config "$giteaconf" --work-path "$giteahome" "$@" -} -function require { - for exe in "$@"; do - command -v "$exe" &>/dev/null || (echo "missing dependency '$exe'"; exit 1) - done +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 } -require systemctl curl xz sha256sum gpg "$sudocmd" # select version to install -if [[ -z "${1:-}" ]]; then +if [[ -z "${ver:-}" ]]; then require jq giteaversion=$(curl --connect-timeout 10 -sL https://dl.gitea.io/gitea/version.json | jq -r .latest.version) else - giteaversion="$1" + giteaversion="$ver" fi + # confirm update -current=$(giteacmd --version | cut --delimiter=' ' --fields=3) +current=$(giteacmd --version | cut -d ' ' -f 3) [[ "$current" == "$giteaversion" ]] && echo "$current is already installed, stopping." && exit 1 -echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md" -echo "Are you ready to update Gitea from ${current} to ${giteaversion}? (y/N)" -read -r confirm -[[ "$confirm" == "y" ]] || [[ "$confirm" == "Y" ]] || exit 1 +if [[ -z "${no_confirm:-}" ]]; then + echo "Make sure to read the changelog first: https://github.com/go-gitea/gitea/blob/main/CHANGELOG.md" + echo "Are you ready to update Gitea from ${current} to ${giteaversion}? (y/N)" + read -r confirm + [[ "$confirm" == "y" ]] || [[ "$confirm" == "Y" ]] || exit 1 +fi + +echo "Upgrading gitea from $current to $giteaversion ..." pushd "$(pwd)" &>/dev/null cd "$giteahome" # needed for gitea dump later @@ -59,9 +99,11 @@ echo "Downloading $binurl..." curl --connect-timeout 10 --silent --show-error --fail --location -O "$binurl{,.sha256,.asc}" # validate checksum & gpg signature (exit script if error) -sha256sum --check "${binname}.xz.sha256" -gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2 -gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; } +sha256sum -c "${binname}.xz.sha256" +if [[ -z "${ignore_gpg:-}" ]]; then + gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2 + gpg --verify "${binname}.xz.asc" "${binname}.xz" || { echo 'Signature does not match'; exit 1; } +fi rm "${binname}".xz.{sha256,asc} # unpack binary + make executable @@ -72,12 +114,14 @@ chmod +x "$binname" # stop gitea, create backup, replace binary, restart gitea echo "Stopping gitea at $(date)" giteacmd manager flush-queues -$sudocmd systemctl stop gitea +$service_stop echo "Creating backup in $giteahome" giteacmd dump $backupopts echo "Updating binary at $giteabin" -mv --force --backup "$binname" "$giteabin" -$sudocmd systemctl start gitea -$sudocmd systemctl status gitea +cp -f "$giteabin" "$giteabin.bak" && mv -f "$binname" "$giteabin" +$service_start +$service_status + +echo "Upgrade to $giteaversion successful!" popd