@Override public ExecResponse call() { checkState(ssh != null, "please call init() before invoking call"); try { ssh.connect(); ExecResponse returnVal; eventBus.post(new StatementOnNodeSubmission(statement, node)); String command = runAsRoot ? execAsRoot(statement.render(OsFamily.UNIX)) : execScriptAsDefaultUser(statement.render(OsFamily.UNIX)); try { returnVal = runCommand(command); } catch (Throwable e) { eventBus.post(new StatementOnNodeFailure(statement, node, e)); throw Throwables.propagate(e); } eventBus.post(new StatementOnNodeCompletion(statement, node, returnVal)); if (logger.isTraceEnabled()) logger.trace("<< %s[%s]", statement, returnVal); else logger.debug("<< %s(%d)", statement, returnVal.getExitStatus()); return returnVal; } finally { if (ssh != null) ssh.disconnect(); } }
protected ExecResponse runAction(String action) { ExecResponse returnVal; String command = (runAsRoot) ? execScriptAsRoot(action) : execScriptAsDefaultUser(action); returnVal = runCommand(command); if (logger.isTraceEnabled()) logger.trace("<< %s[%s]", action, returnVal); else logger.debug("<< %s(%d)", action, returnVal.getExitCode()); return returnVal; }
public static <T> Map<T, Exception> awaitCompletion( Map<T, ? extends Future<?>> responses, ExecutorService exec, @Nullable Long maxTime, final Logger logger, final String logPrefix) { if (responses.size() == 0) return ImmutableMap.of(); final int total = responses.size(); final CountDownLatch doneSignal = new CountDownLatch(total); final AtomicInteger complete = new AtomicInteger(0); final AtomicInteger errors = new AtomicInteger(0); final long start = System.currentTimeMillis(); final Map<T, Exception> errorMap = Maps.newHashMap(); for (final java.util.Map.Entry<T, ? extends Future<?>> future : responses.entrySet()) { Futures.makeListenable(future.getValue(), exec) .addListener( new Runnable() { @Override public void run() { try { future.getValue().get(); complete.incrementAndGet(); } catch (Exception e) { errors.incrementAndGet(); logException(logger, logPrefix, total, complete.get(), errors.get(), start, e); errorMap.put(future.getKey(), e); } doneSignal.countDown(); } @Override public String toString() { return "callGetOnFuture(" + future.getKey() + "," + future.getValue() + ")"; } }, exec); } try { if (maxTime != null) doneSignal.await(maxTime, TimeUnit.MILLISECONDS); else doneSignal.await(); if (errors.get() > 0) { String message = message(logPrefix, total, complete.get(), errors.get(), start); RuntimeException exception = new RuntimeException(message); logger.error(exception, message); } if (logger.isTraceEnabled()) { String message = message(logPrefix, total, complete.get(), errors.get(), start); logger.trace(message); } } catch (InterruptedException e) { String message = message(logPrefix, total, complete.get(), errors.get(), start); TimeoutException exception = new TimeoutException(message); logger.error(exception, message); Throwables.propagate(exception); } return errorMap; }
public ExecResponse runAction(String action) { ExecResponse returnVal; String command = (runAsRoot && Predicates.in(ImmutableSet.of("start", "stop", "run")).apply(action)) ? execScriptAsRoot(action) : execScriptAsDefaultUser(action); returnVal = runCommand(command); if ("status".equals(action)) logger.trace("<< %s(%d)", action, returnVal.getExitCode()); else if (computeLogger.isTraceEnabled()) computeLogger.trace("<< %s[%s]", action, returnVal); else computeLogger.debug("<< %s(%d)", action, returnVal.getExitCode()); return returnVal; }
/** ssh client is initialized through this call. */ protected ExecResponse doCall() { try { ssh.put(initFile, init.render(OsFamily.UNIX)); } catch (SshException e) { // If there's a problem with the sftp configuration, we can try via ssh exec if (logger.isTraceEnabled()) logger.warn( e, "<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); else logger.warn( "<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); ssh.disconnect(); ssh.connect(); ssh.exec("rm " + initFile); ssh.exec( Statements.appendFile( initFile, Splitter.on('\n').split(init.render(OsFamily.UNIX)), AppendFile.MARKER + "_" + init.getInstanceName()) .render(OsFamily.UNIX)); } ssh.exec("chmod 755 " + initFile); setupLinkToInitFile(); runAction("init"); init.getInitStatement() .accept( new AdminAccessVisitor() { @Override public void visit(AdminAccess input) { refreshSshIfNewAdminCredentialsConfigured(input); } }); return runAction("start"); }