2017-02-25 04:18:35 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2018-04-12 22:20:36 +08:00
|
|
|
"context"
|
|
|
|
|
2018-09-18 03:20:16 +08:00
|
|
|
"github.com/containers/buildah/docker"
|
|
|
|
"github.com/containers/buildah/util"
|
2019-07-02 05:14:07 +08:00
|
|
|
"github.com/containers/image/manifest"
|
2017-02-25 04:18:35 +08:00
|
|
|
is "github.com/containers/image/storage"
|
2017-05-19 05:38:41 +08:00
|
|
|
"github.com/containers/image/types"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-06-07 02:20:55 +08:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-06-02 03:23:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2017-02-25 04:18:35 +08:00
|
|
|
)
|
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilderDataFromImage(ctx context.Context, store storage.Store, systemContext *types.SystemContext, imageID, containerName, containerID string) (*Builder, error) {
|
2018-06-12 06:23:48 +08:00
|
|
|
if imageID == "" {
|
|
|
|
return nil, errors.Errorf("Internal error: imageID is empty in importBuilderDataFromImage")
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:23:56 +08:00
|
|
|
storeopts, err := storage.DefaultStoreOptions(false, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uidmap, gidmap := convertStorageIDMaps(storeopts.UIDMap, storeopts.GIDMap)
|
2017-02-25 04:18:35 +08:00
|
|
|
|
2018-06-12 06:23:48 +08:00
|
|
|
ref, err := is.Transport.ParseStoreReference(store, imageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "no such image %q", imageID)
|
|
|
|
}
|
|
|
|
src, err2 := ref.NewImage(ctx, systemContext)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, errors.Wrapf(err2, "error instantiating image")
|
|
|
|
}
|
|
|
|
defer src.Close()
|
2018-06-12 06:41:11 +08:00
|
|
|
|
2018-06-12 06:23:48 +08:00
|
|
|
imageName := ""
|
|
|
|
if img, err3 := store.Image(imageID); err3 == nil {
|
|
|
|
if len(img.Names) > 0 {
|
|
|
|
imageName = img.Names[0]
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
2018-06-12 06:23:48 +08:00
|
|
|
if img.TopLayer != "" {
|
|
|
|
layer, err4 := store.Layer(img.TopLayer)
|
|
|
|
if err4 != nil {
|
|
|
|
return nil, errors.Wrapf(err4, "error reading information about image's top layer")
|
2018-03-08 07:11:43 +08:00
|
|
|
}
|
2018-06-12 06:23:48 +08:00
|
|
|
uidmap, gidmap = convertStorageIDMaps(layer.UIDMap, layer.GIDMap)
|
2017-03-16 05:19:29 +08:00
|
|
|
}
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2019-07-02 05:14:07 +08:00
|
|
|
imageDigest := ""
|
|
|
|
if manifestBytes, _, err := src.Manifest(ctx); err == nil {
|
|
|
|
if manifestDigest, err := manifest.Digest(manifestBytes); err == nil {
|
|
|
|
imageDigest = manifestDigest.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 04:35:43 +08:00
|
|
|
defaultNamespaceOptions, err := DefaultNamespaceOptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-25 04:18:35 +08:00
|
|
|
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: imageName,
|
2017-05-19 05:38:41 +08:00
|
|
|
FromImageID: imageID,
|
2019-07-02 05:14:07 +08:00
|
|
|
FromImageDigest: imageDigest,
|
2017-05-19 05:38:41 +08:00
|
|
|
Container: containerName,
|
|
|
|
ContainerID: containerID,
|
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
|
|
|
ImageAnnotations: map[string]string{},
|
|
|
|
ImageCreatedBy: "",
|
2018-06-27 04:35:43 +08:00
|
|
|
NamespaceOptions: defaultNamespaceOptions,
|
2018-03-08 07:11:43 +08:00
|
|
|
IDMappingOptions: IDMappingOptions{
|
|
|
|
HostUIDMapping: len(uidmap) == 0,
|
|
|
|
HostGIDMapping: len(uidmap) == 0,
|
|
|
|
UIDMap: uidmap,
|
|
|
|
GIDMap: gidmap,
|
|
|
|
},
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 06:41:11 +08:00
|
|
|
if err := builder.initConfig(ctx, src); err != nil {
|
2018-06-12 05:43:21 +08:00
|
|
|
return nil, errors.Wrapf(err, "error preparing image configuration")
|
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilder(ctx context.Context, store storage.Store, options ImportOptions) (*Builder, error) {
|
2017-05-19 05:38:41 +08:00
|
|
|
if options.Container == "" {
|
2017-06-03 00:17:27 +08:00
|
|
|
return nil, errors.Errorf("container name must be specified")
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := store.Container(options.Container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 04:03:13 +08:00
|
|
|
systemContext := getSystemContext(store, &types.SystemContext{}, options.SignaturePolicyPath)
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
builder, err := importBuilderDataFromImage(ctx, store, systemContext, c.ImageID, options.Container, c.ID)
|
2017-05-19 05:38:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:20:55 +08:00
|
|
|
if builder.FromImageID != "" {
|
|
|
|
if d, err2 := digest.Parse(builder.FromImageID); err2 == nil {
|
|
|
|
builder.Docker.Parent = docker.ID(d)
|
|
|
|
} else {
|
|
|
|
builder.Docker.Parent = docker.ID(digest.NewDigestFromHex(digest.Canonical.String(), builder.FromImageID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if builder.FromImage != "" {
|
|
|
|
builder.Docker.ContainerConfig.Image = builder.FromImage
|
|
|
|
}
|
2018-03-08 07:11:43 +08:00
|
|
|
builder.IDMappingOptions.UIDMap, builder.IDMappingOptions.GIDMap = convertStorageIDMaps(c.UIDMap, c.GIDMap)
|
2017-06-07 02:20:55 +08:00
|
|
|
|
2017-02-25 04:18:35 +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-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilderFromImage(ctx context.Context, store storage.Store, options ImportFromImageOptions) (*Builder, error) {
|
2017-05-19 05:38:41 +08:00
|
|
|
if options.Image == "" {
|
2017-06-03 00:17:27 +08:00
|
|
|
return nil, errors.Errorf("image name must be specified")
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 04:03:13 +08:00
|
|
|
systemContext := getSystemContext(store, options.SystemContext, options.SignaturePolicyPath)
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-05-02 03:37:13 +08:00
|
|
|
_, img, err := util.FindImage(store, "", systemContext, options.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error locating image %q for importing settings", options.Image)
|
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-05-02 03:37:13 +08:00
|
|
|
builder, err := importBuilderDataFromImage(ctx, store, systemContext, img.ID, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error importing build settings from image %q", options.Image)
|
2017-06-29 05:07:58 +08:00
|
|
|
}
|
2018-05-02 03:37:13 +08:00
|
|
|
|
|
|
|
return builder, nil
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|