-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·152 lines (137 loc) · 3.38 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
package main
import (
"os"
"time"
"github.com/shufo/ecs-fargate-oneshot/logs"
"github.com/shufo/ecs-fargate-oneshot/tasks"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// Version provides ecs-fargate version
var Version = "default"
func main() {
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
err := run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func run(args []string) error {
cli.VersionFlag = &cli.BoolFlag{
Name: "version", Aliases: []string{"V"},
Usage: "print only the version",
}
app := &cli.App{
Name: "ecs-fargate-oneshot",
Version: Version,
Compiled: time.Now(),
Usage: "run oneshot task on ecs (fargate) with passed parameter",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cluster",
Value: "",
Aliases: []string{"c"},
Usage: "cluster name which task executes for",
Required: true,
},
&cli.StringFlag{
Name: "service, s",
Aliases: []string{"s"},
Usage: "service where task executed in",
Required: true,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Show verbose logs",
Required: false,
},
&cli.BoolFlag{
Name: "progress",
Aliases: []string{"p"},
Usage: "Show progress spinner",
Required: false,
},
},
Commands: []*cli.Command{
{
Name: "run",
Usage: "run the task with given args",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "task-definition",
Aliases: []string{"t"},
Required: true,
},
&cli.Uint64Flag{
Name: "cpu",
Aliases: []string{"C"},
Required: false,
},
&cli.Uint64Flag{
Name: "memory",
Aliases: []string{"m"},
Required: false,
},
&cli.StringFlag{
Name: "container",
Aliases: []string{"n"},
Required: true,
},
&cli.BoolFlag{
Name: "show-cloudwatch-logs",
Aliases: []string{"l"},
Value: false,
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
log.Fatalln("ecs-fargate-oneshot: require commands to execute")
log.Fatalln("e.g. ecs-fargate-oneshot run [options] echo 1")
os.Exit(1)
}
if (c.Uint64("cpu") > 0 || c.Uint64("memory") > 0) && (c.Uint64("cpu") == 0 || c.Uint64("memory") == 0) {
log.Errorln("Both --cpu and --memory must be specified. Only one of them was specified.")
os.Exit(1)
}
if !tasks.ValidateCombinationOfCpuAndMemory(c.Uint64("cpu"), c.Uint64("memory")) {
log.Errorln("The combination of cpu and memory value is invalid.\nCheck https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html for available combination")
os.Exit(1)
}
if c.Bool("verbose") {
log.SetLevel(log.InfoLevel)
}
tasks.RunTask(c)
return nil
},
},
{
Name: "logs",
Usage: "show logs for tasks",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "container",
Aliases: []string{"n"},
Value: "",
Required: true,
},
&cli.StringFlag{
Name: "task-id",
Aliases: []string{"t"},
Value: "",
Required: false,
},
},
Action: func(c *cli.Context) error {
if c.Bool("verbose") {
log.SetLevel(log.InfoLevel)
}
logs.RunShowLogsWithTaskId(c)
return nil
},
},
},
}
return app.Run(args)
}