WHIRR-705. Provide better integration between parameterized classes and Hiera. Contributed by Roman Shaposhnik.
Project: http://git-wip-us.apache.org/repos/asf/whirr/repo Commit: http://git-wip-us.apache.org/repos/asf/whirr/commit/8bc196a8 Tree: http://git-wip-us.apache.org/repos/asf/whirr/tree/8bc196a8 Diff: http://git-wip-us.apache.org/repos/asf/whirr/diff/8bc196a8 Branch: refs/heads/trunk Commit: 8bc196a8a2e2a1c39030a43cb636ebccae090ee8 Parents: f9d0cd4 Author: Andrew Bayer <andrew.ba...@gmail.com> Authored: Sun Mar 31 20:48:28 2013 -0700 Committer: Andrew Bayer <andrew.ba...@gmail.com> Committed: Sun Mar 31 20:48:28 2013 -0700 ---------------------------------------------------------------------- CHANGES.txt | 3 + .../org/apache/whirr/service/puppet/Manifest.java | 15 ++++++- .../whirr/service/puppet/PuppetConstants.java | 3 + .../statements/CreateSitePpAndApplyRoles.java | 37 +++++++++++++-- .../apache/whirr/service/puppet/ManifestTest.java | 21 ++++++++ 5 files changed, 74 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/whirr/blob/8bc196a8/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index b2e86cf..c67d915 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,9 @@ Release 0.8.2 (unreleased changes) IMPROVEMENTS + WHIRR-705. Provide better integration between parameterized + classes and Hiera. (Roman Shaposhnik via abayer) + WHIRR-694. Install puppet from puppetlabs repo instead of ruby gem. (Roman Shaposhnik via abayer) http://git-wip-us.apache.org/repos/asf/whirr/blob/8bc196a8/services/puppet/src/main/java/org/apache/whirr/service/puppet/Manifest.java ---------------------------------------------------------------------- diff --git a/services/puppet/src/main/java/org/apache/whirr/service/puppet/Manifest.java b/services/puppet/src/main/java/org/apache/whirr/service/puppet/Manifest.java index ba77b0c..a41ef95 100644 --- a/services/puppet/src/main/java/org/apache/whirr/service/puppet/Manifest.java +++ b/services/puppet/src/main/java/org/apache/whirr/service/puppet/Manifest.java @@ -117,12 +117,25 @@ public class Manifest { return resource.toString(); } + public String getName() { + return module + (className != null ? "::" + className : ""); + } + + public String getHiera() { + String name = getName(); + StringBuilder result = new StringBuilder(); + for (Map.Entry<String, String> entry : attribs.entrySet()) { + result.append("\n" + name + "::" + entry.getKey() + ": " + entry.getValue()); + } + return result.toString(); + } + List<String> toStringList() { List<String> result = new ArrayList<String>(); // First part of the the resource // class { 'module::className': - result.add("class { '" + module + (className != null ? "::" + className : "") + "':"); + result.add("class { '" + getName() + "':"); // If we have attribs they go in as key => value pairs // These go in _unquoted_; user is responsible for supplying the quotes http://git-wip-us.apache.org/repos/asf/whirr/blob/8bc196a8/services/puppet/src/main/java/org/apache/whirr/service/puppet/PuppetConstants.java ---------------------------------------------------------------------- diff --git a/services/puppet/src/main/java/org/apache/whirr/service/puppet/PuppetConstants.java b/services/puppet/src/main/java/org/apache/whirr/service/puppet/PuppetConstants.java index 4acd385..75adfa6 100644 --- a/services/puppet/src/main/java/org/apache/whirr/service/puppet/PuppetConstants.java +++ b/services/puppet/src/main/java/org/apache/whirr/service/puppet/PuppetConstants.java @@ -26,12 +26,15 @@ import java.util.regex.Pattern; public class PuppetConstants { public static final String PUPPET = "puppet"; public static final String PUPPET_ORIGIN = PUPPET + ".repourl"; + public static final String PUPPET_HIERA_CLASSES = PUPPET + ".hiera_classes"; public static final String PUPPET_ROLE_PREFIX = PUPPET + ":"; public static final String MODULE_SOURCE_SUBKEY = "module"; public static final String MODULES_DIR = "/etc/puppet/modules/"; public static final String SITE_PP_FILE_LOCATION = "/etc/puppet/manifests/site.pp"; public static final String CONF_PP_FILE_LOCATION = "/etc/puppet/manifests/extdata/common.csv"; + public static final String HIERA_COMMON_FILE_LOCATION = "/etc/puppet/hieradata/common.yaml"; + public static final String HIERA_CONF_FILE_LOCATION = "/etc/puppet/hiera.yaml"; public static final Pattern MODULE_KEY_PATTERN = Pattern.compile("^" + PUPPET + "\\.([^.]+)\\." + MODULE_SOURCE_SUBKEY + "$"); http://git-wip-us.apache.org/repos/asf/whirr/blob/8bc196a8/services/puppet/src/main/java/org/apache/whirr/service/puppet/statements/CreateSitePpAndApplyRoles.java ---------------------------------------------------------------------- diff --git a/services/puppet/src/main/java/org/apache/whirr/service/puppet/statements/CreateSitePpAndApplyRoles.java b/services/puppet/src/main/java/org/apache/whirr/service/puppet/statements/CreateSitePpAndApplyRoles.java index 6b65af1..5d95844 100644 --- a/services/puppet/src/main/java/org/apache/whirr/service/puppet/statements/CreateSitePpAndApplyRoles.java +++ b/services/puppet/src/main/java/org/apache/whirr/service/puppet/statements/CreateSitePpAndApplyRoles.java @@ -23,7 +23,10 @@ import static com.google.common.base.Preconditions.checkNotNull; import static org.apache.whirr.service.puppet.PuppetConstants.PUPPET; import static org.apache.whirr.service.puppet.PuppetConstants.SITE_PP_FILE_LOCATION; import static org.apache.whirr.service.puppet.PuppetConstants.CONF_PP_FILE_LOCATION; -import static org.jclouds.scriptbuilder.domain.Statements.appendFile; +import static org.apache.whirr.service.puppet.PuppetConstants.HIERA_COMMON_FILE_LOCATION; +import static org.apache.whirr.service.puppet.PuppetConstants.HIERA_CONF_FILE_LOCATION; +import static org.apache.whirr.service.puppet.PuppetConstants.PUPPET_HIERA_CLASSES; +import static org.jclouds.scriptbuilder.domain.Statements.createOrOverwriteFile; import static org.jclouds.scriptbuilder.domain.Statements.exec; import java.util.Collection; @@ -68,6 +71,7 @@ public class CreateSitePpAndApplyRoles implements Statement { @Override public String render(OsFamily arg0) { + Boolean isHiera = config.getBoolean(PUPPET_HIERA_CLASSES, false); // when we get to the last role, let's cat all the manifests we made together inside a // node default site.pp @@ -75,6 +79,7 @@ public class CreateSitePpAndApplyRoles implements Statement { statements.add(Statements.rm(SITE_PP_FILE_LOCATION)); statements.add(Statements.rm(CONF_PP_FILE_LOCATION)); + statements.add(Statements.rm(HIERA_COMMON_FILE_LOCATION)); Builder<String> sitePp = ImmutableList.<String> builder(); Map<String, Set<String>> puppetRoles = Maps.newHashMap(); @@ -96,6 +101,11 @@ public class CreateSitePpAndApplyRoles implements Statement { confPp.add(puppetClass + "," + Joiner.on(',').join(puppetRoles.get(puppetClass))); } + Builder<String> confHiera = ImmutableList.<String> builder(); + for (String puppetClass : puppetRoles.keySet()) { + confHiera.add(puppetClass + ":\n - " + Joiner.on("\n - ").join(puppetRoles.get(puppetClass))); + } + sitePp.add("$extlookup_datadir='/etc/puppet/manifests/extdata'"); sitePp.add("$extlookup_precedence = ['common']"); sitePp.add("node default {"); @@ -107,12 +117,31 @@ public class CreateSitePpAndApplyRoles implements Statement { String key = it.next(); manifestProps.setProperty(key, config.getProperty(key)); } - sitePp.add(getManifestForClusterSpecAndRole(role, manifestProps).toString()); + Manifest roleManifest = getManifestForClusterSpecAndRole(role, manifestProps); + if (isHiera) { + sitePp.add("include " + roleManifest.getName()); + confHiera.add(roleManifest.getHiera()); + } else { + sitePp.add(roleManifest.toString()); + } } sitePp.add("}"); - statements.add(appendFile(CONF_PP_FILE_LOCATION, confPp.build())); - statements.add(appendFile(SITE_PP_FILE_LOCATION, sitePp.build())); + if (isHiera) { + Builder<String> confPuppetHiera = ImmutableList.<String> builder(); + confPuppetHiera.add("---", + ":backends:", + " - yaml", + ":yaml:", + " :datadir: /etc/puppet/hieradata", + ":hierarchy:", + " - common"); + statements.add(createOrOverwriteFile(HIERA_CONF_FILE_LOCATION, confPuppetHiera.build())); + statements.add(exec("mkdir -p /etc/puppet/hieradata")); + } + statements.add(createOrOverwriteFile(HIERA_COMMON_FILE_LOCATION, confHiera.build())); + statements.add(createOrOverwriteFile(CONF_PP_FILE_LOCATION, confPp.build())); + statements.add(createOrOverwriteFile(SITE_PP_FILE_LOCATION, sitePp.build())); statements.add(exec("puppet apply " + SITE_PP_FILE_LOCATION)); return new StatementList(statements.build()).render(arg0); http://git-wip-us.apache.org/repos/asf/whirr/blob/8bc196a8/services/puppet/src/test/java/org/apache/whirr/service/puppet/ManifestTest.java ---------------------------------------------------------------------- diff --git a/services/puppet/src/test/java/org/apache/whirr/service/puppet/ManifestTest.java b/services/puppet/src/test/java/org/apache/whirr/service/puppet/ManifestTest.java index d5ae418..41ffea3 100644 --- a/services/puppet/src/test/java/org/apache/whirr/service/puppet/ManifestTest.java +++ b/services/puppet/src/test/java/org/apache/whirr/service/puppet/ManifestTest.java @@ -27,6 +27,8 @@ public class ManifestTest { private static final String NGINX_PUPPET = "class { 'nginx':\n}"; private static final String POSTGRES_PUPPET = "class { 'postgresql::server':\n}"; private static final String NTP_PUPPET = "class { 'ntp':\n servers => \"10.0.0.1\",\n}"; + private static final String NTP_PUPPET_HIERA = "\nntp::servers: \"10.0.0.1\""; + private static final String POSTGRES_PUPPET_NAME = "postgresql::server"; // private static final String PUPPET_COMMAND = // "echo 'class { 'ntp::client':\n servers => [\"10.0.0.1\"],\n}' >> /tmp/test.pp\nsudo -i puppet apply /tmp/test.pp\n"; @@ -60,5 +62,24 @@ public class ManifestTest { ntp.toString()); } + + @Test + public void testPuppetConversionWithAttribsHiera() { + + Manifest ntp = new Manifest("ntp"); + ntp.attribs.put("servers", "\"10.0.0.1\""); + + assertEquals("Puppet/Hiera representation is incorrect.", NTP_PUPPET_HIERA, + ntp.getHiera()); + } + + @Test + public void testPuppetConversionHiera() { + + Manifest postgress = new Manifest("postgresql", "server"); + + assertEquals("Puppet/Name representation is incorrect.", POSTGRES_PUPPET_NAME, + postgress.getName()); + } }