Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
Fix staging permissions issues + bump to 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sclevine committed Mar 23, 2017
1 parent 4957c0b commit 0b6d25b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.0
0.6.0
4 changes: 2 additions & 2 deletions local/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ func (r *Runner) buildContainerConfig(config *AppConfig, forwardConfig *service.
}

scriptBuffer := &bytes.Buffer{}
err = template.Must(template.New("").Parse(runnerScript)).Execute(scriptBuffer, options)
if err != nil {

if err := template.Must(template.New("").Parse(runnerScript)).Execute(scriptBuffer, options); err != nil {
return nil, err
}

Expand Down
21 changes: 15 additions & 6 deletions local/stager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"path/filepath"
"strconv"
"strings"
"text/template"

Expand All @@ -18,6 +19,14 @@ import (
"github.com/sclevine/cflocal/utils"
)

const stagerScript = `
set -e
chown -R vcap:vcap /tmp/app
exec su vcap -p -c "PATH=$PATH exec /tmp/lifecycle/builder -buildpackOrder $0 -skipDetect=$1"
`

type Stager struct {
DiegoVersion string
GoVersion string
Expand Down Expand Up @@ -104,14 +113,14 @@ func (s *Stager) Stage(config *StageConfig, color Colorizer) (droplet Stream, er
Name: name + "-stage",
Config: &container.Config{
Hostname: "cflocal",
User: "vcap",
User: "root",
Env: mapToEnv(mergeMaps(env, config.AppConfig.StagingEnv, config.AppConfig.Env)),
Image: "cflocal",
WorkingDir: "/home/vcap",
Entrypoint: strslice.StrSlice{
"/tmp/lifecycle/builder",
"-buildpackOrder", strings.Join(config.Buildpacks, ","),
fmt.Sprintf("-skipDetect=%t", len(config.Buildpacks) == 1),
"/bin/bash", "-c", stagerScript,
strings.Join(config.Buildpacks, ","),
strconv.FormatBool(len(config.Buildpacks) == 1),
},
},
Docker: s.Docker,
Expand Down Expand Up @@ -160,7 +169,7 @@ func (s *Stager) Stage(config *StageConfig, color Colorizer) (droplet Stream, er
return Stream{}, err
}
droplet.ReadCloser = dropletTar // allows removal in error case
dropletReader, err := utils.FileFromTar("droplet", dropletTar)
dropletReader, _, err := utils.FileFromTar("droplet", dropletTar)
if err != nil {
return Stream{}, err
}
Expand Down Expand Up @@ -193,7 +202,7 @@ func (s *Stager) Download(path string) (stream Stream, err error) {
return Stream{}, err
}
stream.ReadCloser = tar // allows deferred removal in error case
reader, err := utils.FileFromTar(filename, tar)
reader, _, err := utils.FileFromTar(filename, tar)
if err != nil {
return Stream{}, err
}
Expand Down
26 changes: 19 additions & 7 deletions local/stager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package local_test

import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
Expand Down Expand Up @@ -72,16 +73,27 @@ var _ = Describe("Stager", func() {
Expect(logs).To(gbytes.Say(`\[some-app\] % \S+ Compile message from stdout\.`))

Expect(droplet.Size).To(BeNumerically(">", 500))
Expect(droplet.Size).To(BeNumerically("<", 1000))
Expect(droplet.Size).To(BeNumerically("<", 1500))

dropletTar, err := gzip.NewReader(droplet)
Expect(err).NotTo(HaveOccurred())
dropletBuffer, err := ioutil.ReadAll(dropletTar)
Expect(err).NotTo(HaveOccurred())

Expect(fileFromTar("./app/some-file", dropletBuffer)).To(Equal("some-contents"))
Expect(fileFromTar("./staging_info.yml", dropletBuffer)).To(ContainSubstring("start_command"))
Expect(fileFromTar("./app/env", dropletBuffer)).To(Equal(stagingEnvFixture))
file1, header1 := fileFromTar("./app/some-file", dropletBuffer)
Expect(file1).To(Equal("some-contents"))
Expect(header1.Uid).To(Equal(2000))
Expect(header1.Gid).To(Equal(2000))

file2, header2 := fileFromTar("./staging_info.yml", dropletBuffer)
Expect(file2).To(ContainSubstring("start_command"))
Expect(header2.Uid).To(Equal(2000))
Expect(header2.Gid).To(Equal(2000))

file3, header3 := fileFromTar("./app/env", dropletBuffer)
Expect(file3).To(Equal(stagingEnvFixture))
Expect(header3.Uid).To(Equal(2000))
Expect(header3.Gid).To(Equal(2000))

// TODO: test that no "some-app-staging-GUID" containers exist

Expand Down Expand Up @@ -120,10 +132,10 @@ var _ = Describe("Stager", func() {
})
})

func fileFromTar(path string, tarball []byte) string {
file, err := utils.FileFromTar(path, bytes.NewReader(tarball))
func fileFromTar(path string, tarball []byte) (string, *tar.Header) {
file, header, err := utils.FileFromTar(path, bytes.NewReader(tarball))
ExpectWithOffset(1, err).NotTo(HaveOccurred())
contents, err := ioutil.ReadAll(file)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return string(contents)
return string(contents), header
}
1 change: 0 additions & 1 deletion utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func (f *FS) Tar(path string) (io.ReadCloser, error) {
}
return archive.TarWithOptions(absPath, &archive.TarOptions{
ExcludePatterns: []string{filepath.Join(path, "*.droplet")},
ChownOpts: &archive.TarChownOptions{UID: 2000, GID: 2000},
})
}

Expand Down
8 changes: 4 additions & 4 deletions utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func TarFile(name string, contents io.Reader, size, mode int64) (io.Reader, erro
return tarBuffer, nil
}

func FileFromTar(name string, archive io.Reader) (io.Reader, error) {
func FileFromTar(name string, archive io.Reader) (file io.Reader, header *tar.Header, err error) {
tarball := tar.NewReader(archive)
for {
header, err := tarball.Next()
header, err = tarball.Next()
if err != nil {
return nil, err
return nil, nil, err
}
if header.Name == name {
break
}
}
return tarball, nil
return tarball, header, nil
}

0 comments on commit 0b6d25b

Please sign in to comment.