From 750a9c61de5133f627f6ea5051479f1eecf4452f Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Tue, 18 Aug 2015 14:38:32 -0700 Subject: [PATCH] Added discard option for docker builder, also reorganized some error messages --- builder/docker/builder.go | 16 ++++++++++++---- builder/docker/config.go | 28 ++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/builder/docker/builder.go b/builder/docker/builder.go index 89880aacc..702d530ce 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -9,8 +9,10 @@ import ( "github.com/mitchellh/packer/packer" ) -const BuilderId = "packer.docker" -const BuilderIdImport = "packer.post-processor.docker-import" +const ( + BuilderId = "packer.docker" + BuilderIdImport = "packer.post-processor.docker-import" +) type Builder struct { config *Config @@ -54,10 +56,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepProvision{}, } - if b.config.Commit { + if b.config.Discard { + log.Print("[DEBUG] Container will be discarded") + } else if b.config.Commit { + log.Print("[DEBUG] Container will be committed") steps = append(steps, new(StepCommit)) - } else { + } else if b.config.ExportPath != "" { + log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath) steps = append(steps, new(StepExport)) + } else { + return nil, ErrArtifactNotUsed } // Setup the state bag and initial state for the steps diff --git a/builder/docker/config.go b/builder/docker/config.go index 36322080c..dc205eaf0 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -12,23 +12,33 @@ import ( "github.com/mitchellh/packer/template/interpolate" ) +var ( + ErrArtifactNotUsed = fmt.Errorf("No instructions given for handling the artifact; expected commit, discard, or export_path") + ErrArtifactUseConflict = fmt.Errorf("Cannot specify more than one of commit, discard, and export_path") + ErrExportPathNotFile = fmt.Errorf("export_path must be a file, not a directory") + ErrImageNotSpecified = fmt.Errorf("Image must be specified") +) + type Config struct { common.PackerConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"` Commit bool + Discard bool ExportPath string `mapstructure:"export_path"` Image string + Pty bool Pull bool RunCommand []string `mapstructure:"run_command"` Volumes map[string]string + // This is used to login to dockerhub to pull a private base container + // For pushing to dockerhub, see the docker post-processors Login bool LoginEmail string `mapstructure:"login_email"` - LoginUsername string `mapstructure:"login_username"` LoginPassword string `mapstructure:"login_password"` LoginServer string `mapstructure:"login_server"` - Pty bool + LoginUsername string `mapstructure:"login_username"` ctx interpolate.Context } @@ -84,18 +94,20 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } if c.Image == "" { errs = packer.MultiErrorAppend(errs, - fmt.Errorf("image must be specified")) + ErrImageNotSpecified) } - if c.ExportPath != "" && c.Commit { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("both commit and export_path cannot be set")) + if (c.ExportPath != "" && c.Commit) || (c.ExportPath != "" && c.Discard) || (c.Commit && c.Discard) { + errs = packer.MultiErrorAppend(errs, ErrArtifactUseConflict) + } + + if c.ExportPath == "" && !c.Commit && !c.Discard { + errs = packer.MultiErrorAppend(errs, ErrArtifactNotUsed) } if c.ExportPath != "" { if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() { - errs = packer.MultiErrorAppend(errs, fmt.Errorf( - "export_path must be a file, not a directory")) + errs = packer.MultiErrorAppend(errs, ErrExportPathNotFile) } }