mirror of https://github.com/apache/jclouds.git
issue 384: vbox 1.5.1
This commit is contained in:
parent
a2922006c9
commit
e27db18cfd
|
@ -27,6 +27,7 @@ import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
@ -158,7 +159,7 @@ public class VirtualBoxComputeServiceContextModule extends
|
||||||
bind(new TypeLiteral<Function<IMachine, SshClient>>() {
|
bind(new TypeLiteral<Function<IMachine, SshClient>>() {
|
||||||
}).to(IMachineToSshClient.class);
|
}).to(IMachineToSshClient.class);
|
||||||
|
|
||||||
bind(ExecutionType.class).toInstance(ExecutionType.GUI);
|
bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS);
|
||||||
bind(LockType.class).toInstance(LockType.Write);
|
bind(LockType.class).toInstance(LockType.Write);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,7 +214,7 @@ public class VirtualBoxComputeServiceContextModule extends
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
protected Predicate<SshClient> sshResponds(SshResponds sshResponds, Timeouts timeouts) {
|
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
|
@Override
|
||||||
|
|
|
@ -166,15 +166,12 @@ public class BridgedIf {
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o)
|
if (this == o)
|
||||||
return true;
|
return true;
|
||||||
if (o instanceof VmSpec) {
|
if (o instanceof BridgedIf) {
|
||||||
BridgedIf other = (BridgedIf) o;
|
BridgedIf other = (BridgedIf) o;
|
||||||
return Objects.equal(name, other.name)
|
return Objects.equal(name, other.name)
|
||||||
&& Objects.equal(guid, other.guid)
|
|
||||||
&& Objects.equal(dhcp, other.dhcp)
|
&& Objects.equal(dhcp, other.dhcp)
|
||||||
&& Objects.equal(ipAddress, other.ipAddress)
|
&& Objects.equal(ipAddress, other.ipAddress)
|
||||||
&& Objects.equal(networkMask, other.networkMask)
|
&& Objects.equal(networkMask, other.networkMask)
|
||||||
&& Objects.equal(ipv6Address, other.ipv6Address)
|
|
||||||
&& Objects.equal(ipv6NetworkMask, other.ipv6NetworkMask)
|
|
||||||
&& Objects.equal(mediumType, other.mediumType)
|
&& Objects.equal(mediumType, other.mediumType)
|
||||||
&& Objects.equal(status, other.status);
|
&& Objects.equal(status, other.status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,10 +167,10 @@ public class NetworkAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NetworkAdapter{" + "networkAttachmentType= "+
|
return "NetworkAdapter{networkAttachmentType="+
|
||||||
networkAttachmentType +
|
networkAttachmentType +
|
||||||
"macAddress= " + macAddress +
|
", macAddress=" + macAddress +
|
||||||
"redirectRules= " + redirectRules +
|
", redirectRules=" + redirectRules +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,9 +101,9 @@ public class NetworkInterfaceCard {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NetworkInterfaceCard{" + "slot= "+
|
return "NetworkInterfaceCard{slot="+
|
||||||
slot +
|
slot +
|
||||||
"networkAdapter= " + networkAdapter +
|
", networkAdapter=" + networkAdapter +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,36 +33,32 @@ import com.google.common.base.Function;
|
||||||
*/
|
*/
|
||||||
public class AttachNicToMachine implements Function<NetworkInterfaceCard, Void> {
|
public class AttachNicToMachine implements Function<NetworkInterfaceCard, Void> {
|
||||||
|
|
||||||
private final String vmName;
|
private final String vmName;
|
||||||
private final MachineUtils machineUtils;
|
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) {
|
@Override
|
||||||
this.vmName = checkNotNull(vmName, "vmName");
|
|
||||||
this.machineUtils = checkNotNull(machineUtils, "machineUtils");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void apply(@Nullable NetworkInterfaceCard nic) {
|
public Void apply(@Nullable NetworkInterfaceCard nic) {
|
||||||
if(hasNatAdapter(nic)) {
|
if (hasNatAdapter(nic)) {
|
||||||
return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic));
|
return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic));
|
||||||
|
} else if (hasBridgedAdapter(nic)) {
|
||||||
} else if (hasBridgedAdapter(nic)) {
|
return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic));
|
||||||
return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic));
|
|
||||||
} else if (hasHostOnlyAdapter(nic)) {
|
} else if (hasHostOnlyAdapter(nic)) {
|
||||||
return machineUtils.writeLockMachineAndApply(vmName, new AttachHostOnlyAdapter(nic));
|
return machineUtils.writeLockMachineAndApply(vmName, new AttachHostOnlyAdapter(nic));
|
||||||
} else
|
} else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasNatAdapter(NetworkInterfaceCard nic) {
|
private boolean hasNatAdapter(NetworkInterfaceCard nic) {
|
||||||
return nic.getNetworkAdapter().getNetworkAttachmentType()
|
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.NAT);
|
||||||
.equals(NetworkAttachmentType.NAT);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
|
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
|
||||||
return nic.getNetworkAdapter().getNetworkAttachmentType()
|
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.Bridged);
|
||||||
.equals(NetworkAttachmentType.Bridged);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {
|
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {
|
||||||
|
|
|
@ -112,7 +112,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
|
||||||
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine, CloneMode.MachineState, options);
|
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine, CloneMode.MachineState, options);
|
||||||
|
|
||||||
progress.waitForCompletion(-1);
|
progress.waitForCompletion(-1);
|
||||||
logger.debug("clone done");
|
logger.debug(String.format("Machine %s is cloned correctly", clonedMachine.getName()));
|
||||||
|
|
||||||
// registering
|
// registering
|
||||||
manager.get().getVBox().registerMachine(clonedMachine);
|
manager.get().getVBox().registerMachine(clonedMachine);
|
||||||
|
|
|
@ -48,6 +48,7 @@ import org.virtualbox_4_1.IMachine;
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.util.concurrent.Futures;
|
import com.google.common.util.concurrent.Futures;
|
||||||
|
@ -105,19 +106,20 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
|
||||||
|
|
||||||
configureOsInstallationWithKeyboardSequence(vmName, installationKeySequence);
|
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);
|
SshClient client = sshClientForIMachine.apply(vm);
|
||||||
|
|
||||||
logger.debug(">> awaiting installation to finish node(%s)", vmName);
|
logger.debug(">> awaiting installation to finish node(%s)", vmName);
|
||||||
|
|
||||||
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName);
|
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName);
|
||||||
|
|
||||||
logger.debug(">> awaiting installation of guest additions on vm: %s", vmName);
|
|
||||||
|
|
||||||
NodeMetadata nodeMetadata = imachineToNodeMetadata.apply(vm);
|
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);
|
logger.debug(">> awaiting post-installation actions on vm: %s", vmName);
|
||||||
ListenableFuture<ExecResponse> execCleanup = machineUtils.runScriptOnNode(nodeMetadata,
|
ListenableFuture<ExecResponse> execCleanup = machineUtils.runScriptOnNode(nodeMetadata,
|
||||||
|
@ -125,9 +127,15 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
|
||||||
ExecResponse cleanupResponse = Futures.getUnchecked(execCleanup);
|
ExecResponse cleanupResponse = Futures.getUnchecked(execCleanup);
|
||||||
checkState(cleanupResponse.getExitStatus() == 0);
|
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;
|
return vm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,12 +19,12 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.functions;
|
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.VirtualBoxComputeServiceContextModule.machineToNodeState;
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR;
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
@ -37,6 +37,8 @@ import org.jclouds.domain.LocationScope;
|
||||||
import org.jclouds.domain.LoginCredentials;
|
import org.jclouds.domain.LoginCredentials;
|
||||||
import org.jclouds.javax.annotation.Nullable;
|
import org.jclouds.javax.annotation.Nullable;
|
||||||
import org.jclouds.logging.Logger;
|
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.IMachine;
|
||||||
import org.virtualbox_4_1.INetworkAdapter;
|
import org.virtualbox_4_1.INetworkAdapter;
|
||||||
import org.virtualbox_4_1.MachineState;
|
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.base.Splitter;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata> {
|
public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
|
private final MachineUtils machineUtils;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public IMachineToNodeMetadata(MachineUtils machineUtils) {
|
||||||
|
this.machineUtils = machineUtils;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NodeMetadata apply(@Nullable IMachine vm) {
|
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);
|
logger.debug("Setting virtualbox node to: " + nodeState + " from machine state: " + vmState);
|
||||||
|
|
||||||
|
/*
|
||||||
// nat adapter
|
// nat adapter
|
||||||
INetworkAdapter natAdapter = vm.getNetworkAdapter(0l);
|
INetworkAdapter natAdapter = vm.getNetworkAdapter(0l);
|
||||||
checkNotNull(natAdapter, "slot 0 networkadapter");
|
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.privateAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + ""));
|
||||||
nodeMetadataBuilder.publicAddresses(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);
|
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
|
||||||
nodeMetadataBuilder.credentials(loginCredentials);
|
nodeMetadataBuilder.credentials(loginCredentials);
|
||||||
|
|
||||||
return nodeMetadataBuilder.build();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.jclouds.ssh.SshClient;
|
||||||
import org.jclouds.virtualbox.domain.BridgedIf;
|
import org.jclouds.virtualbox.domain.BridgedIf;
|
||||||
import org.jclouds.virtualbox.statements.GetIPAddressFromMAC;
|
import org.jclouds.virtualbox.statements.GetIPAddressFromMAC;
|
||||||
import org.jclouds.virtualbox.statements.ScanNetworkWithPing;
|
import org.jclouds.virtualbox.statements.ScanNetworkWithPing;
|
||||||
|
import org.jclouds.virtualbox.util.MachineUtils;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.INetworkAdapter;
|
import org.virtualbox_4_1.INetworkAdapter;
|
||||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
|
@ -59,14 +60,16 @@ public class IMachineToSshClient implements Function<IMachine, SshClient> {
|
||||||
private final SshClient.Factory sshClientFactory;
|
private final SshClient.Factory sshClientFactory;
|
||||||
private final RunScriptOnNode.Factory scriptRunnerFactory;
|
private final RunScriptOnNode.Factory scriptRunnerFactory;
|
||||||
private final Supplier<NodeMetadata> hostSupplier;
|
private final Supplier<NodeMetadata> hostSupplier;
|
||||||
|
private final MachineUtils machineUtils;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public IMachineToSshClient(SshClient.Factory sshClientFactory,
|
public IMachineToSshClient(SshClient.Factory sshClientFactory,
|
||||||
RunScriptOnNode.Factory scriptRunnerFactory,
|
RunScriptOnNode.Factory scriptRunnerFactory,
|
||||||
Supplier<NodeMetadata> hostSupplier) {
|
Supplier<NodeMetadata> hostSupplier, MachineUtils machineUtils) {
|
||||||
this.sshClientFactory = sshClientFactory;
|
this.sshClientFactory = sshClientFactory;
|
||||||
this.scriptRunnerFactory = scriptRunnerFactory;
|
this.scriptRunnerFactory = scriptRunnerFactory;
|
||||||
this.hostSupplier = hostSupplier;
|
this.hostSupplier = hostSupplier;
|
||||||
|
this.machineUtils = machineUtils;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -104,6 +107,9 @@ public class IMachineToSshClient implements Function<IMachine, SshClient> {
|
||||||
NetworkAttachmentType.Bridged)) {
|
NetworkAttachmentType.Bridged)) {
|
||||||
String network = "1.1.1.1";
|
String network = "1.1.1.1";
|
||||||
clientIpAddress = getIpAddressFromBridgedNIC(networkAdapter, network);
|
clientIpAddress = getIpAddressFromBridgedNIC(networkAdapter, network);
|
||||||
|
} else if (networkAdapter.getAttachmentType().equals(
|
||||||
|
NetworkAttachmentType.HostOnly)) {
|
||||||
|
clientIpAddress = machineUtils.getIpAddressFromHostOnlyNIC(vm.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
checkNotNull(clientIpAddress, "clientIpAddress");
|
checkNotNull(clientIpAddress, "clientIpAddress");
|
||||||
|
|
|
@ -76,6 +76,11 @@ public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, ISes
|
||||||
final IProgress progress = machine
|
final IProgress progress = machine
|
||||||
.launchVMProcess(session, type.stringValue(), environment);
|
.launchVMProcess(session, type.stringValue(), environment);
|
||||||
progress.waitForCompletion(-1);
|
progress.waitForCompletion(-1);
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000l);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
propagate(e);
|
||||||
|
}
|
||||||
} catch (VBoxException e) {
|
} catch (VBoxException e) {
|
||||||
ErrorCode errorCode = ErrorCode.valueOf(e);
|
ErrorCode errorCode = ErrorCode.valueOf(e);
|
||||||
switch (errorCode) {
|
switch (errorCode) {
|
||||||
|
|
|
@ -82,6 +82,7 @@ public class MastersLoadingCache extends AbstractLoadingCache<Image, Master> {
|
||||||
|
|
||||||
// TODO parameterize
|
// TODO parameterize
|
||||||
public static final int MASTER_PORT = 2222;
|
public static final int MASTER_PORT = 2222;
|
||||||
|
public static final String HOST_ONLY_IFACE_NAME = "vboxnet0";
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
|
|
|
@ -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_NAME_SEPARATOR;
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
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.NodeSpec;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
import org.jclouds.virtualbox.statements.DeleteGShadowLock;
|
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.MachineController;
|
||||||
import org.jclouds.virtualbox.util.MachineUtils;
|
import org.jclouds.virtualbox.util.MachineUtils;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.INetworkAdapter;
|
|
||||||
import org.virtualbox_4_1.IProgress;
|
import org.virtualbox_4_1.IProgress;
|
||||||
import org.virtualbox_4_1.ISession;
|
import org.virtualbox_4_1.ISession;
|
||||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Splitter;
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.collect.ImmutableSet;
|
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
|
* 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).
|
* synchronized mainly because of snapshot creation (must be synchronized on a per-master-basis).
|
||||||
*
|
*
|
||||||
* @author David Alves
|
* @author David Alves, Andrea Turli
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials<IMachine>> {
|
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
|
// TODO parameterize
|
||||||
public static final String HOST_ONLY_IFACE_NAME = "vboxnet0";
|
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 Supplier<VirtualBoxManager> manager;
|
||||||
private final Function<CloneSpec, IMachine> cloner;
|
private final Function<CloneSpec, IMachine> cloner;
|
||||||
private final AtomicInteger nodePorts;
|
|
||||||
private final AtomicInteger nodeIps;
|
|
||||||
private final MachineUtils machineUtils;
|
private final MachineUtils machineUtils;
|
||||||
private final MachineController machineController;
|
private final MachineController machineController;
|
||||||
|
|
||||||
|
@ -95,8 +79,6 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
||||||
MachineUtils machineUtils, RunScriptOnNode.Factory scriptRunnerFactory, MachineController machineController) {
|
MachineUtils machineUtils, RunScriptOnNode.Factory scriptRunnerFactory, MachineController machineController) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.cloner = cloner;
|
this.cloner = cloner;
|
||||||
this.nodePorts = new AtomicInteger(NODE_PORT_INIT);
|
|
||||||
this.nodeIps = new AtomicInteger(2);
|
|
||||||
this.machineUtils = machineUtils;
|
this.machineUtils = machineUtils;
|
||||||
this.machineController = machineController;
|
this.machineController = machineController;
|
||||||
}
|
}
|
||||||
|
@ -130,18 +112,18 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
||||||
|
|
||||||
// CASE NAT + HOST-ONLY
|
// CASE NAT + HOST-ONLY
|
||||||
NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
.tcpRedirectRule("127.0.0.1", this.nodePorts.getAndIncrement(), "", 22).build();
|
.build();
|
||||||
NetworkInterfaceCard natIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(natAdapter).slot(0L).build();
|
NetworkInterfaceCard natIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(natAdapter).slot(1L).build();
|
||||||
|
|
||||||
NetworkAdapter hostOnlyAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.HostOnly)
|
NetworkAdapter hostOnlyAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.HostOnly)
|
||||||
.staticIp(VMS_NETWORK + this.nodeIps.getAndIncrement()).build();
|
.build();
|
||||||
|
|
||||||
NetworkInterfaceCard hostOnlyIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(hostOnlyAdapter)
|
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);
|
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();
|
.vm(cloneVmSpec).build();
|
||||||
|
|
||||||
IMachine cloned = cloner.apply(cloneSpec);
|
IMachine cloned = cloner.apply(cloneSpec);
|
||||||
|
@ -156,8 +138,8 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
||||||
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
|
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
|
||||||
|
|
||||||
// CASE NAT + HOST-ONLY
|
// 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
|
// TODO get credentials from somewhere else (they are also HC in
|
||||||
// IMachineToSshClient)
|
// IMachineToSshClient)
|
||||||
|
@ -168,21 +150,10 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodeMetadata buildPartialNodeMetadata(IMachine clone) {
|
private NodeMetadata buildPartialNodeMetadata(IMachine clone) {
|
||||||
INetworkAdapter realNatAdapter = clone.getNetworkAdapter(0l);
|
|
||||||
NodeMetadataBuilder nodeMetadataBuilder = new NodeMetadataBuilder();
|
NodeMetadataBuilder nodeMetadataBuilder = new NodeMetadataBuilder();
|
||||||
nodeMetadataBuilder.id(clone.getName());
|
nodeMetadataBuilder.id(clone.getName());
|
||||||
nodeMetadataBuilder.state(VirtualBoxComputeServiceContextModule.machineToNodeState.get(clone.getState()));
|
nodeMetadataBuilder.state(VirtualBoxComputeServiceContextModule.machineToNodeState.get(clone.getState()));
|
||||||
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(realNatAdapter.getNatDriver().getHostIP()));
|
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(machineUtils.getIpAddressFromHostOnlyNIC(clone.getName())));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
|
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
|
||||||
nodeMetadataBuilder.credentials(loginCredentials);
|
nodeMetadataBuilder.credentials(loginCredentials);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint)));
|
||||||
}
|
}
|
||||||
statements.add(exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run"))); //
|
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 VBoxService > /etc/rc.local"))); //
|
||||||
statements.add(exec(String.format("echo exit 0 >> /etc/rc.local"))); //
|
statements.add(exec(String.format("echo exit 0 >> /etc/rc.local"))); //
|
||||||
statements.add(exec(String.format("umount %s", mountPoint)));
|
statements.add(exec(String.format("umount %s", mountPoint)));
|
||||||
|
|
|
@ -20,8 +20,6 @@ package org.jclouds.virtualbox.util;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
import java.awt.print.Printable;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
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.Function;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,9 +67,11 @@ public class MachineController {
|
||||||
ISession session = null;
|
ISession session = null;
|
||||||
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
|
||||||
try {
|
try {
|
||||||
session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
|
session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(),
|
||||||
|
executionType, ""));
|
||||||
} catch (RuntimeException e) {
|
} 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;
|
throw e;
|
||||||
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -83,81 +84,107 @@ public class MachineController {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISession ensureMachineHasPowerDown(String vmName) {
|
public ISession ensureMachineHasPowerDown(String vmName) {
|
||||||
ISession session = manager.get().getSessionObject();
|
ISession session = manager.get().getSessionObject();
|
||||||
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) {
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) {
|
||||||
try {
|
try {
|
||||||
session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, ISession>() {
|
session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
|
||||||
@Override
|
new Function<ISession, ISession>() {
|
||||||
public ISession apply(ISession session) {
|
@Override
|
||||||
IProgress powerDownProgress = session.getConsole().powerDown();
|
public ISession apply(ISession session) {
|
||||||
powerDownProgress.waitForCompletion(-1);
|
IProgress powerDownProgress = session.getConsole().powerDown();
|
||||||
return session;
|
powerDownProgress.waitForCompletion(-1);
|
||||||
}
|
return session;
|
||||||
});
|
}
|
||||||
} catch (RuntimeException e) {
|
});
|
||||||
// sometimes the machine might be powered of between the while
|
} catch (RuntimeException e) {
|
||||||
// test and the call to
|
// sometimes the machine might be powered of between the while
|
||||||
// lockSessionOnMachineAndApply
|
// test and the call to
|
||||||
if (e.getMessage().contains("Invalid machine state: PoweredOff")) {
|
// lockSessionOnMachineAndApply
|
||||||
throw e;
|
if (e.getMessage().contains("Invalid machine state: PoweredOff")) {
|
||||||
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
throw e;
|
||||||
continue;
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
} else {
|
continue;
|
||||||
throw e;
|
} else {
|
||||||
}
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return checkNotNull(session, "session");
|
}
|
||||||
}
|
return checkNotNull(session, "session");
|
||||||
|
}
|
||||||
|
|
||||||
public void ensureMachineIsPaused(String vmName) {
|
/**
|
||||||
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) {
|
* if machine supports ACPI it can be shutdown gently - not powerdown()
|
||||||
try {
|
* http://askubuntu.com/questions/82015/shutting-down-ubuntu-server-running-in-headless-virtualbox
|
||||||
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
|
*/
|
||||||
@Override
|
public ISession ensureMachineIsShutdown(String vmName) {
|
||||||
public Void apply(ISession session) {
|
ISession session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
|
||||||
session.getConsole().pause();
|
new Function<ISession, ISession>() {
|
||||||
return null;
|
@Override
|
||||||
}
|
public ISession apply(ISession session) {
|
||||||
});
|
session.getConsole().powerButton();
|
||||||
} catch (RuntimeException e) {
|
return session;
|
||||||
// sometimes the machine might be powered of between the while
|
}
|
||||||
// test and the call to
|
});
|
||||||
// lockSessionOnMachineAndApply
|
int count = 0;
|
||||||
if (e.getMessage().contains("Invalid machine state: Paused")) {
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff) && count < 10) {
|
||||||
return;
|
try {
|
||||||
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
Thread.sleep(500l * count);
|
||||||
continue;
|
} catch (InterruptedException e) {
|
||||||
} else {
|
Throwables.propagate(e);
|
||||||
throw e;
|
}
|
||||||
}
|
count++;
|
||||||
}
|
}
|
||||||
}
|
return checkNotNull(session, "session");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureMachineIsResumed(String vmName) {
|
public void ensureMachineIsPaused(String vmName) {
|
||||||
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) {
|
||||||
try {
|
try {
|
||||||
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
|
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
|
||||||
@Override
|
@Override
|
||||||
public Void apply(ISession session) {
|
public Void apply(ISession session) {
|
||||||
session.getConsole().resume();
|
session.getConsole().pause();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
// sometimes the machine might be powered of between the while
|
// sometimes the machine might be powered of between the while
|
||||||
// test and the call to
|
// test and the call to
|
||||||
// lockSessionOnMachineAndApply
|
// lockSessionOnMachineAndApply
|
||||||
if (e.getMessage().contains("Invalid machine state: Resumed")) {
|
if (e.getMessage().contains("Invalid machine state: Paused")) {
|
||||||
return;
|
return;
|
||||||
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,11 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.virtualbox.util;
|
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.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.annotation.Resource;
|
||||||
import javax.inject.Named;
|
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.Function;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
@ -187,7 +191,7 @@ public class MachineUtils {
|
||||||
* @return the result from applying the function to the session.
|
* @return the result from applying the function to the session.
|
||||||
*/
|
*/
|
||||||
protected <T> T lockSessionOnMachineAndApply(String machineId, LockType type, Function<ISession, T> function) {
|
protected <T> T lockSessionOnMachineAndApply(String machineId, LockType type, Function<ISession, T> function) {
|
||||||
int retries = 5;
|
int retries = 15;
|
||||||
ISession session = lockSession(machineId, type, retries);
|
ISession session = lockSession(machineId, type, retries);
|
||||||
try {
|
try {
|
||||||
return function.apply(session);
|
return function.apply(session);
|
||||||
|
@ -206,10 +210,6 @@ public class MachineUtils {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
|
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
|
||||||
try {
|
|
||||||
Thread.sleep(300L);
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
}
|
|
||||||
session = manager.get().getSessionObject();
|
session = manager.get().getSessionObject();
|
||||||
immutableMachine.lockMachine(session, type);
|
immutableMachine.lockMachine(session, type);
|
||||||
break;
|
break;
|
||||||
|
@ -264,4 +264,71 @@ public class MachineUtils {
|
||||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ")
|
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ")
|
||||||
|| e.getMessage().contains("Could not find a registered machine with UUID {");
|
|| 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
function cleanupUdevIfNeeded {
|
function cleanupUdevIfNeeded {
|
||||||
if [ -f '/etc/udev/rules.d/70-persistent-net.rules' ]
|
if [ -f '/etc/udev/rules.d/70-persistent-net.rules' ]
|
||||||
then
|
then
|
||||||
rm /etc/udev/rules.d/70-persistent-net.rules;
|
rm /etc/udev/rules.d/70-persistent-net.rules
|
||||||
mkdir /etc/udev/rules.d/70-persistent-net.rules;
|
mkdir /etc/udev/rules.d/70-persistent-net.rules
|
||||||
rm -rf /dev/.udev/;
|
rm -rf /dev/.udev/
|
||||||
rm /lib/udev/rules.d/75-persistent-net-generator.rules
|
rm /lib/udev/rules.d/75-persistent-net-generator.rules
|
||||||
fi
|
fi
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ function installModuleAssistantIfNeeded {
|
||||||
if [ $OSNAME = 'Ubuntu' ]
|
if [ $OSNAME = 'Ubuntu' ]
|
||||||
then
|
then
|
||||||
echo "OS is Ubuntu"
|
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
|
m-a prepare -i
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -163,24 +163,13 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
checkNotNull(mastersCache.apply(template.getImage()));
|
checkNotNull(mastersCache.apply(template.getImage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void undoVm(VmSpec vmSpecification) {
|
|
||||||
machineUtils.writeLockMachineAndApply(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia(
|
|
||||||
vmSpecification));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void undoVm(String vmNameOrId) {
|
protected void undoVm(String vmNameOrId) {
|
||||||
IMachine vm = null;
|
IMachine vm = null;
|
||||||
try {
|
try {
|
||||||
System.out.println("1: " + manager.get().getSessionObject().getState());
|
|
||||||
|
|
||||||
vm = manager.get().getVBox().findMachine(vmNameOrId);
|
vm = manager.get().getVBox().findMachine(vmNameOrId);
|
||||||
VmSpec vmSpec = new IMachineToVmSpec().apply(vm);
|
VmSpec vmSpec = new IMachineToVmSpec().apply(vm);
|
||||||
System.out.println("2: " + vm.getSessionState());
|
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
while (attempts < 10 && !vm.getSessionState().equals(SessionState.Unlocked)) {
|
while (attempts < 10 && !vm.getSessionState().equals(SessionState.Unlocked)) {
|
||||||
// TODO understand why this behavior and see if IEvent can help
|
|
||||||
System.out.println("Unlocking ...");
|
|
||||||
attempts++;
|
attempts++;
|
||||||
try {
|
try {
|
||||||
Thread.sleep(200l);
|
Thread.sleep(200l);
|
||||||
|
|
|
@ -50,8 +50,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
|
||||||
public void testCreatedNodeHasExpectedNameAndWeCanConnectViaSsh() {
|
public void testCreatedNodeHasExpectedNameAndWeCanConnectViaSsh() {
|
||||||
String group = "foo";
|
String group = "foo";
|
||||||
String name = "foo-ef4";
|
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();
|
Template template = context.getComputeService().templateBuilder().build();
|
||||||
machine = adapter.createNodeWithGroupEncodedIntoName(group, name, template);
|
machine = adapter.createNodeWithGroupEncodedIntoName(group, name, template);
|
||||||
|
|
||||||
|
|
|
@ -67,10 +67,9 @@ public class VirtualBoxExperimentLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLaunchCluster() throws RunNodesException {
|
public void testLaunchCluster() throws RunNodesException {
|
||||||
int numNodes = 4;
|
int numNodes = 2;
|
||||||
final String clusterName = "test-launch-cluster";
|
final String clusterName = "test-launch-cluster";
|
||||||
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes,
|
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes, TemplateOptions.Builder.runScript(AdminAccess.standard()));
|
||||||
TemplateOptions.Builder.runScript(AdminAccess.standard()));
|
|
||||||
assertEquals(numNodes, nodes.size(), "wrong number of nodes");
|
assertEquals(numNodes, nodes.size(), "wrong number of nodes");
|
||||||
for (NodeMetadata node : nodes) {
|
for (NodeMetadata node : nodes) {
|
||||||
assertTrue(node.getGroup().equals("test-launch-cluster"));
|
assertTrue(node.getGroup().equals("test-launch-cluster"));
|
||||||
|
|
|
@ -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.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.CloneSpec;
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
import org.jclouds.virtualbox.domain.ExecutionType;
|
|
||||||
import org.jclouds.virtualbox.domain.HardDisk;
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
import org.jclouds.virtualbox.domain.IsoSpec;
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
import org.jclouds.virtualbox.domain.MasterSpec;
|
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.NetworkSpec;
|
||||||
import org.jclouds.virtualbox.domain.StorageController;
|
import org.jclouds.virtualbox.domain.StorageController;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
|
import org.testng.annotations.AfterClass;
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.ISession;
|
|
||||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
import org.virtualbox_4_1.StorageBus;
|
import org.virtualbox_4_1.StorageBus;
|
||||||
|
|
||||||
|
@ -56,20 +53,14 @@ import com.google.inject.Injector;
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest")
|
||||||
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
|
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
private static final boolean IS_LINKED_CLONE = true;
|
private MasterSpec machineSpec;
|
||||||
|
private String instanceName;
|
||||||
private MasterSpec sourceMachineSpec;
|
|
||||||
private CleanupMode mode = CleanupMode.Full;
|
|
||||||
private VmSpec clonedVmSpec;
|
|
||||||
private NetworkSpec cloneNetworkSpec;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@BeforeClass(groups = "live")
|
@BeforeClass(groups = "live")
|
||||||
public void setupClient() {
|
public void setupClient() {
|
||||||
super.setupClient();
|
super.setupClient();
|
||||||
String sourceName = VIRTUALBOX_IMAGE_PREFIX
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
|
||||||
String cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
|
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
StorageController ideController = StorageController
|
StorageController ideController = StorageController
|
||||||
|
@ -78,10 +69,10 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten
|
||||||
.bus(StorageBus.IDE)
|
.bus(StorageBus.IDE)
|
||||||
.attachISO(0, 0, operatingSystemIso)
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
.attachHardDisk(
|
.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();
|
.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();
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
|
|
||||||
Injector injector = context.utils().injector();
|
Injector injector = context.utils().injector();
|
||||||
|
@ -91,65 +82,47 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten
|
||||||
.sourcePath(operatingSystemIso)
|
.sourcePath(operatingSystemIso)
|
||||||
.installationScript(
|
.installationScript(
|
||||||
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
sourceVmSpec.getVmName())).build();
|
instanceVmSpec.getVmName())).build();
|
||||||
|
|
||||||
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
|
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
this.cloneNetworkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
||||||
|
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCloneMachineFromAnotherMachine() throws Exception {
|
public void testCloneMachineFromAnotherMachine() {
|
||||||
CloneSpec cloneSpec = null;
|
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 {
|
try {
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
IMachine source = getSourceNode();
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(cloneNetworkSpec).master(source)
|
// already created
|
||||||
.linked(IS_LINKED_CLONE).build();
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IMachine getSourceNode() {
|
@Override
|
||||||
try {
|
@AfterClass(groups = "live")
|
||||||
Injector injector = context.utils().injector();
|
protected void tearDown() throws Exception {
|
||||||
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(sourceMachineSpec);
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
} catch (IllegalStateException e) {
|
undoVm(vmName);
|
||||||
// already created
|
|
||||||
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId());
|
|
||||||
}
|
}
|
||||||
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -35,7 +35,7 @@ import org.jclouds.json.Json;
|
||||||
import org.jclouds.json.config.GsonModule;
|
import org.jclouds.json.config.GsonModule;
|
||||||
import org.jclouds.ssh.SshClient;
|
import org.jclouds.ssh.SshClient;
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
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.HardDisk;
|
||||||
import org.jclouds.virtualbox.domain.IsoSpec;
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
import org.jclouds.virtualbox.domain.MasterSpec;
|
import org.jclouds.virtualbox.domain.MasterSpec;
|
||||||
|
@ -73,13 +73,14 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
|
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
|
||||||
.getInstance(Json.class));
|
.getInstance(Json.class));
|
||||||
|
|
||||||
private VmSpec vmSpecification;
|
|
||||||
private MasterSpec masterSpec;
|
|
||||||
private Injector injector;
|
private Injector injector;
|
||||||
private Function<IMachine, SshClient> sshClientForIMachine;
|
private Function<IMachine, SshClient> sshClientForIMachine;
|
||||||
private Predicate<SshClient> sshResponds;
|
private Predicate<SshClient> sshResponds;
|
||||||
private String vmName;
|
|
||||||
|
|
||||||
|
private MasterSpec machineSpec;
|
||||||
|
private String instanceName;
|
||||||
|
|
||||||
|
/*
|
||||||
@Override
|
@Override
|
||||||
@BeforeClass(groups = "live")
|
@BeforeClass(groups = "live")
|
||||||
public void setupClient() {
|
public void setupClient() {
|
||||||
|
@ -97,12 +98,13 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
injector = context.utils().injector();
|
injector = context.utils().injector();
|
||||||
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
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();
|
.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
|
masterSpec = MasterSpec
|
||||||
.builder()
|
.builder()
|
||||||
|
@ -113,25 +115,51 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
.installationScript(
|
.installationScript(
|
||||||
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
vmSpecification.getVmName())).build()).network(networkSpec).build();
|
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
|
StorageController ideController = StorageController
|
||||||
public void testCreateImageMachineFromIso() throws Exception {
|
.builder()
|
||||||
IMachine imageMachine = getVmWithGuestAdditionsInstalled();
|
.name("IDE Controller")
|
||||||
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
|
.bus(StorageBus.IDE)
|
||||||
Image newImage = iMachineToImage.apply(imageMachine);
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
assertEquals(vmName, newImage.getName());
|
.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
|
@Test
|
||||||
public void testGuestAdditionsAreInstalled() throws Exception {
|
public void testGuestAdditionsAreInstalled() throws Exception {
|
||||||
try {
|
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);
|
sshClientForIMachine = injector.getInstance(IMachineToSshClient.class);
|
||||||
SshClient client = sshClientForIMachine.apply(machine);
|
SshClient client = sshClientForIMachine.apply(machine);
|
||||||
|
|
||||||
|
@ -141,35 +169,44 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
String vboxVersion = Iterables.get(
|
String vboxVersion = Iterables.get(
|
||||||
Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
|
Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
|
||||||
assertEquals(vboxVersion, machineUtils.sharedLockMachineAndApplyToSession(machine.getName(),
|
assertEquals(vboxVersion,
|
||||||
new Function<ISession, String>() {
|
machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), new Function<ISession, String>() {
|
||||||
@Override
|
@Override
|
||||||
public String apply(ISession session) {
|
public String apply(ISession session) {
|
||||||
return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestAdd/Version");
|
return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestAdd/Version");
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
} finally {
|
} finally {
|
||||||
for (VmSpec spec : ImmutableSet.of(vmSpecification)) {
|
for (VmSpec spec : ImmutableSet.of(machineSpec.getVmSpec())) {
|
||||||
machineController.ensureMachineHasPowerDown(spec.getVmName());
|
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() {
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
try {
|
try {
|
||||||
Injector injector = context.utils().injector();
|
Injector injector = context.utils().injector();
|
||||||
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpec);
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
// already created
|
// already created
|
||||||
return manager.get().getVBox().findMachine(masterSpec.getVmSpec().getVmId());
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@AfterClass(groups = "live")
|
@AfterClass(groups = "live")
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
for (VmSpec spec : ImmutableSet.of(vmSpecification)) {
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
undoVm(spec);
|
undoVm(vmName);
|
||||||
}
|
}
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
|
||||||
import org.jclouds.virtualbox.domain.NetworkSpec;
|
import org.jclouds.virtualbox.domain.NetworkSpec;
|
||||||
import org.jclouds.virtualbox.domain.StorageController;
|
import org.jclouds.virtualbox.domain.StorageController;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
@ -46,95 +47,82 @@ import com.google.inject.Injector;
|
||||||
* @author Mattias Holmqvist
|
* @author Mattias Holmqvist
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
|
||||||
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
BaseVirtualBoxClientLiveTest {
|
|
||||||
|
|
||||||
private String ideControllerName;
|
private String ideControllerName;
|
||||||
private CleanupMode mode;
|
private CleanupMode mode;
|
||||||
|
private String vmName = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupClient() {
|
public void setupClient() {
|
||||||
super.setupClient();
|
super.setupClient();
|
||||||
ideControllerName = "IDE Controller";
|
ideControllerName = "IDE Controller";
|
||||||
mode = CleanupMode.Full;
|
mode = CleanupMode.Full;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateNewMachine() throws Exception {
|
public void testCreateNewMachine() throws Exception {
|
||||||
String vmName = "jclouds-test-create-1-node";
|
vmName = "jclouds-test-create-1-node";
|
||||||
String vmId = UUID.randomUUID().toString();
|
String vmId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
|
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
|
||||||
.attachISO(0, 0, operatingSystemIso)
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
||||||
.attachISO(1, 1, guestAdditionsIso).build();
|
.attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512)
|
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
|
||||||
.controller(ideController).cleanUpMode(mode).osTypeId("Debian")
|
.osTypeId("Debian").forceOverwrite(true).build();
|
||||||
.forceOverwrite(true).build();
|
|
||||||
|
|
||||||
NetworkAdapter networkAdapter = NetworkAdapter.builder()
|
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
.networkAttachmentType(NetworkAttachmentType.NAT)
|
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
||||||
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard
|
.build();
|
||||||
.builder().addNetworkAdapter(networkAdapter).build();
|
|
||||||
|
|
||||||
NetworkSpec networkSpec = NetworkSpec.builder()
|
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
||||||
.addNIC(networkInterfaceCard).build();
|
|
||||||
|
|
||||||
MasterSpec machineSpec = MasterSpec
|
MasterSpec machineSpec = MasterSpec.builder()
|
||||||
.builder()
|
.iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(vmSpec)
|
||||||
.iso(IsoSpec.builder().sourcePath(operatingSystemIso)
|
.network(networkSpec).build();
|
||||||
.installationScript("").build()).vm(vmSpec)
|
// undoVm(vmSpec);
|
||||||
.network(networkSpec).build();
|
IMachine debianNode = context.utils().injector()
|
||||||
undoVm(vmSpec);
|
.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec);
|
||||||
try {
|
IMachine machine = manager.get().getVBox().findMachine(vmName);
|
||||||
IMachine debianNode = context.utils()
|
assertEquals(debianNode.getName(), machine.getName());
|
||||||
.injector()
|
|
||||||
.getInstance(
|
|
||||||
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
|
|
||||||
.apply(machineSpec);
|
|
||||||
IMachine machine = manager.get().getVBox().findMachine(vmName);
|
|
||||||
assertEquals(debianNode.getName(), machine.getName());
|
|
||||||
} finally {
|
|
||||||
undoVm(vmSpec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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)
|
@Test
|
||||||
.attachISO(0, 0, operatingSystemIso)
|
public void testCreateNewMachineWithBadOsType() throws Exception {
|
||||||
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
vmName = "jclouds-test-create-2-node";
|
||||||
.attachISO(1, 1, guestAdditionsIso).build();
|
String vmId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512)
|
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
|
||||||
.controller(ideController).cleanUpMode(mode)
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
||||||
IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso)
|
.attachISO(1, 1, guestAdditionsIso).build();
|
||||||
.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
||||||
|
|
||||||
import org.jclouds.compute.domain.NodeMetadata;
|
import org.jclouds.compute.domain.NodeMetadata;
|
||||||
|
import org.jclouds.virtualbox.util.MachineUtils;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.INATEngine;
|
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"));
|
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,2222,,22"));
|
||||||
|
|
||||||
INetworkAdapter hostOnly = createNiceMock(INetworkAdapter.class);
|
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(MASTER_NAME, node.getName());
|
||||||
assertEquals(0, node.getPrivateAddresses().size());
|
assertEquals(0, node.getPrivateAddresses().size());
|
||||||
|
@ -99,17 +101,14 @@ public class IMachineToNodeMetadataTest {
|
||||||
expect(nat.getNatDriver()).andReturn(natEng).anyTimes();
|
expect(nat.getNatDriver()).andReturn(natEng).anyTimes();
|
||||||
expect(natEng.getHostIP()).andReturn("127.0.0.1").once();
|
expect(natEng.getHostIP()).andReturn("127.0.0.1").once();
|
||||||
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3000,,22"));
|
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(name, node.getName());
|
||||||
assertEquals(group, node.getGroup());
|
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(1, node.getPublicAddresses().size());
|
||||||
assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPublicAddresses(), 0));
|
|
||||||
assertEquals(22, node.getLoginPort());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.BridgedIf;
|
import org.jclouds.virtualbox.domain.BridgedIf;
|
||||||
|
import org.jclouds.virtualbox.domain.BridgedIf.Builder;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
@ -33,8 +34,8 @@ import com.google.common.collect.ImmutableList;
|
||||||
/**
|
/**
|
||||||
* @author Andrea Turli
|
* @author Andrea Turli
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest")
|
@Test(singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesExpectTest")
|
||||||
public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClientLiveTest {
|
public class RetrieveActiveBridgedInterfacesExpectTest {
|
||||||
|
|
||||||
public static final String TEST1 = "Name: eth0\n"
|
public static final String TEST1 = "Name: eth0\n"
|
||||||
+ "GUID: 30687465-0000-4000-8000-00261834d0cb\n" + "Dhcp: Disabled\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"
|
+ "HardwareAddress: 5a:3d:ed:99:3f:ed\n" + "MediumType: Ethernet\n" + "Status: Down\n"
|
||||||
+ "VBoxNetworkName: HostInterfaceNetworking-vbox0\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
|
@Test
|
||||||
public void retrieveBridgedInterfaceNamesTest() {
|
public void retrieveBridgedInterfaceNamesTest() {
|
||||||
List<BridgedIf> activeBridgedInterfaces = retrieveBridgedInterfaceNames(TEST1);
|
List<BridgedIf> activeBridgedInterfaces = retrieveBridgedInterfaceNames(TEST1);
|
||||||
assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces);
|
assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void retrieveAvailableBridgedInterfaceInfoTest() {
|
|
||||||
List<BridgedIf> bridgedInterface = context.utils().injector().getInstance(RetrieveActiveBridgedInterfaces.class)
|
|
||||||
.apply(host.get());
|
|
||||||
assertFalse(bridgedInterface.isEmpty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,10 +57,11 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
|
||||||
URI provider = URI.create("http://localhost:18083/");
|
URI provider = URI.create("http://localhost:18083/");
|
||||||
String identity = "adminstrator";
|
String identity = "adminstrator";
|
||||||
String credential = "12345";
|
String credential = "12345";
|
||||||
|
expect(client.seconds(3)).andReturn(client);
|
||||||
|
|
||||||
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true);
|
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);
|
replay(manager, runScriptOnNodeFactory, client);
|
||||||
|
|
||||||
|
@ -85,13 +86,14 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
|
||||||
String identity = "adminstrator";
|
String identity = "adminstrator";
|
||||||
String credential = "12345";
|
String credential = "12345";
|
||||||
|
|
||||||
|
expect(client.seconds(3)).andReturn(client);
|
||||||
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(false);
|
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(false);
|
||||||
expect(
|
expect(
|
||||||
runScriptOnNodeFactory.create(host, Statements.exec("VBoxManage setproperty websrvauthlibrary null"),
|
runScriptOnNodeFactory.create(host, Statements.exec("VBoxManage setproperty websrvauthlibrary null"),
|
||||||
runAsRoot(false).wrapInInitScript(false))).andReturn(runScriptOnNode);
|
runAsRoot(false).wrapInInitScript(false))).andReturn(runScriptOnNode);
|
||||||
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
|
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
|
||||||
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
|
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
|
||||||
|
expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true);
|
||||||
expect(
|
expect(
|
||||||
runScriptOnNodeFactory.create(host, Statements.exec("vboxwebsrv -t 10000 -v -b"), runAsRoot(false)
|
runScriptOnNodeFactory.create(host, Statements.exec("vboxwebsrv -t 10000 -v -b"), runAsRoot(false)
|
||||||
.wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv"))).andReturn(
|
.wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv"))).andReturn(
|
||||||
|
@ -99,7 +101,7 @@ public class StartVBoxIfNotAlreadyRunningLiveTest {
|
||||||
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
|
expect(runScriptOnNode.init()).andReturn(runScriptOnNode);
|
||||||
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
|
expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0));
|
||||||
|
|
||||||
manager.connect(provider.toASCIIString(), identity, credential);
|
manager.connect(provider.toASCIIString(), "", "");
|
||||||
|
|
||||||
replay(manager, runScriptOnNodeFactory, runScriptOnNode, client);
|
replay(manager, runScriptOnNodeFactory, runScriptOnNode, client);
|
||||||
new StartVBoxIfNotAlreadyRunning((Function) Functions.constant(manager), runScriptOnNodeFactory, client,
|
new StartVBoxIfNotAlreadyRunning((Function) Functions.constant(manager), runScriptOnNodeFactory, client,
|
||||||
|
|
|
@ -27,13 +27,10 @@ import static org.testng.Assert.assertTrue;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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.config.ValueOfConfigurationKeyOrNull;
|
||||||
|
import org.jclouds.ssh.SshClient;
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.CloneSpec;
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
import org.jclouds.virtualbox.domain.ExecutionType;
|
|
||||||
import org.jclouds.virtualbox.domain.HardDisk;
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
import org.jclouds.virtualbox.domain.IsoSpec;
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
import org.jclouds.virtualbox.domain.MasterSpec;
|
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.domain.VmSpec;
|
||||||
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
||||||
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
|
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
|
||||||
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
|
import org.jclouds.virtualbox.functions.IMachineToSshClient;
|
||||||
import org.jclouds.virtualbox.statements.InstallGuestAdditions;
|
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
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.CaseFormat;
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.collect.ImmutableSet;
|
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;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrea Turli
|
* @author Andrea Turli
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "GuestAdditionsInstallerLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "GuestAdditionsInstallerLiveTest")
|
||||||
public class GuestAdditionsInstallerLiveTest extends
|
public class GuestAdditionsInstallerLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
BaseVirtualBoxClientLiveTest {
|
|
||||||
|
|
||||||
private MasterSpec machineSpec;
|
private Injector injector;
|
||||||
|
private Function<IMachine, SshClient> sshClientForIMachine;
|
||||||
|
private Predicate<SshClient> sshResponds;
|
||||||
|
|
||||||
@Override
|
private MasterSpec machineSpec;
|
||||||
@BeforeClass(groups = "live")
|
|
||||||
public void setupClient() {
|
|
||||||
super.setupClient();
|
|
||||||
String instanceName = VIRTUALBOX_IMAGE_PREFIX
|
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
|
|
||||||
.getSimpleName());
|
|
||||||
|
|
||||||
StorageController ideController = StorageController
|
@Override
|
||||||
.builder()
|
@BeforeClass(groups = "live")
|
||||||
.name("IDE Controller")
|
public void setupClient() {
|
||||||
.bus(StorageBus.IDE)
|
super.setupClient();
|
||||||
.attachISO(0, 0, operatingSystemIso)
|
injector = context.utils().injector();
|
||||||
.attachHardDisk(
|
|
||||||
HardDisk.builder().diskpath(adminDisk(instanceName))
|
|
||||||
.controllerPort(0).deviceSlot(1)
|
|
||||||
.autoDelete(true).build())
|
|
||||||
.attachISO(1, 1, guestAdditionsIso).build();
|
|
||||||
|
|
||||||
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName)
|
String instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
.name(instanceName).osTypeId("").memoryMB(512)
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
.cleanUpMode(CleanupMode.Full).controller(ideController)
|
|
||||||
.forceOverwrite(true).build();
|
|
||||||
|
|
||||||
Injector injector = context.utils().injector();
|
StorageController ideController = StorageController
|
||||||
Function<String, String> configProperties = injector
|
.builder()
|
||||||
.getInstance(ValueOfConfigurationKeyOrNull.class);
|
.name("IDE Controller")
|
||||||
IsoSpec isoSpec = IsoSpec
|
.bus(StorageBus.IDE)
|
||||||
.builder()
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
.sourcePath(operatingSystemIso)
|
.attachHardDisk(
|
||||||
.installationScript(
|
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
|
||||||
configProperties.apply(
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
|
|
||||||
"HOSTNAME", instanceVmSpec.getVmName()))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
NetworkAdapter networkAdapter = NetworkAdapter.builder()
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
.networkAttachmentType(NetworkAttachmentType.NAT)
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard
|
|
||||||
.builder().addNetworkAdapter(networkAdapter).build();
|
|
||||||
|
|
||||||
NetworkSpec networkSpec = NetworkSpec.builder()
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
.addNIC(networkInterfaceCard).build();
|
IsoSpec isoSpec = IsoSpec
|
||||||
machineSpec = MasterSpec.builder().iso(isoSpec)
|
.builder()
|
||||||
.vm(instanceVmSpec).network(networkSpec).build();
|
.sourcePath(operatingSystemIso)
|
||||||
}
|
.installationScript(
|
||||||
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
|
instanceVmSpec.getVmName())).build();
|
||||||
|
|
||||||
@Test
|
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
public void testGuestAdditionsAreInstalled() throws Exception {
|
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
||||||
IMachine machine = null;
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
try {
|
.build();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isIpv4(String s) {
|
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
||||||
Pattern pattern = Pattern
|
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
|
||||||
.compile(machineUtils.IP_V4_ADDRESS_PATTERN);
|
}
|
||||||
Matcher matcher = pattern.matcher(s);
|
|
||||||
return matcher.matches();
|
@Test
|
||||||
}
|
public void testGuestAdditionsAreInstalled() throws Exception {
|
||||||
}));
|
IMachine machine = null;
|
||||||
} finally {
|
try {
|
||||||
for (String vmNameOrId : ImmutableSet.of(machine.getName())) {
|
machine = cloneFromMaster();
|
||||||
machineController.ensureMachineHasPowerDown(vmNameOrId);
|
machineController.ensureMachineIsLaunched(machine.getName());
|
||||||
undoVm(vmNameOrId);
|
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() {
|
private IMachine cloneFromMaster() {
|
||||||
IMachine source = getVmWithGuestAdditionsInstalled();
|
IMachine source = getVmWithGuestAdditionsInstalled();
|
||||||
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec())
|
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
|
||||||
.network(machineSpec.getNetworkSpec()).master(source).linked(true).build();
|
.master(source).linked(true).build();
|
||||||
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
||||||
.apply(cloneSpec);
|
.apply(cloneSpec);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,11 @@
|
||||||
package org.jclouds.virtualbox.predicates;
|
package org.jclouds.virtualbox.predicates;
|
||||||
|
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
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.jclouds.virtualbox.predicates.IMachinePredicates.isLinkedClone;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.Set;
|
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||||
|
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.CloneSpec;
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
import org.jclouds.virtualbox.domain.HardDisk;
|
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.StorageController;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
||||||
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
|
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterClass;
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
import org.testng.annotations.BeforeMethod;
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
@ -47,6 +46,7 @@ import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
import org.virtualbox_4_1.StorageBus;
|
import org.virtualbox_4_1.StorageBus;
|
||||||
|
|
||||||
import com.google.common.base.CaseFormat;
|
import com.google.common.base.CaseFormat;
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
|
@ -57,67 +57,74 @@ import com.google.inject.Injector;
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "IMachinePredicatesLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "IMachinePredicatesLiveTest")
|
||||||
public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest {
|
public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
private String osTypeId = "";
|
private MasterSpec machineSpec;
|
||||||
private String ideControllerName = "IDE Controller";
|
private String instanceName;
|
||||||
private String cloneName;
|
|
||||||
private String vmName;
|
|
||||||
private StorageController masterStorageController;
|
|
||||||
private MasterSpec masterMachineSpec;
|
|
||||||
private NetworkSpec networkSpec;
|
|
||||||
private CloneSpec cloneSpec;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@BeforeClass(groups = "live")
|
@BeforeClass(groups = "live")
|
||||||
public void setupClient() {
|
public void setupClient() {
|
||||||
super.setupClient();
|
super.setupClient();
|
||||||
vmName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
|
StorageController ideController = StorageController
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
.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)
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
.deviceSlot(1).build();
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).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();
|
|
||||||
|
|
||||||
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
|
Injector injector = context.utils().injector();
|
||||||
.build();
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
IsoSpec isoSpec = IsoSpec
|
||||||
.build();
|
.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();
|
@Test
|
||||||
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
|
public void testCloneMachineFromAnotherMachine() {
|
||||||
masterMachineSpec);
|
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)
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
.forceOverwrite(true).build();
|
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();
|
@Override
|
||||||
|
@AfterClass(groups = "live")
|
||||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
protected void tearDown() throws Exception {
|
||||||
.apply(cloneSpec);
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
|
undoVm(vmName);
|
||||||
assertTrue(isLinkedClone().apply(clone));
|
}
|
||||||
}
|
super.tearDown();
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,13 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest
|
||||||
InstallGuestAdditions installer = new InstallGuestAdditions(vmSpecification, "4.1.8");
|
InstallGuestAdditions installer = new InstallGuestAdditions(vmSpecification, "4.1.8");
|
||||||
String scripts = installer.render(OsFamily.UNIX);
|
String scripts = installer.render(OsFamily.UNIX);
|
||||||
assertEquals("installModuleAssistantIfNeeded || return 1\n" + "mount -t iso9660 /dev/sr1 /mnt\n"
|
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() {
|
public void testIsoNotPresent() {
|
||||||
|
@ -61,7 +67,12 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest
|
||||||
+ "setupPublicCurl || return 1\n"
|
+ "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"
|
+ "(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"
|
+ "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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.util;
|
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_IMAGE_PREFIX;
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||||
import static org.testng.AssertJUnit.assertTrue;
|
import static org.testng.AssertJUnit.assertTrue;
|
||||||
|
@ -97,12 +96,10 @@ public class MachineControllerLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEnsureMachineisLaunchedAndSessionIsUnlocked() {
|
public void testEnsureMachineisLaunchedAndSessionIsUnlocked() {
|
||||||
IMachine vm = cloneFromMaster();
|
cloneFromMaster();
|
||||||
ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName);
|
ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName);
|
||||||
System.out.println("lock - cloneMachineSession " + cloneMachineSession.toString());
|
|
||||||
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
||||||
cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
|
cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
|
||||||
System.out.println("unlock - cloneMachineSession " + cloneMachineSession.toString());
|
|
||||||
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue