2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-23 10:24:43 -04:00
|
|
|
"sort"
|
2016-09-08 13:11:39 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-02-08 17:15:32 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-06-23 10:24:43 -04:00
|
|
|
"github.com/docker/go-units"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-03 19:48:46 -05:00
|
|
|
defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
namesHeader = "NAMES"
|
|
|
|
commandHeader = "COMMAND"
|
|
|
|
runningForHeader = "CREATED"
|
|
|
|
mountsHeader = "MOUNTS"
|
|
|
|
localVolumes = "LOCAL VOLUMES"
|
|
|
|
networksHeader = "NETWORKS"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// NewContainerFormat returns a Format for rendering using a Context
|
|
|
|
func NewContainerFormat(source string, quiet bool, size bool) Format {
|
|
|
|
switch source {
|
|
|
|
case TableFormatKey:
|
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return DefaultQuietFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
format := defaultContainerTableFormat
|
|
|
|
if size {
|
|
|
|
format += `\t{{.Size}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `container_id: {{.ID}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-13 14:21:07 -04:00
|
|
|
format := `container_id: {{.ID}}
|
|
|
|
image: {{.Image}}
|
|
|
|
command: {{.Command}}
|
|
|
|
created_at: {{.CreatedAt}}
|
2019-07-12 07:10:08 -04:00
|
|
|
state: {{- pad .State 1 0}}
|
2016-09-13 14:21:07 -04:00
|
|
|
status: {{- pad .Status 1 0}}
|
|
|
|
names: {{.Names}}
|
|
|
|
labels: {{- pad .Labels 1 0}}
|
|
|
|
ports: {{- pad .Ports 1 0}}
|
|
|
|
`
|
2016-09-12 16:59:18 -04:00
|
|
|
if size {
|
|
|
|
format += `size: {{.Size}}\n`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(source)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// ContainerWrite renders the context for a list of containers
|
|
|
|
func ContainerWrite(ctx Context, containers []types.Container) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext SubContext) error) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
for _, container := range containers {
|
|
|
|
err := format(&containerContext{trunc: ctx.Trunc, c: container})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
return ctx.Write(newContainerContext(), render)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
type containerContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
HeaderContext
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc bool
|
|
|
|
c types.Container
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:48:46 -05:00
|
|
|
func newContainerContext() *containerContext {
|
|
|
|
containerCtx := containerContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
containerCtx.Header = SubHeaderContext{
|
|
|
|
"ID": ContainerIDHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Names": namesHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Image": ImageHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Command": commandHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"CreatedAt": CreatedAtHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"RunningFor": runningForHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Ports": PortsHeader,
|
2019-07-12 07:10:08 -04:00
|
|
|
"State": StateHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Status": StatusHeader,
|
|
|
|
"Size": SizeHeader,
|
|
|
|
"Labels": LabelsHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Mounts": mountsHeader,
|
|
|
|
"LocalVolumes": localVolumes,
|
|
|
|
"Networks": networksHeader,
|
|
|
|
}
|
|
|
|
return &containerCtx
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *containerContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return MarshalJSON(c)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func (c *containerContext) ID() string {
|
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.c.ID)
|
|
|
|
}
|
|
|
|
return c.c.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Names() string {
|
|
|
|
names := stripNamePrefix(c.c.Names)
|
|
|
|
if c.trunc {
|
|
|
|
for _, name := range names {
|
|
|
|
if len(strings.Split(name, "/")) == 1 {
|
|
|
|
names = []string{name}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Image() string {
|
|
|
|
if c.c.Image == "" {
|
|
|
|
return "<no image>"
|
|
|
|
}
|
|
|
|
if c.trunc {
|
|
|
|
if trunc := stringid.TruncateID(c.c.ImageID); trunc == stringid.TruncateID(c.c.Image) {
|
|
|
|
return trunc
|
|
|
|
}
|
2017-02-08 17:15:32 -05:00
|
|
|
// truncate digest if no-trunc option was not selected
|
|
|
|
ref, err := reference.ParseNormalizedNamed(c.c.Image)
|
|
|
|
if err == nil {
|
|
|
|
if nt, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
// case for when a tag is provided
|
|
|
|
if namedTagged, err := reference.WithTag(reference.TrimNamed(nt), nt.Tag()); err == nil {
|
|
|
|
return reference.FamiliarString(namedTagged)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// case for when a tag is not provided
|
|
|
|
named := reference.TrimNamed(ref)
|
|
|
|
return reference.FamiliarString(named)
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-02-08 17:15:32 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.c.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Command() string {
|
|
|
|
command := c.c.Command
|
|
|
|
if c.trunc {
|
2017-10-13 05:01:32 -04:00
|
|
|
command = Ellipsis(command, 20)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return strconv.Quote(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) CreatedAt() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
return time.Unix(c.c.Created, 0).String()
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) RunningFor() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
createdAt := time.Unix(c.c.Created, 0)
|
2017-02-03 19:48:46 -05:00
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Ports() string {
|
2017-06-23 10:24:43 -04:00
|
|
|
return DisplayablePorts(c.c.Ports)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-07-12 07:10:08 -04:00
|
|
|
func (c *containerContext) State() string {
|
|
|
|
return c.c.State
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func (c *containerContext) Status() string {
|
|
|
|
return c.c.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Size() string {
|
2016-09-04 17:44:34 -04:00
|
|
|
srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3)
|
|
|
|
sv := units.HumanSizeWithPrecision(float64(c.c.SizeRootFs), 3)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
sf := srw
|
|
|
|
if c.c.SizeRootFs > 0 {
|
|
|
|
sf = fmt.Sprintf("%s (virtual %s)", srw, sv)
|
|
|
|
}
|
|
|
|
return sf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Labels() string {
|
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var joinLabels []string
|
|
|
|
for k, v := range c.c.Labels {
|
|
|
|
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return strings.Join(joinLabels, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Label(name string) string {
|
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.c.Labels[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Mounts() string {
|
|
|
|
var name string
|
|
|
|
var mounts []string
|
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Name == "" {
|
|
|
|
name = m.Source
|
|
|
|
} else {
|
|
|
|
name = m.Name
|
|
|
|
}
|
|
|
|
if c.trunc {
|
2017-10-13 05:01:32 -04:00
|
|
|
name = Ellipsis(name, 15)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
mounts = append(mounts, name)
|
|
|
|
}
|
|
|
|
return strings.Join(mounts, ",")
|
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
func (c *containerContext) LocalVolumes() string {
|
|
|
|
count := 0
|
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Driver == "local" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d", count)
|
|
|
|
}
|
2016-10-19 18:09:42 -04:00
|
|
|
|
|
|
|
func (c *containerContext) Networks() string {
|
|
|
|
if c.c.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
networks := []string{}
|
|
|
|
for k := range c.c.NetworkSettings.Networks {
|
|
|
|
networks = append(networks, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(networks, ",")
|
|
|
|
}
|
2017-06-23 10:24:43 -04:00
|
|
|
|
|
|
|
// DisplayablePorts returns formatted string representing open ports of container
|
|
|
|
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
|
|
|
// it's used by command 'docker ps'
|
|
|
|
func DisplayablePorts(ports []types.Port) string {
|
|
|
|
type portGroup struct {
|
|
|
|
first uint16
|
|
|
|
last uint16
|
|
|
|
}
|
|
|
|
groupMap := make(map[string]*portGroup)
|
|
|
|
var result []string
|
|
|
|
var hostMappings []string
|
|
|
|
var groupMapKeys []string
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(ports, func(i, j int) bool {
|
|
|
|
return comparePorts(ports[i], ports[j])
|
|
|
|
})
|
|
|
|
|
2017-06-23 10:24:43 -04:00
|
|
|
for _, port := range ports {
|
|
|
|
current := port.PrivatePort
|
|
|
|
portKey := port.Type
|
|
|
|
if port.IP != "" {
|
|
|
|
if port.PublicPort != current {
|
|
|
|
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
|
|
|
|
}
|
|
|
|
group := groupMap[portKey]
|
|
|
|
|
|
|
|
if group == nil {
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
|
|
|
// record order that groupMap keys are created
|
|
|
|
groupMapKeys = append(groupMapKeys, portKey)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if current == (group.last + 1) {
|
|
|
|
group.last = current
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, formGroup(portKey, group.first, group.last))
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
|
|
|
}
|
|
|
|
for _, portKey := range groupMapKeys {
|
|
|
|
g := groupMap[portKey]
|
|
|
|
result = append(result, formGroup(portKey, g.first, g.last))
|
|
|
|
}
|
|
|
|
result = append(result, hostMappings...)
|
|
|
|
return strings.Join(result, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func formGroup(key string, start, last uint16) string {
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
groupType := parts[0]
|
|
|
|
var ip string
|
|
|
|
if len(parts) > 1 {
|
|
|
|
ip = parts[0]
|
|
|
|
groupType = parts[1]
|
|
|
|
}
|
|
|
|
group := strconv.Itoa(int(start))
|
|
|
|
if start != last {
|
|
|
|
group = fmt.Sprintf("%s-%d", group, last)
|
|
|
|
}
|
|
|
|
if ip != "" {
|
|
|
|
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s", group, groupType)
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
func comparePorts(i, j types.Port) bool {
|
|
|
|
if i.PrivatePort != j.PrivatePort {
|
|
|
|
return i.PrivatePort < j.PrivatePort
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
if i.IP != j.IP {
|
|
|
|
return i.IP < j.IP
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
if i.PublicPort != j.PublicPort {
|
|
|
|
return i.PublicPort < j.PublicPort
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
return i.Type < j.Type
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|