Search in sources :

Example 1 with SshClient

use of org.jclouds.ssh.SshClient in project fabric8 by jboss-fuse.

the class CloudContainerInstallationTask method uploadToNode.

private void uploadToNode(ComputeServiceContext context, NodeMetadata node, LoginCredentials credentials, URL url, String path) {
    Utils utils = context.utils();
    SshClient ssh = credentials != null ? utils.sshForNode().apply(NodeMetadataBuilder.fromNodeMetadata(nodeMetadata).credentials(credentials).build()) : utils.sshForNode().apply(node);
    try (InputStream is = url.openStream()) {
        ssh.connect();
        File distro = Files.createTempFile("/tmp");
        Files.copy(is, new FileOutputStream(distro));
        ssh.put(path, Payloads.newFilePayload(distro));
        distro.delete();
    } catch (IOException e) {
        LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven.");
    } finally {
        if (ssh != null) {
            ssh.disconnect();
        }
    }
}
Also used : SshClient(org.jclouds.ssh.SshClient) Utils(org.jclouds.compute.Utils) ContainerProviderUtils(io.fabric8.internal.ContainerProviderUtils) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 2 with SshClient

use of org.jclouds.ssh.SshClient in project SimianArmy by Netflix.

the class ChaosInstance method canConnectSsh.

/**
 * Check if the SSH credentials are working.
 *
 * This is cached for the duration of this object.
 *
 * @return true iff ssh is configured and able to log on to instance.
 */
public boolean canConnectSsh(ChaosInstance instance) {
    if (!sshConfig.isEnabled()) {
        return false;
    }
    if (canConnectSsh == null) {
        try {
            // It would be nicer to keep this connection open, but then we'd have to be closed.
            SshClient client = connectSsh();
            client.disconnect();
            canConnectSsh = true;
        } catch (Exception e) {
            LOGGER.warn("Error making SSH connection to instance", e);
            canConnectSsh = false;
        }
    }
    return canConnectSsh;
}
Also used : SshClient(org.jclouds.ssh.SshClient)

Example 3 with SshClient

use of org.jclouds.ssh.SshClient in project SimianArmy by Netflix.

the class ChaosInstance method connectSsh.

/**
 * Connect to the instance over SSH.
 *
 * @return {@link SshClient} for connection
 */
public SshClient connectSsh() {
    if (!sshConfig.isEnabled()) {
        throw new IllegalStateException();
    }
    LoginCredentials credentials = sshConfig.getCredentials();
    SshClient ssh = cloudClient.connectSsh(instanceId, credentials);
    return ssh;
}
Also used : LoginCredentials(org.jclouds.domain.LoginCredentials) SshClient(org.jclouds.ssh.SshClient)

Example 4 with SshClient

use of org.jclouds.ssh.SshClient in project SimianArmy by Netflix.

the class ScriptChaosType method apply.

/**
 * Runs the script.
 */
@Override
public void apply(ChaosInstance instance) {
    LOGGER.info("Running script for {} on instance {}", getKey(), instance.getInstanceId());
    SshClient ssh = instance.connectSsh();
    String filename = getKey().toLowerCase() + ".sh";
    URL url = Resources.getResource(ScriptChaosType.class, "/scripts/" + filename);
    String script;
    try {
        script = Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException("Error reading script resource", e);
    }
    ssh.put("/tmp/" + filename, script);
    ExecResponse response = ssh.exec("/bin/bash /tmp/" + filename);
    if (response.getExitStatus() != 0) {
        LOGGER.warn("Got non-zero output from running script: {}", response);
    }
    ssh.disconnect();
}
Also used : SshClient(org.jclouds.ssh.SshClient) ExecResponse(org.jclouds.compute.domain.ExecResponse) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with SshClient

use of org.jclouds.ssh.SshClient in project SimianArmy by Netflix.

the class AWSClient method connectSsh.

@Override
public SshClient connectSsh(String instanceId, LoginCredentials credentials) {
    ComputeService computeService = getJcloudsComputeService();
    String jcloudsId = getJcloudsId(instanceId);
    NodeMetadata node = getJcloudsNode(computeService, jcloudsId);
    node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(credentials).build();
    Utils utils = computeService.getContext().utils();
    SshClient ssh = utils.sshForNode().apply(node);
    ssh.connect();
    return ssh;
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) SshClient(org.jclouds.ssh.SshClient) Utils(org.jclouds.compute.Utils) ComputeService(org.jclouds.compute.ComputeService)

Aggregations

SshClient (org.jclouds.ssh.SshClient)5 IOException (java.io.IOException)2 Utils (org.jclouds.compute.Utils)2 ContainerProviderUtils (io.fabric8.internal.ContainerProviderUtils)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ComputeService (org.jclouds.compute.ComputeService)1 ExecResponse (org.jclouds.compute.domain.ExecResponse)1 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)1 LoginCredentials (org.jclouds.domain.LoginCredentials)1