2016-05-21 20:36:11 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-20 23:11:28 +00:00
|
|
|
"time"
|
2016-05-21 20:36:11 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-11-09 21:32:53 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2016-05-21 20:36:11 +00:00
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImageHistory returns a slice of ImageHistory structures for the specified image
|
|
|
|
// name by walking the image lineage.
|
2016-11-09 21:32:53 +00:00
|
|
|
func (daemon *Daemon) ImageHistory(name string) ([]*image.HistoryResponseItem, error) {
|
2016-07-20 23:11:28 +00:00
|
|
|
start := time.Now()
|
2016-05-21 20:36:11 +00:00
|
|
|
img, err := daemon.GetImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:32:53 +00:00
|
|
|
history := []*image.HistoryResponseItem{}
|
2016-05-21 20:36:11 +00:00
|
|
|
|
|
|
|
layerCounter := 0
|
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
|
|
|
|
for _, h := range img.History {
|
|
|
|
var layerSize int64
|
|
|
|
|
|
|
|
if !h.EmptyLayer {
|
|
|
|
if len(img.RootFS.DiffIDs) <= layerCounter {
|
|
|
|
return nil, fmt.Errorf("too many non-empty layers in History section")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootFS.Append(img.RootFS.DiffIDs[layerCounter])
|
|
|
|
l, err := daemon.layerStore.Get(rootFS.ChainID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layerSize, err = l.DiffSize()
|
|
|
|
layer.ReleaseAndLog(daemon.layerStore, l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
layerCounter++
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:32:53 +00:00
|
|
|
history = append([]*image.HistoryResponseItem{{
|
2016-05-21 20:36:11 +00:00
|
|
|
ID: "<missing>",
|
|
|
|
Created: h.Created.Unix(),
|
|
|
|
CreatedBy: h.CreatedBy,
|
|
|
|
Comment: h.Comment,
|
|
|
|
Size: layerSize,
|
|
|
|
}}, history...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in image IDs and tags
|
|
|
|
histImg := img
|
|
|
|
id := img.ID()
|
|
|
|
for _, h := range history {
|
|
|
|
h.ID = id.String()
|
|
|
|
|
|
|
|
var tags []string
|
2016-09-15 23:37:32 +00:00
|
|
|
for _, r := range daemon.referenceStore.References(id.Digest()) {
|
2016-05-21 20:36:11 +00:00
|
|
|
if _, ok := r.(reference.NamedTagged); ok {
|
2017-01-26 00:54:18 +00:00
|
|
|
tags = append(tags, reference.FamiliarString(r))
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Tags = tags
|
|
|
|
|
|
|
|
id = histImg.Parent
|
|
|
|
if id == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
histImg, err = daemon.GetImage(id.String())
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 23:11:28 +00:00
|
|
|
imageActions.WithValues("history").UpdateSince(start)
|
2016-05-21 20:36:11 +00:00
|
|
|
return history, nil
|
|
|
|
}
|