简体   繁体   中英

Jenkins: configure slave node address dynamically using command or groovy script

I have kinda ssh slave build jenkins setup.

Jenkins server connect to Mac slave thru ssh. build ios apps there. two remote nodes are configured in Jenkins connected to the Mac. The Mac has dhcp.

Every time my mac starts I want to run a script that tell the Jenkin server to configure the node's IP address pointing to the dhcp address that the mac receives. Since its dhcp it changes always.

Is possible to configure such? using shell script or perl ...

e.g. http://jenkins-server:8080/computer/mac-slave-enterprise/configure

is the node config url. If its possible to setup by sending host=10.1.2.100 & Submit=Save or something like this?

I found it is possible run Groovy script at

http://jenkins/script

or from mac command line or sh script,

$ curl -d "script=<your_script_here>" http://jenkins/script

I tried to get some info with this code but no luck, seems I have create SSLLauncher, but lost in how to grab a launcher things. There is no direct setHost or setLauncher thing.

following the tutorial at,

https://wiki.jenkins-ci.org/display/JENKINS/Display+Information+About+Nodes

but cannot set the host address.

println("node desc launcher = " + aSlave.getComputer().getLauncher()); 
  //println("node desc launcher = " + aSlave.getComputer().getLauncher().setHost("10.11.51.70")); 
  println("node launcher host = " + aSlave.getComputer().getLauncher().getHost()); 

  hudson.plugins.sshslaves.SSHLauncher ssl = aSlave.getComputer().getLauncher(); 
  int port = ssl.getPort(); 
  String userName, password, privateKey; 
  userName = ssl.getUsername(); 
  password = ssl.getPassword(); 
  privateKey = ssl.getPrivatekey(); 

  println("user: "+userName + ", pwd: "+password + ", key: "+privateKey); 
// all these values returns null. 

Another way would be to just delete the node and recreate it.

Here is some groovy on how to delete it from here :

  for (aSlave in hudson.model.Hudson.instance.slaves) { 
    if (aSlave.name == "MySlaveToDelete") { 
      println('===================='); 
      println('Name: ' + aSlave.name); 
      println('Shutting down node!!!!'); 
      aSlave.getComputer().setTemporarilyOffline(true,null); 
      aSlave.getComputer().doDoDelete(); 
    } 

And here is how to create one ( source ):

import jenkins.model.*
import hudson.model.*
import hudson.slaves.*

Jenkins.instance.addNode(new DumbSlave("test-script","test slave description","C:\\Jenkins","1",Node.Mode.NORMAL,"test-slave-label",new JNLPLauncher(),new RetentionStrategy.Always(),new LinkedList())) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM