Source:
protected void sendConfiguration(String ip, TargetHandlerParameters parameters) throws IOException { SSHClient ssh = new SSHClient(); try { ssh.loadKnownHosts(); ssh.connect(ip); String user = parameters.getTargetProperties().get(SCP_USER); if(user == null) user = "ubuntu"; String keyfile = parameters.getTargetProperties().get(SCP_KEYFILE); if(keyfile == null) ssh.authPublickey(user); else ssh.authPublickey(user, keyfile); String userData = UserDataHelpers.writeUserDataAsString( parameters.getMessagingProperties(), parameters.getDomain(), parameters.getApplicationName(), parameters.getScopedInstancePath()); File userdataFile = new File("/tmp", USER_DATA_FILE); Utils.writeStringInto(userData, userdataFile); ssh.newSCPFileTransfer().upload(new FileSystemFile(userdataFile), AGENT_CONFIG_DIR); File localAgentConfig = new File("/tmp", "net.roboconf.agent.configuration.cfg"); File remoteAgentConfig = new File(AGENT_CONFIG_DIR, "net.roboconf.agent.configuration.cfg"); ssh.newSCPFileTransfer().download(remoteAgentConfig.getCanonicalPath(), new FileSystemFile("/tmp/")); String config = Utils.readFileContent(localAgentConfig); config = config.replaceFirst("(?m)^\\s*parameters\\s*[:=]\\s*(.*)$", "\nparameters = " + remoteAgentConfig.getCanonicalPath()); Utils.writeStringInto(config, localAgentConfig); ssh.newSCPFileTransfer().upload(new FileSystemFile(localAgentConfig), AGENT_CONFIG_DIR); } finally { try { ssh.disconnect(); ssh.close(); } catch(IOException ignore) { } } }

Target:
protected void sendConfiguration(String ip, TargetHandlerParameters parameters) throws IOException { SSHClient ssh = new SSHClient(); File tmpDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); tmpDir.mkdir(); tmpDir.deleteOnExit(); try { ssh.loadKnownHosts(); ssh.connect(ip); String user = parameters.getTargetProperties().get(SCP_USER); if(user == null) user = "ubuntu"; String keyfile = parameters.getTargetProperties().get(SCP_KEYFILE); if(keyfile == null) ssh.authPublickey(user); else ssh.authPublickey(user, keyfile); String userData = UserDataHelpers.writeUserDataAsString( parameters.getMessagingProperties(), parameters.getDomain(), parameters.getApplicationName(), parameters.getScopedInstancePath()); String agentConfigDir = parameters.getTargetProperties().get(SCP_AGENT_CONFIG_DIR); if(agentConfigDir == null) agentConfigDir = AGENT_CONFIG_DEFAULT_DIR; File userdataFile = new File(tmpDir, USER_DATA_FILE); Utils.writeStringInto(userData, userdataFile); ssh.newSCPFileTransfer().upload(new FileSystemFile(userdataFile), agentConfigDir); File localAgentConfig = new File(tmpDir, "net.roboconf.agent.configuration.cfg"); File remoteAgentConfig = new File(agentConfigDir, "net.roboconf.agent.configuration.cfg"); ssh.newSCPFileTransfer().download(remoteAgentConfig.getCanonicalPath(), new FileSystemFile(tmpDir)); String config = Utils.readFileContent(localAgentConfig); config = config.replaceFirst("(?m)^\\s*parameters\\s*[:=]\\s*(.*)$", "\nparameters = " + remoteAgentConfig.getCanonicalPath()); Utils.writeStringInto(config, localAgentConfig); ssh.newSCPFileTransfer().upload(new FileSystemFile(localAgentConfig), agentConfigDir); } finally { try { ssh.disconnect(); ssh.close(); tmpDir.delete(); } catch(IOException ignore) { } } }