mirror of https://github.com/apache/jclouds.git
Added AttachDistroMediumToMachine and started using it form IsoToIMachine.
This commit is contained in:
parent
f2d4c58a6a
commit
ad8ef18bee
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* *
|
||||||
|
* * 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.DeviceType;
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
import org.virtualbox_4_1.VBoxException;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Mattias Holmqvist
|
||||||
|
*/
|
||||||
|
public class AttachDistroMediumToMachine implements Function<IMachine, Void> {
|
||||||
|
|
||||||
|
private final String controllerIDE;
|
||||||
|
private final IMedium distroMedium;
|
||||||
|
|
||||||
|
public AttachDistroMediumToMachine(String controllerIDE, IMedium distroMedium) {
|
||||||
|
this.controllerIDE = controllerIDE;
|
||||||
|
this.distroMedium = distroMedium;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void apply(@Nullable IMachine machine) {
|
||||||
|
try {
|
||||||
|
int controllerPort = 0;
|
||||||
|
int device = 0;
|
||||||
|
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.DVD, distroMedium);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -133,31 +133,15 @@ public class IsoToIMachine implements Function<String, IMachine> {
|
||||||
// IDE Controller
|
// IDE Controller
|
||||||
ensureMachineHasIDEControllerNamed(vmName, controllerIDE);
|
ensureMachineHasIDEControllerNamed(vmName, controllerIDE);
|
||||||
|
|
||||||
|
// Distribution medium
|
||||||
|
ensureMachineHasAttachedDistroMedium(isoName, workingDir, controllerIDE);
|
||||||
|
|
||||||
// DISK
|
// DISK
|
||||||
String adminDiskPath = workingDir + "/" + adminDisk;
|
String adminDiskPath = workingDir + "/" + adminDisk;
|
||||||
if (new File(adminDiskPath).exists()) {
|
if (new File(adminDiskPath).exists()) {
|
||||||
new File(adminDiskPath).delete();
|
new File(adminDiskPath).delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
final IMedium distroMedium = manager.getVBox().openMedium(workingDir + "/" + isoName, DVD, ReadOnly,
|
|
||||||
forceOverwrite);
|
|
||||||
|
|
||||||
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void apply(IMachine machine) {
|
|
||||||
try {
|
|
||||||
machine.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
|
|
||||||
machine.saveSettings();
|
|
||||||
} catch (VBoxException e) {
|
|
||||||
if (e.getMessage().indexOf("is already attached to port") == -1)
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create and attach hard disk
|
// Create and attach hard disk
|
||||||
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
|
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
|
||||||
long size = 4L * 1024L * 1024L * 1024L - 4L;
|
long size = 4L * 1024L * 1024L * 1024L - 4L;
|
||||||
|
@ -246,6 +230,22 @@ public class IsoToIMachine implements Function<String, IMachine> {
|
||||||
return vm;
|
return vm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ensureMachineHasAttachedDistroMedium(String isoName, String workingDir, String controllerIDE) {
|
||||||
|
final String pathToIsoFile = checkFileExists(workingDir + "/" + isoName);
|
||||||
|
final IMedium distroMedium = manager.getVBox().openMedium(pathToIsoFile, DVD, ReadOnly, forceOverwrite);
|
||||||
|
lockMachineAndApply(manager, Write, vmName,
|
||||||
|
new AttachDistroMediumToMachine(
|
||||||
|
checkNotNull(controllerIDE, "controllerIDE"),
|
||||||
|
checkNotNull(distroMedium, "distroMedium")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String checkFileExists(String filePath) {
|
||||||
|
if (new File(filePath).exists()) {
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("File " + filePath + " does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
public void ensureMachineHasIDEControllerNamed(String vmName, String controllerIDE) {
|
public void ensureMachineHasIDEControllerNamed(String vmName, String controllerIDE) {
|
||||||
lockMachineAndApply(manager, Write, checkNotNull(vmName, "vmName"),
|
lockMachineAndApply(manager, Write, checkNotNull(vmName, "vmName"),
|
||||||
new AddIDEControllerIfNotExists(checkNotNull(controllerIDE, "controllerIDE")));
|
new AddIDEControllerIfNotExists(checkNotNull(controllerIDE, "controllerIDE")));
|
||||||
|
|
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* *
|
||||||
|
* * 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.DeviceType;
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
import org.virtualbox_4_1.VBoxException;
|
||||||
|
|
||||||
|
import static org.easymock.classextension.EasyMock.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Mattias Holmqvist
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit", testName = "AttachDistroMediumToMachineTest")
|
||||||
|
public class AttachDistroMediumToMachineTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAttachDistroMedium() throws Exception {
|
||||||
|
|
||||||
|
String controllerIDE = "IDE Controller";
|
||||||
|
IMedium distroMedium = createNiceMock(IMedium.class);
|
||||||
|
|
||||||
|
IMachine machine = createMock(IMachine.class);
|
||||||
|
|
||||||
|
machine.saveSettings();
|
||||||
|
machine.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
|
||||||
|
|
||||||
|
replay(machine, distroMedium);
|
||||||
|
|
||||||
|
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
|
||||||
|
|
||||||
|
verify(machine);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAcceptAlreadyAttachedDistroMedium() throws Exception {
|
||||||
|
|
||||||
|
String controllerIDE = "IDE Controller";
|
||||||
|
IMedium distroMedium = createNiceMock(IMedium.class);
|
||||||
|
|
||||||
|
IMachine machine = createNiceMock(IMachine.class);
|
||||||
|
|
||||||
|
final StringBuilder errorBuilder = new StringBuilder();
|
||||||
|
errorBuilder.append("VirtualBox error: ");
|
||||||
|
errorBuilder.append("Medium '/Users/johndoe/jclouds-virtualbox-test/ubuntu-11.04-server-i386.iso' ");
|
||||||
|
errorBuilder.append("is already attached to port 0, device 0 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, 0, 0, DeviceType.DVD, distroMedium);
|
||||||
|
expectLastCall().andThrow(isoAttachedException);
|
||||||
|
|
||||||
|
replay(machine, distroMedium);
|
||||||
|
|
||||||
|
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
|
||||||
|
|
||||||
|
verify(machine);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = VBoxException.class)
|
||||||
|
public void testFailOnOtherVBoxErrors() throws Exception {
|
||||||
|
|
||||||
|
String controllerIDE = "IDE Controller";
|
||||||
|
IMedium distroMedium = createNiceMock(IMedium.class);
|
||||||
|
|
||||||
|
IMachine machine = createNiceMock(IMachine.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, 0, 0, DeviceType.DVD, distroMedium);
|
||||||
|
expectLastCall().andThrow(isoAttachedException);
|
||||||
|
|
||||||
|
replay(machine, distroMedium);
|
||||||
|
|
||||||
|
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue