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();
}
}
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()));
}
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);
}
}
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);
}
}
}
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;
}
Aggregations