Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling context and docker files separately #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
<version>1.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>6.1.26</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,48 @@ public String runDetached(String image, String workdir, Map<String, String> volu
return out.toString("UTF-8").trim();
}

public String runAttached(String image, String workdir, Map<String, String> volumes, Map<Integer, Integer> ports, Map<String, String> links, EnvVars environment, String user, String... command) throws IOException, InterruptedException {

ArgumentListBuilder args = dockerCommand()
.add("run");
if (privileged) {
args.add( "--privileged");
}
args.add("--workdir", workdir);
for (Map.Entry<String, String> volume : volumes.entrySet()) {
args.add("--volume", volume.getKey() + ":" + volume.getValue() + ":rw" );
}
for (Map.Entry<Integer, Integer> port : ports.entrySet()) {
args.add("--publish", port.getKey() + ":" + port.getValue());
}
for (Map.Entry<String, String> link : links.entrySet()) {
args.add("--link", link.getKey() + ":" + link.getValue());
}
for (Map.Entry<String, String> e : environment.entrySet()) {
if ("HOSTNAME".equals(e.getKey())) {
continue;
}
args.add("--env");
args.addMasked(e.getKey()+"="+e.getValue());
}

if (command != null) {
args.add(image).add(command);
}

ByteArrayOutputStream out = new ByteArrayOutputStream();

int status = launcher.launch()
.envs(dockerEnv.env())
.cmds(args)
.stdout(out).quiet(!verbose).stderr(listener.getLogger()).join();

if (status != 0) {
throw new RuntimeException("Failed to run docker image");
}
return out.toString("UTF-8").trim();
}

public void executeIn(String container, Launcher.ProcStarter starter) {
List<String> originalCmds = starter.cmds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuil

Map<String, String> links = new HashMap<String, String>();

return runInContainer.getDocker().runDetached(runInContainer.image, workdir,
return runInContainer.getDocker().runAttached(runInContainer.image, workdir,
runInContainer.getVolumesMap(), runInContainer.getPortsMap(), links, environment, userId,
"/bin/cat"); // Command expected to hung until killed
(String)null); // Command expected to hung until killed

} catch (InterruptedException e) {
throw new RuntimeException("Interrupted");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,52 @@ public DockerfileImageSelector(String contextPath, String dockerfile) {
this.dockerfile = dockerfile;
}

private FilePath getDockerFilePath(FilePath ctxPath, Docker docker, AbstractBuild build, TaskListener listener) throws IOException, InterruptedException {

FilePath dfPath = null;

if (dockerfile != null) {

String expandedDockerFile = build.getEnvironment(listener).expand(dockerfile);
dfPath = build.getWorkspace().child(expandedDockerFile);

} else {

dfPath = ctxPath.child("DockerFile");

}

return dfPath;
}

private String getAbsPath(FilePath fp) throws IOException, InterruptedException {

File f = new File(fp.toURI());

return f.getAbsolutePath();
}

@Override
public String prepareDockerImage(Docker docker, AbstractBuild build, TaskListener listener) throws IOException, InterruptedException {

String expandedContextPath = build.getEnvironment(listener).expand(contextPath);
FilePath filePath = build.getWorkspace().child(expandedContextPath);
FilePath ctxPath = build.getWorkspace().child(expandedContextPath);

FilePath dfPath = getDockerFilePath(ctxPath, docker, build, listener);

String hash = filePath.act(new ComputeDockerfileChecksum());
String dfAbsPath = getAbsPath(dfPath);

String hash = dfPath.getParent().act(new ComputeDockerfileChecksum());

// search for a tagged image with this hash ID
if (!docker.hasImage(hash)) {
listener.getLogger().println("Build Docker image from "+expandedContextPath+"/Dockerfile ...");
docker.buildImage(filePath, dockerfile, hash);
}
//MR: we don't want to do this check
//MR: The docker build will already cheerfully cache each line item int he docker file
//MR: but if you use ADD or COPY to bring ina resource, this check will miss and ignore any
//MR: changes to that file.
//if (!docker.hasImage(hash)) {
listener.getLogger().println("Build Docker image from "+dfAbsPath+" ...");
docker.buildImage(ctxPath, dfAbsPath, hash);
//}

return hash;
}
Expand Down