Search in sources :

Example 1 with SSHSession

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

the class RemoteExecutionTwillPreparer method launch.

@Override
protected void launch(TwillRuntimeSpecification twillRuntimeSpec, RuntimeSpecification runtimeSpec, JvmOptions jvmOptions, Map<String, String> environments, Map<String, LocalFile> localFiles, TimeoutChecker timeoutChecker) throws Exception {
    try (SSHSession session = new DefaultSSHSession(sshConfig)) {
        // Validate kerberos setting if any
        Map<String, String> clusterProperties = cluster.getProperties();
        String principal = clusterProperties.get(ClusterProperties.KERBEROS_PRINCIPAL);
        String keytab = clusterProperties.get(ClusterProperties.KERBEROS_KEYTAB);
        validateKerberos(principal, keytab, session);
        String targetPath = session.executeAndWait("mkdir -p ./" + getProgramRunId().getRun(), "echo `pwd`/" + getProgramRunId().getRun()).trim();
        // Upload files
        localizeFiles(session, localFiles, targetPath, runtimeSpec);
        // Upload secrets (service socks proxy, runtime token)
        localizeSecrets(session, targetPath);
        // Currently we only support one TwillRunnable
        String runnableName = runtimeSpec.getName();
        int memory = Resources.computeMaxHeapSize(runtimeSpec.getResourceSpecification().getMemorySize(), twillRuntimeSpec.getReservedMemory(runnableName), twillRuntimeSpec.getMinHeapRatio(runnableName));
        // Spark env setup script
        session.executeAndWait(String.format("bash %s/%s/%s %s/%s/%s %s/%s", targetPath, Constants.Files.RUNTIME_CONFIG_JAR, SETUP_SPARK_SH, targetPath, Constants.Files.RUNTIME_CONFIG_JAR, SETUP_SPARK_PY, targetPath, SPARK_ENV_SH));
        // Generates the launch script
        byte[] scriptContent = generateLaunchScript(targetPath, runnableName, memory, jvmOptions, environments, principal, keytab).getBytes(StandardCharsets.UTF_8);
        // noinspection OctalInteger
        session.copy(new ByteArrayInputStream(scriptContent), targetPath, "launcher.sh", scriptContent.length, 0755, null, null);
        timeoutChecker.throwIfTimeout();
        LOG.info("Starting runnable {} for runId {} with SSH", runnableName, getProgramRunId());
        session.executeAndWait(targetPath + "/launcher.sh");
    }
}
Also used : DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultSSHSession(io.cdap.cdap.common.ssh.DefaultSSHSession)

Example 2 with SSHSession

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

the class SSHSessionTest method testLocalPortForwarding.

@Test
public void testLocalPortForwarding() throws Exception {
    // Starts an echo server for testing the port forwarding
    EchoServer echoServer = new EchoServer();
    echoServer.startAndWait();
    try {
        // Creates the DataConsumer for receiving data and validating the lifecycle
        StringBuilder received = new StringBuilder();
        AtomicBoolean finished = new AtomicBoolean();
        PortForwarding.DataConsumer dataConsumer = new PortForwarding.DataConsumer() {

            private final List<String> messages = new ArrayList<>();

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

            @Override
            public synchronized void flushed() {
                messages.forEach(received::append);
            }

            @Override
            public void finished() {
                finished.set(true);
            }
        };
        SSHConfig sshConfig = getSSHConfig();
        // Creates a SSH session.
        try (SSHSession session = new DefaultSSHSession(sshConfig)) {
            InetSocketAddress bindAddress = echoServer.getBindAddress();
            // Creates local port forward and send data to the echo server through that forwarding channel
            try (PortForwarding portForwarding = session.createLocalPortForward(bindAddress.getHostName(), bindAddress.getPort(), 12345, dataConsumer)) {
                List<String> messages = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    String msg = "Testing" + i;
                    portForwarding.write(StandardCharsets.UTF_8.encode(msg));
                    portForwarding.write(StandardCharsets.UTF_8.encode("\n"));
                    messages.add(msg);
                }
                portForwarding.flush();
                Iterable<String> splits = Splitter.on("\n").omitEmptyStrings().split(received);
                Assert.assertEquals(messages, StreamSupport.stream(splits.spliterator(), false).collect(Collectors.toList()));
            }
            // After closing the port forwarding, the data consumer should have finished.
            Assert.assertTrue(finished.get());
        }
    } finally {
        echoServer.stopAndWait();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) ArrayList(java.util.ArrayList) List(java.util.List) PortForwarding(io.cdap.cdap.runtime.spi.ssh.PortForwarding) RemotePortForwarding(io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding) Test(org.junit.Test)

Example 3 with SSHSession

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

the class SSHSessionTest method testRemotePortForwarding.

@Test
public void testRemotePortForwarding() throws Exception {
    EchoServer echoServer = new EchoServer();
    echoServer.startAndWait();
    try {
        SSHConfig sshConfig = getSSHConfig();
        try (SSHSession session = new DefaultSSHSession(sshConfig)) {
            InetSocketAddress echoServerAddr = echoServer.getBindAddress();
            try (RemotePortForwarding forwarding = session.createRemotePortForward(0, echoServerAddr.getPort())) {
                // Send data to the remotePort, it should get forwarded to the echoServer
                try (Socket socket = new Socket(session.getAddress().getAddress(), forwarding.getRemotePort())) {
                    PrintStream printer = new PrintStream(socket.getOutputStream(), true, "UTF-8");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
                    String msg = "Testing message";
                    printer.println(msg);
                    Assert.assertEquals(msg, reader.readLine());
                }
            }
        }
    } finally {
        echoServer.stopAndWait();
    }
}
Also used : PrintStream(java.io.PrintStream) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) RemotePortForwarding(io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 4 with SSHSession

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

the class SSHSessionTest method testSsh.

@Test
public void testSsh() throws Exception {
    SSHConfig config = getSSHConfig();
    try (SSHSession session = new DefaultSSHSession(config)) {
        for (int i = 0; i < 10; i++) {
            String msg = "Sending some message " + i;
            String result = session.executeAndWait(msg);
            Assert.assertEquals(msg, result);
        }
    }
    // Test the error exit
    try (SSHSession session = new DefaultSSHSession(config)) {
        try {
            session.executeAndWait("failure");
            Assert.fail("Expected failure from ssh command");
        } catch (Exception e) {
        // Expected
        }
    }
}
Also used : SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) Test(org.junit.Test)

Example 5 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)

Aggregations

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