forked from buildkite/ecs-run-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (158 loc) · 4.13 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
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: "task, t",
Usage: "Existing Task definition Arn",
},
&cli.StringFlag{
Name: "name, n",
Usage: "Task name",
},
&cli.StringFlag{
Name: "image, i",
Usage: "Container image name to replace",
},
&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.StringFlag{
Name: "platform-version, p",
Value: "",
Usage: "the platform version the task should run (only for FARGATE)",
},
&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.IntFlag{
Name: "cpu",
Value: 0,
Usage: "Number of cpu units reserved for the container",
},
&cli.IntFlag{
Name: "memory",
Value: 0,
Usage: "Hard limit (in MiB) of memory available to the container",
},
&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 {
if (ctx.String("file") == "" && ctx.String("task") == "") || (ctx.String("file") != "" && ctx.String("task") != "") {
fmt.Fprintf(os.Stderr, "ERROR: either --task or --file is needed\n\n")
cli.ShowAppHelpAndExit(ctx, 1)
}
if !ctx.Bool("debug") {
log.SetOutput(ioutil.Discard)
}
r := runner.New()
r.TaskDefinitionFile = ctx.String("file")
r.TaskDefinition = ctx.String("task")
r.Cluster = ctx.String("cluster")
r.Image = ctx.String("image")
r.Service = ctx.String("service")
r.TaskName = ctx.String("name")
r.LogGroupName = ctx.String("log-group")
r.Fargate = ctx.Bool("fargate")
r.PlatformVersion = ctx.String("platform-version")
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(),
Cpu: ctx.Int64("cpu"),
Memory: ctx.Int64("memory"),
})
}
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)
}
}