This repository has been archived by the owner on Aug 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopenwhisk-playground.js
173 lines (157 loc) · 5.47 KB
/
openwhisk-playground.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
// Licensed to the Apache Software Foundation (ASF) under one or more contributor
// license agreements; and to You under the Apache License, Version 2.0.
'use strict';
const debug = require('debug')('openwhisk-playground')
const openwhisk = require('openwhisk');
const fs = require("fs");
const path = require("path");
const PropertiesReader = require('properties-reader');
var loadDefaultAuth = function () {
//Expected cat ~/.wskprops
//APIHOST=openwhisk.ng.bluemix.net
//NAMESPACE=
//AUTH=8b6d8615-9
const userDir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
const wskFilePath = path.format({ dir: userDir, base: '.wskprops' });
var wskProperties = PropertiesReader(wskFilePath)
var auth = wskProperties.get('AUTH')
return auth
}
const DEFAULT_OW_API_URL = process.env.OW_API_URL || 'https://openwhisk.ng.bluemix.net/api/v1/'
const DEFAULT_OW_NAMESPACE = process.env.OW_NAMESPACE || '_'
const DEFAULT_OW_API_KEY = process.env.OW_API_KEY
var playground = function (config) {
if (!config) {
config = {}
}
var api_key = config.api_key
if (!api_key) {
if (DEFAULT_OW_API_KEY) {
api_key = DEFAULT_OW_API_KEY
} else {
api_key = loadDefaultAuth()
}
}
var api = config.api
if (!api) {
api = DEFAULT_OW_API_URL
}
var api = config.api
if (!api) {
api = DEFAULT_OW_API_URL
}
var namespace = config.namespace
if (!namespace) {
namespace = DEFAULT_OW_NAMESPACE
}
//console.log('auth=' + auth)
this.owParams = { api: api, api_key: api_key, namespace: namespace }
this.ow = openwhisk(this.owParams)
}
playground.prototype.setInjectLogRetrievalFailure = function (inject) {
this.injectLogRetrievalFailure = inject;
}
playground.prototype.getEndpoint = function () {
return this.owParams.api;
}
playground.prototype.getApiKey = function () {
return this.owParams.api_key;
}
playground.prototype.getParameters = function () {
return this.owParams;
}
function merge(obj1, obj2) {
var obj3 = {};
if (obj1.constructor.name !== 'Object') {
obj3.name = obj1.constructor.name
obj3.message = "" + obj1
} else {
for (var attrName1 in obj1) {
obj3[attrName1] = obj1[attrName1]
}
}
for (var attrName2 in obj2) {
obj3[attrName2] = obj2[attrName2]
}
return obj3;
}
function MyBaseOperationError(message, error) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.error = error;
}
function timeout(duration) {
return new Promise(function (resolve) {
setTimeout(resolve, duration)
})
}
playground.prototype.evaluate = function (params, success, errors) {
const actionName = params.actionName
const kind = params.kind
const input_params = params.input_params
const default_params = params.default_params
const code = params.code
debug('evaluate invoking OW actionName=' + actionName + ' kind=' + kind + ' input_params=' + JSON.stringify(input_params) + ' default_params=' + JSON.stringify(default_params) + ' code=' + code)
//require('request-debug')(ow.actions.get_rp());
const options = {
actionName: actionName
};
options.action = {
parameters: default_params,
exec: { actionName: actionName, code: code, kind: kind }
}
//ow.actions.update({actionName: actionName, action: code, kind: kind, params: default_params }).then(result => {
const ow = this.ow
const pipelineResults = {}
pipelineResults.listOfResults = []
var activationId = null
ow.actions.update(options)
.then(result => {
pipelineResults.listOfResults.push(result)
debug("evaluate updated result=" + JSON.stringify(result));
return ow.actions.invoke({ actionName: actionName, params: input_params, blocking: true })
})
.then(invoke_result => {
pipelineResults.listOfResults.push(invoke_result)
debug("evaluate invoked invoke_result=" + JSON.stringify(invoke_result))
activationId = invoke_result.activationId
debug("evaluate activationId=" + activationId);
if (this.injectLogRetrievalFailure) {
throw new MyBaseOperationError('failed', null)
}
return ow.activations.get({ activation: activationId })
})
// .then(log_result => {
//pipelineResults.list.push(log_result)
// console.log('evaluate activati o nget log_result=' + JSON.stringify(log_result))
// return log_result
// })
.catch(err => {
if (activationId) {
const waitMs = 1000
debug('evaluate retry log retrieval after ' + waitMs + 'ms err=' + JSON.stringify(err))
return timeout(waitMs).then(function () {
debug('evaluate skipping err=' + err + ' to get logs for ' + activationId)
return ow.activations.get({ activation: activationId })
})
} else {
debug('evaluate re-throw err=' + err)
throw err
}
})
.then(result => {
//console.log('evaluate pipelineResults=' + JSON.stringify(pipelineResults, null, 2))
// console.log('evaluate final result=' + JSON.stringify(result, null, 2))
var mergedResult = merge(result, pipelineResults)
debug('evaluate final success mergedResult=' + JSON.stringify(mergedResult, null, 2))
success(mergedResult)
})
.catch(err => {
console.log('evaluate final err=' + err) //pipelineResults.list.push(err)
var mergedResult = merge(err, pipelineResults)
debug('evaluate final errors mergedResult=' + JSON.stringify(mergedResult, null, 2))
errors(mergedResult)
})
}
module.exports = playground