From b6340962106b0b5af0a0af37ecf2c97734b6d069 Mon Sep 17 00:00:00 2001 From: Eli Goldberg Date: Sun, 9 Feb 2020 20:38:04 +0200 Subject: [PATCH 1/2] fix region error. fix env vars not propagated to task if -s not provided --- .gitignore | 2 ++ go.mod | 2 ++ main.go | 8 ++++++++ runner/runner.go | 33 ++++++++++++++++++++++++--------- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index d9b4f01..86dc772 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /build/* +__debug_bin +.vscode/ \ No newline at end of file diff --git a/go.mod b/go.mod index 0f88158..a71cb86 100644 --- a/go.mod +++ b/go.mod @@ -16,3 +16,5 @@ require ( gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v2 v2.2.1 // indirect ) + +go 1.13 diff --git a/main.go b/main.go index e4b401d..40a2e8c 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,10 @@ func main() { Value: 1, Usage: "Number of tasks to run", }, + cli.StringFlag{ + Name: "region, r", + Usage: "Reion", + }, cli.BoolFlag{ Name: "deregister", Usage: "Deregister task definition once done", @@ -104,6 +108,10 @@ func main() { r.Count = ctx.Int64("count") r.Deregister = ctx.Bool("deregister") + if r.Region == "" { + r.Region = ctx.String("region") + } + if ctx.Bool("inherit-env") { for _, env := range os.Environ() { r.Environment = append(r.Environment, env) diff --git a/runner/runner.go b/runner/runner.go index dc01bb7..14f87bc 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -18,11 +18,13 @@ import ( "github.com/buildkite/ecs-run-task/parser" ) +// Override .. type Override struct { Service string Command []string } +// Runner .. type Runner struct { Service string TaskName string @@ -40,6 +42,7 @@ type Runner struct { Deregister bool } +// New creates a new instance of a runner func New() *Runner { return &Runner{ Region: os.Getenv("AWS_REGION"), @@ -47,6 +50,7 @@ func New() *Runner { } } +// Run runs the runner func (r *Runner) Run(ctx context.Context) error { taskDefinitionInput, err := parser.Parse(r.TaskDefinitionFile, os.Environ()) if err != nil { @@ -58,7 +62,7 @@ func (r *Runner) Run(ctx context.Context) error { streamPrefix = fmt.Sprintf("run_task_%d", time.Now().Nanosecond()) } - sess := session.Must(session.NewSession(r.Config)) + sess := session.Must(session.NewSession(r.Config.WithRegion(r.Region))) if err := createLogGroup(sess, r.LogGroupName); err != nil { return err @@ -124,6 +128,11 @@ func (r *Runner) Run(ctx context.Context) error { } } + env, err := awsKeyValuePairForEnv(os.LookupEnv, r.Environment) + if err != nil { + return err + } + for _, override := range r.Overrides { if len(override.Command) > 0 { cmds := []*string{} @@ -141,11 +150,6 @@ func (r *Runner) Run(ctx context.Context) error { cmds = append(cmds, aws.String(command)) } - env, err := awsKeyValuePairForEnv(os.LookupEnv, r.Environment) - if err != nil { - return err - } - runTaskInput.Overrides.ContainerOverrides = append( runTaskInput.Overrides.ContainerOverrides, &ecs.ContainerOverride{ @@ -157,6 +161,17 @@ func (r *Runner) Run(ctx context.Context) error { } } + // If no overrides specified, but Environment variables were - should still be overridden + if len(r.Overrides) == 0 { + runTaskInput.Overrides.ContainerOverrides = append( + runTaskInput.Overrides.ContainerOverrides, + &ecs.ContainerOverride{ + Name: taskDefinitionInput.ContainerDefinitions[0].Name, + Environment: env, + }, + ) + } + log.Printf("Running task %s", taskDefinition) runResp, err := svc.RunTask(runTaskInput) if err != nil { @@ -169,7 +184,7 @@ func (r *Runner) Run(ctx context.Context) error { // spawn a log watcher for each container for _, task := range runResp.Tasks { for _, container := range task.Containers { - containerId := path.Base(*container.ContainerArn) + containerID := path.Base(*container.ContainerArn) watcher := &logWatcher{ LogGroupName: r.LogGroupName, LogStreamName: logStreamName(streamPrefix, container, task), @@ -179,11 +194,11 @@ func (r *Runner) Run(ctx context.Context) error { Printer: func(ev *cloudwatchlogs.FilteredLogEvent) bool { finishedPrefix := fmt.Sprintf( "Container %s exited with", - containerId, + containerID, ) if strings.HasPrefix(*ev.Message, finishedPrefix) { log.Printf("Found container finished message for %s: %s", - containerId, *ev.Message) + containerID, *ev.Message) return false } fmt.Println(*ev.Message) From 3308e7d43693d638e0002ec95c3e3012d80732b7 Mon Sep 17 00:00:00 2001 From: Eli Goldberg Date: Mon, 10 Feb 2020 12:42:45 +0200 Subject: [PATCH 2/2] add docs for new "-r" flag --- README.md | 1 + main.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c46f384..47087a5 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ GLOBAL OPTIONS: --env KEY=value, -e KEY=value An environment variable to add in the form KEY=value or `KEY` (shorthand for `KEY=$KEY` to pass through an env var from the current host). Can be specified multiple times --inherit-env, -E Inherit all of the environment variables from the calling shell --count value, -C value Number of tasks to run (default: 1) + --region value, -r value AWS Region --deregister Deregister task definition once done --help, -h show help --version, -v print the version diff --git a/main.go b/main.go index 40a2e8c..529cf0a 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,7 @@ func main() { }, cli.StringFlag{ Name: "region, r", - Usage: "Reion", + Usage: "AWS Region", }, cli.BoolFlag{ Name: "deregister",