Skip to content

Commit c50c8c4

Browse files
committed
feat(cmd/ping): Add gobble ping which ping connection to all hosts using SSH
1 parent 7d27cb6 commit c50c8c4

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

cmd/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
_ "github.com/sikalabs/gobble/cmd/list_schema_versions"
5+
_ "github.com/sikalabs/gobble/cmd/ping"
56
"github.com/sikalabs/gobble/cmd/root"
67
_ "github.com/sikalabs/gobble/cmd/run"
78
_ "github.com/sikalabs/gobble/cmd/utils"

cmd/ping/ping.go

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package ping
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
10+
"github.com/mohae/deepcopy"
11+
"github.com/sikalabs/gobble/cmd/root"
12+
"github.com/sikalabs/gobble/pkg/config"
13+
"github.com/sikalabs/gobble/pkg/libtask"
14+
"github.com/sikalabs/gobble/pkg/task/lib/echo"
15+
"github.com/spf13/cobra"
16+
"gopkg.in/yaml.v2"
17+
)
18+
19+
var FlagConfigFilePath string
20+
21+
var Cmd = &cobra.Command{
22+
Use: "ping",
23+
Short: "Ping all hosts from gobblefile",
24+
Args: cobra.NoArgs,
25+
Run: func(c *cobra.Command, args []string) {
26+
shellReturnCode := 0
27+
conf, err := readConfigFile(FlagConfigFilePath)
28+
if err != nil {
29+
log.Fatalln(err)
30+
}
31+
32+
if conf.Meta.SchemaVersion != 3 {
33+
log.Fatalln(fmt.Errorf("unsupported schema version: %d", conf.Meta.SchemaVersion))
34+
}
35+
36+
conf.AllHosts = conf.Hosts
37+
38+
for hostAliasName, hostAliases := range conf.HostsAliases {
39+
for _, hostAlias := range hostAliases {
40+
conf.AllHosts[hostAliasName] = append(conf.AllHosts[hostAliasName], conf.Hosts[hostAlias]...)
41+
}
42+
}
43+
44+
allHosts := map[string]config.ConfigHost{}
45+
46+
for _, hosts := range conf.AllHosts {
47+
for _, host := range hosts {
48+
allHosts[host.SSHTarget] = host
49+
}
50+
}
51+
52+
for _, host := range allHosts {
53+
ti := libtask.TaskInput{
54+
SSHTarget: host.SSHTarget,
55+
SSHPassword: host.SSHPassword,
56+
SSHOptions: host.SSHOptions,
57+
SudoPassword: host.SudoPassword,
58+
Config: conf,
59+
NoStrictHostKeyChecking: conf.Global.NoStrictHostKeyChecking,
60+
Sudo: false,
61+
Vars: mergeMaps(conf.Global.Vars, host.Vars),
62+
Dry: false,
63+
Quiet: false,
64+
}
65+
taskParams := echo.TaskEcho{
66+
Message: "ping",
67+
}
68+
69+
fmt.Printf("Ping %s using SSH ...", host.SSHTarget)
70+
out := echo.Run(ti, taskParams)
71+
isOK := out.Error == nil
72+
if isOK {
73+
fmt.Printf(" OK\n")
74+
} else {
75+
fmt.Printf(" ERR\n")
76+
shellReturnCode = 1
77+
}
78+
}
79+
os.Exit(shellReturnCode)
80+
},
81+
}
82+
83+
func init() {
84+
root.Cmd.AddCommand(Cmd)
85+
Cmd.Flags().StringVarP(
86+
&FlagConfigFilePath,
87+
"config",
88+
"c",
89+
"gobblefile.yml",
90+
"Path to config file, \"-\" for STDIN",
91+
)
92+
}
93+
94+
func readConfigFile(configFilePath string) (config.Config, error) {
95+
var buf []byte
96+
var err error
97+
c := config.Config{}
98+
99+
if configFilePath == "-" {
100+
// Read from stdin
101+
buf, err = ioutil.ReadAll(bufio.NewReader(os.Stdin))
102+
if err != nil {
103+
return c, err
104+
}
105+
} else {
106+
// Read from file
107+
buf, err = ioutil.ReadFile(configFilePath)
108+
if err != nil {
109+
return c, err
110+
}
111+
}
112+
113+
_ = yaml.Unmarshal(buf, &c)
114+
if err != nil {
115+
return c, err
116+
}
117+
118+
return c, nil
119+
}
120+
121+
func mergeMaps(m1, m2 map[string]interface{}) map[string]interface{} {
122+
if m1 == nil {
123+
m1 = make(map[string]interface{})
124+
}
125+
deepCopyM1 := deepcopy.Copy(m1).(map[string]interface{})
126+
for k, v := range m2 {
127+
deepCopyM1[k] = v
128+
}
129+
return deepCopyM1
130+
}

0 commit comments

Comments
 (0)