Skip to content

Commit

Permalink
Support merger of launch and cache layers
Browse files Browse the repository at this point in the history
* spec #28

Signed-off-by: Emily Casey <ecasey@pivotal.io>
Signed-off-by: Matthew McNew <mmcnew@pivotal.io>
  • Loading branch information
ekcasey authored and matthewmcnew committed Dec 11, 2018
1 parent 24efe0c commit 85da90b
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 97 deletions.
4 changes: 2 additions & 2 deletions acceptance/testdata/mock_buildpacks/first-latest/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ set -o errexit
set -o nounset
set -o pipefail

launch_dir=$4
launch_dir=$1

mkdir "$launch_dir/first-layer"
echo "Latest First Dep Contents" > "$launch_dir/first-layer/first-dep"
ln -snf "$launch_dir/first-layer/first-dep" first-dep

touch "$launch_dir/first-layer.toml"
echo "launch = true" > "$launch_dir/first-layer.toml"
echo 'processes = [{ type = "web", command = "./run"}]' > "$launch_dir/launch.toml"

echo "---> Done"
4 changes: 2 additions & 2 deletions acceptance/testdata/mock_buildpacks/first/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ set -o errexit
set -o nounset
set -o pipefail

launch_dir=$4
launch_dir=$1

mkdir "$launch_dir/first-layer"
echo "First Dep Contents" > "$launch_dir/first-layer/first-dep"
ln -snf "$launch_dir/first-layer/first-dep" first-dep

touch "$launch_dir/first-layer.toml"
echo "launch = true" > "$launch_dir/first-layer.toml"
echo 'processes = [{ type = "web", command = "./run"}]' > "$launch_dir/launch.toml"

ls -al
Expand Down
4 changes: 2 additions & 2 deletions acceptance/testdata/mock_buildpacks/second/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ set -o errexit
set -o nounset
set -o pipefail

launch_dir=$4
launch_dir=$1

mkdir "$launch_dir/second-layer"
echo "Second Dep Contents" > "$launch_dir/second-layer/second-dep"
ln -snf "$launch_dir/second-layer/second-dep" second-dep

touch "$launch_dir/second-layer.toml"
echo "launch = true" > "$launch_dir/second-layer.toml"
echo 'processes = [{ type = "web", command = "./run"}]' > "$launch_dir/launch.toml"

echo "---> Done"
4 changes: 2 additions & 2 deletions acceptance/testdata/mock_buildpacks/third/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ set -o errexit
set -o nounset
set -o pipefail

launch_dir=$4
launch_dir=$1

mkdir "$launch_dir/third-layer"
echo "Third Dep Contents" > "$launch_dir/third-layer/third-dep"
ln -snf "$launch_dir/third-layer/third-dep" third-dep

touch "$launch_dir/third-layer.toml"
echo "launch = true" > "$launch_dir/third-layer.toml"
echo 'processes = [{ type = "web", command = "./run"}]' > "$launch_dir/launch.toml"

