forked from ohsu-comp-bio/funnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_vars.go
243 lines (213 loc) · 5.71 KB
/
parse_vars.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package run
import (
"errors"
"fmt"
"github.com/kballard/go-shellquote"
"github.com/ohsu-comp-bio/funnel/proto/tes"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
// ErrKeyFmt describes an error in input/output/env/tag flag formatting
var ErrKeyFmt = errors.New("Arguments passed to --in, --out and --env must be of the form: KEY=VALUE")
// ErrStorageScheme describes an error in supported storage URL schemes.
var ErrStorageScheme = errors.New("File paths must be prefixed with one of:\n file://\n gs://\n s3://")
// DuplicateKeyErr returns a new error describing conflicting keys for env. vars., inputs, and outputs.
func DuplicateKeyErr(key string) error {
return errors.New("Can't use the same KEY for multiple --in, --out, --env arguments: " + key)
}
// Parse CLI variable definitions (e.g "varname=value") into usable task values.
func valsToTask(vals flagVals) (task *tes.Task, err error) {
// Any error occurring during parsing the variables an preparing the task
// is a fatal error, so I'm using panic/recover to simplify error handling.
defer func() {
if x := recover(); x != nil {
err = x.(error)
}
}()
environ := map[string]string{}
// Build the task message
task = &tes.Task{
Name: vals.name,
Description: vals.description,
Resources: &tes.Resources{
CpuCores: uint32(vals.cpu),
RamGb: vals.ram,
DiskGb: vals.disk,
Zones: vals.zones,
Preemptible: vals.preemptible,
},
Tags: map[string]string{},
}
for _, vol := range vals.volumes {
if !strings.HasPrefix(vol, "/") {
panic(fmt.Errorf("volumes must be absolute paths: %s", vol))
}
task.Volumes = append(task.Volumes, vol)
}
// Create executors
for i, exec := range vals.execs {
// Split command string based on shell syntax.
cmd, _ := shellquote.Split(exec.cmd)
stdinPath := fmt.Sprintf("/opt/funnel/inputs/stdin-%d", i)
stdoutPath := fmt.Sprintf("/opt/funnel/outputs/stdout-%d", i)
stderrPath := fmt.Sprintf("/opt/funnel/outputs/stderr-%d", i)
// Only set the stdin path if the --stdin flag was used.
var stdin string
if exec.stdin != "" {
stdin = stdinPath
task.Inputs = append(task.Inputs, &tes.Input{
Name: fmt.Sprintf("stdin-%d", i),
Url: resolvePath(exec.stdin),
Path: stdinPath,
})
}
if exec.stdout != "" {
task.Outputs = append(task.Outputs, &tes.Output{
Name: fmt.Sprintf("stdout-%d", i),
Url: resolvePath(exec.stdout),
Path: stdoutPath,
})
}
if exec.stderr != "" {
task.Outputs = append(task.Outputs, &tes.Output{
Name: fmt.Sprintf("stderr-%d", i),
Url: resolvePath(exec.stderr),
Path: stderrPath,
})
}
task.Executors = append(task.Executors, &tes.Executor{
Image: vals.container,
Command: cmd,
Workdir: vals.workdir,
Env: environ,
Stdin: stdin,
Stdout: stdoutPath,
Stderr: stderrPath,
})
}
// Helper to make sure variable keys are unique.
setenv := func(key, val string) {
_, exists := environ[key]
if exists {
panic(DuplicateKeyErr(key))
}
environ[key] = val
}
for _, raw := range vals.inputs {
k, v := parseCliVar(raw)
url := resolvePath(v)
path := "/opt/funnel/inputs/" + stripStoragePrefix(url)
setenv(k, path)
task.Inputs = append(task.Inputs, &tes.Input{
Name: k,
Url: url,
Path: path,
})
}
for _, raw := range vals.inputDirs {
k, v := parseCliVar(raw)
url := resolvePath(v)
path := "/opt/funnel/inputs/" + stripStoragePrefix(url)
setenv(k, path)
task.Inputs = append(task.Inputs, &tes.Input{
Name: k,
Url: url,
Path: path,
Type: tes.FileType_DIRECTORY,
})
}
for _, raw := range vals.content {
k, v := parseCliVar(raw)
path := "/opt/funnel/inputs/" + stripStoragePrefix(resolvePath(v))
setenv(k, path)
task.Inputs = append(task.Inputs, &tes.Input{
Name: k,
Path: path,
Content: getContent(v),
})
}
for _, raw := range vals.outputs {
k, v := parseCliVar(raw)
url := resolvePath(v)
path := "/opt/funnel/outputs/" + stripStoragePrefix(url)
setenv(k, path)
task.Outputs = append(task.Outputs, &tes.Output{
Name: k,
Url: url,
Path: path,
})
}
for _, raw := range vals.outputDirs {
k, v := parseCliVar(raw)
url := resolvePath(v)
path := "/opt/funnel/outputs/" + stripStoragePrefix(url)
setenv(k, path)
task.Outputs = append(task.Outputs, &tes.Output{
Name: k,
Url: url,
Path: path,
Type: tes.FileType_DIRECTORY,
})
}
for _, raw := range vals.environ {
k, v := parseCliVar(raw)
setenv(k, v)
}
for _, raw := range vals.tags {
k, v := parseCliVar(raw)
task.Tags[k] = v
}
return
}
func getContent(p string) string {
b, err := ioutil.ReadFile(p)
if err != nil {
panic(err)
}
return string(b)
}
func parseCliVar(raw string) (string, string) {
re := regexp.MustCompile("=")
res := re.Split(raw, -1)
if len(res) != 2 {
panic(ErrKeyFmt)
}
key := res[0]
val := res[1]
return key, val
}
// Give a input/output URL "raw", return the path of the file
// relative to the container.
func containerPath(raw, base string) string {
url := resolvePath(raw)
p := stripStoragePrefix(url)
return base + p
}
func stripStoragePrefix(url string) string {
re := regexp.MustCompile("[a-z0-9]+://")
if !re.MatchString(url) {
panic(ErrStorageScheme)
}
path := re.ReplaceAllString(url, "")
return strings.TrimPrefix(path, "/")
}
func resolvePath(url string) string {
re := regexp.MustCompile("[a-z0-9]+://")
prefixed := re.MatchString(url)
local := strings.HasPrefix(url, "/") || strings.HasPrefix(url, ".") ||
strings.HasPrefix(url, "~") || !prefixed
switch {
case local:
path, err := filepath.Abs(url)
if err != nil {
panic(err)
}
return "file://" + path
case prefixed:
return url
default:
panic(fmt.Errorf("could not resolve filepath: %s", url))
}
}