Skip to content

Commit

Permalink
Adds "--dryrun" flag to exporter binary
Browse files Browse the repository at this point in the history
* when --dryrun=<dir> is specified the exporter will write new layers and
metadata to <dir> but will not export an image

Signed-off-by: Emily Casey <ecasey@pivotal.io>
Signed-off-by: Dave Goddard <dave@goddard.id.au>
  • Loading branch information
dgodd authored and ekcasey committed Nov 15, 2018
1 parent b2745f1 commit 1479ed5
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 214 deletions.
4 changes: 4 additions & 0 deletions cmd/cmd.go
Expand Up @@ -34,6 +34,10 @@ func FlagLaunchDirSrc(dir *string) {
flag.StringVar(dir, "launch-src", DefaultLaunchDir, "path to source launch directory for export step")
}

func FlagDryRunDir(dir *string) {
flag.StringVar(dir, "dryrun", "", "path to store first stage output in. (Don't perform export)")
}

func FlagAppDir(dir *string) {
flag.StringVar(dir, "app", DefaultAppDir, "path to app directory")
}
Expand Down
69 changes: 45 additions & 24 deletions cmd/exporter/main.go
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/BurntSushi/toml"

"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/cmd"
"github.com/buildpack/lifecycle/img"
Expand All @@ -18,6 +17,7 @@ var (
runImage string
launchDir string
launchDirSrc string
dryrun string
appDir string
appDirSrc string
groupPath string
Expand All @@ -33,6 +33,7 @@ func init() {
cmd.FlagLaunchDirSrc(&launchDirSrc)
cmd.FlagAppDir(&appDir)
cmd.FlagAppDirSrc(&appDirSrc)
cmd.FlagDryRunDir(&dryrun)
cmd.FlagGroupPath(&groupPath)
cmd.FlagUseDaemon(&useDaemon)
cmd.FlagUseCredHelpers(&useHelpers)
Expand All @@ -43,14 +44,55 @@ func init() {
func main() {
flag.Parse()
if flag.NArg() > 1 || flag.Arg(0) == "" || runImage == "" {
args := map[string]interface{}{"narg": flag.NArg, "runImage": runImage, "launchDir": launchDir}
args := map[string]interface{}{"narg": flag.NArg(), "runImage": runImage, "launchDir": launchDir}
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "parse arguments", fmt.Sprintf("%+v", args)))
}
repoName = flag.Arg(0)
cmd.Exit(export())
}

func export() error {
var group lifecycle.BuildpackGroup
var err error
if _, err := toml.DecodeFile(groupPath, &group); err != nil {
return cmd.FailErr(err, "read group")
}

exporter := &lifecycle.Exporter{
Buildpacks: group.Buildpacks,
Out: os.Stdout,
Err: os.Stderr,
UID: uid,
GID: gid,
}

if dryrun != "" {
exporter.TmpDir = dryrun
if err := os.MkdirAll(exporter.TmpDir, 0777); err != nil {
return cmd.FailErr(err, "create temp directory")
}
} else {
exporter.TmpDir, err = ioutil.TempDir("", "lifecycle.exporter.layer")
if err != nil {
return cmd.FailErr(err, "create temp directory")
}
defer os.RemoveAll(exporter.TmpDir)
}

_, err = exporter.PrepareExport(
launchDirSrc,
launchDir,
appDirSrc,
appDir,
)
if err != nil {
return cmd.FailErrCode(err, cmd.CodeFailedBuild)
}

if dryrun != "" {
return nil
}

if useHelpers {
if err := img.SetupCredHelpers(repoName, runImage); err != nil {
return cmd.FailErr(err, "setup credential helpers")
Expand Down Expand Up @@ -88,30 +130,9 @@ func export() error {
origImage = nil
}

var group lifecycle.BuildpackGroup
if _, err := toml.DecodeFile(groupPath, &group); err != nil {
return cmd.FailErr(err, "read group")
}

tmpDir, err := ioutil.TempDir("", "lifecycle.exporter.layer")
if err != nil {
return cmd.FailErr(err, "create temp directory")
}
defer os.RemoveAll(tmpDir)

exporter := &lifecycle.Exporter{
Buildpacks: group.Buildpacks,
TmpDir: tmpDir,
Out: os.Stdout,
Err: os.Stderr,
UID: uid,
GID: gid,
}
newImage, err := exporter.Export(
newImage, err := exporter.ExportImage(
launchDirSrc,
launchDir,
appDirSrc,
appDir,
stackImage,
origImage,
)
Expand Down

0 comments on commit 1479ed5

Please sign in to comment.