cleanup on vbox

This commit is contained in:
Adrian Cole 2012-05-07 11:51:29 -07:00
parent a7412c40b5
commit d99c18140e
6 changed files with 142 additions and 89 deletions

View File

@ -33,7 +33,7 @@ import java.util.Properties;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.apis.internal.BaseApiMetadata;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.virtualbox.config.DefaultCacheNodeStoreModule;
import org.jclouds.virtualbox.config.HardcodeLocalhostAsNodeMetadataSupplier;
import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule;
import com.google.common.collect.ImmutableSet;
@ -106,7 +106,7 @@ public class VirtualBoxApiMetadata extends BaseApiMetadata {
.buildVersion("4.1.8r75467")
.defaultProperties(VirtualBoxApiMetadata.defaultProperties())
.view(ComputeServiceContext.class)
.defaultModules(ImmutableSet.<Class<? extends Module>>of(DefaultCacheNodeStoreModule.class, VirtualBoxComputeServiceContextModule.class));
.defaultModules(ImmutableSet.<Class<? extends Module>>of(HardcodeLocalhostAsNodeMetadataSupplier.class, VirtualBoxComputeServiceContextModule.class));
}
@Override

View File

@ -1,36 +0,0 @@
/**
* 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.config;
import java.net.URI;
import org.jclouds.byon.Node;
import org.jclouds.byon.config.CacheNodeStoreModule;
import org.jclouds.compute.domain.OsFamily;
import com.google.common.collect.ImmutableMap;
public class DefaultCacheNodeStoreModule extends CacheNodeStoreModule {
public DefaultCacheNodeStoreModule() {
super(ImmutableMap.of("host", Node.builder().id("host").name("host installing virtualbox").hostname("localhost")
.osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name")).osVersion(
System.getProperty("os.version")).group("ssh").username(System.getProperty("user.name"))
.credentialUrl(URI.create("file://" + System.getProperty("user.home") + "/.ssh/id_rsa")).build()));
}
}

View File

@ -0,0 +1,136 @@
/**
* 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.config;
import java.io.File;
import java.io.IOException;
import javax.inject.Singleton;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import com.google.common.base.Charsets;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
/**
* In particular, this binds {@code Supplier<NodeMetadata>} so that it can be used in ssh commands.
* Ssh is necessary for operations that cannot be performed in the virtual box api, such as clearing
* sessions.
*
* ex. once this is loaded, use Guice to inject {@code Supplier<NodeMetadata>} as {@code host} and
* {@link RunScriptOnNode#Factory} as factory, to start making commands like the following:
*
* <pre>
* import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
* import static org.jclouds.scriptbuilder.domain.Statements.*;
*
* ...
*
* // direct execute a script as opposed to using sudo, or an init wrapper
* ListenableFuture<ExecResponse> fooInTheFuture = factory.submit(host.get(),
* exec("echo foo"), runAsRoot(false).wrapInInitScript(false));
*
* ExecResponse foo = Futures.getUnchecked(fooInTheFuture);
*
* // call a set of commands that are defined in classpath/functions/function_name.sh
* ListenableFuture<ExecResponse> kill = factory.submit(host.get(),
* call("killsession"), runAsRoot(false).wrapInInitScript(false));
*
* ...
*
* </pre>
*
* <h3>Note</h3>
*
* People often forget to call {@link Future#get} when using {@link RunScriptOnNode.Factory#submit}.
* Don't forget!
*
* @author Adrian Cole
*/
public class HardcodeLocalhostAsNodeMetadataSupplier extends AbstractModule {
public static final String HOST_ID = "host";
public static final String HOSTNAME = System.getenv("HOSTNAME");
/**
* Lazy so that we don't hang up the injector reading a file
*/
@Provides
@Singleton
protected Supplier<NodeMetadata> lazySupplyHostAsNodeMetadata() {
return new Supplier<NodeMetadata>() {
@Override
public NodeMetadata get() {
String privateKey = readRsaIdentity();
return new NodeMetadataBuilder()
.id(HOST_ID)
.name("host installing virtualbox")
.hostname(HOSTNAME)
.operatingSystem(OperatingSystem.builder()
.family(OsFamily.LINUX)
.description(System.getProperty("os.name"))
.arch(System.getProperty("os.arch"))
.version(System.getProperty("os.version"))
.build())
.state(NodeState.RUNNING)
.location(new LocationBuilder().id(HOST_ID)
.scope(LocationScope.HOST)
.description(HOSTNAME)
.build())
.credentials(LoginCredentials.builder()
.user(System.getProperty("user.name"))
.privateKey(privateKey)
.build())
.build();
}
};
}
static String readRsaIdentity() {
String privateKey;
try {
File keyFile = new File(System.getProperty("user.home") + "/.ssh/id_rsa");
privateKey = Files.toString(keyFile, Charsets.UTF_8);
} catch (IOException e) {
throw Throwables.propagate(e);
}
return privateKey;
}
@Override
protected void configure() {
}
}

