Skip to content

Instantly share code, notes, and snippets.

@sleekweasel
Last active May 12, 2016 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sleekweasel/139e3eb45b5c215b001e to your computer and use it in GitHub Desktop.
Save sleekweasel/139e3eb45b5c215b001e to your computer and use it in GitHub Desktop.
TeamCity - reboot dependency

To use:

  1. Create a 'reboot' project with the reboot script in it. You'll need to encode the root password in the script so it can run shutdown, or otherwise arrange access to 'shutdown'.
  2. Create a 'await reboot' project with the await script in it. Add the 'reboot' project as a snapshot dependency, that will mark the build as failed if it fails.
  3. Add the plist to your /Library/LaunchAgent/ directory, or add the equivalent 'sed' script into your /etc/rc.local script for proper Unixes.
  4. Add the 'await reboot' project as a dependency to your real build.
echo This task runs after the reboot task, as a buffer between the reboot script and the real reboot.
echo That is, THIS task will get all the INTERRUPTED errors, not the real scripts.
# Note: This script depends on the host machine's reboot process removing the 'rebooting=true' line from the buildAgent.properties
# See the plist in this gist, or add it to your rc.local.
echo Uptime:
uptime
echo Infinite loop until machine reboot, unless reboot is already complete:
while grep rebooting=yes $HOME/buildAgent/conf/buildAgent.properties; do
echo $(date) Awaiting reboot to interrupt task and clear rebooting=yes from buildAgent.properties file.
sleep 15
done
echo "Adding system.rebooting=yes and rebooting=yes in properties, which now has..."
echo rebooting=yes >> $HOME/buildAgent/conf/buildAgent.properties
echo system.rebooting=yes >> $HOME/buildAgent/conf/buildAgent.properties
grep rebooting=yes $HOME/buildAgent/conf/buildAgent.properties
echo TEAMCITY_BUILD_PROPERTIES_FILE $TEAMCITY_BUILD_PROPERTIES_FILE
CONFIG_PROPERTIES=$(grep ^teamcity.configuration.properties.file $TEAMCITY_BUILD_PROPERTIES_FILE | cut -d= -f2)
echo CONFIG_PROPERTIES=$CONFIG_PROPERTIES
TC_SERVER=$(grep '^teamcity.serverUrl' $CONFIG_PROPERTIES | cut -d= -f2)
BUILD_ID=$(grep '^teamcity.build.id' $CONFIG_PROPERTIES | cut -d= -f2)
STATUS=$( echo "${TC_SERVER}/guestAuth/app/rest/builds/id:${BUILD_ID}" | tr -d \\ )
echo Polling $STATUS in background until parent task cannot be confirmed still running. Reboot will follow.
((
while true; do
sleep 5
if curl -s $STATUS | xmllint --xpath "//build/@running" - | grep true ; then
:
else
/bin/echo "PASSWORD" | /usr/bin/sudo -p "" -S /sbin/shutdown -r now
fi
done
) 2>/dev/null >/dev/null &)&
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>remove.rebooting.attribute</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/sed</string>
<string>-i~</string>
<string>-e</string>
<string>/rebooting=yes/d</string>
<string>/Users/qa/buildAgent/conf/buildAgent.properties</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
</dict>
<key>StandardOutPath</key>
<string>/tmp/reset_reboot.out.log</string>
<key>StandardErrorPath</key>
<string>/tmp/reset_reboot.err.log</string>
<key>Debug</key>
<true/>
<key>UserName</key>
<string>qa</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment