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

问题描述 投票:0回答:2

如何从 Jenkins 删除节点? Jenkins 以 Docker Swarm 模式运行。

当我尝试删除离线容器时,swarm 不断生成新容器image1 node

jenkins jenkins-plugins docker-swarm continuous-deployment jenkins-agent
2个回答
2
投票

您可以尝试:

使用 Jenkins 管道:

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

0
投票

有很多代码片段可以用来列出 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()))  
© www.soinside.com 2019 - 2024. All rights reserved.