/
deployment.go
220 lines (173 loc) · 4.66 KB
/
deployment.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
package outback
import (
"fmt"
"regexp"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/koala-labs/outback/pkg/docker"
)
type Deployment struct {
DeployDetails []*DeployDetail
BuildDetail BuildDetail
Err error
}
type DeployDetail struct {
Cluster *ecs.Cluster
Service *ecs.Service
TaskDefinition *ecs.TaskDefinition
TaskDefinitionFamilyName string
RevisionNumber int
Done bool
}
type BuildDetail struct {
Repo string
CommitHash string
Dockerfile string
buildArgs []string
configBuildArgs []string
cacheFrom []string
}
func (d *DeployDetail) SetCluster(cluster *ecs.Cluster) {
d.Cluster = cluster
}
func (d *DeployDetail) SetService(service *ecs.Service) {
d.Service = service
}
func (d *DeployDetail) SetTaskDefinition(taskDef *ecs.TaskDefinition) {
d.TaskDefinition = taskDef
}
func (d *DeployDetail) SetDone(done bool) {
d.Done = done
}
func (d *DeployDetail) SetTaskDefinitionFamilyName(TaskDefinitionFamilyName string) {
d.TaskDefinitionFamilyName = TaskDefinitionFamilyName
}
func (d *DeployDetail) SetRevisionNumber(revisionNumber int) {
d.RevisionNumber = revisionNumber
}
func (d *Deployment) SetRepo(repo string) {
d.BuildDetail.Repo = repo
}
func (d *Deployment) SetCommitHash(commit string) {
d.BuildDetail.CommitHash = commit
}
func (d *Deployment) SetDockerfile(dockerfile string) {
d.BuildDetail.Dockerfile = dockerfile
}
func (d *Deployment) SetBuildArgs(buildArgs []string) {
d.BuildDetail.buildArgs = buildArgs
}
func (d *Deployment) SetConfigBuildArgs(configBuildArgs []string) {
d.BuildDetail.configBuildArgs = configBuildArgs
}
func (d *Deployment) SetBuildCacheFrom(cacheFrom []string) {
d.BuildDetail.cacheFrom = cacheFrom
}
func (d *Deployment) TaskDefinitions() string {
var out strings.Builder
for _, detail := range d.DeployDetails {
out.WriteString(fmt.Sprintf("%s ", *detail.TaskDefinition.TaskDefinitionArn))
}
return out.String()
}
func (d *Deployment) Services() string {
var out strings.Builder
for _, detail := range d.DeployDetails {
out.WriteString(fmt.Sprintf("%s ", *detail.Service.ServiceName))
}
return out.String()
}
func (d *DeployDetail) TaskDefinitionFamily() string {
r := regexp.MustCompile(`([^\/]+)$`)
return r.FindString(*d.TaskDefinition.TaskDefinitionArn)
}
func (u *Outback) NewDeployDetail() *DeployDetail {
return &DeployDetail{
Done: false,
}
}
func (u *Outback) AwaitServicesRunning(deployment *Deployment) chan *DeployDetail {
waitTime := time.Second * 2
doneCh := make(chan *DeployDetail)
for _, detail := range deployment.DeployDetails {
go func(detail *DeployDetail) {
for !detail.Done {
done := u.IsServiceRunning(detail)
detail.SetDone(done)
time.Sleep(waitTime)
}
doneCh <- detail
}(detail)
}
return doneCh
}
func (u *Outback) RollbackAll(deploy *Deployment, deployDetail *DeployDetail) <-chan error {
var wg sync.WaitGroup
errCh := make(chan error)
wg.Add(len(deploy.DeployDetails))
for _, detail := range deploy.DeployDetails {
go func(detail *DeployDetail) {
taskDefName, err := u.RollbackTaskDefinition(detail.Cluster, detail.Service, detail.TaskDefinition, deployDetail.RevisionNumber)
if err != nil {
errCh <- err
wg.Done()
}
detail.SetTaskDefinitionFamilyName(taskDefName)
wg.Done()
}(detail)
}
wg.Wait()
close(errCh)
return errCh
}
func (u *Outback) DeployAll(deploy *Deployment) <-chan error {
var wg sync.WaitGroup
errCh := make(chan error)
wg.Add(len(deploy.DeployDetails))
for _, detail := range deploy.DeployDetails {
go func(detail *DeployDetail) {
taskDef, err := u.UpdateServiceWithNewTaskDefinition(detail.Cluster, detail.Service, deploy.BuildDetail.Repo, deploy.BuildDetail.CommitHash)
if err != nil {
fmt.Printf("Deployment failed: %s \n", err)
errCh <- err
wg.Done()
}
// Set TaskDefinition to the updated services new TaskDefinition
detail.SetTaskDefinition(taskDef)
wg.Done()
}(detail)
}
wg.Wait()
close(errCh)
return errCh
}
func (u *Outback) LoginBuildPushImage(info BuildDetail) error {
var err error
err = u.ECRLogin()
if err != nil {
return err
}
err = docker.ImageBuild(info.Repo, info.CommitHash, info.Dockerfile, info.buildArgs, info.configBuildArgs, info.cacheFrom)
if err != nil {
return err
}
err = docker.ImagePush(info.Repo, info.CommitHash)
if err != nil {
return err
}
return nil
}
func (u *Outback) LoginPullImage(repo string, tag string) error {
var err error
err = u.ECRLogin()
if err != nil {
return err
}
err = docker.ImagePull(repo, tag)
if err != nil {
return err
}
return nil
}