issue 384: vbox 1.5.1

This commit is contained in:
Andrea Turli 2012-03-28 16:00:49 +01:00
parent a2922006c9
commit e27db18cfd
31 changed files with 747 additions and 536 deletions

View File

@ -27,6 +27,7 @@ import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.inject.Named;
import javax.inject.Singleton;
@ -158,7 +159,7 @@ public class VirtualBoxComputeServiceContextModule extends
bind(new TypeLiteral<Function<IMachine, SshClient>>() {
}).to(IMachineToSshClient.class);
bind(ExecutionType.class).toInstance(ExecutionType.GUI);
bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS);
bind(LockType.class).toInstance(LockType.Write);
}
@ -213,7 +214,7 @@ public class VirtualBoxComputeServiceContextModule extends
@Provides
@Singleton
protected Predicate<SshClient> sshResponds(SshResponds sshResponds, Timeouts timeouts) {
return new RetryablePredicate<SshClient>(sshResponds, timeouts.nodeRunning);
return new RetryablePredicate<SshClient>(sshResponds, timeouts.nodeRunning, 500l, TimeUnit.MILLISECONDS);
}
@Override

View File

@ -166,15 +166,12 @@ public class BridgedIf {
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof VmSpec) {
if (o instanceof BridgedIf) {
BridgedIf other = (BridgedIf) o;
return Objects.equal(name, other.name)
&& Objects.equal(guid, other.guid)
&& Objects.equal(dhcp, other.dhcp)
&& Objects.equal(ipAddress, other.ipAddress)
&& Objects.equal(networkMask, other.networkMask)
&& Objects.equal(ipv6Address, other.ipv6Address)
&& Objects.equal(ipv6NetworkMask, other.ipv6NetworkMask)
&& Objects.equal(mediumType, other.mediumType)
&& Objects.equal(status, other.status);
}

View File

@ -167,10 +167,10 @@ public class NetworkAdapter {
@Override
public String toString() {
return "NetworkAdapter{" + "networkAttachmentType= "+
return "NetworkAdapter{networkAttachmentType="+
networkAttachmentType +
"macAddress= " + macAddress +
"redirectRules= " + redirectRules +
", macAddress=" + macAddress +
", redirectRules=" + redirectRules +
'}';
}
}

View File

@ -101,9 +101,9 @@ public class NetworkInterfaceCard {
@Override
public String toString() {
return "NetworkInterfaceCard{" + "slot= "+
return "NetworkInterfaceCard{slot="+
slot +
"networkAdapter= " + networkAdapter +
", networkAdapter=" + networkAdapter +
'}';
}

View File

@ -33,36 +33,32 @@ import com.google.common.base.Function;
*/
public class AttachNicToMachine implements Function<NetworkInterfaceCard, Void> {
private final String vmName;
private final MachineUtils machineUtils;
private final String vmName;
private final MachineUtils machineUtils;
public AttachNicToMachine(String vmName, MachineUtils machineUtils) {
this.vmName = checkNotNull(vmName, "vmName");
this.machineUtils = checkNotNull(machineUtils, "machineUtils");
}
public AttachNicToMachine(String vmName, MachineUtils machineUtils) {
this.vmName = checkNotNull(vmName, "vmName");
this.machineUtils = checkNotNull(machineUtils, "machineUtils");
}
@Override
@Override
public Void apply(@Nullable NetworkInterfaceCard nic) {
if(hasNatAdapter(nic)) {
return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic));
} else if (hasBridgedAdapter(nic)) {
return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic));
if (hasNatAdapter(nic)) {
return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic));
} else if (hasBridgedAdapter(nic)) {
return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic));
} else if (hasHostOnlyAdapter(nic)) {
return machineUtils.writeLockMachineAndApply(vmName, new AttachHostOnlyAdapter(nic));
} else
return null;
}
} else
return null;
}
private boolean hasNatAdapter(NetworkInterfaceCard nic) {
return nic.getNetworkAdapter().getNetworkAttachmentType()
.equals(NetworkAttachmentType.NAT);
}
private boolean hasNatAdapter(NetworkInterfaceCard nic) {
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.NAT);
}
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
return nic.getNetworkAdapter().getNetworkAttachmentType()
.equals(NetworkAttachmentType.Bridged);
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.Bridged);
}
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {

View File

@ -112,7 +112,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine, CloneMode.MachineState, options);
progress.waitForCompletion(-1);
logger.debug("clone done");
logger.debug(String.format("Machine %s is cloned correctly", clonedMachine.getName()));
// registering
manager.get().getVBox().registerMachine(clonedMachine);

View File

@ -48,6 +48,7 @@ import org.virtualbox_4_1.IMachine;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Futures;
@ -105,19 +106,20 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
configureOsInstallationWithKeyboardSequence(vmName, installationKeySequence);
// the OS installation is a long process: let's delay the check for ssh of 30 sec
try {
Thread.sleep(30000l);
} catch (InterruptedException e) {
Throwables.propagate(e);
}
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);
logger.debug(">> awaiting installation of guest additions on vm: %s", vmName);
NodeMetadata nodeMetadata = imachineToNodeMetadata.apply(vm);
ListenableFuture<ExecResponse> execInstallGA = machineUtils.runScriptOnNode(nodeMetadata,
new InstallGuestAdditions(vmSpec, version), RunScriptOptions.NONE);
ExecResponse gaInstallationResponse = Futures.getUnchecked(execInstallGA);
checkState(gaInstallationResponse.getExitStatus() == 0);
logger.debug(">> awaiting post-installation actions on vm: %s", vmName);
ListenableFuture<ExecResponse> execCleanup = machineUtils.runScriptOnNode(nodeMetadata,
@ -125,9 +127,15 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
ExecResponse cleanupResponse = Futures.getUnchecked(execCleanup);
checkState(cleanupResponse.getExitStatus() == 0);
logger.debug("<< installation of image complete. Powering down node(%s)", vmName);
machineController.ensureMachineHasPowerDown(vmName);
logger.debug(">> awaiting installation of guest additions on vm: %s", vmName);
ListenableFuture<ExecResponse> execInstallGA = machineUtils.runScriptOnNode(nodeMetadata,
new InstallGuestAdditions(vmSpec, version), RunScriptOptions.NONE);
ExecResponse gaInstallationResponse = Futures.getUnchecked(execInstallGA);
checkState(gaInstallationResponse.getExitStatus() == 0);
machineController.ensureMachineIsShutdown(vmName);
return vm;
}

View File

@ -19,12 +19,12 @@
package org.jclouds.virtualbox.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule.machineToNodeState;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Named;
@ -37,6 +37,8 @@ import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.util.MachineUtils;
import org.testng.collections.Lists;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INetworkAdapter;
import org.virtualbox_4_1.MachineState;
@ -46,13 +48,23 @@ import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final MachineUtils machineUtils;
@Inject
public IMachineToNodeMetadata(MachineUtils machineUtils) {
this.machineUtils = machineUtils;
}
@Override
public NodeMetadata apply(@Nullable IMachine vm) {
@ -85,6 +97,7 @@ public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata>
logger.debug("Setting virtualbox node to: " + nodeState + " from machine state: " + vmState);
/*
// nat adapter
INetworkAdapter natAdapter = vm.getNetworkAdapter(0l);
checkNotNull(natAdapter, "slot 0 networkadapter");
@ -115,11 +128,53 @@ public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata>
nodeMetadataBuilder.privateAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + ""));
nodeMetadataBuilder.publicAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + ""));
}
*/
nodeMetadataBuilder = getIpAddresses(vm, nodeMetadataBuilder);
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
nodeMetadataBuilder.credentials(loginCredentials);
return nodeMetadataBuilder.build();
}
private NodeMetadataBuilder getIpAddresses(IMachine vm, NodeMetadataBuilder nodeMetadataBuilder) {
List<String> publicIpAddresses = Lists.newArrayList();
List<String> privateIpAddresses = Lists.newArrayList();
for(long slot = 0; slot < 4; slot ++) {
INetworkAdapter adapter = vm.getNetworkAdapter(slot);
if(adapter != null) {
if (adapter.getAttachmentType() == NetworkAttachmentType.NAT) {
publicIpAddresses.add(adapter.getNatDriver().getHostIP());
for (String nameProtocolnumberAddressInboudportGuestTargetport : adapter.getNatDriver().getRedirects()) {
Iterable<String> stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport);
String protocolNumber = Iterables.get(stuff, 1);
String hostAddress = Iterables.get(stuff, 2);
String inboundPort = Iterables.get(stuff, 3);
String targetPort = Iterables.get(stuff, 5);
if ("1".equals(protocolNumber) && "22".equals(targetPort)) {
int inPort = Integer.parseInt(inboundPort);
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(hostAddress)).loginPort(inPort);
}
//privateIpAddresses.add((NodeCreator.VMS_NETWORK + ipTermination) + "");
}
// TODO this could be a public and private address
} else if (adapter.getAttachmentType() == NetworkAttachmentType.Bridged) {
String clientIpAddress = machineUtils.getIpAddressFromBridgedNIC(vm.getName());
//privateIpAddresses.add(clientIpAddress);
publicIpAddresses.add(clientIpAddress);
} else if (adapter.getAttachmentType() == NetworkAttachmentType.HostOnly) {
String clientIpAddress = machineUtils.getIpAddressFromHostOnlyNIC(vm.getName());
publicIpAddresses.add(clientIpAddress);
}
}
}
nodeMetadataBuilder.publicAddresses(publicIpAddresses);
nodeMetadataBuilder.privateAddresses(privateIpAddresses);
return nodeMetadataBuilder;
}
}

View File

@ -39,6 +39,7 @@ import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.domain.BridgedIf;
import org.jclouds.virtualbox.statements.GetIPAddressFromMAC;
import org.jclouds.virtualbox.statements.ScanNetworkWithPing;
import org.jclouds.virtualbox.util.MachineUtils;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INetworkAdapter;
import org.virtualbox_4_1.NetworkAttachmentType;
@ -59,14 +60,16 @@ public class IMachineToSshClient implements Function<IMachine, SshClient> {
private final SshClient.Factory sshClientFactory;
private final RunScriptOnNode.Factory scriptRunnerFactory;
private final Supplier<NodeMetadata> hostSupplier;
private final MachineUtils machineUtils;
@Inject
public IMachineToSshClient(SshClient.Factory sshClientFactory,
RunScriptOnNode.Factory scriptRunnerFactory,
Supplier<NodeMetadata> hostSupplier) {
Supplier<NodeMetadata> hostSupplier, MachineUtils machineUtils) {
this.sshClientFactory = sshClientFactory;
this.scriptRunnerFactory = scriptRunnerFactory;
this.hostSupplier = hostSupplier;
this.machineUtils = machineUtils;
}
@Override
@ -104,6 +107,9 @@ public class IMachineToSshClient implements Function<IMachine, SshClient> {
NetworkAttachmentType.Bridged)) {
String network = "1.1.1.1";
clientIpAddress = getIpAddressFromBridgedNIC(networkAdapter, network);
} else if (networkAdapter.getAttachmentType().equals(
NetworkAttachmentType.HostOnly)) {
clientIpAddress = machineUtils.getIpAddressFromHostOnlyNIC(vm.getName());
}
checkNotNull(clientIpAddress, "clientIpAddress");

View File

@ -76,6 +76,11 @@ public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, ISes
final IProgress progress = machine
.launchVMProcess(session, type.stringValue(), environment);
progress.waitForCompletion(-1);
try {
Thread.sleep(3000l);
} catch (InterruptedException e) {
propagate(e);
}
} catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e);
switch (errorCode) {

View File

@ -82,6 +82,7 @@ public class MastersLoadingCache extends AbstractLoadingCache<Image, Master> {
// TODO parameterize
public static final int MASTER_PORT = 2222;
public static final String HOST_ONLY_IFACE_NAME = "vboxnet0";
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)

View File

@ -24,8 +24,6 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -44,49 +42,35 @@ import org.jclouds.virtualbox.domain.NetworkSpec;
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.statements.EnableNetworkInterface;
import org.jclouds.virtualbox.util.MachineController;
import org.jclouds.virtualbox.util.MachineUtils;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INetworkAdapter;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.NetworkAttachmentType;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* Creates nodes, by cloning a master vm and based on the provided {@link NodeSpec}. Must be
* synchronized mainly because of snapshot creation (must be synchronized on a per-master-basis).
*
* @author David Alves
* @author David Alves, Andrea Turli
*
*/
@Singleton
public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials<IMachine>> {
// TODO parameterize
public static final int NODE_PORT_INIT = 3000;
// TODO parameterize
public static final String VMS_NETWORK = "192.168.86.";
// TODO parameterize
public static final String HOST_ONLY_IFACE_NAME = "vboxnet0";
// TODO parameterize
public static final boolean USE_LINKED = true;
private final Supplier<VirtualBoxManager> manager;
private final Function<CloneSpec, IMachine> cloner;
private final AtomicInteger nodePorts;
private final AtomicInteger nodeIps;
private final MachineUtils machineUtils;
private final MachineController machineController;
@ -95,8 +79,6 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
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;
}
@ -130,18 +112,18 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
// CASE NAT + HOST-ONLY
NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", this.nodePorts.getAndIncrement(), "", 22).build();
NetworkInterfaceCard natIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(natAdapter).slot(0L).build();
.build();
NetworkInterfaceCard natIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(natAdapter).slot(1L).build();
NetworkAdapter hostOnlyAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.HostOnly)
.staticIp(VMS_NETWORK + this.nodeIps.getAndIncrement()).build();
.build();
NetworkInterfaceCard hostOnlyIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(hostOnlyAdapter)
.addHostInterfaceName(HOST_ONLY_IFACE_NAME).slot(1L).build();
.addHostInterfaceName(HOST_ONLY_IFACE_NAME).slot(0L).build();
NetworkSpec networkSpec = createNetworkSpecForHostOnlyNATNICs(natIfaceCard, hostOnlyIfaceCard);
CloneSpec cloneSpec = CloneSpec.builder().linked(USE_LINKED).master(master.getMachine()).network(networkSpec)
CloneSpec cloneSpec = CloneSpec.builder().linked(true).master(master.getMachine()).network(networkSpec)
.vm(cloneVmSpec).build();
IMachine cloned = cloner.apply(cloneSpec);
@ -156,8 +138,8 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
// CASE NAT + HOST-ONLY
machineUtils.runScriptOnNode(partialNodeMetadata, new SetIpAddress(hostOnlyIfaceCard), RunScriptOptions.NONE);
// //
machineUtils.runScriptOnNode(partialNodeMetadata, new EnableNetworkInterface(natIfaceCard), RunScriptOptions.NONE);
// TODO get credentials from somewhere else (they are also HC in
// IMachineToSshClient)
@ -168,21 +150,10 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
}
private NodeMetadata buildPartialNodeMetadata(IMachine clone) {
INetworkAdapter realNatAdapter = clone.getNetworkAdapter(0l);
NodeMetadataBuilder nodeMetadataBuilder = new NodeMetadataBuilder();
nodeMetadataBuilder.id(clone.getName());
nodeMetadataBuilder.state(VirtualBoxComputeServiceContextModule.machineToNodeState.get(clone.getState()));
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(realNatAdapter.getNatDriver().getHostIP()));
for (String nameProtocolnumberAddressInboudportGuestTargetport : realNatAdapter.getNatDriver().getRedirects()) {
Iterable<String> stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport);
String protocolNumber = Iterables.get(stuff, 1);
String inboundPort = Iterables.get(stuff, 3);
String targetPort = Iterables.get(stuff, 5);
if ("1".equals(protocolNumber) && "22".equals(targetPort)) {
int inPort = Integer.parseInt(inboundPort);
nodeMetadataBuilder.loginPort(inPort);
}
}
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(machineUtils.getIpAddressFromHostOnlyNIC(clone.getName())));
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
nodeMetadataBuilder.credentials(loginCredentials);

View File

@ -0,0 +1,88 @@
/**
* 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.statements;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import java.util.List;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
import org.testng.collections.Lists;
/**
* Up the network interface chosen
*
* @author Andrea Turli
*
*/
public class EnableNetworkInterface implements Statement {
private final StatementList statements;
public EnableNetworkInterface(NetworkInterfaceCard networkInterfaceCard) {
int slot = (int) networkInterfaceCard.getSlot();
String iface = null;
switch (slot) {
case 0:
iface = "eth0";
break;
case 1:
iface = "eth1";
break;
case 2:
iface = "eth2";
break;
case 3:
iface = "eth3";
break;
default:
throw new IllegalArgumentException("slot must be 0,1,2,3 (was: " + slot + ")");
}
this.statements = new StatementList(getStatements(iface));
}
private List<Statement> getStatements(String iface) {
/*
* auto eth0
*/
List<Statement> statements = Lists.newArrayList();
statements.add(exec(String.format("echo auto %s >> /etc/network/interfaces", iface))); //
statements.add(exec(String.format("echo iface %s inet dhcp >> /etc/network/interfaces", iface))); //
statements.add(exec("/etc/init.d/networking restart"));
return statements;
}
@Override
public Iterable<String> functionDependencies(OsFamily family) {
return statements.functionDependencies(family);
}
@Override
public String render(OsFamily family) {
if (checkNotNull(family, "family") == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
return statements.render(family);
}
}

View File

@ -88,6 +88,8 @@ public class InstallGuestAdditions implements Statement {
statements.add(exec(String.format("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint)));
}
statements.add(exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run"))); //
statements.add(exec("service vboxadd setup")); //
statements.add(exec("VBoxService")); //
statements.add(exec(String.format("echo VBoxService > /etc/rc.local"))); //
statements.add(exec(String.format("echo exit 0 >> /etc/rc.local"))); //
statements.add(exec(String.format("umount %s", mountPoint)));

View File

@ -20,8 +20,6 @@ package org.jclouds.virtualbox.util;
import static com.google.common.base.Preconditions.checkNotNull;
import java.awt.print.Printable;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
@ -38,6 +36,7 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
/**
@ -68,9 +67,11 @@ public class MachineController {
ISession session = null;
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
try {
session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(),
executionType, ""));
} catch (RuntimeException e) {
if (e.getMessage().contains("org.virtualbox_4_1.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) {
if (e.getMessage().contains(
"org.virtualbox_4_1.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) {
throw e;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
@ -83,81 +84,107 @@ public class MachineController {
}
public ISession ensureMachineHasPowerDown(String vmName) {
ISession session = manager.get().getSessionObject();
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) {
try {
session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, ISession>() {
@Override
public ISession apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return session;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: PoweredOff")) {
throw e;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
return checkNotNull(session, "session");
}
ISession session = manager.get().getSessionObject();
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) {
try {
session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
new Function<ISession, ISession>() {
@Override
public ISession apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return session;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: PoweredOff")) {
throw e;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
return checkNotNull(session, "session");
}
public void ensureMachineIsPaused(String vmName) {
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) {
try {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
session.getConsole().pause();
return null;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: Paused")) {
return;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
}
/**
* if machine supports ACPI it can be shutdown gently - not powerdown()
* http://askubuntu.com/questions/82015/shutting-down-ubuntu-server-running-in-headless-virtualbox
*/
public ISession ensureMachineIsShutdown(String vmName) {
ISession session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
new Function<ISession, ISession>() {
@Override
public ISession apply(ISession session) {
session.getConsole().powerButton();
return session;
}
});
int count = 0;
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff) && count < 10) {
try {
Thread.sleep(500l * count);
} catch (InterruptedException e) {
Throwables.propagate(e);
}
count++;
}
return checkNotNull(session, "session");
}
public void ensureMachineIsResumed(String vmName) {
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
try {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
session.getConsole().resume();
return null;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: Resumed")) {
return;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
}
public void ensureMachineIsPaused(String vmName) {
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) {
try {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
session.getConsole().pause();
return null;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: Paused")) {
return;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
}
public void ensureMachineIsResumed(String vmName) {
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
try {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
session.getConsole().resume();
return null;
}
});
} catch (RuntimeException e) {
// sometimes the machine might be powered of between the while
// test and the call to
// lockSessionOnMachineAndApply
if (e.getMessage().contains("Invalid machine state: Resumed")) {
return;
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
continue;
} else {
throw e;
}
}
}
}
}

View File

@ -18,8 +18,11 @@
*/
package org.jclouds.virtualbox.util;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Named;
@ -43,6 +46,7 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
@ -187,7 +191,7 @@ public class MachineUtils {
* @return the result from applying the function to the session.
*/
protected <T> T lockSessionOnMachineAndApply(String machineId, LockType type, Function<ISession, T> function) {
int retries = 5;
int retries = 15;
ISession session = lockSession(machineId, type, retries);
try {
return function.apply(session);
@ -206,10 +210,6 @@ public class MachineUtils {
while (true) {
try {
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
try {
Thread.sleep(300L);
} catch (InterruptedException e1) {
}
session = manager.get().getSessionObject();
immutableMachine.lockMachine(session, type);
break;
@ -264,4 +264,71 @@ public class MachineUtils {
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ")
|| e.getMessage().contains("Could not find a registered machine with UUID {");
}
public String getIpAddressFromBridgedNIC(String machineName) {
String ip = "";
int attempt = 0;
while (!isIpv4(ip) && attempt < 10) {
ip = this.lockSessionOnMachineAndApply(machineName, LockType.Shared, new Function<ISession, String>() {
@Override
public String apply(ISession session) {
String ip = session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
return ip;
}
});
attempt++;
long sleepTime = 1000 * attempt;
logger.debug("Instance %s is still not ready. Attempt n:%d. Sleeping for %d millisec", machineName, attempt,
sleepTime);
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
Throwables.propagate(e);
}
}
return ip;
}
private boolean isIpv4(String s) {
Pattern pattern = Pattern.compile(this.IP_V4_ADDRESS_PATTERN);
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
public String getIpAddressFromHostOnlyNIC(String machineName) {
// TODO using a caching mechanism to avoid to call everytime this vboxmanage api call
String currentIp = "", previousIp = "1.1.1.1";
int attempt = 0, count = 0;
while(count < 5) {
currentIp = "";
attempt = 0;
while (!isIpv4(currentIp) && attempt < 5) {
currentIp = this.lockSessionOnMachineAndApply(machineName, LockType.Shared, new Function<ISession, String>() {
@Override
public String apply(ISession session) {
return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
}
});
attempt++;
}
if(previousIp.equals(currentIp)) {
count++;
delayer(500l * (count + 1));
} else {
count = 0;
delayer(5000l);
}
previousIp = currentIp;
}
return currentIp;
}
private void delayer(long millisec) {
try {
Thread.sleep(millisec);
} catch (InterruptedException e) {
Throwables.propagate(e);
}
}
}

View File

@ -1,9 +1,9 @@
function cleanupUdevIfNeeded {
if [ -f '/etc/udev/rules.d/70-persistent-net.rules' ]
then
rm /etc/udev/rules.d/70-persistent-net.rules;
mkdir /etc/udev/rules.d/70-persistent-net.rules;
rm -rf /dev/.udev/;
rm /etc/udev/rules.d/70-persistent-net.rules
mkdir /etc/udev/rules.d/70-persistent-net.rules
rm -rf /dev/.udev/
rm /lib/udev/rules.d/75-persistent-net-generator.rules
fi
}

View File

@ -4,7 +4,7 @@ function installModuleAssistantIfNeeded {
if [ $OSNAME = 'Ubuntu' ]
then
echo "OS is Ubuntu"
apt-get -f -y -qq --force-yes install dkms build-essential linux-headers-`uname -r` module-assistant;
apt-get -f -y -qq --force-yes install dkms build-essential linux-headers-`uname -r` module-assistant acpid;
m-a prepare -i
return 0
fi

View File

@ -163,24 +163,13 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
checkNotNull(mastersCache.apply(template.getImage()));
}
protected void undoVm(VmSpec vmSpecification) {
machineUtils.writeLockMachineAndApply(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia(
vmSpecification));
}
protected void undoVm(String vmNameOrId) {
IMachine vm = null;
try {
System.out.println("1: " + manager.get().getSessionObject().getState());
vm = manager.get().getVBox().findMachine(vmNameOrId);
VmSpec vmSpec = new IMachineToVmSpec().apply(vm);
System.out.println("2: " + vm.getSessionState());
int attempts = 0;
while (attempts < 10 && !vm.getSessionState().equals(SessionState.Unlocked)) {
// TODO understand why this behavior and see if IEvent can help
System.out.println("Unlocking ...");
attempts++;
try {
Thread.sleep(200l);

View File

@ -50,8 +50,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
public void testCreatedNodeHasExpectedNameAndWeCanConnectViaSsh() {
String group = "foo";
String name = "foo-ef4";
String machineName = VIRTUALBOX_NODE_PREFIX + "default-ubuntu-11.04-i386-" + group + "-" + name;
String machineName = VIRTUALBOX_NODE_PREFIX + "default-ubuntu-11.04-i386-" + "0x0-" + group + "-0x0-" + name;
Template template = context.getComputeService().templateBuilder().build();
machine = adapter.createNodeWithGroupEncodedIntoName(group, name, template);

View File

@ -67,10 +67,9 @@ public class VirtualBoxExperimentLiveTest {
@Test
public void testLaunchCluster() throws RunNodesException {
int numNodes = 4;
int numNodes = 2;
final String clusterName = "test-launch-cluster";
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes,
TemplateOptions.Builder.runScript(AdminAccess.standard()));
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes, TemplateOptions.Builder.runScript(AdminAccess.standard()));
assertEquals(numNodes, nodes.size(), "wrong number of nodes");
for (NodeMetadata node : nodes) {
assertTrue(node.getGroup().equals("test-launch-cluster"));

View File

@ -23,12 +23,9 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.CloneSpec;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
@ -37,11 +34,11 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.NetworkAttachmentType;
import org.virtualbox_4_1.StorageBus;
@ -56,20 +53,14 @@ import com.google.inject.Injector;
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest")
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
private static final boolean IS_LINKED_CLONE = true;
private MasterSpec sourceMachineSpec;
private CleanupMode mode = CleanupMode.Full;
private VmSpec clonedVmSpec;
private NetworkSpec cloneNetworkSpec;
private MasterSpec machineSpec;
private String instanceName;
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
String sourceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
String cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
instanceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
StorageController ideController = StorageController
@ -78,10 +69,10 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk(sourceName)).controllerPort(0).deviceSlot(1)
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512)
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
Injector injector = context.utils().injector();
@ -91,65 +82,47 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
sourceVmSpec.getVmName())).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
.build();
instanceVmSpec.getVmName())).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
this.cloneNetworkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(cloneNetworkSpec).build();
this.clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode)
.forceOverwrite(true).build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
}
@Test
public void testCloneMachineFromAnotherMachine() throws Exception {
CloneSpec cloneSpec = null;
public void testCloneMachineFromAnotherMachine() {
IMachine source = getVmWithGuestAdditionsInstalled();
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
.master(source).linked(true).build();
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
assertEquals(clone.getName(), cloneSpec.getVmSpec().getVmName());
}
private IMachine getVmWithGuestAdditionsInstalled() {
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
try {
IMachine source = getSourceNode();
cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(cloneNetworkSpec).master(source)
.linked(IS_LINKED_CLONE).build();
if (source.getCurrentSnapshot() != null) {
ISession session = manager.get().openMachineSession(source);
session.getConsole().deleteSnapshot(source.getCurrentSnapshot().getId());
session.unlockMachine();
}
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
assertEquals(clone.getName(), cloneSpec.getVmSpec().getVmName());
new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "").apply(clone);
// TODO ssh into the node
} finally {
Set<VmSpec> specs = cloneSpec == null ? ImmutableSet.of(sourceMachineSpec.getVmSpec()) : ImmutableSet.of(
cloneSpec.getVmSpec(), sourceMachineSpec.getVmSpec());
for (VmSpec spec : specs) {
undoVm(spec);
}
Injector injector = context.utils().injector();
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
}
}
private IMachine getSourceNode() {
try {
Injector injector = context.utils().injector();
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(sourceMachineSpec);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId());
@Override
@AfterClass(groups = "live")
protected void tearDown() throws Exception {
for (String vmName : ImmutableSet.of(instanceName)) {
undoVm(vmName);
}
super.tearDown();
}
}

View File

@ -35,7 +35,7 @@ import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.CloneSpec;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
@ -73,13 +73,14 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class));
private VmSpec vmSpecification;
private MasterSpec masterSpec;
private Injector injector;
private Function<IMachine, SshClient> sshClientForIMachine;
private Predicate<SshClient> sshResponds;
private String vmName;
private MasterSpec machineSpec;
private String instanceName;
/*
@Override
@BeforeClass(groups = "live")
public void setupClient() {
@ -97,12 +98,13 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
injector = context.utils().injector();
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
NetworkInterfaceCard networkInterfaceCard1 = NetworkInterfaceCard.builder().slot(0l)
.addNetworkAdapter(natAdapter).build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard1).build();
masterSpec = MasterSpec
.builder()
@ -113,25 +115,51 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
vmSpecification.getVmName())).build()).network(networkSpec).build();
}*/
undoVm(vmSpecification);
}
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
instanceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
@Test
public void testCreateImageMachineFromIso() throws Exception {
IMachine imageMachine = getVmWithGuestAdditionsInstalled();
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
Image newImage = iMachineToImage.apply(imageMachine);
assertEquals(vmName, newImage.getName());
StorageController ideController = StorageController
.builder()
.name("IDE Controller")
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
injector = context.utils().injector();
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
IsoSpec isoSpec = IsoSpec
.builder()
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
instanceVmSpec.getVmName())).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
}
@Test
public void testGuestAdditionsAreInstalled() throws Exception {
try {
IMachine machine = getVmWithGuestAdditionsInstalled();
IMachine machine = cloneFromMaster();
machineController.ensureMachineIsLaunched(machine.getName());
machineUtils.applyForMachine(machine.getName(), new LaunchMachineIfNotAlreadyRunning(manager.get(),
ExecutionType.GUI, ""));
sshClientForIMachine = injector.getInstance(IMachineToSshClient.class);
SshClient client = sshClientForIMachine.apply(machine);
@ -141,35 +169,44 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
String vboxVersion = Iterables.get(
Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
assertEquals(vboxVersion, machineUtils.sharedLockMachineAndApplyToSession(machine.getName(),
new Function<ISession, String>() {
assertEquals(vboxVersion,
machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), new Function<ISession, String>() {
@Override
public String apply(ISession session) {
return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestAdd/Version");
}
}));
} finally {
for (VmSpec spec : ImmutableSet.of(vmSpecification)) {
machineController.ensureMachineHasPowerDown(spec.getVmName());
for (VmSpec spec : ImmutableSet.of(machineSpec.getVmSpec())) {
machineController.ensureMachineIsShutdown(spec.getVmName());
}
}
}
private IMachine cloneFromMaster() {
IMachine source = getVmWithGuestAdditionsInstalled();
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
.master(source).linked(true).build();
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
}
private IMachine getVmWithGuestAdditionsInstalled() {
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
try {
Injector injector = context.utils().injector();
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpec);
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox().findMachine(masterSpec.getVmSpec().getVmId());
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
}
}
@Override
@AfterClass(groups = "live")
protected void tearDown() throws Exception {
for (VmSpec spec : ImmutableSet.of(vmSpecification)) {
undoVm(spec);
for (String vmName : ImmutableSet.of(instanceName)) {
undoVm(vmName);
}
super.tearDown();
}

View File

@ -33,6 +33,7 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
@ -46,95 +47,82 @@ import com.google.inject.Injector;
* @author Mattias Holmqvist
*/
@Test(groups = "live", singleThreaded = true, testName = "CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
BaseVirtualBoxClientLiveTest {
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
private String ideControllerName;
private CleanupMode mode;
private String ideControllerName;
private CleanupMode mode;
private String vmName = "";
@Override
public void setupClient() {
super.setupClient();
ideControllerName = "IDE Controller";
mode = CleanupMode.Full;
}
@Override
public void setupClient() {
super.setupClient();
ideControllerName = "IDE Controller";
mode = CleanupMode.Full;
}
@Test
public void testCreateNewMachine() throws Exception {
String vmName = "jclouds-test-create-1-node";
String vmId = UUID.randomUUID().toString();
@Test
public void testCreateNewMachine() throws Exception {
vmName = "jclouds-test-create-1-node";
String vmId = UUID.randomUUID().toString();
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, guestAdditionsIso).build();
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512)
.controller(ideController).cleanUpMode(mode).osTypeId("Debian")
.forceOverwrite(true).build();
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
.osTypeId("Debian").forceOverwrite(true).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder()
.networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard
.builder().addNetworkAdapter(networkAdapter).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
NetworkSpec networkSpec = NetworkSpec.builder()
.addNIC(networkInterfaceCard).build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
MasterSpec machineSpec = MasterSpec
.builder()
.iso(IsoSpec.builder().sourcePath(operatingSystemIso)
.installationScript("").build()).vm(vmSpec)
.network(networkSpec).build();
undoVm(vmSpec);
try {
IMachine debianNode = context.utils()
.injector()
.getInstance(
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
.apply(machineSpec);
IMachine machine = manager.get().getVBox().findMachine(vmName);
assertEquals(debianNode.getName(), machine.getName());
} finally {
undoVm(vmSpec);
}
}
MasterSpec machineSpec = MasterSpec.builder()
.iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(vmSpec)
.network(networkSpec).build();
// undoVm(vmSpec);
IMachine debianNode = context.utils().injector()
.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec);
IMachine machine = manager.get().getVBox().findMachine(vmName);
assertEquals(debianNode.getName(), machine.getName());
@Test
public void testCreateNewMachineWithBadOsType() throws Exception {
String vmName = "jclouds-test-create-2-node";
String vmId = UUID.randomUUID().toString();
}
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, guestAdditionsIso).build();
@Test
public void testCreateNewMachineWithBadOsType() throws Exception {
vmName = "jclouds-test-create-2-node";
String vmId = UUID.randomUUID().toString();
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512)
.controller(ideController).cleanUpMode(mode)
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso)
.installationScript("").build();
NetworkSpec networkSpec = NetworkSpec.builder().build();
MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec)
.network(networkSpec).build();
undoVm(vmSpec);
try {
Injector injector = context.utils().injector();
injector.getInstance(
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
.apply(machineSpec);
fail();
} catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e);
// According to the documentation VBOX_E_OBJECT_NOT_FOUND
// if osTypeId is not found.
assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND);
} finally {
undoVm(vmSpec);
}
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, guestAdditionsIso).build();
}
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build();
NetworkSpec networkSpec = NetworkSpec.builder().build();
MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec).network(networkSpec).build();
try {
Injector injector = context.utils().injector();
injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec);
fail();
} catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e);
// According to the documentation VBOX_E_OBJECT_NOT_FOUND
// if osTypeId is not found.
assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND);
}
}
@Override
@AfterMethod(groups = "live")
protected void tearDown() throws Exception {
undoVm(vmName);
super.tearDown();
}
}

View File

@ -29,6 +29,7 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.virtualbox.util.MachineUtils;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INATEngine;
@ -62,10 +63,11 @@ public class IMachineToNodeMetadataTest {
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,2222,,22"));
INetworkAdapter hostOnly = createNiceMock(INetworkAdapter.class);
MachineUtils machineUtils = createNiceMock(MachineUtils.class);
replay(vm, nat, natEng, hostOnly);
replay(vm, nat, natEng, hostOnly, machineUtils);
NodeMetadata node = new IMachineToNodeMetadata().apply(vm);
NodeMetadata node = new IMachineToNodeMetadata(machineUtils).apply(vm);
assertEquals(MASTER_NAME, node.getName());
assertEquals(0, node.getPrivateAddresses().size());
@ -99,17 +101,14 @@ public class IMachineToNodeMetadataTest {
expect(nat.getNatDriver()).andReturn(natEng).anyTimes();
expect(natEng.getHostIP()).andReturn("127.0.0.1").once();
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3000,,22"));
MachineUtils machineUtils = createNiceMock(MachineUtils.class);
replay(vm, nat, natEng, hostOnly);
replay(vm, nat, natEng, hostOnly, machineUtils);
NodeMetadata node = new IMachineToNodeMetadata().apply(vm);
NodeMetadata node = new IMachineToNodeMetadata(machineUtils).apply(vm);
assertEquals(name, node.getName());
assertEquals(group, node.getGroup());
assertEquals(1, node.getPrivateAddresses().size());
assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPrivateAddresses(), 0));
assertEquals(1, node.getPublicAddresses().size());
assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPublicAddresses(), 0));
assertEquals(22, node.getLoginPort());
}
}

