2017-07-10 00:33:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-08-14 19:32:00 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2017-07-10 00:33:50 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
is "github.com/containers/image/storage"
|
|
|
|
"github.com/containers/storage"
|
|
|
|
units "github.com/docker/go-units"
|
2017-08-14 19:32:00 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/cmd/kpod/formats"
|
2017-07-23 23:01:37 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod/common"
|
2017-07-10 00:33:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-10 00:33:50 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
createdByTruncLength = 45
|
|
|
|
idTruncLength = 13
|
|
|
|
)
|
|
|
|
|
|
|
|
// historyOutputParams stores info about each layer
|
|
|
|
type historyOutputParams struct {
|
2017-08-14 19:32:00 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Created string `json:"created"`
|
|
|
|
CreatedBy string `json:"createdby"`
|
|
|
|
Size string `json:"size"`
|
|
|
|
Comment string `json:"comment"`
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// historyOptions stores cli flag values
|
|
|
|
type historyOptions struct {
|
2017-08-14 19:32:00 +00:00
|
|
|
image string
|
|
|
|
human bool
|
|
|
|
truncate bool
|
|
|
|
quiet bool
|
|
|
|
format string
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
historyFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "human, H",
|
|
|
|
Usage: "Display sizes and dates in human readable format",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2017-08-14 19:32:00 +00:00
|
|
|
Name: "no-trunc, notruncate",
|
2017-07-10 00:33:50 +00:00
|
|
|
Usage: "Do not truncate the output",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet, q",
|
|
|
|
Usage: "Display the numeric IDs only",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format",
|
2017-08-14 19:32:00 +00:00
|
|
|
Usage: "Change the output to JSON or a Go template",
|
2017-07-10 00:33:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
historyDescription = "Displays the history of an image. The information can be printed out in an easy to read, " +
|
|
|
|
"or user specified format, and can be truncated."
|
|
|
|
historyCommand = cli.Command{
|
|
|
|
Name: "history",
|
|
|
|
Usage: "Show history of a specified image",
|
|
|
|
Description: historyDescription,
|
|
|
|
Flags: historyFlags,
|
|
|
|
Action: historyCmd,
|
|
|
|
ArgsUsage: "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func historyCmd(c *cli.Context) error {
|
2017-07-27 17:18:07 +00:00
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Could not get config")
|
|
|
|
}
|
|
|
|
store, err := getStore(config)
|
2017-07-10 00:33:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
human := true
|
|
|
|
if c.IsSet("human") {
|
|
|
|
human = c.Bool("human")
|
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
|
|
|
|
truncate := true
|
2017-07-10 00:33:50 +00:00
|
|
|
if c.IsSet("no-trunc") {
|
2017-08-14 19:32:00 +00:00
|
|
|
truncate = !c.Bool("no-trunc")
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
|
|
|
quiet := false
|
|
|
|
if c.IsSet("quiet") {
|
|
|
|
quiet = c.Bool("quiet")
|
|
|
|
}
|
|
|
|
format := ""
|
|
|
|
if c.IsSet("format") {
|
|
|
|
format = c.String("format")
|
|
|
|
}
|
|
|
|
args := c.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
logrus.Errorf("an image name must be specified")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
|
|
|
logrus.Errorf("Kpod history takes at most 1 argument")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
imgName := args[0]
|
|
|
|
|
|
|
|
opts := historyOptions{
|
2017-08-14 19:32:00 +00:00
|
|
|
image: imgName,
|
|
|
|
human: human,
|
|
|
|
truncate: truncate,
|
|
|
|
quiet: quiet,
|
|
|
|
format: format,
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
err = outputHistory(store, opts)
|
2017-07-10 00:33:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
func genHistoryFormat(quiet, truncate, human bool) (format string) {
|
|
|
|
if quiet {
|
|
|
|
return "{{.ID}}"
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
if truncate {
|
|
|
|
format = "table {{ .ID | printf \"%-12.12s\" }} {{ .Created | printf \"%-16s\" }} {{ .CreatedBy | " +
|
|
|
|
"printf \"%-45.45s\" }} {{ .Size | printf \"%-16s\" }} {{ .Comment | printf \"%s\" }}"
|
2017-07-10 00:33:50 +00:00
|
|
|
} else {
|
2017-08-14 19:32:00 +00:00
|
|
|
format = "table {{ .ID | printf \"%-64s\" }} {{ .Created | printf \"%-18s\" }} {{ .CreatedBy | " +
|
|
|
|
"printf \"%-60s\" }} {{ .Size | printf \"%-16s\" }} {{ .Comment | printf \"%s\"}}"
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
return
|
2017-07-10 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// outputTime displays the time stamp in "2017-06-20T20:24:10Z" format
|
|
|
|
func outputTime(tm *time.Time) string {
|
|
|
|
return tm.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputHumanTime displays the time elapsed since creation
|
|
|
|
func outputHumanTime(tm *time.Time) string {
|
|
|
|
return units.HumanDuration(time.Since(*tm))
|
|
|
|
}
|
|
|
|
|
|
|
|
// createJSON retrieves the history of the image and returns a JSON object
|
|
|
|
func createJSON(store storage.Store, opts historyOptions) ([]byte, error) {
|
|
|
|
var (
|
2017-08-14 19:32:00 +00:00
|
|
|
size int64
|
|
|
|
img *storage.Image
|
|
|
|
imageID string
|
|
|
|
layerAll []historyOutputParams
|
|
|
|
createdTime string
|
|
|
|
outputSize string
|
2017-07-10 00:33:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ref, err := is.Transport.ParseStoreReference(store, opts.image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("error parsing reference to image %q: %v", opts.image, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err = is.Transport.GetStoreImage(store, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("no such image %q: %v", opts.image, err)
|
|
|
|
}
|
|
|
|
|
2017-07-23 23:01:37 +00:00
|
|
|
systemContext := common.GetSystemContext("")
|
2017-07-10 00:33:50 +00:00
|
|
|
|
|
|
|
src, err := ref.NewImage(systemContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("error instantiating image %q: %v", opts.image, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oci, err := src.OCIConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
history := oci.History
|
|
|
|
layers := src.LayerInfos()
|
|
|
|
count := 1
|
|
|
|
// iterating backwards to get newwest to oldest
|
|
|
|
for i := len(history) - 1; i >= 0; i-- {
|
|
|
|
if i == len(history)-1 {
|
|
|
|
imageID = img.ID
|
|
|
|
} else {
|
|
|
|
imageID = "<missing>"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !history[i].EmptyLayer {
|
|
|
|
size = layers[len(layers)-count].Size
|
|
|
|
count++
|
|
|
|
} else {
|
|
|
|
size = 0
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
if opts.human {
|
|
|
|
createdTime = outputHumanTime(history[i].Created) + " ago"
|
|
|
|
outputSize = units.HumanSize(float64(size))
|
|
|
|
} else {
|
|
|
|
createdTime = outputTime(history[i].Created)
|
|
|
|
outputSize = strconv.FormatInt(size, 10)
|
|
|
|
}
|
2017-07-10 00:33:50 +00:00
|
|
|
params := historyOutputParams{
|
|
|
|
ID: imageID,
|
2017-08-14 19:32:00 +00:00
|
|
|
Created: createdTime,
|
2017-07-10 00:33:50 +00:00
|
|
|
CreatedBy: history[i].CreatedBy,
|
2017-08-14 19:32:00 +00:00
|
|
|
Size: outputSize,
|
2017-07-10 00:33:50 +00:00
|
|
|
Comment: history[i].Comment,
|
|
|
|
}
|
|
|
|
|
|
|
|
layerAll = append(layerAll, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := json.MarshalIndent(layerAll, "", "\t\t")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("error marshalling to JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = src.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
// historyToGeneric makes an empty array of interfaces for output
|
|
|
|
func historyToGeneric(params []historyOutputParams) []interface{} {
|
|
|
|
genericParams := make([]interface{}, len(params))
|
|
|
|
for i, v := range params {
|
|
|
|
genericParams[i] = interface{}(v)
|
|
|
|
}
|
|
|
|
return genericParams
|
|
|
|
}
|
|
|
|
|
2017-07-10 00:33:50 +00:00
|
|
|
// outputHistory gets the history of the image from the JSON object
|
|
|
|
// and pretty prints it to the screen
|
|
|
|
func outputHistory(store storage.Store, opts historyOptions) error {
|
|
|
|
var (
|
|
|
|
outputCreatedBy string
|
|
|
|
imageID string
|
|
|
|
history []historyOutputParams
|
|
|
|
)
|
|
|
|
|
|
|
|
raw, err := createJSON(store, opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("error creating JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(raw, &history); err != nil {
|
|
|
|
return errors.Errorf("error Unmarshalling JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
historyOutput := []historyOutputParams{}
|
|
|
|
|
|
|
|
historyFormat := opts.format
|
|
|
|
if historyFormat == "" {
|
|
|
|
historyFormat = genHistoryFormat(opts.quiet, opts.truncate, opts.human)
|
|
|
|
}
|
|
|
|
|
2017-07-10 00:33:50 +00:00
|
|
|
for i := 0; i < len(history); i++ {
|
|
|
|
imageID = history[i].ID
|
|
|
|
|
|
|
|
outputCreatedBy = strings.Join(strings.Fields(history[i].CreatedBy), " ")
|
2017-08-14 19:32:00 +00:00
|
|
|
if opts.truncate && len(outputCreatedBy) > createdByTruncLength {
|
2017-07-10 00:33:50 +00:00
|
|
|
outputCreatedBy = outputCreatedBy[:createdByTruncLength-3] + "..."
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
if opts.truncate && i == 0 {
|
2017-07-10 00:33:50 +00:00
|
|
|
imageID = history[i].ID[:idTruncLength]
|
|
|
|
}
|
|
|
|
|
|
|
|
params := historyOutputParams{
|
|
|
|
ID: imageID,
|
|
|
|
Created: history[i].Created,
|
|
|
|
CreatedBy: outputCreatedBy,
|
|
|
|
Size: history[i].Size,
|
|
|
|
Comment: history[i].Comment,
|
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
historyOutput = append(historyOutput, params)
|
|
|
|
}
|
2017-07-10 00:33:50 +00:00
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
var out formats.Writer
|
|
|
|
switch opts.format {
|
|
|
|
case formats.JSONString:
|
|
|
|
out = formats.JSONStructArray{Output: historyToGeneric(historyOutput)}
|
|
|
|
default:
|
|
|
|
out = formats.StdoutTemplateArray{Output: historyToGeneric(historyOutput), Template: historyFormat, Fields: historyOutput[0].headerMap()}
|
2017-07-10 00:33:50 +00:00
|
|
|
|
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
formats.Writer(out).Out()
|
2017-07-10 00:33:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
|
|
|
|
func (h *historyOutputParams) headerMap() map[string]string {
|
|
|
|
v := reflect.Indirect(reflect.ValueOf(h))
|
|
|
|
values := make(map[string]string)
|
|
|
|
for h := 0; h < v.NumField(); h++ {
|
|
|
|
key := v.Type().Field(h).Name
|
|
|
|
value := key
|
|
|
|
values[key] = fmt.Sprintf("%s ", strings.ToUpper(splitCamelCase(value)))
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|