View Javadoc
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;
17  
18  import static com.google.common.base.Optional.absent;
19  import static com.google.common.base.Stopwatch.createStarted;
20  import static java.lang.String.format;
21  import static java.lang.System.currentTimeMillis;
22  import static org.kuali.common.aws.ec2.model.Regions.US_WEST_1;
23  import static org.kuali.common.util.log.Loggers.newLogger;
24  
25  import java.util.List;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  import org.kuali.common.aws.EncryptedAwsCredentials;
30  import org.kuali.common.aws.ec2.impl.DefaultEC2Service;
31  import org.kuali.common.aws.ec2.model.CreateAMIRequest;
32  import org.kuali.common.aws.ec2.model.EC2ServiceContext;
33  import org.kuali.common.aws.ec2.model.ImmutableBlockDeviceMapping;
34  import org.kuali.common.aws.ec2.model.ImmutableTag;
35  import org.kuali.common.aws.ec2.model.RootVolume;
36  import org.kuali.common.aws.status.Auth;
37  import org.kuali.common.util.FormatUtils;
38  import org.kuali.common.util.wait.DefaultWaitService;
39  import org.slf4j.Logger;
40  
41  import com.amazonaws.auth.AWSCredentials;
42  import com.amazonaws.regions.Region;
43  import com.amazonaws.regions.Regions;
44  import com.amazonaws.services.ec2.AmazonEC2Client;
45  import com.amazonaws.services.ec2.model.BlockDeviceMapping;
46  import com.amazonaws.services.ec2.model.CopyImageRequest;
47  import com.amazonaws.services.ec2.model.CopyImageResult;
48  import com.amazonaws.services.ec2.model.EbsBlockDevice;
49  import com.amazonaws.services.ec2.model.Image;
50  import com.amazonaws.services.ec2.model.Instance;
51  import com.google.common.base.Optional;
52  import com.google.common.base.Stopwatch;
53  import com.google.common.collect.Lists;
54  
55  public class DefaultEC2ServiceTest {
56  
57  	private static final Logger logger = newLogger();
58  
59  	@Test
60  	public void testCopyAMI() {
61  		try {
62  			Stopwatch sw = createStarted();
63  			AWSCredentials credentials = Auth.getCredentials(EncryptedAwsCredentials.FOUNDATION);
64  			AmazonEC2Client client = new AmazonEC2Client(credentials);
65  			Region src = Region.getRegion(Regions.US_EAST_1);
66  			String ami = "ami-7dada414";
67  			Region dst = Region.getRegion(Regions.US_WEST_2);
68  			client.setRegion(dst);
69  			CopyImageRequest request = new CopyImageRequest();
70  			request.setSourceRegion(src.getName());
71  			request.setSourceImageId(ami);
72  			logger.info(format("copying [%s] from %s to %s", ami, src, dst));
73  			CopyImageResult result = client.copyImage(request);
74  			String resultAmi = result.getImageId();
75  			logger.info(format("copied [%s] from %s to %s as [%s] - %s", ami, src, dst, resultAmi, FormatUtils.getTime(sw)));
76  		} catch (Exception e) {
77  			e.printStackTrace();
78  		}
79  	}
80  
81  	@Test
82  	@Ignore
83  	public void testCreateAmi() {
84  		try {
85  			DefaultEC2Service service = getUSWestService();
86  			Instance instance = service.getInstance("i-4423721b");
87  			ImmutableTag name = new ImmutableTag("Name", "test.slave.ssd." + (currentTimeMillis() / (1000 * 60)));
88  			RootVolume rootVolume = RootVolume.create(32, true);
89  			int timeoutMillis = FormatUtils.getMillisAsInt("1h");
90  			List<BlockDeviceMapping> additionalMappings = Lists.<BlockDeviceMapping> newArrayList(SSD);
91  			CreateAMIRequest request = CreateAMIRequest.builder().withInstanceId(instance.getInstanceId()).withName(name).withRootVolume(rootVolume)
92  					.withAdditionalMappings(additionalMappings).withTimeoutMillis(timeoutMillis).withDescription("ssd test").build();
93  			service.createAmi(request);
94  		} catch (Throwable e) {
95  			e.printStackTrace();
96  		}
97  	}
98  
99  	@Test
100 	@Ignore
101 	public void testGetAllImages() {
102 		DefaultEC2Service service = getEC2Service();
103 		List<Image> images = service.getMyImages();
104 		info("images: %s", images.size());
105 	}
106 
107 	private static final DefaultEC2Service getUSWestService() {
108 		AWSCredentials credentials = Auth.getCredentials(EncryptedAwsCredentials.FOUNDATION);
109 		EC2ServiceContext context = EC2ServiceContext.builder(credentials).withRegion(US_WEST_1.getName()).build();
110 		return new DefaultEC2Service(context, new DefaultWaitService());
111 	}
112 
113 	private static final DefaultEC2Service getEC2Service() {
114 		AWSCredentials credentials = Auth.getCredentials(EncryptedAwsCredentials.FOUNDATION);
115 		EC2ServiceContext context = EC2ServiceContext.create(credentials);
116 		return new DefaultEC2Service(context, new DefaultWaitService());
117 	}
118 
119 	private final void info(String msg, Object... args) {
120 		if (args == null || args.length == 0) {
121 			logger.info(msg);
122 		} else {
123 			logger.info(String.format(msg, args));
124 		}
125 	}
126 
127 	private static final String SSD_DEVICE_NAME = "/dev/sdb";
128 	private static final String SSD_VIRTUAL_NAME = "ephemeral0";
129 	private static final Optional<EbsBlockDevice> NO_EBS = absent();
130 	private static final Optional<String> ABSENT = absent();
131 
132 	public static final ImmutableBlockDeviceMapping SSD = new ImmutableBlockDeviceMapping(SSD_DEVICE_NAME, NO_EBS, Optional.of(SSD_VIRTUAL_NAME), ABSENT);
133 
134 }