From b697bc5ece790c7b15b54aa81f31717eba10a179 Mon Sep 17 00:00:00 2001 From: John Southworth Date: Wed, 20 Nov 2019 11:14:07 -0800 Subject: [PATCH] add support for preserving the container on exit. --- buildimage.go | 13 +++++++++++++ cmd/danos-buildimage/main.go | 3 +++ 2 files changed, 16 insertions(+) diff --git a/buildimage.go b/buildimage.go index cb5b3f3..92de592 100644 --- a/buildimage.go +++ b/buildimage.go @@ -21,6 +21,8 @@ type Builder struct { srcDir string pkgDir string + cleanContainer bool + containerID string } @@ -62,9 +64,16 @@ func PreferredPackageDirectory(pkgDir string) MakeBuilderOption { } } +func RemoveContainer(remove bool) MakeBuilderOption { + return func(b *Builder) { + b.cleanContainer = remove + } +} + func MakeBuilder(opts ...MakeBuilderOption) (*Builder, error) { b := new(Builder) b.version = "latest" + b.cleanContainer = true for _, opt := range opts { opt(b) } @@ -184,6 +193,10 @@ func (b *Builder) deleteEnvironment() error { if b.containerID == "" { return nil } + if !b.cleanContainer { + log.Println("preserved environment", b.containerID) + return nil + } log.Println("deleting environment", b.containerID) return b.cli.ContainerRemove( b.ctx, diff --git a/cmd/danos-buildimage/main.go b/cmd/danos-buildimage/main.go index 71225dc..e829108 100644 --- a/cmd/danos-buildimage/main.go +++ b/cmd/danos-buildimage/main.go @@ -10,12 +10,14 @@ import ( ) var srcDir, destDir, pkgDir, version string +var noClean bool func init() { flag.StringVar(&srcDir, "src", "", "source directory") flag.StringVar(&destDir, "dest", "..", "destination directory") flag.StringVar(&pkgDir, "pkg", "", "preferred package directory") flag.StringVar(&version, "version", "latest", "version of danos to build for") + flag.BoolVar(&noClean, "no-clean", false, "don't delete the container when done") } func resolvePath(in string) string { @@ -36,6 +38,7 @@ func main() { bimg.SourceDirectory(resolvePath(srcDir)), bimg.DestinationDirectory(resolvePath(destDir)), bimg.PreferredPackageDirectory(resolvePath(pkgDir)), + bimg.RemoveContainer(!noClean), bimg.Version(version), ) if err != nil {