From 969201a2d4e60e3d26e682355417c12e0484aa9f Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 May 2018 14:56:44 -0700 Subject: [PATCH] handle minor shell-local PR suggestions and corrections --- common/shell-local/config.go | 12 ++++++------ common/shell-local/run.go | 8 ++++---- .../source/docs/post-processors/shell-local.html.md | 2 +- website/source/docs/provisioners/shell-local.html.md | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/shell-local/config.go b/common/shell-local/config.go index 846e4b4a4..9eb657ff9 100644 --- a/common/shell-local/config.go +++ b/common/shell-local/config.go @@ -153,7 +153,11 @@ func Validate(config *Config) error { } if config.UseLinuxPathing { for index, script := range config.Scripts { - converted, err := ConvertToLinuxPath(script) + scriptAbsPath, err := filepath.Abs(script) + if err != nil { + return fmt.Errorf("Error converting %s to absolute path: %s", script, err.Error()) + } + converted, err := ConvertToLinuxPath(scriptAbsPath) if err != nil { return err } @@ -202,12 +206,8 @@ func Validate(config *Config) error { } // C:/path/to/your/file becomes /mnt/c/path/to/your/file -func ConvertToLinuxPath(winPath string) (string, error) { +func ConvertToLinuxPath(winAbsPath string) (string, error) { // get absolute path of script, and morph it into the bash path - winAbsPath, err := filepath.Abs(winPath) - if err != nil { - return "", fmt.Errorf("Error converting %s to absolute path: %s", winPath, err.Error()) - } winAbsPath = strings.Replace(winAbsPath, "\\", "/", -1) splitPath := strings.SplitN(winAbsPath, ":/", 2) winBashPath := fmt.Sprintf("/mnt/%s/%s", strings.ToLower(splitPath[0]), splitPath[1]) diff --git a/common/shell-local/run.go b/common/shell-local/run.go index 0457536fa..7ab93e346 100644 --- a/common/shell-local/run.go +++ b/common/shell-local/run.go @@ -32,11 +32,12 @@ func Run(ui packer.Ui, config *Config) (bool, error) { } scripts = append(scripts, tempScriptFileName) - defer os.Remove(tempScriptFileName) // figure out what extension the file should have, and rename it. if config.TempfileExtension != "" { os.Rename(tempScriptFileName, fmt.Sprintf("%s.%s", tempScriptFileName, config.TempfileExtension)) + tempScriptFileName = fmt.Sprintf("%s.%s", tempScriptFileName, config.TempfileExtension) } + defer os.Remove(tempScriptFileName) } // Create environment variables to set before executing the command @@ -83,7 +84,7 @@ func Run(ui packer.Ui, config *Config) (bool, error) { } func createInlineScriptFile(config *Config) (string, error) { - tf, err := ioutil.TempFile(os.TempDir(), "packer-shell") + tf, err := ioutil.TempFile("", "packer-shell") if err != nil { return "", fmt.Errorf("Error preparing shell script: %s", err) } @@ -105,8 +106,7 @@ func createInlineScriptFile(config *Config) (string, error) { return "", fmt.Errorf("Error preparing shell script: %s", err) } - tf.Close() - err = os.Chmod(tf.Name(), 0555) + err = os.Chmod(tf.Name(), 0700) if err != nil { log.Printf("[ERROR] (shell-local): error modifying permissions of temp script file: %s", err.Error()) } diff --git a/website/source/docs/post-processors/shell-local.html.md b/website/source/docs/post-processors/shell-local.html.md index 6812fac2b..ac8056407 100644 --- a/website/source/docs/post-processors/shell-local.html.md +++ b/website/source/docs/post-processors/shell-local.html.md @@ -73,7 +73,7 @@ Optional parameters: choose to try to use shell-local for Powershell or other Windows commands, the environment variables will not be set properly for your environment. - For backwards compatibility, `execute_command` will accept a string insetad + For backwards compatibility, `execute_command` will accept a string instead of an array of strings. If a single string or an array of strings with only one element is provided, Packer will replicate past behavior by appending your `execute_command` to the array of strings `["sh", "-c"]`. For example, diff --git a/website/source/docs/provisioners/shell-local.html.md b/website/source/docs/provisioners/shell-local.html.md index cadb1d6a1..a7400c589 100644 --- a/website/source/docs/provisioners/shell-local.html.md +++ b/website/source/docs/provisioners/shell-local.html.md @@ -89,7 +89,7 @@ Optional parameters: these commands are not officially supported and things like environment variables may not work if you use a different shell than the default. - For backwards compatability, you may also use {{.Command}}, but it is + For backwards compatibility, you may also use {{.Command}}, but it is decoded the same way as {{.Script}}. We recommend using {{.Script}} for the sake of clarity, as even when you set only a single `command` to run, Packer writes it to a temporary file and then runs it as a script.