Make check updates more generic, fix failure notify requirements and update docs

This commit is contained in:
Alexander Schäferdiek 2023-01-25 09:26:00 +01:00
parent dde06a6da3
commit 4d47fd31db
6 changed files with 93 additions and 45 deletions

View file

@ -1,26 +1,30 @@
# README # README
system-helpers is a collection of helper systemd services, systemd timers and system-helpers - Collection of helper systemd services, systemd timers and
shell scripts for common configuration and tasks like shell scripts for common configuration.
* checking if systemd services and timers are running **system-helpers** collection provides the following helpers
* borgmatic backup
* restic backup - checking if systemd services and timers are running (see **man systemd_check**)
* checking if docker containers are running - borgmatic backup (see exposed systemd services *borgmatic@* which require a valid *borgmatic.yml* file in */etc/borgmatic* or your **$HOME/.config/borgmatic**)
* batch docker-compose upgrades - restic backup (see */usr/share/doc/system-helpers/restic* for an example *mybackup*)
* docker housekeeping for removing unused docker containers, volumes and images - checking if docker containers are running (see **man docker_check**)
* dynamic dns update - batch docker-compose upgrades (see **man docker_compose_update**)
* update check for pacman - dynamic dns update (see **man docker_compose_update**)
* memory and disk checks - memory and disk checks (see **man disk_space_alert** and **man memory_usage_alert**)
- docker housekeeping for removing unused docker containers, volumes and images
- update check for pacman (ArchLinux specific, see **man check_updates**)
Provided systemd services mostly support mail notifications if they have failed Provided systemd services mostly support mail notifications if they have failed
via systemd_failure_notify. via `systemd_failure_notify`.
See `/usr/share/doc/system-helpers` for example configurations which should See `/usr/share/doc/system-helpers` for example configurations which should
be copied inside $HOME or /etc depending on the helper script. be copied inside $HOME or /etc depending on the helper script.
## Installation ## Installation
### Package manager for Arch Linux
1. Build a `pacman` package for ArchLinux via `makepkg -csi` inside the `_pkg/` folder 1. Build a `pacman` package for ArchLinux via `makepkg -csi` inside the `_pkg/` folder
2. Alternatively, use `./sync` from the git clone and pick an action which should be self-explaining 2. Alternatively, use `./sync` from the git clone and pick an action which should be self-explaining
3. Use the custom provided ArchLinux repository hosted at `aur.myservermanager.com` 3. Use the custom provided ArchLinux repository hosted at `aur.myservermanager.com`
@ -31,6 +35,11 @@ SigLevel = Never
Server = https://aur.myservermanager.com Server = https://aur.myservermanager.com
``` ```
### system-helpers-sync script to synchronize files to locations
1. Clone this repository
2. Run `./system-helpers-sync` and use the option you like to use
## Usage ## Usage
Services and scripts don't depend on each other but have hints that they can be Services and scripts don't depend on each other but have hints that they can be

View file

@ -17,6 +17,8 @@ If no *CONFIG_FILE* is provided, **check_updates** tries to read from *$HOME/.ch
The following are at least required for the script to work:\ The following are at least required for the script to work:\
- **CHECK_UPDATES_MAIL_ADDRESS=""** which requires *mail.rc* to be configured and '*mailx*' command has be available. - **CHECK_UPDATES_MAIL_ADDRESS=""** which requires *mail.rc* to be configured and '*mailx*' command has be available.
- **CHECK_UPDATES_UPDATES_CMD** is the command for retrieving which updates are available, by default it's set to */usr/bin/checkupdates* (Arch Linux default).
- **CHECK_UPDATES_UPDATES_AMOUNT_CMD** is the command used to determine the amount of updates, by default it's set to */usr/bin/checkupdates | wc -l* (Arch Linux default).
You can copy this script to */usr/local/bin* and use create a custom **CONFIG_FILE** as user. You can copy this script to */usr/local/bin* and use create a custom **CONFIG_FILE** as user.

View file

@ -4,7 +4,7 @@
# NAME # NAME
system-helpers - Collection of helper systemd services, systemd timers and system-helpers - Collection of helper systemd services, systemd timers and
shell scripts for common configuration. Designed for use with ArchLinux, although most scripts should work on any distribution. shell scripts for common configuration.
# DESCRIPTION # DESCRIPTION
**system-helpers** collection provides the following helpers **system-helpers** collection provides the following helpers

View file

