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
/
Copy pathDockerExecutor.java
339 lines (292 loc) · 10.7 KB
/
DockerExecutor.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cmd.docker_daemon_utility;
import cmd.CommandExecutor;
import cmd.StreamGobbler;
import cmd.benchmark_commands.BenchmarkCommandUtility;
import cmd.functionality_commands.AmazonCommandUtility;
import cmd.functionality_commands.GoogleCommandUtility;
import cmd.functionality_commands.OpenWhiskCommandUtility;
import cmd.functionality_commands.output_parsing.ReplyCollector;
import utility.ComposeManager;
import utility.PropertiesManager;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Utility for Docker related executions
*/
@SuppressWarnings({"SpellCheckingInspection", "RedundantSuppression"})
public class DockerExecutor extends CommandExecutor {
/**
* Exit codes
*/
private static final Integer DOCKER_DAEMON_NOT_FOUND_EXIT_CODE = 16;
private static final Integer DOCKER_MISSING_IMAGE = 17;
private static final Integer DOCKER_NEEDS_GOOGLE_INITIALIZATION = 18;
/**
* Docker composition directory
*/
private static final String COMPOSE_DIR = PropertiesManager.getInstance().getProperty(
PropertiesManager.DOCKER_COMPOSE_DIR);
/**
* Docker needed containers
*/
private static final String PULL = "docker pull ";
private static final String AWS_CLI = AmazonCommandUtility.getCli();
private static final String GOOGLE_CLI = GoogleCommandUtility.getCli();
private static final String OPENWHISK_CLI = OpenWhiskCommandUtility.getCli();
private static final String MYSQL = ComposeManager.getInstance().getMysql();
private static final String INFLUX = ComposeManager.getInstance().getInflux();
private static final String GRAFANA = ComposeManager.getInstance().getGrafana();
private static final String WRK2 = BenchmarkCommandUtility.getImg();
/**
* Google cloud CLI configuration
*/
private static final String GOOGLE_CONFIG_CONTAINER = PropertiesManager.getInstance().getProperty(
PropertiesManager.GOOGLE_CONTAINER);
private static final String GOOGLE_CONFIG_COMMAND = "docker run -it --name " + GOOGLE_CONFIG_CONTAINER +
" " + GOOGLE_CLI + " gcloud init";
/**
* New docker-compose start containers report this string
*/
private static final String START_COMPOSITION_SUBSTRING = "Starting";
private static final String NEW_COMPOSITION_SUBSTRING = "Creating";
/**
* Checks if Docker daemon is running
* @throws DockerException if Docker daemon cannot be checked
*/
private static void checkDockerRunning() throws DockerException {
// build command
String cmd = "docker info";
try {
if (!commandSilentExecution(cmd)) {
System.err.println("Docker daemon not in execution");
System.exit(DOCKER_DAEMON_NOT_FOUND_EXIT_CODE);
}
} catch (InterruptedException | IOException e) {
throw new DockerException("Docker daemon not checked: " + e.getMessage());
}
}
/**
* Checks if a specific Docker image exists locally
* @param image image to check
* @return true if image needs to be downloaded, false elsewhere
* @throws InterruptedException process execution related problems
* @throws IOException process start related problems
*/
private static boolean needsDockerImage(String image) throws Exception {
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
try {
// build command
String cmd = "docker images -q " + image;
// start process execution
ReplyCollector collector = new ReplyCollector();
Process process = buildCommand(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), collector::collectResult);
executorServiceOut.submit(outputGobbler);
if (process.waitFor() != 0) {
process.destroy();
throw new Exception("Process exited with error");
}
process.destroy();
// if empty image is not present locally
return collector.getResult().equals("");
} finally {
executorServiceOut.shutdown();
}
}
/**
* Checks if Docker images are missing locally
* @throws DockerException if images cannot be checked
*/
@SuppressWarnings("DuplicatedCode")
private static void checkDockerImages() throws DockerException {
try {
boolean google = needsDockerImage(GOOGLE_CLI);
boolean amazon = needsDockerImage(AWS_CLI);
boolean openWhisk = needsDockerImage(OPENWHISK_CLI);
boolean mySql = needsDockerImage(MYSQL);
boolean influx = needsDockerImage(INFLUX);
boolean grafana = needsDockerImage(GRAFANA);
boolean wrk2 = needsDockerImage(WRK2);
if (google || amazon || openWhisk || mySql || influx || grafana || wrk2) {
System.out.println("Docker images are missing!\n" +
"Please wait for the following command(s) to be executed (several minutes may be needed):");
int counter = 1;
if (google) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + GOOGLE_CLI + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + GOOGLE_CLI)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (amazon) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + AWS_CLI + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + AWS_CLI)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (openWhisk) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + OPENWHISK_CLI + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + OPENWHISK_CLI)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (mySql) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + MYSQL + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + MYSQL)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (influx) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + INFLUX + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + INFLUX)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (grafana) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + GRAFANA + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + GRAFANA)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
if (wrk2) {
System.out.println(counter + ") " + "\u001B[34m" + PULL + WRK2 + "\u001B[0m");
counter++;
if (commandSilentExecution(PULL + WRK2)) {
System.out.println("Completed!");
} else {
System.err.println("Failed!");
System.exit(DOCKER_MISSING_IMAGE);
}
}
counter--;
System.out.println(counter + " out of " + counter + " command(s) executed!");
}
} catch (Exception e) {
throw new DockerException("Could not check Docker images presence: " + e.getMessage());
}
}
/**
* Checks if Google Cloud CLI container is correctly configured in Docker
* @throws DockerException if configuration cannot be checked
*/
private static void checkDockerConfig() throws DockerException {
// build command
String cmd = "docker ps -a";
// start executor
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
// create reply collector
ReplyCollector listCollector = new ReplyCollector();
try {
// start process execution
Process process = buildCommand(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), listCollector::collectResult);
executorServiceOut.submit(outputGobbler);
if (process.waitFor() != 0) {
process.destroy();
throw new DockerException("Could not check Google CLI correct configuration");
}
// check container existence
String list = listCollector.getResult();
if (!list.contains(GOOGLE_CONFIG_CONTAINER)) {
process.destroy();
executorServiceOut.shutdown();
System.err.println("Google CLI initial configuration is needed!\n" +
"Please execute the following command in your shell:\n" +
"\u001B[34m" + GOOGLE_CONFIG_COMMAND + "\u001B[0m");
System.exit(DOCKER_NEEDS_GOOGLE_INITIALIZATION);
}
process.destroy();
} catch (InterruptedException | IOException e) {
throw new DockerException("Could not check Google CLI correct configuration: " + e.getMessage());
} finally {
executorServiceOut.shutdown();
}
}
/**
* Create every subdirectory needed in the Docker compose file
* @throws DockerException if creation error occurs
*/
private static void createDirectoryEnv() throws DockerException {
List<String> subDirectories = ComposeManager.getInstance().getLocalVolumes();
String directoryPath;
File directory;
// create every directory if not already exists
for (String subDirectory : subDirectories) {
directoryPath = COMPOSE_DIR + subDirectory;
directory = new File(directoryPath);
if (!directory.exists()) {
if (!directory.mkdirs()) {
throw new DockerException("Could not create '" + directoryPath + "'");
}
}
}
}
/**
* Deploys Docker compose
* @throws DockerException if error occurs
*/
private static void deployComposition() throws DockerException {
// build command
String cmd = "docker-compose -f " + COMPOSE_DIR + "/docker-compose.yml up -d";
// start executor
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
try {
Process process = buildCommand(cmd).start();
// create, execute and submit output gobblers
ReplyCollector collector = new ReplyCollector();
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), collector::collectResult);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
process.destroy();
throw new DockerException("Could not deploy docker compose environment");
}
String response = collector.getResult();
if (response.contains(START_COMPOSITION_SUBSTRING) || response.contains(NEW_COMPOSITION_SUBSTRING)) {
// need to wait for compose environment coming up
waitFor("Deploying Docker", 30);
}
process.destroy();
} catch (InterruptedException | IOException e) {
throw new DockerException("Docker compose environment not checked: " + e.getMessage());
} finally {
executorServiceErr.shutdown();
}
}
/**
* Checks Docker daemon running and deploys composition
* @throws DockerException if error occurs
*/
public static void checkDocker() throws DockerException {
checkDockerRunning();
checkDockerImages();
checkDockerConfig();
createDirectoryEnv();
deployComposition();
}
}