/
export.js
224 lines (205 loc) · 7.21 KB
/
export.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
/*
Copyright 2019 Adobe Inc. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { flags } = require('@oclif/command')
const fs = require('fs')
const yaml = require('js-yaml')
const path = require('path')
class DeployExport extends RuntimeBaseCommand {
async run () {
const { flags } = this.parse(DeployExport)
try {
const ow = await this.wsk()
const projectEntities = await findProjectEntities(ow, flags.projectname)
const manifest = flags.manifest
const fileDirectory = path.dirname(manifest)
const projectJSON = await createProjectJSON(projectEntities, flags.projectname, ow, fileDirectory)
const yamlObject = { project: projectJSON }
fs.writeFileSync(manifest, yaml.safeDump(yamlObject))
} catch (err) {
this.handleError('Failed to export', err)
}
}
}
async function findProjectEntities (ow, projectName) {
const packages = []
const actions = []
const triggers = []
const rules = []
const options = {}
const resultSync = await ow.packages.list(options)
for (const pkg of resultSync) {
if (pkg.annotations.length > 0) {
const whiskManaged = pkg.annotations.find(elem => elem.key === 'whisk-managed')
if (whiskManaged !== undefined && whiskManaged.value.projectName === projectName) {
packages.push(pkg)
}
}
}
for (const pkg of packages) {
const getPackage = await ow.packages.get(pkg)
for (const action of getPackage.actions) {
const actionName = `${getPackage.name}/${action.name}`
actions.push(actionName)
}
}
const resultTriggerList = await ow.triggers.list()
for (const trigger of resultTriggerList) {
if (trigger.annotations.length > 0) {
const whiskManaged = trigger.annotations.find(elem => elem.key === 'whisk-managed')
if (whiskManaged !== undefined && whiskManaged.value.projectName === projectName) {
triggers.push(trigger.name)
}
}
}
// if no trigger exists with the projectName -> look in rules
const resultRules = await ow.rules.list()
for (const rule of resultRules) {
if (rule.annotations.length > 0) {
const whiskManaged = rule.annotations.find(elem => elem.key === 'whisk-managed')
if (whiskManaged !== undefined && whiskManaged.value.projectName === projectName) {
rules.push(rule)
}
}
}
const entities = {
packages: packages,
actions: actions,
triggers: triggers,
rules: rules
}
return entities
}
async function createProjectJSON (entities, projectname, ow, fileDirectory) {
const project = { name: projectname, packages: {} }
for (const pkg of entities.packages) {
const annotations = returninputsfromKeyValue(pkg.annotations)
project['packages'][pkg.name] = {
version: pkg.version,
namespace: pkg.namespace,
annotations: annotations,
actions: {},
triggers: {},
sequences: {},
rules: {}
}
if (pkg.publish) {
project['packages'][pkg.name]['public'] = pkg.publish
}
}
const filesObject = []
let packageName
for (const action of entities.actions) {
const getAction = await ow.actions.get(action)
packageName = getAction.namespace.split('/').pop()
const actionName = getAction.name
if (getAction.exec.kind !== 'sequence') {
const binaryFlag = getAction.exec.binary || false
let functionValue = `${action}.js`
if (binaryFlag) {
functionValue = `${action}.zip`
}
const limits = {
timeout: getAction.limits.timeout,
memorySize: getAction.limits.memory,
logSize: getAction.limits.logs
}
project['packages'][packageName]['actions'][actionName] = {
version: getAction.version,
namespace: getAction.namespace,
annotations: returninputsfromKeyValue(getAction.annotations),
inputs: returninputsfromKeyValue(getAction.parameters),
function: functionValue,
runtime: getAction.exec.kind,
main: getAction.exec.main || '',
limits: limits
}
const obj = { name: functionValue, code: getAction.exec.code }
filesObject.push(obj)
} else {
let sequenceString = ''
sequenceString = getAction.parameters[0].value
sequenceString = sequenceString.map(action => {
return action.split('/').pop()
})
project['packages'][packageName]['sequences'][actionName] = {
actions: sequenceString.join()
}
}
}
for (const trigger of entities.triggers) {
const getTrigger = await ow.triggers.get(trigger)
project['packages'][packageName]['triggers'][trigger] = {
namespace: getTrigger.namespace,
inputs: returninputsfromKeyValue(getTrigger.parameters),
annotations: returninputsfromKeyValue(getTrigger.annotations)
}
for (const annotation of getTrigger.annotations) {
if (annotation.key === 'feed') {
project['packages'][packageName]['triggers'][trigger]['feed'] = annotation.value
break
}
}
}
for (const rule of entities.rules) {
project['packages'][packageName]['rules'][rule.name] = {
action: rule.action.name,
annotations: returninputsfromKeyValue(rule.annotations),
trigger: rule.trigger.name
}
}
writeFiles(filesObject, fileDirectory)
return project
}
function writeFiles (fileObj, fileDirectory) {
for (const file of fileObj) {
const packageName = path.dirname(file.name)
const fileDirectorywithPackage = `${fileDirectory}/${packageName}`
if (!fs.existsSync(fileDirectorywithPackage)) {
fs.mkdirSync(fileDirectorywithPackage, { recursive: true })
}
file.name = `${fileDirectory}/${file.name}`
if (file.name.endsWith('.zip')) {
const data = Buffer.from(file.code, 'base64')
fs.writeFileSync(file.name, data, 'buffer')
} else {
fs.writeFileSync(file.name, file.code)
}
}
}
function returninputsfromKeyValue (inputs) {
const inputObj = {}
for (const input of inputs) {
// whisk managed annotation not exported to yaml file
// and feed annotation becomes feed property in createProjectJSON
if (input.key !== 'whisk-managed' && input.key !== 'feed') {
inputObj[input.key] = input.value
}
}
return inputObj
}
DeployExport.flags = {
...RuntimeBaseCommand.flags,
manifest: flags.string({
char: 'm',
description: 'the manifest file location', // help description for flag
required: true
}),
projectname: flags.string({
description: 'the name of the project to be undeployed', // help description for flag
required: true
})
}
DeployExport.description = 'Exports managed project assets from Runtime to manifest and function files'
DeployExport.aliases = [
'rt:deploy:export'
]
module.exports = DeployExport