-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.ts
198 lines (164 loc) · 6.72 KB
/
manager.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
import * as cdk from 'aws-cdk-lib';
import { bootstrapApplyCliRun } from './bootstrap-apply';
import { pipelineApplyCliRun } from './pipeline-apply';
import { activateCliRun } from './activate';
import type { EnvironmentVariables } from './exec-utils';
import type { Account, Instance } from './config';
export class CdkManager<A> {
accounts: Array<Account>;
instances: Array<Instance<A>>;
constructor() {
this.accounts = [];
this.instances = [];
}
addAccount(account: Account): void {
this.checkIfAccountNameExists(account.name);
this.accounts.push(account);
}
checkIfAccountNameExists(accountName: string): void {
if (this.getAccountNames().indexOf(accountName) !== -1) {
throw new Error(`Account name already exists: ${accountName}`);
}
}
getAccount(accountName: string): Account {
for (const acc of this.accounts) {
if (acc.name === accountName) {
return acc;
}
}
throw new Error(`No account with name: ${accountName}`);
}
addInstance(instance: Instance<A>): void {
this.checkIfSuffixExists(instance.accountName, instance.suffix);
this.instances.push(instance);
}
checkIfSuffixExists(accountName: string, instanceSuffix: string | undefined): void {
const instanceSuffixes = [];
for (const inst of this.getInstancesForAccount(accountName)) {
instanceSuffixes.push(inst.suffix);
}
if (instanceSuffixes.indexOf(instanceSuffix) !== -1) {
throw new Error(`Suffix ${instanceSuffix} already exists in account ${accountName}`);
}
}
getSubInstancesForAccount(accountName: string): Array<Instance<A>> {
const subInstances: Array<Instance<A>> = [];
for (const instance of this.instances) {
for (const subInstance of instance.sequencedInstances ?? []) {
if (subInstance.accountName === accountName) {
subInstances.push({
...subInstance,
branchName: instance.branchName,
});
}
}
}
return subInstances;
}
getInstancesForAccount(accountName: string): Array<Instance<A>> {
const account = this.getAccount(accountName);
const instances = [];
for (const inst of this.instances) {
if (inst.accountName === account.name) {
instances.push(inst);
}
}
instances.push(...this.getSubInstancesForAccount(accountName));
return instances;
}
getInstanceForAccount(accountName: string, suffix?: string): Instance<A> {
for (const inst of this.getInstancesForAccount(accountName)) {
if (inst.suffix === suffix) {
return inst;
}
}
throw new Error(`No instance with suffix: ${suffix} found for account: ${accountName}`);
}
getInstanceForAccountIfExists(accountName: string, suffix?: string): Instance<A> | undefined {
try {
return this.getInstanceForAccount(accountName, suffix);
} catch {
return undefined;
}
}
getAccountNames(): Array<string> {
const names = [];
for (const acc of this.accounts) {
names.push(acc.name);
}
return names;
}
getPipelineCdkApp(accountName: string, suffix?: string): cdk.App {
const account = this.getAccount(accountName);
const instance = this.getInstanceForAccount(accountName, suffix);
const app = new cdk.App();
this.addPipelineCdkStack(app, account, instance);
return app;
}
getPipelineCdkAppFromEnv(): cdk.App {
const target = process.env['TARGET_ACCOUNT'];
if (!target) {
throw Error('TARGET_ACCOUNT not set');
}
console.log(`Using account ${target}`);
const targetSuffix = process.env['TARGET_ENVIRONMENT_SUFFIX'];
console.log(`Using target suffix ${targetSuffix}`);
return this.getPipelineCdkApp(target, targetSuffix);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
addPipelineCdkStack(app: cdk.App, account: Account, instance: Instance<A>): cdk.Stack {
// This is expected to be a subclass of PipelineStack from
// './pipeline-stack', but doesn't have to be.
throw new Error('Not implemented');
}
getParentInstanceForInstance(instance: Instance<A>): Instance<A> | null {
for (const inst of this.instances) {
for (const subInst of inst.sequencedInstances ?? []) {
if (subInst.suffix == instance.suffix && subInst.accountName === instance.accountName) {
return inst;
}
}
}
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPipelineStackName(account: Account, instance: Instance<A>): string {
throw new Error('Not implemented');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getExtraActivationEnvironmentVariables(envVars: EnvironmentVariables, account: Account, instance?: Instance<A>): Promise<EnvironmentVariables> {
return {};
}
getActivationDefaultAccountName(): string {
throw new Error('Unknown default deployment profile');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDefaultBootstrapDeploymentProfile(account: Account): string {
throw new Error('Unknown default bootstrap profile');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDefaultPipelineDeploymentProfile(account: Account, instance?: Instance<A>): string {
throw new Error('Unknown default deployment profile');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDefaultInstanceProfile(account: Account, instance: Instance<A>): string {
throw new Error('Unknown default instance profile');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDefaultPipelineDeploymentRegion(account: Account, instance?: Instance<A>): string {
throw new Error('Unknown default deployment region');
}
getDefaultInstanceRegion(account: Account, instance: Instance<A>): string {
// Default to the same region as the pipeline
return this.getDefaultPipelineDeploymentRegion(account, instance);
}
runPipelineApplyFromArgv(argv: Array<string>): void {
pipelineApplyCliRun(this, argv);
}
runBootstrapApplyFromArgv(argv: Array<string>): void {
bootstrapApplyCliRun(this, argv);
}
runActivateFromArgv(argv: Array<string>): void {
activateCliRun(this, argv);
}
}