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-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-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-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
|
|
|
|
manifest := []byte{}
|
|
|
|
config := []byte{}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
if options.FromImage == BaseImageFakeName {
|
|
|
|
options.FromImage = ""
|
|
|
|
}
|
2017-02-11 00:48:15 +08:00
|
|
|
image := options.FromImage
|
|
|
|
|
2017-08-23 18:47:13 +08:00
|
|
|
if options.Transport == "" {
|
|
|
|
options.Transport = DefaultTransport
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:48:15 +08:00
|
|
|
systemContext := getSystemContext(options.SignaturePolicyPath)
|
|
|
|
|
2017-03-16 05:19:29 +08:00
|
|
|
imageID := ""
|
2017-02-11 03:45:06 +08:00
|
|
|
if image != "" {
|
2017-10-20 05:47:15 +08:00
|
|
|
var err error
|
2017-04-11 02:15:30 +08:00
|
|
|
if options.PullPolicy == PullAlways {
|
2017-07-29 05:29:37 +08:00
|
|
|
pulledReference, err2 := pullImage(store, options, systemContext)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, errors.Wrapf(err2, "error pulling image %q", image)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
ref = pulledReference
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-07-29 05:29:37 +08:00
|
|
|
if ref == nil {
|
|
|
|
srcRef, err2 := alltransports.ParseImageName(image)
|
|
|
|
if err2 != nil {
|
|
|
|
srcRef2, err3 := alltransports.ParseImageName(options.Registry + image)
|
|
|
|
if err3 != nil {
|
2017-07-31 22:44:21 +08:00
|
|
|
srcRef3, err4 := alltransports.ParseImageName(options.Transport + options.Registry + image)
|
|
|
|
if err4 != nil {
|
|
|
|
return nil, errors.Wrapf(err4, "error parsing image name %q", options.Transport+options.Registry+image)
|
|
|
|
}
|
|
|
|
srcRef2 = srcRef3
|
2017-07-29 05:29:37 +08:00
|
|
|
}
|
|
|
|
srcRef = srcRef2
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err = is.Transport.ParseStoreReference(store, destImage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing reference to image %q", destImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
image = 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 {
|
|
|
|
return nil, errors.Wrapf(err, "no such image %q", transports.ImageName(ref))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-09-01 01:53:13 +08:00
|
|
|
ref2, err2 := pullImage(store, options, systemContext)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, errors.Wrapf(err2, "error pulling image %q", image)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-09-01 01:53:13 +08:00
|
|
|
ref = ref2
|
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
|
|
|
return nil, errors.Wrapf(err, "no such image %q", transports.ImageName(ref))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-03-16 05:19:29 +08:00
|
|
|
imageID = img.ID
|
2017-02-11 00:48:15 +08:00
|
|
|
src, err := ref.NewImage(systemContext)
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error instantiating image for %q", transports.ImageName(ref))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
config, err = src.ConfigBlob()
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error reading image configuration for %q", transports.ImageName(ref))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
manifest, _, err = src.Manifest()
|
|
|
|
if err != nil {
|
2017-07-29 05:29:37 +08:00
|
|
|
return nil, errors.Wrapf(err, "error reading image manifest for %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 != "" {
|
|
|
|
prefix := image
|
|
|
|
s := strings.Split(prefix, "/")
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
name = prefix + "-" + name
|
|
|
|
}
|
|
|
|
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-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-10-20 05:47:15 +08:00
|
|
|
if err := reserveSELinuxLabels(store, container.ID); err != nil {
|
|
|
|
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
|
|
|
|
}
|