mirror of https://github.com/apache/jclouds.git
Continued work on the IsoToIMachine
This commit is contained in:
parent
842da34982
commit
5b4c2ab73b
|
@ -90,6 +90,12 @@
|
|||
<artifactId>jetty-security</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-server</artifactId>
|
||||
<version>7.4.0.RC0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jclouds</groupId>
|
||||
<artifactId>jclouds-core</artifactId>
|
||||
|
|
|
@ -22,17 +22,23 @@
|
|||
package org.jclouds.virtualbox.compute;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.collect.Iterables.filter;
|
||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
import org.jclouds.compute.ComputeServiceAdapter;
|
||||
import org.jclouds.compute.domain.Template;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.domain.Location;
|
||||
import org.jclouds.location.suppliers.JustProvider;
|
||||
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
||||
import org.virtualbox_4_1.CleanupMode;
|
||||
import org.virtualbox_4_1.IMachine;
|
||||
import org.virtualbox_4_1.IProgress;
|
||||
|
@ -78,10 +84,14 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
|
|||
|
||||
@Override
|
||||
public Iterable<IMachine> listImages() {
|
||||
for (IMachine iMachine : manager.getVBox().getMachines()) {
|
||||
System.out.println(iMachine.getName());
|
||||
}
|
||||
return manager.getVBox().getMachines();
|
||||
final Predicate<? super IMachine> imagePredicate = new Predicate<IMachine>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable IMachine iMachine) {
|
||||
return iMachine.getName().startsWith(VIRTUALBOX_IMAGE_PREFIX);
|
||||
}
|
||||
};
|
||||
return filter(manager.getVBox().getMachines(), imagePredicate);
|
||||
// return manager.getVBox().getMachines();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
@ -29,6 +29,8 @@ package org.jclouds.virtualbox.config;
|
|||
*/
|
||||
public interface VirtualBoxConstants {
|
||||
|
||||
public static final String VIRTUALBOX_IMAGE_PREFIX = "jclouds-image-";
|
||||
|
||||
public static final String VIRTUALBOX_PRESEED_URL = "jclouds.virtualbox.preseedurl";
|
||||
|
||||
public static final String VIRTUALBOX_SNAPSHOT_DESCRIPTION = "jclouds.virtualbox.snapshotDescription";
|
||||
|
|
|
@ -21,203 +21,290 @@
|
|||
|
||||
package org.jclouds.virtualbox.functions;
|
||||
|
||||
import static com.google.common.base.Throwables.propagate;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.inject.Inject;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.DefaultHandler;
|
||||
import org.eclipse.jetty.server.handler.HandlerList;
|
||||
import org.eclipse.jetty.server.handler.ResourceHandler;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.domain.ExecResponse;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.javax.annotation.Nullable;
|
||||
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
||||
import org.jclouds.virtualbox.experiment.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.MachineState;
|
||||
import org.virtualbox_4_1.NATProtocol;
|
||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||
import org.virtualbox_4_1.StorageBus;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.ssh.SshException;
|
||||
import org.jclouds.virtualbox.settings.KeyboardScancodes;
|
||||
import org.virtualbox_4_1.*;
|
||||
import org.virtualbox_4_1.jaxws.MediumVariant;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.inject.Inject;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Named;
|
||||
import java.io.File;
|
||||
|
||||
import static com.google.common.base.Throwables.propagate;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
|
||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||
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.LockType.Write;
|
||||
import static org.virtualbox_4_1.NATProtocol.TCP;
|
||||
import static org.virtualbox_4_1.NetworkAttachmentType.NAT;
|
||||
|
||||
public class IsoToIMachine implements Function<String, IMachine> {
|
||||
|
||||
private VirtualBoxManager manager;
|
||||
private String adminDisk;
|
||||
private String diskFormat;
|
||||
private String settingsFile;
|
||||
private String vmName;
|
||||
private String osTypeId;
|
||||
private String vmId;
|
||||
private String controllerIDE;
|
||||
private boolean forceOverwrite;
|
||||
private ComputeServiceContext context;
|
||||
private String hostId;
|
||||
private String guestId;
|
||||
@Resource
|
||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
@Inject
|
||||
public IsoToIMachine(VirtualBoxManager manager, String adminDisk,
|
||||
String diskFormat, String settingsFile, String vmName,
|
||||
String osTypeId, String vmId, boolean forceOverwrite,
|
||||
String controllerIDE, ComputeServiceContext context, String hostId,
|
||||
String guestId) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.adminDisk = adminDisk;
|
||||
this.diskFormat = diskFormat;
|
||||
this.settingsFile = settingsFile;
|
||||
this.vmName = vmName;
|
||||
this.osTypeId = osTypeId;
|
||||
this.vmId = vmId;
|
||||
this.controllerIDE = controllerIDE;
|
||||
this.forceOverwrite = forceOverwrite;
|
||||
this.context = context;
|
||||
this.hostId = hostId;
|
||||
this.guestId = guestId;
|
||||
}
|
||||
private VirtualBoxManager manager;
|
||||
private String adminDisk;
|
||||
private String diskFormat;
|
||||
private String settingsFile;
|
||||
private String vmName;
|
||||
private String osTypeId;
|
||||
private String vmId;
|
||||
private String controllerIDE;
|
||||
private boolean forceOverwrite;
|
||||
private ComputeServiceContext context;
|
||||
private String hostId;
|
||||
private String guestId;
|
||||
private Credentials credentials;
|
||||
|
||||
@Override
|
||||
public IMachine apply(@Nullable String isoName) {
|
||||
IMachine vm = manager.getVBox().createMachine(settingsFile, vmName,
|
||||
osTypeId, vmId, forceOverwrite);
|
||||
assertNotNull(vm.getName());
|
||||
@Inject
|
||||
public IsoToIMachine(VirtualBoxManager manager,
|
||||
String adminDisk,
|
||||
String diskFormat,
|
||||
String settingsFile,
|
||||
String vmName,
|
||||
String osTypeId,
|
||||
String vmId,
|
||||
boolean forceOverwrite,
|
||||
String controllerIDE,
|
||||
ComputeServiceContext context,
|
||||
String hostId,
|
||||
String guestId,
|
||||
Credentials credentials) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.adminDisk = adminDisk;
|
||||
this.diskFormat = diskFormat;
|
||||
this.settingsFile = settingsFile;
|
||||
this.vmName = vmName;
|
||||
this.osTypeId = osTypeId;
|
||||
this.vmId = vmId;
|
||||
this.controllerIDE = controllerIDE;
|
||||
this.forceOverwrite = forceOverwrite;
|
||||
this.context = context;
|
||||
this.hostId = hostId;
|
||||
this.guestId = guestId;
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
IMedium distroMedium = manager.getVBox().openMedium(
|
||||
VirtualBoxConstants.VIRTUALBOX_WORKINGDIR + "/" + isoName,
|
||||
DeviceType.DVD, AccessMode.ReadOnly, forceOverwrite);
|
||||
@Override
|
||||
public IMachine apply(@Nullable String isoName) {
|
||||
|
||||
ISession session = manager.getSessionObject();
|
||||
IMachine machine = manager.getVBox().findMachine(vmName);
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
IMachine mutable = session.getMachine();
|
||||
mutable.addStorageController(controllerIDE, StorageBus.IDE);
|
||||
mutable.saveSettings();
|
||||
Server server = configureJettyServer();
|
||||
try {
|
||||
logger.debug("Starting Jetty server for serving of preseed.cfg...");
|
||||
server.start();
|
||||
logger.debug("Jetty server started.");
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Could not start Jetty server");
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
// CONTROLLER
|
||||
mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
|
||||
mutable.saveSettings();
|
||||
IMachine vm = manager.getVBox().createMachine(settingsFile, vmName, osTypeId, vmId, forceOverwrite);
|
||||
manager.getVBox().registerMachine(vm);
|
||||
|
||||
// DISK
|
||||
IMedium hd = null;
|
||||
if (new File(adminDisk).exists()) {
|
||||
new File(adminDisk).delete();
|
||||
}
|
||||
hd = manager.getVBox().createHardDisk(diskFormat, adminDisk);
|
||||
long size = 4L * 1024L * 1024L * 1024L - 4L;
|
||||
hd.createBaseStorage(new Long(size),
|
||||
new Long(MediumVariant.STANDARD.ordinal()));
|
||||
mutable.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
|
||||
mutable.saveSettings();
|
||||
|
||||
// NIC
|
||||
mutable.getNetworkAdapter(new Long(0)).setAttachmentType(
|
||||
NetworkAttachmentType.NAT);
|
||||
|
||||
|
||||
machine.getNetworkAdapter(new Long(0)).getNatDriver().addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222, "", 22);
|
||||
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
|
||||
mutable.saveSettings();
|
||||
String defaultWorkingDir = System.getProperty("user.home") + "/jclouds-virtualbox-test";
|
||||
String workingDir = System.getProperty(VIRTUALBOX_WORKINGDIR, defaultWorkingDir);
|
||||
IMedium distroMedium = manager.getVBox().openMedium(workingDir + "/" + isoName, DVD, ReadOnly, forceOverwrite);
|
||||
|
||||
launchVMProcess(machine, session);
|
||||
assertEquals(machine.getState(), MachineState.Running);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
propagate(e);
|
||||
}
|
||||
// Change RAM
|
||||
Long memorySize = new Long(1024);
|
||||
ISession session = manager.getSessionObject();
|
||||
IMachine machine = manager.getVBox().findMachine(vmName);
|
||||
machine.lockMachine(session, Write);
|
||||
IMachine mutable = session.getMachine();
|
||||
mutable.setMemorySize(memorySize);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
try {
|
||||
sendKeyboardSequence(VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE);
|
||||
} catch (InterruptedException e) {
|
||||
propagate(e);
|
||||
}
|
||||
// IDE Controller
|
||||
machine.lockMachine(session, Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.addStorageController(controllerIDE, StorageBus.IDE);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
session.unlockMachine();
|
||||
// DISK
|
||||
String adminDiskPath = workingDir + "/" + adminDisk;
|
||||
if (new File(adminDiskPath).exists()) {
|
||||
new File(adminDiskPath).delete();
|
||||
}
|
||||
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);
|
||||
|
||||
return vm;
|
||||
}
|
||||
machine.lockMachine(session, Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
private void launchVMProcess(IMachine machine, ISession session) {
|
||||
IProgress prog = machine.launchVMProcess(session, "gui", "");
|
||||
prog.waitForCompletion(-1);
|
||||
session.unlockMachine();
|
||||
}
|
||||
// Create and attach hard disk
|
||||
machine.lockMachine(session, Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
private void sendKeyboardSequence(String keyboardSequence)
|
||||
throws InterruptedException {
|
||||
String[] sequenceSplited = keyboardSequence.split(" ");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line : sequenceSplited) {
|
||||
String converted = stringToKeycode(line);
|
||||
for (String word : converted.split(" ")) {
|
||||
sb.append("vboxmanage controlvm " + vmName
|
||||
+ " keyboardputscancode " + word + "; ");
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<Enter>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
|
||||
.wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<Return>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
|
||||
.wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
// NIC
|
||||
machine.lockMachine(session, Write);
|
||||
mutable = session.getMachine();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// NAT
|
||||
mutable.getNetworkAdapter(0l).setAttachmentType(NAT);
|
||||
machine.getNetworkAdapter(0l)
|
||||
.getNatDriver()
|
||||
.addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22);
|
||||
mutable.getNetworkAdapter(0l).setEnabled(true);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
private String stringToKeycode(String s) {
|
||||
StringBuilder keycodes = new StringBuilder();
|
||||
if (s.startsWith("<")) {
|
||||
String[] specials = s.split("<");
|
||||
for (int i = 1; i < specials.length; i++) {
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<" + specials[i]) + " ");
|
||||
}
|
||||
return keycodes.toString();
|
||||
}
|
||||
String guestAdditionsDvd = workingDir + "/VBoxGuestAdditions_4.1.2.iso";
|
||||
IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD, AccessMode.ReadOnly, forceOverwrite);
|
||||
machine.lockMachine(session, Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
int i = 0;
|
||||
while (i < s.length()) {
|
||||
String digit = s.substring(i, i + 1);
|
||||
String hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP
|
||||
.get(digit);
|
||||
keycodes.append(hex + " ");
|
||||
if (i != 0 && i % 14 == 0)
|
||||
keycodes.append(" ");
|
||||
i++;
|
||||
}
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<Spacebar>") + " ");
|
||||
IProgress prog = machine.launchVMProcess(session, "gui", "");
|
||||
prog.waitForCompletion(-1);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
propagate(e);
|
||||
}
|
||||
|
||||
return keycodes.toString();
|
||||
}
|
||||
String installKeySequence = System.getProperty(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE, defaultInstallSequence());
|
||||
sendKeyboardSequence(installKeySequence);
|
||||
session.unlockMachine();
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command,
|
||||
RunScriptOptions options) {
|
||||
ExecResponse toReturn = context.getComputeService().runScriptOnNode(
|
||||
nodeId, command, options);
|
||||
assert toReturn.getExitCode() == 0 : toReturn;
|
||||
return toReturn;
|
||||
}
|
||||
boolean sshDeamonIsRunning = false;
|
||||
while (!sshDeamonIsRunning) {
|
||||
try {
|
||||
if (runScriptOnNode(guestId, "id", wrapInInitScript(false)).getExitCode() == 0) {
|
||||
logger.debug("Got response from ssh daemon.");
|
||||
sshDeamonIsRunning = true;
|
||||
}
|
||||
} catch (SshException e) {
|
||||
logger.debug("No response from ssh daemon...");
|
||||
}
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command) {
|
||||
return runScriptOnNode(nodeId, command, wrapInInitScript(false));
|
||||
}
|
||||
logger.debug("Installation of image complete. Powering down...");
|
||||
|
||||
machine.lockMachine(session, LockType.Shared);
|
||||
IProgress powerDownProgress = session.getConsole().powerDown();
|
||||
powerDownProgress.waitForCompletion(-1);
|
||||
session.unlockMachine();
|
||||
|
||||
try {
|
||||
logger.debug("Stopping Jetty server...");
|
||||
server.stop();
|
||||
logger.debug("Jetty server stopped.");
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Could not stop Jetty server.");
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
private Server configureJettyServer() {
|
||||
Server server = new Server(8080);
|
||||
|
||||
ResourceHandler resource_handler = new ResourceHandler();
|
||||
resource_handler.setDirectoriesListed(true);
|
||||
resource_handler.setWelcomeFiles(new String[]{"index.html"});
|
||||
|
||||
resource_handler.setResourceBase(".");
|
||||
logger.info("serving " + resource_handler.getBaseResource());
|
||||
|
||||
HandlerList handlers = new HandlerList();
|
||||
handlers.setHandlers(new Handler[]{resource_handler, new DefaultHandler()});
|
||||
server.setHandler(handlers);
|
||||
return server;
|
||||
}
|
||||
|
||||
private String defaultInstallSequence() {
|
||||
return "<Esc><Esc><Enter> "
|
||||
+ "/install/vmlinuz noapic preseed/url=http://10.0.2.2:8080/src/test/resources/preseed.cfg "
|
||||
+ "debian-installer=en_US auto locale=en_US kbd-chooser/method=us "
|
||||
+ "hostname="
|
||||
+ vmName
|
||||
+ " "
|
||||
+ "fb=false debconf/frontend=noninteractive "
|
||||
+ "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false "
|
||||
+ "initrd=/install/initrd.gz -- <Enter>";
|
||||
}
|
||||
|
||||
private void sendKeyboardSequence(String keyboardSequence) {
|
||||
String[] sequenceSplited = keyboardSequence.split(" ");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line : sequenceSplited) {
|
||||
String converted = stringToKeycode(line);
|
||||
for (String word : converted.split(" ")) {
|
||||
sb.append("vboxmanage controlvm " + vmName
|
||||
+ " keyboardputscancode " + word + "; ");
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Enter>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false).wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Return>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false).wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String stringToKeycode(String s) {
|
||||
StringBuilder keycodes = new StringBuilder();
|
||||
if (s.startsWith("<")) {
|
||||
String[] specials = s.split("<");
|
||||
for (int i = 1; i < specials.length; i++) {
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<" + specials[i]) + " ");
|
||||
}
|
||||
return keycodes.toString();
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < s.length()) {
|
||||
String digit = s.substring(i, i + 1);
|
||||
String hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP
|
||||
.get(digit);
|
||||
keycodes.append(hex + " ");
|
||||
if (i != 0 && i % 14 == 0)
|
||||
keycodes.append(" ");
|
||||
i++;
|
||||
}
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<Spacebar>") + " ");
|
||||
|
||||
return keycodes.toString();
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command, RunScriptOptions options) {
|
||||
return context.getComputeService().runScriptOnNode(nodeId, command, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jclouds.virtualbox.experiment.settings;
|
||||
package org.jclouds.virtualbox.settings;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
|
@ -1,529 +0,0 @@
|
|||
/*
|
||||
* *
|
||||
* * 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.experiment;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.Closeables;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.DefaultHandler;
|
||||
import org.eclipse.jetty.server.handler.HandlerList;
|
||||
import org.eclipse.jetty.server.handler.ResourceHandler;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.domain.ExecResponse;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.net.IPSocket;
|
||||
import org.jclouds.predicates.InetSocketAddressConnect;
|
||||
import org.jclouds.predicates.RetryablePredicate;
|
||||
import org.jclouds.scriptbuilder.domain.OsFamily;
|
||||
import org.jclouds.scriptbuilder.statements.login.AdminAccess;
|
||||
import org.jclouds.scriptbuilder.statements.login.DefaultConfiguration;
|
||||
import org.jclouds.ssh.SshException;
|
||||
import org.jclouds.virtualbox.experiment.settings.KeyboardScancodes;
|
||||
import org.jclouds.virtualbox.functions.IMachineToNodeMetadata;
|
||||
import org.virtualbox_4_1.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Throwables.propagate;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
|
||||
|
||||
public class KickstartTest2 {
|
||||
|
||||
|
||||
protected String provider = "virtualbox";
|
||||
protected String identity;
|
||||
protected String credential;
|
||||
protected URI endpoint;
|
||||
protected String apiVersion;
|
||||
protected String vmName;
|
||||
|
||||
protected Predicate<IPSocket> socketTester;
|
||||
|
||||
protected String settingsFile; // Fully qualified path where the settings
|
||||
protected String osTypeId; // Guest OS Type ID.
|
||||
protected String vmId; // Machine UUID (optional).
|
||||
protected boolean forceOverwrite;
|
||||
protected String diskFormat;
|
||||
|
||||
protected String workingDir;
|
||||
protected String adminDisk;
|
||||
|
||||
protected String guestAdditionsDvd;
|
||||
private URI gaIsoUrl;
|
||||
|
||||
private String gaIsoName;
|
||||
private URI distroIsoUrl;
|
||||
private String distroIsoName;
|
||||
private String controllerIDE;
|
||||
private String keyboardSequence;
|
||||
|
||||
private ComputeServiceContext context;
|
||||
private String hostId = "host";
|
||||
private String guestId = "guest";
|
||||
private String majorVersion;
|
||||
private URI vboxDmg;
|
||||
private String vboxVersionName;
|
||||
private String snapshotDescription;
|
||||
|
||||
protected void setupCredentials() {
|
||||
identity = System.getProperty("test." + provider + ".identity", "administrator");
|
||||
credential = System.getProperty("test." + provider + ".credential", "12345");
|
||||
endpoint = URI.create(System.getProperty("test." + provider + ".endpoint", "http://localhost:18083/"));
|
||||
apiVersion = System.getProperty("test." + provider + ".apiversion", "4.1.2r73507");
|
||||
majorVersion = Iterables.get(Splitter.on('r').split(apiVersion), 0);
|
||||
}
|
||||
|
||||
protected Logger logger() {
|
||||
return context.utils().loggerFactory().getLogger("jclouds.compute");
|
||||
}
|
||||
|
||||
protected void setupConfigurationProperties() {
|
||||
|
||||
controllerIDE = System.getProperty("test." + provider + ".controllerIde", "IDE Controller");
|
||||
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
|
||||
|
||||
// VBOX
|
||||
settingsFile = null;
|
||||
osTypeId = System.getProperty("test." + provider + ".osTypeId", "");
|
||||
vmId = System.getProperty("test." + provider + ".vmId", null);
|
||||
forceOverwrite = true;
|
||||
vmName = System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-kickstart-admin");
|
||||
|
||||
workingDir = System.getProperty("user.home")
|
||||
+ File.separator
|
||||
+ System.getProperty("test." + provider + ".workingDir",
|
||||
"jclouds-virtualbox-test");
|
||||
|
||||
gaIsoName = System.getProperty("test." + provider + ".gaIsoName", "VBoxGuestAdditions_" + majorVersion + ".iso");
|
||||
gaIsoUrl = URI.create(System.getProperty("test." + provider + ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/" + majorVersion + "/" + gaIsoName));
|
||||
|
||||
distroIsoName = System.getProperty("test." + provider + ".distroIsoName", "ubuntu-11.04-server-i386.iso");
|
||||
distroIsoUrl = URI
|
||||
.create(System
|
||||
.getProperty("test." + provider + ".distroIsoUrl",
|
||||
"http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso"));
|
||||
vboxDmg = URI.create(System.getProperty("test." + provider + ".vboxDmg", "http://download.virtualbox.org/virtualbox/4.1.2/VirtualBox-4.1.2-73507-OSX.dmg"));
|
||||
vboxVersionName = System.getProperty("test" + provider + ".vboxVersionName", "VirtualBox-4.1.2-73507-OSX.dmg");
|
||||
|
||||
adminDisk = workingDir
|
||||
+ File.separator
|
||||
+ System.getProperty("test." + provider + ".adminDisk",
|
||||
"admin.vdi");
|
||||
guestAdditionsDvd = workingDir
|
||||
+ File.separator
|
||||
+ System.getProperty("test." + provider + ".guestAdditionsDvd",
|
||||
"VBoxGuestAdditions_" + majorVersion + ".iso");
|
||||
|
||||
snapshotDescription = System.getProperty("test." + provider + "snapshotdescription", "jclouds-virtualbox-snaphot");
|
||||
|
||||
keyboardSequence = System
|
||||
.getProperty(
|
||||
"test." + provider + ".keyboardSequence",
|
||||
"<Esc><Esc><Enter> "
|
||||
+ "/install/vmlinuz noapic preseed/url=http://10.0.2.2:8080/src/test/resources/preseed.cfg "
|
||||
+ "debian-installer=en_US auto locale=en_US kbd-chooser/method=us "
|
||||
+ "hostname="
|
||||
+ vmName
|
||||
+ " "
|
||||
+ "fb=false debconf/frontend=noninteractive "
|
||||
+ "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false "
|
||||
+ "initrd=/install/initrd.gz -- <Enter>");
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
KickstartTest2 test = new KickstartTest2();
|
||||
|
||||
try {
|
||||
test.runAll();
|
||||
System.out.println("over");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void runAll() throws Exception {
|
||||
context = TestUtils.computeServiceForLocalhostAndGuest();
|
||||
socketTester = new RetryablePredicate<IPSocket>(
|
||||
new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS);
|
||||
|
||||
setupCredentials();
|
||||
setupConfigurationProperties();
|
||||
|
||||
downloadFileUnlessPresent(distroIsoUrl, workingDir, distroIsoName);
|
||||
downloadFileUnlessPresent(gaIsoUrl, workingDir, gaIsoName);
|
||||
|
||||
installVbox();
|
||||
configureJettyServer();
|
||||
|
||||
VirtualBoxManager manager = VirtualBoxManager.createInstance("mach");
|
||||
manager.connect(endpoint.toASCIIString(), identity, credential);
|
||||
|
||||
// Create machine
|
||||
IMachine newVM = manager.getVBox().createMachine(settingsFile, vmName, osTypeId, "host", forceOverwrite);
|
||||
manager.getVBox().registerMachine(newVM);
|
||||
|
||||
// Change RAM
|
||||
Long memorySize = new Long(1024);
|
||||
ISession session = manager.getSessionObject();
|
||||
IMachine machine = manager.getVBox().findMachine(vmName);
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
IMachine mutable = session.getMachine();
|
||||
mutable.setMemorySize(memorySize);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
logMachineStatus(machine);
|
||||
|
||||
// Create IDE Controller
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.addStorageController(controllerIDE, StorageBus.IDE);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
IMedium hd = null;
|
||||
if (!new File(adminDisk).exists()) {
|
||||
hd = manager.getVBox().createHardDisk(diskFormat, adminDisk);
|
||||
long size = 4L * 1024L * 1024L * 1024L - 4L;
|
||||
IProgress progress = hd.createBaseStorage(new Long(size), new Long(
|
||||
org.virtualbox_4_1.jaxws.MediumVariant.STANDARD.ordinal()));
|
||||
System.out.println(progress);
|
||||
} else {
|
||||
// TODO disk already exist: open it
|
||||
}
|
||||
|
||||
logMachineStatus(machine);
|
||||
|
||||
// Attach ISO DVD
|
||||
IMedium distroMedium = manager.getVBox().openMedium(
|
||||
workingDir + "/" + distroIsoName, DeviceType.DVD,
|
||||
AccessMode.ReadOnly, forceOverwrite);
|
||||
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
logger().debug("Setting up hard drive...");
|
||||
// Create and attach hard disk
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
logMachineStatus(machine);
|
||||
|
||||
// Configure NIC
|
||||
// NAT
|
||||
logger().debug("Configuring NIC...");
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.getNetworkAdapter(new Long(0)).setAttachmentType(
|
||||
NetworkAttachmentType.NAT);
|
||||
mutable.getNetworkAdapter(new Long(0))
|
||||
.getNatDriver()
|
||||
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222,
|
||||
"", 22);
|
||||
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
// Attach guest additions
|
||||
logger().debug("Attaching guest additions medium...");
|
||||
distroMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD, AccessMode.ReadOnly, forceOverwrite);
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
mutable.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, distroMedium);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
// Start virtual machine
|
||||
logMachineStatus(machine);
|
||||
logger().debug("Starting virtual machine...");
|
||||
IProgress prog = machine.launchVMProcess(session, "gui", "");
|
||||
prog.waitForCompletion(-1);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
propagate(e);
|
||||
}
|
||||
|
||||
logMachineStatus(machine);
|
||||
sendKeyboardSequence(keyboardSequence);
|
||||
|
||||
// test if the sshd on the guest is ready and meanwhile apply AdminAccess
|
||||
boolean sshDeamonIsRunning = false;
|
||||
while (!sshDeamonIsRunning) {
|
||||
try {
|
||||
AdminAccess.standard().init(new DefaultConfiguration()).render(OsFamily.UNIX);
|
||||
if (runScriptOnNode(guestId, "id").getExitCode() == 0)
|
||||
sshDeamonIsRunning = true;
|
||||
} catch (SshException e) {
|
||||
System.err.println("connection reset");
|
||||
}
|
||||
}
|
||||
|
||||
logMachineStatus(machine);
|
||||
logger().debug("Configuring guest additions...");
|
||||
|
||||
// Configure guest additions
|
||||
// TODO generalize
|
||||
if (isUbuntu(guestId)) {
|
||||
runScriptOnNode(guestId, "rm /etc/udev/rules.d/70-persistent-net.rules");
|
||||
runScriptOnNode(guestId, "mkdir /etc/udev/rules.d/70-persistent-net.rules");
|
||||
runScriptOnNode(guestId, "rm -rf /dev/.udev/");
|
||||
runScriptOnNode(guestId, "rm /lib/udev/rules.d/75-persistent-net-generator.rules");
|
||||
runScriptOnNode(guestId, "echo 0 | tee /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts");
|
||||
}
|
||||
|
||||
logMachineStatus(machine);
|
||||
logger().debug("Powering down...");
|
||||
|
||||
machine = manager.getVBox().findMachine(vmName);
|
||||
try {
|
||||
session = manager.getSessionObject();
|
||||
IProgress progress = session.getConsole().powerDown();
|
||||
progress.waitForCompletion(-1);
|
||||
session.unlockMachine();
|
||||
|
||||
while (!machine.getSessionState().equals(SessionState.Unlocked)) {
|
||||
try {
|
||||
System.out
|
||||
.println("waiting for unlocking session - session state: "
|
||||
+ machine.getSessionState());
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
logMachineStatus(machine);
|
||||
logger().debug("Changing to bridged networking...");
|
||||
|
||||
session = manager.getSessionObject();
|
||||
IMachine adminNode = manager.getVBox().findMachine(vmName);
|
||||
adminNode.lockMachine(session, LockType.Write);
|
||||
mutable = session.getMachine();
|
||||
// network
|
||||
String hostInterface = null;
|
||||
String command = "vboxmanage list bridgedifs";
|
||||
try {
|
||||
Process child = Runtime.getRuntime().exec(command);
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(child.getInputStream()));
|
||||
String line = "";
|
||||
boolean found = false;
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null && !found) {
|
||||
if (line.split(":")[0].contains("Name")) {
|
||||
hostInterface = line.split(":")[1];
|
||||
}
|
||||
if (line.split(":")[0].contains("Status") && line.split(":")[1].contains("Up")) {
|
||||
System.out.println("bridge: " + hostInterface.trim());
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mutable.getNetworkAdapter(new Long(0)).setAttachmentType(NetworkAttachmentType.Bridged);
|
||||
mutable.getNetworkAdapter(new Long(0)).setAdapterType(NetworkAdapterType.Am79C973);
|
||||
mutable.getNetworkAdapter(new Long(0)).setMACAddress(manager.getVBox().getHost().generateMACAddress());
|
||||
mutable.getNetworkAdapter(new Long(0)).setBridgedInterface(hostInterface.trim());
|
||||
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
logMachineStatus(machine);
|
||||
logger().debug("Taking snapshot of machine...");
|
||||
session = manager.getSessionObject();
|
||||
machine.lockMachine(session, LockType.Write);
|
||||
if (machine.getCurrentSnapshot() == null || !machine.getCurrentSnapshot().getDescription().equals(snapshotDescription)) {
|
||||
manager.getSessionObject().getConsole().takeSnapshot(machine.getId(), snapshotDescription);
|
||||
}
|
||||
session.unlockMachine();
|
||||
|
||||
logMachineStatus(machine);
|
||||
manager.disconnect();
|
||||
manager.cleanup();
|
||||
|
||||
}
|
||||
|
||||
private void logMachineStatus(IMachine machine) {
|
||||
NodeMetadata node = new IMachineToNodeMetadata().apply(machine);
|
||||
logger().debug("Machine status: " + node.toString());
|
||||
}
|
||||
|
||||
void installVbox() throws Exception {
|
||||
if (runScriptOnNode(hostId, "VBoxManage --version", runAsRoot(false).wrapInInitScript(false)).getExitCode() != 0) {
|
||||
logger().debug("installing virtualbox");
|
||||
if (isOSX(hostId)) {
|
||||
downloadFileUnlessPresent(vboxDmg, workingDir, vboxVersionName);
|
||||
runScriptOnNode(hostId, "hdiutil attach " + workingDir + "/" + vboxVersionName);
|
||||
runScriptOnNode(hostId, "installer -pkg /Volumes/VirtualBox/VirtualBox.mpkg -target /Volumes/Macintosh\\ HD");
|
||||
} else {
|
||||
// TODO other platforms
|
||||
runScriptOnNode(hostId, "cat > /etc/apt/sources.list.d/TODO");
|
||||
runScriptOnNode(
|
||||
hostId,
|
||||
"wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | apt-key add -");
|
||||
runScriptOnNode(hostId, "apt-get update");
|
||||
runScriptOnNode(hostId, "apt-get --yes install virtualbox-4.1");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Server configureJettyServer() throws Exception {
|
||||
Server server = new Server(8080);
|
||||
|
||||
ResourceHandler resource_handler = new ResourceHandler();
|
||||
resource_handler.setDirectoriesListed(true);
|
||||
resource_handler.setWelcomeFiles(new String[]{"index.html"});
|
||||
|
||||
resource_handler.setResourceBase(".");
|
||||
logger().info("serving " + resource_handler.getBaseResource());
|
||||
|
||||
HandlerList handlers = new HandlerList();
|
||||
handlers.setHandlers(new Handler[]{resource_handler,
|
||||
new DefaultHandler()});
|
||||
server.setHandler(handlers);
|
||||
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
protected boolean isOSX(String id) {
|
||||
return context.getComputeService().getNodeMetadata(hostId)
|
||||
.getOperatingSystem().getDescription().equals("Mac OS X");
|
||||
}
|
||||
|
||||
protected boolean isUbuntu(String id) {
|
||||
return context.getComputeService().getNodeMetadata(id)
|
||||
.getOperatingSystem().getDescription().contains("ubuntu");
|
||||
}
|
||||
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command,
|
||||
RunScriptOptions options) {
|
||||
ExecResponse toReturn = context.getComputeService().runScriptOnNode(
|
||||
nodeId, command, options);
|
||||
assert toReturn.getExitCode() == 0 : toReturn;
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command) {
|
||||
return runScriptOnNode(nodeId, command, wrapInInitScript(false));
|
||||
}
|
||||
|
||||
private File downloadFileUnlessPresent(URI sourceURL,
|
||||
String destinationDir, String filename) throws Exception {
|
||||
|
||||
File iso = new File(destinationDir, filename);
|
||||
|
||||
if (!iso.exists()) {
|
||||
InputStream is = context.utils().http().get(sourceURL);
|
||||
checkNotNull(is, "%s not found", sourceURL);
|
||||
try {
|
||||
ByteStreams.copy(is, new FileOutputStream(iso));
|
||||
} finally {
|
||||
Closeables.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
return iso;
|
||||
}
|
||||
|
||||
private void sendKeyboardSequence(String keyboardSequence)
|
||||
throws InterruptedException {
|
||||
String[] sequenceSplited = keyboardSequence.split(" ");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line : sequenceSplited) {
|
||||
String converted = stringToKeycode(line);
|
||||
for (String word : converted.split(" ")) {
|
||||
sb.append("vboxmanage controlvm " + vmName
|
||||
+ " keyboardputscancode " + word + "; ");
|
||||
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Enter>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
|
||||
.wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
|
||||
|
||||
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Return>"))) {
|
||||
runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
|
||||
.wrapInInitScript(false));
|
||||
sb.delete(0, sb.length() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String stringToKeycode(String s) {
|
||||
StringBuilder keycodes = new StringBuilder();
|
||||
if (s.startsWith("<")) {
|
||||
String[] specials = s.split("<");
|
||||
for (int i = 1; i < specials.length; i++) {
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<" + specials[i]) + " ");
|
||||
}
|
||||
return keycodes.toString();
|
||||
}
|
||||
|
||||
|
||||
int i = 0;
|
||||
while (i < s.length()) {
|
||||
String digit = s.substring(i, i + 1);
|
||||
String hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP
|
||||
.get(digit);
|
||||
keycodes.append(hex + " ");
|
||||
if (i != 0 && i % 14 == 0)
|
||||
keycodes.append(" ");
|
||||
i++;
|
||||
}
|
||||
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
|
||||
.get("<Spacebar>") + " ");
|
||||
|
||||
return keycodes.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -18,47 +18,27 @@
|
|||
*/
|
||||
package org.jclouds.virtualbox.experiment;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.Module;
|
||||
import org.jclouds.byon.Node;
|
||||
import org.jclouds.byon.config.CacheNodeStoreModule;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.ComputeServiceContextFactory;
|
||||
import org.jclouds.compute.domain.OsFamily;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.encryption.bouncycastle.config.BouncyCastleCryptoModule;
|
||||
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
|
||||
import org.jclouds.sshj.config.SshjSshClientModule;
|
||||
import org.jclouds.virtualbox.compute.LoadMachineFromVirtualBox;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.Module;
|
||||
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestUtils {
|
||||
public static ComputeServiceContext computeServiceForLocalhost() throws IOException {
|
||||
|
||||
String hostId = System.getProperty(VirtualBoxConstants.VIRTUALBOX_HOST_ID);
|
||||
Node host = Node.builder().id(hostId)
|
||||
.name(System.getProperty(VirtualBoxConstants.VIRTUALBOX_HOST_ID))
|
||||
.hostname(System.getProperty(VirtualBoxConstants.VIRTUALBOX_HOSTNAME))
|
||||
.osFamily(OsFamily.LINUX.toString())
|
||||
.osDescription(System.getProperty("os.name"))
|
||||
.osVersion(System.getProperty("os.version"))
|
||||
.group("ssh")
|
||||
.username(System.getProperty("user.name"))
|
||||
.credentialUrl(privateKeyFile())
|
||||
.build();
|
||||
|
||||
final Map<String, Node> nodeMap = ImmutableMap.<String, Node>builder().put(hostId, host).build();
|
||||
return new ComputeServiceContextFactory().createContext("byon", "foo", "bar", ImmutableSet.<Module>of(
|
||||
new SshjSshClientModule(), new SLF4JLoggingModule(), new BouncyCastleCryptoModule(), new CacheNodeStoreModule(nodeMap)));
|
||||
}
|
||||
|
||||
public static ComputeServiceContext computeServiceForLocalhostAndGuest() throws IOException {
|
||||
|
||||
|
@ -90,6 +70,38 @@ public class TestUtils {
|
|||
new SshjSshClientModule(), new SLF4JLoggingModule(), new BouncyCastleCryptoModule(), new CacheNodeStoreModule(nodeMap)));
|
||||
}
|
||||
|
||||
public static ComputeServiceContext computeServiceForLocalhostAndGuest(String hostId, String hostname, String guestId, String guestHostname,
|
||||
Credentials guestLogin) throws IOException {
|
||||
|
||||
Node host = Node.builder().id(hostId)
|
||||
.name("host installing virtualbox")
|
||||
.hostname(hostname)
|
||||
.osFamily(OsFamily.LINUX.toString())
|
||||
.osDescription(System.getProperty("os.name"))
|
||||
.osVersion(System.getProperty("os.version"))
|
||||
.group("ssh")
|
||||
.username(System.getProperty("user.name"))
|
||||
.credentialUrl(privateKeyFile())
|
||||
.build();
|
||||
Node guest = Node.builder().id(guestId)
|
||||
.name("new guest")
|
||||
.hostname(guestHostname)
|
||||
.loginPort(2222)
|
||||
.osFamily(OsFamily.UBUNTU.toString())
|
||||
.osDescription("ubuntu/11.04")
|
||||
.osVersion(System.getProperty("11.04"))
|
||||
.group("jclouds-linux-image")
|
||||
.username(guestLogin.identity)
|
||||
.sudoPassword(guestLogin.credential)
|
||||
.credential(guestLogin.credential)
|
||||
.build();
|
||||
|
||||
|
||||
final Map<String, Node> nodeMap = ImmutableMap.<String, Node>builder().put(hostId, host).put(guestId, guest).build();
|
||||
return new ComputeServiceContextFactory().createContext("byon", "foo", "bar", ImmutableSet.<Module>of(
|
||||
new SshjSshClientModule(), new SLF4JLoggingModule(), new BouncyCastleCryptoModule(), new CacheNodeStoreModule(nodeMap)));
|
||||
}
|
||||
|
||||
private static URI privateKeyFile() {
|
||||
try {
|
||||
return new URI("file://" + System.getProperty("user.home") + "/.ssh/id_rsa");
|
||||
|
|
|
@ -1,312 +0,0 @@
|
|||
/*
|
||||
* *
|
||||
* * 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.experiment;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Module;
|
||||
import org.jclouds.byon.Node;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.compute.domain.ExecResponse;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
|
||||
import org.jclouds.net.IPSocket;
|
||||
import org.jclouds.predicates.InetSocketAddressConnect;
|
||||
import org.jclouds.predicates.RetryablePredicate;
|
||||
import org.jclouds.rest.RestContextFactory;
|
||||
import org.jclouds.ssh.SshClient;
|
||||
import org.jclouds.sshj.config.SshjSshClientModule;
|
||||
import org.jclouds.virtualbox.compute.LoadMachineFromVirtualBox;
|
||||
import org.jclouds.virtualbox.functions.IMachineToIpAddress;
|
||||
import org.jclouds.virtualbox.functions.IMachineToNodeMetadata;
|
||||
import org.virtualbox_4_1.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
|
||||
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhost;
|
||||
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForVirtualBox;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
public class VirtualBoxLiveTest2 {
|
||||
|
||||
|
||||
protected String provider = "virtualbox";
|
||||
protected String identity;
|
||||
protected String credential;
|
||||
protected URI endpoint;
|
||||
protected String vmName;
|
||||
private String hostId = "host";
|
||||
private String guestId = "guest";
|
||||
|
||||
VirtualBoxManager manager = VirtualBoxManager.createInstance("");
|
||||
|
||||
protected Predicate<IPSocket> socketTester;
|
||||
protected SshClient.Factory sshFactory;
|
||||
|
||||
protected String osUsername;
|
||||
protected String osPassword;
|
||||
protected String controller;
|
||||
protected String diskFormat;
|
||||
|
||||
protected String settingsFile;
|
||||
protected String osTypeId;
|
||||
protected String vmId;
|
||||
protected boolean forceOverwrite;
|
||||
protected String workingDir;
|
||||
protected String clonedDiskPath;
|
||||
protected int numberOfVirtualMachine;
|
||||
protected String originalDisk;
|
||||
private String clonedDisk;
|
||||
private ComputeServiceContext virtualBoxComputeService;
|
||||
|
||||
private String adminNodeName;
|
||||
private Injector injector;
|
||||
private Cache<String, Node> cache;
|
||||
private ComputeServiceContext localhostComputeService;
|
||||
|
||||
protected Logger logger() {
|
||||
return virtualBoxComputeService.utils().loggerFactory().getLogger("jclouds.compute");
|
||||
}
|
||||
|
||||
protected void setupCredentials() {
|
||||
identity = System.getProperty("test." + provider + ".identity",
|
||||
"administrator");
|
||||
credential = System.getProperty("test." + provider + ".credential",
|
||||
"12345");
|
||||
endpoint = URI.create(System.getProperty("test." + provider
|
||||
+ ".endpoint", "http://localhost:18083/"));
|
||||
}
|
||||
|
||||
protected void setupConfigurationProperties() {
|
||||
// VBOX
|
||||
settingsFile = null;
|
||||
osTypeId = System.getProperty("test." + provider + ".osTypeId", "");
|
||||
vmId = System.getProperty("test." + provider + ".vmId", null);
|
||||
forceOverwrite = true;
|
||||
// OS specific information
|
||||
adminNodeName = System.getProperty("test." + provider + ".adminnodename", "jclouds-virtualbox-kickstart-admin");
|
||||
vmName = checkNotNull(System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-node"));
|
||||
osUsername = System.getProperty("test." + provider + ".osusername", "toor");
|
||||
osPassword = System.getProperty("test." + provider + ".ospassword", "password");
|
||||
controller = System.getProperty("test." + provider + ".controller", "IDE Controller");
|
||||
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
|
||||
|
||||
workingDir = System.getProperty("user.home")
|
||||
+ File.separator
|
||||
+ System.getProperty("test." + provider + ".workingDir",
|
||||
"jclouds-virtualbox-test");
|
||||
|
||||
originalDisk = System.getProperty("test." + provider + ".originalDisk", "admin.vdi");
|
||||
|
||||
clonedDisk = System.getProperty("test." + provider + ".clonedDisk", "clone.vdi");
|
||||
clonedDiskPath = workingDir + File.separator + clonedDisk;
|
||||
numberOfVirtualMachine = Integer.parseInt(checkNotNull(System.getProperty("test." + provider
|
||||
+ ".numberOfVirtualMachine", "3")));
|
||||
injector = new RestContextFactory().createContextBuilder(provider,
|
||||
ImmutableSet.<Module>of(new Log4JLoggingModule(), new SshjSshClientModule())).buildInjector();
|
||||
|
||||
sshFactory = injector.getInstance(SshClient.Factory.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
VirtualBoxLiveTest2 virtualBoxLiveTest2 = new VirtualBoxLiveTest2();
|
||||
try {
|
||||
virtualBoxLiveTest2.runAll();
|
||||
} catch (IOException e) {
|
||||
virtualBoxLiveTest2.logger().error(e, "error", null);
|
||||
} catch (InterruptedException e) {
|
||||
virtualBoxLiveTest2.logger().error(e, "error", null);
|
||||
}
|
||||
}
|
||||
|
||||
private void runAll() throws IOException, InterruptedException {
|
||||
localhostComputeService = computeServiceForLocalhost();
|
||||
IMachineToIpAddress iMachineToIpAddress = new IMachineToIpAddress(manager, localhostComputeService.getComputeService());
|
||||
LoadMachineFromVirtualBox cacheLoader = new LoadMachineFromVirtualBox(manager, iMachineToIpAddress);
|
||||
cache = CacheBuilder.newBuilder().build(cacheLoader);
|
||||
virtualBoxComputeService = computeServiceForVirtualBox(cache);
|
||||
socketTester = new RetryablePredicate<IPSocket>(
|
||||
new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS);
|
||||
setupCredentials();
|
||||
setupConfigurationProperties();
|
||||
|
||||
manager.connect(endpoint.toASCIIString(), identity, credential);
|
||||
|
||||
createAndLaunchVirtualMachine(1);
|
||||
|
||||
String instanceName = vmName + "_1";
|
||||
IMachine machine = manager.getVBox().findMachine(instanceName);
|
||||
logMachineStatus(machine);
|
||||
try {
|
||||
ISession machineSession = manager.openMachineSession(machine);
|
||||
IProgress progress = machineSession.getConsole().powerDown();
|
||||
progress.waitForCompletion(-1);
|
||||
machineSession.unlockMachine();
|
||||
while (!machine.getSessionState().equals(SessionState.Unlocked)) {
|
||||
try {
|
||||
logger().debug("Waiting for unlocking session - session state: " + machine.getSessionState());
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
logMachineStatus(machine);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void createAndLaunchVirtualMachine(int i) throws InterruptedException {
|
||||
String instanceName = vmName + "_" + i;
|
||||
IMachine adminNode = manager.getVBox().findMachine(adminNodeName);
|
||||
|
||||
IMachine clonedVM = manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite);
|
||||
List<CloneOptions> options = new ArrayList<CloneOptions>();
|
||||
options.add(CloneOptions.Link);
|
||||
IProgress progress = adminNode.getCurrentSnapshot().getMachine().cloneTo(clonedVM, CloneMode.MachineState, options);
|
||||
|
||||
if (progress.getCompleted())
|
||||
logger().debug("clone done");
|
||||
|
||||
manager.getVBox().registerMachine(clonedVM);
|
||||
|
||||
ISession session = manager.getSessionObject();
|
||||
clonedVM.lockMachine(session, LockType.Write);
|
||||
IMachine mutable = session.getMachine();
|
||||
// network
|
||||
String hostInterface = null;
|
||||
String command = "vboxmanage list bridgedifs";
|
||||
try {
|
||||
Process child = Runtime.getRuntime().exec(command);
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(child.getInputStream()));
|
||||
String line = "";
|
||||
boolean found = false;
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null && !found) {
|
||||
System.out.println("line: " + line);
|
||||
if (line.split(":")[0].contains("Name")) {
|
||||
hostInterface = line.substring(line.indexOf(":") + 1);
|
||||
}
|
||||
if (line.split(":")[0].contains("Status") && line.split(":")[1].contains("Up")) {
|
||||
System.out.println("bridge: " + hostInterface.trim());
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mutable.getNetworkAdapter(new Long(0)).setAttachmentType(NetworkAttachmentType.Bridged);
|
||||
mutable.getNetworkAdapter(new Long(0)).setAdapterType(NetworkAdapterType.Am79C973);
|
||||
mutable.getNetworkAdapter(new Long(0)).setMACAddress("0800279DA478");
|
||||
mutable.getNetworkAdapter(new Long(0)).setBridgedInterface(hostInterface.trim());
|
||||
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
|
||||
mutable.saveSettings();
|
||||
session.unlockMachine();
|
||||
|
||||
System.out.println("\nLaunching VM named " + clonedVM.getName() + " ...");
|
||||
launchVMProcess(clonedVM, manager.getSessionObject());
|
||||
|
||||
clonedVM = manager.getVBox().findMachine(instanceName);
|
||||
|
||||
|
||||
|
||||
try {
|
||||
boolean gotIp = false;
|
||||
while (!gotIp) {
|
||||
Node node = cache.get(instanceName);
|
||||
if (node.getHostname() != null) {
|
||||
gotIp = true;
|
||||
}
|
||||
else {
|
||||
cache.invalidate(node);
|
||||
Thread.sleep(1000);
|
||||
System.out.println("waiting for IP: got IP");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ExecutionException e) {
|
||||
}
|
||||
|
||||
logger().debug("Trying echo...");
|
||||
runScriptOnNode(instanceName, "echo ciao");
|
||||
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command) {
|
||||
return runScriptOnNode(nodeId, command, wrapInInitScript(false));
|
||||
}
|
||||
|
||||
protected ExecResponse runScriptOnNode(String nodeId, String command,
|
||||
RunScriptOptions options) {
|
||||
ExecResponse toReturn = virtualBoxComputeService.getComputeService().runScriptOnNode(nodeId, command, options);
|
||||
assert toReturn.getExitCode() == 0 : toReturn;
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private void checkSSH(IPSocket socket) {
|
||||
socketTester.apply(socket);
|
||||
SshClient client = sshFactory.create(socket, new Credentials(osUsername, osPassword));
|
||||
logger().debug(client.toString());
|
||||
try {
|
||||
client.connect();
|
||||
ExecResponse exec = client.exec("echo hello");
|
||||
System.out.println(exec);
|
||||
assertEquals(exec.getOutput().trim(), "hello");
|
||||
} finally {
|
||||
if (client != null)
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void launchVMProcess(IMachine machine, ISession session) {
|
||||
IProgress prog = machine.launchVMProcess(session, "gui", "");
|
||||
prog.waitForCompletion(-1);
|
||||
session.unlockMachine();
|
||||
}
|
||||
|
||||
private void logMachineStatus(IMachine machine) {
|
||||
NodeMetadata node = new IMachineToNodeMetadata().apply(machine);
|
||||
logger().debug("Machine status: " + node.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -52,7 +52,7 @@ import org.jclouds.scriptbuilder.domain.OsFamily;
|
|||
import org.jclouds.scriptbuilder.statements.login.AdminAccess;
|
||||
import org.jclouds.scriptbuilder.statements.login.DefaultConfiguration;
|
||||
import org.jclouds.ssh.SshException;
|
||||
import org.jclouds.virtualbox.experiment.settings.KeyboardScancodes;
|
||||
import org.jclouds.virtualbox.settings.KeyboardScancodes;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
|
@ -64,7 +64,6 @@ 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.IStorageController;
|
||||
import org.virtualbox_4_1.LockType;
|
||||
import org.virtualbox_4_1.MachineState;
|
||||
import org.virtualbox_4_1.NATProtocol;
|
||||
|
|
|
@ -22,80 +22,73 @@
|
|||
package org.jclouds.virtualbox.functions;
|
||||
|
||||
/**
|
||||
* @author Andrea Turli
|
||||
* @author Andrea Turli, Mattias Holmqvist
|
||||
*/
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.classextension.EasyMock.createNiceMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import org.jclouds.compute.ComputeServiceContext;
|
||||
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
||||
import org.jclouds.virtualbox.experiment.TestUtils;
|
||||
import org.jclouds.compute.domain.Image;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
import org.virtualbox_4_1.IMachine;
|
||||
import org.virtualbox_4_1.IMedium;
|
||||
import org.virtualbox_4_1.INATEngine;
|
||||
import org.virtualbox_4_1.INetworkAdapter;
|
||||
import org.virtualbox_4_1.IProgress;
|
||||
import org.virtualbox_4_1.ISession;
|
||||
import org.virtualbox_4_1.IVirtualBox;
|
||||
import org.virtualbox_4_1.MachineState;
|
||||
import org.virtualbox_4_1.NATProtocol;
|
||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
|
||||
//TODO should it be a live test?
|
||||
@Test(groups = "unit")
|
||||
public class IsoToIMachineTest {
|
||||
import java.util.Set;
|
||||
|
||||
private String settingsFile = "";
|
||||
private boolean forceOverwrite = true;
|
||||
private String vmId = null;
|
||||
private String osTypeId = null;
|
||||
private String controllerIDE = "test-IDE";
|
||||
private String diskFormat = "";
|
||||
private String adminDisk = "testAdmin.vdi";
|
||||
private String guestId = "guestId";
|
||||
private String hostId = "hostId";
|
||||
import static com.google.common.base.Predicates.equalTo;
|
||||
import static com.google.common.collect.Iterables.*;
|
||||
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
@Test
|
||||
public void testConvert() throws Exception {
|
||||
String vmName = "virtualbox-iso-to-machine-test";
|
||||
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
|
||||
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
|
||||
IMachine vm = createNiceMock(IMachine.class);
|
||||
IMedium hd = createNiceMock(IMedium.class);
|
||||
INetworkAdapter iNetworkAdapter = createNiceMock(INetworkAdapter.class);
|
||||
INATEngine inatEngine = createNiceMock(INATEngine.class);
|
||||
IProgress iProgress = createNiceMock(IProgress.class);
|
||||
@Test(groups = "live", singleThreaded = true, testName = "IsoToIMachineTest")
|
||||
public class IsoToIMachineTest extends BaseVirtualBoxClientLiveTest {
|
||||
|
||||
ISession session = createNiceMock(ISession.class);
|
||||
expect(vbm.getVBox()).andReturn(vBox).anyTimes();
|
||||
expect(vbm.getSessionObject()).andReturn(session).anyTimes();
|
||||
expect(vBox.findMachine(vmName)).andReturn(vm).anyTimes();
|
||||
expect(vBox.createMachine(settingsFile, vmName, osTypeId, vmId,
|
||||
forceOverwrite)).andReturn(vm).anyTimes();
|
||||
expect(vm.getName()).andReturn(vmName).anyTimes();
|
||||
expect(vm.getNetworkAdapter(new Long(0))).andReturn(iNetworkAdapter).anyTimes();
|
||||
expect(iNetworkAdapter.getNatDriver()).andReturn(inatEngine).anyTimes();
|
||||
expect(vBox.createHardDisk(diskFormat, adminDisk)).andReturn(hd).anyTimes();
|
||||
expect(vm.launchVMProcess(session, "gui", "")).andReturn(iProgress).anyTimes();
|
||||
expect(session.getMachine()).andReturn(vm).anyTimes();
|
||||
expect(vm.getState()).andReturn(MachineState.Running).anyTimes();
|
||||
private String settingsFile = null;
|
||||
private boolean forceOverwrite = true;
|
||||
private String vmId = "jclouds-image-1";
|
||||
private String osTypeId = "";
|
||||
private String controllerIDE = "IDE Controller";
|
||||
private String diskFormat = "";
|
||||
private String adminDisk = "testadmin.vdi";
|
||||
private String guestId = "guest";
|
||||
private String hostId = "host";
|
||||
|
||||
replay(vbm, vBox, vm, session, hd, iNetworkAdapter, iProgress);
|
||||
private String vmName = "jclouds-image-virtualbox-iso-to-machine-test";
|
||||
|
||||
ComputeServiceContext context = TestUtils
|
||||
.computeServiceForLocalhostAndGuest();
|
||||
IMachine iMachine = new IsoToIMachine(vbm, adminDisk, diskFormat,
|
||||
settingsFile, vmName, osTypeId, vmId, forceOverwrite,
|
||||
controllerIDE, context, hostId, guestId)
|
||||
.apply(VirtualBoxConstants.VIRTUALBOX_DISTRO_ISO_NAME);
|
||||
@BeforeGroups(groups = { "live" })
|
||||
public void setUp() throws Exception {
|
||||
identity = "toor";
|
||||
credential = "password";
|
||||
}
|
||||
|
||||
assertEquals(iMachine.getName(), vmName);
|
||||
@Test
|
||||
public void testCreateImageMachineFromIso() throws Exception {
|
||||
|
||||
}
|
||||
VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();
|
||||
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(hostId, "localhost", guestId, "localhost", new Credentials("toor", "password"));
|
||||
IMachine imageMachine = new IsoToIMachine(manager,
|
||||
adminDisk,
|
||||
diskFormat,
|
||||
settingsFile,
|
||||
vmName,
|
||||
osTypeId,
|
||||
vmId,
|
||||
forceOverwrite,
|
||||
controllerIDE,
|
||||
localHostContext,
|
||||
hostId,
|
||||
guestId,
|
||||
new Credentials("toor", "password")).apply("ubuntu-11.04-server-i386.iso");
|
||||
|
||||
IMachineToImage iMachineToImage = new IMachineToImage(manager);
|
||||
Image newImage = iMachineToImage.apply(imageMachine);
|
||||
Set<? extends Image> images = context.getComputeService().listImages();
|
||||
|
||||
assertTrue(any(images, equalTo(newImage)));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue