cleaning up virtualbox constructors, etc

This commit is contained in:
Adrian Cole 2011-10-29 12:30:24 +02:00
parent ca723ed909
commit c889077f50
5 changed files with 65 additions and 44 deletions

View File

@ -35,7 +35,6 @@ import java.io.File;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import org.eclipse.jetty.server.Server;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.compute.options.RunScriptOptions;
@ -44,9 +43,7 @@ import org.jclouds.domain.Credentials;
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.ssh.SshException; import org.jclouds.ssh.SshException;
import org.jclouds.virtualbox.config.VirtualBoxConstants;
import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning;
import org.jclouds.virtualbox.settings.KeyboardScancodes; import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.AccessMode; import org.virtualbox_4_1.AccessMode;
import org.virtualbox_4_1.DeviceType; import org.virtualbox_4_1.DeviceType;
@ -104,9 +101,12 @@ public class IsoToIMachine implements Function<String, IMachine> {
@Override @Override
public IMachine apply(@Nullable String isoName) { public IMachine apply(@Nullable String isoName) {
String port = System.getProperty(VirtualBoxConstants.VIRTUALBOX_JETTY_PORT, "8080"); // TODO: WTF :) this is a prerequisite, so check state as opposed to
String baseResource = "."; // starting.
Server server = new StartJettyIfNotAlreadyRunning(port).apply(baseResource); // ex checkState(endpoint accessible, "please start jetty on %s",
// endpoint)
// Server server = new
// StartJettyIfNotAlreadyRunning(port).apply(baseResource);
IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(settingsFile, osTypeId, vmId, forceOverwrite, IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(settingsFile, osTypeId, vmId, forceOverwrite,
manager).apply(vmName); manager).apply(vmName);
@ -174,13 +174,18 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
}); });
try { // TODO: See above.
logger.debug("Stopping Jetty server..."); // if you want to manage jetty, do it outside this class as it
server.stop(); // has too many responsibilities otherwise. Allow this class to focus
logger.debug("Jetty server stopped."); // solely on making an IMachine
} catch (Exception e) { //
logger.error(e, "Could not stop Jetty server."); // try {
} // logger.debug("Stopping Jetty server...");
// server.stop();
// logger.debug("Jetty server stopped.");
// } catch (Exception e) {
// logger.error(e, "Could not stop Jetty server.");
// }
return vm; return vm;
} }

View File

@ -19,6 +19,8 @@
package org.jclouds.virtualbox.functions.admin; package org.jclouds.virtualbox.functions.admin;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -47,18 +49,25 @@ public class StartJettyIfNotAlreadyRunning implements Function<String, Server> {
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final Server jetty;
private final int port; private final int port;
public StartJettyIfNotAlreadyRunning(Server jetty, @Named(VirtualBoxConstants.VIRTUALBOX_JETTY_PORT) int port) {
this.jetty = checkNotNull(jetty, "jetty");
this.port = port;
}
// TODO: getting an instance of the Server object should really be done in
// Guice, so inside a *Module class, perhaps as a @Provides method
@Inject @Inject
public StartJettyIfNotAlreadyRunning(@Named(VirtualBoxConstants.VIRTUALBOX_JETTY_PORT) final String port) { public StartJettyIfNotAlreadyRunning(@Named(VirtualBoxConstants.VIRTUALBOX_JETTY_PORT) int port) {
this.port = Integer.parseInt(port); this(ServerJetty.getInstance().getServer(), port);
} }
@Override @Override
public Server apply(@Nullable String baseResource) { public Server apply(@Nullable String baseResource) {
final Server server = ServerJetty.getInstance().getServer(); if (!jetty.getState().equals(Server.STARTED)
// TODO code smell = hard coding addresses or ports!!
if (!server.getState().equals(Server.STARTED)
&& !new InetSocketAddressConnect().apply(new IPSocket("localhost", port))) { && !new InetSocketAddressConnect().apply(new IPSocket("localhost", port))) {
ResourceHandler resource_handler = new ResourceHandler(); ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true); resource_handler.setDirectoriesListed(true);
@ -69,17 +78,17 @@ public class StartJettyIfNotAlreadyRunning implements Function<String, Server> {
HandlerList handlers = new HandlerList(); HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers); jetty.setHandler(handlers);
try { try {
server.start(); jetty.start();
} catch (Exception e) { } catch (Exception e) {
logger.error(e, "Server jetty could not be started at this %s", baseResource); logger.error(e, "Server jetty could not be started at this %s", baseResource);
} }
return server; return jetty;
} else { } else {
logger.debug("Server jetty serving %s already running. Skipping start", baseResource); logger.debug("Server jetty serving %s already running. Skipping start", baseResource);
return server; return jetty;
} }
} }

