/** fails with nice error if the given file does not exist */ public static String requireFile(String file) { return requireTest("-f "+BashStringEscapes.wrapBash(file), "The required file \""+file+"\" does not exist"); }
private boolean checkFileExistsOnEntity(Entity entity, String path) { String cmd = BashCommands.chain( BashCommands.requireTest(String.format("-f \"%s\"", path), "File " + path + " doesn't exist.")); String summary = "Check if file " + path + " exists"; return DynamicTasks.queue(execSshTask(entity, cmd, summary).allowingNonZeroExitCode()).asTask().getUnchecked() == 0; }
/** 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; } }); }
@Test(groups="Integration") public void testRequireTestHandlesSuccess() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceFile1.getPath(), "The requested file does not exist")).newTask(); exec.submit(t).get(); assertEquals(t.getExitCode(), (Integer)0); assertTrue(t.getStderr().equals(""), "Expected no stderr messages, but got: "+t.getStderr()); }
@Test(groups="Integration") public void testRequireFailureExitsImmediately() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), "The requested file does not exist")) .add("echo shouldnae come here").newTask(); exec.submit(t).get(); assertNotEquals(t.getExitCode(), 0); assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); Assert.assertFalse(t.getStdout().contains("shouldnae"), "Expected message in: "+t.getStdout()); }
@Test(groups="Integration") public void testRequireTestHandlesFailure() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), "The requested file does not exist")).newTask(); exec.submit(t).get(); assertNotEquals(t.getExitCode(), 0); assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); }
private boolean fileExists(MySqlNode node, String binLogName) { String dataDir = Strings.nullToEmpty(node.getConfig(MySqlNode.DATA_DIR)); String path = Os.mergePathsUnix(dataDir, binLogName); String cmd = BashCommands.chain( "cd $RUN_DIR", BashCommands.requireTest(String.format("-f \"%s\"", path), "File " + path + " doesn't exist.")); String summary = "Check if file " + path + " exists"; SshMachineLocation machine = EffectorTasks.getSshMachine(node); return Entities.submit(node, SshTasks.newSshExecTaskFactory(machine, cmd) .allowingNonZeroExitCode() .environmentVariable("RUN_DIR", node.getAttribute(SoftwareProcess.RUN_DIR)) .summary(summary) .allowingNonZeroExitCode()).asTask().getUnchecked() == 0; } private void cleanData() {
"cd "+privateTmpDirContainingUnpackedCookbook, "COOKBOOK_EXPANDED_DIR=`ls`", BashCommands.requireTest("`ls | wc -w` -eq 1", "The deployed archive "+cookbookArchiveUrl+" must contain exactly one directory"), "mv $COOKBOOK_EXPANDED_DIR '../"+cookbookName+"'",
"cd "+privateTmpDirContainingUnpackedCookbook, "COOKBOOK_EXPANDED_DIR=`ls`", BashCommands.requireTest("`ls | wc -w` -eq 1", "The deployed archive "+cookbookArchiveUrl+" must contain exactly one directory"), "mv $COOKBOOK_EXPANDED_DIR '../"+cookbookName+"'",