Skip to content

Commit cdc63c8

Browse files
author
Craig Goldstein
committed
WIP: need to fix error handling for goroutine
1 parent 1476299 commit cdc63c8

3 files changed

Lines changed: 50 additions & 38 deletions

File tree

yb-support-tool/cmd/exec.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/spf13/cobra"
@@ -19,8 +20,6 @@ var (
1920
isVerbose bool
2021
)
2122

22-
var inventories []exec.Inventory
23-
2423
var execCmd = &cobra.Command{
2524
Use: "exec",
2625
Short: "Run commands across all nodes in a universe",
@@ -38,8 +37,17 @@ var ybaCmd = &cobra.Command{
3837
Short: "Lookup inventory using YBA API",
3938
Args: cobra.NoArgs,
4039
Run: func(cmd *cobra.Command, args []string) {
41-
inventories = exec.YbaLookup(hostname, apiToken, isInsecure, isVerbose)
42-
exec.SshCmd(inventories, universe, user, command, isParallel, isVerbose)
40+
inventories, err := exec.YbaLookup(hostname, apiToken, isInsecure, isVerbose)
41+
if err != nil {
42+
fmt.Fprintln(os.Stderr, err)
43+
os.Exit(2)
44+
}
45+
46+
err = exec.SshCmd(inventories, universe, user, command, isParallel, isVerbose)
47+
if err != nil {
48+
fmt.Fprintln(os.Stderr, err)
49+
os.Exit(2)
50+
}
4351
},
4452
}
4553

@@ -48,8 +56,17 @@ var fileCmd = &cobra.Command{
4856
Short: "Lookup inventory using inventory file",
4957
Args: cobra.NoArgs,
5058
Run: func(cmd *cobra.Command, args []string) {
51-
inventories = exec.FileLookup(iFileName, isVerbose)
52-
exec.SshCmd(inventories, universe, user, command, isParallel, isVerbose)
59+
inventories, err := exec.FileLookup(iFileName, isVerbose)
60+
if err != nil {
61+
fmt.Fprintln(os.Stderr, err)
62+
os.Exit(2)
63+
}
64+
65+
err = exec.SshCmd(inventories, universe, user, command, isParallel, isVerbose)
66+
if err != nil {
67+
fmt.Fprintln(os.Stderr, err)
68+
os.Exit(2)
69+
}
5370
},
5471
}
5572

yb-support-tool/exec/inventory.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Inventory struct {
3535
} `json:"universeDetails" yaml:"universeDetails"`
3636
}
3737

38-
func YbaLookup(hostname, apiToken string, isInsecure, isVerbose bool) []Inventory {
38+
func YbaLookup(hostname, apiToken string, isInsecure, isVerbose bool) ([]Inventory, error) {
3939

4040
// yugaware-client returns the API response wrapped in "content: "
4141
type apiResp struct {
@@ -72,28 +72,25 @@ func YbaLookup(hostname, apiToken string, isInsecure, isVerbose bool) []Inventor
7272
// run yugaware-client command using cobra
7373
err := ywCommand.Execute()
7474
if err != nil {
75-
fmt.Println(err)
76-
os.Exit(1)
75+
return nil, err
7776
}
7877

7978
buffer, err := buf.Bytes(), err
8079

8180
if err != nil {
82-
fmt.Println(err)
83-
os.Exit(1)
81+
return nil, err
8482
}
8583

8684
err = json.Unmarshal(buffer, &content)
8785
if err != nil {
88-
fmt.Println(err)
89-
os.Exit(1)
86+
return nil, err
9087
}
9188

9289
// content.Content is the raw api response, which is "[]inventory"
93-
return content.Content
90+
return content.Content, nil
9491
}
9592

96-
func FileLookup(iFileName string, isVerbose bool) []Inventory {
93+
func FileLookup(iFileName string, isVerbose bool) ([]Inventory, error) {
9794

9895
var inventories []Inventory
9996

@@ -105,26 +102,24 @@ func FileLookup(iFileName string, isVerbose bool) []Inventory {
105102
inventoryFile, err := os.Open(f)
106103

107104
if err != nil {
108-
fmt.Println(err)
109-
os.Exit(1)
105+
return nil, err
110106
}
111107

112108
defer inventoryFile.Close()
113109

114110
inventoryBytes, err := io.ReadAll(inventoryFile)
115111

116112
if err != nil {
117-
fmt.Println(err)
118-
os.Exit(1)
113+
return nil, err
119114
}
120115

121116
// this will unmarshal both json and yaml properly
122117
err = yaml.Unmarshal(inventoryBytes, &inventories)
123118

124119
if err != nil {
125-
fmt.Printf("Unable to parse inventory file \"%s\": %s", iFileName, err)
126-
os.Exit(1)
120+
return nil, fmt.Errorf("Unable to parse inventory file \"%s\": %s", iFileName, err)
121+
127122
}
128123

129-
return inventories
124+
return inventories, nil
130125
}

yb-support-tool/exec/ssh.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@ import (
1111
"golang.org/x/crypto/ssh"
1212
)
1313

14-
func runSsh(conn *ssh.Client, cmd, nodeIdx, nodeName, nodePrivateIp string, isVerbose bool) []byte {
14+
func runSsh(conn *ssh.Client, cmd, nodeIdx, nodeName, nodePrivateIp string, isVerbose bool) error {
1515

1616
var res []byte
1717

1818
session, err := conn.NewSession()
1919

2020
if err != nil {
21-
fmt.Println(err)
22-
os.Exit(1)
21+
return err
2322
}
2423

2524
defer session.Close()
2625
res, err = session.CombinedOutput(cmd)
2726

2827
if err != nil {
29-
fmt.Println(err)
30-
os.Exit(1)
28+
return err
3129
}
3230

3331
output := string(res)
@@ -54,10 +52,10 @@ func runSsh(conn *ssh.Client, cmd, nodeIdx, nodeName, nodePrivateIp string, isVe
5452
fmt.Print(output)
5553
}
5654

57-
return res
55+
return nil
5856
}
5957

60-
func SshCmd(inventories []Inventory, universe, user, cmd string, isParallel bool, isVerbose bool) {
58+
func SshCmd(inventories []Inventory, universe, user, cmd string, isParallel bool, isVerbose bool) error {
6159

6260
var privateKeyPathPrefix = "/opt/yugabyte/yugaware/data/keys/"
6361
var privateKeyPath string
@@ -74,14 +72,12 @@ func SshCmd(inventories []Inventory, universe, user, cmd string, isParallel bool
7472
pemBytes, err := os.ReadFile(privateKeyPath)
7573

7674
if err != nil {
77-
fmt.Println(err)
78-
os.Exit(1)
75+
return err
7976
}
8077

8178
signer, err := ssh.ParsePrivateKey(pemBytes)
8279
if err != nil {
83-
fmt.Println(err)
84-
os.Exit(1)
80+
return err
8581
}
8682

8783
config := &ssh.ClientConfig{
@@ -120,21 +116,25 @@ func SshCmd(inventories []Inventory, universe, user, cmd string, isParallel bool
120116
conn, err := ssh.Dial("tcp", host, config)
121117

122118
if err != nil {
123-
fmt.Println(err)
124-
os.Exit(1)
119+
return err
125120
}
126121

127122
if isParallel {
128123
wg.Add(1)
129124
go func(wg *sync.WaitGroup) {
130-
runSsh(conn, cmd, nodeIdx, nodeName, nodePrivateIp, isVerbose)
125+
_ = runSsh(conn, cmd, nodeIdx, nodeName, nodePrivateIp, isVerbose)
131126
wg.Done()
132127
}(wg)
128+
133129
} else {
134-
runSsh(conn, cmd, nodeIdx, nodeName, nodePrivateIp, isVerbose)
130+
err := runSsh(conn, cmd, nodeIdx, nodeName, nodePrivateIp, isVerbose)
131+
if err != nil {
132+
return err
133+
}
135134
}
135+
wg.Wait()
136136
}
137-
wg.Wait()
138137
}
139138

139+
return nil
140140
}

0 commit comments

Comments
 (0)