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

This commit is contained in:
Hugo Trippaers 2013-10-15 14:10:10 +02:00 committed by Andrew Phillips
parent 85a1a8c1dd
commit 0401959157
1 changed files with 29 additions and 19 deletions

View File

@ -242,25 +242,35 @@ public class CloudStackComputeServiceAdapter implements
credentialsBuilder.password(vm.getPassword()); credentialsBuilder.password(vm.getPassword());
} }
if (templateOptions.shouldSetupStaticNat()) { try {
Capabilities capabilities = client.getConfigurationApi().listCapabilities(); if (templateOptions.shouldSetupStaticNat()) {
// TODO: possibly not all network ids, do we want to do this Capabilities capabilities = client.getConfigurationApi().listCapabilities();
for (String networkId : options.getNetworkIds()) { // TODO: possibly not all network ids, do we want to do this
logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId); for (String networkId : options.getNetworkIds()) {
PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm); logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId);
logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId());
List<Integer> ports = Ints.asList(templateOptions.getInboundPorts()); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
if (capabilities.getCloudStackVersion().startsWith("2")) { List<Integer> ports = Ints.asList(templateOptions.getInboundPorts());
logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports); if (capabilities.getCloudStackVersion().startsWith("2")) {
Set<IPForwardingRule> rules = setupPortForwardingRulesForIP.apply(ip, ports); logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports);
logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId()); Set<IPForwardingRule> rules = setupPortForwardingRulesForIP.apply(ip, ports);
} else { logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId());
logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports); } else {
Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, ports); logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports);
logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId()); 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()); return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + "", credentialsBuilder.build());
} }