Merge pull request #306 from andreaturli/dev

issue 384: adding MutableMachine
This commit is contained in:
Adrian Cole 2012-01-10 17:27:19 -08:00
commit 1920ea70da
6 changed files with 308 additions and 100 deletions

View File

@ -57,7 +57,7 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder {
properties.put(PROPERTY_IDENTITY, "administrator"); properties.put(PROPERTY_IDENTITY, "administrator");
properties.put(PROPERTY_CREDENTIAL, "12345"); properties.put(PROPERTY_CREDENTIAL, "12345");
properties.put(PROPERTY_IMAGE_ID, "ubuntu-10.04.3-server-i386"); properties.put(PROPERTY_IMAGE_ID, "ubuntu-11.04-server-i386");
properties.put(PROPERTY_IMAGE_LOGIN_USER, "toor:password"); properties.put(PROPERTY_IMAGE_LOGIN_USER, "toor:password");
properties.put(PROPERTY_IMAGE_AUTHENTICATE_SUDO, "true"); properties.put(PROPERTY_IMAGE_AUTHENTICATE_SUDO, "true");

View File

@ -54,6 +54,7 @@ import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning;
import org.jclouds.virtualbox.functions.admin.StartVBoxIfNotAlreadyRunning; import org.jclouds.virtualbox.functions.admin.StartVBoxIfNotAlreadyRunning;
import org.jclouds.virtualbox.predicates.SshResponds; import org.jclouds.virtualbox.predicates.SshResponds;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.MachineState; import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
@ -106,7 +107,9 @@ public class VirtualBoxComputeServiceContextModule extends
bind(new TypeLiteral<Function<IMachine, SshClient>>() { bind(new TypeLiteral<Function<IMachine, SshClient>>() {
}).to(IMachineToSshClient.class); }).to(IMachineToSshClient.class);
bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS); bind(ExecutionType.class).toInstance(ExecutionType.GUI);
bind(LockType.class).toInstance(LockType.Write);
} }
@Provides @Provides

View File

