Added a couple of more functions to box to split up IsoToIMachine.

commit dc23ef0c1c
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 18:18:56 2011 +0200

    Added CreateMediumIfNotAlreadyExists to IsoToIMachine.

commit f530a5d585
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 14:30:42 2011 +0200

    Added @author

commit b64fee01a8
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 14:18:26 2011 +0200

    Added AttachHardDiskToMachineIfNotAlreadyAttached and used it from IsoToIMachine.
This commit is contained in:
Mattias Holmqvist 2011-10-23 18:25:13 +02:00
parent 6d4a605f3f
commit 4e3f9579b1
7 changed files with 446 additions and 45 deletions

View File

@ -0,0 +1,68 @@
/*
* *
* * Licensed to jclouds, Inc. (jclouds) under one or more
* * contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. jclouds licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/
package org.jclouds.virtualbox.functions;
import com.google.common.base.Function;
import org.virtualbox_4_1.*;
import javax.annotation.Nullable;
import static org.virtualbox_4_1.DeviceType.HardDisk;
/**
* @author Mattias Holmqvist
*/
public class AttachHardDiskToMachineIfNotAlreadyAttached implements Function<IMachine, Void> {
private String controllerIDE;
private IMedium hardDisk;
private String adminDiskPath;
private String diskFormat;
private VirtualBoxManager manager;
public AttachHardDiskToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, VirtualBoxManager manager) {
this.controllerIDE = controllerIDE;
this.hardDisk = hardDisk;
this.manager = manager;
}
@Override
public Void apply(@Nullable IMachine machine) {
// Create and attach hard disk
int controllerPort = 0;
int device = 1;
try {
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
machine.saveSettings();
} catch (VBoxException e) {
if (!alreadyAttached(e))
throw e;
}
return null;
}
private boolean alreadyAttached(VBoxException e) {
return e.getMessage().indexOf("is already attached to port") != -1;
}
}

View File

@ -30,6 +30,9 @@ import org.virtualbox_4_1.VirtualBoxManager;
import javax.annotation.Nullable;
/**
* @author Mattias Holmqvist
*/
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function<String, IMachine> {
private String settingsFile;

View File

@ -0,0 +1,99 @@
/*
* *
* * Licensed to jclouds, Inc. (jclouds) under one or more
* * contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. jclouds licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/
package org.jclouds.virtualbox.functions;
import com.google.common.base.Function;
import org.virtualbox_4_1.*;
import javax.annotation.Nullable;
/**
* @author Mattias Holmqvist
*/
public class CreateMediumIfNotAlreadyExists implements Function<String, IMedium> {
private final VirtualBoxManager manager;
private final String diskFormat;
private boolean overwriteIfExists;
public CreateMediumIfNotAlreadyExists(VirtualBoxManager manager, String diskFormat, boolean overwriteIfExists) {
this.manager = manager;
this.diskFormat = diskFormat;
this.overwriteIfExists = overwriteIfExists;
}
@Override
public IMedium apply(@Nullable String path) {
IVirtualBox vBox = manager.getVBox();
try {
final IMedium medium = vBox.findMedium(path, DeviceType.HardDisk);
if (overwriteIfExists) {
final IProgress progress = medium.deleteStorage();
progress.waitForCompletion(-1);
return createNewMedium(vBox, path);
} else {
throw new IllegalStateException("Medium for path " + path + " already exists.");
}
} catch (VBoxException e) {
if (notFoundException(e))
return createNewMedium(vBox, path);
throw e;
}
}
private IMedium createNewMedium(IVirtualBox vBox, String path) {
IMedium hardDisk = vBox.createHardDisk(diskFormat, path);
createBaseStorage(hardDisk);
return hardDisk;
}
private boolean notFoundException(VBoxException e) {
return e.getMessage().indexOf("Could not find an open hard disk with location ") != -1;
}
private void createBaseStorage(IMedium hardDisk) {
try {
long size = 4L * 1024L * 1024L * 1024L - 4L;
IProgress storageCreation = hardDisk.createBaseStorage(size, (long) org.virtualbox_4_1.jaxws.MediumVariant.STANDARD.ordinal());
storageCreation.waitForCompletion(-1);
} catch (VBoxException e) {
if (fileNotFoundException(e)) {
// File for medium could not be found. Something wrong with creation.
hardDisk.deleteStorage();
}
if (!storageAlreadyExists(e)) {
// Hard disk file was created but the storage had been created before that.
throw e;
}
}
}
private boolean fileNotFoundException(VBoxException e) {
return e.getMessage().indexOf("VERR_FILE_NOT_FOUND") != -1;
}
private boolean storageAlreadyExists(VBoxException e) {
return e.getMessage().indexOf("VirtualBox error: Storage for the medium ") != -1 &&
e.getMessage().indexOf("is already created") != -1;
}
}

View File

@ -1,23 +1,4 @@
/*
* *
* * Licensed to jclouds, Inc. (jclouds) under one or more
* * contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. jclouds licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/
package org.jclouds.virtualbox.functions;
@ -35,6 +16,7 @@ import static org.virtualbox_4_1.NATProtocol.TCP;
import static org.virtualbox_4_1.NetworkAttachmentType.NAT;
import java.io.File;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Named;
@ -51,16 +33,7 @@ import org.jclouds.ssh.SshException;
import org.jclouds.virtualbox.config.VirtualBoxConstants;
import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning;
import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.AccessMode;
import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
import org.virtualbox_4_1.jaxws.MediumVariant;
import org.virtualbox_4_1.*;
import com.google.common.base.Function;
import com.google.inject.Inject;
@ -141,22 +114,12 @@ public class IsoToIMachine implements Function<String, IMachine> {
new File(adminDiskPath).delete();
}
// Create and attach hard disk
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
long size = 4L * 1024L * 1024L * 1024L - 4L;
IProgress storageCreation = hd.createBaseStorage(size, (long) MediumVariant.STANDARD.ordinal());
storageCreation.waitForCompletion(-1);
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine machine) {
machine.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
machine.saveSettings();
return null;
}
});
// Attach hard disk to machine
lockMachineAndApply(manager, Write, vmName,
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager));
// NAT
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {

View File

@ -0,0 +1,122 @@
/*
* *
* * Licensed to jclouds, Inc. (jclouds) under one or more
* * contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. jclouds licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/
package org.jclouds.virtualbox.functions;
import org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import static org.easymock.classextension.EasyMock.*;
/**
* @author Mattias Holmqvist
*/
@Test(groups = "unit", testName = "AttachHardDiskToMachineIfNotAlreadyAttachedTest")
public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
@Test
public void testAttachHardDiskIfNotAttached() throws Exception {
String controllerIDE = "IDE Controller";
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskFormat = "vdi";
int controllerPort = 0;
int device = 1;
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createNiceMock(IMedium.class);
IProgress progress = createNiceMock(IProgress.class);
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
machine.saveSettings();
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
verify(machine);
}
@Test
public void testDoNothingIfAlreadyAttachedAttachHardDisk() throws Exception {
String controllerIDE = "IDE Controller";
int controllerPort = 0;
int device = 1;
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createNiceMock(IMedium.class);
final StringBuilder errorBuilder = new StringBuilder();
errorBuilder.append("VirtualBox error: ");
errorBuilder.append("Medium '/Users/mattias/jclouds-virtualbox-test/testadmin.vdi' ");
errorBuilder.append("is already attached to port 0, device 1 of controller 'IDE Controller' ");
errorBuilder.append("of this virtual machine (0x80BB000C)");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
verify(machine);
}
@Test(expectedExceptions = VBoxException.class)
public void testFailOnOtherVBoxError() throws Exception {
String controllerIDE = "IDE Controller";
int controllerPort = 0;
int device = 1;
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createNiceMock(IMedium.class);
final StringBuilder errorBuilder = new StringBuilder();
errorBuilder.append("VirtualBox error: ");
errorBuilder.append("Some other VBox error");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk);
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
}
}

