made jetty serve preseed.cfg from inside the jar (was serving from src/test/resources)

This commit is contained in:
David Ribeiro Alves 2012-03-15 04:42:33 +00:00
parent 04f72a67d7
commit c9c4a1aef0
6 changed files with 92 additions and 19 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:23232/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

@ -19,35 +19,42 @@
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.net.URI;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
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 +67,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 +81,31 @@ 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 {
logger.debug(">> starting jetty to serve %s", preconfigurationUrl);
Resource resource = Resource.newClassPathResource("preseed.cfg");
checkState(resource.getInputStream() != null, "cannot find preseed.cfg");
ContextHandler capHandler = new ContextHandler();
capHandler.setContextPath("/preseed.cfg");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setBaseResource(resource);
capHandler.setHandler(resourceHandler);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { capHandler, new DefaultHandler() });
jetty.setHandler(handlers);
jetty.start();
logger.debug("<< serving %s", resourceHandler.getBaseResource());
} 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

@ -26,7 +26,7 @@ import org.jclouds.scriptbuilder.domain.Statement;
import com.google.common.collect.ImmutableList;
/**
* Deletes /etc/gshadow.lock https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864.
* Deletes /etc/gshadow.lock. see https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864.
*
* @author dralves
*

View File

@ -0,0 +1,59 @@
/**
* 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 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);
// if this opens up a file we're golden
IOUtils.toString(new URL("http://127.0.0.1:" + port + "/preseed.cfg").openStream());
}
}