-
-
Save reysmerwvr/6aa68be67667c2b74db0738c4d709bc7 to your computer and use it in GitHub Desktop.
Script to safely de-register jenkins nodes usage: $ remove-nodes-safely.sh my-node-1 my-node-2 my-node-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -u | |
CI_MASTER_URL="http://ci-1" | |
node_online() { | |
curl --silent "$CI_MASTER_URL/computer/$1/api/json" | grep --silent '"temporarilyOffline":false' | |
} | |
node_busy() { | |
curl --silent "$CI_MASTER_URL/computer/$1/api/json" | grep --silent '"idle":false' | |
} | |
toggle_node_online() { | |
curl --silent "$CI_MASTER_URL/computer/$1/toggleOffline" --request 'POST' --data 'offlineMessage=Pending node re-image' | |
} | |
deregister_node() { | |
curl --silent "$CI_MASTER_URL/computer/$1/doDelete" --request 'POST' --data '' | |
} | |
wait_for_node() { | |
while node_busy $1; do | |
sleep 20 | |
echo -n "." | |
done | |
echo "" | |
} | |
# Mark nodes offline | |
for NODE in "$@" | |
do | |
if node_online $NODE; then | |
echo "Marking node $NODE as offline" | |
toggle_node_online $NODE | |
else | |
echo "Node $NODE is already offline" | |
fi | |
done | |
# Wait for nodes to finish current workload | |
for NODE in "$@" | |
do | |
if node_busy $NODE; then | |
echo "Waiting for node $NODE to finish its current work" | |
wait_for_node $NODE | |
echo "Node $NODE has finished its work" | |
else | |
echo "Node $NODE is not busy" | |
fi | |
done | |
# De-register nodes | |
for NODE in "$@" | |
do | |
echo "Permanently removing $NODE from Jenkins" | |
deregister_node $NODE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment