Skip to content

Commit

Permalink
Merge pull request #35 from buildpack/jkutner/app-dir2
Browse files Browse the repository at this point in the history
Add support for custom app directory with cmd arg
  • Loading branch information
sclevine authored Nov 8, 2018
2 parents aec58da + 39a04c6 commit b2745f1
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 27 deletions.
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type BuildMetadata struct {
Buildpacks []string `toml:"buildpacks"`
}

func (b *Builder) Build(cacheDir, launchDir string, env BuildEnv) (*BuildMetadata, error) {
func (b *Builder) Build(cacheDir, launchDir string, appDir string, env BuildEnv) (*BuildMetadata, error) {
procMap := processMap{}
var buildpackIDs []string
for _, bp := range b.Buildpacks {
Expand All @@ -58,7 +58,7 @@ func (b *Builder) Build(cacheDir, launchDir string, env BuildEnv) (*BuildMetadat
}
cmd := exec.Command(buildPath, b.PlatformDir, bpCacheDir, bpLaunchDir)
cmd.Env = env.List()
cmd.Dir = filepath.Join(launchDir, "app")
cmd.Dir = filepath.Join(appDir)
cmd.Stdin = bytes.NewBuffer(b.In)
cmd.Stdout = b.Out
cmd.Stderr = b.Err
Expand Down
22 changes: 11 additions & 11 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
env.EXPECT().AddEnvDir(filepath.Join(cacheDir, "buildpack2-id", "cache-layer3", "env")),
env.EXPECT().AddEnvDir(filepath.Join(cacheDir, "buildpack2-id", "cache-layer4", "env")),
)
if _, err := builder.Build(cacheDir, launchDir, env); err != nil {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err != nil {
t.Fatalf("Error: %s\n", err)
}
})
Expand All @@ -117,7 +117,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
filepath.Join(appDir, "launch-buildpack1", "launch-layer1"),
filepath.Join(appDir, "launch-buildpack2", "launch-layer2"),
)
if _, err := builder.Build(cacheDir, launchDir, env); err != nil {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err != nil {
t.Fatalf("Error: %s\n", err)
}
testExists(t,
Expand All @@ -127,7 +127,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
})

