Navigation Menu

Skip to content

Commit

Permalink
JENKINS-22083
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Linke committed Mar 7, 2014
1 parent f6f46c1 commit 742de2b
Showing 1 changed file with 80 additions and 47 deletions.
Expand Up @@ -5,16 +5,22 @@
import hudson.model.Result;
import hudson.model.AbstractBuild;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.stream.JsonParsingException;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -25,7 +31,9 @@
/**
* This command creates a new image from specified Dockerfile.
*
* @see http://docs.docker.io/en/latest/reference/api/docker_remote_api_v1.8/#build-an-image-from-dockerfile-via-stdin
* @see http
* ://docs.docker.io/en/latest/reference/api/docker_remote_api_v1.8/#build
* -an-image-from-dockerfile-via-stdin
*
* @author marcus
*
Expand All @@ -44,26 +52,28 @@ public CreateImageCommand(String dockerFolder, String imageTag) {
public String getDockerFolder() {
return dockerFolder;
}

public String getImageTag() {
return imageTag;
}

@Override
public void execute(@SuppressWarnings("rawtypes") AbstractBuild build,
ConsoleLogger console) throws DockerException {
final ConsoleLogger console) throws DockerException {

if (dockerFolder == null) {
throw new IllegalArgumentException("dockerFolder is not configured");
}

if (imageTag == null) {
throw new IllegalArgumentException("imageTag is not configured");
}

String expandedDockerFolder = expandEnvironmentVariables(dockerFolder, build, console);

String expandedImageTag = expandEnvironmentVariables(imageTag, build, console);

String expandedDockerFolder = expandEnvironmentVariables(dockerFolder,
build, console);

String expandedImageTag = expandEnvironmentVariables(imageTag, build,
console);

FilePath folder = new FilePath(new File(expandedDockerFolder));

Expand All @@ -80,72 +90,95 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build,
DockerClient client = getClient();

try {

File docker = new File(expandedDockerFolder);

console.logInfo("Creating docker image from "
+ docker.getAbsolutePath());

ClientResponse response = client.build(docker, expandedImageTag);

if(response.getStatus() != Response.Status.OK.getStatusCode()) {
throw new RuntimeException("Error while calling docker remote api: " + response.getEntity(String.class));
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
throw new RuntimeException(
"Error while calling docker remote api: "
+ response.getEntity(String.class));
}

final List<JsonObject> errors = new ArrayList<JsonObject>();

try {

boolean errorOccured = false;

LineIterator itr = IOUtils.lineIterator(response.getEntityInputStream(), "UTF-8");
while (itr.hasNext()) {
String line = itr.next();

for (String s:line.split("(?<=\\})(?=\\{)")) {

JsonReader reader = Json.createReader(new StringReader(s));
JsonObject json = reader.readObject();
if(json.containsKey("stream")) {

InputStream istream = response.getEntityInputStream();

readJsonStream(istream, new JsonObjectCallback() {
public void callback(JsonObject json) {
if (json.containsKey("stream")) {
console.log(json.getString("stream"));
} else if(json.containsKey("status")) {
} else if (json.containsKey("status")) {
console.log(json.getString("status"));
} else {
errorOccured = true;
} else {
errors.add(json);
console.logError(json.toString());
}

}

}

if(errorOccured) {
build.setResult(Result.FAILURE);
} else {
console.logInfo("Sucessfully created image " + expandedImageTag);
}


}
}
});

if (!errors.isEmpty()) {
build.setResult(Result.FAILURE);
} else {
console.logInfo("Sucessfully created image "
+ expandedImageTag);
}

} finally {
IOUtils.closeQuietly(response.getEntityInputStream());
}



} catch (Exception e) {
throw new RuntimeException(e);
}

}

private String expandEnvironmentVariables(String string, AbstractBuild build,
ConsoleLogger console) {
private interface JsonObjectCallback {
void callback(JsonObject jsonObject);
}

private void readJsonStream(InputStream istream, JsonObjectCallback callback)
throws IOException, UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[8192];
int count;
while ((count = istream.read(buffer)) > 0) {
baos.write(buffer, 0, count);
String s = new String(baos.toByteArray(), "UTF-8");
JsonReader reader = Json.createReader(new StringReader(s));
JsonObject json = null;

try {
json = reader.readObject();
} catch (JsonParsingException e) {
// just ignore and continue
continue;
}

baos.close();
baos = new ByteArrayOutputStream();

callback.callback(json);
}
}

private String expandEnvironmentVariables(String string,
@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLogger console) {
try {
return build.getEnvironment(console.getListener()).expand(string);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


@Extension
public static class CreateImageCommandDescriptor extends
DockerCommandDescriptor {
Expand Down

0 comments on commit 742de2b

Please sign in to comment.