View File

@ -19,67 +19,74 @@
package org.jclouds.virtualbox.functions.admin; package org.jclouds.virtualbox.functions.admin;
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.compute.options.RunScriptOptions.Builder.runAsRoot;
import java.net.URI; import java.net.URI;
import javax.annotation.Nullable;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeService;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
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.Predicate;
public class StartVBoxIfNotAlreadyRunning implements Function<URI, VirtualBoxManager> { public class StartVBoxIfNotAlreadyRunning implements Function<URI, VirtualBoxManager> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private ComputeServiceContext context;
private String hostId;
private Credentials credentials;
public StartVBoxIfNotAlreadyRunning(ComputeServiceContext context, String hostId, Credentials credentials) { private final ComputeService compute;
this.context = context; private final VirtualBoxManager manager;
this.hostId = hostId; private final Predicate<IPSocket> socketTester;
this.credentials = credentials; private final String hostId;
private final Credentials credentials;
public StartVBoxIfNotAlreadyRunning(ComputeService compute, VirtualBoxManager manager,
Predicate<IPSocket> socketTester, String hostId, Credentials credentials) {
this.compute = checkNotNull(compute, "compute");
this.manager = checkNotNull(manager, "manager");
this.socketTester = checkNotNull(socketTester, "socketTester");
this.hostId = checkNotNull(hostId, "hostId");
this.credentials = checkNotNull(credentials, "credentials");
} }
@Override @Override
public VirtualBoxManager apply(@Nullable URI endpoint) { public VirtualBoxManager apply(URI endpoint) {
checkState(compute.getNodeMetadata(hostId) != null, "compute service %s cannot locate node with id %s", compute,
hostId);
checkNotNull(endpoint, "endpoint to virtualbox websrvd is needed");
// TODO Really create new object here? Should we cache these instead? if (socketTester.apply(new IPSocket(endpoint.getHost(), endpoint.getPort()))) {
VirtualBoxManager manager = VirtualBoxManager.createInstance(hostId);
if (new InetSocketAddressConnect().apply(new IPSocket(endpoint.getHost(), endpoint.getPort()))) {
manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential); manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential);
return manager; return manager;
} }
logger.debug("disabling password access"); logger.debug("disabling password access");
context.getComputeService().runScriptOnNode(hostId, "VBoxManage setproperty websrvauthlibrary null", runAsRoot(false).wrapInInitScript(false)); compute.runScriptOnNode(hostId, "VBoxManage setproperty websrvauthlibrary null", runAsRoot(false)
.wrapInInitScript(false));
logger.debug("starting vboxwebsrv"); logger.debug("starting vboxwebsrv");
String vboxwebsrv = "vboxwebsrv -t 10000 -v -b"; String vboxwebsrv = "vboxwebsrv -t 10000 -v -b";
if (isOSX(hostId)) if (isOSX(hostId))
vboxwebsrv = "cd /Applications/VirtualBox.app/Contents/MacOS/ && " + vboxwebsrv; vboxwebsrv = "cd /Applications/VirtualBox.app/Contents/MacOS/ && " + vboxwebsrv;
context.getComputeService().runScriptOnNode(hostId, vboxwebsrv, runAsRoot(false).wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv")); compute.runScriptOnNode(hostId, vboxwebsrv, runAsRoot(false).wrapInInitScript(false).blockOnComplete(false)
.nameTask("vboxwebsrv"));
manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential); manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential);
return manager; return manager;
} }
private boolean isOSX(String hostId) { private boolean isOSX(String hostId) {
return context.getComputeService().getNodeMetadata(hostId).getOperatingSystem().getDescription().equals( return compute.getNodeMetadata(hostId).getOperatingSystem().getDescription().equals("Mac OS X");
"Mac OS X");
} }
} }

View File

@ -130,7 +130,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
} }
@AfterGroups(groups = "live") @AfterGroups(groups = "live")
protected void tearDown() { protected void tearDown() throws Exception {
if (machine != null) if (machine != null)
adapter.destroyNode(machine.getId() + ""); adapter.destroyNode(machine.getId() + "");
super.tearDown(); super.tearDown();

View File

@ -58,7 +58,7 @@ public class TestUtils {
} }
public static ComputeServiceContext computeServiceForLocalhostAndGuest(String hostId, String hostname, public static ComputeServiceContext computeServiceForLocalhostAndGuest(String hostId, String hostname,
String guestId, String guestHostname, Credentials guestLogin) throws IOException { String guestId, String guestHostname, Credentials guestLogin) {
Node host = Node.builder().id(hostId).name("host installing virtualbox").hostname(hostname) Node host = Node.builder().id(hostId).name("host installing virtualbox").hostname(hostname)
.osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name")) .osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name"))