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

Performance improvements for export and analyze #28

Merged
merged 4 commits into from
Sep 13, 2018
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
6 changes: 3 additions & 3 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func testPack(t *testing.T, when spec.G, it spec.S) {

when("'--publish' flag is not specified'", func() {
it("builds and exports an image", func() {
cmd := exec.Command(pack, "build", repoName, "-p", sourceCodePath, "--detect-image", "packsdev/v3:detect")
cmd := exec.Command(pack, "build", repoName, "-p", sourceCodePath)
cmd.Env = append(os.Environ(), "HOME="+homeDir)
run(t, cmd)

Expand All @@ -135,7 +135,7 @@ func testPack(t *testing.T, when spec.G, it spec.S) {
it("builds and exports an image", func() {
runPackBuild := func() string {
t.Helper()
cmd := exec.Command(pack, "build", repoName, "-p", sourceCodePath, "--detect-image", "packsdev/v3:detect", "--publish")
cmd := exec.Command(pack, "build", repoName, "-p", sourceCodePath, "--publish")
cmd.Env = append(os.Environ(), "HOME="+homeDir)
return run(t, cmd)
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func testPack(t *testing.T, when spec.G, it spec.S) {
})
}, spec.Parallel(), spec.Report(report.Terminal{}))

when("create, build, run", func() {
when.Pend("create, build, run", func() {
var tmpDir, detectImageName, buildImageName, repoName, containerName, registryContainerName, registry string

it.Before(func() {
Expand Down
56 changes: 39 additions & 17 deletions analyzer.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
package pack

import (
"context"
"encoding/json"
"os"

"github.com/buildpack/lifecycle"
"github.com/buildpack/packs"
dockercli "github.com/docker/docker/client"
)

func analyzer(group lifecycle.BuildpackGroup, launchDir, repoName string, useDaemon bool) error {
origImage, err := readImage(repoName, useDaemon)
if err != nil {
return err
}

if origImage == nil {
// no previous image to analyze
return nil
}

func analyzer(group lifecycle.BuildpackGroup, workspaceDir, repoName string, useDaemon bool) error {
analyzer := &lifecycle.Analyzer{
Buildpacks: group.Buildpacks,
Out: os.Stdout,
Err: os.Stderr,
}
err = analyzer.Analyze(
launchDir,
origImage,
)
if err != nil {
return packs.FailErrCode(err, packs.CodeFailedBuild)

if useDaemon {
cli, err := dockercli.NewEnvClient()
if err != nil {
return packs.FailErrCode(err, packs.CodeFailedBuild)
}
i, _, err := cli.ImageInspectWithRaw(context.Background(), repoName)
if err != nil {
if dockercli.IsErrNotFound(err) {
// images does not already exist, skip analyze
return nil
}
return packs.FailErrCode(err, packs.CodeFailedBuild)
}
var config lifecycle.AppImageMetadata
if err := json.Unmarshal([]byte(i.Config.Labels["sh.packs.build"]), &config); err != nil {
return packs.FailErrCode(err, packs.CodeFailedBuild)
}
if err := analyzer.AnalyzeConfig(workspaceDir, config); err != nil {
return packs.FailErrCode(err, packs.CodeFailedBuild)
}
} else {
origImage, err := readImage(repoName, useDaemon)
if err != nil {
return err
}

if origImage == nil {
// no previous image to analyze
return nil
}

if err := analyzer.Analyze(workspaceDir, origImage); err != nil {
return packs.FailErrCode(err, packs.CodeFailedBuild)
}
}

return nil
Expand Down
80 changes: 44 additions & 36 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (
"github.com/google/uuid"
)

func Build(appDir, detectImage, repoName string, publish bool) error {
func Build(appDir, buildImage, runImage, repoName string, publish bool) error {
return (&BuildFlags{
AppDir: appDir,
DetectImage: detectImage,
RepoName: repoName,
Publish: publish,
AppDir: appDir,
BuildImage: buildImage,
RunImage: runImage,
RepoName: repoName,
Publish: publish,
}).Run()
}

type BuildFlags struct {
AppDir string
DetectImage string
RepoName string
Publish bool
AppDir string
BuildImage string
RunImage string
RepoName string
Publish bool
}

func (b *BuildFlags) Run() error {
Expand All @@ -38,26 +40,24 @@ func (b *BuildFlags) Run() error {
}

uid := uuid.New().String()
launchVolume := fmt.Sprintf("pack-launch-%x", uid)
workspaceVolume := fmt.Sprintf("pack-workspace-%x", uid)
cacheVolume := fmt.Sprintf("pack-cache-%x", md5.Sum([]byte(b.AppDir)))
defer exec.Command("docker", "volume", "rm", "-f", launchVolume).Run()
defer exec.Command("docker", "volume", "rm", "-f", workspaceVolume).Run()

// fmt.Println("*** COPY APP TO VOLUME:")
if err := copyToVolume(b.DetectImage, launchVolume, b.AppDir, "app"); err != nil {
fmt.Println("*** COPY APP TO VOLUME:")
if err := copyToVolume(b.BuildImage, workspaceVolume, b.AppDir, "app"); err != nil {
return err
}

fmt.Println("*** DETECTING:")
cmd := exec.Command("docker", "run", "--rm", "-v", launchVolume+":/launch", "-v", workspaceVolume+":/workspace", b.DetectImage)
cmd := exec.Command("docker", "run", "--rm", "-v", workspaceVolume+":/workspace", b.BuildImage, "/lifecycle/detector")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}

group, err := groupToml(workspaceVolume, b.DetectImage)
group, err := groupToml(workspaceVolume, b.BuildImage)
if err != nil {
return err
}
Expand All @@ -71,17 +71,17 @@ func (b *BuildFlags) Run() error {
if err := analyzer(group, analyzeTmpDir, b.RepoName, !b.Publish); err != nil {
return err
}
if err := copyToVolume(b.DetectImage, launchVolume, analyzeTmpDir, ""); err != nil {
if err := copyToVolume(b.BuildImage, workspaceVolume, analyzeTmpDir, ""); err != nil {
return err
}

fmt.Println("*** BUILDING:")
cmd = exec.Command("docker", "run",
"--rm",
"-v", launchVolume+":/launch",
"-v", workspaceVolume+":/workspace",
"-v", cacheVolume+":/cache",
group.BuildImage,
b.BuildImage,
"/lifecycle/builder",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -91,26 +91,34 @@ func (b *BuildFlags) Run() error {

if !b.Publish {
fmt.Println("*** PULLING RUN IMAGE LOCALLY:")
if out, err := exec.Command("docker", "pull", group.RunImage).CombinedOutput(); err != nil {
if out, err := exec.Command("docker", "pull", b.RunImage).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
}

fmt.Println("*** EXPORTING:")
localLaunchDir, cleanup, err := exportVolume(b.DetectImage, launchVolume)
if err != nil {
return err
}
defer cleanup()

imgSHA, err := export(group, localLaunchDir, b.RepoName, group.RunImage, !b.Publish, !b.Publish)
if err != nil {
return err
}

if b.Publish {
localWorkspaceDir, cleanup, err := exportVolume(b.BuildImage, workspaceVolume)
if err != nil {
return err
}
defer cleanup()

imgSHA, err := exportRegistry(&group, localWorkspaceDir, b.RepoName, b.RunImage)
if err != nil {
return err
}
fmt.Printf("\n*** Image: %s@%s\n", b.RepoName, imgSHA)
} else {
var buildpacks []string
for _, b := range group.Buildpacks {
buildpacks = append(buildpacks, b.ID)
}

if err := exportDaemon(buildpacks, workspaceVolume, b.RepoName, b.RunImage); err != nil {
return err
}
}

return nil
Expand All @@ -124,13 +132,13 @@ func exportVolume(image, volName string) (string, func(), error) {
cleanup := func() { os.RemoveAll(tmpDir) }

containerName := uuid.New().String()
if output, err := exec.Command("docker", "container", "create", "--name", containerName, "-v", volName+":/launch:ro", image).CombinedOutput(); err != nil {
if output, err := exec.Command("docker", "container", "create", "--name", containerName, "-v", volName+":/workspace:ro", image).CombinedOutput(); err != nil {
cleanup()
fmt.Println(string(output))
return "", func() {}, err
}
defer exec.Command("docker", "rm", containerName).Run()
if output, err := exec.Command("docker", "cp", containerName+":/launch/.", tmpDir).CombinedOutput(); err != nil {
if output, err := exec.Command("docker", "cp", containerName+":/workspace/.", tmpDir).CombinedOutput(); err != nil {
cleanup()
fmt.Println(string(output))
return "", func() {}, err
Expand All @@ -141,12 +149,12 @@ func exportVolume(image, volName string) (string, func(), error) {

func copyToVolume(image, volName, srcDir, destDir string) error {
containerName := uuid.New().String()
if output, err := exec.Command("docker", "container", "create", "--user", "0", "--name", containerName, "--entrypoint", "", "-v", volName+":/launch", image, "chown", "-R", "packs:packs", "/launch").CombinedOutput(); err != nil {
if output, err := exec.Command("docker", "container", "create", "--user", "0", "--name", containerName, "--entrypoint", "", "-v", volName+":/workspace", image, "chown", "-R", "pack:pack", "/workspace").CombinedOutput(); err != nil {
fmt.Println(string(output))
return err
}
defer exec.Command("docker", "rm", containerName).Run()
if output, err := exec.Command("docker", "cp", srcDir+"/.", containerName+":"+filepath.Join("/launch", destDir)).CombinedOutput(); err != nil {
if output, err := exec.Command("docker", "cp", srcDir+"/.", containerName+":"+filepath.Join("/workspace", destDir)).CombinedOutput(); err != nil {
fmt.Println(string(output))
return err
}
Expand All @@ -158,9 +166,9 @@ func copyToVolume(image, volName, srcDir, destDir string) error {
return nil
}

func groupToml(workspaceVolume, detectImage string) (lifecycle.BuildpackGroup, error) {
func groupToml(workspaceVolume, buildImage string) (lifecycle.BuildpackGroup, error) {
var buf bytes.Buffer
cmd := exec.Command("docker", "run", "--rm", "-v", workspaceVolume+":/workspace:ro", "--entrypoint", "", detectImage, "bash", "-c", "cat $PACK_BP_GROUP_PATH")
cmd := exec.Command("docker", "run", "--rm", "-v", workspaceVolume+":/workspace:ro", "--entrypoint", "", buildImage, "bash", "-c", "cat $PACK_GROUP_PATH")
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func main() {
},
}
buildCommand.Flags().StringVarP(&buildFlags.AppDir, "path", "p", wd, "path to app dir")
buildCommand.Flags().StringVar(&buildFlags.DetectImage, "detect-image", "packs/v3:detect", "detect image")
buildCommand.Flags().StringVar(&buildFlags.BuildImage, "build-image", "packs/build:0.0.1-rc.170", "build image")
buildCommand.Flags().StringVar(&buildFlags.RunImage, "run-image", "packs/run:0.0.1-rc.170", "run image")
buildCommand.Flags().BoolVar(&buildFlags.Publish, "publish", false, "publish to registry")

var createFlags pack.Create
Expand Down
Loading