Guest User

Untitled

a guest
Oct 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import hudson.model.*;
  2. import hudson.util.*;
  3. import jenkins.model.*;
  4. import hudson.FilePath.FileCallable;
  5. import hudson.slaves.OfflineCause;
  6. import hudson.node_monitors.*;
  7.  
  8. def DEBUG_MODE = (build.getEnvironment(listener).get('DEBUG_MODE') == "true")
  9. def nodesToCheck = []
  10.  
  11. println "Scanning for slave nodes..."
  12. for(node in Jenkins.instance.nodes){
  13. println "Check node ***${node.name}***"
  14. node.computer.connect(true)
  15. println " label:" + node.getAssignedLabels()
  16. for(label in node.getAssignedLabels()){
  17. if(label.toString().equals("_autoClean")){
  18. nodesToCheck << node
  19. println " BINGO!! This slave is marked as _autoClean! It will be checked to see if its workspaces need to be removed"
  20. }
  21. }
  22. }
  23.  
  24.  
  25. for (node in nodesToCheck) {
  26. println("=== Begin to handle node ${node.name} ===")
  27. computer = node.toComputer()
  28. if (computer.getChannel() == null) continue
  29.  
  30. rootPath = node.getRootPath()
  31. size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size
  32. roundedSize = size / (1024 * 1024 * 1024) as int
  33.  
  34. println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB")
  35. if (roundedSize < 10) {
  36. computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("Temporarily offline for disk cleanup."))
  37. for (job in Jenkins.instance.items) {
  38. if (!(job instanceof hudson.model.FreeStyleProject)) {
  39. continue;
  40. }
  41.  
  42. jobName = job.getFullDisplayName()
  43.  
  44. try{
  45. if (job.isBuilding()) {
  46. if(DEBUG_MODE) println(".. job " + jobName + " is currently running, skipped")
  47. continue
  48. }
  49. } catch(ex) {
  50. println()
  51. println(".. WARNING: fail to get 'isBuilding' status of job ${jobName}, skipped")
  52. println(".... class: ${job.getClass()}")
  53. println(".... exception: ${ex}")
  54. println()
  55. continue
  56. }
  57.  
  58.  
  59.  
  60. workspacePath = node.getWorkspaceFor(job)
  61. if (workspacePath == null) {
  62. continue
  63. }
  64.  
  65.  
  66. try {
  67. customWorkspace = job.getCustomWorkspace()
  68. if (customWorkspace != null) {
  69. workspacePath = node.getRootPath().child(customWorkspace)
  70. }
  71. } catch(ex) {
  72. println()
  73. println(".. WARNING: fail to get custom workspace of job ${jobName}")
  74. println(".... exception: ${ex}")
  75. println()
  76. }
  77.  
  78. pathAsString = workspacePath.getRemote()
  79. if (workspacePath.exists()) {
  80. println(".. wiping out workspaces of job " + jobName)
  81. println(".... workspace = " + workspacePath)
  82.  
  83. if (DEBUG_MODE) {
  84. println(".... under debug mode, ${pathAsString} won't actually be deleted!!!")
  85. } else {
  86. println(".... delete from location ${pathAsString}!!!")
  87. workspacePath.deleteRecursive()
  88. println(".... deleted from location ${pathAsString}!!!")
  89. }
  90.  
  91. }
  92. }
  93.  
  94. computer.setTemporarilyOffline(false, null)
  95. }
  96. }
Add Comment
Please, Sign In to add comment