mirror of https://github.com/apache/jclouds.git
Merge pull request #292 from andreaturli/dev
issue 384: add unlockMachineAndApply to MachineUtils and
This commit is contained in:
commit
775dd3a2b9
|
@ -25,6 +25,7 @@ import org.virtualbox_4_1.ISession;
|
||||||
import org.virtualbox_4_1.LockType;
|
import org.virtualbox_4_1.LockType;
|
||||||
import org.virtualbox_4_1.VBoxException;
|
import org.virtualbox_4_1.VBoxException;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
import org.virtualbox_4_1.jaxws.SessionState;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
|
@ -131,4 +132,52 @@ public class MachineUtils {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlocks the machine and executes the given function using the machine matching the given id.
|
||||||
|
* Since the machine is unlocked it is possible to delete the IMachine.
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @param manager the VirtualBoxManager
|
||||||
|
* @param machineId the id of the machine
|
||||||
|
* @param function the function to execute
|
||||||
|
* @return the result from applying the function to the machine.
|
||||||
|
*/
|
||||||
|
public static <T> T unlockMachineAndApply(VirtualBoxManager manager, final String machineId,
|
||||||
|
final Function<IMachine, T> function) {
|
||||||
|
try {
|
||||||
|
ISession session = manager.getSessionObject();
|
||||||
|
IMachine immutableMachine = manager.getVBox().findMachine(machineId);
|
||||||
|
if(immutableMachine.getSessionState().equals(SessionState.LOCKED))
|
||||||
|
session.unlockMachine();
|
||||||
|
|
||||||
|
return function.apply(immutableMachine);
|
||||||
|
|
||||||
|
} catch (VBoxException e) {
|
||||||
|
throw new RuntimeException(String.format("error applying %s to %s: %s", function, machineId,
|
||||||
|
e.getMessage()), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlocks the machine and executes the given function, if the machine is registered.
|
||||||
|
* Since the machine is unlocked it is possible to delete the machine.
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @param manager the VirtualBoxManager
|
||||||
|
* @param machineId the id of the machine
|
||||||
|
* @param function the function to execute
|
||||||
|
* @return the result from applying the function to the session.
|
||||||
|
*/
|
||||||
|
public static <T> T unlockMachineAndApplyOrReturnNullIfNotRegistered(VirtualBoxManager manager, String machineId,
|
||||||
|
Function<IMachine, T> function) {
|
||||||
|
try {
|
||||||
|
return unlockMachineAndApply(manager, machineId, function);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class);
|
||||||
|
if (vbex != null && vbex.getMessage().indexOf("not find a registered") == -1)
|
||||||
|
throw e;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,8 @@
|
||||||
package org.jclouds.virtualbox.functions;
|
package org.jclouds.virtualbox.functions;
|
||||||
|
|
||||||
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
|
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
|
||||||
import static org.jclouds.virtualbox.util.MachineUtils.lockMachineAndApplyOrReturnNullIfNotRegistered;
|
import static org.jclouds.virtualbox.util.MachineUtils.unlockMachineAndApplyOrReturnNullIfNotRegistered;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.virtualbox_4_1.LockType.Write;
|
|
||||||
|
|
||||||
import org.jclouds.compute.ComputeServiceContext;
|
import org.jclouds.compute.ComputeServiceContext;
|
||||||
import org.jclouds.domain.Credentials;
|
import org.jclouds.domain.Credentials;
|
||||||
|
@ -49,11 +48,11 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||||
|
|
||||||
private static final boolean IS_LINKED_CLONE = true;
|
private static final boolean IS_LINKED_CLONE = true;
|
||||||
private String vmId = "jclouds-image-iso-1";
|
private String vmId = "jclouds-image-iso-1";
|
||||||
private String osTypeId = "DEBIAN";
|
private String osTypeId = "";
|
||||||
private String guestId = "guest";
|
private String guestId = "guest";
|
||||||
private String hostId = "host";
|
private String hostId = "host";
|
||||||
|
|
||||||
private String vmName = "jclouds-image-virtualbox-iso-to-machine-test";
|
private String vmName = "jclouds-virtualbox-clone-test";
|
||||||
private String cloneName = vmName + "_clone";
|
private String cloneName = vmName + "_clone";
|
||||||
private VmSpec clonedVmSpec;
|
private VmSpec clonedVmSpec;
|
||||||
|
|
||||||
|
@ -84,7 +83,8 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||||
manager, workingDir, clonedVmSpec, IS_LINKED_CLONE).apply(master);
|
manager, workingDir, clonedVmSpec, IS_LINKED_CLONE).apply(master);
|
||||||
assertEquals(clone.getName(), clonedVmSpec.getVmName());
|
assertEquals(clone.getName(), clonedVmSpec.getVmName());
|
||||||
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, new IMachineToVmSpec().apply(master)))
|
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, new IMachineToVmSpec().apply(master)))
|
||||||
lockMachineAndApplyOrReturnNullIfNotRegistered(manager, Write, spec.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(spec));
|
unlockMachineAndApplyOrReturnNullIfNotRegistered(manager, spec.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(spec));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IMachine getMasterNode(VirtualBoxManager manager,
|
private IMachine getMasterNode(VirtualBoxManager manager,
|
||||||
|
|
|
@ -19,10 +19,9 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.functions;
|
package org.jclouds.virtualbox.functions;
|
||||||
|
|
||||||
import static org.jclouds.virtualbox.util.MachineUtils.lockMachineAndApplyOrReturnNullIfNotRegistered;
|
import static org.jclouds.virtualbox.util.MachineUtils.unlockMachineAndApplyOrReturnNullIfNotRegistered;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.fail;
|
import static org.testng.Assert.fail;
|
||||||
import static org.virtualbox_4_1.LockType.Write;
|
|
||||||
|
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.ErrorCode;
|
import org.jclouds.virtualbox.domain.ErrorCode;
|
||||||
|
@ -70,14 +69,12 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||||
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
||||||
.memoryMB(512).controller(ideController).cleanUpMode(mode)
|
.memoryMB(512).controller(ideController).cleanUpMode(mode)
|
||||||
.osTypeId("Debian").forceOverwrite(true).build();
|
.osTypeId("Debian").forceOverwrite(true).build();
|
||||||
lockMachineAndApplyOrReturnNullIfNotRegistered(manager, Write, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
unlockMachineAndApplyOrReturnNullIfNotRegistered(manager, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
||||||
|
|
||||||
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(
|
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(
|
||||||
manager, workingDir).apply(launchSpecification);
|
manager, workingDir).apply(launchSpecification);
|
||||||
IMachine machine = manager.getVBox().findMachine(vmName);
|
IMachine machine = manager.getVBox().findMachine(vmName);
|
||||||
assertEquals(debianNode.getName(), machine.getName());
|
assertEquals(debianNode.getName(), machine.getName());
|
||||||
lockMachineAndApplyOrReturnNullIfNotRegistered(manager, Write, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
unlockMachineAndApplyOrReturnNullIfNotRegistered(manager, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -86,8 +83,8 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||||
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
||||||
.memoryMB(512).controller(ideController).cleanUpMode(mode)
|
.memoryMB(512).controller(ideController).cleanUpMode(mode)
|
||||||
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
||||||
lockMachineAndApplyOrReturnNullIfNotRegistered(manager, Write, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
unlockMachineAndApplyOrReturnNullIfNotRegistered(manager, launchSpecification.getVmName(), new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification));
|
||||||
|
//new UnregisterMachineIfExistsAndDeleteItsMedia(launchSpecification).apply(manager.getVBox().findMachine(vmName));
|
||||||
try {
|
try {
|
||||||
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager, workingDir)
|
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager, workingDir)
|
||||||
.apply(launchSpecification);
|
.apply(launchSpecification);
|
||||||
|
|
|
@ -30,6 +30,8 @@ import org.jclouds.virtualbox.domain.ErrorCode;
|
||||||
import org.jclouds.virtualbox.domain.HardDisk;
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.DeviceType;
|
import org.virtualbox_4_1.DeviceType;
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
import org.virtualbox_4_1.IProgress;
|
||||||
import org.virtualbox_4_1.VBoxException;
|
import org.virtualbox_4_1.VBoxException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,9 +43,10 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
|
||||||
public void testCreateMedium() throws Exception {
|
public void testCreateMedium() throws Exception {
|
||||||
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-1.vdi";
|
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-1.vdi";
|
||||||
HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
|
HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
|
||||||
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
IMedium iMedium = new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
||||||
manager.getVBox().findMedium(path, DeviceType.HardDisk);
|
manager.getVBox().findMedium(path, DeviceType.HardDisk);
|
||||||
assertFileCanBeDeleted(path);
|
assertFileCanBeDeleted(path);
|
||||||
|
deleteMediumAndBlockUntilComplete(iMedium);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -63,10 +66,12 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
|
||||||
public void testCreateSameMediumTwiceWhenUsingOverwrite() throws Exception {
|
public void testCreateSameMediumTwiceWhenUsingOverwrite() throws Exception {
|
||||||
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-3.vdi";
|
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-3.vdi";
|
||||||
HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
|
HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
|
||||||
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
IMedium iMedium = new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
||||||
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
iMedium = new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
|
||||||
manager.getVBox().findMedium(path, DeviceType.HardDisk);
|
manager.getVBox().findMedium(path, DeviceType.HardDisk);
|
||||||
assertFileCanBeDeleted(path);
|
assertFileCanBeDeleted(path);
|
||||||
|
deleteMediumAndBlockUntilComplete(iMedium);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertFileCanBeDeleted(String path) {
|
private void assertFileCanBeDeleted(String path) {
|
||||||
|
@ -74,4 +79,10 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
|
||||||
boolean mediumDeleted = file.delete();
|
boolean mediumDeleted = file.delete();
|
||||||
assertTrue(mediumDeleted);
|
assertTrue(mediumDeleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void deleteMediumAndBlockUntilComplete(IMedium medium){
|
||||||
|
final IProgress progress = medium.deleteStorage();
|
||||||
|
progress.waitForCompletion(-1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue