-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
flags.go
254 lines (226 loc) · 8.92 KB
/
flags.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
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Daniel Theophanes (kardianos@gmail.com)
package cli
import (
"flag"
"reflect"
"strings"
"github.com/spf13/cobra"
"github.com/cockroachdb/cockroach/server"
)
var maxResults int64
// pflagValue wraps flag.Value and implements the extra methods of the
// pflag.Value interface.
type pflagValue struct {
flag.Value
}
func (v pflagValue) Type() string {
t := reflect.TypeOf(v.Value).Elem()
return t.Kind().String()
}
func (v pflagValue) IsBoolFlag() bool {
t := reflect.TypeOf(v.Value).Elem()
return t.Kind() == reflect.Bool
}
var flagUsage = map[string]string{
"addr": `
The host:port to bind for HTTP/RPC traffic.
`,
"attrs": `
An ordered, colon-separated list of node attributes. Attributes are
arbitrary strings specifying topography or machine
capabilities. Topography might include datacenter designation
(e.g. "us-west-1a", "us-west-1b", "us-east-1c"). Machine capabilities
might include specialized hardware or number of cores (e.g. "gpu",
"x16c"). The relative geographic proximity of two nodes is inferred
from the common prefix of the attributes list, so topographic
attributes should be specified first and in the same order for all
nodes. For example:
--attrs=us-west-1b,gpu
`,
"cache-size": `
Total size in bytes for caches, shared evenly if there are multiple
storage devices.
`,
"certs": `
Directory containing RSA key and x509 certs. This flag is required if
--insecure=false.
`,
"join": `
A comma-separated list of addresses to use when a new node is joining
an existing cluster. Each address in the list has an optional type:
[type=]<address>. An unspecified type means ip address or dns.
Type is one of:
- tcp: (default if type is omitted): plain ip address or hostname.
- unix: unix socket
- http-lb: HTTP load balancer: we query
http(s)://<address>/_status/details/local
`,
"key-size": `
Key size in bits for CA/Node/Client certificates.
`,
"linearizable": `
Enables linearizable behaviour of operations on this node by making
sure that no commit timestamp is reported back to the client until all
other node clocks have necessarily passed it.
`,
"dev": `
Runs the node as a standalone in-memory cluster and forces --insecure
for all server and client commands. Useful for developing Cockroach
itself.
`,
"insecure": `
Run over plain HTTP. WARNING: this is strongly discouraged.
`,
"max-offset": `
The maximum clock offset for the cluster. Clock offset is measured on
all node-to-node links and if any node notices it has clock offset in
excess of --max-offset, it will commit suicide. Setting this value too
high may decrease transaction performance in the presence of
contention.
`,
"memtable-budget": `
Total size in bytes for memtables, shared evenly if there are multiple
storage devices.
`,
"metrics-frequency": `
Adjust the frequency at which the server records its own internal metrics.
`,
"pgaddr": `
The host:port to bind for Postgres traffic.
`,
"scan-interval": `
Adjusts the target for the duration of a single scan through a store's
ranges. The scan is slowed as necessary to approximately achieve this
duration.
`,
"scan-max-idle-time": `
Adjusts the max idle time of the scanner. This speeds up the scanner on small
clusters to be more responsive.
`,
"time-until-store-dead": `
Adjusts the timeout for stores. If there's been no communication from
a store in this time interval, the store is considered unavailable.
Replicas on an unavailable store will be moved to available ones.
`,
"stores": `
A comma-separated list of stores, specified by a colon-separated list
of device attributes followed by '=' and either a filepath for a
persistent store or an integer size in bytes for an in-memory
store. Device attributes typically include whether the store is flash
(ssd), spinny disk (hdd), fusion-io (fio), in-memory (mem); device
attributes might also include speeds and other specs (7200rpm,
200kiops, etc.). For example:
--stores=hdd:7200rpm=/mnt/hda1,ssd=/mnt/ssd01,ssd=/mnt/ssd02,mem=1073741824
`,
"max-results": `
Define the maximum number of results that will be retrieved.
`,
"balance-mode": `
Determines the criteria used by nodes to make balanced allocation
decisions. Valid options are "usage" (default) or "rangecount".
`,
"password": `
The created user's password. If provided, disables prompting. Pass '-' to provide
the password on standard input.
`,
}
func normalizeStdFlagName(s string) string {
return strings.Replace(s, "_", "-", -1)
}
// initFlags sets the server.Context values to flag values.
// Keep in sync with "server/context.go". Values in Context should be
// settable here.
func initFlags(ctx *server.Context) {
// Map any flags registered in the standard "flag" package into the
// top-level cockroach command.
pf := cockroachCmd.PersistentFlags()
flag.VisitAll(func(f *flag.Flag) {
pf.Var(pflagValue{f.Value}, normalizeStdFlagName(f.Name), f.Usage)
})
{
f := startCmd.Flags()
f.BoolVar(&ctx.EphemeralSingleNode, "dev", ctx.EphemeralSingleNode, flagUsage["dev"])
// Server flags.
f.StringVar(&ctx.Addr, "addr", ctx.Addr, flagUsage["addr"])
f.StringVar(&ctx.PGAddr, "pgaddr", ctx.PGAddr, flagUsage["pgaddr"])
f.StringVar(&ctx.Attrs, "attrs", ctx.Attrs, flagUsage["attrs"])
f.StringVar(&ctx.Stores, "stores", ctx.Stores, flagUsage["stores"])
f.DurationVar(&ctx.MaxOffset, "max-offset", ctx.MaxOffset, flagUsage["max-offset"])
f.DurationVar(&ctx.MetricsFrequency, "metrics-frequency", ctx.MetricsFrequency, flagUsage["metrics-frequency"])
f.Var(&ctx.BalanceMode, "balance-mode", flagUsage["balance-mode"])
// Security flags.
f.StringVar(&ctx.Certs, "certs", ctx.Certs, flagUsage["certs"])
f.BoolVar(&ctx.Insecure, "insecure", ctx.Insecure, flagUsage["insecure"])
// Cluster joining flags.
f.StringVar(&ctx.JoinUsing, "join", ctx.JoinUsing, flagUsage["join"])
// KV flags.
f.BoolVar(&ctx.Linearizable, "linearizable", ctx.Linearizable, flagUsage["linearizable"])
// Engine flags.
f.Int64Var(&ctx.CacheSize, "cache-size", ctx.CacheSize, flagUsage["cache-size"])
f.Int64Var(&ctx.MemtableBudget, "memtable-budget", ctx.MemtableBudget, flagUsage["memtable-budget"])
f.DurationVar(&ctx.ScanInterval, "scan-interval", ctx.ScanInterval, flagUsage["scan-interval"])
f.DurationVar(&ctx.ScanMaxIdleTime, "scan-max-idle-time", ctx.ScanMaxIdleTime, flagUsage["scan-max-idle-time"])
f.DurationVar(&ctx.TimeUntilStoreDead, "time-until-store-dead", ctx.TimeUntilStoreDead, flagUsage["time-until-store-dead"])
if err := startCmd.MarkFlagRequired("stores"); err != nil {
panic(err)
}
}
{
f := exterminateCmd.Flags()
f.StringVar(&ctx.Stores, "stores", ctx.Stores, flagUsage["stores"])
if err := exterminateCmd.MarkFlagRequired("stores"); err != nil {
panic(err)
}
}
for _, cmd := range certCmds {
f := cmd.Flags()
f.StringVar(&ctx.Certs, "certs", ctx.Certs, flagUsage["certs"])
f.IntVar(&keySize, "key-size", defaultKeySize, flagUsage["key-size"])
if err := cmd.MarkFlagRequired("certs"); err != nil {
panic(err)
}
if err := cmd.MarkFlagRequired("key-size"); err != nil {
panic(err)
}
}
setUserCmd.Flags().StringVar(&password, "password", "", flagUsage["password"])
clientCmds := []*cobra.Command{
sqlShellCmd, kvCmd, rangeCmd,
userCmd, zoneCmd, nodeCmd,
exterminateCmd, quitCmd, /* startCmd is covered above */
}
for _, cmd := range clientCmds {
f := cmd.PersistentFlags()
f.BoolVar(&context.EphemeralSingleNode, "dev", context.EphemeralSingleNode, flagUsage["dev"])
f.StringVar(&ctx.Addr, "addr", ctx.Addr, flagUsage["addr"])
f.BoolVar(&ctx.Insecure, "insecure", ctx.Insecure, flagUsage["insecure"])
f.StringVar(&ctx.Certs, "certs", ctx.Certs, flagUsage["certs"])
}
// Max results flag for scan, reverse scan, and range list.
for _, cmd := range []*cobra.Command{scanCmd, reverseScanCmd, lsRangesCmd} {
f := cmd.Flags()
f.Int64Var(&maxResults, "max-results", 1000, flagUsage["max-results"])
}
}
func init() {
initFlags(context)
cobra.OnInitialize(func() {
if context.EphemeralSingleNode {
context.Insecure = true
}
})
}