From 3d7678389f512a95fa205b7a972e6490a6939c00 Mon Sep 17 00:00:00 2001 From: Nishant Totla Date: Thu, 28 Jan 2016 10:39:14 -0800 Subject: [PATCH] Removing backspaces in /info output for new API version Signed-off-by: Nishant Totla --- api/handlers.go | 17 ++++++++++++++++- api/primary.go | 1 + api/primary_test.go | 3 ++- cli/manage.go | 6 +++--- cluster/mesos/cluster.go | 10 +++++----- cluster/swarm/cluster.go | 22 +++++++++++----------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index d75c6d3e6b..2ea153916c 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -15,6 +15,7 @@ import ( "time" "github.com/docker/docker/pkg/parsers/kernel" + versionpkg "github.com/docker/docker/pkg/version" apitypes "github.com/docker/engine-api/types" dockerfilters "github.com/docker/engine-api/types/filters" "github.com/docker/swarm/cluster" @@ -31,7 +32,6 @@ const APIVERSION = "1.21" func getInfo(c *context, w http.ResponseWriter, r *http.Request) { info := apitypes.Info{ Images: len(c.cluster.Images().Filter(cluster.ImageFilterOptions{})), - DriverStatus: c.statusHandler.Status(), NEventsListener: c.eventsHandler.Size(), Debug: c.debug, MemoryLimit: true, @@ -55,6 +55,21 @@ func getInfo(c *context, w http.ResponseWriter, r *http.Request) { ExperimentalBuild: experimental.ENABLED, } + // API versions older than 1.22 use DriverStatus and return \b characters in the output + status := c.statusHandler.Status() + if c.apiVersion != "" && versionpkg.Version(c.apiVersion).LessThan("1.22") { + for i := range status { + if status[i][0][:1] == " " { + status[i][0] = status[i][0][1:] + } else { + status[i][0] = "\b" + status[i][0] + } + } + info.DriverStatus = status + } else { + info.SystemStatus = status + } + if kernelVersion, err := kernel.GetKernelVersion(); err == nil { info.KernelVersion = kernelVersion.String() } diff --git a/api/primary.go b/api/primary.go index d1c62dd06f..b5d30c4c8f 100644 --- a/api/primary.go +++ b/api/primary.go @@ -168,6 +168,7 @@ func setupPrimaryRouter(r *mux.Router, context *context, enableCors bool) { if enableCors { writeCorsHeaders(w, r) } + context.apiVersion = mux.Vars(r)["version"] localFct(context, w, r) } diff --git a/api/primary_test.go b/api/primary_test.go index 865f44e9d0..fa846b8752 100644 --- a/api/primary_test.go +++ b/api/primary_test.go @@ -37,8 +37,9 @@ func TestRequest(t *testing.T) { func TestCorsRequest(t *testing.T) { t.Parallel() + context := &context{} primary := mux.NewRouter() - setupPrimaryRouter(primary, nil, true) + setupPrimaryRouter(primary, context, true) w := httptest.NewRecorder() r, e := http.NewRequest("OPTIONS", "/version", nil) diff --git a/cli/manage.go b/cli/manage.go index 5647315576..52a74f12c7 100644 --- a/cli/manage.go +++ b/cli/manage.go @@ -54,12 +54,12 @@ func (h *statusHandler) Status() [][2]string { if h.candidate != nil && !h.candidate.IsLeader() { status = [][2]string{ - {"\bRole", "replica"}, - {"\bPrimary", h.follower.Leader()}, + {"Role", "replica"}, + {"Primary", h.follower.Leader()}, } } else { status = [][2]string{ - {"\bRole", "primary"}, + {"Role", "primary"}, } } diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index d268117d93..cf9fd4958a 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -399,17 +399,17 @@ func (c *Cluster) TotalCpus() int64 { func (c *Cluster) Info() [][2]string { offers := c.listOffers() info := [][2]string{ - {"\bStrategy", c.scheduler.Strategy()}, - {"\bFilters", c.scheduler.Filters()}, - {"\bOffers", fmt.Sprintf("%d", len(offers))}, + {"Strategy", c.scheduler.Strategy()}, + {"Filters", c.scheduler.Filters()}, + {"Offers", fmt.Sprintf("%d", len(offers))}, } sort.Sort(offerSorter(offers)) for _, offer := range offers { - info = append(info, [2]string{" Offer", offer.Id.GetValue()}) + info = append(info, [2]string{" Offer", offer.Id.GetValue()}) for _, resource := range offer.Resources { - info = append(info, [2]string{" └ " + resource.GetName(), formatResource(resource)}) + info = append(info, [2]string{" └ " + resource.GetName(), formatResource(resource)}) } } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 9fa08b71f2..8f78d0d287 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -823,32 +823,32 @@ func (c *Cluster) TotalCpus() int64 { // Info returns some info about the cluster, like nb or containers / images func (c *Cluster) Info() [][2]string { info := [][2]string{ - {"\bStrategy", c.scheduler.Strategy()}, - {"\bFilters", c.scheduler.Filters()}, - {"\bNodes", fmt.Sprintf("%d", len(c.engines)+len(c.pendingEngines))}, + {"Strategy", c.scheduler.Strategy()}, + {"Filters", c.scheduler.Filters()}, + {"Nodes", fmt.Sprintf("%d", len(c.engines)+len(c.pendingEngines))}, } engines := c.listEngines() sort.Sort(cluster.EngineSorter(engines)) for _, engine := range engines { - info = append(info, [2]string{engine.Name, engine.Addr}) - info = append(info, [2]string{" └ Status", engine.Status()}) - info = append(info, [2]string{" └ Containers", fmt.Sprintf("%d", len(engine.Containers()))}) - info = append(info, [2]string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", engine.UsedCpus(), engine.TotalCpus())}) - info = append(info, [2]string{" └ Reserved Memory", fmt.Sprintf("%s / %s", units.BytesSize(float64(engine.UsedMemory())), units.BytesSize(float64(engine.TotalMemory())))}) + info = append(info, [2]string{" " + engine.Name, engine.Addr}) + info = append(info, [2]string{" └ Status", engine.Status()}) + info = append(info, [2]string{" └ Containers", fmt.Sprintf("%d", len(engine.Containers()))}) + info = append(info, [2]string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", engine.UsedCpus(), engine.TotalCpus())}) + info = append(info, [2]string{" └ Reserved Memory", fmt.Sprintf("%s / %s", units.BytesSize(float64(engine.UsedMemory())), units.BytesSize(float64(engine.TotalMemory())))}) labels := make([]string, 0, len(engine.Labels)) for k, v := range engine.Labels { labels = append(labels, k+"="+v) } sort.Strings(labels) - info = append(info, [2]string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) + info = append(info, [2]string{" └ Labels", fmt.Sprintf("%s", strings.Join(labels, ", "))}) errMsg := engine.ErrMsg() if len(errMsg) == 0 { errMsg = "(none)" } - info = append(info, [2]string{" └ Error", errMsg}) - info = append(info, [2]string{" └ UpdatedAt", engine.UpdatedAt().UTC().Format(time.RFC3339)}) + info = append(info, [2]string{" └ Error", errMsg}) + info = append(info, [2]string{" └ UpdatedAt", engine.UpdatedAt().UTC().Format(time.RFC3339)}) } return info