mirror of https://github.com/apache/jclouds.git
Cleanup and improved readability for CloneAndRegisterMachineFromIMachineIfNotAlreadyExists.
This commit is contained in:
parent
4effcc39ad
commit
65e0b937ae
|
@ -19,37 +19,24 @@
|
|||
|
||||
package org.jclouds.virtualbox.functions;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.jclouds.virtualbox.MachineUtils.lockMachineAndApply;
|
||||
import static org.virtualbox_4_1.LockType.Write;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.inject.Inject;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.virtualbox_4_1.*;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Named;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.domain.ExecResponse;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.virtualbox_4_1.CloneMode;
|
||||
import org.virtualbox_4_1.CloneOptions;
|
||||
import org.virtualbox_4_1.IMachine;
|
||||
import org.virtualbox_4_1.IProgress;
|
||||
import org.virtualbox_4_1.ISnapshot;
|
||||
import org.virtualbox_4_1.IVirtualBox;
|
||||
import org.virtualbox_4_1.VBoxException;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.inject.Inject;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.jclouds.virtualbox.MachineUtils.lockMachineAndApply;
|
||||
import static org.virtualbox_4_1.LockType.Write;
|
||||
|
||||
/**
|
||||
*
|
||||
* CloneAndRegisterMachineFromIMachineIfNotAlreadyExists will take care of the
|
||||
* followings: - cloning the master - register the clone machine -
|
||||
* ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress,
|
||||
|
@ -57,8 +44,7 @@ import com.google.inject.Inject;
|
|||
*
|
||||
* @author Andrea Turli
|
||||
*/
|
||||
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements
|
||||
Function<IMachine, IMachine> {
|
||||
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Function<IMachine, IMachine> {
|
||||
|
||||
@Resource
|
||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
|
@ -82,7 +68,6 @@ Function<IMachine, IMachine> {
|
|||
String settingsFile, String osTypeId, String vmId,
|
||||
boolean forceOverwrite, String cloneName, String hostId,
|
||||
String snashotName, String snapshotDesc, String controllerIDE) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.context = context;
|
||||
this.settingsFile = settingsFile;
|
||||
|
@ -98,39 +83,32 @@ Function<IMachine, IMachine> {
|
|||
|
||||
@Override
|
||||
public IMachine apply(@Nullable IMachine master) {
|
||||
|
||||
final IVirtualBox vBox = manager.getVBox();
|
||||
try {
|
||||
vBox.findMachine(cloneName);
|
||||
throw new IllegalStateException("Machine " + cloneName
|
||||
+ " is already registered.");
|
||||
throw new IllegalStateException("Machine " + cloneName + " is already registered.");
|
||||
} catch (VBoxException e) {
|
||||
if (machineNotFoundException(e))
|
||||
return cloneMachine(vBox, cloneName, master);
|
||||
return cloneMachine(cloneName, master);
|
||||
else
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean machineNotFoundException(VBoxException e) {
|
||||
return e.getMessage().indexOf(
|
||||
"VirtualBox error: Could not find a registered machine named ") != -1;
|
||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ");
|
||||
}
|
||||
|
||||
private IMachine cloneMachine(IVirtualBox vBox, String cloneName,
|
||||
IMachine master) {
|
||||
IMachine clonedMachine = manager.getVBox().createMachine(settingsFile,
|
||||
cloneName, osTypeId, vmId, forceOverwrite);
|
||||
private IMachine cloneMachine(String cloneName, IMachine master) {
|
||||
IMachine clonedMachine = manager.getVBox().createMachine(settingsFile, cloneName, osTypeId, vmId, forceOverwrite);
|
||||
List<CloneOptions> options = new ArrayList<CloneOptions>();
|
||||
options.add(CloneOptions.Link);
|
||||
|
||||
// takeSnapshotIfNotAlreadyExists
|
||||
ISnapshot currentSnapshot = new TakeSnapshotIfNotAlreadyAttached(manager,
|
||||
snapshotName, snapshotDesc).apply(master);
|
||||
ISnapshot currentSnapshot = new TakeSnapshotIfNotAlreadyAttached(manager, snapshotName, snapshotDesc).apply(master);
|
||||
|
||||
// clone
|
||||
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine,
|
||||
CloneMode.MachineState, options);
|
||||
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine, CloneMode.MachineState, options);
|
||||
|
||||
if (progress.getCompleted())
|
||||
logger.debug("clone done");
|
||||
|
@ -139,15 +117,13 @@ Function<IMachine, IMachine> {
|
|||
manager.getVBox().registerMachine(clonedMachine);
|
||||
|
||||
// Bridged Network
|
||||
List<String> activeBridgedInterfaces = new RetrieveActiveBridgedInterfaces(
|
||||
context).apply(hostId);
|
||||
List<String> activeBridgedInterfaces = new RetrieveActiveBridgedInterfaces(context).apply(hostId);
|
||||
checkNotNull(activeBridgedInterfaces);
|
||||
String macAddress = manager.getVBox().getHost().generateMACAddress();
|
||||
|
||||
// TODO this behavior can be improved
|
||||
String bridgedInterface = activeBridgedInterfaces.get(0);
|
||||
ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress,
|
||||
bridgedInterface);
|
||||
ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress, bridgedInterface);
|
||||
|
||||
// detach iso
|
||||
ensureMachineHasDistroMediumDetached(cloneName, controllerIDE);
|
||||
|
@ -155,32 +131,12 @@ Function<IMachine, IMachine> {
|
|||
return clonedMachine;
|
||||
}
|
||||
|
||||
private void ensureBridgedNetworkingIsAppliedToMachine(String vmName,
|
||||
String macAddress, String hostInterface) {
|
||||
lockMachineAndApply(manager, Write, vmName,
|
||||
new AttachBridgedAdapterToMachine(0l, macAddress, hostInterface));
|
||||
private void ensureBridgedNetworkingIsAppliedToMachine(String vmName, String macAddress, String hostInterface) {
|
||||
lockMachineAndApply(manager, Write, vmName, new AttachBridgedAdapterToMachine(0l, macAddress, hostInterface));
|
||||
}
|
||||
|
||||
private void ensureMachineHasDistroMediumDetached(String vmName,
|
||||
String controllerIDE) {
|
||||
lockMachineAndApply(
|
||||
manager,
|
||||
Write,
|
||||
vmName,
|
||||
new DetachDistroMediumToMachine(checkNotNull(controllerIDE,
|
||||
"controllerIDE")));
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command,
|
||||
RunScriptOptions options) {
|
||||
return context.getComputeService().runScriptOnNode(nodeId, command,
|
||||
options);
|
||||
}
|
||||
|
||||
protected <T> T propagate(Exception e) {
|
||||
Throwables.propagate(e);
|
||||
assert false;
|
||||
return null;
|
||||
private void ensureMachineHasDistroMediumDetached(String vmName, String controllerIDE) {
|
||||
lockMachineAndApply(manager, Write, vmName, new DetachDistroMediumToMachine(checkNotNull(controllerIDE, "controllerIDE")));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,8 +42,7 @@ import com.google.common.base.Predicate;
|
|||
* @author Andrea Turli
|
||||
*/
|
||||
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
|
||||
public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||
BaseVirtualBoxClientLiveTest {
|
||||
public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||
|
||||
private String settingsFile = null;
|
||||
private boolean forceOverwrite = true;
|
||||
|
@ -63,29 +62,15 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
|||
|
||||
@Test
|
||||
public void testCloneMachineFromAnotherMachine() throws Exception {
|
||||
VirtualBoxManager manager = (VirtualBoxManager) context
|
||||
.getProviderSpecificContext().getApi();
|
||||
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(
|
||||
hostId, "localhost", guestId, "localhost", new Credentials("toor",
|
||||
"password"));
|
||||
VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();
|
||||
ComputeServiceContext localHostContext =
|
||||
computeServiceForLocalhostAndGuest(hostId, "localhost", guestId, "localhost", new Credentials("toor", "password"));
|
||||
|
||||
IMachine master = null;
|
||||
try {
|
||||
Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(
|
||||
new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
|
||||
master = new IsoToIMachine(manager, adminDisk, diskFormat,
|
||||
settingsFile, vmName, osTypeId, vmId, forceOverwrite,
|
||||
controllerIDE, localHostContext, hostId, guestId, socketTester,
|
||||
"127.0.0.1", 8080).apply(isoName);
|
||||
} catch (IllegalStateException e) {
|
||||
// already created
|
||||
master = manager.getVBox().findMachine(vmName);
|
||||
}
|
||||
IMachine master = getMasterNode(manager, localHostContext);
|
||||
|
||||
if (master.getCurrentSnapshot() != null) {
|
||||
ISession session = manager.openMachineSession(master);
|
||||
session.getConsole().deleteSnapshot(
|
||||
master.getCurrentSnapshot().getId());
|
||||
session.getConsole().deleteSnapshot(master.getCurrentSnapshot().getId());
|
||||
session.unlockMachine();
|
||||
}
|
||||
|
||||
|
@ -95,4 +80,17 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
|||
controllerIDE).apply(master);
|
||||
assertEquals(clone.getNetworkAdapter(0L).getAttachmentType(), Bridged);
|
||||
}
|
||||
|
||||
private IMachine getMasterNode(VirtualBoxManager manager, ComputeServiceContext localHostContext) {
|
||||
try {
|
||||
Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
|
||||
return new IsoToIMachine(manager, adminDisk, diskFormat,
|
||||
settingsFile, vmName, osTypeId, vmId, forceOverwrite,
|
||||
controllerIDE, localHostContext, hostId, guestId, socketTester,
|
||||
"127.0.0.1", 8080).apply(isoName);
|
||||
} catch (IllegalStateException e) {
|
||||
// already created
|
||||
return manager.getVBox().findMachine(vmName);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue