如何在SWARM模式下从Jenkins中删除离线(代理/节点)

eagi6jfj  于 2023-11-17  发布在  Jenkins
关注(0)|答案(2)|浏览(191)

如何从Jenkins中删除节点?Jenkins在Docker Swarm模式下运行。
当我试图删除离线容器时,swarm会继续生成新容器

smdnsysy

smdnsysy1#

您可以尝试:
使用Jenkins Pipeline:

node("master") {
println "Cleaning up offline slaves..."
    hudson.model.Hudson.instance.slaves.each {
      if(it.getComputer().isOffline()) {
        println "Deleting ${it.name}"
        it.getComputer().doDoDelete()
      }
    }
    println "Done."
}

字符串
使用SH

#!/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

zphenhs4

zphenhs42#

Python中有很多代码片段可以用来列出现有的节点。删除从节点的工作方式如下:

request = urllib.request.Request("https://insert_jenkins_url/computer/insert_node_name/doDelete", data=bytes("", "ascii"))
#according to https://stackoverflow.com/a/28052583/4609258 the following is ugly
context = ssl._create_unverified_context() 
base64string = base64.b64encode(bytes('%s:%s' % ('insert_user_name', 'insert_api_token_with_roughly_35_characters'),'ascii'))
request.add_header("Authorization", "Basic %s" % base64string.decode('utf-8'))

with urllib.request.urlopen(request, context=context) as url:
print(str(url.read().decode()))

字符串

相关问题