Update script using best practices

This commit is contained in:
adrien 2022-04-18 23:42:24 +02:00
parent 60baaeac9f
commit 91c3a12fc9
Signed by: adrien
GPG Key ID: 4F17BEA67707AC21
2 changed files with 34 additions and 36 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -1,65 +1,62 @@
#! /usr/bin/env bash #! /usr/bin/env bash
# all executed commands are printed to stdout # All executed commands are printed to stdout
set -x set -x
# redirect stdout (and stderr to stdout) to a file # Redirect stdout (and stderr to stdout) to a file
exec 1> /var/log/updater/gitea-updater/gitea-$(date +%F).log 2>&1 exec 1>/var/log/updater/gitea-updater/gitea-"$(date +%F)".log 2>&1
# abort on nonzero exitstatus # Abort on nonzero exit status
set -o errexit set -o errexit
# abort on unbound variable # Abort on unbound variable
set -o nounset set -o nounset
# don't hide errors within pipes # Don't hide errors within pipes
set -o pipefail set -o pipefail
# set your API key here # Set your API key here
key= key=
# set your chat id here # Set your chat id here
chat_id= chat_id=
function send_message() { function send_message() {
if [ $# -eq 0 ] if [ $# -eq 0 ]; then
then echo "No argument supplied, please specify the message to send"
echo "No argument supplied. Please specify the message to send." else
else curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
curl --silent --show-error --fail --request POST https://api.telegram.org/${key}/sendMessage --data chat_id=${chat_id} --data text="${1}" --output /dev/null fi
fi
} }
function update_gitea() { function update_gitea() {
systemctl stop gitea.service systemctl stop gitea.service
# download the latest version # Download the latest version
curl --location --output /usr/local/bin/gitea --silent --show-error --fail https://dl.gitea.io/gitea/${1}/gitea-${1}-linux-amd64 curl --location --output /usr/local/bin/gitea --silent --show-error --fail --request GET "https://dl.gitea.io/gitea/$1/gitea-$1-linux-amd64"
# set the correct permissions # Set the correct permissions
chmod +x /usr/local/bin/gitea /usr/bin/chmod +x /usr/local/bin/gitea
/usr/sbin/restorecon /usr/local/bin/gitea /usr/sbin/restorecon /usr/local/bin/gitea
/usr/sbin/restorecon /var/lib/gitea/log/gitea.log /usr/sbin/restorecon /var/lib/gitea/log/gitea.log
if systemctl start gitea.service if systemctl start gitea.service; then
then send_message "[Gitea] - Gitea has just been updated"
send_message "[Gitea] - Gitea has just been updated." else
else send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong"
send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong." fi
fi
} }
# retrieve local release # Retrieve local release
local_release=$(/usr/local/bin/gitea --version | awk '{print $3}') local_release=$(/usr/local/bin/gitea --version | awk '{print $3}')
# retrieve the latest release # Retrieve the latest release
latest_release=$(curl -sSf "https://api.github.com/repos/go-gitea/gitea/releases/latest" | grep '"tag_name":' | sed --regexp-extended 's/.*"([^"]+)".*/\1/' | cut --characters=2-) latest_release=$(curl --silent --show-error --fail --request GET "https://api.github.com/repos/go-gitea/gitea/releases/latest" | grep '"tag_name":' | sed --regexp-extended 's/.*"([^"]+)".*/\1/' | cut --characters=2-)
# compare these two versions # Compare these two versions
if [ ${latest_release} == ${local_release} ] if [ "$latest_release" == "$local_release" ]; then
then send_message "[Gitea] - Gitea is up to date"
send_message "[Gitea] - Gitea is up to date."
else else
send_message "[Gitea] - Gitea is not up to date (https://github.com/go-gitea/gitea/releases/tag/v${latest_release})." send_message "[Gitea] - Gitea is not up to date (https://github.com/go-gitea/gitea/releases/tag/v$latest_release)"
update_gitea ${latest_release} update_gitea "$latest_release"
fi fi