1
votes

I am currently working on creating bash scripts to automate the process of doing a clean install and clean uninstall of Bamboo remote agents on a windows 7 VM.

These are my two scripts:

Clean install:

#!/bin/bash 

#Main function
main(){

    URL=https://bamboo.ihs.organization.net/agentServer/agentInstaller/atlassian-bamboo-agent-installer-5.5.0.jar
    mkdir -p install-directory
    #download jar
    cwd=$(pwd)
    echo "INFO - attempting to download jar file to:"
    echo $cwd
    echo "..."
    echo ""
    wget -nc --tries=10 $URL
    cwd=$(pwd)
    if [ $? -eq 1 ]
    then
        echo "ERROR - Failed to download jar file from $URL. It may have changed in the meantime."
        exit 1
    else
        echo "INFO - Succeeded! Jar file downloaded to LOCATION"
    fi
    echo "INFO - Installing bamboo agent..."

    #execute jar
    echo "INFO - unpacking jar file..."
    cwd=$(pwd)
    win_cwd=$(cygpath -w ${cwd})
    echo $win_cwd
    java -Dbamboo.home=$win_cwd/install-directory/ -jar atlassian-bamboo-agent-installer-5.5.0.jar https://bamboo.ihs.organization.net/agentServer/

    if [ $? -eq 1 ]
    then
        echo "ERROR - Failed to unpack jar file."
        exit 1
    else
        echo -n "INFO - Succeeded! Jar file unpacked to "
        echo $cwd/install-directory
    fi
}
main

(basically downloads atlassian-bamboo-agent-installer-5.5.0.jar and installs it into a folder called "install-directory")

Clean uninstall:

 #!/bin/bash 

#Main function
main(){
    cwd=$(pwd)
    install_dir="$cwd""/install-directory"
    win_install_dir=$(cygpath -w ${install_dir})
    if [ -d $install_dir ]; then
        echo -n "INFO - "
        echo -n $install_dir
        echo " detected. "
        cd "$install_dir"
        cd "bin"
        STOP_SCRIPT="StopBambooAgent-NT.bat"
        UNINSTALL_SCRIPT="UninstallBambooAgent-NT.bat"
        cmd /c "$win_install_dir"/bin/"$STOP_SCRIPT"
        cmd /c "$win_install_dir"/bin/"$UNINSTALL_SCRIPT"
        if [ $? -eq 1 ]        
            then
            echo -n "ERROR - '"
            echo -n "$UNINSTALL_SCRIPT" 
            echo "' failed. See output above."
            exit 1
        fi
        cd "../.."
        echo -n "INFO - attempting to remove"
        echo -n $install_dir
        echo "... "
        rm -rf $install_dir
        if [ $? -eq 1 ]        
            then
            echo -n "ERROR - removing '"
            echo -n "$install_dir" 
            echo "' failed. See output above."  
        else
            echo -n "INFO - removing '"
            echo -n "$install_dir" 
            echo "' succeeded!"
        fi
    else
        echo -n "ERROR - "
        echo -n $install_dir
        echo " is expected to exist."
        exit 1
    fi

}   

main

(basically calls StopBambooAgent-NT.bat, then UninstallBambooAgent-NT.bat, the deletes the "install-directory" folder)

The sequence of steps I have been doing:

Run ./clean-install.sh. At this point the agent works, I can go to the server and see it, run jobs on it, etc.

There is a terminal window open with this as output:

****************************************************************                                                                                                    ********************************************************************************                                                                                                    ***********
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] *                                                                                                                                                                                                                                                                                                                                                                 *
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] * Bamboo agent '10.0.2.15 (12)' ready to receive builds.
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] * Remote Agent Home: C:\Users\vagrant\bamboo-agent-dev\install-d                                                                                                    irectory
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] * Broker URL: failover:(tcp://bamboo.ihs.organization.net:54663?wir                                                                                                    eFormat.maxInactivityDuration=300000)?initialReconnectDelay=15000&maxReconnectAt                                                                                                    tempts=10
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] *                                                                                                                                                                                                                                                                                                                                                                 *
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,573 INFO [Thread-1                                                                                                    ] [RemoteAgent] ****************************************************************                                                                                                    ********************************************************************************                                                                                                    ***********
INFO   | jvm 1    | 2015/01/27 11:52:07 | 2015-01-27 03:52:07,588 INFO [QuartzSc                                                                                                    heduler_Worker-1] [AgentHeartBeatJob] executableBuildAgent still unavailable. He                                                                                                    artbeat skipped.

I cntrl-C out of this, and the agent is still there on the server, and I can still configure it, run jobs on it, etc. (At least it seems like that is the case.)

I then stop all the build jobs on the agent and try to uninstall it, and here is where the problem is.

The problem:

Basically when I try to run the uninstall script I get this error message:

INFO - /cygdrive/c/Users/vagrant/bamboo-agent-dev/install-directory detected.
ERROR  | wrapper  | 2015/01/27 11:41:42 | The bamboo-remote-agent service is not installed - The specified service does not exist as an installed service. (0x424)
Press any key to continue . . .

This corresponds to StopBambooAgent.bat being run by the uninstall script.

Furthermore when I try to delete the "install-directory" folder I also get lots of the following error messages (one for each file):

rm: cannot remove ‘/cygdrive/c/Users/vagrant/bamboo-agent-dev/install-directory/bin’: Device or resource busy

indicating to me that I have not stopped the agent properly. These files seem to be held open by a java JVM instance.

Why is it telling me the agent is not installed when it is? Am I trying to stop the remote agent correctly? How can I stop the remote agent?

Thanks very much.

1
$? is the return code of the most recent command. So when you cwd=$(pwd) between wget and the if check you lose the return value of wget. You also don't need that cwd assignment again since you just did it a few lines before that (and haven't changed directory since). You can also likely just use $PWD instead of the pwd command. - Etan Reisner
When automating bamboo installations on linux at work I found that not letting the agent start itself automatically the first time was a better idea. That is using install as an argument to the initial java command and then using the service to start it later was a better installation plan. - Etan Reisner
Hi Etan, thanks for the comments, that parameter was exactly what I was looking for, thanks a million! - OnMyWayToGodDontKnow

1 Answers

1
votes

Via @Etan Reisner

I wasn't installing the bamboo agent as a windows service, despite that being my intention, and that is why the scripts weren't working. To fix this I passed in the installntservice in the java command in the install script.

(See: https://confluence.atlassian.com/display/BAMBOO056/Additional+remote+agent+options#Additionalremoteagentoptions-ntservice)