private int handleScriptOutput(PrintStream out, PrintStream err, Map<? extends NodeMetadata, ExecResponse> responses) { int exitStatus = 0; for (Map.Entry<? extends NodeMetadata, ExecResponse> entry : responses.entrySet()) { out.printf("** Node %s: %s%n", entry.getKey().getId(), Iterables.concat(entry.getKey().getPrivateAddresses(), entry.getKey().getPublicAddresses())); ExecResponse response = entry.getValue(); if (response.getExitStatus() != 0) { exitStatus = response.getExitStatus(); } out.printf("%s%n", response.getOutput()); err.printf("%s%n", response.getError()); } return exitStatus; }
private int handleScriptOutput(PrintStream out, PrintStream err, Map<? extends NodeMetadata, ExecResponse> responses) { int exitStatus = 0; for (Map.Entry<? extends NodeMetadata, ExecResponse> entry : responses.entrySet()) { out.printf("** Node %s: %s%n", entry.getKey().getId(), Iterables.concat(entry.getKey().getPrivateAddresses(), entry.getKey().getPublicAddresses())); ExecResponse response = entry.getValue(); if (response.getExitStatus() != 0) { exitStatus = response.getExitStatus(); } out.printf("%s%n", response.getOutput()); err.printf("%s%n", response.getError()); } return exitStatus; }
public void testExecHostname() throws IOException, InterruptedException { SshClient client = setupClient(); ExecResponse response = client.exec("hostname"); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); }
@Test public void testExecInvalidCommand() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("thisCommandDoesNotExist"); assertNotEquals(response.getExitStatus(), 0); assertTrue(response.getOutput().contains("not found") || response.getError().contains("not found"), "stdout="+response.getOutput()+"; stderr="+response.getError()); } finally { client.disconnect(); } }
private void mapSecurityGroupRuleToIpTables(ComputeService computeService, NodeMetadata node, LoginCredentials credentials, String networkInterface, Iterable<Integer> ports) { for (Integer port : ports) { String insertIptableRule = IptablesCommands.insertIptablesRule(Chain.INPUT, networkInterface, Protocol.TCP, port, Policy.ACCEPT); Statement statement = Statements.newStatementList(exec(insertIptableRule)); ExecResponse response = computeService.runScriptOnNode(node.getId(), statement, overrideLoginCredentials(credentials).runAsRoot(false)); if (response.getExitStatus() != 0) { String msg = String.format("Cannot insert the iptables rule for port %d. Error: %s", port, response.getError()); LOG.error(msg); throw new RuntimeException(msg); } } }
@Test public void testExecHostname() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("hostname"); assertEquals(response.getExitStatus(), 0); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); } finally { client.disconnect(); } }
@Test public void testExecHostnameRepeatedlyWithSameSessions() throws Exception { final SshClient client = setupClient(); try { for (int i = 0; i < 100; i++) { ExecResponse response = client.exec("hostname"); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); //System.out.println("completed (sequentially) "+i); } } finally { client.disconnect(); } }
@Override public ExecResponse apply(@Nullable org.jclouds.compute.domain.ExecResponse input) { if (input == null) { return null; } return ExecResponse.builder().output(input.getOutput()).error(input.getError()).exitStatus(input.getExitStatus()) .build(); } }
@Test public void testExecInvalidCommand() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("thisCommandDoesNotExist"); assertNotEquals(response.getExitStatus(), 0); assertTrue(response.getOutput().contains("not found") || response.getError().contains("not found"), "stdout=" + response.getOutput() + "; stderr=" + response.getError()); } finally { client.disconnect(); } }
@Test public void testExecHostname() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("hostname"); assertEquals(response.getExitStatus(), 0); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); } finally { client.disconnect(); } }
@Test public void testExecHostnameRepeatedlyWithSameSessions() throws Exception { final SshClient client = setupClient(); try { for (int i = 0; i < 100; i++) { ExecResponse response = client.exec("hostname"); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); //System.out.println("completed (sequentially) " + i); } } finally { client.disconnect(); } }
@Test public void testExecHostnameConcurrentlyWithSameSessions() throws Exception { final SshClient client = setupClient(); ListeningExecutorService userExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); List<ListenableFuture<ExecResponse>> futures = Lists.newArrayList(); try { for (int i = 0; i < 100; i++) { futures.add(userExecutor.submit(new Callable<ExecResponse>() { @Override public ExecResponse call() { ExecResponse response = client.exec("hostname"); //System.out.println("completed (concurrently) "+count.incrementAndGet()); return response; } })); } List<ExecResponse> responses = Futures.allAsList(futures).get(3000, TimeUnit.SECONDS); for (ExecResponse response : responses) { assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); } } finally { userExecutor.shutdownNow(); client.disconnect(); } }
@Test public void testExecHostnameConcurrentlyWithSameSessions() throws Exception { final SshClient client = setupClient(); ListeningExecutorService userExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); List<ListenableFuture<ExecResponse>> futures = Lists.newArrayList(); try { for (int i = 0; i < 100; i++) { futures.add(userExecutor.submit(new Callable<ExecResponse>() { @Override public ExecResponse call() { ExecResponse response = client.exec("hostname"); //System.out.println("completed (concurrently) " + count.incrementAndGet()); return response; } })); } List<ExecResponse> responses = Futures.allAsList(futures).get(3000, TimeUnit.SECONDS); for (ExecResponse response : responses) { assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); } } finally { userExecutor.shutdownNow(); client.disconnect(); } }
/** * Runs a script on the target node. */ protected void runScriptOnNode(Exchange exchange) throws CamelException { String script = exchange.getIn().getBody(String.class); String nodeId = getNodeId(exchange); String user = getUser(exchange); LoginCredentials credentials = null; if (ObjectHelper.isNotEmpty(user)) { credentials = LoginCredentials.builder().user(user).build(); } ExecResponse execResponse = null; if (credentials == null) { execResponse = computeService.runScriptOnNode(nodeId, script); } else { execResponse = computeService.runScriptOnNode(nodeId, script, RunScriptOptions.Builder.overrideLoginCredentials(credentials).runAsRoot(false)); } if (execResponse == null) { throw new CamelExchangeException("Failed to receive response for run script operation on node: " + nodeId + " using script: " + script, exchange); } exchange.setProperty(JcloudsConstants.RUN_SCRIPT_ERROR, execResponse.getError()); exchange.setProperty(JcloudsConstants.RUN_SCRIPT_EXIT_CODE, execResponse.getExitStatus()); exchange.getOut().setBody(execResponse.getOutput()); }
protected ServiceStats trackAvailabilityOfProcessOnNode(Statement process, String processName, NodeMetadata node) { ServiceStats stats = new ServiceStats(); Stopwatch watch = Stopwatch.createStarted(); ExecResponse exec = client.runScriptOnNode(node.getId(), process, runAsRoot(false).wrapInInitScript(false)); stats.backgroundProcessMilliseconds = watch.elapsed(TimeUnit.MILLISECONDS); watch.reset().start(); HostAndPort socket = null; try { socket = openSocketFinder.findOpenSocketOnNode(node, 8080, 600, TimeUnit.SECONDS); } catch (NoSuchElementException e) { throw new NoSuchElementException(format("%s%n%s%s", e.getMessage(), exec.getOutput(), exec.getError())); } stats.socketOpenMilliseconds = watch.elapsed(TimeUnit.MILLISECONDS); getAnonymousLogger().info(format("<< %s on node(%s)[%s] %s", processName, node.getId(), socket, stats)); return stats; }
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node, String taskName) throws IOException { SshClient ssh = view.utils().sshForNode().apply(node); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); ExecResponse exec = ssh.exec("java -version"); assert exec.getError().indexOf("OpenJDK") != -1 || exec.getOutput().indexOf("OpenJDK") != -1 : exec + "\n" + ssh.exec("cat /tmp/" + taskName + "/" + taskName + ".sh /tmp/" + taskName + "/stdout.log /tmp/" + taskName + "/stderr.log"); } finally { if (ssh != null) ssh.disconnect(); } }
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node, String taskName) throws IOException { SshClient ssh = view.utils().sshForNode().apply(node); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); ExecResponse exec = ssh.exec("java -version"); assert exec.getError().indexOf("OpenJDK") != -1 || exec.getOutput().indexOf("OpenJDK") != -1 : exec + "\n" + ssh.exec("cat /tmp/" + taskName + "/" + taskName + ".sh /tmp/" + taskName + "/stdout.log /tmp/" + taskName + "/stderr.log"); } finally { if (ssh != null) ssh.disconnect(); } }
protected void trackProcessOnNode(Statement process, String processName, NodeMetadata node) { ServiceStats stats = new ServiceStats(); Stopwatch watch = Stopwatch.createStarted(); ExecResponse exec = client.runScriptOnNode(node.getId(), process, runAsRoot(false).wrapInInitScript(false)); stats.backgroundProcessMilliseconds = watch.elapsed(TimeUnit.MILLISECONDS); Container container = client.getContext().unwrapApi(DockerApi.class).getContainerApi().inspectContainer(node.getId()); Map<String, List<Map<String, String>>> ports = container.networkSettings().ports(); int port = Integer.parseInt(getOnlyElement(ports.get("8080/tcp")).get("HostPort")); watch.reset().start(); HostAndPort socket; try { socket = openSocketFinder.findOpenSocketOnNode(node, port, 600, TimeUnit.SECONDS); } catch (NoSuchElementException e) { throw new NoSuchElementException(format("%s%n%s%s", e.getMessage(), exec.getOutput(), exec.getError())); } stats.socketOpenMilliseconds = watch.elapsed(TimeUnit.MILLISECONDS); getAnonymousLogger().info(format("<< %s on node(%s)[%s] %s", processName, node.getId(), socket, stats)); }
protected ServiceStats trackAvailabilityOfProcessOnNode(Statement process, String processName, NodeMetadata node) { ServiceStats stats = new ServiceStats(); Stopwatch watch = new Stopwatch().start(); ExecResponse exec = client.runScriptOnNode(node.getId(), process, runAsRoot(false).wrapInInitScript(false)); stats.backgroundProcessMilliseconds = watch.elapsedTime(TimeUnit.MILLISECONDS); watch.reset().start(); HostAndPort socket = null; try { socket = openSocketFinder.findOpenSocketOnNode(node, 8080, 60, TimeUnit.SECONDS); } catch (NoSuchElementException e) { throw new NoSuchElementException(format("%s%n%s%s", e.getMessage(), exec.getOutput(), exec.getError())); } stats.socketOpenMilliseconds = watch.elapsedTime(TimeUnit.MILLISECONDS); getAnonymousLogger().info(format("<< %s on node(%s)[%s] %s", processName, node.getId(), socket, stats)); return stats; }