You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by an...@apache.org on 2015/02/03 12:41:45 UTC

[14/17] incubator-brooklyn git commit: Support scriptDir location config

Support scriptDir location config

This modifies SshMachineLocation to propagate certain config keys to
flags on SshTool execCommand and execScript invocations; the first use
of this is to propagate the "scriptDir" config. JcloudsLocation is also
modified to propagate this configuration. The result is that "scriptDir"
can be specified in the configuration for a jclouds-based location, and
the value of this property will be passed to every invocation of
SshTool.execScript().

The use case for this is to allow Brooklyn to be usable on locations
where the VM image mounts "/tmp" with "noexec".

Also adds some integration tests to verify the correct operation of the
scriptDir flag.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/fc41bb9f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/fc41bb9f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/fc41bb9f

Branch: refs/heads/master
Commit: fc41bb9f516aae2e232a634e8c3524e4649d55a6
Parents: bd9b292
Author: Richard Downer <ri...@apache.org>
Authored: Tue Jan 27 16:06:07 2015 +0000
Committer: Andrea Turli <an...@gmail.com>
Committed: Tue Feb 3 11:25:07 2015 +0100

----------------------------------------------------------------------
 .../location/basic/SshMachineLocation.java      | 24 +++++++++++--
 .../SshMachineLocationIntegrationTest.java      | 38 +++++++++++++++++++-
 .../ssh/SshToolAbstractIntegrationTest.java     | 13 +++++++
 .../location/jclouds/JcloudsLocation.java       |  1 +
 4 files changed, 73 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/fc41bb9f/core/src/main/java/brooklyn/location/basic/SshMachineLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/SshMachineLocation.java b/core/src/main/java/brooklyn/location/basic/SshMachineLocation.java
index 4a165f2..9482d02 100644
--- a/core/src/main/java/brooklyn/location/basic/SshMachineLocation.java
+++ b/core/src/main/java/brooklyn/location/basic/SshMachineLocation.java
@@ -19,6 +19,8 @@
 package brooklyn.location.basic;
 
 import static brooklyn.util.GroovyJavaMethods.truth;
+
+import com.google.common.annotations.Beta;
 import groovy.lang.Closure;
 
 import java.io.Closeable;
@@ -110,6 +112,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.net.HostAndPort;
 
@@ -216,6 +219,14 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat
                 }
             }));
 
+    /**
+     * The set of config keys on this location which become default values for properties when invoking an SSH
+     * operation.
+     */
+    @Beta
+    public static final Set<ConfigKey<?>> SSH_CONFIG_GIVEN_TO_PROPS = ImmutableSet.<ConfigKey<?>>of(
+            SCRIPT_DIR);
+
     private Task<?> cleanupTask;
     /** callers should use {@link #getSshPoolCache()} */
     @Nullable 
