From 8beafe25ae5d126cc355b9d17e030d1e38db7744 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Fri, 8 Jul 2016 19:34:37 +0100 Subject: [PATCH] provisioner/file: Clean up temporary files --- .../provisioners/file/resource_provisioner.go | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/builtin/provisioners/file/resource_provisioner.go b/builtin/provisioners/file/resource_provisioner.go index ac87d7583..2cd060b63 100644 --- a/builtin/provisioners/file/resource_provisioner.go +++ b/builtin/provisioners/file/resource_provisioner.go @@ -27,10 +27,13 @@ func (p *ResourceProvisioner) Apply( } // Get the source - src, err := p.getSrc(c) + src, deleteSource, err := p.getSrc(c) if err != nil { return err } + if deleteSource { + defer os.Remove(src) + } // Get 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 -func (p *ResourceProvisioner) getSrc(c *terraform.ResourceConfig) (string, error) { +func (p *ResourceProvisioner) getSrc(c *terraform.ResourceConfig) (string, bool, error) { var src string sRaw, ok := c.Config["source"] if 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 { file, err := ioutil.TempFile("", "tf-file-content") if err != nil { - return "", err + return "", true, err } + contentStr, ok := content.(string) if !ok { - return "", fmt.Errorf("Unsupported 'content' type! Must be string.") + return "", true, fmt.Errorf("Unsupported 'content' type! Must be string.") } if _, err = file.WriteString(contentStr); err != nil { - return "", err + return "", true, err } - src = file.Name() + + return file.Name(), true, nil } - return homedir.Expand(src) + expansion, err := homedir.Expand(src) + return expansion, false, err } // copyFiles is used to copy the files from a source to a destination