/
ping.go
65 lines (58 loc) · 1.6 KB
/
ping.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
package main
import (
"fmt"
"time"
"github.com/seveas/herd"
"github.com/seveas/herd/ssh"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var pingCmd = &cobra.Command{
Use: "ping [options] glob [filters] [<+|-> glob [filters]...]",
Short: "Connect to hosts and check if they are alive",
Example: " herd ping *.site2.example.com os=Debian",
DisableFlagsInUseLine: true,
RunE: runPing,
}
func init() {
rootCmd.AddCommand(pingCmd)
}
func runPing(cmd *cobra.Command, args []string) error {
if !rootCmd.PersistentFlags().Lookup("output").Changed {
viper.Set("Output", herd.OutputInline)
}
splitAt := cmd.ArgsLenAtDash()
if splitAt != -1 {
return fmt.Errorf("Command provided, but ping mode doesn't support that")
}
cmd.SilenceErrors = true
cmd.SilenceUsage = true
executor, err := ssh.NewPingExecutor(viper.GetDuration("SshAgentTimeout"), *currentUser.user)
if err != nil {
bail(err.Error())
}
engine, err := setupScriptEngine(executor)
if err != nil {
return err
}
defer engine.End()
if err = engine.ParseCommandLine(args, splitAt); err != nil {
logrus.Error(err.Error())
return err
}
engine.Execute()
if len(args) == 0 {
hosts := engine.Registry.Search("*", []herd.MatchAttribute{}, []string{}, 0)
engine.Hosts.AddHosts(hosts)
}
oc := engine.Ui.OutputChannel()
pc := engine.Ui.ProgressChannel(time.Now().Add(engine.Runner.GetTimeout()))
hi, err := engine.Runner.Run("", pc, oc)
if err != nil {
logrus.Error(err.Error())
return nil
}
engine.Ui.PrintHistoryItem(hi)
return nil
}