provisioner/shell: Fix envVarFile clean up issue

This change ensures the deletion of the external envVarFile (use_env_var_file = true) occurs after all script files have been executed and deleted.

Build results before change
```
> packer build build.pkr.hcl                                                                                                                                                             [~0]
docker: output will be in this color.

==> docker: Creating a temporary directory for sharing data...
==> docker: Pulling Docker image: ubuntu:bionic
    docker: bionic: Pulling from library/ubuntu
    docker: Digest: sha256:8d31dad0c58f552e890d68bbfb735588b6b820a46e459672d96e585871acc110
    docker: Status: Image is up to date for ubuntu:bionic
    docker: docker.io/library/ubuntu:bionic
==> docker: Starting docker container...
    docker: Run command: docker run -v /home/wilken/.packer.d/tmp476880774:/packer-files -d -i -t --entrypoint=/bin/sh -- ubuntu:bionic
    docker: Container ID: 812069b4d70746a6d4592a8f75c06867c6774b8b0bd81ade76eae7926a30f64b
==> docker: Using docker communicator to connect: 172.17.0.2
==> docker: Provisioning with shell script: sample.sh
    docker: Sample Script
    docker: wilken is NotForSale!
==> docker: Provisioning with shell script: foobar.sh
==> docker: /bin/sh: 1: .: Can't open /tmp/varfile_2555.sh
==> docker: Provisioning step had errors: Running the cleanup provisioner, if present...
==> docker: Killing the container: 812069b4d70746a6d4592a8f75c06867c6774b8b0bd81ade76eae7926a30f64b
Build 'docker' errored: Script exited with non-zero exit status: 2.Allowed exit codes are: [0]

==> Some builds didn't complete successfully and had errors:
--> docker: Script exited with non-zero exit status: 2.Allowed exit codes are: [0]

==> Builds finished but no artifacts were created.
```

Build results after change
```
> packer build build.pkr.hcl
docker: output will be in this color.

==> docker: Creating a temporary directory for sharing data...
==> docker: Pulling Docker image: ubuntu:bionic
    docker: bionic: Pulling from library/ubuntu
    docker: Digest: sha256:8d31dad0c58f552e890d68bbfb735588b6b820a46e459672d96e585871acc110
    docker: Status: Image is up to date for ubuntu:bionic
    docker: docker.io/library/ubuntu:bionic
==> docker: Starting docker container...
    docker: Run command: docker run -v /home/wilken/.packer.d/tmp819845000:/packer-files -d -i -t --entrypoint=/bin/sh -- ubuntu:bionic
    docker: Container ID: d8ed9100ff5017379bfc0d80703b2b2d10c4104941663aa5ddbbf18f6dcf74a5
==> docker: Using docker communicator to connect: 172.17.0.2
==> docker: Provisioning with shell script: sample.sh
    docker: Sample Script
    docker: wilken is NotForSale!
==> docker: Provisioning with shell script: foobar.sh
    docker: FooBar Script
    docker: wilken is NotForSale!
==> docker: Committing the container
    docker: Image ID: sha256:a412e43f134431f2049a7d06d5d691aad3717d91dd2ee5e2575e05b89384a4b3
==> docker: Killing the container: d8ed9100ff5017379bfc0d80703b2b2d10c4104941663aa5ddbbf18f6dcf74a5
Build 'docker' finished.

==> Builds finished. The artifacts of successful builds are:
--> docker: Imported Docker image: sha256:a412e43f134431f2049a7d06d5d691aad3717d91dd2ee5e2575e05b89384a4b3
```
This commit is contained in:
nywilken 2020-01-21 20:33:49 -05:00
parent b0b10f6939
commit b80882f3b8
1 changed files with 17 additions and 18 deletions

View File

@ -129,8 +129,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if p.config.RemotePath == "" {
p.config.RemotePath = fmt.Sprintf(
"%s/%s", p.config.RemoteFolder, p.config.RemoteFile)
p.config.RemotePath = fmt.Sprintf("%s/%s", p.config.RemoteFolder, p.config.RemoteFile)
}
if p.config.Scripts == nil {
@ -232,7 +231,6 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
}
p.config.envVarFile = tf.Name()
defer os.Remove(p.config.envVarFile)
// upload the var file
var cmd *packer.RemoteCmd
@ -256,9 +254,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
Command: fmt.Sprintf("chmod 0600 %s", remoteVFName),
}
if err := comm.Start(ctx, cmd); err != nil {
return fmt.Errorf(
"Error chmodding script file to 0600 in remote "+
"machine: %s", err)
return fmt.Errorf("Error chmodding script file to 0600 in remote machine: %s", err)
}
cmd.Wait()
p.config.envVarFile = remoteVFName
@ -345,19 +341,22 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
return err
}
if !p.config.SkipClean {
if p.config.SkipClean {
continue
}
// 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.cleanupRemoteFile(p.config.RemotePath, comm)
if err != nil {
return err
}
err = p.cleanupRemoteFile(p.config.envVarFile, comm)
if err != nil {
return err
}
// 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.
if err := p.cleanupRemoteFile(p.config.RemotePath, comm); err != nil {
return err
}
}
if !p.config.SkipClean {
if err := p.cleanupRemoteFile(p.config.envVarFile, comm); err != nil {
return err
}
}