Monday, July 9, 2012

Automate if it adds value

"Automate if it adds value" is the driving force behind the creation if this short and simple shell script.

 

Though this script seems trivial but if we consider the task it is achieving, it is becomes important and its creation joyful. In our team we recently received hardware boxes to setup QA environment for testing. Usually the development environment and production releases something called release tool which is pretty much standard way of deployments. But for QA environment, since it is setup phase we have some constraints to setup the release tool. But we had decided in our retrospective that for every release from now on, we will complete QA testing using QA environment. Thus leading to QA environment setup gradually and leading to the requirement of QA deployments and this script. We plan for this with in our sprints. The DEV and QA, working on the story both take the ownership of setting up this QA environment bit for their release.

 

Now with this context, we had to make a temporary arrangement for deploying stuff to QA box. And I had already listed down a few sequential unix commands to achieve this. Eventually it became cumbersome, error prone and boring to repeat the same commands many times. So I thought of quickly writing a simple shell script to get this task done and make it interesting.

 

Three keywords:

 

1.       Simple

2.       Quick

3.       Interesting

 

I had seen many shell scripts in the past and executed them, opened them in VI and edited them but had never written them from scratch. So learning something new and of course achieving some automation makes it interesting. Initially when I started with the task, the simple requirements were; accept the application name, version and user details from command line and deploy to a standard path. As I progressed I added parameterization, error handling and positive and negative logging.

 

[Standard start of a shell script, shell and usage]

 

#!/bin/bash

if [ $# -lt 3 ];then

echo "Usage: ./qadeploy /appMainFolderName /appVersion userLogin";

exit

fi

 

[Printing some messages indicating the start and making clear the input values]

 

echo "Starting QA deployment for $1$2"

echo "App to be deployed is $1"

echo "App version to be deployed = $2"

echo "User = $3"

 

[Creating a temp directory because it is required to change the owner of files before final deployment]

 

echo "Creating temp directory /home/$3$1$2"

`mkdir -p /home/$3$1$2`

 

[Using the Secure Copy command for copying from remote location, it prompts for password]

 

echo "Starting to copy to temp location..."

`scp -r $3@dsos37a-0104.eu.hedani.net:/app/credit$1$2 /home/$3$1`

if [ $? -ne 0 ]; then

  echo "Error executing: scp"

  exit

fi

 

[Change the permissions of the copied contents so that the next copy or move command will change the owner of the files]

 

echo "Changing permission to 777"

`chmod -R 777 /home/$3$1`

if [ $? -ne 0 ]; then

  echo "Error executing: chmod"

  exit

fi

 

[Clear the already existing application contents]

 

echo "Removing previous entry for the app at location: /app/credit$1$2"

`rm -rf /app/credit$1$2`

if [ $? -ne 0 ]; then

  echo "Error executing: rm -rf /app/credit$1$2"

  exit

fi

 

[Make the final move towards the deployment and clearing the temporary contents]

 

echo "Moving the app from temp (/home/$3$1$2) to real location: /app/credit$1"

`mv /home/$3$1$2 /app/credit$1`

if [ $? -ne 0 ]; then

  echo "Error executing: mv /home/$3$1$2 /app/credit$1"

  exit

fi

 

[Deployment complete]

 

echo "Deployment complete for $1!"