forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_manager.go
236 lines (194 loc) · 5.91 KB
/
stack_manager.go
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
236
package cloudformation
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/cloudfoundry/bosh-bootloader/aws/cloudformation/templates"
)
var StackNotFound error = errors.New("stack not found")
type logger interface {
Step(message string, a ...interface{})
Dot()
}
type cloudFormationClientProvider interface {
GetCloudFormationClient() Client
}
type StackManager struct {
cloudFormationClientProvider cloudFormationClientProvider
logger logger
}
func NewStackManager(cloudFormationClientProvider cloudFormationClientProvider, logger logger) StackManager {
return StackManager{
cloudFormationClientProvider: cloudFormationClientProvider,
logger: logger,
}
}
func (s StackManager) CreateOrUpdate(name string, template templates.Template, tags Tags) error {
s.logger.Step("checking if cloudformation stack %q exists", name)
_, err := s.Describe(name)
switch err {
case StackNotFound:
return s.create(name, template, tags)
case nil:
return s.Update(name, template, tags)
default:
return err
}
}
func (s StackManager) cloudFormationClient() Client {
return s.cloudFormationClientProvider.GetCloudFormationClient()
}
func (s StackManager) Describe(name string) (Stack, error) {
if name == "" {
return Stack{}, StackNotFound
}
output, err := s.cloudFormationClient().DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: aws.String(name),
})
if err != nil {
switch err.(type) {
case awserr.RequestFailure:
requestFailure := err.(awserr.RequestFailure)
if requestFailure.StatusCode() == 400 && requestFailure.Code() == "ValidationError" &&
requestFailure.Message() == fmt.Sprintf("Stack with id %s does not exist", name) {
return Stack{}, StackNotFound
}
return Stack{}, err
default:
return Stack{}, err
}
}
for _, s := range output.Stacks {
if s.StackName != nil && *s.StackName == name {
status := "UNKNOWN"
if s.StackStatus != nil {
status = *s.StackStatus
}
stack := Stack{
Name: *s.StackName,
Status: status,
Outputs: map[string]string{},
}
for _, output := range s.Outputs {
if output.OutputKey == nil {
return Stack{}, errors.New("failed to parse outputs")
}
value := ""
if output.OutputValue != nil {
value = *output.OutputValue
}
stack.Outputs[*output.OutputKey] = value
}
return stack, nil
}
}
return Stack{}, StackNotFound
}
func (s StackManager) WaitForCompletion(name string, sleepInterval time.Duration, action string) error {
stack, err := s.Describe(name)
if err != nil {
if err == StackNotFound {
s.logger.Step(fmt.Sprintf("finished %s", action))
return nil
}
return err
}
switch stack.Status {
case cloudformation.StackStatusCreateComplete,
cloudformation.StackStatusUpdateComplete,
cloudformation.StackStatusDeleteComplete:
s.logger.Step(fmt.Sprintf("finished %s", action))
return nil
case cloudformation.StackStatusCreateFailed,
cloudformation.StackStatusRollbackComplete,
cloudformation.StackStatusRollbackFailed,
cloudformation.StackStatusUpdateRollbackComplete,
cloudformation.StackStatusUpdateRollbackFailed,
cloudformation.StackStatusDeleteFailed:
return fmt.Errorf(`CloudFormation failure on stack '%s'.
Check the AWS console for error events related to this stack,
and/or open a GitHub issue at https://github.com/cloudfoundry/bosh-bootloader/issues.`, name)
default:
s.logger.Dot()
time.Sleep(sleepInterval)
return s.WaitForCompletion(name, sleepInterval, action)
}
return nil
}
func (s StackManager) Delete(name string) error {
s.logger.Step("deleting cloudformation stack")
_, err := s.cloudFormationClient().DeleteStack(&cloudformation.DeleteStackInput{
StackName: &name,
})
if err != nil {
return err
}
return nil
}
func (s StackManager) create(name string, template templates.Template, tags Tags) error {
s.logger.Step("creating cloudformation stack")
templateJson, err := json.Marshal(&template)
if err != nil {
return err
}
awsTags := tags.toAWSTags()
params := &cloudformation.CreateStackInput{
StackName: aws.String(name),
Capabilities: []*string{aws.String("CAPABILITY_IAM"), aws.String("CAPABILITY_NAMED_IAM")},
TemplateBody: aws.String(string(templateJson)),
Tags: awsTags,
}
_, err = s.cloudFormationClient().CreateStack(params)
if err != nil {
return err
}
return nil
}
func (s StackManager) Update(name string, template templates.Template, tags Tags) error {
s.logger.Step("updating cloudformation stack")
templateJson, err := json.Marshal(&template)
if err != nil {
return err
}
awsTags := tags.toAWSTags()
params := &cloudformation.UpdateStackInput{
StackName: aws.String(name),
Capabilities: []*string{aws.String("CAPABILITY_IAM"), aws.String("CAPABILITY_NAMED_IAM")},
TemplateBody: aws.String(string(templateJson)),
Tags: awsTags,
}
_, err = s.cloudFormationClient().UpdateStack(params)
if err != nil {
switch err.(type) {
case awserr.RequestFailure:
requestFailure := err.(awserr.RequestFailure)
if requestFailure.StatusCode() == 400 && requestFailure.Code() == "ValidationError" {
switch requestFailure.Message() {
case "No updates are to be performed.":
return nil
case fmt.Sprintf("Stack [%s] does not exist", name):
return StackNotFound
default:
}
}
return err
default:
return err
}
}
return nil
}
func (s StackManager) GetPhysicalIDForResource(stackName string, logicalResourceID string) (string, error) {
describeStackResourceOutput, err := s.cloudFormationClient().DescribeStackResource(&cloudformation.DescribeStackResourceInput{
StackName: aws.String(stackName),
LogicalResourceId: aws.String(logicalResourceID),
})
if err != nil {
return "", err
}
return aws.StringValue(describeStackResourceOutput.StackResourceDetail.PhysicalResourceId), nil
}