use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.
the class ToolInstallation method fakeHome.
protected String fakeHome(String binary, String homeEnvName) {
try {
final File home = File.createTempFile("toolhome", binary);
home.delete();
new File(home, "bin").mkdirs();
home.deleteOnExit();
if (SystemUtils.IS_OS_UNIX) {
final String path = new CommandBuilder("which", binary).popen().asText().trim();
final String code = String.format("#!/bin/sh\nexport %s=\nexec %s \"$@\"\n", homeEnvName, path);
final File command = new File(home, "bin/" + binary);
FileUtils.writeStringToFile(command, code);
command.setExecutable(true);
} else {
String path = new CommandBuilder("where.exe", binary).popen().asText().trim();
// where will return all matches and we only want the first.
path = path.replaceAll("\r\n.*", "");
final String code = String.format("set %s=\r\ncall %s %%*\r\n", homeEnvName, path);
final File command = new File(home, "bin/" + binary + ".cmd");
FileUtils.writeStringToFile(command, code);
command.setExecutable(true);
}
return home.getAbsolutePath();
} catch (IOException ex) {
throw new Error(ex);
} catch (InterruptedException ex) {
throw new Error(ex);
}
}
use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.
the class DeployPluginTest method deploy_sample_webapp_to_tomcat7.
@Test
@Native("bash")
@WithCredentials(credentialType = WithCredentials.USERNAME_PASSWORD, values = { "admin", "tomcat" }, id = "tomcat")
public void deploy_sample_webapp_to_tomcat7() throws IOException, InterruptedException {
if (SystemUtils.IS_OS_WINDOWS) {
// TODO move somewhere else...
String path = new CommandBuilder("where.exe", "bash.exe").popen().asText().trim();
// where will return all matches and we only want the first.
path = path.replaceAll("\r\n.*", "");
JenkinsConfig conf = jenkins.getConfigPage();
JenkinsConfig cp = jenkins.getConfigPage();
cp.configure();
cp.setShell(path);
cp.save();
}
Tomcat7Container f = docker.get();
FreeStyleJob j = jenkins.jobs.create();
j.configure();
ShellBuildStep s;
{
s = j.addShellStep(resource("/deploy_plugin/build-war.sh"));
DeployPublisher d = j.addPublisher(DeployPublisher.class);
d.war.set("my-webapp/target/*.war");
d.contextPath.set("test");
d.useContainer("Tomcat 7.x Remote", "Tomcat 7.x");
d.setCredentials("tomcat");
d.url.set(f.getUrl().toExternalForm());
}
j.save();
Build b = j.startBuild().shouldSucceed();
b.shouldContainsConsoleOutput("to container Tomcat 7.x Remote");
assertThat(readText(f), containsString("Hello World!"));
j.configure();
s.command("cd my-webapp && echo '<html><body>Hello Jenkins</body></html>' > src/main/webapp/index.jsp && mvn install");
j.save();
b = j.startBuild().shouldSucceed();
b.shouldContainsConsoleOutput("Redeploying");
assertThat(readText(f), containsString("Hello Jenkins"));
}
use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.
the class TSRDockerImage method start.
/**
* Starts a container from this image.
*/
public <T extends DockerContainer> T start(Class<T> type, int[] ports, int localPortOffset, String ipAddress, CommandBuilder options, CommandBuilder cmd) throws InterruptedException, IOException {
CommandBuilder docker = Docker.cmd("run");
File cidFile = File.createTempFile("docker", "cid");
cidFile.delete();
// strange behaviour in some docker version cidfile needs to come before
docker.add("--cidfile=" + cidFile);
for (int p : ports) {
if (// No manual offset, let docker figure out the best port for itself
localPortOffset == 0) {
docker.add("-p", ipAddress + "::" + p);
} else {
int localPort = localPortOffset + p;
docker.add("-p", ipAddress + ":" + localPort + ":" + p);
}
}
docker.add(options);
docker.add(tag);
docker.add(cmd);
// initially create a log file here
File tmplog = File.createTempFile("docker", "log");
Process p = docker.build().redirectInput(new File("/dev/null")).redirectErrorStream(true).redirectOutput(tmplog).start();
Thread.sleep(1000);
if (cidFile.exists()) {
try {
p.exitValue();
throw new IOException("docker died unexpectedly: " + docker + "\n" + FileUtils.readFileToString(tmplog));
} catch (IllegalThreadStateException e) {
// Docker is still running okay.
}
String cid;
do {
Thread.sleep(500);
cid = FileUtils.readFileToString(cidFile);
} while (cid == null || cid.length() == 0);
// rename the log file to match the container name
File logfile = new File("/tmp/" + cidFile + ".log");
tmplog.renameTo(logfile);
System.out.printf("Launching Docker container %s: logfile is at %s\n", cid, logfile);
try {
T t = type.newInstance();
t.init(cid, p, logfile);
return t;
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
} else {
try {
p.exitValue();
throw new IOException("docker died unexpectedly: " + docker + "\n" + FileUtils.readFileToString(tmplog));
} catch (IllegalThreadStateException e) {
throw new IOException("docker didn't leave CID file yet still running. Huh?: " + docker + "\n" + FileUtils.readFileToString(tmplog));
}
}
}
use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.
the class RemoteJenkinsController method startNow.
@Override
public void startNow() throws IOException {
CommandBuilder cb = new CommandBuilder("ssh", "-i", privateKeyLocation.getAbsolutePath(), "-t", "-oStrictHostKeyChecking=no", String.format("%s@%s", machine.getUser(), machine.getPublicIpAddress())).add("java -DJENKINS_HOME=" + jenkinsHome + " -jar " + jenkinsWarLocation + " --ajp13Port=-1" + " --controlPort=" + controlPort + " --httpPort=" + httpPort);
localLogger.info("Launching Jenkins: " + cb);
this.process = cb.popen();
/**
* Write to System.out so that JUnit Attachment Plugin catches up the log
* https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin
*/
System.out.println(String.format("[[ATTACHMENT|%s]]", logFile.getAbsolutePath()));
logWatcher = new JenkinsLogWatcher(getLogId(), process, logFile, getLogPrinter());
logWatcher.start();
try {
this.logWatcher.waitTillReady();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations