Skip to content

Commit c22f3bd

Browse files
authored
Merge pull request #765 from elezar/use-logger-in-toolkit-install
Simplify standalone installer
2 parents 6375e83 + 6bd292e commit c22f3bd

29 files changed

+393
-267
lines changed

tools/container/container.go renamed to cmd/nvidia-ctk-installer/container/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"github.com/sirupsen/logrus"
2525
"github.com/urfave/cli/v2"
2626

27+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/operator"
2728
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
28-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
2929
)
3030

3131
const (

tools/container/runtime/containerd/config_v1_test.go renamed to cmd/nvidia-ctk-installer/container/runtime/containerd/config_v1_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
testlog "github.com/sirupsen/logrus/hooks/test"
2424
"github.com/stretchr/testify/require"
2525

26+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2627
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
2728
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
28-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
2929
)
3030

3131
func TestUpdateV1ConfigDefaultRuntime(t *testing.T) {

tools/container/runtime/containerd/config_v2_test.go renamed to cmd/nvidia-ctk-installer/container/runtime/containerd/config_v2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
testlog "github.com/sirupsen/logrus/hooks/test"
2424
"github.com/stretchr/testify/require"
2525

26+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2627
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
2728
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
28-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
2929
)
3030

3131
const (

tools/container/runtime/containerd/containerd.go renamed to cmd/nvidia-ctk-installer/container/runtime/containerd/containerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
log "github.com/sirupsen/logrus"
2424
cli "github.com/urfave/cli/v2"
2525

26+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2627
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2728
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
2829
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
29-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
3030
)
3131

3232
const (

tools/container/runtime/crio/crio.go renamed to cmd/nvidia-ctk-installer/container/runtime/crio/crio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
log "github.com/sirupsen/logrus"
2525
cli "github.com/urfave/cli/v2"
2626

27+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2728
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
2829
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2930
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
3031
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/ocihook"
3132
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
32-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
3333
)
3434

3535
const (

tools/container/runtime/docker/docker.go renamed to cmd/nvidia-ctk-installer/container/runtime/docker/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
log "github.com/sirupsen/logrus"
2323
cli "github.com/urfave/cli/v2"
2424

25+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2526
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2627
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/docker"
27-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
2828
)
2929

3030
const (

tools/container/runtime/docker/docker_test.go renamed to cmd/nvidia-ctk-installer/container/runtime/docker/docker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222

2323
"github.com/stretchr/testify/require"
2424

25+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
2526
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/docker"
26-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
2727
)
2828

2929
func TestUpdateConfigDefaultRuntime(t *testing.T) {

tools/container/runtime/runtime.go renamed to cmd/nvidia-ctk-installer/container/runtime/runtime.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121

2222
"github.com/urfave/cli/v2"
2323

24-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
25-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/containerd"
26-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/crio"
27-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/docker"
24+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container"
25+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/containerd"
26+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/crio"
27+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/docker"
2828
)
2929

3030
const (

tools/container/toolkit/executable.go renamed to cmd/nvidia-ctk-installer/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 renamed to cmd/nvidia-ctk-installer/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",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 (
20+
"fmt"
21+
"io"
22+
"os"
23+
"path/filepath"
24+
25+
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
26+
)
27+
28+
type fileInstaller struct {
29+
logger logger.Interface
30+
// sourceRoot specifies the root that is searched for the components to install.
31+
sourceRoot string
32+
}
33+
34+
// installFileToFolder copies a source file to a destination folder.
35+
// The path of the input file is ignored.
36+
// e.g. installFileToFolder("/some/path/file.txt", "/output/path")
37+
// will result in a file "/output/path/file.txt" being generated
38+
func (t *fileInstaller) installFileToFolder(destFolder string, src string) (string, error) {
39+
name := filepath.Base(src)
40+
return t.installFileToFolderWithName(destFolder, name, src)
41+
}
42+
43+
// cp src destFolder/name
44+
func (t *fileInstaller) installFileToFolderWithName(destFolder string, name, src string) (string, error) {
45+
dest := filepath.Join(destFolder, name)
46+
err := t.installFile(dest, src)
47+
if err != nil {
48+
return "", fmt.Errorf("error copying '%v' to '%v': %v", src, dest, err)
49+
}
50+
return dest, nil
51+
}
52+
53+
// installFile copies a file from src to dest and maintains
54+
// file modes
55+
func (t *fileInstaller) installFile(dest string, src string) error {
56+
src = filepath.Join(t.sourceRoot, src)
57+
t.logger.Infof("Installing '%v' to '%v'", src, dest)
58+
59+
source, err := os.Open(src)
60+
if err != nil {
61+
return fmt.Errorf("error opening source: %v", err)
62+
}
63+
defer source.Close()
64+
65+
destination, err := os.Create(dest)
66+
if err != nil {
67+
return fmt.Errorf("error creating destination: %v", err)
68+
}
69+
defer destination.Close()
70+
71+
_, err = io.Copy(destination, source)
72+
if err != nil {
73+
return fmt.Errorf("error copying file: %v", err)
74+
}
75+
76+
err = applyModeFromSource(dest, src)
77+
if err != nil {
78+
return fmt.Errorf("error setting destination file mode: %v", err)
79+
}
80+
return nil
81+
}
82+
83+
// applyModeFromSource sets the file mode for a destination file
84+
// to match that of a specified source file
85+
func applyModeFromSource(dest string, src string) error {
86+
sourceInfo, err := os.Stat(src)
87+
if err != nil {
88+
return fmt.Errorf("error getting file info for '%v': %v", src, err)
89+
}
90+
err = os.Chmod(dest, sourceInfo.Mode())
91+
if err != nil {
92+
return fmt.Errorf("error setting mode for '%v': %v", dest, err)
93+
}
94+
return nil
95+
}
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 renamed to cmd/nvidia-ctk-installer/container/toolkit/runtime.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"path/filepath"
2222

23-
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
23+
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/operator"
2424
)
2525

2626
const (
@@ -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 renamed to cmd/nvidia-ctk-installer/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)