View File

@ -24,22 +24,14 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_DEFAU
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_DEFAULT_IMAGE_VERSION;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import org.eclipse.jetty.server.Server;
import org.jclouds.ContextBuilder;
import org.jclouds.byon.BYONApiMetadata;
import org.jclouds.byon.Node;
import org.jclouds.byon.functions.NodeToNodeMetadata;
import org.jclouds.byon.suppliers.SupplyFromProviderURIOrNodesProperty;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ImageExtension;
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
import org.jclouds.compute.config.ComputeServiceAdapterContextModule;
@ -51,11 +43,8 @@ import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
import org.jclouds.domain.Location;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.ssh.SshClient;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.jclouds.virtualbox.Host;
import org.jclouds.virtualbox.compute.VirtualBoxComputeServiceAdapter;
import org.jclouds.virtualbox.compute.VirtualBoxImageExtension;
import org.jclouds.virtualbox.domain.CloneSpec;
@ -86,17 +75,13 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
@ -157,10 +142,6 @@ public class VirtualBoxComputeServiceContextModule extends
bind(new TypeLiteral<Server>() {
}).to((Class) PreseedCfgServer.class).asEagerSingleton();
// for byon
bind(new TypeLiteral<Function<URI, InputStream>>() {
}).to(SupplyFromProviderURIOrNodesProperty.class);
bind(new TypeLiteral<Function<IMachine, SshClient>>() {
}).to(IMachineToSshClient.class);
@ -168,15 +149,6 @@ public class VirtualBoxComputeServiceContextModule extends
bind(LockType.class).toInstance(LockType.Write);
}
@Provides
@Host
@Singleton
protected ComputeServiceContext provideHostController() {
return ContextBuilder.newBuilder(new BYONApiMetadata()).credentials("", "")
.modules(ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule()))
.build(ComputeServiceContext.class);
}
@Provides
@Singleton
protected Function<Supplier<NodeMetadata>, VirtualBoxManager> provideVBox() {
@ -195,12 +167,6 @@ public class VirtualBoxComputeServiceContextModule extends
};
}
@Provides
@Singleton
protected Supplier defaultClient(Supplier<VirtualBoxManager> in) {
return in;
}
@Provides
@Singleton
protected Predicate<SshClient> sshResponds(SshResponds sshResponds, Timeouts timeouts) {
@ -213,19 +179,6 @@ public class VirtualBoxComputeServiceContextModule extends
.osArchMatches(VIRTUALBOX_DEFAULT_IMAGE_ARCH);
}
@Provides
@Singleton
protected Supplier<NodeMetadata> host(Supplier<LoadingCache<String, Node>> nodes, NodeToNodeMetadata converter)
throws ExecutionException {
return Suppliers.compose(Functions.compose(converter, new Function<LoadingCache<String, Node>, Node>() {
@Override
public Node apply(LoadingCache<String, Node> arg0) {
return arg0.apply("host");
}
}), nodes);
}
@Override
protected Optional<ImageExtension> provideImageExtension(Injector i) {
return Optional.of(i.getInstance(ImageExtension.class));

View File

@ -29,12 +29,12 @@ import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.BuildVersion;
import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
@ -74,7 +74,7 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
IMachineToNodeMetadata imachineToNodeMetadata, Predicate<SshClient> sshResponds,
Function<IMachine, SshClient> sshClientForIMachine, MachineUtils machineUtils,
MachineController machineController, @Named(Constants.PROPERTY_BUILD_VERSION) String version,
MachineController machineController, @BuildVersion String version,
@Named(VIRTUALBOX_PRECONFIGURATION_URL) String preconfigurationUrl) {
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.sshResponds = sshResponds;

View File

@ -42,10 +42,10 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.BuildVersion;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.Master;
@ -104,7 +104,7 @@ public class MastersLoadingCache extends AbstractLoadingCache<Image, Master> {
private final String preconfigurationUrl;
@Inject
public MastersLoadingCache(@Named(Constants.PROPERTY_BUILD_VERSION) String version,
public MastersLoadingCache(@BuildVersion String version,
@Named(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) String installationKeySequence,
@Named(VIRTUALBOX_PRECONFIGURATION_URL) String preconfigurationUrl,
@Named(VIRTUALBOX_WORKINGDIR) String workingDir, Function<MasterSpec, IMachine> masterLoader,