mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-02-16 17:42:20 +00:00
These changes remove the use of discover.Config which was used to pass the driver root and the nvidiaCTK path in some cases. Instead, the nvidiaCTKPath is resolved at the begining of runtime invocation to ensure that this is valid at all points where it is used. Signed-off-by: Evan Lezar <elezar@nvidia.com>
114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
/**
|
|
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
**/
|
|
|
|
package runtime
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
)
|
|
|
|
// Run is an entry point that allows for idiomatic handling of errors
|
|
// when calling from the main function.
|
|
func (r rt) Run(argv []string) (rerr error) {
|
|
defer func() {
|
|
if rerr != nil {
|
|
r.logger.Errorf("%v", rerr)
|
|
}
|
|
}()
|
|
|
|
printVersion := hasVersionFlag(argv)
|
|
if printVersion {
|
|
fmt.Printf("%v version %v\n", "NVIDIA Container Runtime", info.GetVersionString(fmt.Sprintf("spec: %v", specs.Version)))
|
|
}
|
|
|
|
cfg, err := config.GetConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("error loading config: %v", err)
|
|
}
|
|
err = r.logger.Update(
|
|
cfg.NVIDIAContainerRuntimeConfig.DebugFilePath,
|
|
cfg.NVIDIAContainerRuntimeConfig.LogLevel,
|
|
argv,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to set up logger: %v", err)
|
|
}
|
|
defer func() {
|
|
if rerr != nil {
|
|
r.logger.Errorf("%v", rerr)
|
|
}
|
|
r.logger.Reset()
|
|
}()
|
|
|
|
// We apply some config updates here to ensure that the config is valid in
|
|
// all cases.
|
|
if r.modeOverride != "" {
|
|
cfg.NVIDIAContainerRuntimeConfig.Mode = r.modeOverride
|
|
}
|
|
cfg.NVIDIACTKConfig.Path = discover.FindNvidiaCTK(r.logger.Logger, cfg.NVIDIACTKConfig.Path)
|
|
|
|
// Print the config to the output.
|
|
configJSON, err := json.MarshalIndent(cfg, "", " ")
|
|
if err == nil {
|
|
r.logger.Infof("Running with config:\n%v", string(configJSON))
|
|
} else {
|
|
r.logger.Infof("Running with config:\n%+v", cfg)
|
|
}
|
|
|
|
r.logger.Debugf("Command line arguments: %v", argv)
|
|
runtime, err := newNVIDIAContainerRuntime(r.logger.Logger, cfg, argv)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create NVIDIA Container Runtime: %v", err)
|
|
}
|
|
|
|
if printVersion {
|
|
fmt.Print("\n")
|
|
}
|
|
return runtime.Exec(argv)
|
|
}
|
|
|
|
func (r rt) Errorf(format string, args ...interface{}) {
|
|
r.logger.Errorf(format, args...)
|
|
}
|
|
|
|
// TODO: This should be refactored / combined with parseArgs in logger.
|
|
func hasVersionFlag(args []string) bool {
|
|
for i := 0; i < len(args); i++ {
|
|
param := args[i]
|
|
|
|
parts := strings.SplitN(param, "=", 2)
|
|
trimmed := strings.TrimLeft(parts[0], "-")
|
|
// If this is not a flag we continue
|
|
if parts[0] == trimmed {
|
|
continue
|
|
}
|
|
|
|
// Check the version flag
|
|
if trimmed == "version" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|