1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/parsers/operatingsystem/operatingsystem_windows.go
Jean Rouge d363a1881e Adding OS version info to the nodes' Info struct
This is needed so that we can add OS version constraints in Swarmkit, which
does require the engine to report its host's OS version (see
https://github.com/docker/swarmkit/issues/2770).

The OS version is parsed from the `os-release` file on Linux, and from the
`ReleaseId` string value of the `SOFTWARE\Microsoft\Windows NT\CurrentVersion`
registry key on Windows.

Added unit tests when possible, as well as Prometheus metrics.

Signed-off-by: Jean Rouge <rougej+github@gmail.com>
2019-06-06 22:40:10 +00:00

63 lines
1.7 KiB
Go

package operatingsystem // import "github.com/docker/docker/pkg/parsers/operatingsystem"
import (
"fmt"
"github.com/docker/docker/pkg/system"
"golang.org/x/sys/windows/registry"
)
// GetOperatingSystem gets the name of the current operating system.
func GetOperatingSystem() (string, error) {
os, err := withCurrentVersionRegistryKey(func(key registry.Key) (os string, err error) {
if os, _, err = key.GetStringValue("ProductName"); err != nil {
return "", err
}
releaseId, _, err := key.GetStringValue("ReleaseId")
if err != nil {
return
}
os = fmt.Sprintf("%s Version %s", os, releaseId)
buildNumber, _, err := key.GetStringValue("CurrentBuildNumber")
if err != nil {
return
}
ubr, _, err := key.GetIntegerValue("UBR")
if err != nil {
return
}
os = fmt.Sprintf("%s (OS Build %s.%d)", os, buildNumber, ubr)
return
})
if os == "" {
// Default return value
os = "Unknown Operating System"
}
return os, err
}
func withCurrentVersionRegistryKey(f func(registry.Key) (string, error)) (string, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return "", err
}
defer key.Close()
return f(key)
}
// GetOperatingSystemVersion gets the version of the current operating system, as a string.
func GetOperatingSystemVersion() (string, error) {
version := system.GetOSVersion()
return fmt.Sprintf("%d.%d.%d", version.MajorVersion, version.MinorVersion, version.Build), nil
}
// IsContainerized returns true if we are running inside a container.
// No-op on Windows, always returns false.
func IsContainerized() (bool, error) {
return false, nil
}