/
deploy.go
155 lines (143 loc) · 4.17 KB
/
deploy.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
package forgelib
import (
"fmt"
"github.com/ghodss/yaml"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudformation"
)
// DeployOut provides a controlled format for information to be passed out of
// the Deploy function
type DeployOut struct {
Message string
}
// Deploy will create or update the stack (depending on its current state)
func (s *Stack) Deploy() (output DeployOut, err error) {
validationResult, err := cfnClient.ValidateTemplate(
&cloudformation.ValidateTemplateInput{
TemplateBody: aws.String(s.TemplateBody),
},
)
if err != nil {
return output, err
}
if err := s.GetStackInfo(); err != nil {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Message() {
case fmt.Sprintf("Stack with id %s does not exist", s.StackID),
fmt.Sprintf("Stack with id %s does not exist", s.StackName):
default:
return output, err
}
} else {
return output, err
}
}
var tags []*cloudformation.Tag
if s.TagsBody != "" {
tags, err = parseTags(s.TagsBody)
if err != nil {
return output, err
}
} else if s.StackInfo != nil {
tags = s.StackInfo.Tags
}
var inputParams []*cloudformation.Parameter
parsedParameters := []*cloudformation.Parameter{}
if len(s.ParameterBodies) != 0 {
parsedParameters, err = parseParameters(s.ParameterBodies)
if err != nil {
return output, err
}
}
TEMPLATE_PARAMETERS:
for i := 0; i < len((*validationResult).Parameters); i++ {
parameterKey := *(*validationResult).Parameters[i].ParameterKey
if v, ok := s.ParameterOverrides[parameterKey]; ok {
param := cloudformation.Parameter{
ParameterKey: aws.String(parameterKey),
ParameterValue: aws.String(v),
}
inputParams = append(inputParams, ¶m)
continue TEMPLATE_PARAMETERS
}
for j := 0; j < len(parsedParameters); j++ {
if *parsedParameters[j].ParameterKey == parameterKey {
inputParams = append(inputParams, parsedParameters[j])
continue TEMPLATE_PARAMETERS
}
}
}
var roleARN *string
if s.CfnRoleName != "" {
roleARNString, err := roleARNFromName(s.CfnRoleName)
if err != nil {
return output, err
}
roleARN = &roleARNString
}
var inputStackPolicy *string
if s.StackPolicyBody != "" {
jsonStackPolicy, err := yaml.YAMLToJSON([]byte(s.StackPolicyBody))
if err != nil {
return output, err
}
jsonStackPolicyString := string(jsonStackPolicy)
inputStackPolicy = &jsonStackPolicyString
}
if s.StackInfo == nil {
createOut, err := cfnClient.CreateStack(
&cloudformation.CreateStackInput{
StackName: aws.String(s.StackName),
TemplateBody: aws.String(s.TemplateBody),
OnFailure: aws.String("DELETE"),
Capabilities: validationResult.Capabilities,
Tags: tags,
Parameters: inputParams,
RoleARN: roleARN,
StackPolicyBody: inputStackPolicy,
EnableTerminationProtection: aws.Bool(s.TerminationProtection),
},
)
if err != nil {
return output, err
}
s.StackID = *createOut.StackId
} else {
// Only SET termination protection, do not remove
if s.StackInfo.EnableTerminationProtection != nil &&
!*s.StackInfo.EnableTerminationProtection &&
s.TerminationProtection {
_, err := cfnClient.UpdateTerminationProtection(
&cloudformation.UpdateTerminationProtectionInput{
EnableTerminationProtection: aws.Bool(s.TerminationProtection),
StackName: aws.String(s.StackID),
},
)
if err != nil {
return output, err
}
}
_, err := cfnClient.UpdateStack(
&cloudformation.UpdateStackInput{
StackName: aws.String(s.StackID),
TemplateBody: aws.String(s.TemplateBody),
Capabilities: validationResult.Capabilities,
Tags: tags,
Parameters: inputParams,
RoleARN: roleARN,
StackPolicyBody: inputStackPolicy,
},
)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
noUpdatesErr := "No updates are to be performed."
if awsErr.Message() == noUpdatesErr {
return DeployOut{Message: noUpdatesErr}, nil
}
}
return output, err
}
}
return
}