From cccdb756d14b9c62295e0df9ded5fab5256227ed Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Wed, 6 Nov 2013 10:56:41 +0100 Subject: [PATCH] JCLOUDS-365: ChefSolo should not depend on InstallChefGems --- .../statements/chef/ChefSolo.java | 31 +++++++--------- .../statements/chef/ChefSoloTest.java | 36 +++++++------------ 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/statements/chef/ChefSolo.java b/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/statements/chef/ChefSolo.java index c7a0931435..ad507eaa5b 100644 --- a/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/statements/chef/ChefSolo.java +++ b/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/statements/chef/ChefSolo.java @@ -21,6 +21,7 @@ import static com.google.common.collect.Iterables.transform; import static org.jclouds.scriptbuilder.domain.Statements.createOrOverwriteFile; import static org.jclouds.scriptbuilder.domain.Statements.exec; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -77,7 +78,6 @@ public class ChefSolo implements Statement { private List roles = Lists.newArrayList(); private List databags = Lists.newArrayList(); private RunList runlist; - private String chefVersion; /** * Directory where Chef Solo will store files. @@ -237,22 +237,13 @@ public class ChefSolo implements Statement { return this; } - /** - * The version of the Chef gem to install. - */ - public Builder chefVersion(String chefVersion) { - this.chefVersion = checkNotNull(chefVersion, "chefVersion"); - return this; - } - public ChefSolo build() { return new ChefSolo(Optional.of(fileCachePath), Optional.fromNullable(rolePath), Optional.fromNullable(databagPath), Optional.of(cookbookPath.build()), Optional.fromNullable(cookbooksArchiveLocation), Optional.fromNullable(jsonAttributes), Optional.fromNullable(group), Optional.fromNullable(interval), Optional.fromNullable(logLevel), Optional.fromNullable(logFile), Optional.fromNullable(nodeName), Optional.fromNullable(splay), - Optional.fromNullable(user), Optional.of(roles), Optional.of(databags), Optional.fromNullable(runlist), - Optional.fromNullable(chefVersion)); + Optional.fromNullable(user), Optional.of(roles), Optional.of(databags), Optional.fromNullable(runlist)); } } @@ -273,14 +264,12 @@ public class ChefSolo implements Statement { private Optional> roles; private Optional> databags; private RunList runlist; - private final InstallChefGems installChefGems; protected ChefSolo(Optional fileCachePath, Optional rolePath, Optional databagPath, Optional> cookbookPath, Optional cookbooksArchiveLocation, Optional jsonAttributes, Optional group, Optional interval, Optional logLevel, Optional logFile, Optional nodeName, Optional splay, - Optional user, Optional> roles, Optional> databags, - Optional runlist, Optional chefVersion) { + Optional user, Optional> roles, Optional> databags, Optional runlist) { this.fileCachePath = checkNotNull(fileCachePath, "fileCachePath must be set").or(DEFAULT_SOLO_PATH); this.rolePath = checkNotNull(rolePath, "rolePath must be set").or(this.fileCachePath + "/roles"); this.databagPath = checkNotNull(databagPath, "databagPath must be set").or(this.fileCachePath + "/data_bags"); @@ -296,13 +285,12 @@ public class ChefSolo implements Statement { this.roles = checkNotNull(roles, "roles must be set"); this.databags = checkNotNull(databags, "databags must be set"); this.runlist = checkNotNull(runlist, "runlist must be set").or(RunList.builder().build()); - this.user = checkNotNull(user, "chefVersion must be set"); + this.user = checkNotNull(user, "user must be set"); if (!checkNotNull(cookbookPath, "cookbookPath must be set").isPresent() || cookbookPath.get().isEmpty()) { this.cookbookPath = ImmutableList. of(this.fileCachePath + "/cookbooks"); } else { this.cookbookPath = ImmutableList. copyOf(cookbookPath.get()); } - this.installChefGems = InstallChefGems.builder().version(chefVersion.orNull()).build(); } @Override @@ -312,8 +300,8 @@ public class ChefSolo implements Statement { } ImmutableList.Builder statements = ImmutableList.builder(); - statements.add(installChefGems); + checkChefIsInstalled(statements); createSoloConfiguration(statements); createRolesIfNecessary(statements); createDatabagsIfNecessary(statements); @@ -354,7 +342,14 @@ public class ChefSolo implements Statement { @Override public Iterable functionDependencies(OsFamily family) { - return installChefGems.functionDependencies(family); + return Collections.emptyList(); + } + + private void checkChefIsInstalled(ImmutableList.Builder statements) { + statements.add(exec("if ! hash chef-solo 2>/dev/null; then")); + statements.add(exec("echo 'chef-solo not found. Please, install Chef first'")); + statements.add(exec("exit 1")); + statements.add(exec("fi")); } @VisibleForTesting diff --git a/scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/chef/ChefSoloTest.java b/scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/chef/ChefSoloTest.java index 0b96ad7412..34bc581844 100644 --- a/scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/chef/ChefSoloTest.java +++ b/scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/chef/ChefSoloTest.java @@ -323,72 +323,58 @@ public class ChefSoloTest { public void testChefWoloWithDefaultConfiguration() throws IOException { String script = ChefSolo.builder().build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname`\n"); } public void testChefWoloWithNodeName() throws IOException { String script = ChefSolo.builder().nodeName("foo").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N foo\n"); } public void testChefSoloWithGroup() throws IOException { String script = ChefSolo.builder().group("foo").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -g foo\n"); } public void testChefSoloWithInterval() throws IOException { String script = ChefSolo.builder().interval(15).build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -i 15\n"); } public void testChefSoloWithLogLevel() throws IOException { String script = ChefSolo.builder().logLevel("debug").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -l debug\n"); } public void testChefSoloWithLogFile() throws IOException { String script = ChefSolo.builder().logFile("/var/log/solo.log").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -L /var/log/solo.log\n"); } public void testChefSoloWithCookbooksLocation() throws IOException { String script = ChefSolo.builder().cookbooksArchiveLocation("/tmp/cookbooks").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -r /tmp/cookbooks\n"); } public void testChefSoloWithSplay() throws IOException { String script = ChefSolo.builder().splay(15).build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -s 15\n"); } public void testChefSoloWithUser() throws IOException { String script = ChefSolo.builder().user("foo").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems() + createConfigFile() + createNodeFile() + assertEquals(script, checkSoloExists() + createConfigFile() + createNodeFile() + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname` -u foo\n"); } - public void testChefSoloWithChefGemVersion() throws IOException { - String script = ChefSolo.builder().chefVersion(">= 0.10.8").build().render(OsFamily.UNIX); - assertEquals(script, installChefGems(">= 0.10.8") + createConfigFile() + createNodeFile() - + "chef-solo -c /var/chef/solo.rb -j /var/chef/node.json -N `hostname`\n"); - } - - private static String installChefGems() throws IOException { - return "gem install chef --no-rdoc --no-ri\n"; - } - - private static String installChefGems(String version) throws IOException { - return "gem install chef -v '" + version + "' --no-rdoc --no-ri\n"; - } - private static String createConfigFile() { return "mkdir -p /var/chef\nmkdir -p /var/chef/cookbooks\ncat > /var/chef/solo.rb <<-'END_OF_JCLOUDS_FILE'\n" + "\tfile_cache_path \"/var/chef\"\n\tcookbook_path [\"/var/chef/cookbooks\"]\n" @@ -399,4 +385,8 @@ public class ChefSoloTest { return "cat > /var/chef/node.json <<-'END_OF_JCLOUDS_FILE'\n\t{\"run_list\":[]}\nEND_OF_JCLOUDS_FILE\n"; } + private static String checkSoloExists() { + return "if ! hash chef-solo 2>/dev/null; then\necho 'chef-solo not found. Please, install Chef first'\nexit 1\nfi\n"; + } + }