builder/docker: perform cleanup in run method, not prematurely

This commit is contained in:
Mitchell Hashimoto 2013-11-09 10:15:25 -08:00
parent 44a41451f0
commit 23ad5442ec
1 changed files with 8 additions and 2 deletions

View File

@ -35,15 +35,17 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error {
return err return err
} }
outputFile.Close() outputFile.Close()
defer os.Remove(outputFile.Name())
// This file will store the exit code of the command once it is complete. // This file will store the exit code of the command once it is complete.
exitCodePath := outputFile.Name() + "-exit" exitCodePath := outputFile.Name() + "-exit"
defer os.Remove(exitCodePath)
cmd := exec.Command("docker", "attach", c.ContainerId) cmd := exec.Command("docker", "attach", c.ContainerId)
stdin_w, err := cmd.StdinPipe() stdin_w, err := cmd.StdinPipe()
if err != nil { if err != nil {
// We have to do some cleanup since run was never called
os.Remove(outputFile.Name())
os.Remove(exitCodePath)
return err return err
} }
@ -103,6 +105,10 @@ func (c *Communicator) run(cmd *exec.Cmd, remote *packer.RemoteCmd, stdin_w io.W
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() 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 // 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 // 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 // a single file. This lets us determine both when the command