Puppet masterless provisioner optionally cleans staging directory

When puppet is executed in masterless mode it didn't remove
staging directory, this can be a problem because it leaves all
the modules and manifests in the built image.
This is specially problematic when building docker images as they
can be left in the layers unless an specific cleanup is done after
running puppet.

This change adds a flag `clean_staging_directory` to puppet
masterless provisioner so it takes care of this cleanup.
This commit is contained in:
Jaime Soriano Pastor 2015-07-17 22:24:23 +02:00
parent ac75d5329e
commit be8c9dddf2
1 changed files with 25 additions and 0 deletions

View File

@ -45,6 +45,9 @@ type Config struct {
// permissions in this directory. // permissions in this directory.
StagingDir string `mapstructure:"staging_directory"` StagingDir string `mapstructure:"staging_directory"`
// If true, staging directory is removed after executing puppet.
CleanStagingDir bool `mapstructure:"clean_staging_directory"`
// The directory from which the command will be executed. // The directory from which the command will be executed.
// Packer requires the directory to exist when running puppet. // Packer requires the directory to exist when running puppet.
WorkingDir string `mapstructure:"working_directory"` WorkingDir string `mapstructure:"working_directory"`
@ -237,6 +240,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus) return fmt.Errorf("Puppet exited with a non-zero exit status: %d", cmd.ExitStatus)
} }
if p.config.CleanStagingDir {
if err := p.removeDir(ui, comm, p.config.StagingDir); err != nil {
return fmt.Errorf("Error removing staging directory: %s", err)
}
}
return nil return nil
} }
@ -325,6 +334,22 @@ func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir stri
return nil return nil
} }
func (p *Provisioner) removeDir(ui packer.Ui, comm packer.Communicator, dir string) error {
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("rm -fr '%s'", dir),
}
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
}
if cmd.ExitStatus != 0 {
return fmt.Errorf("Non-zero exit status.")
}
return nil
}
func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, dst string, src string) error { func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, dst string, src string) error {
if err := p.createDir(ui, comm, dst); err != nil { if err := p.createDir(ui, comm, dst); err != nil {
return err return err