-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
52 lines (45 loc) · 1007 Bytes
/
runner.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 bulkssh
// Runner the main ssh runner object of the bulkssh package
type Runner struct {
InCh chan *Request
OutCh chan *Request
}
// NewRunner instantiates a Runner object
func NewRunner(routines int) *Runner {
inch := make(chan *Request)
outch := make(chan *Request)
runner := &Runner{
inch,
outch,
}
for i := 0; i < routines; i++ {
go runner.requestListener()
}
return runner
}
func (r *Runner) requestListener() {
//fmt.Printf("dataFetcher-%d launched\n", id)
for {
select {
case req := <-r.InCh:
//fmt.Printf("dataFetcher-%d starting request for %s\n", id, req.host.hostname)
r.handleRequest(req)
}
}
}
func (r *Runner) handleRequest(req *Request) {
connection, err := sshInit(req)
if err != nil {
req.Error = err
} else {
defer sshDisconnect(connection)
for _, cmd := range req.Commands {
output, err := sshRun(connection, cmd, req.CommandTimeout)
cmd.Error = err
if output != nil {
cmd.Output = *output
}
}
}
r.OutCh <- req
}