Skip to content

Instantly share code, notes, and snippets.

@nichollsc81
Last active April 13, 2022 14:55
Show Gist options
  • Save nichollsc81/32719b0f112ba15de27c2251355bd773 to your computer and use it in GitHub Desktop.
Save nichollsc81/32719b0f112ba15de27c2251355bd773 to your computer and use it in GitHub Desktop.
Restarts TC agents
#!/bin/bash
restartWindowsAgents () {
local TEAMCITY_USER_NAME=$1
local TEAMCITY_USER_SECRET=$2
local TEAMCITY_TARGET_AGENTS="$3"
# teamcity root
TEAMCITY_URI='https://my.teamcity.local'
if [ -z "${TEAMCITY_TARGET_AGENTS}" ]; then
# extract agents if none are supplied
SERVERS=$(curl -k -u $TEAMCITY_USER_NAME:$TEAMCITY_USER_SECRET "${TEAMCITY_URI}/httpAuth/app/rest/agents?includeDisconnected=false" --GET)
# limit windows agents
AGENTS=$(echo $SERVERS | xpath -e '//@name' 2>/dev/null | sed -n 's/[^\"]*\"\([^\"]*\)\"[^\"]*/\1/gp' | grep win)
else
# else split on comma seperated list
AGENTS=(${TEAMCITY_TARGET_AGENTS//,/ })
fi
# iterate over agents and schedule restarts
for AGENT in ${AGENTS[@]}; do
# tc agent id
AGENT_ID=$(curl -k -u $TEAMCITY_USER_NAME:$TEAMCITY_USER_SECRET "${TEAMCITY_URI}/app/rest/agents/name:$AGENT/id" 2>/dev/null)
if [ -n "$AGENT_ID" ]; then
echo "Determined id of agent $AGENT : $AGENT_ID"
echo "Scheduling restart..."
curl -k -i -u $TEAMCITY_USER_NAME:$TEAMCITY_USER_SECRET -X POST "${TEAMCITY_URI}/remoteAccess/reboot.html?agent=${AGENT_ID}&rebootAfterBuild=true"
else
echo "Couldn't determing unique id of agent $a"
fi
done
}
(
set -e
restartWindowsAgents $1 $2 $3
)
exit_status=$?
if [ ${exit_status} -ne 0 ]; then
echo "Exception encountered, likely supplied credentials."
exit "${exit_status}"
fi
# reboot specified agents
# ./restartWindowsTeamCityAgents.sh tc_username tc_password 'win-agent1,win-agent2'
# reboot ALL the agents
# ./restartWindowsTeamCityAgents.sh tc_username tc_password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment