You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2018/01/18 09:02:48 UTC

[1/7] brooklyn-server git commit: WinRM: hold mutex during install commands

Repository: brooklyn-server
Updated Branches:
  refs/heads/master 469d192db -> 1082d1af5


WinRM: hold mutex during install commands

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

Branch: refs/heads/master
Commit: f3809df4f4cae32b3d243804ff3eca3abdcbcc2a
Parents: 49bd6c7
Author: Aled Sage <al...@gmail.com>
Authored: Wed Jan 17 09:21:09 2018 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Jan 17 09:21:09 2018 +0000

----------------------------------------------------------------------
 .../AbstractSoftwareProcessWinRmDriver.java     | 51 ++++++++-----
 .../base/VanillaWindowsProcessWinRmDriver.java  | 12 +--
 .../base/lifecycle/WinRmExecuteHelper.java      | 62 ++++++++++++---
 .../base/VanillaWindowsProcessTest.java         | 80 ++++++++++++++++++++
 .../core/internal/winrm/RecordingWinRmTool.java | 11 ++-
 5 files changed, 179 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f3809df4/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
index 9b625a3..014af20 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
@@ -29,13 +29,7 @@ import java.util.Map;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.cxf.interceptor.Fault;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
+import javax.xml.ws.WebServiceException;
 
 import org.apache.brooklyn.api.entity.EntityLocal;
 import org.apache.brooklyn.api.mgmt.Task;
@@ -49,6 +43,7 @@ import org.apache.brooklyn.entity.software.base.lifecycle.WinRmExecuteHelper;
 import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmTool;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse;
+import org.apache.brooklyn.util.core.mutex.MutexSupport;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.exceptions.ReferenceWithError;
@@ -56,8 +51,14 @@ import org.apache.brooklyn.util.repeat.Repeater;
 import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
+import org.apache.cxf.interceptor.Fault;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import javax.xml.ws.WebServiceException;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 
 public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwareProcessDriver implements NativeWindowsScriptRunner {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractSoftwareProcessWinRmDriver.class);
@@ -77,6 +78,19 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     protected WinRmExecuteHelper newScript(String phase) {
         return newScript(Maps.<String, Object>newLinkedHashMap(), phase);
     }
+    
+    protected WinRmExecuteHelper newScript(String command, String psCommand, String phase) {
+        return newScript(command, psCommand, phase, null);
+    }
+
+    protected WinRmExecuteHelper newScript(String command, String psCommand, String phase, String ntDomain) {
+        return newScript(phase)
+                .setNtDomain(ntDomain)
+                .setCommand(command)
+                .setPsCommand(psCommand)
+                .failOnNonZeroResultCode()
+                .gatherOutput();
+    }
 
     protected WinRmExecuteHelper newScript(Map<String, ?> flags, String phase) {
         if (!Entities.isManaged(getEntity()))
@@ -89,10 +103,12 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     @Override
     public void runPreInstallCommand() {
         if (Strings.isNonBlank(getEntity().getConfig(BrooklynConfigKeys.PRE_INSTALL_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND))) {
-            executeCommandInTask(
+            newScript(
                     getEntity().getConfig(BrooklynConfigKeys.PRE_INSTALL_COMMAND),
                     getEntity().getConfig(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND),
-                    "pre-install-command");
+                    "pre-install-command")
+                .useMutex(getLocation().mutexes(), "installation lock at host", "installing "+elvis(entity,this))
+                .execute();
         }
         if (entity.getConfig(VanillaWindowsProcess.PRE_INSTALL_REBOOT_REQUIRED)) {
             rebootAndWait();
@@ -107,10 +123,12 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     @Override
     public void runPostInstallCommand() {
         if (Strings.isNonBlank(entity.getConfig(BrooklynConfigKeys.POST_INSTALL_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND))) {
-            executeCommandInTask(
+            newScript(
                     getEntity().getConfig(BrooklynConfigKeys.POST_INSTALL_COMMAND),
                     getEntity().getConfig(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND),
-                    "post-install-command");
+                    "post-install-command")
+            .useMutex(getLocation().mutexes(), "installation lock at host", "installing "+elvis(entity,this))
+            .execute();
         }
     }
 
@@ -168,13 +186,8 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     }
 
     protected int executeCommandInTask(String command, String psCommand, String phase, String ntDomain) {
-        return newScript(phase)
-                .setNtDomain(ntDomain)
-                .setCommand(command)
-                .setPsCommand(psCommand)
-                .failOnNonZeroResultCode()
-                .gatherOutput()
-                .execute();
+        WinRmExecuteHelper helper = newScript(command, psCommand, phase, ntDomain);
+        return helper.execute();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f3809df4/software/base/src/main/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessWinRmDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessWinRmDriver.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessWinRmDriver.java
index 854823a..d901242 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessWinRmDriver.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessWinRmDriver.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.entity.software.base;
 
+import static org.apache.brooklyn.util.JavaGroovyEquivalents.elvis;
+
 import org.apache.brooklyn.api.entity.EntityLocal;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.entity.software.base.lifecycle.WinRmExecuteHelper;
@@ -58,11 +60,11 @@ public class VanillaWindowsProcessWinRmDriver extends AbstractSoftwareProcessWin
 
         // TODO: Follow install path of VanillaSoftwareProcessSshDriver
         if(Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.INSTALL_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND))) {
-            executeCommandInTask(
-                    getEntity().getConfig(VanillaWindowsProcess.INSTALL_COMMAND),
-                    getEntity().getConfig(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND),
-                    "install-command",
-                    hostname);
+            String cmd = getEntity().getConfig(VanillaWindowsProcess.INSTALL_COMMAND);
+            String psCmd = getEntity().getConfig(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND);
+            newScript(cmd, psCmd, "install-command", hostname)
+                    .useMutex(getLocation().mutexes(), "installation lock at host", "installing "+elvis(entity,this))
+                    .execute();
         }
         if (entity.getConfig(VanillaWindowsProcess.INSTALL_REBOOT_REQUIRED)) {
             rebootAndWait(hostname);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f3809df4/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java
index be23ca7..5fe5824 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java
@@ -33,6 +33,7 @@ import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.mgmt.TaskQueueingContext;
 import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmTool;
+import org.apache.brooklyn.util.core.mutex.WithMutexes;
 import org.apache.brooklyn.util.core.task.DynamicTasks;
 import org.apache.brooklyn.util.core.task.TaskBuilder;
 import org.apache.brooklyn.util.core.task.Tasks;
@@ -64,6 +65,18 @@ public class WinRmExecuteHelper {
     protected Predicate<? super Integer> resultCodeCheck = Predicates.alwaysTrue();
     protected ByteArrayOutputStream stdout, stderr, stdin;
 
+    protected Runnable mutexAcquire = new Runnable() {
+        @Override
+        public void run() {
+        }
+    };
+
+    protected Runnable mutexRelease = new Runnable() {
+        @Override
+        public void run() {
+        }
+    };
+
     public WinRmExecuteHelper(NativeWindowsScriptRunner runner, String summary) {
         this.runner = runner;
         this.summary = summary;
@@ -84,6 +97,30 @@ public class WinRmExecuteHelper {
         return this;
     }
 
+    /**
+     * indicates that the script should acquire the given mutexId on the given mutexSupport
+     * and maintain it for the duration of script execution;
+     * typically used to prevent parallel scripts from conflicting in access to a resource
+     * (e.g. a folder, or a config file used by a process)
+     */
+    public WinRmExecuteHelper useMutex(final WithMutexes mutexSupport, final String mutexId, final String description) {
+        mutexAcquire = new Runnable() {
+            @Override
+            public void run() {
+                mutexSupport.acquireMutex(mutexId, description);
+            }
+        };
+
+        mutexRelease = new Runnable() {
+            @Override
+            public void run() {
+                mutexSupport.releaseMutex(mutexId);
+            }
+        };
+
+        return this;
+    }
+
     /** queues the task for execution if we are in a {@link TaskQueueingContext} (e.g. EffectorTaskFactory);
      * or if we aren't in a queueing context, it will submit the task (assuming there is an {@link ExecutionContext}
      * _and_ block until completion, throwing on error */
@@ -144,16 +181,21 @@ public class WinRmExecuteHelper {
 
     public int executeInternal() {
         int result;
-        if (gatherOutput) {
-            if (stdout==null) stdout = new ByteArrayOutputStream();
-            if (stderr==null) stderr = new ByteArrayOutputStream();
-            flags.put("out", stdout);
-            flags.put("err", stderr);
-        }
-        flags.put(WinRmTool.COMPUTER_NAME, domain);
-        result = runner.executeNativeOrPsCommand(flags, command, psCommand, summary, false);
-        if (!resultCodeCheck.apply(result)) {
-            throw logWithDetailsAndThrow(format("Execution failed, invalid result %s for %s", result, summary), null);
+        mutexAcquire.run();
+        try {
+            if (gatherOutput) {
+                if (stdout==null) stdout = new ByteArrayOutputStream();
+                if (stderr==null) stderr = new ByteArrayOutputStream();
+                flags.put("out", stdout);
+                flags.put("err", stderr);
+            }
+            flags.put(WinRmTool.COMPUTER_NAME, domain);
+            result = runner.executeNativeOrPsCommand(flags, command, psCommand, summary, false);
+            if (!resultCodeCheck.apply(result)) {
+                throw logWithDetailsAndThrow(format("Execution failed, invalid result %s for %s", result, summary), null);
+            }
+        } finally {
+            mutexRelease.run();
         }
         return result;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f3809df4/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
index 60a72ed..50f84e5 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -27,12 +27,17 @@ import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.location.LocationSpec;
 import org.apache.brooklyn.api.location.MachineLocation;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation;
 import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool;
+import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomResponse;
+import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomResponseGenerator;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams;
+import org.apache.brooklyn.util.core.mutex.WithMutexes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.AfterMethod;
@@ -124,6 +129,81 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
     }
     
+    @Test
+    public void testMutexForInstallSteps() throws Exception {
+        RecordingWithMutexes mutexSupport = new RecordingWithMutexes();
+        
+        loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
+                .configure(FixedListMachineProvisioningLocation.MACHINE_SPECS, ImmutableList.<LocationSpec<? extends MachineLocation>>of(
+                        LocationSpec.create(WinRmMachineLocationWithRecordingMutex.class)
+                                .configure("mutexSupport", mutexSupport)
+                                .configure("address", "1.2.3.4")
+                                .configure(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()))));
+        
+        class MyResponseGenerator implements CustomResponseGenerator {
+            private final boolean expectesMutex;
+            MyResponseGenerator(boolean expectesMutex) {
+                this.expectesMutex = expectesMutex;
+            }
+            @Override public CustomResponse generate(ExecParams execParams) {
+                assertEquals(mutexSupport.holdsMutex("installation lock at host"), expectesMutex);
+                return new CustomResponse(0, "", "");
+            }
+        };
+        
+        RecordingWinRmTool.setCustomResponse(".*preInstallCommand.*", new MyResponseGenerator(true));
+        RecordingWinRmTool.setCustomResponse(".*installCommand.*", new MyResponseGenerator(true));
+        RecordingWinRmTool.setCustomResponse(".*postInstallCommand.*", new MyResponseGenerator(true));
+
+        RecordingWinRmTool.setCustomResponse(".*preInstallPowershell.*", new MyResponseGenerator(true));
+        RecordingWinRmTool.setCustomResponse(".*installPowershell.*", new MyResponseGenerator(true));
+        RecordingWinRmTool.setCustomResponse(".*postInstallPowershell.*", new MyResponseGenerator(true));
+
+        RecordingWinRmTool.setCustomResponse(".*", new MyResponseGenerator(false));
+
+        app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
+                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
+                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
+                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
+                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
+                .configure(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND, "preInstallPowershell")
+                .configure(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND, "installPowershell")
+                .configure(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND, "postInstallPowershell")
+                .configure(VanillaWindowsProcess.PRE_CUSTOMIZE_COMMAND, "preCustomizeCommand")
+                .configure(VanillaWindowsProcess.CUSTOMIZE_COMMAND, "customizeCommand")
+                .configure(VanillaWindowsProcess.POST_CUSTOMIZE_COMMAND, "postCustomizeCommand")
+                .configure(VanillaWindowsProcess.PRE_LAUNCH_COMMAND, "preLaunchCommand")
+                .configure(VanillaWindowsProcess.LAUNCH_COMMAND, "launchCommand")
+                .configure(VanillaWindowsProcess.POST_LAUNCH_COMMAND, "postLaunchCommand")
+                .configure(VanillaWindowsProcess.CHECK_RUNNING_COMMAND, "checkRunningCommand")
+                .configure(VanillaWindowsProcess.STOP_COMMAND, "stopCommand"));
+        app.start(ImmutableList.of(loc));
+
+        assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
+                "preInstallCommand", "installCommand", "postInstallCommand",
+                "preInstallPowershell", "installPowershell", "postInstallPowershell",
+                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", 
+                "preLaunchCommand", "launchCommand", "postLaunchCommand", 
+                "checkRunningCommand"));
+        
+        app.stop();
+
+        assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
+    }
+    
+    public static class RecordingWithMutexes implements WithMutexes {
+        // TODO record whether we have the lock
+        
+    }
+    public static class WinRmMachineLocationWithRecordingMutex extends WinRmMachineLocation {
+        public static final ConfigKey<WithMutexes> MUTEX_SUPPORT = ConfigKeys.newConfigKey(WithMutexes.class, "mutexSupport");
+        
+        @Override
+        public WithMutexes mutexes() {
+            return config().get(MUTEX_SUPPORT);
+        }
+    }
+    
     protected void assertExecsContain(List<? extends ExecParams> actuals, List<String> expectedCmds) {
         String errMsg = "actuals="+actuals+"; expected="+expectedCmds;
         assertTrue(actuals.size() >= expectedCmds.size(), "actualSize="+actuals.size()+"; expectedSize="+expectedCmds.size()+"; "+errMsg);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f3809df4/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
----------------------------------------------------------------------
diff --git a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
index 116dc7d..40eff1b 100644
--- a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
+++ b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
@@ -21,6 +21,8 @@ package org.apache.brooklyn.util.core.internal.winrm;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.io.InputStream;
+import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -30,7 +32,6 @@ import org.apache.brooklyn.util.stream.Streams;
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 
 /**
  * For stubbing out the {@link WinRmTool}, so that no real winrm commands are executed.
@@ -98,7 +99,7 @@ public class RecordingWinRmTool implements WinRmTool {
     
     public static List<ExecParams> execs = Lists.newCopyOnWriteArrayList();
     public static List<Map<?,?>> constructorProps = Lists.newCopyOnWriteArrayList();
-    public static Map<String, CustomResponseGenerator> customResponses = Maps.newConcurrentMap();
+    public static Map<String, CustomResponseGenerator> customResponses = Collections.synchronizedMap(new LinkedHashMap<>());
     
     public static void clear() {
         execs.clear();
@@ -163,8 +164,12 @@ public class RecordingWinRmTool implements WinRmTool {
     }
     
     protected WinRmToolResponse generateResponse(ExecParams execParams) {
+        LinkedHashMap<String, CustomResponseGenerator> customResponsesCopy;
+        synchronized (customResponses) {
+            customResponsesCopy = new LinkedHashMap<>(customResponses);
+        }
         for (String cmd : execParams.commands) {
-            for (Entry<String, CustomResponseGenerator> entry : customResponses.entrySet()) {
+            for (Entry<String, CustomResponseGenerator> entry : customResponsesCopy.entrySet()) {
                 if (cmd.matches(entry.getKey())) {
                     CustomResponseGenerator responseGenerator = entry.getValue();
                     CustomResponse response = responseGenerator.generate(execParams);


[2/7] brooklyn-server git commit: RecordingwithMutexes

Posted by al...@apache.org.
RecordingwithMutexes


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

Branch: refs/heads/master
Commit: 8d365ddb2128afc91b4ffaba94f0f52c6949b51b
Parents: f3809df
Author: Duncan Grant <du...@cloudsoftcorp.com>
Authored: Wed Jan 17 11:52:31 2018 +0000
Committer: Duncan Grant <du...@cloudsoftcorp.com>
Committed: Wed Jan 17 11:52:31 2018 +0000

----------------------------------------------------------------------
 .../base/VanillaWindowsProcessTest.java         | 71 ++++++++++++++------
 1 file changed, 51 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8d365ddb/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
index 50f84e5..5f8a555 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -18,10 +18,12 @@
  */
 package org.apache.brooklyn.entity.software.base;
 
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 import java.util.List;
+import java.util.Map;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.Location;
@@ -33,10 +35,12 @@ import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation;
 import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomResponse;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomResponseGenerator;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams;
+import org.apache.brooklyn.util.core.mutex.MutexSupport;
 import org.apache.brooklyn.util.core.mutex.WithMutexes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -128,29 +132,29 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
 
         assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
     }
-    
+
     @Test
     public void testMutexForInstallSteps() throws Exception {
         RecordingWithMutexes mutexSupport = new RecordingWithMutexes();
-        
+
         loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
                 .configure(FixedListMachineProvisioningLocation.MACHINE_SPECS, ImmutableList.<LocationSpec<? extends MachineLocation>>of(
                         LocationSpec.create(WinRmMachineLocationWithRecordingMutex.class)
                                 .configure("mutexSupport", mutexSupport)
                                 .configure("address", "1.2.3.4")
                                 .configure(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()))));
-        
+
         class MyResponseGenerator implements CustomResponseGenerator {
-            private final boolean expectesMutex;
-            MyResponseGenerator(boolean expectesMutex) {
-                this.expectesMutex = expectesMutex;
+            private final boolean expectsMutex;
+            MyResponseGenerator(boolean expectsMutex) {
+                this.expectsMutex = expectsMutex;
             }
             @Override public CustomResponse generate(ExecParams execParams) {
-                assertEquals(mutexSupport.holdsMutex("installation lock at host"), expectesMutex);
+                assertEquals(mutexSupport.holdsMutex("installation lock at host"), expectsMutex);
                 return new CustomResponse(0, "", "");
             }
         };
-        
+
         RecordingWinRmTool.setCustomResponse(".*preInstallCommand.*", new MyResponseGenerator(true));
         RecordingWinRmTool.setCustomResponse(".*installCommand.*", new MyResponseGenerator(true));
         RecordingWinRmTool.setCustomResponse(".*postInstallCommand.*", new MyResponseGenerator(true));
@@ -163,9 +167,9 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
 
         app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
                 .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
-                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
-                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
-                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
+//                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
+//                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
+//                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
                 .configure(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND, "preInstallPowershell")
                 .configure(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND, "installPowershell")
                 .configure(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND, "postInstallPowershell")
@@ -180,30 +184,57 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         app.start(ImmutableList.of(loc));
 
         assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
-                "preInstallCommand", "installCommand", "postInstallCommand",
+//                "preInstallCommand", "installCommand", "postInstallCommand",
                 "preInstallPowershell", "installPowershell", "postInstallPowershell",
-                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", 
-                "preLaunchCommand", "launchCommand", "postLaunchCommand", 
+                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand",
+                "preLaunchCommand", "launchCommand", "postLaunchCommand",
                 "checkRunningCommand"));
-        
+
         app.stop();
 
         assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
     }
-    
+
     public static class RecordingWithMutexes implements WithMutexes {
-        // TODO record whether we have the lock
-        
+        Map<String, Boolean> locks = new MutableMap<>();
+        WithMutexes mutexes = new MutexSupport();
+
+        @Override
+        public boolean hasMutex(String mutexId) {
+            return mutexes.hasMutex(mutexId);
+        }
+
+        @Override
+        public void acquireMutex(String mutexId, String description) {
+            locks.put(mutexId, true);
+            mutexes.acquireMutex(mutexId, description);
+        }
+
+        @Override
+        public boolean tryAcquireMutex(String mutexId, String description) {
+            return mutexes.tryAcquireMutex(mutexId, description);
+        }
+
+        @Override
+        public void releaseMutex(String mutexId) {
+            locks.put(mutexId, false);
+            mutexes.releaseMutex(mutexId);
+        }
+
+        public boolean holdsMutex(String mutexId) {
+            return locks.get(mutexId);
+        }
     }
+
     public static class WinRmMachineLocationWithRecordingMutex extends WinRmMachineLocation {
         public static final ConfigKey<WithMutexes> MUTEX_SUPPORT = ConfigKeys.newConfigKey(WithMutexes.class, "mutexSupport");
-        
+
         @Override
         public WithMutexes mutexes() {
             return config().get(MUTEX_SUPPORT);
         }
     }
-    
+
     protected void assertExecsContain(List<? extends ExecParams> actuals, List<String> expectedCmds) {
         String errMsg = "actuals="+actuals+"; expected="+expectedCmds;
         assertTrue(actuals.size() >= expectedCmds.size(), "actualSize="+actuals.size()+"; expectedSize="+expectedCmds.size()+"; "+errMsg);


[5/7] brooklyn-server git commit: Add mutex to copPreInstallResources

Posted by al...@apache.org.
Add mutex to copPreInstallResources

Should probably do this in the same way as with copyInstallResources.


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

Branch: refs/heads/master
Commit: 90c63b2cc018e3d6586880354de95923b999718f
Parents: bad7ec0
Author: Duncan Grant <du...@cloudsoftcorp.com>
Authored: Wed Jan 17 16:03:25 2018 +0000
Committer: Duncan Grant <du...@cloudsoftcorp.com>
Committed: Wed Jan 17 16:03:25 2018 +0000

----------------------------------------------------------------------
 .../AbstractSoftwareProcessWinRmDriver.java     | 21 ++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/90c63b2c/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
index 28a6b97..b7d23ea 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
@@ -171,6 +171,20 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
         }
     }
 
+    @Override
+    public void copyPreInstallResources() {
+        final WithMutexes mutexSupport = getLocation().mutexes();
+        String mutexId = "installation lock at host";
+        mutexSupport.acquireMutex(mutexId, "pre-installing " + elvis(entity, this));
+        try {
+            super.copyPreInstallResources();
+        } catch (Exception e) {
+            LOG.warn("Error copying pre-install resources", e);
+            throw Exceptions.propagate(e);
+        } finally {
+            mutexSupport.releaseMutex(mutexId);
+        }
+    }
 
     @Override
     public void copyInstallResources() {
@@ -180,7 +194,7 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
         try {
             super.copyInstallResources();
         } catch (Exception e) {
-//            log.warn("Error copying install resources", e);
+            LOG.warn("Error copying install resources", e);
             throw Exceptions.propagate(e);
         } finally {
             mutexSupport.releaseMutex(mutexId);
@@ -190,13 +204,12 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     @Override
     public void copyCustomizeResources() {
         final WithMutexes mutexSupport = getLocation().mutexes();
-        final String description = "customizing " + elvis(entity, this);
         String mutexId = "installation lock at host";
-        mutexSupport.acquireMutex(mutexId, description);
+        mutexSupport.acquireMutex(mutexId, "customizing " + elvis(entity, this));
         try {
             super.copyCustomizeResources();
         } catch (Exception e) {
-//            log.warn("Error copying customize resources", e);
+            LOG.warn("Error copying customize resources", e);
             throw Exceptions.propagate(e);
         } finally {
             mutexSupport.releaseMutex(mutexId);


[4/7] brooklyn-server git commit: Synchronise on install mutex in copy resources

Posted by al...@apache.org.
Synchronise on install mutex in copy resources

Specifically we use the same mutex for copyInstallResources and
copyCustomizeResources.


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

Branch: refs/heads/master
Commit: bad7ec05bfdcb3db7a5cb7071fa87bac3de08a07
Parents: e5e9922
Author: Duncan Grant <du...@cloudsoftcorp.com>
Authored: Wed Jan 17 14:14:21 2018 +0000
Committer: Duncan Grant <du...@cloudsoftcorp.com>
Committed: Wed Jan 17 14:14:21 2018 +0000

----------------------------------------------------------------------
 .../AbstractSoftwareProcessWinRmDriver.java     | 41 +++++++++++++++++---
 .../base/VanillaWindowsProcessTest.java         | 31 ++++++++++++---
 .../core/internal/winrm/RecordingWinRmTool.java |  5 ++-
 3 files changed, 64 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bad7ec05/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
index 014af20..28a6b97 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessWinRmDriver.java
@@ -43,7 +43,7 @@ import org.apache.brooklyn.entity.software.base.lifecycle.WinRmExecuteHelper;
 import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmTool;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse;
-import org.apache.brooklyn.util.core.mutex.MutexSupport;
+import org.apache.brooklyn.util.core.mutex.WithMutexes;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.exceptions.ReferenceWithError;
@@ -55,7 +55,6 @@ import org.apache.cxf.interceptor.Fault;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
@@ -78,7 +77,7 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     protected WinRmExecuteHelper newScript(String phase) {
         return newScript(Maps.<String, Object>newLinkedHashMap(), phase);
     }
-    
+
     protected WinRmExecuteHelper newScript(String command, String psCommand, String phase) {
         return newScript(command, psCommand, phase, null);
     }
@@ -172,6 +171,38 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
         }
     }
 
+
+    @Override
+    public void copyInstallResources() {
+        final WithMutexes mutexSupport = getLocation().mutexes();
+        String mutexId = "installation lock at host";
+        mutexSupport.acquireMutex(mutexId, "installing " + elvis(entity, this));
+        try {
+            super.copyInstallResources();
+        } catch (Exception e) {
+//            log.warn("Error copying install resources", e);
+            throw Exceptions.propagate(e);
+        } finally {
+            mutexSupport.releaseMutex(mutexId);
+        }
+    }
+
+    @Override
+    public void copyCustomizeResources() {
+        final WithMutexes mutexSupport = getLocation().mutexes();
+        final String description = "customizing " + elvis(entity, this);
+        String mutexId = "installation lock at host";
+        mutexSupport.acquireMutex(mutexId, description);
+        try {
+            super.copyCustomizeResources();
+        } catch (Exception e) {
+//            log.warn("Error copying customize resources", e);
+            throw Exceptions.propagate(e);
+        } finally {
+            mutexSupport.releaseMutex(mutexId);
+        }
+    }
+
     @Override
     public WinRmMachineLocation getLocation() {
         return (WinRmMachineLocation)super.getLocation();
@@ -199,7 +230,7 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
     public int executePsCommand(Map flags, String command, String phase) {
         return executeNativeOrPsCommand(flags, null, command, phase, true);
     }
-    
+
     /**
      * @deprecated since 0.5.0; instead rely on {@link org.apache.brooklyn.api.entity.drivers.downloads.DownloadResolverManager} to inc
      *
@@ -241,7 +272,7 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
         if (createParentDir) {
             createDirectory(getDirectory(target), "Creating resource directory");
         }
-        
+
         InputStream stream = null;
         try {
             Tasks.setBlockingDetails("retrieving resource "+source+" for copying across");

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bad7ec05/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
index 2cd6fca..712ced6 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -18,10 +18,10 @@
  */
 package org.apache.brooklyn.entity.software.base;
 
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
+import static org.testng.Assert.*;
 
+import java.io.File;
+import java.nio.file.Files;
 import java.util.List;
 import java.util.Map;
 
@@ -42,13 +42,16 @@ import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomRes
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams;
 import org.apache.brooklyn.util.core.mutex.MutexSupport;
 import org.apache.brooklyn.util.core.mutex.WithMutexes;
+import org.apache.brooklyn.util.text.Identifiers;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
 public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
 
@@ -177,15 +180,24 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
 
         createLocationWithMutexSupport(mutexSupport);
 
-
+        RecordingWinRmTool.setCustomResponse(".*my.*File.*", new MyResponseGenerator(true,mutexSupport));;
+        RecordingWinRmTool.setCustomResponse(".*New-Item.*", new MyResponseGenerator(true,mutexSupport));;
         RecordingWinRmTool.setCustomResponse(".*preInstallPowershell.*", new MyResponseGenerator(true, mutexSupport));
         RecordingWinRmTool.setCustomResponse(".*installPowershell.*", new MyResponseGenerator(true, mutexSupport));
         RecordingWinRmTool.setCustomResponse(".*postInstallPowershell.*", new MyResponseGenerator(true, mutexSupport));
 
         RecordingWinRmTool.setCustomResponse(".*", new MyResponseGenerator(false, mutexSupport));
 
+        File instFile = Files.createTempFile("VanillaWindows-" + Identifiers.makeRandomId(6), "txt").toFile();
+        com.google.common.io.Files.write("myPreInstallFile", instFile, Charsets.UTF_8);
+
+        File custFile = Files.createTempFile("VanillaWindows-" + Identifiers.makeRandomId(6), "txt").toFile();
+        com.google.common.io.Files.write("myPreCustomizeFile", custFile, Charsets.UTF_8);
+
         app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
                 .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
+                .configure(SoftwareProcess.INSTALL_FILES, ImmutableMap.of(instFile.getAbsolutePath(), "c:\\tempInstFile"))
+                .configure(SoftwareProcess.CUSTOMIZE_FILES, ImmutableMap.of(custFile.getAbsolutePath(), "c:\\tempCustFile"))
                 .configure(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND, "preInstallPowershell")
                 .configure(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND, "installPowershell")
                 .configure(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND, "postInstallPowershell")
@@ -200,8 +212,15 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         app.start(ImmutableList.of(loc));
 
         assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
-                "preInstallPowershell", "installPowershell", "postInstallPowershell",
-                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand",
+                "preInstallPowershell",
+                "New-Item.*",
+                "myPreInstallFile",
+                "installPowershell",
+                "postInstallPowershell",
+                "New-Item.*",
+                "myPreCustomizeFile",
+                "preCustomizeCommand",
+                "customizeCommand", "postCustomizeCommand",
                 "preLaunchCommand", "launchCommand", "postLaunchCommand",
                 "checkRunningCommand"));
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bad7ec05/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
----------------------------------------------------------------------
diff --git a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
index 40eff1b..113ef9b 100644
--- a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
+++ b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
@@ -153,8 +153,9 @@ public class RecordingWinRmTool implements WinRmTool {
 
     @Override
     public WinRmToolResponse copyToServer(InputStream source, String destination) {
-        execs.add(new ExecParams(ExecType.COPY_TO_SERVER, ownConstructorProps, ImmutableList.of(new String(Streams.readFully(source)))));
-        return new WinRmToolResponse("", "", 0);
+        ExecParams execParams = new ExecParams(ExecType.COPY_TO_SERVER, ownConstructorProps, ImmutableList.of(new String(Streams.readFully(source))));
+        execs.add(execParams);
+        return generateResponse(execParams);
     }
 
     @Override


[3/7] brooklyn-server git commit: Duplicate mutex test using ssh commands

Posted by al...@apache.org.
Duplicate mutex test using ssh commands


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

Branch: refs/heads/master
Commit: e5e9922baf348e1c72bcc9277ccb16cf702b200c
Parents: 8d365dd
Author: Duncan Grant <du...@cloudsoftcorp.com>
Authored: Wed Jan 17 12:05:30 2018 +0000
Committer: Duncan Grant <du...@cloudsoftcorp.com>
Committed: Wed Jan 17 12:05:30 2018 +0000

----------------------------------------------------------------------
 .../base/VanillaWindowsProcessTest.java         | 93 ++++++++++++++------
 1 file changed, 65 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e5e9922b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
index 5f8a555..2cd6fca 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -134,42 +134,58 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
     }
 
     @Test
-    public void testMutexForInstallSteps() throws Exception {
+    public void testMutexForSshInstallSteps() throws Exception {
         RecordingWithMutexes mutexSupport = new RecordingWithMutexes();
 
-        loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
-                .configure(FixedListMachineProvisioningLocation.MACHINE_SPECS, ImmutableList.<LocationSpec<? extends MachineLocation>>of(
-                        LocationSpec.create(WinRmMachineLocationWithRecordingMutex.class)
-                                .configure("mutexSupport", mutexSupport)
-                                .configure("address", "1.2.3.4")
-                                .configure(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()))));
+        createLocationWithMutexSupport(mutexSupport);
 
-        class MyResponseGenerator implements CustomResponseGenerator {
-            private final boolean expectsMutex;
-            MyResponseGenerator(boolean expectsMutex) {
-                this.expectsMutex = expectsMutex;
-            }
-            @Override public CustomResponse generate(ExecParams execParams) {
-                assertEquals(mutexSupport.holdsMutex("installation lock at host"), expectsMutex);
-                return new CustomResponse(0, "", "");
-            }
-        };
+        RecordingWinRmTool.setCustomResponse(".*preInstallCommand.*", new MyResponseGenerator(true, mutexSupport));
+        RecordingWinRmTool.setCustomResponse(".*installCommand.*", new MyResponseGenerator(true, mutexSupport));
+        RecordingWinRmTool.setCustomResponse(".*postInstallCommand.*", new MyResponseGenerator(true, mutexSupport));
 
-        RecordingWinRmTool.setCustomResponse(".*preInstallCommand.*", new MyResponseGenerator(true));
-        RecordingWinRmTool.setCustomResponse(".*installCommand.*", new MyResponseGenerator(true));
-        RecordingWinRmTool.setCustomResponse(".*postInstallCommand.*", new MyResponseGenerator(true));
+        RecordingWinRmTool.setCustomResponse(".*", new MyResponseGenerator(false, mutexSupport));
 
-        RecordingWinRmTool.setCustomResponse(".*preInstallPowershell.*", new MyResponseGenerator(true));
-        RecordingWinRmTool.setCustomResponse(".*installPowershell.*", new MyResponseGenerator(true));
-        RecordingWinRmTool.setCustomResponse(".*postInstallPowershell.*", new MyResponseGenerator(true));
+        app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
+                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
+                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
+                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
+                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
+                .configure(VanillaWindowsProcess.PRE_CUSTOMIZE_COMMAND, "preCustomizeCommand")
+                .configure(VanillaWindowsProcess.CUSTOMIZE_COMMAND, "customizeCommand")
+                .configure(VanillaWindowsProcess.POST_CUSTOMIZE_COMMAND, "postCustomizeCommand")
+                .configure(VanillaWindowsProcess.PRE_LAUNCH_COMMAND, "preLaunchCommand")
+                .configure(VanillaWindowsProcess.LAUNCH_COMMAND, "launchCommand")
+                .configure(VanillaWindowsProcess.POST_LAUNCH_COMMAND, "postLaunchCommand")
+                .configure(VanillaWindowsProcess.CHECK_RUNNING_COMMAND, "checkRunningCommand")
+                .configure(VanillaWindowsProcess.STOP_COMMAND, "stopCommand"));
+        app.start(ImmutableList.of(loc));
 
-        RecordingWinRmTool.setCustomResponse(".*", new MyResponseGenerator(false));
+        assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
+                "preInstallCommand", "installCommand", "postInstallCommand",
+                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand",
+                "preLaunchCommand", "launchCommand", "postLaunchCommand",
+                "checkRunningCommand"));
+
+        app.stop();
+
+        assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
+    }
+
+    @Test
+    public void testMutexForPowerShellInstallSteps() throws Exception {
+        RecordingWithMutexes mutexSupport = new RecordingWithMutexes();
+
+        createLocationWithMutexSupport(mutexSupport);
+
+
+        RecordingWinRmTool.setCustomResponse(".*preInstallPowershell.*", new MyResponseGenerator(true, mutexSupport));
+        RecordingWinRmTool.setCustomResponse(".*installPowershell.*", new MyResponseGenerator(true, mutexSupport));
+        RecordingWinRmTool.setCustomResponse(".*postInstallPowershell.*", new MyResponseGenerator(true, mutexSupport));
+
+        RecordingWinRmTool.setCustomResponse(".*", new MyResponseGenerator(false, mutexSupport));
 
         app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
                 .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
-//                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
-//                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
-//                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
                 .configure(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND, "preInstallPowershell")
                 .configure(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND, "installPowershell")
                 .configure(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND, "postInstallPowershell")
@@ -184,7 +200,6 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         app.start(ImmutableList.of(loc));
 
         assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
-//                "preInstallCommand", "installCommand", "postInstallCommand",
                 "preInstallPowershell", "installPowershell", "postInstallPowershell",
                 "preCustomizeCommand", "customizeCommand", "postCustomizeCommand",
                 "preLaunchCommand", "launchCommand", "postLaunchCommand",
@@ -195,6 +210,15 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
     }
 
+    void createLocationWithMutexSupport(RecordingWithMutexes mutexSupport) {
+        loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
+                .configure(FixedListMachineProvisioningLocation.MACHINE_SPECS, ImmutableList.<LocationSpec<? extends MachineLocation>>of(
+                        LocationSpec.create(WinRmMachineLocationWithRecordingMutex.class)
+                                .configure("mutexSupport", mutexSupport)
+                                .configure("address", "1.2.3.4")
+                                .configure(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()))));
+    }
+
     public static class RecordingWithMutexes implements WithMutexes {
         Map<String, Boolean> locks = new MutableMap<>();
         WithMutexes mutexes = new MutexSupport();
@@ -226,6 +250,19 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
         }
     }
 
+    private static class MyResponseGenerator implements CustomResponseGenerator {
+        private final boolean expectsMutex;
+        private RecordingWithMutexes mutexSupport;
+
+        MyResponseGenerator(boolean expectsMutex, RecordingWithMutexes mutexSupport) {
+            this.expectsMutex = expectsMutex;
+            this.mutexSupport = mutexSupport;
+        }
+        @Override public CustomResponse generate(ExecParams execParams) {
+            assertEquals(mutexSupport.holdsMutex("installation lock at host"), expectsMutex);
+            return new CustomResponse(0, "", "");
+        }
+    };
     public static class WinRmMachineLocationWithRecordingMutex extends WinRmMachineLocation {
         public static final ConfigKey<WithMutexes> MUTEX_SUPPORT = ConfigKeys.newConfigKey(WithMutexes.class, "mutexSupport");
 


[7/7] brooklyn-server git commit: This closes #935

Posted by al...@apache.org.
This closes #935


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

Branch: refs/heads/master
Commit: 1082d1af5b94afbb68c90ed07245113b1fa2919d
Parents: 469d192 ec2e6b9
Author: Aled Sage <al...@gmail.com>
Authored: Thu Jan 18 09:02:34 2018 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Jan 18 09:02:34 2018 +0000

----------------------------------------------------------------------
 .../base/AbstractSoftwareProcessSshDriver.java  |  19 +-
 .../AbstractSoftwareProcessWinRmDriver.java     |  99 ++++++++---
 .../base/VanillaWindowsProcessWinRmDriver.java  |  12 +-
 .../base/lifecycle/WinRmExecuteHelper.java      |  62 +++++--
 .../base/VanillaWindowsProcessTest.java         | 173 ++++++++++++++++++-
 .../core/internal/winrm/RecordingWinRmTool.java |  16 +-
 6 files changed, 335 insertions(+), 46 deletions(-)
----------------------------------------------------------------------



[6/7] brooklyn-server git commit: Single mutex for installation

Posted by al...@apache.org.
Single mutex for installation

A single mutex id is used for copying resources and installation tasks.
This may slow down an installation but prevents the issue where e.g. 2
entities are trying to write to the same location or use the same shared
file.


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

Branch: refs/heads/master
Commit: ec2e6b9ea0f0f75f6f3edfbb5269f6c4781bbffe
Parents: 90c63b2
Author: Duncan Grant <du...@cloudsoftcorp.com>
Authored: Wed Jan 17 16:51:49 2018 +0000
Committer: Duncan Grant <du...@cloudsoftcorp.com>
Committed: Wed Jan 17 16:51:49 2018 +0000

----------------------------------------------------------------------
 .../base/AbstractSoftwareProcessSshDriver.java   | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ec2e6b9e/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessSshDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessSshDriver.java b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessSshDriver.java
index f6885c2..a6b0def 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessSshDriver.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessSshDriver.java
@@ -293,9 +293,24 @@ public abstract class AbstractSoftwareProcessSshDriver extends AbstractSoftwareP
     }
 
     @Override
+    public void copyPreInstallResources() {
+        final WithMutexes mutexSupport = getLocation().mutexes();
+        String mutexId = "installation lock at host";
+        mutexSupport.acquireMutex(mutexId, "pre-installation lock at host for files and templates");
+        try {
+            super.copyPreInstallResources();
+        } catch (Exception e) {
+            log.warn("Error copying pre-install resources", e);
+            throw Exceptions.propagate(e);
+        } finally {
+            mutexSupport.releaseMutex(mutexId);
+        }
+    }
+
+    @Override
     public void copyInstallResources() {
         final WithMutexes mutexSupport = getLocation().mutexes();
-        final String mutexId = "installing " + elvis(entity, this);
+        String mutexId = "installation lock at host";
         mutexSupport.acquireMutex(mutexId, "installation lock at host for files and templates");
         try {
             super.copyInstallResources();
@@ -310,7 +325,7 @@ public abstract class AbstractSoftwareProcessSshDriver extends AbstractSoftwareP
     @Override
     public void copyCustomizeResources() {
         final WithMutexes mutexSupport = getLocation().mutexes();
-        final String mutexId = "customizing " + elvis(entity, this);
+        String mutexId = "installation lock at host";
         mutexSupport.acquireMutex(mutexId, "installation lock at host for files and templates");
         try {
             super.copyCustomizeResources();