2016-08-23 19:25:43 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2016-11-17 00:46:37 -05:00
|
|
|
"fmt"
|
2016-10-18 00:36:52 -04:00
|
|
|
"regexp"
|
2017-05-16 19:56:56 -04:00
|
|
|
"runtime"
|
2017-04-12 17:04:49 -04:00
|
|
|
"sync/atomic"
|
2016-12-07 17:02:13 -05:00
|
|
|
"time"
|
2016-10-18 00:36:52 -04:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-08-23 19:25:43 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-17 00:46:37 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-12-07 17:02:13 -05:00
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2016-08-23 19:25:43 -04:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/directory"
|
2017-05-16 19:56:56 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2016-08-23 19:25:43 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2017-02-18 21:11:48 -05:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-04-12 17:04:49 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-16 11:37:30 -04:00
|
|
|
// errPruneRunning is returned when a prune request is received while
|
2017-04-12 17:04:49 -04:00
|
|
|
// one is in progress
|
2017-05-16 11:37:30 -04:00
|
|
|
errPruneRunning = fmt.Errorf("a prune operation is already running")
|
2017-05-05 12:37:06 -04:00
|
|
|
|
|
|
|
containersAcceptedFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
|
|
|
volumesAcceptedFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
}
|
|
|
|
imagesAcceptedFilters = map[string]bool{
|
|
|
|
"dangling": true,
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
|
|
|
networksAcceptedFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
)
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// ContainersPrune removes unused containers
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error) {
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.ContainersPruneReport{}
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(containersAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
allContainers := daemon.List()
|
|
|
|
for _, c := range allContainers {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("ContainersPrune operation cancelled: %#v", *rep)
|
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
if !c.IsRunning() {
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && c.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
if !matchLabels(pruneFilters, c.Config.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-18 21:11:48 -05:00
|
|
|
cSize, _ := daemon.getSize(c.ID)
|
2016-08-23 19:25:43 -04:00
|
|
|
// TODO: sets RmLink to true?
|
|
|
|
err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{})
|
|
|
|
if err != nil {
|
2016-10-08 06:38:25 -04:00
|
|
|
logrus.Warnf("failed to prune container %s: %v", c.ID, err)
|
2016-08-23 19:25:43 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cSize > 0 {
|
|
|
|
rep.SpaceReclaimed += uint64(cSize)
|
|
|
|
}
|
|
|
|
rep.ContainersDeleted = append(rep.ContainersDeleted, c.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// VolumesPrune removes unused local volumes
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Args) (*types.VolumesPruneReport, error) {
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(volumesAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.VolumesPruneReport{}
|
|
|
|
|
|
|
|
pruneVols := func(v volume.Volume) error {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("VolumesPrune operation cancelled: %#v", *rep)
|
2017-04-11 15:52:33 -04:00
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
name := v.Name()
|
|
|
|
refs := daemon.volumes.Refs(v)
|
|
|
|
|
|
|
|
if len(refs) == 0 {
|
2017-02-04 12:10:05 -05:00
|
|
|
detailedVolume, ok := v.(volume.DetailedVolume)
|
|
|
|
if ok {
|
|
|
|
if !matchLabels(pruneFilters, detailedVolume.Labels()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
vSize, err := directory.Size(v.Path())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not determine size of volume %s: %v", name, err)
|
|
|
|
}
|
2017-09-24 12:19:59 -04:00
|
|
|
err = daemon.volumeRm(v)
|
2016-08-23 19:25:43 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not remove volume %s: %v", name, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rep.SpaceReclaimed += uint64(vSize)
|
|
|
|
rep.VolumesDeleted = append(rep.VolumesDeleted, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
err = daemon.traverseLocalVolumes(pruneVols)
|
2017-07-06 04:47:21 -04:00
|
|
|
if err == context.Canceled {
|
|
|
|
return rep, nil
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// ImagesPrune removes unused images
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
|
2017-05-16 19:56:56 -04:00
|
|
|
// TODO @jhowardmsft LCOW Support: This will need revisiting later.
|
|
|
|
platform := runtime.GOOS
|
2017-06-26 12:11:54 -04:00
|
|
|
if system.LCOWSupported() {
|
2017-05-16 19:56:56 -04:00
|
|
|
platform = "linux"
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(imagesAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.ImagesPruneReport{}
|
|
|
|
|
2016-11-17 00:46:37 -05:00
|
|
|
danglingOnly := true
|
2017-09-26 07:39:56 -04:00
|
|
|
if pruneFilters.Contains("dangling") {
|
2016-11-17 00:46:37 -05:00
|
|
|
if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") {
|
|
|
|
danglingOnly = false
|
|
|
|
} else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"dangling", pruneFilters.Get("dangling")}
|
2016-11-17 00:46:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
var allImages map[image.ID]*image.Image
|
2016-11-17 00:46:37 -05:00
|
|
|
if danglingOnly {
|
2017-05-16 19:56:56 -04:00
|
|
|
allImages = daemon.stores[platform].imageStore.Heads()
|
2016-08-23 19:25:43 -04:00
|
|
|
} else {
|
2017-05-16 19:56:56 -04:00
|
|
|
allImages = daemon.stores[platform].imageStore.Map()
|
2016-08-23 19:25:43 -04:00
|
|
|
}
|
|
|
|
allContainers := daemon.List()
|
|
|
|
imageRefs := map[string]bool{}
|
|
|
|
for _, c := range allContainers {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
imageRefs[c.ID] = true
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter intermediary images and get their unique size
|
2017-05-16 19:56:56 -04:00
|
|
|
allLayers := daemon.stores[platform].layerStore.Map()
|
2016-08-23 19:25:43 -04:00
|
|
|
topImages := map[image.ID]*image.Image{}
|
|
|
|
for id, img := range allImages {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
dgst := digest.Digest(id)
|
2017-08-17 18:43:36 -04:00
|
|
|
if len(daemon.referenceStore.References(dgst)) == 0 && len(daemon.stores[platform].imageStore.Children(id)) != 0 {
|
2017-04-11 15:52:33 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !until.IsZero() && img.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-26 15:54:33 -04:00
|
|
|
if img.Config != nil && !matchLabels(pruneFilters, img.Config.Labels) {
|
2017-04-11 15:52:33 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
topImages[id] = img
|
2017-02-04 12:10:05 -05:00
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:52:33 -04:00
|
|
|
canceled := false
|
|
|
|
deleteImagesLoop:
|
2016-08-23 19:25:43 -04:00
|
|
|
for id := range topImages {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// we still want to calculate freed size and return the data
|
|
|
|
canceled = true
|
|
|
|
break deleteImagesLoop
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
dgst := digest.Digest(id)
|
|
|
|
hex := dgst.Hex()
|
|
|
|
if _, ok := imageRefs[hex]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:27:56 -05:00
|
|
|
deletedImages := []types.ImageDeleteResponseItem{}
|
2017-08-17 18:43:36 -04:00
|
|
|
refs := daemon.referenceStore.References(dgst)
|
2016-08-23 19:25:43 -04:00
|
|
|
if len(refs) > 0 {
|
2017-01-20 16:42:55 -05:00
|
|
|
shouldDelete := !danglingOnly
|
|
|
|
if !shouldDelete {
|
|
|
|
hasTag := false
|
|
|
|
for _, ref := range refs {
|
|
|
|
if _, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
hasTag = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only delete if it's untagged (i.e. repo:<none>)
|
|
|
|
shouldDelete = !hasTag
|
2016-08-23 19:25:43 -04:00
|
|
|
}
|
|
|
|
|
2017-01-20 16:42:55 -05:00
|
|
|
if shouldDelete {
|
|
|
|
for _, ref := range refs {
|
|
|
|
imgDel, err := daemon.ImageDelete(ref.String(), false, true)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not delete reference %s: %v", ref.String(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deletedImages = append(deletedImages, imgDel...)
|
2016-08-23 19:25:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
imgDel, err := daemon.ImageDelete(hex, false, true)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not delete image %s: %v", hex, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deletedImages = append(deletedImages, imgDel...)
|
|
|
|
}
|
|
|
|
|
|
|
|
rep.ImagesDeleted = append(rep.ImagesDeleted, deletedImages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute how much space was freed
|
|
|
|
for _, d := range rep.ImagesDeleted {
|
|
|
|
if d.Deleted != "" {
|
|
|
|
chid := layer.ChainID(d.Deleted)
|
|
|
|
if l, ok := allLayers[chid]; ok {
|
|
|
|
diffSize, err := l.DiffSize()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("failed to get layer %s size: %v", chid, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rep.SpaceReclaimed += uint64(diffSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:52:33 -04:00
|
|
|
if canceled {
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("ImagesPrune operation cancelled: %#v", *rep)
|
2017-04-11 15:52:33 -04:00
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
return rep, nil
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
|
|
|
|
// localNetworksPrune removes unused local networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filters.Args) *types.NetworksPruneReport {
|
2016-10-18 00:36:52 -04:00
|
|
|
rep := &types.NetworksPruneReport{}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
until, _ := getUntilFromPruneFilters(pruneFilters)
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
l := func(nw libnetwork.Network) bool {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
// context cancelled
|
2017-04-11 15:52:33 -04:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
2017-04-07 14:18:51 -04:00
|
|
|
if nw.Info().ConfigOnly() {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && nw.Info().Created().After(until) {
|
|
|
|
return false
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
if !matchLabels(pruneFilters, nw.Info().Labels()) {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
nwName := nw.Name()
|
2016-12-29 07:09:55 -05:00
|
|
|
if runconfig.IsPreDefinedNetwork(nwName) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(nw.Endpoints()) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err := daemon.DeleteNetwork(nw.ID()); err != nil {
|
|
|
|
logrus.Warnf("could not remove local network %s: %v", nwName, err)
|
|
|
|
return false
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nwName)
|
2016-10-18 00:36:52 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
daemon.netController.WalkNetworks(l)
|
2016-12-29 07:09:55 -05:00
|
|
|
return rep
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// clusterNetworksPrune removes unused cluster networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
|
2016-10-18 00:36:52 -04:00
|
|
|
rep := &types.NetworksPruneReport{}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
until, _ := getUntilFromPruneFilters(pruneFilters)
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
cluster := daemon.GetCluster()
|
2017-03-28 06:46:42 -04:00
|
|
|
|
|
|
|
if !cluster.IsManager() {
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
networks, err := cluster.GetNetworks()
|
|
|
|
if err != nil {
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
networkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`)
|
|
|
|
for _, nw := range networks {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
if nw.Ingress {
|
|
|
|
// Routing-mesh network removal has to be explicitly invoked by user
|
|
|
|
continue
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2017-04-11 15:52:33 -04:00
|
|
|
if !until.IsZero() && nw.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !matchLabels(pruneFilters, nw.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// https://github.com/docker/docker/issues/24186
|
|
|
|
// `docker network inspect` unfortunately displays ONLY those containers that are local to that node.
|
|
|
|
// So we try to remove it anyway and check the error
|
|
|
|
err = cluster.RemoveNetwork(nw.ID)
|
|
|
|
if err != nil {
|
|
|
|
// we can safely ignore the "network .. is in use" error
|
|
|
|
match := networkIsInUse.FindStringSubmatch(err.Error())
|
|
|
|
if len(match) != 2 || match[1] != nw.ID {
|
|
|
|
logrus.Warnf("could not remove cluster network %s: %v", nw.Name, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nw.Name)
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworksPrune removes unused networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(networksAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
if _, err := getUntilFromPruneFilters(pruneFilters); err != nil {
|
|
|
|
return nil, err
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
|
|
|
|
rep := &types.NetworksPruneReport{}
|
2017-04-11 15:52:33 -04:00
|
|
|
if clusterRep, err := daemon.clusterNetworksPrune(ctx, pruneFilters); err == nil {
|
2017-02-04 12:10:05 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, clusterRep.NetworksDeleted...)
|
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
|
2017-04-11 15:52:33 -04:00
|
|
|
localRep := daemon.localNetworksPrune(ctx, pruneFilters)
|
2016-12-29 07:09:55 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, localRep.NetworksDeleted...)
|
2017-04-11 15:52:33 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("NetworksPrune operation cancelled: %#v", *rep)
|
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
return rep, nil
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
|
|
|
func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {
|
|
|
|
until := time.Time{}
|
2017-09-26 07:39:56 -04:00
|
|
|
if !pruneFilters.Contains("until") {
|
2016-12-07 17:02:13 -05:00
|
|
|
return until, nil
|
|
|
|
}
|
|
|
|
untilFilters := pruneFilters.Get("until")
|
|
|
|
if len(untilFilters) > 1 {
|
|
|
|
return until, fmt.Errorf("more than one until filter specified")
|
|
|
|
}
|
|
|
|
ts, err := timetypes.GetTimestamp(untilFilters[0], time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
seconds, nanoseconds, err := timetypes.ParseTimestamps(ts, 0)
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
until = time.Unix(seconds, nanoseconds)
|
|
|
|
return until, nil
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
|
|
|
|
func matchLabels(pruneFilters filters.Args, labels map[string]string) bool {
|
|
|
|
if !pruneFilters.MatchKVList("label", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// By default MatchKVList will return true if field (like 'label!') does not exist
|
2017-09-26 07:39:56 -04:00
|
|
|
// So we have to add additional Contains("label!") check
|
|
|
|
if pruneFilters.Contains("label!") {
|
2017-02-04 12:10:05 -05:00
|
|
|
if pruneFilters.MatchKVList("label!", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|