1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove Job from Info API

Two main things
- Create a real struct Info for all of the data with the proper types
- Add test for REST API get info

Signed-off-by: Hu Keping <hukeping@huawei.com>
This commit is contained in:
Hu Keping 2015-04-11 01:26:30 +08:00
parent 24b89b7098
commit f4942ed864
7 changed files with 163 additions and 169 deletions

View file

@ -1,12 +1,11 @@
package client package client
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"time"
"github.com/Sirupsen/logrus" "github.com/docker/docker/api/types"
"github.com/docker/docker/engine"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/units" "github.com/docker/docker/pkg/units"
) )
@ -19,127 +18,75 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
cmd.Require(flag.Exact, 0) cmd.Require(flag.Exact, 0)
cmd.ParseFlags(args, false) cmd.ParseFlags(args, false)
body, _, err := readBody(cli.call("GET", "/info", nil, nil)) rdr, _, err := cli.call("GET", "/info", nil, nil)
if err != nil { if err != nil {
return err return err
} }
out := engine.NewOutput() info := &types.Info{}
remoteInfo, err := out.AddEnv() if err := json.NewDecoder(rdr).Decode(info); err != nil {
if err != nil { return fmt.Errorf("Error reading remote info: %v", err)
return err
} }
if _, err := out.Write(body); err != nil { fmt.Fprintf(cli.out, "Containers: %d\n", info.Containers)
logrus.Errorf("Error reading remote info: %s", err) fmt.Fprintf(cli.out, "Images: %d\n", info.Images)
return err fmt.Fprintf(cli.out, "Storage Driver: %s\n", info.Driver)
} if info.DriverStatus != nil {
out.Close() for _, pair := range info.DriverStatus {
if remoteInfo.Exists("Containers") {
fmt.Fprintf(cli.out, "Containers: %d\n", remoteInfo.GetInt("Containers"))
}
if remoteInfo.Exists("Images") {
fmt.Fprintf(cli.out, "Images: %d\n", remoteInfo.GetInt("Images"))
}
if remoteInfo.Exists("Driver") {
fmt.Fprintf(cli.out, "Storage Driver: %s\n", remoteInfo.Get("Driver"))
}
if remoteInfo.Exists("DriverStatus") {
var driverStatus [][2]string
if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil {
return err
}
for _, pair := range driverStatus {
fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1]) fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1])
} }
} }
if remoteInfo.Exists("ExecutionDriver") { fmt.Fprintf(cli.out, "Execution Driver: %s\n", info.ExecutionDriver)
fmt.Fprintf(cli.out, "Execution Driver: %s\n", remoteInfo.Get("ExecutionDriver")) fmt.Fprintf(cli.out, "Logging Driver: %s\n", info.LoggingDriver)
} fmt.Fprintf(cli.out, "Kernel Version: %s\n", info.KernelVersion)
if remoteInfo.Exists("LoggingDriver") { fmt.Fprintf(cli.out, "Operating System: %s\n", info.OperatingSystem)
fmt.Fprintf(cli.out, "Logging Driver: %s\n", remoteInfo.Get("LoggingDriver")) fmt.Fprintf(cli.out, "CPUs: %d\n", info.NCPU)
} fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(info.MemTotal)))
if remoteInfo.Exists("KernelVersion") { fmt.Fprintf(cli.out, "Name: %s\n", info.Name)
fmt.Fprintf(cli.out, "Kernel Version: %s\n", remoteInfo.Get("KernelVersion")) fmt.Fprintf(cli.out, "ID: %s\n", info.ID)
}
if remoteInfo.Exists("OperatingSystem") { if info.Debug || os.Getenv("DEBUG") != "" {
fmt.Fprintf(cli.out, "Operating System: %s\n", remoteInfo.Get("OperatingSystem")) fmt.Fprintf(cli.out, "Debug mode (server): %v\n", info.Debug)
} fmt.Fprintf(cli.out, "Debug mode (client): %v\n", os.Getenv("DEBUG") != "")
if remoteInfo.Exists("NCPU") { fmt.Fprintf(cli.out, "File Descriptors: %d\n", info.NFd)
fmt.Fprintf(cli.out, "CPUs: %d\n", remoteInfo.GetInt("NCPU")) fmt.Fprintf(cli.out, "Goroutines: %d\n", info.NGoroutines)
} fmt.Fprintf(cli.out, "System Time: %s\n", info.SystemTime)
if remoteInfo.Exists("MemTotal") { fmt.Fprintf(cli.out, "EventsListeners: %d\n", info.NEventsListener)
fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(remoteInfo.GetInt64("MemTotal")))) fmt.Fprintf(cli.out, "Init SHA1: %s\n", info.InitSha1)
} fmt.Fprintf(cli.out, "Init Path: %s\n", info.InitPath)
if remoteInfo.Exists("Name") { fmt.Fprintf(cli.out, "Docker Root Dir: %s\n", info.DockerRootDir)
fmt.Fprintf(cli.out, "Name: %s\n", remoteInfo.Get("Name"))
}
if remoteInfo.Exists("ID") {
fmt.Fprintf(cli.out, "ID: %s\n", remoteInfo.Get("ID"))
} }
if remoteInfo.GetBool("Debug") || os.Getenv("DEBUG") != "" { if info.HttpProxy != "" {
if remoteInfo.Exists("Debug") { fmt.Fprintf(cli.out, "Http Proxy: %s\n", info.HttpProxy)
fmt.Fprintf(cli.out, "Debug mode (server): %v\n", remoteInfo.GetBool("Debug"))
} }
fmt.Fprintf(cli.out, "Debug mode (client): %v\n", os.Getenv("DEBUG") != "") if info.HttpsProxy != "" {
if remoteInfo.Exists("NFd") { fmt.Fprintf(cli.out, "Https Proxy: %s\n", info.HttpsProxy)
fmt.Fprintf(cli.out, "File Descriptors: %d\n", remoteInfo.GetInt("NFd"))
} }
if remoteInfo.Exists("NGoroutines") { if info.NoProxy != "" {
fmt.Fprintf(cli.out, "Goroutines: %d\n", remoteInfo.GetInt("NGoroutines")) fmt.Fprintf(cli.out, "No Proxy: %s\n", info.NoProxy)
} }
if remoteInfo.Exists("SystemTime") {
t, err := remoteInfo.GetTime("SystemTime") if info.IndexServerAddress != "" {
if err != nil {
logrus.Errorf("Error reading system time: %v", err)
} else {
fmt.Fprintf(cli.out, "System Time: %s\n", t.Format(time.UnixDate))
}
}
if remoteInfo.Exists("NEventsListener") {
fmt.Fprintf(cli.out, "EventsListeners: %d\n", remoteInfo.GetInt("NEventsListener"))
}
if initSha1 := remoteInfo.Get("InitSha1"); initSha1 != "" {
fmt.Fprintf(cli.out, "Init SHA1: %s\n", initSha1)
}
if initPath := remoteInfo.Get("InitPath"); initPath != "" {
fmt.Fprintf(cli.out, "Init Path: %s\n", initPath)
}
if root := remoteInfo.Get("DockerRootDir"); root != "" {
fmt.Fprintf(cli.out, "Docker Root Dir: %s\n", root)
}
}
if remoteInfo.Exists("HttpProxy") {
fmt.Fprintf(cli.out, "Http Proxy: %s\n", remoteInfo.Get("HttpProxy"))
}
if remoteInfo.Exists("HttpsProxy") {
fmt.Fprintf(cli.out, "Https Proxy: %s\n", remoteInfo.Get("HttpsProxy"))
}
if remoteInfo.Exists("NoProxy") {
fmt.Fprintf(cli.out, "No Proxy: %s\n", remoteInfo.Get("NoProxy"))
}
if len(remoteInfo.GetList("IndexServerAddress")) != 0 {
cli.LoadConfigFile() cli.LoadConfigFile()
u := cli.configFile.Configs[remoteInfo.Get("IndexServerAddress")].Username u := cli.configFile.Configs[info.IndexServerAddress].Username
if len(u) > 0 { if len(u) > 0 {
fmt.Fprintf(cli.out, "Username: %v\n", u) fmt.Fprintf(cli.out, "Username: %v\n", u)
fmt.Fprintf(cli.out, "Registry: %v\n", remoteInfo.GetList("IndexServerAddress")) fmt.Fprintf(cli.out, "Registry: %v\n", info.IndexServerAddress)
} }
} }
if remoteInfo.Exists("MemoryLimit") && !remoteInfo.GetBool("MemoryLimit") { if !info.MemoryLimit {
fmt.Fprintf(cli.err, "WARNING: No memory limit support\n") fmt.Fprintf(cli.err, "WARNING: No memory limit support\n")
} }
if remoteInfo.Exists("SwapLimit") && !remoteInfo.GetBool("SwapLimit") { if !info.SwapLimit {
fmt.Fprintf(cli.err, "WARNING: No swap limit support\n") fmt.Fprintf(cli.err, "WARNING: No swap limit support\n")
} }
if remoteInfo.Exists("IPv4Forwarding") && !remoteInfo.GetBool("IPv4Forwarding") { if !info.IPv4Forwarding {
fmt.Fprintf(cli.err, "WARNING: IPv4 forwarding is disabled.\n") fmt.Fprintf(cli.err, "WARNING: IPv4 forwarding is disabled.\n")
} }
if remoteInfo.Exists("Labels") { if info.Labels != nil {
fmt.Fprintln(cli.out, "Labels:") fmt.Fprintln(cli.out, "Labels:")
for _, attribute := range remoteInfo.GetList("Labels") { for _, attribute := range info.Labels {
fmt.Fprintf(cli.out, " %s\n", attribute) fmt.Fprintf(cli.out, " %s\n", attribute)
} }
} }

View file

@ -367,8 +367,13 @@ func getImagesViz(eng *engine.Engine, version version.Version, w http.ResponseWr
func getInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
eng.ServeHTTP(w, r)
return nil info, err := getDaemon(eng).SystemInfo()
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, info)
} }
func getEvents(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getEvents(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -34,33 +34,6 @@ func TesthttpError(t *testing.T) {
} }
} }
func TestGetInfo(t *testing.T) {
eng := engine.New()
var called bool
eng.Register("info", func(job *engine.Job) error {
called = true
v := &engine.Env{}
v.SetInt("Containers", 1)
v.SetInt("Images", 42000)
if _, err := v.WriteTo(job.Stdout); err != nil {
return err
}
return nil
})
r := serveRequest("GET", "/info", nil, eng, t)
if !called {
t.Fatalf("handler was not called")
}
v := readEnv(r.Body, t)
if v.GetInt("Images") != 42000 {
t.Fatalf("%#v\n", v)
}
if v.GetInt("Containers") != 1 {
t.Fatalf("%#v\n", v)
}
assertContentType(r, "application/json", t)
}
func TestGetContainersByName(t *testing.T) { func TestGetContainersByName(t *testing.T) {
eng := engine.New() eng := engine.New()
name := "container_name" name := "container_name"

View file

@ -122,3 +122,36 @@ type Version struct {
Arch string Arch string
KernelVersion string `json:",omitempty"` KernelVersion string `json:",omitempty"`
} }
// GET "/info"
type Info struct {
ID string
Containers int
Images int
Driver string
DriverStatus [][2]string
MemoryLimit bool
SwapLimit bool
IPv4Forwarding bool
Debug bool
NFd int
NGoroutines int
SystemTime string
ExecutionDriver string
LoggingDriver string
NEventsListener int
KernelVersion string
OperatingSystem string
IndexServerAddress string
RegistryConfig interface{}
InitSha1 string
InitPath string
NCPU int
MemTotal int64
DockerRootDir string
HttpProxy string
HttpsProxy string
NoProxy string
Name string
Labels []string
}

View file

@ -119,7 +119,6 @@ type Daemon struct {
func (daemon *Daemon) Install(eng *engine.Engine) error { func (daemon *Daemon) Install(eng *engine.Engine) error {
for name, method := range map[string]engine.Handler{ for name, method := range map[string]engine.Handler{
"container_inspect": daemon.ContainerInspect, "container_inspect": daemon.ContainerInspect,
"info": daemon.CmdInfo,
"execCreate": daemon.ContainerExecCreate, "execCreate": daemon.ContainerExecCreate,
"execStart": daemon.ContainerExecStart, "execStart": daemon.ContainerExecStart,
} { } {

View file

@ -6,8 +6,8 @@ import (
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/autogen/dockerversion" "github.com/docker/docker/autogen/dockerversion"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/parsers/kernel" "github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/parsers/operatingsystem" "github.com/docker/docker/pkg/parsers/operatingsystem"
@ -16,7 +16,7 @@ import (
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
func (daemon *Daemon) CmdInfo(job *engine.Job) error { func (daemon *Daemon) SystemInfo() (*types.Info, error) {
images, _ := daemon.Graph().Map() images, _ := daemon.Graph().Map()
var imgcount int var imgcount int
if images == nil { if images == nil {
@ -52,47 +52,46 @@ func (daemon *Daemon) CmdInfo(job *engine.Job) error {
initPath = daemon.SystemInitPath() initPath = daemon.SystemInitPath()
} }
v := &engine.Env{} v := &types.Info{
v.SetJson("ID", daemon.ID) ID: daemon.ID,
v.SetInt("Containers", len(daemon.List())) Containers: len(daemon.List()),
v.SetInt("Images", imgcount) Images: imgcount,
v.Set("Driver", daemon.GraphDriver().String()) Driver: daemon.GraphDriver().String(),
v.SetJson("DriverStatus", daemon.GraphDriver().Status()) DriverStatus: daemon.GraphDriver().Status(),
v.SetBool("MemoryLimit", daemon.SystemConfig().MemoryLimit) MemoryLimit: daemon.SystemConfig().MemoryLimit,
v.SetBool("SwapLimit", daemon.SystemConfig().SwapLimit) SwapLimit: daemon.SystemConfig().SwapLimit,
v.SetBool("IPv4Forwarding", !daemon.SystemConfig().IPv4ForwardingDisabled) IPv4Forwarding: !daemon.SystemConfig().IPv4ForwardingDisabled,
v.SetBool("Debug", os.Getenv("DEBUG") != "") Debug: os.Getenv("DEBUG") != "",
v.SetInt("NFd", fileutils.GetTotalUsedFds()) NFd: fileutils.GetTotalUsedFds(),
v.SetInt("NGoroutines", runtime.NumGoroutine()) NGoroutines: runtime.NumGoroutine(),
v.Set("SystemTime", time.Now().Format(time.RFC3339Nano)) SystemTime: time.Now().Format(time.RFC3339Nano),
v.Set("ExecutionDriver", daemon.ExecutionDriver().Name()) ExecutionDriver: daemon.ExecutionDriver().Name(),
v.Set("LoggingDriver", daemon.defaultLogConfig.Type) LoggingDriver: daemon.defaultLogConfig.Type,
v.SetInt("NEventsListener", daemon.EventsService.SubscribersCount()) NEventsListener: daemon.EventsService.SubscribersCount(),
v.Set("KernelVersion", kernelVersion) KernelVersion: kernelVersion,
v.Set("OperatingSystem", operatingSystem) OperatingSystem: operatingSystem,
v.Set("IndexServerAddress", registry.IndexServerAddress()) IndexServerAddress: registry.IndexServerAddress(),
v.SetJson("RegistryConfig", daemon.RegistryService.Config) RegistryConfig: daemon.RegistryService.Config,
v.Set("InitSha1", dockerversion.INITSHA1) InitSha1: dockerversion.INITSHA1,
v.Set("InitPath", initPath) InitPath: initPath,
v.SetInt("NCPU", runtime.NumCPU()) NCPU: runtime.NumCPU(),
v.SetInt64("MemTotal", meminfo.MemTotal) MemTotal: meminfo.MemTotal,
v.Set("DockerRootDir", daemon.Config().Root) DockerRootDir: daemon.Config().Root,
if httpProxy := os.Getenv("http_proxy"); httpProxy != "" { Labels: daemon.Config().Labels,
v.Set("HttpProxy", httpProxy)
}
if httpsProxy := os.Getenv("https_proxy"); httpsProxy != "" {
v.Set("HttpsProxy", httpsProxy)
}
if noProxy := os.Getenv("no_proxy"); noProxy != "" {
v.Set("NoProxy", noProxy)
} }
if httpProxy := os.Getenv("http_proxy"); httpProxy != "" {
v.HttpProxy = httpProxy
}
if httpsProxy := os.Getenv("https_proxy"); httpsProxy != "" {
v.HttpsProxy = httpsProxy
}
if noProxy := os.Getenv("no_proxy"); noProxy != "" {
v.NoProxy = noProxy
}
if hostname, err := os.Hostname(); err == nil { if hostname, err := os.Hostname(); err == nil {
v.SetJson("Name", hostname) v.Name = hostname
} }
v.SetList("Labels", daemon.Config().Labels)
if _, err := v.WriteTo(job.Stdout); err != nil { return v, nil
return err
}
return nil
} }

View file

@ -0,0 +1,38 @@
package main
import (
"net/http"
"strings"
"testing"
)
func TestInfoApi(t *testing.T) {
endpoint := "/info"
statusCode, body, err := sockRequest("GET", endpoint, nil)
if err != nil || statusCode != http.StatusOK {
t.Fatalf("Expected %d from info request, got %d", http.StatusOK, statusCode)
}
// always shown fields
stringsToCheck := []string{
"ID",
"Containers",
"Images",
"ExecutionDriver",
"LoggingDriver",
"OperatingSystem",
"NCPU",
"MemTotal",
"KernelVersion",
"Driver"}
out := string(body)
for _, linePrefix := range stringsToCheck {
if !strings.Contains(out, linePrefix) {
t.Errorf("couldn't find string %v in output", linePrefix)
}
}
logDone("container REST API - check GET /info")
}