| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 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.BlockUntilInitScriptStatusIsZeroThenReturnOutput; |
| 35 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 36 | import org.jclouds.compute.callables.RunScriptOnNodeAsInitScriptUsingSsh; |
| 37 | import org.jclouds.compute.callables.RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete; |
| 38 | import org.jclouds.compute.callables.RunScriptOnNodeUsingSsh; |
| 39 | import org.jclouds.compute.domain.ComputeMetadata; |
| 40 | import org.jclouds.compute.domain.Hardware; |
| 41 | import org.jclouds.compute.domain.Image; |
| 42 | import org.jclouds.compute.domain.NodeMetadata; |
| 43 | import org.jclouds.compute.domain.OsFamily; |
| 44 | import org.jclouds.compute.domain.TemplateBuilder; |
| 45 | import org.jclouds.compute.functions.CreateSshClientOncePortIsListeningOnNode; |
| 46 | import org.jclouds.compute.functions.TemplateOptionsToStatement; |
| 47 | import org.jclouds.compute.options.RunScriptOptions; |
| 48 | import org.jclouds.compute.options.TemplateOptions; |
| 49 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 50 | import org.jclouds.compute.strategy.CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap; |
| 51 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 52 | import org.jclouds.json.Json; |
| 53 | import org.jclouds.location.config.LocationModule; |
| 54 | import org.jclouds.rest.AuthorizationException; |
| 55 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 56 | import org.jclouds.scriptbuilder.domain.Statement; |
| 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().implement(RunScriptOnNodeUsingSsh.class, Names.named("direct"), |
| 87 | RunScriptOnNodeUsingSsh.class).implement(RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete.class, |
| 88 | Names.named("blocking"), RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete.class).implement( |
| 89 | RunScriptOnNodeAsInitScriptUsingSsh.class, Names.named("nonblocking"), |
| 90 | RunScriptOnNodeAsInitScriptUsingSsh.class).build(RunScriptOnNodeFactoryImpl.Factory.class)); |
| 91 | |
| 92 | install(new PersistNodeCredentialsModule()); |
| 93 | |
| 94 | bind(RunScriptOnNode.Factory.class).to(RunScriptOnNodeFactoryImpl.class); |
| 95 | |
| 96 | install(new FactoryModuleBuilder().implement(new TypeLiteral<Callable<Void>>() { |
| 97 | }, CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.class).implement( |
| 98 | new TypeLiteral<Function<NodeMetadata, Void>>() { |
| 99 | }, CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.class).build( |
| 100 | CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.Factory.class)); |
| 101 | |
| 102 | install(new FactoryModuleBuilder().implement(new TypeLiteral<Callable<RunScriptOnNode>>() { |
| 103 | }, InitializeRunScriptOnNodeOrPlaceInBadMap.class).build(InitializeRunScriptOnNodeOrPlaceInBadMap.Factory.class)); |
| 104 | |
| 105 | install(new FactoryModuleBuilder().build(BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory.class)); |
| 106 | } |
| 107 | |
| 108 | @Singleton |
| 109 | static class RunScriptOnNodeFactoryImpl implements RunScriptOnNode.Factory { |
| 110 | |
| 111 | static interface Factory { |
| 112 | |
| 113 | @Named("direct") |
| 114 | RunScriptOnNodeUsingSsh exec(NodeMetadata node, Statement script, RunScriptOptions options); |
| 115 | |
| 116 | @Named("blocking") |
| 117 | RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete backgroundAndBlockOnComplete(NodeMetadata node, |
| 118 | Statement script, RunScriptOptions options); |
| 119 | |
| 120 | @Named("nonblocking") |
| 121 | RunScriptOnNodeAsInitScriptUsingSsh background(NodeMetadata node, Statement script, RunScriptOptions options); |
| 122 | } |
| 123 | |
| 124 | private final Factory factory; |
| 125 | |
| 126 | @Inject |
| 127 | RunScriptOnNodeFactoryImpl(Factory factory) { |
| 128 | this.factory = checkNotNull(factory, "factory"); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public RunScriptOnNode create(NodeMetadata node, Statement runScript, RunScriptOptions options) { |
| 133 | checkNotNull(node, "node"); |
| 134 | checkNotNull(runScript, "runScript"); |
| 135 | checkNotNull(options, "options"); |
| 136 | return !options.shouldWrapInInitScript() ? factory.exec(node, runScript, options) : (options |
| 137 | .shouldBlockOnComplete() ? factory.backgroundAndBlockOnComplete(node, runScript, options) : factory |
| 138 | .background(node, runScript, options)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public BlockUntilInitScriptStatusIsZeroThenReturnOutput submit(NodeMetadata node, Statement script, |
| 143 | RunScriptOptions options) { |
| 144 | checkNotNull(node, "node"); |
| 145 | checkNotNull(script, "script"); |
| 146 | checkNotNull(options, "options"); |
| 147 | options.shouldWrapInInitScript(); |
| 148 | return factory.backgroundAndBlockOnComplete(node, script, options).init().future(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Provides |
| 153 | @Singleton |
| 154 | public Map<OsFamily, Map<String, String>> provideOsVersionMap(ComputeServiceConstants.ReferenceData data, Json json) { |
| 155 | return json.fromJson(data.osVersionMapJson, new TypeLiteral<Map<OsFamily, Map<String, String>>>() { |
| 156 | }.getType()); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * The default template if none is provided. |
| 161 | */ |
| 162 | @Provides |
| 163 | @Named("DEFAULT") |
| 164 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 165 | return template.osFamily(UBUNTU).osVersionMatches("1[012].[01][04]").os64Bit(true); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * The default options if none are provided. |
| 170 | */ |
| 171 | @Provides |
| 172 | @Named("DEFAULT") |
| 173 | protected TemplateOptions provideTemplateOptions(Injector injector, TemplateOptions options) { |
| 174 | return options; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * supplies how the tag is encoded into the name. A string of hex characters is the last argument |
| 179 | * and tag is the first |
| 180 | */ |
| 181 | @Provides |
| 182 | @Named("NAMING_CONVENTION") |
| 183 | @Singleton |
| 184 | protected String provideNamingConvention() { |
| 185 | return "%s-%s"; |
| 186 | } |
| 187 | |
| 188 | protected AtomicReference<AuthorizationException> authException = new AtomicReference<AuthorizationException>(); |
| 189 | |
| 190 | @Provides |
| 191 | @Singleton |
| 192 | protected Supplier<Map<String, ? extends Image>> provideImageMap(@Memoized Supplier<Set<? extends Image>> images) { |
| 193 | return Suppliers.compose(new Function<Set<? extends Image>, Map<String, ? extends Image>>() { |
| 194 | |
| 195 | @Override |
| 196 | public Map<String, ? extends Image> apply(Set<? extends Image> from) { |
| 197 | return Maps.uniqueIndex(from, new Function<Image, String>() { |
| 198 | |
| 199 | @Override |
| 200 | public String apply(Image from) { |
| 201 | return from.getId(); |
| 202 | } |
| 203 | |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | }, images); |
| 208 | } |
| 209 | |
| 210 | @Provides |
| 211 | @Singleton |
| 212 | @Memoized |
| 213 | protected Supplier<Set<? extends Image>> supplyImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 214 | final Supplier<Set<? extends Image>> imageSupplier) { |
| 215 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Image>>(authException, |
| 216 | seconds, new Supplier<Set<? extends Image>>() { |
| 217 | @Override |
| 218 | public Set<? extends Image> get() { |
| 219 | return imageSupplier.get(); |
| 220 | } |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | @Provides |
| 225 | @Singleton |
| 226 | protected Supplier<Map<String, ? extends Hardware>> provideSizeMap(@Memoized Supplier<Set<? extends Hardware>> sizes) { |
| 227 | return Suppliers.compose(new Function<Set<? extends Hardware>, Map<String, ? extends Hardware>>() { |
| 228 | |
| 229 | @Override |
| 230 | public Map<String, ? extends Hardware> apply(Set<? extends Hardware> from) { |
| 231 | return Maps.uniqueIndex(from, new Function<Hardware, String>() { |
| 232 | |
| 233 | @Override |
| 234 | public String apply(Hardware from) { |
| 235 | return from.getId(); |
| 236 | } |
| 237 | |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | }, sizes); |
| 242 | } |
| 243 | |
| 244 | @Provides |
| 245 | @Singleton |
| 246 | @Memoized |
| 247 | protected Supplier<Set<? extends Hardware>> supplySizeCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 248 | final Supplier<Set<? extends Hardware>> hardwareSupplier) { |
| 249 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Hardware>>(authException, |
| 250 | seconds, new Supplier<Set<? extends Hardware>>() { |
| 251 | @Override |
| 252 | public Set<? extends Hardware> get() { |
| 253 | return hardwareSupplier.get(); |
| 254 | } |
| 255 | }); |
| 256 | } |
| 257 | |
| 258 | @Provides |
| 259 | @Singleton |
| 260 | protected Function<ComputeMetadata, String> indexer() { |
| 261 | return new Function<ComputeMetadata, String>() { |
| 262 | @Override |
| 263 | public String apply(ComputeMetadata from) { |
| 264 | return from.getProviderId(); |
| 265 | } |
| 266 | }; |
| 267 | } |
| 268 | |
| 269 | } |