@ -26,6 +26,7 @@ import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndAp
import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Shared;
import java.net.URI; import java.net.URI;
import java.util.List;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
@ -50,7 +51,10 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject; import com.google.inject.Inject;
@Singleton @Singleton
@ -74,12 +78,15 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
private final Function<IMachine, SshClient> sshClientForIMachine; private final Function<IMachine, SshClient> sshClientForIMachine;
@Inject @Inject
public CreateAndInstallVm(Supplier<VirtualBoxManager> manager, public CreateAndInstallVm(
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists, Supplier<VirtualBoxManager> manager,
ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, Predicate<SshClient> sshResponds, CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
Function<IMachine, SshClient> sshClientForIMachine, Supplier<NodeMetadata> host, ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull,
RunScriptOnNode.Factory scriptRunner, @Preconfiguration Supplier<URI> preconfiguration, Predicate<SshClient> sshResponds,
ExecutionType executionType) { Function<IMachine, SshClient> sshClientForIMachine,
Supplier<NodeMetadata> host, RunScriptOnNode.Factory scriptRunner,
@Preconfiguration Supplier<URI> preconfiguration,
ExecutionType executionType) {
this.manager = manager; this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists; this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull; this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull;
@ -97,10 +104,14 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
// note this may not be reachable, as this likely uses the 10.2.2 address // note this may not be reachable, as this likely uses the 10.2.2 address
URI preconfigurationUri = preconfiguration.get(); URI preconfigurationUri = preconfiguration.get();
String keySequence = valueOfConfigurationKeyOrNull.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace( String keySequence = valueOfConfigurationKeyOrNull
"PRECONFIGURATION_URL", preconfigurationUri.toASCIIString()).replace("HOSTNAME", vmName); .apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
.replace("PRECONFIGURATION_URL",
preconfigurationUri.toASCIIString())
.replace("HOSTNAME", vmName);
final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists.apply(vmSpec); final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists
.apply(vmSpec);
// Launch machine and wait for it to come online // Launch machine and wait for it to come online
ensureMachineIsLaunched(vmName); ensureMachineIsLaunched(vmName);
@ -110,24 +121,30 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
SshClient client = sshClientForIMachine.apply(vm); SshClient client = sshClientForIMachine.apply(vm);
logger.debug(">> awaiting installation to finish node(%s)", vmName); logger.debug(">> awaiting installation to finish node(%s)", vmName);
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName); checkState(sshResponds.apply(client),
"timed out waiting for guest %s to be accessible via ssh", vmName);
logger.debug("<< installation of image complete. Powering down node(%s)", vmName); logger.debug("<< installation of image complete. Powering down node(%s)",
lockSessionOnMachineAndApply(manager.get(), Shared, vmName, new Function<ISession, Void>() { vmName);
lockSessionOnMachineAndApply(manager.get(), Shared, vmName,
new Function<ISession, Void>() {
@Override @Override
public Void apply(ISession session) { public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown(); IProgress powerDownProgress = session.getConsole()
powerDownProgress.waitForCompletion(-1); .powerDown();
return null; powerDownProgress.waitForCompletion(-1);
} return null;
}
}); });
return vm; return vm;
} }
private void ensureMachineIsLaunched(String vmName) { private void ensureMachineIsLaunched(String vmName) {
applyForMachine(manager.get(), vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, "")); applyForMachine(manager.get(), vmName,
new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType,
""));
} }
private void sendKeyboardSequence(String keyboardSequence, String vmName) { private void sendKeyboardSequence(String keyboardSequence, String vmName) {
@ -136,16 +153,20 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
for (String line : splitSequence) { for (String line : splitSequence) {
String converted = stringToKeycode(line); String converted = stringToKeycode(line);
for (String word : converted.split(" ")) { for (String word : converted.split(" ")) {
sb.append("vboxmanage controlvm ").append(vmName).append(" keyboardputscancode ").append(word).append("; "); sb.append("vboxmanage controlvm ").append(vmName)
.append(" keyboardputscancode ").append(word).append("; ");
runScriptIfWordEndsWith(sb, word, "<Enter>"); runScriptIfWordEndsWith(sb, word, "<Enter>");
runScriptIfWordEndsWith(sb, word, "<Return>"); runScriptIfWordEndsWith(sb, word, "<Return>");
} }
} }
} }
private void runScriptIfWordEndsWith(StringBuilder sb, String word, String key) { private void runScriptIfWordEndsWith(StringBuilder sb, String word,
String key) {
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get(key))) { if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get(key))) {
scriptRunner.create(host.get(), Statements.exec(sb.toString()), runAsRoot(false).wrapInInitScript(false)).init().call(); scriptRunner
.create(host.get(), Statements.exec(sb.toString()),
runAsRoot(false).wrapInInitScript(false)).init().call();
sb.delete(0, sb.length() - 1); sb.delete(0, sb.length() - 1);
} }
} }
@ -155,7 +176,9 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
if (s.startsWith("<")) { if (s.startsWith("<")) {
String[] specials = s.split("<"); String[] specials = s.split("<");
for (int i = 1; i < specials.length; i++) { for (int i = 1; i < specials.length; i++) {
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<" + specials[i])).append(" "); keycodes.append(
KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<"
+ specials[i])).append(" ");
} }
return keycodes.toString(); return keycodes.toString();
} }
@ -169,8 +192,11 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
keycodes.append(" "); keycodes.append(" ");
i++; i++;
} }
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Spacebar>")).append(" "); keycodes.append(
KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Spacebar>"))
.append(" ");
return keycodes.toString(); return keycodes.toString();
} }
} }

View File

@ -0,0 +1,85 @@
/*
* 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 javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.virtualbox_4_1.IMachine;
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 com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
@Singleton
public class MutableMachine implements Function<String, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Supplier<VirtualBoxManager> manager;
private final LockType lockType;
@Inject
public MutableMachine(Supplier<VirtualBoxManager> manager,
LockType lockType) {
this.manager = manager;
this.lockType = lockType;
}
@Override
public IMachine apply(String machineId) {
return lockSessionOnMachineAndReturn(manager.get(), lockType, machineId);
}
/**
* Locks the machine and executes the given function using the current session.
* Since the machine is locked it is possible to perform some modifications to the IMachine.
* <p/>
* Unlocks the machine before returning.
*
* @param manager the VirtualBoxManager
* @param type the kind of lock to use when initially locking the machine.
* @param machineId the id of the machine
* @return the result from applying the function to the session.
*/
public static IMachine lockSessionOnMachineAndReturn(VirtualBoxManager manager, LockType type, String machineId) {
try {
ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId);
immutableMachine.lockMachine(session, type);
return immutableMachine;
} catch (VBoxException e) {
throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId,
type, e.getMessage()), e);
}
}
}

