Files
skopeo/new.go
Nalin Dahyabhai 20f2cb9dbe Fix a number of issues flagged by golint
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-02-10 14:46:09 -05:00

149 lines
3.7 KiB
Go

package buildah
import (
"fmt"
"github.com/Sirupsen/logrus"
is "github.com/containers/image/storage"
"github.com/containers/storage/storage"
)
const (
// BaseImageFakeName is the "name" of a source image which we interpret
// as "no image".
BaseImageFakeName = "scratch"
)
func newBuilder(store storage.Store, options BuilderOptions) (*Builder, error) {
var img *storage.Image
manifest := []byte{}
config := []byte{}
name := "working-container"
if options.FromImage == BaseImageFakeName {
options.FromImage = ""
}
image := options.FromImage
if options.Container != "" {
name = options.Container
} else {
if image != "" {
name = image + "-" + name
}
}
if name != "" {
var err error
suffix := 1
tmpName := name
for err != storage.ErrContainerUnknown {
_, err = store.GetContainer(tmpName)
if err == nil {
suffix++
tmpName = fmt.Sprintf("%s-%d", name, suffix)
}
}
name = tmpName
}
systemContext := getSystemContext(options.SignaturePolicyPath)
if image != "" {
if options.PullAlways {
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 {
if err == storage.ErrImageUnknown && !options.PullIfMissing {
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)
}
image = img.ID
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{}
container, err := store.CreateContainer("", []string{name}, image, "", "", &coptions)
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{
store: store,
Type: containerType,
FromImage: image,
Config: config,
Manifest: manifest,
Container: name,
ContainerID: container.ID,
Mounts: []string{},
Links: []string{},
Annotations: map[string]string{},
Env: []string{},
Cmd: []string{},
Entrypoint: []string{},
Expose: map[string]interface{}{},
Labels: map[string]string{},
Volumes: []string{},
Arg: map[string]string{},
}
if options.Mount {
_, err = builder.Mount("")
if err != nil {
return nil, fmt.Errorf("error mounting build container: %v", err)
}
if options.Link != "" {
err = builder.Link(options.Link)
if err != nil {
return nil, fmt.Errorf("error crerating link to build container root: %v", err)
}
}
}
err = builder.Save()
if err != nil {
return nil, fmt.Errorf("error saving builder state: %v", err)
}
return builder, nil
}