2017-02-11 00:48:15 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-17 03:48:21 +08:00
|
|
|
"os"
|
2017-03-24 04:18:40 +08:00
|
|
|
"strings"
|
2017-02-11 00:48:15 +08:00
|
|
|
|
|
|
|
is "github.com/containers/image/storage"
|
2017-07-29 05:29:37 +08:00
|
|
|
"github.com/containers/image/transports"
|
|
|
|
"github.com/containers/image/transports/alltransports"
|
|
|
|
"github.com/containers/image/types"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-10-20 05:47:15 +08:00
|
|
|
"github.com/opencontainers/selinux/go-selinux"
|
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2017-03-22 04:57:07 +08:00
|
|
|
"github.com/openshift/imagebuilder"
|
2017-06-02 03:23:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2017-06-29 05:07:58 +08:00
|
|
|
"github.com/projectatomic/buildah/util"
|
2017-10-10 03:05:56 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-11 03:45:06 +08:00
|
|
|
// BaseImageFakeName is the "name" of a source image which we interpret
|
|
|
|
// as "no image".
|
2017-03-22 04:57:07 +08:00
|
|
|
BaseImageFakeName = imagebuilder.NoBaseImageSpecifier
|
2017-08-23 18:47:13 +08:00
|
|
|
|
|
|
|
// DefaultTransport is a prefix that we apply to an image name if we
|
|
|
|
// can't find one in the local Store, in order to generate a source
|
|
|
|
// reference for the image that we can then copy to the local Store.
|
|
|
|
DefaultTransport = "docker://"
|
2017-06-29 05:07:58 +08:00
|
|
|
|
|
|
|
// minimumTruncatedIDLength is the minimum length of an identifier that
|
|
|
|
// we'll accept as possibly being a truncated image ID.
|
|
|
|
minimumTruncatedIDLength = 3
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
2017-10-20 05:47:15 +08:00
|
|
|
func reserveSELinuxLabels(store storage.Store, id string) error {
|
|
|
|
if selinux.GetEnabled() {
|
|
|
|
containers, err := store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
if id == c.ID {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
b, err := OpenBuilder(store, c.ID)
|
|
|
|
if err != nil {
|
2017-11-17 03:48:21 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Ignore not exist errors since containers probably created by other tool
|
|
|
|
// TODO, we need to read other containers json data to reserve their SELinux labels
|
2017-10-20 05:47:15 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Prevent containers from using same MCS Label
|
|
|
|
if err := label.ReserveLabel(b.ProcessLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
func pullAndFindImage(store storage.Store, imageName string, options BuilderOptions, sc *types.SystemContext) (*storage.Image, types.ImageReference, error) {
|
|
|
|
ref, err := pullImage(store, imageName, options, sc)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error pulling image %q: %v", imageName, err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
img, err := is.Transport.GetStoreImage(store, ref)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error reading pulled image %q: %v", imageName, err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return img, ref, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:04:09 +08:00
|
|
|
func getImageName(name string, img *storage.Image) string {
|
|
|
|
imageName := name
|
|
|
|
if len(img.Names) > 0 {
|
|
|
|
imageName = img.Names[0]
|
|
|
|
// When the image used by the container is a tagged image
|
|
|
|
// the container name might be set to the original image instead of
|
|
|
|
// the image given in the "form" command line.
|
|
|
|
// This loop is supposed to fix this.
|
|
|
|
for _, n := range img.Names {
|
|
|
|
if strings.Contains(n, name) {
|
|
|
|
imageName = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return imageName
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
func imageNamePrefix(imageName string) string {
|
|
|
|
prefix := imageName
|
|
|
|
s := strings.Split(imageName, "/")
|
|
|
|
if len(s) > 0 {
|
|
|
|
prefix = s[len(s)-1]
|
|
|
|
}
|
|
|
|
s = strings.Split(prefix, ":")
|
|
|
|
if len(s) > 0 {
|
|
|
|
prefix = s[0]
|
|
|
|
}
|
|
|
|
s = strings.Split(prefix, "@")
|
|
|
|
if len(s) > 0 {
|
|
|
|
prefix = s[0]
|
|
|
|
}
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageManifestAndConfig(ref types.ImageReference, systemContext *types.SystemContext) (manifest, config []byte, err error) {
|
|
|
|
if ref != nil {
|
|
|
|
src, err := ref.NewImage(systemContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "error instantiating image for %q", transports.ImageName(ref))
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
config, err := src.ConfigBlob()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "error reading image configuration for %q", transports.ImageName(ref))
|
|
|
|
}
|
|
|
|
manifest, _, err := src.Manifest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "error reading image manifest for %q", transports.ImageName(ref))
|
|
|
|
}
|
|
|
|
return manifest, config, nil
|
|
|
|
}
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:48:15 +08:00
|
|
|
func newBuilder(store storage.Store, options BuilderOptions) (*Builder, error) {
|
2017-07-29 05:29:37 +08:00
|
|
|
var ref types.ImageReference
|
2017-02-11 00:48:15 +08:00
|
|
|
var img *storage.Image
|
2017-06-29 05:07:58 +08:00
|
|
|
var err error
|
|
|
|
var manifest []byte
|
|
|
|
var config []byte
|
2017-02-11 00:48:15 +08:00
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
if options.FromImage == BaseImageFakeName {
|
|
|
|
options.FromImage = ""
|
|
|
|
}
|
2017-08-23 18:47:13 +08:00
|
|
|
if options.Transport == "" {
|
|
|
|
options.Transport = DefaultTransport
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
systemContext := getSystemContext(options.SystemContext, options.SignaturePolicyPath)
|
|
|
|
|
|
|
|
for _, image := range util.ResolveName(options.FromImage, options.Registry, systemContext, store) {
|
|
|
|
if len(image) >= minimumTruncatedIDLength {
|
|
|
|
if img, err = store.Image(image); err == nil && img != nil && strings.HasPrefix(img.ID, image) {
|
|
|
|
if ref, err = is.Transport.ParseStoreReference(store, img.ID); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing reference to image %q", img.ID)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 00:48:15 +08:00
|
|
|
|
2017-04-11 02:15:30 +08:00
|
|
|
if options.PullPolicy == PullAlways {
|
2017-06-29 05:07:58 +08:00
|
|
|
pulledImg, pulledReference, err2 := pullAndFindImage(store, image, options, systemContext)
|
2017-07-29 05:29:37 +08:00
|
|
|
if err2 != nil {
|
2017-06-29 05:07:58 +08:00
|
|
|
logrus.Debugf("error pulling and reading image %q: %v", image, err2)
|
2018-01-23 23:27:32 +08:00
|
|
|
err = err2
|
2017-06-29 05:07:58 +08:00
|
|
|
continue
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
ref = pulledReference
|
2017-06-29 05:07:58 +08:00
|
|
|
img = pulledImg
|
|
|
|
break
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
srcRef, err2 := alltransports.ParseImageName(image)
|
|
|
|
if err2 != nil {
|
|
|
|
if options.Transport == "" {
|
|
|
|
logrus.Debugf("error parsing image name %q: %v", image, err2)
|
2018-01-23 23:27:32 +08:00
|
|
|
err = err2
|
2017-06-29 05:07:58 +08:00
|
|
|
continue
|
2017-07-29 05:29:37 +08:00
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
srcRef2, err3 := alltransports.ParseImageName(options.Transport + image)
|
|
|
|
if err3 != nil {
|
|
|
|
logrus.Debugf("error parsing image name %q: %v", image, err2)
|
2018-01-23 23:27:32 +08:00
|
|
|
err = err3
|
2017-06-29 05:07:58 +08:00
|
|
|
continue
|
2017-07-29 05:29:37 +08:00
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
srcRef = srcRef2
|
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
destImage, err2 := localImageNameForReference(store, srcRef)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, errors.Wrapf(err2, "error computing local image name for %q", transports.ImageName(srcRef))
|
|
|
|
}
|
|
|
|
if destImage == "" {
|
|
|
|
return nil, errors.Errorf("error computing local image name for %q", transports.ImageName(srcRef))
|
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
ref, err = is.Transport.ParseStoreReference(store, destImage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing reference to image %q", destImage)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
img, err = is.Transport.GetStoreImage(store, ref)
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
if errors.Cause(err) == storage.ErrImageUnknown && options.PullPolicy != PullIfMissing {
|
2017-06-29 05:07:58 +08:00
|
|
|
logrus.Debugf("no such image %q: %v", transports.ImageName(ref), err)
|
|
|
|
continue
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
pulledImg, pulledReference, err2 := pullAndFindImage(store, image, options, systemContext)
|
2017-09-01 01:53:13 +08:00
|
|
|
if err2 != nil {
|
2017-06-29 05:07:58 +08:00
|
|
|
logrus.Debugf("error pulling and reading image %q: %v", image, err2)
|
2018-01-23 23:27:32 +08:00
|
|
|
err = err2
|
2017-06-29 05:07:58 +08:00
|
|
|
continue
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
ref = pulledReference
|
|
|
|
img = pulledImg
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.FromImage != "" && (ref == nil || img == nil) {
|
2018-01-23 23:27:32 +08:00
|
|
|
// If options.FromImage is set but we ended up
|
|
|
|
// with nil in ref or in img then there was an error that
|
|
|
|
// we should return.
|
|
|
|
return nil, util.GetFailureCause(err, errors.Wrapf(storage.ErrImageUnknown, "no such image %q", options.FromImage))
|
2017-06-29 05:07:58 +08:00
|
|
|
}
|
|
|
|
image := options.FromImage
|
|
|
|
imageID := ""
|
|
|
|
if img != nil {
|
2018-01-29 09:08:06 +08:00
|
|
|
image = getImageName(imageNamePrefix(image), img)
|
2017-03-16 05:19:29 +08:00
|
|
|
imageID = img.ID
|
2017-06-29 05:07:58 +08:00
|
|
|
}
|
|
|
|
if manifest, config, err = imageManifestAndConfig(ref, systemContext); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading data from image %q", transports.ImageName(ref))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-07-29 05:29:37 +08:00
|
|
|
name := "working-container"
|
|
|
|
if options.Container != "" {
|
|
|
|
name = options.Container
|
|
|
|
} else {
|
|
|
|
var err2 error
|
|
|
|
if image != "" {
|
2017-06-29 05:07:58 +08:00
|
|
|
name = imageNamePrefix(image) + "-" + name
|
2017-07-29 05:29:37 +08:00
|
|
|
}
|
|
|
|
suffix := 1
|
|
|
|
tmpName := name
|
|
|
|
for errors.Cause(err2) != storage.ErrContainerUnknown {
|
|
|
|
_, err2 = store.Container(tmpName)
|
|
|
|
if err2 == nil {
|
|
|
|
suffix++
|
|
|
|
tmpName = fmt.Sprintf("%s-%d", name, suffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name = tmpName
|
|
|
|
}
|
2017-06-29 05:07:58 +08:00
|
|
|
|
2017-02-11 00:48:15 +08:00
|
|
|
coptions := storage.ContainerOptions{}
|
2017-03-16 05:19:29 +08:00
|
|
|
container, err := store.CreateContainer("", []string{name}, imageID, "", "", &coptions)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2017-06-02 03:23:02 +08:00
|
|
|
return nil, errors.Wrapf(err, "error creating container")
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err2 := store.DeleteContainer(container.ID); err != nil {
|
|
|
|
logrus.Errorf("error deleting container %q: %v", container.ID, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-06-29 05:07:58 +08:00
|
|
|
if err = reserveSELinuxLabels(store, container.ID); err != nil {
|
2017-10-20 05:47:15 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
processLabel, mountLabel, err := label.InitLabels(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:48:15 +08:00
|
|
|
builder := &Builder{
|
2017-11-08 06:44:24 +08:00
|
|
|
store: store,
|
|
|
|
Type: containerType,
|
|
|
|
FromImage: image,
|
|
|
|
FromImageID: imageID,
|
|
|
|
Config: config,
|
|
|
|
Manifest: manifest,
|
|
|
|
Container: name,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ImageAnnotations: map[string]string{},
|
|
|
|
ImageCreatedBy: "",
|
|
|
|
ProcessLabel: processLabel,
|
|
|
|
MountLabel: mountLabel,
|
|
|
|
DefaultMountsFilePath: options.DefaultMountsFilePath,
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Mount {
|
2017-10-20 05:47:15 +08:00
|
|
|
_, err = builder.Mount(mountLabel)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2017-06-02 03:23:02 +08:00
|
|
|
return nil, errors.Wrapf(err, "error mounting build container")
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
builder.initConfig()
|
2017-02-11 00:48:15 +08:00
|
|
|
err = builder.Save()
|
|
|
|
if err != nil {
|
2017-06-02 03:23:02 +08:00
|
|
|
return nil, errors.Wrapf(err, "error saving builder state")
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|