First step on IsoSpec/NetworkSpec/IMachineSpec. Also reverted ubuntu version to 11.04.

This commit is contained in:
Mattias Holmqvist 2012-01-15 23:41:11 +01:00
parent f1427fbb74
commit ef0b689113
14 changed files with 545 additions and 206 deletions

View File

@ -64,7 +64,8 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder {
properties.put(VIRTUALBOX_ISO_URL, "http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso");
properties.put(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE, "<Esc><Esc><Enter> "
+ "/install/vmlinuz noapic preseed/url=PRECONFIGURATION_URL "
+ "debian-installer=en_US auto locale=en_US kbd-chooser/method=us " + "hostname=" + "HOSTNAME "
+ "debian-installer=en_US auto locale=en_US kbd-chooser/method=us "
+ "hostname=" + "HOSTNAME "
+ "fb=false debconf/frontend=noninteractive "
+ "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false "
+ "initrd=/install/initrd.gz -- <Enter>");

View File

@ -0,0 +1,113 @@
/*
* 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.domain;
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A complete specification of a "master" node, including the ISO, networking setup
* and the physical machine specification.
*/
public class IMachineSpec {
private VmSpec vmSpec;
private IsoSpec isoSpec;
private NetworkSpec networkSpec;
public static Builder builder() {
return new Builder();
}
public static class Builder {
private VmSpec vmSpec;
private IsoSpec isoSpec;
private NetworkSpec networkSpec;
public Builder vm(VmSpec vmSpec) {
this.vmSpec = vmSpec;
return this;
}
public Builder network(NetworkSpec networkSpec) {
this.networkSpec = networkSpec;
return this;
}
public Builder iso(IsoSpec isoSpec) {
this.isoSpec = isoSpec;
return this;
}
public IMachineSpec build() {
return new IMachineSpec(vmSpec, isoSpec, networkSpec);
}
}
public IMachineSpec(VmSpec vmSpec, IsoSpec isoSpec, NetworkSpec networkSpec) {
checkNotNull(vmSpec, "vmSpec");
checkNotNull(isoSpec, "isoSpec");
checkNotNull(networkSpec, "networkSpec");
this.vmSpec = vmSpec;
this.isoSpec = isoSpec;
this.networkSpec = networkSpec;
}
public VmSpec getVmSpec() {
return vmSpec;
}
public IsoSpec getIsoSpec() {
return isoSpec;
}
public NetworkSpec getNetworkSpec() {
return networkSpec;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof VmSpec) {
IMachineSpec other = (IMachineSpec) o;
return Objects.equal(vmSpec, other.vmSpec) &&
Objects.equal(isoSpec, other.isoSpec) &&
Objects.equal(networkSpec, other.networkSpec);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(vmSpec,isoSpec,networkSpec);
}
@Override
public String toString() {
return "IMachineSpec{" +
"vmSpec=" + vmSpec +
", isoSpec=" + isoSpec +
", networkSpec=" + networkSpec +
'}';
}
}

View File

@ -0,0 +1,116 @@
/*
* 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.domain;
import com.google.common.base.Objects;
import com.google.common.base.Supplier;
import org.jclouds.virtualbox.Preconfiguration;
import java.net.URI;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The information needed to create a machine from a .iso file.
*/
public class IsoSpec {
private final String installationKeySequence;
private final String sourcePath;
private final Supplier<URI> preConfigurationUri;
public IsoSpec(String sourcePath, String installationKeySequence, @Preconfiguration Supplier<URI> preConfigurationUri) {
checkNotNull(sourcePath, "sourcePath");
checkNotNull(installationKeySequence, "installationKeySequence");
checkNotNull(preConfigurationUri, "preConfigurationUri");
this.sourcePath = sourcePath;
this.installationKeySequence = installationKeySequence;
this.preConfigurationUri = preConfigurationUri;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String installationSequence;
private Supplier<URI> preConfigurationUri;
private String sourcePath;
public Builder installationScript(String installationSequence) {
this.installationSequence = installationSequence;
return this;
}
public Builder preConfiguration(Supplier<URI> preConfigurationUri) {
this.preConfigurationUri = preConfigurationUri;
return this;
}
public Builder sourcePath(String sourcePath) {
this.sourcePath = sourcePath;
return this;
}
public IsoSpec build() {
return new IsoSpec(sourcePath, installationSequence, preConfigurationUri);
}
}
public String getInstallationKeySequence() {
return installationKeySequence;
}
public Supplier<URI> getPreConfigurationUri() {
return preConfigurationUri;
}
public String getSourcePath() {
return sourcePath;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof VmSpec) {
IsoSpec other = (IsoSpec) o;
return Objects.equal(sourcePath, other.sourcePath) &&
Objects.equal(installationKeySequence, other.installationKeySequence) &&
Objects.equal(preConfigurationUri, other.preConfigurationUri);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(sourcePath, installationKeySequence, preConfigurationUri);
}
@Override
public String toString() {
return "IsoSpec{" +
"sourcePath='" + sourcePath + '\'' +
"installationKeySequence='" + installationKeySequence + '\'' +
", preConfigurationUri=" + preConfigurationUri +
'}';
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.domain;
import com.google.common.base.Objects;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Describes the network configuration for a VirtualBox machine.
*/
public class NetworkSpec {
private final Map<Long, NatAdapter> natNetworkAdapters;
public NetworkSpec(final Map<Long, NatAdapter> natNetworkAdapters) {
checkNotNull(natNetworkAdapters, "natNetworkAdapters");
this.natNetworkAdapters = natNetworkAdapters;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Map<Long, NatAdapter> natNetworkAdapters = new HashMap<Long, NatAdapter>();
public Builder natNetworkAdapter(int slot, NatAdapter adapter) {
this.natNetworkAdapters.put((long) slot, adapter);
return this;
}
public NetworkSpec build() {
return new NetworkSpec(natNetworkAdapters);
}
}
public Map<Long, NatAdapter> getNatNetworkAdapters() {
return Collections.unmodifiableMap(natNetworkAdapters);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof VmSpec) {
NetworkSpec other = (NetworkSpec) o;
return Objects.equal(natNetworkAdapters, other.natNetworkAdapters);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(natNetworkAdapters);
}
@Override
public String toString() {
return "NetworkSpec{" +
"natNetworkAdapters=" + natNetworkAdapters +
'}';
}
}

View File

@ -18,18 +18,13 @@
*/
package org.jclouds.virtualbox.domain;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Objects;
import org.virtualbox_4_1.CleanupMode;
import com.google.common.base.Objects;
import java.util.*;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A description of a Virtual Machine in VirtualBox.
@ -41,16 +36,14 @@ public class VmSpec {
private final String vmId;
private final long memory;
private final boolean forceOverwrite;
private final Map<Long, NatAdapter> natNetworkAdapters;
private final Set<StorageController> controllers;
private final CleanupMode cleanupMode;
public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters, CleanupMode cleanupMode) {
public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, CleanupMode cleanupMode) {
checkNotNull(vmId, "vmId");
checkNotNull(vmName, "vmName");
checkArgument(memory > 0, "memory must be > 0");
checkNotNull(controllers, "controllers");
checkNotNull(natNetworkAdapters, "natNetworkAdapters");
checkNotNull(cleanupMode, "cleanupMode");
this.vmId = vmId;
this.vmName = vmName;
@ -58,7 +51,6 @@ public class VmSpec {
this.memory = memory;
this.controllers = controllers;
this.forceOverwrite = forceOverwrite;
this.natNetworkAdapters = natNetworkAdapters;
this.cleanupMode = cleanupMode;
}
@ -74,7 +66,6 @@ public class VmSpec {
private String id;
private String osTypeId = "";
private boolean forceOverwrite;
private Map<Long, NatAdapter> natNetworkAdapters = new HashMap<Long, NatAdapter>();
private long memory;
private CleanupMode cleanUpMode;
@ -103,11 +94,6 @@ public class VmSpec {
return this;
}
public Builder natNetworkAdapter(int slot, NatAdapter adapter) {
this.natNetworkAdapters.put((long) slot, adapter);
return this;
}
public Builder memoryMB(int memorySize) {
this.memory = (long) memorySize;
return this;
@ -122,11 +108,10 @@ public class VmSpec {
checkNotNull(name, "name");
checkNotNull(id, "id");
checkArgument(memory > 0, "Memory must be set");
return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, natNetworkAdapters, cleanUpMode);
return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, cleanUpMode);
}
}
}
public String getVmId() {
return vmId;
}
@ -151,10 +136,6 @@ public class VmSpec {
return Collections.unmodifiableSet(controllers);
}
public Map<Long, NatAdapter> getNatNetworkAdapters() {
return Collections.unmodifiableMap(natNetworkAdapters);
}
public CleanupMode getCleanupMode() {
return cleanupMode;
}
@ -169,7 +150,6 @@ public class VmSpec {
Objects.equal(osTypeId, other.osTypeId) &&
Objects.equal(memory, other.memory) &&
Objects.equal(forceOverwrite, other.forceOverwrite) &&
Objects.equal(natNetworkAdapters, other.natNetworkAdapters) &&
Objects.equal(controllers, other.controllers) &&
Objects.equal(cleanupMode, other.cleanupMode);
}
@ -178,7 +158,7 @@ public class VmSpec {
@Override
public int hashCode() {
return Objects.hashCode(vmId, vmName, osTypeId, memory, forceOverwrite, natNetworkAdapters, controllers);
return Objects.hashCode(vmId, vmName, osTypeId, memory, forceOverwrite, controllers);
}
@Override
@ -189,7 +169,6 @@ public class VmSpec {
", memory='" + memory + '\'' +
", vmId='" + vmId + '\'' +
", forceOverwrite=" + forceOverwrite +
", natNetworkAdapters=" + natNetworkAdapters +
", controllers=" + controllers +
", cleanupMode=" + cleanupMode +
'}';

View File

@ -18,30 +18,20 @@
*/
package org.jclouds.virtualbox.functions;
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_INSTALLATION_KEY_SEQUENCE;
import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine;
import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndApply;
import static org.virtualbox_4_1.LockType.Shared;
import java.net.URI;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.callables.RunScriptOnNode.Factory;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.Preconfiguration;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.IMachineSpec;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.IMachine;
@ -49,16 +39,19 @@ import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import java.net.URI;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine;
import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndApply;
import static org.virtualbox_4_1.LockType.Shared;
@Singleton
public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
public class CreateAndInstallVm implements Function<IMachineSpec, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -66,9 +59,7 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
private final Supplier<VirtualBoxManager> manager;
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
private final ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull;
private final Supplier<URI> preconfiguration;
private final Predicate<SshClient> sshResponds;
private final ExecutionType executionType;
@ -78,61 +69,48 @@ public class CreateAndInstallVm implements Function<VmSpec, IMachine> {
private final Function<IMachine, SshClient> sshClientForIMachine;
@Inject
public CreateAndInstallVm(
Supplier<VirtualBoxManager> manager,
public CreateAndInstallVm(Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull,
Predicate<SshClient> sshResponds,
Function<IMachine, SshClient> sshClientForIMachine,
Supplier<NodeMetadata> host, RunScriptOnNode.Factory scriptRunner,
@Preconfiguration Supplier<URI> preconfiguration,
ExecutionType executionType) {
Predicate<SshClient> sshResponds, Function<IMachine, SshClient> sshClientForIMachine,
Supplier<NodeMetadata> host, RunScriptOnNode.Factory scriptRunner, ExecutionType executionType) {
this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull;
this.sshResponds = sshResponds;
this.sshClientForIMachine = sshClientForIMachine;
this.scriptRunner = scriptRunner;
this.host = host;
this.preconfiguration = preconfiguration;
this.executionType = executionType;
}
@Override
public IMachine apply(VmSpec vmSpec) {
public IMachine apply(IMachineSpec machineSpec) {
VmSpec vmSpec = machineSpec.getVmSpec();
IsoSpec isoSpec = machineSpec.getIsoSpec();
String vmName = vmSpec.getVmName();
// note this may not be reachable, as this likely uses the 10.2.2 address
URI preconfigurationUri = preconfiguration.get();
String keySequence = valueOfConfigurationKeyOrNull
.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
.replace("PRECONFIGURATION_URL",
preconfigurationUri.toASCIIString())
.replace("HOSTNAME", vmName);
final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists
.apply(vmSpec);
final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists.apply(machineSpec);
// Launch machine and wait for it to come online
ensureMachineIsLaunched(vmName);
sendKeyboardSequence(keySequence, vmName);
URI uri = isoSpec.getPreConfigurationUri().get();
String installationKeySequence = isoSpec.getInstallationKeySequence()
.replace("PRECONFIGURATION_URL", uri.toASCIIString());
sendKeyboardSequence(installationKeySequence, vmName);
SshClient client = sshClientForIMachine.apply(vm);
logger.debug(">> awaiting installation to finish node(%s)", vmName);
checkState(sshResponds.apply(client),
"timed out waiting for guest %s to be accessible via ssh", vmName);
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName);
logger.debug("<< installation of image complete. Powering down node(%s)",
vmName);
lockSessionOnMachineAndApply(manager.get(), Shared, vmName,
new Function<ISession, Void>() {
logger.debug("<< installation of image complete. Powering down node(%s)", vmName);
lockSessionOnMachineAndApply(manager.get(), Shared, vmName, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole()
.powerDown();
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return null;
}

View File

@ -36,12 +36,7 @@ import javax.inject.Singleton;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.config.VirtualBoxConstants;
import org.jclouds.virtualbox.domain.DeviceDetails;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoImage;
import org.jclouds.virtualbox.domain.NatAdapter;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.virtualbox_4_1.AccessMode;
import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.IMachine;
@ -57,7 +52,7 @@ import com.google.common.base.Supplier;
* @author Mattias Holmqvist
*/
@Singleton
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function<VmSpec, IMachine> {
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function<IMachineSpec, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -74,9 +69,9 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
}
@Override
public IMachine apply(@Nullable VmSpec launchSpecification) {
public IMachine apply(@Nullable IMachineSpec launchSpecification) {
final IVirtualBox vBox = manager.get().getVBox();
String vmName = launchSpecification.getVmName();
String vmName = launchSpecification.getVmSpec().getVmName();
try {
vBox.findMachine(vmName);
throw new IllegalStateException("Machine " + vmName + " is already registered.");
@ -92,17 +87,20 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ");
}
private IMachine createMachine(IVirtualBox vBox, VmSpec vmSpec) {
private IMachine createMachine(IVirtualBox vBox, IMachineSpec machineSpec) {
VmSpec vmSpec = machineSpec.getVmSpec();
String settingsFile = vBox.composeMachineFilename(vmSpec.getVmName(), workingDir);
IMachine newMachine = vBox.createMachine(settingsFile, vmSpec.getVmName(), vmSpec.getOsTypeId(),
vmSpec.getVmId(), vmSpec.isForceOverwrite());
manager.get().getVBox().registerMachine(newMachine);
ensureConfiguration(vmSpec);
ensureConfiguration(machineSpec);
return newMachine;
}
private void ensureConfiguration(VmSpec vmSpec) {
private void ensureConfiguration(IMachineSpec machineSpec) {
VmSpec vmSpec = machineSpec.getVmSpec();
NetworkSpec networkSpec = machineSpec.getNetworkSpec();
String vmName = vmSpec.getVmName();
// Change RAM
@ -118,7 +116,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
setupDvdsForController(vmSpec, vmName, controller);
// NAT
Map<Long, NatAdapter> natNetworkAdapters = vmSpec.getNatNetworkAdapters();
Map<Long, NatAdapter> natNetworkAdapters = networkSpec.getNatNetworkAdapters();
for (Map.Entry<Long, NatAdapter> natAdapterAndSlot : natNetworkAdapters.entrySet()) {
long slotId = natAdapterAndSlot.getKey();
NatAdapter natAdapter = natAdapterAndSlot.getValue();

View File

@ -70,9 +70,6 @@ public class VmSpecTest {
.osTypeId("Ubuntu")
.memoryMB(1024)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter(
0,
NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build())
.forceOverwrite(true)
.controller(
StorageController.builder().name("Controller")

View File

@ -22,10 +22,9 @@ package org.jclouds.virtualbox.functions;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.testng.Assert.assertEquals;
import com.google.inject.Injector;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
@ -45,7 +44,8 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
private static final boolean IS_LINKED_CLONE = true;
private VmSpec clonedVmSpec;
private VmSpec sourceVmSpec;
private IMachineSpec sourceMachineSpec;
private CleanupMode mode = CleanupMode.Full;
@ -64,8 +64,11 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).autoDelete(true).build()).attachISO(1, 1,
guestAdditionsIso).build();
sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode(
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode(
CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
IsoSpec isoSpec = IsoSpec.builder().build();
NetworkSpec networkSpec = NetworkSpec.builder().build();
sourceMachineSpec = IMachineSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(networkSpec).build();
clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode)
.forceOverwrite(true).build();
@ -86,7 +89,7 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
IS_LINKED_CLONE).apply(source);
assertEquals(clone.getName(), clonedVmSpec.getVmName());
} finally {
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceVmSpec))
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceMachineSpec.getVmSpec()))
undoVm(spec);
}
@ -94,11 +97,12 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
private IMachine getSourceNode() {
try {
return context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
sourceVmSpec);
Injector injector = context.utils().injector();
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
sourceMachineSpec);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox().findMachine(sourceVmSpec.getVmId());
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId());
}
}
}

View File

@ -19,28 +19,19 @@
package org.jclouds.virtualbox.functions;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.transform;
import static org.testng.Assert.assertTrue;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.NatAdapter;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -48,9 +39,16 @@ import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.StorageBus;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.inject.Guice;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.transform;
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.Assert.assertTrue;
/**
* @author Andrea Turli, Mattias Holmqvist
@ -80,14 +78,26 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
vmSpecification = VmSpec.builder().id("jclouds-image-iso-1").name(vmName).memoryMB(512).osTypeId("")
.controller(ideController)
.forceOverwrite(true)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
.cleanUpMode(CleanupMode.Full).build();
undoVm(vmSpecification);
}
public void testCreateImageMachineFromIso() throws Exception {
Injector injector = context.utils().injector();
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
IMachine imageMachine = context.utils().injector().getInstance(CreateAndInstallVm.class).apply(vmSpecification);
IMachineSpec machineSpec = IMachineSpec.builder().vm(vmSpecification)
.iso(IsoSpec.builder()
.sourcePath(operatingSystemIso)
.installationScript(configProperties
.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
.replace("HOSTNAME", vmSpecification.getVmName()))
.preConfiguration(preconfigurationUri)
.build())
.network(NetworkSpec.builder()
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build())
.build()).build();
IMachine imageMachine = injector.getInstance(CreateAndInstallVm.class).apply(machineSpec);
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
Image newImage = iMachineToImage.apply(imageMachine);
@ -107,6 +117,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
}
};
}
@Override
@AfterClass(groups = "live")
protected void tearDown() throws Exception {

View File

@ -21,11 +21,9 @@ package org.jclouds.virtualbox.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import com.google.inject.Injector;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ErrorCode;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
@ -47,38 +45,62 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
super.setupClient();
ideControllerName = "IDE Controller";
mode = CleanupMode.Full;
ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE).attachISO(0, 0,
operatingSystemIso).attachHardDisk(
HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).build()).attachISO(1, 1,
ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).build()).attachISO(1, 1,
guestAdditionsIso).build();
}
@Test
public void testCreateNewMachine() throws Exception {
String vmName = "jclouds-test-create-1-node";
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController)
.cleanUpMode(mode).osTypeId("Debian").forceOverwrite(true).build();
undoVm(launchSpecification);
VmSpec vmSpec = VmSpec.builder()
.id(vmName)
.name(vmName)
.memoryMB(512)
.controller(ideController)
.cleanUpMode(mode)
.osTypeId("Debian")
.forceOverwrite(true).build();
IMachineSpec machineSpec = IMachineSpec.builder()
.iso(IsoSpec.builder()
.sourcePath(operatingSystemIso)
.installationScript("")
.preConfiguration(preconfigurationUri)
.build())
.vm(vmSpec)
.network(NetworkSpec.builder().build()).build();
undoVm(vmSpec);
try {
IMachine debianNode = context.utils().injector().getInstance(
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(launchSpecification);
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec);
IMachine machine = manager.get().getVBox().findMachine(vmName);
assertEquals(debianNode.getName(), machine.getName());
} finally {
undoVm(launchSpecification);
undoVm(vmSpec);
}
}
@Test
public void testCreateNewMachineWithBadOsType() throws Exception {
String vmName = "jclouds-test-create-2-node";
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController)
VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController)
.cleanUpMode(mode).osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
undoVm(launchSpecification);
IsoSpec isoSpec = IsoSpec.builder()
.sourcePath(operatingSystemIso)
.installationScript("")
.preConfiguration(preconfigurationUri)
.build();
NetworkSpec networkSpec = NetworkSpec.builder().build();
IMachineSpec machineSpec = IMachineSpec.builder()
.iso(isoSpec)
.vm(vmSpec)
.network(networkSpec).build();
undoVm(vmSpec);
try {
context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
launchSpecification);
Injector injector = context.utils().injector();
injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
.apply(machineSpec);
fail();
} catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e);
@ -86,7 +108,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
// if osTypeId is not found.
assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND);
} finally {
undoVm(launchSpecification);
undoVm(vmSpec);
}
}

View File

@ -27,7 +27,14 @@ import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import com.google.common.base.Supplier;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import org.easymock.EasyMock;
import org.jclouds.virtualbox.Preconfiguration;
import org.jclouds.virtualbox.domain.IMachineSpec;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.VmSpec;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
@ -40,6 +47,8 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Suppliers;
import java.net.URI;
/**
* @author Mattias Holmqvist
*/
@ -51,11 +60,14 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
VirtualBoxManager manager = createMock(VirtualBoxManager.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
Supplier<URI> preconfiguration = createNiceMock(Supplier.class);
String vmName = "jclouds-image-my-ubuntu-image";
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
CleanupMode.Full).build();
IMachineSpec machineSpec = IMachineSpec.builder()
.iso(IsoSpec.builder().sourcePath("some.iso").installationScript("").preConfiguration(preconfiguration).build())
.vm(vmSpec)
.network(NetworkSpec.builder().build()).build();
IMachine createdMachine = createMock(IMachine.class);
ISession session = createMock(ISession.class);
@ -87,7 +99,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, createdMachine, vBox, session);
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification);
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec);
verify(manager, createdMachine, vBox, session);
}
@ -97,6 +109,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
Supplier<URI> preconfiguration = createNiceMock(Supplier.class);
String vmName = "jclouds-image-my-ubuntu-image";
IMachine registeredMachine = createMock(IMachine.class);
@ -108,7 +121,15 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
CleanupMode.Full).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification);
IMachineSpec machineSpec = IMachineSpec.builder()
.iso(IsoSpec.builder()
.sourcePath("some.iso")
.installationScript("dostuff")
.preConfiguration(preconfiguration).build())
.vm(launchSpecification)
.network(NetworkSpec.builder().build()).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec);
}
@Test(expectedExceptions = VBoxException.class)
@ -116,6 +137,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
Supplier<URI> preconfiguration = createNiceMock(Supplier.class);
String vmName = "jclouds-image-my-ubuntu-image";
String errorMessage = "VirtualBox error: Soem other VBox error";
@ -130,7 +152,15 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").cleanUpMode(CleanupMode.Full)
.memoryMB(1024).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification);
IMachineSpec machineSpec = IMachineSpec.builder()
.iso(IsoSpec.builder()
.sourcePath("some.iso")
.installationScript("dostuff")
.preConfiguration(preconfiguration).build())
.vm(launchSpecification)
.network(NetworkSpec.builder().build()).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec);
}

