Skip to content

Commit

Permalink
Implement process type specific env vars
Browse files Browse the repository at this point in the history
Include process type specific paths when processing env vars during launch.

Spec: buildpacks/spec#98

Signed-off-by: Jesse Brown <jabrown85@gmail.com>
  • Loading branch information
jabrown85 committed Jul 1, 2020
1 parent dee8ab6 commit caa8457
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 8 deletions.
12 changes: 9 additions & 3 deletions acceptance/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) {
}
assertOutput(t, cmd, expected)
})

it("sets env vars from process specific directories", func() {
cmd := exec.Command("docker", "run", "--rm", launchImage, "worker")
expected := "worker-process-val"
assertOutput(t, cmd, expected)
})
})

it("respects CNB_APP_DIR and CNB_LAYERS_DIR environment variables", func() {
Expand All @@ -83,11 +89,11 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) {
})

it("sets env vars from layers", func() {
cmd := exec.Command("docker", "run", "--rm", launchImage, "echo $SOME_VAR $OTHER_VAR")
cmd := exec.Command("docker", "run", "--rm", launchImage, "echo $SOME_VAR $OTHER_VAR $WORKER_VAR")
if runtime.GOOS == "windows" {
cmd = exec.Command("docker", "run", "--rm", launchImage, "echo %SOME_VAR% %OTHER_VAR%")
cmd = exec.Command("docker", "run", "--rm", launchImage, "echo %SOME_VAR% %OTHER_VAR% %WORKER_VAR%")
}
assertOutput(t, cmd, "sourced bp profile\nsourced app profile\nsome-bp-val other-bp-val")
assertOutput(t, cmd, "sourced bp profile\nsourced app profile\nsome-bp-val other-bp-val worker-no-process-val")
})

it("passes through env vars from user, excluding excluded vars", func() {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
command = "echo"
args = ["Executing other-process process-type"]
direct = true

[[processes]]
type = "worker"
command = "echo $WORKER_VAR"
direct = false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker-no-process-val
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker-process-val
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
command = "ping"
args = ["/?"]
direct = true

[[processes]]
type = "worker"
command = "echo %WORKER_VAR%"
direct = false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker-no-process-val
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker-process-val
21 changes: 16 additions & 5 deletions launch/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type Launcher struct {
}

func (l *Launcher) Launch(self string, cmd []string) error {
if err := l.env(); err != nil {
return errors.Wrap(err, "modify env")
}
process, err := l.processFor(cmd)
if err != nil {
return errors.Wrap(err, "determine start command")
}
if err := l.env(process); err != nil {
return errors.Wrap(err, "modify env")
}
if err := os.Chdir(l.AppDir); err != nil {
return errors.Wrap(err, "change to app directory")
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (l *Launcher) Launch(self string, cmd []string) error {
return nil
}

func (l *Launcher) env() error {
func (l *Launcher) env(process Process) error {
appInfo, err := os.Stat(l.AppDir)
if err != nil {
return errors.Wrap(err, "find app directory")
Expand All @@ -106,7 +106,18 @@ func (l *Launcher) env() error {
if err := l.Env.AddEnvDir(filepath.Join(path, "env")); err != nil {
return err
}
return l.Env.AddEnvDir(filepath.Join(path, "env.launch"))
if process.Type != "" {
if err := l.Env.AddEnvDir(filepath.Join(path, "env", process.Type)); err != nil {
return err
}
}
if err := l.Env.AddEnvDir(filepath.Join(path, "env.launch")); err != nil {
return err
}
if process.Type == "" {
return nil
}
return l.Env.AddEnvDir(filepath.Join(path, "env.launch", process.Type))
}); err != nil {
return errors.Wrap(err, "add layer env")
}
Expand Down
8 changes: 8 additions & 0 deletions launch/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,24 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) {
env.EXPECT().AddRootDir(filepath.Join(tmpDir, "launch", "bp.1", "layer1")),
env.EXPECT().AddRootDir(filepath.Join(tmpDir, "launch", "bp.1", "layer2")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer1", "env")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer1", "env", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer1", "env.launch")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer1", "env.launch", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer2", "env")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer2", "env", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer2", "env.launch")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.1", "layer2", "env.launch", "start")),

env.EXPECT().AddRootDir(filepath.Join(tmpDir, "launch", "bp.2", "layer3")),
env.EXPECT().AddRootDir(filepath.Join(tmpDir, "launch", "bp.2", "layer4")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer3", "env")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer3", "env", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer3", "env.launch")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer3", "env.launch", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer4", "env")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer4", "env", "start")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer4", "env.launch")),
env.EXPECT().AddEnvDir(filepath.Join(tmpDir, "launch", "bp.2", "layer4", "env.launch", "start")),
)
if err := launcher.Launch("/path/to/launcher", []string{"start"}); err != nil {
t.Fatal(err)
Expand Down

0 comments on commit caa8457

Please sign in to comment.