Add platform and its components to docker version output

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 <tibor@docker.com>
This commit is contained in:
Tibor Vass 2017-12-01 14:47:20 +01:00
parent fe3941af7d
commit 5f4c5f8bb6
4 changed files with 89 additions and 37 deletions

View File

@ -1,34 +1,49 @@
package system package system
import ( import (
"fmt"
"runtime" "runtime"
"time" "time"
"golang.org/x/net/context"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/templates" "github.com/docker/cli/templates"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context"
) )
var versionTemplate = `Client: var versionTemplate = `{{with .Client -}}
Version: {{.Client.Version}} Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
API version: {{.Client.APIVersion}}{{if ne .Client.APIVersion .Client.DefaultAPIVersion}} (downgraded from {{.Client.DefaultAPIVersion}}){{end}} Version: {{.Version}}
Go version: {{.Client.GoVersion}} API version: {{.APIVersion}}{{if ne .APIVersion .DefaultAPIVersion}} (downgraded from {{.DefaultAPIVersion}}){{end}}
Git commit: {{.Client.GitCommit}} Go version: {{.GoVersion}}
Built: {{.Client.BuildTime}} Git commit: {{.GitCommit}}
OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .ServerOK}} Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}}
{{- end}}
Server: {{- if .ServerOK}}{{with .Server}}
Version: {{.Server.Version}}
API version: {{.Server.APIVersion}} (minimum version {{.Server.MinAPIVersion}}) Server:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
Go version: {{.Server.GoVersion}} {{- range $component := .Components}}
Git commit: {{.Server.GitCommit}} {{$component.Name}}:
Built: {{.Server.BuildTime}} {{- if eq $component.Name "Engine" }}
OS/Arch: {{.Server.Os}}/{{.Server.Arch}} Version: {{.Version}}
Experimental: {{.Server.Experimental}}{{end}}` 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 { type versionOptions struct {
format string format string
@ -41,6 +56,8 @@ type versionInfo struct {
} }
type clientVersion struct { type clientVersion struct {
Platform struct{ Name string } `json:",omitempty"`
Version string Version string
APIVersion string `json:"ApiVersion"` APIVersion string `json:"ApiVersion"`
DefaultAPIVersion string `json:"DefaultAPIVersion,omitempty"` DefaultAPIVersion string `json:"DefaultAPIVersion,omitempty"`
@ -77,6 +94,14 @@ func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command {
return cmd 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 { func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
ctx := context.Background() ctx := context.Background()
@ -103,22 +128,41 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
Arch: runtime.GOARCH, Arch: runtime.GOARCH,
}, },
} }
vd.Client.Platform.Name = cli.PlatformName
serverVersion, err := dockerCli.Client().ServerVersion(ctx)
if err == nil {
vd.Server = &serverVersion
}
// first we need to make BuildTime more human friendly // first we need to make BuildTime more human friendly
t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime) vd.Client.BuildTime = reformatDate(vd.Client.BuildTime)
if errTime == nil {
vd.Client.BuildTime = t.Format(time.ANSIC) 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() { if !foundEngine {
t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime) vd.Server.Components = append(vd.Server.Components, types.ComponentVersion{
if errTime == nil { Name: "Engine",
vd.Server.BuildTime = t.Format(time.ANSIC) 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),
},
})
} }
} }

View File

@ -3,6 +3,7 @@ package cli
// Default build-time variable. // Default build-time variable.
// These values are overriding via ldflags // These values are overriding via ldflags
var ( var (
PlatformName = ""
Version = "unknown-version" Version = "unknown-version"
GitCommit = "unknown-commit" GitCommit = "unknown-commit"
BuildTime = "unknown-buildtime" BuildTime = "unknown-buildtime"

View File

@ -10,7 +10,7 @@ CROSS_IMAGE_NAME = docker-cli-cross$(IMAGE_TAG)
VALIDATE_IMAGE_NAME = docker-cli-shell-validate$(IMAGE_TAG) VALIDATE_IMAGE_NAME = docker-cli-shell-validate$(IMAGE_TAG)
MOUNTS = -v "$(CURDIR)":/go/src/github.com/docker/cli MOUNTS = -v "$(CURDIR)":/go/src/github.com/docker/cli
VERSION = $(shell cat VERSION) VERSION = $(shell cat VERSION)
ENVVARS = -e VERSION=$(VERSION) -e GITCOMMIT ENVVARS = -e VERSION=$(VERSION) -e GITCOMMIT -e PLATFORM
# build docker image (dockerfiles/Dockerfile.build) # build docker image (dockerfiles/Dockerfile.build)
.PHONY: build_docker_image .PHONY: build_docker_image

View File

@ -1,15 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eu set -eu
PLATFORM=${PLATFORM:-}
VERSION=${VERSION:-"unknown-version"} VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)} 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/')} 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="\ export LDFLAGS="\
-w \ -w \
-X github.com/docker/cli/cli.GitCommit=${GITCOMMIT} \ ${PLATFORM_LDFLAGS} \
-X github.com/docker/cli/cli.BuildTime=${BUILDTIME} \ -X \"github.com/docker/cli/cli.GitCommit=${GITCOMMIT}\" \
-X github.com/docker/cli/cli.Version=${VERSION} \ -X \"github.com/docker/cli/cli.BuildTime=${BUILDTIME}\" \
-X \"github.com/docker/cli/cli.Version=${VERSION}\" \
${LDFLAGS:-} \ ${LDFLAGS:-} \
" "