Merge pull request #447 from dralves/jclouds-vbox-nat-host-only-working-cluster-and-live-test

whirr working with vbox
This commit is contained in:
Adrian Cole 2012-03-15 00:34:50 -07:00
commit d778465f02
14 changed files with 223 additions and 57 deletions

View File

@ -78,7 +78,7 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder {
properties.put(VIRTUALBOX_IMAGES_DESCRIPTOR, yamlDescriptor);
properties.put(VIRTUALBOX_PRECONFIGURATION_URL, "http://10.0.2.2:8080/src/test/resources/preseed.cfg");
properties.put(VIRTUALBOX_PRECONFIGURATION_URL, "http://10.0.2.2:23232/preseed.cfg");
return properties;
}

View File

@ -96,7 +96,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
NodeSpec nodeSpec = NodeSpec.builder().master(master).name(name).tag(tag).template(template).build();
return cloneCreator.apply(nodeSpec);
} catch (Exception e) {
throw new RuntimeException(e);
throw Throwables.propagate(e);
}
}

View File

@ -70,7 +70,7 @@ import org.jclouds.virtualbox.functions.IMachineToHardware;
import org.jclouds.virtualbox.functions.IMachineToImage;
import org.jclouds.virtualbox.functions.IMachineToNodeMetadata;
import org.jclouds.virtualbox.functions.IMachineToSshClient;
import org.jclouds.virtualbox.functions.MastersCache;
import org.jclouds.virtualbox.functions.MastersLoadingCache;
import org.jclouds.virtualbox.functions.NodeCreator;
import org.jclouds.virtualbox.functions.YamlImagesFromFileConfig;
import org.jclouds.virtualbox.functions.admin.FileDownloadFromURI;
@ -141,7 +141,7 @@ public class VirtualBoxComputeServiceContextModule extends
}).to((Class) YamlImagesFromFileConfig.class);
// the master machines cache
bind(new TypeLiteral<LoadingCache<Image, Master>>() {
}).to((Class) MastersCache.class);
}).to((Class) MastersLoadingCache.class);
// the master creating function
bind(new TypeLiteral<Function<MasterSpec, IMachine>>() {
}).to((Class) CreateAndInstallVm.class);

View File

@ -99,6 +99,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
.getVBox()
.createMachine(settingsFile, vmSpec.getVmName(), vmSpec.getOsTypeId(), vmSpec.getVmId(),
vmSpec.isForceOverwrite());
List<CloneOptions> options = new ArrayList<CloneOptions>();
if (isLinkedClone)
options.add(CloneOptions.Link);

View File

