/
deployment.go
65 lines (61 loc) · 1.6 KB
/
deployment.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
53
54
55
56
57
58
59
60
61
62
63
64
65
package client
import (
"github.com/dyntek-services-inc/goconfigure/inventory"
"github.com/dyntek-services-inc/goconfigure/render"
"log"
"os"
"path/filepath"
"strings"
)
type Deployment struct {
template string
loggingDirectory string
}
func NewDeployment(template, loggingDir string) Deployment {
return Deployment{
template: template,
loggingDirectory: loggingDir,
}
}
func (d *Deployment) Deploy(inv *inventory.Inventory, auth Authentication) error {
rc := make([]chan []string, len(inv.Hosts)) // The response channels.
for ih, host := range inv.Hosts {
log.Printf("starting deployment for %s", host.Hostname)
rc[ih] = make(chan []string)
handler, err := auth(host)
if err != nil {
return err
}
log.Printf("finished connecting to %s", host.Hostname)
go func(ro chan []string, h Handler, host inventory.Host) {
rtplc := render.Commands(host.Data, d.template)
cc := make([]chan string, len(rtplc))
for i, c := range rtplc {
cc[i] = make(chan string)
go func(co chan string, ci string) {
r, err := h.Send(ci)
if err != nil {
log.Fatal(err)
}
co <- r
}(cc[i], c)
}
cco := make([]string, len(rtplc))
for i, co := range cc {
cco[i] = <-co
}
log.Printf("finished deployment of %s", host.Hostname)
ro <- cco
}(rc[ih], handler, host)
}
for ri, ro := range rc {
rro := <-ro
tr := strings.Join(rro, "\n")
of := filepath.Join(d.loggingDirectory, inv.Hosts[ri].Hostname)
log.Printf("writing output to %s.txt", of)
if err := os.WriteFile(of+".txt", []byte(tr), 0666); err != nil {
return err
}
}
return nil
}