it("should return build metadata when processes are present", func() {
metadata, err := builder.Build(cacheDir, launchDir, env)
metadata, err := builder.Build(cacheDir, launchDir, appDir, env)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
Expand All @@ -145,7 +145,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {

it("should return build metadata when processes are not present", func() {
mkfile(t, "test", filepath.Join(appDir, "skip-processes"))
metadata, err := builder.Build(cacheDir, launchDir, env)
metadata, err := builder.Build(cacheDir, launchDir, appDir, env)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
Expand All @@ -161,7 +161,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
mkfile(t, "some-data",
filepath.Join(platformDir, "env", "SOME_VAR"),
)
if _, err := builder.Build(cacheDir, launchDir, env); err != nil {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err != nil {
t.Fatalf("Error: %s\n", err)
}
testExists(t,
Expand All @@ -171,7 +171,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
})

it("should connect stdout and stdin to the terminal", func() {
if _, err := builder.Build(cacheDir, launchDir, env); err != nil {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err != nil {
t.Fatalf("Error: %s\n", err)
}
if stdout.String() != "STDOUT1\nSTDOUT2\n" {
Expand All @@ -186,15 +186,15 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
when("building fails", func() {
it("should error when launch directories cannot be created", func() {
mkfile(t, "some-data", filepath.Join(launchDir, "buildpack1-id"))
_, err := builder.Build(cacheDir, launchDir, env)
_, err := builder.Build(cacheDir, launchDir, appDir, env)
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("Incorrect error: %s\n", err)
}
})

it("should error when cache directories cannot be created", func() {
mkfile(t, "some-data", filepath.Join(cacheDir, "buildpack1-id"))
_, err := builder.Build(cacheDir, launchDir, env)
_, err := builder.Build(cacheDir, launchDir, appDir, env)
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("Incorrect error: %s\n", err)
}
Expand All @@ -205,7 +205,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
if err := os.RemoveAll(platformDir); err != nil {
t.Fatalf("Error: %s\n", err)
}
_, err := builder.Build(cacheDir, launchDir, env)
_, err := builder.Build(cacheDir, launchDir, appDir, env)
if _, ok := err.(*exec.ExitError); !ok {
t.Fatalf("Incorrect error: %s\n", err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
filepath.Join(appDir, "cache-buildpack1", "cache-layer1"),
filepath.Join(appDir, "cache-buildpack1", "cache-layer2"),
)
if _, err := builder.Build(cacheDir, launchDir, env); err != appendErr {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err != appendErr {
t.Fatalf("Incorrect error: %s\n", err)
}
})
Expand All @@ -246,7 +246,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
it("should error when launch.toml is not writable", func() {
env.EXPECT().List().Return([]string{"ID=1"})
mkdir(t, filepath.Join(launchDir, "buildpack1-id", "launch.toml"))
if _, err := builder.Build(cacheDir, launchDir, env); err == nil {
if _, err := builder.Build(cacheDir, launchDir, appDir, env); err == nil {
t.Fatal("Expected error")
}
})
Expand Down
3 changes: 3 additions & 0 deletions cmd/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
groupPath string
planPath string
launchDir string
appDir string
cacheDir string
platformDir string
)
Expand All @@ -24,6 +25,7 @@ func init() {
cmd.FlagGroupPath(&groupPath)
cmd.FlagPlanPath(&planPath)
cmd.FlagLaunchDir(&launchDir)
cmd.FlagAppDir(&appDir)
cmd.FlagCacheDir(&cacheDir)
cmd.FlagPlatformDir(&platformDir)
}
Expand Down Expand Up @@ -67,6 +69,7 @@ func build() error {
metadata, err := builder.Build(
cacheDir,
launchDir,
appDir,
env,
)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const (
DefaultLaunchDir = "/workspace"
DefaultAppDir = "/workspace/app"
DefaultCacheDir = "/cache"
DefaultBuildpacksDir = "/buildpacks"
DefaultPlatformDir = "/platform"
Expand All @@ -33,6 +34,14 @@ func FlagLaunchDirSrc(dir *string) {
flag.StringVar(dir, "launch-src", DefaultLaunchDir, "path to source launch directory for export step")
}

func FlagAppDir(dir *string) {
flag.StringVar(dir, "app", DefaultAppDir, "path to app directory")
}

func FlagAppDirSrc(dir *string) {
flag.StringVar(dir, "app-src", DefaultAppDir, "path to app directory for export step")
}

func FlagCacheDir(dir *string) {
flag.StringVar(dir, "cache", DefaultCacheDir, "path to cache directory")
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/detector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/cmd"
)

var (
buildpacksDir string
launchDir string
appDir string
orderPath string
groupPath string
planPath string
)

func init() {
cmd.FlagBuildpacksDir(&buildpacksDir)
cmd.FlagLaunchDir(&launchDir)
cmd.FlagAppDir(&appDir)
cmd.FlagOrderPath(&orderPath)

cmd.FlagGroupPath(&groupPath)
Expand All @@ -48,7 +47,7 @@ func detect() error {
return cmd.FailErr(err, "read buildpack order file")
}

info, group := order.Detect(logger, filepath.Join(launchDir, "app"))
info, group := order.Detect(logger, appDir)
if group == nil {
return cmd.FailCode(cmd.CodeFailedDetect, "detect")
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
runImage string
launchDir string
launchDirSrc string
appDir string
appDirSrc string
groupPath string
useDaemon bool
useHelpers bool
Expand All @@ -29,6 +31,8 @@ func init() {
cmd.FlagRunImage(&runImage)
cmd.FlagLaunchDir(&launchDir)
cmd.FlagLaunchDirSrc(&launchDirSrc)
cmd.FlagAppDir(&appDir)
cmd.FlagAppDirSrc(&appDirSrc)
cmd.FlagGroupPath(&groupPath)
cmd.FlagUseDaemon(&useDaemon)
cmd.FlagUseCredHelpers(&useHelpers)
Expand Down Expand Up @@ -106,6 +110,8 @@ func export() error {
newImage, err := exporter.Export(
launchDirSrc,
launchDir,
appDirSrc,
appDir,
stackImage,
origImage,
)
Expand Down
8 changes: 8 additions & 0 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var (
launchDir string
appDir string
)

func main() {
Expand All @@ -32,6 +33,12 @@ func launch() error {
}
os.Unsetenv(lifecycle.EnvLaunchDir)

appDir := cmd.DefaultAppDir
if v := os.Getenv(lifecycle.EnvAppDir); v != "" {
appDir = v
}
os.Unsetenv(lifecycle.EnvAppDir)

var metadata lifecycle.BuildMetadata
metadataPath := filepath.Join(launchDir, "config", "metadata.toml")
if _, err := toml.DecodeFile(metadataPath, &metadata); err != nil {
Expand All @@ -41,6 +48,7 @@ func launch() error {
launcher := &lifecycle.Launcher{
DefaultProcessType: defaultProcessType,
LaunchDir: launchDir,
AppDir: appDir,
Processes: metadata.Processes,
Buildpacks: metadata.Buildpacks,
Exec: syscall.Exec,
Expand Down
6 changes: 3 additions & 3 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Exporter struct {
UID, GID int
}

func (e *Exporter) Export(launchDirSrc, launchDirDst string, runImage, origImage v1.Image) (v1.Image, error) {
func (e *Exporter) Export(launchDirSrc, launchDirDst, appDirSrc, appDirDst string, runImage, origImage v1.Image) (v1.Image, error) {
metadata := AppImageMetadata{}

if err := addRunImageMetadata(runImage, &metadata); err != nil {
Expand All @@ -36,8 +36,8 @@ func (e *Exporter) Export(launchDirSrc, launchDirDst string, runImage, origImage
repoImage, appLayerDigest, err := e.addDirAsLayer(
runImage,
filepath.Join(e.TmpDir, "app.tgz"),
filepath.Join(launchDirSrc, "app"),
filepath.Join(launchDirDst, "app"),
appDirSrc,
appDirDst,
)
if err != nil {
return nil, errors.Wrap(err, "append app layer to run image")
Expand Down
12 changes: 8 additions & 4 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func testExporter(t *testing.T, when spec.G, it spec.S) {
})

it("should process a simple launch directory", func() {
image, err := exporter.Export("testdata/exporter/first/launch", "/launch/dest", runImage, nil)
image, err := exporter.Export("testdata/exporter/first/launch", "/launch/dest",
"testdata/exporter/first/launch/app", "/launch/dest/app", runImage, nil)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
Expand Down Expand Up @@ -164,7 +165,8 @@ func testExporter(t *testing.T, when spec.G, it spec.S) {
exporter.GID = 5678
})
it("sets uid/gid on the layer files", func() {
image, err := exporter.Export("testdata/exporter/first/launch", "/launch/dest", runImage, nil)
image, err := exporter.Export("testdata/exporter/first/launch", "/launch/dest",
"testdata/exporter/first/launch/app", "/launch/dest/app", runImage, nil)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
Expand Down Expand Up @@ -197,14 +199,16 @@ func testExporter(t *testing.T, when spec.G, it spec.S) {
var firstImage v1.Image
it.Before(func() {
var err error
firstImage, err = exporter.Export("testdata/exporter/first/launch", "/launch/dest", runImage, nil)
firstImage, err = exporter.Export("testdata/exporter/first/launch", "/launch/dest",
"testdata/exporter/first/launch/app", "/launch/dest/app", runImage, nil)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
})

it("should reuse layers if there is a layer TOML file", func() {
image, err := exporter.Export("testdata/exporter/second/launch", "/launch/dest", runImage, firstImage)
image, err := exporter.Export("testdata/exporter/second/launch", "/launch/dest",
"testdata/exporter/first/launch/app", "/launch/dest/app", runImage, firstImage)
if err != nil {
t.Fatalf("Error: %s\n", err)
}
Expand Down
7 changes: 4 additions & 3 deletions launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Launcher struct {
DefaultProcessType string
LaunchDir string
AppDir string
Processes []Process
Buildpacks []string
Exec func(argv0 string, argv []string, envv []string) error
Expand All @@ -26,7 +27,7 @@ func (l *Launcher) Launch(executable, startCommand string) error {
Map: POSIXLaunchEnv,
}
if err := l.eachDir(l.LaunchDir, func(bp string) error {
if bp == "app" {
if filepath.Clean(l.AppDir) == filepath.Join(l.LaunchDir, bp) {
return nil
}
bpPath := filepath.Join(l.LaunchDir, bp)
Expand All @@ -36,7 +37,7 @@ func (l *Launcher) Launch(executable, startCommand string) error {
}); err != nil {
return errors.Wrap(err, "modify env")
}
if err := os.Chdir(filepath.Join(l.LaunchDir, "app")); err != nil {
if err := os.Chdir(l.AppDir); err != nil {
return errors.Wrap(err, "change to app directory")
}

Expand Down Expand Up @@ -89,7 +90,7 @@ func (l *Launcher) profileD() (string, error) {
}
}

if err := appendIfFile(filepath.Join(l.LaunchDir, "app", ".profile")); err != nil {
if err := appendIfFile(filepath.Join(l.AppDir, ".profile")); err != nil {
return "", err
}

Expand Down
1 change: 1 addition & 0 deletions launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) {
launcher = &lifecycle.Launcher{
DefaultProcessType: "web",
LaunchDir: filepath.Join(tmpDir, "launch"),
AppDir: filepath.Join(tmpDir, "launch", "app"),
Processes: []lifecycle.Process{
{Type: "other", Command: "some-other-process"},
{Type: "web", Command: "some-web-process"},
Expand Down
1 change: 1 addition & 0 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lifecycle
const (
MetadataLabel = "io.buildpacks.lifecycle.metadata"
EnvLaunchDir = "PACK_LAUNCH_DIR"
EnvAppDir = "PACK_APP_DIR"
)

type AppImageMetadata struct {
Expand Down

0 comments on commit b2745f1

Please sign in to comment.