Skip to content

Instantly share code, notes, and snippets.

@erick-thompson
Created October 4, 2017 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erick-thompson/0f1a6e855f3fbf5784799a56c149e098 to your computer and use it in GitHub Desktop.
Save erick-thompson/0f1a6e855f3fbf5784799a56c149e098 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ---------------------------------------------------------------------
# TeamCity build agent bootstrap script
# ---------------------------------------------------------------------
# Parameters:
#
# $1 required: TeamCity Server URL
# $2 required: TeamCity Agent installation path
# $3 required: Authorization token. Must be "-1" if no auto registration required
# $4 optional: Agent account
# $5 optional: Account password
#
# ---------------------------------------------------------------------
clean(){
if [ "ALL" == "$1" ]; then
script=true
agent=true
elif [ "SCRIPT" == "$1" ]; then
script=true
elif [ "AGENT" == "$1" ]; then
agent=true
fi
echo "Cleaning temporary installation's resources..."
cd "$RUNNING_FOLDER"
if [ "$script" = true ]; then
echo "Removing '$RUNNING_SCRIPT'"
rm -f "$RUNNING_SCRIPT" 2>&1
fi
if [ "$agent" = true ]; then
echo "Removing '$AGENT_INSTALLATION_FOLDER'"
rm -rf "$AGENT_INSTALLATION_FOLDER" >/dev/null 2>&1
fi
}
##
# check parameters
##
if [ -z "$1" ]; then
echo "usage $0 [TC Agent listening port]" >&2
exit 1
fi
##
#
##
echo "Agent installation is executing on '`hostname`'..."
##
# global variables
##
export RUNNING_SCRIPT=$0
export RUNNING_FOLDER=`pwd`
export AGENT_ARC_NAME=buildAgent.zip
export TC_AGENT_PORT=9090
if [ -z "$2" ]; then
export AGENT_INSTALLATION_FOLDER="$HOME/BuildAgent-$(date +%Y%m%d%H%M%S)"
else
export AGENT_INSTALLATION_FOLDER=$2
fi
export AGENT_INSTALLATION_SCRIPT=install.sh
##
# Useful variables
##
SERVER_URL="$1"
AGENT_ARC_URL="$SERVER_URL/update/$AGENT_ARC_NAME"
#have to enforce load shell settings due to ssh security issues. TODO: ksh?
echo "Restoring user's environment..."
[ -e /etc/profile ] && source /etc/profile ;
[ -e ~/.profile ] && source ~/.profile ;
[ -e ~/.bash_profile ] && source ~/.bash_profile ;
[ -e ~/.bashrc ] && source ~/.bashrc ;
##
# Ensure all required utilities installed before doing anything else
##
echo "Looking for unzip"
unzip_exec=$(which unzip 2>/dev/null)
if [ -z "$unzip_exec" ]; then
echo "'unzip' not found. Will be unable to unpack Agent's installation. Terminating[1]"
clean SCRIPT
exit 1
fi
##
# check Agent already installed
##
echo "Looking for existing installations..."
if [ -e "$AGENT_INSTALLATION_FOLDER" ]; then
echo "Found Agent installation in $AGENT_INSTALLATION_FOLDER"
"$AGENT_INSTALLATION_FOLDER/bin/agent.sh" stop 2>&1
clean AGENT
fi
##
# Clean old installation to prevent fetch errors
##
if [ -f "./$AGENT_ARC_NAME" ]; then
echo "Cleaning old installtion files"
rm -f "./$AGENT_ARC_NAME"
fi
echo "Downloading Agent's Installation from $SERVER_URL to $RUNNING_FOLDER..."
#check is there wget
echo "Looking for 'wget'..."
wget_exec=`which wget 2>/dev/null`
if [ -n "$wget_exec" ]; then
echo "'wget' found:"
wget --version 2>&1 | head -n3
wget --tries=1 --connect-timeout=30 --progress=dot:mega -O "$AGENT_ARC_NAME" "$AGENT_ARC_URL" 2>&1
if [ $? -ne 0 ]; then
echo "Could not download Agent from '$AGENT_ARC_URL'. Terminating[1]"
clean ALL
exit 1
fi
else
#check is there curl
echo "Looking for 'curl'..."
curl_exec=`which curl 2>/dev/null`
if [ -n "$curl_exec" ]; then
echo "'curl' found:"
curl --version 2>&1
curl --connect-timeout 30 --output "$AGENT_ARC_NAME" "$AGENT_ARC_URL" 2>&1
if [ $? -ne 0 ]; then
echo "Could not download Agent from '$AGENT_ARC_URL'. Terminating[1]"
clean ALL
exit 1
fi
else
echo "Neither 'curl' nor 'wget' found. Cannot download Agent's installation. Terminating[1]"
clean SCRIPT
exit 1
fi
fi
# check archive uploaded
if [ ! -e $AGENT_ARC_NAME ]; then
echo "Agent's Installation was not properly uploaded. Terminating[2]"
clean ALL
exit 1
fi
##
# unpack archive & clean
##
echo "Extracting agent into '$AGENT_INSTALLATION_FOLDER'..."
mkdir -p "$AGENT_INSTALLATION_FOLDER"
[ -d "$AGENT_INSTALLATION_FOLDER" ] || { echo "Cannot create agent installation folder '$AGENT_INSTALLATION_FOLDER'. Terminating[1]"; exit 1; }
unzip -q -o "$AGENT_ARC_NAME" -d "$AGENT_INSTALLATION_FOLDER" 2>&1
rm -f "./$AGENT_ARC_NAME"
if [ ! -e "$AGENT_INSTALLATION_FOLDER" ]; then
echo "Agent's Installation was not properly extracted. Terminating[3]"
clean ALL
exit 1
fi
##
# start installation script
##
echo "Launching Agent installation..."
cd "$AGENT_INSTALLATION_FOLDER/bin"
if [ ! -e $AGENT_INSTALLATION_SCRIPT ]; then
echo "Could not found installation script '$AGENT_INSTALLATION_SCRIPT'. Terminating[4]"
clean ALL
exit 1
fi
chmod +x "$AGENT_INSTALLATION_SCRIPT"
"./$AGENT_INSTALLATION_SCRIPT" "$SERVER_URL" "$3" "$4" "$5"
isecode=$?
if [ $isecode -ne 0 ]; then
echo "Agent Installation script returned non-zero exit code '$isecode'. Terminating[4]"
clean ALL
exit $isecode
fi
clean SCRIPT
echo "Successfully installed build agent on '`hostname`' to '$AGENT_INSTALLATION_FOLDER'"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment