diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java index 7ca72d7108..fcaac83da1 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java @@ -158,7 +158,7 @@ public class VirtualBoxComputeServiceContextModule extends bind(new TypeLiteral>() { }).to(IMachineToSshClient.class); - bind(ExecutionType.class).toInstance(ExecutionType.GUI); + bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS); bind(LockType.class).toInstance(LockType.Write); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java index 92be138447..bf75aa686d 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java @@ -147,7 +147,7 @@ public class CreateAndInstallVm implements Function { .split(installationKeySequence), new StringToKeyCode()); for (List scancodes : scancodelist) { - machineUtils.readLockMachineAndApplyToSession(vmName,new SendScancodes(scancodes)); + machineUtils.sharedLockMachineAndApplyToSession(vmName,new SendScancodes(scancodes)); } } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java index e2b7e7e040..bef7f792c7 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java @@ -46,6 +46,7 @@ import org.jclouds.virtualbox.domain.NodeSpec; import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.statements.DeleteGShadowLock; import org.jclouds.virtualbox.statements.SetIpAddress; +import org.jclouds.virtualbox.util.MachineController; import org.jclouds.virtualbox.util.MachineUtils; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -83,23 +84,22 @@ public class NodeCreator implements Function manager; private final Function cloner; private final AtomicInteger nodePorts; private final AtomicInteger nodeIps; - private MachineUtils machineUtils; + private final MachineUtils machineUtils; + private final MachineController machineController; @Inject public NodeCreator(Supplier manager, Function cloner, - MachineUtils machineUtils, RunScriptOnNode.Factory scriptRunnerFactory) { + MachineUtils machineUtils, RunScriptOnNode.Factory scriptRunnerFactory, MachineController machineController) { this.manager = manager; this.cloner = cloner; this.nodePorts = new AtomicInteger(NODE_PORT_INIT); this.nodeIps = new AtomicInteger(2); this.machineUtils = machineUtils; + this.machineController = machineController; } @Override @@ -147,7 +147,7 @@ public class NodeCreator implements Function() { + machineUtils.sharedLockMachineAndApplyToSession(vmName, new Function() { @Override public Void apply(ISession session) { IProgress powerDownProgress = session.getConsole().powerDown(); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java index 8c527d85ba..29745fd14b 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java @@ -57,15 +57,12 @@ public class MachineUtils { private final Supplier manager; private final Factory scriptRunner; - private final Supplier host; @Inject - public MachineUtils(Supplier manager, RunScriptOnNode.Factory scriptRunner, - Supplier host) { + public MachineUtils(Supplier manager, RunScriptOnNode.Factory scriptRunner) { super(); this.manager = manager; this.scriptRunner = scriptRunner; - this.host = host; } public ListenableFuture runScriptOnNode(NodeMetadata metadata, Statement statement, @@ -100,10 +97,11 @@ public class MachineUtils { }); } - + /** - * Locks the machine and executes the given function using the machine matching the given id. - * The machine is write locked and modifications to the session that reflect on the machine can be done safely. + * Locks the machine and executes the given function using the machine matching the given id. The + * machine is write locked and modifications to the session that reflect on the machine can be + * done safely. *

* Unlocks the machine before returning. * @@ -116,10 +114,11 @@ public class MachineUtils { public T writeLockMachineAndApplyToSession(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Write, function); } - + /** - * Locks the machine and executes the given function using the machine matching the given id. - * The machine is read locked, which means that settings can be read safely (but not changed) by function. + * Locks the machine and executes the given function using the machine matching the given id. The + * machine is read locked, which means that settings can be read safely (but not changed) by + * function. *

* Unlocks the machine before returning. * @@ -129,7 +128,7 @@ public class MachineUtils { * the function to execute * @return the result from applying the function to the machine. */ - public T readLockMachineAndApply(final String machineId, final Function function) { + public T sharedLockMachineAndApply(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Shared, new Function() { @Override @@ -144,10 +143,11 @@ public class MachineUtils { }); } - + /** - * Locks the machine and executes the given function to the session using the machine matching the given id. - * The machine is read locked, which means that settings can be read safely (but not changed) by function. + * Locks the machine and executes the given function to the session using the machine matching + * the given id. The machine is read locked, which means that settings can be read safely (but + * not changed) by function. *

* Unlocks the machine before returning. * @@ -157,10 +157,9 @@ public class MachineUtils { * the function to execute * @return the result from applying the function to the machine. */ - public T readLockMachineAndApplyToSession(final String machineId, final Function function) { + public T sharedLockMachineAndApplyToSession(final String machineId, final Function function) { return lockSessionOnMachineAndApply(machineId, LockType.Shared, function); } - /** * Locks the machine and executes the given function using the current session. Since the machine @@ -183,11 +182,14 @@ public class MachineUtils { int retries = 5; int count = 0; ISession session; + long time = System.currentTimeMillis(); while (true) { try { - session = manager.get().getSessionObject(); IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); + session = manager.get().getSessionObject(); immutableMachine.lockMachine(session, type); + System.err.println("ACQUIRED LOCK ["+time+"]"); + print(); break; } catch (VBoxException e) { VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); @@ -195,7 +197,7 @@ public class MachineUtils { return null; } count++; - logger.warn("Could not lock machine (try %i of %i). Error: %s", retries, count, e.getMessage()); + logger.warn(e, "Could not lock machine (try %d of %d). Error: %s", count, retries, e.getMessage()); if (count == retries) { throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId, type, e.getMessage()), e); @@ -213,6 +215,14 @@ public class MachineUtils { type, e.getMessage()), e); } finally { session.unlockMachine(); + System.err.println("RELEASED LOCK ["+time+"]"); + print(); + } + } + + void print() { + for (StackTraceElement element : Thread.currentThread().getStackTrace()){ + System.err.println(element.toString()); } } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index 4c3485ed09..114e23d83e 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -142,7 +142,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { String vboxVersion = Iterables.get( Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0); - assertEquals(vboxVersion, machineUtils.readLockMachineAndApplyToSession(machine.getName(), + assertEquals(vboxVersion, machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), new Function() { @Override public String apply(ISession session) { diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java index efd1bdae47..c726cf2639 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java @@ -110,7 +110,7 @@ public class GuestAdditionsInstallerLiveTest extends machineUtils.applyForMachine(machine.getName(), new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "")); - assertTrue(machineUtils.readLockMachineAndApplyToSession( + assertTrue(machineUtils.sharedLockMachineAndApplyToSession( machine.getName(), new Function() { @Override