Skip to content

Commit

Permalink
Add platform and its components to docker version output
Browse files Browse the repository at this point in the history
The Server section of version output is now composed of an Engine
component and potentially more, based on what the /version endpoint
returns.

Signed-off-by: Tibor Vass <[email protected]>
  • Loading branch information
Tibor Vass committed Dec 7, 2017
1 parent fe3941a commit 5f4c5f8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 38 deletions.
106 changes: 75 additions & 31 deletions cli/command/system/version.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package system

import (
"fmt"
"runtime"
"time"

"golang.org/x/net/context"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

var versionTemplate = `Client:
Version: {{.Client.Version}}
API version: {{.Client.APIVersion}}{{if ne .Client.APIVersion .Client.DefaultAPIVersion}} (downgraded from {{.Client.DefaultAPIVersion}}){{end}}
Go version: {{.Client.GoVersion}}
Git commit: {{.Client.GitCommit}}
Built: {{.Client.BuildTime}}
OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .ServerOK}}
Server:
Version: {{.Server.Version}}
API version: {{.Server.APIVersion}} (minimum version {{.Server.MinAPIVersion}})
Go version: {{.Server.GoVersion}}
Git commit: {{.Server.GitCommit}}
Built: {{.Server.BuildTime}}
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Experimental: {{.Server.Experimental}}{{end}}`
var versionTemplate = `{{with .Client -}}
Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
Version: {{.Version}}
API version: {{.APIVersion}}{{if ne .APIVersion .DefaultAPIVersion}} (downgraded from {{.DefaultAPIVersion}}){{end}}
Go version: {{.GoVersion}}
Git commit: {{.GitCommit}}
Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}}
{{- end}}
{{- if .ServerOK}}{{with .Server}}
Server:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
{{- range $component := .Components}}
{{$component.Name}}:
{{- if eq $component.Name "Engine" }}
Version: {{.Version}}
API version: {{index .Details "ApiVersion"}} (minimum version {{index .Details "MinAPIVersion"}})
Go version: {{index .Details "GoVersion"}}
Git commit: {{index .Details "GitCommit"}}
Built: {{index .Details "BuildTime"}}
OS/Arch: {{index .Details "Os"}}/{{index .Details "Arch"}}
Experimental: {{index .Details "Experimental"}}
{{- else -}}
Version: {{$component.Version}}
{{- range $k, $v := $component.Details}}
{{$k}}: {{$v}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}{{end}}`

type versionOptions struct {
format string
Expand All @@ -41,6 +56,8 @@ type versionInfo struct {
}

type clientVersion struct {
Platform struct{ Name string } `json:",omitempty"`

Version string
APIVersion string `json:"ApiVersion"`
DefaultAPIVersion string `json:"DefaultAPIVersion,omitempty"`
Expand Down Expand Up @@ -77,6 +94,14 @@ func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command {
return cmd
}

func reformatDate(buildTime string) string {
t, errTime := time.Parse(time.RFC3339Nano, buildTime)
if errTime == nil {
return t.Format(time.ANSIC)
}
return buildTime
}

func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
ctx := context.Background()

Expand All @@ -103,22 +128,41 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
Arch: runtime.GOARCH,
},
}

serverVersion, err := dockerCli.Client().ServerVersion(ctx)
if err == nil {
vd.Server = &serverVersion
}
vd.Client.Platform.Name = cli.PlatformName

// first we need to make BuildTime more human friendly
t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime)
if errTime == nil {
vd.Client.BuildTime = t.Format(time.ANSIC)
}
vd.Client.BuildTime = reformatDate(vd.Client.BuildTime)

sv, err := dockerCli.Client().ServerVersion(ctx)
if err == nil {
vd.Server = &sv
foundEngine := false
for _, component := range sv.Components {
if component.Name == "Engine" {
foundEngine = true
buildTime, ok := component.Details["BuildTime"]
if ok {
component.Details["BuildTime"] = reformatDate(buildTime)
}
break
}
}

if vd.ServerOK() {
t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
if errTime == nil {
vd.Server.BuildTime = t.Format(time.ANSIC)
if !foundEngine {
vd.Server.Components = append(vd.Server.Components, types.ComponentVersion{
Name: "Engine",
Version: sv.Version,
Details: map[string]string{
"ApiVersion": sv.APIVersion,
"MinAPIVersion": sv.MinAPIVersion,
"GitCommit": sv.GitCommit,
"GoVersion": sv.GoVersion,
"Os": sv.Os,
"Arch": sv.Arch,
"BuildTime": reformatDate(vd.Server.BuildTime),
"Experimental": fmt.Sprintf("%t", sv.Experimental),
},
})
}
}

Expand Down
7 changes: 4 additions & 3 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package cli
// Default build-time variable.
// These values are overriding via ldflags
var (
Version = "unknown-version"
GitCommit = "unknown-commit"
BuildTime = "unknown-buildtime"
PlatformName = ""
Version = "unknown-version"
GitCommit = "unknown-commit"
BuildTime = "unknown-buildtime"
)
2 changes: 1 addition & 1 deletion docker.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CROSS_IMAGE_NAME = docker-cli-cross$(IMAGE_TAG)
VALIDATE_IMAGE_NAME = docker-cli-shell-validate$(IMAGE_TAG)
MOUNTS = -v "$(CURDIR)":/go/src/github.com/docker/cli
VERSION = $(shell cat VERSION)
ENVVARS = -e VERSION=$(VERSION) -e GITCOMMIT
ENVVARS = -e VERSION=$(VERSION) -e GITCOMMIT -e PLATFORM

# build docker image (dockerfiles/Dockerfile.build)
.PHONY: build_docker_image
Expand Down
13 changes: 10 additions & 3 deletions scripts/build/.variables
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#!/usr/bin/env bash
set -eu

PLATFORM=${PLATFORM:-}
VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
BUILDTIME=${BUILDTIME:-$(date --utc --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')}

PLATFORM_LDFLAGS=
if test -n "${PLATFORM}"; then
PLATFORM_LDFLAGS="-X \"github.com/docker/cli/cli.PlatformName=${PLATFORM}\""
fi

export LDFLAGS="\
-w \
-X github.com/docker/cli/cli.GitCommit=${GITCOMMIT} \
-X github.com/docker/cli/cli.BuildTime=${BUILDTIME} \
-X github.com/docker/cli/cli.Version=${VERSION} \
${PLATFORM_LDFLAGS} \
-X \"github.com/docker/cli/cli.GitCommit=${GITCOMMIT}\" \
-X \"github.com/docker/cli/cli.BuildTime=${BUILDTIME}\" \
-X \"github.com/docker/cli/cli.Version=${VERSION}\" \
${LDFLAGS:-} \
"

Expand Down

0 comments on commit 5f4c5f8

Please sign in to comment.