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 * @author Mattias Holmqvist
*/ */
public class AttachHardDiskToMachineIfNotAlreadyAttached implements Function<IMachine, Void> { public class AttachMediumToMachineIfNotAlreadyAttached implements Function<IMachine, Void> {
private String controllerIDE; private String controllerIDE;
private IMedium hardDisk; private IMedium hardDisk;
private String adminDiskPath; private int controllerPort;
private String diskFormat; private int device;
private VirtualBoxManager manager; 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.controllerIDE = controllerIDE;
this.hardDisk = hardDisk; this.hardDisk = hardDisk;
this.manager = manager; this.controllerPort = controllerPort;
this.device = device;
this.deviceType = deviceType;
} }
@Override @Override
public Void apply(@Nullable IMachine machine) { public Void apply(@Nullable IMachine machine) {
// Create and attach medium
// Create and attach hard disk
int controllerPort = 0;
int device = 1;
try { try {
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk); machine.attachDevice(controllerIDE, controllerPort, device, deviceType, hardDisk);
machine.saveSettings(); machine.saveSettings();
} catch (VBoxException e) { } catch (VBoxException e) {
if (!alreadyAttached(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.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_WORKINGDIR;
import static org.virtualbox_4_1.AccessMode.ReadOnly; import static org.virtualbox_4_1.AccessMode.ReadOnly;
import static org.virtualbox_4_1.DeviceType.DVD; 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.Shared;
import static org.virtualbox_4_1.LockType.Write; import static org.virtualbox_4_1.LockType.Write;
@ -119,8 +120,11 @@ public class IsoToIMachine implements Function<String, IMachine> {
new File(adminDiskPath).delete(); new File(adminDiskPath).delete();
} }
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
// Attach hard disk to machine // Attach hard disk to machine
ensureMachineHasHardDiskAttached(vmName, adminDiskPath); ensureMachineHasHardDiskAttached(vmName, hardDisk);
// NAT // NAT
ensureNATNetworkingIsAppliedToMachine(vmName); ensureNATNetworkingIsAppliedToMachine(vmName);
@ -129,16 +133,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD, final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD,
AccessMode.ReadOnly, forceOverwrite); AccessMode.ReadOnly, forceOverwrite);
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() { // Guest additions
ensureGuestAdditionsMediumIsAttached(vmName, guestAdditionsDvdMedium);
@Override
public Void apply(IMachine machine) {
machine.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
machine.saveSettings();
return null;
}
});
IProgress prog = vm.launchVMProcess(manager.getSessionObject(), "gui", ""); IProgress prog = vm.launchVMProcess(manager.getSessionObject(), "gui", "");
prog.waitForCompletion(-1); prog.waitForCompletion(-1);
@ -184,11 +180,14 @@ public class IsoToIMachine implements Function<String, IMachine> {
return vm; return vm;
} }
private void ensureMachineHasHardDiskAttached(String vmName, String path) { private void ensureGuestAdditionsMediumIsAttached(String vmName, final IMedium guestAdditionsDvdMedium) {
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(path);
lockMachineAndApply(manager, Write, vmName, 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) { 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 org.virtualbox_4_1.*;
import static org.easymock.classextension.EasyMock.*; import static org.easymock.classextension.EasyMock.*;
import static org.virtualbox_4_1.DeviceType.HardDisk;
/** /**
* @author Mattias Holmqvist * @author Mattias Holmqvist
*/ */
@Test(groups = "unit", testName = "AttachHardDiskToMachineIfNotAlreadyAttachedTest") @Test(groups = "unit", testName = "AttachMediumToMachineIfNotAlreadyAttachedTest")
public class AttachHardDiskToMachineIfNotAlreadyAttachedTest { public class AttachMediumToMachineIfNotAlreadyAttachedTest {
@Test @Test
public void testAttachHardDiskIfNotAttached() throws Exception { public void testAttachHardDiskIfNotAttached() throws Exception {
@ -49,11 +50,11 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk); expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress); expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk); machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
machine.saveSettings(); machine.saveSettings();
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
verify(machine); verify(machine);
@ -79,12 +80,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
String isoAlreadyAttachedException = errorBuilder.toString(); String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException); 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); expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
verify(machine); verify(machine);
@ -108,12 +109,12 @@ public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
String isoAlreadyAttachedException = errorBuilder.toString(); String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException); 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); expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine); new AttachMediumToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, controllerPort, device, HardDisk).apply(machine);
} }