This repository was archived by the owner on Apr 18, 2024. It is now read-only.
forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.js
More file actions
121 lines (102 loc) · 3.25 KB
/
Copy pathremove.js
File metadata and controls
121 lines (102 loc) · 3.25 KB
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
"use strict";
const path = require("path");
const chalk = require("chalk");
const { logger } = require("@serverless-stack/core");
const paths = require("./util/paths");
const {
synth,
writeConfig,
destroyInit,
destroyPoll,
} = require("./util/cdkHelpers");
const { STACK_DESTROY_STATUS } = require("@serverless-stack/core");
module.exports = async function (argv, config, cliInfo) {
// Skip building functions on remove
await writeConfig({
...config,
skipBuild: true,
});
// Normalize stack name
const stackPrefix = `${config.stage}-${config.name}-`;
let stackName = argv.stack;
if (stackName) {
stackName = stackName.startsWith(stackPrefix)
? stackName
: `${stackPrefix}${stackName}`;
}
////////////////////////
// Remove debug stack //
////////////////////////
if (!stackName) {
const debugStackName = `${stackPrefix}debug-stack`;
logger.info(chalk.grey(`Removing ${debugStackName} stack`));
// Note: When removing the debug stack, the current working directory is user's app.
// Setting the current working directory to debug stack cdk app directory to allow
// Lambda Function construct be able to reference code with relative path.
process.chdir(path.join(paths.ownPath, "assets", "debug-stack"));
try {
const appBuildLibPath = path.join(paths.appBuildPath, "lib");
await removeApp({
...cliInfo.cdkOptions,
app: `node bin/index.js ${debugStackName} ${config.stage} ${config.region} ${paths.appPath} ${appBuildLibPath}`,
output: "cdk.out",
});
} finally {
// Note: Restore working directory
process.chdir(paths.appPath);
}
}
////////////////
// Remove app //
////////////////
logger.info(chalk.grey("Removing " + (stackName ? stackName : "stacks")));
const stackStates = await removeApp(cliInfo.cdkOptions, stackName);
// Print remove result
printResults(stackStates);
// Check all stacks deployed successfully
if (
stackStates.some(({ status }) => status === STACK_DESTROY_STATUS.FAILED)
) {
throw new Error(`Failed to remove the app`);
}
return stackStates.map((stackState) => ({
name: stackState.name,
status: stackState.status,
}));
};
async function removeApp(cdkOptions, stackName) {
// Build
await synth(cdkOptions);
// Initialize destroy
let { stackStates, isCompleted } = await destroyInit(cdkOptions, stackName);
// Loop until remove is complete
do {
// Update remove status
const response = await destroyPoll(cdkOptions, stackStates);
stackStates = response.stackStates;
isCompleted = response.isCompleted;
// Wait for 5 seconds
if (!isCompleted) {
logger.info("Checking remove status...");
await new Promise((resolve) => setTimeout(resolve, 5000));
}
} while (!isCompleted);
return stackStates;
}
function printResults(stackStates) {
stackStates.forEach(({ name, status, errorMessage }) => {
logger.info(`\nStack ${name}`);
logger.info(` Status: ${formatStackStatus(status)}`);
if (errorMessage) {
logger.info(` Error: ${errorMessage}`);
}
});
logger.info("");
}
function formatStackStatus(status) {
return {
succeeded: "removed",
failed: "failed",
skipped: "not removed",
}[status];
}