mirror of https://github.com/apache/jclouds.git
Merge pull request #519 from andreaturli/1.5.1
issue 384: preparing 1.5.0-beta - guestAdditions installations fixed
This commit is contained in:
commit
358737c70f
|
@ -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;
|
||||||
|
@ -137,8 +138,7 @@ public class VirtualBoxComputeServiceContextModule extends
|
||||||
bind(new TypeLiteral<Supplier<Map<Image, YamlImage>>>() {
|
bind(new TypeLiteral<Supplier<Map<Image, YamlImage>>>() {
|
||||||
}).to((Class) ImagesToYamlImagesFromYamlDescriptor.class);
|
}).to((Class) ImagesToYamlImagesFromYamlDescriptor.class);
|
||||||
// the yaml config provider
|
// the yaml config provider
|
||||||
bind(new TypeLiteral<Supplier<String>>() {
|
bind(YamlImagesFromFileConfig.class);
|
||||||
}).to((Class) YamlImagesFromFileConfig.class);
|
|
||||||
// the master machines cache
|
// the master machines cache
|
||||||
bind(new TypeLiteral<LoadingCache<Image, Master>>() {
|
bind(new TypeLiteral<LoadingCache<Image, Master>>() {
|
||||||
}).to((Class) MastersLoadingCache.class);
|
}).to((Class) MastersLoadingCache.class);
|
||||||
|
@ -213,7 +213,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,38 +33,34 @@ 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) {
|
|
||||||
return nic.getNetworkAdapter().getNetworkAttachmentType()
|
|
||||||
.equals(NetworkAttachmentType.NAT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
|
|
||||||
return nic.getNetworkAdapter().getNetworkAttachmentType()
|
|
||||||
.equals(NetworkAttachmentType.Bridged);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasNatAdapter(NetworkInterfaceCard nic) {
|
||||||
|
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.NAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasBridgedAdapter(NetworkInterfaceCard nic) {
|
||||||
|
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.Bridged);
|
||||||
|
}
|
||||||
|
|
||||||
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {
|
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {
|
||||||
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.HostOnly);
|
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.HostOnly);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -57,98 +58,94 @@ import com.google.inject.Inject;
|
||||||
@Singleton
|
@Singleton
|
||||||
public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
|
public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
|
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
|
||||||
private final Predicate<SshClient> sshResponds;
|
private final Predicate<SshClient> sshResponds;
|
||||||
private LoadingCache<IsoSpec, URI> preConfiguration;
|
private LoadingCache<IsoSpec, URI> preConfiguration;
|
||||||
private final Function<IMachine, SshClient> sshClientForIMachine;
|
private final Function<IMachine, SshClient> sshClientForIMachine;
|
||||||
private final MachineUtils machineUtils;
|
private final MachineUtils machineUtils;
|
||||||
private final IMachineToNodeMetadata imachineToNodeMetadata;
|
private final IMachineToNodeMetadata imachineToNodeMetadata;
|
||||||
private final MachineController machineController;
|
private final MachineController machineController;
|
||||||
private final String version;
|
private final String version;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CreateAndInstallVm(
|
public CreateAndInstallVm(
|
||||||
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
|
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
|
||||||
IMachineToNodeMetadata imachineToNodeMetadata,
|
IMachineToNodeMetadata imachineToNodeMetadata, Predicate<SshClient> sshResponds,
|
||||||
Predicate<SshClient> sshResponds,
|
Function<IMachine, SshClient> sshClientForIMachine, MachineUtils machineUtils,
|
||||||
Function<IMachine, SshClient> sshClientForIMachine,
|
@Preconfiguration LoadingCache<IsoSpec, URI> preConfiguration, MachineController machineController,
|
||||||
MachineUtils machineUtils,
|
@Named(Constants.PROPERTY_BUILD_VERSION) String version) {
|
||||||
@Preconfiguration LoadingCache<IsoSpec, URI> preConfiguration,
|
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
|
||||||
MachineController machineController, @Named(Constants.PROPERTY_BUILD_VERSION) String version) {
|
this.sshResponds = sshResponds;
|
||||||
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
|
this.sshClientForIMachine = sshClientForIMachine;
|
||||||
this.sshResponds = sshResponds;
|
this.machineUtils = machineUtils;
|
||||||
this.sshClientForIMachine = sshClientForIMachine;
|
this.preConfiguration = preConfiguration;
|
||||||
this.machineUtils = machineUtils;
|
this.imachineToNodeMetadata = imachineToNodeMetadata;
|
||||||
this.preConfiguration = preConfiguration;
|
this.machineController = machineController;
|
||||||
this.imachineToNodeMetadata = imachineToNodeMetadata;
|
this.version = Iterables.get(Splitter.on('r').split(version), 0);
|
||||||
this.machineController = machineController;
|
}
|
||||||
this.version = Iterables.get(Splitter.on('r').split(version), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IMachine apply(MasterSpec masterSpec) {
|
public IMachine apply(MasterSpec masterSpec) {
|
||||||
|
|
||||||
VmSpec vmSpec = masterSpec.getVmSpec();
|
VmSpec vmSpec = masterSpec.getVmSpec();
|
||||||
IsoSpec isoSpec = masterSpec.getIsoSpec();
|
IsoSpec isoSpec = masterSpec.getIsoSpec();
|
||||||
String vmName = vmSpec.getVmName();
|
String vmName = vmSpec.getVmName();
|
||||||
|
|
||||||
IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists
|
IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists.apply(masterSpec);
|
||||||
.apply(masterSpec);
|
|
||||||
|
|
||||||
// Launch machine and wait for it to come online
|
// Launch machine and wait for it to come online
|
||||||
machineController.ensureMachineIsLaunched(vmName);
|
machineController.ensureMachineIsLaunched(vmName);
|
||||||
|
|
||||||
URI uri = preConfiguration.getUnchecked(isoSpec);
|
URI uri = preConfiguration.getUnchecked(isoSpec);
|
||||||
String installationKeySequence = isoSpec.getInstallationKeySequence()
|
String installationKeySequence = isoSpec.getInstallationKeySequence().replace("PRECONFIGURATION_URL",
|
||||||
.replace("PRECONFIGURATION_URL", uri.toASCIIString());
|
uri.toASCIIString());
|
||||||
|
|
||||||
configureOsInstallationWithKeyboardSequence(vmName,
|
configureOsInstallationWithKeyboardSequence(vmName, installationKeySequence);
|
||||||
installationKeySequence);
|
|
||||||
|
|
||||||
SshClient client = sshClientForIMachine.apply(vm);
|
|
||||||
|
|
||||||
logger.debug(">> awaiting installation to finish node(%s)", vmName);
|
// 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);
|
||||||
|
|
||||||
checkState(sshResponds.apply(client),
|
logger.debug(">> awaiting installation to finish node(%s)", vmName);
|
||||||
"timed out waiting for guest %s to be accessible via ssh",
|
|
||||||
vmName);
|
|
||||||
|
|
||||||
logger.debug(">> awaiting installation of guest additions on vm: %s", vmName);
|
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName);
|
||||||
|
|
||||||
ListenableFuture<ExecResponse> execFuture = machineUtils.runScriptOnNode(imachineToNodeMetadata.apply(vm),
|
NodeMetadata nodeMetadata = imachineToNodeMetadata.apply(vm);
|
||||||
new InstallGuestAdditions(vmSpec, version), RunScriptOptions.NONE);
|
|
||||||
ExecResponse execResponse = Futures.getUnchecked(execFuture);
|
|
||||||
|
|
||||||
checkState(execResponse.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,
|
||||||
|
call("cleanupUdevIfNeeded"), RunScriptOptions.NONE);
|
||||||
|
ExecResponse cleanupResponse = Futures.getUnchecked(execCleanup);
|
||||||
|
checkState(cleanupResponse.getExitStatus() == 0);
|
||||||
|
|
||||||
NodeMetadata vmMetadata = imachineToNodeMetadata.apply(vm);
|
|
||||||
|
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);
|
||||||
|
|
||||||
execFuture = machineUtils.runScriptOnNode(vmMetadata, call("cleanupUdevIfNeeded"), RunScriptOptions.NONE);
|
machineController.ensureMachineIsShutdown(vmName);
|
||||||
|
|
||||||
execResponse = Futures.getUnchecked(execFuture);
|
|
||||||
checkState(execResponse.getExitStatus() == 0);
|
|
||||||
|
|
||||||
logger.debug("<< installation of image complete. Powering down node(%s)", vmName);
|
|
||||||
|
|
||||||
machineController.ensureMachineHasPowerDown(vmName);
|
|
||||||
return vm;
|
return vm;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureOsInstallationWithKeyboardSequence(String vmName,
|
private void configureOsInstallationWithKeyboardSequence(String vmName, String installationKeySequence) {
|
||||||
String installationKeySequence) {
|
Iterable<List<Integer>> scancodelist = transform(Splitter.on(" ").split(installationKeySequence),
|
||||||
Iterable<List<Integer>> scancodelist = transform(Splitter.on(" ")
|
new StringToKeyCode());
|
||||||
.split(installationKeySequence), new StringToKeyCode());
|
|
||||||
|
|
||||||
for (List<Integer> scancodes : scancodelist) {
|
for (List<Integer> scancodes : scancodelist) {
|
||||||
machineUtils.sharedLockMachineAndApplyToSession(vmName,new SendScancodes(scancodes));
|
machineUtils.sharedLockMachineAndApplyToSession(vmName, new SendScancodes(scancodes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(publicIpAddresses);
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
|
@ -79,13 +79,15 @@ public class IMachineToVmSpec implements Function<IMachine, VmSpec> {
|
||||||
for (IMediumAttachment iMediumAttachment : machine.getMediumAttachmentsOfController(iStorageController
|
for (IMediumAttachment iMediumAttachment : machine.getMediumAttachmentsOfController(iStorageController
|
||||||
.getName())) {
|
.getName())) {
|
||||||
IMedium iMedium = iMediumAttachment.getMedium();
|
IMedium iMedium = iMediumAttachment.getMedium();
|
||||||
if (iMedium.getDeviceType().equals(DeviceType.HardDisk)) {
|
if(iMedium != null) {
|
||||||
storageControlleBuiler.attachHardDisk(HardDisk.builder().diskpath(iMedium.getLocation())
|
if (iMedium.getDeviceType().equals(DeviceType.HardDisk)) {
|
||||||
.autoDelete(true).controllerPort(iMediumAttachment.getPort())
|
storageControlleBuiler.attachHardDisk(HardDisk.builder().diskpath(iMedium.getLocation())
|
||||||
.deviceSlot(iMediumAttachment.getDevice().intValue()).build());
|
.autoDelete(true).controllerPort(iMediumAttachment.getPort())
|
||||||
} else if (iMedium.getDeviceType().equals(DeviceType.DVD)) {
|
.deviceSlot(iMediumAttachment.getDevice().intValue()).build());
|
||||||
storageControlleBuiler.attachISO(iMediumAttachment.getPort(), iMediumAttachment.getDevice().intValue(),
|
} else if (iMedium.getDeviceType().equals(DeviceType.DVD)) {
|
||||||
iMedium.getLocation());
|
storageControlleBuiler.attachISO(iMediumAttachment.getPort(), iMediumAttachment.getDevice().intValue(),
|
||||||
|
iMedium.getLocation());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
controllers.add(storageControlleBuiler.name(iStorageController.getName()).bus(iStorageController.getBus())
|
controllers.add(storageControlleBuiler.name(iStorageController.getName()).bus(iStorageController.getBus())
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.jclouds.virtualbox.domain.ErrorCode;
|
||||||
import org.jclouds.virtualbox.domain.ExecutionType;
|
import org.jclouds.virtualbox.domain.ExecutionType;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.IProgress;
|
import org.virtualbox_4_1.IProgress;
|
||||||
|
import org.virtualbox_4_1.ISession;
|
||||||
import org.virtualbox_4_1.SessionState;
|
import org.virtualbox_4_1.SessionState;
|
||||||
import org.virtualbox_4_1.VBoxException;
|
import org.virtualbox_4_1.VBoxException;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
@ -52,7 +53,7 @@ import com.google.common.base.Function;
|
||||||
* @author Mattias Holmqvist
|
* @author Mattias Holmqvist
|
||||||
* @see ErrorCode
|
* @see ErrorCode
|
||||||
*/
|
*/
|
||||||
public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, Void> {
|
public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, ISession> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
|
@ -69,14 +70,17 @@ public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, Void
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(@Nullable IMachine machine) {
|
public ISession apply(@Nullable IMachine machine) {
|
||||||
|
ISession session = manager.getSessionObject();
|
||||||
try {
|
try {
|
||||||
final IProgress progress = machine
|
final IProgress progress = machine
|
||||||
.launchVMProcess(manager.getSessionObject(), type.stringValue(), environment);
|
.launchVMProcess(session, type.stringValue(), environment);
|
||||||
progress.waitForCompletion(-1);
|
progress.waitForCompletion(-1);
|
||||||
Thread.sleep(5000);
|
try {
|
||||||
} catch (InterruptedException e) {
|
Thread.sleep(3000l);
|
||||||
propagate(e);
|
} 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) {
|
||||||
|
@ -88,11 +92,12 @@ public class LaunchMachineIfNotAlreadyRunning implements Function<IMachine, Void
|
||||||
propagate(e);
|
propagate(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (manager.getSessionObject().getState() == SessionState.Locked) {
|
if (session.getState() == SessionState.Locked) {
|
||||||
// Remove session lock taken by launchVmProcess()
|
// Remove session lock taken by launchVmProcess()
|
||||||
manager.getSessionObject().unlockMachine();
|
session.unlockMachine();
|
||||||
|
// TODO this unlock is not IMMEDIATELY visible outside (vbox doc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return session;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -170,7 +171,7 @@ public class MastersLoadingCache extends AbstractLoadingCache<Image, Master> {
|
||||||
|
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
.slot(0L).build();
|
.slot(0L).build();
|
||||||
|
|
||||||
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
||||||
|
|
||||||
MasterSpec masterSpec = MasterSpec
|
MasterSpec masterSpec = MasterSpec
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/**
|
/**
|
||||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
* contributor license agreements. See the NOTICE file
|
* contributor license agreements. See the NOTICE file
|
||||||
* distributed with this work for additional information
|
* distributed with this work for additional information
|
||||||
|
@ -20,21 +20,26 @@
|
||||||
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.checkNotNull;
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||||
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 java.util.List;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
|
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
|
||||||
import org.jclouds.compute.callables.RunScriptOnNode;
|
import org.jclouds.compute.callables.RunScriptOnNode;
|
||||||
|
import org.jclouds.compute.callables.RunScriptOnNode.Factory;
|
||||||
|
import org.jclouds.compute.domain.ExecResponse;
|
||||||
import org.jclouds.compute.domain.NodeMetadata;
|
import org.jclouds.compute.domain.NodeMetadata;
|
||||||
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
||||||
import org.jclouds.compute.options.RunScriptOptions;
|
import org.jclouds.compute.options.RunScriptOptions;
|
||||||
import org.jclouds.domain.LoginCredentials;
|
import org.jclouds.domain.LoginCredentials;
|
||||||
|
import org.jclouds.scriptbuilder.domain.Statements;
|
||||||
import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule;
|
import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule;
|
||||||
import org.jclouds.virtualbox.domain.CloneSpec;
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
import org.jclouds.virtualbox.domain.Master;
|
import org.jclouds.virtualbox.domain.Master;
|
||||||
|
@ -44,19 +49,21 @@ 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.HostNetworkInterfaceType;
|
||||||
|
import org.virtualbox_4_1.IDHCPServer;
|
||||||
|
import org.virtualbox_4_1.IHostNetworkInterface;
|
||||||
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.Predicate;
|
||||||
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;
|
import com.google.common.collect.Iterables;
|
||||||
|
@ -65,38 +72,29 @@ 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
|
|
||||||
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;
|
||||||
|
private final Factory runScriptOnNodeFactory;
|
||||||
|
private final Supplier<NodeMetadata> host;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public NodeCreator(Supplier<VirtualBoxManager> manager, Function<CloneSpec, IMachine> cloner,
|
public NodeCreator(Supplier<VirtualBoxManager> manager, Function<CloneSpec, IMachine> cloner, Factory runScriptOnNodeFactory,
|
||||||
|
Supplier<NodeMetadata> host,
|
||||||
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.runScriptOnNodeFactory = checkNotNull(runScriptOnNodeFactory, "runScriptOnNodeFactory");
|
||||||
this.nodeIps = new AtomicInteger(2);
|
this.host = checkNotNull(host, "host");
|
||||||
|
|
||||||
this.machineUtils = machineUtils;
|
this.machineUtils = machineUtils;
|
||||||
this.machineController = machineController;
|
this.machineController = machineController;
|
||||||
}
|
}
|
||||||
|
@ -130,18 +128,21 @@ 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();
|
||||||
|
|
||||||
|
// create new hostOnly interface if needed, otherwise use the one already there with dhcp enabled ...
|
||||||
|
String hostOnlyIfName = getHostOnlyIfOrCreate();
|
||||||
|
|
||||||
NetworkInterfaceCard hostOnlyIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(hostOnlyAdapter)
|
NetworkInterfaceCard hostOnlyIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(hostOnlyAdapter)
|
||||||
.addHostInterfaceName(HOST_ONLY_IFACE_NAME).slot(1L).build();
|
.addHostInterfaceName(hostOnlyIfName).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 +157,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 +169,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);
|
||||||
|
@ -194,5 +184,94 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
||||||
NetworkInterfaceCard hostOnlyIfaceCard) {
|
NetworkInterfaceCard hostOnlyIfaceCard) {
|
||||||
return NetworkSpec.builder().addNIC(natIfaceCard).addNIC(hostOnlyIfaceCard).build();
|
return NetworkSpec.builder().addNIC(natIfaceCard).addNIC(hostOnlyIfaceCard).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getHostOnlyIfOrCreate() {
|
||||||
|
|
||||||
|
IHostNetworkInterface availableHostInterfaceIf = returnExistingHostNetworkInterfaceWithDHCPenabledOrNull(manager
|
||||||
|
.get().getVBox().getHost().getNetworkInterfaces());
|
||||||
|
if (availableHostInterfaceIf==null) {
|
||||||
|
final String hostOnlyIfName = createHostOnlyIf();
|
||||||
|
assignDHCPtoHostOnlyInterface(hostOnlyIfName);
|
||||||
|
return hostOnlyIfName;
|
||||||
|
} else {
|
||||||
|
return availableHostInterfaceIf.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assignDHCPtoHostOnlyInterface(final String hostOnlyIfName) {
|
||||||
|
List<IHostNetworkInterface> availableNetworkInterfaces = manager.get().getVBox().getHost()
|
||||||
|
.getNetworkInterfaces();
|
||||||
|
|
||||||
|
IHostNetworkInterface iHostNetworkInterfaceWithHostOnlyIfName = Iterables.getOnlyElement(Iterables.filter(availableNetworkInterfaces, new Predicate<IHostNetworkInterface>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(IHostNetworkInterface iHostNetworkInterface) {
|
||||||
|
return iHostNetworkInterface.getName().equals(hostOnlyIfName);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
String hostOnlyIfIpAddress = iHostNetworkInterfaceWithHostOnlyIfName.getIPAddress();
|
||||||
|
String dhcpIpAddress = hostOnlyIfIpAddress.substring(0, hostOnlyIfIpAddress.lastIndexOf(".")) + ".254";
|
||||||
|
String dhcpNetmask = "255.255.255.0";
|
||||||
|
String dhcpLowerIp = hostOnlyIfIpAddress.substring(0, hostOnlyIfIpAddress.lastIndexOf(".")) + ".2";
|
||||||
|
String dhcpUpperIp = hostOnlyIfIpAddress.substring(0, hostOnlyIfIpAddress.lastIndexOf(".")) + ".253";
|
||||||
|
|
||||||
|
ExecResponse response = runScriptOnNodeFactory
|
||||||
|
.create(host.get(),
|
||||||
|
Statements.exec(String
|
||||||
|
.format("VBoxManage dhcpserver add --ifname %s --ip %s --netmask %s --lowerip %s --upperip %s --enable",
|
||||||
|
hostOnlyIfName, dhcpIpAddress, dhcpNetmask, dhcpLowerIp, dhcpUpperIp)), runAsRoot(false).wrapInInitScript(false)).init().call();
|
||||||
|
checkState(response.getExitStatus()==0);
|
||||||
|
/*
|
||||||
|
runScriptOnNodeFactory
|
||||||
|
.create(host.get(),
|
||||||
|
Statements.exec(String.format("VBoxManage hostonlyif ipconfig %s --ip %s",
|
||||||
|
hostOnlyIfName, hostOnlyIfIpAddress)), runAsRoot(false).wrapInInitScript(false)).init().call();
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createHostOnlyIf() {
|
||||||
|
final String hostOnlyIfName;
|
||||||
|
ExecResponse createHostOnyResponse = runScriptOnNodeFactory
|
||||||
|
.create(host.get(), Statements.exec("VBoxManage hostonlyif create"),
|
||||||
|
runAsRoot(false).wrapInInitScript(false)).init().call();
|
||||||
|
String output = createHostOnyResponse.getOutput();
|
||||||
|
checkState(createHostOnyResponse.getExitStatus()==0);
|
||||||
|
checkState(output.contains("'"), "cannot create hostonlyif");
|
||||||
|
hostOnlyIfName = output.substring(output.indexOf("'") + 1, output.lastIndexOf("'"));
|
||||||
|
return hostOnlyIfName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IHostNetworkInterface returnExistingHostNetworkInterfaceWithDHCPenabledOrNull(Iterable<IHostNetworkInterface> availableNetworkInterfaces) {
|
||||||
|
checkNotNull(availableNetworkInterfaces);
|
||||||
|
return Iterables.getFirst(filterAvailableNetworkInterfaceByHostOnlyAndDHCPenabled(availableNetworkInterfaces), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param availableNetworkInterfaces
|
||||||
|
* @param hostOnlyIfIpAddress
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Iterable<IHostNetworkInterface> filterAvailableNetworkInterfaceByHostOnlyAndDHCPenabled(Iterable<IHostNetworkInterface> availableNetworkInterfaces) {
|
||||||
|
Iterable<IHostNetworkInterface> filteredNetworkInterfaces = Iterables.filter(availableNetworkInterfaces, new Predicate<IHostNetworkInterface>() {
|
||||||
|
@Override
|
||||||
|
public boolean apply(IHostNetworkInterface iHostNetworkInterface) {
|
||||||
|
// this is an horrible workaround cause iHostNetworkInterface.getDhcpEnabled is working only for windows host
|
||||||
|
boolean match = false;
|
||||||
|
List<IDHCPServer> availableDHCPservers = manager.get().getVBox().getDHCPServers();
|
||||||
|
for (IDHCPServer idhcpServer : availableDHCPservers) {
|
||||||
|
if(idhcpServer.getEnabled() && idhcpServer.getNetworkName().equals(iHostNetworkInterface.getNetworkName()))
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
return iHostNetworkInterface.getInterfaceType().equals(HostNetworkInterfaceType.HostOnly) &&
|
||||||
|
match;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return filteredNetworkInterfaces;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.compute.domain.Image;
|
import org.jclouds.compute.domain.Image;
|
||||||
import org.jclouds.virtualbox.domain.YamlImage;
|
import org.jclouds.virtualbox.domain.YamlImage;
|
||||||
|
import org.jclouds.virtualbox.functions.YamlImagesFromFileConfig;
|
||||||
import org.yaml.snakeyaml.Loader;
|
import org.yaml.snakeyaml.Loader;
|
||||||
import org.yaml.snakeyaml.TypeDescription;
|
import org.yaml.snakeyaml.TypeDescription;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
@ -47,7 +48,7 @@ public class ImagesToYamlImagesFromYamlDescriptor implements Supplier<Map<Image,
|
||||||
private String yamlDescriptor;
|
private String yamlDescriptor;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ImagesToYamlImagesFromYamlDescriptor(Supplier<String> yamlDescriptorSupplier) {
|
public ImagesToYamlImagesFromYamlDescriptor(/*Supplier<String> yamlDescriptorSupplier*/YamlImagesFromFileConfig yamlDescriptorSupplier) {
|
||||||
this.yamlDescriptor = yamlDescriptorSupplier.get();
|
this.yamlDescriptor = yamlDescriptorSupplier.get();
|
||||||
checkNotNull(yamlDescriptor, "yaml descriptor");
|
checkNotNull(yamlDescriptor, "yaml descriptor");
|
||||||
checkState(!yamlDescriptor.equals(""), "yaml descriptor is empty");
|
checkState(!yamlDescriptor.equals(""), "yaml descriptor is empty");
|
||||||
|
|
|
@ -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,10 @@ 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 exit 0 >> /etc/rc.local"))); //
|
||||||
statements.add(exec(String.format("umount %s", mountPoint)));
|
statements.add(exec(String.format("umount %s", mountPoint)));
|
||||||
return statements;
|
return statements;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.virtualbox.util;
|
package org.jclouds.virtualbox.util;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
@ -28,11 +30,13 @@ import org.jclouds.virtualbox.domain.ExecutionType;
|
||||||
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
|
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
|
||||||
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.LockType;
|
||||||
|
import org.virtualbox_4_1.MachineState;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
import org.virtualbox_4_1.jaxws.MachineState;
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,7 +56,6 @@ public class MachineController {
|
||||||
private final MachineUtils machineUtils;
|
private final MachineUtils machineUtils;
|
||||||
private final ExecutionType executionType;
|
private final ExecutionType executionType;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public MachineController(Supplier<VirtualBoxManager> manager, MachineUtils machineUtils, ExecutionType executionType) {
|
public MachineController(Supplier<VirtualBoxManager> manager, MachineUtils machineUtils, ExecutionType executionType) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
@ -60,32 +63,128 @@ public class MachineController {
|
||||||
this.executionType = executionType;
|
this.executionType = executionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureMachineIsLaunched(String vmName) {
|
public ISession ensureMachineIsLaunched(String vmName) {
|
||||||
machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
|
ISession session = null;
|
||||||
}
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
|
||||||
|
try {
|
||||||
|
session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(),
|
||||||
|
executionType, ""));
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getMessage().contains(
|
||||||
|
"org.virtualbox_4_1.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) {
|
||||||
|
throw e;
|
||||||
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return checkNotNull(session, "session");
|
||||||
|
}
|
||||||
|
|
||||||
|
public ISession ensureMachineHasPowerDown(String vmName) {
|
||||||
|
ISession session = manager.get().getSessionObject();
|
||||||
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) {
|
||||||
|
try {
|
||||||
|
session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
|
||||||
|
new Function<ISession, ISession>() {
|
||||||
|
@Override
|
||||||
|
public ISession apply(ISession session) {
|
||||||
|
IProgress powerDownProgress = session.getConsole().powerDown();
|
||||||
|
powerDownProgress.waitForCompletion(-1);
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
// sometimes the machine might be powered of between the while
|
||||||
|
// test and the call to
|
||||||
|
// lockSessionOnMachineAndApply
|
||||||
|
if (e.getMessage().contains("Invalid machine state: PoweredOff")) {
|
||||||
|
throw e;
|
||||||
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return checkNotNull(session, "session");
|
||||||
|
}
|
||||||
|
|
||||||
public void ensureMachineHasPowerDown(String vmName) {
|
/**
|
||||||
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.POWERED_OFF)) {
|
* 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.sharedLockMachineAndApplyToSession(vmName, new Function<ISession, Void>() {
|
*/
|
||||||
@Override
|
public ISession ensureMachineIsShutdown(String vmName) {
|
||||||
public Void apply(ISession session) {
|
ISession session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared,
|
||||||
IProgress powerDownProgress = session.getConsole().powerDown();
|
new Function<ISession, ISession>() {
|
||||||
powerDownProgress.waitForCompletion(-1);
|
@Override
|
||||||
return null;
|
public ISession apply(ISession session) {
|
||||||
}
|
session.getConsole().powerButton();
|
||||||
});
|
return session;
|
||||||
} catch (RuntimeException e) {
|
}
|
||||||
// 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: PoweredOff")) {
|
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 ensureMachineIsPaused(String vmName) {
|
||||||
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) {
|
||||||
|
try {
|
||||||
|
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
|
||||||
|
@Override
|
||||||
|
public Void apply(ISession session) {
|
||||||
|
session.getConsole().pause();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
// sometimes the machine might be powered of between the while
|
||||||
|
// test and the call to
|
||||||
|
// lockSessionOnMachineAndApply
|
||||||
|
if (e.getMessage().contains("Invalid machine state: Paused")) {
|
||||||
|
return;
|
||||||
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ensureMachineIsResumed(String vmName) {
|
||||||
|
while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) {
|
||||||
|
try {
|
||||||
|
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
|
||||||
|
@Override
|
||||||
|
public Void apply(ISession session) {
|
||||||
|
session.getConsole().resume();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
// sometimes the machine might be powered of between the while
|
||||||
|
// test and the call to
|
||||||
|
// lockSessionOnMachineAndApply
|
||||||
|
if (e.getMessage().contains("Invalid machine state: Resumed")) {
|
||||||
|
return;
|
||||||
|
} else if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,12 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.virtualbox.util;
|
package org.jclouds.virtualbox.util;
|
||||||
|
|
||||||
|
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;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
@ -34,11 +40,13 @@ import org.jclouds.util.Throwables2;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.ISession;
|
import org.virtualbox_4_1.ISession;
|
||||||
import org.virtualbox_4_1.LockType;
|
import org.virtualbox_4_1.LockType;
|
||||||
|
import org.virtualbox_4_1.SessionState;
|
||||||
import org.virtualbox_4_1.VBoxException;
|
import org.virtualbox_4_1.VBoxException;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.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;
|
||||||
|
|
||||||
|
@ -50,7 +58,11 @@ import com.google.inject.Inject;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class MachineUtils {
|
public class MachineUtils {
|
||||||
|
|
||||||
|
public final String IP_V4_ADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
|
||||||
|
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
|
||||||
|
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
@ -178,11 +190,23 @@ public class MachineUtils {
|
||||||
* the function to execute
|
* the function to execute
|
||||||
* @return the result from applying the function to the session.
|
* @return the result from applying the function to the session.
|
||||||
*/
|
*/
|
||||||
private <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);
|
||||||
|
try {
|
||||||
|
return function.apply(session);
|
||||||
|
} catch (VBoxException e) {
|
||||||
|
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId,
|
||||||
|
type, e.getMessage()), e);
|
||||||
|
} finally {
|
||||||
|
if (session != null && session.getState().equals(SessionState.Locked))
|
||||||
|
session.unlockMachine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ISession lockSession(String machineId, LockType type, int retries) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
ISession session;
|
ISession session;
|
||||||
long time = System.currentTimeMillis();
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
|
IMachine immutableMachine = manager.get().getVBox().findMachine(machineId);
|
||||||
|
@ -206,14 +230,8 @@ public class MachineUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
checkState(session.getState().equals(SessionState.Locked));
|
||||||
return function.apply(session);
|
return checkNotNull(session, "session");
|
||||||
} catch (VBoxException e) {
|
|
||||||
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId,
|
|
||||||
type, e.getMessage()), e);
|
|
||||||
} finally {
|
|
||||||
session.unlockMachine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print() {
|
void print() {
|
||||||
|
@ -246,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,12 +1,9 @@
|
||||||
function cleanupUdevIfNeeded {
|
function cleanupUdevIfNeeded {
|
||||||
# unset OSNAME;
|
if [ -f '/etc/udev/rules.d/70-persistent-net.rules' ]
|
||||||
# local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift
|
|
||||||
# if [ $OSNAME = 'Ubuntu' ]
|
|
||||||
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,11 +4,8 @@ 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 build-essential 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
|
||||||
rm /etc/udev/rules.d/70-persistent-net.rules;
|
return 0
|
||||||
mkdir /etc/udev/rules.d/70-persistent-net.rules;
|
|
||||||
rm -rf /dev/.udev/;
|
|
||||||
rm /lib/udev/rules.d/75-persistent-net-generator.rules
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
package org.jclouds.virtualbox;
|
package org.jclouds.virtualbox;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
@ -38,25 +39,41 @@ import org.jclouds.compute.domain.Template;
|
||||||
import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate;
|
import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate;
|
||||||
import org.jclouds.concurrent.MoreExecutors;
|
import org.jclouds.concurrent.MoreExecutors;
|
||||||
import org.jclouds.concurrent.config.ExecutorServiceModule;
|
import org.jclouds.concurrent.config.ExecutorServiceModule;
|
||||||
|
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||||
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
|
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
|
||||||
import org.jclouds.sshj.config.SshjSshClientModule;
|
import org.jclouds.sshj.config.SshjSshClientModule;
|
||||||
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
import org.jclouds.virtualbox.config.VirtualBoxConstants;
|
||||||
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
import org.jclouds.virtualbox.domain.IsoSpec;
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
import org.jclouds.virtualbox.domain.Master;
|
import org.jclouds.virtualbox.domain.Master;
|
||||||
|
import org.jclouds.virtualbox.domain.MasterSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkAdapter;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.StorageController;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
|
import org.jclouds.virtualbox.functions.IMachineToVmSpec;
|
||||||
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia;
|
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia;
|
||||||
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.testng.annotations.AfterClass;
|
import org.testng.annotations.AfterClass;
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
|
import org.virtualbox_4_1.SessionState;
|
||||||
|
import org.virtualbox_4_1.StorageBus;
|
||||||
|
import org.virtualbox_4_1.VBoxException;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
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.Injector;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,18 +163,69 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
checkNotNull(mastersCache.apply(template.getImage()));
|
checkNotNull(mastersCache.apply(template.getImage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void undoVm(String vmNameOrId) {
|
||||||
|
IMachine vm = null;
|
||||||
|
try {
|
||||||
|
vm = manager.get().getVBox().findMachine(vmNameOrId);
|
||||||
|
VmSpec vmSpec = new IMachineToVmSpec().apply(vm);
|
||||||
|
int attempts = 0;
|
||||||
|
while (attempts < 10 && !vm.getSessionState().equals(SessionState.Unlocked)) {
|
||||||
|
attempts++;
|
||||||
|
try {
|
||||||
|
Thread.sleep(200l);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
machineUtils.applyForMachine(vmNameOrId, new UnregisterMachineIfExistsAndDeleteItsMedia(vmSpec));
|
||||||
|
|
||||||
protected void undoVm(VmSpec vmSpecification) {
|
} catch (VBoxException e) {
|
||||||
machineUtils.writeLockMachineAndApply(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia(
|
if (e.getMessage().contains("Could not find a registered machine named"))
|
||||||
vmSpecification));
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String adminDisk(String vmName) {
|
public String adminDisk(String vmName) {
|
||||||
return workingDir + File.separator + vmName + ".vdi";
|
return workingDir + File.separator + vmName + ".vdi";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MasterSpec getMasterSpecForTest() {
|
||||||
|
String masterName = "jclouds-image-0x0-default-ubuntu-11.04-i386";
|
||||||
|
StorageController ideController = StorageController
|
||||||
|
.builder()
|
||||||
|
.name("IDE Controller")
|
||||||
|
.bus(StorageBus.IDE)
|
||||||
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(
|
||||||
|
HardDisk.builder().diskpath(adminDisk(masterName)).controllerPort(0).deviceSlot(1)
|
||||||
|
.autoDelete(true).build()).attachISO(1, 0, guestAdditionsIso).build();
|
||||||
|
|
||||||
|
VmSpec sourceVmSpec = VmSpec.builder().id(masterName).name(masterName)
|
||||||
|
.osTypeId("").memoryMB(512).cleanUpMode(CleanupMode.Full)
|
||||||
|
.controller(ideController).forceOverwrite(true).build();
|
||||||
|
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
Function<String, String> configProperties = injector
|
||||||
|
.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
|
IsoSpec isoSpec = IsoSpec
|
||||||
|
.builder()
|
||||||
|
.sourcePath(operatingSystemIso)
|
||||||
|
.installationScript(
|
||||||
|
configProperties.apply(
|
||||||
|
VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
|
||||||
|
"HOSTNAME", sourceVmSpec.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();
|
||||||
|
return MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec)
|
||||||
|
.network(networkSpec).build();
|
||||||
|
}
|
||||||
|
|
||||||
@AfterClass(groups = "live")
|
@AfterClass(groups = "live")
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
if (context != null)
|
if (context != null)
|
||||||
|
|
|
@ -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();
|
||||||
try {
|
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());
|
||||||
|
|
||||||
IMachine source = getSourceNode();
|
|
||||||
|
|
||||||
cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(cloneNetworkSpec).master(source)
|
|
||||||
.linked(IS_LINKED_CLONE).build();
|
|
||||||
|
|
||||||
if (source.getCurrentSnapshot() != null) {
|
|
||||||
ISession session = manager.get().openMachineSession(source);
|
|
||||||
session.getConsole().deleteSnapshot(source.getCurrentSnapshot().getId());
|
|
||||||
session.unlockMachine();
|
|
||||||
}
|
|
||||||
|
|
||||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
|
||||||
.apply(cloneSpec);
|
|
||||||
assertEquals(clone.getName(), cloneSpec.getVmSpec().getVmName());
|
|
||||||
|
|
||||||
new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "").apply(clone);
|
|
||||||
|
|
||||||
// TODO ssh into the node
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
Set<VmSpec> specs = cloneSpec == null ? ImmutableSet.of(sourceMachineSpec.getVmSpec()) : ImmutableSet.of(
|
|
||||||
cloneSpec.getVmSpec(), sourceMachineSpec.getVmSpec());
|
|
||||||
for (VmSpec spec : specs) {
|
|
||||||
undoVm(spec);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IMachine getSourceNode() {
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
try {
|
try {
|
||||||
Injector injector = context.utils().injector();
|
Injector injector = context.utils().injector();
|
||||||
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(sourceMachineSpec);
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
// already created
|
// already created
|
||||||
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId());
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@AfterClass(groups = "live")
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
|
undoVm(vmName);
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@BeforeClass(groups = "live")
|
||||||
|
public void setupClient() {
|
||||||
|
super.setupClient();
|
||||||
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
undoVm(vmSpecification);
|
StorageController ideController = StorageController
|
||||||
}
|
.builder()
|
||||||
|
.name("IDE Controller")
|
||||||
|
.bus(StorageBus.IDE)
|
||||||
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(
|
||||||
|
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
|
||||||
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
@Test
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
public void testCreateImageMachineFromIso() throws Exception {
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
IMachine imageMachine = getVmWithGuestAdditionsInstalled();
|
|
||||||
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
|
injector = context.utils().injector();
|
||||||
Image newImage = iMachineToImage.apply(imageMachine);
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
assertEquals(vmName, newImage.getName());
|
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
|
||||||
|
public void testCreateNewMachine() throws Exception {
|
||||||
|
vmName = "jclouds-test-create-1-node";
|
||||||
|
String vmId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateNewMachine() throws Exception {
|
|
||||||
String vmName = "jclouds-test-create-1-node";
|
|
||||||
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)
|
|
||||||
.attachISO(0, 0, operatingSystemIso)
|
|
||||||
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
|
||||||
.attachISO(1, 1, guestAdditionsIso).build();
|
|
||||||
|
|
||||||
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512)
|
@Test
|
||||||
.controller(ideController).cleanUpMode(mode)
|
public void testCreateNewMachineWithBadOsType() throws Exception {
|
||||||
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
vmName = "jclouds-test-create-2-node";
|
||||||
IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso)
|
String vmId = UUID.randomUUID().toString();
|
||||||
.installationScript("").build();
|
|
||||||
NetworkSpec networkSpec = NetworkSpec.builder().build();
|
|
||||||
MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec)
|
|
||||||
.network(networkSpec).build();
|
|
||||||
undoVm(vmSpec);
|
|
||||||
try {
|
|
||||||
Injector injector = context.utils().injector();
|
|
||||||
injector.getInstance(
|
|
||||||
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
|
|
||||||
.apply(machineSpec);
|
|
||||||
fail();
|
|
||||||
} catch (VBoxException e) {
|
|
||||||
ErrorCode errorCode = ErrorCode.valueOf(e);
|
|
||||||
// According to the documentation VBOX_E_OBJECT_NOT_FOUND
|
|
||||||
// if osTypeId is not found.
|
|
||||||
assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND);
|
|
||||||
} finally {
|
|
||||||
undoVm(vmSpec);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
|
||||||
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build())
|
||||||
|
.attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
|
VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
|
||||||
|
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
||||||
|
IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build();
|
||||||
|
NetworkSpec networkSpec = NetworkSpec.builder().build();
|
||||||
|
MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec).network(networkSpec).build();
|
||||||
|
try {
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec);
|
||||||
|
fail();
|
||||||
|
} catch (VBoxException e) {
|
||||||
|
ErrorCode errorCode = ErrorCode.valueOf(e);
|
||||||
|
// According to the documentation VBOX_E_OBJECT_NOT_FOUND
|
||||||
|
// if osTypeId is not found.
|
||||||
|
assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@AfterMethod(groups = "live")
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
undoVm(vmName);
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,13 +63,14 @@ 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(1, node.getPrivateAddresses().size());
|
||||||
assertEquals(1, node.getPublicAddresses().size());
|
assertEquals(1, node.getPublicAddresses().size());
|
||||||
assertEquals("127.0.0.1", Iterables.get(node.getPublicAddresses(), 0));
|
assertEquals("127.0.0.1", Iterables.get(node.getPublicAddresses(), 0));
|
||||||
assertEquals(MastersLoadingCache.MASTER_PORT, node.getLoginPort());
|
assertEquals(MastersLoadingCache.MASTER_PORT, node.getLoginPort());
|
||||||
|
@ -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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -28,9 +28,9 @@ import org.jclouds.compute.domain.Image;
|
||||||
import org.jclouds.compute.domain.ImageBuilder;
|
import org.jclouds.compute.domain.ImageBuilder;
|
||||||
import org.jclouds.compute.domain.OperatingSystem;
|
import org.jclouds.compute.domain.OperatingSystem;
|
||||||
import org.jclouds.compute.domain.OsFamily;
|
import org.jclouds.compute.domain.OsFamily;
|
||||||
|
import org.jclouds.virtualbox.functions.YamlImagesFromFileConfig;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,29 +39,23 @@ import com.google.common.collect.Iterables;
|
||||||
@Test(groups = "unit")
|
@Test(groups = "unit")
|
||||||
public class ImageFromYamlStringTest {
|
public class ImageFromYamlStringTest {
|
||||||
|
|
||||||
public static final Image TEST1 = new ImageBuilder()
|
public static final Image TEST1 = new ImageBuilder()
|
||||||
.id("default-ubuntu-11.04-i386")
|
.id("default-ubuntu-11.04-i386")
|
||||||
.name("ubuntu-11.04-server-i386")
|
.name("ubuntu-11.04-server-i386")
|
||||||
.description("ubuntu 11.04 server (i386)")
|
.description("ubuntu 11.04 server (i386)")
|
||||||
.operatingSystem(
|
.operatingSystem(
|
||||||
OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU)
|
OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU).version("11.04").build())
|
||||||
.version("11.04").build()).build();
|
.build();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNodesParse() throws Exception {
|
public void testNodesParse() throws Exception {
|
||||||
|
|
||||||
final StringBuilder yamlFileLines = new StringBuilder();
|
final StringBuilder yamlFileLines = new StringBuilder();
|
||||||
for (Object line : IOUtils.readLines(new InputStreamReader(getClass().getResourceAsStream("/default-images.yaml")))) {
|
for (Object line : IOUtils
|
||||||
yamlFileLines.append(line).append("\n");
|
.readLines(new InputStreamReader(getClass().getResourceAsStream("/default-images.yaml")))) {
|
||||||
}
|
yamlFileLines.append(line).append("\n");
|
||||||
|
|
||||||
ImagesToYamlImagesFromYamlDescriptor parser = new ImagesToYamlImagesFromYamlDescriptor(new Supplier<String>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String get() {
|
|
||||||
return yamlFileLines.toString();
|
|
||||||
}
|
}
|
||||||
});
|
ImagesToYamlImagesFromYamlDescriptor parser = new ImagesToYamlImagesFromYamlDescriptor(new YamlImagesFromFileConfig("/default-images.yaml"));
|
||||||
assertEquals(Iterables.getFirst(parser.get().keySet(), null), TEST1);
|
assertEquals(Iterables.getFirst(parser.get().keySet(), null), TEST1);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
@ -84,14 +85,15 @@ 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(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,
|
||||||
|
|
|
@ -19,13 +19,18 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.predicates;
|
package org.jclouds.virtualbox.predicates;
|
||||||
|
|
||||||
|
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.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
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.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;
|
||||||
|
@ -34,8 +39,9 @@ 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.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.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;
|
||||||
|
@ -46,6 +52,7 @@ 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.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
|
@ -53,18 +60,22 @@ 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 sourceMachineSpec;
|
private Injector injector;
|
||||||
|
private Function<IMachine, SshClient> sshClientForIMachine;
|
||||||
|
private Predicate<SshClient> sshResponds;
|
||||||
|
|
||||||
@Override
|
private MasterSpec machineSpec;
|
||||||
@BeforeClass(groups = "live")
|
|
||||||
public void setupClient() {
|
@Override
|
||||||
super.setupClient();
|
@BeforeClass(groups = "live")
|
||||||
String sourceName = VIRTUALBOX_IMAGE_PREFIX
|
public void setupClient() {
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
|
super.setupClient();
|
||||||
.getSimpleName());
|
injector = context.utils().injector();
|
||||||
|
|
||||||
|
String instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
StorageController ideController = StorageController
|
StorageController ideController = StorageController
|
||||||
.builder()
|
.builder()
|
||||||
|
@ -72,72 +83,81 @@ public class GuestAdditionsInstallerLiveTest extends
|
||||||
.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, 0, guestAdditionsIso).build();
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName)
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
.osTypeId("").memoryMB(512).cleanUpMode(CleanupMode.Full)
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
.controller(ideController).forceOverwrite(true).build();
|
|
||||||
|
|
||||||
Injector injector = context.utils().injector();
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
Function<String, String> configProperties = injector
|
IsoSpec isoSpec = IsoSpec
|
||||||
.getInstance(ValueOfConfigurationKeyOrNull.class);
|
.builder()
|
||||||
IsoSpec isoSpec = IsoSpec
|
.sourcePath(operatingSystemIso)
|
||||||
.builder()
|
.installationScript(
|
||||||
.sourcePath(operatingSystemIso)
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
.installationScript(
|
instanceVmSpec.getVmName())).build();
|
||||||
configProperties.apply(
|
|
||||||
VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
|
|
||||||
"HOSTNAME", sourceVmSpec.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()
|
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
.addNIC(networkInterfaceCard).build();
|
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
||||||
sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec)
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
.network(networkSpec).build();
|
.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 {
|
IMachine machine = null;
|
||||||
IMachine machine = getVmWithGuestAdditionsInstalled();
|
try {
|
||||||
machineUtils.applyForMachine(machine.getName(),
|
machine = cloneFromMaster();
|
||||||
new LaunchMachineIfNotAlreadyRunning(manager.get(),
|
machineController.ensureMachineIsLaunched(machine.getName());
|
||||||
ExecutionType.GUI, ""));
|
sshClientForIMachine = injector.getInstance(IMachineToSshClient.class);
|
||||||
assertTrue(machineUtils.sharedLockMachineAndApplyToSession(
|
SshClient client = sshClientForIMachine.apply(machine);
|
||||||
machine.getName(),
|
|
||||||
new Function<ISession, Boolean>() {
|
|
||||||
@Override
|
|
||||||
public Boolean apply(ISession session) {
|
|
||||||
return session.getMachine().getGuestPropertyValue(
|
|
||||||
"/VirtualBox/GuestAdd/Version") != null;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} finally {
|
|
||||||
for (VmSpec spec : ImmutableSet.of(sourceMachineSpec.getVmSpec())) {
|
|
||||||
machineController.ensureMachineHasPowerDown(spec.getVmName());
|
|
||||||
undoVm(spec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
sshResponds = injector.getInstance(SshResponds.class);
|
||||||
|
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh",
|
||||||
|
machine.getName());
|
||||||
|
|
||||||
private IMachine getVmWithGuestAdditionsInstalled() {
|
assertTrue(machineUtils.sharedLockMachineAndApplyToSession(machine.getName(),
|
||||||
try {
|
new Function<ISession, Boolean>() {
|
||||||
Injector injector = context.utils().injector();
|
@Override
|
||||||
return injector.getInstance(CreateAndInstallVm.class).apply(
|
public Boolean apply(ISession session) {
|
||||||
sourceMachineSpec);
|
String s = session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
|
||||||
} catch (IllegalStateException e) {
|
return isIpv4(s);
|
||||||
// already created
|
}
|
||||||
return manager.get().getVBox()
|
|
||||||
.findMachine(sourceMachineSpec.getVmSpec().getVmId());
|
private boolean isIpv4(String s) {
|
||||||
}
|
Pattern pattern = Pattern.compile(machineUtils.IP_V4_ADDRESS_PATTERN);
|
||||||
}
|
Matcher matcher = pattern.matcher(s);
|
||||||
|
return matcher.matches();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
} finally {
|
||||||
|
for (String vmNameOrId : ImmutableSet.of(machine.getName())) {
|
||||||
|
machineController.ensureMachineHasPowerDown(vmNameOrId);
|
||||||
|
undoVm(vmNameOrId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMachine cloneFromMaster() {
|
||||||
|
IMachine source = getVmWithGuestAdditionsInstalled();
|
||||||
|
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
|
||||||
|
.master(source).linked(true).build();
|
||||||
|
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
||||||
|
.apply(cloneSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
|
try {
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// already created
|
||||||
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
@ -56,68 +56,75 @@ 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 MasterSpec machineSpec;
|
||||||
|
private String instanceName;
|
||||||
|
|
||||||
private String osTypeId = "";
|
@Override
|
||||||
private String ideControllerName = "IDE Controller";
|
@BeforeClass(groups = "live")
|
||||||
private String cloneName;
|
public void setupClient() {
|
||||||
private String vmName;
|
super.setupClient();
|
||||||
private StorageController masterStorageController;
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
private MasterSpec masterMachineSpec;
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
private NetworkSpec networkSpec;
|
|
||||||
private CloneSpec cloneSpec;
|
|
||||||
|
|
||||||
@Override
|
StorageController ideController = StorageController
|
||||||
@BeforeClass(groups = "live")
|
.builder()
|
||||||
public void setupClient() {
|
.name("IDE Controller")
|
||||||
super.setupClient();
|
.bus(StorageBus.IDE)
|
||||||
vmName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(
|
||||||
|
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
|
||||||
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
|
|
||||||
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk(vmName)).autoDelete(true).controllerPort(0)
|
Injector injector = context.utils().injector();
|
||||||
.deviceSlot(1).build();
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
|
IsoSpec isoSpec = IsoSpec
|
||||||
.attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build();
|
.builder()
|
||||||
VmSpec masterSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).osTypeId(osTypeId)
|
.sourcePath(operatingSystemIso)
|
||||||
.controller(masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build();
|
.installationScript(
|
||||||
masterMachineSpec = MasterSpec.builder()
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
.iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(masterSpec)
|
instanceVmSpec.getVmName())).build();
|
||||||
.network(NetworkSpec.builder().build()).build();
|
|
||||||
|
|
||||||
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
|
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
|
||||||
.build();
|
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
|
||||||
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
this.networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
|
||||||
|
machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloneMachineFromAnotherMachine() {
|
||||||
|
IMachine source = getVmWithGuestAdditionsInstalled();
|
||||||
|
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
|
||||||
|
.master(source).linked(true).build();
|
||||||
|
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
||||||
|
.apply(cloneSpec);
|
||||||
|
assertTrue(isLinkedClone().apply(clone));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
public void testLinkedClone() {
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
|
try {
|
||||||
Injector injector = context.utils().injector();
|
Injector injector = context.utils().injector();
|
||||||
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
masterMachineSpec);
|
} catch (IllegalStateException e) {
|
||||||
|
// already created
|
||||||
VmSpec clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full)
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
.forceOverwrite(true).build();
|
}
|
||||||
|
}
|
||||||
this.cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(networkSpec).master(master).linked(true).build();
|
|
||||||
|
@Override
|
||||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
@AfterClass(groups = "live")
|
||||||
.apply(cloneSpec);
|
protected void tearDown() throws Exception {
|
||||||
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
assertTrue(isLinkedClone().apply(clone));
|
undoVm(vmName);
|
||||||
}
|
}
|
||||||
|
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,9 +45,15 @@ 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() {
|
||||||
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE).build();
|
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE).build();
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,140 @@
|
||||||
|
/**
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||||
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||||
|
import static org.testng.AssertJUnit.assertTrue;
|
||||||
|
|
||||||
|
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||||
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.MasterSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkAdapter;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.StorageController;
|
||||||
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
|
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
||||||
|
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
|
||||||
|
import org.testng.annotations.AfterClass;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.ISession;
|
||||||
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
|
import org.virtualbox_4_1.SessionState;
|
||||||
|
import org.virtualbox_4_1.StorageBus;
|
||||||
|
|
||||||
|
import com.google.common.base.CaseFormat;
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
|
@Test(groups = "live", testName = "MachineControllerLiveTest")
|
||||||
|
public class MachineControllerLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
|
private MasterSpec machineSpec;
|
||||||
|
private String instanceName;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@BeforeClass(groups = "live")
|
||||||
|
public void setupClient() {
|
||||||
|
super.setupClient();
|
||||||
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
|
StorageController ideController = StorageController
|
||||||
|
.builder()
|
||||||
|
.name("IDE Controller")
|
||||||
|
.bus(StorageBus.IDE)
|
||||||
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(
|
||||||
|
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
|
||||||
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
|
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
|
IsoSpec isoSpec = IsoSpec
|
||||||
|
.builder()
|
||||||
|
.sourcePath(operatingSystemIso)
|
||||||
|
.installationScript(
|
||||||
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
|
instanceVmSpec.getVmName())).build();
|
||||||
|
|
||||||
|
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 testEnsureMachineisLaunchedAndSessionIsUnlocked() {
|
||||||
|
cloneFromMaster();
|
||||||
|
ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName);
|
||||||
|
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
||||||
|
cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
|
||||||
|
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dependsOnMethods="testEnsureMachineisLaunchedAndSessionIsUnlocked")
|
||||||
|
public void testEnsureMachineCanBePoweredOffMoreThanOneTimeAndSessionIsUnlocked() {
|
||||||
|
ISession cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
|
||||||
|
cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
|
||||||
|
assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMachine cloneFromMaster() {
|
||||||
|
IMachine source = getVmWithGuestAdditionsInstalled();
|
||||||
|
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
|
||||||
|
.master(source).linked(true).build();
|
||||||
|
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
||||||
|
.apply(cloneSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
|
try {
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// already created
|
||||||
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@AfterClass(groups = "live")
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
|
undoVm(vmName);
|
||||||
|
}
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,144 @@
|
||||||
|
/**
|
||||||
|
* 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.util;
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||||
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||||
|
|
||||||
|
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||||
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
|
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.ExecutionType;
|
||||||
|
import org.jclouds.virtualbox.domain.HardDisk;
|
||||||
|
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.MasterSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkAdapter;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
|
||||||
|
import org.jclouds.virtualbox.domain.NetworkSpec;
|
||||||
|
import org.jclouds.virtualbox.domain.StorageController;
|
||||||
|
import org.jclouds.virtualbox.domain.VmSpec;
|
||||||
|
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
|
||||||
|
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
|
||||||
|
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
|
||||||
|
import org.testng.annotations.AfterClass;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.ISession;
|
||||||
|
import org.virtualbox_4_1.LockType;
|
||||||
|
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||||
|
import org.virtualbox_4_1.SessionState;
|
||||||
|
import org.virtualbox_4_1.StorageBus;
|
||||||
|
|
||||||
|
import com.google.common.base.CaseFormat;
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
|
@Test(groups = "live", testName = "MachineControllerLiveTest")
|
||||||
|
public class MachineUtilsLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
|
private MasterSpec machineSpec;
|
||||||
|
private String instanceName;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@BeforeClass(groups = "live")
|
||||||
|
public void setupClient() {
|
||||||
|
super.setupClient();
|
||||||
|
instanceName = VIRTUALBOX_IMAGE_PREFIX
|
||||||
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
|
StorageController ideController = StorageController
|
||||||
|
.builder()
|
||||||
|
.name("IDE Controller")
|
||||||
|
.bus(StorageBus.IDE)
|
||||||
|
.attachISO(0, 0, operatingSystemIso)
|
||||||
|
.attachHardDisk(
|
||||||
|
HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1)
|
||||||
|
.autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build();
|
||||||
|
|
||||||
|
VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512)
|
||||||
|
.cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||||
|
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||||
|
IsoSpec isoSpec = IsoSpec
|
||||||
|
.builder()
|
||||||
|
.sourcePath(operatingSystemIso)
|
||||||
|
.installationScript(
|
||||||
|
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME",
|
||||||
|
instanceVmSpec.getVmName())).build();
|
||||||
|
|
||||||
|
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 lockSessionOnMachine() {
|
||||||
|
IMachine machine = cloneFromMaster();
|
||||||
|
ISession session = machineUtils.lockSessionOnMachineAndApply(instanceName, LockType.Shared,
|
||||||
|
new Function<ISession, ISession>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ISession apply(ISession session) {
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
checkState(session.getState().equals(SessionState.Unlocked));
|
||||||
|
machine = manager.get().getVBox().findMachine(instanceName);
|
||||||
|
undoVm(instanceName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private IMachine cloneFromMaster() {
|
||||||
|
IMachine source = getVmWithGuestAdditionsInstalled();
|
||||||
|
CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec())
|
||||||
|
.master(source).linked(true).build();
|
||||||
|
return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils)
|
||||||
|
.apply(cloneSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMachine getVmWithGuestAdditionsInstalled() {
|
||||||
|
MasterSpec masterSpecForTest = super.getMasterSpecForTest();
|
||||||
|
try {
|
||||||
|
Injector injector = context.utils().injector();
|
||||||
|
return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// already created
|
||||||
|
return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@AfterClass(groups = "live")
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
for (String vmName : ImmutableSet.of(instanceName)) {
|
||||||
|
undoVm(vmName);
|
||||||
|
}
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue