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"
|
2016-12-07 17:02:13 -05:00
|
|
|
"time"
|
2016-10-18 00:36:52 -04:00
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"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"
|
|
|
|
"github.com/docker/docker/reference"
|
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-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-08-23 19:25:43 -04:00
|
|
|
)
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// ContainersPrune removes unused containers
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) ContainersPrune(pruneFilters filters.Args) (*types.ContainersPruneReport, error) {
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.ContainersPruneReport{}
|
|
|
|
|
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 {
|
|
|
|
if !c.IsRunning() {
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && c.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
cSize, _ := daemon.getSize(c)
|
|
|
|
// 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
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) VolumesPrune(pruneFilters filters.Args) (*types.VolumesPruneReport, error) {
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.VolumesPruneReport{}
|
|
|
|
|
|
|
|
pruneVols := func(v volume.Volume) error {
|
|
|
|
name := v.Name()
|
|
|
|
refs := daemon.volumes.Refs(v)
|
|
|
|
|
|
|
|
if len(refs) == 0 {
|
|
|
|
vSize, err := directory.Size(v.Path())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not determine size of volume %s: %v", name, err)
|
|
|
|
}
|
|
|
|
err = daemon.volumes.Remove(v)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
err := daemon.traverseLocalVolumes(pruneVols)
|
|
|
|
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// ImagesPrune removes unused images
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) ImagesPrune(pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.ImagesPruneReport{}
|
|
|
|
|
2016-11-17 00:46:37 -05:00
|
|
|
danglingOnly := true
|
|
|
|
if pruneFilters.Include("dangling") {
|
|
|
|
if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") {
|
|
|
|
danglingOnly = false
|
|
|
|
} else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") {
|
|
|
|
return nil, fmt.Errorf("Invalid filter 'dangling=%s'", pruneFilters.Get("dangling"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-08-23 19:25:43 -04:00
|
|
|
allImages = daemon.imageStore.Heads()
|
|
|
|
} else {
|
|
|
|
allImages = daemon.imageStore.Map()
|
|
|
|
}
|
|
|
|
allContainers := daemon.List()
|
|
|
|
imageRefs := map[string]bool{}
|
|
|
|
for _, c := range allContainers {
|
|
|
|
imageRefs[c.ID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter intermediary images and get their unique size
|
|
|
|
allLayers := daemon.layerStore.Map()
|
|
|
|
topImages := map[image.ID]*image.Image{}
|
|
|
|
for id, img := range allImages {
|
|
|
|
dgst := digest.Digest(id)
|
|
|
|
if len(daemon.referenceStore.References(dgst)) == 0 && len(daemon.imageStore.Children(id)) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && img.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
topImages[id] = img
|
|
|
|
}
|
|
|
|
|
|
|
|
for id := range topImages {
|
|
|
|
dgst := digest.Digest(id)
|
|
|
|
hex := dgst.Hex()
|
|
|
|
if _, ok := imageRefs[hex]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
deletedImages := []types.ImageDelete{}
|
|
|
|
refs := daemon.referenceStore.References(dgst)
|
|
|
|
if len(refs) > 0 {
|
2016-11-17 00:46:37 -05:00
|
|
|
if danglingOnly {
|
2016-08-23 19:25:43 -04:00
|
|
|
// Not a dangling image
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nrRefs := len(refs)
|
|
|
|
for _, ref := range refs {
|
|
|
|
// If nrRefs == 1, we have an image marked as myreponame:<none>
|
|
|
|
// i.e. the tag content was changed
|
|
|
|
if _, ok := ref.(reference.Canonical); ok && nrRefs > 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rep, nil
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
|
|
|
|
// localNetworksPrune removes unused local networks
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) localNetworksPrune(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
|
|
|
|
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
l := func(nw libnetwork.Network) bool {
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && nw.Info().Created().After(until) {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
nwName := nw.Name()
|
|
|
|
predefined := runconfig.IsPreDefinedNetwork(nwName)
|
|
|
|
if !predefined && len(nw.Endpoints()) == 0 {
|
|
|
|
if err = daemon.DeleteNetwork(nw.ID()); err != nil {
|
|
|
|
logrus.Warnf("could not remove network %s: %v", nwName, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nwName)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
daemon.netController.WalkNetworks(l)
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// clusterNetworksPrune removes unused cluster networks
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) clusterNetworksPrune(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
|
|
|
|
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
cluster := daemon.GetCluster()
|
|
|
|
networks, err := cluster.GetNetworks()
|
|
|
|
if err != nil {
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
networkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`)
|
|
|
|
for _, nw := range networks {
|
|
|
|
if nw.Name == "ingress" {
|
|
|
|
continue
|
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && nw.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
// 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 network %s: %v", nw.Name, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nw.Name)
|
|
|
|
}
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworksPrune removes unused networks
|
2016-11-17 00:46:37 -05:00
|
|
|
func (daemon *Daemon) NetworksPrune(pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
|
2016-10-18 00:36:52 -04:00
|
|
|
rep := &types.NetworksPruneReport{}
|
2016-11-17 00:46:37 -05:00
|
|
|
clusterRep, err := daemon.clusterNetworksPrune(pruneFilters)
|
2016-10-18 00:36:52 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not remove cluster networks: %v", err)
|
|
|
|
} else {
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, clusterRep.NetworksDeleted...)
|
|
|
|
}
|
2016-11-17 00:46:37 -05:00
|
|
|
localRep, err := daemon.localNetworksPrune(pruneFilters)
|
2016-10-18 00:36:52 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not remove local networks: %v", err)
|
|
|
|
} else {
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, localRep.NetworksDeleted...)
|
|
|
|
}
|
|
|
|
return rep, err
|
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
|
|
|
func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {
|
|
|
|
until := time.Time{}
|
|
|
|
if !pruneFilters.Include("until") {
|
|
|
|
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
|
|
|
|
}
|