Package org.jclouds.compute.domain

Examples of org.jclouds.compute.domain.ExecResponse


                        LoginCredentials.builder().user("root").password("romeo").build())).andThrow(
                  new AuthorizationException("Auth fail", null));

            // run script without backgrounding (via predicate)
            client2.connect();
            expect(client2.exec("hostname\n")).andReturn(new ExecResponse("stub-r\n", "", 0));
            client2.disconnect();

            // run script without backgrounding (via id)
            client2.connect();
            expect(client2.exec("hostname\n")).andReturn(new ExecResponse("stub-r\n", "", 0));
            client2.disconnect();

            client2.connect();
            try {
               runScript(client2, "runScriptWithCreds",
                        Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
                                 .getResourceAsStream("/runscript.sh")), 2);
            } catch (IOException e) {
               Throwables.propagate(e);
            }
            client2.disconnect();

            client2New.connect();
            try {
               runScript(client2New, "adminUpdate", Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
                        .getResourceAsStream("/runscript_adminUpdate.sh")), 2);
            } catch (IOException e) {
               Throwables.propagate(e);
            }
            client2New.disconnect();

            // check id
            client2Foo.connect();
            expect(client2Foo.getUsername()).andReturn("foo").atLeastOnce();
            expect(client2Foo.getHostAddress()).andReturn("foo").atLeastOnce();
            expect(client2Foo.exec("echo $USER\n")).andReturn(new ExecResponse("foo\n", "", 0));
            client2Foo.disconnect();

            expect(
                  factory.create(HostAndPort.fromParts("144.175.1.3", 22),
                        LoginCredentials.builder().user("root").password("password3").build())).andReturn(client3)
                  .times(2);
            expect(
                  factory.create(HostAndPort.fromParts("144.175.1.4", 22),
                        LoginCredentials.builder().user("root").password("password4").build())).andReturn(client4)
                  .times(2);
            expect(
                  factory.create(HostAndPort.fromParts("144.175.1.5", 22),
                        LoginCredentials.builder().user("root").password("password5").build())).andReturn(client5)
                  .times(2);
            expect(
                  factory.create(HostAndPort.fromParts("144.175.1.6", 22),
                        LoginCredentials.builder().user("root").password("password6").build())).andReturn(client6)
                  .times(2);
            expect(
                  factory.create(HostAndPort.fromParts("144.175.1.7", 22),
                        LoginCredentials.builder().user("root").password("password7").build())).andReturn(client7)
                  .times(2);

            runScriptAndInstallSsh(client3, "bootstrap", 3);
            runScriptAndInstallSsh(client4, "bootstrap", 4);
            runScriptAndInstallSsh(client5, "bootstrap", 5);
            runScriptAndInstallSsh(client6, "bootstrap", 6);
            runScriptAndInstallSsh(client7, "bootstrap", 7);

            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.1", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client1);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.2", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client2);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.3", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client3);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.4", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client4);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.5", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client5);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.6", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client6);
            expect(
                  factory.create(eq(HostAndPort.fromParts("144.175.1.7", 22)),
                        eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
                  .andReturn(client7);

            helloAndJava(client2);
            helloAndJava(client3);
            helloAndJava(client4);
            helloAndJava(client5);
            helloAndJava(client6);
            helloAndJava(client7);

            replay(factory);
            replay(client1);
            replay(client1New);
            replay(client2);
            replay(client2New);
            replay(client2Foo);
            replay(client3);
            replay(client4);
            replay(client5);
            replay(client6);
            replay(client7);

            bind(SshClient.Factory.class).toInstance(factory);
         }

         private void runScriptAndService(SshClient client, SshClient clientNew) {
            client.connect();

            try {
               String scriptName = "configure-jetty";
               client.put("/tmp/init-" + scriptName, Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
                        .getResourceAsStream("/initscript_with_jetty.sh")));
               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("localhost").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);

               // note we have to reconnect here, as we updated the login user.
               client.disconnect();

               clientNew.connect();              
               expect(clientNew.getUsername()).andReturn("web").atLeastOnce();
               expect(clientNew.getHostAddress()).andReturn("localhost").atLeastOnce();
               expect(clientNew.exec("head -1 /usr/local/jetty/VERSION.txt | cut -f1 -d ' '\n")).andReturn(EXEC_GOOD);
               clientNew.disconnect();
                             
               clientNew.connect();
               expect(clientNew.exec("java -fullversion\n")).andReturn(EXEC_GOOD);
               clientNew.disconnect();

               String startJetty = new StringBuilder()
                  .append("cd /usr/local/jetty").append('\n')
                  .append("nohup java -jar start.jar jetty.port=8080 > start.out 2> start.err < /dev/null &").append('\n')
                  .append("test $? && sleep 1").append('\n').toString();

               clientNew.connect();
               expect(clientNew.exec(startJetty)).andReturn(EXEC_GOOD);
               clientNew.disconnect();

               clientNew.connect();
               expect(clientNew.exec("cd /usr/local/jetty\n./bin/jetty.sh stop\n")).andReturn(EXEC_GOOD);
               clientNew.disconnect();

               clientNew.connect();
               expect(clientNew.exec(startJetty)).andReturn(EXEC_GOOD);
               clientNew.disconnect();

            } catch (IOException e) {
               Throwables.propagate(e);
            }
            clientNew.disconnect();

         }

         private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
            client.connect();

            try {
               runScript(client, scriptName, Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
                        .getResourceAsStream("/initscript_with_java.sh")), nodeId);
            } catch (IOException e) {
               Throwables.propagate(e);
            }

            client.disconnect();

         }

         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);
         }

         private void helloAndJava(SshClient client) {
            client.connect();

            expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0));
            expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0));

            client.disconnect();
         }

      };
