make varfile name unique and make sure to remove it from guest system if cleanup is true.

This commit is contained in:
Megan Marsh 2018-08-30 11:02:56 -07:00
parent 2c9a205f11
commit ab13c73277

View File

@ -259,7 +259,8 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
if !p.config.Binary {
r = &UnixReader{Reader: r}
}
remoteVFName := fmt.Sprintf("%s/%s", p.config.RemoteFolder, "varfile")
remoteVFName := fmt.Sprintf("%s/%s", p.config.RemoteFolder,
fmt.Sprintf("varfile_%d.sh", rand.Intn(9999)))
if err := comm.Upload(remoteVFName, r, nil); err != nil {
return fmt.Errorf("Error uploading envVarFile: %s", err)
}
@ -359,30 +360,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
// Delete the temporary file we created. We retry this a few times
// since if the above rebooted we have to wait until the reboot
// completes.
err = p.retryable(func() error {
cmd = &packer.RemoteCmd{
Command: fmt.Sprintf("rm -f %s", p.config.RemotePath),
}
if err := comm.Start(cmd); err != nil {
return fmt.Errorf(
"Error removing temporary script at %s: %s",
p.config.RemotePath, err)
}
cmd.Wait()
// treat disconnects as retryable by returning an error
if cmd.ExitStatus == packer.CmdDisconnect {
return fmt.Errorf("Disconnect while removing temporary script.")
}
return nil
})
err = p.cleanupRemoteFile(p.config.RemotePath, comm)
if err != nil {
return err
}
if cmd.ExitStatus != 0 {
return fmt.Errorf(
"Error removing temporary script at %s!",
p.config.RemotePath)
err = p.cleanupRemoteFile(p.config.envVarFile, comm)
if err != nil {
return err
}
}
}
@ -390,6 +374,36 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return nil
}
func (p *Provisioner) cleanupRemoteFile(path string, comm packer.Communicator) error {
err := p.retryable(func() error {
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf("rm -f %s", path),
}
if err := comm.Start(cmd); err != nil {
return fmt.Errorf(
"Error removing temporary script at %s: %s",
path, err)
}
cmd.Wait()
// treat disconnects as retryable by returning an error
if cmd.ExitStatus == packer.CmdDisconnect {
return fmt.Errorf("Disconnect while removing temporary script.")
}
if cmd.ExitStatus != 0 {
return fmt.Errorf(
"Error removing temporary script at %s!",
path)
}
return nil
})
if err != nil {
return err
}
return nil
}
func (p *Provisioner) Cancel() {
// Just hard quit. It isn't a big deal if what we're doing keeps
// running on the other side.