Skip to content

Commit

Permalink
Merge pull request adoptium#13 from VermaSh/master
Browse files Browse the repository at this point in the history
Added storage monitoring Jenkins job
  • Loading branch information
VermaSh authored Mar 29, 2018
2 parents d4e03c3 + 01459bd commit 6e8c8f2
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 2 deletions.
167 changes: 167 additions & 0 deletions Jenkins_jobs/WorkspaceInfo.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
@Library('NodeHelper') _
import jenkins.model.Jenkins;
import hudson.model.Computer;

clones = [:]
List<String> newMachines = new ArrayList<String>();

/* 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
}

10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)```
Expand All @@ -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
Expand Down

0 comments on commit 6e8c8f2

Please sign in to comment.