View File

@ -22,9 +22,22 @@ import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.findPid; import static org.jclouds.scriptbuilder.domain.Statements.findPid;
import static org.jclouds.scriptbuilder.domain.Statements.kill; import static org.jclouds.scriptbuilder.domain.Statements.kill;
import static org.jclouds.scriptbuilder.domain.Statements.newStatementList; import static org.jclouds.scriptbuilder.domain.Statements.newStatementList;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.callables.RunScriptOnNode.Factory;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.util.Throwables2; import org.jclouds.util.Throwables2;
import org.jclouds.virtualbox.functions.MutableMachine;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType; import org.virtualbox_4_1.LockType;
@ -33,16 +46,61 @@ import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
/** /**
* Utilities for executing functions on a VirtualBox machine. * Utilities for executing functions on a VirtualBox machine.
* *
* @author Adrian Cole, Mattias Holmqvist * @author Adrian Cole, Mattias Holmqvist, Andrea Turli
*/ */
@Singleton
public class MachineUtils { public class MachineUtils {
public static <T> T applyForMachine(VirtualBoxManager manager, final String machineId, final Function<IMachine, T> function) { @Resource
final IMachine immutableMachine = manager.getVBox().findMachine(machineId); @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Supplier<VirtualBoxManager> manager;
private final LockType lockType;
private final Factory scriptRunner;
private final Supplier<NodeMetadata> host;
@Inject
public MachineUtils(Supplier<VirtualBoxManager> manager, LockType lockType, RunScriptOnNode.Factory scriptRunner, Supplier<NodeMetadata> host) {
super();
this.manager = manager;
this.lockType = lockType;
this.scriptRunner = scriptRunner;
this.host = host;
}
public <T> Function<String, T> mutateMachine(String machineId,
Function<IMachine, T> function) {
try {
return Functions.compose(function, new MutableMachine(manager, lockType));
} finally {
unlockMachine(machineId);
}
}
protected void unlockMachine(final String machineId) {
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
if (immutableMachine.getSessionState().equals(SessionState.Locked)) {
Statement kill = newStatementList(call("default"),
findPid(immutableMachine.getSessionPid().toString()), kill());
scriptRunner
.create(host.get(), kill,
runAsRoot(false).wrapInInitScript(false)).init().call();
}
}
public static <T> T applyForMachine(VirtualBoxManager manager,
final String machineId, final Function<IMachine, T> function) {
final IMachine immutableMachine = manager.getVBox()
.findMachine(machineId);
return new Function<IMachine, T>() { return new Function<IMachine, T>() {
@Override @Override
public T apply(IMachine machine) { public T apply(IMachine machine) {
@ -57,48 +115,60 @@ public class MachineUtils {
} }
/** /**
* Locks the machine and executes the given function using the machine matching the given id. * Locks the machine and executes the given function using the machine
* Since the machine is locked it is possible to perform some modifications to the IMachine. * matching the given id. Since the machine is locked it is possible to
* perform some modifications to the IMachine.
* <p/> * <p/>
* Unlocks the machine before returning. * Unlocks the machine before returning.
* *
* @param manager the VirtualBoxManager * @param manager
* @param type the kind of lock to use when initially locking the machine. * the VirtualBoxManager
* @param machineId the id of the machine * @param type
* @param function the function to execute * the kind of lock to use when initially locking the machine.
* @param machineId
* the id of the machine
* @param function
* the function to execute
* @return the result from applying the function to the machine. * @return the result from applying the function to the machine.
*/ */
public static <T> T lockMachineAndApply(VirtualBoxManager manager, final LockType type, final String machineId, public static <T> T lockMachineAndApply(VirtualBoxManager manager,
final Function<IMachine, T> function) { final LockType type, final String machineId,
return lockSessionOnMachineAndApply(manager, type, machineId, new Function<ISession, T>() { final Function<IMachine, T> function) {
return lockSessionOnMachineAndApply(manager, type, machineId,
new Function<ISession, T>() {
@Override @Override
public T apply(ISession session) { public T apply(ISession session) {
return function.apply(session.getMachine()); return function.apply(session.getMachine());
} }
@Override @Override
public String toString() { public String toString() {
return function.toString(); return function.toString();
} }
}); });
} }
/** /**
* Locks the machine and executes the given function using the current session. * Locks the machine and executes the given function using the current
* Since the machine is locked it is possible to perform some modifications to the IMachine. * session. Since the machine is locked it is possible to perform some
* modifications to the IMachine.
* <p/> * <p/>
* Unlocks the machine before returning. * Unlocks the machine before returning.
* *
* @param manager the VirtualBoxManager * @param manager
* @param type the kind of lock to use when initially locking the machine. * the VirtualBoxManager
* @param machineId the id of the machine * @param type
* @param function the function to execute * the kind of lock to use when initially locking the machine.
* @param machineId
* the id of the machine
* @param function
* the function to execute
* @return the result from applying the function to the session. * @return the result from applying the function to the session.
*/ */
public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager, LockType type, String machineId, public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager,
Function<ISession, T> function) { LockType type, String machineId, Function<ISession, T> function) {
try { try {
ISession session = manager.getSessionObject(); ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId); IMachine immutableMachine = manager.getVBox().findMachine(machineId);
@ -109,42 +179,52 @@ public class MachineUtils {
session.unlockMachine(); session.unlockMachine();
} }
} catch (VBoxException e) { } catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId, throw new RuntimeException(String.format(
type, e.getMessage()), e); "error applying %s to %s with %s lock: %s", function, machineId,
type, e.getMessage()), e);
} }
} }
/** /**
* Locks the machine and executes the given function using the current session, if the machine is registered. * Locks the machine and executes the given function using the current
* Since the machine is locked it is possible to perform some modifications to the IMachine. * session, if the machine is registered. Since the machine is locked it is
* possible to perform some modifications to the IMachine.
* <p/> * <p/>
* Unlocks the machine before returning. * Unlocks the machine before returning.
* *
* @param manager the VirtualBoxManager * @param manager
* @param type the kind of lock to use when initially locking the machine. * the VirtualBoxManager
* @param machineId the id of the machine * @param type
* @param function the function to execute * the kind of lock to use when initially locking the machine.
* @param machineId
* the id of the machine
* @param function
* the function to execute
* @return the result from applying the function to the session. * @return the result from applying the function to the session.
*/ */
public static <T> T lockMachineAndApplyOrReturnNullIfNotRegistered(VirtualBoxManager manager, LockType type, String machineId, public static <T> T lockMachineAndApplyOrReturnNullIfNotRegistered(
Function<IMachine, T> function) { VirtualBoxManager manager, LockType type, String machineId,
Function<IMachine, T> function) {
try { try {
return lockMachineAndApply(manager, type, machineId, function); return lockMachineAndApply(manager, type, machineId, function);
} catch (RuntimeException e) { } catch (RuntimeException e) {
VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); VBoxException vbex = Throwables2.getFirstThrowableOfType(e,
if (vbex != null && vbex.getMessage().indexOf("not find a registered") == -1) VBoxException.class);
if (vbex != null
&& vbex.getMessage().indexOf("not find a registered") == -1)
throw e; throw e;
return null; return null;
} }
} }
/** /**
* Unlocks the machine and executes the given function using the machine matching the given id. * Unlocks the machine and executes the given function using the machine
* Since the machine is unlocked it is possible to delete the IMachine. * matching the given id. Since the machine is unlocked it is possible to
* delete the IMachine.
* <p/> * <p/>
* *
*<h3>Note!</h3> Currently, this can only unlock the machine, if the lock was created in the * <h3>Note!</h3> Currently, this can only unlock the machine, if the lock
* current session. * was created in the current session.
* *
* @param manager * @param manager
* the VirtualBoxManager * the VirtualBoxManager
@ -154,42 +234,54 @@ public class MachineUtils {
* the function to execute * the function to execute
* @return the result from applying the function to the machine. * @return the result from applying the function to the machine.
*/ */
public static <T> T unlockMachineAndApply(VirtualBoxManager manager, final String machineId, public static <T> T unlockMachineAndApply(VirtualBoxManager manager,
final Function<IMachine, T> function) { final String machineId, final Function<IMachine, T> function) {
ISession session = manager.getSessionObject();
try { try {
ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId); IMachine immutableMachine = manager.getVBox().findMachine(machineId);
SessionState state = immutableMachine.getSessionState(); SessionState state = immutableMachine.getSessionState();
Statement kill = newStatementList(call("default"),
findPid(immutableMachine.getSessionPid().toString()), kill());
if (state.equals(SessionState.Locked)) if (state.equals(SessionState.Locked))
session.unlockMachine(); // session.unlockMachine();
//TODO: wire this in kill.render(OsFamily.UNIX);
Statement kill = newStatementList(call("default"), findPid(machineId), kill()); // TODO: wire this in
return function.apply(immutableMachine); return function.apply(immutableMachine);
} catch (VBoxException e) { } catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s: %s", function, machineId, e.getMessage()), session.unlockMachine();
e); throw new RuntimeException(String.format(
"error applying %s to %s: %s", function, machineId,
e.getMessage()), e);
} }
} }
/** /**
* Unlocks the machine and executes the given function, if the machine is registered. * Unlocks the machine and executes the given function, if the machine is
* Since the machine is unlocked it is possible to delete the machine. * registered. Since the machine is unlocked it is possible to delete the
* machine.
* <p/> * <p/>
* *
* @param manager the VirtualBoxManager * @param manager
* @param machineId the id of the machine * the VirtualBoxManager
* @param function the function to execute * @param machineId
* the id of the machine
* @param function
* the function to execute
* @return the result from applying the function to the session. * @return the result from applying the function to the session.
*/ */
public static <T> T unlockMachineAndApplyOrReturnNullIfNotRegistered(VirtualBoxManager manager, String machineId, public static <T> T unlockMachineAndApplyOrReturnNullIfNotRegistered(
Function<IMachine, T> function) { VirtualBoxManager manager, String machineId,
Function<IMachine, T> function) {
try { try {
return unlockMachineAndApply(manager, machineId, function); return unlockMachineAndApply(manager, machineId, function);
} catch (RuntimeException e) { } catch (RuntimeException e) {
VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); VBoxException vbex = Throwables2.getFirstThrowableOfType(e,
if (vbex != null && vbex.getMessage().indexOf("not find a registered") == -1) VBoxException.class);
if (vbex != null
&& vbex.getMessage().indexOf("not find a registered") == -1)
throw e; throw e;
return null; return null;
} }

View File

@ -38,7 +38,10 @@ import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule; import org.jclouds.sshj.config.SshjSshClientModule;
import org.jclouds.virtualbox.config.VirtualBoxConstants; import org.jclouds.virtualbox.config.VirtualBoxConstants;
import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia;
import org.jclouds.virtualbox.util.MachineUtils;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -124,12 +127,11 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
adminDisk = workingDir + "/testadmin.vdi"; adminDisk = workingDir + "/testadmin.vdi";
operatingSystemIso = String.format("%s/%s.iso", workingDir, imageId); operatingSystemIso = String.format("%s/%s.iso", workingDir, imageId);
guestAdditionsIso = String.format("%s/VBoxGuestAdditions_%s.iso", workingDir, hostVersion); guestAdditionsIso = String.format("%s/VBoxGuestAdditions_%s.iso", workingDir, hostVersion);
} }
protected void undoVm(VmSpec vmSpecification) { protected void undoVm(VmSpec vmSpecification) {
unlockMachineAndApplyOrReturnNullIfNotRegistered(manager.get(), vmSpecification.getVmName(), MachineUtils machineUtils = context.utils().injector().getInstance(MachineUtils.class);
new UnregisterMachineIfExistsAndDeleteItsMedia(vmSpecification)); machineUtils.mutateMachine(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia(vmSpecification));
} }
@AfterClass(groups = "live") @AfterClass(groups = "live")