#!/bin/bash
################################################################################
#    HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
################################################################################

INSTALL_DIR=/opt/HPCCSystems
CONFIG_DIR=/etc/HPCCSystems
ENV_XML_FILE=environment.xml
ENV_CONF_FILE=environment.conf
PID_DIR=/var/run/HPCCSystems
LOCK_DIR=/var/lock/HPCCSystems
LOG_DIR=/var/log/HPCCSystems
INIT_PATH=/etc/init.d


source  ${INSTALL_DIR}/etc/init.d/hpcc_common
which_pidof
set_environmentvars

if [ $# -lt 1 ]; then
    echo usage: $0 thorinstance 
    exit 1
fi

PATH_PRE=`type -path hpcc_setenv`
if [ -z "$PATH_PRE" ]; then
    # assume default conf location
    PATH_PRE=`cat ${CONFIG_DIR}/environment.conf | sed -n "/\[DEFAULT\]/,/\[/p" | grep "^path=" | sed -e 's/^path=//'`/sbin/hpcc_setenv
fi
source ${PATH_PRE} ""
is_user ${user}
DEPLOY_DIR=${INSTALL_DIR}/bin
ENVPATH=${CONFIG_DIR}/${ENV_XML_FILE}
RUN_DIR=`cat ${HPCC_CONFIG} | sed -n "/\[DEFAULT\]/,/\[/p" | grep "^runtime=" | sed -e 's/^runtime=//'`
INSTANCE_DIR=$RUN_DIR/$1

LOGPATH=`updtdalienv $ENVPATH -d log backupnode backupnode`
export logpthtail="`date +%Y_%m_%d_%H_%M_%S`"
export logfile="$LOGPATH/backupnode_${logpthtail}.log"
mkdir -p `dirname $logfile`

pid=`${PIDOF} backupnode`
if [ -n "$pid" ]; then
   log "stopping previous backupnode processes $pid"
   killall backupnode
fi

log "------------------------------"
log "starting backupnode ..."

if [ ! -e $INSTANCE_DIR ] ; then
  # perhaps they gave a full path?
  if [ ! -e $1 ] ; then
    echo "Usage: $0 thor_cluster_name"
    exit 1
  fi
  INSTANCE_DIR=$1
fi

cd $INSTANCE_DIR

PID_NAME="$PID/`basename $INSTANCE_DIR`.pid"
BACKUPNODE_DATA=`updtdalienv $ENVPATH -d data backupnode backupnode`
if [ -z "$BACKUPNODE_DATA" ]; then
    echo "cannot determine backupnode directory"
    exit 1
fi
. $INSTANCE_DIR/setvars

if [ ! -z ${THORPRIMARY} ]; then
    groupName=${THORPRIMARY}
else
    groupName=${THORNAME}
fi
daliadmin server=$DALISERVER dfsgroup ${groupName} $INSTANCE_DIR/backupnode.slaves
errcode=$?
if [ 0 != ${errcode} ]; then
    echo "failed to lookup dali group for $groupName"
    exit 1
fi

BACKUPNODE_DATA=$BACKUPNODE_DATA/last_backup
if [ "$localthor" == "true" ]; then
 BACKUPNODE_REMOTEDATA=$BACKUPNODE_DATA
else
 BACKUPNODE_REMOTEDATA=//$THORMASTER$BACKUPNODE_DATA
fi

mkdir -p $BACKUPNODE_DATA
rm -f $BACKUPNODE_DATA/*.ERR
rm -f $BACKUPNODE_DATA/*.DAT

log "Using backupnode directory $BACKUPNODE_DATA"
log "Reading slaves file $INSTANCE_DIR/backupnode.slaves"
log "Scanning files from dali ..."

NODEGROUP=$THORPRIMARY
if [ -z "$THORPRIMARY" ]; then
  NODEGROUP=$THORNAME
fi


$DEPLOY_DIR/backupnode -O $DALISERVER $NODEGROUP $BACKUPNODE_DATA >> $logfile 2>&1
if [ $? -ne 0 ]; then
  echo Backupnode failed - see $logfile
  exit 1
fi

# maximum number of threads frunssh will be permitted to use (capped by # slaves)
MAXTHREADS=1000

frunssh $INSTANCE_DIR/backupnode.slaves "killall backupnode" -i:$SSHidentityfile -u:$SSHusername -pe:$SSHpassword -t:$SSHtimeout -a:$SSHretries -n:$MAXTHREADS -b >> $logfile 2>&1
log "frunssh $INSTANCE_DIR/backupnode.slaves \"/bin/sh -c 'mkdir -p $LOGPATH; mkdir -p $INSTANCE_DIR; $DEPLOY_DIR/backupnode -T -X $BACKUPNODE_REMOTEDATA %n %c %a %x $2 > $LOGPATH/backup_node_%n_${logpthtail}.log 2>&1'\" -i:$SSHidentityfile -u:$SSHusername -pe:$SSHpassword -t:$SSHtimeout -a:$SSHretries -n:$MAXTHREADS -b >> $logfile 2>&1"
frunssh $INSTANCE_DIR/backupnode.slaves "/bin/sh -c 'mkdir -p $LOGPATH; mkdir -p $INSTANCE_DIR; $DEPLOY_DIR/backupnode -T -X $BACKUPNODE_REMOTEDATA %n %c %a %x $2 > $LOGPATH/backup_node_%n_${logpthtail}.log 2>&1'" -i:$SSHidentityfile -u:$SSHusername -pe:$SSHpassword -t:$SSHtimeout -a:$SSHretries -n:$MAXTHREADS -b >> $logfile 2>&1

log "------------------------------"
log "Waiting for backup to complete"

nohup backupnode -W $INSTANCE_DIR/backupnode.slaves $BACKUPNODE_DATA >> $logfile 2>&1 &
pid=`${PIDOF} backupnode`
trap "echo start_backupnode exiting, backupnode process $pid still continuing; exit 0" 2
sleep 5
if [ -n "$pid" ]; then
  tail --pid $pid -f $logfile 2>/dev/null
fi
