1

I am using the bash trap to make sure one function runs at any cost. I know trap is not specific to exitO or 1. Here is what I have done.

#!/bin/bash

set -e

#array to store server and deployed status
declare -A server_deployed


#path to file containing the server inventory 
    readonly filepath="/var/jenkins_home/workspace/server_list.txt"
    #array to store to list of IPS
    declare -A result #associative array

    if [[ $TARGET == "ALL" ]]; then
        while read line ; do
           server_name=` echo $line | cut -d= -f1 `
           result+=(["$server_name"]=${line#*=})
        done < $filepath
    else
       singleserver=`cat $filepath | grep "$TARGET"`
       server_name=`echo $singleserver| cut -d= -f1 ` # get the servername 
       serverip=`echo $singleserver| cut -d= -f2 ` # get tje server ip
       result+=(["$server_name"]=$serverip) # gets the ip which is after equalto
     fi


#function to send slack notification everytime
function sendMessage(){

    for sd in "${!server_deployed[@]}"
      do
        echo "#########################################################"
         echo "Sucecssfully deployed on $sd" 
         echo "#########################################################"
     done

   #let find the unsuccess list ,for which we need to find the array diff
   for server_name in "${!result[@]}"
     do
       for sd in "${!server_deployed[@]}"
         do
           if [[ "$server_name" != "$sd" ]]; then
             echo "Failed to deploy on $server_name"
           fi
        done
     done
}

trap "sendMessage" INT EXIT

for server in "${!result[@]}"
  do

    upstream="MBM-TEST-ADMIN-BUILD"
    #bundle filename
    bundle="mbm_admin_dist"

    name=$server
    instance=${result[$name]}

    #------copying the hash.php in build job to the deployed server home location
    # ssh and move to desired location
     sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /tmp/test/admin_hash.json ubuntu@$instance:/home/ubuntu/

    mbmcms_workspace="/var/jenkins_home/workspace/$upstream"
    cd $mbmcms_workspace


    #remove if exist
    if [ -e "$bundle.zip" ]
    then
        echo "Already Exist so Removing it First !!"
        rm -f  $bundle.zip
    fi

    #check if the dist folder exist that comes from successful build
    if [ ! -d "dist" ]; then
      echo "--------------------------------------------------------------"
      echo "The dist folder does not exist, Please run BUILD job First !!"
      echo "--------------------------------------------------------------"
      exit 1
    fi

    #zip the file from the TEST & Build jobs 
    echo "starting to zip mbm-admin dist file created after test and build success!!"
    zip --symlinks -x *.git* -r $bundle ./dist



    #---------------------------------------------------------------
    # copy development.php if non-production else copy production.php
    # the file goes to the view/cms/
    #---------------------------------------------------------------
    sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null development.php ubuntu@$instance:/home/ubuntu
    sudo scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null production.php ubuntu@$instance:/home/ubuntu


    #copy the bundle file to instance
    scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r $bundle.zip ubuntu@$instance:/home/ubuntu/


    ssh -tt -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@$instance '

    SOURCE_FOLDER='/home/ubuntu/mbm-admin/dist/'
    DESTINATION_FOLDER='/home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console/dist/'

    rm -rf mbm-admin
    mkdir -p mbm-admin
    mv mbm_admin_dist.zip mbm-admin
    cd mbm-admin


    unzip mbm_admin_dist.zip
    rm -f mbm_admin_dist.zip



    sudo rsync -arz --force --delete --progress  $SOURCE_FOLDER $DESTINATION_FOLDER

    #moving the copied file to the /app/views/admin/admin_console
    sudo mv /home/ubuntu/development.php /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console
    sudo mv /home/ubuntu/production.php /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console


    #moving the hash.php to the desired location
    sudo cp -fv /home/ubuntu/admin_hash.json /home/ubuntu/wpdata/plugins/manager/app/views/admin/admin_console/hash.json
    '
    server_deployed+=(["$server"]="SUCCESS")

done

Here is the trap , as seen the script section

trap "sendMessage" INT EXIT

I need to run the sendMessage function whenever some exit code is detected or the program exits due to any error.

Problem: When I put the exit 1 at the end of the script the function gets called by the trap but suppose, If I put it somewhere in the middle or exactly after the main for loop starts the trap does not catch the exit code.

What am I not understanding or missing exactly in here?

Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
  • Try moving the `trap` command to the top of the script. – choroba Mar 24 '19 at 10:49
  • That ain't solving the issue too @choroba – Tara Prasad Gurung Mar 24 '19 at 10:52
  • 1
    Unrelated to your question, the attempt to nest single quotes in the last `ssh` is an error, but probably harmless. – tripleee Mar 24 '19 at 10:55
  • 1
    Change your second line to: `set -euo pipefail`. That's the unofficial strict mode for bash - making your script safer and easier to develop. `-e exit on first error`, `-u exit when an undefined variable such as $FO0 is accessed`, `-o pipefail exit when | any |cmd | in | a | pipe has exitcode != 0`. Adding `x`to `set` will print all commands for debugging. `set -euxo pipefail` – Michael D. Mar 24 '19 at 11:39

0 Answers0