-
Notifications
You must be signed in to change notification settings - Fork 98
/
TaskOperations.ts
89 lines (76 loc) · 3.17 KB
/
TaskOperations.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
/*!
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT
*/
import CloudFormation = require('aws-sdk/clients/cloudformation')
import * as tl from 'azure-pipelines-task-lib/task'
import {
captureStackOutputs,
testStackHasResources,
waitForStackCreation,
waitForStackUpdate
} from 'Common/cloudformationutils'
import { ignoreStackOutputs, stackOutputsAsJson, TaskParameters } from './TaskParameters'
export class TaskOperations {
public constructor(
public readonly cloudFormationClient: CloudFormation,
public readonly taskParameters: TaskParameters
) {}
public async execute(): Promise<void> {
const stackId = await this.verifyResourcesExist(
this.taskParameters.changeSetName,
this.taskParameters.stackName
)
let waitForUpdate: boolean = false
if (stackId) {
waitForUpdate = await testStackHasResources(this.cloudFormationClient, this.taskParameters.stackName)
}
console.log(tl.loc('ExecutingChangeSet', this.taskParameters.changeSetName, this.taskParameters.stackName))
try {
await this.cloudFormationClient
.executeChangeSet({
ChangeSetName: this.taskParameters.changeSetName,
StackName: this.taskParameters.stackName
})
.promise()
if (waitForUpdate) {
await waitForStackUpdate(this.cloudFormationClient, this.taskParameters.stackName)
} else {
await waitForStackCreation(this.cloudFormationClient, this.taskParameters.stackName)
}
if (this.taskParameters.outputVariable) {
console.log(tl.loc('SettingOutputVariable', this.taskParameters.outputVariable))
tl.setVariable(this.taskParameters.outputVariable, stackId)
}
if (this.taskParameters.captureStackOutputs !== ignoreStackOutputs) {
await captureStackOutputs(
this.cloudFormationClient,
this.taskParameters.stackName,
this.taskParameters.captureStackOutputs === stackOutputsAsJson,
this.taskParameters.captureAsSecuredVars
)
}
console.log(tl.loc('TaskCompleted', this.taskParameters.changeSetName))
} catch (err) {
console.error(tl.loc('ExecuteChangeSetFailed', (err as Error).message), err)
throw err
}
}
private async verifyResourcesExist(changeSetName: string, stackName: string): Promise<string> {
try {
const request: CloudFormation.DescribeChangeSetInput = {
ChangeSetName: changeSetName
}
if (stackName) {
request.StackName = stackName
}
const response = await this.cloudFormationClient.describeChangeSet(request).promise()
if (!response.StackId) {
return ''
}
return response.StackId
} catch (err) {
throw new Error(tl.loc('ChangeSetDoesNotExist', changeSetName))
}
}
}