如何在SWARM模式下从Jenkins删除脱机(从/节点)

时间:2018-02-03 16:29:11

标签: jenkins jenkins-plugins docker-swarm continuous-deployment jenkins-slave

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

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

2 个答案:

答案 0 :(得分:1)

您可以尝试:

使用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

答案 1 :(得分: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()))