prysm-pulse/shared/prereq/prereq.go
Preston Van Loon 7cc32c4dda
Various code inspection resolutions (#7438)
* remove unused code

* remove defer use in loop

* Remove unused methods and constants

* gofmt and gaz

* nilness check

* remove unused args

* Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437

* replace empty slice declaration

* Remove unnecessary type conversions

* remove redundant type declaration

* rename receivers to be consistent

* Remove bootnode query tool. It is now obsolete by discv5

* Remove relay node. It is no longer used or supported

* Revert "Remove relay node. It is no longer used or supported"

This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810.

* Delete unused test directory

* Delete unsupported gcp startup script

* Delete old k8s script

* build fixes

* fix build

* go mod tidy

* revert slasher/db/kv/block_header.go

* fix build

* remove redundant nil check

* combine func args

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-10-12 08:11:05 +00:00

109 lines
3.2 KiB
Go

package prereq
import (
"context"
"os/exec"
"runtime"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type platform struct {
os string
arch string
majorVersion int
minorVersion int
}
var (
// execShellOutput has execShellOutputFunc as the default but can be changed for testing purposes.
execShellOutput = execShellOutputFunc
runtimeOS = runtime.GOOS
runtimeArch = runtime.GOARCH
)
// execShellOutputFunc passes a command and args to exec.CommandContext and returns the result as a string
func execShellOutputFunc(ctx context.Context, command string, args ...string) (string, error) {
result, err := exec.CommandContext(ctx, command, args...).Output()
if err != nil {
return "", errors.Wrap(err, "error in command execution")
}
return string(result), nil
}
func getSupportedPlatforms() []platform {
return []platform{
{os: "linux", arch: "amd64"},
{os: "linux", arch: "arm64"},
{os: "darwin", arch: "amd64", majorVersion: 10, minorVersion: 14},
{os: "windows", arch: "amd64"},
}
}
// parseVersion takes a string and splits it using sep separator, and outputs a slice of integers
// corresponding to version numbers. If it cannot find num level of versions, it returns an error
func parseVersion(input string, num int, sep string) ([]int, error) {
var version = make([]int, num)
components := strings.Split(input, sep)
for i, component := range components {
components[i] = strings.TrimSpace(component)
}
if len(components) < num {
return nil, errors.New("insufficient information about version")
}
for i := range version {
var err error
version[i], err = strconv.Atoi(components[i])
if err != nil {
return nil, errors.Wrap(err, "error during conversion")
}
}
return version, nil
}
// meetsMinPlatformReqs returns true if the runtime matches any on the list of supported platforms
func meetsMinPlatformReqs(ctx context.Context) (bool, error) {
okPlatforms := getSupportedPlatforms()
for _, platform := range okPlatforms {
if runtimeOS == platform.os && runtimeArch == platform.arch {
// If MacOS we make sure it meets the minimum version cutoff
if runtimeOS == "darwin" {
versionStr, err := execShellOutput(ctx, "uname", "-r")
if err != nil {
return false, errors.Wrap(err, "error obtaining MacOS version")
}
version, err := parseVersion(versionStr, 2, ".")
if err != nil {
return false, errors.Wrap(err, "error parsing version")
}
if version[0] != platform.majorVersion {
return version[0] > platform.majorVersion, nil
}
if version[1] < platform.minorVersion {
return false, nil
}
return true, nil
}
// Otherwise we have a match between runtime and our list of accepted platforms
return true, nil
}
}
return false, nil
}
// WarnIfNotSupported warns if the user's platform is not supported or if it fails to detect user's platform
func WarnIfNotSupported(ctx context.Context) {
supported, err := meetsMinPlatformReqs(ctx)
if err != nil {
log.Warnf("Failed to detect host platform: %v", err)
return
}
if !supported {
log.Warn("This platform is not supported. The following platforms are supported: Linux/AMD64," +
" Linux/ARM64, Mac OS X/AMD64 (10.14+ only), and Windows/AMD64")
}
}