ls -al
Expand Down
84 changes: 47 additions & 37 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"context"
"crypto/md5"
"fmt"
"github.com/buildpack/lifecycle/image"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"io"
"io/ioutil"
"log"
Expand All @@ -19,6 +16,10 @@ import (
"strings"
"time"

"github.com/buildpack/lifecycle/image"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"

"github.com/buildpack/pack/config"
"github.com/buildpack/pack/docker"
"github.com/buildpack/pack/fs"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/buildpack/lifecycle"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/google/uuid"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -69,8 +69,8 @@ type BuildConfig struct {
FS FS
Config *config.Config
// Above are copied from BuildFactory
WorkspaceVolume string
CacheVolume string
//WorkspaceVolume string
CacheVolume string
}

const (
Expand Down Expand Up @@ -140,7 +140,7 @@ func (bf *BuildFactory) BuildConfigFromFlags(f *BuildFlags) (*BuildConfig, error
Log: bf.Log,
FS: bf.FS,
Config: bf.Config,
WorkspaceVolume: fmt.Sprintf("pack-workspace-%x", uuid.New().String()),
//WorkspaceVolume: fmt.Sprintf("pack-workspace-%x", uuid.New().String()),
CacheVolume: fmt.Sprintf("pack-cache-%x", md5.Sum([]byte(appDir))),
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func Build(appDir, buildImage, runImage, repoName string, publish bool) error {
}

func (b *BuildConfig) Run() error {
defer b.Cli.VolumeRemove(context.Background(), b.WorkspaceVolume, true)
//defer b.Cli.VolumeRemove(context.Background(), b.WorkspaceVolume, true)

if err := b.Detect(); err != nil {
return err
Expand Down Expand Up @@ -323,7 +323,7 @@ func (b *BuildConfig) Detect() error {
},
}, &container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s:", b.WorkspaceVolume, launchDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, launchDir),
},
}, nil, "")
if err != nil {
Expand Down Expand Up @@ -357,19 +357,19 @@ func (b *BuildConfig) Detect() error {
orderToml = tomlBuilder.String()
}

uid, gid, err := b.packUidGid(b.Builder)
if err != nil {
return errors.Wrap(err, "detect")
}

tr, errChan := b.FS.CreateTarReader(b.AppDir, launchDir+"/app", uid, gid)
if err := b.Cli.CopyToContainer(ctx, ctr.ID, "/", tr, dockertypes.CopyToContainerOptions{}); err != nil {
tr, errChan := b.FS.CreateTarReader(b.AppDir, launchDir+"/app", 0, 0)
if err := b.Cli.CopyToContainer(ctx, ctr.ID, "/", tr, dockertypes.CopyToContainerOptions{
}); err != nil {
return errors.Wrap(err, "copy app to workspace volume")
}
if err := <-errChan; err != nil {
return errors.Wrap(err, "copy app to workspace volume")
}

uid, gid, err := b.packUidGid(b.Builder)
if err != nil {
return errors.Wrap(err, "get pack uid gid")
}
if err := b.chownDir(launchDir+"/app", uid, gid); err != nil {
return errors.Wrap(err, "chown app to workspace volume")
}
Expand All @@ -379,7 +379,8 @@ func (b *BuildConfig) Detect() error {
if err != nil {
return errors.Wrap(err, "converting order TOML to tar reader")
}
if err := b.Cli.CopyToContainer(ctx, ctr.ID, "/", ftr, dockertypes.CopyToContainerOptions{}); err != nil {
if err := b.Cli.CopyToContainer(ctx, ctr.ID, "/", ftr, dockertypes.CopyToContainerOptions{
}); err != nil {
return errors.Wrap(err, fmt.Sprintf("creating %s", orderPath))
}
}
Expand All @@ -397,7 +398,7 @@ func (b *BuildConfig) Analyze() error {
}
hostConfig := &container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s:", b.WorkspaceVolume, launchDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, launchDir),
},
}

Expand All @@ -410,15 +411,15 @@ func (b *BuildConfig) Analyze() error {
ctrConf.Env = []string{fmt.Sprintf(`PACK_REGISTRY_AUTH=%s`, authHeader)}
ctrConf.Cmd = []string{
"/lifecycle/analyzer",
"-launch", launchDir,
"-layers", launchDir,
"-group", groupPath,
b.RepoName,
}
hostConfig.NetworkMode = "host"
} else {
ctrConf.Cmd = []string{
"/lifecycle/analyzer",
"-launch", launchDir,
"-layers", launchDir,
"-group", groupPath,
"-daemon",
b.RepoName,
Expand All @@ -437,14 +438,12 @@ func (b *BuildConfig) Analyze() error {
return errors.Wrap(err, "analyze run container")
}

if ctrConf.User == "root" {
uid, gid, err := b.packUidGid(b.Builder)
if err != nil {
return errors.Wrap(err, "getting pack uid/gid")
}
if err := b.chownDir(launchDir, uid, gid); err != nil {
return errors.Wrap(err, "chown launch dir")
}
uid, gid, err := b.packUidGid(b.Builder)
if err != nil {
return errors.Wrap(err, "get pack uid and gid")
}
if err := b.chownDir(launchDir, uid, gid); err != nil {
return errors.Wrap(err, "chown launch dir")
}

return nil
Expand All @@ -469,16 +468,14 @@ func (b *BuildConfig) Build() error {
Cmd: []string{
"/lifecycle/builder",
"-buildpacks", buildpacksDir,
"-launch", launchDir,
"-cache", cacheDir,
"-layers", launchDir,
"-group", groupPath,
"-plan", planPath,
"-platform", platformDir,
},
}, &container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s:", b.WorkspaceVolume, launchDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, cacheDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, launchDir),
},
}, nil, "")
if err != nil {
Expand All @@ -503,7 +500,11 @@ func (b *BuildConfig) Build() error {
}
}

return b.Cli.RunContainer(ctx, ctr.ID, b.Stdout, b.Stderr)
err = b.Cli.RunContainer(ctx, ctr.ID, b.Stdout, b.Stderr)
if err != nil {
return errors.Wrap(err, "running builder in container")
}
return nil
}

func parseEnvFile(envFile string) (map[string]string, error) {
Expand Down Expand Up @@ -555,7 +556,7 @@ func (b *BuildConfig) Export() error {
}
hostConfig := &container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s:", b.WorkspaceVolume, launchDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, launchDir),
},
}

Expand All @@ -569,7 +570,7 @@ func (b *BuildConfig) Export() error {
ctrConf.Cmd = []string{
"/lifecycle/exporter",
"-image", b.RunImage,
"-launch", launchDir,
"-layers", launchDir,
"-group", groupPath,
b.RepoName,
}
Expand All @@ -578,7 +579,7 @@ func (b *BuildConfig) Export() error {
ctrConf.Cmd = []string{
"/lifecycle/exporter",
"-image", b.RunImage,
"-launch", launchDir,
"-layers", launchDir,
"-group", groupPath,
"-daemon",
b.RepoName,
Expand All @@ -593,6 +594,15 @@ func (b *BuildConfig) Export() error {
}
defer b.Cli.ContainerRemove(ctx, ctr.ID, dockertypes.ContainerRemoveOptions{})

uid, gid, err := b.packUidGid(b.Builder)
if err != nil {
return errors.Wrap(err, "get pack uid and gid")
}
if err := b.chownDir(launchDir, uid, gid); err != nil {
return errors.Wrap(err, "chown launch dir")
}


if err := b.Cli.RunContainer(ctx, ctr.ID, b.Stdout, b.Stderr); err != nil {
return errors.Wrap(err, "run lifecycle/exporter")
}
Expand Down Expand Up @@ -636,7 +646,7 @@ func (b *BuildConfig) chownDir(path string, uid, gid int) error {
User: "root",
}, &container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s:", b.WorkspaceVolume, launchDir),
fmt.Sprintf("%s:%s:", b.CacheVolume, launchDir),
},
}, nil, "")
if err != nil {
Expand Down
Loading

0 comments on commit 85da90b

Please sign in to comment.