Createing a Linux Slave via groovy scripts, which uses ssh to connect

1,021 views
Skip to first unread message

munsingh

unread,
Nov 7, 2014, 5:46:45 AM11/7/14
to jenkin...@googlegroups.com
Hello Jenkins Developers,

I need to create Jenkins Linux Slaves via script. I have been able to create Windows Slaves, which connect via JNLP by using the folowing code:

Jenkins.instance.addNode( new DumbSlave(strSlaveName,
                                        strSlaveDescription,
                                        strWorkspace,
                                        strSlaveNumExecutors,
                                        Node.Mode.NORMAL,
                                        strLabel,
                                        "true" == strSlaveIsWindows ? new JNLPLauncher() : new CommandLauncher( "172.16.222.151 root" ),
                                        new RetentionStrategy.Always(),
                                        new LinkedList()));


However I am unable to create Linux Slaves, which "Launch Slave agents on Unix machines via ssh. The CommandLauncher class creates a slave, which "Launch Slave agents via execution of command via the master".
I see that the ComputerLauncher class has three sub-classes: CommandLauncher, ComputerLauncherFilter, DelegatingComputerLauncher, JNLPLauncher.

I am not sure whether the ComputerLauncherFilter or the DelegatingComputerLauncher class needs to be used.

Any help is highly appreciated.

Thanks,
Manish




Robert Sandell

unread,
Nov 7, 2014, 11:24:28 AM11/7/14
to jenkin...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

munsingh

unread,
Nov 10, 2014, 12:51:56 AM11/10/14
to jenkin...@googlegroups.com
Thanks Robert.

QQ: How do I instantiate this class via groovy script, can I import this class in groovy and use it?

Regards,
Manish

Robert Sandell

unread,
Nov 10, 2014, 5:36:04 AM11/10/14
to jenkin...@googlegroups.com
Yes, the groovy script console should have access to all classes in Jenkins including plugin classes.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

munsingh

unread,
Nov 10, 2014, 5:57:55 AM11/10/14
to jenkin...@googlegroups.com
Ok. I tried creating an instance of SSHLauncher class in my script and imported the plugins as follows:

import hudson.plugins.sshslaves.*;

And I try to instantiate the SSHLauncher object by calling:
new SSHLauncher( "172.16.222.142", "22", "jenkins" )

however I get the error:

startup failed:
Script1.groovy: 40: unable to resolve class SSHLauncher
@ line 40, column 62.
   indows ? new JNLPLauncher() : new SSHLau


Looks like it is unable to find the SSHLauncher class. Am I missing something here?

Thanks for your help.

Regards,
Manish

munsingh

unread,
Nov 10, 2014, 6:28:52 AM11/10/14
to jenkin...@googlegroups.com
Ok I got it working. I was trying to execute my script as part of a job via the Execute System Groovy Script->Groovy Command option. The Check-Syntax was unable to check the syntax as it was unable to find the SSHSlaves class. However it was working when running the job.

This could be a bug in the Check Syntax functionality, wherein it is unable to look classes from plugins.

So a big Thanks Robert!! Much appreciated.

Robert Sandell

unread,
Nov 10, 2014, 1:39:25 PM11/10/14
to jenkin...@googlegroups.com
Well It "works on my machine (TM)"

  println(hudson.plugins.sshslaves.SSHLauncher.class.getName())

provides an output without an error. Try that.
It could be mistaking the error from that there is no known constructor that only takes three arguments from what I can see?

/B

munsingh

unread,
Nov 12, 2014, 2:05:22 AM11/12/14
to jenkin...@googlegroups.com
Any idea on how to get the credentialsId programmatically?

-Manish

Robert Sandell

unread,
Nov 12, 2014, 5:06:34 AM11/12/14
to jenkin...@googlegroups.com
Similar to

But unless you'll be running the script on multiple masters you could just hard code the id into the script if you are using the same credential to connect all the slaves.

/B

munsingh

unread,
Nov 13, 2014, 7:28:49 AM11/13/14
to jenkin...@googlegroups.com
Got it. Thx.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/JRu4R9bKibU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
May the force be with you
Reply all
Reply to author
Forward
0 new messages