1 /**
2 * Copyright 2004-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.common.aws.ec2.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.common.aws.ec2.model.EC2ServiceContext;
22 import org.kuali.common.aws.ec2.model.LaunchInstanceContext;
23 import org.kuali.common.aws.ec2.model.RootVolume;
24 import org.kuali.common.util.Str;
25 import org.kuali.common.util.nullify.NullUtils;
26 import org.kuali.common.util.spring.SpringUtils;
27 import org.kuali.common.util.spring.env.EnvironmentService;
28 import org.springframework.util.Assert;
29
30 import com.amazonaws.regions.RegionUtils;
31 import com.amazonaws.services.ec2.AmazonEC2Client;
32 import com.amazonaws.services.ec2.model.InstanceType;
33 import com.amazonaws.services.ec2.model.Tag;
34 import com.google.common.base.Optional;
35 import com.google.common.collect.ImmutableList;
36
37 public class LaunchUtils {
38
39 // private static final String AMI_KEY = "ec2.ami";
40 // private static final String KEY_NAME_KEY = "ec2.keyName";
41 // private static final String KEY_FINGERPRINT_KEY = "ec2.keyFingerprint";
42 // private static final String PUBLIC_KEY_KEY = "ec2.publicKey";
43 // private static final String PRIVATE_KEY_KEY = "ec2.privateKey";
44 private static final String TYPE_KEY = "ec2.type";
45 // TODO Be smarter about allowing overrides for security groups / permissions
46 // private static final String SECURITY_GROUPS_KEY = "ec2.securityGroups";
47 private static final String TAGS_KEY = "ec2.tags";
48 // private static final String AVAILABILITY_ZONE_KEY = "ec2.availabilityZone";
49 // private static final String LAUNCH_TIMEOUT_KEY = "ec2.launchTimeout";
50 // private static final String PREVENT_TERMINATION_KEY = "ec2.preventTermination";
51 // private static final String EBS_OPTIMIZED_KEY = "ec2.ebsOptimized";
52 // private static final String ENABLE_MONITORING_KEY = "ec2.enableMonitoring";
53 private static final String ROOT_VOLUME_SIZE_KEY = "ec2.rootVolume.sizeInGigabytes";
54 private static final String ROOT_VOLUME_DELETE_KEY = "ec2.rootVolume.deleteOnTermination";
55
56 // private static final KeyPair NOKEYPAIR = new KeyPair.Builder(NullUtils.NONE).build();
57
58 // private static final LaunchInstanceContext NOCONTEXT = new LaunchInstanceContext.Builder(NullUtils.NONE, NOKEYPAIR).build();
59
60 protected static AmazonEC2Client newAmazonEC2Client(EC2ServiceContext context) {
61 if (context.getConfiguration().isPresent()) {
62 return new AmazonEC2Client(context.getCredentials(), context.getConfiguration().get());
63 } else {
64 return new AmazonEC2Client(context.getCredentials());
65 }
66 }
67
68 public static AmazonEC2Client getClient(EC2ServiceContext context) {
69 AmazonEC2Client client = newAmazonEC2Client(context);
70 if (context.getTimeOffsetInSeconds().isPresent()) {
71 client.setTimeOffset(context.getTimeOffsetInSeconds().get());
72 }
73 client.setRegion(RegionUtils.getRegion(context.getRegion()));
74 if (context.getEndpoint().isPresent()) {
75 client.setEndpoint(context.getEndpoint().get());
76 }
77 return client;
78 }
79
80 /**
81 * Use the values from <code>provided</code> except where they are overidden by values from the environment.
82 */
83 public static LaunchInstanceContext getContext(EnvironmentService env, LaunchInstanceContext provided) {
84 throw new UnsupportedOperationException("don't call this method");
85 /*
86 * String ami = NullUtils.trimToNull(env.getString(AMI_KEY, provided.getAmi())); KeyPair keyPair = getKeyPair(env, provided.getKeyPair()); InstanceType type = getType(env,
87 * provided.getType()); int timeoutMillis = SpringUtils.getMillisAsInt(env, LAUNCH_TIMEOUT_KEY, provided.getTimeoutMillis()); boolean ebsOptimized =
88 * env.getBoolean(EBS_OPTIMIZED_KEY, provided.isEbsOptimized()); boolean enableMonitoring = env.getBoolean(ENABLE_MONITORING_KEY, provided.isEnableMonitoring()); boolean
89 * preventTermination = env.getBoolean(PREVENT_TERMINATION_KEY, provided.isPreventTermination()); Optional<RootVolume> rootVolume = getRootVolume(env,
90 * provided.getRootVolume()); List<Tag> tags = getTags(env, provided.getTags()); Optional<String> availabilityZone = SpringUtils.getString(env, AVAILABILITY_ZONE_KEY,
91 * provided.getAvailabilityZone());
92 *
93 * // TODO Provide a way to parse security groups and permissions from a specially formatted string? // ec2.securityGroups=ci:Continuous Integration:,ci.master:Jenkins CI
94 * Server - Master:[22|tcp|{0.0.0.0/0}{192.0.0.0/0}][80|tcp|{0.0.0.0/0}] // TODO OR possibly just parse a simple comma delimited string of security group names and then
95 * look up the actual security groups on Amazon? // List<String> securityGroups = SpringUtils.getStrings(env, SECURITY_GROUPS_KEY, provided.getSecurityGroups());
96 * List<KualiSecurityGroup> securityGroups = provided.getSecurityGroups();
97 */
98
99 // return new LaunchInstanceContext.Builder(ami, keyPair).copy(provided).type(type).availabilityZone(availabilityZone.orNull()).tags(tags).securityGroups(securityGroups)
100 // .preventTermination(preventTermination).rootVolume(rootVolume.orNull()).timeoutMillis(timeoutMillis).ebsOptimized(ebsOptimized).enableMonitoring(enableMonitoring)
101 // .build();
102 }
103
104 protected static Optional<RootVolume> getRootVolume(EnvironmentService env, Optional<RootVolume> provided) {
105 throw new UnsupportedOperationException("don't call this method");
106 /*
107 * Optional<Integer> sizeInGigabytes = getSizeInGigaBytes(env, provided); Optional<Boolean> deleteOnTermination = getDeleteOnTermination(env, provided); if
108 * (deleteOnTermination.isPresent() || sizeInGigabytes.isPresent()) { return Optional.of(new RootVolume.Builder(sizeInGigabytes, deleteOnTermination).build()); } else {
109 * return Optional.absent(); }
110 */
111 }
112
113 protected static Optional<Boolean> getDeleteOnTermination(EnvironmentService env, Optional<RootVolume> provided) {
114 if (env.containsProperty(ROOT_VOLUME_DELETE_KEY)) {
115 return SpringUtils.getOptionalBoolean(env, ROOT_VOLUME_SIZE_KEY);
116 } else {
117 return provided.isPresent() ? provided.get().getDeleteOnTermination() : Optional.<Boolean> absent();
118 }
119 }
120
121 protected static Optional<Integer> getSizeInGigaBytes(EnvironmentService env, Optional<RootVolume> provided) {
122 if (env.containsProperty(ROOT_VOLUME_SIZE_KEY)) {
123 return SpringUtils.getOptionalInteger(env, ROOT_VOLUME_SIZE_KEY);
124 } else {
125 return provided.isPresent() ? provided.get().getSizeInGigabytes() : Optional.<Integer> absent();
126 }
127 }
128
129 protected static List<Tag> getTags(EnvironmentService env, List<Tag> provided) {
130 if (env.containsProperty(TAGS_KEY)) {
131 return getTags(env);
132 } else {
133 return provided;
134 }
135 }
136
137 protected static List<Tag> getTags(EnvironmentService env) {
138 List<String> list = SpringUtils.getNoneSensitiveListFromCSV(env, TAGS_KEY, NullUtils.NONE);
139 List<Tag> tags = new ArrayList<Tag>();
140 for (String element : list) {
141 String[] tokens = Str.splitAndTrim(element, "=");
142 Assert.isTrue(tokens.length == 2, "Expected exactly 2 tokens");
143 String key = tokens[0];
144 String value = tokens[1];
145 Tag tag = new Tag(key, value);
146 tags.add(tag);
147 }
148 return ImmutableList.copyOf(tags);
149 }
150
151 protected static InstanceType getType(EnvironmentService env, InstanceType type) {
152 return InstanceType.fromValue(env.getString(TYPE_KEY, type.toString()));
153 }
154
155 }