-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
460 lines (385 loc) · 13.9 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// Config keeps the required configuration for the action.
type Config struct {
AWSRegion string
ECSClusterName string
TaskDefinitionName string
ContainerMakeTarget []string // If is set as a string it will unmarshall as a slice with 1 string
AWSVpcSubnet string
AWSVpcSecurityGroups []string // Up to 5 security groups can be provided: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_AwsVpcConfiguration.html
CloudWatchLogsGroupName string
CloudWatchLogsStreamName string
MaxLogLines int
TimeoutMillis int
LogFilters []string // To show full logs, set LOG_FILTERS=".*"
RepoName string // Repo to clone by the Docker image (container)
GitCloneURL string // Full repository URL for cloning
Ref string // Reference to clone, no default
}
const (
defaultTimeoutMillis = 240000
defaultMaxLogLines = 200
logLinesReqBackoff = 5 * time.Second
)
func LoadConfig() Config {
viper.BindEnv("aws_region")
viper.BindEnv("ecs_cluster_name")
viper.BindEnv("task_definition_name")
viper.BindEnv("container_make_target")
viper.BindEnv("aws_vpc_subnet")
viper.BindEnv("aws_vpc_security_groups")
viper.BindEnv("cloud_watch_logs_group_name")
viper.BindEnv("cloud_watch_logs_stream_name")
viper.BindEnv("timeout_millis")
viper.BindEnv("max_log_lines")
viper.BindEnv("log_filters")
viper.BindEnv("repo_name")
viper.BindEnv("git_clone_url")
viper.BindEnv("ref")
timeoutMillis := viper.GetInt("timeout_millis")
if timeoutMillis == 0 {
timeoutMillis = defaultTimeoutMillis
}
maxLogLines := viper.GetInt("max_log_lines")
if maxLogLines == 0 {
maxLogLines = defaultMaxLogLines
}
return Config{
AWSRegion: viper.GetString("aws_region"),
ECSClusterName: viper.GetString("ecs_cluster_name"),
TaskDefinitionName: viper.GetString("task_definition_name"),
ContainerMakeTarget: viper.GetStringSlice("container_make_target"),
AWSVpcSubnet: viper.GetString("aws_vpc_subnet"),
AWSVpcSecurityGroups: viper.GetStringSlice("aws_vpc_security_groups"),
CloudWatchLogsGroupName: viper.GetString("cloud_watch_logs_group_name"),
CloudWatchLogsStreamName: viper.GetString("cloud_watch_logs_stream_name"),
LogFilters: viper.GetStringSlice("log_filters"),
TimeoutMillis: timeoutMillis,
MaxLogLines: maxLogLines,
RepoName: viper.GetString("repo_name"),
GitCloneURL: viper.GetString("git_clone_url"),
Ref: viper.GetString("ref"),
}
}
func main() {
params := LoadConfig()
logFilters := make([]*regexp.Regexp, len(params.LogFilters))
for i, filter := range params.LogFilters {
logFilters[i] = regexp.MustCompile(filter)
}
taskRunner, cfg := prepareFargateTask(params)
timeout := time.Duration(params.TimeoutMillis) * time.Millisecond
id, err := runFargateTask(timeout, taskRunner)
if err != nil {
log.Fatalf("failed to run task: %v", err)
}
// to be able to add timeout later
ctx := context.Background()
printFargateTaskLogs(ctx, params, cfg, taskRunner, id, logFilters)
}
func prepareFargateTask(params Config) (*TaskRunner, aws.Config) {
cfg, err := config.LoadDefaultConfig(
context.TODO(),
)
if err != nil {
log.Fatalf("failed: %v", err)
}
taskSpecs := &ecs.RunTaskInput{
Cluster: ¶ms.ECSClusterName,
TaskDefinition: ¶ms.TaskDefinitionName,
LaunchType: ecsTypes.LaunchTypeFargate,
NetworkConfiguration: &ecsTypes.NetworkConfiguration{
AwsvpcConfiguration: &ecsTypes.AwsVpcConfiguration{
Subnets: []string{params.AWSVpcSubnet},
SecurityGroups: params.AWSVpcSecurityGroups,
},
},
}
// modify task input if command is specified
if len(params.ContainerMakeTarget) > 0 {
taskDefinition := &ecs.DescribeTaskDefinitionInput{
TaskDefinition: ¶ms.TaskDefinitionName,
}
containerOverride := NewContainerOverride(taskDefinition, ecs.NewFromConfig(cfg))
ctx, cancelFn := context.WithTimeout(context.Background(), time.Duration(params.TimeoutMillis)*time.Millisecond)
defer cancelFn()
containers, err := containerOverride.GetContainersOverride(ctx, params.ContainerMakeTarget, params.RepoName, params.GitCloneURL, params.Ref)
if err != nil {
log.Fatalf("failed: %v", err)
}
taskSpecs.Overrides = &ecsTypes.TaskOverride{
ContainerOverrides: containers,
}
}
return NewTaskRunner(taskSpecs, ecs.NewFromConfig(cfg)), cfg
}
func runFargateTask(timeout time.Duration, taskRunner *TaskRunner) (string, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), timeout)
defer cancelFn()
// TODO: https://github.com/newrelic/infrastructure-agent/issues/1248
taskOutput, err := taskRunner.Run(ctx)
if err != nil {
log.Fatalf("failed to run task: %v", err)
}
id, err := getTaskID(taskOutput)
if err != nil {
log.Fatalf("failed to configure log tailer: %v", err)
}
return id, nil
}
// printLogLine writes the provided line into the writer if it matches any of the regular expressions
func printLogLine(line string, writer io.Writer, logFilters []*regexp.Regexp) {
for _, filter := range logFilters {
if filter.MatchString(line) {
fmt.Fprintf(writer, "%s\n", line)
return
}
}
}
func printFargateTaskLogs(ctx context.Context, params Config, cfg aws.Config, taskRunner *TaskRunner, id string, logFilters []*regexp.Regexp) {
logTailerConfig := CloudWatchLogTailerConfig{
LogGroupName: params.CloudWatchLogsGroupName,
LogStreamName: fmt.Sprintf("%s/%s", params.CloudWatchLogsStreamName, id),
MaxLines: params.MaxLogLines,
}
// Add information to identify cloudwatch logs stream.
printLogsInfo(params, id)
logTailer := NewCloudWatchLogTailer(logTailerConfig, cloudwatchlogs.NewFromConfig(cfg))
for {
if len(logFilters) > 0 {
logs, err := logTailer.GetLogs(ctx)
if err != nil {
log.Fatalf("failed to read logs: %v", err)
}
for _, line := range logs {
printLogLine(*line.Message, log.Writer(), logFilters)
}
}
finished, exitCode, err := taskRunner.IsFinished()
if err != nil {
log.Fatalf("failed to check if task has finished: %v", err)
}
if finished {
os.Exit(exitCode)
}
time.Sleep(logLinesReqBackoff)
}
}
func printLogsInfo(params Config, taskID string) {
source := fmt.Sprintf("%s/%s", params.CloudWatchLogsStreamName, taskID)
fmt.Fprintf(log.Writer(), "Fetching logs from: %s\n", source)
fmt.Fprintf(log.Writer(), "Tail logs:\n")
fmt.Fprintf(log.Writer(), " tools/aws_logs.sh --group-name=%s --stream-name=%s --tail\n",
params.CloudWatchLogsGroupName, source)
fmt.Fprintf(log.Writer(), "Download full logs:\n")
fmt.Fprintf(log.Writer(), " tools/aws_logs.sh --group-name=%s --stream-name=%s --output-file=aws_logs_%d.txt\n",
params.CloudWatchLogsGroupName, source, time.Now().Unix())
}
// ContainerOverride returns a list of containers definition with an override command
type ContainerOverride struct {
specs *ecs.DescribeTaskDefinitionInput
awsClient *ecs.Client
}
// NewContainerOverride returns a new ContainerOverride.
func NewContainerOverride(taskDefinition *ecs.DescribeTaskDefinitionInput, awsClient *ecs.Client) *ContainerOverride {
return &ContainerOverride{
specs: taskDefinition,
awsClient: awsClient,
}
}
// GetContainerOverride returns a container configuration with a new command
func (co *ContainerOverride) GetContainersOverride(ctx context.Context, command []string, repoName, gitCloneURL, ref string) ([]ecsTypes.ContainerOverride, error) {
var err error
task, err := co.awsClient.DescribeTaskDefinition(ctx, co.specs)
if err != nil {
return nil, errors.Wrap(err, "failed to describe task definitions")
}
containerOverrides := make([]ecsTypes.ContainerOverride, len(task.TaskDefinition.ContainerDefinitions))
for i, container := range task.TaskDefinition.ContainerDefinitions {
containerEnvironment := container.Environment
if repoName != "" {
containerEnvironment = append(containerEnvironment, ecsTypes.KeyValuePair{
Name: aws.String("REPO_NAME"),
Value: aws.String(repoName),
})
}
if gitCloneURL != "" {
containerEnvironment = append(containerEnvironment, ecsTypes.KeyValuePair{
Name: aws.String("GIT_CLONE_URL"),
Value: aws.String(gitCloneURL),
})
}
if ref != "" {
containerEnvironment = append(containerEnvironment, ecsTypes.KeyValuePair{
Name: aws.String("REF"),
Value: aws.String(ref),
})
}
containerOverrides[i] = ecsTypes.ContainerOverride{
Name: container.Name,
Environment: containerEnvironment,
EnvironmentFiles: container.EnvironmentFiles,
Command: command,
ResourceRequirements: container.ResourceRequirements,
}
}
return containerOverrides, nil
}
// TaskRunner runs a new task based on provided specs.
type TaskRunner struct {
specs *ecs.RunTaskInput
awsClient *ecs.Client
runningTask *ecs.RunTaskOutput
}
// NewTaskRunner returns a new TaskRunner.
func NewTaskRunner(taskSpecs *ecs.RunTaskInput, awsClient *ecs.Client) *TaskRunner {
return &TaskRunner{
specs: taskSpecs,
awsClient: awsClient,
}
}
// Run starts ecs task and waits for it to be in running state.
func (tr *TaskRunner) Run(ctx context.Context) (*ecs.DescribeTasksOutput, error) {
var err error
tr.runningTask, err = tr.awsClient.RunTask(ctx, tr.specs)
if err != nil {
return nil, errors.Wrap(err, "failed to run task")
}
log.Println("Waiting for task to run...")
defer func() {
log.Println("Task is running!")
}()
return tr.WaitForStatus(ctx, "RUNNING")
}
// GetStatus pulls ecs task status.
func (tr *TaskRunner) GetStatus(ctx context.Context) (*ecs.DescribeTasksOutput, error) {
if tr.runningTask == nil ||
len(tr.runningTask.Tasks) == 0 ||
tr.runningTask.Tasks[0].TaskArn == nil {
return nil, fmt.Errorf("task not started")
}
taskArn := *tr.runningTask.Tasks[0].TaskArn
specs := &ecs.DescribeTasksInput{
Tasks: []string{taskArn},
Cluster: tr.specs.Cluster,
}
return tr.awsClient.DescribeTasks(ctx, specs)
}
// WaitForStatus pulls ecs task until desired state is reached or ctx canceled.
func (tr *TaskRunner) WaitForStatus(ctx context.Context, status string) (*ecs.DescribeTasksOutput, error) {
for {
select {
case <-time.After(time.Second):
output, err := tr.GetStatus(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get status while waiting for task")
}
if len(output.Tasks) > 0 && output.Tasks[0].LastStatus != nil {
lastStatus := *output.Tasks[0].LastStatus
if lastStatus == status {
return output, nil
}
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
// IsFinished checks if the ecs task has finised and returns the exit code of the container.
func (tr *TaskRunner) IsFinished() (finished bool, exitCode int, err error) {
exitCode = -1
// TODO: pass context && should retry this?
status, err := tr.GetStatus(context.Background())
if err != nil {
err = errors.Wrap(err, "failed to get task status while checking if has finished")
return
}
if status == nil || len(status.Tasks) == 0 {
err = errors.New("failed to get task status, empty status/tasks")
return
}
task := (*status).Tasks[0]
if task.LastStatus != nil && *task.LastStatus == "STOPPED" {
finished = true
if len(task.Containers) > 0 && task.Containers[0].ExitCode != nil {
exitCode = int(*status.Tasks[0].Containers[0].ExitCode)
}
}
return
}
// CloudWatchLogTailerConfig configures CloudWatchLogTailer.
type CloudWatchLogTailerConfig struct {
LogGroupName string
LogStreamName string
MaxLines int
}
// CloudWatchLogTailer is used to fetch logs from cloudwatchlogs service.
type CloudWatchLogTailer struct {
config CloudWatchLogTailerConfig
awsClient *cloudwatchlogs.Client
nextToken string
}
// NewCloudWatchLogTailer returns new CloudWatchLogTailer.
func NewCloudWatchLogTailer(config CloudWatchLogTailerConfig, awsClient *cloudwatchlogs.Client) *CloudWatchLogTailer {
return &CloudWatchLogTailer{
config: config,
awsClient: awsClient,
}
}
// GetLogs returns the latest log lines available in the configured cloudwatchlogs.
func (c *CloudWatchLogTailer) GetLogs(ctx context.Context) ([]types.OutputLogEvent, error) {
cfg := &cloudwatchlogs.GetLogEventsInput{
Limit: aws.Int32(int32(c.config.MaxLines)),
LogGroupName: aws.String(c.config.LogGroupName),
LogStreamName: aws.String(c.config.LogStreamName),
StartFromHead: aws.Bool(true),
}
if c.nextToken != "" {
cfg.NextToken = aws.String(c.nextToken)
}
logEventsResp, err := c.awsClient.GetLogEvents(ctx, cfg)
if err != nil {
return nil, err
}
// GetLogEvents can return empty results while there are more log events available through the token.
if len(logEventsResp.Events) > 0 {
// If you have reached the end of the stream, it returns the same token you passed in.
c.nextToken = *logEventsResp.NextForwardToken
}
return logEventsResp.Events, nil
}
// getTaskID will check the ecs.DescribeTasksOutput object and extract the id we use to build
// the LogStreamName from taskArn.
func getTaskID(taskOutput *ecs.DescribeTasksOutput) (string, error) {
if taskOutput == nil ||
len(taskOutput.Tasks) == 0 ||
taskOutput.Tasks[0].TaskArn == nil {
return "", errors.New("failed to get task id from empty task")
}
taskArn := *taskOutput.Tasks[0].TaskArn
i := strings.LastIndex(taskArn, "/")
if i < 0 {
return "", fmt.Errorf("failed to get task id, bad taskArn format: '%s'", taskArn)
}
return taskArn[i+1:], nil
}