#!/bin/bash
# This script should be run on the Jenkins master. We set up a job in Jenkins to run this once a week on master.
function jenkins-cli {
java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080 "$@"
}
for slave in slavename1 slavename2 slavename3; do
if [[ `curl -s "http://localhost:8080/computer/${slave}/api/xml?xpath=*/offline/text()"` = "true" ]]; then
echo "$slave is already offline. Skipping cleanup"
else
echo "Cleaning up docker on $slave"
echo "Taking $slave offline"
jenkins-cli offline-node $slave -m "Scheduled docker cleanup is running" && \
echo "Waiting on $slave to go offline" && \
jenkins-cli wait-node-offline $slave && \
while [[ `curl -s "http://localhost:8080/computer/${slave}/api/xml?xpath=*/idle/text()"` != "true" ]]; do echo "Waiting on $slave to be idle" && sleep 5; done && \
echo "Running cleanup_docker on $slave" && \
ssh -o "StrictHostKeyChecking no" $slave -i /var/lib/jenkins/.ssh/id_rsa "sudo /usr/local/bin/cleanup_docker"
echo "Bringing $slave back online"
jenkins-cli online-node $slave
fi
2条答案
按热度按时间smdnsysy1#
您可以尝试:
使用Jenkins Pipeline:
字符串
使用SH
型
zphenhs42#
Python中有很多代码片段可以用来列出现有的节点。删除从节点的工作方式如下:
字符串