X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Fsync.go;h=bee0133e4fd4326ef1610325fc6ae8649147da29;hb=fd97e8019e2ab166d9475ed59782c86247d8430b;hp=77e83c4187592ca1bd5cc31e44da3f4acfb4830d;hpb=5d6cc7f14a4bacc36bf3a23cd735a75ad4a90f1d;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 77e83c4..bee0133 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -20,9 +20,11 @@ package main import ( "flag" "fmt" + "io/fs" "log" "os" "os/signal" + "runtime" "sort" "strings" "sync" @@ -105,6 +107,10 @@ func MainSync(args []string) error { os.Exit(1) } + if runtime.GOOS == "windows" { + log.Print("WARNING: Windows support is experimental!") + } + cfg, allHosts, allGroups, err := LoadBaseFiles() if err != nil { return err @@ -122,7 +128,8 @@ func MainSync(args []string) error { return fmt.Errorf("no hosts found") } - isTTY := term.IsTerminal(int(os.Stdout.Fd())) + isTTY := term.IsTerminal(int(os.Stdout.Fd())) && + term.IsTerminal(int(os.Stderr.Fd())) done := make(chan bool) // Collect events from all hosts and print them @@ -226,10 +233,7 @@ func MainSync(args []string) error { func hostsToSync(names []string, allHosts *config.Hosts, allGroups map[string][]string) ([]*config.Host, error) { - detectedMap := make(map[string]bool) - for _, x := range config.TransitivelyDetectedGroups(allGroups) { - detectedMap[x] = true - } + detectedMap := config.TransitivelyDetectedGroups(allGroups) const detectedErr = ` @@ -249,13 +253,13 @@ are only available after the hosts were contacted. nameMatched := make(map[string]bool) // To detect typos we must check all given names but one host can be // matched by multiple names (e.g. two groups with overlapping hosts) - hostMatched := make(map[string]bool) + hostAdded := make(map[string]bool) var res []*config.Host for _, host := range allHosts.List { if nameMap[host.Name] { res = append(res, host) - hostMatched[host.Name] = true + hostAdded[host.Name] = true nameMatched[host.Name] = true } @@ -266,9 +270,9 @@ are only available after the hosts were contacted. } for _, x := range groups { if nameMap[x] { - if !hostMatched[host.Name] { + if !hostAdded[host.Name] { res = append(res, host) - hostMatched[host.Name] = true + hostAdded[host.Name] = true } nameMatched[x] = true } @@ -366,9 +370,25 @@ func (s *Sync) Host(wg *sync.WaitGroup) error { wg.Done() }() + helpers, err := fs.Sub(RemoteHelpers, "remote") + if err != nil { + conn.Kill() + return err + } + // Connect to remote host - err := conn.DialSSH(s.host.SshUser, s.host.Name, s.config.SshConfig) + user := s.host.SshUser + if user == "" { + user = s.config.SshUser + } + err = conn.DialSSH(rpc.SSHConfig{ + Host: s.host.Name, + User: user, + SshConfig: s.config.SshConfig, + RemoteHelpers: helpers, + }) if err != nil { + conn.Kill() return err } defer conn.Kill() @@ -402,9 +422,7 @@ func (s *Sync) Host(wg *sync.WaitGroup) error { return nil } -func (s *Sync) logf(level safcm.LogLevel, escaped bool, - format string, a ...interface{}) { - +func (s *Sync) log(level safcm.LogLevel, escaped bool, msg string) { if s.config.LogLevel < level { return } @@ -412,16 +430,16 @@ func (s *Sync) logf(level safcm.LogLevel, escaped bool, Host: s.host, Log: Log{ Level: level, - Text: fmt.Sprintf(format, a...), + Text: msg, }, Escaped: escaped, } } func (s *Sync) logDebugf(format string, a ...interface{}) { - s.logf(safcm.LogDebug, false, format, a...) + s.log(safcm.LogDebug, false, fmt.Sprintf(format, a...)) } func (s *Sync) logVerbosef(format string, a ...interface{}) { - s.logf(safcm.LogVerbose, false, format, a...) + s.log(safcm.LogVerbose, false, fmt.Sprintf(format, a...)) } // sendRecv sends a message over conn and waits for the response. Any MsgLog @@ -439,7 +457,7 @@ func (s *Sync) sendRecv(conn *rpc.Conn, msg safcm.Msg) (safcm.Msg, error) { } log, ok := x.(safcm.MsgLog) if ok { - s.logf(log.Level, false, "%s", log.Text) + s.log(log.Level, false, log.Text) continue } return x, nil