Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

fix region error. fix env vars not propagated to task if -s not provided #30

Merged
merged 3 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build/*
__debug_bin
.vscode/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ require (
golang.org/x/text v0.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

go 1.13
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func main() {
Value: 1,
Usage: "Number of tasks to run",
},
&cli.StringFlag{
Name: "region, r",
Usage: "AWS Region",
},
&cli.BoolFlag{
Name: "deregister",
Usage: "Deregister task definition once done",
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 24 additions & 9 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,13 +42,15 @@ type Runner struct {
Deregister bool
}

// New creates a new instance of a runner
func New() *Runner {
return &Runner{
Region: os.Getenv("AWS_REGION"),
Config: aws.NewConfig(),
}
}

// Run runs the runner
func (r *Runner) Run(ctx context.Context) error {
taskDefinitionInput, err := parser.Parse(r.TaskDefinitionFile, os.Environ())
if err != nil {
Expand All @@ -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
Expand Down Expand Up @@ -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{}
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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)
Expand Down