Custom Search

ScriptLab - Holiday gift script

Got it go back Home

Distribute script: distribute_config.sh

Motivation:

If you are working on a cluster with many ENVs, and you need to distribute new configuration folder(with all content) to all of them.

you can use that script(also include backup for old directory files configuration).

Execute command:

"$(basename $0) "| tee -a $logfile>

"e.g. $(basename $0) CONFIG "| tee -a $logfile

More info:

Beside that you need to prepare also the account_list (under CONFIG folder)– file containing all the accounts in the cluster,

(for example crm1@hostname,crm2@hostname etc)And configuration(under CONFIG folder) – the directory folder you need to distribute

The script:

#!/usr/bin/ksh

PrintUsage()
{
   echo "Usage:" | tee -a $logfile
   echo "$(basename $0) <Name of directory for which need to distribute configuration> "| tee -a $logfile
   echo "e.g. $(basename $0) CONFIG_APP_1 "| tee -a $logfile
   exit 1
}

if [ $# -ne 1  ]
then
        PrintUsage
        exit
fi

export dirname=$1
export timestamp=$(date +%Y%m%d_%H%M%S)
export logfile=logs/${dirname}_$timestamp

if [ ! -d ${dirname} ]; then
     echo " CONFIG directory doesn't exist bye"| tee -a $logfile
     exit 1
fi

if [ ! -f ${dirname}/account_list_${dirname} ]; then
     echo " File containing list doesn't exist bye"| tee -a $logfile
     exit 1
fi

if [ ! -d ${dirname}/configuration ]; then
     echo " \"configuration\" directory inside ${dirname} doesn't exist bye"| tee -a $logfile
     exit 1
fi


 
echo "You are about to distribute ${dirname}/configuration to production."| tee -a $logfile
echo " "| tee -a $logfile

#############Check if directory exist###################
for instance in `cat ${dirname}/account_list_${dirname} | grep -v "#" | awk '{print $2"@"$1}'`
do
ssh $instance "if [ ! -d configuration ]; then ; echo "configuration directory does not exit on $instance" ; fi "| tee -a $logfile
done

echo "Please type YES to continue or NO to cancel the operation"| tee -a $logfile

read first_answer
if [ $first_answer != "YES" ] ; then
  exit 1
fi

echo " "| tee -a $logfile 
echo "Distributing ${dirname}/configuration to production"| tee -a $logfile
sleep 2

###Take backup of file######################
echo "Taking Backup of configuration folder"
for instance in `cat ${dirname}/account_list_${dirname} | grep -v "#" | awk '{print $2"@"$1}'`
do
ssh $instance "if [ ! -d configuration ]; then ;echo \"File not present on $instance\"; else ; cp -pr configuration configuration_$timestamp; fi" | tee -a $logfile
done

echo "Backup complete. And is present at:"
for instance in `cat ${dirname}/account_list_${dirname} | grep -v "#" | awk '{print $2"@"$1}'`
do
echo $instance | tee -a $logfile
ssh $instance "ls -ld configuration_$timestamp; " | tee -a $logfile
done

echo "Please type YES to continue or NO to cancel the operation"| tee -a $logfile

read first_answer
if [ $first_answer != "YES" ] ; then
  exit 1
fi


######Distribute folder #####################3

for instance in `cat ${dirname}/account_list_${dirname} | grep -v "#" | awk '{print $2"@"$1}'`
do
ssh $instance "mv configuration configuration_$$"
scp -prd ${dirname}/configuration $instance:.
if [ $? = 0 ];then
    echo "Tranfer successful on $instance. Removing old configuration" | tee -a $logfile
    ssh $instance "rm -rf  configuration_$$"
else
    echo "Transfer is not successful. Not deleting configuration folder. It is present with name configuration_$$" | tee -a $logfile
fi
done


###Count files###

for instance in `cat ${dirname}/account_list_${dirname} | grep -v "#" | awk '{print $2"@"$1}'`
do
echo "Total number of files in configuration on $instance:"| tee -a $logfile
ssh $instance "find configuration -type f | wc -l"| tee -a $logfile
done