Skip to content

Commit

Permalink
Handle pre/post_compile scripts without shebangs.
Browse files Browse the repository at this point in the history
[#156381551]

Signed-off-by: Tyler Phelan <[email protected]>
  • Loading branch information
astrieanna authored and Tyler Phelan committed Apr 3, 2018
1 parent 2c8495d commit 5c74de6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
4 changes: 0 additions & 4 deletions fixtures/with_hooks/bin/post_compile
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
#!/usr/bin/env bash

set -uexo pipefail

echo 'Echo from app post compile'
echo
41 changes: 23 additions & 18 deletions src/python/hooks/hooks_app.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package hooks

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"

"github.com/cloudfoundry/libbuildpack"
)
Expand All @@ -17,36 +19,38 @@ func init() {
}

func (h AppHook) BeforeCompile(compiler *libbuildpack.Stager) error {
path := filepath.Join(compiler.BuildDir(), "bin", "pre_compile")
return runHook("pre_compile", compiler)
}

func (h AppHook) AfterCompile(compiler *libbuildpack.Stager) error {
return runHook("post_compile", compiler)
}

func runHook(scriptName string, compiler *libbuildpack.Stager) error {
path := filepath.Join(compiler.BuildDir(), "bin", scriptName)
if exists, err := libbuildpack.FileExists(path); err != nil {
return err
} else if exists {
compiler.Logger().BeginStep("Running pre-compile hook")
compiler.Logger().BeginStep("Running " + scriptName + " hook")
if err := os.Chmod(path, 0755); err != nil {
return err
}
cmd := exec.Command(path)
cmd.Dir = compiler.BuildDir()
output, err := cmd.Output()

fileContents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
compiler.Logger().Info("%s", output)

}
return nil
}
shebangRegex := regexp.MustCompile("^\\s*#!")
hasShebang := shebangRegex.Match(fileContents)

func (h AppHook) AfterCompile(compiler *libbuildpack.Stager) error {
path := filepath.Join(compiler.BuildDir(), "bin", "post_compile")
if exists, err := libbuildpack.FileExists(path); err != nil {
return err
} else if exists {
compiler.Logger().BeginStep("Running post-compile hook")
if err := os.Chmod(path, 0755); err != nil {
return err
var cmd *exec.Cmd
if hasShebang {
cmd = exec.Command(path)
} else {
cmd = exec.Command("/bin/sh", path)
}
cmd := exec.Command(path)

cmd.Dir = compiler.BuildDir()
output, err := cmd.Output()
if err != nil {
Expand All @@ -55,4 +59,5 @@ func (h AppHook) AfterCompile(compiler *libbuildpack.Stager) error {
compiler.Logger().Info("%s", output)
}
return nil

}
44 changes: 28 additions & 16 deletions src/python/hooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = Describe("Hooks", func() {
Expect(os.RemoveAll(buildDir)).To(Succeed())
})

Context("BeforeCompile", func() {
Describe("BeforeCompile", func() {
Context("bin/pre_compile exists", func() {
BeforeEach(func() {
Expect(os.Mkdir(filepath.Join(buildDir, "bin"), 0755)).To(Succeed())
Expand All @@ -51,10 +51,10 @@ var _ = Describe("Hooks", func() {
fileInfo, err := os.Stat(filepath.Join(buildDir, "bin", "pre_compile"))
Expect(err).ToNot(HaveOccurred())
Expect(fileInfo.Mode()).To(Equal(os.FileMode(0755)))
Expect(buffer.String()).To(ContainSubstring("Running pre-compile hook"))
Expect(buffer.String()).To(ContainSubstring("Running pre_compile hook"))
})

It("runs file", func() {
It("runs successfully", func() {
Expect(filepath.Join(buildDir, "fred.txt")).ToNot(BeARegularFile())

Expect(hook.BeforeCompile(stager)).To(Succeed())
Expand All @@ -64,15 +64,20 @@ var _ = Describe("Hooks", func() {
})
})

Context("bin/pre_compile does NOT exist", func() {
It("does nothing", func() {
Expect(hook.BeforeCompile(stager)).To(Succeed())
Expect(buffer.String()).NotTo(ContainSubstring("Running pre-compile hook"))
})
It("runs successfully when bin/pre_compile exists but has no shebang", func() {
Expect(os.Mkdir(filepath.Join(buildDir, "bin"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(buildDir, "bin", "pre_compile"), []byte("echo 'Hello world!'"), 0644)).To(Succeed())
Expect(hook.BeforeCompile(stager)).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Hello world!"))
})

It("does nothing when bin/pre_compile does NOT exist", func() {
Expect(hook.BeforeCompile(stager)).To(Succeed())
Expect(buffer.String()).NotTo(ContainSubstring("Running pre_compile hook"))
})
})

Context("AfterCompile", func() {
Describe("AfterCompile", func() {
Context("bin/post_compile exists", func() {
BeforeEach(func() {
Expect(os.Mkdir(filepath.Join(buildDir, "bin"), 0755)).To(Succeed())
Expand All @@ -83,9 +88,10 @@ var _ = Describe("Hooks", func() {
fileInfo, err := os.Stat(filepath.Join(buildDir, "bin", "post_compile"))
Expect(err).ToNot(HaveOccurred())
Expect(fileInfo.Mode()).To(Equal(os.FileMode(0755)))
Expect(buffer.String()).To(ContainSubstring("Running post-compile hook"))
Expect(buffer.String()).To(ContainSubstring("Running post_compile hook"))
})
It("runs file", func() {

It("runs successfully", func() {
Expect(filepath.Join(buildDir, "fred.txt")).ToNot(BeARegularFile())

Expect(hook.AfterCompile(stager)).To(Succeed())
Expand All @@ -94,11 +100,17 @@ var _ = Describe("Hooks", func() {
Expect(ioutil.ReadFile(filepath.Join(buildDir, "fred.txt"))).To(Equal([]byte("john")))
})
})
Context("bin/post_compile does NOT exist", func() {
It("does nothing", func() {
Expect(hook.AfterCompile(stager)).To(Succeed())
Expect(buffer.String()).NotTo(ContainSubstring("Running post-compile hook"))
})

It("runs successfully when bin/post_compile exists but has no shebang", func() {
Expect(os.Mkdir(filepath.Join(buildDir, "bin"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(buildDir, "bin", "post_compile"), []byte("echo 'Hello world!'"), 0644)).To(Succeed())
Expect(hook.AfterCompile(stager)).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Hello world!"))
})

It("does nothing when bin/post_compile does NOT exist", func() {
Expect(hook.AfterCompile(stager)).To(Succeed())
Expect(buffer.String()).NotTo(ContainSubstring("Running post_compile hook"))
})
})
})

0 comments on commit 5c74de6

Please sign in to comment.