From d99c18140eaecac612f53b399d1e5636df21c37f Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 7 May 2012 11:51:29 -0700 Subject: [PATCH] cleanup on vbox --- .../virtualbox/VirtualBoxApiMetadata.java | 4 +- .../config/DefaultCacheNodeStoreModule.java | 36 ----- ...rdcodeLocalhostAsNodeMetadataSupplier.java | 136 ++++++++++++++++++ ...VirtualBoxComputeServiceContextModule.java | 47 ------ .../functions/CreateAndInstallVm.java | 4 +- .../functions/MastersLoadingCache.java | 4 +- 6 files changed, 142 insertions(+), 89 deletions(-) delete mode 100644 labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/DefaultCacheNodeStoreModule.java create mode 100644 labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/HardcodeLocalhostAsNodeMetadataSupplier.java diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxApiMetadata.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxApiMetadata.java index 00d3069fda..c3fc23ef86 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxApiMetadata.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxApiMetadata.java @@ -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.>of(DefaultCacheNodeStoreModule.class, VirtualBoxComputeServiceContextModule.class)); + .defaultModules(ImmutableSet.>of(HardcodeLocalhostAsNodeMetadataSupplier.class, VirtualBoxComputeServiceContextModule.class)); } @Override diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/DefaultCacheNodeStoreModule.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/DefaultCacheNodeStoreModule.java deleted file mode 100644 index 14b2165d6a..0000000000 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/DefaultCacheNodeStoreModule.java +++ /dev/null @@ -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())); - } -} diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/HardcodeLocalhostAsNodeMetadataSupplier.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/HardcodeLocalhostAsNodeMetadataSupplier.java new file mode 100644 index 0000000000..eb8bf949f3 --- /dev/null +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/HardcodeLocalhostAsNodeMetadataSupplier.java @@ -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} 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} as {@code host} and + * {@link RunScriptOnNode#Factory} as factory, to start making commands like the following: + * + *
+ * 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 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 kill = factory.submit(host.get(), 
+ *          call("killsession"), runAsRoot(false).wrapInInitScript(false));
+ * 
+ * ...
+ * 
+ * 
+ * + *

Note

+ * + * 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 lazySupplyHostAsNodeMetadata() { + return new Supplier() { + + @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() { + + } +} \ No newline at end of file diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java index 399813766a..0ac7cebc6d 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java @@ -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() { }).to((Class) PreseedCfgServer.class).asEagerSingleton(); - // for byon - bind(new TypeLiteral>() { - }).to(SupplyFromProviderURIOrNodesProperty.class); - bind(new TypeLiteral>() { }).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. of(new SLF4JLoggingModule(), new SshjSshClientModule())) - .build(ComputeServiceContext.class); - } - @Provides @Singleton protected Function, VirtualBoxManager> provideVBox() { @@ -195,12 +167,6 @@ public class VirtualBoxComputeServiceContextModule extends }; } - @Provides - @Singleton - protected Supplier defaultClient(Supplier in) { - return in; - } - @Provides @Singleton protected Predicate sshResponds(SshResponds sshResponds, Timeouts timeouts) { @@ -213,19 +179,6 @@ public class VirtualBoxComputeServiceContextModule extends .osArchMatches(VIRTUALBOX_DEFAULT_IMAGE_ARCH); } - @Provides - @Singleton - protected Supplier host(Supplier> nodes, NodeToNodeMetadata converter) - throws ExecutionException { - return Suppliers.compose(Functions.compose(converter, new Function, Node>() { - - @Override - public Node apply(LoadingCache arg0) { - return arg0.apply("host"); - } - }), nodes); - } - @Override protected Optional provideImageExtension(Injector i) { return Optional.of(i.getInstance(ImageExtension.class)); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java index a848671835..b359cd47ff 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java @@ -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 { CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists, IMachineToNodeMetadata imachineToNodeMetadata, Predicate sshResponds, Function 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; diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersLoadingCache.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersLoadingCache.java index e54c01101e..42f95069d0 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersLoadingCache.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersLoadingCache.java @@ -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 { 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 masterLoader,