1
0
mirror of https://github.com/containers/buildah.git synced 2025-04-19 18:02:18 +03:00
buildah/commit.go
Nalin Dahyabhai 3065ea3d16 Add a "tag" command
Add a "tag" command, for adding names to locally-stored images.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>

Closes: #110
Approved by: rhatdan
2017-05-19 19:59:47 +00:00

73 lines
2.3 KiB
Go

package buildah
import (
"io"
"github.com/Sirupsen/logrus"
"github.com/containers/image/copy"
"github.com/containers/image/signature"
"github.com/containers/image/storage"
"github.com/containers/image/types"
"github.com/containers/storage/pkg/archive"
"github.com/projectatomic/buildah/util"
)
// CommitOptions can be used to alter how an image is committed.
type CommitOptions struct {
// Compression specifies the type of compression which is applied to
// layer blobs. The default is to not use compression, but
// archive.Gzip is recommended.
Compression archive.Compression
// SignaturePolicyPath specifies an override location for the signature
// policy which should be used for verifying the new image as it is
// being written. Except in specific circumstances, no value should be
// specified, indicating that the shared, system-wide default policy
// should be used.
SignaturePolicyPath string
// AdditionalTags is a list of additional names to add to the image, if
// the transport to which we're writing the image gives us a way to add
// them.
AdditionalTags []string
// ReportWriter is an io.Writer which will be used to log the writing
// of the new image.
ReportWriter io.Writer
}
// Commit writes the contents of the container, along with its updated
// configuration, to a new image in the specified location, and if we know how,
// add any additional tags that were specified.
func (b *Builder) Commit(dest types.ImageReference, options CommitOptions) error {
policy, err := signature.DefaultPolicy(getSystemContext(options.SignaturePolicyPath))
if err != nil {
return err
}
policyContext, err := signature.NewPolicyContext(policy)
if err != nil {
return err
}
src, err := b.makeContainerImageRef(options.Compression)
if err != nil {
return err
}
err = copy.Image(policyContext, dest, src, getCopyOptions(options.ReportWriter))
if err != nil {
return err
}
if len(options.AdditionalTags) > 0 {
switch dest.Transport().Name() {
case storage.Transport.Name():
img, err := storage.Transport.GetStoreImage(b.store, dest)
if err != nil {
return err
}
err = util.AddImageNames(b.store, img, options.AdditionalTags)
if err != nil {
return err
}
default:
logrus.Warnf("don't know how to add tags to images stored in %q transport", dest.Transport().Name())
}
}
return nil
}