Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60127071
scitas-motd-update
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Apr 27, 17:20
Size
3 KB
Mime Type
text/x-shellscript
Expires
Mon, Apr 29, 17:20 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17305307
Attached To
rSCMOTDSCRIPTS scitas-motd-scripts
scitas-motd-update
View Options
#!/bin/bash
### Global variables
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
# Location of the configuration file
SMOTD_CONFIG=${SMOTD_CONFIG:-/etc/scitas-motd.conf}
# Read configuration options
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
# 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}
# Check if curl is available
${SMOTD_CURL} --version 2>&1 > /dev/null
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} \
"${SMOTD_URL}/fragments${SMOTD_CURL_SUFFIX}" \
--output "${SMOTD_WORKDIR}/fragments" 2>&1 > /dev/null
if [ ! $? ]
then
log "[ERROR] Failed to get the fragments list from: ${SMOTD_URL}" stderr
exit 1
fi
# Getting 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 "${SMOTD_WORKDIR}/${frag}" 2>&1 > /dev/null \
&& 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"
currtime=$(date +%Y%m%d.%H%M%S)
tmpmotd="${SMOTD_WORKDIR}/motd.scitas.${currtime}"
rm -f "${tmpmotd}"
mkdir -p $(dirname "${tmpmotd}")
echo "###########################################################" \
>> "${tmpmotd}"
while read frag
do
cat "${SMOTD_WORKDIR}/${frag}" >> "${tmpmotd}"
echo "###########################################################" \
>> "${tmpmotd}"
done < "${SMOTD_WORKDIR}/fragments"
if ! diff --brief --new-file ${SMOTD} ${tmpmotd} > /dev/null
then
mv "${tmpmotd}" ${SMOTD}
else
rm -f "${tmpmotd}"
fi
exit 0
Event Timeline
Log In to Comment