Page MenuHomec4science

scitas-motd-update
No OneTemporary

File Metadata

Created
Fri, Dec 27, 18:09

scitas-motd-update

#!/bin/bash
### Global variables
# The double quotes inside the parenthesis do not interfere with the ones on
# the outside.
APPNAME="$(basename "${0}")"
PID="${$}"
### Functions
# Logging template
log() { echo "$(date) $(hostname -s) ${APPNAME}[${PID}]: ${1}" > /dev/"${2:-stdout}"; }
### locking wizardry taken from https://gist.github.com/przemoc/571091
## Copyright (C) 2009 Przemyslaw Pawelczyk <przemoc@gmail.com>
## License: GNU General Public License v2, v3
#
# Lockable script boilerplate
### HEADER ###
if [ -w /var/lock ]
then
LOCKFILE="/var/lock/$(basename "$0")"
else
LOCKFILE="/tmp/$(basename "$0").lock"
fi
LOCKFD=99
# PRIVATE
_lock() { flock -"$1" $LOCKFD; }
_no_more_locking() { _lock u; _lock xn && rm -f "$LOCKFILE"; }
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; }
# ON START
_prepare_locking
# PUBLIC
exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail
exlock() { _lock x; } # obtain an exclusive lock
shlock() { _lock s; } # obtain a shared lock
unlock() { _lock u; } # drop a lock
### BEGIN OF SCRIPT ###
# Simplest example is avoiding running multiple instances of script.
if ! exlock_now
then
log "failed to aquire lock. Exiting!"
exit 1
fi
# Remember! Lock file is removed when one of the scripts exits and it is
# the only script holding the lock or lock is not acquired at all.
### End of locking wizardry
## Read configuration options
SMOTD_CONFIG=${SMOTD_CONFIG:-/etc/scitas-motd.conf}
if [ -r "${SMOTD_CONFIG}" ]
then
. "${SMOTD_CONFIG}"
fi
# check that all the required configuration options are set
REQUIRED_OPTIONS="SMOTD_URL"
MISSING_OPTION=false
for option in ${REQUIRED_OPTIONS}
do
if [ ! -v ${option} ]
then
log "[ERROR] Missing configuration value: '${option}'" stderr
MISSING_OPTION=true
fi
done
if ${MISSING_OPTION}
then
log "[ERROR] Missing configuration options, update ${SMOTD_CONFIG}" stderr
exit 1
fi
# and set every other option to the defaults
SMOTD=${SMOTD:-/etc/motd.scitas}
SMOTD_CURL=${SMOTD_CURL:-curl}
SMOTD_CURL_OPT=${SMOTD_CURL_OPT:-"--location --fail --silent --max-time 30"}
SMOTD_CURL_SUFFIX=${SMOTD_CURL_SUFFIX}
WORKDIR=${SMOTD_WORKDIR:-/var/run/scitas-motd}
FRAGMENTS="${WORKDIR}/fragments"
## Make sure curl is available
"${SMOTD_CURL}" --version > /dev/null 2>&1
if [ ! $? ]
then
log "[ERROR] curl (${SMOTD_CURL}) not available!" stderr
exit 1
fi
## Prepare the work directory for downloading the fragment list
if [ ! -d "${WORKDIR}" ]
then
if ! mkdir -p "${WORKDIR}"
then
log "[ERROR] Failed to create WORKDIR: ${WORKDIR}" stderr
exit 1
fi
elif [ ! -w "${WORKDIR}" ]
then
log "[ERROR] Can't write to WORKDIR: ${WORKDIR}" stderr
exit 1
fi
if [ -e "${FRAGMENTS}" ]
then
if ! mv -f "${FRAGMENTS}" "${FRAGMENTS}.old"
then
log "[ERROR] Can't rewrite fragments file: ${FRAGMENTS}"
exit 1
fi
fi
## Getting the list of basenames for the fragments
# The SMOTD_CURL_OPT are explicitly left unquoted for expansion
if ! "${SMOTD_CURL}" ${SMOTD_CURL_OPT} \
"${SMOTD_URL}/fragments${SMOTD_CURL_SUFFIX}" \
--output "${FRAGMENTS}" > /dev/null 2>&1
then
log "[ERROR] Failed to get the fragments list from: ${SMOTD_URL}" stderr
exit 1
elif [ ! -s "${FRAGMENTS}" ]
then
log "[ERROR] Retrieved fragments file is empty: ${FRAGMENTS}"
exit 1
fi
## Get the fragments, all the listed fragments listed are mandatory
while read frag
do
FOUND=false
# from general to more specific, more specific wins
for suffix in '' ".${SMOTD_CLUSTER}" ".$(hostname -s)"
do
"${SMOTD_CURL}" ${SMOTD_CURL_OPT} \
"${SMOTD_URL}/${frag}${suffix}${SMOTD_CURL_SUFFIX}" \
--output "${WORKDIR}/${frag}" > /dev/null 2>&1 \
&& FOUND=true
done
# it's ok as long as any one of the fragments is found
if ! ${FOUND}
then
log "[ERROR] Couldn't get the fragment '${frag}' from ${SMOTD_URL}" stderr
exit 1
fi
done < "${WORKDIR}/fragments"
## Build the final outopput file
currtime=$(date +%Y%m%d.%H%M%S)
tmpmotd="${WORKDIR}/motd.scitas.${currtime}"
rm -f "${tmpmotd}"
mkdir -p "$(dirname "${tmpmotd}")"
echo "###########################################################" \
>> "${tmpmotd}"
while read frag
do
cat "${WORKDIR}/${frag}" >> "${tmpmotd}"
echo "###########################################################" \
>> "${tmpmotd}"
done < "${WORKDIR}/fragments"
if ! diff --brief --new-file "${SMOTD}" "${tmpmotd}" > /dev/null
then
mv "${tmpmotd}" "${SMOTD}"
else
rm -f "${tmpmotd}"
fi
exit 0

Event Timeline