#!/bin/bash
#
# OnApp
# Cron script for update recovery ISO template
## Changelog ##
# * Tue Sep 10 2013 - Add checking for whether IMGs exist before sync

ONAPP_VERSION="3.1"
CONNECT_TIMEOUT=300
RANDOMIZE=600
RECOVERY_PATH=/onapp/tools/recovery/
RECOVERY_IMGS="recovery-centos-${ONAPP_VERSION}.initrd recovery-centos-${ONAPP_VERSION}.iso recovery-centos-${ONAPP_VERSION}.kernel"
source /etc/onapp.conf

[ ! -e $RECOVERY_PATH ] && mkdir -p $RECOVERY_PATH

if [ "x${FILE_TRANSFER_SERVER}" = "x" ]; then
    echo "File Transfer Server isn't configured.
Please run: # /onapp/onapp-hv-install/onapp-hv-config.sh -f <FTS_IP>
    where: FTP_IP - File Transfer Server FQDN or IP address from which recovery images are synced"
    exit 1
fi

SLEEP=$((RANDOM%$RANDOMIZE+0))
echo "Sync '$RECOVERY_IMGS' in $SLEEP seconds..."
sleep $SLEEP
for IMG in ${RECOVERY_IMGS}; do
    rsync -r -e "ssh -o ConnectTimeout=${CONNECT_TIMEOUT}" root@$FILE_TRANSFER_SERVER:/data/${IMG} $RECOVERY_PATH
done
