|
| 1 | +package env |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// ExecEnv is an execution environment. |
| 12 | +type ExecEnv interface { |
| 13 | + // WrapCommand modifies an existing `exec.Cmd` such that it runs in this environment. |
| 14 | + WrapCommand(cmd *exec.Cmd) error |
| 15 | + |
| 16 | + // PathFromEnv converts the given path from inside the environment into a path outside the |
| 17 | + // environment. |
| 18 | + PathFromEnv(path string) (string, error) |
| 19 | + |
| 20 | + // PathToEnv converts the given path from outside the environment into a path inside the |
| 21 | + // environment. |
| 22 | + PathToEnv(path string) (string, error) |
| 23 | + |
| 24 | + // FixPermissions ensures that the user executing this process owns the file at the given path |
| 25 | + // outside the environment. |
| 26 | + FixPermissions(path string) error |
| 27 | + |
| 28 | + // HasBinary returns true iff the given binary name is available in this environment. |
| 29 | + HasBinary(name string) bool |
| 30 | +} |
| 31 | + |
| 32 | +// PlainEnv is a plain execution environment that executes all commands directly. |
| 33 | +type PlainEnv struct{} |
| 34 | + |
| 35 | +// NewPlainEnv creates a new plain execution environment. |
| 36 | +func NewPlainEnv() *PlainEnv { |
| 37 | + return &PlainEnv{} |
| 38 | +} |
| 39 | + |
| 40 | +// WrapCommand implements ExecEnv. |
| 41 | +func (pe *PlainEnv) WrapCommand(*exec.Cmd) error { |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// PathFromEnv implements ExecEnv. |
| 46 | +func (pe *PlainEnv) PathFromEnv(path string) (string, error) { |
| 47 | + return path, nil |
| 48 | +} |
| 49 | + |
| 50 | +// PathToEnv implements ExecEnv. |
| 51 | +func (pe *PlainEnv) PathToEnv(path string) (string, error) { |
| 52 | + return path, nil |
| 53 | +} |
| 54 | + |
| 55 | +// FixPermissions implements ExecEnv. |
| 56 | +func (pe *PlainEnv) FixPermissions(string) error { |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// HasBinary implements ExecEnv. |
| 61 | +func (pe *PlainEnv) HasBinary(name string) bool { |
| 62 | + path, err := exec.LookPath(name) |
| 63 | + return err == nil && path != "" |
| 64 | +} |
| 65 | + |
| 66 | +// DockerEnv is a Docker-based execution environment that executes all commands inside a Docker |
| 67 | +// container using the configured image. |
| 68 | +type DockerEnv struct { |
| 69 | + image string |
| 70 | + volumes map[string]string |
| 71 | +} |
| 72 | + |
| 73 | +// NewDockerEnv creates a new Docker-based execution environment. |
| 74 | +func NewDockerEnv(image, baseDir, dirMount string) *DockerEnv { |
| 75 | + return &DockerEnv{ |
| 76 | + image: image, |
| 77 | + volumes: map[string]string{ |
| 78 | + baseDir: dirMount, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// AddDirectory exposes a host directory to the container under the same path. |
| 84 | +func (de *DockerEnv) AddDirectory(path string) { |
| 85 | + de.volumes[path] = path |
| 86 | +} |
| 87 | + |
| 88 | +// WrapCommand implements ExecEnv. |
| 89 | +func (de *DockerEnv) WrapCommand(cmd *exec.Cmd) error { |
| 90 | + origArgs := cmd.Args |
| 91 | + |
| 92 | + var err error |
| 93 | + wd := cmd.Dir |
| 94 | + if wd == "" { |
| 95 | + wd, err = os.Getwd() |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + } |
| 100 | + workDir, err := de.PathToEnv(wd) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("bad working directory: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + var envArgs []string //nolint: prealloc |
| 106 | + for _, envKV := range cmd.Env { |
| 107 | + envArgs = append(envArgs, "--env", envKV) |
| 108 | + } |
| 109 | + // When no environment is set, copy over any OASIS_ and ROFL_ variables. |
| 110 | + if len(cmd.Env) == 0 { |
| 111 | + for _, envKV := range os.Environ() { |
| 112 | + if !strings.HasPrefix(envKV, "OASIS_") && !strings.HasPrefix(envKV, "ROFL_") { |
| 113 | + continue |
| 114 | + } |
| 115 | + envArgs = append(envArgs, "--env", envKV) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + cmd.Path, err = exec.LookPath("docker") |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to find 'docker': %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + cmd.Args = []string{ |
| 125 | + "docker", "run", |
| 126 | + "--rm", |
| 127 | + "--platform", "linux/amd64", |
| 128 | + "--workdir", workDir, |
| 129 | + } |
| 130 | + for hostDir, bindDir := range de.volumes { |
| 131 | + cmd.Args = append(cmd.Args, "--volume", hostDir+":"+bindDir) |
| 132 | + } |
| 133 | + cmd.Args = append(cmd.Args, envArgs...) |
| 134 | + cmd.Args = append(cmd.Args, de.image) |
| 135 | + cmd.Args = append(cmd.Args, origArgs...) |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// PathFromEnv implements ExecEnv. |
| 141 | +func (de *DockerEnv) PathFromEnv(path string) (string, error) { |
| 142 | + for hostDir, bindDir := range de.volumes { |
| 143 | + if !strings.HasPrefix(path, bindDir) { |
| 144 | + continue |
| 145 | + } |
| 146 | + relPath, err := filepath.Rel(bindDir, path) |
| 147 | + if err != nil { |
| 148 | + return "", fmt.Errorf("bad path: %w", err) |
| 149 | + } |
| 150 | + return filepath.Join(hostDir, relPath), nil |
| 151 | + } |
| 152 | + return "", fmt.Errorf("bad path '%s'", path) |
| 153 | +} |
| 154 | + |
| 155 | +// PathToEnv implements ExecEnv. |
| 156 | +func (de *DockerEnv) PathToEnv(path string) (string, error) { |
| 157 | + for hostDir, bindDir := range de.volumes { |
| 158 | + if !strings.HasPrefix(path, hostDir) { |
| 159 | + continue |
| 160 | + } |
| 161 | + relPath, err := filepath.Rel(hostDir, path) |
| 162 | + if err != nil { |
| 163 | + return "", fmt.Errorf("bad path: %w", err) |
| 164 | + } |
| 165 | + return filepath.Join(bindDir, relPath), nil |
| 166 | + } |
| 167 | + return "", fmt.Errorf("bad path '%s'", path) |
| 168 | +} |
| 169 | + |
| 170 | +// FixPermissions implements ExecEnv. |
| 171 | +func (de *DockerEnv) FixPermissions(path string) error { |
| 172 | + path, err := de.PathToEnv(path) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + cmd := exec.Command("chown", fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()), path) //nolint: gosec |
| 178 | + if err = de.WrapCommand(cmd); err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + return cmd.Run() |
| 182 | +} |
| 183 | + |
| 184 | +// HasBinary implements ExecEnv. |
| 185 | +func (de *DockerEnv) HasBinary(string) bool { |
| 186 | + return true |
| 187 | +} |
0 commit comments