@ -18,9 +18,9 @@
*/
package org.jclouds.virtualbox.functions;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import java.net.URI;
import java.util.List;
@ -44,12 +44,10 @@ import org.jclouds.virtualbox.util.MachineController;
import org.jclouds.virtualbox.util.MachineUtils;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.LockType;
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.cache.LoadingCache;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@ -62,7 +60,6 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Supplier<VirtualBoxManager> manager;
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
private final GuestAdditionsInstaller guestAdditionsInstaller;
private final Predicate<SshClient> sshResponds;
@ -75,7 +72,6 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
@Inject
public CreateAndInstallVm(
Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
GuestAdditionsInstaller guestAdditionsInstaller,
IMachineToNodeMetadata imachineToNodeMetadata,
@ -83,7 +79,6 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
Function<IMachine, SshClient> sshClientForIMachine,
MachineUtils machineUtils,
@Preconfiguration LoadingCache<IsoSpec, URI> preConfiguration, MachineController machineController) {
this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.sshResponds = sshResponds;
this.sshClientForIMachine = sshClientForIMachine;
@ -113,6 +108,7 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
configureOsInstallationWithKeyboardSequence(vmName,
installationKeySequence);
SshClient client = sshClientForIMachine.apply(vm);
logger.debug(">> awaiting installation to finish node(%s)", vmName);
@ -126,13 +122,13 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
logger.debug(">> awaiting post-installation actions on vm: %s", vmName);
// NodeMetadata vmMetadata = imachineToNodeMetadata.apply(vm);
NodeMetadata vmMetadata = imachineToNodeMetadata.apply(vm);
// ListenableFuture<ExecResponse> execFuture =
// machineUtils.runScriptOnNode(vmMetadata, call("cleanupUdevIfNeeded"), RunScriptOptions.NONE);
ListenableFuture<ExecResponse> execFuture =
machineUtils.runScriptOnNode(vmMetadata, call("cleanupUdevIfNeeded"), RunScriptOptions.NONE);
// ExecResponse execResponse = Futures.getUnchecked(execFuture);
// checkState(execResponse.getExitStatus() == 0);
ExecResponse execResponse = Futures.getUnchecked(execFuture);
checkState(execResponse.getExitStatus() == 0);
logger.debug(
"<< installation of image complete. Powering down node(%s)",

View File

@ -64,18 +64,21 @@ import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.cache.AbstractLoadingCache;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
/**
* Does most of the work wrt to creating the master image.
* A {@link LoadingCache} for masters. If the requested master has been previously created this
* returns it, if not it coordinates its creation including downloading isos and creating
* cache/config directories.
*
* @author dralves
*
*/
@Singleton
public class MastersCache extends AbstractLoadingCache<Image, Master> {
public class MastersLoadingCache extends AbstractLoadingCache<Image, Master> {
// TODO parameterize
public static final int MASTER_PORT = 2222;
@ -94,7 +97,7 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
private String version;
@Inject
public MastersCache(@Named(Constants.PROPERTY_BUILD_VERSION) String version,
public MastersLoadingCache(@Named(Constants.PROPERTY_BUILD_VERSION) String version,
@Named(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) String installationKeySequence,
@Named(VIRTUALBOX_WORKINGDIR) String workingDir, Function<MasterSpec, IMachine> masterLoader,
Supplier<Map<Image, YamlImage>> yamlMapper, Supplier<VirtualBoxManager> manager,
@ -114,7 +117,7 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
this.version = Iterables.get(Splitter.on('r').split(version), 0);
this.isoDownloader = isoDownloader;
}
@PostConstruct
public void createCacheDirStructure() {
if (!new File(workingDir).exists()) {
@ -201,7 +204,7 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
}
return null;
}
private String getFilePathOrDownload(String httpUrl) throws ExecutionException {
String fileName = httpUrl.substring(httpUrl.lastIndexOf('/') + 1, httpUrl.length());
File localFile = new File(isosDir, fileName);

View File

@ -43,10 +43,12 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.NodeSpec;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.statements.DeleteGShadowLock;
import org.jclouds.virtualbox.statements.SetIpAddress;
import org.jclouds.virtualbox.util.MachineUtils;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.NetworkAttachmentType;
import org.virtualbox_4_1.VirtualBoxManager;
@ -54,6 +56,13 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
* Creates nodes, by cloning a master vm and based on the provided {@link NodeSpec}. Must be
* synchronized mainly because of snapshot creation (must be synchronized on a per-master-basis).
*
* @author dralves
*
*/
@Singleton
public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials<IMachine>> {
@ -69,6 +78,9 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
// TODO parameterize
public static final boolean USE_LINKED = true;
// TODO parameterize
public static final ExecutionType EXECUTION_TYPE = ExecutionType.HEADLESS;
private final Supplier<VirtualBoxManager> manager;
private final Function<CloneSpec, IMachine> cloner;
private final AtomicInteger nodePorts;
@ -95,7 +107,7 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
}
@Override
public NodeAndInitialCredentials<IMachine> apply(NodeSpec nodeSpec) {
public synchronized NodeAndInitialCredentials<IMachine> apply(NodeSpec nodeSpec) {
checkNotNull(nodeSpec, "NodeSpec");
@ -109,7 +121,8 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
} catch (Exception e) {
throw new RuntimeException("error opening vbox machine session: " + e.getMessage(), e);
}
session.getConsole().deleteSnapshot(master.getMachine().getCurrentSnapshot().getId());
IProgress progress = session.getConsole().deleteSnapshot(master.getMachine().getCurrentSnapshot().getId());
progress.waitForCompletion(-1);
session.unlockMachine();
}
String masterNameWithoutPrefix = master.getSpec().getVmSpec().getVmName().replace(VIRTUALBOX_IMAGE_PREFIX, "");
@ -142,7 +155,11 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
IMachine cloned = cloner.apply(cloneSpec);
new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "").apply(cloned);
new LaunchMachineIfNotAlreadyRunning(manager.get(), EXECUTION_TYPE, "").apply(cloned);
// see DeleteGShadowLock for a detailed explanation
machineUtils
.runScriptOnNode(imachineToNodeMetadata.apply(cloned), new DeleteGShadowLock(), RunScriptOptions.NONE);
// CASE NAT + HOST-ONLY
machineUtils.runScriptOnNode(imachineToNodeMetadata.apply(cloned), new SetIpAddress(hostOnlyIfaceCard),

View File

@ -40,16 +40,14 @@ import com.google.common.base.Throwables;
*/
public class TakeSnapshotIfNotAlreadyAttached implements Function<IMachine, ISnapshot> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private Supplier<VirtualBoxManager> manager;
private String snapshotName;
private String snapshotDesc;
public TakeSnapshotIfNotAlreadyAttached(Supplier<VirtualBoxManager> manager, String snapshotName, String snapshotDesc) {
this.manager = manager;
this.snapshotName = snapshotName;
@ -60,18 +58,34 @@ public class TakeSnapshotIfNotAlreadyAttached implements Function<IMachine, ISna
public ISnapshot apply(@Nullable IMachine machine) {
// Snapshot a machine
ISession session = null;
if(machine.getCurrentSnapshot() == null ) {
try {
session = manager.get().openMachineSession(machine);
IProgress progress = session.getConsole().takeSnapshot(snapshotName, snapshotDesc);
if (progress.getCompleted())
logger.debug("Snapshot %s (description: %s) taken from %s", snapshotName, snapshotDesc, machine.getName());
} catch (Exception e) {
logger.error(e, "Problem creating snapshot %s (descripton: %s) from machine %s", snapshotName, snapshotDesc, machine.getName());
Throwables.propagate(e);
assert false;
} finally {
session.unlockMachine();
if (machine.getCurrentSnapshot() == null) {
int retries = 10;
while (true) {
try {
session = manager.get().openMachineSession(machine);
IProgress progress = session.getConsole().takeSnapshot(snapshotName, snapshotDesc);
progress.waitForCompletion(-1);
logger.debug("Snapshot %s (description: %s) taken from %s", snapshotName, snapshotDesc,
machine.getName());
break;
} catch (Exception e) {
if (e.getMessage().contains("VirtualBox error: The object is not ready")) {
retries--;
if (retries == 0) {
logger.error(e,
"Problem creating snapshot (too many retries) %s (descripton: %s) from machine %s",
snapshotName, snapshotDesc, machine.getName());
throw Throwables.propagate(e);
}
}
logger.error(e, "Problem creating snapshot %s (descripton: %s) from machine %s", snapshotName,
snapshotDesc, machine.getName());
throw Throwables.propagate(e);
} finally {
if (session != null) {
session.unlockMachine();
}
}
}
}
return machine.getCurrentSnapshot();

View File

@ -19,35 +19,49 @@
package org.jclouds.virtualbox.functions.admin;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.propagate;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_PRECONFIGURATION_URL;
import java.io.IOException;
import java.net.URI;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.domain.IsoSpec;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheLoader;
import com.google.inject.Singleton;
/**
* @author Andrea Turli
* Sets up jetty so that it can serve the preseed.cfg file to automate master creation.
*
* TODO - Probably we can make this only start jetty. This has not been used to serve isos.
*
* @author Andrea Turli, David Alves
*/
@Singleton
public class StartJettyIfNotAlreadyRunning extends CacheLoader<IsoSpec, URI> {
@Resource
@javax.annotation.Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private Server jetty;
@ -60,7 +74,7 @@ public class StartJettyIfNotAlreadyRunning extends CacheLoader<IsoSpec, URI> {
}
@Override
public URI load(IsoSpec isoSpec) throws Exception {
public URI load(IsoSpec isoSpec) throws Exception {
try {
start();
} catch (Exception e) {
@ -74,24 +88,36 @@ public class StartJettyIfNotAlreadyRunning extends CacheLoader<IsoSpec, URI> {
if (jetty.getState().equals(Server.STARTED)) {
logger.debug("not starting jetty, as existing host is serving %s", preconfigurationUrl);
} else {
logger.debug(">> starting jetty to serve %s", preconfigurationUrl);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(true);
resourceHandler.setWelcomeFiles(new String[]{"index.html"});
resourceHandler.setResourceBase("");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resourceHandler, new DefaultHandler()});
jetty.setHandler(handlers);
try {
// find the the parent dir inside the jar to serve the file from
final String preseedFile = IOUtils
.toString(Resource.newSystemResource("preseed.cfg").getURL().openStream());
checkState(preseedFile != null);
// since we're only serving the preseed.cfg file respond to all requests with it
jetty.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println(preseedFile);
}
});
jetty.start();
} catch (Exception e) {
logger.error(e, "Server jetty could not be started for %s", preconfigurationUrl);
throw Throwables.propagate(e);
}
logger.debug("<< serving %s", resourceHandler.getBaseResource());
}
}
@PreDestroy()

View File

@ -0,0 +1,48 @@
/**
* 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 org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import com.google.common.collect.ImmutableList;
/**
* Deletes /etc/gshadow.lock. see https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864.
*
* @author dralves
*
*/
public class DeleteGShadowLock implements Statement {
@Override
public Iterable<String> functionDependencies(OsFamily family) {
return ImmutableList.of();
}
@Override
public String render(OsFamily family) {
if (checkNotNull(family, "family") == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
return "rm -f /etc/passwd.lock /etc/group.lock /etc/gshadow.lock";
}
}

View File

@ -10,6 +10,5 @@ function installModuleAssistantIfNeeded {
mkdir /etc/udev/rules.d/70-persistent-net.rules;
rm -rf /dev/.udev/;
rm /lib/udev/rules.d/75-persistent-net-generator.rules
rm -f /etc/passwd.lock /etc/group.lock /etc/gshadow.lock
fi
}

View File

@ -0,0 +1,62 @@
/**
* 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;
import static junit.framework.Assert.assertEquals;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Server;
import org.jclouds.virtualbox.config.VirtualBoxConstants;
import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning;
import org.testng.annotations.Test;
/**
* Tests that jetty is able to serve the preseed.cfg file. This test is here to have access to the
* defaultProperties() method in {@link VirtualBoxPropertiesBuilder}.
*
* @author dralves
*
*/
@Test(groups = "live", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningLiveTest")
public class StartJettyIfNotAlreadyRunningLiveTest {
@Test
public void testJettyServerServesPreseedFile() throws Exception {
Properties props = new VirtualBoxPropertiesBuilder().defaultProperties();
String preconfigurationUrl = props.getProperty(VirtualBoxConstants.VIRTUALBOX_PRECONFIGURATION_URL);
int port = URI.create(preconfigurationUrl).getPort();
Server server = new Server(port);
StartJettyIfNotAlreadyRunning starter = new StartJettyIfNotAlreadyRunning(preconfigurationUrl, server);
starter.load(null);
String preseedFileFromJetty = IOUtils.toString(new URL("http://127.0.0.1:" + port + "/preseed.cfg").openStream());
String preseedFileFromFile = IOUtils
.toString(this.getClass().getClassLoader().getResourceAsStream("preseed.cfg")) + "\n";
assertEquals(preseedFileFromFile, preseedFileFromJetty);
}
}

View File

@ -66,7 +66,7 @@ public class VirtualBoxExperimentLiveTest {
@Test
public void testLaunchCluster() throws RunNodesException {
int numNodes = 1;
int numNodes = 4;
final String clusterName = "test-launch-cluster";
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes,
TemplateOptions.Builder.runScript(AdminAccess.standard()));