move lookupTaskBin to executor_linux, for os dependency clarity

This commit is contained in:
Lang Martin
2019-05-07 16:58:27 -04:00
parent 2ea860a087
commit 538f387d8d
4 changed files with 76 additions and 76 deletions

View File

@@ -17,6 +17,7 @@ import (
"github.com/hashicorp/consul-template/signals"
hclog "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/stats"
cstructs "github.com/hashicorp/nomad/client/structs"
shelpers "github.com/hashicorp/nomad/helper/stats"
@@ -770,3 +771,43 @@ func cmdMounts(mounts []*drivers.MountConfig) []*lconfigs.Mount {
return r
}
// lookupTaskBin finds the file `bin` in taskDir/local, taskDir in that order, then performs
// a PATH search inside taskDir. It returns an absolute path. See also executor.lookupBin
func lookupTaskBin(taskDir string, bin string) (string, error) {
// Check in the local directory
localDir := filepath.Join(taskDir, allocdir.TaskLocal)
local := filepath.Join(localDir, bin)
if _, err := os.Stat(local); err == nil {
return local, nil
}
// Check at the root of the task's directory
root := filepath.Join(taskDir, bin)
if _, err := os.Stat(root); err == nil {
return root, nil
}
// Check the host PATH anchored inside the taskDir
if !strings.Contains(bin, "/") {
envPath := os.Getenv("PATH")
for _, dir := range filepath.SplitList(envPath) {
if dir == "" {
// match unix shell behavior, empty path element == .
dir = "."
}
for _, root := range []string{localDir, taskDir} {
path := filepath.Join(root, dir, bin)
f, err := os.Stat(path)
if err != nil {
continue
}
if m := f.Mode(); !m.IsDir() {
return path, nil
}
}
}
}
return "", fmt.Errorf("file %s not found under path %s", bin, taskDir)
}