Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement process type specific env vars #327

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions acceptance/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ 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("sources scripts from process specific directories", func() {
cmd := exec.Command("docker", "run", "--rm", launchImage, "worker")
expected := "sourced bp profile\nsourced bp worker profile\nsourced app profile"
assertOutput(t, cmd, expected)
})
})

it("respects CNB_APP_DIR and CNB_LAYERS_DIR environment variables", func() {
Expand All @@ -83,11 +95,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
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-process-val
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 @@
echo "sourced bp worker profile"
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-process-val
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 @@
echo sourced bp worker profile
34 changes: 24 additions & 10 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 All @@ -53,7 +53,7 @@ func (l *Launcher) Launch(self string, cmd []string) error {
}
return nil
}
profileCmds, err := l.profileD()
profileCmds, err := l.profileD(process)
if err != nil {
return errors.Wrap(err, "determine profile")
}
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,15 +106,21 @@ 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 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")
}
return nil
})
}

func (l *Launcher) profileD() ([]string, error) {
func (l *Launcher) profileD(process Process) ([]string, error) {
var out []string

appendIfFile := func(path string) error {
Expand Down Expand Up @@ -143,9 +149,17 @@ func (l *Launcher) profileD() ([]string, error) {
if runtime.GOOS == "windows" {
fileGlob += ".bat"
}
scripts, err := filepath.Glob(filepath.Join(layersDir, EscapeID(bp.ID), "*", "profile.d", fileGlob))
if err != nil {
return nil, err
globPaths := []string{filepath.Join(layersDir, EscapeID(bp.ID), "*", "profile.d", fileGlob)}
if process.Type != "" {
globPaths = append(globPaths, filepath.Join(layersDir, EscapeID(bp.ID), "*", "profile.d", process.Type, fileGlob))
}
var scripts []string
for _, globPath := range globPaths {
matches, err := filepath.Glob(globPath)
if err != nil {
return nil, err
}
scripts = append(scripts, matches...)
}
for _, script := range scripts {
if err := appendIfFile(script); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions launch/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,19 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) {
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.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.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.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.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 Expand Up @@ -344,15 +348,21 @@ echo $OUT

mkdir(t,
filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d"),
filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d", "start"),
filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d"),
filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d", "start"),
)

if runtime.GOOS == "windows" {
mkfile(t, "set OUT=%OUT%prof1,", filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d", "prof1.bat"))
mkfile(t, "set OUT=%OUT%prof1start,", filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d", "start", "prof1.bat"))
mkfile(t, "set OUT=%OUT%prof2,", filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d", "prof2.bat"))
mkfile(t, "set OUT=%OUT%prof2start,", filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d", "start", "prof2.bat"))
} else {
mkfile(t, "export OUT=${OUT}prof1,", filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d", "prof1"))
mkfile(t, "export OUT=${OUT}prof1start,", filepath.Join(tmpDir, "launch", "bp.1", "layer", "profile.d", "start", "prof1"))
mkfile(t, "export OUT=${OUT}prof2,", filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d", "prof2"))
mkfile(t, "export OUT=${OUT}prof2start,", filepath.Join(tmpDir, "launch", "bp.2", "layer", "profile.d", "start", "prof2"))
}

env.EXPECT().AddRootDir(gomock.Any()).AnyTimes()
Expand All @@ -369,7 +379,7 @@ echo $OUT
stderr := rdfile(t, filepath.Join(tmpDir, "stderr"))
t.Fatalf("stdout was empty: stderr: %s\n", stderr)
}
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof1,prof2,\n"); diff != "" {
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof1,prof1start,prof2,prof2start,\n"); diff != "" {
t.Fatalf("syscall.Exec stdout did not match: (-got +want)\n%s\n", diff)
}
})
Expand All @@ -389,7 +399,7 @@ echo $OUT
stderr := rdfile(t, filepath.Join(tmpDir, "stderr"))
t.Fatalf("stdout was empty: stderr: %s\n", stderr)
}
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof2,prof1,\n"); diff != "" {
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof2,prof2start,prof1,prof1start,\n"); diff != "" {
t.Fatalf("syscall.Exec stdout did not match: (-got +want)\n%s\n", diff)
}
})
Expand All @@ -414,7 +424,7 @@ echo $OUT
stderr := rdfile(t, filepath.Join(tmpDir, "stderr"))
t.Fatalf("stdout was empty: stderr: %s\n", stderr)
}
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof1,prof2,profile\n"); diff != "" {
if diff := cmp.Diff(strings.ReplaceAll(stdout, "\r\n", "\n"), "hi from app\nprof1,prof1start,prof2,prof2start,profile\n"); diff != "" {
t.Fatalf("syscall.Exec stdout did not match: (-got +want)\n%s\n", diff)
}
})
Expand Down