commit 45cb2af189c711f7cb93b7105061d74da43b7f43 Author: adrien Date: Sun Dec 13 14:09:28 2020 +0100 First commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ae88d09 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Adrien + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..81590f4 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# 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. + +## 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 + +### 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. + +## Installation +Since this script must be executed with root rights, it is a good practice to place it in `/usr/local/sbin/`. + +``` +sudo curl -Lo /usr/local/sbin/gitea-updater -sSf https://gitea.illuad.fr/adrien/gitea-updater/raw/branch/master/gitea-updater +sudo chmod 750 /usr/local/sbin/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=/" /usr/local/sbin/tls-checker +``` + +For the `chat_id` variable. + +``` +sudo sed -i "s/chat_id=/chat_id=/" /usr/local/sbin/tls-checker +``` + +## 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 +``` + +Every day at 1:00 am, the script will check if the Gitea is up to date. diff --git a/gitea-updater b/gitea-updater new file mode 100644 index 0000000..f858c19 --- /dev/null +++ b/gitea-updater @@ -0,0 +1,58 @@ +#! /usr/bin/env bash + +# abort on nonzero exitstatus +set -o errexit + +# abort on unbound variable +set -o nounset + +# don't hide errors within pipes +set -o pipefail + +# set your API key here +key= + +# set your chat id here +chat_id= + +function send_message { + if [ $# -eq 0 ] + then + echo "No argument supplied. Please specify the message to send." + 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 + fi +} + +function update_gitea { + systemctl stop gitea.service + + # download the latest version + sudo curl --location --output /usr/local/bin/gitea --silent --show-error --fail https://dl.gitea.io/gitea/${1}/gitea-${1}-linux-amd64 + + # set the correct permissions + sudo chmod +x /usr/local/bin/gitea + restorecon /usr/local/bin/gitea + + if systemctl start gitea.service + then + send_message "[Gitea] - Gitea has just been updated." + else + send_message "[Gitea] - Gitea service did not start correctly. Please log in as soon as possible and see what went wrong." + fi +} + +# retrieve local release +local_release=$(/usr/local/bin/gitea --version | awk '{print $3}') + +# 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-) + +# compare these two versions +if [ ${latest_release} == ${local_release} ] +then + send_message "[Gitea] - Gitea is up to date." +else + 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