Skip to content

Commit 2d685f7

Browse files
committed
[no-relnote] Use logger in toolkit installation
Signed-off-by: Evan Lezar <[email protected]>
1 parent fedc6ee commit 2d685f7

File tree

9 files changed

+217
-103
lines changed

9 files changed

+217
-103
lines changed

tools/container/nvidia-toolkit/run.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func main() {
7474
type app struct {
7575
logger logger.Interface
7676
defaultRoot string
77+
78+
toolkit *toolkit.Installer
7779
}
7880

7981
func new(logger logger.Interface, defaultRoot string) *cli.App {
@@ -96,6 +98,7 @@ func (a app) build() *cli.App {
9698
c.Description = "DESTINATION points to the host path underneath which the nvidia-container-toolkit should be installed.\nIt will be installed at ${DESTINATION}/toolkit"
9799
c.Version = Version
98100
c.Before = func(ctx *cli.Context) error {
101+
a.init(&options)
99102
return a.validateFlags(ctx, &options)
100103
}
101104
c.Action = func(ctx *cli.Context) error {
@@ -150,6 +153,13 @@ func (a app) build() *cli.App {
150153
return c
151154
}
152155

156+
func (a *app) init(o *options) {
157+
a.toolkit = toolkit.NewInstaller(
158+
toolkit.WithLogger(a.logger),
159+
toolkit.WithToolkitRoot(o.toolkitRoot()),
160+
)
161+
}
162+
153163
func (a *app) validateFlags(_ *cli.Context, o *options) error {
154164
if o.root == "" {
155165
return fmt.Errorf("the install root must be specified")
@@ -161,7 +171,7 @@ func (a *app) validateFlags(_ *cli.Context, o *options) error {
161171
return fmt.Errorf("invalid toolkit.pid path %v", o.pidFile)
162172
}
163173

164-
if err := toolkit.ValidateOptions(&o.toolkitOptions, o.toolkitRoot()); err != nil {
174+
if err := a.toolkit.ValidateOptions(&o.toolkitOptions); err != nil {
165175
return err
166176
}
167177
if err := runtime.ValidateOptions(&o.runtimeOptions, o.runtime, o.toolkitRoot()); err != nil {
@@ -187,7 +197,12 @@ func (a *app) Run(c *cli.Context, o *options) error {
187197

188198
o.toolkitOptions.ContainerRuntimeRuntimes = *cli.NewStringSlice(lowlevelRuntimePaths...)
189199
}
190-
err = toolkit.Install(c, &o.toolkitOptions, "", o.toolkitRoot())
200+
201+
installer := toolkit.NewInstaller(
202+
toolkit.WithLogger(a.logger),
203+
toolkit.WithToolkitRoot(o.toolkitRoot()),
204+
)
205+
err = installer.Install(c, &o.toolkitOptions)
191206
if err != nil {
192207
return fmt.Errorf("unable to install toolkit: %v", err)
193208
}

tools/container/nvidia-toolkit/run_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
"fmt"
2121
"testing"
2222

23+
testlog "github.com/sirupsen/logrus/hooks/test"
2324
"github.com/stretchr/testify/require"
2425
)
2526

2627
func TestParseArgs(t *testing.T) {
28+
logger, _ := testlog.NewNullLogger()
2729
testCases := []struct {
2830
args []string
2931
expectedRemaining []string
@@ -70,7 +72,7 @@ func TestParseArgs(t *testing.T) {
7072

7173
for i, tc := range testCases {
7274
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
73-
remaining, root, err := ParseArgs(tc.args)
75+
remaining, root, err := ParseArgs(logger, tc.args)
7476
if tc.expectedError != nil {
7577
require.EqualError(t, err, tc.expectedError.Error())
7678
} else {

tools/container/toolkit/executable.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"path/filepath"
2424
"sort"
2525
"strings"
26-
27-
log "github.com/sirupsen/logrus"
2826
)
2927

3028
type executableTarget struct {
@@ -33,6 +31,7 @@ type executableTarget struct {
3331
}
3432

3533
type executable struct {
34+
fileInstaller
3635
source string
3736
target executableTarget
3837
env map[string]string
@@ -43,21 +42,21 @@ type executable struct {
4342
// install installs an executable component of the NVIDIA container toolkit. The source executable
4443
// is copied to a `.real` file and a wapper is created to set up the environment as required.
4544
func (e executable) install(destFolder string) (string, error) {
46-
log.Infof("Installing executable '%v' to %v", e.source, destFolder)
45+
e.logger.Infof("Installing executable '%v' to %v", e.source, destFolder)
4746

4847
dotfileName := e.dotfileName()
4948

50-
installedDotfileName, err := installFileToFolderWithName(destFolder, dotfileName, e.source)
49+
installedDotfileName, err := e.installFileToFolderWithName(destFolder, dotfileName, e.source)
5150
if err != nil {
5251
return "", fmt.Errorf("error installing file '%v' as '%v': %v", e.source, dotfileName, err)
5352
}
54-
log.Infof("Installed '%v'", installedDotfileName)
53+
e.logger.Infof("Installed '%v'", installedDotfileName)
5554

5655
wrapperFilename, err := e.installWrapper(destFolder, installedDotfileName)
5756
if err != nil {
5857
return "", fmt.Errorf("error wrapping '%v': %v", installedDotfileName, err)
5958
}
60-
log.Infof("Installed wrapper '%v'", wrapperFilename)
59+
e.logger.Infof("Installed wrapper '%v'", wrapperFilename)
6160

6261
return wrapperFilename, nil
6362
}

tools/container/toolkit/executable_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import (
2323
"strings"
2424
"testing"
2525

26+
testlog "github.com/sirupsen/logrus/hooks/test"
2627
"github.com/stretchr/testify/require"
2728
)
2829

2930
func TestWrapper(t *testing.T) {
31+
logger, _ := testlog.NewNullLogger()
32+
3033
const shebang = "#! /bin/sh"
3134
const destFolder = "/dest/folder"
3235
const dotfileName = "source.real"
@@ -98,6 +101,8 @@ func TestWrapper(t *testing.T) {
98101
for i, tc := range testCases {
99102
buf := &bytes.Buffer{}
100103

104+
tc.e.logger = logger
105+
101106
err := tc.e.writeWrapperTo(buf, destFolder, dotfileName)
102107
require.NoError(t, err)
103108

@@ -107,6 +112,8 @@ func TestWrapper(t *testing.T) {
107112
}
108113

109114
func TestInstallExecutable(t *testing.T) {
115+
logger, _ := testlog.NewNullLogger()
116+
110117
inputFolder, err := os.MkdirTemp("", "")
111118
require.NoError(t, err)
112119
defer os.RemoveAll(inputFolder)
@@ -121,6 +128,9 @@ func TestInstallExecutable(t *testing.T) {
121128
require.NoError(t, sourceFile.Close())
122129

123130
e := executable{
131+
fileInstaller: fileInstaller{
132+
logger: logger,
133+
},
124134
source: source,
125135
target: executableTarget{
126136
dotfileName: "input.real",

tools/container/toolkit/options.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
# Copyright 2024 NVIDIA CORPORATION
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 toolkit
18+
19+
import "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
20+
21+
// An Option provides a mechanism to configure an Installer.
22+
type Option func(*Installer)
23+
24+
func WithLogger(logger logger.Interface) Option {
25+
return func(i *Installer) {
26+
i.logger = logger
27+
}
28+
}
29+
30+
func WithToolkitRoot(toolkitRoot string) Option {
31+
return func(i *Installer) {
32+
i.toolkitRoot = toolkitRoot
33+
}
34+
}
35+
36+
func WithSourceRoot(sourceRoot string) Option {
37+
return func(i *Installer) {
38+
i.sourceRoot = sourceRoot
39+
}
40+
}

tools/container/toolkit/runtime.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const (
2929

3030
// installContainerRuntimes sets up the NVIDIA container runtimes, copying the executables
3131
// and implementing the required wrapper
32-
func installContainerRuntimes(sourceRoot string, toolkitDir string) error {
32+
func (t *Installer) installContainerRuntimes(toolkitDir string) error {
3333
runtimes := operator.GetRuntimes()
3434
for _, runtime := range runtimes {
35-
r := newNvidiaContainerRuntimeInstaller(filepath.Join(sourceRoot, runtime.Path))
35+
r := t.newNvidiaContainerRuntimeInstaller(runtime.Path)
3636

3737
_, err := r.install(toolkitDir)
3838
if err != nil {
@@ -46,17 +46,17 @@ func installContainerRuntimes(sourceRoot string, toolkitDir string) error {
4646
// This installer will copy the specified source executable to the toolkit directory.
4747
// The executable is copied to a file with the same name as the source, but with a ".real" suffix and a wrapper is
4848
// created to allow for the configuration of the runtime environment.
49-
func newNvidiaContainerRuntimeInstaller(source string) *executable {
49+
func (t *Installer) newNvidiaContainerRuntimeInstaller(source string) *executable {
5050
wrapperName := filepath.Base(source)
5151
dotfileName := wrapperName + ".real"
5252
target := executableTarget{
5353
dotfileName: dotfileName,
5454
wrapperName: wrapperName,
5555
}
56-
return newRuntimeInstaller(source, target, nil)
56+
return t.newRuntimeInstaller(source, target, nil)
5757
}
5858

59-
func newRuntimeInstaller(source string, target executableTarget, env map[string]string) *executable {
59+
func (t *Installer) newRuntimeInstaller(source string, target executableTarget, env map[string]string) *executable {
6060
preLines := []string{
6161
"",
6262
"cat /proc/modules | grep -e \"^nvidia \" >/dev/null 2>&1",
@@ -74,10 +74,11 @@ func newRuntimeInstaller(source string, target executableTarget, env map[string]
7474
}
7575

7676
r := executable{
77-
source: source,
78-
target: target,
79-
env: runtimeEnv,
80-
preLines: preLines,
77+
fileInstaller: t.fileInstaller,
78+
source: source,
79+
target: target,
80+
env: runtimeEnv,
81+
preLines: preLines,
8182
}
8283

8384
return &r

tools/container/toolkit/runtime_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ import (
2121
"strings"
2222
"testing"
2323

24+
testlog "github.com/sirupsen/logrus/hooks/test"
2425
"github.com/stretchr/testify/require"
2526
)
2627

2728
func TestNvidiaContainerRuntimeInstallerWrapper(t *testing.T) {
28-
r := newNvidiaContainerRuntimeInstaller(nvidiaContainerRuntimeSource)
29+
logger, _ := testlog.NewNullLogger()
30+
i := Installer{
31+
fileInstaller: fileInstaller{
32+
logger: logger,
33+
},
34+
}
35+
r := i.newNvidiaContainerRuntimeInstaller(nvidiaContainerRuntimeSource)
2936

3037
const shebang = "#! /bin/sh"
3138
const destFolder = "/dest/folder"

0 commit comments

Comments
 (0)