Commit be91ae48 authored by Jean-Claude BAU's avatar Jean-Claude BAU Committed by Maciej Lipinski

New feature: Automatic update of the leap seconds file

- Install cron
- New leap_seconds_file_update script. Launched at startup and then 1
time per day by cron
- Kconfig update to setup from where the file should be downloaded
parent 0172b343
......@@ -21,6 +21,10 @@ config DOTCONF_INFO
Free-text information about switch's dot-config. This field is
for information purpose only.
#
# Select source of dot-config file
#
choice DOTCONF_SOURCE
prompt "Source for a run-time replacement of dot-config"
default DOTCONF_SOURCE_TRY_DHCP
......@@ -64,6 +68,45 @@ config DOTCONF_URL
MACADDR are substituted before retrieving the file.
Example: "tftp://morgana/wrs-config-IPADDR"
#
# Select source of leap seconds file
#
choice LEAPSEC_SOURCE
prompt "Source for a run-time replacement of leap seconds file"
default LEAPSEC_SOURCE_LOCAL
config LEAPSEC_SOURCE_LOCAL
bool "Use local leap seconds file"
help
A leap seconds file (leap-seconds.list) is already present in the White Rabbit Switch.
If you select this option, the leap seconds file is not replaced
at run time.
config LEAPSEC_SOURCE_REMOTE_FORCE
bool "Force remote leap seconds file"
help
Use the URL to leap secondw file provided in LEAPSEC_URL.
If the file cannot be downloaded, it will cause errors in SNMP's objects.
config LEAPSEC_SOURCE_REMOTE_TRY
bool "Try remote leap seconds file"
help
The same as LEAPSEC_SOURCE_REMOTE_FORCE, but this option does not
cause errors in SNMP's objects if the switch fails to retrieve
the URL to the leap seconds files.
endchoice
config LEAPSEC_URL
string "URL for a run-time replacement of leap seconds file"
default "ftp://ftp.nist.gov/pub/time/leap-seconds.list"
depends on LEAPSEC_SOURCE_REMOTE_TRY || LEAPSEC_SOURCE_REMOTE_FORCE
help
tftp://, ftp:// or http:// URLs are allowed. Names are allowed if
you configured a DNS server. The special strings HOSTNAME, BOOTSERVER, IPADDR and
MACADDRare substituted before retrieving the file.
Example: "tftp://BOOTSERVER/leap-seconds.IPADDR"
config BR2_CONFIGFILE
string "Configuration file for Buildroot"
default "wrs_release_br2_config"
......
# run 1 time per day at 9:10. The script leap_seconds_file_update.sh is then executed randomly in a 23 hours range
10 9 * * * /wr/init.d/leap_seconds_file_update.sh -r 1380
#!/bin/sh
dotconfig=/wr/etc/dot-config
crond=/usr/sbin/crond
start() {
echo -n "Starting cron daemon: "
if [ -f $dotconfig ]; then
. $dotconfig
else
echo "$0 unable to source dot-config ($dotconfig)!"
fi
WRS_LOG=$WRS_LOG_OTHER
# if empty turn it to /dev/null
if [ -z $WRS_LOG ]; then
WRS_LOG="/dev/null";
fi
# if a pathname, use it
if echo "$WRS_LOG" | grep / > /dev/null; then
eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
else
# not a pathname: use verbatim
eval LOGPIPE=\" 2\>\&1 \| logger -t crond -p $WRS_LOG\"
fi
# set msg level
if [ ! -z $CONFIG_WRS_LOG_LEVEL_OTHER ]; then
WRS_MSG_LEVEL=$CONFIG_WRS_LOG_LEVEL_OTHER
export WRS_MSG_LEVEL
fi
# be carefull with pidof, no running script should have the same name as
# process
if pidof crond > /dev/null; then
# crond already running
echo "Failed (already running?)"
else
eval $crond -S -b -c /etc/cron.d $LOGPIPE
echo "OK"
fi
}
stop() {
echo -n "Stopping crond: "
start-stop-daemon -K -q --exec $crond
if [ $? -eq 0 ]; then
echo "OK"
else
echo "Failed"
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
#!/bin/bash
# This script downloads the leap_seconds.list file.
# At startup, this scrip must be started after the dot-config startup script
# to be sur to use the correct version of dot-config
# This script must be launched one time per day to search for a new leap seconds file
wait_before_processing() {
range=$1
error=0
rangeMax=1380 # Default/max value: 23 hours
[[ ! "$range" =~ ^-?[0-9]+$ ]] && error=1
if (( error == 1 )) || (( $range < 0 )) || (( $range > $rangeMax )) ; then
range=$rangeMax
eval echo "Invalid parameter for -r option. Use default value: $rangeMax mn" $LOGPIPE
fi
sleepTimeMin=`expr ${RANDOM} % $range`
sleepTimeSec=`expr $sleepTimeMin \* 60`
/bin/sleep $sleepTimeSec
}
tmpdir="/tmp"
prefix="leapseconds_download"
leapSecondsFileName="leap-seconds.list"
leapSeconds="/wr/etc/$leapSecondsFileName"
leapSecondsSource="$tmpdir/${prefix}_source"
leapSecondsStatus="$tmpdir/${prefix}_status"
leapSecondsSourceUrl="$tmpdir/${prefix}_url"
dotConfig="/wr/etc/dot-config"
dotConfigStatus="$tmpdir/dot-config_status"
tmpconfig="$tmpdir/$leapSecondsFileName"
bootServerIp="$tmpdir/boot_server_ip"
#
# Apply dot-config configuration
#
if [ -f $dotConfig ]; then
# source dot-config
. $dotConfig
else
echo "$0 unable to source dot-config ($dotConfig)!"
fi
WRS_LOG=$CONFIG_WRS_LOG_OTHER
# if empty turn it to /dev/null
if [ -z $WRS_LOG ]; then
WRS_LOG="/dev/null";
fi
# if a pathname, use it
if echo "$WRS_LOG" | grep / > /dev/null; then
eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
else
# not a pathname: use verbatim
eval LOGPIPE=\" 2\>\&1 \| logger -t leap_seconds_file_update -p $WRS_LOG\"
fi
# Read options
if [ "$#" -ge 2 ] && [ "$1" == "-r" ] ; then
wait_before_processing $2
fi
# Check if leap seconds file need run-time updates
CONFIG_LEAPSEC_URL=`echo $CONFIG_LEAPSEC_URL | tr "\t " `
if [ "$CONFIG_LEAPSEC_SOURCE_REMOTE_FORCE" = "y" ] || [ "$CONFIG_LEAPSEC_SOURCE_REMOTE_TRY" = "y" ] \
|| [ -n "$CONFIG_LEAPSEC_URL" ] ; then
# get URL via DHCP
if [ "$CONFIG_LEAPSEC_SOURCE_REMOTE_TRY" = "y" ] ; then
echo "try_remote" > $leapSecondsSource
else
echo "force_remote" > $leapSecondsSource
fi
URL="$CONFIG_LEAPSEC_URL"
# Check if the URL constans BOOTSERVER key . We need then to get the IP@ of the boot server
if [ "${URL/BOOTSERVER}" != "$URL" ] ; then
if [ -f $bootServerIp ]; then
bsip=`cat $bootServerIp | tr -d " \n\t"`
fi
if [ -z "$bsip" ] ; then
# let udhcpc run /wr/bin/dhcp_get_filename.sh script to get "filename"
# from DHCP (which contain url with dot-config).
/sbin/udhcpc -i eth0 -s /wr/bin/dhcp_get_filename.sh -O bootfile -o \
-n -q -f -t 5 &>/dev/null
fi
# Try again now
if [ -f $bootServerIp ]; then
bsip=`cat $bootServerIp | tr -d " \n\t"`
URL=`echo $URL | sed -e s/BOOTSERVER/$bsip/g`
fi
if [ -z "$bsip" ] ; then
# Cannot get the bootserver IP@
echo "dhcp_error" > $leapSecondsStatus
eval echo "Unable to get boot server IP. Use local leap seconds file" $LOGPIPE
exit
fi;
fi
# Check if the URL constains MACADDR key
if [ "${URL/MACADDR}" != "$URL" ] ; then
macaddr=$(cat /sys/class/net/eth0/address)
URL=`echo $URL | sed -e s/MACADDR/$macaddr/g`
fi
# Check if the URL constains IPADDR key
if [ "${URL/IPADDR}" != "$URL" ] ; then
ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
if [ -z "$ipaddr" ] ; then
# if no IP address available from barebox try to get IP from old dot-config
# /etc/init.d/network will run later again
/etc/init.d/network
ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
fi
URL=`echo $URL | sed -e s/IPADDR/$ipaddr/g`
fi
# Check if the URL constains HOSTNAME key
if [ "${URL/HOSTNAME}" != "$URL" ] ; then
host_name=`hostname`
URL=`echo $URL | sed -e s/HOSTNAME/$host_name/g`
fi
# split the parts, as we need to handle tftp by hand
proto=$(echo $URL | cut -d: -f 1)
host=$(echo $URL | cut -d/ -f 3)
filename=$(echo $URL | cut -d/ -f 4-)
# save URL, to be used by SNMPd
echo "$URL" > $leapSecondsSourceUrl
rm -f $tmpconfig
case $proto in
http|ftp)
wget $URL -O $tmpconfig
;;
tftp)
tftp -g -r "$filename" -l $tmpconfig $host
;;
*)
eval echo "Invalid URL to leap seconds file: \"$URL\""$LOGPIPE;
echo "invalid_url" > $leapSecondsStatus;
exit 1
;;
esac
if [ -f $tmpconfig ]; then
# Compare the file with the current
leapSecondUpdate=0
for dir in /etc /usr/etc ; do
cmp -s $tmpconfig $dir/$leapSecondsFileName || leapSecondUpdate=1
done;
if [ $leapSecondUpdate == 1 ] ; then
# Check file integrity (simple check)
tagCount=0
while read a ; do
if [[ "$a" =~ ^#@ ]] || [[ "$a" =~ ^#\$ ]] || [[ "$a" =~ ^#h ]] ; then
tagCount=`expr $tagCount + 1 `
fi
done < $tmpconfig
if [ $tagCount == 3 ] ; then
# The file seems OK
# The file must be copy into /etc and /wr/etc
for dir in /etc /usr/etc ; do
cp $tmpconfig $dir/$leapSecondsFileName.new
mv -f $dir/$leapSecondsFileName $dir/$leapSecondsFileName.old
mv $dir/$leapSecondsFileName.new $dir/$leapSecondsFileName
done
echo "updated" > $leapSecondsStatus
eval echo "leap seconds file updated" $LOGPIPE
else
echo "file_invalid" > $leapSecondsStatus
eval echo "Errors detected. Invalid leap seconds file \"$URL\". Using local one" $LOGPIPE
fi
else
echo "no_changes" > $leapSecondsStatus
eval echo "No changes detected" $LOGPIPE
fi
else
echo "download_error" > $leapSecondsStatus
eval echo "Download error of leap seconds file \"$URL\". Using local one" $LOGPIPE
fi
else
echo "local" > $leapSecondsSource
eval echo "Using local \"$leapSecondsFileName\" file" $LOGPIPE
fi
../init.d/leap_seconds_file_update
\ No newline at end of file
../init.d/cron.sh
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment