Repository: jclouds
Updated Branches:
  refs/heads/master 85a1a8c1d -> 040195915


JCLOUDS-347: Implement a poor-mans rollback if CloudStack static NAT creation 
fails


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

Branch: refs/heads/master
Commit: 0401959157832c803ad56bc5973814b1b854525d
Parents: 85a1a8c
Author: Hugo Trippaers <[email protected]>
Authored: Tue Oct 15 14:10:10 2013 +0200
Committer: Andrew Phillips <[email protected]>
Committed: Thu Mar 27 13:01:40 2014 +0100

----------------------------------------------------------------------
 .../CloudStackComputeServiceAdapter.java        | 48 ++++++++++++--------
 1 file changed, 29 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/04019591/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java
----------------------------------------------------------------------
diff --git 
a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java
 
b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java
index 9b9ef1d..9c596e4 100644
--- 
a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java
+++ 
b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java
@@ -242,25 +242,35 @@ public class CloudStackComputeServiceAdapter implements
          credentialsBuilder.password(vm.getPassword());
       }
       
-      if (templateOptions.shouldSetupStaticNat()) {
-         Capabilities capabilities = 
client.getConfigurationApi().listCapabilities();
-         // TODO: possibly not all network ids, do we want to do this
-         for (String networkId : options.getNetworkIds()) {
-            logger.debug(">> creating static NAT for virtualMachine(%s) in 
network(%s)", vm.getId(), networkId);
-            PublicIPAddress ip = 
staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
-            logger.trace("<< static NATed IPAddress(%s) to 
virtualMachine(%s)", ip.getId(), vm.getId());
-            vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
-            List<Integer> ports = 
Ints.asList(templateOptions.getInboundPorts());
-            if (capabilities.getCloudStackVersion().startsWith("2")) {
-               logger.debug(">> setting up IP forwarding for IPAddress(%s) 
rules(%s)", ip.getId(), ports);
-               Set<IPForwardingRule> rules = 
setupPortForwardingRulesForIP.apply(ip, ports);
-               logger.trace("<< setup %d IP forwarding rules on 
IPAddress(%s)", rules.size(), ip.getId());
-            } else {
-               logger.debug(">> setting up firewall rules for IPAddress(%s) 
rules(%s)", ip.getId(), ports);
-               Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, 
ports);
-               logger.trace("<< setup %d firewall rules on IPAddress(%s)", 
rules.size(), ip.getId());
-            }
-         }
+      try {
+          if (templateOptions.shouldSetupStaticNat()) {
+             Capabilities capabilities = 
client.getConfigurationApi().listCapabilities();
+             // TODO: possibly not all network ids, do we want to do this
+             for (String networkId : options.getNetworkIds()) {
+                logger.debug(">> creating static NAT for virtualMachine(%s) in 
network(%s)", vm.getId(), networkId);
+                PublicIPAddress ip = 
staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
+                logger.trace("<< static NATed IPAddress(%s) to 
virtualMachine(%s)", ip.getId(), vm.getId());
+                vm = 
client.getVirtualMachineApi().getVirtualMachine(vm.getId());
+                List<Integer> ports = 
Ints.asList(templateOptions.getInboundPorts());
+                if (capabilities.getCloudStackVersion().startsWith("2")) {
+                   logger.debug(">> setting up IP forwarding for IPAddress(%s) 
rules(%s)", ip.getId(), ports);
+                   Set<IPForwardingRule> rules = 
setupPortForwardingRulesForIP.apply(ip, ports);
+                   logger.trace("<< setup %d IP forwarding rules on 
IPAddress(%s)", rules.size(), ip.getId());
+                } else {
+                   logger.debug(">> setting up firewall rules for 
IPAddress(%s) rules(%s)", ip.getId(), ports);
+                   Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, 
ports);
+                   logger.trace("<< setup %d firewall rules on IPAddress(%s)", 
rules.size(), ip.getId());
+                }
+             }
+          }
+      } catch (RuntimeException re) {
+          logger.error("-- exception after node has been created, trying to 
destroy the created virtualMachine(%s)", vm.getId());
+          try {
+              destroyNode(vm.getId());
+          } catch (RuntimeException re2) {
+              logger.debug("-- exception in exceptionHandler while executing 
destroyNode for virtualMachine(%s), ignoring and rethrowing original 
exception", vm.getId());
+          }
+          throw re;
       }
       return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + 
"", credentialsBuilder.build());
    }

Reply via email to