forked from pressly/sup
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.go
More file actions
155 lines (140 loc) · 3.2 KB
/
task.go
File metadata and controls
155 lines (140 loc) · 3.2 KB
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
package sup
import (
"fmt"
"io"
"os"
"github.com/pkg/errors"
)
// Task represents a set of commands to be run.
type Task struct {
Run string
Input io.Reader
Clients []Client
TTY bool
}
func (sup *Stackup) createTasks(cmd *Command, clients []Client, env string) ([]*Task, error) {
var tasks []*Task
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "resolving CWD failed")
}
// Anything to upload?
for _, upload := range cmd.Upload {
uploadFile, err := ResolveLocalPath(cwd, upload.Src, env)
if err != nil {
return nil, errors.Wrap(err, "upload: "+upload.Src)
}
uploadTarReader, err := NewTarStreamReader(cwd, uploadFile, upload.Exc)
if err != nil {
return nil, errors.Wrap(err, "upload: "+upload.Src)
}
task := Task{
Run: RemoteTarCommand(upload.Dst),
Input: uploadTarReader,
TTY: false,
}
if cmd.Once {
task.Clients = []Client{clients[0]}
tasks = append(tasks, &task)
} else if cmd.Serial > 0 {
// Each "serial" task 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 := task
copy.Clients = clients[i:j]
tasks = append(tasks, ©)
}
} else {
task.Clients = clients
tasks = append(tasks, &task)
}
}
// Script. Read the file as a multiline input command.
if cmd.Script != "" {
f, err := os.Open(cmd.Script)
if err != nil {
return nil, errors.Wrap(err, "can't open script")
}
data, err := io.ReadAll(f)
if err != nil {
return nil, errors.Wrap(err, "can't read script")
}
task := Task{
Run: string(data),
TTY: true,
}
if sup.debug {
task.Run = "set -x;" + task.Run
}
if cmd.Stdin {
task.Input = os.Stdin
}
if cmd.Once {
task.Clients = []Client{clients[0]}
tasks = append(tasks, &task)
} else if cmd.Serial > 0 {
// Each "serial" task 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 := task
copy.Clients = clients[i:j]
tasks = append(tasks, ©)
}
} else {
task.Clients = clients
tasks = append(tasks, &task)
}
}
var localClients []Client
if cmd.Local {
for _, cl := range clients {
localClients = append(localClients, ConvertClientToLocal(cl))
}
clients = localClients
}
// Remote command.
if cmd.Run != "" {
task := Task{
Run: cmd.Run,
TTY: true,
}
if sup.debug {
task.Run = "set -x;" + task.Run
}
if cmd.Stdin {
task.Input = os.Stdin
}
if cmd.Once {
task.Clients = []Client{clients[0]}
tasks = append(tasks, &task)
} else if cmd.Serial > 0 {
// Each "serial" task 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 := task
copy.Clients = clients[i:j]
tasks = append(tasks, ©)
}
} else {
task.Clients = clients
tasks = append(tasks, &task)
}
}
return tasks, nil
}
type ErrTask struct {
Task *Task
Reason string
}
func (e ErrTask) Error() string {
return fmt.Sprintf(`Run("%v"): %v`, e.Task, e.Reason)
}