| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.jclouds.glesys.compute; |
| 18 | |
| 19 | import static com.google.common.base.Charsets.UTF_8; |
| 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.contains; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static com.google.common.collect.Iterables.find; |
| 25 | import static com.google.common.io.BaseEncoding.base16; |
| 26 | import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue; |
| 27 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 28 | import static org.jclouds.util.Predicates2.retry; |
| 29 | |
| 30 | import java.util.Map; |
| 31 | import java.util.Map.Entry; |
| 32 | import java.util.Set; |
| 33 | import java.util.UUID; |
| 34 | |
| 35 | import javax.annotation.Resource; |
| 36 | import javax.inject.Inject; |
| 37 | import javax.inject.Named; |
| 38 | import javax.inject.Singleton; |
| 39 | |
| 40 | import org.jclouds.Constants; |
| 41 | import org.jclouds.collect.Memoized; |
| 42 | import org.jclouds.compute.ComputeService; |
| 43 | import org.jclouds.compute.ComputeServiceAdapter; |
| 44 | import org.jclouds.compute.domain.Hardware; |
| 45 | import org.jclouds.compute.domain.HardwareBuilder; |
| 46 | import org.jclouds.compute.domain.Processor; |
| 47 | import org.jclouds.compute.domain.Template; |
| 48 | import org.jclouds.compute.domain.Volume; |
| 49 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 50 | import org.jclouds.compute.predicates.ImagePredicates; |
| 51 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 52 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 53 | import org.jclouds.domain.Location; |
| 54 | import org.jclouds.domain.LoginCredentials; |
| 55 | import org.jclouds.glesys.GleSYSApi; |
| 56 | import org.jclouds.glesys.GleSYSAsyncApi; |
| 57 | import org.jclouds.glesys.compute.options.GleSYSTemplateOptions; |
| 58 | import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer; |
| 59 | import org.jclouds.glesys.domain.OSTemplate; |
| 60 | import org.jclouds.glesys.domain.Server; |
| 61 | import org.jclouds.glesys.domain.ServerDetails; |
| 62 | import org.jclouds.glesys.domain.ServerSpec; |
| 63 | import org.jclouds.glesys.options.CreateServerOptions; |
| 64 | import org.jclouds.glesys.options.DestroyServerOptions; |
| 65 | import org.jclouds.location.predicates.LocationPredicates; |
| 66 | import org.jclouds.logging.Logger; |
| 67 | |
| 68 | import com.google.common.base.Function; |
| 69 | import com.google.common.base.Joiner; |
| 70 | import com.google.common.base.Predicate; |
| 71 | import com.google.common.base.Supplier; |
| 72 | import com.google.common.collect.FluentIterable; |
| 73 | import com.google.common.collect.ImmutableList; |
| 74 | import com.google.common.collect.ImmutableSet; |
| 75 | import com.google.common.collect.Iterables; |
| 76 | import com.google.common.util.concurrent.ListenableFuture; |
| 77 | import com.google.common.util.concurrent.ListeningExecutorService; |
| 78 | |
| 79 | /** |
| 80 | * defines the connection between the {@link GleSYSApi} implementation and |
| 81 | * the jclouds {@link ComputeService} |
| 82 | * |
| 83 | */ |
| 84 | @Singleton |
| 85 | public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<ServerDetails, Hardware, OSTemplate, String> { |
| 86 | |
| 87 | @Resource |
| 88 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 89 | protected Logger logger = Logger.NULL; |
| 90 | |
| 91 | private final GleSYSApi api; |
| 92 | private final GleSYSAsyncApi aapi; |
| 93 | private final ListeningExecutorService userExecutor; |
| 94 | private final Timeouts timeouts; |
| 95 | private final Supplier<Set<? extends Location>> locations; |
| 96 | |
| 97 | @Inject |
| 98 | public GleSYSComputeServiceAdapter(GleSYSApi api, GleSYSAsyncApi aapi, |
| 99 | @Named(Constants.PROPERTY_USER_THREADS) ListeningExecutorService userExecutor, Timeouts timeouts, |
| 100 | @Memoized Supplier<Set<? extends Location>> locations) { |
| 101 | this.api = checkNotNull(api, "api"); |
| 102 | this.aapi = checkNotNull(aapi, "aapi"); |
| 103 | this.userExecutor = checkNotNull(userExecutor, "userExecutor"); |
| 104 | this.timeouts = checkNotNull(timeouts, "timeouts"); |
| 105 | this.locations = checkNotNull(locations, "locations"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public NodeAndInitialCredentials<ServerDetails> createNodeWithGroupEncodedIntoName(String group, String name, |
| 110 | Template template) { |
| 111 | checkNotNull(template, "template was null"); |
| 112 | checkNotNull(template.getOptions(), "template options was null"); |
| 113 | checkArgument(template.getOptions().getClass().isAssignableFrom(GleSYSTemplateOptions.class), |
| 114 | "options class %s should have been assignable from GleSYSTemplateOptions", template.getOptions().getClass()); |
| 115 | |
| 116 | GleSYSTemplateOptions templateOptions = template.getOptions().as(GleSYSTemplateOptions.class); |
| 117 | |
| 118 | CreateServerOptions createServerOptions = new CreateServerOptions(); |
| 119 | createServerOptions.ip(templateOptions.getIp()); |
| 120 | |
| 121 | Map<String, String> md = metadataAndTagsAsCommaDelimitedValue(template.getOptions()); |
| 122 | if (md.size() > 0) { |
| 123 | String description = Joiner.on('\n').withKeyValueSeparator("=").join(md); |
| 124 | // TODO: get glesys to stop stripping out equals and commas! |
| 125 | createServerOptions.description(base16().lowerCase().encode(description.getBytes(UTF_8))); |
| 126 | } |
| 127 | ServerSpec.Builder<?> builder = ServerSpec.builder(); |
| 128 | builder.datacenter(template.getLocation().getId()); |
| 129 | builder.templateName(template.getImage().getId()); |
| 130 | builder.platform(template.getHardware().getHypervisor()); |
| 131 | builder.memorySizeMB(template.getHardware().getRam()); |
| 132 | builder.diskSizeGB(Math.round(template.getHardware().getVolumes().get(0).getSize())); |
| 133 | builder.cpuCores((int) template.getHardware().getProcessors().get(0).getCores()); |
| 134 | builder.transferGB(templateOptions.getTransferGB()); |
| 135 | ServerSpec spec = builder.build(); |
| 136 | |
| 137 | |
| 138 | // use random root password unless one was provided via template options |
| 139 | String password = templateOptions.hasRootPassword() ? templateOptions.getRootPassword() : getRandomPassword(); |
| 140 | |
| 141 | logger.debug(">> creating new Server spec(%s) name(%s) options(%s)", spec, name, createServerOptions); |
| 142 | ServerDetails result = api.getServerApi().createWithHostnameAndRootPassword(spec, name, password, |
| 143 | createServerOptions); |
| 144 | logger.trace("<< server(%s)", result.getId()); |
| 145 | |
| 146 | return new NodeAndInitialCredentials<ServerDetails>(result, result.getId() + "", LoginCredentials.builder() |
| 147 | .password(password).build()); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return a generated random password string |
| 152 | */ |
| 153 | private String getRandomPassword() { |
| 154 | return UUID.randomUUID().toString().replace("-",""); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Iterable<Hardware> listHardwareProfiles() { |
| 159 | Set<? extends Location> locationsSet = locations.get(); |
| 160 | ImmutableSet.Builder<Hardware> hardwareToReturn = ImmutableSet.builder(); |
| 161 | |
| 162 | // do this loop after dupes are filtered, else OOM |
| 163 | Set<OSTemplate> images = listImages(); |
| 164 | |
| 165 | for (Entry<String, AllowedArgumentsForCreateServer> platformToArgs : api.getServerApi() |
| 166 | .getAllowedArgumentsForCreateByPlatform().entrySet()) |
| 167 | for (String datacenter : platformToArgs.getValue().getDataCenters()) |
| 168 | for (int diskSizeGB : platformToArgs.getValue().getDiskSizesInGB().getAllowedUnits()) |
| 169 | for (int cpuCores : platformToArgs.getValue().getCpuCoreOptions().getAllowedUnits()) |
| 170 | for (int memorySizeMB : platformToArgs.getValue().getMemorySizesInMB().getAllowedUnits()) { |
| 171 | ImmutableSet.Builder<String> templatesSupportedBuilder = ImmutableSet.builder(); |
| 172 | for (OSTemplate template : images) { |
| 173 | if (template.getPlatform().equals(platformToArgs.getKey()) |
| 174 | && diskSizeGB >= template.getMinDiskSize() && memorySizeMB >= template.getMinMemSize()) |
| 175 | templatesSupportedBuilder.add(template.getName()); |
| 176 | } |
| 177 | ImmutableSet<String> templatesSupported = templatesSupportedBuilder.build(); |
| 178 | if (templatesSupported.size() > 0) |
| 179 | hardwareToReturn.add(new HardwareBuilder() |
| 180 | .ids(String.format( |
| 181 | "datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)", datacenter, |
| 182 | platformToArgs.getKey(), cpuCores, memorySizeMB, diskSizeGB)).ram(memorySizeMB) |
| 183 | .processors(ImmutableList.of(new Processor(cpuCores, 1.0))) |
| 184 | .volumes(ImmutableList.<Volume> of(new VolumeImpl((float) diskSizeGB, true, true))) |
| 185 | .hypervisor(platformToArgs.getKey()) |
| 186 | .location(find(locationsSet, LocationPredicates.idEquals(datacenter))) |
| 187 | .supportsImage(ImagePredicates.idIn(templatesSupported)).build()); |
| 188 | } |
| 189 | |
| 190 | return hardwareToReturn.build(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Set<OSTemplate> listImages() { |
| 195 | return api.getServerApi().listTemplates().toSet(); |
| 196 | } |
| 197 | |
| 198 | // cheat until we have a getTemplate command |
| 199 | @Override |
| 200 | public OSTemplate getImage(final String id) { |
| 201 | return find(listImages(), new Predicate<OSTemplate>(){ |
| 202 | |
| 203 | @Override |
| 204 | public boolean apply(OSTemplate input) { |
| 205 | return input.getName().equals(id); |
| 206 | } |
| 207 | |
| 208 | }, null); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Iterable<ServerDetails> listNodes() { |
| 213 | return transformParallel(api.getServerApi().list(), new Function<Server, ListenableFuture<? extends ServerDetails>>() { |
| 214 | public ListenableFuture<ServerDetails> apply(Server from) { |
| 215 | return aapi.getServerApi().get(from.getId()); |
| 216 | } |
| 217 | }, userExecutor, null, logger, "server details"); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Iterable<ServerDetails> listNodesByIds(final Iterable<String> ids) { |
| 222 | return filter(listNodes(), new Predicate<ServerDetails>() { |
| 223 | |
| 224 | @Override |
| 225 | public boolean apply(ServerDetails server) { |
| 226 | return contains(ids, server.getId()); |
| 227 | } |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Set<String> listLocations() { |
| 233 | return FluentIterable.from(api.getServerApi().getAllowedArgumentsForCreateByPlatform().values()) |
| 234 | .transformAndConcat(new Function<AllowedArgumentsForCreateServer, Set<String>>() { |
| 235 | @Override |
| 236 | public Set<String> apply(AllowedArgumentsForCreateServer arg0) { |
| 237 | return arg0.getDataCenters(); |
| 238 | } |
| 239 | }).toSet(); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public ServerDetails getNode(String id) { |
| 244 | return api.getServerApi().get(id); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void destroyNode(String id) { |
| 249 | retry(new Predicate<String>() { |
| 250 | public boolean apply(String arg0) { |
| 251 | try { |
| 252 | api.getServerApi().destroy(arg0, DestroyServerOptions.Builder.discardIp()); |
| 253 | return true; |
| 254 | } catch (IllegalStateException e) { |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | }, timeouts.nodeTerminated).apply(id); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void rebootNode(String id) { |
| 263 | api.getServerApi().reboot(id); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void resumeNode(String id) { |
| 268 | api.getServerApi().start(id); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void suspendNode(String id) { |
| 273 | api.getServerApi().stop(id); |
| 274 | } |
| 275 | } |