Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>diskcheck</artifactId>
<version>0.27</version>
<version>0.28</version>
<packaging>hpi</packaging>

<licenses>
Expand Down
139 changes: 52 additions & 87 deletions src/main/java/org/jenkinsci/plugin/Diskcheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,22 @@
import java.io.IOException;
import java.io.PrintStream;



/**
* Class to allow any build step to be performed before the SCM checkout occurs.
*
* @author Manoj Thakkar
*
*/


public class Diskcheck extends BuildWrapper {


public final boolean failOnError;

/**
* Constructor taking a list of buildsteps to use.
*
* @param buildstep
* list of but steps configured in the UI
* list of build steps configured in the UI
*/
@DataBoundConstructor
public Diskcheck(boolean failOnError) {
Expand All @@ -62,6 +58,11 @@ public Environment setUp(AbstractBuild build, Launcher launcher,
return new NoopEnv();
}

@Override
public Descriptor getDescriptor() {
return (Descriptor) super.getDescriptor();
}

/**
* Overridden precheckout step, this is where wedo all the work.
*
Expand All @@ -75,19 +76,12 @@ public Environment setUp(AbstractBuild build, Launcher launcher,
* @param listener
*/

@Override
public Descriptor getDescriptor() {
return (Descriptor) super.getDescriptor();
}
@Override
public void preCheckout(AbstractBuild build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
PrintStream log = listener.getLogger();
// Default value of disk space check is 1Gb
int SpaceThreshold;
SpaceThreshold = PluginImpl.getInstance().getSpacecheck();
final int SpaceThreshold = PluginImpl.getInstance().getSpacecheck();


log.println("Disk space threshold is set to :" + SpaceThreshold + "Gb");
log.println("Checking disk space Now ");

Expand All @@ -96,85 +90,61 @@ public void preCheckout(AbstractBuild build, Launcher launcher,
build.getWorkspace().mkdirs();
}

Node node1 = build.getBuiltOn();
Computer Comp = node1.toComputer();
String NodeName = build.getBuiltOnStr();
/* if (Comp.getChannel()==null)
{
log.println("Can not get slave infomration wait for 10 sec \n");
Thread.sleep(10000);
if (Comp.getChannel()==null)
{
log.println("Waited long enough to get slave information exiting discheck for now \n");
System.exit(0);
}

}
*/

if ( DiskSpaceMonitor.DESCRIPTOR.get(Comp)== null )
{ log.println("No Slave Data available trying to get data from slave");
Thread.sleep(10000);
if ( DiskSpaceMonitor.DESCRIPTOR.get(Comp)== null )

log.println(" Could not get Slave Information , Exiting Disk check for this slave");
System.exit(0);
final Computer Comp = build.getBuiltOn().toComputer();
final String NodeName = build.getBuiltOnStr() == "" ? "master" : build.getBuiltOnStr();

if ( DiskSpaceMonitor.DESCRIPTOR.get(Comp) == null ) {
log.println("No Slave Data available trying to get data from slave");
Thread.sleep(10000); // ???

if ( DiskSpaceMonitor.DESCRIPTOR.get(Comp) == null ) {
log.println(" Could not get Slave Information, Exiting Disk check for this slave");
System.exit(0);
}
}

long size=0;
try
{
size = DiskSpaceMonitor.DESCRIPTOR.get(Comp).size;
int roundedSize = 0;
try {
long size = DiskSpaceMonitor.DESCRIPTOR.get(Comp).size;
roundedSize = (int) (size / (1024 * 1024 * 1024));
}
catch(NullPointerException e ){
log.println("Could not get Slave Information , Exiting Disk check for this slave");
System.exit(0);
}
int roundedSize = (int) (size / (1024 * 1024 * 1024));
log.println("Total Disk Space Available is: " + roundedSize + "Gb");

if (build.getBuiltOnStr() == "") {
NodeName = "master";
catch (NullPointerException e) {
log.println("Could not get Slave disk size Information: " + e + ".\nExiting Disk check for this slave.");
System.exit(0);
}

log.println(" Node Name: " + NodeName);
log.println("Total Disk Space Available is: " + roundedSize + "Gb");
log.println("Node Name: " + NodeName);

if (PluginImpl.getInstance().isDiskrecyclerenabled()) {
if (roundedSize < SpaceThreshold) {
if (roundedSize < SpaceThreshold) {
if (PluginImpl.getInstance().isDiskrecyclerenabled()) {
log.println("Disk Recycler is Enabled so I am going to wipe off the workspace Directory Now ");
String mycommand = "echo $WORKSPACE; rm -rf $WORKSPACE/../; df -k .";
String mywincommand = "echo Deleting file from %WORKSPACE% && Del /R %WORKSPACE%";

/**
* This method will return the command intercepter as per the
* node OS
*
* @param launcher
* @param script
* @return CommandInterpreter
*/

CommandInterpreter runscript;
if (launcher.isUnix())
runscript = new Shell(mycommand);
else
runscript = new BatchFile(mywincommand);
if (launcher.isUnix()) {
String cmd = "echo Deleting directory $WORKSPACE && rm -rf $WORKSPACE";

runscript = new Shell(cmd);
}
else {
/*
rmdir syntax
/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S.
*/
String cmd = "echo Deleting directory %WORKSPACE% && rmdir /S /Q %WORKSPACE%";

runscript = new BatchFile(cmd);
}

Result result = runscript.perform(build, launcher, listener) ? Result.SUCCESS
: Result.FAILURE;
Result result = runscript.perform(build, launcher, listener) ? Result.SUCCESS : Result.FAILURE;

if (result.toString() == "FAILURE") {
throw new AbortException(
"Something went wrong while deleting Files , Please check the error message above");
throw new AbortException("Something went wrong while deleting workspace. Please check the error message above");
}
}
}

log.println("Running Prebuild steps");
if (roundedSize < SpaceThreshold
&& !(PluginImpl.getInstance().isDiskrecyclerenabled())) {
throw new AbortException(
"Disk Space is too low please look into it before starting a build");

} else {
throw new AbortException("Disk Space is too low please look into it before starting a build");
}
}

Expand All @@ -186,19 +156,14 @@ public static final class DescriptorImpl extends Descriptor<BuildWrapper> {
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Check Disk Space";
}


return "Check Disk Space";
}

public DescriptorImpl() {
load();
}


}


class NoopEnv extends Environment {
}
}
26 changes: 10 additions & 16 deletions src/main/java/org/jenkinsci/plugin/PluginImpl.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,26 @@ public void stop() throws Exception {
}

@Override
public void configure(StaplerRequest req, JSONObject formData)
throws IOException, ServletException, FormException {
formData=formData.getJSONObject("disk-check");
spacecheck=formData.getInt("spacecheck");
diskrecyclerenabled=formData.getBoolean("diskrecyclerenabled");
public void configure(StaplerRequest req, JSONObject formData) throws IOException, ServletException, FormException {
formData = formData.getJSONObject("disk-check");
spacecheck = formData.getInt("spacecheck");
diskrecyclerenabled = formData.getBoolean("diskrecyclerenabled");

save();
super.configure(req, formData);

}



public void setDiskrecyclerenabled(boolean diskrecyclerenabled) {
this.diskrecyclerenabled = diskrecyclerenabled;
}
this.diskrecyclerenabled = diskrecyclerenabled;
}

public int getSpacecheck()
{
public int getSpacecheck() {
return spacecheck;
}

public void setSpacecheck(int spaceheck)
{
this.spacecheck = spacecheck;
}
public void setSpacecheck(int spaceheck) {
this.spacecheck = spacecheck;
}

public boolean isDiskrecyclerenabled() {
return diskrecyclerenabled;
Expand Down