#!/bin/bash

# Script for controlling deploy script
#
# Assumption: deployment script returning with code 0
#             run correctly and everything is done
#
# This script is run inside of KVM, by passing its
# URL with bootstrap-script-url
# The script configuration happens by passing
# it in text form with data-to-vm
#
# Format of data-to-vm is shell script:
#  URL=<url>\nWAITTIME=<seconds>\nTRIES=<amount>
#
# MANDATORY values in configuration:
#  * URL - the url of the script to download and test
# OPTIONAL values in configuration:
#  * WAITTIME - waiting time, before next try
#  * TRIES - amount of tries
#  * any other exported variables can be used by the deployment script
# note: you need to export the variables for them to be used in the deployment script

# Possible TODOs:
#  * post results on each try
#  * use function + trap to assure posting on exit

LOG_FILE=/var/log/test-script-deployment.log

wget -O /tmp/test-script.cfg."$$" -q http://10.0.2.100/data

source /tmp/test-script.cfg."$$"

if [ -z "$LOG_FILE" ] ; then
  echo "Output log file is missing"
  exit 1
fi

if [ -z "$URL" ] ; then
  echo "URL is missing." >> "$LOG_FILE" 2>&1
  exit 1
fi

export WAITTIME=${WAITTIME:-360}
echo "INFO: WAITTIME is $WAITTIME" >> "$LOG_FILE" 2>&1
export TRIES=${TRIES:-80}
echo "INFO: TRIES is $TRIES" >> "$LOG_FILE" 2>&1

DEPLOYMENT_SCRIPT=/tmp/test-script-deployment.bash."$$"
wget -O "$DEPLOYMENT_SCRIPT" -q "$URL"

if [[ ! -s "$DEPLOYMENT_SCRIPT" ]] ; then
  echo "exit 1" > "$DEPLOYMENT_SCRIPT"
fi

function add_log ()
{
  LOG_FILE="$1"
  for f in /opt/slapos/log/slapos-node-{software,instance}.log ; do
    echo "Tail of '$f':" >> "$LOG_FILE"
    tail -n 500 "$f" >> "$LOG_FILE"
  done
}

function add_checks ()
{
  LOG_FILE="$1"
  echo 'lsof -Pni' >> "$LOG_FILE" 2>&1
  lsof -Pni >> "$LOG_FILE" 2>&1
  echo 'iptables-save' >> "$LOG_FILE" 2>&1
  iptables-save >> "$LOG_FILE" 2>&1
  for f in /tmp/playbook-* ; do echo "$f" ; cat "$f"; echo; done >> "$LOG_FILE" 2>&1
  echo 'slapos node status' >> "$LOG_FILE" 2>&1
  slapos node status >> "$LOG_FILE" 2>&1
}
function upload ()
{
  try="$1"
  LOG_FILE="$2"
  add_log "$LOG_FILE"
  add_checks "$LOG_FILE"
  t=`date '+%Y%m%d%H%S'`
  mv "$LOG_FILE" ${LOG_FILE}."$t"
  curl -q -X POST --data-urlencode "path=test-script-result/log-file.log.$t" --data-urlencode "content@${LOG_FILE}.$t" http://10.0.2.100/
}

try=1
while true; do
  echo "$0: Try $try. Running '/bin/bash $DEPLOYMENT_SCRIPT'" >> "$LOG_FILE" 2>&1
  /bin/bash "$DEPLOYMENT_SCRIPT" >> "$LOG_FILE" 2>&1
  result="$?"
  if [ "$result" == 0 ] ; then
    echo "$0: Try $try. Script executed successfully." >> "$LOG_FILE" 2>&1
    upload "$try" "$LOG_FILE"
    break
  fi
  if (( try > TRIES )) ; then
    echo "$0: Try $try. Amount of tries $TRIES exceeded, giving up." >> $LOG_FILE 2>&1
    upload "$try" "$LOG_FILE"
    break
  fi
  # wait WAITTIME before checking the state
  echo "$0: Try $try. Sleeping $WAITTIME before retry." >> $LOG_FILE 2>&1
  upload "$try" "$LOG_FILE"
  sleep "$WAITTIME"
  ((try++))
done

exit "$result"
