diff --git a/Jenkins_jobs/WorkspaceInfo.groovy b/Jenkins_jobs/WorkspaceInfo.groovy new file mode 100644 index 0000000..51172ee --- /dev/null +++ b/Jenkins_jobs/WorkspaceInfo.groovy @@ -0,0 +1,167 @@ +@Library('NodeHelper') _ +import jenkins.model.Jenkins; +import hudson.model.Computer; + +clones = [:] +List newMachines = new ArrayList(); + +/* Iterates over all the online nodes in Jenkins and + * prints contents of workspace folder along with + * the space they occupy. With exception of tmp directory + */ +node { + stage('Print_Space_Monitoring_Data') { + NodeHelper nodeHelper = new NodeHelper(); + + String projectLabel; + if (params.projectLabel.length() > 1) { + projectLabel = params.projectLabel; + } else { + projectLabel = "all"; + } + + def currentInstance = Jenkins.getInstance(); + Computer[] computers; + + if (currentInstance != null) { + computers = currentInstance.getComputers(); + } + + for (Computer computer : computers) { + String machineName = computer.getName(); + + if (computer.isOnline() && computer.getName() != "") { + String kernelName = nodeHelper.getOsKernelInfo(computer.getName()).get(0).toLowerCase() + + if (projectLabel.equals("all") + || nodeHelper.getLabels(computer.getName()).contains(projectLabel)) { + + String workspaceDirectory = nodeHelper.getHomeDirectoryPath(machineName); + + switch (kernelName) { + case 'linux': + clones[machineName] = { + node (machineName) { + workspaceDirectory += "/workspace" + + /* #!/bin/sh -e\n is there to keep the output to console as clean as + * possible + */ + String workspaceStats = sh ( + script: '#!/bin/sh -e\n' + 'du -sh ' + workspaceDirectory, + returnStdout: true).trim() + + String subdirectories = sh ( + script: '#!/bin/sh -e\n du -sh ' + workspaceDirectory + '/* | sort -h', + returnStdout: true).trim() + + println beautify(machineName,workspaceDirectory, workspaceStats,subdirectories) + + cleanWs() + } + } + break; + case 'aix': + clones[machineName] = { + node (machineName) { + workspaceDirectory += "/workspace" + + String workspaceStats = sh ( + script: '#!/bin/sh -e\n' + 'du -sg ' + workspaceDirectory, + returnStdout: true).trim() + + String subdirectories = sh ( + script: '#!/bin/sh -e\n du -sg ' + workspaceDirectory + '/* | sort -n', + returnStdout: true).trim() + + println beautify(machineName + 'in Gb',workspaceDirectory, workspaceStats,subdirectories) + + cleanWs() + } + } + break; + case 'mac': + clones[machineName] = { + node (machineName) { + workspaceDirectory += "/workspace" + + String workspaceStats = sh ( + script: '#!/bin/sh -e\n' + 'du -sh ' + workspaceDirectory, + returnStdout: true).trim() + + String subdirectories = sh ( + script: '#!/bin/sh -e\n du -sh ' + workspaceDirectory + '/* | sort -n', + returnStdout: true).trim() + + println beautify(machineName,workspaceDirectory, workspaceStats,subdirectories) + + cleanWs() + } + } + break; + /* This is commented out because it takes way too long to return + * it isn't a top priority + * dir /s /-c + */ + case 'windows': + // clones[machineName] = { + // node(machineName) { + // cleanWs() + // NodeHelper nodeHelper = new NodeHelper(); + // workspaceDirectory += "\\workspace"; + // println workspaceDirectory + // // TODO: get path to directory from the api + // String workspaceStats = sh ( + // script: '#!/bin/sh -e\n' 'du -sh ' workspaceDirectory, + // returnStdout: true).trim() + + // String script = 'set x; du -sh ' workspaceDirectory '\\* | sort -rn'; + // String subdirectories = sh ( + // script: 'script', + // returnStdout: true).trim() + + + // println beautify(machineName,workspaceDirectory, workspaceStats,subdirectories); + + // cleanWs() + // } + // } + // } + // break; + case 'zos': + default: + println ("Support for ${kernelName} is yet to be implemented"); + break; + } + } + } + } + currentInstance = null; + computers = null; + + cleanWs() + } +} + +@NonCPS +def beautify(machineName, workspaceDirectory, workspaceStats, subdirectories) { + workspaceStats = workspaceStats.replaceAll("\\s+", " "); + subdirectories = subdirectories.replace(workspaceDirectory + "/", ""); + String output = "\n\n=======================================================================\n"; + output += "Disk stats for ${machineName}" + output += "\nWorkspace:\n${workspaceStats}"; + def subdirectoriesArray = subdirectories.split("\n"); + for (String line : subdirectoriesArray) { + if (!line.contains("tmp")) { + line = line.replaceAll("\\s+", " ") + output += "\n ${line}"; + } + } + output += "\n=======================================================================\n\n"; +} + + +timeout(time: params.timeout, unit: 'HOURS') { + parallel clones +} + diff --git a/README.md b/README.md index eafa43c..a1b5dd2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # openjdk-jenkins-helper -The NodeHelper API contains helper functions to query basic machine stats in real time, add new machines, update/overwrite labels. -## Functions +## Files +### API Functions (NodeHelper.groovy) +The NodeHelper API contains helper functions to query basic machine stats in real time, add new machines, update/overwrite labels. * Get CPU count ```String getCpuCount(String computerName)``` * Get Installed memory ```Tuple getMemory(String computerName)``` * Get OS information ```Tuple getOsInfo(String computerName)``` @@ -17,6 +18,11 @@ The NodeHelper API contains helper functions to query basic machine stats in rea * Add Label ```String addLabel(String computerName, String label)``` * Append Label ```String appendlabel(String computerName, String label)``` +### Space Monitoring (WorkspaceInfo.groovy) +Iterates over online nodes on Jenkins and prints the contents of the workspace directory along with the space they occupy +* The computers it iterates over can be limited by input parameter, ```projectLabel``` +* As of now, it only works for linux, aix, and mac + ## How-to ### Setup