From 23ad5442ece00e904d358a17181c8d4f526b7d4f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 9 Nov 2013 10:15:25 -0800 Subject: [PATCH] builder/docker: perform cleanup in run method, not prematurely --- builder/docker/communicator.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index d757e89cb..118fc0956 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -35,15 +35,17 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error { return err } outputFile.Close() - defer os.Remove(outputFile.Name()) // This file will store the exit code of the command once it is complete. exitCodePath := outputFile.Name() + "-exit" - defer os.Remove(exitCodePath) cmd := exec.Command("docker", "attach", c.ContainerId) stdin_w, err := cmd.StdinPipe() if err != nil { + // We have to do some cleanup since run was never called + os.Remove(outputFile.Name()) + os.Remove(exitCodePath) + return err } @@ -103,6 +105,10 @@ func (c *Communicator) run(cmd *exec.Cmd, remote *packer.RemoteCmd, stdin_w io.W c.lock.Lock() defer c.lock.Unlock() + // Clean up after ourselves by removing our temporary files + defer os.Remove(outputFile.Name()) + defer os.Remove(exitCodePath) + // Modify the remote command so that all the output of the commands // go to a single file and so that the exit code is redirected to // a single file. This lets us determine both when the command