private void addUnixRunScript(String runScript, StringBuilder builder) { builder.append("# add desired commands from the user\n"); Builder<String> userCommands = ImmutableList.builder(); userCommands.add("cd " + pwd); for (Statement statement : statements) { if (statement instanceof Call || (statement instanceof StatementList && any(StatementList.class.cast(statement).delegate(), instanceOf(Call.class)))) { statement = new ExitInsteadOfReturn(statement); } userCommands.addAll(Splitter.on('\n').split(statement.render(OsFamily.UNIX))); } builder.append(appendFile(runScript, userCommands.build(), DELIMITER).render(OsFamily.UNIX)); }
@Override public Iterable<String> functionDependencies(OsFamily family) { Builder<String> functions = ImmutableList.builder(); for (Statement statement : delegate()) { functions.addAll(statement.functionDependencies(family)); } return functions.build(); }
public void testCreateDefaultNodeConfiguration() { ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ChefSolo solo = ChefSolo.builder().build(); solo.createNodeConfiguration(statements); ImmutableList<Statement> statementList = statements.build(); Statement expected = createOrOverwriteFile(ChefSolo.DEFAULT_SOLO_PATH + "/node.json", ImmutableSet.of("{\"run_list\":[]}")); assertEquals(statementList.size(), 1); assertEquals(statementList.get(0).render(OsFamily.UNIX), expected.render(OsFamily.UNIX)); }
/** * {@inheritDoc} */ @Override public String render(OsFamily arg0) { StringBuilder command = new StringBuilder(); command.append("git clone"); if (gitRepoAndRef.getBranch().isPresent()) command.append(" -b ").append(gitRepoAndRef.getBranch().get()); command.append(' ').append(gitRepoAndRef.getRepository().toASCIIString()); if (directory.isPresent()) command.append(' ').append(directory.get()); command.append("{lf}"); command.append("{cd} ").append( directory.or(Iterables.getLast(Splitter.on('/').split(gitRepoAndRef.getRepository().getPath())).replace(".git", ""))); if (gitRepoAndRef.getTag().isPresent()) { command.append("{lf}").append("git checkout ").append(gitRepoAndRef.getTag().get()); } return Statements.exec(command.toString()).render(arg0); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public void testWINDOWS() { assertEquals(statement.render(OsFamily.WINDOWS), "call :default\r\nif errorlevel 1 goto abort\r\necho started\r\n"); } }
public void testExtractTargzIntoDirectoryUNIX() { Statement save = Statements .extractTargzIntoDirectory( URI.create("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.tar.gz"), "/opt/minecraft"); assertEquals( save.render(OsFamily.UNIX), "curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.tar.gz |(mkdir -p /opt/minecraft &&cd /opt/minecraft &&tar -xpzf -)\n"); }
public void testSaveHttpResponseToUNIX() { Statement save = Statements.saveHttpResponseTo( URI.create("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"), "/opt/minecraft", "minecraft_server.jar"); assertEquals( save.render(OsFamily.UNIX), "(mkdir -p /opt/minecraft && cd /opt/minecraft && [ ! -f minecraft_server.jar ] && curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -C - -X GET https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar >minecraft_server.jar)\n"); }
public void testExtractTargzAndFlattenIntoDirectoryUNIX() { Statement save = Statements .extractTargzAndFlattenIntoDirectory( URI.create("http://www.us.apache.org/dist/maven/binaries/apache-maven-3.0.4-bin.tar.gz"), "/usr/local/maven"); assertEquals( save.render(OsFamily.UNIX), "mkdir /tmp/$$\n" + "curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET http://www.us.apache.org/dist/maven/binaries/apache-maven-3.0.4-bin.tar.gz |(mkdir -p /tmp/$$ &&cd /tmp/$$ &&tar -xpzf -)\n" + "mkdir -p /usr/local/maven\n" + "mv /tmp/$$/*/* /usr/local/maven\n" + "rm -rf /tmp/$$\n"); }
public String render(OsFamily family) { String linesToPrepend = Joiner.on('\n').withKeyValueSeparator(" ").join(params); Statement prependSshdConfig = exec(String.format( "exec 3<> %1$s && awk -v TEXT=\"%2$s\n\" 'BEGIN {print TEXT}{print}' %1$s >&3", sshdConfig, linesToPrepend)); Statement reloadSshdConfig = exec("hash service 2>&- && service ssh reload 2>&- || /etc/init.d/ssh* reload"); return newStatementList(prependSshdConfig, reloadSshdConfig).render(family); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testCreateWheelWindowsNotSupported() { SudoStatements.createWheel().render(OsFamily.WINDOWS); } }
@Override public Iterable<String> apply(Statement from) { return from.functionDependencies(osFamily); } })));
public void testCreateDefaultNodeConfiguration() { ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ChefSolo solo = ChefSolo.builder().build(); solo.createNodeConfiguration(statements); ImmutableList<Statement> statementList = statements.build(); Statement expected = createOrOverwriteFile(ChefSolo.DEFAULT_SOLO_PATH + "/node.json", ImmutableSet.of("{\"run_list\":[]}")); assertEquals(statementList.size(), 1); assertEquals(statementList.get(0).render(OsFamily.UNIX), expected.render(OsFamily.UNIX)); }
/** * {@inheritDoc} */ @Override public String render(OsFamily arg0) { StringBuilder command = new StringBuilder(); command.append("git clone"); if (gitRepoAndRef.getBranch().isPresent()) command.append(" -b ").append(gitRepoAndRef.getBranch().get()); command.append(' ').append(gitRepoAndRef.getRepository().toASCIIString()); if (directory.isPresent()) command.append(' ').append(directory.get()); command.append("{lf}"); command.append("{cd} ").append( directory.or(Iterables.getLast(Splitter.on('/').split(gitRepoAndRef.getRepository().getPath())).replace(".git", ""))); if (gitRepoAndRef.getTag().isPresent()) { command.append("{lf}").append("git checkout ").append(gitRepoAndRef.getTag().get()); } return Statements.exec(command.toString()).render(arg0); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public void testCreateWheelUNIX() { assertEquals( SudoStatements.createWheel().render(OsFamily.UNIX), "cat > /etc/sudoers <<-'END_OF_JCLOUDS_FILE'\n"+ "\troot ALL = (ALL) ALL\n"+ "\t%wheel ALL = (ALL) NOPASSWD:ALL\n"+ "END_OF_JCLOUDS_FILE\n"+ "chmod 0440 /etc/sudoers\n"); }
public void testExtractTargzIntoDirectoryUNIX() { Statement save = Statements .extractTargzIntoDirectory( URI.create("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.tar.gz"), "/opt/minecraft"); assertEquals( save.render(OsFamily.UNIX), "curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.tar.gz |(mkdir -p /opt/minecraft &&cd /opt/minecraft &&tar -xpzf -)\n"); }
@Override public String render(OsFamily family) { if (family == OsFamily.WINDOWS) { throw new UnsupportedOperationException("windows not yet implemented"); } Statement statement = version.isPresent() ? exec(String.format("gem install chef -v '%s' --no-rdoc --no-ri", version.get())) : exec("gem install chef --no-rdoc --no-ri"); return statement.render(family); }