mirror of https://github.com/apache/jclouds.git
Merge pull request #508 from dralves/jclouds-vbox
vbox - guests are now reachable through a non-nat ip
This commit is contained in:
commit
c604b47f2d
|
@ -17,8 +17,8 @@ That's it! Enjoy local cluster goodness by running:
|
|||
For java guidance look into src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java.
|
||||
|
||||
For now nat+host-only is the only available network configuration, nodes should be accessible from the host by:
|
||||
> ssh -i ~/.ssh/id_rsa -o "UserKnownHostsFile /dev/null" -o StrictHostKeyChecking=no dralves@127.0.0.1 -p 300X
|
||||
where X is the node index with regard to creation order (0,1,2,etc...)
|
||||
> ssh -i ~/.ssh/id_rsa -o "UserKnownHostsFile /dev/null" -o StrictHostKeyChecking=no me@192.168.86.X
|
||||
where X is the node index with regard to creation order starting at 2 (2,3,4, etc...)
|
||||
|
||||
It *should* behave as anyother provider, if not please report.
|
||||
|
||||
|
@ -26,4 +26,5 @@ It *should* behave as anyother provider, if not please report.
|
|||
|
||||
- jclouds-vbox is still at alpha stage please report any issues you find.
|
||||
- jclouds-vbox has been mostly tested on Mac OSX, it might work on Linux, but it won't work on windows for the moment.
|
||||
- cached isos, vm's and most configs are kept at ~/.jclouds-vbox/ by default.
|
||||
- cached isos, vm's and most configs are kept at ~/.jclouds-vbox/ by default.
|
||||
- jclouds-vbox assumes vbox has the default host-only network vboxnet0, that the network is in 192.168.86.0/255.255.255.0 and that the host as address 1 in this network.
|
|
@ -248,7 +248,8 @@ public class VirtualBoxComputeServiceContextModule extends
|
|||
|
||||
@VisibleForTesting
|
||||
public static final Map<MachineState, NodeState> machineToNodeState = ImmutableMap
|
||||
.<MachineState, NodeState> builder().put(MachineState.Running, NodeState.RUNNING)
|
||||
.<MachineState, NodeState> builder()
|
||||
.put(MachineState.Running, NodeState.RUNNING)
|
||||
.put(MachineState.PoweredOff, NodeState.SUSPENDED)
|
||||
.put(MachineState.DeletingSnapshot, NodeState.PENDING)
|
||||
.put(MachineState.DeletingSnapshotOnline, NodeState.PENDING)
|
||||
|
@ -260,12 +261,15 @@ public class VirtualBoxComputeServiceContextModule extends
|
|||
.put(MachineState.Stopping, NodeState.PENDING)
|
||||
.put(MachineState.Restoring, NodeState.PENDING)
|
||||
// TODO What to map these states to?
|
||||
.put(MachineState.FirstOnline, NodeState.PENDING).put(MachineState.FirstTransient, NodeState.PENDING)
|
||||
.put(MachineState.LastOnline, NodeState.PENDING).put(MachineState.LastTransient, NodeState.PENDING)
|
||||
.put(MachineState.Teleported, NodeState.PENDING).put(MachineState.TeleportingIn, NodeState.PENDING)
|
||||
.put(MachineState.TeleportingPausedVM, NodeState.PENDING).put(MachineState.Aborted, NodeState.ERROR)
|
||||
.put(MachineState.FirstOnline, NodeState.PENDING)
|
||||
.put(MachineState.FirstTransient, NodeState.PENDING)
|
||||
.put(MachineState.LastOnline, NodeState.PENDING)
|
||||
.put(MachineState.LastTransient, NodeState.PENDING)
|
||||
.put(MachineState.Teleported, NodeState.PENDING)
|
||||
.put(MachineState.TeleportingIn, NodeState.PENDING)
|
||||
.put(MachineState.TeleportingPausedVM, NodeState.PENDING)
|
||||
.put(MachineState.Aborted, NodeState.ERROR)
|
||||
.put(MachineState.Stuck, NodeState.ERROR)
|
||||
|
||||
.put(MachineState.Null, NodeState.UNRECOGNIZED).build();
|
||||
|
||||
}
|
||||
|
|
|
@ -88,17 +88,17 @@ public class IMachineToNodeMetadata implements Function<IMachine, NodeMetadata>
|
|||
for (String nameProtocolnumberAddressInboudportGuestTargetport : natAdapter.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);
|
||||
ipTermination = inPort % NodeCreator.NODE_PORT_INIT;
|
||||
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(hostAddress)).loginPort(inPort);
|
||||
ipTermination = inPort % NodeCreator.NODE_PORT_INIT + 2;
|
||||
// nodeMetadataBuilder.publicAddresses(ImmutableSet.of(hostAddress)).loginPort(inPort);
|
||||
}
|
||||
}
|
||||
|
||||
nodeMetadataBuilder.privateAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + ""));
|
||||
nodeMetadataBuilder.publicAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + ""));
|
||||
|
||||
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
|
||||
nodeMetadataBuilder.credentials(loginCredentials);
|
||||
|
|
|
@ -23,7 +23,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -32,9 +31,10 @@ import javax.inject.Singleton;
|
|||
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
|
||||
import org.jclouds.compute.callables.RunScriptOnNode;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
import org.jclouds.compute.domain.NodeMetadataBuilder;
|
||||
import org.jclouds.compute.options.RunScriptOptions;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.virtualbox.domain.BridgedIf;
|
||||
import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule;
|
||||
import org.jclouds.virtualbox.domain.CloneSpec;
|
||||
import org.jclouds.virtualbox.domain.ExecutionType;
|
||||
import org.jclouds.virtualbox.domain.Master;
|
||||
|
@ -48,13 +48,17 @@ import org.jclouds.virtualbox.statements.SetIpAddress;
|
|||
import org.jclouds.virtualbox.util.MachineUtils;
|
||||
import org.virtualbox_4_1.CleanupMode;
|
||||
import org.virtualbox_4_1.IMachine;
|
||||
import org.virtualbox_4_1.INetworkAdapter;
|
||||
import org.virtualbox_4_1.IProgress;
|
||||
import org.virtualbox_4_1.ISession;
|
||||
import org.virtualbox_4_1.NetworkAttachmentType;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
/**
|
||||
* Creates nodes, by cloning a master vm and based on the provided {@link NodeSpec}. Must be
|
||||
|
@ -70,7 +74,7 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
|||
public static final int NODE_PORT_INIT = 3000;
|
||||
|
||||
// TODO parameterize
|
||||
public static final String VMS_NETWORK = "33.33.33.";
|
||||
public static final String VMS_NETWORK = "192.168.86.";
|
||||
|
||||
// TODO parameterize
|
||||
public static final String HOST_ONLY_IFACE_NAME = "vboxnet0";
|
||||
|
@ -97,8 +101,8 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
|||
RunScriptOnNode.Factory scriptRunnerFactory, Supplier<NodeMetadata> hostSupplier) {
|
||||
this.manager = manager;
|
||||
this.cloner = cloner;
|
||||
this.nodePorts = new AtomicInteger(NODE_PORT_INIT + 1);
|
||||
this.nodeIps = new AtomicInteger(1);
|
||||
this.nodePorts = new AtomicInteger(NODE_PORT_INIT);
|
||||
this.nodeIps = new AtomicInteger(2);
|
||||
this.machineUtils = machineUtils;
|
||||
this.imachineToNodeMetadata = imachineToNodeMetadata;
|
||||
this.scriptRunnerFactory = scriptRunnerFactory;
|
||||
|
@ -145,10 +149,6 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
|||
.addHostInterfaceName(HOST_ONLY_IFACE_NAME).slot(1L).build();
|
||||
|
||||
NetworkSpec networkSpec = createNetworkSpecForHostOnlyNATNICs(natIfaceCard, hostOnlyIfaceCard);
|
||||
// //
|
||||
|
||||
// CASE BRIDGED
|
||||
// NetworkSpec networkSpec = createNetworkSpecForBridgedNIC();
|
||||
|
||||
CloneSpec cloneSpec = CloneSpec.builder().linked(USE_LINKED).master(master.getMachine()).network(networkSpec)
|
||||
.vm(cloneVmSpec).build();
|
||||
|
@ -157,13 +157,15 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
|||
|
||||
new LaunchMachineIfNotAlreadyRunning(manager.get(), EXECUTION_TYPE, "").apply(cloned);
|
||||
|
||||
// IMachineToNodeMetadata produces the final ip's but these need to be set before so we build a
|
||||
// NodeMetadata just for the sake of running the gshadow and setip scripts
|
||||
NodeMetadata partialNodeMetadata = buildPartialNodeMetadata(cloned);
|
||||
|
||||
// see DeleteGShadowLock for a detailed explanation
|
||||
machineUtils
|
||||
.runScriptOnNode(imachineToNodeMetadata.apply(cloned), new DeleteGShadowLock(), RunScriptOptions.NONE);
|
||||
machineUtils.runScriptOnNode(partialNodeMetadata, new DeleteGShadowLock(), RunScriptOptions.NONE);
|
||||
|
||||
// CASE NAT + HOST-ONLY
|
||||
machineUtils.runScriptOnNode(imachineToNodeMetadata.apply(cloned), new SetIpAddress(hostOnlyIfaceCard),
|
||||
RunScriptOptions.NONE);
|
||||
machineUtils.runScriptOnNode(partialNodeMetadata, new SetIpAddress(hostOnlyIfaceCard), RunScriptOptions.NONE);
|
||||
// //
|
||||
|
||||
// TODO get credentials from somewhere else (they are also HC in
|
||||
|
@ -173,23 +175,33 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
|
|||
|
||||
return nodeAndInitialCredentials;
|
||||
}
|
||||
|
||||
private NodeMetadata buildPartialNodeMetadata(IMachine clone) {
|
||||
INetworkAdapter realNatAdapter = clone.getNetworkAdapter(0l);
|
||||
NodeMetadataBuilder nodeMetadataBuilder = new NodeMetadataBuilder();
|
||||
nodeMetadataBuilder.id(clone.getName());
|
||||
nodeMetadataBuilder.state(VirtualBoxComputeServiceContextModule.machineToNodeState.get(clone.getState()));
|
||||
nodeMetadataBuilder.publicAddresses(ImmutableSet.of(realNatAdapter.getNatDriver().getHostIP()));
|
||||
for (String nameProtocolnumberAddressInboudportGuestTargetport : realNatAdapter.getNatDriver().getRedirects()) {
|
||||
Iterable<String> stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport);
|
||||
String protocolNumber = Iterables.get(stuff, 1);
|
||||
String inboundPort = Iterables.get(stuff, 3);
|
||||
String targetPort = Iterables.get(stuff, 5);
|
||||
if ("1".equals(protocolNumber) && "22".equals(targetPort)) {
|
||||
int inPort = Integer.parseInt(inboundPort);
|
||||
nodeMetadataBuilder.loginPort(inPort);
|
||||
}
|
||||
}
|
||||
|
||||
LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true);
|
||||
nodeMetadataBuilder.credentials(loginCredentials);
|
||||
|
||||
return nodeMetadataBuilder.build();
|
||||
}
|
||||
|
||||
private NetworkSpec createNetworkSpecForHostOnlyNATNICs(NetworkInterfaceCard natIfaceCard,
|
||||
NetworkInterfaceCard hostOnlyIfaceCard) {
|
||||
return NetworkSpec.builder().addNIC(natIfaceCard).addNIC(hostOnlyIfaceCard).build();
|
||||
}
|
||||
|
||||
private NetworkSpec createNetworkSpecForBridgedNIC() {
|
||||
List<BridgedIf> activeBridgedInterfaces = new RetrieveActiveBridgedInterfaces(scriptRunnerFactory)
|
||||
.apply(hostSupplier.get());
|
||||
BridgedIf bridgedActiveInterface = checkNotNull(activeBridgedInterfaces.get(0), "activeBridgedIf");
|
||||
|
||||
NetworkAdapter bridgedAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged)
|
||||
.build();
|
||||
NetworkInterfaceCard bridgedNIC = NetworkInterfaceCard.builder().addNetworkAdapter(bridgedAdapter)
|
||||
.addHostInterfaceName(bridgedActiveInterface.getName()).slot(0L).build();
|
||||
|
||||
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(bridgedNIC).build();
|
||||
return networkSpec;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -119,10 +119,8 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
|
|||
}
|
||||
|
||||
protected void ensureIdentityPropertyIsSpecifiedOrTakeFromDefaults() {
|
||||
Properties defaultVBoxProperties = new VirtualBoxPropertiesBuilder().build();
|
||||
if (!System.getProperties().containsKey("test." + provider + ".identity"))
|
||||
System.setProperty("test." + provider + ".identity",
|
||||
defaultVBoxProperties.getProperty(Constants.PROPERTY_IDENTITY));
|
||||
System.setProperty("test." + provider + ".identity", "administrator");
|
||||
}
|
||||
|
||||
@BeforeClass(groups = "live")
|
||||
|
|
|
@ -44,7 +44,7 @@ public class IMachineToNodeMetadataTest {
|
|||
IMachine vm = createNiceMock(IMachine.class);
|
||||
|
||||
expect(vm.getName()).andReturn("mocked-vm").anyTimes();
|
||||
expect(vm.getState()).andReturn(MachineState.PoweredOff).once();
|
||||
expect(vm.getState()).andReturn(MachineState.PoweredOff).anyTimes();
|
||||
|
||||
INetworkAdapter nat = createNiceMock(INetworkAdapter.class);
|
||||
INATEngine natEng = createNiceMock(INATEngine.class);
|
||||
|
@ -53,7 +53,7 @@ public class IMachineToNodeMetadataTest {
|
|||
expect(nat.getAttachmentType()).andReturn(NetworkAttachmentType.NAT).once();
|
||||
expect(nat.getNatDriver()).andReturn(natEng).anyTimes();
|
||||
expect(natEng.getHostIP()).andReturn("127.0.0.1").once();
|
||||
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3001,,22"));
|
||||
expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3000,,22"));
|
||||
|
||||
INetworkAdapter hostOnly = createNiceMock(INetworkAdapter.class);
|
||||
|
||||
|
@ -63,9 +63,9 @@ public class IMachineToNodeMetadataTest {
|
|||
|
||||
assertEquals("mocked-vm", node.getName());
|
||||
assertEquals(1, node.getPrivateAddresses().size());
|
||||
assertEquals((NodeCreator.VMS_NETWORK + 1), Iterables.get(node.getPrivateAddresses(), 0));
|
||||
assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPrivateAddresses(), 0));
|
||||
assertEquals(1, node.getPublicAddresses().size());
|
||||
assertEquals("127.0.0.1", Iterables.get(node.getPublicAddresses(), 0));
|
||||
assertEquals(3001, node.getLoginPort());
|
||||
assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPublicAddresses(), 0));
|
||||
assertEquals(22, node.getLoginPort());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue