-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfile_mapper.go
305 lines (270 loc) · 8.01 KB
/
file_mapper.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package worker
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
proto "github.com/golang/protobuf/proto"
"github.com/ohsu-comp-bio/funnel/tes"
"github.com/ohsu-comp-bio/funnel/util/fsutil"
)
// FileMapper is responsible for mapping paths into a working directory on the
// worker's host file system.
//
// Every task needs it's own directory to work in. When a file is downloaded for
// a task, it needs to be stored in the task's working directory. Similar for task
// outputs, uploads, stdin/out/err, etc. FileMapper helps the worker engine
// manage all these paths.
type FileMapper struct {
Volumes []Volume
Inputs []*tes.Input
Outputs []*tes.Output
WorkDir string
}
// Volume represents a volume mounted into a docker container.
// This includes a HostPath, the path on the host file system,
// and a ContainerPath, the path on the container file system,
// and whether the volume is read-only.
type Volume struct {
// The path in tes worker.
HostPath string
// The path in Docker.
ContainerPath string
Readonly bool
}
// NewFileMapper returns a new FileMapper, which maps files into the given
// base directory.
func NewFileMapper(dir string) *FileMapper {
dir, _ = filepath.Abs(dir)
return &FileMapper{
Volumes: []Volume{},
Inputs: []*tes.Input{},
Outputs: []*tes.Output{},
WorkDir: dir,
}
}
// MapTask adds all the volumes, inputs, and outputs in the given Task to the FileMapper.
func (mapper *FileMapper) MapTask(task *tes.Task) error {
// Validate working directory
if !filepath.IsAbs(mapper.WorkDir) {
return fmt.Errorf("Mapper.WorkDir is not an absolute path")
}
// Create the working directory
err := fsutil.EnsureDir(mapper.WorkDir)
if err != nil {
return err
}
// Add all the volumes to the mapper
for _, vol := range task.Volumes {
err = mapper.AddTmpVolume(vol)
if err != nil {
return err
}
}
err = mapper.AddTmpVolume("/tmp")
if err != nil {
return err
}
// Add all the inputs to the mapper
for _, input := range task.Inputs {
err = mapper.AddInput(input)
if err != nil {
return err
}
}
// Add all the outputs to the mapper
for _, output := range task.Outputs {
err = mapper.AddOutput(output)
if err != nil {
return err
}
}
return nil
}
// AddVolume adds a mapped volume to the mapper. A corresponding Volume record
// is added to mapper.Volumes.
//
// If the volume paths are invalid or can't be mapped, an error is returned.
func (mapper *FileMapper) AddVolume(hostPath string, mountPoint string, readonly bool) error {
vol := Volume{
HostPath: hostPath,
ContainerPath: mountPoint,
Readonly: readonly,
}
for i, v := range mapper.Volumes {
// check if this volume is already present in the mapper
if vol == v {
return nil
}
// If the proposed RW Volume is a subpath of an existing RW Volume
// do not add it to the mapper
// If an existing RW Volume is a subpath of the proposed RW Volume, replace it with
// the proposed RW Volume
if !vol.Readonly && !v.Readonly {
if mapper.IsSubpath(vol.ContainerPath, v.ContainerPath) {
return nil
} else if mapper.IsSubpath(v.ContainerPath, vol.ContainerPath) {
mapper.Volumes[i] = vol
return nil
}
}
}
mapper.Volumes = append(mapper.Volumes, vol)
return nil
}
// HostPath returns a mapped path.
//
// The path is concatenated to the mapper's base dir.
// e.g. If the mapper is configured with a base dir of "/tmp/mapped_files", then
// mapper.HostPath("/home/ubuntu/myfile") will return "/tmp/mapped_files/home/ubuntu/myfile".
//
// The mapped path is required to be a subpath of the mapper's base directory.
// e.g. mapper.HostPath("../../foo") should fail with an error.
func (mapper *FileMapper) HostPath(src string) (string, error) {
p := path.Join(mapper.WorkDir, src)
p = path.Clean(p)
if !mapper.IsSubpath(p, mapper.WorkDir) {
return "", fmt.Errorf("Invalid path: %s is not a valid subpath of %s", p, mapper.WorkDir)
}
return p, nil
}
// OpenHostFile opens a file on the host file system at a mapped path.
// "src" is an unmapped path. This function will handle mapping the path.
//
// # This function calls os.Open
//
// If the path can't be mapped or the file can't be opened, an error is returned.
func (mapper *FileMapper) OpenHostFile(src string) (*os.File, error) {
p, perr := mapper.HostPath(src)
if perr != nil {
return nil, perr
}
f, oerr := os.Open(p)
if oerr != nil {
return nil, oerr
}
return f, nil
}
// CreateHostFile creates a file on the host file system at a mapped path.
// "src" is an unmapped path. This function will handle mapping the path.
//
// # This function calls os.Create
//
// If the path can't be mapped or the file can't be created, an error is returned.
func (mapper *FileMapper) CreateHostFile(src string) (*os.File, error) {
p, perr := mapper.HostPath(src)
if perr != nil {
return nil, perr
}
err := fsutil.EnsurePath(p)
if err != nil {
return nil, err
}
f, oerr := os.Create(p)
if oerr != nil {
return nil, oerr
}
return f, nil
}
// AddTmpVolume creates a directory on the host based on the declared path in
// the container and adds it to mapper.Volumes.
//
// If the path can't be mapped, an error is returned.
func (mapper *FileMapper) AddTmpVolume(mountPoint string) error {
hostPath, err := mapper.HostPath(mountPoint)
if err != nil {
return err
}
err = fsutil.EnsureDir(hostPath)
if err != nil {
return err
}
err = mapper.AddVolume(hostPath, mountPoint, false)
if err != nil {
return err
}
return nil
}
// AddInput adds an input to the mapped files for the given tes.Input.
// A copy of the tes.Input will be added to mapper.Inputs, with the
// "Path" field updated to the mapped host path.
//
// If the path can't be mapped an error is returned.
func (mapper *FileMapper) AddInput(input *tes.Input) error {
hostPath, err := mapper.HostPath(input.Path)
if err != nil {
return err
}
err = fsutil.EnsurePath(hostPath)
if err != nil {
return err
}
// Add input volumes
err = mapper.AddVolume(hostPath, input.Path, true)
if err != nil {
return err
}
// If 'content' field is set create the file
if input.Content != "" {
err := os.WriteFile(hostPath, []byte(input.Content), 0775)
if err != nil {
return fmt.Errorf("Error writing content of task input to file %v", err)
}
return nil
}
// Create a tes.Input for the input with a path mapped to the host
hostIn := proto.Clone(input).(*tes.Input)
hostIn.Path = hostPath
mapper.Inputs = append(mapper.Inputs, hostIn)
return nil
}
// AddOutput adds an output to the mapped files for the given tes.Output.
// A copy of the tes.Output will be added to mapper.Outputs, with the
// "Path" field updated to the mapped host path.
//
// If the path can't be mapped, an error is returned.
func (mapper *FileMapper) AddOutput(output *tes.Output) error {
hostPath, err := mapper.HostPath(output.Path)
if err != nil {
return err
}
hostDir := hostPath
mountDir := output.Path
if output.Type == tes.FileType_FILE {
hostDir = path.Dir(hostPath)
mountDir = path.Dir(output.Path)
}
err = fsutil.EnsureDir(hostDir)
if err != nil {
return err
}
// Add output volumes
err = mapper.AddVolume(hostDir, mountDir, false)
if err != nil {
return err
}
// Create a tes.Output for the out with a path mapped to the host
hostOut := proto.Clone(output).(*tes.Output)
hostOut.Path = hostPath
mapper.Outputs = append(mapper.Outputs, hostOut)
return nil
}
// IsSubpath returns true if the given path "p" is a subpath of "base".
func (mapper *FileMapper) IsSubpath(p string, base string) bool {
return strings.HasPrefix(p, base)
}
// ContainerPath returns an unmapped path.
//
// The mapper's base dir is stripped from the path.
// e.g. If the mapper is configured with a base dir of "/tmp/mapped_files", then
// mapper.ContainerPath("/tmp/mapped_files/home/ubuntu/myfile") will return "/home/ubuntu/myfile".
func (mapper *FileMapper) ContainerPath(src string) string {
p := strings.TrimPrefix(src, mapper.WorkDir)
p = path.Clean("/" + p)
return p
}
// Cleanup deletes the working directory.
func (mapper *FileMapper) Cleanup() error {
return os.RemoveAll(mapper.WorkDir)
}