2014-07-31 21:26:25 +00:00
|
|
|
package daemon
|
2014-07-30 02:04:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-06-02 19:08:39 +00:00
|
|
|
"strconv"
|
2014-07-30 02:04:39 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-04-04 00:39:06 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/nat"
|
2014-07-30 02:04:39 +00:00
|
|
|
"github.com/docker/docker/pkg/graphdb"
|
2014-06-02 19:08:39 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
2014-07-30 02:04:39 +00:00
|
|
|
)
|
|
|
|
|
2014-07-31 21:26:25 +00:00
|
|
|
// List returns an array of all containers registered in the daemon.
|
|
|
|
func (daemon *Daemon) List() []*Container {
|
|
|
|
return daemon.containers.List()
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
type ContainersConfig struct {
|
|
|
|
All bool
|
|
|
|
Since string
|
|
|
|
Before string
|
|
|
|
Limit int
|
|
|
|
Size bool
|
|
|
|
Filters string
|
|
|
|
}
|
2015-04-04 00:39:06 +00:00
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, error) {
|
2014-07-30 02:04:39 +00:00
|
|
|
var (
|
|
|
|
foundBefore bool
|
|
|
|
displayed int
|
2015-04-07 19:34:30 +00:00
|
|
|
all = config.All
|
|
|
|
n = config.Limit
|
2014-06-02 19:08:39 +00:00
|
|
|
psFilters filters.Args
|
2015-03-26 01:40:23 +00:00
|
|
|
filtExited []int
|
2014-07-30 02:04:39 +00:00
|
|
|
)
|
2015-04-07 19:34:30 +00:00
|
|
|
containers := []*types.Container{}
|
2014-07-30 02:04:39 +00:00
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
psFilters, err := filters.FromParam(config.Filters)
|
2014-06-02 19:08:39 +00:00
|
|
|
if err != nil {
|
2015-04-07 19:34:30 +00:00
|
|
|
return nil, err
|
2014-06-02 19:08:39 +00:00
|
|
|
}
|
|
|
|
if i, ok := psFilters["exited"]; ok {
|
|
|
|
for _, value := range i {
|
|
|
|
code, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
2015-04-07 19:34:30 +00:00
|
|
|
return nil, err
|
2014-06-02 19:08:39 +00:00
|
|
|
}
|
2015-03-26 01:40:23 +00:00
|
|
|
filtExited = append(filtExited, code)
|
2014-06-02 19:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:14:04 +00:00
|
|
|
if i, ok := psFilters["status"]; ok {
|
|
|
|
for _, value := range i {
|
|
|
|
if value == "exited" {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 02:04:39 +00:00
|
|
|
names := map[string][]string{}
|
2014-07-31 21:26:25 +00:00
|
|
|
daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
|
2014-07-30 02:04:39 +00:00
|
|
|
names[e.ID()] = append(names[e.ID()], p)
|
|
|
|
return nil
|
2015-01-14 23:26:42 +00:00
|
|
|
}, 1)
|
2014-07-30 02:04:39 +00:00
|
|
|
|
2014-07-31 21:26:25 +00:00
|
|
|
var beforeCont, sinceCont *Container
|
2015-04-07 19:34:30 +00:00
|
|
|
if config.Before != "" {
|
|
|
|
beforeCont, err = daemon.Get(config.Before)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
2015-04-07 19:34:30 +00:00
|
|
|
return nil, err
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
if config.Since != "" {
|
|
|
|
sinceCont, err = daemon.Get(config.Since)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
2015-04-07 19:34:30 +00:00
|
|
|
return nil, err
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errLast := errors.New("last container")
|
2014-07-31 21:26:25 +00:00
|
|
|
writeCont := func(container *Container) error {
|
2014-07-30 02:04:39 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-04-07 19:34:30 +00:00
|
|
|
if !container.Running && !all && n <= 0 && config.Since == "" && config.Before == "" {
|
2014-07-30 02:04:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-10-13 06:12:44 +00:00
|
|
|
if !psFilters.Match("name", container.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !psFilters.Match("id", container.ID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-07 00:04:10 +00:00
|
|
|
if !psFilters.MatchKVList("label", container.Config.Labels) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
if config.Before != "" && !foundBefore {
|
2014-07-30 02:04:39 +00:00
|
|
|
if container.ID == beforeCont.ID {
|
|
|
|
foundBefore = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if n > 0 && displayed == n {
|
|
|
|
return errLast
|
|
|
|
}
|
2015-04-07 19:34:30 +00:00
|
|
|
if config.Since != "" {
|
2014-07-30 02:04:39 +00:00
|
|
|
if container.ID == sinceCont.ID {
|
|
|
|
return errLast
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 01:40:23 +00:00
|
|
|
if len(filtExited) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for _, code := range filtExited {
|
2015-01-08 22:51:47 +00:00
|
|
|
if code == container.ExitCode && !container.Running {
|
2015-03-26 01:40:23 +00:00
|
|
|
shouldSkip = false
|
2014-06-02 19:08:39 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 01:40:23 +00:00
|
|
|
if shouldSkip {
|
2014-06-02 19:08:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 06:12:44 +00:00
|
|
|
|
|
|
|
if !psFilters.Match("status", container.State.StateString()) {
|
|
|
|
return nil
|
2014-09-26 23:25:50 +00:00
|
|
|
}
|
2014-07-30 02:04:39 +00:00
|
|
|
displayed++
|
2015-04-07 19:34:30 +00:00
|
|
|
newC := &types.Container{
|
2015-04-04 00:39:06 +00:00
|
|
|
ID: container.ID,
|
|
|
|
Names: names[container.ID],
|
|
|
|
}
|
2015-04-01 14:08:00 +00:00
|
|
|
newC.Image = container.Config.Image
|
2014-07-30 02:04:39 +00:00
|
|
|
if len(container.Args) > 0 {
|
|
|
|
args := []string{}
|
|
|
|
for _, arg := range container.Args {
|
|
|
|
if strings.Contains(arg, " ") {
|
|
|
|
args = append(args, fmt.Sprintf("'%s'", arg))
|
|
|
|
} else {
|
|
|
|
args = append(args, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argsAsString := strings.Join(args, " ")
|
|
|
|
|
2015-04-04 00:39:06 +00:00
|
|
|
newC.Command = fmt.Sprintf("%s %s", container.Path, argsAsString)
|
2014-07-30 02:04:39 +00:00
|
|
|
} else {
|
2015-04-04 00:39:06 +00:00
|
|
|
newC.Command = fmt.Sprintf("%s", container.Path)
|
|
|
|
}
|
|
|
|
newC.Created = int(container.Created.Unix())
|
|
|
|
newC.Status = container.State.String()
|
|
|
|
|
|
|
|
newC.Ports = []types.Port{}
|
|
|
|
for port, bindings := range container.NetworkSettings.Ports {
|
|
|
|
p, _ := nat.ParsePort(port.Port())
|
|
|
|
if len(bindings) == 0 {
|
|
|
|
newC.Ports = append(newC.Ports, types.Port{
|
|
|
|
PrivatePort: p,
|
|
|
|
Type: port.Proto(),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, binding := range bindings {
|
|
|
|
h, _ := nat.ParsePort(binding.HostPort)
|
|
|
|
newC.Ports = append(newC.Ports, types.Port{
|
|
|
|
PrivatePort: p,
|
|
|
|
PublicPort: h,
|
|
|
|
Type: port.Proto(),
|
|
|
|
IP: binding.HostIp,
|
|
|
|
})
|
|
|
|
}
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|
2015-04-04 00:39:06 +00:00
|
|
|
|
2015-04-07 19:34:30 +00:00
|
|
|
if config.Size {
|
2014-07-30 02:04:39 +00:00
|
|
|
sizeRw, sizeRootFs := container.GetSize()
|
2015-04-04 00:39:06 +00:00
|
|
|
newC.SizeRw = int(sizeRw)
|
|
|
|
newC.SizeRootFs = int(sizeRootFs)
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|
2015-04-04 00:39:06 +00:00
|
|
|
newC.Labels = container.Config.Labels
|
|
|
|
containers = append(containers, newC)
|
2014-07-30 02:04:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 21:26:25 +00:00
|
|
|
for _, container := range daemon.List() {
|
2014-07-30 02:04:39 +00:00
|
|
|
if err := writeCont(container); err != nil {
|
|
|
|
if err != errLast {
|
2015-04-07 19:34:30 +00:00
|
|
|
return nil, err
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 19:34:30 +00:00
|
|
|
return containers, nil
|
2014-07-30 02:04:39 +00:00
|
|
|
}
|