2022-01-14 13:57:51 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, 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 configure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-23 12:50:48 +00:00
|
|
|
"path/filepath"
|
2022-01-14 13:57:51 +00:00
|
|
|
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/docker"
|
2022-01-14 13:57:51 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultRuntime = "docker"
|
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
// defaultNVIDIARuntimeName is the default name to use in configs for the NVIDIA Container Runtime
|
|
|
|
defaultNVIDIARuntimeName = "nvidia"
|
|
|
|
// defaultNVIDIARuntimeExecutable is the default NVIDIA Container Runtime executable file name
|
|
|
|
defaultNVIDIARuntimeExecutable = "nvidia-container-runtime"
|
|
|
|
defailtNVIDIARuntimeExpecutablePath = "/usr/bin/nvidia-container-runtime"
|
|
|
|
|
2023-03-23 12:57:18 +00:00
|
|
|
defaultContainerdConfigFilePath = "/etc/containerd/config.toml"
|
|
|
|
defaultCrioConfigFilePath = "/etc/crio/crio.conf"
|
|
|
|
defaultDockerConfigFilePath = "/etc/docker/daemon.json"
|
2022-01-14 13:57:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type command struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommand constructs an configure command with the specified logger
|
|
|
|
func NewCommand(logger *logrus.Logger) *cli.Command {
|
|
|
|
c := command{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
return c.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
// config defines the options that can be set for the CLI through config files,
|
|
|
|
// environment variables, or command line config
|
|
|
|
type config struct {
|
|
|
|
dryRun bool
|
|
|
|
runtime string
|
|
|
|
configFilePath string
|
2023-03-23 12:50:48 +00:00
|
|
|
|
|
|
|
nvidiaRuntime struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
setAsDefault bool
|
|
|
|
}
|
2022-01-14 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m command) build() *cli.Command {
|
|
|
|
// Create a config struct to hold the parsed environment variables or command line flags
|
|
|
|
config := config{}
|
|
|
|
|
|
|
|
// Create the 'configure' command
|
|
|
|
configure := cli.Command{
|
|
|
|
Name: "configure",
|
|
|
|
Usage: "Add a runtime to the specified container engine",
|
2023-03-23 12:50:48 +00:00
|
|
|
Before: func(c *cli.Context) error {
|
|
|
|
return validateFlags(c, &config)
|
|
|
|
},
|
2022-01-14 13:57:51 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return m.configureWrapper(c, &config)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configure.Flags = []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "dry-run",
|
|
|
|
Usage: "update the runtime configuration as required but don't write changes to disk",
|
|
|
|
Destination: &config.dryRun,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "runtime",
|
2023-03-23 12:57:18 +00:00
|
|
|
Usage: "the target runtime engine; one of [containerd, crio, docker]",
|
2022-01-14 13:57:51 +00:00
|
|
|
Value: defaultRuntime,
|
|
|
|
Destination: &config.runtime,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "path to the config file for the target runtime",
|
|
|
|
Destination: &config.configFilePath,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "nvidia-runtime-name",
|
|
|
|
Usage: "specify the name of the NVIDIA runtime that will be added",
|
2023-03-23 12:50:48 +00:00
|
|
|
Value: defaultNVIDIARuntimeName,
|
|
|
|
Destination: &config.nvidiaRuntime.name,
|
2022-01-14 13:57:51 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2023-03-23 12:50:48 +00:00
|
|
|
Name: "nvidia-runtime-path",
|
|
|
|
Aliases: []string{"runtime-path"},
|
2022-01-14 13:57:51 +00:00
|
|
|
Usage: "specify the path to the NVIDIA runtime executable",
|
2023-03-23 12:50:48 +00:00
|
|
|
Value: defaultNVIDIARuntimeExecutable,
|
|
|
|
Destination: &config.nvidiaRuntime.path,
|
2022-01-14 13:57:51 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2023-03-23 12:50:48 +00:00
|
|
|
Name: "nvidia-set-as-default",
|
|
|
|
Aliases: []string{"set-as-default"},
|
|
|
|
Usage: "set the NVIDIA runtime as the default runtime",
|
|
|
|
Destination: &config.nvidiaRuntime.setAsDefault,
|
2022-01-14 13:57:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &configure
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
func validateFlags(c *cli.Context, config *config) error {
|
|
|
|
switch config.runtime {
|
2023-03-23 12:57:18 +00:00
|
|
|
case "containerd", "crio", "docker":
|
2023-03-23 12:50:48 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognized runtime '%v'", config.runtime)
|
|
|
|
}
|
|
|
|
|
2022-01-14 13:57:51 +00:00
|
|
|
switch config.runtime {
|
2023-03-23 12:57:18 +00:00
|
|
|
case "containerd", "crio":
|
2023-03-23 12:50:48 +00:00
|
|
|
if config.nvidiaRuntime.path == defaultNVIDIARuntimeExecutable {
|
|
|
|
config.nvidiaRuntime.path = defailtNVIDIARuntimeExpecutablePath
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(config.nvidiaRuntime.path) {
|
|
|
|
return fmt.Errorf("the NVIDIA runtime path %q is not an absolute path", config.nvidiaRuntime.path)
|
|
|
|
}
|
2022-01-14 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
return nil
|
2022-01-14 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
// configureWrapper updates the specified container engine config to enable the NVIDIA runtime
|
|
|
|
func (m command) configureWrapper(c *cli.Context, config *config) error {
|
|
|
|
configFilePath := config.resolveConfigFilePath()
|
2022-01-14 13:57:51 +00:00
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
var cfg engine.Interface
|
|
|
|
var err error
|
|
|
|
switch config.runtime {
|
2023-03-23 12:57:18 +00:00
|
|
|
case "containerd":
|
|
|
|
cfg, err = containerd.New(
|
|
|
|
containerd.WithPath(configFilePath),
|
|
|
|
)
|
2023-03-23 12:50:48 +00:00
|
|
|
case "crio":
|
|
|
|
cfg, err = crio.New(
|
|
|
|
crio.WithPath(configFilePath),
|
|
|
|
)
|
|
|
|
case "docker":
|
|
|
|
cfg, err = docker.New(
|
|
|
|
docker.WithPath(configFilePath),
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unrecognized runtime '%v'", config.runtime)
|
|
|
|
}
|
|
|
|
if err != nil || cfg == nil {
|
|
|
|
return fmt.Errorf("unable to load config for runtime %v: %v", config.runtime, err)
|
2022-01-14 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
err = cfg.AddRuntime(
|
2023-03-23 12:50:48 +00:00
|
|
|
config.nvidiaRuntime.name,
|
|
|
|
config.nvidiaRuntime.path,
|
|
|
|
config.nvidiaRuntime.setAsDefault,
|
2022-07-14 14:19:19 +00:00
|
|
|
)
|
2022-01-14 13:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to update config: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
outputPath := config.getOuputConfigPath()
|
|
|
|
n, err := cfg.Save(outputPath)
|
2022-01-14 13:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to flush config: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
if n == 0 {
|
2023-03-23 12:50:48 +00:00
|
|
|
m.logger.Infof("Removed empty config from %v", outputPath)
|
2023-02-23 13:43:15 +00:00
|
|
|
} else {
|
2023-03-23 12:50:48 +00:00
|
|
|
m.logger.Infof("Wrote updated config to %v", outputPath)
|
2023-02-23 13:43:15 +00:00
|
|
|
}
|
2023-03-23 12:50:48 +00:00
|
|
|
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
|
2022-01-14 13:57:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
// resolveConfigFilePath returns the default config file path for the configured container engine
|
|
|
|
func (c *config) resolveConfigFilePath() string {
|
|
|
|
if c.configFilePath != "" {
|
|
|
|
return c.configFilePath
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
2023-03-23 12:50:48 +00:00
|
|
|
switch c.runtime {
|
2023-03-23 12:57:18 +00:00
|
|
|
case "containerd":
|
|
|
|
return defaultContainerdConfigFilePath
|
2023-03-23 12:50:48 +00:00
|
|
|
case "crio":
|
|
|
|
return defaultCrioConfigFilePath
|
|
|
|
case "docker":
|
|
|
|
return defaultDockerConfigFilePath
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
2023-03-23 12:50:48 +00:00
|
|
|
return ""
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2023-03-23 12:50:48 +00:00
|
|
|
// getOuputConfigPath returns the configured config path or "" if dry-run is enabled
|
|
|
|
func (c *config) getOuputConfigPath() string {
|
|
|
|
if c.dryRun {
|
|
|
|
return ""
|
2023-02-23 12:49:54 +00:00
|
|
|
}
|
2023-03-23 12:50:48 +00:00
|
|
|
return c.resolveConfigFilePath()
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|