2017-05-30 11:17:16 +02:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type List struct {
|
2017-05-30 16:39:14 +02:00
|
|
|
Command string `json:"cmd"`
|
2017-05-30 14:35:11 +02:00
|
|
|
Clients map[string]*ListResult `json:"clients"`
|
2017-05-30 11:17:16 +02:00
|
|
|
sshManager *Manager
|
|
|
|
}
|
|
|
|
type ListResult struct {
|
|
|
|
ssh *ssh.Client
|
2017-05-30 14:35:11 +02:00
|
|
|
Running bool `json:"running"`
|
|
|
|
WithError bool `json:"with_error"`
|
|
|
|
Result string `json:"result"`
|
2017-05-30 11:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) CreateList(cmd string) *List {
|
|
|
|
list := &List{
|
2017-05-30 16:39:14 +02:00
|
|
|
Command: cmd,
|
2017-05-30 11:17:16 +02:00
|
|
|
sshManager: m,
|
|
|
|
Clients: make(map[string]*ListResult),
|
|
|
|
}
|
|
|
|
for host, client := range m.clients {
|
2017-05-30 14:35:11 +02:00
|
|
|
list.Clients[host] = &ListResult{Running: true, ssh: client}
|
2017-05-30 11:17:16 +02:00
|
|
|
}
|
|
|
|
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()
|
2017-05-30 16:39:14 +02:00
|
|
|
result, err := l.sshManager.run(host, client.ssh, l.Command)
|
2017-05-30 14:35:11 +02:00
|
|
|
client.Running = false
|
2017-05-30 11:17:16 +02:00
|
|
|
if err != nil {
|
|
|
|
client.WithError = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.Result = SSHResultToString(result)
|
|
|
|
}
|