2021-09-06 11:26:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-05-12 12:02:06 +00:00
|
|
|
"strings"
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-03-15 15:27:34 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2022-05-12 12:02:06 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2021-09-06 11:26:48 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 12:02:06 +00:00
|
|
|
// version must be set by go build's -X main.version= option in the Makefile.
|
|
|
|
var version = "unknown"
|
|
|
|
|
|
|
|
// gitCommit will be the hash that the binary was built from
|
|
|
|
// and will be populated by the Makefile
|
|
|
|
var gitCommit = ""
|
2022-05-12 10:05:55 +00:00
|
|
|
|
2021-09-06 11:26:48 +00:00
|
|
|
var logger = NewLogger()
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := run(os.Args)
|
|
|
|
if err != nil {
|
2022-03-30 11:18:57 +00:00
|
|
|
logger.Errorf("%v", err)
|
2021-09-06 11:26:48 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run is an entry point that allows for idiomatic handling of errors
|
|
|
|
// when calling from the main function.
|
2022-02-22 13:17:10 +00:00
|
|
|
func run(argv []string) (rerr error) {
|
2022-05-12 12:02:06 +00:00
|
|
|
printVersion := hasVersionFlag(argv)
|
|
|
|
if printVersion {
|
|
|
|
fmt.Printf("%v version %v\n", "NVIDIA Container Runtime", info.GetVersionString(fmt.Sprintf("spec: %v", specs.Version)))
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
cfg, err := config.GetConfig()
|
2021-09-06 11:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading config: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
logger, err = UpdateLogger(
|
|
|
|
cfg.NVIDIAContainerRuntimeConfig.DebugFilePath,
|
|
|
|
cfg.NVIDIAContainerRuntimeConfig.LogLevel,
|
|
|
|
argv,
|
|
|
|
)
|
2021-09-06 11:26:48 +00:00
|
|
|
if err != nil {
|
2022-05-05 15:27:36 +00:00
|
|
|
return fmt.Errorf("failed to set up logger: %v", err)
|
2022-03-11 15:37:41 +00:00
|
|
|
}
|
2022-09-19 13:21:57 +00:00
|
|
|
defer func() {
|
|
|
|
if rerr != nil {
|
|
|
|
logger.Errorf("%v", rerr)
|
|
|
|
}
|
|
|
|
logger.Reset()
|
|
|
|
}()
|
2022-03-11 15:37:41 +00:00
|
|
|
|
2022-05-05 11:45:27 +00:00
|
|
|
logger.Debugf("Command line arguments: %v", argv)
|
2022-03-24 11:45:03 +00:00
|
|
|
runtime, err := newNVIDIAContainerRuntime(logger.Logger, cfg, argv)
|
2021-09-06 11:26:48 +00:00
|
|
|
if err != nil {
|
2022-03-15 12:29:14 +00:00
|
|
|
return fmt.Errorf("failed to create NVIDIA Container Runtime: %v", err)
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 12:02:06 +00:00
|
|
|
if printVersion {
|
|
|
|
fmt.Print("\n")
|
|
|
|
}
|
2022-03-24 11:45:03 +00:00
|
|
|
return runtime.Exec(argv)
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
2022-05-12 12:02:06 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|