ShellScript Project Files

scripts/utils.sh

#!/bin/bash

# will print the error code and return from script
# will take two args.
# 1 = error code
# 2 = error message.
. ./scripts/variables.sh

function print_exit(){
    local error_code=${1}
    local error_msg=${2}
    echo -e "${RED}[Fail] ${error_msg} ${NOCOLOR}" 1>&2
    exit ${error_code}
}

function showBanner(){
    banner_file=${1}
    cat ${banner_file}
}

function showProgress(){
    local last_command_pid=${1}
    while ps | grep -i "${last_command_pid}" > /dev/null
    do
        for i in '-' '\' '|' '/'
        do
            echo -ne "\b${i}"
            sleep 0.20
        done
        echo -en "\b"
    done
}

function installPackage() {
    local packageName=${1}
    apt-get install -y ${packageName} > /dev/null &
    last_command_pid=$!
    showProgress ${last_command_pid}
    wait ${last_command_pid} || print_exit 1 "not able to install ${packageName}."
}

function mavenTarget(){
    local mavenCmd=${1}
    mvn ${mavenCmd} > /dev/null &
    last_command_pid=$!
    showProgress ${last_command_pid}
    wait ${last_command_pid} || print_exit 1 "${mavenCmd} fail."
}

scripts/variable.sh

# colors variables.
RED='\033[0;31m'
GREEN='\033[0;32m'
NOCOLOR='\033[0m'

setup.sh

#!/bin/bash
#####
# this script will setup this project.
# run ./setup.sh to run this project.
#####
# Include files.
. ./scripts/utils.sh
. ./scripts/variables.sh



function clean_up(){
    if rm -rf ./target
    then
        echo -e "${GREEN}clean up successfull.${NOCOLOR}"
    else
        echo -e "${GREEN}not able to do clean up.${NOCOLOR}"
    fi
}

trap "clean_up;exit 2" 2

showBanner scripts/banner.txt

if [[ $UID != 0 ]]
then
    print_exit 1 "user is not a root user"
fi

read -p "please enter access path " APP_CONTEXT
APP_CONTEXT=${APP_CONTEXT:-app}

apt-get update > /dev/null &
last_command_pid=$!
showProgress ${last_command_pid}

wait ${last_command_pid} || print_exit 1 "not able to update the repository."


installPackage maven
installPackage tomcat9
mavenTarget test
mavenTarget package

if cp -rf target/hello-world-0.0.1-SNAPSHOT.war /var/lib/tomcat9/webapps/${APP_CONTEXT}.war
then
    echo "application Deployed successfully. you can access it on http://{IPADDRESS}/${APP_CONTEXT}"
else
    print_exit 1 "not able to Deploy the application."
fi

# Clean Up code.

clean_up
exit 0

Project Source Code

Click Here for project source Code

Demo Video

Click Here for Demo Video