View File

@ -26,6 +26,7 @@ import java.util.List;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.BridgedIf;
import org.jclouds.virtualbox.domain.BridgedIf.Builder;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
@ -33,8 +34,8 @@ import com.google.common.collect.ImmutableList;
/**
* @author Andrea Turli
*/
@Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest")
public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClientLiveTest {
@Test(singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesExpectTest")
public class RetrieveActiveBridgedInterfacesExpectTest {
public static final String TEST1 = "Name: eth0\n"
+ "GUID: 30687465-0000-4000-8000-00261834d0cb\n" + "Dhcp: Disabled\n"
@ -48,19 +49,16 @@ public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClien
+ "HardwareAddress: 5a:3d:ed:99:3f:ed\n" + "MediumType: Ethernet\n" + "Status: Down\n"
+ "VBoxNetworkName: HostInterfaceNetworking-vbox0\n";
public static final List<String> expectedBridgedInterfaces = ImmutableList.of("eth0", "vbox0");
public static final List<BridgedIf> expectedBridgedInterfaces = ImmutableList.of(
BridgedIf.builder().name("eth0").ip("209.x.x.x").networkMask("255.255.255.0").mediumType("Ethernet")
.status("Up").build(),
BridgedIf.builder().name("vbox0").ip("192.168.56.1").networkMask("255.255.255.0").mediumType("Ethernet")
.status("Down").build());
@Test
public void retrieveBridgedInterfaceNamesTest() {
List<BridgedIf> activeBridgedInterfaces = retrieveBridgedInterfaceNames(TEST1);
assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces);
}
@Test
public void retrieveAvailableBridgedInterfaceInfoTest() {
List<BridgedIf> bridgedInterface = context.utils().injector().getInstance(RetrieveActiveBridgedInterfaces.class)
.apply(host.get());
assertFalse(bridgedInterface.isEmpty());
assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces);
}
}

View File

@ -57,10 +57,11 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
URI provider = URI.create("http://localhost:18083/");
String identity = "adminstrator";
String credential = "12345";
expect(client.seconds(3)).andReturn(client);
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true);
manager.connect(provider.toASCIIString(), identity, credential);
manager.connect(provider.toASCIIString(), "", "");
replay(manager, runScriptOnNodeFactory, client);
@ -85,13 +86,14 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
String identity = "adminstrator";
String credential = "12345";
expect(client.seconds(3)).andReturn(client);
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(false);
expect(
runScriptOnNodeFactory.create(host, Statements.exec("VBoxManage setproperty websrvauthlibrary null"),
runAsRoot(false).wrapInInitScript(false))).andReturn(runScriptOnNode);
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true);
expect(
runScriptOnNodeFactory.create(host, Statements.exec("vboxwebsrv -t 10000 -v -b"), runAsRoot(false)
.wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv"))).andReturn(
@ -99,7 +101,7 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
manager.connect(provider.toASCIIString(), identity, credential);
manager.connect(provider.toASCIIString(), "", "");
replay(manager, runScriptOnNodeFactory, runScriptOnNode, client);
new StartVBoxIfNotAlreadyRunning((Function) Functions.constant(manager), runScriptOnNodeFactory, client,

View File

@ -27,13 +27,10 @@ import static org.testng.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.CloneSpec;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
@ -44,8 +41,7 @@ import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
import org.jclouds.virtualbox.statements.InstallGuestAdditions;
import org.jclouds.virtualbox.functions.IMachineToSshClient;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
@ -56,107 +52,99 @@ import org.virtualbox_4_1.StorageBus;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Injector;
/**
* @author Andrea Turli
*/
@Test(groups = "live", singleThreaded = true, testName = "GuestAdditionsInstallerLiveTest")
public class GuestAdditionsInstallerLiveTest extends
BaseVirtualBoxClientLiveTest {
public class GuestAdditionsInstallerLiveTest extends BaseVirtualBoxClientLiveTest {
private MasterSpec machineSpec;
private Injector injector;
private Function<IMachine, SshClient> sshClientForIMachine;
private Predicate<SshClient> sshResponds;
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
String instanceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
.getSimpleName());
private MasterSpec machineSpec;
StorageController ideController = StorageController
.builder()
.name("IDE Controller")
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk(instanceName))
.controllerPort(0).deviceSlot(1)
.autoDelete(true).build())
.attachISO(1, 1, guestAdditionsIso).build();
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
injector = context.utils().injector();
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName)
.name(instanceName).osTypeId("").memoryMB(512)
.cleanUpMode(CleanupMode.Full).controller(ideController)
.forceOverwrite(true).build();
String instanceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
Injector injector = context.utils().injector();
Function<String, String> configProperties = injector
.getInstance(ValueOfConfigurationKeyOrNull.class);
IsoSpec isoSpec = IsoSpec
.builder()
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(
VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
"HOSTNAME", instanceVmSpec.getVmName()))
.build();
StorageController ideController = StorageController
.builder()
.name("IDE Controller")
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder()
.networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard
.builder().addNetworkAdapter(networkAdapter).build();
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
NetworkSpec networkSpec = NetworkSpec.builder()
.addNIC(networkInterfaceCard).build();
machineSpec = MasterSpec.builder().iso(isoSpec)
.vm(instanceVmSpec).network(networkSpec).build();
}
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
IsoSpec isoSpec = IsoSpec
.builder()
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
instanceVmSpec.getVmName())).build();
@Test
public void testGuestAdditionsAreInstalled() throws Exception {
IMachine machine = null;
try {
machine = cloneFromMaster();
machineUtils.applyForMachine(machine.getName(),
new LaunchMachineIfNotAlreadyRunning(manager.get(),
ExecutionType.GUI, ""));
assertTrue(machineUtils.sharedLockMachineAndApplyToSession(
machine.getName(),
new Function<ISession, Boolean>() {
@Override
public Boolean apply(ISession session) {
String s = session
.getMachine()
.getGuestPropertyValue(
"/VirtualBox/GuestInfo/Net/0/V4/IP");
return isIpv4(s);
}
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
private boolean isIpv4(String s) {
Pattern pattern = Pattern
.compile(machineUtils.IP_V4_ADDRESS_PATTERN);
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
}));
} finally {
for (String vmNameOrId : ImmutableSet.of(machine.getName())) {
machineController.ensureMachineHasPowerDown(vmNameOrId);
undoVm(vmNameOrId);
}
}
}
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
}
@Test
public void testGuestAdditionsAreInstalled() throws Exception {
IMachine machine = null;
try {
machine = cloneFromMaster();
machineController.ensureMachineIsLaunched(machine.getName());
sshClientForIMachine = injector.getInstance(IMachineToSshClient.class);
SshClient client = sshClientForIMachine.apply(machine);
sshResponds = injector.getInstance(SshResponds.class);
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh",
machine.getName());
assertTrue(machineUtils.sharedLockMachineAndApplyToSession(machine.getName(),
new Function<ISession, Boolean>() {
@Override
public Boolean apply(ISession session) {
String s = session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
return isIpv4(s);
}
private boolean isIpv4(String s) {
Pattern pattern = Pattern.compile(machineUtils.IP_V4_ADDRESS_PATTERN);
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
}));
} finally {
for (String vmNameOrId : ImmutableSet.of(machine.getName())) {
machineController.ensureMachineHasPowerDown(vmNameOrId);
undoVm(vmNameOrId);
}
}
}
private IMachine cloneFromMaster() {
IMachine source = getVmWithGuestAdditionsInstalled();
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec())
.network(machineSpec.getNetworkSpec()).master(source).linked(true).build();
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
.master(source).linked(true).build();
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
}

