mirror of https://github.com/docker/docs.git
Merge pull request #744 from aluzzardi/ps-filters
container: Use state strings from dockerclient instead of embedding them
This commit is contained in:
commit
1bca3fb483
|
@ -60,7 +60,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/samalba/dockerclient",
|
"ImportPath": "github.com/samalba/dockerclient",
|
||||||
"Rev": "5e5020b90dd4657c33d446356556481182d2d66b"
|
"Rev": "4e6f4a21c07510dd6446d28053351a275591f08d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/samuel/go-zookeeper/zk",
|
"ImportPath": "github.com/samuel/go-zookeeper/zk",
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package dockerclient
|
package dockerclient
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/units"
|
||||||
|
)
|
||||||
|
|
||||||
type ContainerConfig struct {
|
type ContainerConfig struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
|
@ -78,15 +83,7 @@ type PortBinding struct {
|
||||||
HostPort string
|
HostPort string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContainerInfo struct {
|
type State struct {
|
||||||
Id string
|
|
||||||
Created string
|
|
||||||
Path string
|
|
||||||
Name string
|
|
||||||
Args []string
|
|
||||||
ExecIDs []string
|
|
||||||
Config *ContainerConfig
|
|
||||||
State struct {
|
|
||||||
Running bool
|
Running bool
|
||||||
Paused bool
|
Paused bool
|
||||||
Restarting bool
|
Restarting bool
|
||||||
|
@ -99,6 +96,61 @@ type ContainerInfo struct {
|
||||||
FinishedAt time.Time
|
FinishedAt time.Time
|
||||||
Ghost bool
|
Ghost bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a human-readable description of the state
|
||||||
|
// Stoken from docker/docker/daemon/state.go
|
||||||
|
func (s *State) String() string {
|
||||||
|
if s.Running {
|
||||||
|
if s.Paused {
|
||||||
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
||||||
|
}
|
||||||
|
if s.Restarting {
|
||||||
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Dead {
|
||||||
|
return "Dead"
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.FinishedAt.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// StateString returns a single string to describe state
|
||||||
|
// Stoken from docker/docker/daemon/state.go
|
||||||
|
func (s *State) StateString() string {
|
||||||
|
if s.Running {
|
||||||
|
if s.Paused {
|
||||||
|
return "paused"
|
||||||
|
}
|
||||||
|
if s.Restarting {
|
||||||
|
return "restarting"
|
||||||
|
}
|
||||||
|
return "running"
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Dead {
|
||||||
|
return "dead"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "exited"
|
||||||
|
}
|
||||||
|
|
||||||
|
type ContainerInfo struct {
|
||||||
|
Id string
|
||||||
|
Created string
|
||||||
|
Path string
|
||||||
|
Name string
|
||||||
|
Args []string
|
||||||
|
ExecIDs []string
|
||||||
|
Config *ContainerConfig
|
||||||
|
State *State
|
||||||
Image string
|
Image string
|
||||||
NetworkSettings struct {
|
NetworkSettings struct {
|
||||||
IPAddress string `json:"IpAddress"`
|
IPAddress string `json:"IpAddress"`
|
||||||
|
|
|
@ -158,7 +158,7 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
if !filters.MatchKVList("label", container.Config.Labels) {
|
if !filters.MatchKVList("label", container.Config.Labels) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !filters.Match("status", container.StateString()) {
|
if !filters.Match("status", container.Info.State.StateString()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Update the Status. The one we have is stale from the last `docker ps` the engine sent.
|
// Update the Status. The one we have is stale from the last `docker ps` the engine sent.
|
||||||
// `Status()` will generate a new one
|
// `Status()` will generate a new one
|
||||||
tmp.Status = container.Status()
|
tmp.Status = container.Info.State.String()
|
||||||
if !container.Engine.IsHealthy() {
|
if !container.Engine.IsHealthy() {
|
||||||
tmp.Status = "Pending"
|
tmp.Status = "Pending"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import "github.com/samalba/dockerclient"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/units"
|
|
||||||
"github.com/samalba/dockerclient"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Container is exported
|
// Container is exported
|
||||||
type Container struct {
|
type Container struct {
|
||||||
|
@ -16,50 +10,3 @@ type Container struct {
|
||||||
Info dockerclient.ContainerInfo
|
Info dockerclient.ContainerInfo
|
||||||
Engine *Engine
|
Engine *Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status returns a human-readable description of the state
|
|
||||||
// Stoken from docker/docker/daemon/state.go
|
|
||||||
func (c *Container) Status() string {
|
|
||||||
s := c.Info.State
|
|
||||||
if s.Running {
|
|
||||||
if s.Paused {
|
|
||||||
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
||||||
}
|
|
||||||
if s.Restarting {
|
|
||||||
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.Dead {
|
|
||||||
return "Dead"
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.FinishedAt.IsZero() {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// StateString returns a single string to describe state
|
|
||||||
// Stoken from docker/docker/daemon/state.go
|
|
||||||
func (c *Container) StateString() string {
|
|
||||||
s := c.Info.State
|
|
||||||
if s.Running {
|
|
||||||
if s.Paused {
|
|
||||||
return "paused"
|
|
||||||
}
|
|
||||||
if s.Restarting {
|
|
||||||
return "restarting"
|
|
||||||
}
|
|
||||||
return "running"
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.Dead {
|
|
||||||
return "dead"
|
|
||||||
}
|
|
||||||
|
|
||||||
return "exited"
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue