releasePortForwarding now uses Task F/W to invoke portForwarder.closePortForwarding
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2c194d10 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2c194d10 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2c194d10 Branch: refs/heads/master Commit: 2c194d1078d21053127b3a4e077853d1eb467d48 Parents: 4ed9e97 Author: Mark McKenna <m4rkmcke...@gmail.com> Authored: Wed Dec 2 16:06:21 2015 +0000 Committer: Mark McKenna <m4rkmcke...@gmail.com> Committed: Fri Dec 11 15:49:12 2015 +0000 ---------------------------------------------------------------------- .../location/jclouds/JcloudsLocation.java | 53 ++++++++++++++------ 1 file changed, 39 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2c194d10/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java index 8a666c6..2f08af8 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsLocation.java @@ -57,6 +57,8 @@ import org.apache.brooklyn.api.location.MachineManagementMixins; import org.apache.brooklyn.api.location.NoMachinesAvailableException; import org.apache.brooklyn.api.location.PortRange; import org.apache.brooklyn.api.mgmt.AccessController; +import org.apache.brooklyn.api.mgmt.Task; +import org.apache.brooklyn.api.mgmt.TaskAdaptable; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.config.ConfigKey.HasConfigKey; import org.apache.brooklyn.core.config.ConfigUtils; @@ -96,6 +98,9 @@ import org.apache.brooklyn.util.core.internal.ssh.ShellTool; import org.apache.brooklyn.util.core.internal.ssh.SshTool; import org.apache.brooklyn.util.core.internal.winrm.WinRmTool; import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse; +import org.apache.brooklyn.util.core.task.DynamicTasks; +import org.apache.brooklyn.util.core.task.TaskBuilder; +import org.apache.brooklyn.util.core.task.Tasks; import org.apache.brooklyn.util.core.text.TemplateProcessor; import org.apache.brooklyn.util.exceptions.CompoundRuntimeException; import org.apache.brooklyn.util.exceptions.Exceptions; @@ -2437,13 +2442,13 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im } } - protected void releasePortForwarding(SshMachineLocation machine) { + protected void releasePortForwarding(final SshMachineLocation machine) { // TODO Implementation needs revisisted. It relies on deprecated PortForwardManager methods. boolean usePortForwarding = Boolean.TRUE.equals(machine.getConfig(USE_PORT_FORWARDING)); - JcloudsPortForwarderExtension portForwarder = machine.getConfig(PORT_FORWARDER); + final JcloudsPortForwarderExtension portForwarder = machine.getConfig(PORT_FORWARDER); PortForwardManager portForwardManager = machine.getConfig(PORT_FORWARDING_MANAGER); - NodeMetadata node = (machine instanceof JcloudsSshMachineLocation) ? ((JcloudsSshMachineLocation) machine).getNode() : null; + final NodeMetadata node = (machine instanceof JcloudsSshMachineLocation) ? ((JcloudsSshMachineLocation) machine).getNode() : null; if (portForwarder == null) { LOG.debug("No port-forwarding to close (because portForwarder null) on release of " + machine); @@ -2467,15 +2472,35 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im mappings = ImmutableSet.of(); } - for (PortMapping mapping : mappings) { - HostAndPort publicEndpoint = mapping.getPublicEndpoint(); - int targetPort = mapping.getPrivatePort(); - Protocol protocol = Protocol.TCP; + final TaskBuilder<Void> builder = TaskBuilder.<Void>builder() + .parallel(true) + .displayName("close port-forwarding at "+machine); + + for (final PortMapping mapping : mappings) { + final HostAndPort publicEndpoint = mapping.getPublicEndpoint(); + final int targetPort = mapping.getPrivatePort(); + final Protocol protocol = Protocol.TCP; if (publicEndpoint != null) { - LOG.debug("Closing port-forwarding at {} for machine {}: {}->{}", new Object[] {this, machine, publicEndpoint, targetPort}); - portForwarder.closePortForwarding(node, targetPort, publicEndpoint, protocol); + builder.add(TaskBuilder.builder().displayName("Close port-forward at " +machine).body(new Runnable() { + @Override + public void run() { + LOG.debug("Closing port-forwarding at {} for machine {}: {}->{}", new Object[] {this, machine, publicEndpoint, targetPort}); + portForwarder.closePortForwarding(node, targetPort, publicEndpoint, protocol); + } + }).build()); } } + final Task<Void> task = builder.build(); + final DynamicTasks.TaskQueueingResult<Void> queueResult = DynamicTasks.queueIfPossible(task); + if(!queueResult.isQueuedOrSubmitted()){ + getManagementContext().getExecutionManager().submit(queueResult); + } + final String origDetails = Tasks.setBlockingDetails("waiting for closing port-forwarding of "+machine); + try { + task.blockUntilEnded(); + } finally { + Tasks.setBlockingDetails(origDetails); + } } // Forget all port mappings associated with this VM @@ -2530,11 +2555,11 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im protected String getFirstReachableAddress(NodeMetadata node, ConfigBag setup) { String pollForFirstReachable = setup.get(POLL_FOR_FIRST_REACHABLE_ADDRESS); - + boolean enabled = !"false".equalsIgnoreCase(pollForFirstReachable); String result; if (enabled) { - Duration timeout = "true".equals(pollForFirstReachable) ? Duration.FIVE_MINUTES : Duration.of(pollForFirstReachable); + Duration timeout = "true".equals(pollForFirstReachable) ? Duration.FIVE_MINUTES : Duration.of(pollForFirstReachable); result = JcloudsUtil.getFirstReachableAddress(node, timeout); LOG.debug("Using first-reachable address "+result+" for node "+node+" in "+this); } else { @@ -2579,7 +2604,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im final HostAndPort hostAndPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get() : HostAndPort.fromParts(vmIp, vmPort); final AtomicReference<LoginCredentials> credsSuccessful = new AtomicReference<LoginCredentials>(); - // Don't use config that relates to the final user credentials (those have nothing to do + // Don't use config that relates to the final user credentials (those have nothing to do // with the initial credentials of the VM returned by the cloud provider). // The createTemporaryWinRmMachineLocation deals with removing that. ConfigBag winrmProps = ConfigBag.newInstanceCopying(setup); @@ -2594,7 +2619,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im for (Map.Entry<WinRmMachineLocation, LoginCredentials> entry : machinesToTry.entrySet()) { WinRmMachineLocation machine = entry.getKey(); WinRmToolResponse response = machine.executeScript( - ImmutableMap.of(WinRmTool.PROP_EXEC_TRIES.getName(), 1), + ImmutableMap.of(WinRmTool.PROP_EXEC_TRIES.getName(), 1), ImmutableList.of("echo testing")); boolean success = (response.getStatusCode() == 0); if (success) { @@ -2604,7 +2629,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation im } return false; }}; - + waitForReachable(checker, connectionDetails, credentialsToTry, setup, timeout); } finally { for (WinRmMachineLocation machine : machinesToTry.keySet()) {