public static boolean validateMd5Sum(Connection connection, String target, String expectedMd5Sum){ //jenkins.war exists, lets check the md5 sum ByteArrayOutputStream baos = new ByteArrayOutputStream(); int status = 0; try { status = connection.exec(String.format("md5sum %s", Ssh.escape(target)), baos); if(status == 0){ String[] values = baos.toString("UTF-8").split(" "); return (values.length > 1 && values[0].equals(expectedMd5Sum)); } } catch (IOException | InterruptedException e) { logger.error(e.getMessage()); } return false; }
protected void reportEnvironment(TaskListener listener) throws IOException, InterruptedException { listener.getLogger().println(Messages._SSHLauncher_RemoteUserEnvironment(getTimestamp())); connection.exec("set",listener.getLogger()); }
private boolean executeRemote(EC2Computer computer, Connection conn, String checkCommand, String command, PrintStream logger, TaskListener listener) throws IOException, InterruptedException { logInfo(computer, listener,"Verifying: " + checkCommand); if (conn.exec(checkCommand, logger) != 0) { logInfo(computer, listener, "Installing: " + command); if (conn.exec(command, logger) != 0) { logWarning(computer, listener, "Failed to install: " + command); return false; } } return true; }
protected void reportEnvironment(TaskListener listener) throws IOException, InterruptedException { listener.getLogger().println(Messages._SSHLauncher_RemoteUserEnvironment(getTimestamp())); connection.exec("set",listener.getLogger()); }
public static boolean remoteFileExists(Connection connection, String target, String expectedMd5Sum){ try { int status = connection.exec(String.format("stat %s > /dev/null 2>&1", Ssh.escape(target)), System.out); if(status == 0){ return expectedMd5Sum == null || validateMd5Sum(connection, target, expectedMd5Sum); } } catch (IOException | InterruptedException e) { logger.error(e.getMessage()); } return false; }
/** * Makes sure that SSH connection won't produce any unwanted text, which will interfere with sftp execution. */ private void verifyNoHeaderJunk(TaskListener listener) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); connection.exec("exit 0",baos); final String s; //TODO: Seems we need to retrieve the encoding from the connection destination try { s = baos.toString(Charset.defaultCharset().name()); } catch (UnsupportedEncodingException ex) { // Should not happen throw new IOException("Default encoding is unsupported", ex); } if (s.length()!=0) { listener.getLogger().println(Messages.SSHLauncher_SSHHeaderJunkDetected()); listener.getLogger().println(s); throw new AbortException(); } }
/** * Makes sure that SSH connection won't produce any unwanted text, which will interfere with sftp execution. */ private void verifyNoHeaderJunk(TaskListener listener) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); connection.exec("true",baos); final String s; //TODO: Seems we need to retrieve the encoding from the connection destination try { s = baos.toString(Charset.defaultCharset().name()); } catch (UnsupportedEncodingException ex) { // Should not happen throw new IOException("Default encoding is unsupported", ex); } if (s.length()!=0) { listener.getLogger().println(Messages.SSHLauncher_SSHHeaderJunkDetected()); listener.getLogger().println(s); throw new AbortException(); } }
public int executeRemoteCommand(String cmd, OutputStream os) { Session session = null; try { session = connection.openSession(); int status = connection.exec(cmd, os); if (status != 0) { throw new RuntimeException("Failed to execute command: " + cmd + ", exit code = " + status); } return status; } catch (InterruptedException | IOException e) { throw new AssertionError(e); } finally { if (session != null) { session.close(); } } }
@Override public void close() throws IOException { logger.info("Destroying node: " + nodeMetadata); try (Ssh ssh = connect()) { ssh.getConnection().exec(String.format("pkill -u $(id -u %s)", getUser()), System.out); } catch (InterruptedException e) { //ignore logger.error(e.getMessage()); } machineProvider.offer(this); }
@NonNull private String checkJavaVersion(TaskListener listener, String javaCommand) throws IOException, InterruptedException { listener.getLogger().println(Messages.SSHLauncher_CheckingDefaultJava(SSHLauncher.getTimestamp(),javaCommand)); StringWriter output = new StringWriter(); // record output from Java ByteArrayOutputStream out = new ByteArrayOutputStream(); connection.exec(javaCommand + " "+ jvmOptions + " -version",out); //TODO: Seems we need to retrieve the encoding from the connection destination BufferedReader r = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(out.toByteArray()), Charset.defaultCharset())); final String result = checkJavaVersion(listener.getLogger(), javaCommand, r, output); if(null == result) { listener.getLogger().println(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); listener.getLogger().println(output); throw new IOException(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); } else { return result; } }
@NonNull private String checkJavaVersion(TaskListener listener, String javaCommand) throws IOException, InterruptedException { listener.getLogger().println(Messages.SSHLauncher_CheckingDefaultJava(getTimestamp(),javaCommand)); StringWriter output = new StringWriter(); // record output from Java ByteArrayOutputStream out = new ByteArrayOutputStream(); connection.exec(javaCommand + " "+getJvmOptions() + " -version",out); //TODO: Seems we need to retrieve the encoding from the connection destination BufferedReader r = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(out.toByteArray()), Charset.defaultCharset())); final String result = checkJavaVersion(listener.getLogger(), javaCommand, r, output); if(null == result) { listener.getLogger().println(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); listener.getLogger().println(output); throw new IOException(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); } else { return result; } }
/** * Method copies the agent jar to the remote system using scp. * * @param listener The listener. * @param workingDirectory The directory into which the agent jar will be copied. * * @throws IOException If something goes wrong. * @throws InterruptedException If something goes wrong. */ private void copySlaveJarUsingSCP(TaskListener listener, String workingDirectory) throws IOException, InterruptedException { SCPClient scp = new SCPClient(connection); try { // check if the working directory exists if (connection.exec("test -d " + workingDirectory ,listener.getLogger())!=0) { listener.getLogger().println( Messages.SSHLauncher_RemoteFSDoesNotExist(getTimestamp(), workingDirectory)); // working directory doesn't exist, lets make it. if (connection.exec("mkdir -p " + workingDirectory, listener.getLogger())!=0) { listener.getLogger().println("Failed to create "+workingDirectory); } } // delete the agent jar as we do with SFTP connection.exec("rm " + workingDirectory + SLASH_AGENT_JAR, new NullStream()); // SCP it to the agent. hudson.Util.ByteArrayOutputStream2 doesn't work for this. It pads the byte array. listener.getLogger().println(Messages.SSHLauncher_CopyingAgentJar(getTimestamp())); scp.put(new Slave.JnlpJar(AGENT_JAR).readFully(), AGENT_JAR, workingDirectory, "0644"); } catch (IOException e) { throw new IOException(Messages.SSHLauncher_ErrorCopyingAgentJarInto(workingDirectory), e); } }
/** * Method copies the slave jar to the remote system using scp. * * @param listener The listener. * @param workingDirectory The directory into which the slave jar will be copied. * * @throws IOException If something goes wrong. * @throws InterruptedException If something goes wrong. */ private void copySlaveJarUsingSCP(TaskListener listener, String workingDirectory) throws IOException, InterruptedException { SCPClient scp = new SCPClient(connection); try { // check if the working directory exists if (connection.exec("test -d " + workingDirectory ,listener.getLogger())!=0) { listener.getLogger().println( Messages.SSHLauncher_RemoteFSDoesNotExist(getTimestamp(), workingDirectory)); // working directory doesn't exist, lets make it. if (connection.exec("mkdir -p " + workingDirectory, listener.getLogger())!=0) { listener.getLogger().println("Failed to create "+workingDirectory); } } // delete the slave jar as we do with SFTP connection.exec("rm " + workingDirectory + "/slave.jar", new NullStream()); // SCP it to the slave. hudson.Util.ByteArrayOutputStream2 doesn't work for this. It pads the byte array. listener.getLogger().println(Messages.SSHLauncher_CopyingSlaveJar(getTimestamp())); scp.put(new Slave.JnlpJar("slave.jar").readFully(), "slave.jar", workingDirectory, "0644"); } catch (IOException e) { throw new IOException(Messages.SSHLauncher_ErrorCopyingSlaveJarInto(workingDirectory), e); } }
private void copyAutoterminateScript(String host) throws IOException, InterruptedException { //run terminate script URL script = this.getClass().getClassLoader().getResource("org/jenkinsci/test/acceptance/machine/autoterminate.sh"); File tempScriptFile = File.createTempFile("autoterminate", ".sh"); try { FileUtils.copyURLToFile(script, tempScriptFile); logger.info(String.format( "Executing auto-terminate script on remote machine: %s, it will terminate after 180 minutes of " + "inactivity.", host)); try (Ssh ssh = new Ssh(host)) { authenticator().authenticate(ssh.getConnection()); ssh.copyTo(tempScriptFile.getAbsolutePath(), "autoterminate.sh", "."); ssh.executeRemoteCommand("chmod +x ./autoterminate.sh ; touch nohup.out"); //wait for 3 hours before termination ssh.getConnection().exec(String .format("nohup ./autoterminate.sh %s %s `</dev/null` >nohup.out 2>&1 &", config.getUser(), AUTO_TERMINATE_TIMEOUT), System.out); } } finally { if (!tempScriptFile.delete()) { tempScriptFile.deleteOnExit(); } } }
@Override public void postStartupSetup(NodeMetadata node) { String host = node.getPublicAddresses().iterator().next(); try (Ssh ssh = new Ssh(host)) { authenticator().authenticate(ssh.getConnection()); ssh.getConnection().exec(String.format("pkill -u $(id -u %s)", config.getUser()), System.out); //set instance initiated shutdown behavior to terminate. This means any shutdown will result in to instance // termination EC2Api client = contextBuilder.buildApi(EC2Api.class); client.getInstanceApi().get().setInstanceInitiatedShutdownBehaviorForInstanceInRegion(config.getRegion(), node.getProviderId(), Volume.InstanceInitiatedShutdownBehavior.TERMINATE); copyAutoterminateScript(host); } catch (IOException | InterruptedException e) { logger.error(e.getMessage()); throw new RuntimeException(e); } }
public void run() { // this would fail if the connection is already lost, so we want to check that. // TODO: Connection class should expose whether it is still connected or not. SFTPv3Client sftpClient = null; try { sftpClient = new SFTPv3Client(connection); sftpClient.rm(fileName); } catch (Exception e) { if (sftpClient == null) {// system without SFTP try { connection.exec("rm " + fileName, listener.getLogger()); } catch (Error error) { throw error; } catch (Throwable x) { x.printStackTrace(listener.error(Messages.SSHLauncher_ErrorDeletingFile(getTimestamp()))); // We ignore other Exception types } } else { e.printStackTrace(listener.error(Messages.SSHLauncher_ErrorDeletingFile(getTimestamp()))); } } finally { if (sftpClient != null) { sftpClient.close(); } } } });
public void run() { // this would fail if the connection is already lost, so we want to check that. // TODO: Connection class should expose whether it is still connected or not. SFTPv3Client sftpClient = null; try { sftpClient = new SFTPv3Client(connection); sftpClient.rm(fileName); } catch (Exception e) { if (sftpClient == null) {// system without SFTP try { connection.exec("rm " + fileName, listener.getLogger()); } catch (Error error) { throw error; } catch (Throwable x) { x.printStackTrace(listener.error(Messages.SSHLauncher_ErrorDeletingFile(getTimestamp()))); // We ignore other Exception types } } else { e.printStackTrace(listener.error(Messages.SSHLauncher_ErrorDeletingFile(getTimestamp()))); } } finally { if (sftpClient != null) { sftpClient.close(); } } } });
conn.exec("mkdir -p " + tmpDir, logger); if(initScript!=null && initScript.trim().length()>0 && conn.exec("test -e ~/.hudson-run-init", logger) !=0) { logger.println("Executing init script"); scp.put(initScript.getBytes("UTF-8"),"init.sh",tmpDir,"0700"); if(conn.exec("java -fullversion", logger) !=0) { logger.println("Installing Java"); if(conn.exec("wget -nv -O " + tmpDir + "/" + jdk + ".tgz '" + url + "'", logger) !=0) { logger.println("Failed to download Java"); return; if(conn.exec(buildUpCommand(computer, "tar xz -C /usr -f " + tmpDir + "/" + jdk + ".tgz"), logger) !=0) { logger.println("Failed to install Java"); return; if(conn.exec(buildUpCommand(computer, "ln -s /usr/" + jdk + "/bin/java /bin/java"), logger) !=0) { logger.println("Failed to symlink Java"); return;