2018-02-28 14:53:53 -05:00
|
|
|
package shell_local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2018-02-28 14:53:53 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-10-18 16:10:04 -04:00
|
|
|
"runtime"
|
2018-02-28 14:53:53 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2018-07-17 11:49:36 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2018-02-28 14:53:53 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2018-02-28 14:53:53 -05:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
2019-12-12 13:59:44 -05:00
|
|
|
func Run(ctx context.Context, ui packer.Ui, config *Config, generatedData map[string]interface{}) (bool, error) {
|
|
|
|
if generatedData != nil {
|
|
|
|
config.generatedData = generatedData
|
|
|
|
} else {
|
|
|
|
// No fear; probably just in the post-processor, not provisioner.
|
|
|
|
// Make sure it's not a nil map so we can assign to it later.
|
|
|
|
config.generatedData = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
config.ctx.Data = generatedData
|
2018-10-19 01:52:20 -04:00
|
|
|
// Check if shell-local can even execute against this runtime OS
|
|
|
|
if len(config.OnlyOn) > 0 {
|
2018-10-19 14:48:30 -04:00
|
|
|
runCommand := false
|
2018-10-19 01:52:20 -04:00
|
|
|
for _, os := range config.OnlyOn {
|
|
|
|
if os == runtime.GOOS {
|
|
|
|
runCommand = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 14:48:30 -04:00
|
|
|
if !runCommand {
|
|
|
|
ui.Say(fmt.Sprintf("Skipping shell-local due to runtime OS"))
|
|
|
|
log.Printf("[INFO] (shell-local): skipping shell-local due to missing runtime OS")
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-10-19 01:52:20 -04:00
|
|
|
}
|
|
|
|
|
2018-02-28 14:53:53 -05:00
|
|
|
scripts := make([]string, len(config.Scripts))
|
2018-05-21 18:19:27 -04:00
|
|
|
if len(config.Scripts) > 0 {
|
|
|
|
copy(scripts, config.Scripts)
|
|
|
|
} else if config.Inline != nil {
|
|
|
|
// If we have an inline script, then turn that into a temporary
|
|
|
|
// shell script and use that.
|
2018-02-28 15:17:40 -05:00
|
|
|
tempScriptFileName, err := createInlineScriptFile(config)
|
2018-02-28 14:53:53 -05:00
|
|
|
if err != nil {
|
2018-02-28 15:17:40 -05:00
|
|
|
return false, err
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|
2018-04-04 14:07:10 -04:00
|
|
|
|
|
|
|
// figure out what extension the file should have, and rename it.
|
|
|
|
if config.TempfileExtension != "" {
|
|
|
|
os.Rename(tempScriptFileName, fmt.Sprintf("%s.%s", tempScriptFileName, config.TempfileExtension))
|
2018-05-21 17:56:44 -04:00
|
|
|
tempScriptFileName = fmt.Sprintf("%s.%s", tempScriptFileName, config.TempfileExtension)
|
2018-04-04 14:07:10 -04:00
|
|
|
}
|
2019-04-04 16:22:07 -04:00
|
|
|
|
|
|
|
scripts = append(scripts, tempScriptFileName)
|
|
|
|
|
2018-05-21 17:56:44 -04:00
|
|
|
defer os.Remove(tempScriptFileName)
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create environment variables to set before executing the command
|
2018-03-12 14:25:39 -04:00
|
|
|
flattenedEnvVars, err := createFlattenedEnvVars(config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-02-28 14:53:53 -05:00
|
|
|
|
|
|
|
for _, script := range scripts {
|
|
|
|
interpolatedCmds, err := createInterpolatedCommands(config, script, flattenedEnvVars)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-10-19 01:52:20 -04:00
|
|
|
ui.Say(fmt.Sprintf("Running local shell script: %s", script))
|
2018-02-28 14:53:53 -05:00
|
|
|
|
2018-10-19 01:52:20 -04:00
|
|
|
comm := &Communicator{
|
|
|
|
ExecuteCommand: interpolatedCmds,
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:52:20 -04:00
|
|
|
// The remoteCmd generated here isn't actually run, but it allows us to
|
|
|
|
// use the same interafce for the shell-local communicator as we use for
|
|
|
|
// the other communicators; ultimately, this command is just used for
|
|
|
|
// buffers and for reading the final exit status.
|
|
|
|
flattenedCmd := strings.Join(interpolatedCmds, " ")
|
|
|
|
cmd := &packer.RemoteCmd{Command: flattenedCmd}
|
|
|
|
log.Printf("[INFO] (shell-local): starting local command: %s", flattenedCmd)
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2018-10-19 01:52:20 -04:00
|
|
|
return false, fmt.Errorf(
|
|
|
|
"Error executing script: %s\n\n"+
|
|
|
|
"Please see output above for more information.",
|
|
|
|
script)
|
|
|
|
}
|
2019-11-21 19:43:13 -05:00
|
|
|
|
|
|
|
if err := config.ValidExitCode(cmd.ExitStatus()); err != nil {
|
|
|
|
return false, err
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:17:40 -05:00
|
|
|
func createInlineScriptFile(config *Config) (string, error) {
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-shell")
|
2018-02-28 15:17:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
2018-04-04 14:07:10 -04:00
|
|
|
defer tf.Close()
|
2018-02-28 15:17:40 -05:00
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
2018-04-04 14:07:10 -04:00
|
|
|
if config.InlineShebang != "" {
|
|
|
|
shebang := fmt.Sprintf("#!%s\n", config.InlineShebang)
|
2018-05-21 14:25:51 -04:00
|
|
|
log.Printf("[INFO] (shell-local): Prepending inline script with %s", shebang)
|
2018-04-04 14:07:10 -04:00
|
|
|
writer.WriteString(shebang)
|
|
|
|
}
|
2018-05-08 13:35:09 -04:00
|
|
|
|
2018-02-28 15:17:40 -05:00
|
|
|
for _, command := range config.Inline {
|
2018-05-08 13:35:09 -04:00
|
|
|
// interpolate command to check for template variables.
|
2019-10-14 10:15:47 -04:00
|
|
|
command, err := interpolate.Render(command, &config.ctx)
|
2018-05-08 13:35:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:17:40 -05:00
|
|
|
if _, err := writer.WriteString(command + "\n"); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-21 17:56:44 -04:00
|
|
|
err = os.Chmod(tf.Name(), 0700)
|
2018-03-02 15:32:34 -05:00
|
|
|
if err != nil {
|
2018-05-21 14:25:51 -04:00
|
|
|
log.Printf("[ERROR] (shell-local): error modifying permissions of temp script file: %s", err.Error())
|
2018-03-02 15:32:34 -05:00
|
|
|
}
|
2018-02-28 15:17:40 -05:00
|
|
|
return tf.Name(), nil
|
|
|
|
}
|
|
|
|
|
2018-02-28 14:53:53 -05:00
|
|
|
// Generates the final command to send to the communicator, using either the
|
|
|
|
// user-provided ExecuteCommand or defaulting to something that makes sense for
|
|
|
|
// the host OS
|
|
|
|
func createInterpolatedCommands(config *Config, script string, flattenedEnvVars string) ([]string, error) {
|
2019-12-12 13:59:44 -05:00
|
|
|
config.generatedData["Vars"] = flattenedEnvVars
|
|
|
|
config.generatedData["Script"] = script
|
|
|
|
config.generatedData["Command"] = script
|
|
|
|
|
|
|
|
config.ctx.Data = config.generatedData
|
2018-02-28 14:53:53 -05:00
|
|
|
|
|
|
|
interpolatedCmds := make([]string, len(config.ExecuteCommand))
|
|
|
|
for i, cmd := range config.ExecuteCommand {
|
2019-10-14 10:15:47 -04:00
|
|
|
interpolatedCmd, err := interpolate.Render(cmd, &config.ctx)
|
2018-02-28 14:53:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error processing command: %s", err)
|
|
|
|
}
|
|
|
|
interpolatedCmds[i] = interpolatedCmd
|
|
|
|
}
|
|
|
|
return interpolatedCmds, nil
|
|
|
|
}
|
|
|
|
|
2018-03-12 14:25:39 -04:00
|
|
|
func createFlattenedEnvVars(config *Config) (string, error) {
|
|
|
|
flattened := ""
|
2018-02-28 14:53:53 -05:00
|
|
|
envVars := make(map[string]string)
|
|
|
|
|
|
|
|
// Always available Packer provided env vars
|
|
|
|
envVars["PACKER_BUILD_NAME"] = fmt.Sprintf("%s", config.PackerBuildName)
|
|
|
|
envVars["PACKER_BUILDER_TYPE"] = fmt.Sprintf("%s", config.PackerBuilderType)
|
|
|
|
|
2018-12-05 19:39:46 -05:00
|
|
|
// expose ip address variables
|
2018-07-17 11:49:36 -04:00
|
|
|
httpAddr := common.GetHTTPAddr()
|
|
|
|
if httpAddr != "" {
|
2018-12-05 19:39:46 -05:00
|
|
|
envVars["PACKER_HTTP_ADDR"] = httpAddr
|
|
|
|
}
|
|
|
|
httpIP := common.GetHTTPIP()
|
|
|
|
if httpIP != "" {
|
|
|
|
envVars["PACKER_HTTP_IP"] = httpIP
|
|
|
|
}
|
|
|
|
httpPort := common.GetHTTPPort()
|
|
|
|
if httpPort != "" {
|
|
|
|
envVars["PACKER_HTTP_PORT"] = httpPort
|
2018-07-17 11:49:36 -04:00
|
|
|
}
|
|
|
|
|
2018-02-28 14:53:53 -05:00
|
|
|
// Split vars into key/value components
|
|
|
|
for _, envVar := range config.Vars {
|
2019-10-14 10:15:47 -04:00
|
|
|
envVar, err := interpolate.Render(envVar, &config.ctx)
|
2018-05-08 13:35:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Split vars into key/value components
|
2018-02-28 14:53:53 -05:00
|
|
|
keyValue := strings.SplitN(envVar, "=", 2)
|
|
|
|
// Store pair, replacing any single quotes in value so they parse
|
|
|
|
// correctly with required environment variable format
|
|
|
|
envVars[keyValue[0]] = strings.Replace(keyValue[1], "'", `'"'"'`, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a list of env var keys in sorted order
|
|
|
|
var keys []string
|
|
|
|
for k := range envVars {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
2018-03-12 14:25:39 -04:00
|
|
|
flattened += fmt.Sprintf(config.EnvVarFormat, key, envVars[key])
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|
2018-03-12 14:25:39 -04:00
|
|
|
return flattened, nil
|
2018-02-28 14:53:53 -05:00
|
|
|
}
|