provisioner/file: Clean up temporary files

This commit is contained in:
James Nugent 2016-07-08 19:34:37 +01:00
parent 800f7d2e06
commit 8beafe25ae
1 changed files with 14 additions and 8 deletions

View File

@ -27,10 +27,13 @@ func (p *ResourceProvisioner) Apply(
} }
// Get the source // Get the source
src, err := p.getSrc(c) src, deleteSource, err := p.getSrc(c)
if err != nil { if err != nil {
return err return err
} }
if deleteSource {
defer os.Remove(src)
}
// Get destination // Get destination
dRaw := c.Config["destination"] dRaw := c.Config["destination"]
@ -62,13 +65,13 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string
} }
// getSrc returns the file to use as source // getSrc returns the file to use as source
func (p *ResourceProvisioner) getSrc(c *terraform.ResourceConfig) (string, error) { func (p *ResourceProvisioner) getSrc(c *terraform.ResourceConfig) (string, bool, error) {
var src string var src string
sRaw, ok := c.Config["source"] sRaw, ok := c.Config["source"]
if ok { if ok {
if src, ok = sRaw.(string); !ok { if src, ok = sRaw.(string); !ok {
return "", fmt.Errorf("Unsupported 'source' type! Must be string.") return "", false, fmt.Errorf("Unsupported 'source' type! Must be string.")
} }
} }
@ -76,19 +79,22 @@ func (p *ResourceProvisioner) getSrc(c *terraform.ResourceConfig) (string, error
if ok { if ok {
file, err := ioutil.TempFile("", "tf-file-content") file, err := ioutil.TempFile("", "tf-file-content")
if err != nil { if err != nil {
return "", err return "", true, err
}
contentStr, ok := content.(string)
if !ok {
return "", fmt.Errorf("Unsupported 'content' type! Must be string.")
}
if _, err = file.WriteString(contentStr); err != nil {
return "", err
}
src = file.Name()
} }
return homedir.Expand(src) contentStr, ok := content.(string)
if !ok {
return "", true, fmt.Errorf("Unsupported 'content' type! Must be string.")
}
if _, err = file.WriteString(contentStr); err != nil {
return "", true, err
}
return file.Name(), true, nil
}
expansion, err := homedir.Expand(src)
return expansion, false, err
} }
// copyFiles is used to copy the files from a source to a destination // copyFiles is used to copy the files from a source to a destination