Friday, August 17, 2018

Linux - How to remotely call a script with parameter over ssh

Below is an example on how to remotely call a bash script with a parameter over ssh.  I've done this multiples times in the past, but decided to document it this time so next time I can just reference this post.  This assumes that ssh to remote server passwordlessly already works with the intended user.

The example here performs an automated database script deployment for the sprint branch that is currently being worked.  Atlassian bamboo is being used for CI, but the DB script part was still being done manually.  This was a home-grown solution, the SQL for Bamboo add-on was not available and not used.

Environment: 
Source and Target DB server both centos-release-6-8.el6.centos.12.3.x86_64

The bamboo server was already configured with SSH Task's that called scripts to download the source code for the sprint branch on the source server.  So by the time the cherryshoe_db_copy_deploy.sh script is called, the database scripts files already reside on the source server.  We need the sprint number passed as a parameter because we have to know which sprint folder to copy.  This script then preps folders on the DB server (cherryshoe_db_prep.sh), copies the DB scripts, and runs scripts on the DB server (cherryshoe_db_deploy.sh).

cherryshoe_db_copy_deploy.sh
#!/bin/sh
usage () {
  echo "Usage (sprint number)"
}

SPRINT_NUMBER=$1
if [ -z "$1" ]
  then
   usage
   exit 1
fi

BUILD_HOME=/opt/app/cherryshoe/cherryshoe
BUILD_DIR=/opt/app/cherryshoe/Deploy
REMOTE_USER=cherryshoeuser
REMOTE_MACHINE=10.21.14.72
REMOTE_PATH_TO_SCRIPT=/opt/app/cherryshoe/build/database/automated_deployment/TEST

echo SPRINT_NUMBER $SPRINT_NUMBER
echo GIT_USER $GIT_USER
echo BUILD_HOME $BUILD_HOME
echo REMOTE_USER $REMOTE_USER
echo REMOTE_MACHINE $REMOTE_MACHINE
echo REMOTE_PATH_TO_SCRIPT $REMOTE_PATH_TO_SCRIPT
###########################################################

cd $BUILD_HOME

echo Call remote script to create sprint DB automated deployment folder if not exists
ssh $REMOTE_USER@$REMOTE_MACHINE "cd $REMOTE_PATH_TO_SCRIPT;./cherryshoe_db_prep.sh $SPRINT_NUMBER"

echo Copy current branch DB scripts to DB server
cd $BUILD_HOME
cd database/scripts/release_scripts/$SPRINT_NUMBER
scp *.sql $REMOTE_USER@$REMOTE_MACHINE:$REMOTE_PATH_TO_SCRIPT/$SPRINT_NUMBER
scp *.sh $REMOTE_USER@$REMOTE_MACHINE:$REMOTE_PATH_TO_SCRIPT/$SPRINT_NUMBER

echo Call remote script to perform deployment of db scripts
ssh $REMOTE_USER@$REMOTE_MACHINE "cd $REMOTE_PATH_TO_SCRIPT;./cherryshoe_db_deploy.sh $SPRINT_NUMBER"

echo DONE

cherryshoe_db_prep.sh
#!/bin/sh

if [ -z "$1" ]
  then
    echo "No sprint folder specified"
    exit 1
fi

SPRINT_FOLDER=$1
echo SPRINT_FOLDER: $SPRINT_FOLDER

DB_SCRIPT_DEPLOY_HOME=/opt/app/cherryshoe/build/database/automated_deployment/TEST

echo  Create sprint DB automated deployment folder if not exists.
cd $DB_SCRIPT_DEPLOY_HOME
mkdir -p $SPRINT_FOLDER

echo DONE

cherryshoe_db_deploy.sh
#!/bin/sh

if [ -z "$1" ]
  then
    echo "No sprint folder specified"
    exit 1
fi


SPRINT_FOLDER=$1
echo SPRINT_FOLDER: $SPRINT_FOLDER

DB_SCRIPT_DEPLOY_HOME=/opt/app/cherryshoe/build/database/automated_deployment/TEST
DB_USERNAME=cherryshoe
DB_PASSWORD=cherryshoe
DB_SCHEMA=cherryshoetest

echo changing to sprint folder: $DB_SCRIPT_DEPLOY_HOME/$SPRINT_FOLDER
cd $DB_SCRIPT_DEPLOY_HOME/$SPRINT_FOLDER

BASHFILE=$(ls | grep .sh)
echo BASHFILE: $BASHFILE

echo check if bash file exists
if [ -e "$BASHFILE" ]
then
  echo make sprint script file readable and executable
  chmod +rx *.sh

  echo executing bash file
  ./$BASHFILE $DB_SCHEMA $DB_USERNAME $DB_PASSWORD
else
  echo bash file does not exist, SKIPPING
fi

echo DONE



This article helped a lot.

No comments:

Post a Comment

I appreciate your time in leaving a comment!