0

I am facing an error while uploading a war file to the staging environment. Have written a shell script which uses eb cli to deploy artifact to the environment.

config.yml looks like this:

branch-defaults:
  default:
    environment: testseries-stag-env
deploy:
  artifact: temp/servercp/testseries/testseries-service/target/testseries-service-1.0.0.war
environment-defaults:
  testseries-stag-env:
    branch: null
    repository: null
global:
  application_name: testseries
  default_ec2_keyname: testseries-stag
  default_platform: arn:aws:elasticbeanstalk:us-east-1::platform/Tomcat 8 with Java
    8 running on 64bit Amazon Linux/2.7.5
  default_region: us-east-1
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: null
  sc: null
  workspace_type: Application

build.sh looks like this:

#!/usr/bin/env bash

# Takes a fresh clone of the dependent repositories and installs them locally.
# Deploys test-series to staging env. See .elasticbeanstalk for more details on this
# Access key Id and secret must be configured in the system to use this shell script
# If you have key and secret with you but system is not configured with it, this script will do it for you. You will
# need to provide the values when requested

# This may contain system dependent bugs. Please report any to devops@adda247.com

timestamp() {
  date +"%d-%h_%H:%M:%S"
}

function installHomeBrew()
{
   /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
}

function installEbCliMac()
{
    brew install awsebcli
}

function installEbCliLinux()
{
    pip install --upgrade --user awsebcli
}

function installPip()
{
    curl -O https://bootstrap.pypa.io/get-pip.py
    if [[ -x $(command -v python) ]]; then
        echo "Python found"
        python get-pip.py --user
    else
        if [[ -x $(command -v python3) ]]; then
            echo "Python3 found"
            python3 get-pip.py --user
        else
#           Only valid for nix instances which support python3. Fails on other systems
            echo "Python not found. Installing..."
            sudo apt-get install python3
            python3 get-pip.py --user
        fi
    fi
    echo "Installing pip with python"
    pip install --upgrade --user pip
}

function installAWSCLI()
{
    if [[ ${OSTYPE} == 'linux-gnu' ]]; then
        pip install awscli --upgrade --user
    elif [[ ${OSTYPE} == 'darwin'* ]]; then
        brew install awscli
    fi
}

function ifEnvVariablesExist()
{
    if [[  -z ${AWS_ACCESS_KEY_ID} || -z ${AWS_SECRET_ACCESS_KEY} ]]; then
        echo false
    else
        echo true
    fi
}

function ifConfigExists()
{
    if [[ -e ${HOME}/.aws/credentials ]]; then
        echo true
    else
        echo false
    fi
}

function deployWar()
{
    version_label=testseries-dev-$(timestamp)
    eb deploy --label=${version_label}
}

function credentialsExist()
{
    env_var=$(ifEnvVariablesExist)
    if [[ ${env_var} == true ]]; then
        echo "reading from environment variables"
    else
        config_file=$(ifConfigExists)
        if [[ ${config_file} == true ]]; then
            echo "reading from config file"
        else
            echo "Neither environment variables nor config file found"
            echo "******   Configuring AWS for you. Please enter aws id and secret when prompted  ******"
            aws_exists=$(command -v aws)
            if [[ -x ${aws_exists} ]]; then
                echo "aws cli is present. Configuring"
            else
                echo "aws cli is absent. Installing"
                installAWSCLI
            fi
            aws configure
            if [[ $? -ne 0 ]]; then
                echo "Unsuccessful configuration. Exiting"
                exit $?
            fi
        fi
    fi
}

function proceedForMac()
{
    eb_cli_exists=$(command -v eb)
    if [[ -x ${eb_cli_exists} ]]; then
        echo "eb cli is present"
    else
        echo "eb cli is absent. Installing with HomeBrew"
        brew_exists=$(command -v brew)
        if [[ -x ${brew_exists} ]]; then
            echo "HomeBrew is present"
        else
            echo "HomeBrew is absent. Installing"
            installHomeBrew
        fi
        installEbCliMac
    fi

    credentialsExist
    deployWar
}

