Skip to content

Commit 4da1db4

Browse files
committed
TOFIX: detect host type
Signed-off-by: Evan Lezar <[email protected]>
1 parent bfea632 commit 4da1db4

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

cmd/nvidia-ctk-installer/toolkit/installer/installer.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type toolkitInstaller struct {
3838
ignoreErrors bool
3939
sourceRoot string
4040

41+
hostRoot string
42+
packageType string
4143
artifactRoot *artifactRoot
4244

4345
ensureTargetDirectory Installer
@@ -47,7 +49,10 @@ var _ Installer = (*toolkitInstaller)(nil)
4749

4850
// New creates a toolkit installer with the specified options.
4951
func New(opts ...Option) (Installer, error) {
50-
t := &toolkitInstaller{}
52+
t := &toolkitInstaller{
53+
hostRoot: "/",
54+
packageType: "auto",
55+
}
5156
for _, opt := range opts {
5257
opt(t)
5358
}
@@ -56,7 +61,11 @@ func New(opts ...Option) (Installer, error) {
5661
t.logger = logger.New()
5762
}
5863
if t.sourceRoot == "" {
59-
t.sourceRoot = "/"
64+
sourceRoot, err := defaultSourceRoot(t.hostRoot, t.packageType)
65+
if err != nil {
66+
return nil, err
67+
}
68+
t.sourceRoot = sourceRoot
6069
}
6170
if t.artifactRoot == nil {
6271
artifactRoot, err := newArtifactRoot(t.logger, t.sourceRoot)

cmd/nvidia-ctk-installer/toolkit/installer/options.go

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ func WithSourceRoot(sourceRoot string) Option {
4545
ti.sourceRoot = sourceRoot
4646
}
4747
}
48+
49+
func WithPackageType(packageType string) Option {
50+
return func(ti *toolkitInstaller) {
51+
ti.packageType = packageType
52+
}
53+
}
54+
55+
func WithHostRoot(hostRoot string) Option {
56+
return func(ti *toolkitInstaller) {
57+
ti.hostRoot = hostRoot
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
*
3+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
*
18+
*/
19+
package installer
20+
21+
import (
22+
"fmt"
23+
"os"
24+
"path/filepath"
25+
)
26+
27+
func resolvePackageType(hostRoot string, packageType string) (rPackageTypes string, rerr error) {
28+
if packageType != "" && packageType != "auto" {
29+
return packageType, nil
30+
}
31+
32+
if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/rpm")); err != nil && !info.IsDir() {
33+
return "rpm", nil
34+
}
35+
if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/dpkg")); err != nil && !info.IsDir() {
36+
return "deb", nil
37+
}
38+
39+
return "deb", nil
40+
}
41+
42+
func defaultSourceRoot(hostRoot string, packageType string) (string, error) {
43+
resolvedPackageType, err := resolvePackageType(hostRoot, packageType)
44+
if err != nil {
45+
return "", err
46+
}
47+
switch resolvedPackageType {
48+
case "deb":
49+
return "/artifacts/deb", nil
50+
case "rpm":
51+
return "/artifacts/rpm", nil
52+
default:
53+
return "", fmt.Errorf("invalid package type: %v", resolvedPackageType)
54+
}
55+
}

0 commit comments

Comments
 (0)