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 pathGoogleCommandUtility.java
356 lines (322 loc) · 11.8 KB
/
GoogleCommandUtility.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package cmd.functionality_commands;
import cmd.CommandUtility;
import utility.PropertiesManager;
/**
* Utility for Google Cloud CLI command build
*/
@SuppressWarnings({"unused", "RedundantSuppression"})
public class GoogleCommandUtility extends CommandUtility {
/**
* Public constant variables: Languages
*/
public static final String PYTHON_3_7_RUNTIME = "python37";
public static final String GO_1_RUNTIME = "go111";
public static final String JAVA_11_RUNTIME = "java11";
public static final String NODE_10_RUNTIME = "nodejs10";
/**
* Public constant variables: Zones
*/
public static final String NORTH_VIRGINIA = "us-east4";
public static final String IOWA = "us-central1";
/**
* Public constant variables: Big Table memorization mediums
*/
public static final String BT_HARD_DISK_STORAGE = "HDD";
public static final String BT_SOLID_STATE_STORAGE = "SSD";
/**
* Docker utils
*/
private static final String PREAMBLE = "docker" + SEP + "run" + SEP + "--rm" + SEP + "-i";
private static final String GOOGLE_CLI = "google/cloud-sdk:316.0.0";
private static final String GOOGLE_CONFIG_BIND = "--volumes-from" + SEP +
PropertiesManager.getInstance().getProperty(PropertiesManager.GOOGLE_CONTAINER);
/**
* Google Cloud Functions commands
*/
private static final String FUNCTIONS = "gcloud" + SEP + "functions";
private static final String DEPLOY_FUNCTION_CMD = FUNCTIONS + SEP + "deploy";
private static final String REMOVE_FUNCTION_CMD = FUNCTIONS + SEP + "delete";
/**
* Google Cloud Workflows [BETA] commands
*/
private static final String WORKFLOWS = "gcloud" + SEP + "beta" + SEP + "workflows";
private static final String DEPLOY_WORKFLOW_CMD = WORKFLOWS + SEP + "deploy";
private static final String REMOVE_WORKFLOW_CMD = WORKFLOWS + SEP + "delete";
/**
* Google Cloud BigTable commands
*/
private static final String CLOUD_BIG_TABLE = "cbt";
@SuppressWarnings("SpellCheckingInspection")
private static final String CBT_CREATE_INSTANCE = CLOUD_BIG_TABLE + SEP + "createinstance";
@SuppressWarnings("SpellCheckingInspection")
private static final String CREATE_TABLE = "createtable";
@SuppressWarnings("SpellCheckingInspection")
private static final String CREATE_FAMILY = "createfamily";
@SuppressWarnings("SpellCheckingInspection")
private static final String CBT_DELETE_INSTANCE = CLOUD_BIG_TABLE + SEP + "deleteinstance";
/**
* Google Cloud Storage Commands
*/
private static final String CLOUD_STORAGE_UTILS = "gsutil";
private static final String CLOUD_STORAGE_CREATE_BUCKET = CLOUD_STORAGE_UTILS + SEP + "mb";
private static final String CLOUD_STORAGE_DELETE_BUCKET = CLOUD_STORAGE_UTILS + SEP + "rm";
/**
* Google CLI Docker image getter
* @return CLI Docker image name
*/
public static String getCli() {
return GOOGLE_CLI;
}
/**
* Builds Google Cloud CLI command for Functions function deployment
* @param functionName name of function to deploy
* @param runtime runtime of function to deploy
* @param entryPoint entry point location of function to deploy
* @param timeout timeout in seconds of function to deploy
* @param memory memory amount in megabytes of function to deploy
* @param region region of deployment for function to deploy
* @param functionDirPath path of the folder containing function and requirements
* @return command as string
*/
public static String buildGoogleCloudFunctionsDeployCommand(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String functionDirPath) {
return // command beginning
PREAMBLE + SEP +
// volume attachment
"-v" + SEP + functionDirPath + ":" + FUNCTIONALITIES_DIR + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to deploy a new function
DEPLOY_FUNCTION_CMD + SEP +
// function name
functionName + SEP +
// deployment options
"--allow-unauthenticated" + SEP +
"--memory=" + memory + "MB" + SEP +
"--runtime=" + runtime + SEP +
"--timeout=" + timeout + "s" + SEP +
"--region=" + region + SEP +
"--trigger-http" + SEP +
"--stage-bucket=" +
PropertiesManager.getInstance().getProperty(PropertiesManager.GOOGLE_STAGE_BUCKET) + SEP +
"--source=" + FUNCTIONALITIES_DIR + SEP +
"--entry-point=" + entryPoint;
}
/**
* Builds Google Cloud CLI command to deploy a new workflow to Google Cloud Platform Workflows [BETA]
* @param workflowName name of the new workflow
* @param region workflow region of deployment
* @param workflowDirPath path of the directory containing flow definition yaml
* @param fileName yaml workflow definition file name
* @return command as string
*/
public static String buildGoogleCloudWorkflowsDeployCommand(String workflowName, String region,
String workflowDirPath, String fileName) {
return // command beginning
PREAMBLE + SEP +
// volume attachment
"-v" + SEP + workflowDirPath + ":" + FUNCTIONALITIES_DIR + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to deploy a new workflow
DEPLOY_WORKFLOW_CMD + SEP +
// function name
workflowName + SEP +
"--location=" + region + SEP +
"--source=" + FUNCTIONALITIES_DIR + getPathSep() + fileName;
}
/**
* Builds Google Cloud CLI command to create a new BigTable instance
* @param name name of the instance
* @param id id to assign to the instance
* @param clusterId instance cluster id
* @param region region of the instance cluster
* @param clusterNodes number of nodes in the instance cluster
* @param storageType type of storage: HDD or SSD
* @return command as string
*/
public static String buildGoogleCloudBigTableCreateInstanceCommand(String name, String id, String clusterId,
String region, int clusterNodes,
String storageType) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to deploy a new instance
CBT_CREATE_INSTANCE + SEP +
// instance options
id + SEP + name + SEP +
clusterId + SEP + region + "-a" + SEP +
clusterNodes + SEP + storageType;
}
/**
* Builds Google Cloud CLI command to create a new table in Big Table
* @param instanceId id of the Big Table instance
* @param tableName name of the table
* @return command as string
*/
public static String buildGoogleCloudBigTableCreateTableCommand(String instanceId, String tableName) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to deploy a new table
CLOUD_BIG_TABLE + SEP +
"-instance=" + instanceId + SEP +
CREATE_TABLE + SEP +
// table name
tableName;
}
/**
* Builds Google Cloud CLI command to create a new family in a table in Big Table
* @param instanceId id of the Big Table instance
* @param tableName name of the table
* @param familyName name of the new family
* @return command as string
*/
public static String buildGoogleCloudBigTableCreateFamilyCommand(String instanceId, String tableName,
String familyName) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to add a new column family
CLOUD_BIG_TABLE + SEP +
"-instance=" + instanceId + SEP +
CREATE_FAMILY + SEP +
// table and family
tableName + SEP + familyName;
}
/**
* Builds Google Cloud CLI command for Cloud Storage bucket creation
* @param bucketName name of the new bucket
* @param region region of the new bucket
* @return command as string
*/
public static String buildGoogleCloudStorageBucketCreationCommand(String bucketName, String region) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to create a new bucket
CLOUD_STORAGE_CREATE_BUCKET + SEP +
"-l" + SEP + region + SEP +
// bucket name
"gs://" + bucketName;
}
/**
* Builds Google Cloud CLI command for Functions function deletion
* @param functionName name of the function to remove
* @param region function to remove region of deployment
* @return command as string
*/
public static String buildGoogleCloudFunctionsRemoveCommand(String functionName, String region) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to remove a function
REMOVE_FUNCTION_CMD + SEP +
// function name
functionName + SEP +
"--region=" + region + SEP +
"--quiet";
}
/**
* Builds Google Cloud CLI command to remove a workflow from Google Cloud Platform Workflows [BETA]
* @param workflowName name of the workflow to delete
* @param region workflow to delete region of deployment
* @return command as string
*/
public static String buildGoogleCloudWorkflowsRemoveCommand(String workflowName, String region) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to remove a workflow
REMOVE_WORKFLOW_CMD + SEP +
// function name
workflowName + SEP +
"--location=" + region + SEP +
"--quiet";
}
/**
* Builds Google Cloud CLI command to delete a BigTable instance
* @param id id of the instance to delete
* @return command as string
*/
public static String buildGoogleCloudBigTableDropInstanceCommand(String id) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to deploy a delete an instance
CBT_DELETE_INSTANCE + SEP +
// instance id
id;
}
/**
* Builds Google Cloud CLI command for Cloud Storage bucket deletion
* @param bucketName name of the bucket to delete
* @return command as string
*/
public static String buildGoogleCloudStorageBucketDropCommand(String bucketName) {
return // command beginning
PREAMBLE + SEP +
// project config binding
GOOGLE_CONFIG_BIND + SEP +
// select docker image to use
GOOGLE_CLI + SEP +
// CLI command to delete a bucket
CLOUD_STORAGE_DELETE_BUCKET + SEP +
"-r" + SEP +
"-f" + SEP +
// bucket name
"gs://" + bucketName;
}
/**
* Translates function name and runtime to a string that will work as function identifier
* @param functionalityName name of the function to apply id
* @param runtime to translate
* @return function identifier
* @throws IllegalNameException if functionalityName is an illegal name
*/
@SuppressWarnings("DuplicatedCode")
public static String applyRuntimeId(String functionalityName, String runtime) throws IllegalNameException {
if (needsRuntimeId(functionalityName)) {
switch (runtime) {
case PYTHON_3_7_RUNTIME:
return functionalityName + PYTHON_ID;
case JAVA_11_RUNTIME:
return functionalityName + JAVA_ID;
case NODE_10_RUNTIME:
return functionalityName + NODE_ID;
case GO_1_RUNTIME:
return functionalityName + GO_1_RUNTIME;
default:
return functionalityName + OTHERS_ID;
}
} else {
return functionalityName;
}
}
}