-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcockroach.go
705 lines (621 loc) · 20.8 KB
/
cockroach.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package install
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/cockroachdb/cockroach/pkg/cmd/roachprod/config"
"github.com/cockroachdb/cockroach/pkg/cmd/roachprod/ssh"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/version"
"github.com/cockroachdb/errors"
)
// StartOpts TODO(peter): document
var StartOpts struct {
Encrypt bool
Sequential bool
SkipInit bool
StoreCount int
}
// Cockroach TODO(peter): document
type Cockroach struct{}
func cockroachNodeBinary(c *SyncedCluster, node int) string {
if filepath.IsAbs(config.Binary) {
return config.Binary
}
if !c.IsLocal() {
return "./" + config.Binary
}
path := filepath.Join(fmt.Sprintf(os.ExpandEnv("${HOME}/local/%d"), node), config.Binary)
if _, err := os.Stat(path); err == nil {
return path
}
// For "local" clusters we have to find the binary to run and translate it to
// an absolute path. First, look for the binary in PATH.
path, err := exec.LookPath(config.Binary)
if err != nil {
if strings.HasPrefix(config.Binary, "/") {
return config.Binary
}
// We're unable to find the binary in PATH and "binary" is a relative path:
// look in the cockroach repo.
gopath := os.Getenv("GOPATH")
if gopath == "" {
return config.Binary
}
path = gopath + "/src/github.com/cockroachdb/cockroach/" + config.Binary
var err2 error
path, err2 = exec.LookPath(path)
if err2 != nil {
return config.Binary
}
}
path, err = filepath.Abs(path)
if err != nil {
return config.Binary
}
return path
}
func getCockroachVersion(c *SyncedCluster, node int) (*version.Version, error) {
sess, err := c.newSession(node)
if err != nil {
return nil, err
}
defer sess.Close()
var verString string
cmd := cockroachNodeBinary(c, node) + " version"
out, err := sess.CombinedOutput(cmd + " --build-tag")
if err != nil {
// The --build-tag may not be supported. Try without.
// Note: this way to extract the version number is brittle.
// It should be removed once 'roachprod' is not used
// to invoke pre-v20.2 binaries any more.
sess, err := c.newSession(node)
if err != nil {
return nil, err
}
defer sess.Close()
out, err = sess.CombinedOutput(cmd)
if err != nil {
return nil, errors.Wrapf(err, "~ %s\n%s", cmd, out)
}
matches := regexp.MustCompile(`(?m)^Build Tag:\s+(.*)$`).FindSubmatch(out)
if len(matches) != 2 {
return nil, fmt.Errorf("unable to parse cockroach version output:%s", out)
}
verString = string(matches[1])
} else {
verString = strings.TrimSpace(string(out))
}
return version.Parse(verString)
}
// GetAdminUIPort returns the admin UI port for ths specified RPC port.
func GetAdminUIPort(connPort int) int {
return connPort + 1
}
func argExists(args []string, target string) int {
for i, arg := range args {
if arg == target || strings.HasPrefix(arg, target+"=") {
return i
}
}
return -1
}
// Start implements the ClusterImpl.NodeDir interface, and powers `roachprod
// start`. Starting the first node is special-cased quite a bit, it's used to
// distribute certs, set cluster settings, and initialize the cluster. Also,
// if we're only starting a single node in the cluster and it happens to be the
// "first" node (node 1, as understood by SyncedCluster.ServerNodes), we use
// `start-single-node` (this was written to provide a short hand to start a
// single node cluster with a replication factor of one).
func (r Cockroach) Start(c *SyncedCluster, extraArgs []string) {
h := &crdbInstallHelper{c: c, r: r}
h.distributeCerts()
nodes := c.ServerNodes()
var parallelism = 0
if StartOpts.Sequential {
parallelism = 1
}
fmt.Printf("%s: starting nodes\n", c.Name)
c.Parallel("", len(nodes), parallelism, func(nodeIdx int) ([]byte, error) {
vers, err := getCockroachVersion(c, nodes[nodeIdx])
if err != nil {
return nil, err
}
// NB: if cockroach started successfully, we ignore the output as it is
// some harmless start messaging.
if _, err := h.startNode(nodeIdx, extraArgs, vers); err != nil {
return nil, err
}
// We reserve a few special operations (bootstrapping, and setting
// cluster settings) for node 1.
if node := nodes[nodeIdx]; node != 1 {
return nil, nil
}
// NB: The code blocks below are not parallelized, so it's safe for us
// to use fmt.Printf style logging.
// 1. We don't init invoked using `--skip-init`.
// 2. We don't init when invoking with `start-single-node`.
// 3. For nodes running <20.1, the --join flags are constructed in a
// manner such that the first node doesn't have any (see
// `generateStartArgs`),which prompts CRDB to auto-initialize. For
// nodes running >=20.1, we need to explicitly initialize.
if StartOpts.SkipInit {
return nil, nil
}
shouldInit := !h.useStartSingleNode(vers) && vers.AtLeast(version.MustParse("v20.1.0"))
if shouldInit {
fmt.Printf("%s: initializing cluster\n", h.c.Name)
initOut, err := h.initializeCluster(nodeIdx)
if err != nil {
log.Fatalf("unable to initialize cluster: %v", err)
}
if initOut != "" {
fmt.Println(initOut)
}
}
if !vers.AtLeast(version.MustParse("v20.1.0")) {
// Given #51897 remains unresolved, master-built roachprod is used
// to run roachtests against the 20.1 branch. Some of those
// roachtests test mixed-version clusters that start off at 19.2.
// Consequently, we manually add this `cluster-bootstrapped` file
// where roachprod expects to find it for already-initialized
// clusters. This is a pretty gross hack, that we should address by
// addressing #51897.
//
// TODO(irfansharif): Remove this once #51897 is resolved.
markBootstrap := fmt.Sprintf("touch %s/%s", h.c.Impl.NodeDir(h.c, nodes[nodeIdx], 1 /* storeIndex */), "cluster-bootstrapped")
cmdOut, err := h.run(nodeIdx, markBootstrap)
if err != nil {
log.Fatalf("unable to run cmd: %v", err)
}
if cmdOut != "" {
fmt.Println(cmdOut)
}
}
// We're sure to set cluster settings after having initialized the
// cluster.
fmt.Printf("%s: setting cluster settings\n", h.c.Name)
clusterSettingsOut, err := h.setClusterSettings(nodeIdx)
if err != nil {
log.Fatalf("unable to set cluster settings: %v", err)
}
if clusterSettingsOut != "" {
fmt.Println(clusterSettingsOut)
}
return nil, nil
})
}
// NodeDir implements the ClusterImpl.NodeDir interface.
func (Cockroach) NodeDir(c *SyncedCluster, index, storeIndex int) string {
if c.IsLocal() {
if storeIndex != 1 {
panic("Cockroach.NodeDir only supports one store for local deployments")
}
return os.ExpandEnv(fmt.Sprintf("${HOME}/local/%d/data", index))
}
return fmt.Sprintf("/mnt/data%d/cockroach", storeIndex)
}
// LogDir implements the ClusterImpl.NodeDir interface.
func (Cockroach) LogDir(c *SyncedCluster, index int) string {
dir := "${HOME}/logs"
if c.IsLocal() {
dir = os.ExpandEnv(fmt.Sprintf("${HOME}/local/%d/logs", index))
}
return dir
}
// CertsDir implements the ClusterImpl.NodeDir interface.
func (Cockroach) CertsDir(c *SyncedCluster, index int) string {
dir := "${HOME}/certs"
if c.IsLocal() {
dir = os.ExpandEnv(fmt.Sprintf("${HOME}/local/%d/certs", index))
}
return dir
}
// NodeURL implements the ClusterImpl.NodeDir interface.
func (Cockroach) NodeURL(c *SyncedCluster, host string, port int) string {
url := fmt.Sprintf("'postgres://root@%s:%d", host, port)
if c.Secure {
url += "?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&" +
"sslrootcert=certs%2Fca.crt&sslmode=verify-full"
} else {
url += "?sslmode=disable"
}
url += "'"
return url
}
// NodePort implements the ClusterImpl.NodeDir interface.
func (Cockroach) NodePort(c *SyncedCluster, index int) int {
const basePort = 26257
port := basePort
if c.IsLocal() {
port += (index - 1) * 2
}
return port
}
// NodeUIPort implements the ClusterImpl.NodeDir interface.
func (r Cockroach) NodeUIPort(c *SyncedCluster, index int) int {
return GetAdminUIPort(r.NodePort(c, index))
}
// SQL implements the ClusterImpl.NodeDir interface.
func (r Cockroach) SQL(c *SyncedCluster, args []string) error {
if len(args) == 0 || len(c.Nodes) == 1 {
// If no arguments, we're going to get an interactive SQL shell. Require
// exactly one target and ask SSH to provide a pseudoterminal.
if len(args) == 0 && len(c.Nodes) != 1 {
return fmt.Errorf("invalid number of nodes for interactive sql: %d", len(c.Nodes))
}
url := r.NodeURL(c, "localhost", r.NodePort(c, c.Nodes[0]))
binary := cockroachNodeBinary(c, c.Nodes[0])
allArgs := []string{binary, "sql", "--url", url}
allArgs = append(allArgs, ssh.Escape(args))
return c.SSH([]string{"-t"}, allArgs)
}
// Otherwise, assume the user provided the "-e" flag, so we can reasonably
// execute the query on all specified nodes.
type result struct {
node int
output string
}
resultChan := make(chan result, len(c.Nodes))
display := fmt.Sprintf("%s: executing sql", c.Name)
c.Parallel(display, len(c.Nodes), 0, func(nodeIdx int) ([]byte, error) {
sess, err := c.newSession(c.Nodes[nodeIdx])
if err != nil {
return nil, err
}
defer sess.Close()
var cmd string
if c.IsLocal() {
cmd = fmt.Sprintf(`cd ${HOME}/local/%d ; `, c.Nodes[nodeIdx])
}
cmd += cockroachNodeBinary(c, c.Nodes[nodeIdx]) + " sql --url " +
r.NodeURL(c, "localhost", r.NodePort(c, c.Nodes[nodeIdx])) + " " +
ssh.Escape(args)
out, err := sess.CombinedOutput(cmd)
if err != nil {
return nil, errors.Wrapf(err, "~ %s\n%s", cmd, out)
}
resultChan <- result{node: c.Nodes[nodeIdx], output: string(out)}
return nil, nil
})
results := make([]result, 0, len(c.Nodes))
for range c.Nodes {
results = append(results, <-resultChan)
}
sort.Slice(results, func(i, j int) bool {
return results[i].node < results[j].node
})
for _, r := range results {
fmt.Printf("node %d:\n%s", r.node, r.output)
}
return nil
}
type crdbInstallHelper struct {
c *SyncedCluster
r Cockroach
}
func (h *crdbInstallHelper) startNode(
nodeIdx int, extraArgs []string, vers *version.Version,
) (string, error) {
startCmd, err := h.generateStartCmd(nodeIdx, extraArgs, vers)
if err != nil {
return "", err
}
nodes := h.c.ServerNodes()
sess, err := h.c.newSession(nodes[nodeIdx])
if err != nil {
return "", err
}
defer sess.Close()
out, err := sess.CombinedOutput(startCmd)
if err != nil {
return "", errors.Wrapf(err, "~ %s\n%s", startCmd, out)
}
return strings.TrimSpace(string(out)), nil
}
func (h *crdbInstallHelper) generateStartCmd(
nodeIdx int, extraArgs []string, vers *version.Version,
) (string, error) {
args, err := h.generateStartArgs(nodeIdx, extraArgs, vers)
if err != nil {
return "", err
}
// For a one-node cluster, use `start-single-node` to disable replication.
// For everything else we'll fall back to using `cockroach start`.
var startCmd string
if h.useStartSingleNode(vers) {
startCmd = "start-single-node"
} else {
startCmd = "start"
}
nodes := h.c.ServerNodes()
logDir := h.c.Impl.LogDir(h.c, nodes[nodeIdx])
binary := cockroachNodeBinary(h.c, nodes[nodeIdx])
keyCmd := h.generateKeyCmd(nodeIdx, extraArgs)
// NB: this is awkward as when the process fails, the test runner will show an
// unhelpful empty error (since everything has been redirected away). This is
// unfortunately equally awkward to address.
cmd := fmt.Sprintf(`
ulimit -c unlimited; mkdir -p %[1]s;
echo ">>> roachprod start: $(date)" >> %[1]s/roachprod.log;
ps axeww -o pid -o command >> %[1]s/roachprod.log;
[ -x /usr/bin/lslocks ] && /usr/bin/lslocks >> %[1]s/roachprod.log; %[2]s
export ROACHPROD=%[3]d%[4]s;
GOTRACEBACK=crash COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING=1 %[5]s \
%[6]s %[7]s %[8]s >> %[1]s/cockroach.stdout.log \
2>> %[1]s/cockroach.stderr.log \
|| (x=$?; cat %[1]s/cockroach.stderr.log; exit $x)`,
logDir, // [1]
keyCmd, // [2]
nodes[nodeIdx], // [3]
h.c.Tag, // [4]
h.getEnvVars(), // [5]
binary, // [6]
startCmd, // [7]
strings.Join(args, " "), // [8]
)
return cmd, nil
}
func (h *crdbInstallHelper) generateStartArgs(
nodeIdx int, extraArgs []string, vers *version.Version,
) ([]string, error) {
var args []string
nodes := h.c.ServerNodes()
args = append(args, "--background")
if h.c.Secure {
args = append(args, "--certs-dir="+h.c.Impl.CertsDir(h.c, nodes[nodeIdx]))
} else {
args = append(args, "--insecure")
}
var storeDirs []string
if idx := argExists(extraArgs, "--store"); idx == -1 {
for i := 1; i <= StartOpts.StoreCount; i++ {
storeDir := h.c.Impl.NodeDir(h.c, nodes[nodeIdx], i)
storeDirs = append(storeDirs, storeDir)
args = append(args, "--store=path="+storeDir)
}
} else {
storeDir := strings.TrimPrefix(extraArgs[idx], "--store=")
storeDirs = append(storeDirs, storeDir)
}
if StartOpts.Encrypt {
// Encryption at rest is turned on for the cluster.
for _, storeDir := range storeDirs {
// TODO(windchan7): allow key size to be specified through flags.
encryptArgs := "--enterprise-encryption=path=%s,key=%s/aes-128.key,old-key=plain"
encryptArgs = fmt.Sprintf(encryptArgs, storeDir, storeDir)
args = append(args, encryptArgs)
}
}
logDir := h.c.Impl.LogDir(h.c, nodes[nodeIdx])
if vers.AtLeast(version.MustParse("v21.1.0-alpha.0")) {
// Specify exit-on-error=false to work around #62763.
args = append(args, `--log "file-defaults: {dir: '`+logDir+`', exit-on-error: false}"`)
} else {
args = append(args, "--log-dir="+logDir)
}
if vers.AtLeast(version.MustParse("v1.1.0")) {
cache := 25
if h.c.IsLocal() {
cache /= len(nodes)
if cache == 0 {
cache = 1
}
}
args = append(args, fmt.Sprintf("--cache=%d%%", cache))
args = append(args, fmt.Sprintf("--max-sql-memory=%d%%", cache))
}
if h.c.IsLocal() {
// This avoids annoying firewall prompts on Mac OS X.
if vers.AtLeast(version.MustParse("v2.1.0")) {
args = append(args, "--listen-addr=127.0.0.1")
} else {
args = append(args, "--host=127.0.0.1")
}
}
port := h.r.NodePort(h.c, nodes[nodeIdx])
args = append(args, fmt.Sprintf("--port=%d", port))
args = append(args, fmt.Sprintf("--http-port=%d", GetAdminUIPort(port)))
if locality := h.c.locality(nodes[nodeIdx]); locality != "" {
if idx := argExists(extraArgs, "--locality"); idx == -1 {
args = append(args, "--locality="+locality)
}
}
if !h.useStartSingleNode(vers) {
// --join flags are unsupported/unnecessary in `cockroach
// start-single-node`. That aside, setting up --join flags is a bit
// precise. We have every node point to node 1. For clusters running
// <20.1, we have node 1 not point to anything (which in turn is used to
// trigger auto-initialization node 1). For clusters running >=20.1,
// node 1 also points to itself, and an explicit `cockroach init` is
// needed.
if nodes[nodeIdx] != 1 || vers.AtLeast(version.MustParse("v20.1.0")) {
args = append(args, fmt.Sprintf("--join=%s:%d", h.c.host(1), h.r.NodePort(h.c, 1)))
}
}
if h.shouldAdvertisePublicIP() {
args = append(args, fmt.Sprintf("--advertise-host=%s", h.c.host(nodeIdx+1)))
} else if !h.c.IsLocal() {
// Explicitly advertise by IP address so that we don't need to
// deal with cross-region name resolution. The `hostname -I`
// prints all IP addresses for the host and then we'll select
// the first from the list.
args = append(args, "--advertise-host=$(hostname -I | awk '{print $1}')")
}
// Argument template expansion is node specific (e.g. for {store-dir}).
e := expander{
node: nodes[nodeIdx],
}
for _, arg := range extraArgs {
expandedArg, err := e.expand(h.c, arg)
if err != nil {
return nil, err
}
args = append(args, strings.Split(expandedArg, " ")...)
}
return args, nil
}
func (h *crdbInstallHelper) initializeCluster(nodeIdx int) (string, error) {
nodes := h.c.ServerNodes()
initCmd := h.generateInitCmd(nodeIdx)
sess, err := h.c.newSession(nodes[nodeIdx])
if err != nil {
return "", err
}
defer sess.Close()
out, err := sess.CombinedOutput(initCmd)
if err != nil {
return "", errors.Wrapf(err, "~ %s\n%s", initCmd, out)
}
return strings.TrimSpace(string(out)), nil
}
func (h *crdbInstallHelper) setClusterSettings(nodeIdx int) (string, error) {
nodes := h.c.ServerNodes()
clusterSettingCmd := h.generateClusterSettingCmd(nodeIdx)
sess, err := h.c.newSession(nodes[nodeIdx])
if err != nil {
return "", err
}
defer sess.Close()
out, err := sess.CombinedOutput(clusterSettingCmd)
if err != nil {
return "", errors.Wrapf(err, "~ %s\n%s", clusterSettingCmd, out)
}
return strings.TrimSpace(string(out)), nil
}
func (h *crdbInstallHelper) generateClusterSettingCmd(nodeIdx int) string {
nodes := h.c.ServerNodes()
license := envutil.EnvOrDefaultString("COCKROACH_DEV_LICENSE", "")
if license == "" {
fmt.Printf("%s: COCKROACH_DEV_LICENSE unset: enterprise features will be unavailable\n",
h.c.Name)
}
var clusterSettingCmd string
if h.c.IsLocal() {
clusterSettingCmd = `cd ${HOME}/local/1 ; `
}
binary := cockroachNodeBinary(h.c, nodes[nodeIdx])
path := fmt.Sprintf("%s/%s", h.c.Impl.NodeDir(h.c, nodes[nodeIdx], 1 /* storeIndex */), "settings-initialized")
url := h.r.NodeURL(h.c, "localhost", h.r.NodePort(h.c, 1))
// We ignore failures to set remote_debugging.mode, which was
// removed in v21.2.
clusterSettingCmd += fmt.Sprintf(`
if ! test -e %s ; then
COCKROACH_CONNECT_TIMEOUT=0 %s sql --url %s -e "SET CLUSTER SETTING server.remote_debugging.mode = 'any'" || true;
COCKROACH_CONNECT_TIMEOUT=0 %s sql --url %s -e "
SET CLUSTER SETTING cluster.organization = 'Cockroach Labs - Production Testing';
SET CLUSTER SETTING enterprise.license = '%s';" \
&& touch %s
fi`, path, binary, url, binary, url, license, path)
return clusterSettingCmd
}
func (h *crdbInstallHelper) generateInitCmd(nodeIdx int) string {
nodes := h.c.ServerNodes()
var initCmd string
if h.c.IsLocal() {
initCmd = `cd ${HOME}/local/1 ; `
}
path := fmt.Sprintf("%s/%s", h.c.Impl.NodeDir(h.c, nodes[nodeIdx], 1 /* storeIndex */), "cluster-bootstrapped")
url := h.r.NodeURL(h.c, "localhost", h.r.NodePort(h.c, nodes[nodeIdx]))
binary := cockroachNodeBinary(h.c, nodes[nodeIdx])
initCmd += fmt.Sprintf(`
if ! test -e %[1]s ; then
COCKROACH_CONNECT_TIMEOUT=0 %[2]s init --url %[3]s && touch %[1]s
fi`, path, binary, url)
return initCmd
}
func (h *crdbInstallHelper) generateKeyCmd(nodeIdx int, extraArgs []string) string {
if !StartOpts.Encrypt {
return ""
}
nodes := h.c.ServerNodes()
var storeDirs []string
if idx := argExists(extraArgs, "--store"); idx == -1 {
for i := 1; i <= StartOpts.StoreCount; i++ {
storeDir := h.c.Impl.NodeDir(h.c, nodes[nodeIdx], i)
storeDirs = append(storeDirs, storeDir)
}
} else {
storeDir := strings.TrimPrefix(extraArgs[idx], "--store=")
storeDirs = append(storeDirs, storeDir)
}
// Command to create the store key.
var keyCmd strings.Builder
for _, storeDir := range storeDirs {
fmt.Fprintf(&keyCmd, `
mkdir -p %[1]s;
if [ ! -e %[1]s/aes-128.key ]; then
openssl rand -out %[1]s/aes-128.key 48;
fi;`, storeDir)
}
return keyCmd.String()
}
func (h *crdbInstallHelper) useStartSingleNode(vers *version.Version) bool {
return len(h.c.VMs) == 1 && vers.AtLeast(version.MustParse("v19.2.0"))
}
// distributeCerts, like the name suggests, distributes certs if it's a secure
// cluster and we're starting n1.
func (h *crdbInstallHelper) distributeCerts() {
for _, node := range h.c.ServerNodes() {
if node == 1 && h.c.Secure {
h.c.DistributeCerts()
break
}
}
}
func (h *crdbInstallHelper) shouldAdvertisePublicIP() bool {
// If we're creating nodes that span VPC (e.g. AWS multi-region or
// multi-cloud), we'll tell the nodes to advertise their public IPs
// so that attaching nodes to the cluster Just Works.
for i, vpc := range h.c.VPCs {
if i > 0 && vpc != h.c.VPCs[0] {
return true
}
}
return false
}
func (h *crdbInstallHelper) getEnvVars() string {
var buf strings.Builder
for _, v := range os.Environ() {
if strings.HasPrefix(v, "COCKROACH_") {
if buf.Len() > 0 {
buf.WriteString(" ")
}
buf.WriteString(v)
}
}
if len(h.c.Env) > 0 {
if buf.Len() > 0 {
buf.WriteString(" ")
}
buf.WriteString(h.c.Env)
}
return buf.String()
}
func (h *crdbInstallHelper) run(nodeIdx int, cmd string) (string, error) {
nodes := h.c.ServerNodes()
sess, err := h.c.newSession(nodes[nodeIdx])
if err != nil {
return "", err
}
defer sess.Close()
out, err := sess.CombinedOutput(cmd)
if err != nil {
return "", errors.Wrapf(err, "~ %s\n%s", cmd, out)
}
return strings.TrimSpace(string(out)), nil
}