remove redundant keep_input_artifact from compress pp and clarify keep behavior in shell-local pp

This commit is contained in:
Megan Marsh 2019-04-03 12:03:40 -07:00
parent 056fcb7cea
commit 1b77b05ce2
2 changed files with 17 additions and 15 deletions

View File

@ -37,10 +37,9 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
// Fields from config file // Fields from config file
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
Format string `mapstructure:"format"` Format string `mapstructure:"format"`
CompressionLevel int `mapstructure:"compression_level"` CompressionLevel int `mapstructure:"compression_level"`
KeepInputArtifact bool `mapstructure:"keep_input_artifact"`
// Derived fields // Derived fields
Archive string Archive string
@ -115,7 +114,6 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
fmt.Println(target) fmt.Println(target)
} }
keep := p.config.KeepInputArtifact
newArtifact := &Artifact{Path: target} newArtifact := &Artifact{Path: target}
if err = os.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil { if err = os.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil {
@ -168,19 +166,19 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ui.Say(fmt.Sprintf("Tarring %s with %s", target, compression)) ui.Say(fmt.Sprintf("Tarring %s with %s", target, compression))
err = createTarArchive(artifact.Files(), output) err = createTarArchive(artifact.Files(), output)
if err != nil { if err != nil {
return nil, keep, false, fmt.Errorf("Error creating tar: %s", err) return nil, false, false, fmt.Errorf("Error creating tar: %s", err)
} }
case "zip": case "zip":
ui.Say(fmt.Sprintf("Zipping %s", target)) ui.Say(fmt.Sprintf("Zipping %s", target))
err = createZipArchive(artifact.Files(), output) err = createZipArchive(artifact.Files(), output)
if err != nil { if err != nil {
return nil, keep, false, fmt.Errorf("Error creating zip: %s", err) return nil, false, false, fmt.Errorf("Error creating zip: %s", err)
} }
default: default:
// Filename indicates no tarball (just compress) so we'll do an io.Copy // Filename indicates no tarball (just compress) so we'll do an io.Copy
// into our compressor. // into our compressor.
if len(artifact.Files()) != 1 { if len(artifact.Files()) != 1 {
return nil, keep, false, fmt.Errorf( return nil, false, false, fmt.Errorf(
"Can only have 1 input file when not using tar/zip. Found %d "+ "Can only have 1 input file when not using tar/zip. Found %d "+
"files: %v", len(artifact.Files()), artifact.Files()) "files: %v", len(artifact.Files()), artifact.Files())
} }
@ -189,21 +187,21 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
source, err := os.Open(archiveFile) source, err := os.Open(archiveFile)
if err != nil { if err != nil {
return nil, keep, false, fmt.Errorf( return nil, false, false, fmt.Errorf(
"Failed to open source file %s for reading: %s", "Failed to open source file %s for reading: %s",
archiveFile, err) archiveFile, err)
} }
defer source.Close() defer source.Close()
if _, err = io.Copy(output, source); err != nil { if _, err = io.Copy(output, source); err != nil {
return nil, keep, false, fmt.Errorf("Failed to compress %s: %s", return nil, false, false, fmt.Errorf("Failed to compress %s: %s",
archiveFile, err) archiveFile, err)
} }
} }
ui.Say(fmt.Sprintf("Archive %s completed", target)) ui.Say(fmt.Sprintf("Archive %s completed", target))
return newArtifact, keep, false, nil return newArtifact, false, false, nil
} }
func (config *Config) detectFromFilename() { func (config *Config) detectFromFilename() {

View File

@ -39,10 +39,14 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
// this particular post-processor doesn't do anything with the artifact // this particular post-processor doesn't do anything with the artifact
// except to return it. // except to return it.
retBool, retErr := sl.Run(ui, &p.config) success, retErr := sl.Run(ui, &p.config)
if !retBool { if !success {
return nil, retBool, false, retErr return nil, false, false, retErr
} }
return artifact, retBool, false, retErr // Force shell-local pp to keep the input artifact, because otherwise we'll
// lose it instead of being able to pass it through. If oyu want to delete
// the input artifact for a shell local pp, use the artifice pp to create a
// new artifact
return artifact, true, true, retErr
} }