You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2014/11/23 23:00:49 UTC

[08/50] [abbrv] jclouds git commit: JCLOUDS-286: Use by default the Omnibus installer

JCLOUDS-286: Use by default the Omnibus installer


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/373f9ead
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/373f9ead
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/373f9ead

Branch: refs/heads/1.8.x
Commit: 373f9ead72a20f5f0f330b4a1c1bfb95a2be3068
Parents: c9b5656
Author: Ignasi Barrera <na...@apache.org>
Authored: Tue Sep 17 14:43:16 2013 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Thu Sep 19 23:45:06 2013 +0200

----------------------------------------------------------------------
 .../java/org/jclouds/chef/ChefApiMetadata.java  |  4 +-
 .../chef/config/ChefBootstrapModule.java        | 28 +++++-
 .../org/jclouds/chef/config/ChefProperties.java | 15 +++-
 .../org/jclouds/chef/config/InstallChef.java    | 39 ++++++++
 .../chef/functions/GroupToBootScript.java       | 10 ++-
 .../org/jclouds/chef/ChefApiExpectTest.java     |  2 +-
 .../chef/functions/GroupToBootScriptTest.java   | 93 +++++++++++++++++---
 7 files changed, 167 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/main/java/org/jclouds/chef/ChefApiMetadata.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/main/java/org/jclouds/chef/ChefApiMetadata.java b/apis/chef/src/main/java/org/jclouds/chef/ChefApiMetadata.java
index e28b481..951d2d2 100644
--- a/apis/chef/src/main/java/org/jclouds/chef/ChefApiMetadata.java
+++ b/apis/chef/src/main/java/org/jclouds/chef/ChefApiMetadata.java
@@ -23,6 +23,7 @@ import static org.jclouds.Constants.PROPERTY_TIMEOUTS_PREFIX;
 import static org.jclouds.chef.config.ChefProperties.CHEF_BOOTSTRAP_DATABAG;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEMS;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEM_SYSTEM;
+import static org.jclouds.chef.config.ChefProperties.CHEF_USE_OMNIBUS;
 
 import java.net.URI;
 import java.util.Properties;
