-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-apply.ts
158 lines (137 loc) · 5.46 KB
/
bootstrap-apply.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
import * as cmdTs from 'cmd-ts';
import { parse as parseYaml } from 'yaml';
import { BaseCliCommand } from './cli-utils';
import type { CommandSet, EnvironmentVariables } from './exec-utils';
import { commandSetToString, execPromise, executeCommand } from './exec-utils';
import type { CdkManager } from './manager';
export const bootstrapApplyCliArgs = {
account: cmdTs.option({
type: cmdTs.optional(cmdTs.string),
long: 'account',
}),
region: cmdTs.option({
type: cmdTs.optional(cmdTs.string),
long: 'region',
}),
noDefaultProfiles: cmdTs.flag({
type: cmdTs.boolean,
long: 'no-default-profiles',
}),
apply: cmdTs.flag({
type: cmdTs.boolean,
long: 'apply',
}),
};
export interface BootstrapApplyCliCommandArgs {
account: string | undefined;
region: string | undefined;
apply: boolean;
noDefaultProfiles: boolean;
}
export class BootstrapApplyCliCommand<A, M extends CdkManager<A>> extends BaseCliCommand<A, M> {
getBootstrapCommands(accountName: string, region: string | undefined, noDefaultProfiles: boolean, currentCdkProvidedBootstrapVersion: number): Array<CommandSet> {
const account = this.manager.getAccount(accountName);
if (!account.cdkBootstrap) {
return [];
}
if (!account.cdkBootstrap.enabled) {
return [];
}
const bootstrapConfig = account.cdkBootstrap;
if (region && !bootstrapConfig.regions.includes(region)) {
console.error(`Skipping account ${accountName} for selected region ${region}, as not enabled`);
return [];
}
if (currentCdkProvidedBootstrapVersion < bootstrapConfig.minimumVersion) {
console.error(`Skipping account ${accountName} as it requires CDK bootstrap version ${bootstrapConfig.minimumVersion}, but version ${currentCdkProvidedBootstrapVersion} is required`);
return [];
}
const selectedRegions = region ? [region] : bootstrapConfig.regions;
const trustedAccountNumbers = [];
for (const trustedAccountName of bootstrapConfig.trustedAccountNames ?? []) {
trustedAccountNumbers.push(this.manager.getAccount(trustedAccountName).accountNumber);
}
const trustFlags = [];
for (const trustedAccountNumber of trustedAccountNumbers) {
trustFlags.push(...['--trust', trustedAccountNumber]);
trustFlags.push(...['--trust-for-lookup', trustedAccountNumber]);
}
const commands: Array<CommandSet> = [];
const env: EnvironmentVariables = {
NO_SYNTH: 'yes',
};
if (!noDefaultProfiles) {
env['AWS_PROFILE'] = this.manager.getDefaultBootstrapDeploymentProfile(account);
}
for (const selectedRegion of selectedRegions) {
const commandParts = [
'npm',
'run',
'--',
'cdk',
'bootstrap',
`aws://${account.accountNumber}/${selectedRegion}`,
...trustFlags,
'--cloudformation-execution-policies',
'arn:aws:iam::aws:policy/AdministratorAccess',
];
commands.push([
{
command: commandParts.join(' '),
env: env,
},
]);
}
return commands;
}
async getCdkBootstrapVersion(): Promise<number> {
const { stdout } = await execPromise('npm run --silent -- cdk bootstrap --show-template', {
env: {
...process.env,
NO_SYNTH: 'yes',
},
});
const parsed = parseYaml(stdout);
const version = parsed?.Resources?.CdkBootstrapVersion?.Properties?.Value;
if (!version) {
throw new Error('Unable to find a version number from bootstrap template');
}
return parseInt(version, 10);
}
async handler(args: BootstrapApplyCliCommandArgs): Promise<void> {
const { account, region, apply, noDefaultProfiles } = args;
const selectedAccountNames = account ? [account] : this.manager.getAccountNames();
const commands = [];
const currentCdkProvidedBootstrapVersion = await this.getCdkBootstrapVersion();
for (const selectedAccountName of selectedAccountNames) {
commands.push(...this.getBootstrapCommands(selectedAccountName, region, noDefaultProfiles, currentCdkProvidedBootstrapVersion));
}
if (apply) {
console.error('Bootstrapping:');
for (const command of commands) {
await executeCommand(command);
// TODO: print the account, region and new bootstrap version
}
} else {
console.error('Not doing anything with --apply flag. Would run the following:');
if (commands.length) {
for (const command of commands) {
console.log(commandSetToString(command));
}
} else {
console.error('(no commands to run)');
}
}
}
}
export const bootstrapApplyCliRun = <A, M extends CdkManager<A>>(manager: M, argv: Array<string>): void => {
const cls = new BootstrapApplyCliCommand(manager);
cmdTs.run(
cmdTs.command({
name: 'bootstrap-apply',
args: bootstrapApplyCliArgs,
handler: cls.handler.bind(cls),
}),
argv,
);
};