Skip to content

Instantly share code, notes, and snippets.

@kbrnsr
Forked from rb2k/gist:8372402
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save kbrnsr/7766b2c8a3b9b44e6d1b to your computer and use it in GitHub Desktop.

Select an option

Save kbrnsr/7766b2c8a3b9b44e6d1b to your computer and use it in GitHub Desktop.

Revisions

  1. @rb2k rb2k revised this gist Jan 11, 2014. 1 changed file with 3 additions and 18 deletions.
    21 changes: 3 additions & 18 deletions gistfile1.groovy
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,12 @@
    // 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.slaves.OfflineCause.SimpleOfflineCause
    import hudson.node_monitors.*;


    class OfflineDiskCleanupMessage extends org.jvnet.localizer.Localizable {
    def message
    OfflineDiskCleanupMessage() {
    super(null, null, [])
    this.message = "disk cleanup"
    }
    String toString() {
    this.message
    }
    String toString(java.util.Locale l) {
    toString()
    }
    }


    for (node in Jenkins.instance.nodes) {
    computer = node.toComputer()
    if (computer.getChannel() == null) continue
    @@ -32,7 +17,7 @@ for (node in Jenkins.instance.nodes) {

    println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB")
    if (roundedSize < 10) {
    computer.setTemporarilyOffline(true, SimpleOfflineCause.create(new OfflineDiskCleanupMessage()))
    computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("disk cleanup"))
    for (item in Jenkins.instance.items) {
    jobName = item.getFullDisplayName()

  2. @rb2k rb2k created this gist Jan 11, 2014.
    71 changes: 71 additions & 0 deletions gistfile1.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    import hudson.model.*;
    import hudson.util.*;
    import jenkins.model.*;
    import hudson.FilePath.FileCallable;
    import hudson.slaves.OfflineCause;
    import hudson.slaves.OfflineCause.SimpleOfflineCause
    import hudson.node_monitors.*;


    class OfflineDiskCleanupMessage extends org.jvnet.localizer.Localizable {
    def message
    OfflineDiskCleanupMessage() {
    super(null, null, [])
    this.message = "disk cleanup"
    }
    String toString() {
    this.message
    }
    String toString(java.util.Locale l) {
    toString()
    }
    }


    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, SimpleOfflineCause.create(new OfflineDiskCleanupMessage()))
    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)
    }
    }