2017-06-27 13:45:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-16 17:24:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-06-30 19:10:57 +00:00
|
|
|
"io"
|
2017-06-16 17:24:00 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-06-30 19:10:57 +00:00
|
|
|
|
|
|
|
cp "github.com/containers/image/copy"
|
2017-06-27 13:45:25 +00:00
|
|
|
is "github.com/containers/image/storage"
|
2017-06-30 19:10:57 +00:00
|
|
|
"github.com/containers/image/types"
|
2017-06-27 13:45:25 +00:00
|
|
|
"github.com/containers/storage"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2017-06-16 17:24:00 +00:00
|
|
|
type imageMetadata struct {
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
CreatedTime time.Time `json:"created-time"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Blobs []types.BlobInfo `json:"blob-list"`
|
|
|
|
Layers map[string][]string `json:"layers"`
|
|
|
|
SignatureSizes []string `json:"signature-sizes"`
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:45:25 +00:00
|
|
|
func getStore(c *cli.Context) (storage.Store, error) {
|
|
|
|
options := storage.DefaultStoreOptions
|
2017-06-16 17:24:00 +00:00
|
|
|
if c.GlobalIsSet("root") {
|
2017-06-30 19:10:57 +00:00
|
|
|
options.GraphRoot = c.GlobalString("root")
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
if c.GlobalIsSet("runroot") {
|
2017-06-30 19:10:57 +00:00
|
|
|
options.RunRoot = c.GlobalString("runroot")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:45:25 +00:00
|
|
|
if c.GlobalIsSet("storage-driver") {
|
|
|
|
options.GraphDriverName = c.GlobalString("storage-driver")
|
|
|
|
}
|
|
|
|
if c.GlobalIsSet("storage-opt") {
|
|
|
|
opts := c.GlobalStringSlice("storage-opt")
|
|
|
|
if len(opts) > 0 {
|
|
|
|
options.GraphDriverOptions = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store, err := storage.GetStore(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
is.Transport.SetStore(store)
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findImage(store storage.Store, image string) (*storage.Image, error) {
|
|
|
|
var img *storage.Image
|
|
|
|
ref, err := is.Transport.ParseStoreReference(store, image)
|
|
|
|
if err == nil {
|
|
|
|
img, err = is.Transport.GetStoreImage(store, ref)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
img2, err2 := store.Image(image)
|
|
|
|
if err2 != nil {
|
|
|
|
if ref == nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing reference to image %q", image)
|
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "unable to locate image %q", image)
|
|
|
|
}
|
|
|
|
img = img2
|
|
|
|
}
|
|
|
|
return img, nil
|
2017-06-30 19:10:57 +00:00
|
|
|
}
|
2017-06-27 13:45:25 +00:00
|
|
|
|
2017-06-30 19:10:57 +00:00
|
|
|
func getCopyOptions(reportWriter io.Writer) *cp.Options {
|
|
|
|
return &cp.Options{
|
|
|
|
ReportWriter: reportWriter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSystemContext(signaturePolicyPath string) *types.SystemContext {
|
|
|
|
sc := &types.SystemContext{}
|
|
|
|
if signaturePolicyPath != "" {
|
|
|
|
sc.SignaturePolicyPath = signaturePolicyPath
|
|
|
|
}
|
|
|
|
return sc
|
2017-06-27 13:45:25 +00:00
|
|
|
}
|
2017-06-16 17:24:00 +00:00
|
|
|
|
|
|
|
func parseMetadata(image storage.Image) (imageMetadata, error) {
|
|
|
|
var im imageMetadata
|
|
|
|
|
|
|
|
dec := json.NewDecoder(strings.NewReader(image.Metadata))
|
|
|
|
if err := dec.Decode(&im); err != nil {
|
|
|
|
return imageMetadata{}, err
|
|
|
|
}
|
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSize(image storage.Image, store storage.Store) (int64, error) {
|
|
|
|
|
|
|
|
is.Transport.SetStore(store)
|
|
|
|
storeRef, err := is.Transport.ParseStoreReference(store, "@"+image.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
img, err := storeRef.NewImage(nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error with NewImage")
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
imgSize, err := img.Size()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting size")
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return imgSize, nil
|
|
|
|
}
|