Skip to content

Commit 2a5d5c4

Browse files
committed
[no-relnote] Move fileinstaller to separate file
Signed-off-by: Evan Lezar <[email protected]>
1 parent bc19180 commit 2a5d5c4

File tree

2 files changed

+115
-87
lines changed

2 files changed

+115
-87
lines changed
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+
}

tools/container/toolkit/toolkit.go

Lines changed: 20 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package toolkit
1818

1919
import (
2020
"fmt"
21-
"io"
2221
"os"
2322
"path/filepath"
2423
"strings"
@@ -80,28 +79,6 @@ type Options struct {
8079
optInFeatures cli.StringSlice
8180
}
8281

83-
type Installer struct {
84-
fileInstaller
85-
toolkitRoot string
86-
}
87-
88-
type fileInstaller struct {
89-
logger logger.Interface
90-
sourceRoot string
91-
}
92-
93-
func NewInstaller(opts ...Option) *Installer {
94-
i := &Installer{}
95-
for _, opt := range opts {
96-
opt(i)
97-
}
98-
99-
if i.logger == nil {
100-
i.logger = logger.New()
101-
}
102-
return i
103-
}
104-
10582
func Flags(opts *Options) []cli.Flag {
10683
flags := []cli.Flag{
10784
&cli.StringFlag{
@@ -234,6 +211,26 @@ func Flags(opts *Options) []cli.Flag {
234211
return flags
235212
}
236213

214+
// An Installer is used to install the NVIDIA Container Toolkit from the toolkit container.
215+
type Installer struct {
216+
fileInstaller
217+
// toolkitRoot specifies the destination path at which the toolkit is installed.
218+
toolkitRoot string
219+
}
220+
221+
// NewInstaller creates an installer for the NVIDIA Container Toolkit.
222+
func NewInstaller(opts ...Option) *Installer {
223+
i := &Installer{}
224+
for _, opt := range opts {
225+
opt(i)
226+
}
227+
228+
if i.logger == nil {
229+
i.logger = logger.New()
230+
}
231+
return i
232+
}
233+
237234
// ValidateOptions checks whether the specified options are valid
238235
func (t *Installer) ValidateOptions(opts *Options) error {
239236
if t == nil {
@@ -280,7 +277,6 @@ func (t *Installer) ValidateOptions(opts *Options) error {
280277
}
281278

282279
// Install installs the components of the NVIDIA container toolkit.
283-
// The specified sourceRoot is searched for the components to install.
284280
// Any existing installation is removed.
285281
func (t *Installer) Install(cli *cli.Context, opts *Options) error {
286282
if t == nil {
@@ -622,69 +618,6 @@ func (t *Installer) installSymlink(toolkitRoot string, link string, target strin
622618
return nil
623619
}
624620

625-
// installFileToFolder copies a source file to a destination folder.
626-
// The path of the input file is ignored.
627-
// e.g. installFileToFolder("/some/path/file.txt", "/output/path")
628-
// will result in a file "/output/path/file.txt" being generated
629-
func (t *fileInstaller) installFileToFolder(destFolder string, src string) (string, error) {
630-
name := filepath.Base(src)
631-
return t.installFileToFolderWithName(destFolder, name, src)
632-
}
633-
634-
// cp src destFolder/name
635-
func (t *fileInstaller) installFileToFolderWithName(destFolder string, name, src string) (string, error) {
636-
dest := filepath.Join(destFolder, name)
637-
err := t.installFile(dest, src)
638-
if err != nil {
639-
return "", fmt.Errorf("error copying '%v' to '%v': %v", src, dest, err)
640-
}
641-
return dest, nil
642-
}
643-
644-
// installFile copies a file from src to dest and maintains
645-
// file modes
646-
func (t *fileInstaller) installFile(dest string, src string) error {
647-
src = filepath.Join(t.sourceRoot, src)
648-
t.logger.Infof("Installing '%v' to '%v'", src, dest)
649-
650-
source, err := os.Open(src)
651-
if err != nil {
652-
return fmt.Errorf("error opening source: %v", err)
653-
}
654-
defer source.Close()
655-
656-
destination, err := os.Create(dest)
657-
if err != nil {
658-
return fmt.Errorf("error creating destination: %v", err)
659-
}
660-
defer destination.Close()
661-
662-
_, err = io.Copy(destination, source)
663-
if err != nil {
664-
return fmt.Errorf("error copying file: %v", err)
665-
}
666-
667-
err = applyModeFromSource(dest, src)
668-
if err != nil {
669-
return fmt.Errorf("error setting destination file mode: %v", err)
670-
}
671-
return nil
672-
}
673-
674-
// applyModeFromSource sets the file mode for a destination file
675-
// to match that of a specified source file
676-
func applyModeFromSource(dest string, src string) error {
677-
sourceInfo, err := os.Stat(src)
678-
if err != nil {
679-
return fmt.Errorf("error getting file info for '%v': %v", src, err)
680-
}
681-
err = os.Chmod(dest, sourceInfo.Mode())
682-
if err != nil {
683-
return fmt.Errorf("error setting mode for '%v': %v", dest, err)
684-
}
685-
return nil
686-
}
687-
688621
// findLibrary searches a set of candidate libraries in the specified root for
689622
// a given library name
690623
func (t *Installer) findLibrary(libName string) (string, error) {

0 commit comments

Comments
 (0)