This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 257
/
deploy.js
411 lines (386 loc) · 12.3 KB
/
deploy.js
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
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* 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
*
* https://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.
*/
const os = require('os');
const fs = require('fs');
const path = require('path');
const itm = require('@ibm-functions/iam-token-manager');
const openwhisk = require('openwhisk');
const async = require('async');
const request = require('request');
const argv = require('yargs')
.command('install', 'Install Cloud Functions actions')
.command('uninstall', 'Uninstall Cloud Functions actions')
.command('disable', 'Disable video and image processing')
.command('enable', 'Enable video and image processing')
.command('update', 'Update action code')
.command('register_callback', 'Register the Speech to Text action as callback')
.option('apihost', {
alias: 'a',
describe: 'Cloud Functions API host',
type: 'string'
})
.option('auth', {
alias: 'u',
describe: 'Cloud Functions authorization key',
type: 'string'
})
.option('namespace', {
alias: 'n',
describe: 'Cloud Functions namespace ID',
type: 'string'
})
.count('verbose')
.alias('v', 'verbose')
.help()
.alias('h', 'help')
.alias('', 'help')
.argv;
const VERBOSE_LEVEL = argv.verbose;
function WARN(...args) { VERBOSE_LEVEL >= 0 && console.log.apply(console, args); } // eslint-disable-line
function INFO(...args) { VERBOSE_LEVEL >= 1 && console.log.apply(console, args); } // eslint-disable-line
function DEBUG(...args) { VERBOSE_LEVEL >= 2 && console.log.apply(console, args); } // eslint-disable-line
if (!argv.install &&
!argv.uninstall &&
!argv.disable &&
!argv.enable &&
!argv.update &&
!argv.register_callback) {
WARN('No command specified.');
process.exit(1);
}
// load Cloud Functions CLI configuration if it exists
const wskCliPropsPath = path.join(os.homedir(), '.wskprops');
if (fs.existsSync(wskCliPropsPath)) {
require('dotenv').config({ path: wskCliPropsPath });
WARN('Initialized Cloud Functions host and key from', wskCliPropsPath);
}
if (argv.apihost) {
WARN('Cloud Functions host is set on command line.');
}
if (argv.auth) {
WARN('Cloud Functions authorization key is set on command line.');
}
if (argv.namespace) {
WARN('Cloud Functions namespace ID is set on the command line.');
}
// load configuration options
if (fs.existsSync('local.env')) {
WARN('Loading Dark Vision configuration from local.env');
require('dotenv').config({ path: 'local.env' });
} else {
WARN('No local.env found. Dark Vision configuration will be loaded from environment variables.');
}
if (argv.register_callback) {
registerCallback();
} else {
const authHandler = new itm({
iamApikey: argv.auth || process.env.AUTH
});
const openwhiskOptions = {
apihost: argv.apihost || process.env.APIHOST,
auth_handler: authHandler,
namespace: argv.namespace || process.env.NAMESPACE_ID,
};
const openwhiskClient = openwhisk(openwhiskOptions);
if (argv.install) {
install(openwhiskClient);
} else if (argv.uninstall) {
uninstall(openwhiskClient);
} else if (argv.disable) {
disable(openwhiskClient);
} else if (argv.enable) {
enable(openwhiskClient);
} else if (argv.update) {
update(openwhiskClient);
}
}
function registerCallback() {
WARN('Registering Speech to Text callback...');
waterfall([
(callback) => {
request({
method: 'POST',
auth: {
username: process.env.STT_USERNAME,
password: process.env.STT_PASSWORD,
},
url: `${process.env.STT_URL}/v1/register_callback?callback_url=${process.env.STT_CALLBACK_URL}&user_secret=${process.env.STT_CALLBACK_SECRET}`
}, (err, response, body) => {
if (!err) {
WARN(body);
}
callback(err);
});
}
]);
}
function install(ow) {
WARN('Installing artifacts...');
waterfall([
(callback) => {
call(ow, 'package', 'create', 'vision', callback);
},
(callback) => {
const keyAndValues = {
cloudantUrl: `https://${process.env.CLOUDANT_username}:${process.env.CLOUDANT_password}@${process.env.CLOUDANT_host}`,
cloudantDbName: process.env.CLOUDANT_db,
watsonApiKey: process.env.WATSON_API_KEY,
sttUrl: process.env.STT_URL,
sttUsername: process.env.STT_USERNAME,
sttPassword: process.env.STT_PASSWORD,
sttCallbackSecret: process.env.STT_CALLBACK_SECRET,
sttCallbackUrl: process.env.STT_CALLBACK_URL,
nluUrl: process.env.NLU_URL,
nluUsername: process.env.NLU_USERNAME,
nluPassword: process.env.NLU_PASSWORD,
cosEndpoint: process.env.COS_ENDPOINT || '',
cosApiKey: process.env.COS_API_KEY || '',
cosBucket: process.env.COS_BUCKET || '',
cosInstanceId: process.env.COS_INSTANCE_ID || '',
};
call(ow, 'package', 'update', {
packageName: 'vision',
package: {
parameters: toParameters(keyAndValues)
}
}, callback);
},
(callback) => {
const keyAndValues = {
username: process.env.CLOUDANT_username,
password: process.env.CLOUDANT_password,
host: process.env.CLOUDANT_host
};
call(ow, 'package', 'create', {
packageName: 'vision-cloudant',
package: {
parameters: toParameters(keyAndValues),
binding: {
namespace: 'whisk.system',
name: 'cloudant'
}
}
}, callback);
},
(callback) => {
call(ow, 'trigger', 'create', {
triggerName: 'vision-cloudant-trigger',
trigger: {
annotations: [{
key: 'feed',
value: 'vision-cloudant/changes'
}]
}
}, callback);
},
(callback) => {
call(ow, 'feed', 'create', {
feedName: 'vision-cloudant/changes',
trigger: 'vision-cloudant-trigger',
params: {
dbname: process.env.CLOUDANT_db
}
}, callback);
},
(callback) => {
call(ow, 'action', 'create', {
actionName: 'vision/extractor',
action: {
exec: {
kind: 'blackbox',
code: '',
image: process.env.DOCKER_EXTRACTOR_NAME
},
limits: {
timeout: 300000
}
}
}, callback);
},
makeChangeListenerTask(ow, true),
makeActionTask(ow, 'textanalysis', true),
makeActionTask(ow, 'analysis', true),
makeSpeechToTextTask(ow, true),
(callback) => {
call(ow, 'rule', 'create', {
ruleName: 'vision-rule',
action: 'vision-cloudant-changelistener',
trigger: 'vision-cloudant-trigger'
}, callback);
}
]);
}
function makeSpeechToTextTask(ow, isCreate) {
return makeActionTask(ow, 'speechtotext', isCreate, {
annotations: [{
key: 'web-export',
value: true
},
{
key: 'final',
value: true
}]
});
}
function makeActionTask(ow, actionName, isCreate, options = {}) {
// wsk action create vision/speechtotext --kind nodejs:8 speechtotext/speechtotext.zip
return (callback) => {
const files = {
'package.json': `processing/${actionName}/package.json`,
'lib/cloudantstorage.js': 'web/lib/cloudantstorage.js',
'lib/cloudobjectstorage.js': 'web/lib/cloudobjectstorage.js',
'lib/cloudant-designs.json': 'web/lib/cloudant-designs.json'
};
files[`${actionName}.js`] = `processing/${actionName}/${actionName}.js`;
const actionCode = buildZip(files);
call(ow, 'action', isCreate ? 'create' : 'update', {
actionName: `vision/${actionName}`,
action: {
exec: {
kind: 'nodejs:10',
code: actionCode,
binary: true
},
limits: {
timeout: 300000
},
annotations: options.annotations || []
}
}, callback);
};
}
function makeChangeListenerTask(ow, isCreate) {
// wsk action create vision-cloudant-changelistener --kind nodejs:8 changelistener/changelistener.zip\
// -p cloudantUrl https://$CLOUDANT_username:$CLOUDANT_password@$CLOUDANT_host\
// -p cloudantDbName $CLOUDANT_db
return (callback) => {
const actionCode = buildZip({
'package.json': 'processing/changelistener/package.json',
'changelistener.js': 'processing/changelistener/changelistener.js',
'lib/cloudantstorage.js': 'web/lib/cloudantstorage.js',
'lib/cloudobjectstorage.js': 'web/lib/cloudobjectstorage.js',
'lib/cloudant-designs.json': 'web/lib/cloudant-designs.json'
});
call(ow, 'action', isCreate ? 'create' : 'update', {
actionName: 'vision-cloudant-changelistener',
action: {
exec: {
kind: 'nodejs:10',
code: actionCode,
binary: true
},
parameters: [{
key: 'cloudantUrl',
value: `https://${process.env.CLOUDANT_username}:${process.env.CLOUDANT_password}@${process.env.CLOUDANT_host}`
}, {
key: 'cloudantDbName',
value: process.env.CLOUDANT_db
}]
}
}, callback);
};
}
function uninstall(ow) {
WARN('Uninstalling artifacts...');
waterfall([
callback => call(ow, 'action', 'delete', 'vision/analysis', callback),
callback => call(ow, 'action', 'delete', 'vision/extractor', callback),
callback => call(ow, 'action', 'delete', 'vision/speechtotext', callback),
callback => call(ow, 'action', 'delete', 'vision/textanalysis', callback),
callback => call(ow, 'rule', 'disable', 'vision-rule', callback),
callback => call(ow, 'rule', 'delete', 'vision-rule', callback),
callback => call(ow, 'action', 'delete', 'vision-cloudant-changelistener', callback),
callback => call(ow, 'trigger', 'delete', 'vision-cloudant-trigger', callback),
(callback) => {
call(ow, 'feed', 'delete', {
feedName: 'vision-cloudant/changes',
trigger: 'vision-cloudant-trigger',
params: {
dbname: process.env.CLOUDANT_db
}
}, callback);
},
callback => call(ow, 'package', 'delete', 'vision-cloudant', callback),
callback => call(ow, 'package', 'delete', 'vision', callback)
]);
}
function disable(ow) {
WARN('Disabling video and image processing...');
waterfall([
// wsk rule disable vision-rule
(callback) => {
call(ow, 'rule', 'disable', 'vision-rule', callback);
}
]);
}
function enable(ow) {
WARN('Enabling video and image processing...');
waterfall([
// wsk rule enable vision-rule
(callback) => {
call(ow, 'rule', 'enable', 'vision-rule', callback);
}
]);
}
function update(ow) {
WARN('Updating action code...');
waterfall([
makeChangeListenerTask(ow, false),
makeActionTask(ow, 'textanalysis', false),
makeActionTask(ow, 'analysis', false),
makeSpeechToTextTask(ow, false),
]);
}
// call the Cloud Functions client API dynamically
function call(ow, resource, verb, callOptions, callback) {
let params = callOptions;
if (typeof callOptions === 'string') {
params = {};
params[`${resource}Name`] = callOptions;
}
DEBUG(`[${resource} ${verb} ${params[`${resource}Name`]}]`);
DEBUG(JSON.stringify(params));
ow[`${resource}s`][verb](params).then(() => {
WARN(`${resource} ${verb} ${params[`${resource}Name`]} [OK]`);
callback(null);
}).catch((err) => {
WARN(`${resource} ${verb} ${params[`${resource}Name`]} [KO]`, err.message);
DEBUG(`${resource} ${verb} ${params[`${resource}Name`]} [KO]`, err);
callback(null);
});
}
function toParameters(keyAndValues) {
return Object.keys(keyAndValues).map(key => ({
key,
value: keyAndValues[key]
}));
}
function waterfall(tasks) {
async.waterfall(tasks, (err) => {
if (err) {
DEBUG('Failed', err);
} else {
WARN('Done');
}
});
}
function buildZip(files) {
const actionZip = require('node-zip')();
Object.keys(files).forEach((filename) => {
actionZip.file(filename, fs.readFileSync(files[filename]));
});
return actionZip.generate({ base64: true, compression: 'DEFLATE' });
}