2017-02-11 00:48:15 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-24 04:18:40 +08:00
|
|
|
"strings"
|
2017-02-11 00:48:15 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
is "github.com/containers/image/storage"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-03-22 04:57:07 +08:00
|
|
|
"github.com/openshift/imagebuilder"
|
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-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func newBuilder(store storage.Store, options BuilderOptions) (*Builder, error) {
|
|
|
|
var img *storage.Image
|
|
|
|
manifest := []byte{}
|
|
|
|
config := []byte{}
|
|
|
|
|
|
|
|
name := "working-container"
|
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
|
|
|
|
if options.Container != "" {
|
|
|
|
name = options.Container
|
|
|
|
} else {
|
|
|
|
if image != "" {
|
2017-03-24 04:18:40 +08:00
|
|
|
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
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if name != "" {
|
|
|
|
var err error
|
|
|
|
suffix := 1
|
|
|
|
tmpName := name
|
|
|
|
for err != storage.ErrContainerUnknown {
|
2017-05-17 23:53:28 +08:00
|
|
|
_, err = store.Container(tmpName)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err == nil {
|
|
|
|
suffix++
|
|
|
|
tmpName = fmt.Sprintf("%s-%d", name, suffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name = tmpName
|
|
|
|
}
|
|
|
|
|
|
|
|
systemContext := getSystemContext(options.SignaturePolicyPath)
|
|
|
|
|
2017-03-16 05:19:29 +08:00
|
|
|
imageID := ""
|
2017-02-11 03:45:06 +08:00
|
|
|
if image != "" {
|
2017-04-11 02:15:30 +08:00
|
|
|
if options.PullPolicy == PullAlways {
|
2017-02-11 00:48:15 +08:00
|
|
|
err := pullImage(store, options, systemContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error pulling image %q: %v", image, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ref, err := is.Transport.ParseStoreReference(store, image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing reference to image %q: %v", image, err)
|
|
|
|
}
|
|
|
|
img, err = is.Transport.GetStoreImage(store, ref)
|
|
|
|
if err != nil {
|
2017-04-11 02:15:30 +08:00
|
|
|
if err == storage.ErrImageUnknown && options.PullPolicy != PullIfMissing {
|
2017-02-11 00:48:15 +08:00
|
|
|
return nil, fmt.Errorf("no such image %q: %v", image, err)
|
|
|
|
}
|
|
|
|
err = pullImage(store, options, systemContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error pulling image %q: %v", image, err)
|
|
|
|
}
|
|
|
|
ref, err = is.Transport.ParseStoreReference(store, image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing reference to image %q: %v", image, err)
|
|
|
|
}
|
|
|
|
img, err = is.Transport.GetStoreImage(store, ref)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("no such image %q: %v", image, err)
|
|
|
|
}
|
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 {
|
|
|
|
return nil, fmt.Errorf("error instantiating image: %v", err)
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
config, err = src.ConfigBlob()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading image configuration: %v", err)
|
|
|
|
}
|
|
|
|
manifest, _, err = src.Manifest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading image manifest: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("error creating container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err2 := store.DeleteContainer(container.ID); err != nil {
|
|
|
|
logrus.Errorf("error deleting container %q: %v", container.ID, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
builder := &Builder{
|
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
|
|
|
store: store,
|
|
|
|
Type: containerType,
|
|
|
|
FromImage: image,
|
|
|
|
FromImageID: imageID,
|
|
|
|
Config: config,
|
|
|
|
Manifest: manifest,
|
|
|
|
Container: name,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ImageAnnotations: map[string]string{},
|
|
|
|
ImageCreatedBy: "",
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Mount {
|
|
|
|
_, err = builder.Mount("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error mounting build container: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("error saving builder state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|