Skip to content

Commit

Permalink
Merge pull request #149 from buildpack/dist-spec-and-build-plan
Browse files Browse the repository at this point in the history
Implement distribution spec and contractual build plan RFCs
  • Loading branch information
sclevine authored Aug 7, 2019
2 parents 864e362 + 22c8aab commit f16cbd7
Show file tree
Hide file tree
Showing 60 changed files with 1,639 additions and 1,098 deletions.
31 changes: 11 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
sudo: required
services:
- docker

install:
- set -e

jobs:
include:
- stage: unit test
language: go
go:
- 1.12.x
env:
- GO111MODULE=on
go_import_path: github.com/buildpack/lifecycle
script: |
make test
language: go
go:
- 1.12.x
before_script:
- mkdir /tmp/yj
- wget /~https://github.com/sclevine/yj/releases/download/v4.0.0/yj-linux -O /tmp/yj/yj
- chmod +x "/tmp/yj/yj"
- export PATH=/tmp/yj:$PATH
script:
- make test
branches:
only:
- master
- master
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export GO111MODULE = on

GOCMD?=go
GOENV=GOOS=linux GOARCH=amd64 CGO_ENABLED=0
GOBUILD=$(GOCMD) build -mod=vendor -ldflags "-X 'github.com/buildpack/lifecycle.Version=$(LIFECYCLE_VERSION)' -X 'github.com/buildpack/lifecycle.SCMRepository=$(SCM_REPO)' -X 'github.com/buildpack/lifecycle.SCMCommit=$(SCM_COMMIT)'"
Expand All @@ -23,6 +24,9 @@ imports:
$(GOCMD) install -mod=vendor golang.org/x/tools/cmd/goimports
test -z $$(goimports -l -w -local github.com/buildpack/lifecycle $$(find . -type f -name '*.go' -not -path "./vendor/*"))

tools:
$(GOCMD) install -mod=vendor github.com/sclevine/yj

format:
test -z $$($(GOCMD) fmt ./...)

Expand All @@ -31,10 +35,10 @@ vet:

test: unit acceptance

unit: format imports vet
unit: format imports tools vet
$(GOTEST) -v -count=1 ./...

acceptance: format imports vet
acceptance: format imports tools vet
$(GOTEST) -v -count=1 -tags=acceptance ./acceptance/...

clean:
Expand Down
4 changes: 2 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Analyzer struct {
AnalyzedPath string
AppDir string
Buildpacks []*Buildpack
Buildpacks []Buildpack
GID, UID int
LayersDir string
Out, Err *log.Logger
Expand All @@ -33,7 +33,7 @@ func (a *Analyzer) Analyze(image imgutil.Image) (metadata.AnalyzedMetadata, erro

if !a.SkipLayers {
for _, buildpack := range a.Buildpacks {
bpLayersDir, err := readBuildpackLayersDir(a.LayersDir, *buildpack)
bpLayersDir, err := readBuildpackLayersDir(a.LayersDir, buildpack)
if err != nil {
return metadata.AnalyzedMetadata{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
appDir = filepath.Join(layerDir, "some-app-dir")
stdout, stderr = &bytes.Buffer{}, &bytes.Buffer{}
analyzer = &lifecycle.Analyzer{
Buildpacks: []*lifecycle.Buildpack{{ID: "metdata.buildpack"}, {ID: "no.cache.buildpack"}, {ID: "no.metadata.buildpack"}},
Buildpacks: []lifecycle.Buildpack{{ID: "metdata.buildpack"}, {ID: "no.cache.buildpack"}, {ID: "no.metadata.buildpack"}},
AppDir: appDir,
LayersDir: layerDir,
AnalyzedPath: filepath.Join(tmpDir, "some-previous-file.toml"),
Expand Down Expand Up @@ -181,7 +181,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
})

it("should only write layer TOML files that correspond to detected buildpacks", func() {
analyzer.Buildpacks = []*lifecycle.Buildpack{{ID: "no.cache.buildpack"}}
analyzer.Buildpacks = []lifecycle.Buildpack{{ID: "no.cache.buildpack"}}

if _, err := analyzer.Analyze(image); err != nil {
t.Fatalf("Error: %s\n", err)
Expand Down
78 changes: 38 additions & 40 deletions archive/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func WriteTarArchive(w io.Writer, srcDir string, uid, gid int) error {
tw := tar.NewWriter(w)
defer tw.Close()

err := writeParentDirectoryHeaders(srcDir, tw, uid, gid)
err := addParentDirs(srcDir, tw, uid, gid)
if err != nil {
return err
}
Expand All @@ -40,22 +40,21 @@ func WriteTarArchive(w io.Writer, srcDir string, uid, gid int) error {
if err != nil {
return err
}
if fi.Mode()&os.ModeSocket != 0 {
return nil
}
var header *tar.Header
var target string
if fi.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(file)
if err != nil {
return err
}
header, err = tar.FileInfoHeader(fi, target)
if err != nil {
return err
}
} else {
header, err = tar.FileInfoHeader(fi, fi.Name())
target, err = os.Readlink(file)
if err != nil {
return err
}
}
header, err = tar.FileInfoHeader(fi, target)
if err != nil {
return err
}
header.Name = file
header.ModTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)
header.Uid = uid
Expand All @@ -80,40 +79,36 @@ func WriteTarArchive(w io.Writer, srcDir string, uid, gid int) error {
})
}

func writeParentDirectoryHeaders(tarDir string, tw *tar.Writer, uid int, gid int) error {
func addParentDirs(tarDir string, tw *tar.Writer, uid, gid int) error {
parent := filepath.Dir(tarDir)
if parent == "." || parent == "/" {
return nil
} else {
if err := writeParentDirectoryHeaders(parent, tw, uid, gid); err != nil {
return err
}
}

info, err := os.Stat(parent)
if err != nil {
return err
}
if err := addParentDirs(parent, tw, uid, gid); err != nil {
return err
}

header, err := tar.FileInfoHeader(info, parent)
if err != nil {
return err
}
header.Name = parent
header.ModTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)
info, err := os.Stat(parent)
if err != nil {
return err
}

if err := tw.WriteHeader(header); err != nil {
return err
}
return nil
header, err := tar.FileInfoHeader(info, parent)
if err != nil {
return err
}
header.Name = parent
header.ModTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)

return tw.WriteHeader(header)
}

func Untar(r io.Reader, dest string) error {
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
return nil
}
if err != nil {
Expand All @@ -130,20 +125,13 @@ func Untar(r io.Reader, dest string) error {
case tar.TypeReg, tar.TypeRegA:
_, err := os.Stat(filepath.Dir(path))
if os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return err
}
}

fh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdr.FileInfo().Mode())
if err != nil {
return err
}
if _, err := io.Copy(fh, tr); err != nil {
fh.Close()
if err := writeFile(tr, path, hdr.FileInfo().Mode()); err != nil {
return err
}
fh.Close()
case tar.TypeSymlink:
if err := os.Symlink(hdr.Linkname, path); err != nil {
return err
Expand All @@ -153,3 +141,13 @@ func Untar(r io.Reader, dest string) error {
}
}
}

func writeFile(in io.Reader, path string, mode os.FileMode) error {
fh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
if err != nil {
return err
}
defer fh.Close()
_, err = io.Copy(fh, in)
return err
}
8 changes: 0 additions & 8 deletions bin/format

This file was deleted.

Loading

0 comments on commit f16cbd7

Please sign in to comment.