/
main.go
205 lines (196 loc) · 4.4 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"os"
"path"
"strings"
)
func main() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "An entropy and failure injection management API for Docker platforms."
app.Version = VERSION
app.Authors = []cli.Author{{Name: "Jeff Nickoloff", Email: "jeff@allingeek.com"}}
app.Flags = flags
app.Commands = commands
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func commandNotYetImplemented(c *cli.Context) error {
fmt.Printf("Invocation: %s [xxx] %s\n", c.Command.Name, c.Args())
fmt.Println("This command has yet to be implemented.")
return nil
}
var (
flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: fmt.Sprintf("Log level (options: debug, info, panic)"),
},
cli.StringSliceFlag{
Name: "host, H",
EnvVar: "ENTROPY_HOST",
Value: &cli.StringSlice{"tcp://:2476"},
Usage: "Entropy endpoint host.",
},
}
// entropy run -f 30s -p .10 --failure recv_drop --target label=service=myserv
commands = []cli.Command{
{
Name: "manage",
ShortName: "m",
Usage: "Start the Entropy manager",
Flags: []cli.Flag{
cli.StringFlag{
Name: "cacert",
Usage: "",
Value: "",
},
cli.StringFlag{
Name: "cert",
Usage: "",
Value: "",
},
cli.StringFlag{
Name: "key",
Usage: "",
Value: "",
},
cli.BoolTFlag{
Name: "tlsverify",
Usage: "",
},
cli.StringFlag{
Name: "image",
Usage: "Gremlin image",
Value: "allingeek/gremlins",
},
},
Before: func(c *cli.Context) error {
args := c.Args()
if len(args) == 0 {
return errors.New("Required target is missing.")
}
return nil
},
Action: func(c *cli.Context) error {
m := &manager{
Target: c.Args()[0],
Image: c.String("image"),
DockerEvents: new(DockerEventContext),
EntropyEvents: new(EntropyEventContext)}
log.Printf("Target has been set to: %s", m.Target)
for _, v := range c.GlobalStringSlice("host") {
if strings.HasPrefix(v, "tcp://") {
m.Tcp = strings.TrimPrefix(v, "tcp://")
}
if strings.HasPrefix(v, "unix://") {
m.Unix = strings.TrimPrefix(v, "unix://")
}
}
m.Start()
log.Println("Done")
return nil
},
},
{
Name: "create",
Usage: "Create a failure policy",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "Policy name.",
Value: "",
},
cli.StringFlag{
Name: "criteria, c",
Usage: "Container selection criteria.",
Value: "",
},
cli.StringFlag{
Name: "frequency, f",
Usage: "Failure frequency.",
Value: "30s",
},
cli.StringFlag{
Name: "probability, p",
Usage: "Probability of a failure occuring at the next interval.",
Value: ".1",
},
cli.StringFlag{
Name: "image, i",
Usage: "The failure injection image to use.",
Value: "alpine",
},
cli.StringFlag{
Name: "failure, e",
Usage: "The name of the failure (supported by the provided image).",
},
},
Action: ClientCreatePolicy,
},
{
Name: "rm",
Usage: "Remove failure policy",
Action: DeletePolicy,
},
{
Name: "ls",
Usage: "List failure policies",
Action: RequestPolicyList,
},
{
Name: "lsi",
Usage: "List failure injectors",
Action: RequestInjectorList,
},
{
Name: "run",
Usage: "Create and start failure policy",
Action: commandNotYetImplemented,
},
{
Name: "start",
Usage: "Start a failure policy",
Action: commandNotYetImplemented,
},
{
Name: "stop",
Usage: "Stop a failure policy",
Action: commandNotYetImplemented,
},
{
Name: "version",
ShortName: "v",
Usage: "Show version",
Action: func(c *cli.Context) error {
PrintVersion()
return nil
},
},
}
)