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

feat(CLI): Add '--detail' for detail version information (#283) #471

Merged
merged 3 commits into from
Jun 20, 2022
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
22 changes: 22 additions & 0 deletions pkg/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ var CommandVersion = &cli.Command{
Value: false,
Aliases: []string{"s"},
},
&cli.BoolFlag{
Name: "detail",
Usage: "Print details about the envd environment",
Value: false,
Aliases: []string{"d"},
},
},
}

func printVersion(ctx *cli.Context) error {
short := ctx.Bool("short")
detail := ctx.Bool("detail")
ver := version.GetVersion()
detailVer, err := version.GetDetailedVersion(ctx)
fmt.Printf("envd: %s\n", ver)
if short {
return nil
Expand All @@ -52,5 +60,19 @@ func printVersion(ctx *cli.Context) error {
fmt.Printf(" GoVersion: %s\n", ver.GoVersion)
fmt.Printf(" Compiler: %s\n", ver.Compiler)
fmt.Printf(" Platform: %s\n", ver.Platform)
if detail {
if err != nil {
fmt.Printf("Error in getting details from Docker Server: %s\n", err)
} else {
fmt.Printf(" OSType: %s\n", detailVer.OSType)
if detailVer.OSVersion != "" {
fmt.Printf(" OSVersion: %s\n", detailVer.OSVersion)
}
fmt.Printf(" KernelVersion: %s\n", detailVer.KernelVersion)
fmt.Printf(" DockerHostVersion: %s\n", detailVer.DockerVersion)
fmt.Printf(" ContainerRuntimes: %s\n", detailVer.ContainerRuntimes)
fmt.Printf(" DefaultRuntime: %s\n", detailVer.DefaultRuntime)
}
}
return nil
}
6 changes: 6 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type Client interface {
ListImage(ctx context.Context) ([]types.ImageSummary, error)
GetImage(ctx context.Context, image string) (types.ImageSummary, error)

GetInfo(ctx context.Context) (types.Info, error)

// GPUEnabled returns true if nvidia container runtime exists in docker daemon.
GPUEnabled(ctx context.Context) (bool, error)
}
Expand Down Expand Up @@ -215,6 +217,10 @@ func (c generalClient) GetContainer(ctx context.Context, cname string) (types.Co
return c.ContainerInspect(ctx, cname)
}

func (c generalClient) GetInfo(ctx context.Context) (types.Info, error) {
return c.Info(ctx)
}

func (c generalClient) Destroy(ctx context.Context, name string) (string, error) {
logger := logrus.WithField("container", name)
// Refer to https://docs.docker.com/engine/reference/commandline/container_kill/
Expand Down
12 changes: 12 additions & 0 deletions pkg/envd/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Engine interface {
ResumeEnvironment(ctx context.Context, env string) (string, error)
ListEnvironment(ctx context.Context) ([]types.EnvdEnvironment, error)
ListEnvDependency(ctx context.Context, env string) (*types.Dependency, error)
GetInfo(ctx context.Context) (*types.EnvdInfo, error)
}

type generalEngine struct {
Expand Down Expand Up @@ -139,3 +140,14 @@ func (e generalEngine) ListEnvDependency(
}
return dep, nil
}

func (e generalEngine) GetInfo(ctx context.Context) (*types.EnvdInfo, error) {
info, err := e.dockerCli.GetInfo(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get docker client info")
}
return &types.EnvdInfo{
Info: info,
}, nil

}
4 changes: 4 additions & 0 deletions pkg/types/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type EnvdManifest struct {
Dependency `json:",inline,omitempty"`
}

type EnvdInfo struct {
types.Info
}

type Dependency struct {
APTPackages []string `json:"apt_packages,omitempty"`
PyPIPackages []string `json:"pypi_packages,omitempty"`
Expand Down
61 changes: 58 additions & 3 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"runtime"
"strings"
"sync"

"github.com/cockroachdb/errors"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/types"
"github.com/urfave/cli/v2"
)

var (
Expand Down Expand Up @@ -53,12 +58,22 @@ type Version struct {
Platform string
}

type DetailedVersion struct {
OSVersion string
OSType string
KernelVersion string
Architecture string
DockerVersion string
ContainerRuntimes string
DefaultRuntime string
}

func (v Version) String() string {
return v.Version
}

// GetVersion returns the version information
func GetVersion() Version {
// Get Envd version information
func GetEnvdVersion() string {
var versionStr string

if gitCommit != "" && gitTag != "" && gitTreeState == "clean" {
Expand All @@ -82,8 +97,22 @@ func GetVersion() Version {
versionStr += "+unknown"
}
}
return versionStr
}

func GetRuntimes(info *types.EnvdInfo) string {
runtimesMap := info.Runtimes
keys := make([]string, 0, len(runtimesMap))
for k := range runtimesMap {
keys = append(keys, k)
}
return "[" + strings.Join(keys, ",") + "]"
}

// GetVersion returns the version information
func GetVersion() Version {
return Version{
Version: versionStr,
Version: GetEnvdVersion(),
BuildDate: buildDate,
GitCommit: gitCommit,
GitTag: gitTag,
Expand All @@ -94,6 +123,32 @@ func GetVersion() Version {
}
}

func GetDetailedVersion(clicontext *cli.Context) (DetailedVersion, error) {
engine, err := envd.New(clicontext.Context)
if err != nil {
return DetailedVersion{}, errors.Wrap(
err, "failed to create engine for docker server",
)
}

info, err := engine.GetInfo(clicontext.Context)
if err != nil {
return DetailedVersion{}, errors.Wrap(
err, "failed to get detailed version info from docker server",
)
}

return DetailedVersion{
OSVersion: info.OSVersion,
OSType: info.OSType,
KernelVersion: info.KernelVersion,
DockerVersion: info.ServerVersion,
Architecture: info.Architecture,
DefaultRuntime: info.DefaultRuntime,
ContainerRuntimes: GetRuntimes(info),
}, nil
}

var (
reRelease *regexp.Regexp
reDev *regexp.Regexp
Expand Down