forked from blaines/tasque-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws_ecs.go
350 lines (320 loc) · 10.5 KB
/
aws_ecs.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/blaines/tasque-go/result"
"github.com/fsouza/go-dockerclient"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
// AWSECS hello world
type AWSECS struct {
ecsTaskDefinition *string
overrideContainerName *string
overridePayloadKey *string
heartbeatDuration time.Duration
taskArn string
handler MessageHandler
timeout time.Duration
docker *Docker
result result.Result
}
// Docker hello world
type Docker struct {
client *docker.Client
eventsCh chan *docker.APIEvents
}
// InstanceMetadata hello world
type InstanceMetadata struct {
client *ec2metadata.EC2Metadata
document ec2metadata.EC2InstanceIdentityDocument
}
// ECSMetadata hello world
type ECSMetadata struct {
Cluster string `json:"Cluster"`
ContainerInstanceArn string `json:"ContainerInstanceArn"`
Version string `json:"Version"`
}
func (executable AWSECS) Execute(handler MessageHandler) {
executable.handler = handler
executable.execute(handler)
}
func (executable *AWSECS) Result() result.Result {
return executable.result
}
func (ecsmeta *ECSMetadata) init() {
client := &http.Client{}
req, err := http.NewRequest("GET", "http://localhost:51678/v1/metadata", nil)
if err != nil {
log.Fatalln(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, _ := ioutil.ReadAll(resp.Body)
if err := json.Unmarshal(body, &ecsmeta); err != nil {
fmt.Println(string(body))
panic(err)
}
} else {
panic("ECS metadata service did not return 200")
}
}
// If the metadata service can't be reached, what do we do?
// -> Retry
// -> Alert?
// -> Devmode
// Currently this will run for 30 seconds and panic/die if it can't connect.
// Devmode option skips this
func (m *InstanceMetadata) init() *ec2metadata.EC2Metadata {
// Locate this instance
timeoutDuration, _ := time.ParseDuration("30s")
timeout := time.After(timeoutDuration)
i := 0
for {
i++
select {
default:
log.Printf("[INFO] Connecting metadata service (%d)", i)
sess, err := session.NewSession()
if err != nil {
fmt.Println("failed to create session,", err)
panic("failed to create session")
}
m.client = ec2metadata.New(sess)
m.document, _ = m.client.GetInstanceIdentityDocument()
if m.client.Available() {
log.Printf("[INFO] AWS EC2 instance detected via default metadata API endpoint")
return m.client
}
case <-timeout:
panic("AWS metadata service connection failed")
}
}
}
func (executable AWSECS) execute(handler MessageHandler) {
handler.initialize()
if handler.receive() {
executable.executableTimeoutHelper(handler)
}
}
func (executable *AWSECS) executableTimeoutHelper(handler MessageHandler) {
// Channel receives exit event
ch := make(chan error)
go func() {
ch <- executable.executionHelper(handler.body(), handler.id())
}()
select {
case err := <-ch:
if err != nil {
log.Printf("E: %s %s", *executable.ecsTaskDefinition, err.Error())
if strings.Contains(err.Error(), "InvalidParameterException") {
executable.result.SetExit("PARAMETER")
} else if executable.result.Exit == "" {
executable.result.SetExit("UNKNOWN")
}
handler.failure(executable.result)
} else {
log.Printf("I: %s finished successfully", *executable.ecsTaskDefinition)
handler.success()
}
case <-time.After(executable.timeout):
err := fmt.Errorf("%s timed out after %f seconds", *executable.ecsTaskDefinition, executable.timeout.Seconds())
log.Println(err)
executable.result.SetExit("TIMEOUT")
handler.failure(executable.result)
}
}
func (executable *AWSECS) executionHelper(messageBody *string, messageID *string) error {
var err error
var taskArn string
taskArn, err = executable.startECSContainer(messageBody, messageID)
executable.taskArn = taskArn
if err != nil {
return err
}
err = executable.monitorDocker()
if err != nil {
return err
}
return nil
}
// Task ARN is part of Docker labels...
// "com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:770136283015:task/d8e65fde-65dc-4e46-aeaa-8b2b33215349",
func (executable *AWSECS) startECSContainer(messageBody *string, messageID *string) (string, error) {
e := &ECSMetadata{}
m := &InstanceMetadata{}
m.init()
e.init()
var ecsCluster *string
var containerInstanceID *string
ecsCluster = aws.String(e.Cluster)
containerInstanceID = aws.String(e.ContainerInstanceArn)
// Start ECS task on self
sess, err := session.NewSession(&aws.Config{Region: aws.String("us-west-2")})
if err != nil {
fmt.Println("failed to create session,", err)
return "", err
}
svc := ecs.New(sess)
params := &ecs.StartTaskInput{
ContainerInstances: []*string{
containerInstanceID,
},
TaskDefinition: executable.ecsTaskDefinition,
Cluster: ecsCluster,
Overrides: &ecs.TaskOverride{
ContainerOverrides: []*ecs.ContainerOverride{
{
Environment: []*ecs.KeyValuePair{
{
Name: executable.overridePayloadKey,
Value: aws.String(*messageBody),
},
},
Name: executable.overrideContainerName,
},
},
},
StartedBy: aws.String("tasque"),
}
resp, err := svc.StartTask(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println("Error:", err.Error())
return "", err
}
// Pretty-print the response data.
fmt.Println(resp)
if len(resp.Failures) > 0 {
var err error
// There were errors starting the container
reason := resp.Failures[0].Reason
if strings.Contains(*reason, "CPU") {
executable.result.SetExit("CPU")
err = fmt.Errorf("%s %s The cpu requested by the task is unavailable on the given container instance. You may need to add container instances to your cluster", *reason, *resp.Failures[0].Arn)
} else if strings.Contains(*reason, "MEMORY") {
executable.result.SetExit("MEMORY")
err = fmt.Errorf("%s %s The memory requested by the task is unavailable on the given container instance. You may need to add container instances to your cluster", *reason, *resp.Failures[0].Arn)
} else if strings.Contains(*reason, "RESOURCE") {
executable.result.SetExit("RESOURCE")
err = fmt.Errorf("%s %s The resource or resources requested by the task are unavailable on the given container instance. If the resource is CPU or memory, you may need to add container instances to your cluster", *reason, *resp.Failures[0].Arn)
} else if strings.Contains(*reason, "AGENT") {
executable.result.SetExit("AGENT")
err = fmt.Errorf("%s %s The container instance that you attempted to launch a task onto has an agent which is currently disconnected. In order to prevent extended wait times for task placement, the request was rejected", *reason, *resp.Failures[0].Arn)
} else if strings.Contains(*reason, "ATTRIBUTE") {
executable.result.SetExit("ATTRIBUTE")
err = fmt.Errorf("%s %s Your task definition contains a parameter that requires a specific container instance attribute that is not available on your container instances. For more information on which attributes are required for specific task definition parameters and agent configuration variables, see Task Definition Parameters and Amazon ECS Container Agent Configuration", *reason, *resp.Failures[0].Arn)
} else {
// Unrecognized error
executable.result.SetExit("UNKNOWN")
err = fmt.Errorf("Unrecognized error: '%s' %+v", *reason, resp)
}
return "", err
}
taskArn := resp.Tasks[0].Containers[0].TaskArn
return *taskArn, nil
}
func (executable *AWSECS) monitorDocker() error {
executable.docker.addListener()
// Monitor docker events for sibling Projector task
status, err := executable.listenForDie()
if err != nil {
return err
}
executable.result.SetExit(status)
if status == "0" {
// status is die
log.Printf("[INFO] Execution completed successfully")
executable.success()
return nil
}
// non-zero exit
log.Printf("[ERROR] Execution completed with non-zero exit status")
err = fmt.Errorf("%s died with non-zero exit status (exit code %s)", *executable.ecsTaskDefinition, status)
executable.failure()
return err
}
func (executable *AWSECS) listenForDie() (exitCode string, err error) {
log.Printf("[INFO] Monitoring Docker events.")
log.Printf("[DEBUG] %+v\n", executable.docker)
timeout := time.After(executable.timeout)
ticker := time.NewTicker(executable.heartbeatDuration)
defer func() {
executable.docker.removeListener()
ticker.Stop()
}()
for {
select {
case msg := <-executable.docker.eventsCh:
if msg != nil {
matched := msg.Actor.Attributes["com.amazonaws.ecs.task-arn"] == executable.taskArn
if matched {
log.Printf("[DEBUG] %+v\n", msg)
switch msg.Action {
case "die":
log.Printf("[INFO] Container die event")
return msg.Actor.Attributes["exitCode"], nil
case "start":
log.Printf("[INFO] Container start event")
executable.result.SetHost(msg.ID[0:12])
// Ticker to check docker container status
go func() {
for t := range ticker.C {
container, err := executable.docker.client.InspectContainer(msg.ID)
if err != nil {
log.Println(fmt.Errorf("There was an error checking container status %s", err.Error()))
}
if container.State.Running == true {
executable.handler.heartbeat()
log.Println("Heartbeat", t)
} else {
log.Printf("Container state is %s", container.State.Status)
}
}
}()
}
}
}
case <-timeout:
log.Printf("[INFO] Instance timeout reached.")
err := fmt.Errorf("Docker container %s timed out after %f seconds", *executable.ecsTaskDefinition, executable.timeout.Seconds())
return "timeout", err
}
}
}
func (dockerobj *Docker) connect(dockerEndpointPath string) {
log.Printf("[INFO] Connecting to Docker API.")
endpoint := dockerEndpointPath
client, err := docker.NewClient(endpoint)
if err != nil {
panic(err)
}
dockerobj.client = client
dockerobj.eventsCh = make(chan *docker.APIEvents)
}
func (dockerobj *Docker) addListener() {
err := dockerobj.client.AddEventListener(dockerobj.eventsCh)
if err != nil {
log.Fatal(err)
}
}
func (dockerobj *Docker) removeListener() {
err := dockerobj.client.RemoveEventListener(dockerobj.eventsCh)
if err != nil {
log.Fatal(err)
}
}
func (executable *AWSECS) success() {}
func (executable *AWSECS) failure() {}