-
-
Save indrgun/2f9ae635e3043f6a83b11608be7d2779 to your computer and use it in GitHub Desktop.
A jenkins script to clean up workspaces on slaves
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a slave has < 10 GB of free space, wipe out workspaces if it does | |
import hudson.model.*; | |
import hudson.util.*; | |
import jenkins.model.*; | |
import hudson.FilePath.FileCallable; | |
import hudson.slaves.OfflineCause; | |
import hudson.node_monitors.*; | |
for (node in Jenkins.instance.nodes) { | |
computer = node.toComputer() | |
if (computer.getChannel() == null) continue | |
rootPath = node.getRootPath() | |
size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size | |
roundedSize = size / (1024 * 1024 * 1024) as int | |
println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB") | |
if (roundedSize < 10) { | |
computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("disk cleanup")) | |
for (item in Jenkins.instance.items) { | |
jobName = item.getFullDisplayName() | |
if (item.isBuilding()) { | |
println(".. job " + jobName + " is currently running, skipped") | |
continue | |
} | |
println(".. wiping out workspaces of job " + jobName) | |
workspacePath = node.getWorkspaceFor(item) | |
if (workspacePath == null) { | |
println(".... could not get workspace path") | |
continue | |
} | |
println(".... workspace = " + workspacePath) | |
customWorkspace = item.getCustomWorkspace() | |
if (customWorkspace != null) { | |
workspacePath = node.getRootPath().child(customWorkspace) | |
println(".... custom workspace = " + workspacePath) | |
} | |
pathAsString = workspacePath.getRemote() | |
if (workspacePath.exists()) { | |
workspacePath.deleteRecursive() | |
println(".... deleted from location " + pathAsString) | |
} else { | |
println(".... nothing to delete at " + pathAsString) | |
} | |
} | |
computer.setTemporarilyOffline(false, null) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hudson.model.* | |
import hudson.util.* | |
import hudson.FilePath | |
import jenkins.model.Jenkins | |
import com.cloudbees.hudson.plugins.folder.* | |
import hudson.FilePath.FileCallable | |
import hudson.node_monitors.* | |
def projects = Jenkins.getInstance().items | |
nodeWorkspaceMap = new HashMap<Node, List<TopLevelItem>>() | |
def purgeWorkSpaceonNode(items) { | |
final Map<Node, List<TopLevelItem>> nodeWorkspaceMap = new HashMap<Node, List<TopLevelItem>>() | |
final List<TopLevelItem> listJenkinsJobs = new ArrayList<TopLevelItem>() | |
recurse_jenkins_items(items, listJenkinsJobs) | |
final Computer[] computers = Jenkins.getInstance().getComputers() | |
for (final Computer computer: computers) { | |
if (computer.getChannel() == null) continue | |
def node = computer.getNode() | |
size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size | |
roundedSize = size / (1024 * 1024 * 1024) as int | |
//If node/agent has < 10 GB of free space, process the workspace purging | |
if (roundedSize < 10) { | |
final List<TopLevelItem> workspaceList = new ArrayList<Item>() | |
for (final TopLevelItem project: listJenkinsJobs) { | |
final FilePath fp = node.getWorkspaceFor(project) | |
// ensure there is a workspace for the job | |
if (fp != null && fp.exists()) { | |
workspaceList.add(project) | |
} | |
} | |
nodeWorkspaceMap.put(node, workspaceList) | |
} | |
} | |
//Process the purge of workspace of job in the node: | |
nodeWorkspaceMap.each { node, projects -> | |
try { | |
println("===========================================") | |
println("--- Processing Node Name : " + node.displayName) | |
for (item in projects) { | |
jobName = item.getFullName() | |
if (item.isBuilding()) { | |
println("--- job: " + jobName + " is currently running, skipped!") | |
continue | |
} | |
println ("--- purging workspace of job: " + jobName) | |
workspacePath = node.getWorkspaceFor(item) | |
if (workspacePath != null && workspacePath.exists()) { | |
pathAsString = workspacePath.getRemote() | |
workspacePath.deleteRecursive() | |
println("--- deleted from location " + pathAsString) | |
} | |
} | |
} catch (IOException e) { | |
println("--- unable to purge: " + e.getMessage()) | |
e.printStackTrace() | |
} | |
} | |
} | |
def recurse_jenkins_items(items, jobList) { | |
items.each { item -> | |
if (item instanceof Folder) { | |
recurse_jenkins_items(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems(), jobList) | |
} else { | |
jobList.add(item) | |
} | |
} | |
} | |
purgeWorkSpaceonNode(projects) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment