Example #1
1
 private Integer startTunnelSSH(Instance instance) throws InterruptedException {
   Instance tunnelInstance = runningInstances(tunnelResourceId).get(0);
   Integer localPort = Double.valueOf(Randoms.number(3000, 10000)).intValue();
   CountDownLatch latch = new CountDownLatch(1);
   Thread tunnelThread =
       new Thread(
           () -> {
             Process process = null;
             try {
               Path keyPath = KeyPair.keyFile(tunnelInstance.getKeyName(), env);
               String userAndHost = "ubuntu@" + hostName(tunnelInstance);
               String portBinding =
                   Strings.format("{}:{}:22", localPort, instance.getPrivateIpAddress());
               List<String> command = tunnelCommand(keyPath, userAndHost, portBinding);
               logger.info("tunnel command => {}", String.join(" ", command));
               process = new ProcessBuilder().command(command).start();
               process.getInputStream().read(); // wait until there is output
               latch.countDown();
               process.waitFor();
             } catch (InterruptedException | IOException e) {
               throw new IllegalStateException(e);
             } finally {
               if (process != null) process.destroy();
             }
           });
   tunnelThread.setDaemon(true);
   tunnelThread.start();
   latch.await();
   return localPort;
 }
Example #2
0
 private void ssh(Instance instance, Integer tunnelPort) throws IOException, InterruptedException {
   Path keyPath = KeyPair.keyFile(instance.getKeyName(), env);
   String userAndHost;
   if (tunnelPort != null) userAndHost = "ubuntu@localhost";
   else userAndHost = "ubuntu@" + hostName(instance);
   List<String> command = command(keyPath, userAndHost, tunnelPort);
   logger.info("command => {}", String.join(" ", command));
   Process process = new ProcessBuilder().inheritIO().command(command).start();
   process.waitFor();
   logger.info("session disconnected");
 }