From f07d110e85729216c7af80fe84c3ef3e93b846de Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Fri, 8 Apr 2022 07:18:49 +0200 Subject: [PATCH] Use DefaultExecutableDir to determine default paths This change adds a DefaultExecutableDir = /usr/bin constant that is used to construct default paths for executables instead of specifying these explicitly. Signed-off-by: Evan Lezar --- cmd/nvidia-container-runtime/modifier/stable.go | 10 +++++----- internal/config/config.go | 3 +++ internal/discover/legacy.go | 8 +++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/nvidia-container-runtime/modifier/stable.go b/cmd/nvidia-container-runtime/modifier/stable.go index fa0fb8f1..194a337f 100644 --- a/cmd/nvidia-container-runtime/modifier/stable.go +++ b/cmd/nvidia-container-runtime/modifier/stable.go @@ -19,18 +19,18 @@ package modifier import ( "os" "os/exec" + "path/filepath" "strings" + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) const ( - nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" - nvidiaContainerRuntimeHookDefaultPath = "/usr/bin/nvidia-container-runtime-hook" - - nvidiaContainerToolkitExecutable = "nvidia-container-toolkit" + nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" + nvidiaContainerToolkitExecutable = "nvidia-container-toolkit" ) // NewStableRuntimeModifier creates an OCI spec modifier that inserts the NVIDIA Container Runtime Hook into an OCI @@ -52,7 +52,7 @@ type stableRuntimeModifier struct { func (m stableRuntimeModifier) Modify(spec *specs.Spec) error { path, err := exec.LookPath(nvidiaContainerRuntimeHookExecutable) if err != nil { - path = nvidiaContainerRuntimeHookDefaultPath + path = filepath.Join(config.DefaultExecutableDir, nvidiaContainerRuntimeHookExecutable) _, err = os.Stat(path) if err != nil { return err diff --git a/internal/config/config.go b/internal/config/config.go index 9f4f16f3..da6de2de 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -31,6 +31,9 @@ const ( ) var ( + // DefaultExecutableDir specifies the default path to use for executables if they cannot be located in the path. + DefaultExecutableDir = "/usr/bin" + configDir = "/etc/" ) diff --git a/internal/discover/legacy.go b/internal/discover/legacy.go index adc4c0c5..d95d6833 100644 --- a/internal/discover/legacy.go +++ b/internal/discover/legacy.go @@ -17,6 +17,9 @@ package discover import ( + "path/filepath" + + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" "github.com/sirupsen/logrus" @@ -41,14 +44,13 @@ type legacy struct { var _ Discover = (*legacy)(nil) const ( - nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" - nvidiaContainerRuntimeHookDefaultFilePath = "/usr/bin/nvidia-container-runtime-hook" + nvidiaContainerRuntimeHookExecutable = "nvidia-container-runtime-hook" ) // Hooks returns the "legacy" NVIDIA Container Runtime hook. This mirrors the behaviour of the stable // modifier. func (d legacy) Hooks() ([]Hook, error) { - hookPath := nvidiaContainerRuntimeHookDefaultFilePath + hookPath := filepath.Join(config.DefaultExecutableDir, nvidiaContainerRuntimeHookExecutable) targets, err := d.lookup.Locate(nvidiaContainerRuntimeHookExecutable) if err != nil { d.logger.Warnf("Failed to locate %v: %v", nvidiaContainerRuntimeHookExecutable, err)