-
Notifications
You must be signed in to change notification settings - Fork 1
/
book.go
159 lines (127 loc) · 3.36 KB
/
book.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
package play
import (
"io"
"io/ioutil"
"os"
"path"
"github.com/pkg/errors"
)
// Book represents a set of commands to be run.
type Book struct {
clients []Client
run string
input io.Reader
once bool
tty bool
}
func (play *Play) createBooks(clients []Client, cmd *Command, env string) (books []*Book, err error) {
var allBooks []*Book
// Upload.
// Always run upload first.
if len(cmd.Uploads) > 0 {
uploadBooks, uploadErr := play.createUploadBooks(clients, cmd.Uploads, env)
if uploadErr != nil {
err = errors.Wrap(uploadErr, "can't create upload book")
return
}
allBooks = append(allBooks, uploadBooks...)
}
// Script.
// Read the script as a multi lines input command.
if cmd.Script != "" {
data, ioerr := ioutil.ReadFile(path.Clean(cmd.Script))
if ioerr != nil {
err = errors.Wrap(ioerr, "can't read script")
return
}
// overwrite clients with local host connection
if cmd.Locally {
local := NewLocalClient(env)
local.Connect("localhost")
clients = []Client{local}
}
scriptBooks, scriptErr := play.createShellBooks(clients, string(data), env, cmd.Stdin)
if scriptErr != nil {
err = errors.Wrap(scriptErr, "can't create script book: "+string(data))
return
}
allBooks = append(allBooks, scriptBooks...)
}
// Command.
if cmd.Run != "" {
// overwrite clients with local host connection
if cmd.Locally {
local := NewLocalClient(env)
local.Connect("localhost")
clients = []Client{local}
}
shellBooks, shellErr := play.createShellBooks(clients, cmd.Run, env, cmd.Stdin)
if shellErr != nil {
err = errors.Wrap(shellErr, "can't create shell book: "+cmd.Run)
return
}
allBooks = append(allBooks, shellBooks...)
}
for _, book := range allBooks {
switch {
case cmd.Once: // TODO: try cmd over all of clients until one success?
book.clients = []Client{clients[0]}
book.once = cmd.Once
books = append(books, book)
case cmd.Serial > 0: // Each "serial" book client group is executed sequentially.
for i := 0; i < len(clients); i += cmd.Serial {
j := i + cmd.Serial
if j > len(clients) {
j = len(clients)
}
copy := *book
copy.clients = clients[i:j]
books = append(books, ©)
}
default:
book.clients = clients
books = append(books, book)
}
}
return
}
func (play *Play) createUploadBooks(clients []Client, uploads map[string]Upload, env string) (books []*Book, err error) {
cwd, err := os.Getwd()
if err != nil {
err = errors.Wrap(err, "os.Getwd() failed")
return
}
for _, upload := range uploads {
uploadFile, uploadErr := ResolveLocalPath(cwd, upload.Src, env)
if uploadErr != nil {
err = errors.Wrap(uploadErr, "resolve local path failed: "+upload.Src)
return
}
uploadTarReader, uploadTarErr := NewTarStreamReader(cwd, uploadFile, upload.Filter)
if uploadTarErr != nil {
err = errors.Wrap(uploadTarErr, "create tar stream of local path failed: "+upload.Src)
return
}
book := Book{
run: RemoteTarCommand(upload.Dst),
input: uploadTarReader,
tty: false,
}
books = append(books, &book)
}
return
}
func (play *Play) createShellBooks(clients []Client, shell, env string, stdin bool) (books []*Book, err error) {
book := Book{
run: shell,
tty: true,
}
if stdin {
book.input = os.Stdin
}
if play.debug {
book.run = "set -x;" + book.run
}
books = append(books, &book)
return
}