Skip to content

Commit a720504

Browse files
authored
Merge pull request #992 from elezar/cleanup-nvidia-ctk-installer
Cleanup nvidia ctk installer
2 parents 0254075 + 75a30af commit a720504

File tree

2 files changed

+29
-136
lines changed

2 files changed

+29
-136
lines changed

cmd/nvidia-ctk-installer/main.go

+27-73
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"os/signal"
77
"path/filepath"
8-
"strings"
98
"syscall"
109

1110
"github.com/urfave/cli/v2"
@@ -20,10 +19,11 @@ import (
2019
const (
2120
toolkitPidFilename = "toolkit.pid"
2221
defaultPidFile = "/run/nvidia/toolkit/" + toolkitPidFilename
23-
toolkitSubDir = "toolkit"
2422

25-
defaultRuntime = "docker"
26-
defaultRuntimeArgs = ""
23+
defaultToolkitInstallDir = "/usr/local/nvidia"
24+
toolkitSubDir = "toolkit"
25+
26+
defaultRuntime = "docker"
2727
)
2828

2929
var availableRuntimes = map[string]struct{}{"docker": {}, "crio": {}, "containerd": {}}
@@ -34,36 +34,29 @@ var signalReceived = make(chan bool, 1)
3434

3535
// options stores the command line arguments
3636
type options struct {
37-
noDaemon bool
38-
runtime string
39-
runtimeArgs string
40-
root string
41-
pidFile string
42-
sourceRoot string
37+
toolkitInstallDir string
38+
39+
noDaemon bool
40+
runtime string
41+
pidFile string
42+
sourceRoot string
4343

4444
toolkitOptions toolkit.Options
4545
runtimeOptions runtime.Options
4646
}
4747

4848
func (o options) toolkitRoot() string {
49-
return filepath.Join(o.root, toolkitSubDir)
49+
return filepath.Join(o.toolkitInstallDir, toolkitSubDir)
5050
}
5151

5252
func main() {
5353
logger := logger.New()
54-
55-
remainingArgs, root, err := ParseArgs(logger, os.Args)
56-
if err != nil {
57-
logger.Errorf("Error: unable to parse arguments: %v", err)
58-
os.Exit(1)
59-
}
60-
61-
c := NewApp(logger, root)
54+
c := NewApp(logger)
6255

6356
// Run the CLI
6457
logger.Infof("Starting %v", c.Name)
65-
if err := c.Run(remainingArgs); err != nil {
66-
logger.Errorf("error running nvidia-toolkit: %v", err)
58+
if err := c.Run(os.Args); err != nil {
59+
logger.Errorf("error running %v: %v", c.Name, err)
6760
os.Exit(1)
6861
}
6962

@@ -73,18 +66,14 @@ func main() {
7366
// An app represents the nvidia-ctk-installer.
7467
type app struct {
7568
logger logger.Interface
76-
// defaultRoot stores the root to use if the --root flag is not specified.
77-
defaultRoot string
7869

7970
toolkit *toolkit.Installer
8071
}
8172

8273
// NewApp creates the CLI app fro the specified options.
83-
// defaultRoot is used as the root if not specified via the --root flag.
84-
func NewApp(logger logger.Interface, defaultRoot string) *cli.App {
74+
func NewApp(logger logger.Interface) *cli.App {
8575
a := app{
86-
logger: logger,
87-
defaultRoot: defaultRoot,
76+
logger: logger,
8877
}
8978
return a.build()
9079
}
@@ -96,9 +85,7 @@ func (a app) build() *cli.App {
9685
// Create the top-level CLI
9786
c := cli.NewApp()
9887
c.Name = "nvidia-ctk-installer"
99-
c.Usage = "Install the nvidia-container-toolkit for use by a given runtime"
100-
c.UsageText = "[DESTINATION] [-n | --no-daemon] [-r | --runtime] [-u | --runtime-args]"
101-
c.Description = "DESTINATION points to the host path underneath which the nvidia-container-toolkit should be installed.\nIt will be installed at ${DESTINATION}/toolkit"
88+
c.Usage = "Install the NVIDIA Container Toolkit and configure the specified runtime to use the `nvidia` runtime."
10289
c.Version = info.GetVersionString()
10390
c.Before = func(ctx *cli.Context) error {
10491
return a.Before(ctx, &options)
@@ -124,21 +111,16 @@ func (a app) build() *cli.App {
124111
Destination: &options.runtime,
125112
EnvVars: []string{"RUNTIME"},
126113
},
127-
// TODO: Remove runtime-args
128-
&cli.StringFlag{
129-
Name: "runtime-args",
130-
Aliases: []string{"u"},
131-
Usage: "arguments to pass to 'docker', 'crio', or 'containerd' setup command",
132-
Value: defaultRuntimeArgs,
133-
Destination: &options.runtimeArgs,
134-
EnvVars: []string{"RUNTIME_ARGS"},
135-
},
136114
&cli.StringFlag{
137-
Name: "root",
138-
Value: a.defaultRoot,
139-
Usage: "the folder where the NVIDIA Container Toolkit is to be installed. It will be installed to `ROOT`/toolkit",
140-
Destination: &options.root,
141-
EnvVars: []string{"ROOT"},
115+
Name: "toolkit-install-dir",
116+
Aliases: []string{"root"},
117+
Usage: "The directory where the NVIDIA Container Toolkit is to be installed. " +
118+
"The components of the toolkit will be installed to `ROOT`/toolkit. " +
119+
"Note that in the case of a containerized installer, this is the path in the container and it is " +
120+
"recommended that this match the path on the host.",
121+
Value: defaultToolkitInstallDir,
122+
Destination: &options.toolkitInstallDir,
123+
EnvVars: []string{"TOOLKIT_INSTALL_DIR", "ROOT"},
142124
},
143125
&cli.StringFlag{
144126
Name: "source-root",
@@ -172,7 +154,7 @@ func (a *app) Before(c *cli.Context, o *options) error {
172154
}
173155

174156
func (a *app) validateFlags(c *cli.Context, o *options) error {
175-
if o.root == "" {
157+
if o.toolkitInstallDir == "" {
176158
return fmt.Errorf("the install root must be specified")
177159
}
178160
if _, exists := availableRuntimes[o.runtime]; !exists {
@@ -236,34 +218,6 @@ func (a *app) Run(c *cli.Context, o *options) error {
236218
return nil
237219
}
238220

239-
// ParseArgs checks if a single positional argument was defined and extracts this the root.
240-
// If no positional arguments are defined, it is assumed that the root is specified as a flag.
241-
func ParseArgs(logger logger.Interface, args []string) ([]string, string, error) {
242-
logger.Infof("Parsing arguments")
243-
244-
if len(args) < 2 {
245-
return args, "", nil
246-
}
247-
248-
var lastPositionalArg int
249-
for i, arg := range args {
250-
if strings.HasPrefix(arg, "-") {
251-
break
252-
}
253-
lastPositionalArg = i
254-
}
255-
256-
if lastPositionalArg == 0 {
257-
return args, "", nil
258-
}
259-
260-
if lastPositionalArg == 1 {
261-
return append([]string{args[0]}, args[2:]...), args[1], nil
262-
}
263-
264-
return nil, "", fmt.Errorf("unexpected positional argument(s) %v", args[2:lastPositionalArg+1])
265-
}
266-
267221
func (a *app) initialize(pidFile string) error {
268222
a.logger.Infof("Initializing")
269223

cmd/nvidia-ctk-installer/main_test.go

+2-63
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package main
1818

1919
import (
20-
"fmt"
2120
"os"
2221
"path/filepath"
2322
"strings"
@@ -29,67 +28,6 @@ import (
2928
"github.com/NVIDIA/nvidia-container-toolkit/internal/test"
3029
)
3130

32-
func TestParseArgs(t *testing.T) {
33-
logger, _ := testlog.NewNullLogger()
34-
testCases := []struct {
35-
args []string
36-
expectedRemaining []string
37-
expectedRoot string
38-
expectedError error
39-
}{
40-
{
41-
args: []string{},
42-
expectedRemaining: []string{},
43-
expectedRoot: "",
44-
expectedError: nil,
45-
},
46-
{
47-
args: []string{"app"},
48-
expectedRemaining: []string{"app"},
49-
},
50-
{
51-
args: []string{"app", "root"},
52-
expectedRemaining: []string{"app"},
53-
expectedRoot: "root",
54-
},
55-
{
56-
args: []string{"app", "--flag"},
57-
expectedRemaining: []string{"app", "--flag"},
58-
},
59-
{
60-
args: []string{"app", "root", "--flag"},
61-
expectedRemaining: []string{"app", "--flag"},
62-
expectedRoot: "root",
63-
},
64-
{
65-
args: []string{"app", "root", "not-root", "--flag"},
66-
expectedError: fmt.Errorf("unexpected positional argument(s) [not-root]"),
67-
},
68-
{
69-
args: []string{"app", "root", "not-root"},
70-
expectedError: fmt.Errorf("unexpected positional argument(s) [not-root]"),
71-
},
72-
{
73-
args: []string{"app", "root", "not-root", "also"},
74-
expectedError: fmt.Errorf("unexpected positional argument(s) [not-root also]"),
75-
},
76-
}
77-
78-
for i, tc := range testCases {
79-
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
80-
remaining, root, err := ParseArgs(logger, tc.args)
81-
if tc.expectedError != nil {
82-
require.EqualError(t, err, tc.expectedError.Error())
83-
} else {
84-
require.NoError(t, err)
85-
}
86-
87-
require.ElementsMatch(t, tc.expectedRemaining, remaining)
88-
require.Equal(t, tc.expectedRoot, root)
89-
})
90-
}
91-
}
92-
9331
func TestApp(t *testing.T) {
9432
t.Setenv("__NVCT_TESTING_DEVICES_ARE_FILES", "true")
9533
logger, _ := testlog.NewNullLogger()
@@ -468,10 +406,11 @@ swarm-resource = ""
468406
toolkitRoot := filepath.Join(testRoot, "toolkit-test")
469407
toolkitConfigFile := filepath.Join(toolkitRoot, "toolkit/.config/nvidia-container-runtime/config.toml")
470408

471-
app := NewApp(logger, toolkitRoot)
409+
app := NewApp(logger)
472410

473411
testArgs := []string{
474412
"nvidia-ctk-installer",
413+
"--toolkit-install-dir=" + toolkitRoot,
475414
"--no-daemon",
476415
"--cdi-output-dir=" + cdiOutputDir,
477416
"--config=" + runtimeConfigFile,

0 commit comments

Comments
 (0)