forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
119 lines (100 loc) · 3.45 KB
/
Copy pathdeploy.js
File metadata and controls
119 lines (100 loc) · 3.45 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
"use strict";
const path = require("path");
const fs = require("fs-extra");
const chalk = require("chalk");
const { logger } = require("@serverless-stack/core");
const paths = require("./util/paths");
const { synth, parallelDeploy } = require("./util/cdkHelpers");
module.exports = async function (argv, config, cliInfo) {
logger.info(chalk.grey("Deploying " + (argv.stack ? argv.stack : "stacks")));
// Build
await synth(cliInfo.cdkOptions);
// Loop until deployment is complete
let stackStates;
let isCompleted;
do {
// Get CFN events before update
const prevEventCount = stackStates ? getEventCount(stackStates) : 0;
// Update deploy status
const response = await parallelDeploy({
...cliInfo.cdkOptions,
stackName: argv.stack,
cdkOutputPath: path.join(paths.appPath, paths.appBuildDir, "cdk.out"),
}, stackStates);
stackStates = response.stackStates;
isCompleted = response.isCompleted;
// Wait for 5 seconds
if (!response.isCompleted) {
// Get CFN events after update. If events count did not change, we need to print out a
// message to let users know we are still checking.
const currEventCount = getEventCount(stackStates);
if (currEventCount === prevEventCount) {
logger.info("Checking deploy status...");
}
await new Promise((resolve) => setTimeout(resolve, 5000));
}
} while (!isCompleted);
// This is native CDK option. According to CDK documentation:
// If an outputs file has been specified, create the file path and write stack outputs to it once.
// Outputs are written after all stacks have been deployed. If a stack deployment fails,
// all of the outputs from successfully deployed stacks before the failure will still be written.
if (argv.outputsFile) {
writeOutputsFile(stackStates, path.join(paths.appPath, argv.outputsFile));
}
// Print deploy result
printResults(stackStates);
return stackStates.map((stackState) => ({
name: stackState.name,
status: stackState.status,
outputs: stackState.outputs,
}));
};
function getEventCount(stackStates) {
return stackStates.reduce(
(acc, stackState) => acc + (stackState.events || []).length,
0
);
}
function printResults(stackStates) {
stackStates.forEach(({ name, status, errorMessage, outputs, exports }) => {
logger.info(`\nStack ${name}`);
logger.info(` Status: ${formatStackStatus(status)}`);
if (errorMessage) {
logger.info(` Error: ${errorMessage}`);
}
if (Object.keys(outputs || {}).length > 0) {
logger.info(" Outputs:");
Object.keys(outputs).forEach((name) =>
logger.info(` ${name}: ${outputs[name]}`)
);
}
if (Object.keys(exports || {}).length > 0) {
logger.info(" Exports:");
Object.keys(exports).forEach((name) =>
logger.info(` ${name}: ${exports[name]}`)
);
}
});
logger.info("");
}
async function writeOutputsFile(stackStates, outputsFileWithPath) {
const stackOutputs = stackStates.reduce((acc, { name, outputs }) => {
if (Object.keys(outputs || {}).length > 0) {
return {...acc, [name]: outputs };
}
return acc;
}, {});
fs.ensureFileSync(outputsFileWithPath);
await fs.writeJson(outputsFileWithPath, stackOutputs, {
spaces: 2,
encoding: 'utf8',
});
}
function formatStackStatus(status) {
return {
failed: "failed",
succeeded: "deployed",
unchanged: "no changes",
skipped: "not deployed",
}[status];
}