JCLOUDS-365: ChefSolo should not depend on InstallChefGems

This commit is contained in:
Ignasi Barrera 2013-11-06 10:56:41 +01:00
parent 4500dbe2ef
commit cccdb756d1
2 changed files with 26 additions and 41 deletions

View File

@ -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<Role> roles = Lists.newArrayList();
private List<DataBag> 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<List<Role>> roles;
private Optional<List<DataBag>> databags;
private RunList runlist;
private final InstallChefGems installChefGems;
protected ChefSolo(Optional<String> fileCachePath, Optional<String> rolePath, Optional<String> databagPath,
Optional<ImmutableList<String>> cookbookPath, Optional<String> cookbooksArchiveLocation,
Optional<String> jsonAttributes, Optional<String> group, Optional<Integer> interval,
Optional<String> logLevel, Optional<String> logFile, Optional<String> nodeName, Optional<Integer> splay,
Optional<String> user, Optional<List<Role>> roles, Optional<List<DataBag>> databags,
Optional<RunList> runlist, Optional<String> chefVersion) {
Optional<String> user, Optional<List<Role>> roles, Optional<List<DataBag>> databags, Optional<RunList> 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.<String> of(this.fileCachePath + "/cookbooks");
} else {
this.cookbookPath = ImmutableList.<String> copyOf(cookbookPath.get());
}
this.installChefGems = InstallChefGems.builder().version(chefVersion.orNull()).build();
}
@Override
@ -312,8 +300,8 @@ public class ChefSolo implements Statement {
}
ImmutableList.Builder<Statement> 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<String> functionDependencies(OsFamily family) {
return installChefGems.functionDependencies(family);
return Collections.emptyList();
}
private void checkChefIsInstalled(ImmutableList.Builder<Statement> 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

View File

@ -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";
}
}