private void uploadToNode(ComputeServiceContext context, NodeMetadata node, LoginCredentials credentials, URL url, String path) { Utils utils = context.utils(); SshClient ssh = credentials != null ? utils .sshForNode() .apply(NodeMetadataBuilder.fromNodeMetadata(nodeMetadata) .credentials(credentials).build()) : utils.sshForNode().apply(node); try (InputStream is = url.openStream(); ) { ssh.connect(); File distro = Files.createTempFile("/tmp"); Files.copy(is, new FileOutputStream(distro)); ssh.put(path, Payloads.newFilePayload(distro)); distro.delete(); } catch (IOException e) { LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven."); } finally { if (ssh != null) { ssh.disconnect(); } } } }
@Test public void testPutAndGet() throws IOException { temp = File.createTempFile("foo", "bar"); temp.deleteOnExit(); SshClient client = setupClient(); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); Payload input = setupClient().get(temp.getAbsolutePath()); String contents = Strings2.toString(input); assertEquals(contents, "rabbit"); }
/** * Upload a file on a selected node * @param sourcePath path to the file to be uploaded * @param destinationPath path to the file to be created * @param nodeId Id of a node * @param login user login * @param key key to connect */ public void uploadFile(String sourcePath, String destinationPath, String nodeId, String login, String key){ try { String contentKey=FileUtils.readFileToString(new File(key)); SshClient ssh = compute.getContext().getUtils().sshForNode().apply(NodeMetadataBuilder.fromNodeMetadata(getNodeById(nodeId)).credentials(new LoginCredentials(login, null, contentKey, true)).build()); try { ssh.connect(); ssh.put(destinationPath, Payloads.newPayload(new File(sourcePath))); } finally { if (ssh != null) ssh.disconnect(); journal.log(Level.INFO, ">> File uploaded!"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private void runScript(SshClient client, String scriptName, String script, int nodeId) { client.put("/tmp/init-" + scriptName, script); expect(client.exec("chmod 755 /tmp/init-" + scriptName)).andReturn(EXEC_GOOD); expect(client.exec("ln -fs /tmp/init-" + scriptName + " " + scriptName)).andReturn(EXEC_GOOD); expect(client.getUsername()).andReturn("root").atLeastOnce(); expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce(); expect(client.exec("/tmp/init-" + scriptName + " init")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " start")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_GOOD); // next status says the script is done, since not found. expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_BAD); expect(client.exec("/tmp/init-" + scriptName + " stdout")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " stderr")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " exitstatus")).andReturn(EXEC_RC_GOOD); }
public void testPutAndGet() throws IOException { temp = File.createTempFile("foo", "bar"); try { SshClient client = setupClient(); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); Payload input = client.get(temp.getAbsolutePath()); String contents = Strings2.toStringAndClose(input.openStream()); assertEquals(contents, "rabbit"); } finally { temp.delete(); } }
@Test public void testPutAndGet() throws IOException { temp = File.createTempFile("foo", "bar"); try { SshClient client = setupClient(); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); Payload input = setupClient().get(temp.getAbsolutePath()); String contents = Strings2.toStringAndClose(input.openStream()); assertEquals(contents, "rabbit"); } finally { temp.delete(); } }
private void runScript(SshClient client, String scriptName, String script, int nodeId) { client.put("/tmp/init-" + scriptName, script); expect(client.exec("chmod 755 /tmp/init-" + scriptName)).andReturn(EXEC_GOOD); expect(client.exec("ln -fs /tmp/init-" + scriptName + " " + scriptName)).andReturn(EXEC_GOOD); expect(client.getUsername()).andReturn("root").atLeastOnce(); expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce(); expect(client.exec("/tmp/init-" + scriptName + " init")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " start")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_GOOD); // next status says the script is done, since not found. expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_BAD); expect(client.exec("/tmp/init-" + scriptName + " stdout")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " stderr")).andReturn(EXEC_GOOD); expect(client.exec("/tmp/init-" + scriptName + " exitstatus")).andReturn(EXEC_RC_GOOD); }
try { try { ssh.put(initFile, init.render(OsFamily.UNIX)); } catch (SshException e) {
try { try { ssh.put(initFile, init.render(OsFamily.UNIX)); } catch (SshException e) {
try { try { ssh.put(initFile, init.render(OsFamily.UNIX)); } catch (SshException e) {
System.out.printf("%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId()); String script = "/tmp/mkebsboot-init.sh"; ssh.put(script, Payloads.newStringPayload(mkEbsBoot));
System.out.printf("%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId()); String script = "/tmp/mkebsboot-init.sh"; ssh.put(script, Payloads.newStringPayload(mkEbsBoot));
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX)); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();