View File

@ -20,11 +20,11 @@
package org.jclouds.virtualbox.predicates;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.jclouds.virtualbox.predicates.IMachinePredicates.isLinkedClone;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.CloneSpec;
import org.jclouds.virtualbox.domain.HardDisk;
@ -36,10 +36,9 @@ import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
import org.testng.annotations.AfterMethod;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
@ -47,6 +46,7 @@ import org.virtualbox_4_1.NetworkAttachmentType;
import org.virtualbox_4_1.StorageBus;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
@ -57,67 +57,74 @@ import com.google.inject.Injector;
@Test(groups = "live", singleThreaded = true, testName = "IMachinePredicatesLiveTest")
public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest {
private String osTypeId = "";
private String ideControllerName = "IDE Controller";
private String cloneName;
private String vmName;
private StorageController masterStorageController;
private MasterSpec masterMachineSpec;
private NetworkSpec networkSpec;
private CloneSpec cloneSpec;
private MasterSpec machineSpec;
private String instanceName;
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
vmName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
instanceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
StorageController ideController = StorageController
.builder()
.name("IDE Controller")
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk(vmName)).autoDelete(true).controllerPort(0)
.deviceSlot(1).build();
masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build();
VmSpec masterSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).osTypeId(osTypeId)
.controller(masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build();
masterMachineSpec = MasterSpec.builder()
.iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(masterSpec)
.network(NetworkSpec.builder().build()).build();
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
.build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
Injector injector = context.utils().injector();
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
IsoSpec isoSpec = IsoSpec
.builder()
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
instanceVmSpec.getVmName())).build();
this.networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
}
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
@Test
public void testLinkedClone() {
}
Injector injector = context.utils().injector();
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
masterMachineSpec);
@Test
public void testCloneMachineFromAnotherMachine() {
IMachine source = getVmWithGuestAdditionsInstalled();
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
.master(source).linked(true).build();
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
assertTrue(isLinkedClone().apply(clone));
}
VmSpec clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full)
.forceOverwrite(true).build();
private IMachine getVmWithGuestAdditionsInstalled() {
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
try {
Injector injector = context.utils().injector();
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
}
}
this.cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(networkSpec).master(master).linked(true).build();
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
.apply(cloneSpec);
assertTrue(isLinkedClone().apply(clone));
}
@BeforeMethod
@AfterMethod
void cleanUpVms() {
Set<VmSpec> specs = cloneSpec != null ? ImmutableSet.of(cloneSpec.getVmSpec(), masterMachineSpec.getVmSpec())
: ImmutableSet.of(masterMachineSpec.getVmSpec());
for (VmSpec spec : specs)
this.undoVm(spec);
@Override
@AfterClass(groups = "live")
protected void tearDown() throws Exception {
for (String vmName : ImmutableSet.of(instanceName)) {
undoVm(vmName);
}
super.tearDown();
}
}

View File

@ -45,7 +45,13 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest
InstallGuestAdditions installer = new InstallGuestAdditions(vmSpecification, "4.1.8");
String scripts = installer.render(OsFamily.UNIX);
assertEquals("installModuleAssistantIfNeeded || return 1\n" + "mount -t iso9660 /dev/sr1 /mnt\n"
+ "/mnt/VBoxLinuxAdditions.run\n" + "umount /mnt\n", scripts);
+ "/mnt/VBoxLinuxAdditions.run\n"
+ "service vboxadd setup\n"
+ "VBoxService\n"
+ "echo VBoxService > /etc/rc.local\n"
+ "echo exit 0 >> /etc/rc.local\n"
+ "umount /mnt\n"
, scripts);
}
public void testIsoNotPresent() {
@ -61,7 +67,12 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest
+ "setupPublicCurl || return 1\n"
+ "(mkdir -p /tmp/ && cd /tmp/ && [ ! -f VBoxGuestAdditions_4.1.8.iso ] && curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -C - -X GET http://download.virtualbox.org/virtualbox/4.1.8/VBoxGuestAdditions_4.1.8.iso >VBoxGuestAdditions_4.1.8.iso)\n"
+ "mount -o loop /tmp/VBoxGuestAdditions_4.1.8.iso /mnt\n"
+ "/mnt/VBoxLinuxAdditions.run\n" + "umount /mnt\n", scripts);
+ "/mnt/VBoxLinuxAdditions.run\n"
+ "service vboxadd setup\n"
+ "VBoxService\n"
+ "echo VBoxService > /etc/rc.local\n"
+ "echo exit 0 >> /etc/rc.local\n"
+ "umount /mnt\n", scripts);
}
}

View File

@ -19,7 +19,6 @@
package org.jclouds.virtualbox.util;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.testng.AssertJUnit.assertTrue;
@ -97,12 +96,10 @@ public class MachineControllerLiveTest extends BaseVirtualBoxClientLiveTest {
@Test
public void testEnsureMachineisLaunchedAndSessionIsUnlocked() {
IMachine vm = cloneFromMaster();
cloneFromMaster();
ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName);
System.out.println("lock - cloneMachineSession " + cloneMachineSession.toString());
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
System.out.println("unlock - cloneMachineSession " + cloneMachineSession.toString());
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
}