This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TablesCommandExecutor.java
294 lines (248 loc) · 9.5 KB
/
TablesCommandExecutor.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package cmd.functionality_commands;
import cmd.CommandExecutor;
import cmd.StreamGobbler;
import cmd.docker_daemon_utility.DockerException;
import cmd.docker_daemon_utility.DockerExecutor;
import databases.mysql.CloudEntityData;
import databases.mysql.daos.TablesRepositoryDAO;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Utility for CLI cloud NoSQL storage related command execution
*/
@SuppressWarnings("unused")
public class TablesCommandExecutor extends CommandExecutor {
/**
* Creates a new table to Google CLoud Big Table
* @param tableName name of the new table
* @param region region of deployment
* @param nodes number of nodes to store the table
* @param storageType type of storage: HDD or SSD
* @param columnFamilies column families of the table
*/
public static void createGoogleTable(String tableName, String region, int nodes, String storageType,
String... columnFamilies) {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not create table '" + tableName + "' on Google: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Creating table \"" + tableName + "\" to Google..." +
"\u001B[0m" + "\n");
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
String cmd;
try {
Process process;
StreamGobbler errorGobbler;
// generate id
String instanceId = tableName.toLowerCase().replace("_", "-") + "-" +
System.currentTimeMillis();
instanceId = instanceId.substring(0, Math.min(instanceId.length(), 30));
String clusterId = "cl-" + instanceId;
clusterId = clusterId.substring(0, Math.min(clusterId.length(), 30));
// create instance
System.out.println("Creating instance for '" + tableName + "'");
cmd = GoogleCommandUtility.buildGoogleCloudBigTableCreateInstanceCommand(tableName,
instanceId,
clusterId,
region,
nodes,
storageType);
process = buildCommand(cmd).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.out::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create instance for table '" + tableName + "' on Google");
process.destroy();
return;
}
process.destroy();
TablesRepositoryDAO.persistGoogle(instanceId, tableName);
// create table
System.out.println("Creating '" + tableName + "'");
cmd = GoogleCommandUtility.buildGoogleCloudBigTableCreateTableCommand(instanceId, tableName);
process = buildCommand(cmd).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.out::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create table '" + tableName + "' on Google");
process.destroy();
return;
}
process.destroy();
// create column families
for (String family : columnFamilies) {
System.out.println("Creating family '" + family + "' in '" + tableName + "'");
cmd = GoogleCommandUtility.buildGoogleCloudBigTableCreateFamilyCommand(instanceId, tableName, family);
process = buildCommand(cmd).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.out::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create family '" + family + " in '" + tableName + "' on Google");
process.destroy();
return;
}
process.destroy();
}
System.out.println("'" + tableName + "' created on Google");
} catch (InterruptedException | IOException e) {
System.out.println("'" + tableName + "' creation on Google failed: " + e.getMessage());
} finally {
executorServiceErr.shutdown();
}
}
/**
* Creates a new table to Amazon Dynamo DB
* @param tableName name of the new table
* @param directoryAbsolutePath path of the table definition file directory
* @param definitionFileName table definition file name
* @param region region for table creation
*/
public static void createAmazonTable(String tableName, String directoryAbsolutePath, String definitionFileName,
String region) {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not create table '" + tableName + "' on Amazon: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Creating table \"" + tableName + "\" to Amazon Web Services..." +
"\u001B[0m" + "\n");
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
try {
Process process;
StreamGobbler errorGobbler;
String cmd = AmazonCommandUtility.buildDynamoTableCreationCommand(tableName, directoryAbsolutePath,
definitionFileName, region);
process = buildCommand(cmd).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create table '" + tableName + "' on Amazon");
process.destroy();
return;
}
process.destroy();
TablesRepositoryDAO.persistAmazon(tableName, region);
waitFor("Table creation", 10);
System.out.println("'" + tableName + "' created on Amazon");
} catch (InterruptedException | IOException e) {
System.out.println("'" + tableName + "' creation on Amazon failed: " + e.getMessage());
} finally {
executorServiceErr.shutdown();
}
}
/**
* Removes an instance storing a table in Google Cloud Big Table
* @param instanceId id of the instance
* @param tableName name of the table stored
* @throws IOException exception related to process execution
* @throws InterruptedException exception related to Thread management
*/
private static void removeGoogleInstance(String instanceId, String tableName)
throws IOException, InterruptedException {
String cmd = GoogleCommandUtility.buildGoogleCloudBigTableDropInstanceCommand(instanceId);
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.out::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not delete instance '" + tableName + "' from Google");
} else {
System.out.println("'" + tableName + "' instance removed from Google!");
}
process.destroy();
executorServiceErr.shutdown();
}
/**
* Removes a table from Amazon Dynamo DB
* @param tableName name of the table to remove
* @param region region of the table to remove
* @throws IOException exception related to process execution
* @throws InterruptedException exception related to Thread management
*/
private static void removeAmazonTable(String tableName, String region)
throws IOException, InterruptedException {
String cmd = AmazonCommandUtility.buildDynamoTableDropCommand(tableName, region);
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not delete table '" + tableName + "' from Amazon");
} else {
System.out.println("'" + tableName + "' table removed from Amazon!");
}
process.destroy();
executorServiceErr.shutdown();
}
/**
* Removes every instance and table from Google Cloud Big Table
*/
public static void cleanupGoogleCloudTables() {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not cleanup Google tables environment: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Cleaning up Google tables environment..." +
"\u001B[0m" + "\n");
List<CloudEntityData> toRemove = TablesRepositoryDAO.getGoogles();
if (toRemove == null) {
return;
}
for (CloudEntityData elem : toRemove) {
try {
removeGoogleInstance(elem.getId(), elem.getEntityName());
} catch (InterruptedException | IOException e) {
System.err.println("Could not delete Google table '" + elem.getEntityName() + "': " +
e.getMessage());
}
}
TablesRepositoryDAO.dropGoogle();
if (!toRemove.isEmpty()) {
// cluster deallocate
waitFor("Cleanup", 60);
}
System.out.println("\u001B[32m" + "\nGoogle cleanup completed!\n" + "\u001B[0m");
}
/**
* Removes every table from Amazon Dynamo DB
*/
public static void cleanupAmazonCloudTables() {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not cleanup Amazon tables environment: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Cleaning up Amazon tables environment..." +
"\u001B[0m" + "\n");
List<CloudEntityData> toRemove = TablesRepositoryDAO.getAmazons();
if (toRemove == null) {
return;
}
for (CloudEntityData elem : toRemove) {
try {
removeAmazonTable(elem.getEntityName(), elem.getRegion());
} catch (InterruptedException | IOException e) {
System.err.println("Could not delete Amazon table '" + elem.getEntityName() + "': " +
e.getMessage());
}
}
TablesRepositoryDAO.dropAmazon();
if (!toRemove.isEmpty()) {
// let remote environment align state
waitFor("Cleanup", 5);
}
System.out.println("\u001B[32m" + "\nAmazon cleanup completed!\n" + "\u001B[0m");
}
}