Skip to content

Instantly share code, notes, and snippets.

@reysmerwvr
Forked from scarytom/remove-nodes-safely.sh
Created April 21, 2020 22:00
Show Gist options
  • Save reysmerwvr/6aa68be67667c2b74db0738c4d709bc7 to your computer and use it in GitHub Desktop.
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
#!/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