| 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.cloudsigma.compute; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.filter; |
| 23 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.concurrent.ExecutorService; |
| 27 | import java.util.concurrent.Future; |
| 28 | |
| 29 | import javax.annotation.Resource; |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Named; |
| 32 | import javax.inject.Singleton; |
| 33 | |
| 34 | import org.jclouds.Constants; |
| 35 | import org.jclouds.cloudsigma.CloudSigmaClient; |
| 36 | import org.jclouds.cloudsigma.domain.Device; |
| 37 | import org.jclouds.cloudsigma.domain.DriveInfo; |
| 38 | import org.jclouds.cloudsigma.domain.DriveType; |
| 39 | import org.jclouds.cloudsigma.domain.Server; |
| 40 | import org.jclouds.cloudsigma.domain.ServerInfo; |
| 41 | import org.jclouds.cloudsigma.options.CloneDriveOptions; |
| 42 | import org.jclouds.cloudsigma.reference.CloudSigmaConstants; |
| 43 | import org.jclouds.cloudsigma.util.Servers; |
| 44 | import org.jclouds.compute.ComputeService; |
| 45 | import org.jclouds.compute.ComputeServiceAdapter; |
| 46 | import org.jclouds.compute.domain.Hardware; |
| 47 | import org.jclouds.compute.domain.HardwareBuilder; |
| 48 | import org.jclouds.compute.domain.Image; |
| 49 | import org.jclouds.compute.domain.Processor; |
| 50 | import org.jclouds.compute.domain.Template; |
| 51 | import org.jclouds.compute.domain.Volume; |
| 52 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 53 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 54 | import org.jclouds.domain.Credentials; |
| 55 | import org.jclouds.domain.Location; |
| 56 | import org.jclouds.location.suppliers.JustProvider; |
| 57 | import org.jclouds.logging.Logger; |
| 58 | |
| 59 | import com.google.common.base.Function; |
| 60 | import com.google.common.base.Predicate; |
| 61 | import com.google.common.base.Predicates; |
| 62 | import com.google.common.cache.Cache; |
| 63 | import com.google.common.collect.ImmutableList; |
| 64 | import com.google.common.collect.ImmutableSet; |
| 65 | import com.google.common.collect.ImmutableSet.Builder; |
| 66 | import com.google.common.util.concurrent.Futures; |
| 67 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 68 | |
| 69 | /** |
| 70 | * defines the connection between the {@link CloudSigmaClient} implementation |
| 71 | * and the jclouds {@link ComputeService} |
| 72 | * |
| 73 | */ |
| 74 | @Singleton |
| 75 | public class CloudSigmaComputeServiceAdapter implements |
| 76 | ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> { |
| 77 | private static final Predicate<DriveInfo> PREINSTALLED_DISK = Predicates.and(Predicates.notNull(), |
| 78 | new Predicate<DriveInfo>() { |
| 79 | |
| 80 | @Override |
| 81 | public boolean apply(DriveInfo drive) { |
| 82 | return drive.getType().equals(DriveType.DISK) && drive.getDriveType().contains("preinstalled"); |
| 83 | } |
| 84 | |
| 85 | }); |
| 86 | private final CloudSigmaClient client; |
| 87 | private final Predicate<DriveInfo> driveNotClaimed; |
| 88 | private final JustProvider locationSupplier; |
| 89 | private final String defaultVncPassword; |
| 90 | private final Cache<String, DriveInfo> cache; |
| 91 | private final ExecutorService executor; |
| 92 | |
| 93 | @Resource |
| 94 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 95 | protected Logger logger = Logger.NULL; |
| 96 | |
| 97 | @Inject |
| 98 | public CloudSigmaComputeServiceAdapter(CloudSigmaClient client, Predicate<DriveInfo> driveNotClaimed, |
| 99 | JustProvider locationSupplier, @Named(CloudSigmaConstants.PROPERTY_VNC_PASSWORD) String defaultVncPassword, |
| 100 | Cache<String, DriveInfo> cache, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { |
| 101 | this.client = checkNotNull(client, "client"); |
| 102 | this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed"); |
| 103 | this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); |
| 104 | this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword"); |
| 105 | this.cache = checkNotNull(cache, "cache"); |
| 106 | this.executor = checkNotNull(executor, "executor"); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template, |
| 111 | Map<String, Credentials> credentialStore) { |
| 112 | long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l); |
| 113 | logger.debug(">> imaging boot drive source(%s) bytes(%d)", template.getImage().getId(), bootSize); |
| 114 | DriveInfo drive = client.cloneDrive(template.getImage().getId(), template.getImage().getId(), |
| 115 | new CloneDriveOptions().size(bootSize)); |
| 116 | boolean success = driveNotClaimed.apply(drive); |
| 117 | logger.debug("<< image(%s) complete(%s)", drive.getUuid(), success); |
| 118 | if (!success) { |
| 119 | client.destroyDrive(drive.getUuid()); |
| 120 | throw new IllegalStateException("could not image drive in time!"); |
| 121 | } |
| 122 | |
| 123 | Server toCreate = Servers.small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam()) |
| 124 | .cpu((int) (template.getHardware().getProcessors().get(0).getSpeed())).build(); |
| 125 | |
| 126 | logger.debug(">> creating server"); |
| 127 | ServerInfo from = client.createServer(toCreate); |
| 128 | logger.debug("<< created server(%s)", from.getUuid()); |
| 129 | logger.debug(">> starting server(%s)", from.getUuid()); |
| 130 | client.startServer(from.getUuid()); |
| 131 | // store the credentials so that later functions can use them |
| 132 | credentialStore.put("node#" + from.getUuid(), new Credentials("root", defaultVncPassword)); |
| 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, 4 * 1024, 16 * 1024, 32 * 1024 }) { |
| 141 | final float size = (float) cpu / 100; |
| 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(client.listStandardDrives(), |
| 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 | }, executor, null, logger, "drives"); |
| 188 | return filter(drives, PREINSTALLED_DISK); |
| 189 | } |
| 190 | |
| 191 | @SuppressWarnings("unchecked") |
| 192 | @Override |
| 193 | public Iterable<ServerInfo> listNodes() { |
| 194 | return (Iterable<ServerInfo>) client.listServerInfo(); |
| 195 | } |
| 196 | |
| 197 | @SuppressWarnings("unchecked") |
| 198 | @Override |
| 199 | public Iterable<Location> listLocations() { |
| 200 | return (Iterable<Location>) locationSupplier.get(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public ServerInfo getNode(String id) { |
| 205 | return client.getServerInfo(id); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void destroyNode(String id) { |
| 210 | ServerInfo server = getNode(id); |
| 211 | if (server != null) { |
| 212 | client.stopServer(id); |
| 213 | client.destroyServer(id); |
| 214 | for (Device dev : server.getDevices().values()) |
| 215 | client.destroyDrive(dev.getDriveUuid()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void rebootNode(String id) { |
| 221 | client.resetServer(id); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void resumeNode(String id) { |
| 226 | client.startServer(id); |
| 227 | |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void suspendNode(String id) { |
| 232 | client.stopServer(id); |
| 233 | } |
| 234 | } |