Skip to content

Instantly share code, notes, and snippets.

@scarytom
Last active May 9, 2023 20:16
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save scarytom/5910362 to your computer and use it in GitHub Desktop.
Save scarytom/5910362 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
@zerogvt
Copy link

zerogvt commented Jul 20, 2018

👍 🥇

@spkane
Copy link

spkane commented Apr 16, 2020

Using something like -K- <<< "-u ${JENKINS_USER}:${JENKINS_PASSWORD}" at the end of these curl statements will allow you to auth against Jenkins as well.

@scarytom
Copy link
Author

Thanks for the tip. I don't have any auth on my internal jenkins, but feel free to copy this script and modify it to suit your needs

@spkane
Copy link

spkane commented Apr 16, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment