-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
52 lines (45 loc) · 1.05 KB
/
list.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
package ssh
import (
"sync"
"golang.org/x/crypto/ssh"
)
type List struct {
Command string `json:"cmd"`
Clients map[string]*ListResult `json:"clients"`
sshManager *Manager
}
type ListResult struct {
ssh *ssh.Client
Running bool `json:"running"`
WithError bool `json:"with_error"`
Result string `json:"result"`
}
func (m *Manager) CreateList(cmd string) *List {
list := &List{
Command: cmd,
sshManager: m,
Clients: make(map[string]*ListResult),
}
for host, client := range m.clients {
list.Clients[host] = &ListResult{Running: true, ssh: client}
}
return list
}
func (l List) Run() {
wg := new(sync.WaitGroup)
for host, client := range l.Clients {
wg.Add(1)
go l.runlistelement(host, client, wg)
}
wg.Wait()
}
func (l List) runlistelement(host string, client *ListResult, wg *sync.WaitGroup) {
defer wg.Done()
result, err := l.sshManager.run(host, client.ssh, l.Command)
client.Running = false
if err != nil {
client.WithError = true
return
}
client.Result = SSHResultToString(result)
}