commit de0e27536d3353a14e7e064bc26e415b42eb305b
parent fb2c7506ecb0ec580f6dec344549474a9984a68d
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Mon, 17 Aug 2020 19:54:28 +0200

Unify notify function for all scripts

Now it is found on notify.sh, scripts that need to notify can simply
source this file.

Diffstat:
Mcheck-changes-website.sh | 18++++++------------
Mdeploy-website.sh | 13++++---------
Mlogin-notify.sh | 10+++-------
Anotify.sh | 28++++++++++++++++++++++++++++
Mwebsite-backup.sh | 10++--------
5 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/check-changes-website.sh b/check-changes-website.sh @@ -16,22 +16,16 @@ # Script that notifies through Gotify when a website has changed. -GOTIFY_DOMAIN="gotify.oscarbenedito.com" -API_TOKEN="$(cat "$(dirname "$(realpath "$0")")/gotify_token.txt")" +# File must implement notify funtion +. "$(dirname "$(realpath "$0")")/notify.sh" -check_and_send_message() { +check_and_notify() { newhash="$(curl "$URL" 2>/dev/null | sha256sum | cut -f 1 -d " ")" - [ "$HASH" != "$newhash" ] && \ - curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \ - -F "title=$TITLE" \ - -F "message=$URL" \ - -F "priority=5" \ - >/dev/null 2>&1 + [ "$HASH" != "$newhash" ] && notify "$TITLE" "$URL" } - +# Example usage: HASH="<hash>" URL="<url>" TITLE="<title>" - -check_and_send_message +check_and_notify diff --git a/deploy-website.sh b/deploy-website.sh @@ -16,10 +16,11 @@ # Script to deploy a website built with Hugo. +# File must implement notify funtion +. "$(dirname "$(realpath "$0")")/notify.sh" + HUGO_PATH="/root/oscarbenedito.com" WEB_PATH="/srv/oscarbenedito.com" -GOTIFY_DOMAIN="gotify.oscarbenedito.com" -FILE_DIR="$(dirname "$(realpath "$0")")" # Pull rewritting history if needed, check that commit is PGP signed by a known # key and if so, rebuild the website @@ -53,12 +54,6 @@ sed -i "/<url>/{:a;N;/<\/url>/!ba};N;/<loc>https:\/\/oscarbenedito\.com${path}<\ rsync --perms --recursive --checksum --delete "$HUGO_PATH/public/" "$WEB_PATH" # Notify -API_TOKEN="$(cat "$FILE_DIR/gotify_token.txt")" TITLE="Web update triggered" MESSAGE="Git hooks triggered an update of the website." - -curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \ - -F "title=$TITLE" \ - -F "message=$MESSAGE" \ - -F "priority=5" \ - >/dev/null 2>&1 +notify "$TITLE" "$MESSAGE" diff --git a/login-notify.sh b/login-notify.sh @@ -16,17 +16,13 @@ # Script that notifies Gotify when someone logs in through SSH to a computer/server. -GOTIFY_DOMAIN="gotify.oscarbenedito.com" -API_TOKEN="$(cat "$(dirname "$(realpath "$0")")/gotify_token.txt")" +# File must implement notify funtion +. "$(dirname "$(realpath "$0")")/notify.sh" if [ "$PAM_TYPE" != "close_session" ]; then TITLE="SSH login: ${PAM_USER}@$(hostname)" MESSAGE="IP: ${PAM_RHOST} Date: $(TZ='Europe/Madrid' date)" - curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \ - -F "title=$TITLE" \ - -F "message=$MESSAGE" \ - -F "priority=5" \ - >/dev/null 2>&1 + notify "$TITLE" "$MESSAGE" fi diff --git a/notify.sh b/notify.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env sh +# Gotify notify +# Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# Implements notify function, that notifies admin through Gotify. This file is +# imported by other scripts, but it could easily be used on its own adding lines +# that call the function. + +notify() { + GOTIFY_DOMAIN="gotify.oscarbenedito.com" + GOTIFY_TOKEN="$(cat "$(dirname "$(realpath "$0")")/gotify_token.txt")" + curl -X POST "https://$GOTIFY_DOMAIN/message?token=$GOTIFY_TOKEN" \ + -F "title=$1" -F "message=$2" -F "priority=${3:-5}" \ + >/dev/null 2>&1 +} diff --git a/website-backup.sh b/website-backup.sh @@ -19,8 +19,7 @@ # adding a date to the filename. FILE_DIR="$(dirname "$(realpath "$0")")" -GOTIFY_DOMAIN="gotify.oscarbenedito.com" -API_TOKEN="$(cat "$FILE_DIR/gotify_token.txt")" +. "$FILE_DIR/notify.sh" URL_FILE="$FILE_DIR/urls.txt" BACKUP_PATH="$HOME/backups" @@ -29,12 +28,7 @@ save() { wget --quiet --output-document "$OUTPUT" "$URL" ; } error_message () { TITLE="Website backup error" MESSAGE="Error backing up $OUTPUT" - - curl -X POST "https://$GOTIFY_DOMAIN/message?token=$API_TOKEN" \ - -F "title=$TITLE" \ - -F "message=$MESSAGE" \ - -F "priority=5" \ - >/dev/null 2>&1 + notify "$TITLE" "$MESSAGE" } while IFS= read -r line