TOFIX: detect host type
Some checks failed
CI Pipeline / code-scanning (push) Has been cancelled
CI Pipeline / variables (push) Has been cancelled
CI Pipeline / golang (push) Has been cancelled
CI Pipeline / image (push) Has been cancelled
CI Pipeline / e2e-test (push) Has been cancelled

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2025-03-14 15:14:38 +02:00
parent bfea63246d
commit 4da1db4549
No known key found for this signature in database
3 changed files with 78 additions and 2 deletions

View File

@ -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)

View File

@ -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
}
}

View File

@ -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)
}
}