This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.go
150 lines (135 loc) · 3.46 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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/buildkite/ecs-run-task/runner"
"github.com/urfave/cli/v2"
)
var (
Version string
)
func main() {
app := cli.NewApp()
app.Name = "ecs-run-task"
app.Usage = "run a once-off task on ECS and tail the output from cloudwatch"
app.UsageText = "ecs-run-task [options] [command override]"
app.Version = Version
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Show debugging information",
},
&cli.StringFlag{
Name: "file, f",
Usage: "Task definition file in JSON or YAML",
},
&cli.StringFlag{
Name: "name, n",
Usage: "Task name",
},
&cli.StringFlag{
Name: "cluster, c",
Value: "default",
Usage: "ECS cluster name",
},
&cli.StringFlag{
Name: "log-group, l",
Value: "ecs-task-runner",
Usage: "Cloudwatch Log Group Name to write logs to",
},
&cli.StringFlag{
Name: "service, s",
Value: "",
Usage: "service to replace cmd for",
},
&cli.BoolFlag{
Name: "fargate",
Usage: "Specified if task is to be run under FARGATE as opposed to EC2",
},
&cli.StringSliceFlag{
Name: "security-group",
Usage: "Security groups to launch task in (required for FARGATE). Can be specified multiple times",
},
&cli.StringSliceFlag{
Name: "subnet",
Usage: "Subnet to launch task in (required for FARGATE). Can be specified multiple times",
},
&cli.StringSliceFlag{
Name: "env, e",
Usage: "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",
},
&cli.BoolFlag{
Name: "inherit-env, E",
Usage: "Inherit all of the environment variables from the calling shell",
},
&cli.IntFlag{
Name: "count, C",
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",
},
}
app.Action = func(ctx *cli.Context) error {
requireFlagValue(ctx, "file")
if _, err := os.Stat(ctx.String("file")); err != nil {
return cli.NewExitError(err, 1)
}
if !ctx.Bool("debug") {
log.SetOutput(ioutil.Discard)
}
r := runner.New()
r.TaskDefinitionFile = ctx.String("file")
r.Cluster = ctx.String("cluster")
r.TaskName = ctx.String("name")
r.LogGroupName = ctx.String("log-group")
r.Fargate = ctx.Bool("fargate")
r.SecurityGroups = ctx.StringSlice("security-group")
r.Subnets = ctx.StringSlice("subnet")
r.Environment = ctx.StringSlice("env")
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)
}
}
if args := ctx.Args(); args.Len() > 0 {
r.Overrides = append(r.Overrides, runner.Override{
Service: ctx.String("service"),
Command: args.Slice(),
})
}
if err := r.Run(context.Background()); err != nil {
if ec, ok := err.(cli.ExitCoder); ok {
return ec
}
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func requireFlagValue(ctx *cli.Context, name string) {
if ctx.String(name) == "" {
fmt.Fprintf(os.Stderr, "ERROR: Required flag %q isn't set\n\n", name)
cli.ShowAppHelpAndExit(ctx, 1)
}
}