From 3d23655f2f42523e4f9d278e8603517751fe3800 Mon Sep 17 00:00:00 2001 From: David Zanetti Date: Mon, 16 Nov 2015 16:37:09 +1300 Subject: [PATCH 1/3] Add "noclean" boolean to shell provisioner. This stops the provisioner from attempting to remove helper scripts it creates. As noted on #2803 this can be useful when deleting the build user from an AMI or other template. --- provisioner/shell/provisioner.go | 46 +++++++++++-------- .../docs/provisioners/shell.html.markdown | 4 ++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 3c32b3fc2..82d084d25 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -58,6 +58,9 @@ type Config struct { // This can be set high to allow for reboots. RawStartRetryTimeout string `mapstructure:"start_retry_timeout"` + // Whether to clean scripts up + NoClean bool + startRetryTimeout time.Duration ctx interpolate.Context } @@ -271,29 +274,32 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { return fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus) } - // 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 !p.config.NoClean { + + // 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() + return nil + }) + if err != nil { + return err } - if err := comm.Start(cmd); err != nil { + + if cmd.ExitStatus != 0 { return fmt.Errorf( - "Error removing temporary script at %s: %s", - p.config.RemotePath, err) + "Error removing temporary script at %s!", + p.config.RemotePath) } - cmd.Wait() - return nil - }) - if err != nil { - return err - } - - if cmd.ExitStatus != 0 { - return fmt.Errorf( - "Error removing temporary script at %s!", - p.config.RemotePath) } } diff --git a/website/source/docs/provisioners/shell.html.markdown b/website/source/docs/provisioners/shell.html.markdown index 9cd05ef12..c7ee7143a 100644 --- a/website/source/docs/provisioners/shell.html.markdown +++ b/website/source/docs/provisioners/shell.html.markdown @@ -88,6 +88,10 @@ Optional parameters: system reboot. Set this to a higher value if reboots take a longer amount of time. +- `noclean` (boolean) - If true, specifies that the helper scripts uploaded + to the system will not be removed by Packer. This defaults to false + (clean scripts from the system). + ## Execute Command Example To many new users, the `execute_command` is puzzling. However, it provides an From 2668747c71a441f029c17388d3a5ce7b6a8b614f Mon Sep 17 00:00:00 2001 From: David Zanetti Date: Thu, 19 Nov 2015 16:02:45 +1300 Subject: [PATCH 2/3] Rename noclean shell option to skip_clean, per pull feedback --- provisioner/shell/provisioner.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 82d084d25..78826b638 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -59,7 +59,7 @@ type Config struct { RawStartRetryTimeout string `mapstructure:"start_retry_timeout"` // Whether to clean scripts up - NoClean bool + SkipClean bool `mapstructure:"skip_clean"` startRetryTimeout time.Duration ctx interpolate.Context @@ -274,7 +274,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { return fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus) } - if !p.config.NoClean { + if !p.config.SkipClean { // Delete the temporary file we created. We retry this a few times // since if the above rebooted we have to wait until the reboot From 6c121fa35bccca7ac75eb3bd37e967d2cc52d048 Mon Sep 17 00:00:00 2001 From: David Zanetti Date: Thu, 19 Nov 2015 16:06:59 +1300 Subject: [PATCH 3/3] Change shell doc from 'noclean' to 'skip_clean' --- website/source/docs/provisioners/shell.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/source/docs/provisioners/shell.html.markdown b/website/source/docs/provisioners/shell.html.markdown index c7ee7143a..f01a7eb9b 100644 --- a/website/source/docs/provisioners/shell.html.markdown +++ b/website/source/docs/provisioners/shell.html.markdown @@ -88,9 +88,9 @@ Optional parameters: system reboot. Set this to a higher value if reboots take a longer amount of time. -- `noclean` (boolean) - If true, specifies that the helper scripts uploaded - to the system will not be removed by Packer. This defaults to false - (clean scripts from the system). +- `skip_clean` (boolean) - If true, specifies that the helper scripts + uploaded to the system will not be removed by Packer. This defaults to + false (clean scripts from the system). ## Execute Command Example