Browse code

Merge pull request #17478 from vdemeester/pr-13921

Carry#13921 : Expand /info: Expose OSType (GOOS), Architecture (GOARCH)

Michael Crosby authored on 2015/11/18 08:44:57
Showing 12 changed files
... ...
@@ -59,6 +59,8 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
59 59
 
60 60
 	ioutils.FprintfIfNotEmpty(cli.out, "Kernel Version: %s\n", info.KernelVersion)
61 61
 	ioutils.FprintfIfNotEmpty(cli.out, "Operating System: %s\n", info.OperatingSystem)
62
+	ioutils.FprintfIfNotEmpty(cli.out, "OSType: %s\n", info.OSType)
63
+	ioutils.FprintfIfNotEmpty(cli.out, "Architecture: %s\n", info.Architecture)
62 64
 	fmt.Fprintf(cli.out, "CPUs: %d\n", info.NCPU)
63 65
 	fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(info.MemTotal)))
64 66
 	ioutils.FprintfIfNotEmpty(cli.out, "Name: %s\n", info.Name)
... ...
@@ -208,6 +208,8 @@ type Info struct {
208 208
 	NEventsListener    int
209 209
 	KernelVersion      string
210 210
 	OperatingSystem    string
211
+	OSType             string
212
+	Architecture       string
211 213
 	IndexServerAddress string
212 214
 	RegistryConfig     *registry.ServiceConfig
213 215
 	InitSha1           string
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/docker/pkg/fileutils"
12 12
 	"github.com/docker/docker/pkg/parsers/kernel"
13 13
 	"github.com/docker/docker/pkg/parsers/operatingsystem"
14
+	"github.com/docker/docker/pkg/platform"
14 15
 	"github.com/docker/docker/pkg/sysinfo"
15 16
 	"github.com/docker/docker/pkg/system"
16 17
 	"github.com/docker/docker/registry"
... ...
@@ -77,6 +78,8 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
77 77
 		KernelVersion:      kernelVersion,
78 78
 		OperatingSystem:    operatingSystem,
79 79
 		IndexServerAddress: registry.IndexServer,
80
+		OSType:             platform.OSType,
81
+		Architecture:       platform.Architecture,
80 82
 		RegistryConfig:     daemon.RegistryService.Config,
81 83
 		InitSha1:           dockerversion.InitSHA1,
82 84
 		InitPath:           initPath,
... ...
@@ -1884,6 +1884,7 @@ Display system-wide information
1884 1884
     Content-Type: application/json
1885 1885
 
1886 1886
     {
1887
+        "Architecture": "x86_64",
1887 1888
         "Containers": 11,
1888 1889
         "CpuCfsPeriod": true,
1889 1890
         "CpuCfsQuota": true,
... ...
@@ -1915,6 +1916,7 @@ Display system-wide information
1915 1915
         "Name": "prod-server-42",
1916 1916
         "NoProxy": "9.81.1.160",
1917 1917
         "OomKillDisable": true,
1918
+        "OSType": "linux",
1918 1919
         "OperatingSystem": "Boot2Docker",
1919 1920
         "RegistryConfig": {
1920 1921
             "IndexConfigs": {
... ...
@@ -31,6 +31,8 @@ For example:
31 31
     Execution Driver: native-0.2
32 32
     Logging Driver: json-file
33 33
     Kernel Version: 3.19.0-22-generic
34
+    OSType: linux
35
+    Architecture: x86_64
34 36
     Operating System: Ubuntu 15.04
35 37
     CPUs: 24
36 38
     Total Memory: 62.86 GiB
... ...
@@ -23,6 +23,8 @@ func (s *DockerSuite) TestInfoApi(c *check.C) {
23 23
 		"LoggingDriver",
24 24
 		"OperatingSystem",
25 25
 		"NCPU",
26
+		"OSType",
27
+		"Architecture",
26 28
 		"MemTotal",
27 29
 		"KernelVersion",
28 30
 		"Driver",
... ...
@@ -19,6 +19,8 @@ func (s *DockerSuite) TestInfoEnsureSucceeds(c *check.C) {
19 19
 		"Containers:",
20 20
 		"Images:",
21 21
 		"Execution Driver:",
22
+		"OSType:",
23
+		"Architecture:",
22 24
 		"Logging Driver:",
23 25
 		"Operating System:",
24 26
 		"CPUs:",
... ...
@@ -44,6 +44,8 @@ Here is a sample output:
44 44
      Network: bridge null host
45 45
     Kernel Version: 3.13.0-24-generic
46 46
     Operating System: Ubuntu 14.04 LTS
47
+    OSType: linux
48
+    Architecture: x86_64
47 49
     CPUs: 1
48 50
     Total Memory: 2 GiB
49 51
 
50 52
new file mode 100644
... ...
@@ -0,0 +1,15 @@
0
+package platform
1
+
2
+import (
3
+	"os/exec"
4
+)
5
+
6
+// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
7
+func GetRuntimeArchitecture() (string, error) {
8
+	cmd := exec.Command("uname", "-m")
9
+	machine, err := cmd.Output()
10
+	if err != nil {
11
+		return "", err
12
+	}
13
+	return string(machine), nil
14
+}
0 15
new file mode 100644
... ...
@@ -0,0 +1,28 @@
0
+// Package platform provides helper function to get the runtime architecture
1
+// for different platforms.
2
+package platform
3
+
4
+import (
5
+	"syscall"
6
+)
7
+
8
+// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
9
+func GetRuntimeArchitecture() (string, error) {
10
+	utsname := &syscall.Utsname{}
11
+	if err := syscall.Uname(utsname); err != nil {
12
+		return "", err
13
+	}
14
+	return charsToString(utsname.Machine), nil
15
+}
16
+
17
+func charsToString(ca [65]int8) string {
18
+	s := make([]byte, len(ca))
19
+	var lens int
20
+	for ; lens < len(ca); lens++ {
21
+		if ca[lens] == 0 {
22
+			break
23
+		}
24
+		s[lens] = uint8(ca[lens])
25
+	}
26
+	return string(s[0:lens])
27
+}
0 28
new file mode 100644
... ...
@@ -0,0 +1,52 @@
0
+package platform
1
+
2
+import (
3
+	"fmt"
4
+	"syscall"
5
+	"unsafe"
6
+)
7
+
8
+var (
9
+	modkernel32       = syscall.NewLazyDLL("kernel32.dll")
10
+	procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
11
+)
12
+
13
+// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
14
+type systeminfo struct {
15
+	wProcessorArchitecture      uint16
16
+	wReserved                   uint16
17
+	dwPageSize                  uint32
18
+	lpMinimumApplicationAddress uintptr
19
+	lpMaximumApplicationAddress uintptr
20
+	dwActiveProcessorMask       uintptr
21
+	dwNumberOfProcessors        uint32
22
+	dwProcessorType             uint32
23
+	dwAllocationGranularity     uint32
24
+	wProcessorLevel             uint16
25
+	wProcessorRevision          uint16
26
+}
27
+
28
+// Constants
29
+const (
30
+	ProcessorArchitecture64   = 9 // PROCESSOR_ARCHITECTURE_AMD64
31
+	ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
32
+	ProcessorArchitecture32   = 0 // PROCESSOR_ARCHITECTURE_INTEL
33
+	ProcessorArchitectureArm  = 5 // PROCESSOR_ARCHITECTURE_ARM
34
+)
35
+
36
+var sysinfo systeminfo
37
+
38
+// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
39
+func GetRuntimeArchitecture() (string, error) {
40
+	syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
41
+	switch sysinfo.wProcessorArchitecture {
42
+	case ProcessorArchitecture64, ProcessorArchitectureIA64:
43
+		return "x86_64", nil
44
+	case ProcessorArchitecture32:
45
+		return "i686", nil
46
+	case ProcessorArchitectureArm:
47
+		return "arm", nil
48
+	default:
49
+		return "", fmt.Errorf("Unknown processor architecture")
50
+	}
51
+}
0 52
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+package platform
1
+
2
+import (
3
+	"runtime"
4
+
5
+	"github.com/Sirupsen/logrus"
6
+)
7
+
8
+var (
9
+	// Architecture holds the runtime architecture of the process.
10
+	Architecture string
11
+	// OSType holds the runtime operating system type (Linux, …) of the process.
12
+	OSType string
13
+)
14
+
15
+func init() {
16
+	var err error
17
+	Architecture, err = GetRuntimeArchitecture()
18
+	if err != nil {
19
+		logrus.Errorf("Could no read system architecture info: %v", err)
20
+	}
21
+	OSType = runtime.GOOS
22
+}