aslak

Untitled

Sep 12th, 2010
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1.       ComputeServiceContext computeContext = new ComputeServiceContextFactory().createContext(
  2.             config.getProvider(),
  3.             config.getAccount(),
  4.             config.getKey(),
  5.             ImmutableSet.of(
  6.                   new Log4JLoggingModule(),
  7.                   new JschSshClientModule()));
  8.  
  9.       ComputeService computeService = computeContext.getComputeService();
  10.  
  11.       Map<? extends NodeMetadata, ExecResponse> responses = computeService.runScriptOnNodesMatching(
  12.             NodePredicates.withTag(config.getTag()),
  13.             Payloads.newStringPayload(createScript()),
  14.             RunScriptOptions.Builder.runAsRoot(true));
  15.      
  16.       for(Map.Entry<? extends NodeMetadata, ExecResponse> response : responses.entrySet())
  17.       {
  18.          System.out.println(response.getKey().getId() + " " +  response.getKey().getName() + " " + response.getKey().getTag());
  19.          System.out.println(response.getValue().getExitCode());
  20.          System.out.println(response.getValue().getError());
  21.          System.out.println(response.getValue().getOutput());
  22.       }
  23.    }
  24.    
  25.    private String createScript()
  26.    {
  27.       return Statements.newStatementList(
  28.                Statements.createFile(
  29.                  "root.was.here",
  30.                      ImmutableList.of(
  31.                         "root.was.here"
  32.               )),
  33.               Statements.exec("apt get foo"),
  34.               Statements.exec("apt install foo")
  35.        ).render(OsFamily.UNIX);
  36.      
  37.    }
  38.  
  39. '
  40.   /**
  41.    * build a shell script that invokes the contents of the http request in bash.
  42.    *
  43.    * @return a shell script that will invoke the http request
  44.    */
  45.   public static String buildCurluntar(String destination, HttpRequest request) {
  46.      String headers = Joiner.on(' ').join(
  47.               Iterables.transform(request.getHeaders().entries(), new Function<Entry<String, String>, String>() {
  48.  
  49.                  @Override
  50.                  public String apply(Entry<String, String> from) {
  51.                     return String.format("-H \"%s: %s\"", from.getKey(), from.getValue());
  52.                  }
  53.  
  54.               }));
  55.      return String.format("mkdir -p "+ destination+ ";cd "+ destination+ ";curl -s --retry 20 %s %s |tar -xzf -\n", headers, request.getEndpoint().toASCIIString());
  56.   }
Add Comment
Please, Sign In to add comment