ansible-local: Add ability to clean staging directory.

Signed-off-by: Krzysztof Wilczynski <kw@linux.com>
This commit is contained in:
Krzysztof Wilczynski 2017-11-21 21:09:01 +01:00
parent baacb7e173
commit 10370adbab
No known key found for this signature in database
GPG Key ID: 7C64768D3DE334E7
3 changed files with 57 additions and 2 deletions

View File

@ -48,6 +48,9 @@ type Config struct {
// permissions in this directory.
StagingDir string `mapstructure:"staging_directory"`
// If true, staging directory is removed after executing ansible.
CleanStagingDir bool `mapstructure:"clean_staging_directory"`
// The optional inventory file
InventoryFile string `mapstructure:"inventory_file"`
@ -260,6 +263,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
if err := p.executeAnsible(ui, comm); err != nil {
return fmt.Errorf("Error executing Ansible: %s", err)
}
if p.config.CleanStagingDir {
ui.Message("Removing staging directory...")
if err := p.removeDir(ui, comm, p.config.StagingDir); err != nil {
return fmt.Errorf("Error removing staging directory: %s", err)
}
}
return nil
}
@ -367,15 +377,33 @@ func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst, sr
}
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("mkdir -p '%s'", dir),
}
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
}
if cmd.ExitStatus != 0 {
return fmt.Errorf("Non-zero exit status.")
return fmt.Errorf("Non-zero exit status. See output above for more information.")
}
return nil
}
func (p *Provisioner) removeDir(ui packer.Ui, comm packer.Communicator, dir string) error {
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("rm -rf '%s'", dir),
}
ui.Message(fmt.Sprintf("Removing directory: %s", dir))
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
}
if cmd.ExitStatus != 0 {
return fmt.Errorf("Non-zero exit status. See output above for more information.")
}
return nil
}

View File

@ -188,3 +188,26 @@ func TestProvisionerPrepare_Dirs(t *testing.T) {
t.Fatalf("err: %s", err)
}
}
func TestProvisionerPrepare_CleanStagingDir(t *testing.T) {
var p Provisioner
config := testConfig()
playbook_file, err := ioutil.TempFile("", "playbook")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(playbook_file.Name())
config["playbook_file"] = playbook_file.Name()
config["clean_staging_directory"] = true
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
if !p.config.CleanStagingDir {
t.Fatalf("expected clean_staging_directory to be set")
}
}

View File

@ -140,6 +140,10 @@ chi-appservers
are not correct, use a shell provisioner prior to this to configure it
properly.
- `clean_staging_directory` (boolean) - If set to `true`, the content of
the `staging_directory` will be removed after executing ansible. By
default, this is set to `false`.
## Default Extra Variables
In addition to being able to specify extra arguments using the