Add .sh extension and improve control structure to avoid else statement

This commit is contained in:
adrien 2022-06-12 21:33:29 +02:00
parent 91c3a12fc9
commit a9e0aeab6d
Signed by: adrien
GPG Key ID: 4F17BEA67707AC21
2 changed files with 39 additions and 24 deletions

View File

@ -1,28 +1,37 @@
# gitea-updater
This script checks if Gitea is up to date, updates it if necessary and sends a message to a Telegram bot.
## Introduction
The trap to avoid when installing custom software is to update it. It seems obvious but it is never very easy because there is often a compilation part, interoperability management between bricks, backups and so on.
The trap to avoid when installing custom software is to update it. It seems obvious but it is never very easy because
there is often a compilation part, interoperability management between bricks, backups and so on.
## Requirements
### Software
It is clearly necessary to have deployed a Gitea instance on your server. I wrote an article about this topic here: https://illuad.fr/2020/08/01/install-a-gitea-server.html
Since a message is sent to a Telegram bot, it is necessary to have one configured. I wrote an article about this topic here: https://illuad.fr/2020/10/27/get-a-telegram-alert-on-a-ssh-login-with-pam.html
It is clearly necessary to have deployed a Gitea instance on your server. I wrote an article about this topic
here: https://illuad.fr/2020/08/01/install-a-gitea-server.html
Since a message is sent to a Telegram bot, it is necessary to have one configured. I wrote an article about this topic
here: https://illuad.fr/2020/10/27/get-a-telegram-alert-on-a-ssh-login-with-pam.html
### System
This script can run on any GNU/Linux machine.
This script uses `curl` and `restorecon` commands but if you have followed my article, some of them are required which means they will necessarily be installed.
This script uses `curl` and `restorecon` commands but if you have followed my article, some of them are required which
means they will necessarily be installed.
## Installation
Since this script must be executed with root rights, it is a good practice to place it in `/usr/local/sbin/`.
```
curl -LOsSf https://gitea.illuad.fr/adrien/gitea-updater/raw/branch/master/gitea-updater
sudo mv gitea-updater /usr/local/sbin
sudo chmod 750 /usr/local/sbin/gitea-updater
curl -LOsSf https://gitea.illuad.fr/adrien/gitea-updater/raw/branch/master/gitea-updater.sh
sudo mv gitea-updater.sh /usr/local/sbin
sudo chmod 750 /usr/local/sbin/gitea-updater.sh
```
Create the logs' directory.
@ -32,29 +41,32 @@ sudo mkdir -p /var/log/updater/gitea-updater
```
## Configuration
This script requires the configuration of 2 variables to work: `key` and `chat_id`.
Variables `key` and `chat_id` correspond to the API key and the chat id obtained during the bot creation process.
#### Fast variables setting
For the `key` variable.
```
sudo sed -i "s/key=/key=<your_key>/" /usr/local/sbin/gitea-updater
sudo sed -i "s/key=/key=<your_key>/" /usr/local/sbin/gitea-updater.sh
```
For the `chat_id` variable.
```
sudo sed -i "s/chat_id=/chat_id=<your_chat_id>/" /usr/local/sbin/gitea-updater
sudo sed -i "s/chat_id=/chat_id=<your_chat_id>/" /usr/local/sbin/gitea-updater.sh
```
## Automation
Running this script automatically is a good idea, here is what you should have in the cron jobs of the root user.
```
sudo crontab -l
0 1 * * * /usr/local/sbin/gitea-updater
0 1 * * * /usr/local/sbin/gitea-updater.sh
```
Every day at 1:00 am, the script will check if the Gitea is up to date.
Every day at 1:00 am, the script will check if the Gitea is up-to-date.

View File

@ -1,4 +1,4 @@
#! /usr/bin/env bash
#!/usr/bin/env bash
# All executed commands are printed to stdout
set -x
@ -22,11 +22,13 @@ key=
chat_id=
function send_message() {
if [ $# -eq 0 ]; then
echo "No argument supplied, please specify the message to send"
else
if ! [ $# -eq 0 ]; then
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
exit
fi
echo "No argument supplied, please specify the message to send"
exit 1
}
function update_gitea() {
@ -36,15 +38,16 @@ function update_gitea() {
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
/usr/bin/chmod +x /usr/local/bin/gitea
/usr/sbin/restorecon /usr/local/bin/gitea
/usr/sbin/restorecon /var/lib/gitea/log/gitea.log
chmod +x /usr/local/bin/gitea
restorecon /usr/local/bin/gitea
restorecon /var/lib/gitea/log/gitea.log
if systemctl start gitea.service; then
send_message "[Gitea] - Gitea has just been updated"
else
if ! systemctl start gitea.service; then
send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong"
exit 1
fi
send_message "[Gitea] - Gitea has just been updated"
}
# Retrieve local release
@ -54,9 +57,9 @@ local_release=$(/usr/local/bin/gitea --version | awk '{print $3}')
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
if [ "$latest_release" == "$local_release" ]; then
send_message "[Gitea] - Gitea is up to date"
else
if ! [ "$latest_release" == "$local_release" ]; then
send_message "[Gitea] - Gitea is not up to date (https://github.com/go-gitea/gitea/releases/tag/v$latest_release)"
update_gitea "$latest_release"
fi
send_message "[Gitea] - Gitea is up to date"