function proceedForLinux()
{
    eb_cli_exists=$(command -v eb)
    if [[ -x ${eb_cli_exists} ]]; then
        echo "eb cli is present"
    else
        echo "eb cli is absent. Installing with pip"
        pip_exists=$(command -v pip)
        if [[ -x ${pip_exists} ]]; then
            echo "pip is present"
        else
            echo "pip is absent. Installing"
            installPip
        fi
        export PATH=~/.local/bin:$PATH
        if [ -f ~/.bash_profile ]; then
            source ~/.bash_profile
        fi
        installEbCliLinux
    fi

    credentialsExist
    deployWar
}

WD=$(pwd)
BUILD_DIR=$(pwd)/temp

if [[ -d ${BUILD_DIR} ]]; then
    rm -rf ${BUILD_DIR}
fi

mkdir ${BUILD_DIR}
chmod 777 ${BUILD_DIR}

cd ${BUILD_DIR}
SSH_KEY_PATH=~/.ssh/id_rsa.pub

if [[ -f ${SSH_KEY_PATH} ]]; then
    git clone git@github.com:metiseduventures/servercp.git
else
    git clone https://github.com/metiseduventures/servercp.git
fi


echo "Building servercp"
cd ${BUILD_DIR}/servercp/testseries
git checkout dev
mvn clean install
if [[ $? -ne 0 ]]; then
    echo "Project build unsuccessful. Please correct testseries project in servercp and run again"
    exit $?
fi

echo "Build Successful. Deploying Artifact"
if [[ ${OSTYPE} == 'linux-gnu' ]]; then
    proceedForLinux
elif [[ ${OSTYPE} == 'darwin'* ]]; then
    proceedForMac
fi

eb deploy command is in deployWar function where it fails

Folder structure is

/--* .elasticbeanstalk/config.yml
   * build.sh

The build.sh shell script clones the repository inside temp folder and builds the project which completes successfully

Console Output after running is:

Build Successful. Deploying Artifact
eb cli is present
reading from config file
ERROR: Application Version does not exist locally 
(temp/servercp/testseries/testseries-service/target/testseries-service- 
1.0.0.war). Try uploading the Application Version again.

However, temp/servercp/testseries/testseries-service/target/testseries-service-1.0.0.war exists alright.

If I immediately run eb deploy --version-label="my-label" after that, artifact gets deployed alright. But when same thing is being executed from inside a script, it gives the above error. I tried sleeping for 2 secs before eb deploy command, but still nothing

Any help appreciated. Thanks in advance

Nikhil Sahu
  • 2,463
  • 2
  • 32
  • 48
  • Just to make sure -- is `temp/path/to/testseries-service-1.0.0.war` a path on your computer? Your "Folder structure" indicates you only have `elasticbeanstalk` and `build.sh`. – progfan Jul 04 '18 at 18:18
  • yes the script creates `temp` folder parallel to `build.sh` then clones the repo and builds the specific project – Nikhil Sahu Jul 05 '18 at 12:01
  • Okay, can you upload the related sections of your `build.sh` -- where it performs `eb deploy` and clones `temp`? – progfan Jul 05 '18 at 16:43
  • Updated question to include build.sh – Nikhil Sahu Jul 06 '18 at 08:39

2 Answers2

2

I think the problem is due to the location from where your eb deploy (without --label) is happening. With cd ${BUILD_DIR}/servercp/testseries, you are somewhere deep inside your project, whereas temp/.../*.war is a path relative to the root of your project. I think a cd ${WD} just before the eb deploy should fix your problem.

progfan
  • 2,454
  • 3
  • 22
  • 28
0

What's your build.sh's content? I think maybe you could give it a sleep 2 break before the real deploy command?

ddwolf
  • 91
  • 2
  • 10
  • Yeah, I was thinking to try this. But still that should not be required. Lemme see if that works. – Nikhil Sahu Jul 05 '18 at 12:00
  • didn't work. Same error :/ . Have updated question to include build.sh – Nikhil Sahu Jul 06 '18 at 08:38
  • 1
    Before the eb deploy command, do a check, check if the file exists?if not, then check the file existence after the git clone command. Something went wrong, let's locate where it happened. – ddwolf Jul 10 '18 at 01:30
  • Got the issue. Seems i am going into the project to build it so the deploy command is executing inside the `testseries` folder but not from the path of deploy script. I saved the current directory and moved to it before executing eb deploy. This uploaded it alright. – Nikhil Sahu Jul 10 '18 at 16:29