@@ -44,7 +45,7 @@ import com.google.inject.Module;
  * @author Ignasi Barrera
  */
 public class ChefApiMetadata extends BaseHttpApiMetadata<ChefApi> {
-   
+
    /**
     * The default Chef Server API version to use.
     */
@@ -78,6 +79,7 @@ public class ChefApiMetadata extends BaseHttpApiMetadata<ChefApi> {
       properties.setProperty(CHEF_BOOTSTRAP_DATABAG, "bootstrap");
       properties.setProperty(CHEF_UPDATE_GEM_SYSTEM, "false");
       properties.setProperty(CHEF_UPDATE_GEMS, "false");
+      properties.setProperty(CHEF_USE_OMNIBUS, "true");
       return properties;
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/main/java/org/jclouds/chef/config/ChefBootstrapModule.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/main/java/org/jclouds/chef/config/ChefBootstrapModule.java b/apis/chef/src/main/java/org/jclouds/chef/config/ChefBootstrapModule.java
index 3d9d04f..65d1422 100644
--- a/apis/chef/src/main/java/org/jclouds/chef/config/ChefBootstrapModule.java
+++ b/apis/chef/src/main/java/org/jclouds/chef/config/ChefBootstrapModule.java
@@ -19,6 +19,7 @@ package org.jclouds.chef.config;
 import static org.jclouds.chef.config.ChefProperties.CHEF_GEM_SYSTEM_VERSION;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEMS;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEM_SYSTEM;
+import static org.jclouds.chef.config.ChefProperties.CHEF_USE_OMNIBUS;
 import static org.jclouds.chef.config.ChefProperties.CHEF_VERSION;
 
 import javax.inject.Named;
@@ -27,6 +28,7 @@ import javax.inject.Singleton;
 import org.jclouds.scriptbuilder.domain.Statement;
 import org.jclouds.scriptbuilder.domain.StatementList;
 import org.jclouds.scriptbuilder.statements.chef.InstallChefGems;
+import org.jclouds.scriptbuilder.statements.chef.InstallChefUsingOmnibus;
 import org.jclouds.scriptbuilder.statements.ruby.InstallRuby;
 import org.jclouds.scriptbuilder.statements.ruby.InstallRubyGems;
 
@@ -45,8 +47,7 @@ public class ChefBootstrapModule extends AbstractModule {
    @Provides
    @Named("installChefGems")
    @Singleton
-   Statement installChef(BootstrapProperties bootstrapProperties) {
-
+   Statement installChefGems(BootstrapProperties bootstrapProperties) {
       InstallRubyGems installRubyGems = InstallRubyGems.builder()
             .version(bootstrapProperties.gemSystemVersion().orNull())
             .updateSystem(bootstrapProperties.updateGemSystem(), bootstrapProperties.gemSystemVersion().orNull())
@@ -58,6 +59,21 @@ public class ChefBootstrapModule extends AbstractModule {
       return new StatementList(InstallRuby.builder().build(), installRubyGems, installChef);
    }
 
+   @Provides
+   @Named("installChefOmnibus")
+   @Singleton
+   Statement installChefUsingOmnibus() {
+      return new InstallChefUsingOmnibus();
+   }
+
+   @Provides
+   @InstallChef
+   @Singleton
+   Statement installChef(BootstrapProperties bootstrapProperties, @Named("installChefGems") Statement installChefGems,
+         @Named("installChefOmnibus") Statement installChefOmnibus) {
+      return bootstrapProperties.useOmnibus() ? installChefOmnibus : installChefGems;
+   }
+
    @Singleton
    private static class BootstrapProperties {
       @Named(CHEF_VERSION)
@@ -76,6 +92,10 @@ public class ChefBootstrapModule extends AbstractModule {
       @Inject
       private String updateGemsProperty;
 
+      @Named(CHEF_USE_OMNIBUS)
+      @Inject
+      private String useOmnibus;
+
       public Optional<String> chefVersion() {
          return Optional.fromNullable(chefVersionProperty);
       }
@@ -91,6 +111,10 @@ public class ChefBootstrapModule extends AbstractModule {
       public boolean updateGems() {
          return Boolean.parseBoolean(updateGemsProperty);
       }
+
+      public boolean useOmnibus() {
+         return Boolean.parseBoolean(useOmnibus);
+      }
    }
 
    @Override

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/main/java/org/jclouds/chef/config/ChefProperties.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/main/java/org/jclouds/chef/config/ChefProperties.java b/apis/chef/src/main/java/org/jclouds/chef/config/ChefProperties.java
index 75b3aa8..a6cf5f2 100644
--- a/apis/chef/src/main/java/org/jclouds/chef/config/ChefProperties.java
+++ b/apis/chef/src/main/java/org/jclouds/chef/config/ChefProperties.java
@@ -31,7 +31,7 @@ public interface ChefProperties {
    public static final String CHEF_LOGGER = "jclouds.chef";
 
    /**
-    * Ddatabag that holds chef bootstrap hints, should be a json ball in the
+    * Databag that holds chef bootstrap hints, should be a json ball in the
     * following format:
     * <p>
     * {"tag":{"run_list":["recipe[apache2]"]}}
@@ -91,7 +91,7 @@ public interface ChefProperties {
    /**
     * Boolean property. Default (false).
     * <p>
-    * When bootstrapping a node, updates teh existing gems before installing
+    * When bootstrapping a node, updates the existing gems before installing
     * Chef.
     * <p>
     * This property must be set prior to running the
@@ -99,4 +99,15 @@ public interface ChefProperties {
     */
    public static final String CHEF_UPDATE_GEMS = "chef.update-gems";
 
+   /**
+    * Boolean property. Default (true).
+    * <p>
+    * When bootstrapping a node, install the Chef client using the Omnibus
+    * installer.
+    * <p>
+    * This property must be set prior to running the
+    * {@link ChefService#createBootstrapScriptForGroup(String)} method.
+    */
+   public static final String CHEF_USE_OMNIBUS = "chef.use-omnibus";
+
 }

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/main/java/org/jclouds/chef/config/InstallChef.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/main/java/org/jclouds/chef/config/InstallChef.java b/apis/chef/src/main/java/org/jclouds/chef/config/InstallChef.java
new file mode 100644
index 0000000..909b99a
--- /dev/null
+++ b/apis/chef/src/main/java/org/jclouds/chef/config/InstallChef.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jclouds.chef.config;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Used to configure the Chef install script.
+ * 
+ * @author Ignasi Barrera
+ */
+@Target({ METHOD, PARAMETER, FIELD })
+@Retention(RUNTIME)
+@Qualifier
+public @interface InstallChef {
+
+}

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/main/java/org/jclouds/chef/functions/GroupToBootScript.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/main/java/org/jclouds/chef/functions/GroupToBootScript.java b/apis/chef/src/main/java/org/jclouds/chef/functions/GroupToBootScript.java
index 9154471..707119b 100644
--- a/apis/chef/src/main/java/org/jclouds/chef/functions/GroupToBootScript.java
+++ b/apis/chef/src/main/java/org/jclouds/chef/functions/GroupToBootScript.java
@@ -34,11 +34,13 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import org.jclouds.chef.config.InstallChef;
 import org.jclouds.chef.config.Validator;
 import org.jclouds.crypto.Pems;
 import org.jclouds.domain.JsonBall;
 import org.jclouds.json.Json;
 import org.jclouds.location.Provider;
+import org.jclouds.scriptbuilder.ExitInsteadOfReturn;
 import org.jclouds.scriptbuilder.domain.Statement;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -68,19 +70,19 @@ public class GroupToBootScript implements Function<String, Statement> {
    private final Supplier<URI> endpoint;
    private final Json json;
    private final CacheLoader<String, ? extends JsonBall> bootstrapConfigForGroup;
-   private final Statement installChefGems;
+   private final Statement installChef;
    private final Optional<String> validatorName;
    private final Optional<PrivateKey> validatorCredential;
 
    @Inject
    public GroupToBootScript(@Provider Supplier<URI> endpoint, Json json,
          CacheLoader<String, ? extends JsonBall> bootstrapConfigForGroup,
-         @Named("installChefGems") Statement installChefGems, @Validator Optional<String> validatorName,
+         @InstallChef Statement installChef, @Validator Optional<String> validatorName,
          @Validator Optional<PrivateKey> validatorCredential) {
       this.endpoint = checkNotNull(endpoint, "endpoint");
       this.json = checkNotNull(json, "json");
       this.bootstrapConfigForGroup = checkNotNull(bootstrapConfigForGroup, "bootstrapConfigForGroup");
-      this.installChefGems = checkNotNull(installChefGems, "installChefGems");
+      this.installChef = checkNotNull(installChef, "installChef");
       this.validatorName = checkNotNull(validatorName, "validatorName");
       this.validatorCredential = checkNotNull(validatorCredential, validatorCredential);
    }
@@ -124,7 +126,7 @@ public class GroupToBootScript implements Function<String, Statement> {
       String strOptions = Joiner.on(' ').withKeyValueSeparator(" ").join(options.build());
       Statement runChef = exec("chef-client " + strOptions);
 
-      return newStatementList(installChefGems, createChefConfigDir, createClientRb, createValidationPem,
+      return newStatementList(new ExitInsteadOfReturn(installChef), createChefConfigDir, createClientRb, createValidationPem,
             createFirstBoot, runChef);
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java
index 634c5da..3a7bae4 100644
--- a/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java
+++ b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java
@@ -45,7 +45,7 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
      provider = "chef";
    }
 
-   private HttpRequest.Builder getHttpRequestBuilder(String method, String endPoint) {
+   private HttpRequest.Builder<?> getHttpRequestBuilder(String method, String endPoint) {
       return HttpRequest.builder() //
                   .method(method) //
                   .endpoint("http://localhost:4000" + endPoint) //

http://git-wip-us.apache.org/repos/asf/jclouds/blob/373f9ead/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java
----------------------------------------------------------------------
diff --git a/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java b/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java
index 7b51f04..fa9cf3f 100644
--- a/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java
+++ b/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java
@@ -22,6 +22,7 @@ import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEMS;
 import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEM_SYSTEM;
+import static org.jclouds.chef.config.ChefProperties.CHEF_USE_OMNIBUS;
 import static org.testng.Assert.assertEquals;
 
 import java.io.IOException;
@@ -31,6 +32,7 @@ import java.security.PrivateKey;
 import org.jclouds.chef.ChefApiMetadata;
 import org.jclouds.chef.config.ChefBootstrapModule;
 import org.jclouds.chef.config.ChefParserModule;
+import org.jclouds.chef.config.InstallChef;
 import org.jclouds.chef.domain.DatabagItem;
 import org.jclouds.crypto.PemsTest;
 import org.jclouds.domain.JsonBall;
@@ -64,21 +66,34 @@ public class GroupToBootScriptTest {
 
    private Json json;
    private Statement installChefGems;
+   private Statement installChefOmnibus;
    private Optional<String> validatorName;
 
    @BeforeClass
    public void setup() {
-      Injector injector = Guice.createInjector(new AbstractModule() {
+      Injector injectorGems = Guice.createInjector(new AbstractModule() {
          @Override
          protected void configure() {
             bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
             bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEM_SYSTEM)).toInstance("true");
             bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEMS)).toInstance("true");
+            bind(String.class).annotatedWith(Names.named(CHEF_USE_OMNIBUS)).toInstance("false");
          }
       }, new ChefParserModule(), new GsonModule(), new ChefBootstrapModule());
 
-      json = injector.getInstance(Json.class);
-      installChefGems = injector.getInstance(Key.get(Statement.class, Names.named("installChefGems")));
+      Injector injectorOmnibus = Guice.createInjector(new AbstractModule() {
+         @Override
+         protected void configure() {
+            bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
+            bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEM_SYSTEM)).toInstance("true");
+            bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEMS)).toInstance("true");
+            bind(String.class).annotatedWith(Names.named(CHEF_USE_OMNIBUS)).toInstance("true");
+         }
+      }, new ChefParserModule(), new GsonModule(), new ChefBootstrapModule());
+
+      json = injectorGems.getInstance(Json.class);
+      installChefGems = injectorGems.getInstance(Key.get(Statement.class, InstallChef.class));
+      installChefOmnibus = injectorOmnibus.getInstance(Key.get(Statement.class, InstallChef.class));
       validatorName = Optional.<String> of("chef-validator");
    }
 
@@ -130,13 +145,15 @@ public class GroupToBootScriptTest {
 
       assertEquals(
             fn.apply("foo").render(OsFamily.UNIX),
-            Resources.toString(Resources.getResource("test_install_ruby." + ShellToken.SH.to(OsFamily.UNIX)),
-                  Charsets.UTF_8)
-                  + Resources.toString(
-                        Resources.getResource("test_install_rubygems." + ShellToken.SH.to(OsFamily.UNIX)),
+            exitInsteadOfReturn(
+                  OsFamily.UNIX,
+                  Resources.toString(Resources.getResource("test_install_ruby." + ShellToken.SH.to(OsFamily.UNIX)),
                         Charsets.UTF_8)
-                  + "gem install chef --no-rdoc --no-ri\n"
-                  + Resources.toString(Resources.getResource("bootstrap.sh"), Charsets.UTF_8));
+                        + Resources.toString(
+                              Resources.getResource("test_install_rubygems." + ShellToken.SH.to(OsFamily.UNIX)),
+                              Charsets.UTF_8)
+                        + "gem install chef --no-rdoc --no-ri\n"
+                        + Resources.toString(Resources.getResource("bootstrap.sh"), Charsets.UTF_8)));
 
       verify(validatorKey);
    }
@@ -155,14 +172,62 @@ public class GroupToBootScriptTest {
 
       assertEquals(
             fn.apply("foo").render(OsFamily.UNIX),
-            Resources.toString(Resources.getResource("test_install_ruby." + ShellToken.SH.to(OsFamily.UNIX)),
-                  Charsets.UTF_8)
-                  + Resources.toString(
-                        Resources.getResource("test_install_rubygems." + ShellToken.SH.to(OsFamily.UNIX)),
+            exitInsteadOfReturn(
+                  OsFamily.UNIX,
+                  Resources.toString(Resources.getResource("test_install_ruby." + ShellToken.SH.to(OsFamily.UNIX)),
                         Charsets.UTF_8)
-                  + "gem install chef --no-rdoc --no-ri\n"
+                        + Resources.toString(
+                              Resources.getResource("test_install_rubygems." + ShellToken.SH.to(OsFamily.UNIX)),
+                              Charsets.UTF_8)
+                        + "gem install chef --no-rdoc --no-ri\n"
+                        + Resources.toString(Resources.getResource("bootstrap-env.sh"), Charsets.UTF_8)));
+
+      verify(validatorKey);
+   }
+
+   public void testOneRecipeOmnibus() throws IOException {
+      Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
+      GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")), json,
+            CacheLoader.from(Functions.forMap(ImmutableMap.<String, JsonBall> of("foo", new JsonBall(
+                  "{\"tomcat6\":{\"ssl_port\":8433},\"run_list\":[\"recipe[apache2]\",\"role[webserver]\"]}")))),
+            installChefOmnibus, validatorName, validatorCredential);
+
+      PrivateKey validatorKey = validatorCredential.get();
+      expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
+      replay(validatorKey);
+
+      assertEquals(
+            fn.apply("foo").render(OsFamily.UNIX),
+            "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
+                  + "-X GET  https://www.opscode.com/chef/install.sh |(bash)\n"
+                  + Resources.toString(Resources.getResource("bootstrap.sh"), Charsets.UTF_8));
+
+      verify(validatorKey);
+   }
+
+   public void testOneRecipeAndEnvironmentOmnibus() throws IOException {
+      Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
+      GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")), json,
+            CacheLoader.from(Functions.forMap(ImmutableMap.<String, JsonBall> of("foo", new JsonBall(
+                  "{\"tomcat6\":{\"ssl_port\":8433},\"environment\":\"env\","
+                        + "\"run_list\":[\"recipe[apache2]\",\"role[webserver]\"]}")))), installChefOmnibus,
+            validatorName, validatorCredential);
+
+      PrivateKey validatorKey = validatorCredential.get();
+      expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
+      replay(validatorKey);
+
+      assertEquals(
+            fn.apply("foo").render(OsFamily.UNIX),
+            "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
+                  + "-X GET  https://www.opscode.com/chef/install.sh |(bash)\n"
                   + Resources.toString(Resources.getResource("bootstrap-env.sh"), Charsets.UTF_8));
 
       verify(validatorKey);
    }
+
+   private static String exitInsteadOfReturn(OsFamily family, String input) {
+      return input.replaceAll(ShellToken.RETURN.to(family), ShellToken.EXIT.to(family));
+   }
+
 }