View Full Code Here


         if (options.shouldBlockUntilRunning()) {
            pollNodeRunning.apply(node);
            if (statement != null) {
               RunScriptOnNode runner = initScriptRunnerFactory.create(node.get(), statement, options, badNodes).call();
               if (runner != null) {
                  ExecResponse exec = runner.call();
                  customizationResponses.put(node.get(), exec);
               }
            }
            if (options.getPort() > 0) {
               openSocketFinder.findOpenSocketOnNode(node.get(), options.getPort(), options.getSeconds(),
View Full Code Here

   protected void doConnectViaSsh(VirtualGuest guest, LoginCredentials creds) {
      SshClient ssh = sshFactory.create(HostAndPort.fromParts(guest.getPrimaryIpAddress(), 22), creds);
      try {
         ssh.connect();
         ExecResponse hello = ssh.exec("echo hello");
         assertEquals(hello.getOutput().trim(), "hello");
         System.err.println(ssh.exec("df -k").getOutput());
         System.err.println(ssh.exec("mount").getOutput());
         System.err.println(ssh.exec("uname -a").getOutput());
      } finally {
         if (ssh != null)
View Full Code Here

            Command output = session.exec(checkNotNull(command, "command"));
            String outputString = IOUtils.readFully(output.getInputStream()).toString();
            output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS);
            int errorStatus = output.getExitStatus();
            String errorString = IOUtils.readFully(output.getErrorStream()).toString();
            return new ExecResponse(outputString, errorString, errorStatus);
         } finally {
            clear();
         }
      }
View Full Code Here

   protected void checkSSH(HostAndPort socket) {
      socketTester.apply(socket);
      SshClient client = sshFactory.create(socket, loginCredentials);
      try {
         client.connect();
         ExecResponse exec = client.exec("echo hello");
         System.out.println(exec);
         assertEquals(exec.getOutput().trim(), "hello");
      } finally {
         if (client != null)
            client.disconnect();
      }
   }
View Full Code Here

   protected void doConnectViaSsh(Server server, LoginCredentials creds) throws IOException {
      SshClient ssh = Guice.createInjector(new SshjSshClientModule()).getInstance(SshClient.Factory.class).create(
               HostAndPort.fromParts(server.getVnc().getIp(), 22), creds);
      try {
         ssh.connect();
         ExecResponse hello = ssh.exec("echo hello");
         assertEquals(hello.getOutput().trim(), "hello");
         System.err.println(ssh.exec("df -k").getOutput());
         System.err.println(ssh.exec("mount").getOutput());
         System.err.println(ssh.exec("uname -a").getOutput());
      } finally {
         if (ssh != null)
View Full Code Here

        LoginCredentials credentials = null;

        if (user != null) {
            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.getExitCode());
        exchange.getOut().setBody(execResponse.getOutput());
    }
View Full Code Here

        Credentials credentials = null;

        if (user != null) {
            credentials = new Credentials(user, null);
        }
        ExecResponse execResponse = null;

        if (credentials == null) {
            execResponse = computeService.runScriptOnNode(nodeId, script);
        } else {
            execResponse = computeService.runScriptOnNode(nodeId, script, RunScriptOptions.Builder.overrideCredentialsWith(credentials).runAsRoot(false));
        }

        if (execResponse == null) {
            throw new CamelException("Failed to receive response for run script operation.");
        }

        exchange.setProperty(JcloudsConstants.RUN_SCRIPT_ERROR, execResponse.getError());
        exchange.setProperty(JcloudsConstants.RUN_SCRIPT_EXIT_CODE, execResponse.getExitCode());
        if (execResponse != null) {
            exchange.getOut().setBody(execResponse.getOutput());
        }
    }
View Full Code Here

    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.getExitCode() != 0) {
        rc = response.getExitCode();
      }
      out.printf("%s%n", response.getOutput());
      err.printf("%s%n", response.getError());
    }
    return rc;
  }
View Full Code Here

   public void testCanRunCommandAsCurrentUser() throws Exception {
      Set<? extends NodeMetadata> nodes = view.getComputeService().createNodesInGroup("goo", 1);
      NodeMetadata node = Iterables.get(nodes, 0);

      try {
         ExecResponse response = view.getComputeService().runScriptOnNode(node.getId(), exec("id"),
                  wrapInInitScript(false).runAsRoot(false));
         assert response.getOutput().trim().contains(System.getProperty("user.name")) : node + ": " + response;
      } finally {
         view.getComputeService().destroyNode(node.getId());
      }
   }
View Full Code Here

TOP

Related Classes of org.jclouds.compute.domain.ExecResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.