2017-02-11 00:48:15 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2017-07-29 05:29:37 +08:00
|
|
|
"strings"
|
|
|
|
|
2017-02-11 00:48:15 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-06-05 21:42:30 +08:00
|
|
|
cp "github.com/containers/image/copy"
|
2017-02-14 00:44:47 +08:00
|
|
|
"github.com/containers/image/docker/reference"
|
2017-02-11 00:48:15 +08:00
|
|
|
"github.com/containers/image/signature"
|
|
|
|
is "github.com/containers/image/storage"
|
2017-07-29 05:29:37 +08:00
|
|
|
"github.com/containers/image/transports"
|
2017-03-22 04:38:50 +08:00
|
|
|
"github.com/containers/image/transports/alltransports"
|
2017-02-11 00:48:15 +08:00
|
|
|
"github.com/containers/image/types"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-06-02 03:23:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
2017-07-29 05:29:37 +08:00
|
|
|
func localImageNameForReference(store storage.Store, srcRef types.ImageReference) (string, error) {
|
|
|
|
if srcRef == nil {
|
|
|
|
return "", errors.Errorf("reference to image is empty")
|
|
|
|
}
|
|
|
|
ref := srcRef.DockerReference()
|
|
|
|
if ref == nil {
|
|
|
|
name := srcRef.StringWithinTransport()
|
|
|
|
_, err := is.Transport.ParseStoreReference(store, name)
|
|
|
|
if err == nil {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
if strings.LastIndex(name, "/") != -1 {
|
|
|
|
name = name[strings.LastIndex(name, "/")+1:]
|
|
|
|
_, err = is.Transport.ParseStoreReference(store, name)
|
|
|
|
if err == nil {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("reference to image %q is not a named reference", transports.ImageName(srcRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
name := ""
|
|
|
|
if named, ok := ref.(reference.Named); ok {
|
|
|
|
name = named.Name()
|
|
|
|
if namedTagged, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
name = name + ":" + namedTagged.Tag()
|
|
|
|
}
|
|
|
|
if canonical, ok := ref.(reference.Canonical); ok {
|
|
|
|
name = name + "@" + canonical.Digest().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := is.Transport.ParseStoreReference(store, name); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "error parsing computed local image name %q", name)
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pullImage(store storage.Store, options BuilderOptions, sc *types.SystemContext) (types.ImageReference, error) {
|
2017-02-11 00:48:15 +08:00
|
|
|
name := options.FromImage
|
|
|
|
|
|
|
|
spec := name
|
|
|
|
if options.Registry != "" {
|
|
|
|
spec = options.Registry + spec
|
|
|
|
}
|
2017-07-31 22:44:21 +08:00
|
|
|
spec2 := spec
|
|
|
|
if options.Transport != "" {
|
|
|
|
spec2 = options.Transport + spec
|
|
|
|
}
|
2017-02-11 00:48:15 +08:00
|
|
|
|
2017-03-22 04:38:50 +08:00
|
|
|
srcRef, err := alltransports.ParseImageName(name)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2017-03-22 04:38:50 +08:00
|
|
|
srcRef2, err2 := alltransports.ParseImageName(spec)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err2 != nil {
|
2017-07-31 22:44:21 +08:00
|
|
|
srcRef3, err3 := alltransports.ParseImageName(spec2)
|
|
|
|
if err3 != nil {
|
|
|
|
return nil, errors.Wrapf(err3, "error parsing image name %q", spec2)
|
|
|
|
}
|
|
|
|
srcRef2 = srcRef3
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
srcRef = srcRef2
|
|
|
|
}
|
|
|
|
|
2017-07-29 05:29:37 +08:00
|
|
|
destName, err := localImageNameForReference(store, srcRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error computing local image name for %q", transports.ImageName(srcRef))
|
|
|
|
}
|
|
|
|
if destName == "" {
|
|
|
|
return nil, errors.Errorf("error computing local image name for %q", transports.ImageName(srcRef))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-07-29 05:29:37 +08:00
|
|
|
destRef, err := is.Transport.ParseStoreReference(store, destName)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error parsing image name %q", destName)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := signature.DefaultPolicy(sc)
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error obtaining default signature policy")
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
policyContext, err := signature.NewPolicyContext(policy)
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error creating new signature policy context")
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-07-31 22:44:21 +08:00
|
|
|
logrus.Debugf("copying %q to %q", transports.ImageName(srcRef), transports.ImageName(destRef))
|
2017-02-11 00:48:15 +08:00
|
|
|
|
2017-06-05 21:42:30 +08:00
|
|
|
err = cp.Image(policyContext, destRef, srcRef, getCopyOptions(options.ReportWriter))
|
2017-07-29 05:29:37 +08:00
|
|
|
return destRef, err
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|