2017-07-06 14:28:13 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-22 11:16:03 +00:00
|
|
|
"sync"
|
2017-07-06 14:28:13 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/libcompose/project/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Containers lists the containers for the specified services. Can be filter using
|
|
|
|
// the Filter struct.
|
|
|
|
func (p *Project) Containers(ctx context.Context, filter Filter, services ...string) ([]string, error) {
|
|
|
|
containers := []string{}
|
2018-01-22 11:16:03 +00:00
|
|
|
var lock sync.Mutex
|
|
|
|
|
2017-07-06 14:28:13 +00:00
|
|
|
err := p.forEach(services, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
|
|
|
|
wrapper.Do(nil, events.NoEvent, events.NoEvent, func(service Service) error {
|
|
|
|
serviceContainers, innerErr := service.Containers(ctx)
|
|
|
|
if innerErr != nil {
|
|
|
|
return innerErr
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range serviceContainers {
|
|
|
|
running := container.IsRunning(ctx)
|
|
|
|
switch filter.State {
|
|
|
|
case Running:
|
|
|
|
if !running {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case Stopped:
|
|
|
|
if running {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case AnyState:
|
|
|
|
// Don't do a thing
|
|
|
|
default:
|
|
|
|
// Invalid state filter
|
|
|
|
return fmt.Errorf("Invalid container filter: %s", filter.State)
|
|
|
|
}
|
|
|
|
containerID := container.ID()
|
2018-01-22 11:16:03 +00:00
|
|
|
lock.Lock()
|
2017-07-06 14:28:13 +00:00
|
|
|
containers = append(containers, containerID)
|
2018-01-22 11:16:03 +00:00
|
|
|
lock.Unlock()
|
2017-07-06 14:28:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return containers, nil
|
|
|
|
}
|