forked from pressly/sup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
138 lines (126 loc) · 2.72 KB
/
task.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
package sup
import (
"fmt"
"io"
"io/ioutil"
"os"
)
// Task represents a set of commands to be run.
type Task struct {
Run string
Input io.Reader
Clients []Client
}
func CreateTasks(cmd *Command, clients []Client, env string) ([]*Task, error) {
var tasks []*Task
// Anything to upload?
for _, upload := range cmd.Upload {
task := Task{
Run: RemoteTarCommand(upload.Dst),
Input: NewTarStreamReader(upload.Src, upload.Exc, env),
}
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, err
}
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
task := Task{
Run: string(data),
}
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)
}
}
// Local command.
if cmd.Local != "" {
local := &LocalhostClient{
env: env + `export SUP_HOST="localhost";`,
}
local.Connect("localhost")
task := &Task{
Run: cmd.Local,
Clients: []Client{local},
}
if cmd.Stdin {
task.Input = os.Stdin
}
tasks = append(tasks, task)
}
// Remote command.
if cmd.Run != "" {
task := Task{
Run: cmd.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)
}