Search in sources :

Example 11 with SSHSession

use of io.cdap.cdap.runtime.spi.ssh.SSHSession in project cdap by caskdata.

the class SSHSessionTest method testForwardingOnSessionClose.

@Test
public void testForwardingOnSessionClose() throws Exception {
    EchoServer echoServer = new EchoServer();
    echoServer.startAndWait();
    try {
        SSHConfig sshConfig = getSSHConfig();
        AtomicBoolean finished = new AtomicBoolean(false);
        PortForwarding portForwarding;
        // Creates a SSH session
        try (SSHSession session = new DefaultSSHSession(sshConfig)) {
            InetSocketAddress bindAddress = echoServer.getBindAddress();
            // Creates a port forwarding and send some data
            BlockingQueue<String> received = new LinkedBlockingQueue<>();
            portForwarding = session.createLocalPortForward(bindAddress.getHostName(), bindAddress.getPort(), 12345, new PortForwarding.DataConsumer() {

                @Override
                public void received(ByteBuffer buffer) {
                    received.add(StandardCharsets.UTF_8.decode(buffer).toString());
                }

                @Override
                public void finished() {
                    finished.set(true);
                }
            });
            portForwarding.write(StandardCharsets.UTF_8.encode("Testing"));
            portForwarding.flush();
            Assert.assertEquals("Testing", received.poll(5, TimeUnit.SECONDS));
        }
        // After closing of the SSH session, the port forwarding should be closed as well
        Assert.assertTrue(finished.get());
        // Writing to a closed port forwarding should fails.
        try {
            portForwarding.write(StandardCharsets.UTF_8.encode("Testing 2"));
            Assert.fail("Expected failure when writing to closed PortForwarding");
        } catch (IOException e) {
        // expected
        }
    } finally {
        echoServer.stopAndWait();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) PortForwarding(io.cdap.cdap.runtime.spi.ssh.PortForwarding) RemotePortForwarding(io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding) Test(org.junit.Test)

Example 12 with SSHSession

use of io.cdap.cdap.runtime.spi.ssh.SSHSession in project cdap by caskdata.

the class SSHSessionTest method testScp.

@Test
public void testScp() throws Exception {
    SSHConfig config = getSSHConfig();
    // Generate some content
    File file = TEMP_FOLDER.newFile();
    try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
        for (int i = 0; i < 10; i++) {
            writer.write("Message " + i);
            writer.newLine();
        }
    }
    // SCP the file to the given directory
    File targetFolder = TEMP_FOLDER.newFolder();
    try (SSHSession session = new DefaultSSHSession(config)) {
        session.copy(file.toPath(), targetFolder.getAbsolutePath());
    }
    // Verify
    File uploadedFile = new File(targetFolder, file.getName());
    Assert.assertTrue(uploadedFile.exists());
    Assert.assertArrayEquals(Files.readAllBytes(file.toPath()), Files.readAllBytes(uploadedFile.toPath()));
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 13 with SSHSession

use of io.cdap.cdap.runtime.spi.ssh.SSHSession in project cdap by caskdata.

the class RemoteHadoopProvisioner method deleteCluster.

@Override
public void deleteCluster(ProvisionerContext context, Cluster cluster) {
    // delete jars copied over by CDAP
    // TODO: (CDAP-13795) move this logic into the platform
    String programName = context.getProgramRunInfo().getProgram();
    String runId = context.getProgramRunInfo().getRun();
    String remoteIP = getMasterExternalIp(cluster);
    try (SSHSession session = createSSHSession(context, remoteIP)) {
        LOG.debug("Cleaning up remote cluster resources for program {} run {}", programName, runId);
        session.executeAndWait(String.format("rm -rf %s", runId));
        LOG.debug("Completed remote cluster clean up for program {} run {}", programName, runId);
    } catch (IOException e) {
        LOG.warn("Unable to clean up resources for program {} run {} on the remote cluster. " + "The run directory may need to be manually deleted on cluster node {}.", programName, runId, remoteIP, e);
    }
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) IOException(java.io.IOException)

Example 14 with SSHSession

use of io.cdap.cdap.runtime.spi.ssh.SSHSession in project cdap by caskdata.

the class RemoteHadoopProvisioner method initializeCluster.

@Override
public void initializeCluster(ProvisionerContext context, Cluster cluster) throws Exception {
    RemoteHadoopConf conf = RemoteHadoopConf.fromProperties(context.getProperties());
    String initializationAction = conf.getInitializationAction();
    if (initializationAction != null && !initializationAction.isEmpty()) {
        try (SSHSession session = createSSHSession(context, getMasterExternalIp(cluster))) {
            LOG.debug("Initializing action: {}", initializationAction);
            String output = session.executeAndWait(initializationAction);
            LOG.debug("Initialization action completed: {}", output);
        }
    }
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession)

Example 15 with SSHSession

use of io.cdap.cdap.runtime.spi.ssh.SSHSession in project cdap by caskdata.

the class SSHRemoteExecutionService method createServiceProxyTunnel.

/**
 * Creates a remote port forwarding SSH tunnel for remote runtime to access CDAP services.
 */
private SSHSession createServiceProxyTunnel() throws IOException {
    SSHSession session = new DefaultSSHSession(sshConfig);
    ProgramRunId programRunId = getProgramRunId();
    // Creates a new remote port forwarding from the session.
    // We don't need to care about closing the forwarding as it will last until the session get closed,
    // in which the remote port forwarding will be closed automatically
    int remotePort = session.createRemotePortForward(0, serviceSocksProxyPort).getRemotePort();
    LOG.debug("Service SOCKS proxy started on port {} for program run {}", remotePort, programRunId);
    ServiceSocksProxyInfo info = new ServiceSocksProxyInfo(remotePort);
    // Upload the service socks proxy information to the remote runtime
    String targetPath = session.executeAndWait("echo `pwd`/" + programRunId.getRun()).trim();
    session.executeAndWait("mkdir -p " + targetPath);
    byte[] content = GSON.toJson(info).getBytes(StandardCharsets.UTF_8);
    String targetFileName = Constants.RuntimeMonitor.SERVICE_PROXY_FILE + "-" + programRunId.getRun() + ".json";
    String tmpFileName = targetFileName + "-" + System.currentTimeMillis() + ".tmp";
    // noinspection OctalInteger
    session.copy(new ByteArrayInputStream(content), "/tmp", tmpFileName, content.length, 0600, null, null);
    session.executeAndWait(String.format("mv /tmp/%s /tmp/%s", tmpFileName, targetFileName));
    LOG.debug("Service proxy file uploaded to remote runtime for program run {}", programRunId);
    return session;
}
Also used : ServiceSocksProxyInfo(io.cdap.cdap.internal.app.runtime.monitor.ServiceSocksProxyInfo) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession)

Aggregations

SSHSession (io.cdap.cdap.runtime.spi.ssh.SSHSession)22 Test (org.junit.Test)10 DefaultSSHSession (io.cdap.cdap.common.ssh.DefaultSSHSession)8 IOException (java.io.IOException)8 RemotePortForwarding (io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding)6 InetSocketAddress (java.net.InetSocketAddress)6 PortForwarding (io.cdap.cdap.runtime.spi.ssh.PortForwarding)4 SSHProcess (io.cdap.cdap.runtime.spi.ssh.SSHProcess)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 ByteBuffer (java.nio.ByteBuffer)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 JSchException (com.jcraft.jsch.JSchException)2 ServiceSocksProxyInfo (io.cdap.cdap.internal.app.runtime.monitor.ServiceSocksProxyInfo)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2 Cluster (io.cdap.cdap.runtime.spi.provisioner.Cluster)2 ClusterStatus (io.cdap.cdap.runtime.spi.provisioner.ClusterStatus)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2