/
cloud-assembly.ts
387 lines (329 loc) · 11.6 KB
/
cloud-assembly.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as semver from 'semver';
import { error, print, warning } from '../../logging';
import { flatten } from '../../util';
import { versionNumber } from '../../version';
// namespace object imports won't work in the bundle for function exports
// eslint-disable-next-line @typescript-eslint/no-require-imports
const minimatch = require('minimatch');
export enum DefaultSelection {
/**
* Returns an empty selection in case there are no selectors.
*/
None = 'none',
/**
* If the app includes a single stack, returns it. Otherwise throws an exception.
* This behavior is used by "deploy".
*/
OnlySingle = 'single',
/**
* Returns all stacks in the main (top level) assembly only.
*/
MainAssembly = 'main',
/**
* If no selectors are provided, returns all stacks in the app,
* including stacks inside nested assemblies.
*/
AllStacks = 'all',
}
export interface SelectStacksOptions {
/**
* Extend the selection to upstread/downstream stacks
* @default ExtendedStackSelection.None only select the specified stacks.
*/
extend?: ExtendedStackSelection;
/**
* The behavior if if no selectors are privided.
*/
defaultBehavior: DefaultSelection;
}
/**
* When selecting stacks, what other stacks to include because of dependencies
*/
export enum ExtendedStackSelection {
/**
* Don't select any extra stacks
*/
None,
/**
* Include stacks that this stack depends on
*/
Upstream,
/**
* Include stacks that depend on this stack
*/
Downstream
}
/**
* A specification of which stacks should be selected
*/
export interface StackSelector {
/**
* Whether all stacks at the top level assembly should
* be selected and nothing else
*/
allTopLevel?: boolean,
/**
* A list of patterns to match the stack hierarchical ids
*/
patterns: string[],
}
/**
* A single Cloud Assembly and the operations we do on it to deploy the artifacts inside
*/
export class CloudAssembly {
/**
* The directory this CloudAssembly was read from
*/
public readonly directory: string;
constructor(public readonly assembly: cxapi.CloudAssembly) {
this.directory = assembly.directory;
}
public async selectStacks(selector: StackSelector, options: SelectStacksOptions): Promise<StackCollection> {
const asm = this.assembly;
const topLevelStacks = asm.stacks;
const stacks = semver.major(asm.version) < 10 ? asm.stacks : asm.stacksRecursively;
const allTopLevel = selector.allTopLevel ?? false;
const patterns = sanitizePatterns(selector.patterns);
if (stacks.length === 0) {
throw new Error('This app contains no stacks');
}
if (allTopLevel) {
return this.selectTopLevelStacks(stacks, topLevelStacks, options.extend);
} else if (patterns.length > 0) {
return this.selectMatchingStacks(stacks, patterns, options.extend);
} else {
return this.selectDefaultStacks(stacks, topLevelStacks, options.defaultBehavior);
}
}
private selectTopLevelStacks(stacks: cxapi.CloudFormationStackArtifact[],
topLevelStacks: cxapi.CloudFormationStackArtifact[],
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {
if (topLevelStacks.length > 0) {
return this.extendStacks(topLevelStacks, stacks, extend);
} else {
throw new Error('No stack found in the main cloud assembly. Use "list" to print manifest');
}
}
private selectMatchingStacks(stacks: cxapi.CloudFormationStackArtifact[],
patterns: string[],
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {
// cli tests use this to ensure tests do not depend on legacy behavior
// (otherwise they will fail in v2)
const disableLegacy = process.env.CXAPI_DISABLE_SELECT_BY_ID === '1';
const matchingPattern = (pattern: string) => (stack: cxapi.CloudFormationStackArtifact) => {
if (minimatch(stack.hierarchicalId, pattern)) {
return true;
} else if (!disableLegacy && stack.id === pattern && semver.major(versionNumber()) < 2) {
warning('Selecting stack by identifier "%s". This identifier is deprecated and will be removed in v2. Please use "%s" instead.', chalk.bold(stack.id), chalk.bold(stack.hierarchicalId));
warning('Run "cdk ls" to see a list of all stack identifiers');
return true;
}
return false;
};
const matchedStacks = flatten(patterns.map(pattern => stacks.filter(matchingPattern(pattern))));
return this.extendStacks(matchedStacks, stacks, extend);
}
private selectDefaultStacks(stacks: cxapi.CloudFormationStackArtifact[],
topLevelStacks: cxapi.CloudFormationStackArtifact[],
defaultSelection: DefaultSelection) {
switch (defaultSelection) {
case DefaultSelection.MainAssembly:
return new StackCollection(this, topLevelStacks);
case DefaultSelection.AllStacks:
return new StackCollection(this, stacks);
case DefaultSelection.None:
return new StackCollection(this, []);
case DefaultSelection.OnlySingle:
if (topLevelStacks.length === 1) {
return new StackCollection(this, topLevelStacks);
} else {
throw new Error('Since this app includes more than a single stack, specify which stacks to use (wildcards are supported) or specify `--all`\n' +
`Stacks: ${stacks.map(x => x.hierarchicalId).join(' · ')}`);
}
default:
throw new Error(`invalid default behavior: ${defaultSelection}`);
}
}
private extendStacks(matched: cxapi.CloudFormationStackArtifact[],
all: cxapi.CloudFormationStackArtifact[],
extend: ExtendedStackSelection = ExtendedStackSelection.None) {
const allStacks = new Map<string, cxapi.CloudFormationStackArtifact>();
for (const stack of all) {
allStacks.set(stack.hierarchicalId, stack);
}
const index = indexByHierarchicalId(matched);
switch (extend) {
case ExtendedStackSelection.Downstream:
includeDownstreamStacks(index, allStacks);
break;
case ExtendedStackSelection.Upstream:
includeUpstreamStacks(index, allStacks);
break;
}
// Filter original array because it is in the right order
const selectedList = all.filter(s => index.has(s.hierarchicalId));
return new StackCollection(this, selectedList);
}
/**
* Select a single stack by its ID
*/
public stackById(stackId: string) {
return new StackCollection(this, [this.assembly.getStackArtifact(stackId)]);
}
}
/**
* A collection of stacks and related artifacts
*
* In practice, not all artifacts in the CloudAssembly are created equal;
* stacks can be selected independently, but other artifacts such as asset
* bundles cannot.
*/
export class StackCollection {
constructor(public readonly assembly: CloudAssembly, public readonly stackArtifacts: cxapi.CloudFormationStackArtifact[]) {
}
public get stackCount() {
return this.stackArtifacts.length;
}
public get firstStack() {
if (this.stackCount < 1) {
throw new Error('StackCollection contains no stack artifacts (trying to access the first one)');
}
return this.stackArtifacts[0];
}
public get stackIds(): string[] {
return this.stackArtifacts.map(s => s.id);
}
public reversed() {
const arts = [...this.stackArtifacts];
arts.reverse();
return new StackCollection(this.assembly, arts);
}
public filter(predicate: (art: cxapi.CloudFormationStackArtifact) => boolean): StackCollection {
return new StackCollection(this.assembly, this.stackArtifacts.filter(predicate));
}
public concat(other: StackCollection): StackCollection {
return new StackCollection(this.assembly, this.stackArtifacts.concat(other.stackArtifacts));
}
/**
* Extracts 'aws:cdk:warning|info|error' metadata entries from the stack synthesis
*/
public processMetadataMessages(options: MetadataMessageOptions = {}) {
let warnings = false;
let errors = false;
for (const stack of this.stackArtifacts) {
for (const message of stack.messages) {
switch (message.level) {
case cxapi.SynthesisMessageLevel.WARNING:
warnings = true;
printMessage(warning, 'Warning', message.id, message.entry);
break;
case cxapi.SynthesisMessageLevel.ERROR:
errors = true;
printMessage(error, 'Error', message.id, message.entry);
break;
case cxapi.SynthesisMessageLevel.INFO:
printMessage(print, 'Info', message.id, message.entry);
break;
}
}
}
if (errors && !options.ignoreErrors) {
throw new Error('Found errors');
}
if (options.strict && warnings) {
throw new Error('Found warnings (--strict mode)');
}
function printMessage(logFn: (s: string) => void, prefix: string, id: string, entry: cxapi.MetadataEntry) {
logFn(`[${prefix} at ${id}] ${entry.data}`);
if (options.verbose && entry.trace) {
logFn(` ${entry.trace.join('\n ')}`);
}
}
}
}
export interface MetadataMessageOptions {
/**
* Whether to be verbose
*
* @default false
*/
verbose?: boolean;
/**
* Don't stop on error metadata
*
* @default false
*/
ignoreErrors?: boolean;
/**
* Treat warnings in metadata as errors
*
* @default false
*/
strict?: boolean;
}
function indexByHierarchicalId(stacks: cxapi.CloudFormationStackArtifact[]): Map<string, cxapi.CloudFormationStackArtifact> {
const result = new Map<string, cxapi.CloudFormationStackArtifact>();
for (const stack of stacks) {
result.set(stack.hierarchicalId, stack);
}
return result;
}
/**
* Calculate the transitive closure of stack dependents.
*
* Modifies `selectedStacks` in-place.
*/
function includeDownstreamStacks(
selectedStacks: Map<string, cxapi.CloudFormationStackArtifact>,
allStacks: Map<string, cxapi.CloudFormationStackArtifact>) {
const added = new Array<string>();
let madeProgress;
do {
madeProgress = false;
for (const [id, stack] of allStacks) {
// Select this stack if it's not selected yet AND it depends on a stack that's in the selected set
if (!selectedStacks.has(id) && (stack.dependencies || []).some(dep => selectedStacks.has(dep.id))) {
selectedStacks.set(id, stack);
added.push(id);
madeProgress = true;
}
}
} while (madeProgress);
if (added.length > 0) {
print('Including depending stacks: %s', chalk.bold(added.join(', ')));
}
}
/**
* Calculate the transitive closure of stack dependencies.
*
* Modifies `selectedStacks` in-place.
*/
function includeUpstreamStacks(
selectedStacks: Map<string, cxapi.CloudFormationStackArtifact>,
allStacks: Map<string, cxapi.CloudFormationStackArtifact>) {
const added = new Array<string>();
let madeProgress = true;
while (madeProgress) {
madeProgress = false;
for (const stack of selectedStacks.values()) {
// Select an additional stack if it's not selected yet and a dependency of a selected stack (and exists, obviously)
for (const dependencyId of stack.dependencies.map(x => x.manifest.displayName ?? x.id)) {
if (!selectedStacks.has(dependencyId) && allStacks.has(dependencyId)) {
added.push(dependencyId);
selectedStacks.set(dependencyId, allStacks.get(dependencyId)!);
madeProgress = true;
}
}
}
}
if (added.length > 0) {
print('Including dependency stacks: %s', chalk.bold(added.join(', ')));
}
}
function sanitizePatterns(patterns: string[]): string[] {
let sanitized = patterns.filter(s => s != null); // filter null/undefined
sanitized = [...new Set(sanitized)]; // make them unique
return sanitized;
}