jenkins groovy admin script to restart all slaves

115 views
Skip to first unread message

tre...@gmail.com

unread,
Sep 7, 2018, 11:30:28 AM9/7/18
to Jenkins Users
Hey,

Trying to create a script that restarts all slaves using Jenkins Administrative Script console.

Script 1:
import hudson.util.RemotingDiagnostics;

createShellScriptCommand = "echo \"/usr/sbin/shutdown -r now"
grantExecutionPrvilige = "chmod +x /home/nirdevadm/shutdown.sh"
executeShellScriptCommand = "echo \"vm-pass\" | sudo -S '/home/nirdevadm/shutdown.sh'"

for (slave in hudson.model.Hudson.instance.slaves) {
    println slave.name;
    println RemotingDiagnostics.executeGroovy(createShellScriptCommand, slave.getChannel());
    println RemotingDiagnostics.executeGroovy(grantExecutionPrvilige, slave.getChannel());
    println RemotingDiagnostics.executeGroovy(executeShellScriptCommand, slave.getChannel());
}

Script 1 fails with:

cript1.groovy: 1: unexpected char: 0xFFFF @ line 1, column 32.
   echo "/usr/sbin/shutdown -r now
                                  ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
groovy.lang.MissingPropertyException: No such property: chmod for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)

groovy.lang.MissingPropertyException: No such property: sudo for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)

Script 2:


import hudson.slaves.SlaveComputer
import jenkins.model.Jenkins

createShellScriptCommand = "echo \"/usr/sbin/shutdown -r now"
grantExecutionPrvilige = "chmod +x /home/nirdevadm/shutdown.sh"
executeShellScriptCommand = "echo \"vm-pass\" | sudo -S '/home/nirdevadm/shutdown.sh'"

Jenkins.instance.getComputers()
        .findAll { it instanceof SlaveComputer }
        .each { computer ->
Process p = createShellScriptCommand.execute()
Process z = grantExecutionPrvilige.execute()
Process x = executeShellScriptCommand.execute()
}
return;

Script 2 seems to compile and does NOT fail but it does nothing as the slaves are not restarted.


It all boils down to these 3 unix commands that I want to execute trough groovy (they work well in isolation, executed directly on the slave):
echo "/usr/sbin/shutdown -r now" > /home/nirdevadm/shutdown.sh
chmod +x /home/nirdevadm/shutdown.sh
echo "vm-pass" | sudo -S '/home/nirdevadm/shutdown.sh'


Any suggestions what am I doing wrong? 
Reply all
Reply to author
Forward
0 new messages