Search in sources :

Example 6 with CommandBuilder

use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.

the class WorkflowPluginTest method sshGitInsideDocker.

@Category(DockerTest.class)
@WithDocker
@WithPlugins({ "workflow-cps@2.0", "workflow-job@2.0", "workflow-durable-task-step@2.0", "docker-workflow@1.5", "git", "ssh-agent@1.10", "ssh-slaves@1.11" })
@WithCredentials(credentialType = WithCredentials.SSH_USERNAME_PRIVATE_KEY, values = { "git", "/org/jenkinsci/test/acceptance/docker/fixtures/GitContainer/unsafe" }, id = "gitcreds")
@Issue("JENKINS-27152")
@Test
public void sshGitInsideDocker() throws Exception {
    GitContainer gitContainer = gitServer.get();
    try (GitRepo repo = new GitRepo()) {
        repo.changeAndCommitFoo("Initial commit");
        repo.transferToDockerContainer(gitContainer.host(), gitContainer.port());
        DumbSlave slave = jenkins.slaves.create(DumbSlave.class);
        slave.setExecutors(1);
        // TODO perhaps should be a constant in SshdContainer
        slave.remoteFS.set("/home/test");
        SshSlaveLauncher launcher = slave.setLauncher(SshSlaveLauncher.class);
        Process proc = new ProcessBuilder("stat", "-c", "%g", "/var/run/docker.sock").start();
        String group = IOUtils.toString(proc.getInputStream()).trim();
        Assume.assumeThat("docker.sock can be statted", proc.waitFor(), is(0));
        try {
            Integer.parseInt(group);
        } catch (NumberFormatException x) {
            Assume.assumeNoException("unexpected output from stat on docker.sock", x);
        }
        // Note that we need to link to the Git container both on the agent, for the benefit of JGit (TODO pending JENKINS-30600), and in the build container, for the benefit of git-pull:
        DockerAgentContainer agentContainer = agent.starter().withOptions(new CommandBuilder("--volume=/var/run/docker.sock:/var/run/docker.sock", "--env=DOCKER_GROUP=" + group, "--link=" + gitContainer.getCid() + ":git")).start();
        launcher.host.set(agentContainer.ipBound(22));
        launcher.port(agentContainer.port(22));
        launcher.pwdCredentials("test", "test");
        launcher.setSshHostKeyVerificationStrategy(SshSlaveLauncher.NonVerifyingKeyVerificationStrategy.class);
        slave.save();
        {
            // TODO JENKINS-30600 workaround
            JGitInstallation.addJGit(jenkins);
            find(by.button("Save")).click();
        }
        WorkflowJob job = jenkins.jobs.create(WorkflowJob.class);
        String networkOptions = StringUtils.isNotBlank(System.getenv("DOCKER_FIXTURES_NETWORK")) ? " --network=" + System.getenv("DOCKER_FIXTURES_NETWORK") : "";
        String options = "--link=" + gitContainer.getCid() + ":git" + networkOptions;
        String repoUrl = StringUtils.isNotBlank(System.getenv("DOCKER_FIXTURES_NETWORK")) ? gitContainer.getRepoUrlInsideDocker() : gitContainer.getRepoUrlInsideDocker("git");
        job.script.set("node('" + slave.getName() + "') {\n" + "  docker.image('cloudbees/java-build-tools').inside('" + options + "') {\n" + // TODO JENKINS-30600: "    git url: '" + gitContainer.getRepoUrlInsideDocker("git") + "', credentialsId: 'gitcreds'\n" +
        "    checkout([$class: 'GitSCM', userRemoteConfigs: [[url: '" + repoUrl + "', credentialsId: 'gitcreds']], gitTool: 'jgit'])\n" + "    sh 'mkdir ~/.ssh && echo StrictHostKeyChecking no > ~/.ssh/config'\n" + "    sshagent(['gitcreds']) {sh 'ls -l $SSH_AUTH_SOCK && git pull origin master'}\n" + "  }\n" + "}");
        job.sandbox.check();
        job.save();
        assertThat(job.startBuild().shouldSucceed().getConsole(), containsString("-> FETCH_HEAD"));
    }
}
Also used : SshSlaveLauncher(org.jenkinsci.test.acceptance.plugins.ssh_slaves.SshSlaveLauncher) GitContainer(org.jenkinsci.test.acceptance.docker.fixtures.GitContainer) GitRepo(org.jenkinsci.test.acceptance.plugins.git.GitRepo) DockerAgentContainer(org.jenkinsci.test.acceptance.docker.fixtures.DockerAgentContainer) Matchers.containsString(org.hamcrest.Matchers.containsString) DumbSlave(org.jenkinsci.test.acceptance.po.DumbSlave) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) WorkflowJob(org.jenkinsci.test.acceptance.po.WorkflowJob) Category(org.junit.experimental.categories.Category) WithCredentials(org.jenkinsci.test.acceptance.junit.WithCredentials) Issue(org.jvnet.hudson.test.Issue) AbstractJUnitTest(org.jenkinsci.test.acceptance.junit.AbstractJUnitTest) Test(org.junit.Test) DockerTest(org.jenkinsci.test.acceptance.junit.DockerTest) WithPlugins(org.jenkinsci.test.acceptance.junit.WithPlugins) WithDocker(org.jenkinsci.test.acceptance.junit.WithDocker)