View File

@ -28,10 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.NatAdapter;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
@ -66,8 +63,7 @@ public class UnregisterMachineIfExistsAndDeleteItsMediaTest {
VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId)
.controller(ideController)
.forceOverwrite(true)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
.cleanUpMode(CleanupMode.Full).build();
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.findMachine(vmName)).andReturn(registeredMachine);

View File

@ -22,10 +22,9 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import com.google.inject.Injector;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.domain.*;
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
import org.testng.annotations.AfterMethod;
@ -53,7 +52,7 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
private String cloneName;
private String vmName;
private StorageController masterStorageController;
private VmSpec masterSpec;
private IMachineSpec masterMachineSpec;
private VmSpec cloneSpec;
@Override
@ -70,8 +69,15 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
.build();
masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE).attachISO(0, 0,
operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build();
masterSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId).controller(
VmSpec masterSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId).controller(
masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build();
masterMachineSpec = IMachineSpec.builder()
.iso(IsoSpec.builder()
.sourcePath(operatingSystemIso)
.preConfiguration(preconfigurationUri)
.installationScript("").build())
.vm(masterSpec)
.network(NetworkSpec.builder().build()).build();
cloneSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full)
.forceOverwrite(true).build();
@ -80,8 +86,9 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
@Test
public void testLinkedClone() {
IMachine master = context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
.apply(masterSpec);
Injector injector = context.utils().injector();
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
.apply(masterMachineSpec);
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, cloneSpec,
IS_LINKED_CLONE).apply(master);
@ -102,7 +109,7 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
@BeforeMethod
@AfterMethod
void cleanUpVms() {
for (VmSpec spec : ImmutableSet.of(cloneSpec, masterSpec))
for (VmSpec spec : ImmutableSet.of(cloneSpec, masterMachineSpec.getVmSpec()))
this.undoVm(spec);
}
}