Search in sources :

Example 11 with CommandBuilder

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);
    }
}
Also used : IOException(java.io.IOException) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) File(java.io.File)

Example 12 with CommandBuilder

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"));
}
Also used : DeployPublisher(org.jenkinsci.test.acceptance.plugins.deploy.DeployPublisher) Build(org.jenkinsci.test.acceptance.po.Build) JenkinsConfig(org.jenkinsci.test.acceptance.po.JenkinsConfig) ShellBuildStep(org.jenkinsci.test.acceptance.po.ShellBuildStep) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Tomcat7Container(org.jenkinsci.test.acceptance.docker.fixtures.Tomcat7Container) FreeStyleJob(org.jenkinsci.test.acceptance.po.FreeStyleJob) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) Test(org.junit.Test)

Example 13 with CommandBuilder

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));
        }
    }
}
Also used : IOException(java.io.IOException) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) File(java.io.File)

Example 14 with CommandBuilder

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);
    }
}
Also used : CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Aggregations

CommandBuilder (org.jenkinsci.utils.process.CommandBuilder)14 File (java.io.File)9 IOException (java.io.IOException)6 InterruptedIOException (java.io.InterruptedIOException)3 DockerImage (org.jenkinsci.test.acceptance.docker.DockerImage)2 Test (org.junit.Test)2 MalformedURLException (java.net.MalformedURLException)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 DockerAgentContainer (org.jenkinsci.test.acceptance.docker.fixtures.DockerAgentContainer)1 GitContainer (org.jenkinsci.test.acceptance.docker.fixtures.GitContainer)1 SAMLContainer (org.jenkinsci.test.acceptance.docker.fixtures.SAMLContainer)1 Tomcat7Container (org.jenkinsci.test.acceptance.docker.fixtures.Tomcat7Container)1 AbstractJUnitTest (org.jenkinsci.test.acceptance.junit.AbstractJUnitTest)1 DockerTest (org.jenkinsci.test.acceptance.junit.DockerTest)1 WithCredentials (org.jenkinsci.test.acceptance.junit.WithCredentials)1 WithDocker (org.jenkinsci.test.acceptance.junit.WithDocker)1 WithPlugins (org.jenkinsci.test.acceptance.junit.WithPlugins)1 DeployPublisher (org.jenkinsci.test.acceptance.plugins.deploy.DeployPublisher)1 GitRepo (org.jenkinsci.test.acceptance.plugins.git.GitRepo)1