commit eab1b231323b2a9479aea12ec61118e7fb47e959 Author: snow flurry Date: Wed Oct 27 17:28:24 2021 -0700 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..3bb3c60 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# gitea-update + +Automatic Gitea update script for Linux, using the Github releases API. + +## Requirements + +* POSIX /bin/sh +* jq +* basename + +## How to use + +``` +$ ./gitea-update +``` diff --git a/gitea-update b/gitea-update new file mode 100755 index 0000000..9db62b3 --- /dev/null +++ b/gitea-update @@ -0,0 +1,108 @@ +#!/bin/sh + +RELEASE_API="https://api.github.com/repos/go-gitea/gitea/releases/latest" +DEFAULT_ARCH="linux-amd64" +GITEA_PATH="/usr/local/bin/gitea" +GITEA_VAR="/var/lib/gitea" + +info() { + echo "[INFO] $1" +} + +debug() { + if [ -n "$GU_DEBUG" ] ; then + echo "[DEBUG] $1" + fi +} + +die() { + echo "[FATAL] $1" >&2 + if [ -n "${tmpdir}" ] && [ -d "${tmpdir}" ] ; then + rm -r "${tmpdir}" + fi + exit 1 +} + +prompt() { + while true ; do + printf "%s [y/n] " "$1" + read -r yesno + case $yesno in + [Yy]) + return 0 # true + ;; + [Nn]) + return 1 # false + ;; + *) + echo "Please respond with \`y' or \`n'." + ;; + esac + done +} + +download() { + fname="$(basename "$1")" + info "Getting $fname..." + curl -sLo "${tmpdir}/$fname" "$1" || die "Unable to download $fname" +} + +parse_binlink() { + jq -r ".assets[] | select(.name | endswith(\"$DEFAULT_ARCH\")) | .browser_download_url" < "$1" +} + +# gather data... +tmpdir="${TMP:-/tmp}/gitea-update" +backupdir="/opt/gitea.bak" +release_file="$tmpdir/reldata" + +mkdir -p "$tmpdir" + +curl -s "$RELEASE_API" > "$tmpdir/reldata" || die "Unable to get Gitea release information" +binary_link="$(parse_binlink "$release_file")" || die "Unable to get Gitea binary URL" +debug "link: $binary_link" +hash_link="${binary_link}.sha256" +# gpg_link="${binary_link}.asc" +new_ver="$(jq -r ".name" < "$release_file")" +old_ver="$(/usr/local/bin/gitea --version | awk 'match($0, /version ([0-9.]+) /,ver) { print ver[1] }')" + +if [ "v$old_ver" = "$new_ver" ] ; then + echo "No update available." + exit +fi + +echo "Gitea update available! New version is $new_ver, we're currently on v$old_ver." +prompt "Update to $new_ver?" || exit + +## Gather the files! +download "$binary_link" +download "$hash_link" +# TODO: gpg_link + +info "Confirming hash matches binary file..." +( + cd "$tmpdir" && + sha256sum -c "$(basename "$hash_link")" +) || die "SHA-256 checksum of gitea executable didn't match!" + +info "Stopping Gitea..." + +info "Backing everything up..." +sudo mkdir -p "$backupdir/bin" "$backupdir/var.lib" "$backupdir/etc" +sudo cp "$GITEA_PATH" "$backupdir/bin/gitea-$old_ver" || die "Backup of binary failed!" +sudo cp -a "$GITEA_VAR" "$backupdir/var.lib/gitea-$old_ver" || die "Backup of /var/lib/gitea folder failed!" +sudo cp -a "/etc/gitea" "$backupdir/etc/gitea-$old_ver" || die "Backup of /etc/gitea folder failed!" + +info "Stopping Gitea and performing the install!!" +sudo systemctl stop gitea || die "Unable to stop gitea!" +sudo mv "$tmpdir/$(basename "$binary_link")" "$GITEA_PATH" || die "Unable to move new gitea!" +sudo chown root:root "$GITEA_PATH" +sudo chmod 0755 "$GITEA_PATH" + +info "Upgrade complete, restarting Gitea..." +sudo systemctl start gitea || die "Unable to start gitea!" + +info "Last minute cleanup..." +rm -r "$tmpdir" +info "Process done. Gitea should be available momentary." +