-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
383 lines (329 loc) · 9.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main // import "github.com/CenturyLinkLabs/panamaxcli"
import (
"crypto/x509"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/CenturyLinkLabs/panamax-remote-agent-go/client"
"github.com/CenturyLinkLabs/panamaxcli/actions"
"github.com/CenturyLinkLabs/panamaxcli/config"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
const (
Version = "0.1.0"
verificationWarning = `There was a problem verifying the Panamax Agent's SSL certificate! Please check the README if you are unsure why this might occur:
https://github.com/CenturyLinkLabs/panamaxcli
If you're positive that this is not an issue, you can rerun your command with the --insecure flag. The error is:
%s`
)
var (
Config config.Config
Commands []cli.Command
)
func init() {
client.DefaultHTTPTimeout = 10
Commands = []cli.Command{
{
Name: "remote",
Aliases: []string{"re"},
Usage: "Manage remotes",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List remotes",
Action: remoteListAction,
},
{
Name: "describe",
Aliases: []string{"d"},
Usage: "Describe a remote",
Description: "Arguments is optionally the name of the remote. When omitted, the active remote will be used.",
Before: actionRequiresArgument("optional:remote name"),
Action: remoteDescribeAction,
},
{
Name: "add",
Usage: "Add a remote",
Description: "Arguments are the name of the remote and the path to the token file.",
Before: actionRequiresArgument("remote name", "token path"),
Action: remoteAddAction,
},
{
Name: "active",
Usage: "Set the active remote",
Description: "Argument is a remote name.",
Before: actionRequiresArgument("remote name"),
Action: setActiveRemoteAction,
},
{
Name: "remove",
Usage: "Remove a remote",
Description: "Argument is a remote name.",
Before: actionRequiresArgument("remote name"),
Action: removeRemoteAction,
},
{
Name: "token",
Usage: "Show the remote's token",
Description: "Argument is a remote name.",
Before: actionRequiresArgument("optional:remote name"),
Action: getTokenAction,
},
},
},
{
Name: "deployment",
Aliases: []string{"de"},
Usage: "Manage deployments",
Before: actionRequiresActiveRemote,
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List deployments",
Action: deploymentsListAction,
},
{
Name: "describe",
Aliases: []string{"d"},
Usage: "Describe a deployment",
Description: "Argument is a deployment ID.",
Before: actionRequiresArgument("deployment ID"),
Action: describeDeploymentAction,
},
{
Name: "create",
Usage: "Deploy a template",
Description: "Argument is the path to a Panamax template.",
Before: actionRequiresArgument("template path"),
Action: createDeploymentAction,
},
{
Name: "redeploy",
Usage: "Redeploy a deployment",
Description: "Argument is a deployment ID.",
Before: actionRequiresArgument("deployment ID"),
Action: redeployDeploymentAction,
},
{
Name: "delete",
Usage: "Delete a deployment",
Description: "Argument is a deployment ID.",
Before: actionRequiresArgument("deployment ID"),
Action: deleteDeploymentAction,
},
},
},
}
}
func main() {
app := cli.NewApp()
app.Name = "panamaxcli"
app.Version = Version
app.Usage = "Panamax command-line utility."
app.Authors = []cli.Author{{"CenturyLink Labs", "[email protected]"}}
app.Commands = Commands
app.Before = initializeApp
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "Enable verbose logging",
},
cli.BoolFlag{
Name: "insecure",
Usage: "Skip SSL certificate verification",
},
}
app.Run(os.Args)
}
func initializeApp(c *cli.Context) error {
if c.GlobalBool("debug") {
// Remote Agent Client will write to logrus with helpful info!
log.SetLevel(log.DebugLevel)
}
if c.GlobalBool("insecure") {
// Remote Agent Client will not verify SSL cert. This is probably bad, but
// is useful for old certs that don't have the proper SAN IP settings.
client.SkipSSLVerify = true
}
// Surprise! CLI wants an error from this method but, only uses it to abort
// execution, not for display anywhere.
if err := loadConfig(c); err != nil {
log.Error(err)
return err
}
return nil
}
func loadConfig(c *cli.Context) error {
path, err := makeConfigPath()
if err != nil {
return err
}
fileConfig := config.FileConfig{Path: path}
if err := fileConfig.Load(); err != nil {
return err
}
Config = config.Config(&fileConfig)
return nil
}
func actionRequiresArgument(args ...string) func(c *cli.Context) error {
return func(c *cli.Context) error {
requiredCount := len(args)
for i, arg := range args {
if strings.HasPrefix(arg, "optional:") {
requiredCount = i
}
}
if len(c.Args()) < requiredCount || len(c.Args()) > len(args) {
s := strings.Join(args, ", ")
message := fmt.Sprintf("This command requires the following arguments: %s", s)
log.Errorln(message)
return errors.New(message)
}
return nil
}
}
func actionRequiresActiveRemote(c *cli.Context) error {
arg := c.Args().First()
isHelp := (arg == "help" || arg == "h")
if !isHelp && Config.Active() == nil {
message := "an active remote is required for this command"
log.Errorln(message)
return errors.New(message)
}
return nil
}
func remoteAddAction(c *cli.Context) {
name := c.Args().First()
path := c.Args().Get(1)
output, err := actions.AddRemoteByPath(Config, name, path)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func removeRemoteAction(c *cli.Context) {
name := c.Args().First()
output, err := actions.RemoveRemote(Config, name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func remoteListAction(c *cli.Context) {
output := actions.ListRemotes(Config)
fmt.Println(output.ToPrettyOutput())
}
func remoteDescribeAction(c *cli.Context) {
name, err := explicitOrActiveRemoteName(c)
if err != nil {
log.Error(err)
return
}
output, err := actions.DescribeRemote(Config, name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func setActiveRemoteAction(c *cli.Context) {
name := c.Args().First()
output, err := actions.SetActiveRemote(Config, name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func deploymentsListAction(c *cli.Context) {
output, err := actions.ListDeployments(*Config.Active())
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func createDeploymentAction(c *cli.Context) {
path := c.Args().First()
output, err := actions.CreateDeployment(*Config.Active(), path)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func describeDeploymentAction(c *cli.Context) {
name := c.Args().First()
output, err := actions.DescribeDeployment(*Config.Active(), name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func redeployDeploymentAction(c *cli.Context) {
name := c.Args().First()
output, err := actions.RedeployDeployment(*Config.Active(), name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func deleteDeploymentAction(c *cli.Context) {
name := c.Args().First()
output, err := actions.DeleteDeployment(*Config.Active(), name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func getTokenAction(c *cli.Context) {
name, err := explicitOrActiveRemoteName(c)
if err != nil {
log.Error(err)
return
}
output, err := actions.GetRemoteToken(Config, name)
if err != nil {
fatalError(err)
}
fmt.Println(output.ToPrettyOutput())
}
func fatalError(err error) {
if uErr, ok := err.(*url.Error); ok {
if hErr, ok := uErr.Err.(x509.HostnameError); ok {
err = fmt.Errorf(verificationWarning, hErr.Error())
}
}
log.Fatal(err)
}
func explicitOrActiveRemoteName(c *cli.Context) (string, error) {
if c.Args().First() == "" && Config.Active() == nil {
return "", errors.New("you must provide a remote name or set an active remote!")
}
name := c.Args().First()
if name == "" {
name = Config.Active().Name
}
return name, nil
}
func makeConfigPath() (string, error) {
// Stolen from: https://github.com/awslabs/aws-sdk-go/pull/136 Originally
// cleaner with os/user.Current(), but that failed under cross-compilation on
// non-linux platforms.
dir := os.Getenv("HOME") // *nix
if dir == "" { // Windows
dir = os.Getenv("USERPROFILE")
}
if dir == "" {
return "", errors.New("Couldn't determine your home directory!")
}
panamaxDir := filepath.Join(dir, ".panamax")
if _, err := os.Stat(panamaxDir); os.IsNotExist(err) {
if err := os.Mkdir(panamaxDir, 0700); err != nil {
return "", err
}
}
return filepath.Join(panamaxDir, "remotes"), nil
}