handle minor shell-local PR suggestions and corrections

pull/5956/head
Megan Marsh 8 years ago
parent 1fdf763d0f
commit 969201a2d4

@ -153,7 +153,11 @@ func Validate(config *Config) error {
} }
if config.UseLinuxPathing { if config.UseLinuxPathing {
for index, script := range config.Scripts { 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 { if err != nil {
return err return err
} }
@ -202,12 +206,8 @@ func Validate(config *Config) error {
} }
// C:/path/to/your/file becomes /mnt/c/path/to/your/file // 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 // 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) winAbsPath = strings.Replace(winAbsPath, "\\", "/", -1)
splitPath := strings.SplitN(winAbsPath, ":/", 2) splitPath := strings.SplitN(winAbsPath, ":/", 2)
winBashPath := fmt.Sprintf("/mnt/%s/%s", strings.ToLower(splitPath[0]), splitPath[1]) winBashPath := fmt.Sprintf("/mnt/%s/%s", strings.ToLower(splitPath[0]), splitPath[1])

@ -32,11 +32,12 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
} }
scripts = append(scripts, tempScriptFileName) scripts = append(scripts, tempScriptFileName)
defer os.Remove(tempScriptFileName)
// figure out what extension the file should have, and rename it. // figure out what extension the file should have, and rename it.
if config.TempfileExtension != "" { if config.TempfileExtension != "" {
os.Rename(tempScriptFileName, fmt.Sprintf("%s.%s", tempScriptFileName, 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 // 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) { func createInlineScriptFile(config *Config) (string, error) {
tf, err := ioutil.TempFile(os.TempDir(), "packer-shell") tf, err := ioutil.TempFile("", "packer-shell")
if err != nil { if err != nil {
return "", fmt.Errorf("Error preparing shell script: %s", err) 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) return "", fmt.Errorf("Error preparing shell script: %s", err)
} }
tf.Close() err = os.Chmod(tf.Name(), 0700)
err = os.Chmod(tf.Name(), 0555)
if err != nil { if err != nil {
log.Printf("[ERROR] (shell-local): error modifying permissions of temp script file: %s", err.Error()) log.Printf("[ERROR] (shell-local): error modifying permissions of temp script file: %s", err.Error())
} }

@ -73,7 +73,7 @@ Optional parameters:
choose to try to use shell-local for Powershell or other Windows commands, choose to try to use shell-local for Powershell or other Windows commands,
the environment variables will not be set properly for your environment. 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 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 one element is provided, Packer will replicate past behavior by appending
your `execute_command` to the array of strings `["sh", "-c"]`. For example, your `execute_command` to the array of strings `["sh", "-c"]`. For example,

@ -89,7 +89,7 @@ Optional parameters:
these commands are not officially supported and things like environment these commands are not officially supported and things like environment
variables may not work if you use a different shell than the default. 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 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, 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. Packer writes it to a temporary file and then runs it as a script.

Loading…
Cancel
Save