| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.compute.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 23 | import static org.jclouds.compute.domain.OsFamily.UBUNTU; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.Callable; |
| 28 | import java.util.concurrent.atomic.AtomicReference; |
| 29 | |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 35 | import org.jclouds.compute.callables.RunScriptOnNodeAsInitScriptUsingSsh; |
| 36 | import org.jclouds.compute.callables.RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete; |
| 37 | import org.jclouds.compute.callables.RunScriptOnNodeUsingSsh; |
| 38 | import org.jclouds.compute.domain.ComputeMetadata; |
| 39 | import org.jclouds.compute.domain.Hardware; |
| 40 | import org.jclouds.compute.domain.Image; |
| 41 | import org.jclouds.compute.domain.NodeMetadata; |
| 42 | import org.jclouds.compute.domain.OsFamily; |
| 43 | import org.jclouds.compute.domain.TemplateBuilder; |
| 44 | import org.jclouds.compute.functions.CreateSshClientOncePortIsListeningOnNode; |
| 45 | import org.jclouds.compute.functions.TemplateOptionsToStatement; |
| 46 | import org.jclouds.compute.options.RunScriptOptions; |
| 47 | import org.jclouds.compute.options.TemplateOptions; |
| 48 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 49 | import org.jclouds.compute.strategy.CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap; |
| 50 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 51 | import org.jclouds.json.Json; |
| 52 | import org.jclouds.location.config.LocationModule; |
| 53 | import org.jclouds.rest.AuthorizationException; |
| 54 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 55 | import org.jclouds.scriptbuilder.domain.Statement; |
| 56 | import org.jclouds.scriptbuilder.domain.Statements; |
| 57 | import org.jclouds.ssh.SshClient; |
| 58 | |
| 59 | import com.google.common.base.Function; |
| 60 | import com.google.common.base.Supplier; |
| 61 | import com.google.common.base.Suppliers; |
| 62 | import com.google.common.collect.Maps; |
| 63 | import com.google.inject.AbstractModule; |
| 64 | import com.google.inject.Inject; |
| 65 | import com.google.inject.Injector; |
| 66 | import com.google.inject.Provides; |
| 67 | import com.google.inject.TypeLiteral; |
| 68 | import com.google.inject.assistedinject.FactoryModuleBuilder; |
| 69 | import com.google.inject.name.Names; |
| 70 | |
| 71 | /** |
| 72 | * |
| 73 | * @author Adrian Cole |
| 74 | */ |
| 75 | public abstract class BaseComputeServiceContextModule extends AbstractModule { |
| 76 | |
| 77 | @Override |
| 78 | protected void configure() { |
| 79 | install(new LocationModule(authException)); |
| 80 | install(new ComputeServiceTimeoutsModule()); |
| 81 | bind(new TypeLiteral<Function<NodeMetadata, SshClient>>() { |
| 82 | }).to(CreateSshClientOncePortIsListeningOnNode.class); |
| 83 | bind(new TypeLiteral<Function<TemplateOptions, Statement>>() { |
| 84 | }).to(TemplateOptionsToStatement.class); |
| 85 | |
| 86 | install(new FactoryModuleBuilder() |
| 87 | .implement(RunScriptOnNode.class, Names.named("direct"), RunScriptOnNodeUsingSsh.class) |
| 88 | .implement(RunScriptOnNode.class, Names.named("blocking"), |
| 89 | RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete.class) |
| 90 | .implement(RunScriptOnNode.class, Names.named("nonblocking"), RunScriptOnNodeAsInitScriptUsingSsh.class) |
| 91 | .build(RunScriptOnNodeFactoryImpl.Factory.class)); |
| 92 | |
| 93 | install(new PersistNodeCredentialsModule()); |
| 94 | |
| 95 | bind(RunScriptOnNode.Factory.class).to(RunScriptOnNodeFactoryImpl.class); |
| 96 | |
| 97 | install(new FactoryModuleBuilder().implement(new TypeLiteral<Callable<Void>>() { |
| 98 | }, CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.class) |
| 99 | .implement(new TypeLiteral<Function<NodeMetadata, Void>>() { |
| 100 | }, CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.class) |
| 101 | .build(CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.Factory.class)); |
| 102 | |
| 103 | install(new FactoryModuleBuilder().implement(new TypeLiteral<Callable<RunScriptOnNode>>() { |
| 104 | }, InitializeRunScriptOnNodeOrPlaceInBadMap.class).build(InitializeRunScriptOnNodeOrPlaceInBadMap.Factory.class)); |
| 105 | } |
| 106 | |
| 107 | @Singleton |
| 108 | static class RunScriptOnNodeFactoryImpl implements RunScriptOnNode.Factory { |
| 109 | |
| 110 | static interface Factory { |
| 111 | |
| 112 | @Named("direct") |
| 113 | RunScriptOnNode exec(NodeMetadata node, Statement script, RunScriptOptions options); |
| 114 | |
| 115 | @Named("blocking") |
| 116 | RunScriptOnNode backgroundAndBlockOnComplete(NodeMetadata node, Statement script, RunScriptOptions options); |
| 117 | |
| 118 | @Named("nonblocking") |
| 119 | RunScriptOnNode background(NodeMetadata node, Statement script, RunScriptOptions options); |
| 120 | } |
| 121 | |
| 122 | private final Factory factory; |
| 123 | |
| 124 | @Inject |
| 125 | RunScriptOnNodeFactoryImpl(Factory factory) { |
| 126 | this.factory = checkNotNull(factory, "factory"); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public RunScriptOnNode create(NodeMetadata node, Statement runScript, RunScriptOptions options) { |
| 131 | checkNotNull(node, "node"); |
| 132 | checkNotNull(runScript, "runScript"); |
| 133 | checkNotNull(options, "options"); |
| 134 | return !options.shouldWrapInInitScript() ? factory.exec(node, runScript, options) : (options |
| 135 | .shouldBlockOnComplete() ? factory.backgroundAndBlockOnComplete(node, runScript, options) : factory |
| 136 | .background(node, runScript, options)); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public RunScriptOnNode create(NodeMetadata node, String script) { |
| 141 | return create(node, Statements.exec(checkNotNull(script, "script"))); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public RunScriptOnNode create(NodeMetadata node, Statement script) { |
| 146 | return create(node, script, RunScriptOptions.NONE); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Provides |
| 151 | @Singleton |
| 152 | public Map<OsFamily, Map<String, String>> provideOsVersionMap(ComputeServiceConstants.ReferenceData data, Json json) { |
| 153 | return json.fromJson(data.osVersionMapJson, new TypeLiteral<Map<OsFamily, Map<String, String>>>() { |
| 154 | }.getType()); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * The default template if none is provided. |
| 159 | */ |
| 160 | @Provides |
| 161 | @Named("DEFAULT") |
| 162 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 163 | return template.osFamily(UBUNTU).osVersionMatches("1[10].[10][04]").os64Bit(true); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * The default options if none are provided. |
| 168 | */ |
| 169 | @Provides |
| 170 | @Named("DEFAULT") |
| 171 | protected TemplateOptions provideTemplateOptions(Injector injector, TemplateOptions options) { |
| 172 | return options; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * supplies how the tag is encoded into the name. A string of hex characters |
| 177 | * is the last argument and tag is the first |
| 178 | */ |
| 179 | @Provides |
| 180 | @Named("NAMING_CONVENTION") |
| 181 | @Singleton |
| 182 | protected String provideNamingConvention() { |
| 183 | return "%s-%s"; |
| 184 | } |
| 185 | |
| 186 | protected AtomicReference<AuthorizationException> authException = new AtomicReference<AuthorizationException>(); |
| 187 | |
| 188 | @Provides |
| 189 | @Singleton |
| 190 | protected Supplier<Map<String, ? extends Image>> provideImageMap(@Memoized Supplier<Set<? extends Image>> images) { |
| 191 | return Suppliers.compose(new Function<Set<? extends Image>, Map<String, ? extends Image>>() { |
| 192 | |
| 193 | @Override |
| 194 | public Map<String, ? extends Image> apply(Set<? extends Image> from) { |
| 195 | return Maps.uniqueIndex(from, new Function<Image, String>() { |
| 196 | |
| 197 | @Override |
| 198 | public String apply(Image from) { |
| 199 | return from.getId(); |
| 200 | } |
| 201 | |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | }, images); |
| 206 | } |
| 207 | |
| 208 | @Provides |
| 209 | @Singleton |
| 210 | @Memoized |
| 211 | protected Supplier<Set<? extends Image>> supplyImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 212 | final Supplier<Set<? extends Image>> imageSupplier) { |
| 213 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Image>>(authException, |
| 214 | seconds, new Supplier<Set<? extends Image>>() { |
| 215 | @Override |
| 216 | public Set<? extends Image> get() { |
| 217 | return imageSupplier.get(); |
| 218 | } |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | @Provides |
| 223 | @Singleton |
| 224 | protected Supplier<Map<String, ? extends Hardware>> provideSizeMap(@Memoized Supplier<Set<? extends Hardware>> sizes) { |
| 225 | return Suppliers.compose(new Function<Set<? extends Hardware>, Map<String, ? extends Hardware>>() { |
| 226 | |
| 227 | @Override |
| 228 | public Map<String, ? extends Hardware> apply(Set<? extends Hardware> from) { |
| 229 | return Maps.uniqueIndex(from, new Function<Hardware, String>() { |
| 230 | |
| 231 | @Override |
| 232 | public String apply(Hardware from) { |
| 233 | return from.getId(); |
| 234 | } |
| 235 | |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | }, sizes); |
| 240 | } |
| 241 | |
| 242 | @Provides |
| 243 | @Singleton |
| 244 | @Memoized |
| 245 | protected Supplier<Set<? extends Hardware>> supplySizeCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 246 | final Supplier<Set<? extends Hardware>> hardwareSupplier) { |
| 247 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Hardware>>(authException, |
| 248 | seconds, new Supplier<Set<? extends Hardware>>() { |
| 249 | @Override |
| 250 | public Set<? extends Hardware> get() { |
| 251 | return hardwareSupplier.get(); |
| 252 | } |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | @Provides |
| 257 | @Singleton |
| 258 | protected Function<ComputeMetadata, String> indexer() { |
| 259 | return new Function<ComputeMetadata, String>() { |
| 260 | @Override |
| 261 | public String apply(ComputeMetadata from) { |
| 262 | return from.getProviderId(); |
| 263 | } |
| 264 | }; |
| 265 | } |
| 266 | |
| 267 | } |