forked from liferay/liferay-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3Cleaner.java
202 lines (146 loc) · 5.65 KB
/
S3Cleaner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.amazontools;
import com.amazonaws.services.autoscaling.model.AutoScalingGroup;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult;
import com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsRequest;
import com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsResult;
import com.amazonaws.services.autoscaling.model.LaunchConfiguration;
import com.amazonaws.services.ec2.model.DescribeImagesRequest;
import com.amazonaws.services.ec2.model.DescribeImagesResult;
import com.amazonaws.services.ec2.model.Image;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import jargs.gnu.CmdLineParser;
import java.util.ArrayList;
import java.util.List;
/**
* @author Mladen Cikara
*/
public class S3Cleaner extends BaseAMITool {
public static void main(String[] args) throws Exception {
CmdLineParser cmdLineParser = new CmdLineParser();
CmdLineParser.Option propertiesFileNameOption =
cmdLineParser.addStringOption("properties.file.name");
cmdLineParser.parse(args);
try {
new S3Cleaner(
(String)cmdLineParser.getOptionValue(propertiesFileNameOption));
}
catch (Exception e) {
e.printStackTrace();
System.exit(-1);
return;
}
System.exit(0);
}
public S3Cleaner(String propertiesFileName) throws Exception {
super(propertiesFileName);
deleteBucketNames();
}
protected void deleteBucketName(String bucketName) {
ObjectListing objectListing = amazonS3Client.listObjects(bucketName);
List<S3ObjectSummary> objectSummaries =
objectListing.getObjectSummaries();
if (!objectSummaries.isEmpty()) {
DeleteObjectsRequest deleteObjectsRequest =
new DeleteObjectsRequest(bucketName);
List<DeleteObjectsRequest.KeyVersion> keyVersions =
new ArrayList<DeleteObjectsRequest.KeyVersion>();
for (S3ObjectSummary objectSummary : objectSummaries) {
keyVersions.add(
new DeleteObjectsRequest.KeyVersion(
objectSummary.getKey()));
}
deleteObjectsRequest.setKeys(keyVersions);
amazonS3Client.deleteObjects(deleteObjectsRequest);
}
amazonS3Client.deleteBucket(bucketName);
}
protected void deleteBucketNames() {
System.out.println("Deleting buckets");
List<String> timestamps = new ArrayList<String>();
List<String> imageNames = getImageNames(
getImageIds(getLaunchConfigurationNames()));
for (String imageName : imageNames) {
timestamps.add(getTimestamp(imageName));
}
List<String> bucketNames = getBucketNames(timestamps);
for (String bucketName : bucketNames) {
System.out.println("Deleting bucket " + bucketName);
deleteBucketName(bucketName);
}
}
protected List<String> getBucketNames(List<String> timestamps) {
List<String> bucketNames = new ArrayList<String>();
List<Bucket> buckets = amazonS3Client.listBuckets();
for (Bucket bucket : buckets) {
String name = bucket.getName();
if (name.startsWith("frw-cluster") ||
name.startsWith("lcs-cluster")) {
String timestamp = getTimestamp(name);
if (!timestamps.contains(timestamp)) {
bucketNames.add(bucket.getName());
}
}
}
return bucketNames;
}
protected List<String> getImageIds(List<String> launchConfigurationNames) {
List<String> imageIds = new ArrayList<String>();
DescribeLaunchConfigurationsRequest
describeLaunchConfigurationsRequest =
new DescribeLaunchConfigurationsRequest();
describeLaunchConfigurationsRequest.setLaunchConfigurationNames(
launchConfigurationNames);
DescribeLaunchConfigurationsResult
describeLaunchConfigurationsResult =
amazonAutoScalingClient.describeLaunchConfigurations(
describeLaunchConfigurationsRequest);
for (LaunchConfiguration launchConfiguration :
describeLaunchConfigurationsResult.getLaunchConfigurations()) {
imageIds.add(launchConfiguration.getImageId());
}
return imageIds;
}
protected List<String> getLaunchConfigurationNames() {
List<String> launchConfigurationNames = new ArrayList<String>();
DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult =
amazonAutoScalingClient.describeAutoScalingGroups();
List<AutoScalingGroup> autoScalingGroups =
describeAutoScalingGroupsResult.getAutoScalingGroups();
for (AutoScalingGroup autoScalingGroup : autoScalingGroups) {
launchConfigurationNames.add(
autoScalingGroup.getLaunchConfigurationName());
}
return launchConfigurationNames;
}
protected List<String> getImageNames(List<String> imageIds) {
List<String> imageNames = new ArrayList<String>();
DescribeImagesRequest describeImagesRequest =
new DescribeImagesRequest();
describeImagesRequest.setImageIds(imageIds);
DescribeImagesResult describeImagesResult =
amazonEC2Client.describeImages(describeImagesRequest);
for (Image image : describeImagesResult.getImages()) {
imageNames.add(image.getName());
}
return imageNames;
}
protected String getTimestamp(String imageName) {
return imageName.substring(imageName.lastIndexOf('-') + 1);
}
}