@@ -610,7 +621,7 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat
         return execCommands(MutableMap.<String,Object>of(), summaryForLogging, commands, env);
     }
     public int execCommands(Map<String,?> props, String summaryForLogging, List<String> commands, Map<String,?> env) {
-        return newExecWithLoggingHelpers().execCommands(props, summaryForLogging, commands, env);
+        return newExecWithLoggingHelpers().execCommands(augmentPropertiesWithSshConfigGivenToProps(props), summaryForLogging, commands, env);
     }
 
     /**
@@ -630,7 +641,16 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat
         return execScript(MutableMap.<String,Object>of(), summaryForLogging, commands, env);
     }
     public int execScript(Map<String,?> props, String summaryForLogging, List<String> commands, Map<String,?> env) {
-        return newExecWithLoggingHelpers().execScript(props, summaryForLogging, commands, env);
+        return newExecWithLoggingHelpers().execScript(augmentPropertiesWithSshConfigGivenToProps(props), summaryForLogging, commands, env);
+    }
+
+    private Map<String, Object> augmentPropertiesWithSshConfigGivenToProps(Map<String, ?> props) {
+        Map<String,Object> augmentedProps = Maps.newHashMap(props);
+        for (ConfigKey<?> config : SSH_CONFIG_GIVEN_TO_PROPS) {
+            if (!props.containsKey(config.getName()))
+                augmentedProps.put(config.getName(), getConfig(config));
+        }
+        return augmentedProps;
     }
 
     protected ExecWithLoggingHelpers newExecWithLoggingHelpers() {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/fc41bb9f/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java b/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
index 5fbe030..63f5902 100644
--- a/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
+++ b/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
@@ -21,7 +21,11 @@ package brooklyn.location.basic;
 import java.io.ByteArrayOutputStream;
 import java.security.KeyPair;
 import java.util.Arrays;
+import java.util.Map;
 
+import brooklyn.util.internal.ssh.SshTool;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
@@ -38,6 +42,8 @@ import brooklyn.util.internal.ssh.sshj.SshjTool.SshjToolBuilder;
 
 import com.google.common.base.Preconditions;
 
+import static org.testng.Assert.assertEquals;
+
 public class SshMachineLocationIntegrationTest {
 
     protected TestApplication app;
@@ -75,7 +81,37 @@ public class SshMachineLocationIntegrationTest {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         int result = tool.execCommands(MutableMap.<String,Object>of("out", out), Arrays.asList("date"));
         Assert.assertTrue(out.toString().contains(" 20"), "out="+out);
-        Assert.assertEquals(result, 0);
+        assertEquals(result, 0);
+    }
+
+    @Test(groups = "Integration")
+    public void testExecScriptScriptDirFlagIsRespected() throws Exception {
+        // For explanation of (some of) the magic behind this command, see http://stackoverflow.com/a/229606/68898
+        final String command = "if [[ \"$0\" == \"/var/tmp/\"* ]]; then true; else false; fi";
+
+        LocalhostMachineProvisioningLocation lhp = (LocalhostMachineProvisioningLocation) mgmt.getLocationRegistry().resolve("localhost", true, null).orNull();
+        SshMachineLocation sm = lhp.obtain();
+
+        Map<String, Object> props = ImmutableMap.<String, Object>builder()
+                .put(SshTool.PROP_SCRIPT_DIR.getName(), "/var/tmp")
+                .build();
+        int rc = sm.execScript(props, "Test script directory execution", ImmutableList.of(command));
+        assertEquals(rc, 0);
     }
 
+    @Test(groups = "Integration")
+    public void testLocationScriptDirConfigIsRespected() throws Exception {
+        // For explanation of (some of) the magic behind this command, see http://stackoverflow.com/a/229606/68898
+        final String command = "if [[ \"$0\" == \"/var/tmp/\"* ]]; then true; else false; fi";
+
+        Map<String, Object> locationConfig = ImmutableMap.<String, Object>builder()
+                .put(SshMachineLocation.SCRIPT_DIR.getName(), "/var/tmp")
+                .build();
+
+        LocalhostMachineProvisioningLocation lhp = (LocalhostMachineProvisioningLocation) mgmt.getLocationRegistry().resolve("localhost", locationConfig);
+        SshMachineLocation sm = lhp.obtain();
+
+        int rc = sm.execScript("Test script directory execution", ImmutableList.of(command));
+        assertEquals(rc, 0);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/fc41bb9f/core/src/test/java/brooklyn/util/internal/ssh/SshToolAbstractIntegrationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/util/internal/ssh/SshToolAbstractIntegrationTest.java b/core/src/test/java/brooklyn/util/internal/ssh/SshToolAbstractIntegrationTest.java
index 1501585..84b1029 100644
--- a/core/src/test/java/brooklyn/util/internal/ssh/SshToolAbstractIntegrationTest.java
+++ b/core/src/test/java/brooklyn/util/internal/ssh/SshToolAbstractIntegrationTest.java
@@ -285,4 +285,17 @@ public abstract class SshToolAbstractIntegrationTest extends ShellToolAbstractTe
         Assert.assertEquals(CONTENTS.trim(), contents.trim());
     }
 
+    @Test(groups = {"Integration"})
+    public void testScriptDirPropertiesIsRespected() {
+        // For explanation of (some of) the magic behind this command, see http://stackoverflow.com/a/229606/68898
+        final String command = "if [[ \"$0\" == \"/var/tmp/\"* ]]; then true; else false; fi";
+
+        SshTool sshTool = newTool(ImmutableMap.<String, Object>builder()
+                .put(SshTool.PROP_HOST.getName(), "localhost")
+                .build());
+        int rc = sshTool.execScript(ImmutableMap.<String, Object>builder()
+                .put(SshTool.PROP_SCRIPT_DIR.getName(), "/var/tmp")
+                .build(), ImmutableList.of(command));
+        assertEquals(rc, 0);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/fc41bb9f/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
index b10973d..7abbaca 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
@@ -1694,6 +1694,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im
                     .configureIfNotNull(CLOUD_REGION_ID, nodeRegion)
                     .configure(CALLER_CONTEXT, setup.get(CALLER_CONTEXT))
                     .configure(SshMachineLocation.DETECT_MACHINE_DETAILS, setup.get(SshMachineLocation.DETECT_MACHINE_DETAILS))
+                    .configure(SshMachineLocation.SCRIPT_DIR, setup.get(SshMachineLocation.SCRIPT_DIR))
                     .configureIfNotNull(USE_PORT_FORWARDING, setup.get(USE_PORT_FORWARDING))
                     .configureIfNotNull(PORT_FORWARDER, setup.get(PORT_FORWARDER))
                     .configureIfNotNull(PORT_FORWARDING_MANAGER, setup.get(PORT_FORWARDING_MANAGER)));