Use lookup.GetPath from runtime hook

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-05-10 14:53:07 +02:00
parent 1d2e1bd403
commit b8ef6be6ea

View File

@ -6,21 +6,20 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
) )
var ( var (
debugflag = flag.Bool("debug", false, "enable debug output") debugflag = flag.Bool("debug", false, "enable debug output")
forceflag = flag.Bool("force", false, "force execution of prestart hook in experimental mode") forceflag = flag.Bool("force", false, "force execution of prestart hook in experimental mode")
configflag = flag.String("config", "", "configuration file") configflag = flag.String("config", "", "configuration file")
defaultPATH = []string{"/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"}
) )
func exit() { func exit() {
@ -36,28 +35,16 @@ func exit() {
os.Exit(0) os.Exit(0)
} }
func getPATH(config CLIConfig) string {
dirs := filepath.SplitList(os.Getenv("PATH"))
// directories from the hook environment have higher precedence
dirs = append(dirs, defaultPATH...)
if config.Root != nil {
rootDirs := []string{}
for _, dir := range dirs {
rootDirs = append(rootDirs, path.Join(*config.Root, dir))
}
// directories with the root prefix have higher precedence
dirs = append(rootDirs, dirs...)
}
return strings.Join(dirs, ":")
}
func getCLIPath(config CLIConfig) string { func getCLIPath(config CLIConfig) string {
if config.Path != nil { if config.Path != nil {
return *config.Path return *config.Path
} }
if err := os.Setenv("PATH", getPATH(config)); err != nil { var root string
if config.Root != nil {
root = *config.Root
}
if err := os.Setenv("PATH", lookup.GetPath(root)); err != nil {
log.Panicln("couldn't set PATH variable:", err) log.Panicln("couldn't set PATH variable:", err)
} }