-
Notifications
You must be signed in to change notification settings - Fork 35
/
undeploy.js
200 lines (179 loc) · 6.87 KB
/
undeploy.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
/*
Copyright 2019 Adobe. 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 ora = require('ora')
const chalk = require('chalk')
const { Flags } = require('@oclif/core')
const BaseCommand = require('../../BaseCommand')
const webLib = require('@adobe/aio-lib-web')
const { runInProcess, buildExtensionPointPayloadWoMetadata, getCliInfo } = require('../../lib/app-helper')
const rtLib = require('@adobe/aio-lib-runtime')
const { sendAuditLogs, getAuditLogEvent } = require('../../lib/audit-logger')
class Undeploy extends BaseCommand {
async run () {
// cli input
const { flags } = await this.parse(Undeploy)
const undeployConfigs = await this.getAppExtConfigs(flags)
let libConsoleCLI
if (flags.unpublish) {
// force login at beginning (if required)
libConsoleCLI = await this.getLibConsoleCLI()
}
// 1. undeploy actions and web assets for each extension
const keys = Object.keys(undeployConfigs)
const values = Object.values(undeployConfigs)
if (
(!flags.unpublish && !flags['web-assets'] && !flags.actions)
) {
this.error('Nothing to be done 🚫')
}
const spinner = ora()
try {
const aioConfig = (await this.getFullConfig()).aio
const cliDetails = await getCliInfo()
const logEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_UNDEPLOY')
// 1.1. send audit log event for successful undeploy
if (logEvent) {
await sendAuditLogs(cliDetails.accessToken, logEvent, cliDetails.env)
} else {
this.log(chalk.red(chalk.bold('Warning: No valid config data found to send audit log event for deployment.')))
}
for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
const v = values[i]
await this.undeployOneExt(k, v, flags, spinner)
const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED')
if (assetUndeployLogEvent) {
await sendAuditLogs(cliDetails.accessToken, assetUndeployLogEvent, cliDetails.env)
}
}
// 1.2. unpublish extension manifest
if (flags.unpublish && !(keys.length === 1 && keys[0] === 'application')) {
const payload = await this.unpublishExtensionPoints(libConsoleCLI, undeployConfigs, aioConfig, flags['force-unpublish'])
this.log(chalk.blue(chalk.bold(`New Extension Point(s) in Workspace '${aioConfig.project.workspace.name}': '${Object.keys(payload.endpoints)}'`)))
} else {
this.log('skipping unpublish phase...')
}
} catch (error) {
spinner.stop()
// delegate to top handler
throw error
}
// final message
this.log(chalk.green(chalk.bold('Undeploy done !')))
}
async undeployOneExt (extName, config, flags, spinner) {
const onProgress = !flags.verbose
? info => {
spinner.text = info
}
: info => {
spinner.info(chalk.dim(`${info}`))
spinner.start()
}
// undeploy
try {
await runInProcess(config.hooks['pre-app-undeploy'], config)
const hookResults = await this.config.runHook('pre-undeploy-event-reg', { appConfig: config })
if (hookResults?.failures?.length > 0) {
// output should be "Error : <plugin-name> : <error-message>\n" for each failure
this.error(hookResults.failures.map(f => `${f.plugin.name} : ${f.error.message}`).join('\nError: '), { exit: 1 })
}
} catch (err) {
this.log(err)
}
if (flags.actions) {
if (config.app.hasBackend) {
try {
const script = await runInProcess(config.hooks['undeploy-actions'], config)
if (!script) {
await rtLib.undeployActions(config)
}
spinner.succeed(chalk.green(`Un-Deploying actions for ${extName}`))
} catch (err) {
spinner.fail(chalk.green(`Un-Deploying actions for ${extName}`))
throw err
}
} else {
this.log('no manifest file, skipping action undeploy')
}
}
if (flags['web-assets']) {
if (config.app.hasFrontend) {
try {
const script = await runInProcess(config.hooks['undeploy-static'], config)
if (!script) {
await webLib.undeployWeb(config, onProgress)
}
spinner.succeed(chalk.green(`Un-Deploying web assets for ${extName}`))
} catch (err) {
spinner.fail(chalk.green(`Un-Deploying web assets for ${extName}`))
throw err
}
} else {
this.log('no frontend, skipping frontend undeploy')
}
}
try {
await runInProcess(config.hooks['post-app-undeploy'], config)
} catch (err) {
this.log(err)
}
}
async unpublishExtensionPoints (libConsoleCLI, deployConfigs, aioConfig, force) {
const payload = buildExtensionPointPayloadWoMetadata(deployConfigs)
let res
if (force) {
// publish and overwrite any previous published endpoints (delete them)
res = await libConsoleCLI.updateExtensionPoints(aioConfig.project.org, aioConfig.project, aioConfig.project.workspace, { endpoints: {} })
return res
}
// publish without overwritting, meaning partial publish (for a subset of ext points) are supported
res = await libConsoleCLI.removeSelectedExtensionPoints(aioConfig.project.org, aioConfig.project, aioConfig.project.workspace, payload)
return res
}
}
Undeploy.description = `Undeploys an Adobe I/O App
`
Undeploy.flags = {
...BaseCommand.flags,
actions: Flags.boolean({
description: '[default: true] Undeploy actions if any',
default: true,
allowNo: true
}),
events: Flags.boolean({
description: '[default: true] Undeploy (unregister) events if any',
default: true,
allowNo: true
}),
'web-assets': Flags.boolean({
description: '[default: true] Undeploy web-assets if any',
default: true,
allowNo: true
}),
extension: Flags.string({
description: 'Undeploy only a specific extension, the flags can be specified multiple times',
char: 'e',
multiple: true
}),
unpublish: Flags.boolean({
description: '[default: true] Unpublish selected extension(s) from Exchange',
allowNo: true,
default: true
}),
'force-unpublish': Flags.boolean({
description: 'Force unpublish extension(s) from Exchange, will delete all extension points',
default: false,
exclusive: ['unpublish'] // unpublish is excluded
})
}
Undeploy.args = {}
module.exports = Undeploy