Search in sources :

Example 6 with Ssh

use of org.jenkinsci.test.acceptance.Ssh in project acceptance-test-harness by jenkinsci.

the class RemoteJenkinsProvider method createNewJenkinsController.

private JenkinsController createNewJenkinsController() {
    Machine machine = machineProvider.get();
    try {
        // install jenkins WAR
        jenkinsResolver.materialize(machine, jenkinsWar);
    } catch (Exception e) {
        logger.error("Error during setting up Jenkins: " + e.getMessage(), e);
        throw new AssertionError(e);
    }
    String jenkinsHome = machine.dir() + newJenkinsHome() + "/";
    String pluginDir = jenkinsHome + "plugins/";
    String path = JenkinsResolver.JENKINS_TEMP_DIR + "form-element-path.hpi";
    // install form-path-element plugin
    new PluginDownloader("form-element-path").materialize(machine, path);
    try (Ssh ssh = machine.connect()) {
        ssh.executeRemoteCommand("mkdir -p " + pluginDir);
        ssh.executeRemoteCommand(String.format("cp %s %s", path, pluginDir));
    }
    return new RemoteJenkinsController(injector, machine, jenkinsHome, jenkinsWar, privateKeyFile);
}
Also used : PluginDownloader(org.jenkinsci.test.acceptance.resolver.PluginDownloader) Ssh(org.jenkinsci.test.acceptance.Ssh) RemoteJenkinsController(org.jenkinsci.test.acceptance.controller.RemoteJenkinsController) IOException(java.io.IOException)

Example 7 with Ssh

use of org.jenkinsci.test.acceptance.Ssh in project acceptance-test-harness by jenkinsci.

the class TSREc2Provider method postStartupSetup.

@Override
public void postStartupSetup(NodeMetadata node) {
    String host = node.getPublicAddresses().iterator().next();
    try (Ssh ssh = new Ssh(host)) {
        authenticator().authenticate(ssh.getConnection());
        ssh.getConnection().exec(String.format("pkill -u $(id -u %s)", config.getUser()), System.out);
        // set instance initiated shutdown behavior to terminate. This means any shutdown will result in to instance
        // termination
        EC2Api client = contextBuilder.buildApi(EC2Api.class);
        client.getInstanceApi().get().setInstanceInitiatedShutdownBehaviorForInstanceInRegion(config.getRegion(), node.getProviderId(), Volume.InstanceInitiatedShutdownBehavior.TERMINATE);
        copyAutoterminateScript(host);
    } catch (IOException | InterruptedException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) Ssh(org.jenkinsci.test.acceptance.Ssh) IOException(java.io.IOException)

Example 8 with Ssh

use of org.jenkinsci.test.acceptance.Ssh in project acceptance-test-harness by jenkinsci.

the class TSREc2Provider method copyAutoterminateScript.

private void copyAutoterminateScript(String host) throws IOException, InterruptedException {
    // run terminate script
    URL script = this.getClass().getClassLoader().getResource("org/jenkinsci/test/acceptance/machine/autoterminate.sh");
    File tempScriptFile = File.createTempFile("autoterminate", ".sh");
    try {
        FileUtils.copyURLToFile(script, tempScriptFile);
        logger.info(String.format("Executing auto-terminate script on remote machine: %s, it will terminate after 180 minutes of " + "inactivity.", host));
        try (Ssh ssh = new Ssh(host)) {
            authenticator().authenticate(ssh.getConnection());
            ssh.copyTo(tempScriptFile.getAbsolutePath(), "autoterminate.sh", ".");
            ssh.executeRemoteCommand("chmod +x ./autoterminate.sh ; touch nohup.out");
            // wait for 3 hours before termination
            ssh.getConnection().exec(String.format("nohup ./autoterminate.sh %s %s `</dev/null` >nohup.out 2>&1 &", config.getUser(), AUTO_TERMINATE_TIMEOUT), System.out);
        }
    } finally {
        if (!tempScriptFile.delete()) {
            tempScriptFile.deleteOnExit();
        }
    }
}
Also used : Ssh(org.jenkinsci.test.acceptance.Ssh) File(java.io.File) URL(java.net.URL)

Example 9 with Ssh

use of org.jenkinsci.test.acceptance.Ssh in project acceptance-test-harness by jenkinsci.

the class RemoteJenkinsController method populateJenkinsHome.

@Override
public void populateJenkinsHome(byte[] _template, boolean clean) throws IOException {
    boolean running = isRunning();
    try (Ssh connection = machine.connect()) {
        stop();
        if (clean) {
            connection.executeRemoteCommand("rm -rf " + Ssh.escape(jenkinsHome) + "; mkdir -p " + Ssh.escape(jenkinsHome));
        }
        File template = File.createTempFile("template", ".dat");
        try {
            org.apache.commons.io.FileUtils.writeByteArrayToFile(template, _template);
            connection.copyTo(template.getAbsolutePath(), ".home-template.zip", jenkinsHome);
        } finally {
            template.delete();
        }
        String templateArchive = Ssh.escape(jenkinsHome + (jenkinsHome.endsWith("/") ? "" : "/") + ".home-template.zip");
        connection.executeRemoteCommand("unzip -o " + templateArchive + " -d " + Ssh.escape(jenkinsHome) + " && rm -f " + templateArchive);
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        if (running && !isRunning()) {
            start();
        }
    }
}
Also used : Ssh(org.jenkinsci.test.acceptance.Ssh) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 10 with Ssh

use of org.jenkinsci.test.acceptance.Ssh in project acceptance-test-harness by jenkinsci.

the class Ec2Provider method copyAutoterminateScript.

private void copyAutoterminateScript(String host) throws IOException, InterruptedException {
    // run terminate script
    URL script = this.getClass().getClassLoader().getResource("org/jenkinsci/test/acceptance/machine/autoterminate.sh");
    File tempScriptFile = File.createTempFile("autoterminate", ".sh");
    try {
        FileUtils.copyURLToFile(script, tempScriptFile);
        logger.info(String.format("Executing auto-terminate script on remote machine: %s, it will terminate after 180 minutes of " + "inactivity.", host));
        try (Ssh ssh = new Ssh(host)) {
            authenticator().authenticate(ssh.getConnection());
            ssh.copyTo(tempScriptFile.getAbsolutePath(), "autoterminate.sh", ".");
            ssh.executeRemoteCommand("chmod +x ./autoterminate.sh ; touch nohup.out");
            // wait for 3 hours before termination
            ssh.getConnection().exec(String.format("nohup ./autoterminate.sh %s %s `</dev/null` >nohup.out 2>&1 &", config.getUser(), AUTO_TERMINATE_TIMEOUT), System.out);
        }
    } finally {
        if (!tempScriptFile.delete()) {
            tempScriptFile.deleteOnExit();
        }
    }
}
Also used : Ssh(org.jenkinsci.test.acceptance.Ssh) File(java.io.File) URL(java.net.URL)

Aggregations

Ssh (org.jenkinsci.test.acceptance.Ssh)12 IOException (java.io.IOException)8 File (java.io.File)3 URL (java.net.URL)3 EC2Api (org.jclouds.ec2.EC2Api)2 InterruptedIOException (java.io.InterruptedIOException)1 MalformedURLException (java.net.MalformedURLException)1 RemoteJenkinsController (org.jenkinsci.test.acceptance.controller.RemoteJenkinsController)1 PluginDownloader (org.jenkinsci.test.acceptance.resolver.PluginDownloader)1