|
| 1 | +package executor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "regexp" |
| 11 | + "slices" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/codeclysm/extract/v4" |
| 15 | + "github.com/dstackai/dstack/runner/internal/gerrors" |
| 16 | + "github.com/dstackai/dstack/runner/internal/log" |
| 17 | +) |
| 18 | + |
| 19 | +var renameRegex = regexp.MustCompile(`^([^/]*)(/|$)`) |
| 20 | + |
| 21 | +func (ex *RunExecutor) AddFileArchive(id string, src io.Reader) error { |
| 22 | + if err := os.MkdirAll(ex.archiveDir, 0o755); err != nil { |
| 23 | + return gerrors.Wrap(err) |
| 24 | + } |
| 25 | + archivePath := path.Join(ex.archiveDir, id) |
| 26 | + archive, err := os.Create(archivePath) |
| 27 | + if err != nil { |
| 28 | + return gerrors.Wrap(err) |
| 29 | + } |
| 30 | + defer func() { _ = archive.Close() }() |
| 31 | + if _, err = io.Copy(archive, src); err != nil { |
| 32 | + return gerrors.Wrap(err) |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// setupFiles must be called from Run |
| 38 | +func (ex *RunExecutor) setupFiles(ctx context.Context) error { |
| 39 | + homeDir := ex.workingDir |
| 40 | + uid := -1 |
| 41 | + gid := -1 |
| 42 | + if ex.jobSpec.User != nil { |
| 43 | + if ex.jobSpec.User.HomeDir != "" { |
| 44 | + homeDir = ex.jobSpec.User.HomeDir |
| 45 | + } |
| 46 | + if ex.jobSpec.User.Uid != nil { |
| 47 | + uid = int(*ex.jobSpec.User.Uid) |
| 48 | + } |
| 49 | + if ex.jobSpec.User.Gid != nil { |
| 50 | + gid = int(*ex.jobSpec.User.Gid) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + for _, fa := range ex.run.RunSpec.FileArchives { |
| 55 | + log.Trace(ctx, "Extracting file archive", "id", fa.Id, "path", fa.Path) |
| 56 | + |
| 57 | + p := path.Clean(fa.Path) |
| 58 | + // `~username[/path/to]` is not supported |
| 59 | + if p == "~" { |
| 60 | + p = homeDir |
| 61 | + } else if rest, found := strings.CutPrefix(p, "~/"); found { |
| 62 | + p = path.Join(homeDir, rest) |
| 63 | + } else if !path.IsAbs(p) { |
| 64 | + p = path.Join(ex.workingDir, p) |
| 65 | + } |
| 66 | + dir, root := path.Split(p) |
| 67 | + if err := mkdirAll(ctx, dir, uid, gid); err != nil { |
| 68 | + return gerrors.Wrap(err) |
| 69 | + } |
| 70 | + |
| 71 | + if err := os.RemoveAll(p); err != nil { |
| 72 | + log.Warning(ctx, "Failed to remove", "path", p, "err", err) |
| 73 | + } |
| 74 | + |
| 75 | + archivePath := path.Join(ex.archiveDir, fa.Id) |
| 76 | + archive, err := os.Open(archivePath) |
| 77 | + if err != nil { |
| 78 | + return gerrors.Wrap(err) |
| 79 | + } |
| 80 | + defer func() { |
| 81 | + _ = archive.Close() |
| 82 | + if err := os.Remove(archivePath); err != nil { |
| 83 | + log.Warning(ctx, "Failed to remove archive", "path", archivePath, "err", err) |
| 84 | + } |
| 85 | + }() |
| 86 | + |
| 87 | + var paths []string |
| 88 | + repl := fmt.Sprintf("%s$2", root) |
| 89 | + renameAndRemember := func(s string) string { |
| 90 | + s = renameRegex.ReplaceAllString(s, repl) |
| 91 | + paths = append(paths, s) |
| 92 | + return s |
| 93 | + } |
| 94 | + if err := extract.Tar(ctx, archive, dir, renameAndRemember); err != nil { |
| 95 | + return gerrors.Wrap(err) |
| 96 | + } |
| 97 | + |
| 98 | + if uid != -1 || gid != -1 { |
| 99 | + for _, p := range paths { |
| 100 | + if err := os.Chown(path.Join(dir, p), uid, gid); err != nil { |
| 101 | + log.Warning(ctx, "Failed to chown", "path", p, "err", err) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func mkdirAll(ctx context.Context, p string, uid int, gid int) error { |
| 111 | + var paths []string |
| 112 | + for { |
| 113 | + p = path.Dir(p) |
| 114 | + if p == "/" { |
| 115 | + break |
| 116 | + } |
| 117 | + paths = append(paths, p) |
| 118 | + } |
| 119 | + for _, p := range slices.Backward(paths) { |
| 120 | + if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) { |
| 121 | + if err := os.Mkdir(p, 0o755); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if err := os.Chown(p, uid, gid); err != nil { |
| 125 | + log.Warning(ctx, "Failed to chown", "path", p, "err", err) |
| 126 | + } |
| 127 | + } else if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
0 commit comments