/
main.go
208 lines (166 loc) · 4.58 KB
/
main.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
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
"github.com/opolis/deployer/pipeline"
)
func init() {
log.SetFormatter(&log.JSONFormatter{DisableTimestamp: true})
}
func main() {
lambda.Start(Handler)
}
func Handler(event events.CodePipelineEvent) error {
defer func() {
if r := recover(); r != nil {
log.Errorln("recovered from panic:", r)
}
}()
// AWS session
sess := session.Must(session.NewSession())
pipeline := pipeline.NewAWSPipelineManager(sess)
id := event.CodePipelineJob.ID
data := event.CodePipelineJob.Data
log := log.WithFields(log.Fields{"jobId": id})
// Get input artifacts
stackArtifact, buildArtifact, err := getArtifacts(sess, data)
if err != nil {
if err := pipeline.JobFailure(id, err.Error()); err != nil {
log.Errorln("could not post failure", id, err.Error())
}
return nil
}
defer stackArtifact.Close()
defer buildArtifact.Close()
// Read deploy bucket name
bucket, err := readDeployBucket(stackArtifact)
if err != nil {
if err := pipeline.JobFailure(id, err.Error()); err != nil {
log.Errorln("could not post failure", id, err.Error())
}
return nil
}
// Deploy build artifact
if err := deployBuild(sess, bucket, buildArtifact); err != nil {
if err := pipeline.JobFailure(id, err.Error()); err != nil {
log.Errorln("could not post failure", id, err.Error())
}
return nil
}
if err := pipeline.JobSuccess(id); err != nil {
log.Errorln("could not post success", id, err.Error())
}
return nil
}
//
// Helpers
//
// getAritifacts - return (stack artifact, build artifact, error)
func getArtifacts(sess *session.Session, data events.CodePipelineData) (io.ReadCloser, io.ReadCloser, error) {
if len(data.InputArtifacts) == 0 {
return nil, nil, errors.New("no input artifacts")
}
// Check for deploy stack and build output artifacts
if len(data.InputArtifacts) == 2 {
stack, err := getS3(sess, data.InputArtifacts[0])
if err != nil {
return nil, nil, err
}
build, err := getS3(sess, data.InputArtifacts[1])
if err != nil {
return nil, nil, err
}
return stack, build, nil
}
return nil, nil, errors.New("invalid amount of parameters")
}
func getS3(sess *session.Session, input events.CodePipelineInputArtifact) (io.ReadCloser, error) {
bucket := input.Location.S3Location.BucketName
key := input.Location.S3Location.ObjectKey
resp, err := s3.New(sess).GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
return resp.Body, err
}
func readDeployBucket(stackArtifact io.ReadCloser) (string, error) {
buffer, err := ioutil.ReadAll(stackArtifact)
if err != nil {
return "", err
}
// open the zip archive for reading
reader, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
if err != nil {
return "", err
}
// look for `outputs.json` and marshal into a map
// extracting the `Bucket` key
for _, f := range reader.File {
if f.Name == "outputs.json" {
body, err := f.Open()
if err != nil {
return "", err
}
content, _ := ioutil.ReadAll(body)
body.Close()
var object map[string]string
if err := json.Unmarshal(content, &object); err != nil {
return "", err
}
return object["Bucket"], nil
}
}
return "", errors.New("outputs.json does not exist in stack artifact")
}
func deployBuild(sess *session.Session, bucket string, buildArtifact io.ReadCloser) error {
buffer, err := ioutil.ReadAll(buildArtifact)
if err != nil {
return err
}
// open the zip archive for reading
reader, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
if err != nil {
return err
}
// Iterate through the files in the archive, and upload them to S3
for _, f := range reader.File {
key := f.Name
body, err := f.Open()
if err != nil {
return err
}
content, _ := ioutil.ReadAll(body)
body.Close()
contentType := http.DetectContentType(content)
// manual override for svg content
if strings.HasSuffix(key, ".svg") {
contentType = "image/svg+xml"
}
// manual override for css content
if strings.HasSuffix(key, ".css") {
contentType = "text/css"
}
_, err = s3.New(sess).PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(content),
Bucket: aws.String(bucket),
Key: aws.String(key),
ContentType: aws.String(contentType),
})
if err != nil {
return err
}
}
return nil
}