Integer checkExists = DynamicTasks.queue(SshEffectorTasks.ssh("ls \""+brooklynGlobalPropertiesRemotePath+"\"").allowingNonZeroExitCode()).get(); boolean doUpload = true; if (checkExists==0) {
protected boolean tryStopPid() { Integer pid = entity().getAttribute(Attributes.PID); if (pid==null) { if (entity().getAttribute(Attributes.SERVICE_STATE_ACTUAL)==Lifecycle.RUNNING && getPidFile()==null) log.warn("No PID recorded for "+entity()+" when running, with PID file "+getPidFile()+"; skipping kill in "+Tasks.current()); else if (log.isDebugEnabled()) log.debug("No PID recorded for "+entity()+"; skipping ("+entity().getAttribute(Attributes.SERVICE_STATE_ACTUAL)+" / "+getPidFile()+")"); return false; } // allow non-zero exit as process may have already been killed DynamicTasks.queue(SshEffectorTasks.ssh( "kill "+pid, "sleep 5", BashCommands.ok("kill -9 "+pid)).allowingNonZeroExitCode().runAsRoot()).block(); if (DynamicTasks.queue(SshEffectorTasks.isPidRunning(pid).runAsRoot()).get()) { throw new IllegalStateException("Process for "+entity()+" in "+pid+" still running after kill"); } entity().sensors().set(Attributes.PID, null); return true; }
protected boolean tryStopPid() { Integer pid = entity().getAttribute(Attributes.PID); if (pid==null) { if (entity().getAttribute(Attributes.SERVICE_STATE_ACTUAL)==Lifecycle.RUNNING && getPidFile()==null) log.warn("No PID recorded for "+entity()+" when running, with PID file "+getPidFile()+"; skipping kill in "+Tasks.current()); else if (log.isDebugEnabled()) log.debug("No PID recorded for "+entity()+"; skipping ("+entity().getAttribute(Attributes.SERVICE_STATE_ACTUAL)+" / "+getPidFile()+")"); return false; } // allow non-zero exit as process may have already been killed DynamicTasks.queue(SshEffectorTasks.ssh( "kill "+pid, "sleep 5", BashCommands.ok("kill -9 "+pid)).allowingNonZeroExitCode().runAsRoot()).block(); if (DynamicTasks.queue(SshEffectorTasks.isPidRunning(pid).runAsRoot()).get()) { throw new IllegalStateException("Process for "+entity()+" in "+pid+" still running after kill"); } entity().sensors().set(Attributes.PID, null); return true; }
if (DynamicTasks.queue(SshEffectorTasks.ssh(sudoAsUser("postgres", "ls "+getInstallDir())).allowingNonZeroExitCode() .summary("check postgres user can access install dir")).asTask().getUnchecked()!=0) { log.info("Postgres install dir "+getInstallDir()+" for "+getEntity()+" is not accessible to user 'postgres'; " + "using "+altInstallDir+" instead"); String newRunDir = Urls.mergePaths(altTarget, "apps", getEntity().getApplication().getId(), getEntity().getId()); if (DynamicTasks.queue(SshEffectorTasks.ssh("ls "+altInstallDir+"/pg_ctl").allowingNonZeroExitCode() .summary("check whether "+altInstallDir+" is set up")).asTask().getUnchecked()==0) {
@Override public void customize() { DynamicTasks.queue(SshEffectorTasks.ssh(sudoAsUser("postgres", "/etc/init.d/postgresql stop")).allowingNonZeroExitCode()).get();
/** task which returns 0 if pid in the given file is running; * method accepts wildcards so long as they match a single file on the remote end * <p> * returns 1 if no matching file, * 1 if matching file but no matching process, * and 2 if 2+ matching files */ public static SshEffectorTaskFactory<Integer> codePidFromFileRunning(final String pidFile) { return ssh(BashCommands.chain( // this fails, but isn't an error BashCommands.requireTest("-f "+pidFile, "The PID file "+pidFile+" does not exist."), // this fails and logs an error picked up later BashCommands.requireTest("`ls "+pidFile+" | wc -w` -eq 1", "ERROR: there are multiple matching PID files"), // this fails and logs an error picked up later BashCommands.require("cat "+pidFile, "ERROR: the PID file "+pidFile+" cannot be read (permissions?)."), // finally check the process "ps -p `cat "+pidFile+"`")).summary("PID file "+pidFile+" is-running check (exit code)") .allowingNonZeroExitCode() .addCompletionListener(new Function<ProcessTaskWrapper<?>,Void>() { @Override public Void apply(ProcessTaskWrapper<?> input) { if (input.getStderr().contains("ERROR:")) throw new IllegalStateException("Invalid or inaccessible PID filespec: "+pidFile); return null; } }); }
@Override public int execCommandStatusTimeout(String command, Duration timeout) { ProcessTaskWrapper<Object> task = SshEffectorTasks.ssh(command) .environmentVariables(((AbstractSoftwareProcessSshDriver) getDriver()).getShellEnvironment()) .returning(ScriptReturnType.EXIT_CODE) .allowingNonZeroExitCode() .machine(getMachine()) .summary(command) .newTask(); try { Object result = DynamicTasks.queueIfPossible(task) .executionContext(this) .orSubmitAsync() .asTask() .get(timeout); return (Integer) result; } catch (TimeoutException te) { throw new IllegalStateException("Timed out running command: " + command); } catch (Exception e) { Integer exitCode = task.getExitCode(); LOG.warn("Command failed, return code {}: {}", exitCode == null ? -1 : exitCode, task.getStderr()); throw Exceptions.propagate(e); } }
@Test(groups="Live") public void testPostgresStartsAndStops() throws Exception { ChefLiveTestSupport.installBrooklynChefHostedConfig(app); psql = app.createAndManageChild(PostgreSqlSpecs.specChef()); app.start(ImmutableList.of(targetLocation)); Entities.submit(psql, SshEffectorTasks.ssh("ps aux | grep [p]ostgres").requiringExitCodeZero()); SshMachineLocation targetMachine = EffectorTasks.getSshMachine(psql); psql.stop(); try { // if host is still contactable ensure postgres is not running ProcessTaskWrapper<Integer> t = Entities.submit(app, SshEffectorTasks.ssh("ps aux | grep [p]ostgres").machine(targetMachine).allowingNonZeroExitCode()); t.getTask().blockUntilEnded(Duration.TEN_SECONDS); if (!t.isDone()) Assert.fail("Task not finished yet: "+t.getTask()); Assert.assertNotEquals(t.get(), 0, "Task ended with code "+t.get()+"; output: "+t.getStdout() ); } catch (Exception e) { // host has been killed, that is fine log.info("Machine "+targetMachine+" destroyed on stop (expected - "+e+")"); } }
/** task which returns 0 if pid is running */ public static SshEffectorTaskFactory<Integer> codePidRunning(Integer pid) { return ssh("ps -p "+pid).summary("PID "+pid+" is-running check (exit code)").allowingNonZeroExitCode(); }