This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (142 loc) · 4.25 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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
)
const ecsPollInterval = 3 * time.Second
func exitUsage() {
fmt.Println("deploy-ecs-task cluster service-name container-name=image:version")
os.Exit(1)
}
func main() {
if len(os.Args) < 3 {
exitUsage()
}
cluster := os.Args[1]
serviceName := os.Args[2]
replaces := map[string]string{}
for _, s := range strings.Split(os.Args[3], ",") {
s = strings.TrimSpace(s)
parts := strings.Split(s, "=")
if len(parts) != 2 {
fmt.Println("must specify image replace in format container-name=new-docker-image:version")
exitUsage()
}
replaces[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
sess := session.Must(session.NewSession())
svc := ecs.New(sess)
taskDefinition := getCurrentTaskDefinition(svc, cluster, serviceName)
for _, container := range taskDefinition.ContainerDefinitions {
for targetName, targetImage := range replaces {
if *container.Name != targetName {
continue
}
container.SetImage(targetImage)
}
}
newTaskArn := uploadTask(svc, taskDefinition)
updateService(svc, cluster, serviceName, newTaskArn)
}
func getCurrentTaskDefinition(svc *ecs.ECS, cluster string, serviceName string) *ecs.TaskDefinition {
serviceResp, err := svc.DescribeServices(&ecs.DescribeServicesInput{
Cluster: &cluster,
Services: []*string{&serviceName},
})
if err != nil {
log.Fatal("failed to find existing service:", err)
}
if len(serviceResp.Failures) > 0 {
log.Fatal("failed to find existing service:", serviceResp.Failures[0])
}
if len(serviceResp.Services) == 0 {
log.Fatal("service not found")
}
taskDef, err := svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: serviceResp.Services[0].TaskDefinition,
})
if err != nil {
log.Fatal("failed to find existing task definition:", err)
}
return taskDef.TaskDefinition
}
func uploadTask(svc *ecs.ECS, def *ecs.TaskDefinition) string {
// stupid hack to move data between the different types
taskJson, err := json.Marshal(def)
if err != nil {
panic(err)
}
var input ecs.RegisterTaskDefinitionInput
if err = json.Unmarshal(taskJson, &input); err != nil {
panic(err)
}
if err := input.Validate(); err != nil {
log.Fatal("failed to validate new task definition", err)
}
log.Printf("Registering a task for %s\n", *input.Family)
resp, err := svc.RegisterTaskDefinition(&input)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to register task definition: %s\n", err)
os.Exit(1)
}
log.Printf("Created %s\n", *resp.TaskDefinition.TaskDefinitionArn)
return *resp.TaskDefinition.TaskDefinitionArn
}
func updateService(svc *ecs.ECS, cluster, service, taskDefinition string) {
log.Printf("Updating service %s\n", service)
_, err := svc.UpdateService(&ecs.UpdateServiceInput{
Cluster: &cluster,
Service: &service,
TaskDefinition: &taskDefinition,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to update service %s on cluster %s: %s\n", service, cluster, err)
os.Exit(1)
}
pollUntilTaskDeployed(svc, cluster, service, taskDefinition)
}
func getService(svc *ecs.ECS, service, cluster string) (*ecs.Service, error) {
resp, err := svc.DescribeServices(&ecs.DescribeServicesInput{
Services: []*string{aws.String(service)},
Cluster: aws.String(cluster),
})
if err != nil {
return nil, err
}
if len(resp.Failures) > 0 {
return nil, errors.New(*resp.Failures[0].Reason)
}
if len(resp.Services) != 1 {
return nil, errors.New("multiple services with the same name")
}
return resp.Services[0], nil
}
func pollUntilTaskDeployed(svc *ecs.ECS, service string, cluster string, task string) {
lastSeen := time.Now().Add(-1 * time.Minute)
for {
service, err := getService(svc, cluster, service)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get service: %s\n", err)
os.Exit(1)
}
for i := len(service.Events) - 1; i >= 0; i-- {
event := service.Events[i]
if event.CreatedAt.After(lastSeen) {
log.Println(*event.Message)
lastSeen = *event.CreatedAt
}
}
if len(service.Deployments) == 1 && *service.Deployments[0].TaskDefinition == task {
return
}
time.Sleep(ecsPollInterval)
}
}