@ -2,65 +2,95 @@
# usage # usage
usage() { usage() {
USAGE=$(cat <<EOF USAGE=$(
cat <<EOF
Usage: check_updates [CONFIG_FILE (absolute path)] Usage: check_updates [CONFIG_FILE (absolute path)]
Sends out update notifications. Please see man check_updates. Sends out update notifications. Please see man check_updates.
EOF EOF
) )
echo "$USAGE"; echo "$USAGE"
} }
set -e; set -e
HOSTNAME=$(hostname)
UPDATES=$(/usr/bin/checkupdates)
UPDATES_AMOUNT=$(/usr/bin/checkupdates|wc -l)
CHECK_UPDATES_MAIL_ADDRESS="";
# check for config file # check for config file
apply_config() { apply_config() {
local config=$1; local config=$1
if [[ ! -f "$config" ]]; then if [[ ! -f "$config" ]]; then
echo "No config file specified"; echo "No config file specified"
echo ""; echo ""
usage; usage
exit 1; exit 1
fi fi
set -a; set -a
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$config"; source "$config"
set +a; set +a
} }
source_config() { source_config() {
local config=$1; local config=$1
local configFallback=$2; local configFallback=$2
local configGlobalFallback=$3; local configGlobalFallback=$3
if [[ -f "$config" ]]; then if [[ -f "$config" ]]; then
apply_config "$config"; apply_config "$config"
return; return
fi fi
if [[ -f "$configFallback" ]]; then if [[ -f "$configFallback" ]]; then
apply_config "$configFallback"; apply_config "$configFallback"
return; return
fi fi
if [[ -f "$configGlobalFallback" ]]; then if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback"; apply_config "$configGlobalFallback"
return; return
fi fi
} }
apply_defaults() {
[[ -z ${CHECK_UPDATES_UPDATES_CMD} ]] && CHECK_UPDATES_UPDATES_CMD="/usr/bin/checkupdates"
[[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]] && CHECK_UPDATES_UPDATES_AMOUNT_CMD="/usr/bin/checkupdates | wc -l"
}
check_required() {
if [[ -z ${CHECK_UPDATES_UPDATES_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_CMD is required"
exit 1
fi
if [[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_AMOUNT_CMD is required"
exit 1
fi
if [[ -z ${CHECK_UPDATES_MAIL_ADDRESS} ]]; then
echo "CHECK_UPDATES_MAIL_ADDRESS is required"
exit 1
fi
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
}
source_config "$1" "$HOME/.check_updates.conf" "/etc/check_updates.conf" source_config "$1" "$HOME/.check_updates.conf" "/etc/check_updates.conf"
apply_defaults
check_required
if [ "$UPDATES_AMOUNT" -gt "0" ]; then HOSTNAME=$(hostname)
mailx -s "[updates $HOSTNAME]" "$CHECK_UPDATES_MAIL_ADDRESS" << EOF CHECK_UPDATES_UPDATES=$(eval "${CHECK_UPDATES_UPDATES_CMD}")
There are $UPDATES_AMOUNT updates available on $HOSTNAME. CHECK_UPDATES_UPDATES_AMOUNT=$(eval "${CHECK_UPDATES_UPDATES_AMOUNT_CMD}")
$UPDATES if [[ "$CHECK_UPDATES_UPDATES_AMOUNT" -gt "0" ]]; then
SUBJECT="[updates ${HOSTNAME}]"
MESSAGE=$(
cat <<EOF
There are ${CHECK_UPDATES_UPDATES_AMOUNT} updates available on ${HOSTNAME}.
${CHECK_UPDATES_UPDATES};
EOF EOF
)
echo "$MESSAGE" | mailx -Ssendwait -s "$SUBJECT" "$CHECK_UPDATES_MAIL_ADDRESS"
fi fi

View file

@ -51,11 +51,12 @@ check_requirements() {
local gotifyEnabled=$2; local gotifyEnabled=$2;
if [[ "${mailEnabled}" == "true" ]]; then if [[ "${mailEnabled}" == "true" ]]; then
type mailx &> /dev/null || echo "Requiring 'mailx' but it's not installed"; exit 1 type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
fi fi
if [[ "${gotifyEnabled}" == "true" ]]; then if [[ "${gotifyEnabled}" == "true" ]]; then
type gotify &> /dev/null || echo "Requiring 'gotify' but it's not installed"; exit 1 type gotify &> /dev/null || { echo "Requiring 'gotify' but it's not installed"; exit 1; }
fi fi
} }

View file

@ -46,6 +46,12 @@ The following are at least required for the script to work:
- \f[B]CHECK_UPDATES_MAIL_ADDRESS=\[lq]\[lq]\f[R] which requires - \f[B]CHECK_UPDATES_MAIL_ADDRESS=\[lq]\[lq]\f[R] which requires
\f[I]mail.rc\f[R] to be configured and `\f[I]mailx\f[R]' command has be \f[I]mail.rc\f[R] to be configured and `\f[I]mailx\f[R]' command has be
available. available.
- \f[B]CHECK_UPDATES_UPDATES_CMD\f[R] is the command for retrieving
which updates are available, by default it\[cq]s set to
\f[I]/usr/bin/checkupdates\f[R] (Arch Linux default).
- \f[B]CHECK_UPDATES_UPDATES_AMOUNT_CMD\f[R] is the command used to
determine the amount of updates, by default it\[cq]s set to
\f[I]/usr/bin/checkupdates | wc -l\f[R] (Arch Linux default).
.PP .PP
You can copy this script to \f[I]/usr/local/bin\f[R] and use create a You can copy this script to \f[I]/usr/local/bin\f[R] and use create a
custom \f[B]CONFIG_FILE\f[R] as user. custom \f[B]CONFIG_FILE\f[R] as user.