2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2017-08-08 15:43:48 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatePlatform determines if a platform structure is valid.
|
|
|
|
// TODO This is a temporary function - can be replaced by parsing from
|
|
|
|
// https://github.com/containerd/containerd/pull/1403/files at a later date.
|
|
|
|
// @jhowardmsft
|
|
|
|
func ValidatePlatform(platform *specs.Platform) error {
|
|
|
|
platform.Architecture = strings.ToLower(platform.Architecture)
|
|
|
|
platform.OS = strings.ToLower(platform.OS)
|
2017-09-13 15:49:04 -04:00
|
|
|
// Based on https://github.com/moby/moby/pull/34642#issuecomment-330375350, do
|
|
|
|
// not support anything except operating system.
|
|
|
|
if platform.Architecture != "" {
|
2017-08-08 15:43:48 -04:00
|
|
|
return fmt.Errorf("invalid platform architecture %q", platform.Architecture)
|
|
|
|
}
|
|
|
|
if platform.OS != "" {
|
|
|
|
if !(platform.OS == runtime.GOOS || (LCOWSupported() && platform.OS == "linux")) {
|
|
|
|
return fmt.Errorf("invalid platform os %q", platform.OS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(platform.OSFeatures) != 0 {
|
|
|
|
return fmt.Errorf("invalid platform osfeatures %q", platform.OSFeatures)
|
|
|
|
}
|
|
|
|
if platform.OSVersion != "" {
|
|
|
|
return fmt.Errorf("invalid platform osversion %q", platform.OSVersion)
|
|
|
|
}
|
|
|
|
if platform.Variant != "" {
|
|
|
|
return fmt.Errorf("invalid platform variant %q", platform.Variant)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePlatform parses a platform string in the format os[/arch[/variant]
|
|
|
|
// into an OCI image-spec platform structure.
|
|
|
|
// TODO This is a temporary function - can be replaced by parsing from
|
|
|
|
// https://github.com/containerd/containerd/pull/1403/files at a later date.
|
|
|
|
// @jhowardmsft
|
|
|
|
func ParsePlatform(in string) *specs.Platform {
|
|
|
|
p := &specs.Platform{}
|
|
|
|
elements := strings.SplitN(strings.ToLower(in), "/", 3)
|
|
|
|
if len(elements) == 3 {
|
|
|
|
p.Variant = elements[2]
|
|
|
|
}
|
|
|
|
if len(elements) >= 2 {
|
|
|
|
p.Architecture = elements[1]
|
|
|
|
}
|
|
|
|
if len(elements) >= 1 {
|
|
|
|
p.OS = elements[0]
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2017-09-19 15:14:46 -04:00
|
|
|
|
|
|
|
// IsOSSupported determines if an operating system is supported by the host
|
|
|
|
func IsOSSupported(os string) bool {
|
|
|
|
if runtime.GOOS == os {
|
|
|
|
return true
|
|
|
|
}
|
2017-11-20 11:33:20 -05:00
|
|
|
if LCOWSupported() && os == "linux" {
|
2017-09-19 15:14:46 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|