Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add more daemon option in get /info API #1122

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/alibaba/pouch/apis/types"

units "github.com/docker/go-units"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func prettyPrintInfo(cli *Cli, info *types.SystemInfo) error {
}

fmt.Fprintln(os.Stdout, "CPUs:", info.NCPU)
fmt.Fprintln(os.Stdout, "Total Memory:", info.MemTotal)
fmt.Fprintln(os.Stdout, "Total Memory: "+units.BytesSize(float64(info.MemTotal)))
fmt.Fprintln(os.Stdout, "Pouch Root Dir:", info.PouchRootDir)
fmt.Fprintln(os.Stdout, "LiveRestoreEnabled:", info.LiveRestoreEnabled)
if info.RegistryConfig != nil && (len(info.RegistryConfig.InsecureRegistryCIDRs) > 0 || len(info.RegistryConfig.IndexConfigs) > 0) {
Expand Down
38 changes: 31 additions & 7 deletions daemon/mgr/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mgr

import (
"fmt"
"os"
"runtime"
"strings"
"sync/atomic"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/system"
"github.com/alibaba/pouch/registry"
"github.com/alibaba/pouch/version"

Expand Down Expand Up @@ -73,6 +75,27 @@ func (mgr *SystemManager) Info() (types.SystemInfo, error) {
return nil
})

hostname := "<unknown>"
if name, err := os.Hostname(); err != nil {
logrus.Warnf("failed to get hostname: %v", err)
} else {
hostname = name
}

totalMem := int64(0)
if mem, err := system.GetTotalMem(); err != nil {
logrus.Warnf("failed to get system mem: %v", err)
} else {
totalMem = int64(mem)
}

OSName := "<unknown>"
if osName, err := system.GetOSName(); err != nil {
logrus.Warnf("failed to get operating system: %v", err)
} else {
OSName = osName
}

info := types.SystemInfo{
// architecture: ,
// CgroupDriver: ,
Expand All @@ -83,7 +106,8 @@ func (mgr *SystemManager) Info() (types.SystemInfo, error) {
ContainersStopped: cStopped,
Debug: mgr.config.Debug,
DefaultRuntime: mgr.config.DefaultRuntime,
// Driver: ,
// FIXME: avoid hard code
Driver: "overlayfs",
// DriverStatus: ,
// ExperimentalBuild: ,
HTTPProxy: mgr.config.ImageProxy,
Expand All @@ -96,12 +120,12 @@ func (mgr *SystemManager) Info() (types.SystemInfo, error) {
Labels: mgr.config.Labels,
// LiveRestoreEnabled: ,
// LoggingDriver: ,
// MemTotal: ,
// Name: ,
// NCPU: ,
// OperatingSystem: ,
OSType: runtime.GOOS,
PouchRootDir: mgr.config.HomeDir,
MemTotal: totalMem,
Name: hostname,
NCPU: int64(runtime.NumCPU()),
OperatingSystem: OSName,
OSType: runtime.GOOS,
PouchRootDir: mgr.config.HomeDir,
// RegistryConfig: ,
// RuncCommit: ,
// Runtimes: ,
Expand Down
52 changes: 52 additions & 0 deletions pkg/system/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package system

import (
"bufio"
"fmt"
"os"
"strings"
)

// file to check to determine Operating System
const etcOsRelease = "/etc/os-release"

// GetOSName gets data in /etc/os-release and gets OS name.
// For example, in a Ubuntu host, fetched data are like:
// root@i-8brpbc9t:~# cat /etc/os-release
// NAME="Ubuntu"
// VERSION="16.04.2 LTS (Xenial Xerus)"
// ID=ubuntu
// ID_LIKE=debian
// PRETTY_NAME="Ubuntu 16.04.2 LTS"
// VERSION_ID="16.04"
// HOME_URL="http://www.ubuntu.com/"
// SUPPORT_URL="http://help.ubuntu.com/"
// BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
// VERSION_CODENAME=xenial
// UBUNTU_CODENAME=xenial
func GetOSName() (string, error) {
etcOsReleaseFile, err := os.Open(etcOsRelease)
if err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("failed to open %s: %v", etcOsRelease, err)
}
}
defer etcOsReleaseFile.Close()

var prettyName string

scanner := bufio.NewScanner(etcOsReleaseFile)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "PRETTY_NAME=") {
continue
}

data := strings.SplitN(line, "=", 2)
prettyName = data[1]
return prettyName, nil
}

return "Linux", nil

}
24 changes: 24 additions & 0 deletions pkg/system/sysinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package system

import (
"syscall"
)

// getSysInfo gets sysinfo.
func getSysInfo() (*syscall.Sysinfo_t, error) {
si := &syscall.Sysinfo_t{}
err := syscall.Sysinfo(si)
if err != nil {
return nil, err
}
return si, nil
}

// GetTotalMem gets total ram of host.
func GetTotalMem() (uint64, error) {
si, err := getSysInfo()
if err != nil {
return 0, err
}
return si.Totalram, nil
}
4 changes: 2 additions & 2 deletions test/api_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func (suite *APISystemSuite) TestInfo(c *check.C) {
kernelInfo := "<unknown>"
if Info, err := kernel.GetKernelVersion(); err == nil {
kernelInfo = Info.String()

}
// TODO more variables are to be checked.
c.Assert(got.IndexServerAddress, check.Equals, "https://index.docker.io/v1/")
c.Assert(got.KernelVersion, check.Equals, kernelInfo)
c.Assert(got.OSType, check.Equals, runtime.GOOS)
c.Assert(got.ServerVersion, check.Equals, version.Version)

c.Assert(got.Driver, check.Equals, "overlayfs")
c.Assert(got.NCPU, check.Equals, int64(runtime.NumCPU()))
}

// TestVersion tests /version API.
Expand Down