Skip to content

Commit

Permalink
Use new defaults
Browse files Browse the repository at this point in the history
* lifecycle creates a second volume for the app

[buildpacks/lifecycle#77]

Signed-off-by: Emily Casey <[email protected]>
  • Loading branch information
ekcasey committed Apr 1, 2019
1 parent 5a87cc9 commit 61888f1
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 175 deletions.
139 changes: 12 additions & 127 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ type BuildConfig struct {
LifecycleConfig build.LifecycleConfig
}

const (
layersDir = "/workspace"
buildpacksDir = "/buildpacks"
platformDir = "/platform"
orderPath = "/buildpacks/order.toml"
groupPath = `/workspace/group.toml`
planPath = "/workspace/plan.toml"
appDir = "/workspace/app"
)

func DefaultBuildFactory(logger *logging.Logger, cache Cache, dockerClient Docker, fetcher Fetcher) (*BuildFactory, error) {
f := &BuildFactory{
Logger: logger,
Expand Down Expand Up @@ -331,66 +321,25 @@ func (b *BuildConfig) Run(ctx context.Context) error {
}

func (b *BuildConfig) detect(ctx context.Context, lifecycle *build.Lifecycle) error {
phase, err := lifecycle.NewPhase(
"detector",
build.WithArgs(
"-buildpacks", buildpacksDir,
"-order", orderPath,
"-group", groupPath,
"-plan", planPath,
"-app", appDir,
),
)
detect, err := lifecycle.NewDetect()
if err != nil {
return err
}
defer phase.Cleanup()
return phase.Run(ctx)
defer detect.Cleanup()
return detect.Run(ctx)
}

func (b *BuildConfig) restore(ctx context.Context, lifecycle *build.Lifecycle) error {
phase, err := lifecycle.NewPhase(
"restorer",
build.WithArgs(
"-image", b.Cache.Image(),
"-group", groupPath,
"-layers", layersDir,
),
build.WithDaemonAccess(),
)

restore, err := lifecycle.NewRestore(b.Cache.Image())
if err != nil {
return err
}
defer phase.Cleanup()
return phase.Run(ctx)
defer restore.Cleanup()
return restore.Run(ctx)
}

func (b *BuildConfig) analyze(ctx context.Context, lifecycle *build.Lifecycle) error {
var analyze *build.Phase
var err error
if b.Publish {
analyze, err = lifecycle.NewPhase(
"analyzer",
build.WithRegistryAccess(b.RepoName, b.RunImage),
build.WithArgs(
"-layers", layersDir,
"-group", groupPath,
b.RepoName,
),
)
} else {
analyze, err = lifecycle.NewPhase(
"analyzer",
build.WithDaemonAccess(),
build.WithArgs(
"-layers", layersDir,
"-group", groupPath,
"-daemon",
b.RepoName,
),
)
}
analyze, err := lifecycle.NewAnalyze(b.RepoName, b.Publish)
if err != nil {
return err
}
Expand All @@ -399,70 +348,16 @@ func (b *BuildConfig) analyze(ctx context.Context, lifecycle *build.Lifecycle) e
}

func (b *BuildConfig) build(ctx context.Context, lifecycle *build.Lifecycle) error {
build, err := lifecycle.NewPhase(
"builder",
build.WithArgs(
"-buildpacks", buildpacksDir,
"-layers", layersDir,
"-app", appDir,
"-group", groupPath,
"-plan", planPath,
"-platform", platformDir,
),
)
build, err := lifecycle.NewBuild()
if err != nil {
return err
}
defer build.Cleanup()
return build.Run(ctx)
}

type exporterArgs struct {
args []string
repoName string
}

func (e *exporterArgs) add(args ...string) {
e.args = append(e.args, args...)
}

func (e *exporterArgs) daemon() {
e.args = append(e.args, "-daemon")
}

func (e *exporterArgs) list() []string {
e.args = append(e.args, e.repoName)
return e.args
}

func (b *BuildConfig) export(ctx context.Context, lifecycle *build.Lifecycle) error {
var (
export *build.Phase
err error
)

args := &exporterArgs{repoName: b.RepoName}
args.add(
"-image", b.RunImage,
"-layers", layersDir,
"-app", appDir,
"-group", groupPath,
)

if b.Publish {
export, err = lifecycle.NewPhase(
"exporter",
build.WithRegistryAccess(b.RepoName, b.RunImage),
build.WithArgs(args.list()...),
)
} else {
args.daemon()
export, err = lifecycle.NewPhase(
"exporter",
build.WithDaemonAccess(),
build.WithArgs(args.list()...),
)
}
export, err := lifecycle.NewExport(b.RepoName, b.RunImage, b.Publish)
if err != nil {
return err
}
Expand All @@ -471,22 +366,12 @@ func (b *BuildConfig) export(ctx context.Context, lifecycle *build.Lifecycle) er
}

func (b *BuildConfig) cache(ctx context.Context, lifecycle *build.Lifecycle) error {
phase, err := lifecycle.NewPhase(
"cacher",
build.WithArgs(
"-image", b.Cache.Image(),
"-group", groupPath,
"-layers", layersDir,
),
build.WithDaemonAccess(),
)

cache, err := lifecycle.NewCache(b.Cache.Image())
if err != nil {
return err
}
defer phase.Cleanup()

return phase.Run(ctx)
defer cache.Cleanup()
return cache.Run(ctx)
}

func parseEnvFile(filename string) (map[string]string, error) {
Expand Down
73 changes: 39 additions & 34 deletions build/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"archive/tar"
"context"
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"io"
"io/ioutil"
"math/rand"
Expand All @@ -20,27 +18,28 @@ import (
"github.com/BurntSushi/toml"
"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/image"

"github.com/buildpack/pack/archive"
"github.com/buildpack/pack/docker"
"github.com/buildpack/pack/style"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/pkg/errors"

"github.com/buildpack/pack/archive"
"github.com/buildpack/pack/docker"
"github.com/buildpack/pack/logging"
"github.com/buildpack/pack/style"
)

type Lifecycle struct {
BuilderImage string
Logger *logging.Logger
Docker Docker
WorkspaceVolume string
uid, gid int
appDir string
appOnce *sync.Once
BuilderImage string
Logger *logging.Logger
Docker Docker
LayersVolume string
AppVolume string
uid, gid int
appDir string
appOnce *sync.Once
}

type Docker interface {
Expand All @@ -54,10 +53,6 @@ type Docker interface {
VolumeList(ctx context.Context, filter filters.Args) (volume.VolumeListOKBody, error)
}

const (
launchDir = "/workspace"
)

type LifecycleConfig struct {
BuilderImage string
Logger *logging.Logger
Expand Down Expand Up @@ -123,23 +118,30 @@ func NewLifecycle(c LifecycleConfig) (*Lifecycle, error) {
}

return &Lifecycle{
BuilderImage: builder.Name(),
Logger: c.Logger,
Docker: client,
WorkspaceVolume: "pack-workspace-" + randString(10),
appDir: c.AppDir,
uid: uid,
gid: gid,
appOnce: &sync.Once{},
BuilderImage: builder.Name(),
Logger: c.Logger,
Docker: client,
LayersVolume: "pack-layers-" + randString(10),
AppVolume: "pack-app-" + randString(10),
appDir: c.AppDir,
uid: uid,
gid: gid,
appOnce: &sync.Once{},
}, nil
}

func (l *Lifecycle) Cleanup() error {
_, err := l.Docker.ImageRemove(context.Background(), l.BuilderImage, types.ImageRemoveOptions{})
if err != nil {
return err
var reterr error
if _, err := l.Docker.ImageRemove(context.Background(), l.BuilderImage, types.ImageRemoveOptions{}); err != nil {
reterr = errors.Wrapf(err, "failed to clean up builder image %s", l.BuilderImage)
}
if err := l.Docker.VolumeRemove(context.Background(), l.LayersVolume, true); err != nil {
reterr = errors.Wrapf(err, "failed to clean up layers volume %s", l.LayersVolume)
}
if err := l.Docker.VolumeRemove(context.Background(), l.AppVolume, true); err != nil {
reterr = errors.Wrapf(err, "failed to clean up app volume %s", l.AppVolume)
}
return l.Docker.VolumeRemove(context.Background(), l.WorkspaceVolume, true)
return reterr
}

func randString(n int) string {
Expand Down Expand Up @@ -181,14 +183,17 @@ func tarEnvFile(tmpDir string, env map[string]string) (string, error) {
tw := tar.NewWriter(fh)
defer tw.Close()
for k, v := range env {
if err := tw.WriteHeader(&tar.Header{Name: "/platform/env/" + k, Size: int64(len(v)), Mode: 0444, ModTime: now}); err != nil {
if err := tw.WriteHeader(&tar.Header{Name: filepath.Join(platformDir, "env", k), Size: int64(len(v)), Mode: 0444, ModTime: now}); err != nil {
return "", err
}
if _, err := tw.Write([]byte(v)); err != nil {
return "", err
}
}
if err := tw.WriteHeader(&tar.Header{Typeflag: tar.TypeDir, Name: "/platform/env/", Mode: 0555, ModTime: now}); err != nil {
if err := tw.WriteHeader(&tar.Header{Typeflag: tar.TypeDir, Name: filepath.Join(platformDir, "env"), Mode: 0555, ModTime: now}); err != nil {
return "", err
}
if err := tw.WriteHeader(&tar.Header{Typeflag: tar.TypeDir, Name: platformDir, Mode: 0555, ModTime: now}); err != nil {
return "", err
}
return fh.Name(), nil
Expand Down Expand Up @@ -217,7 +222,7 @@ func createBuildpacksTars(tmpDir string, buildpacks []string, logger *logging.Lo

tarFile := filepath.Join(tmpDir, fmt.Sprintf("%s.%s.tar", buildpackTOML.Buildpack.EscapedID(), version))

if err := archive.CreateTar(tarFile, bp, filepath.Join("/buildpacks", buildpackTOML.Buildpack.EscapedID(), version), uid, gid); err != nil {
if err := archive.CreateTar(tarFile, bp, filepath.Join(buildpacksDir, buildpackTOML.Buildpack.EscapedID(), version), uid, gid); err != nil {
return nil, err
}

Expand Down Expand Up @@ -254,7 +259,7 @@ func orderTar(tmpDir string, buildpacks []*lifecycle.Buildpack) (string, error)
orderToml := tomlBuilder.String()
err := archive.CreateSingleFileTar(
filepath.Join(tmpDir, "order.tar"),
"/buildpacks/order.toml",
orderPath,
orderToml,
)
if err != nil {
Expand Down
Loading

0 comments on commit 61888f1

Please sign in to comment.