private Connection connectToSsh(Computer computer, PrintStream logger) throws RequestUnsuccessfulException, DigitalOceanException { final long timeout = TimeUnit2.MINUTES.toMillis(computer.getCloud().getTimeoutMinutes()); final long startTime = System.currentTimeMillis(); final int sleepTime = 10; long waitTime; while ((waitTime = System.currentTimeMillis() - startTime) < timeout) { // Hack to fetch this each time through the loop to get the latest information. final Droplet droplet = DigitalOcean.getDroplet( computer.getCloud().getAuthToken(), computer.getNode().getDropletId()); if (isDropletStarting(droplet)) { logger.println( "Waiting for droplet to enter ACTIVE state. Sleeping " + sleepTime + " seconds."); } else { try { final String host = getIpAddress(computer); if (Strings.isNullOrEmpty(host) || "0.0.0.0".equals(host)) { logger.println( "No ip address yet, your host is most likely waiting for an ip address."); } else { int port = computer.getSshPort(); Connection conn = getDropletConnection(host, port, logger); if (conn != null) { return conn; } } } catch (IOException e) { // Ignore, we'll retry. } logger.println("Waiting for SSH to come up. Sleeping " + sleepTime + " seconds."); } sleep(sleepTime); } throw new RuntimeException( format( "Timed out after %d seconds of waiting for ssh to become available (max timeout configured is %s)", waitTime / 1000, timeout / 1000)); }
private boolean runInitScript( final Computer computer, final PrintStream logger, final Connection conn, final SCPClient scp) throws IOException, InterruptedException { String initScript = Util.fixEmptyAndTrim(computer.getNode().getInitScript()); if (initScript == null) { return true; } if (conn.exec("test -e ~/.hudson-run-init", logger) == 0) { return true; } logger.println("Executing init script"); scp.put(initScript.getBytes("UTF-8"), "init.sh", "/tmp", "0700"); Session session = conn.openSession(); session.requestDumbPTY(); // so that the remote side bundles stdout and stderr session.execCommand(buildUpCommand(computer, "/tmp/init.sh")); session.getStdin().close(); // nothing to write here session.getStderr().close(); // we are not supposed to get anything from stderr IOUtils.copy(session.getStdout(), logger); int exitStatus = waitCompletion(session); if (exitStatus != 0) { logger.println("init script failed: exit code=" + exitStatus); return false; } session.close(); // Needs a tty to run sudo. session = conn.openSession(); session.requestDumbPTY(); // so that the remote side bundles stdout and stderr session.execCommand(buildUpCommand(computer, "touch ~/.hudson-run-init")); session.close(); return true; }
/** * Connects to the given {@link Computer} via SSH and installs Java/Jenkins agent if necessary. */ @Override public void launch(SlaveComputer _computer, TaskListener listener) { Computer computer = (Computer) _computer; PrintStream logger = listener.getLogger(); Date startDate = new Date(); logger.println("Start time: " + getUtcDate(startDate)); final Connection conn; Connection cleanupConn = null; boolean successful = false; try { conn = connectToSsh(computer, logger); cleanupConn = conn; logger.println("Authenticating as " + computer.getRemoteAdmin()); if (!conn.authenticateWithPublicKey( computer.getRemoteAdmin(), computer.getNode().getPrivateKey().toCharArray(), "")) { logger.println("Authentication failed"); throw new Exception("Authentication failed"); } final SCPClient scp = conn.createSCPClient(); if (!runInitScript(computer, logger, conn, scp)) { return; } if (!installJava(logger, conn)) { return; } logger.println("Copying slave.jar"); scp.put(Jenkins.getInstance().getJnlpJars("slave.jar").readFully(), "slave.jar", "/tmp"); String jvmOpts = Util.fixNull(computer.getNode().getJvmOpts()); String launchString = "java " + jvmOpts + " -jar /tmp/slave.jar"; logger.println("Launching slave agent: " + launchString); final Session sess = conn.openSession(); sess.execCommand(launchString); computer.setChannel( sess.getStdout(), sess.getStdin(), logger, new Channel.Listener() { @Override public void onClosed(Channel channel, IOException cause) { sess.close(); conn.close(); } }); successful = true; } catch (Exception e) { LOGGER.log(Level.WARNING, e.getMessage(), e); try { Jenkins.getInstance().removeNode(computer.getNode()); } catch (Exception ee) { ee.printStackTrace(logger); } e.printStackTrace(logger); } finally { Date endDate = new Date(); logger.println("Done setting up at: " + getUtcDate(endDate)); logger.println( "Done in " + TimeUnit2.MILLISECONDS.toSeconds(endDate.getTime() - startDate.getTime()) + " seconds"); if (cleanupConn != null && !successful) { cleanupConn.close(); } } }