forked from pressly/sup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsup.go
197 lines (171 loc) · 4.56 KB
/
sup.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package sup
import (
"errors"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"github.com/goware/prefixer"
"golang.org/x/crypto/ssh"
)
const VERSION = "0.3"
type Stackup struct {
conf *Supfile
}
func New(conf *Supfile) (*Stackup, error) {
return &Stackup{
conf: conf,
}, nil
}
// Run runs set of commands on multiple hosts defined by network sequentially.
// TODO: This megamoth method needs a big refactor and should be split
// to multiple smaller methods.
func (sup *Stackup) Run(network *Network, commands ...*Command) error {
if len(commands) == 0 {
return errors.New("no commands to be run")
}
// Process all ENVs into a string of form
// `export FOO="bar"; export BAR="baz";`.
env := ``
for _, v := range append(sup.conf.Env, network.Env...) {
env += v.AsExport() + " "
}
// Create clients for every host (either SSH or Localhost).
var (
clients []Client
bastion *SSHClient
)
if network.Bastion != "" {
bastion = &SSHClient{}
if err := bastion.Connect(network.Bastion); err != nil {
return err
}
}
for i, host := range network.Hosts {
// Localhost client.
if host == "localhost" {
local := &LocalhostClient{
env: env + `export SUP_HOST="` + host + `";`,
}
if err := local.Connect(host); err != nil {
return err
}
clients = append(clients, local)
continue
}
// Add host ip
ips, err := net.LookupIP(host[strings.Index(host, "@")+1:])
if err == nil && len(ips) > 0 {
env += `export SUP_LOCAL_IP="` + ips[0].String() + `";`
}
// SSH client.
remote := &SSHClient{
env: env + `export SUP_HOST="` + host + `";`,
color: Colors[i%len(Colors)],
}
if bastion != nil {
if err := remote.ConnectWith(host, bastion.DialThrough); err != nil {
return err
}
} else {
if err := remote.Connect(host); err != nil {
return err
}
}
defer remote.Close()
clients = append(clients, remote)
}
maxLen := 0
for _, c := range clients {
_, prefixLen := c.Prefix()
if prefixLen > maxLen {
maxLen = prefixLen
}
}
// Run command or run multiple commands defined by target sequentially.
for _, cmd := range commands {
// Translate command into task(s).
tasks, err := CreateTasks(cmd, clients, env)
if err != nil {
return fmt.Errorf("CreateTasks(): %s", err)
}
// Run tasks sequentially.
for _, task := range tasks {
var writers []io.Writer
var wg sync.WaitGroup
// Run tasks on the provided clients.
for _, c := range task.Clients {
prefix, prefixLen := c.Prefix()
if len(prefix) < maxLen { // Left padding.
prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix
}
err := c.Run(task)
if err != nil {
return fmt.Errorf("%s%v", prefix, err)
}
// Copy over tasks's STDOUT.
wg.Add(1)
go func(c Client) {
defer wg.Done()
_, err := io.Copy(os.Stdout, prefixer.New(c.Stdout(), prefix))
if err != nil && err != io.EOF {
// TODO: io.Copy() should not return io.EOF at all.
// Upstream bug? Or prefixer.WriteTo() bug?
fmt.Fprintf(os.Stderr, "%sSTDOUT: %v", prefix, err)
}
}(c)
// Copy over tasks's STDERR.
wg.Add(1)
go func(c Client) {
defer wg.Done()
_, err := io.Copy(os.Stderr, prefixer.New(c.Stderr(), prefix))
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "%sSTDERR: %v", prefix, err)
}
}(c)
writers = append(writers, c.Stdin())
}
// Copy over task's STDIN.
if task.Input != nil {
go func() {
writer := io.MultiWriter(writers...)
_, err := io.Copy(writer, task.Input)
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "STDIN: %v", err)
}
// TODO: Use MultiWriteCloser (not in Stdlib), so we can writer.Close() instead?
for _, c := range clients {
c.WriteClose()
}
}()
}
// Wait for all I/O operations first.
wg.Wait()
// Make sure each client finishes the task, return on failure.
for _, c := range task.Clients {
wg.Add(1)
go func(c Client) {
defer wg.Done()
if err := c.Wait(); err != nil {
prefix, prefixLen := c.Prefix()
if len(prefix) < maxLen { // Left padding.
prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix
}
if e, ok := err.(*ssh.ExitError); ok && e.ExitStatus() != 15 {
// TODO: Store all the errors, and print them after Wait().
fmt.Fprintf(os.Stderr, "%sexit %v\n", prefix, e.ExitStatus())
os.Exit(e.ExitStatus())
}
fmt.Fprintf(os.Stderr, "%s%v\n", prefix, err)
os.Exit(1)
}
}(c)
}
// Wait for all commands to finish.
wg.Wait()
}
}
return nil
}