2016-07-25 15:24:34 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-07-25 15:24:34 -04:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli/command/inspect"
|
2017-01-26 16:08:07 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-07-25 15:24:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
const serviceInspectPrettyTemplate Format = `
|
|
|
|
ID: {{.ID}}
|
|
|
|
Name: {{.Name}}
|
|
|
|
{{- if .Labels }}
|
|
|
|
Labels:
|
|
|
|
{{- range $k, $v := .Labels }}
|
|
|
|
{{ $k }}{{if $v }}={{ $v }}{{ end }}
|
|
|
|
{{- end }}{{ end }}
|
2016-09-25 04:47:45 -04:00
|
|
|
Service Mode:
|
|
|
|
{{- if .IsModeGlobal }} Global
|
|
|
|
{{- else if .IsModeReplicated }} Replicated
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- if .ModeReplicatedReplicas }}
|
|
|
|
Replicas: {{ .ModeReplicatedReplicas }}
|
|
|
|
{{- end }}{{ end }}
|
|
|
|
{{- if .HasUpdateStatus }}
|
|
|
|
UpdateStatus:
|
|
|
|
State: {{ .UpdateStatusState }}
|
2016-12-01 17:08:06 -05:00
|
|
|
{{- if .HasUpdateStatusStarted }}
|
2016-07-25 15:24:34 -04:00
|
|
|
Started: {{ .UpdateStatusStarted }}
|
2016-12-01 17:08:06 -05:00
|
|
|
{{- end }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- if .UpdateIsCompleted }}
|
|
|
|
Completed: {{ .UpdateStatusCompleted }}
|
|
|
|
{{- end }}
|
|
|
|
Message: {{ .UpdateStatusMessage }}
|
|
|
|
{{- end }}
|
|
|
|
Placement:
|
|
|
|
{{- if .TaskPlacementConstraints -}}
|
|
|
|
Contraints: {{ .TaskPlacementConstraints }}
|
|
|
|
{{- end }}
|
|
|
|
{{- if .HasUpdateConfig }}
|
|
|
|
UpdateConfig:
|
|
|
|
Parallelism: {{ .UpdateParallelism }}
|
2016-09-02 17:12:05 -04:00
|
|
|
{{- if .HasUpdateDelay}}
|
2016-07-25 15:24:34 -04:00
|
|
|
Delay: {{ .UpdateDelay }}
|
|
|
|
{{- end }}
|
|
|
|
On failure: {{ .UpdateOnFailure }}
|
2016-09-02 17:12:05 -04:00
|
|
|
{{- if .HasUpdateMonitor}}
|
|
|
|
Monitoring Period: {{ .UpdateMonitor }}
|
|
|
|
{{- end }}
|
|
|
|
Max failure ratio: {{ .UpdateMaxFailureRatio }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- end }}
|
|
|
|
ContainerSpec:
|
|
|
|
Image: {{ .ContainerImage }}
|
|
|
|
{{- if .ContainerArgs }}
|
|
|
|
Args: {{ range $arg := .ContainerArgs }}{{ $arg }} {{ end }}
|
|
|
|
{{- end -}}
|
|
|
|
{{- if .ContainerEnv }}
|
|
|
|
Env: {{ range $env := .ContainerEnv }}{{ $env }} {{ end }}
|
|
|
|
{{- end -}}
|
|
|
|
{{- if .ContainerWorkDir }}
|
|
|
|
Dir: {{ .ContainerWorkDir }}
|
|
|
|
{{- end -}}
|
|
|
|
{{- if .ContainerUser }}
|
|
|
|
User: {{ .ContainerUser }}
|
|
|
|
{{- end }}
|
|
|
|
{{- if .ContainerMounts }}
|
|
|
|
Mounts:
|
|
|
|
{{- end }}
|
|
|
|
{{- range $mount := .ContainerMounts }}
|
|
|
|
Target = {{ $mount.Target }}
|
|
|
|
Source = {{ $mount.Source }}
|
|
|
|
ReadOnly = {{ $mount.ReadOnly }}
|
|
|
|
Type = {{ $mount.Type }}
|
|
|
|
{{- end -}}
|
|
|
|
{{- if .HasResources }}
|
|
|
|
Resources:
|
|
|
|
{{- if .HasResourceReservations }}
|
|
|
|
Reservations:
|
|
|
|
{{- if gt .ResourceReservationNanoCPUs 0.0 }}
|
|
|
|
CPU: {{ .ResourceReservationNanoCPUs }}
|
|
|
|
{{- end }}
|
|
|
|
{{- if .ResourceReservationMemory }}
|
|
|
|
Memory: {{ .ResourceReservationMemory }}
|
2016-09-26 05:12:24 -04:00
|
|
|
{{- end }}{{ end }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- if .HasResourceLimits }}
|
|
|
|
Limits:
|
|
|
|
{{- if gt .ResourceLimitsNanoCPUs 0.0 }}
|
|
|
|
CPU: {{ .ResourceLimitsNanoCPUs }}
|
|
|
|
{{- end }}
|
|
|
|
{{- if .ResourceLimitMemory }}
|
|
|
|
Memory: {{ .ResourceLimitMemory }}
|
2016-09-26 05:12:24 -04:00
|
|
|
{{- end }}{{ end }}{{ end }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- if .Networks }}
|
|
|
|
Networks:
|
|
|
|
{{- range $network := .Networks }} {{ $network }}{{ end }} {{ end }}
|
2016-09-25 04:47:45 -04:00
|
|
|
Endpoint Mode: {{ .EndpointMode }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- if .Ports }}
|
|
|
|
Ports:
|
|
|
|
{{- range $port := .Ports }}
|
2017-02-10 01:53:18 -05:00
|
|
|
PublishedPort = {{ $port.PublishedPort }}
|
2016-07-25 15:24:34 -04:00
|
|
|
Protocol = {{ $port.Protocol }}
|
|
|
|
TargetPort = {{ $port.TargetPort }}
|
2017-01-05 14:21:22 -05:00
|
|
|
PublishMode = {{ $port.PublishMode }}
|
2016-07-25 15:24:34 -04:00
|
|
|
{{- end }} {{ end -}}
|
|
|
|
`
|
|
|
|
|
|
|
|
// NewServiceFormat returns a Format for rendering using a Context
|
|
|
|
func NewServiceFormat(source string) Format {
|
|
|
|
switch source {
|
|
|
|
case PrettyFormatKey:
|
|
|
|
return serviceInspectPrettyTemplate
|
|
|
|
default:
|
|
|
|
return Format(strings.TrimPrefix(source, RawFormatKey))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceInspectWrite renders the context for a list of services
|
|
|
|
func ServiceInspectWrite(ctx Context, refs []string, getRef inspect.GetRefFunc) error {
|
|
|
|
if ctx.Format != serviceInspectPrettyTemplate {
|
|
|
|
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
|
|
|
|
}
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, ref := range refs {
|
|
|
|
serviceI, _, err := getRef(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service, ok := serviceI.(swarm.Service)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("got wrong object to inspect")
|
|
|
|
}
|
|
|
|
if err := format(&serviceInspectContext{Service: service}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&serviceInspectContext{}, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceInspectContext struct {
|
|
|
|
swarm.Service
|
|
|
|
subContext
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (ctx *serviceInspectContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return marshalJSON(ctx)
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
func (ctx *serviceInspectContext) ID() string {
|
|
|
|
return ctx.Service.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) Name() string {
|
|
|
|
return ctx.Service.Spec.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) Labels() map[string]string {
|
|
|
|
return ctx.Service.Spec.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) IsModeGlobal() bool {
|
|
|
|
return ctx.Service.Spec.Mode.Global != nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:12:24 -04:00
|
|
|
func (ctx *serviceInspectContext) IsModeReplicated() bool {
|
|
|
|
return ctx.Service.Spec.Mode.Replicated != nil
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
func (ctx *serviceInspectContext) ModeReplicatedReplicas() *uint64 {
|
|
|
|
return ctx.Service.Spec.Mode.Replicated.Replicas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasUpdateStatus() bool {
|
2016-12-01 17:08:06 -05:00
|
|
|
return ctx.Service.UpdateStatus != nil && ctx.Service.UpdateStatus.State != ""
|
2016-07-25 15:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateStatusState() swarm.UpdateState {
|
|
|
|
return ctx.Service.UpdateStatus.State
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:08:06 -05:00
|
|
|
func (ctx *serviceInspectContext) HasUpdateStatusStarted() bool {
|
|
|
|
return ctx.Service.UpdateStatus.StartedAt != nil
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
func (ctx *serviceInspectContext) UpdateStatusStarted() string {
|
2016-12-01 17:08:06 -05:00
|
|
|
return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.StartedAt))
|
2016-07-25 15:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateIsCompleted() bool {
|
2016-12-01 17:08:06 -05:00
|
|
|
return ctx.Service.UpdateStatus.State == swarm.UpdateStateCompleted && ctx.Service.UpdateStatus.CompletedAt != nil
|
2016-07-25 15:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateStatusCompleted() string {
|
2016-12-01 17:08:06 -05:00
|
|
|
return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.CompletedAt))
|
2016-07-25 15:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateStatusMessage() string {
|
|
|
|
return ctx.Service.UpdateStatus.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) TaskPlacementConstraints() []string {
|
|
|
|
if ctx.Service.Spec.TaskTemplate.Placement != nil {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.Placement.Constraints
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasUpdateConfig() bool {
|
|
|
|
return ctx.Service.Spec.UpdateConfig != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateParallelism() uint64 {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.Parallelism
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasUpdateDelay() bool {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.Delay.Nanoseconds() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateDelay() time.Duration {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.Delay
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateOnFailure() string {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.FailureAction
|
|
|
|
}
|
|
|
|
|
2016-09-02 17:12:05 -04:00
|
|
|
func (ctx *serviceInspectContext) HasUpdateMonitor() bool {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.Monitor.Nanoseconds() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateMonitor() time.Duration {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.Monitor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) UpdateMaxFailureRatio() float32 {
|
|
|
|
return ctx.Service.Spec.UpdateConfig.MaxFailureRatio
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
func (ctx *serviceInspectContext) ContainerImage() string {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ContainerArgs() []string {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ContainerEnv() []string {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Env
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ContainerWorkDir() string {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ContainerUser() string {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ContainerMounts() []mounttypes.Mount {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Mounts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasResources() bool {
|
|
|
|
return ctx.Service.Spec.TaskTemplate.Resources != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasResourceReservations() bool {
|
2016-11-16 19:46:31 -05:00
|
|
|
if ctx.Service.Spec.TaskTemplate.Resources == nil || ctx.Service.Spec.TaskTemplate.Resources.Reservations == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-07-25 15:24:34 -04:00
|
|
|
return ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs > 0 || ctx.Service.Spec.TaskTemplate.Resources.Reservations.MemoryBytes > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ResourceReservationNanoCPUs() float64 {
|
|
|
|
if ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs == 0 {
|
|
|
|
return float64(0)
|
|
|
|
}
|
|
|
|
return float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs) / 1e9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ResourceReservationMemory() string {
|
|
|
|
if ctx.Service.Spec.TaskTemplate.Resources.Reservations.MemoryBytes == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return units.BytesSize(float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.MemoryBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) HasResourceLimits() bool {
|
2016-11-16 19:46:31 -05:00
|
|
|
if ctx.Service.Spec.TaskTemplate.Resources == nil || ctx.Service.Spec.TaskTemplate.Resources.Limits == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-07-25 15:24:34 -04:00
|
|
|
return ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs > 0 || ctx.Service.Spec.TaskTemplate.Resources.Limits.MemoryBytes > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ResourceLimitsNanoCPUs() float64 {
|
|
|
|
return float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs) / 1e9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) ResourceLimitMemory() string {
|
|
|
|
if ctx.Service.Spec.TaskTemplate.Resources.Limits.MemoryBytes == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return units.BytesSize(float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.MemoryBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *serviceInspectContext) Networks() []string {
|
|
|
|
var out []string
|
|
|
|
for _, n := range ctx.Service.Spec.Networks {
|
|
|
|
out = append(out, n.Target)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-09-25 04:47:45 -04:00
|
|
|
func (ctx *serviceInspectContext) EndpointMode() string {
|
|
|
|
if ctx.Service.Spec.EndpointSpec == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(ctx.Service.Spec.EndpointSpec.Mode)
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
func (ctx *serviceInspectContext) Ports() []swarm.PortConfig {
|
|
|
|
return ctx.Service.Endpoint.Ports
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
defaultServiceTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Mode}}\t{{.Replicas}}\t{{.Image}}"
|
|
|
|
|
|
|
|
serviceIDHeader = "ID"
|
|
|
|
modeHeader = "MODE"
|
|
|
|
replicasHeader = "REPLICAS"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewServiceListFormat returns a Format for rendering using a service Context
|
|
|
|
func NewServiceListFormat(source string, quiet bool) Format {
|
|
|
|
switch source {
|
|
|
|
case TableFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return defaultQuietFormat
|
|
|
|
}
|
|
|
|
return defaultServiceTableFormat
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `id: {{.ID}}`
|
|
|
|
}
|
|
|
|
return `id: {{.ID}}\nname: {{.Name}}\nmode: {{.Mode}}\nreplicas: {{.Replicas}}\nimage: {{.Image}}\n`
|
|
|
|
}
|
|
|
|
return Format(source)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceListInfo stores the information about mode and replicas to be used by template
|
|
|
|
type ServiceListInfo struct {
|
|
|
|
Mode string
|
|
|
|
Replicas string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceListWrite writes the context
|
|
|
|
func ServiceListWrite(ctx Context, services []swarm.Service, info map[string]ServiceListInfo) error {
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, service := range services {
|
|
|
|
serviceCtx := &serviceContext{service: service, mode: info[service.ID].Mode, replicas: info[service.ID].Replicas}
|
|
|
|
if err := format(serviceCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&serviceContext{}, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceContext struct {
|
|
|
|
HeaderContext
|
|
|
|
service swarm.Service
|
|
|
|
mode string
|
|
|
|
replicas string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return marshalJSON(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) ID() string {
|
|
|
|
c.AddHeader(serviceIDHeader)
|
|
|
|
return stringid.TruncateID(c.service.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) Name() string {
|
|
|
|
c.AddHeader(nameHeader)
|
|
|
|
return c.service.Spec.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) Mode() string {
|
|
|
|
c.AddHeader(modeHeader)
|
|
|
|
return c.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) Replicas() string {
|
|
|
|
c.AddHeader(replicasHeader)
|
|
|
|
return c.replicas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceContext) Image() string {
|
|
|
|
c.AddHeader(imageHeader)
|
|
|
|
image := c.service.Spec.TaskTemplate.ContainerSpec.Image
|
2017-01-25 19:54:18 -05:00
|
|
|
if ref, err := reference.ParseNormalizedNamed(image); err == nil {
|
|
|
|
// update image string for display, (strips any digest)
|
|
|
|
if nt, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
if namedTagged, err := reference.WithTag(reference.TrimNamed(nt), nt.Tag()); err == nil {
|
|
|
|
image = reference.FamiliarString(namedTagged)
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|