Virtualbox: Re-used AttachMediumToMachineIfNotAlreadyAttached for attaching guest additions DVD.

This commit is contained in:
Mattias Holmqvist 2011-10-24 00:10:00 +02:00
parent 394bb1b602
commit 03e0bd46ba
3 changed files with 33 additions and 34 deletions

View File

@ -29,28 +29,27 @@ import static org.virtualbox_4_1.DeviceType.HardDisk;
/**
* @author Mattias Holmqvist
*/
public class AttachHardDiskToMachineIfNotAlreadyAttached implements Function<IMachine, Void> {
public class AttachMediumToMachineIfNotAlreadyAttached implements Function<IMachine, Void> {
private String controllerIDE;
private IMedium hardDisk;
private String adminDiskPath;
private String diskFormat;
private VirtualBoxManager manager;
private int controllerPort;
private int device;
private DeviceType deviceType;
public AttachHardDiskToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, VirtualBoxManager manager) {
public AttachMediumToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, int controllerPort, int device, DeviceType deviceType) {
this.controllerIDE = controllerIDE;
this.hardDisk = hardDisk;
this.manager = manager;
this.controllerPort = controllerPort;
this.device = device;
this.deviceType = deviceType;
}
@Override
public Void apply(@Nullable IMachine machine) {
// Create and attach hard disk
int controllerPort = 0;
int device = 1;
// Create and attach medium
try {
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
machine.attachDevice(controllerIDE, controllerPort, device, deviceType, hardDisk);
machine.saveSettings();
} catch (VBoxException e) {
if (!alreadyAttached(e))

View File

@ -27,6 +27,7 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTA
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_WORKINGDIR;
import static org.virtualbox_4_1.AccessMode.ReadOnly;
import static org.virtualbox_4_1.DeviceType.DVD;
import static org.virtualbox_4_1.DeviceType.HardDisk;
import static org.virtualbox_4_1.LockType.Shared;
import static org.virtualbox_4_1.LockType.Write;
@ -119,8 +120,11 @@ public class IsoToIMachine implements Function<String, IMachine> {
new File(adminDiskPath).delete();
}
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
// Attach hard disk to machine
ensureMachineHasHardDiskAttached(vmName, adminDiskPath);
ensureMachineHasHardDiskAttached(vmName, hardDisk);
// NAT
ensureNATNetworkingIsAppliedToMachine(vmName);
@ -129,16 +133,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD,
AccessMode.ReadOnly, forceOverwrite);
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine machine) {
machine.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
machine.saveSettings();
return null;
}
});
// Guest additions
ensureGuestAdditionsMediumIsAttached(vmName, guestAdditionsDvdMedium);
IProgress prog = vm.launchVMProcess(manager.getSessionObject(), "gui", "");
prog.waitForCompletion(-1);
@ -184,11 +180,14 @@ public class IsoToIMachine implements Function<String, IMachine> {
return vm;
}
private void ensureMachineHasHardDiskAttached(String vmName, String path) {
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(path);
private void ensureGuestAdditionsMediumIsAttached(String vmName, final IMedium guestAdditionsDvdMedium) {
lockMachineAndApply(manager, Write, vmName,
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager));
new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, guestAdditionsDvdMedium, 1, 1, DeviceType.DVD));
}
private void ensureMachineHasHardDiskAttached(String vmName, IMedium hardDisk) {
lockMachineAndApply(manager, Write, vmName,
new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, 0, 1, HardDisk));
}
private void ensureMachineHasMemory(String vmName, final long memorySize) {

View File

@ -23,12 +23,13 @@ import org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import static org.easymock.classextension.EasyMock.*;
import static org.virtualbox_4_1.DeviceType.HardDisk;
/**
* @author Mattias Holmqvist
*/
@Test(groups = "unit", testName = "AttachHardDiskToMachineIfNotAlreadyAttachedTest")
public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
@Test(groups = "unit", testName = "AttachMediumToMachineIfNotAlreadyAttachedTest")
public class AttachMediumToMachineIfNotAlreadyAttachedTest {
@Test
public void testAttachHardDiskIfNotAttached() throws Exception {
@ -49,11 +50,11 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
machine.saveSettings();
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
verify(machine);
@ -79,12 +80,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
verify(machine);
@ -108,12 +109,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
}