-
Notifications
You must be signed in to change notification settings - Fork 131
/
rp-build-task-plugin.ts
235 lines (197 loc) · 10.6 KB
/
rp-build-task-plugin.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
import CloudFormation, { DescribeTypeRegistrationOutput, ListTypeVersionsOutput, UpdateStackInput } from 'aws-sdk/clients/cloudformation';
import { IOrganizationBinding } from '~parser/parser';
import { IBuildTaskConfiguration } from '~build-tasks/build-configuration';
import { IBuildTaskPluginCommandArgs, IBuildTaskPlugin, CommonTaskAttributeNames } from '~plugin/plugin';
import { IPluginTask, IPluginBinding } from '~plugin/plugin-binder';
import { IPerformTasksCommandArgs } from '~commands/index';
import { Validator } from '~parser/validator';
import { OrgFormationError } from '~org-formation-error';
import { AwsUtil, CfnUtil } from '~util/aws-util';
import { ConsoleUtil } from '~util/console-util';
const communityResourceProviderCatalog = 'community-resource-provider-catalog';
const communityResourceProviderCatalogGov = 'community-resource-provider-catalog-gov';
export class RpBuildTaskPlugin implements IBuildTaskPlugin<IRpBuildTaskConfig, IRpCommandArgs, IRpTask> {
type = 'register-type';
typeForTask = 'register-type';
convertToCommandArgs(config: IRpBuildTaskConfig, command: IPerformTasksCommandArgs): IRpCommandArgs {
Validator.ThrowForUnknownAttribute(config, config.LogicalName, ...CommonTaskAttributeNames, 'Path',
'FailedTaskTolerance', 'MaxConcurrentTasks', 'RoleArn', 'ResourceType', 'SchemaHandlerPackage',
'ExecutionRole');
if (typeof config.SchemaHandlerPackage !== 'string') {
throw new OrgFormationError(`task ${config.LogicalName} does not have required attribute SchemaHandlerPackage`);
}
return {
...command,
name: config.LogicalName,
schemaHandlerPackage: config.SchemaHandlerPackage,
resourceType: config.ResourceType,
failedTolerance: config.FailedTaskTolerance ?? 0,
maxConcurrent: config.MaxConcurrentTasks ?? 1,
organizationBinding: config.OrganizationBinding,
taskRoleName: config.TaskRoleName,
executionRole: config.ExecutionRole,
};
}
validateCommandArgs(command: IRpCommandArgs): void {
if (typeof command.resourceType === 'undefined') {
throw new OrgFormationError(`task ${command.name} attribute ResourceType is required`);
}
if (typeof command.schemaHandlerPackage === 'undefined') {
throw new OrgFormationError(`task ${command.name} attribute SchemaHandlerPackage is required`);
}
if (typeof command.schemaHandlerPackage === 'string' && !command.schemaHandlerPackage.startsWith('s3://')) {
throw new OrgFormationError(`task ${command.name} SchemaHandlerPackage attribute expected to start with 's3://' (expected format is s3://<bucket>/<path>/<package>.zip). Found: ${command.schemaHandlerPackage}`);
}
}
getValuesForEquality(command: IRpCommandArgs): any {
return {
resourceType: command.resourceType,
schemaHandlerPackage: command.schemaHandlerPackage,
executionRole: command.executionRole,
v: '2',
};
}
convertToTask(command: IRpCommandArgs, globalHash: string): IRpTask {
return {
schemaHandlerPackage: command.schemaHandlerPackage,
resourceType: command.resourceType,
executionRole: command.executionRole,
name: command.name,
hash: globalHash,
type: this.type,
forceDeploy: typeof command.forceDeploy === 'boolean' ? command.forceDeploy : false,
logVerbose: typeof command.verbose === 'boolean' ? command.verbose : false,
};
}
getPhysicalIdForCleanup(command: IRpBuildTaskConfig): string | undefined {
return command.ResourceType;
}
async performRemove(binding: IPluginBinding<IRpTask> /* , resolver: CfnExpressionResolver*/): Promise<void> {
const cfn = await AwsUtil.GetCloudFormation(binding.target.accountId, binding.target.region, binding.task.taskRoleName, null, AwsUtil.GetIsPartition());
let listVersionsResponse: ListTypeVersionsOutput = {};
do {
listVersionsResponse = await cfn.listTypeVersions({ Type: 'RESOURCE', TypeName: binding.task.resourceType, NextToken: listVersionsResponse.NextToken }).promise();
for (const version of listVersionsResponse.TypeVersionSummaries) {
if (!version.IsDefaultVersion) {
await cfn.deregisterType({ Type: 'RESOURCE', TypeName: binding.task.resourceType, VersionId: version.VersionId }).promise();
}
}
} while (listVersionsResponse.NextToken);
await cfn.deregisterType({ Type: 'RESOURCE', TypeName: binding.task.resourceType }).promise();
}
async performCreateOrUpdate(binding: IPluginBinding<IRpTask> /* , resolver: CfnExpressionResolver */): Promise<void> {
const { task, target, previousBindingLocalHash } = binding;
if (task.forceDeploy !== true &&
task.taskLocalHash !== undefined &&
task.taskLocalHash === previousBindingLocalHash) {
ConsoleUtil.LogDebug(`Workload (${this.typeForTask}) ${task.name} in ${target.accountId}/${target.region} skipped, task itself did not change. Use ForceTask to force deployment.`);
return;
}
const catalog = this.getCatalogBucket(AwsUtil.GetIsPartition());
const cfn = await AwsUtil.GetCloudFormation(target.accountId, target.region, task.taskRoleName, task.taskViaRoleArn, AwsUtil.GetIsPartition());
let roleArn = task.executionRole;
const schemaHandlerPackage = task.schemaHandlerPackage;
if (roleArn === undefined) {
roleArn = await this.ensureExecutionRole(cfn, schemaHandlerPackage, catalog);
}
await retryTypeRegistrationWrapper(task.resourceType, async () => {
const response = await cfn.registerType({
TypeName: task.resourceType,
Type: 'RESOURCE',
SchemaHandlerPackage: task.schemaHandlerPackage,
ExecutionRoleArn: roleArn,
}).promise();
let registrationStatus: DescribeTypeRegistrationOutput = {};
do {
await sleep(3000);
registrationStatus = await cfn.describeTypeRegistration({ RegistrationToken: response.RegistrationToken }).promise();
} while (registrationStatus.ProgressStatus === 'IN_PROGRESS');
if (registrationStatus.ProgressStatus !== 'COMPLETE') {
throw new OrgFormationError(`Registration of Resource Type ${task.resourceType} failed. ${registrationStatus.Description}`);
}
await cfn.setTypeDefaultVersion({ Arn: registrationStatus.TypeVersionArn }).promise();
});
}
private async ensureExecutionRole(cfn: CloudFormation, handlerPackageUrl: string, catalog: Catalog): Promise<string> {
if (handlerPackageUrl === undefined || !handlerPackageUrl.startsWith(catalog.uri)) {
throw new OrgFormationError('Can only automatically install ExecutionRole for resource providers hosted on community-resource-provider-catalog or community-resource-provider-catalog-gov. As a workaround, you can use the native CloudFormation resource to register the type: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-resourceversion.html');
}
const { name, version } = this.getResourceRoleName(handlerPackageUrl, catalog);
const updateStackInput: UpdateStackInput = {
StackName: name,
TemplateURL: `${catalog.path}${name}-${version}.yml`,
Capabilities: ['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'],
};
const stacks = await CfnUtil.UpdateOrCreateStack(cfn, updateStackInput);
const stack = stacks.Stacks[0];
const output = stack.Outputs.find(x => x.OutputKey === 'ExecutionRoleArn');
return output.OutputValue;
}
private getResourceRoleName(handlerPackageUrl: string, catalog: Catalog): { name: string; version: string } {
const packageNameWithVersion = handlerPackageUrl.replace(catalog.uri, '').replace('.zip', '');
const packageNameWithVersionParts = packageNameWithVersion.split('-');
const version = packageNameWithVersionParts[packageNameWithVersionParts.length - 1];
const nameParts = packageNameWithVersionParts.slice(0, -1);
const name = nameParts.join('-') + '-resource-role';
return { name, version };
}
private getCatalogBucket(isPartition: boolean): Catalog {
return {
bucket: (isPartition) ? communityResourceProviderCatalogGov : communityResourceProviderCatalog,
uri: (isPartition) ? `s3://${communityResourceProviderCatalogGov}/` : `s3://${communityResourceProviderCatalog}/`,
path: (isPartition) ? `https://${communityResourceProviderCatalogGov}.s3-us-gov-west-1.amazonaws.com/` : `https://${communityResourceProviderCatalog}.s3.amazonaws.com/`,
};
}
appendResolvers(): Promise<void> {
return Promise.resolve();
}
}
const retryTypeRegistrationWrapper = async (type: string, fn: () => Promise<any>): Promise<void> => {
let retryCount = 0;
let retryTypeRegistration = false;
do {
retryTypeRegistration = false;
try {
await fn();
} catch (err) {
const message = err.message as string;
if (message && -1 !== message.indexOf('Deployment is currently in DEPLOY_STAGE of status FAILED')) {
if (retryCount >= 20) {
throw err;
}
ConsoleUtil.LogInfo(`Type registration ${type} is in DEPLOY_STAGE of status FAILED. waiting.... `);
retryCount += 1;
await sleep(1000 * (26 + (4 * Math.random())));
retryTypeRegistration = true;
} else {
throw err;
}
}
} while (retryTypeRegistration);
};
interface Catalog {
bucket: string;
uri: string;
path: string;
}
interface IRpBuildTaskConfig extends IBuildTaskConfiguration {
SchemaHandlerPackage: string;
ResourceType: string;
OrganizationBinding: IOrganizationBinding;
MaxConcurrentTasks?: number;
FailedTaskTolerance?: number;
ExecutionRole?: string;
}
export interface IRpCommandArgs extends IBuildTaskPluginCommandArgs {
schemaHandlerPackage: string;
resourceType: string;
executionRole?: string;
}
export interface IRpTask extends IPluginTask {
schemaHandlerPackage: string;
resourceType: string;
executionRole?: string;
}
const sleep = (time: number): Promise<void> => {
return new Promise(resolve => setTimeout(resolve, time));
};