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