2021-10-29 14:44:31 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# 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 oci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewLowLevelRuntime creates a Runtime that wraps a low-level runtime executable.
|
|
|
|
// The executable specified is taken from the list of supplied candidates, with the first match
|
2022-01-31 14:11:36 +00:00
|
|
|
// present in the PATH being selected. A logger is also specified.
|
|
|
|
func NewLowLevelRuntime(logger *log.Logger, candidates []string) (Runtime, error) {
|
2021-11-16 20:37:50 +00:00
|
|
|
runtimePath, err := findRuntime(logger, candidates)
|
2021-10-29 14:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error locating runtime: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
return NewRuntimeForPath(logger, runtimePath)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// findRuntime checks elements in a list of supplied candidates for a matching executable in the PATH.
|
|
|
|
// The absolute path to the first match is returned.
|
2021-11-16 20:37:50 +00:00
|
|
|
func findRuntime(logger *log.Logger, candidates []string) (string, error) {
|
2021-10-29 14:44:31 +00:00
|
|
|
if len(candidates) == 0 {
|
|
|
|
return "", fmt.Errorf("at least one runtime candidate must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, candidate := range candidates {
|
2021-11-16 20:37:50 +00:00
|
|
|
logger.Infof("Looking for runtime binary '%v'", candidate)
|
2021-10-29 14:44:31 +00:00
|
|
|
runcPath, err := exec.LookPath(candidate)
|
|
|
|
if err == nil {
|
2021-11-16 20:37:50 +00:00
|
|
|
logger.Infof("Found runtime binary '%v'", runcPath)
|
2021-10-29 14:44:31 +00:00
|
|
|
return runcPath, nil
|
|
|
|
}
|
2021-11-16 20:37:50 +00:00
|
|
|
logger.Warnf("Runtime binary '%v' not found: %v", candidate, err)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("no runtime binary found from candidate list: %v", candidates)
|
|
|
|
}
|