Reputation: 705
I've a script to find the all idle slaves and create a text file using Jenkins system groovy. I'm able to create a empty file and find all idle slaves where as coming to append to the file "idle_slaves.list" i got java.io.FileNotFoundException exception. Can somebody help me on this?
import jenkins.*
import hudson.*
import jenkins.model.Jenkins
jenkins = jenkins.model.Jenkins
File file1 = new File(build.workspace.toString() + "/idle_slaves.list")
if(build.workspace.isRemote())
{
channel = build.workspace.channel;
fp = new FilePath(channel, build.workspace.toString() + "/idle_slaves.list")
} else {
fp = new FilePath(new File(build.workspace.toString() + "/idle_slaves.list"))
}
if(fp != null)
{
fp.write("", null); //writing to file
}
for (node in Jenkins.instance.nodes) {
computer = node.toComputer()
if (computer.getChannel() == null) {
continue
}
if (computer.isIdle()) {
slave = node.name.toString()
file1.append( slave )
}
}
Jenkins 2.46
groovy 2.0
Upvotes: 4
Views: 3013
Reputation: 16564
Instead of manually read, why not use jenkins/hudson libraries :
https://serverfault.com/a/889954
pipeline {
agent {label 'slave'}
stages {
...
}
}
https://stackoverflow.com/a/43111789/3957754
def jenkins = Jenkins.instance
def computers = jenkins.computers
computers.each{
println "${it.displayName} ${it.hostName}"
}
https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/disableSlaveNodeStartsWith.groovy
https://wiki.jenkins.io/display/JENKINS/Display+Information+About+Nodes
for (aSlave in hudson.model.Hudson.instance.slaves) {
Upvotes: 0
Reputation: 2322
Actually the file itself is not created yet :)
File file1 = new File(build.workspace.toString() + "/idle_slaves.list")
just creates a pointer to the path if you want to create the file, then add the following line
file1.createNewFile()
Also ensure the file that you create has permission access, otherwise you will get permission denied.
You add the following piece of code to confirm.
// check if the file exists
boolean exists = file.exists();
if(exists == true)
{
// printing the permissions associated with the file
println "Executable: " + file.canExecute();
println "Readable: " + file.canRead();
println "Writable: "+ file.canWrite();
}
else
{
println "File not found.";
}
Hope it helps :)
Upvotes: 2