Skip to content
Permalink
Browse files

[FIXED JENKINS-23025] Expand parameters in all commands

  • Loading branch information
vjuranek committed May 20, 2014
1 parent d1e4d11 commit 67761a6ec915a0eeb51a5b00fbd235c783c80e08
@@ -5,6 +5,7 @@
import hudson.model.AbstractBuild;

import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerException;
@@ -57,13 +58,15 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

CommitConfig cfg = new CommitConfig(containerId);
String containerIdRes = Resolver.buildVar(build, containerId);

CommitConfig cfg = new CommitConfig(containerIdRes);
cfg.setRepo(repo);
cfg.setTag(tag);
cfg.setRun(runCmd);
String imageId = getClient().commit(cfg);

console.logInfo("Container " + containerId + " commited as image " + imageId);
console.logInfo("Container " + containerIdRes + " commited as image " + imageId);
}

@Extension
@@ -5,6 +5,7 @@

import org.jenkinsci.plugins.dockerbuildstep.action.EnvInvisibleAction;
import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerClient;
@@ -53,13 +54,18 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
if (image == null || image.isEmpty()) {
throw new IllegalArgumentException("At least one parameter is required");
}

String imageRes = Resolver.buildVar(build, image);
String commandRes = Resolver.buildVar(build, command);
String hostNameRes = Resolver.buildVar(build, hostName);

ContainerConfig cfg = new ContainerConfig();
cfg.setImage(image);
cfg.setCmd(new String[] { command });
cfg.setHostName(hostName);
cfg.setImage(imageRes);
cfg.setCmd(new String[] { commandRes });
cfg.setHostName(hostNameRes);
DockerClient client = getClient();
ContainerCreateResponse resp = client.createContainer(cfg);
console.logInfo("created container id " + resp.getId() + " (from image " + image + ")");
console.logInfo("created container id " + resp.getId() + " (from image " + imageRes + ")");

