forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deisctl.go
214 lines (198 loc) · 6.24 KB
/
deisctl.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
// +build !windows
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/deis/deis/deisctl/backend/fleet"
"github.com/deis/deis/deisctl/client"
"github.com/deis/deis/deisctl/utils"
"github.com/deis/deis/version"
docopt "github.com/docopt/docopt-go"
)
// main exits with the return value of Command(os.Args[1:]), deferring all logic to
// a func we can test.
func main() {
os.Exit(Command(nil))
}
// Command executes the given deisctl command line.
func Command(argv []string) int {
deisctlMotd := utils.DeisIfy("Deis Control Utility")
usage := deisctlMotd + `
Usage: deisctl [options] <command> [<args>...]
Commands, use "deisctl help <command>" to learn more:
install install components, or the entire platform
uninstall uninstall components
list list installed components
start start components
stop stop components
restart stop, then start components
scale grow or shrink the number of routers, registries or store gateways
journal print the log output of a component
config set platform or component values
refresh-units refresh unit files from GitHub
help show the help screen for a command
Options:
-h --help show this help screen
--endpoint=<url> etcd endpoint for fleet [default: http://127.0.0.1:4001]
--etcd-cafile=<path> etcd CA file authentication [default: ]
--etcd-certfile=<path> etcd cert file authentication [default: ]
--etcd-key-prefix=<path> keyspace for fleet data in etcd [default: /_coreos.com/fleet/]
--etcd-keyfile=<path> etcd key file authentication [default: ]
--known-hosts-file=<path> where to store remote fingerprints [default: ~/.ssh/known_hosts]
--request-timeout=<secs> seconds before a request is considered failed [default: 10.0]
--ssh-timeout=<secs> seconds before SSH connection is considered failed [default: 10.0]
--strict-host-key-checking verify SSH host keys [default: true]
--tunnel=<host> SSH tunnel for communication with fleet and etcd [default: ]
--version print the version of deisctl
`
// pre-parse command-line arguments
argv, helpFlag := parseArgs(argv)
// give docopt an optional final false arg so it doesn't call os.Exit()
args, err := docopt.Parse(usage, argv, false, version.Version, true, false)
if err != nil || len(args) == 0 {
if helpFlag {
fmt.Print(usage)
return 0
}
return 1
}
command := args["<command>"]
setTunnel := true
// "--help" and "refresh-units" doesn't need SSH tunneling
if helpFlag || command == "refresh-units" {
setTunnel = false
}
setGlobalFlags(args, setTunnel)
// clean up the args so subcommands don't need to reparse them
argv = removeGlobalArgs(argv)
// construct a client
c, err := client.NewClient("fleet")
if err != nil {
fmt.Printf("Error: %v\n", err)
return 1
}
// Dispatch the command, passing the argv through so subcommands can
// re-parse it according to their usage strings.
switch command {
case "list":
err = c.List(argv)
case "scale":
err = c.Scale(argv)
case "start":
err = c.Start(argv)
case "restart":
err = c.Restart(argv)
case "stop":
err = c.Stop(argv)
case "status":
err = c.Status(argv)
case "journal":
err = c.Journal(argv)
case "install":
err = c.Install(argv)
case "uninstall":
err = c.Uninstall(argv)
case "config":
err = c.Config(argv)
case "refresh-units":
err = c.RefreshUnits(argv)
case "help":
fmt.Print(usage)
return 0
default:
fmt.Println(`Found no matching command, try "deisctl help"
Usage: deisctl <command> [<args>...] [options]`)
return 1
}
if err != nil {
fmt.Printf("Error: %v\n", err)
return 1
}
return 0
}
// isGlobalArg returns true if a string looks like it is a global deisctl option flag,
// such as "--tunnel".
func isGlobalArg(arg string) bool {
prefixes := []string{
"--endpoint=",
"--etcd-key-prefix=",
"--etcd-keyfile=",
"--etcd-certfile=",
"--etcd-cafile=",
// "--experimental-api=",
"--known-hosts-file=",
"--request-timeout=",
"--ssh-timeout=",
"--strict-host-key-checking=",
"--tunnel=",
}
for _, p := range prefixes {
if strings.HasPrefix(arg, p) {
return true
}
}
return false
}
// parseArgs returns the provided args with "--help" as the last arg if need be,
// and a boolean to indicate whether help was requested.
func parseArgs(argv []string) ([]string, bool) {
if argv == nil {
argv = os.Args[1:]
}
if len(argv) == 1 {
// rearrange "deisctl --help" as "deisctl help"
if argv[0] == "--help" || argv[0] == "-h" {
argv[0] = "help"
}
}
if len(argv) >= 2 {
// rearrange "deisctl help <command>" as "deisctl <command> --help"
if argv[0] == "help" || argv[0] == "--help" || argv[0] == "-h" {
argv = append(argv[1:], "--help")
}
}
helpFlag := false
for _, a := range argv {
if a == "help" || a == "--help" || a == "-h" {
helpFlag = true
break
}
}
return argv, helpFlag
}
// removeGlobalArgs returns the given args without any global option flags, to make
// re-parsing by subcommands easier.
func removeGlobalArgs(argv []string) []string {
var v []string
for _, a := range argv {
if !isGlobalArg(a) {
v = append(v, a)
}
}
return v
}
// setGlobalFlags sets fleet provider options based on deisctl global flags.
func setGlobalFlags(args map[string]interface{}, setTunnel bool) {
fleet.Flags.Endpoint = args["--endpoint"].(string)
fleet.Flags.EtcdKeyPrefix = args["--etcd-key-prefix"].(string)
fleet.Flags.EtcdKeyFile = args["--etcd-keyfile"].(string)
fleet.Flags.EtcdCertFile = args["--etcd-certfile"].(string)
fleet.Flags.EtcdCAFile = args["--etcd-cafile"].(string)
//fleet.Flags.UseAPI = args["--experimental-api"].(bool)
fleet.Flags.KnownHostsFile = args["--known-hosts-file"].(string)
fleet.Flags.StrictHostKeyChecking = args["--strict-host-key-checking"].(bool)
timeout, _ := strconv.ParseFloat(args["--request-timeout"].(string), 64)
fleet.Flags.RequestTimeout = timeout
sshTimeout, _ := strconv.ParseFloat(args["--ssh-timeout"].(string), 64)
fleet.Flags.SSHTimeout = sshTimeout
if setTunnel == true {
tunnel := args["--tunnel"].(string)
if tunnel != "" {
fleet.Flags.Tunnel = tunnel
} else {
fleet.Flags.Tunnel = os.Getenv("DEISCTL_TUNNEL")
}
}
}