freifunkmanager/ssh/list.go

53 lines
1.0 KiB
Go
Raw Normal View History

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"`
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"`
}
func (m *Manager) CreateList(cmd string) *List {
list := &List{
2017-05-30 16:39:14 +02:00
Command: cmd,
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}
}
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
if err != nil {
client.WithError = true
return
}
client.Result = SSHResultToString(result)
}