/*
* if (resp.getWarnings() != null) { for (String warn : resp.getWarnings()) System.out.println("WARN: " + warn);
@@ -22,6 +22,7 @@

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

import com.kpelykh.docker.client.DockerClient;
@@ -69,10 +70,13 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build,
throw new IllegalArgumentException("imageTag is not configured");
}

String expandedDockerFolder = expandEnvironmentVariables(dockerFolder,
String dockerFolderRes = Resolver.buildVar(build, dockerFolder);
String imageTagRes = Resolver.buildVar(build, imageTag);

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

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

FilePath folder = new FilePath(new File(expandedDockerFolder));
@@ -7,6 +7,7 @@
import java.util.List;

import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerClient;
@@ -22,7 +23,7 @@
*/
public class KillCommand extends DockerCommand {

private String containerIds;
private final String containerIds;

@DataBoundConstructor
public KillCommand(String containerIds) {
@@ -40,7 +41,9 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

List<String> ids = Arrays.asList(containerIds.split(","));
String containerIdsRes = Resolver.buildVar(build, containerIds);

List<String> ids = Arrays.asList(containerIdsRes.split(","));
DockerClient client = getClient();
for (String id : ids) {
id = id.trim();
@@ -9,6 +9,7 @@
import javax.ws.rs.core.Response;

import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerClient;
@@ -57,14 +58,18 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

console.logInfo("Pulling image " + fromImage);
String fromImageRes = Resolver.buildVar(build, fromImage);
String tagRes = Resolver.buildVar(build, tag);
String registryRes = Resolver.buildVar(build, registry);

console.logInfo("Pulling image " + fromImageRes);
DockerClient client = getClient();
ClientResponse resp = client.pull(fromImage, tag, registry);
ClientResponse resp = client.pull(fromImageRes, tagRes, registryRes);
if (Response.Status.Family.SUCCESSFUL.equals(resp.getStatusInfo().getFamily())) {
console.logInfo("Downloading ... ");
} else {
console.logError("Something went wrong, response code is " + resp.getStatus() + ". Failing the build ...");
throw new AbortException("Failed to pull Docker image name " + fromImage);
throw new AbortException("Failed to pull Docker image name " + fromImageRes);
}

// wait for the image to be downloaded
@@ -73,7 +78,7 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
Thread.currentThread().sleep(15 * 1000); // wait 15 sec
} catch (InterruptedException e) {
// TODO log
throw new AbortException("Download of Docker image name " + fromImage + " was interrupted");
throw new AbortException("Download of Docker image name " + fromImageRes + " was interrupted");
}
}

@@ -7,6 +7,7 @@
import java.util.List;

import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerClient;
@@ -40,8 +41,10 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
if (containerIds == null || containerIds.isEmpty()) {
throw new IllegalArgumentException("At least one parameter is required");
}

String containerIdsRes = Resolver.buildVar(build, containerIds);

List<String> ids = Arrays.asList(containerIds.split(","));
List<String> ids = Arrays.asList(containerIdsRes.split(","));
DockerClient client = getClient();
for (String id : ids) {
id = id.trim();
@@ -7,6 +7,7 @@
import java.util.List;

import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;

import com.kpelykh.docker.client.DockerClient;
@@ -22,8 +23,8 @@
*/
public class RestartCommand extends DockerCommand {

private String containerIds;
private int timeout;
private final String containerIds;
private final int timeout;

@DataBoundConstructor
public RestartCommand(String containerIds, int timeout) {
@@ -45,7 +46,9 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

List<String> ids = Arrays.asList(containerIds.split(","));
String containerIdsRes = Resolver.buildVar(build, containerIds);

List<String> ids = Arrays.asList(containerIdsRes.split(","));
DockerClient client = getClient();
for(String id : ids) {
id = id.trim();
@@ -24,7 +24,7 @@
*/
public class StartByImageIdCommand extends DockerCommand {

private String imageId;
private final String imageId;

@DataBoundConstructor
public StartByImageIdCommand(String imageId) {
@@ -42,12 +42,12 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

imageId = Resolver.buildVar(build, imageId);
String imageIdRes = Resolver.buildVar(build, imageId);

DockerClient client = getClient();
List<Container> containers = getClient().listContainers(true);
for (Container c : containers) {
if (imageId.equalsIgnoreCase(c.getImage())) {
if (imageIdRes.equalsIgnoreCase(c.getImage())) {
client.startContainer(c.getId());
console.logInfo("started container id " + c.getId());

@@ -28,7 +28,7 @@
*/
public class StartCommand extends DockerCommand {

private String containerIds;
private final String containerIds;
private final String waitPorts;

@DataBoundConstructor
@@ -52,9 +52,9 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

containerIds = Resolver.buildVar(build, containerIds);
String containerIdsRes = Resolver.buildVar(build, containerIds);

List<String> ids = Arrays.asList(containerIds.split(","));
List<String> ids = Arrays.asList(containerIdsRes.split(","));
DockerClient client = getClient();
//TODO check, if container exists and is stopped (probably catch exception)
for (String id : ids) {
@@ -21,7 +21,7 @@
*/
public class StopByImageIdCommand extends DockerCommand {

private String imageId;
private final String imageId;

@DataBoundConstructor
public StopByImageIdCommand(String imageId) {
@@ -39,12 +39,12 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

imageId = Resolver.buildVar(build, imageId);
String imageIdRes = Resolver.buildVar(build, imageId);

DockerClient client = getClient();
List<Container> containers = getClient().listContainers(false);
for (Container c : containers) {
if (imageId.equalsIgnoreCase(c.getImage())) {
if (imageIdRes.equalsIgnoreCase(c.getImage())) {
client.stopContainer(c.getId());
console.logInfo("stop container id " + c.getId());
}
@@ -23,7 +23,7 @@
*/
public class StopCommand extends DockerCommand {

private String containerIds;
private final String containerIds;

@DataBoundConstructor
public StopCommand(String containerIds) {
@@ -41,9 +41,9 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
throw new IllegalArgumentException("At least one parameter is required");
}

containerIds = Resolver.buildVar(build, containerIds);
String containerIdsRes = Resolver.buildVar(build, containerIds);

List<String> ids = Arrays.asList(containerIds.split(","));
List<String> ids = Arrays.asList(containerIdsRes.split(","));
DockerClient client = getClient();
//TODO check, if container is actually running
for (String id : ids) {
@@ -1,9 +1,14 @@
package org.jenkinsci.plugins.dockerbuildstep.util;

import hudson.EnvVars;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.util.LogTaskListener;
import hudson.util.VariableResolver;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Convenient class for resolving/expanding various variabales.
*
@@ -16,10 +21,13 @@ public static String buildVar(final AbstractBuild<?, ?> build,final String toRes
VariableResolver<String> vr = build.getBuildVariableResolver();
String resolved = Util.replaceMacro(toResolve, vr);
try {
resolved = build.getEnvironment().expand(resolved); //TODO avoid deprecated method
EnvVars env = build.getEnvironment(new LogTaskListener(LOG, Level.INFO));
resolved = env.expand(resolved);
} catch (Exception e) {
//TODO no-op?
}
return resolved;
}

private static final Logger LOG = Logger.getLogger(Resolver.class.getName());
}

0 comments on commit 67761a6

Please sign in to comment.
You can’t perform that action at this time.