1 package org.kuali.common.devops.ubuntu;
2
3 import static com.amazonaws.regions.Region.getRegion;
4 import static com.amazonaws.regions.Regions.US_WEST_1;
5 import static com.google.common.base.Stopwatch.createStarted;
6 import static java.lang.String.format;
7 import static org.kuali.common.util.encrypt.Encryption.getDefaultEncryptor;
8 import static org.kuali.common.util.log.Loggers.newLogger;
9
10 import java.util.List;
11
12 import org.junit.Test;
13 import org.kuali.common.aws.ec2.api.EC2Service;
14 import org.kuali.common.aws.ec2.impl.DefaultEC2Service;
15 import org.kuali.common.aws.ec2.model.ImmutableTag;
16 import org.kuali.common.aws.ec2.model.VolumeRequest;
17 import org.kuali.common.aws.model.ImmutableAWSCredentials;
18 import org.kuali.common.core.json.api.JsonService;
19 import org.kuali.common.core.json.jackson.JacksonJsonService;
20 import org.kuali.common.core.ssh.KeyPair;
21 import org.kuali.common.core.system.VirtualSystem;
22 import org.kuali.common.util.channel.api.ChannelService;
23 import org.kuali.common.util.channel.api.SecureChannel;
24 import org.kuali.common.util.channel.impl.DefaultChannelService;
25 import org.kuali.common.util.channel.model.ChannelContext;
26 import org.kuali.common.util.encrypt.Encryptor;
27 import org.slf4j.Logger;
28
29 import com.amazonaws.auth.AWSCredentials;
30 import com.amazonaws.regions.Region;
31 import com.amazonaws.services.ec2.model.Instance;
32 import com.amazonaws.services.ec2.model.Tag;
33 import com.google.common.base.Stopwatch;
34 import com.google.common.collect.ImmutableList;
35
36 public class MuckWithVolumes {
37
38 private static final Logger logger = newLogger();
39
40 @Test
41 public void test() {
42 try {
43 Stopwatch sw = createStarted();
44 Region region = getRegion(US_WEST_1);
45 VirtualSystem vs = VirtualSystem.build();
46 String instanceId = vs.getProperty("instance", "i-df5b6581");
47 JsonService json = new JacksonJsonService();
48 AWSCredentials credentials = getFoundation();
49 EC2Service ec2 = new DefaultEC2Service(credentials, region.getName());
50 Instance instance = ec2.getInstance(instanceId);
51 Tag name = new ImmutableTag("Name", "ci.master.jobs");
52 Tag stack = new ImmutableTag("Stack", "test");
53 List<Tag> tags = ImmutableList.of(name, stack);
54 ChannelContext ctx = ChannelContext.builder(instance.getPublicDnsName()).privateKey(getPrivateKey()).username("root").build();
55 ChannelService cs = new DefaultChannelService();
56 SecureChannel channel = cs.openChannel(ctx);
57
58 info("instance -> %s", instance.getInstanceId());
59 info("hostname -> %s", instance.getPublicDnsName());
60
61 String zone = instance.getPlacement().getAvailabilityZone();
62 String volumeId = ec2.createVolume(zone, 1024);
63 info("create -> %s", volumeId);
64 ec2.tag(volumeId, tags);
65 String device = "/dev/xvdf";
66 String dir = "/home/tomcat7/.jenkins/jobs";
67 VolumeRequest request = VolumeRequest.builder().withDevice(device).withInstanceId(instanceId).withVolumeId(volumeId).build();
68 info("attach -> %s %s:%s", request.getVolumeId(), instance.getPublicDnsName(), device);
69 ec2.attachVolume(request);
70
71 channel.exec(format("mkfs -t ext4 %s", device));
72 channel.exec(format("mkdir -p %s", dir));
73 channel.exec(format("chown -R tomcat7:tomcat7 %s", dir));
74 channel.exec(format("mount %s %s", device, dir));
75
76
77
78
79
80
81 } catch (Throwable e) {
82 e.printStackTrace();
83 }
84 }
85
86 protected static void debug(String msg, Object... args) {
87 logger.debug((args == null || args.length == 0) ? msg : format(msg, args));
88 }
89
90 protected static void info(String msg, Object... args) {
91 logger.info((args == null || args.length == 0) ? msg : format(msg, args));
92 }
93
94 private static String getPrivateKey() {
95 Encryptor enc = getDefaultEncryptor();
96 String publicKey = enc
97 .decrypt("U2FsdGVkX1/mOVOU9KiA43fWz10qtG08ixrGGBk+DsuFEikZ1N3bz5BUiPD4G7okAeQgen+RQ4E40/AybeLRjJUuOgLUdeOCpcK6u/H+EZs+N820xZe1Hpq/wchfs13npj6gWpOWylQAhl5DSioe5wHxkQC3hORV13sr5Mhaf+SorF/NPyzvERsJZ/FoScV+RcGxLfwVmlAUEVdsYNfgUepXPUWwTDmNu4FcRO/8ud7Kth7E3BkwG7sTN9n7pkEbA632ZdjcccBIptOfurArMLORN8VsCbiQU8uFo2leudzsa5zdx6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9EHzAgrrV93ZMUgypO7KEVvGeXvDPghMiPOhwpGBlullTxNORYDwiTi+WiG9CgSNh6nWZlW8w8+W6mKQeaximfAL/VN+c7QUCQpONBPsPjYcZ8WuJmJn0YxrPJoS3n0mOuOfbe+Uq9mBfU5WZfvma+1KlYK+gNUdbLmv1HZiUwcwAO2HYW/EQOfU5YBjS97PO4MReqDzI7tY3pz2toxs5qHiy4hs0/PwOe2duWj/fDlsNFAKQnR6uuIAUBLt2S1ipAW6dxvYMzj5LhPXz4nDVnImsbSZw8zcViJlzylxrJZNBJYLeShAHm+VcIISoxED89/lL7kSWGlEHBrpMnpAdr77jmY0mkEHvOYsdw0G/S0tRTS8rACvP+tRRdmIj+QLQCRWY8w==");
100 return KeyPair.builder("kuali-devops").withPublicKey(publicKey).withPrivateKey(privateKey).build().getPrivateKey();
101 }
102
103 private static AWSCredentials getFoundation() {
104 Encryptor enc = getDefaultEncryptor();
105 String accessKey = enc.decrypt("U2FsdGVkX19A2e6dN/ipVfb/9n0DROCPIrLK6H8PvvPmt0h6cBqccGaJW0NSoX3S");
106 String secretKey = enc.decrypt("U2FsdGVkX19Y9SZ5GAU82/X5Z0xZdeQf7DFuVDW07R9lfyHK4VaOj5R7pviRBKmIyn7jrVT2lv8Edeu7098k1A==");
107 return ImmutableAWSCredentials.build(accessKey, secretKey);
108 }
109
110 }