Skip to content

Commit

Permalink
Merge pull request #11 from buildpack/detectimage
Browse files Browse the repository at this point in the history
Add DetectImage flag
  • Loading branch information
dgodd authored Aug 20, 2018
2 parents 7e2499b + a11d606 commit 153cc40
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
42 changes: 35 additions & 7 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package acceptance_test

import (
"bytes"
"encoding/hex"
"errors"
"io/ioutil"
Expand Down Expand Up @@ -102,7 +103,6 @@ func testPack(t *testing.T, when spec.G, it spec.S) {
exec.Command("docker", "kill", containerName).Run()
exec.Command("docker", "rmi", repoName).Run()
exec.Command("docker", "kill", registryContainerName).Run()
exec.Command("docker", "rmi", registryContainerName).Run()
})

when("'--publish' flag is not specified'", func() {
Expand Down Expand Up @@ -139,19 +139,47 @@ func testPack(t *testing.T, when spec.G, it spec.S) {
}, spec.Parallel(), spec.Report(report.Terminal{}))

when("'--publish' flag is specified", func() {
it("builds and exports an image", func() {
repo := "some-org/" + randString(10)
repoName := fmt.Sprintf("localhost:%s/%s", registryPort, repo)

it.Before(func() {
t.Log("push v3/packs:run to local registry")
for _, name := range []string{"detect", "analyze", "build", "export", "run"} {
for _, name := range []string{"analyze", "build", "export", "run"} {
run(t, exec.Command("docker", "tag", "packs/v3:"+name, fmt.Sprintf("host-machine.local:%s/packs/v3:%s", registryPort, name)))
}
run(t, exec.Command("docker", "tag", "packs/v3:run", fmt.Sprintf("localhost:%s/packs/v3:run", registryPort)))
run(t, exec.Command("docker", "push", fmt.Sprintf("localhost:%s/packs/v3:run", registryPort)))

// Build copy of packs/v3:detect with all group repositories pointing to image in local registry
cmd := exec.Command("docker", "build", "-t", fmt.Sprintf("host-machine.local:%s/packs/v3:detect", registryPort), "-")
cmd.Stdin = bytes.NewReader([]byte(fmt.Sprintf(`
FROM packs/v3:detect
USER root
RUN sed -i 's/"packs\/v3"/"host-machine.local:%s\/packs\/v3"/' /buildpacks/order.toml
USER packs
`, registryPort)))
run(t, cmd)
})

it.After(func() {
for _, name := range []string{"detect", "analyze", "build", "export", "run"} {
exec.Command("docker", "rmi", fmt.Sprintf("host-machine.local:%s/packs/v3:%s", registryPort, name)).Run()
}
})

it("builds and exports an image", func() {
repo := "some-org/" + randString(10)
repoName := fmt.Sprintf("localhost:%s/%s", registryPort, repo)

t.Log("run pack build")
cmd := exec.Command(pack, "build", fmt.Sprintf("host-machine.local:%s/%s", registryPort, repo), "-p", sourceCodePath, "-s", fmt.Sprintf("host-machine.local:%s/packs/v3", registryPort), "--publish")
cmd := exec.Command(
pack, "build",
fmt.Sprintf("host-machine.local:%s/%s", registryPort, repo),
"-p", sourceCodePath,
"--detect-image", fmt.Sprintf("host-machine.local:%s/packs/v3:detect", registryPort),
"--analyze-image", fmt.Sprintf("host-machine.local:%s/packs/v3:analyze", registryPort),
"--export-image", fmt.Sprintf("host-machine.local:%s/packs/v3:export", registryPort),
"--publish",
)
cmd.Env = append(os.Environ(), "HOME="+homeDir)
run(t, cmd)

Expand Down
40 changes: 34 additions & 6 deletions build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pack

import (
"bytes"
"crypto/md5"
"fmt"
"io"
Expand All @@ -9,9 +10,11 @@ import (
"os/exec"
"path/filepath"
"runtime"

"github.com/BurntSushi/toml"
)

func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) error {
func Build(appDir, detectImage, analyzeImage, exportImage, repoName, hostMachineIP string, publish bool) error {
tempDir, err := ioutil.TempDir("/tmp", "lifecycle.pack.build.")
if err != nil {
return err
Expand All @@ -38,7 +41,7 @@ func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) erro
}

fmt.Println("*** DETECTING:")
cmd := exec.Command("docker", "run", "-v", filepath.Join(tempDir, "launch", "app")+":/launch/app", "-v", filepath.Join(tempDir, "workspace")+":/workspace", stackName+":detect")
cmd := exec.Command("docker", "run", "-v", filepath.Join(tempDir, "launch", "app")+":/launch/app", "-v", filepath.Join(tempDir, "workspace")+":/workspace", detectImage)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand All @@ -54,7 +57,7 @@ func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) erro
"-v", filepath.Join(tempDir, "docker-config.json") + ":/etc/docker/daemon.json",
"-v", filepath.Join(tempDir, "launch") + ":/launch",
"-v", filepath.Join(tempDir, "workspace") + ":/workspace:ro",
stackName + ":analyze",
analyzeImage,
}
if hostMachineIP != "" {
args = append([]string{args[0], "--add-host", "host-machine.local:" + hostMachineIP}, args[1:]...)
Expand All @@ -68,13 +71,19 @@ func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) erro
return err
}

// Read groupRepoImage from ENV:PACK_BP_GROUP_PATH
groupRepoImage, err := groupTomlRepository(tempDir, detectImage)
if err != nil {
return err
}

fmt.Println("*** BUILDING:")
cmd = exec.Command("docker", "run",
"-v", filepath.Join(tempDir, "launch")+":/launch",
"-v", filepath.Join(tempDir, "workspace")+":/workspace",
"-v", cacheDir+":/cache",
"-v", filepath.Join(tempDir, "platform")+":/platform",
stackName+":build",
groupRepoImage+":build",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -91,8 +100,8 @@ func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) erro
"-v", filepath.Join(tempDir, "docker-config.json") + ":/etc/docker/daemon.json",
"-v", filepath.Join(tempDir, "launch") + ":/launch:ro",
"-v", filepath.Join(tempDir, "workspace") + ":/workspace:ro",
stackName + ":export",
"-stack", stackName,
exportImage,
"-stack", groupRepoImage,
}
if hostMachineIP != "" {
args = append([]string{args[0], "--add-host", "host-machine.local:" + hostMachineIP}, args[1:]...)
Expand All @@ -110,6 +119,25 @@ func Build(appDir, stackName, repoName, hostMachineIP string, publish bool) erro
return nil
}

func groupTomlRepository(tempDir, detectImage string) (string, error) {
var buf bytes.Buffer
cmd := exec.Command("docker", "run", "-v", filepath.Join(tempDir, "workspace")+":/workspace:ro", "--entrypoint", "", detectImage, "bash", "-c", "cat $PACK_BP_GROUP_PATH")
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}

var groupToml struct {
Repository string `toml:"repository"`
}
if _, err := toml.Decode(buf.String(), &groupToml); err != nil {
return "", err
}

return groupToml.Repository, nil
}

func cacheDir(appDir string) (string, error) {
homeDir := os.Getenv("HOME")
if runtime.GOOS == "windows" {
Expand Down
8 changes: 5 additions & 3 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import (
func main() {
wd, _ := os.Getwd()

var appDir, stackName string
var appDir, detectImage, analyzeImage, exportImage string
var publish bool
buildCommand := &cobra.Command{
Use: "build [IMAGE NAME]",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repoName := args[0]
return pack.Build(appDir, stackName, repoName, os.Getenv("PACK_HOST_MACHINE_IP"), publish)
return pack.Build(appDir, detectImage, analyzeImage, exportImage, repoName, os.Getenv("PACK_HOST_MACHINE_IP"), publish)
},
}
buildCommand.Flags().StringVarP(&appDir, "path", "p", wd, "path to app dir")
buildCommand.Flags().StringVarP(&stackName, "stack", "s", "packs/v3", "stack")
buildCommand.Flags().StringVar(&detectImage, "detect-image", "packs/v3:detect", "detect image")
buildCommand.Flags().StringVar(&analyzeImage, "analyze-image", "packs/v3:analyze", "detect image")
buildCommand.Flags().StringVar(&exportImage, "export-image", "packs/v3:export", "detect image")
buildCommand.Flags().BoolVarP(&publish, "publish", "r", false, "publish to registry")

rootCmd := &cobra.Command{Use: "pack"}
Expand Down

0 comments on commit 153cc40

Please sign in to comment.