use of io.cdap.cdap.internal.app.runtime.monitor.ServiceSocksProxyInfo 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