You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2015/05/22 11:05:04 UTC

[19/23] incubator-brooklyn git commit: ssh command sensor allows configuring the dir where it runs

ssh command sensor allows configuring the dir where it runs

fixes bug if ssh sensor used with empty software process entity, where run dir isn't yet created


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

Branch: refs/heads/master
Commit: 44273ad5042dc11a9d1dee07df264f7c03a88144
Parents: 776ad43
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Thu May 21 23:42:12 2015 +0100
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Thu May 21 23:50:34 2015 +0100

----------------------------------------------------------------------
 .../entity/software/ssh/SshCommandEffector.java |  6 ++--
 .../entity/software/ssh/SshCommandSensor.java   | 37 ++++++++++++++++----
 2 files changed, 35 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/44273ad5/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandEffector.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandEffector.java b/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandEffector.java
index 4cc1b5c..200d0ce 100644
--- a/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandEffector.java
+++ b/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandEffector.java
@@ -40,6 +40,7 @@ import com.google.common.base.Preconditions;
 public final class SshCommandEffector extends AddEffector {
     
     public static final ConfigKey<String> EFFECTOR_COMMAND = ConfigKeys.newStringConfigKey("command");
+    public static final ConfigKey<String> EFFECTOR_EXECUTION_DIR = SshCommandSensor.SENSOR_EXECUTION_DIR;
     
     public SshCommandEffector(ConfigBag params) {
         super(newEffectorBuilder(params).build());
@@ -59,10 +60,12 @@ public final class SshCommandEffector extends AddEffector {
     protected static class Body extends EffectorBody<String> {
         private final Effector<?> effector;
         private final String command;
+        private final String executionDir;
 
         public Body(Effector<?> eff, ConfigBag params) {
             this.effector = eff;
             this.command = Preconditions.checkNotNull(params.get(EFFECTOR_COMMAND), "command must be supplied when defining this effector");
+            this.executionDir = params.get(EFFECTOR_EXECUTION_DIR);
             // TODO could take a custom "env" aka effectorShellEnv
         }
 
@@ -70,8 +73,7 @@ public final class SshCommandEffector extends AddEffector {
         public String call(ConfigBag params) {
             String command = this.command;
             
-            String runDir = entity().getAttribute(SoftwareProcess.RUN_DIR);
-            if (runDir!=null) command = "cd '"+runDir+"'\n"+command;
+            command = SshCommandSensor.makeCommandExecutingInDirectory(command, executionDir, entity());
             
             MutableMap<String, String> env = MutableMap.of();
             // first set all declared parameters, including default values

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/44273ad5/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandSensor.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandSensor.java b/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandSensor.java
index 51e2917..2c918f9 100644
--- a/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandSensor.java
+++ b/software/base/src/main/java/brooklyn/entity/software/ssh/SshCommandSensor.java
@@ -37,6 +37,7 @@ import brooklyn.event.feed.ssh.SshValueFunctions;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
 import brooklyn.util.flags.TypeCoercions;
+import brooklyn.util.os.Os;
 import brooklyn.util.text.Strings;
 
 import com.google.common.annotations.Beta;
@@ -59,14 +60,20 @@ public final class SshCommandSensor<T> extends AddSensor<T> {
     private static final Logger LOG = LoggerFactory.getLogger(SshCommandSensor.class);
 
     public static final ConfigKey<String> SENSOR_COMMAND = ConfigKeys.newStringConfigKey("command", "SSH command to execute for sensor");
+    public static final ConfigKey<String> SENSOR_EXECUTION_DIR = ConfigKeys.newStringConfigKey("executionDir", "Directory where the command should run; "
+        + "if not supplied, executes in the entity's run dir (or home dir if no run dir is defined); "
+        + "use '~' to always execute in the home dir, or 'custom-feed/' to execute in a custom-feed dir relative to the run dir");
 
     protected final String command;
+    protected final String executionDir;
 
     public SshCommandSensor(final ConfigBag params) {
         super(params);
 
         // TODO create a supplier for the command string to support attribute embedding
         command = Preconditions.checkNotNull(params.get(SENSOR_COMMAND), "command");
+        
+        executionDir = params.get(SENSOR_EXECUTION_DIR);
     }
 
     @Override
@@ -87,12 +94,7 @@ public final class SshCommandSensor<T> extends AddSensor<T> {
         Supplier<String> commandSupplier = new Supplier<String>() {
             @Override
             public String get() {
-                String finalCommand = command;
-                String runDir = entity.getAttribute(SoftwareProcess.RUN_DIR);
-                if (runDir != null) {
-                    finalCommand = "cd '"+runDir+"' && "+finalCommand;
-                }
-                return finalCommand;
+                return makeCommandExecutingInDirectory(command, executionDir, entity);
             }
         };
 
@@ -115,4 +117,27 @@ public final class SshCommandSensor<T> extends AddSensor<T> {
                 .build();
     }
 
+    static String makeCommandExecutingInDirectory(String command, String executionDir, EntityLocal entity) {
+        String finalCommand = command;
+        String execDir = executionDir;
+        if (Strings.isBlank(execDir)) {
+            // default to run dir
+            execDir = entity.getAttribute(SoftwareProcess.RUN_DIR);
+            // if no run dir, default to home
+            if (Strings.isBlank(execDir)) {
+                execDir = "~";
+            }
+        } else if (!Os.isAbsolutish(execDir)) {
+            // relative paths taken wrt run dir
+            String runDir = entity.getAttribute(SoftwareProcess.RUN_DIR);
+            if (!Strings.isBlank(runDir)) {
+                execDir = Os.mergePaths(runDir, execDir);
+            }
+        }
+        if (!"~".equals(execDir)) {
+            finalCommand = "mkdir -p '"+execDir+"' && cd '"+execDir+"' && "+finalCommand;
+        }
+        return finalCommand;
+    }
+
 }