| 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.elasticstack.compute; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.notNull; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 25 | import static org.jclouds.elasticstack.util.Servers.small; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.concurrent.ExecutorService; |
| 29 | import java.util.concurrent.Future; |
| 30 | |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.Constants; |
| 37 | import org.jclouds.compute.ComputeService; |
| 38 | import org.jclouds.compute.ComputeServiceAdapter; |
| 39 | import org.jclouds.compute.domain.Hardware; |
| 40 | import org.jclouds.compute.domain.HardwareBuilder; |
| 41 | import org.jclouds.compute.domain.Image; |
| 42 | import org.jclouds.compute.domain.Processor; |
| 43 | import org.jclouds.compute.domain.Template; |
| 44 | import org.jclouds.compute.domain.Volume; |
| 45 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 46 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 47 | import org.jclouds.domain.Credentials; |
| 48 | import org.jclouds.domain.Location; |
| 49 | import org.jclouds.elasticstack.ElasticStackClient; |
| 50 | import org.jclouds.elasticstack.domain.Device; |
| 51 | import org.jclouds.elasticstack.domain.Drive; |
| 52 | import org.jclouds.elasticstack.domain.DriveInfo; |
| 53 | import org.jclouds.elasticstack.domain.ImageConversionType; |
| 54 | import org.jclouds.elasticstack.domain.Server; |
| 55 | import org.jclouds.elasticstack.domain.ServerInfo; |
| 56 | import org.jclouds.elasticstack.domain.ServerStatus; |
| 57 | import org.jclouds.elasticstack.domain.WellKnownImage; |
| 58 | import org.jclouds.elasticstack.reference.ElasticStackConstants; |
| 59 | import org.jclouds.location.suppliers.JustProvider; |
| 60 | import org.jclouds.logging.Logger; |
| 61 | |
| 62 | import com.google.common.base.Function; |
| 63 | import com.google.common.base.Predicate; |
| 64 | import com.google.common.cache.Cache; |
| 65 | import com.google.common.collect.ImmutableList; |
| 66 | import com.google.common.collect.ImmutableSet; |
| 67 | import com.google.common.collect.ImmutableSet.Builder; |
| 68 | import com.google.common.util.concurrent.Futures; |
| 69 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 70 | |
| 71 | /** |
| 72 | * defines the connection between the {@link ElasticStackClient} implementation |
| 73 | * and the jclouds {@link ComputeService} |
| 74 | * |
| 75 | */ |
| 76 | @Singleton |
| 77 | public class ElasticStackComputeServiceAdapter implements |
| 78 | ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> { |
| 79 | private final ElasticStackClient client; |
| 80 | private final Predicate<DriveInfo> driveNotClaimed; |
| 81 | private final Map<String, WellKnownImage> preinstalledImages; |
| 82 | private final Cache<String, DriveInfo> cache; |
| 83 | private final JustProvider locationSupplier; |
| 84 | private final String defaultVncPassword; |
| 85 | private final ExecutorService executor; |
| 86 | |
| 87 | @Resource |
| 88 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 89 | protected Logger logger = Logger.NULL; |
| 90 | |
| 91 | @Inject |
| 92 | public ElasticStackComputeServiceAdapter(ElasticStackClient client, Predicate<DriveInfo> driveNotClaimed, |
| 93 | JustProvider locationSupplier, Map<String, WellKnownImage> preinstalledImages, Cache<String, DriveInfo> cache, |
| 94 | @Named(ElasticStackConstants.PROPERTY_VNC_PASSWORD) String defaultVncPassword, |
| 95 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { |
| 96 | this.client = checkNotNull(client, "client"); |
| 97 | this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed"); |
| 98 | this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); |
| 99 | this.preinstalledImages = checkNotNull(preinstalledImages, "preinstalledImages"); |
| 100 | this.cache = checkNotNull(cache, "cache"); |
| 101 | this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword"); |
| 102 | this.executor = checkNotNull(executor, "executor"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template, |
| 107 | Map<String, Credentials> credentialStore) { |
| 108 | long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l); |
| 109 | |
| 110 | logger.debug(">> creating boot drive bytes(%d)", bootSize); |
| 111 | DriveInfo drive = client |
| 112 | .createDrive(new Drive.Builder().name(template.getImage().getId()).size(bootSize).build()); |
| 113 | logger.debug("<< drive(%s)", drive.getUuid()); |
| 114 | |
| 115 | logger.debug(">> imaging boot drive source(%s)", template.getImage().getId()); |
| 116 | client.imageDrive(template.getImage().getId(), drive.getUuid(), ImageConversionType.GUNZIP); |
| 117 | boolean success = driveNotClaimed.apply(drive); |
| 118 | logger.debug("<< imaged (%s)", success); |
| 119 | if (!success) { |
| 120 | client.destroyDrive(drive.getUuid()); |
| 121 | throw new IllegalStateException("could not image drive in time!"); |
| 122 | } |
| 123 | |
| 124 | Server toCreate = small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam()) |
| 125 | .cpu((int) (template.getHardware().getProcessors().get(0).getSpeed())).build(); |
| 126 | |
| 127 | ServerInfo from = client.createServer(toCreate); |
| 128 | client.startServer(from.getUuid()); |
| 129 | from = client.getServerInfo(from.getUuid()); |
| 130 | // store the credentials so that later functions can use them |
| 131 | credentialStore.put("node#" + from.getUuid(), new Credentials( |
| 132 | template.getImage().getDefaultCredentials().identity, from.getVnc().getPassword())); |
| 133 | return from; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Iterable<Hardware> listHardwareProfiles() { |
| 138 | Builder<Hardware> hardware = ImmutableSet.<Hardware> builder(); |
| 139 | for (double cpu : new double[] { 1000, 5000, 10000, 20000 }) |
| 140 | for (int ram : new int[] { 512, 1024, 2048, 4096, 8192 }) { |
| 141 | final float size = (float) cpu / 1000; |
| 142 | String id = String.format("cpu=%f,ram=%s,disk=%f", cpu, ram, size); |
| 143 | hardware.add(new HardwareBuilder().supportsImage(new Predicate<Image>() { |
| 144 | |
| 145 | @Override |
| 146 | public boolean apply(Image input) { |
| 147 | String toParse = input.getUserMetadata().get("size"); |
| 148 | return (toParse != null && new Float(toParse) <= size); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return "sizeLessThanOrEqual(" + size + ")"; |
| 154 | } |
| 155 | |
| 156 | }).ids(id).ram(ram).processors(ImmutableList.of(new Processor(1, cpu))) |
| 157 | .volumes(ImmutableList.<Volume> of(new VolumeImpl(size, true, true))).build()); |
| 158 | } |
| 159 | return hardware.build(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * look up the current standard images and do not error out, if they are not |
| 164 | * found. |
| 165 | */ |
| 166 | @Override |
| 167 | public Iterable<DriveInfo> listImages() { |
| 168 | Iterable<DriveInfo> drives = transformParallel(preinstalledImages.keySet(), |
| 169 | new Function<String, Future<DriveInfo>>() { |
| 170 | |
| 171 | @Override |
| 172 | public Future<DriveInfo> apply(String input) { |
| 173 | try { |
| 174 | return Futures.immediateFuture(cache.getUnchecked(input)); |
| 175 | } catch (NullPointerException e) { |
| 176 | logger.debug("drive %s not found", input); |
| 177 | } catch (UncheckedExecutionException e) { |
| 178 | logger.warn(e, "error finding drive %s: %s", input, e.getMessage()); |
| 179 | } |
| 180 | return Futures.immediateFuture(null); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String toString() { |
| 185 | return "seedDriveCache()"; |
| 186 | } |
| 187 | |
| 188 | }, executor, null, logger, "drives"); |
| 189 | return filter(drives, notNull()); |
| 190 | } |
| 191 | |
| 192 | @SuppressWarnings("unchecked") |
| 193 | @Override |
| 194 | public Iterable<ServerInfo> listNodes() { |
| 195 | return (Iterable<ServerInfo>) client.listServerInfo(); |
| 196 | } |
| 197 | |
| 198 | @SuppressWarnings("unchecked") |
| 199 | @Override |
| 200 | public Iterable<Location> listLocations() { |
| 201 | return (Iterable<Location>) locationSupplier.get(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public ServerInfo getNode(String id) { |
| 206 | return client.getServerInfo(id); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void destroyNode(String id) { |
| 211 | ServerInfo server = getNode(id); |
| 212 | if (server != null) { |
| 213 | if (server.getStatus() != ServerStatus.STOPPED) |
| 214 | client.stopServer(id); |
| 215 | client.destroyServer(id); |
| 216 | for (Device dev : server.getDevices().values()) |
| 217 | client.destroyDrive(dev.getDriveUuid()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void rebootNode(String id) { |
| 223 | client.resetServer(id); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void resumeNode(String id) { |
| 228 | client.startServer(id); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void suspendNode(String id) { |
| 233 | client.stopServer(id); |
| 234 | } |
| 235 | } |