diff --git a/cmd/nvidia-ctk-installer/toolkit/installer/installer.go b/cmd/nvidia-ctk-installer/toolkit/installer/installer.go index 86c734a5..6cb3ed88 100644 --- a/cmd/nvidia-ctk-installer/toolkit/installer/installer.go +++ b/cmd/nvidia-ctk-installer/toolkit/installer/installer.go @@ -38,6 +38,8 @@ type toolkitInstaller struct { ignoreErrors bool sourceRoot string + hostRoot string + packageType string artifactRoot *artifactRoot ensureTargetDirectory Installer @@ -47,7 +49,10 @@ var _ Installer = (*toolkitInstaller)(nil) // New creates a toolkit installer with the specified options. func New(opts ...Option) (Installer, error) { - t := &toolkitInstaller{} + t := &toolkitInstaller{ + hostRoot: "/", + packageType: "auto", + } for _, opt := range opts { opt(t) } @@ -56,7 +61,11 @@ func New(opts ...Option) (Installer, error) { t.logger = logger.New() } if t.sourceRoot == "" { - t.sourceRoot = "/" + sourceRoot, err := defaultSourceRoot(t.hostRoot, t.packageType) + if err != nil { + return nil, err + } + t.sourceRoot = sourceRoot } if t.artifactRoot == nil { artifactRoot, err := newArtifactRoot(t.logger, t.sourceRoot) diff --git a/cmd/nvidia-ctk-installer/toolkit/installer/options.go b/cmd/nvidia-ctk-installer/toolkit/installer/options.go index 153884b7..14bdef9d 100644 --- a/cmd/nvidia-ctk-installer/toolkit/installer/options.go +++ b/cmd/nvidia-ctk-installer/toolkit/installer/options.go @@ -45,3 +45,15 @@ func WithSourceRoot(sourceRoot string) Option { ti.sourceRoot = sourceRoot } } + +func WithPackageType(packageType string) Option { + return func(ti *toolkitInstaller) { + ti.packageType = packageType + } +} + +func WithHostRoot(hostRoot string) Option { + return func(ti *toolkitInstaller) { + ti.hostRoot = hostRoot + } +} diff --git a/cmd/nvidia-ctk-installer/toolkit/installer/package-type.go b/cmd/nvidia-ctk-installer/toolkit/installer/package-type.go new file mode 100644 index 00000000..107a0dea --- /dev/null +++ b/cmd/nvidia-ctk-installer/toolkit/installer/package-type.go @@ -0,0 +1,55 @@ +/* +* +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +* +*/ +package installer + +import ( + "fmt" + "os" + "path/filepath" +) + +func resolvePackageType(hostRoot string, packageType string) (rPackageTypes string, rerr error) { + if packageType != "" && packageType != "auto" { + return packageType, nil + } + + if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/rpm")); err != nil && !info.IsDir() { + return "rpm", nil + } + if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/dpkg")); err != nil && !info.IsDir() { + return "deb", nil + } + + return "deb", nil +} + +func defaultSourceRoot(hostRoot string, packageType string) (string, error) { + resolvedPackageType, err := resolvePackageType(hostRoot, packageType) + if err != nil { + return "", err + } + switch resolvedPackageType { + case "deb": + return "/artifacts/deb", nil + case "rpm": + return "/artifacts/rpm", nil + default: + return "", fmt.Errorf("invalid package type: %v", resolvedPackageType) + } +}