View File

@ -32,6 +32,9 @@ import static org.easymock.EasyMock.anyBoolean;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.*;
/**
* @author Mattias Holmqvist
*/
@Test(groups = "unit", testName = "CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest")
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {

View File

@ -0,0 +1,143 @@
/*
* *
* * Licensed to jclouds, Inc. (jclouds) under one or more
* * contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. jclouds licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/
package org.jclouds.virtualbox.functions;
import org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.*;
import static org.testng.Assert.assertNotSame;
/**
* @author Mattias Holmqvist
*/
public class CreateMediumIfNotAlreadyExistsTest {
@Test
public void testCreateMediumWhenDiskDoesNotExists() throws Exception {
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskFormat = "vdi";
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createMock(IMedium.class);
IProgress progress = createNiceMock(IProgress.class);
StringBuilder errorBuilder = new StringBuilder();
errorBuilder.append("org.virtualbox_4_1.VBoxException: VirtualBox error: ");
errorBuilder.append("Could not find an open hard disk with location ");
errorBuilder.append("'/Users/johndoe/jclouds-virtualbox-test/testadmin.vdi' (0x80BB0001)");
String errorMessage = errorBuilder.toString();
expect(manager.getVBox()).andReturn(vBox).anyTimes();
VBoxException notFoundException = new VBoxException(createNiceMock(Throwable.class), errorMessage);
expect(vBox.findMedium(adminDiskPath, DeviceType.HardDisk)).andThrow(notFoundException);
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
replay(manager, machine, vBox, hardDisk);
new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
verify(machine, vBox);
}
@Test
public void testDeleteAndCreateNewStorageWhenMediumExistsAndUsingOverwrite() throws Exception {
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskFormat = "vdi";
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createMock(IMedium.class);
IMedium newHardDisk = createMock(IMedium.class);
IProgress progress = createNiceMock(IProgress.class);
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.findMedium(adminDiskPath, DeviceType.HardDisk)).andReturn(hardDisk);
expect(hardDisk.deleteStorage()).andReturn(progress);
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(newHardDisk);
expect(newHardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
replay(manager, machine, vBox, hardDisk, newHardDisk, progress);
IMedium newDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
verify(machine, vBox, hardDisk);
assertNotSame(newDisk, hardDisk);
}
@Test(expectedExceptions = IllegalStateException.class)
public void testFailWhenMediumExistsAndNotUsingOverwrite() throws Exception {
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskFormat = "vdi";
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createMock(IMedium.class);
IMedium newHardDisk = createMock(IMedium.class);
IProgress progress = createNiceMock(IProgress.class);
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.findMedium(adminDiskPath, DeviceType.HardDisk)).andReturn(hardDisk);
replay(manager, machine, vBox, hardDisk, newHardDisk, progress);
new CreateMediumIfNotAlreadyExists(manager, diskFormat, false).apply(adminDiskPath);
}
@Test(expectedExceptions = VBoxException.class)
public void testFailOnOtherVBoxException() throws Exception {
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskFormat = "vdi";
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMedium hardDisk = createMock(IMedium.class);
IProgress progress = createNiceMock(IProgress.class);
String errorMessage = "VirtualBox error: Some other VBox error";
expect(manager.getVBox()).andReturn(vBox).anyTimes();
VBoxException notFoundException = new VBoxException(createNiceMock(Throwable.class), errorMessage);
expect(vBox.findMedium(adminDiskPath, DeviceType.HardDisk)).andThrow(notFoundException);
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
replay(manager, machine, vBox, hardDisk);
new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
}
}