|
| 1 | +/** |
| 2 | +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package configure |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/runtime/nvidia" |
| 25 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/config/docker" |
| 26 | + "github.com/sirupsen/logrus" |
| 27 | + "github.com/urfave/cli/v2" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultRuntime = "docker" |
| 32 | + |
| 33 | + defaultDockerConfigFilePath = "/etc/docker/daemon.json" |
| 34 | +) |
| 35 | + |
| 36 | +type command struct { |
| 37 | + logger *logrus.Logger |
| 38 | +} |
| 39 | + |
| 40 | +// NewCommand constructs an configure command with the specified logger |
| 41 | +func NewCommand(logger *logrus.Logger) *cli.Command { |
| 42 | + c := command{ |
| 43 | + logger: logger, |
| 44 | + } |
| 45 | + return c.build() |
| 46 | +} |
| 47 | + |
| 48 | +// config defines the options that can be set for the CLI through config files, |
| 49 | +// environment variables, or command line config |
| 50 | +type config struct { |
| 51 | + dryRun bool |
| 52 | + runtime string |
| 53 | + configFilePath string |
| 54 | + nvidiaOptions nvidia.Options |
| 55 | +} |
| 56 | + |
| 57 | +func (m command) build() *cli.Command { |
| 58 | + // Create a config struct to hold the parsed environment variables or command line flags |
| 59 | + config := config{} |
| 60 | + |
| 61 | + // Create the 'configure' command |
| 62 | + configure := cli.Command{ |
| 63 | + Name: "configure", |
| 64 | + Usage: "Add a runtime to the specified container engine", |
| 65 | + Action: func(c *cli.Context) error { |
| 66 | + return m.configureWrapper(c, &config) |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + configure.Flags = []cli.Flag{ |
| 71 | + &cli.BoolFlag{ |
| 72 | + Name: "dry-run", |
| 73 | + Usage: "update the runtime configuration as required but don't write changes to disk", |
| 74 | + Destination: &config.dryRun, |
| 75 | + }, |
| 76 | + &cli.StringFlag{ |
| 77 | + Name: "runtime", |
| 78 | + Usage: "the target runtime engine. One of [docker]", |
| 79 | + Value: defaultRuntime, |
| 80 | + Destination: &config.runtime, |
| 81 | + }, |
| 82 | + &cli.StringFlag{ |
| 83 | + Name: "config", |
| 84 | + Usage: "path to the config file for the target runtime", |
| 85 | + Destination: &config.configFilePath, |
| 86 | + }, |
| 87 | + &cli.StringFlag{ |
| 88 | + Name: "nvidia-runtime-name", |
| 89 | + Usage: "specify the name of the NVIDIA runtime that will be added", |
| 90 | + Value: nvidia.RuntimeName, |
| 91 | + Destination: &config.nvidiaOptions.RuntimeName, |
| 92 | + }, |
| 93 | + &cli.StringFlag{ |
| 94 | + Name: "runtime-path", |
| 95 | + Usage: "specify the path to the NVIDIA runtime executable", |
| 96 | + Value: nvidia.RuntimeExecutable, |
| 97 | + Destination: &config.nvidiaOptions.RuntimePath, |
| 98 | + }, |
| 99 | + &cli.BoolFlag{ |
| 100 | + Name: "set-as-default", |
| 101 | + Usage: "set the specified runtime as the default runtime", |
| 102 | + Destination: &config.nvidiaOptions.SetAsDefault, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + return &configure |
| 107 | +} |
| 108 | + |
| 109 | +func (m command) configureWrapper(c *cli.Context, config *config) error { |
| 110 | + switch config.runtime { |
| 111 | + case "docker": |
| 112 | + return m.configureDocker(c, config) |
| 113 | + } |
| 114 | + |
| 115 | + return fmt.Errorf("unrecognized runtime '%v'", config.runtime) |
| 116 | +} |
| 117 | + |
| 118 | +// configureDocker updates the docker config to enable the NVIDIA Container Runtime |
| 119 | +func (m command) configureDocker(c *cli.Context, config *config) error { |
| 120 | + configFilePath := config.configFilePath |
| 121 | + if configFilePath == "" { |
| 122 | + configFilePath = defaultDockerConfigFilePath |
| 123 | + } |
| 124 | + |
| 125 | + cfg, err := docker.LoadConfig(configFilePath) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("unable to load config: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + defaultRuntime := config.nvidiaOptions.DefaultRuntime() |
| 131 | + runtimeConfig := config.nvidiaOptions.Runtime().DockerRuntimesConfig() |
| 132 | + err = docker.UpdateConfig(cfg, defaultRuntime, runtimeConfig) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("unable to update config: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if config.dryRun { |
| 138 | + output, err := json.MarshalIndent(cfg, "", " ") |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("unable to convert to JSON: %v", err) |
| 141 | + } |
| 142 | + os.Stdout.WriteString(fmt.Sprintf("%s\n", output)) |
| 143 | + return nil |
| 144 | + } |
| 145 | + err = docker.FlushConfig(cfg, configFilePath) |
| 146 | + if err != nil { |
| 147 | + return fmt.Errorf("unable to flush config: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + m.logger.Infof("Wrote updated config to %v", configFilePath) |
| 151 | + m.logger.Infof("It is recommended that the docker daemon be restarted.") |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
0 commit comments