Skip to content

Commit

Permalink
De-duplicate environments before bootstrap (#625)
Browse files Browse the repository at this point in the history
If not de-duplicating, multiple-stack applications that involve several
stacks in the same account and region will attempt to bootstrap in
parallel, and fail miserably. De-duplicating should make this... just
work.
  • Loading branch information
RomainMuller authored and rix0rrr committed Sep 3, 2018
1 parent 1e627e2 commit 6b793e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/@aws-cdk/cdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ async function initCommandLine() {
environmentGlobs = [ '**' ]; // default to ALL
}
const stackInfos = await selectStacks();
const availableEnvironments = stackInfos.map(stack => stack.environment)
.filter(env => env !== undefined);
const availableEnvironments = distinct(stackInfos.map(stack => stack.environment)
.filter(env => env !== undefined) as cxapi.Environment[]);
const environments = availableEnvironments.filter(env => environmentGlobs.find(glob => minimatch(env!.name, glob)));
if (environments.length === 0) {
const globs = JSON.stringify(environmentGlobs);
Expand All @@ -284,6 +284,24 @@ async function initCommandLine() {
throw e;
}
}));

/**
* De-duplicates a list of environments, such that a given account and region is only represented exactly once
* in the result.
*
* @param envs the possibly full-of-duplicates list of environments.
*
* @return a de-duplicated list of environments.
*/
function distinct(envs: cxapi.Environment[]): cxapi.Environment[] {
const unique: { [id: string]: cxapi.Environment } = {};
for (const env of envs) {
const id = `${env.account || 'default'}/${env.region || 'default'}`;
if (id in unique) { continue; }
unique[id] = env;
}
return Object.values(unique);
}
}

/**
Expand Down

0 comments on commit 6b793e9

Please sign in to comment.