| 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.gogrid.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.*; |
| 22 | |
| 23 | import java.security.SecureRandom; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.compute.domain.Hardware; |
| 32 | import org.jclouds.compute.domain.NodeMetadata; |
| 33 | import org.jclouds.compute.domain.Template; |
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 35 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; |
| 36 | import org.jclouds.domain.Credentials; |
| 37 | import org.jclouds.gogrid.GoGridClient; |
| 38 | import org.jclouds.gogrid.domain.Ip; |
| 39 | import org.jclouds.gogrid.domain.IpType; |
| 40 | import org.jclouds.gogrid.domain.PowerCommand; |
| 41 | import org.jclouds.gogrid.domain.Server; |
| 42 | import org.jclouds.gogrid.options.GetIpListOptions; |
| 43 | import org.jclouds.gogrid.predicates.ServerLatestJobCompleted; |
| 44 | import org.jclouds.logging.Logger; |
| 45 | import org.jclouds.predicates.RetryablePredicate; |
| 46 | |
| 47 | import com.google.common.base.Function; |
| 48 | import com.google.common.base.Throwables; |
| 49 | import com.google.common.collect.Iterables; |
| 50 | |
| 51 | /** |
| 52 | * @author Oleksiy Yarmula |
| 53 | */ |
| 54 | @Singleton |
| 55 | public class FindIpThenCreateNodeInGroup implements CreateNodeWithGroupEncodedIntoName { |
| 56 | @Resource |
| 57 | protected Logger logger = Logger.NULL; |
| 58 | |
| 59 | private final GoGridClient client; |
| 60 | private final Function<Hardware, String> sizeToRam; |
| 61 | private final Function<Server, NodeMetadata> serverToNodeMetadata; |
| 62 | private final RetryablePredicate<Server> serverLatestJobCompleted; |
| 63 | private final RetryablePredicate<Server> serverLatestJobCompletedShort; |
| 64 | private final Map<String, Credentials> credentialStore; |
| 65 | |
| 66 | |
| 67 | @Inject |
| 68 | protected FindIpThenCreateNodeInGroup(GoGridClient client, Map<String, Credentials> credentialStore, |
| 69 | Function<Server, NodeMetadata> serverToNodeMetadata, Function<Hardware, String> sizeToRam, |
| 70 | Timeouts timeouts) { |
| 71 | this.client = checkNotNull(client, "client"); |
| 72 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 73 | this.serverToNodeMetadata = checkNotNull(serverToNodeMetadata, "serverToNodeMetadata"); |
| 74 | this.sizeToRam = checkNotNull(sizeToRam, "sizeToRam"); |
| 75 | this.serverLatestJobCompleted = new RetryablePredicate<Server>( |
| 76 | new ServerLatestJobCompleted(client.getJobServices()), |
| 77 | timeouts.nodeRunning * 9l / 10l); |
| 78 | this.serverLatestJobCompletedShort = new RetryablePredicate<Server>( |
| 79 | new ServerLatestJobCompleted(client.getJobServices()), |
| 80 | timeouts.nodeRunning * 1l / 10l); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { |
| 85 | Server addedServer = null; |
| 86 | boolean notStarted = true; |
| 87 | int numOfRetries = 20; |
| 88 | GetIpListOptions unassignedIps = new GetIpListOptions() |
| 89 | .onlyUnassigned() |
| 90 | .inDatacenter(template.getLocation().getId()) |
| 91 | .onlyWithType(IpType.PUBLIC); |
| 92 | // lock-free consumption of a shared resource: IP address pool |
| 93 | while (notStarted) { // TODO: replace with Predicate-based thread |
| 94 | // collision avoidance for simplicity |
| 95 | Set<Ip> availableIps = client.getIpServices().getIpList(unassignedIps); |
| 96 | if (availableIps.isEmpty()) |
| 97 | throw new RuntimeException("No IPs available on this identity."); |
| 98 | int ipIndex = new SecureRandom().nextInt(availableIps.size()); |
| 99 | Ip availableIp = Iterables.get(availableIps, ipIndex); |
| 100 | try { |
| 101 | addedServer = addServer(name, template, availableIp); |
| 102 | notStarted = false; |
| 103 | } catch (Exception e) { |
| 104 | if (--numOfRetries == 0) |
| 105 | Throwables.propagate(e); |
| 106 | notStarted = true; |
| 107 | } |
| 108 | } |
| 109 | if (template.getOptions().shouldBlockUntilRunning()) { |
| 110 | serverLatestJobCompleted.apply(addedServer); |
| 111 | client.getServerServices().power(addedServer.getName(), PowerCommand.START); |
| 112 | serverLatestJobCompletedShort.apply(addedServer); |
| 113 | addedServer = Iterables.getOnlyElement(client.getServerServices().getServersByName( |
| 114 | addedServer.getName())); |
| 115 | } |
| 116 | Credentials credentials = client.getServerServices().getServerCredentialsList().get(addedServer.getName()); |
| 117 | if (credentials != null) |
| 118 | credentialStore.put("node#" + addedServer.getId(), credentials); |
| 119 | else |
| 120 | logger.warn("couldn't get credentials for server %s", addedServer.getName()); |
| 121 | return serverToNodeMetadata.apply(addedServer); |
| 122 | } |
| 123 | |
| 124 | private Server addServer(String name, Template template, Ip availableIp) { |
| 125 | Server addedServer = client.getServerServices().addServer(name, |
| 126 | checkNotNull(template.getImage().getProviderId()), |
| 127 | sizeToRam.apply(template.getHardware()), availableIp.getIp()); |
| 128 | return addedServer; |
| 129 | } |
| 130 | } |