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
system-helpers is a collection of helper systemd services, systemd timers and
shell scripts for common configuration and tasks like
system-helpers - Collection of helper systemd services, systemd timers and
shell scripts for common configuration.
* checking if systemd services and timers are running
* borgmatic backup
* restic backup
* checking if docker containers are running
* batch docker-compose upgrades
* docker housekeeping for removing unused docker containers, volumes and images
* dynamic dns update
* update check for pacman
* memory and disk checks
**system-helpers** collection provides the following helpers
- checking if systemd services and timers are running (see **man systemd_check**)
- borgmatic backup (see exposed systemd services *borgmatic@* which require a valid *borgmatic.yml* file in */etc/borgmatic* or your **$HOME/.config/borgmatic**)
- restic backup (see */usr/share/doc/system-helpers/restic* for an example *mybackup*)
- checking if docker containers are running (see **man docker_check**)
- batch docker-compose upgrades (see **man docker_compose_update**)
- dynamic dns update (see **man docker_compose_update**)
- 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
via systemd_failure_notify.
via `systemd_failure_notify`.
See `/usr/share/doc/system-helpers` for example configurations which should
be copied inside $HOME or /etc depending on the helper script.
## Installation
### Package manager for Arch Linux
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
3. Use the custom provided ArchLinux repository hosted at `aur.myservermanager.com`
@ -31,6 +35,11 @@ SigLevel = Never
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
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:\
- **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.

View file

@ -4,7 +4,7 @@
# NAME
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
**system-helpers** collection provides the following helpers

View file

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

View file

@ -51,11 +51,12 @@ check_requirements() {
local gotifyEnabled=$2;
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
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
}

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[I]mail.rc\f[R] to be configured and `\f[I]mailx\f[R]' command has be
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
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.