diff --git a/scitas-motd-scripts/bin/scitas-motd-update b/scitas-motd-scripts/bin/scitas-motd-update index bf14494..df8235a 100755 --- a/scitas-motd-scripts/bin/scitas-motd-update +++ b/scitas-motd-scripts/bin/scitas-motd-update @@ -1,150 +1,182 @@ #!/bin/bash ### Global variables -APPNAME="$(basename ${0})" +# 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}; } +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 ## License: GNU General Public License v2, v3 # # Lockable script boilerplate ### HEADER ### if [ -w /var/lock ] then - LOCKFILE="/var/lock/`basename $0`" + LOCKFILE="/var/lock/$(basename "$0")" else - LOCKFILE="/tmp/`basename $0`.lock" + LOCKFILE="/tmp/$(basename "$0").lock" fi LOCKFD=99 # PRIVATE -_lock() { flock -$1 $LOCKFD; } -_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; } +_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 -# Location of the configuration file -SMOTD_CONFIG=${SMOTD_CONFIG:-/etc/scitas-motd.conf} -# Read configuration options -if [ -r ${SMOTD_CONFIG} ] +## Read configuration options +SMOTD_CONFIG=${SMOTD_CONFIG:-/etc/scitas-motd.conf} +if [ -r "${SMOTD_CONFIG}" ] then - . ${SMOTD_CONFIG} + . "${SMOTD_CONFIG}" fi -# Check that all the required configuration options are set +# 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 -# Set every other option to the defaults +# 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} -SMOTD_WORKDIR=${SMOTD_WORKDIR:-/var/run/scitas-motd} +WORKDIR=${SMOTD_WORKDIR:-/var/run/scitas-motd} +FRAGMENTS="${WORKDIR}/fragments" + -# Check if curl is available -${SMOTD_CURL} --version 2>&1 > /dev/null +## 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 -# Getting the list of basenames for the fragments -rm -f "${SMOTD_WORKDIR}/fragments" -mkdir -p ${SMOTD_WORKDIR} -${SMOTD_CURL} ${SMOTD_CURL_OPT} \ + +## 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 "${SMOTD_WORKDIR}/fragments" 2>&1 > /dev/null -if [ ! $? ] + --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 -# Getting the fragments, all the listed fragments listed are mandatory + +## 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_CURL}" ${SMOTD_CURL_OPT} \ "${SMOTD_URL}/${frag}${suffix}${SMOTD_CURL_SUFFIX}" \ - --output "${SMOTD_WORKDIR}/${frag}" 2>&1 > /dev/null \ + --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 < "${SMOTD_WORKDIR}/fragments" +done < "${WORKDIR}/fragments" + +## Build the final outopput file currtime=$(date +%Y%m%d.%H%M%S) -tmpmotd="${SMOTD_WORKDIR}/motd.scitas.${currtime}" +tmpmotd="${WORKDIR}/motd.scitas.${currtime}" rm -f "${tmpmotd}" -mkdir -p $(dirname "${tmpmotd}") +mkdir -p "$(dirname "${tmpmotd}")" echo "###########################################################" \ >> "${tmpmotd}" while read frag do - cat "${SMOTD_WORKDIR}/${frag}" >> "${tmpmotd}" + cat "${WORKDIR}/${frag}" >> "${tmpmotd}" echo "###########################################################" \ >> "${tmpmotd}" -done < "${SMOTD_WORKDIR}/fragments" +done < "${WORKDIR}/fragments" -if ! diff --brief --new-file ${SMOTD} ${tmpmotd} > /dev/null +if ! diff --brief --new-file "${SMOTD}" "${tmpmotd}" > /dev/null then - mv "${tmpmotd}" ${SMOTD} + mv "${tmpmotd}" "${SMOTD}" else rm -f "${tmpmotd}" fi exit 0