issue 384: change MachineUtils to use MutableMachine function

This commit is contained in:
andreaturli 2012-01-11 00:19:29 +00:00
parent 31cf231965
commit b82d68d9fd
7 changed files with 308 additions and 243 deletions

View File

@ -57,7 +57,7 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder {
properties.put(PROPERTY_IDENTITY, "administrator");
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_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.predicates.SshResponds;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.VirtualBoxManager;
@ -106,7 +107,9 @@ public class VirtualBoxComputeServiceContextModule extends
bind(new TypeLiteral<Function<IMachine, SshClient>>() {
}).to(IMachineToSshClient.class);
bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS);
bind(ExecutionType.class).toInstance(ExecutionType.GUI);
bind(LockType.class).toInstance(LockType.Write);
}
@Provides

View File

@ -27,6 +27,7 @@ import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndAp
import static org.virtualbox_4_1.LockType.Shared;
import java.net.URI;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Named;
@ -51,7 +52,10 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
@Singleton
@ -75,12 +79,15 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
private final Function<IMachine, SshClient> sshClientForIMachine;
@Inject
public CreateAndInstallVm(Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, Predicate<SshClient> sshResponds,
Function<IMachine, SshClient> sshClientForIMachine, Supplier<NodeMetadata> host,
RunScriptOnNode.Factory scriptRunner, @Preconfiguration Supplier<URI> preconfiguration,
ExecutionType executionType) {
public CreateAndInstallVm(
Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull,
Predicate<SshClient> sshResponds,
Function<IMachine, SshClient> sshClientForIMachine,
Supplier<NodeMetadata> host, RunScriptOnNode.Factory scriptRunner,
@Preconfiguration Supplier<URI> preconfiguration,
ExecutionType executionType) {
this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull;
@ -98,10 +105,14 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
// note this may not be reachable, as this likely uses the 10.2.2 address
URI preconfigurationUri = preconfiguration.get();
String keySequence = valueOfConfigurationKeyOrNull.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
"PRECONFIGURATION_URL", preconfigurationUri.toASCIIString()).replace("HOSTNAME", vmName);
String keySequence = valueOfConfigurationKeyOrNull
.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
ensureMachineIsLaunched(vmName);
@ -111,24 +122,30 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
SshClient client = sshClientForIMachine.apply(vm);
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);
lockSessionOnMachineAndApply(manager.get(), Shared, vmName, new Function<ISession, Void>() {
logger.debug("<< installation of image complete. Powering down node(%s)",
vmName);
lockSessionOnMachineAndApply(manager.get(), Shared, vmName,
new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return null;
}
@Override
public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole()
.powerDown();
powerDownProgress.waitForCompletion(-1);
return null;
}
});
});
return vm;
}
private void ensureMachineIsLaunched(String vmName) {
applyForMachine(manager.get(), vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
private void ensureMachineIsLaunched(String vmName) {
applyForMachine(manager.get(), vmName,
new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType,
""));
}
private void sendKeyboardSequence(String keyboardSequence, String vmName) {
@ -137,16 +154,20 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
for (String line : splitSequence) {
String converted = stringToKeycode(line);
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, "<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))) {
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);
}
}
@ -156,7 +177,9 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
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])).append(" ");
keycodes.append(
KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<"
+ specials[i])).append(" ");
}
return keycodes.toString();
}
@ -170,8 +193,11 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
keycodes.append(" ");
i++;
}
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Spacebar>")).append(" ");
keycodes.append(
KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Spacebar>"))
.append(" ");
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

@ -1,143 +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.functions;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine;
import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndApply;
import static org.virtualbox_4_1.LockType.Shared;
import java.net.URI;
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.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.SshClient;
import org.jclouds.util.Throwables2;
import org.jclouds.virtualbox.Preconfiguration;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
@Singleton
public class MutateMachine implements Function<String, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Supplier<VirtualBoxManager> manager;
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
private final ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull;
private final Supplier<URI> preconfiguration;
private final Predicate<SshClient> sshResponds;
private final ExecutionType executionType;
private final Factory scriptRunner;
private final Supplier<NodeMetadata> host;
private final Function<IMachine, SshClient> sshClientForIMachine;
@Inject
public MutateMachine(Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, Predicate<SshClient> sshResponds,
Function<IMachine, SshClient> sshClientForIMachine, Supplier<NodeMetadata> host,
RunScriptOnNode.Factory scriptRunner, @Preconfiguration Supplier<URI> preconfiguration,
ExecutionType executionType) {
this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull;
this.sshResponds = sshResponds;
this.sshClientForIMachine = sshClientForIMachine;
this.scriptRunner = scriptRunner;
this.host = host;
this.preconfiguration = preconfiguration;
this.executionType = executionType;
}
@Override
public IMachine apply(String machineId) {
return lockMachineAndApply(manager.get(), LockType.Write, machineId);
}
/**
* Locks the machine and executes the given function using the machine matching the given id.
* 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 locked machine.
*/
public static IMachine lockMachineAndApply(VirtualBoxManager manager, final LockType type, final String machineId) {
return lockSessionOnMachineAndReturn(manager, type, 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

@ -23,9 +23,22 @@ import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.findPid;
import static org.jclouds.scriptbuilder.domain.Statements.kill;
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.Statements;
import org.jclouds.util.Throwables2;
import org.jclouds.virtualbox.functions.MutableMachine;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType;
@ -34,16 +47,61 @@ import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
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.
*
* @author Adrian Cole, Mattias Holmqvist
*
* @author Adrian Cole, Mattias Holmqvist, Andrea Turli
*/
@Singleton
public class MachineUtils {
public static <T> T applyForMachine(VirtualBoxManager manager, final String machineId, final Function<IMachine, T> function) {
final IMachine immutableMachine = manager.getVBox().findMachine(machineId);
@Resource
@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>() {
@Override
public T apply(IMachine machine) {
@ -58,48 +116,60 @@ public class MachineUtils {
}
/**
* Locks the machine and executes the given function using the machine matching the given id.
* Since the machine is locked it is possible to perform some modifications to the IMachine.
* Locks the machine and executes the given function using the machine
* matching the given id. 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
* @param function the function to execute
*
* @param manager
* the VirtualBoxManager
* @param type
* 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.
*/
public static <T> T lockMachineAndApply(VirtualBoxManager manager, final LockType type, final String machineId,
final Function<IMachine, T> function) {
return lockSessionOnMachineAndApply(manager, type, machineId, new Function<ISession, T>() {
public static <T> T lockMachineAndApply(VirtualBoxManager manager,
final LockType type, final String machineId,
final Function<IMachine, T> function) {
return lockSessionOnMachineAndApply(manager, type, machineId,
new Function<ISession, T>() {
@Override
public T apply(ISession session) {
return function.apply(session.getMachine());
}
@Override
public T apply(ISession session) {
return function.apply(session.getMachine());
}
@Override
public String toString() {
return function.toString();
}
@Override
public String toString() {
return function.toString();
}
});
});
}
/**
* 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.
* 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
* @param function the function to execute
*
* @param manager
* the VirtualBoxManager
* @param type
* 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.
*/
public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager, LockType type, String machineId,
Function<ISession, T> function) {
public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager,
LockType type, String machineId, Function<ISession, T> function) {
try {
ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId);
@ -110,42 +180,52 @@ public class MachineUtils {
session.unlockMachine();
}
} catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId,
type, e.getMessage()), e);
throw new RuntimeException(String.format(
"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.
* Since the machine is locked it is possible to perform some modifications to the IMachine.
* Locks the machine and executes the given function using the current
* session, if the machine is registered. 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
* @param function the function to execute
*
* @param manager
* the VirtualBoxManager
* @param type
* 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.
*/
public static <T> T lockMachineAndApplyOrReturnNullIfNotRegistered(VirtualBoxManager manager, LockType type, String machineId,
Function<IMachine, T> function) {
public static <T> T lockMachineAndApplyOrReturnNullIfNotRegistered(
VirtualBoxManager manager, LockType type, String machineId,
Function<IMachine, T> function) {
try {
return lockMachineAndApply(manager, type, machineId, function);
} catch (RuntimeException e) {
VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class);
if (vbex != null && vbex.getMessage().indexOf("not find a registered") == -1)
VBoxException vbex = Throwables2.getFirstThrowableOfType(e,
VBoxException.class);
if (vbex != null
&& vbex.getMessage().indexOf("not find a registered") == -1)
throw e;
return null;
}
}
/**
* Unlocks the machine and executes the given function using the machine matching the given id.
* Since the machine is unlocked it is possible to delete the IMachine.
* Unlocks the machine and executes the given function using the machine
* matching the given id. Since the machine is unlocked it is possible to
* delete the IMachine.
* <p/>
*
*<h3>Note!</h3> Currently, this can only unlock the machine, if the lock was created in the
* current session.
* <h3>Note!</h3> Currently, this can only unlock the machine, if the lock
* was created in the current session.
*
* @param manager
* the VirtualBoxManager
@ -155,42 +235,54 @@ public class MachineUtils {
* the function to execute
* @return the result from applying the function to the machine.
*/
public static <T> T unlockMachineAndApply(VirtualBoxManager manager, final String machineId,
final Function<IMachine, T> function) {
public static <T> T unlockMachineAndApply(VirtualBoxManager manager,
final String machineId, final Function<IMachine, T> function) {
ISession session = manager.getSessionObject();
try {
ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId);
SessionState state = immutableMachine.getSessionState();
Statement kill = newStatementList(call("default"),
findPid(immutableMachine.getSessionPid().toString()), kill());
if (state.equals(SessionState.Locked))
session.unlockMachine();
//TODO: wire this in
Statement kill = newStatementList(call("default"), findPid(machineId), kill());
// session.unlockMachine();
kill.render(OsFamily.UNIX);
// TODO: wire this in
return function.apply(immutableMachine);
} catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s: %s", function, machineId, e.getMessage()),
e);
session.unlockMachine();
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.
* Since the machine is unlocked it is possible to delete the machine.
* Unlocks the machine and executes the given function, if the machine is
* registered. Since the machine is unlocked it is possible to delete the
* machine.
* <p/>
*
* @param manager the VirtualBoxManager
* @param machineId the id of the machine
* @param function the function to execute
*
* @param manager
* the VirtualBoxManager
* @param machineId
* the id of the machine
* @param function
* the function to execute
* @return the result from applying the function to the session.
*/
public static <T> T unlockMachineAndApplyOrReturnNullIfNotRegistered(VirtualBoxManager manager, String machineId,
Function<IMachine, T> function) {
public static <T> T unlockMachineAndApplyOrReturnNullIfNotRegistered(
VirtualBoxManager manager, String machineId,
Function<IMachine, T> function) {
try {
return unlockMachineAndApply(manager, machineId, function);
} catch (RuntimeException e) {
VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class);
if (vbex != null && vbex.getMessage().indexOf("not find a registered") == -1)
VBoxException vbex = Throwables2.getFirstThrowableOfType(e,
VBoxException.class);
if (vbex != null
&& vbex.getMessage().indexOf("not find a registered") == -1)
throw e;
return null;
}

View File

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