github.com/crquan/docker@v1.8.1/graph/history.go (about)

     1  package graph
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/image"
     9  	"github.com/docker/docker/utils"
    10  )
    11  
    12  // WalkHistory calls the handler function for each image in the
    13  // provided images lineage starting from immediate parent.
    14  func (graph *Graph) WalkHistory(img *image.Image, handler func(image.Image) error) (err error) {
    15  	currentImg := img
    16  	for currentImg != nil {
    17  		if handler != nil {
    18  			if err := handler(*currentImg); err != nil {
    19  				return err
    20  			}
    21  		}
    22  		currentImg, err = graph.GetParent(currentImg)
    23  		if err != nil {
    24  			return fmt.Errorf("Error while getting parent image: %v", err)
    25  		}
    26  	}
    27  	return nil
    28  }
    29  
    30  // depth returns the number of parents for a
    31  // current image
    32  func (graph *Graph) depth(img *image.Image) (int, error) {
    33  	var (
    34  		count  = 0
    35  		parent = img
    36  		err    error
    37  	)
    38  
    39  	for parent != nil {
    40  		count++
    41  		parent, err = graph.GetParent(parent)
    42  		if err != nil {
    43  			return -1, err
    44  		}
    45  	}
    46  	return count, nil
    47  }
    48  
    49  // Set the max depth to the aufs default that most
    50  // kernels are compiled with
    51  // For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
    52  const MaxImageDepth = 127
    53  
    54  // CheckDepth returns an error if the depth of an image, as returned
    55  // by ImageDepth, is too large to support creating a container from it
    56  // on this daemon.
    57  func (graph *Graph) CheckDepth(img *image.Image) error {
    58  	// We add 2 layers to the depth because the container's rw and
    59  	// init layer add to the restriction
    60  	depth, err := graph.depth(img)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	if depth+2 >= MaxImageDepth {
    65  		return fmt.Errorf("Cannot create container with more than %d parents", MaxImageDepth)
    66  	}
    67  	return nil
    68  }
    69  
    70  func (s *TagStore) History(name string) ([]*types.ImageHistory, error) {
    71  	foundImage, err := s.LookupImage(name)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	lookupMap := make(map[string][]string)
    77  	for name, repository := range s.Repositories {
    78  		for tag, id := range repository {
    79  			// If the ID already has a reverse lookup, do not update it unless for "latest"
    80  			if _, exists := lookupMap[id]; !exists {
    81  				lookupMap[id] = []string{}
    82  			}
    83  			lookupMap[id] = append(lookupMap[id], utils.ImageReference(name, tag))
    84  		}
    85  	}
    86  
    87  	history := []*types.ImageHistory{}
    88  
    89  	err = s.graph.WalkHistory(foundImage, func(img image.Image) error {
    90  		history = append(history, &types.ImageHistory{
    91  			ID:        img.ID,
    92  			Created:   img.Created.Unix(),
    93  			CreatedBy: strings.Join(img.ContainerConfig.Cmd.Slice(), " "),
    94  			Tags:      lookupMap[img.ID],
    95  			Size:      img.Size,
    96  			Comment:   img.Comment,
    97  		})
    98  		return nil
    99  	})
   100  
   101  	return history, err
   102  }
   103  
   104  func (graph *Graph) GetParent(img *image.Image) (*image.Image, error) {
   105  	if img.Parent == "" {
   106  		return nil, nil
   107  	}
   108  	return graph.Get(img.Parent)
   109  }
   110  
   111  func (graph *Graph) GetParentsSize(img *image.Image, size int64) int64 {
   112  	parentImage, err := graph.GetParent(img)
   113  	if err != nil || parentImage == nil {
   114  		return size
   115  	}
   116  	size += parentImage.Size
   117  	return graph.GetParentsSize(parentImage, size)
   118  }