-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
142 lines (115 loc) · 3.64 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
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/buildkite/ecs-run-task/parser"
"github.com/urfave/cli/v2"
)
const ECS_POLL_INTERVAL = 1 * time.Second
const version = "dev"
func main() {
app := cli.NewApp()
app.Name = "ecs-upload-task"
app.Usage = "upload ecs task definitions and update ECS services"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
&cli.StringFlag{Name: "file", Value: "taskdefinition.json", Usage: "the task definition to upload"},
&cli.StringFlag{Name: "cluster", Value: "default", Usage: "The cluster to update the services on"},
&cli.StringFlag{Name: "service", Usage: "Optional service name to update"},
&cli.BoolFlag{Name: "dry-run", Value: false, Usage: "Parse the template without running upload"},
}
app.Run(os.Args)
}
func run(c *cli.Context) error {
sess := session.Must(session.NewSession())
svc := ecs.New(sess)
cluster := c.String("cluster")
filename := c.String("file")
dryrun := c.Bool("dry-run")
if dryrun {
_, err := parser.Parse(filename, os.Environ())
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse task definition: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "Template %s is parsed successfully\n", filename)
os.Exit(0)
}
taskDefinition := uploadTask(svc, filename)
if serviceName := c.String("service"); serviceName != "" {
updateService(svc, serviceName, cluster, taskDefinition)
}
return nil
}
func uploadTask(svc *ecs.ECS, filename string) string {
taskDefinitionInput, err := parser.Parse(filename, os.Environ())
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse task definition: %s\n", err)
os.Exit(1)
}
log.Printf("Registering a task for %s\n", *taskDefinitionInput.Family)
resp, err := svc.RegisterTaskDefinition(taskDefinitionInput)
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, service, cluster, 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(ECS_POLL_INTERVAL)
}
}