Example 7 with CommandBuilder

use of org.jenkinsci.utils.process.CommandBuilder in project blueocean-plugin by jenkinsci.

the class BlueOceanWinstoneController method startProcess.

@Override
public ProcessInputStream startProcess() throws IOException {
    File javaHome = getJavaHome();
    String java = javaHome == null ? "java" : String.format("%s/bin/java", javaHome.getAbsolutePath());
    CommandBuilder cb = new CommandBuilder(java);
    if (JAVA_OPTS != null && !JAVA_OPTS.isEmpty()) {
        cb.addAll(JAVA_OPTS);
    }
    cb.add("-Duser.language=en", "-jar", war, "--ajp13Port=-1", "--httpPort=" + httpPort);
    cb.env.putAll(commonLaunchEnv());
    System.out.println("Starting Jenkins: " + cb.toString());
    return cb.popen();
}
Also used : CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) File(java.io.File)

Example 8 with CommandBuilder

use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.

the class WinstoneController method startProcess.

@Override
public ProcessInputStream startProcess() throws IOException {
    File javaHome = getJavaHome();
    String java = javaHome == null ? "java" : String.format("%s/bin/java", javaHome.getAbsolutePath());
    CommandBuilder cb = new CommandBuilder(java);
    cb.addAll(JENKINS_JAVA_OPTS);
    cb.addAll(JAVA_OPTS);
    cb.add("-Duser.language=en", "-Djenkins.formelementpath.FormElementPathPageDecorator.enabled=true", "-jar", war, "--ajp13Port=-1", "--httpPort=" + httpPort);
    cb.addAll(JENKINS_OPTS);
    cb.env.putAll(commonLaunchEnv());
    LOGGER.info("Starting Jenkins: " + cb.toString());
    return cb.popen();
}
Also used : CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) File(java.io.File)

Example 9 with CommandBuilder

use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.

the class TomcatController method stopNow.

@Override
public void stopNow() throws IOException {
    System.out.println("    Stopping a temporary Jenkins/Tomcat instance\n");
    CommandBuilder cb = new CommandBuilder(catalinaHome + "/bin/shutdown.sh");
    try {
        int status = cb.system();
        if (status != 0) {
            throw new RuntimeException("Cannot stop Tomcat");
        }
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : IOException(java.io.IOException) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder)

Example 10 with CommandBuilder

use of org.jenkinsci.utils.process.CommandBuilder in project acceptance-test-harness by jenkinsci.

the class WinstoneDockerController method startProcess.

@Override
public ProcessInputStream startProcess() throws IOException {
    try {
        // can't mount symlink very well, so we need to resolve it
        File war = this.war.getCanonicalFile();
        CommandBuilder opts = new CommandBuilder();
        opts.add("-v", getJenkinsHome() + ":/work");
        opts.add("-v", war.getParent() + ":/war");
        // TODO: unify ID and fixture
        DockerImage img;
        if (dockerImage != null)
            img = new DockerImage(dockerImage);
        else
            img = docker.build(fixtureType);
        container = img.start(fixtureType).withOptions(opts).start();
        CommandBuilder cmds = new CommandBuilder();
        cmds.add("java");
        cmds.add("-DJENKINS_HOME=/work");
        cmds.add("-Djenkins.formelementpath.FormElementPathPageDecorator.enabled=true");
        cmds.add("-jar", "/war/" + war.getName());
        cmds.add("--ajp13Port=-1", "--controlPort=8081", "--httpPort=8080");
        return container.popen(cmds);
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException("Failed to launch winstone").initCause(e);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DockerImage(org.jenkinsci.test.acceptance.docker.DockerImage) CommandBuilder(org.jenkinsci.utils.process.CommandBuilder) File(java.io.File)

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