Remote agent scripts and use cases

Upgrades occur in two separate steps, as agents may need restarts and other long running operations.

See sample scripts in /agent-side-scripts for more working samples.

 

Upgrade?

  1. The agent may be told NO, or to WAIT.  Waiting should check back more often based on the duration of maintenance and number of agents rotating. 
  2. When told YES, the agent should check the VERSION to target
  3. When told YES the agent should check BUSY, and keep polling /agent/<id> for the status until it is idle.

Fully automated CI/CD release pipelines and rolling upgrades for bamboo remote agents
Release new agent capabilities with confidence!

This example uses the maintenance APIs and capabilities APIs and a deployment task to enable an end-to-end delivery pipeline for your bamboo remote agents.

Publish agent configuration through a build pipeline and publish the updates. Agents then completely tear down and rebuild themselves from source at specified target version whether using docker, chef, ansible, etc.

See Fully automated CI/CD release pipelines and rolling upgrades for bamboo remote agents for example scripts.

 

 

 Click here to expand for < v1.5 examples
#!/bin/bash
#
#  This script should be triggered by crontab or similar scheduler on regular interval (i.e, Daily)
#   It will check in with the master server, and execute "UPGRADE_JOB" if allowed.
#



#
#   CHANGE THIS STUFF
#
CORE_DOMAIN="localhost:6990/bamboo" #domain, port and context without environment prefixes.
## change case statement on line 41 if your prefixes are not dev-,test-,perf- or you don't use https.

UUID="7ba167aa-631c-4771-b197-fcb459cc7fd7"

SIBLING_PATIENCE_TIME=600 # will wait 10 minutes
SIBLING_PATIENCE_COUNT=18 # will repeat 6 times. 




#
# Edit this to do whatever is considered a local upgrade.
# consider lifecycle, its very likely this running shell will be terminated a part of upgrade.
# SOMETHING will need to tell the master server everything is OK once back online
#  /rest/agent/latest/mothermayi/finish/<task_ID>.text
#
upgradeLocalAgent(){

    # save taskID to a file we can use after restart
    cp $TMPFILE/state.txt ~/finishTaskOnStartup.txt


    # call commands, chef, puppet,REAL WORK, etc here.
    echo "beep bop, upgrading and saving task ID to file."

}



#
#  DON'T CHANGE THIS STUFF
#  (unless you know why :) )
#

echo "Running Upgrade Check for Agent $agentId"



#harvest host name to know environment, and agent ID from config.
agentId=`cat /opt/atlassian/bamboo_home/bamboo-agent.cfg.xml | grep -oPm1 "(?<=<id>)[^<]+"`

thisbox=`hostname`
envKey=${thisbox:4:1}
case "$envKey" in
    d)
    bambooUrl=https://dev-${CORE_DOMAIN}
    ;;
    t)
    bambooUrl=https://test-${CORE_DOMAIN}
    ;;
    f)
    bambooUrl=https://perf-${CORE_DOMAIN}
    ;;
    p)
    bambooUrl=https://${CORE_DOMAIN}
    ;;
    e)
#local testing
    bambooUrl=http://${CORE_DOMAIN}
    ;;
    *)
    echo "Unable to decipher environment from host $thisbox"
    exit 2
    ;;
esac


 
# we use function recursion, and track attempts to break out.
let attempts=1
checkBambooMaster(){
    curl -H "uuid: ${UUID}" -X POST -k -b $TMPFILE/cookies "$bambooUrl/rest/agent/latest/mothermayi/upgrade/$agentId.text" -o $TMPFILE/state.txt 2>/dev/null


    # MOnitor status until the agent is idle
    source $TMPFILE/state.txt
    if [ "$PCODE" == "YES_CHILD" ]
    then
        #server says we can upgrade, make sure we are idle.
        echo "Master server says I can upgrade to version ${PVERSION}"
        if [ "$BUSY"  == "true" ]; then
            printf "\tAgent is still running a job, waiting ..\n" 
            # while polling, and is still running, slee
            running=1
            while [ $running -eq 1 ]
            do
                sleep 60
                curl -k -b $TMPFILE/cookies "$bambooUrl/rest/agent/latest/$agentId/text" -o $TMPFILE/state.txt 2>/dev/null
                source $TMPFILE/state.txt

                if [ "$BUSY"  == "false" ]; then
                    printf "\tYay, agent is now idle!\n"
                    break
                else
                    printf "\tstill busy..\n"
                fi
            done
        fi
    elif [ "$PCODE" == "WAIT_FOR_SIBLINGS" ]
    then
        # allowed to upgrae, but too many others are working right now, check back in a few

        echo "Master server wants me to wait, this is my $attempts attempt."
        echo "${PCODE}: ${PMESSAGE}"
        if [ $attempts -gt $SIBLING_PATIENCE_COUNT ]
        then
            echo "Siblings have exhausted my patience. INcrease wait times, offset cycles, or increase concurrency"
            exit 9
        fi
        let attempts+=1
        sleep $SIBLING_PATIENCE_TIME
        checkBambooMaster #will recurse back into this functuion
    elif [ "$PCODE" == "NO_CHILD" ]
    then
        echo "Master server says I can not upgrade now."
        echo "$PCODE: $PMESSAGE"
        exit 0
    elif [ "$PCODE" == "UH_OH" ]
    then
        echo "ERROR:  Master server is reporting an issue."
        echo "$PCODE: $PMESSAGE ,  existing Task ID: $TASK"
        exit 0
    else

        echo "ERROR:  I don't understand server response!"
        cat $TMPFILE/state.txt
        exit 9
    fi

}









#check for required libraries/tools and setup tempdir
command -v curl >/dev/null 2>&1 || { echo "Required tool 'curl' not found" ;exit 2; }
scriptname=`basename $0`
TMPFILE=`mktemp -d /tmp/requestUpgrade.XXXXXX` || exit 1





# disable agent in bamboo
echo 
echo "Checking upgrade rules on master server ${bambooUrl}"
# rest endpoint is anonymous, but needs valid cookie set by UI...
curl -k -c $TMPFILE/cookies "$bambooUrl" > /dev/null 2>&1


# begin check
checkBambooMaster



# assume success as function above exits on NOs. So call out heavy lifting function for maintenance.
echo
echo "Agent is disabled and idle, starting upgrade"


upgradeLocalAgent

Finish

#Use the file save above to send TASK id back to server and enable the agent
 
curl -H "uuid: ${UUID}" -X POST -k -b $TMPFILE/cookies "$bambooUrl/rest/agent/latest/mothermayi/finish/${TASK}.text" -o $TMPFILE/state.txt 2>/dev/null