-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfigure.java
470 lines (413 loc) · 17.6 KB
/
Configure.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2017 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @author Esteban Posada <epm105@hotmail.com>
* @copyright Adobe 2017
* @version 1.0 - first release
* @license Apache License, Version 2.0
*
* This program creates, updates the actions necessary. It also creates the
* corresponding json files that are the parameters to the actions. This
* program was created with the only intention of making the tutorial shorter
* and easier. People are happy if they need to think less!
*/
import java.util.*;
import java.io.*;
public class Configure {
// Constant for the name of the file we will store the package information
// paths
public static final String LIB_PATH = "./lib/";
public static final String PARAMS_PATH = LIB_PATH + "params/";
// is dinamically set by user input, trusting user input.
public static String LINKS_PATH = "";
// parameter files
public static final String INFO_FILE = PARAMS_PATH + "info.txt";
public static final String COMMAND_PARAMS = PARAMS_PATH + "commands-params.json";
public static final String EVENT_PARAMS = PARAMS_PATH + "events-params.json";
public static final String AUTH_PARAMS = PARAMS_PATH + "auth-params.json";
// library files
public static final String COMMAND_FILE = LIB_PATH + "commands.js";
public static final String EVENT_FILE = LIB_PATH + "events.js";
public static final String AUTH_FILE = LIB_PATH + "authentication.js";
public static final String EVENT_TEMP_FILE = LIB_PATH + "eventsTemp.js";
public static final String SETUP_FILE = LIB_PATH + "setupeventurl.js";
public static final String PERSIST_FILE = "./node_modules/openwhisk-cache-redis/" +
"openwhisk-cache-redis-0.0.2.js";
public static void main(String[] args) throws FileNotFoundException {
Scanner ask = new Scanner(System.in);
System.out.println();
System.out.println("Set up your bot production ecosystem!");
System.out.println("Please type one of the following options:");
System.out.println();
System.out.println(" 1. setup");
System.out.println(" 2. install");
System.out.println(" 3. update-commands");
System.out.println(" 4. update-distribution");
System.out.println(" 5. update-events");
System.out.println(" 6. delete-build-files");
System.out.println();
System.out.print("Type the option number: ");
String option = ask.nextLine();
String name = "";
switch(option) {
case "1" :
setup();
break;
case "2" :
name = readPackage();
install(name);
break;
case "6" :
deleteAll();
break;
case "3" :
name = readPackage();
updateCommands(name);
break;
case "4" :
name = readPackage();
updateDistribution(name);
break;
case "5" :
name = readPackage();
updateEvents(name);
break;
default :
System.out.println("\nInvalid option number: " + option);
}
System.out.println();
}
public static void setup() throws FileNotFoundException {
System.out.println();
System.out.println("setting up OpenWhisk and OW actions...");
System.out.println("creating identification files for your new bot...");
// in Scanner
Scanner input = new Scanner(System.in);
// Ask user for name of OW package in where to store the bot project
System.out.println("----------------OW PROJECT---------------");
System.out.println("Enter your OW main (without namespace and/or package) end-point for web actions: ");
LINKS_PATH = input.nextLine();
System.out.print("Enter the name of your OW namespace: ");
String namespaceName = input.nextLine();
System.out.print("Enter the name of your bot project (package name): ");
String packageName = input.nextLine();
System.out.println("You package names is: " + packageName + ". Please remember this name!");
System.out.println("----------------OW PROJECT---------------");
System.out.println();
// Ask the user for the App information
System.out.println("----------------BOT INFORMATION----------");
System.out.print("Enter the verification token: ");
String verifyToken = input.nextLine();
System.out.print("Enter the cliend id: ");
String clientId = input.nextLine();
System.out.print("Enter the cliend secret: ");
String clientSecret = input.nextLine();
System.out.print("Enter a secret state (will be used to prevent attacks, " +
"please remember this state): ");
String state = input.nextLine();
System.out.println("----------------BOT INFORMATION----------");
System.out.println();
System.out.println("----------------USEFUL LINKS-------------");
System.out.println("Events link: " + LINKS_PATH + namespaceName + "/" + packageName + "/events");
System.out.println("Auth link: " + LINKS_PATH + namespaceName + "/" + packageName + "/authentication");
System.out.println("Commands link: " + LINKS_PATH + namespaceName + "/" + packageName + "/commands");
System.out.println("----------------USEFUL LINKS-------------");
// Save the packageName in a txt file, will need this package name
// and the namespace name in other stages
File directory = new File(PARAMS_PATH);
directory.mkdir();
PrintStream projectInfo = new PrintStream(new File(INFO_FILE));
projectInfo.println(namespaceName);
projectInfo.println(packageName);
// Create package with given name
String[] packageS = {"wsk", "package", "create", "" + packageName};
try {
Process proc = new ProcessBuilder(packageS).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// Create the parameter files
// Auth param file
PrintStream authParams = new PrintStream(new File(AUTH_PARAMS));
String body = "{ \"check\": \"" + state + "\", \"id\": " + "\"" +
clientId + "\", \"secret\": \"" + clientSecret + "\" }";
authParams.print(body);
// Event param file
PrintStream eventParams = new PrintStream(new File(EVENT_PARAMS));
body = "{ \"verifyToken\": \"" + verifyToken + "\" }";
eventParams.print(body);
// Command param file
PrintStream commandParams = new PrintStream(new File(COMMAND_PARAMS));
body = "{ \"verifyToken\": \"" + verifyToken + "\" }";
commandParams.print(body);
String[] action = {"wsk", "action", "create", packageName + "/events",
SETUP_FILE, "--web", "true"};
String[] installGruntCLI = {"npm", "install", "-g", "grunt-cli"};
String[] runOurNpmInstall = {"npm", "install"};
try {
Process proc = new ProcessBuilder(action).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(installGruntCLI).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(runOurNpmInstall).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
System.out.println();
System.out.println("we are done setting up everything, please continue " +
"following the instructions.");
}
public static void install(String packageName) {
System.out.println();
System.out.println("installing required actions and files...");
// Before we create the actions the actions to deploy, merge the lib files
// and the developers files (events and commands)
String[] mergeCommands = {"grunt", "--gruntfile", "GruntfileCommands.js"};
String[] mergeEvents = {"grunt", "--gruntfile", "GruntfileEvents.js"};
// Merge with the new merged commands and events files the verificationLib
String[] mergeVerCommands = {"grunt", "--gruntfile", "GruntfileVerCommands.js"};
String[] mergeVerEvents = {"grunt", "--gruntfile", "GruntfileVerEvents.js"};
try {
Process proc = new ProcessBuilder(mergeCommands).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(mergeEvents).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(mergeVerCommands).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(mergeVerEvents).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// Now, create the commands and authentication action and update the events
// action
// Commands action
String[] commandAction = {"wsk", "action", "create", packageName + "/commands",
COMMAND_FILE, "--param-file",
COMMAND_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(commandAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// Authentication action
String[] authAction = {"wsk", "action", "create", packageName + "/authentication",
AUTH_FILE, "--param-file",
AUTH_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(authAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// Update events action
String[] eventsAction = {"wsk", "action", "update", packageName + "/events",
EVENT_FILE, "--param-file",
EVENT_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(eventsAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// Create the persist action inside another
String[] cachePackage = {"wsk", "package", "create", "cache"};
String[] persistAction = {"wsk", "action", "create", "cache/persist",
PERSIST_FILE,
"--param", "redis_host", "//10.0.2.159:6380"};
try {
Process proc = new ProcessBuilder(cachePackage).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(persistAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
System.out.println();
System.out.println("we are done installing the actions!");
}
public static void deleteAll() {
// Create the persist action inside another
String[] rmParams = {"rm", "-rf", PARAMS_PATH};
String[] rmConfigure = {"rm", "Configure.class"};
try {
Process proc = new ProcessBuilder(rmParams).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(rmConfigure).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
}
public static void updateCommands(String packageName) {
// Merge the files and create a new commands.js
String[] mergeCommands = {"grunt", "--gruntfile", "GruntfileCommands.js"};
// Merge with the new merged commands and events files the verificationLib
String[] mergeVerCommands = {"grunt", "--gruntfile", "GruntfileVerCommands.js"};
try {
Process proc = new ProcessBuilder(mergeCommands).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(mergeVerCommands).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// update commands action
String[] commandAction = {"wsk", "action", "update", packageName + "/commands",
COMMAND_FILE, "--param-file",
COMMAND_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(commandAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
System.out.println();
System.out.println("the commands action was updates!\n");
}
public static void updateDistribution(String packageName) {
// Merge the files and create a new commands.js
String[] mergeAuth = {"grunt", "--gruntfile", "GruntfileAuth.js"};
// update authentication action
String[] authAction = {"wsk", "action", "update", packageName + "/authentication",
AUTH_FILE, "--param-file",
AUTH_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(mergeAuth).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(authAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
System.out.println();
System.out.println("the distribution pages were updated!\n");
}
public static void updateEvents(String packageName) {
// Merge the files and create a new commands.js
String[] mergeEvents = {"grunt", "--gruntfile", "GruntfileEvents.js"};
// Merge with the new merged commands and events files the verificationLib
String[] mergeVerEvents = {"grunt", "--gruntfile", "GruntfileVerEvents.js"};
try {
Process proc = new ProcessBuilder(mergeEvents).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
try {
Process proc = new ProcessBuilder(mergeVerEvents).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
// update events action
String[] eventsAction = {"wsk", "action", "update", packageName + "/events",
EVENT_FILE, "--param-file",
EVENT_PARAMS, "--web", "true"};
try {
Process proc = new ProcessBuilder(eventsAction).start();
proc.waitFor();
} catch(Exception err) {
System.out.println("An error happened, please contact us or try again!");
System.exit(1);
}
System.out.println();
System.out.println("the events action was updates!\n");
}
public static String readPackage() throws FileNotFoundException {
Scanner file = new Scanner(new File(INFO_FILE));
file.nextLine();
return file.nextLine();
}
public static String readNamespace() throws FileNotFoundException {
Scanner file = new Scanner(new File(INFO_FILE));
return file.nextLine();
}
}