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 pathOpenWhiskCommandUtility.java
226 lines (201 loc) · 7.26 KB
/
OpenWhiskCommandUtility.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
package cmd.functionality_commands;
import cmd.CommandUtility;
import utility.PropertiesManager;
/**
* Utility for OpenWhisk CLI command build
*/
@SuppressWarnings({"unused", "RedundantSuppression"})
public class OpenWhiskCommandUtility extends CommandUtility {
/**
* Public constant variables: Languages
*/
public static final String PYTHON_3_RUNTIME = "python:3";
public static final String GO_1_RUNTIME = "go:1.11";
public static final String JAVA_8_RUNTIME = "java:8";
public static final String NODE_10_RUNTIME = "nodejs:10";
/**
* Docker utils
*/
private static final String PREAMBLE = "docker" + SEP + "run" + SEP + "--rm" + SEP + "-i";
@SuppressWarnings("SpellCheckingInspection")
private static final String OPENWHISK_CLI = "francescom412/ow-utils-complete:63a5498";
@SuppressWarnings("SpellCheckingInspection")
private static final String OPENWHISK_AUTH_CLOSURE = "--apihost" + SEP +
PropertiesManager.getInstance().getProperty(PropertiesManager.OPENWHISK_HOST) + SEP + "--auth" + SEP +
PropertiesManager.getInstance().getProperty(PropertiesManager.OPENWHISK_AUTH);
/**
* Command preambles
*/
private static final String INSECURE_OPTION = "-i";
private static final String WSK = "wsk" + SEP + INSECURE_OPTION;
/**
* Wsk actions
*/
private static final String ACTION = WSK + SEP + "action";
private static final String CREATE_ACTION = ACTION + SEP + "create";
private static final String GET_ACTION = ACTION + SEP + "get";
private static final String DELETE_ACTION = ACTION + SEP + "delete";
private static final String UPDATE_ACTION = ACTION + SEP + "update";
/**
* Compositions command
*/
private static final String DEPLOY_COMPOSITION = "deploy" + SEP + INSECURE_OPTION;
// local command, does not need insecure option
private static final String CREATE_COMPOSITION = "compose";
/**
* OpenWhisk CLI Docker image getter
* @return CLI Docker image name
*/
public static String getCli() {
return OPENWHISK_CLI;
}
/**
* Builds OpenWhisk CLI command for new action creation
* @param actionName name of the action
* @param runtime runtime of the new function
* @param timeout timeout in seconds of function to deploy
* @param memory memory amount in megabytes of function to deploy
* @param zipFolder path of the folder containing the zip package with the function
* @param zipName name of the zip package with the function
* @return command as string
*/
public static String buildActionDeployCommand(String actionName, String runtime, String entryPoint, Integer timeout,
Integer memory, String zipFolder, String zipName, boolean enableWeb) {
String webDeploy = enableWeb ? "true" : "false";
return // command beginning
PREAMBLE + SEP +
// volume attachment
"-v" + SEP + zipFolder + ":" + FUNCTIONALITIES_DIR + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
CREATE_ACTION + SEP +
// parameters setting
actionName + SEP +
"--web" + SEP + webDeploy + SEP +
"--kind" + SEP + runtime + SEP +
"--main" + SEP + entryPoint + SEP +
"--memory" + SEP + memory + SEP +
"--timeout" + SEP + timeout*1000 + SEP +
FUNCTIONALITIES_DIR + "/" + zipName + SEP +
// configuration binding
OPENWHISK_AUTH_CLOSURE;
}
/**
* Builds OpenWhisk CLI command for action URL retrieval
* @param actionName name of the action associated to the desired url
* @return command as string
*/
public static String buildActionUrlGetterCommand(String actionName) {
return // command beginning
PREAMBLE + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
GET_ACTION + SEP +
// parameters setting
actionName + SEP +
"--url" + SEP +
// configuration binding
OPENWHISK_AUTH_CLOSURE;
}
/**
* Builds OpenWhisk CLI command for composition json file creation starting from javascript composition description
* @param compositionFilePath absolute path of the javascript composition description
* @return command as string
*/
public static String buildCompositionFileCreationCommand(String compositionFilePath) {
return // command beginning
PREAMBLE + SEP +
// volume attachment
"-v" + SEP + compositionFilePath + ":" + FUNCTIONALITIES_DIR + "/" + "composition.js" + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
CREATE_COMPOSITION + SEP +
// parameters setting
FUNCTIONALITIES_DIR + "/" + "composition.js";
}
/**
* Builds OpenWhisk CLI command for composition deploy
* @param compositionName name of the new composition
* @param compositionJsonPath absolute path of the json file containing the composition description
* @return command as string
*/
public static String buildCompositionDeployCommand(String compositionName, String compositionJsonPath) {
return // command beginning
PREAMBLE + SEP +
// volume attachment
"-v" + SEP + compositionJsonPath + ":" + FUNCTIONALITIES_DIR + "/" + "composition.json" + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
DEPLOY_COMPOSITION + SEP +
// parameters setting
compositionName + SEP +
FUNCTIONALITIES_DIR + "/" + "composition.json" + SEP +
// configuration binding
OPENWHISK_AUTH_CLOSURE;
}
/**
* Builds OpenWhisk CLI command to enable composition web reachability
* @param compositionName name of the composition
* @return command as string
*/
public static String buildCompositionWebEnableCommand(String compositionName) {
return // command beginning
PREAMBLE + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
UPDATE_ACTION + SEP +
// parameters setting
compositionName + SEP +
"--web" + SEP + "true" + SEP +
// configuration binding
OPENWHISK_AUTH_CLOSURE;
}
/**
* Builds OpenWhisk CLI command for action deletion
* @param actionName name of the action to delete
* @return command as string
*/
public static String buildActionDeletionCommand(String actionName) {
return // command beginning
PREAMBLE + SEP +
// select docker image to use
OPENWHISK_CLI + SEP +
// operation define
DELETE_ACTION + SEP +
// parameters setting
actionName + SEP +
// configuration binding
OPENWHISK_AUTH_CLOSURE;
}
/**
* 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_RUNTIME:
return functionalityName + PYTHON_ID;
case JAVA_8_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;
}
}
}