-
Notifications
You must be signed in to change notification settings - Fork 3
/
wormhole.go
134 lines (110 loc) · 3.06 KB
/
wormhole.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/mihaitodor/wormhole/config"
"github.com/mihaitodor/wormhole/inventory"
"github.com/mihaitodor/wormhole/playbook"
"github.com/mihaitodor/wormhole/transport"
log "github.com/sirupsen/logrus"
)
func Run(ctx context.Context, conf config.Config, playbook *playbook.Playbook, inventory inventory.Inventory) {
for start := 0; start < len(inventory); start += conf.MaxConcurrentConnections {
end := start + conf.MaxConcurrentConnections
if end > len(inventory) {
end = len(inventory)
}
log.Infof("Running playbook on servers: %s",
strings.Join(inventory[start:end].GetAllServers(nil), ", "),
)
// Open a ssh session to each server in the current batch
var connections []transport.Connection
for _, server := range inventory[start:end] {
conn, err := transport.NewConnection(server, conf.ConnectTimeout)
if err != nil {
err = fmt.Errorf("Failed to connect to server %q: %s", server.GetAddress(), err)
server.SetError(err)
log.Warn(err)
continue
}
connections = append(connections, conn)
}
if len(connections) == 0 {
continue
}
var wg sync.WaitGroup
wg.Add(len(connections))
for _, conn := range connections {
go playbook.Run(ctx, &wg, conn, conf)
}
wg.Wait()
// Close ssh clients
for _, conn := range connections {
err := conn.Close()
if err != nil {
log.Warnf(
"Failed to close ssh connection to server %q: %s",
conn.GetAddress(), err,
)
}
}
for _, server := range inventory[start:end] {
if server.GetError() == nil {
server.MarkFinished()
}
}
// Check if the user has requested cancellation
err := ctx.Err()
if err != nil {
log.Warnf("Skipping the rest of the hosts due to: %s", err)
return
}
}
}
func InitGracefulStop() context.Context {
gracefulStop := make(chan os.Signal, 1)
signal.Notify(gracefulStop, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sig := <-gracefulStop
log.Warnf("Received signal %q. Exiting as soon as possible!", sig)
cancel()
}()
return ctx
}
func main() {
conf := config.NewConfing()
inventory, err := inventory.NewInventory(conf.Inventory)
if err != nil {
log.Fatalf("Failed to load inventory: %s", err)
}
playbook, err := playbook.NewPlaybook(conf.Playbook)
if err != nil {
log.Fatalf("Failed to load playbook: %s", err)
}
ctx := InitGracefulStop()
Run(ctx, conf, playbook, inventory)
completed := inventory.GetAllCompletedServers()
if len(completed) > 0 {
log.Infof("Playbook ran successfully on servers: %s", strings.Join(completed, ", "))
}
failed := inventory.GetAllFailedServers()
if len(failed) > 0 {
log.Errorf("Playbook failed on servers: %s", strings.Join(failed, ", "))
}
skipped := inventory.GetAllPendingServers()
if len(skipped) > 0 {
log.Infof("Playbook didn't run on servers: %s", strings.Join(skipped, ", "))
}
select {
case <-ctx.Done():
log.Fatalf("Abnormal termination due to: %s", ctx.Err())
default:
// Exit normally
}
}