Skip to content

Commit

Permalink
Merge pull request #19 from jglick/temp-dir-JENKINS-27152
Browse files Browse the repository at this point in the history
[JENKINS-27152] Store control files outside of the workspace
  • Loading branch information
jglick committed Mar 4, 2016
2 parents 92014f1 + 28ca15e commit d66df29
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
Expand Up @@ -140,11 +140,11 @@ private ShellController(FilePath ws) throws IOException, InterruptedException {
super(ws);
}

public FilePath getScriptFile(FilePath ws) {
public FilePath getScriptFile(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("script.sh");
}

FilePath pidFile(FilePath ws) {
FilePath pidFile(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("pid");
}

Expand Down
Expand Up @@ -31,6 +31,7 @@
import hudson.model.TaskListener;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel;
import hudson.slaves.WorkspaceList;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -77,10 +78,14 @@ protected FileMonitoringController doLaunch(FilePath workspace, Launcher launche
}

protected static class FileMonitoringController extends Controller {

/** Absolute path of {@link #controlDir()}. */
private String controlDir;

/**
* Unique ID among all the {@link Controller}s that share the same workspace.
* @deprecated used only in pre-1.8
*/
private final String id = Util.getDigestOf(UUID.randomUUID().toString()).substring(0,8);
private String id;

/**
* Byte offset in the file that has been reported thus far.
Expand All @@ -89,7 +94,10 @@ protected static class FileMonitoringController extends Controller {

protected FileMonitoringController(FilePath ws) throws IOException, InterruptedException {
// can't keep ws reference because Controller is expected to be serializable
controlDir(ws).mkdirs();
ws.mkdirs();
FilePath cd = tempDir(ws).child("durable-" + Util.getDigestOf(UUID.randomUUID().toString()).substring(0,8));
cd.mkdirs();
controlDir = cd.getRemote();
}

@Override public final boolean writeLog(FilePath workspace, OutputStream sink) throws IOException, InterruptedException {
Expand Down Expand Up @@ -158,22 +166,39 @@ private static class WriteLog extends MasterToSlaveFileCallable<Long> {

/**
* Directory in which this controller can place files.
* Unique among all the controllers sharing the same workspace.
*/
public FilePath controlDir(FilePath ws) {
return ws.child(".jenkins-" + id);
public FilePath controlDir(FilePath ws) throws IOException, InterruptedException {
if (controlDir != null) { // normal case
return ws.child(controlDir); // despite the name, this is an absolute path
}
assert id != null;
FilePath cd = ws.child("." + id); // compatibility with 1.6
if (!cd.isDirectory()) {
cd = ws.child(".jenkins-" + id); // compatibility with 1.7
}
controlDir = cd.getRemote();
id = null;
LOGGER.info("using migrated control directory " + controlDir + " for remainder of this task");
return cd;
}

// TODO 1.652 use WorkspaceList.tempDir
private static FilePath tempDir(FilePath ws) {
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
}

/**
* File in which the exit code of the process should be reported.
*/
public FilePath getResultFile(FilePath workspace) {
public FilePath getResultFile(FilePath workspace) throws IOException, InterruptedException {
return controlDir(workspace).child("jenkins-result.txt");
}

/**
* File in which the stdout/stderr
*/
public FilePath getLogFile(FilePath workspace) {
public FilePath getLogFile(FilePath workspace) throws IOException, InterruptedException {
return controlDir(workspace).child("jenkins-log.txt");
}

Expand Down
Expand Up @@ -76,11 +76,11 @@ private BatchController(FilePath ws) throws IOException, InterruptedException {
super(ws);
}

public FilePath getBatchFile1(FilePath ws) {
public FilePath getBatchFile1(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("jenkins-wrap.bat");
}

public FilePath getBatchFile2(FilePath ws) {
public FilePath getBatchFile2(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("jenkins-main.bat");
}

Expand Down
Expand Up @@ -37,7 +37,9 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import org.junit.Before;
import org.jvnet.hudson.test.Issue;

public class BourneShellScriptTest extends Assert {

Expand All @@ -53,7 +55,7 @@ public class BourneShellScriptTest extends Assert {

@Before public void vars() {
listener = StreamTaskListener.fromStdout();
ws = j.jenkins.getRootPath();
ws = j.jenkins.getRootPath().child("ws");
launcher = j.jenkins.createLauncher(listener);
}

Expand Down Expand Up @@ -107,4 +109,17 @@ public void smokeTest() throws Exception {
c.cleanup(ws);
}

@Issue("JENKINS-27152")
@Test public void cleanWorkspace() throws Exception {
Controller c = new BourneShellScript("touch stuff && echo ---`ls -1a`---").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(0, c.exitStatus(ws, launcher).intValue());
assertThat(baos.toString(), containsString("---. .. stuff---"));
c.cleanup(ws);
}

}

0 comments on commit d66df29

Please sign in to comment.