2013-05-24 01:38:40 -04:00
|
|
|
// This package implements a provisioner for Packer that executes
|
|
|
|
// shell scripts within the remote machine.
|
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2013-06-11 16:52:32 -04:00
|
|
|
"bufio"
|
2013-06-11 16:42:15 -04:00
|
|
|
"errors"
|
2013-06-11 17:09:31 -04:00
|
|
|
"fmt"
|
2013-09-20 13:21:59 -04:00
|
|
|
"io"
|
2013-05-28 00:54:19 -04:00
|
|
|
"log"
|
2015-06-18 04:38:23 -04:00
|
|
|
"math/rand"
|
2013-05-28 00:54:19 -04:00
|
|
|
"os"
|
2017-01-23 05:10:14 -05:00
|
|
|
"sort"
|
2013-06-03 02:39:04 -04:00
|
|
|
"strings"
|
2013-08-12 18:49:55 -04:00
|
|
|
"time"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2019-03-14 06:57:54 -04:00
|
|
|
"github.com/hashicorp/packer/common/shell"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-05-24 01:38:40 -04:00
|
|
|
)
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
type Config struct {
|
2019-03-14 06:57:54 -04:00
|
|
|
shell.Provisioner `mapstructure:",squash"`
|
2013-06-11 16:42:15 -04:00
|
|
|
|
2013-07-07 20:52:20 -04:00
|
|
|
// The shebang value used when running inline scripts.
|
|
|
|
InlineShebang string `mapstructure:"inline_shebang"`
|
|
|
|
|
2018-10-24 08:04:08 -04:00
|
|
|
// A duration of how long to pause after the provisioner
|
|
|
|
RawPauseAfter string `mapstructure:"pause_after"`
|
|
|
|
|
|
|
|
PauseAfter time.Duration
|
|
|
|
|
2018-08-29 13:44:21 -04:00
|
|
|
// Write the Vars to a file and source them from there rather than declaring
|
|
|
|
// inline
|
|
|
|
UseEnvVarFile bool `mapstructure:"use_env_var_file"`
|
|
|
|
|
2016-04-26 19:04:29 -04:00
|
|
|
// The remote folder where the local shell script will be uploaded to.
|
|
|
|
// This should be set to a pre-existing directory, it defaults to /tmp
|
|
|
|
RemoteFolder string `mapstructure:"remote_folder"`
|
|
|
|
|
|
|
|
// The remote file name of the local shell script.
|
|
|
|
// This defaults to script_nnn.sh
|
|
|
|
RemoteFile string `mapstructure:"remote_file"`
|
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
// The timeout for retrying to start the process. Until this timeout
|
|
|
|
// is reached, if the provisioner can't start a process, it retries.
|
|
|
|
// This can be set high to allow for reboots.
|
|
|
|
RawStartRetryTimeout string `mapstructure:"start_retry_timeout"`
|
|
|
|
|
2015-11-15 22:37:09 -05:00
|
|
|
// Whether to clean scripts up
|
2015-11-18 22:02:45 -05:00
|
|
|
SkipClean bool `mapstructure:"skip_clean"`
|
2015-11-15 22:37:09 -05:00
|
|
|
|
2017-10-12 02:35:24 -04:00
|
|
|
ExpectDisconnect bool `mapstructure:"expect_disconnect"`
|
2016-10-19 21:30:19 -04:00
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
startRetryTimeout time.Duration
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2018-08-29 13:44:21 -04:00
|
|
|
// name of the tmp environment variable file, if UseEnvVarFile is true
|
|
|
|
envVarFile string
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2015-05-27 17:50:20 -04:00
|
|
|
config Config
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
2013-06-07 02:14:31 -04:00
|
|
|
type ExecuteCommandTemplate struct {
|
2018-08-29 13:44:21 -04:00
|
|
|
Vars string
|
|
|
|
EnvVarFile string
|
|
|
|
Path string
|
2013-06-07 02:14:31 -04:00
|
|
|
}
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2015-05-27 17:50:20 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2016-02-01 16:28:49 -05:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
|
|
|
},
|
2015-05-27 17:50:20 -04:00
|
|
|
}, raws...)
|
2013-08-08 19:36:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
|
2013-06-07 02:14:31 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
2017-06-21 19:11:47 -04:00
|
|
|
p.config.ExecuteCommand = "chmod +x {{.Path}}; {{.Vars}} {{.Path}}"
|
2018-08-29 13:44:21 -04:00
|
|
|
if p.config.UseEnvVarFile == true {
|
|
|
|
p.config.ExecuteCommand = "chmod +x {{.Path}}; . {{.EnvVarFile}} && {{.Path}}"
|
|
|
|
}
|
2013-06-07 02:14:31 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 16:42:15 -04:00
|
|
|
if p.config.Inline != nil && len(p.config.Inline) == 0 {
|
|
|
|
p.config.Inline = nil
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:52:20 -04:00
|
|
|
if p.config.InlineShebang == "" {
|
2015-06-11 17:19:23 -04:00
|
|
|
p.config.InlineShebang = "/bin/sh -e"
|
2013-07-07 20:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
if p.config.RawStartRetryTimeout == "" {
|
|
|
|
p.config.RawStartRetryTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:04:29 -04:00
|
|
|
if p.config.RemoteFolder == "" {
|
|
|
|
p.config.RemoteFolder = "/tmp"
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RemoteFile == "" {
|
|
|
|
p.config.RemoteFile = fmt.Sprintf("script_%d.sh", rand.Intn(9999))
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:06:34 -04:00
|
|
|
if p.config.RemotePath == "" {
|
2015-06-18 04:38:23 -04:00
|
|
|
p.config.RemotePath = fmt.Sprintf(
|
2016-04-26 19:04:29 -04:00
|
|
|
"%s/%s", p.config.RemoteFolder, p.config.RemoteFile)
|
2013-05-27 18:06:34 -04:00
|
|
|
}
|
2013-06-06 20:07:42 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
if p.config.Scripts == nil {
|
|
|
|
p.config.Scripts = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
2013-06-27 08:39:16 -04:00
|
|
|
if p.config.Vars == nil {
|
|
|
|
p.config.Vars = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
var errs *packer.MultiError
|
2013-06-27 13:56:46 -04:00
|
|
|
if p.config.Script != "" && len(p.config.Scripts) > 0 {
|
2013-08-06 18:30:36 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Only one of script or scripts can be specified."))
|
2013-06-11 16:42:15 -04:00
|
|
|
}
|
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
if p.config.Script != "" {
|
|
|
|
p.config.Scripts = []string{p.config.Script}
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.Scripts) == 0 && p.config.Inline == nil {
|
2013-08-06 18:30:36 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Either a script file or inline script must be specified."))
|
2013-06-20 16:45:54 -04:00
|
|
|
} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {
|
2013-08-06 18:30:36 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
errors.New("Only a script file or an inline script can be specified, not both."))
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range p.config.Scripts {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
2013-08-06 18:30:36 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Bad script '%s': %s", path, err))
|
2013-06-11 16:42:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 08:39:16 -04:00
|
|
|
// Do a check for bad environment variables, such as '=foo', 'foobar'
|
2017-01-23 05:10:14 -05:00
|
|
|
for _, kv := range p.config.Vars {
|
2014-04-26 17:51:56 -04:00
|
|
|
vs := strings.SplitN(kv, "=", 2)
|
2013-06-27 08:39:16 -04:00
|
|
|
if len(vs) != 2 || vs[0] == "" {
|
2013-08-06 18:30:36 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
2013-07-14 21:07:49 -04:00
|
|
|
fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
|
2013-06-27 08:39:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
if p.config.RawStartRetryTimeout != "" {
|
|
|
|
p.config.startRetryTimeout, err = time.ParseDuration(p.config.RawStartRetryTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing start_retry_timeout: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:04:08 -04:00
|
|
|
if p.config.RawPauseAfter != "" {
|
|
|
|
p.config.PauseAfter, err = time.ParseDuration(p.config.RawPauseAfter)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing pause_after: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:30:36 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-06-11 16:42:15 -04:00
|
|
|
}
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
return nil
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
2013-06-26 20:52:49 -04:00
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-06-20 16:45:54 -04:00
|
|
|
scripts := make([]string, len(p.config.Scripts))
|
2013-06-23 14:56:27 -04:00
|
|
|
copy(scripts, p.config.Scripts)
|
2013-06-11 16:52:32 -04:00
|
|
|
|
|
|
|
// If we have an inline script, then turn that into a temporary
|
|
|
|
// shell script and use that.
|
|
|
|
if p.config.Inline != nil {
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-shell")
|
2013-06-11 16:52:32 -04:00
|
|
|
if err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Error preparing shell script: %s", err)
|
2013-06-11 16:52:32 -04:00
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Set the path to the temporary file
|
2013-06-20 16:45:54 -04:00
|
|
|
scripts = append(scripts, tf.Name())
|
2013-06-11 16:52:32 -04:00
|
|
|
|
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
2013-07-07 20:52:20 -04:00
|
|
|
writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
|
2013-06-11 16:52:32 -04:00
|
|
|
for _, command := range p.config.Inline {
|
2013-06-11 17:09:31 -04:00
|
|
|
if _, err := writer.WriteString(command + "\n"); err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Error preparing shell script: %s", err)
|
2013-06-11 16:52:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Error preparing shell script: %s", err)
|
2013-06-11 16:52:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tf.Close()
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:44:21 -04:00
|
|
|
if p.config.UseEnvVarFile == true {
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-shell-vars")
|
2018-08-29 13:44:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error preparing shell script: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
|
|
|
if _, err := writer.WriteString(p.createEnvVarFileContent()); 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.config.envVarFile = tf.Name()
|
|
|
|
defer os.Remove(p.config.envVarFile)
|
|
|
|
|
|
|
|
// upload the var file
|
|
|
|
var cmd *packer.RemoteCmd
|
|
|
|
err = p.retryable(func() error {
|
|
|
|
if _, err := tf.Seek(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var r io.Reader = tf
|
|
|
|
if !p.config.Binary {
|
|
|
|
r = &UnixReader{Reader: r}
|
|
|
|
}
|
2018-08-30 14:02:56 -04:00
|
|
|
remoteVFName := fmt.Sprintf("%s/%s", p.config.RemoteFolder,
|
|
|
|
fmt.Sprintf("varfile_%d.sh", rand.Intn(9999)))
|
2018-08-29 13:44:21 -04:00
|
|
|
if err := comm.Upload(remoteVFName, r, nil); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading envVarFile: %s", err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
|
|
|
|
cmd = &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("chmod 0600 %s", remoteVFName),
|
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf(
|
2018-08-30 17:36:14 -04:00
|
|
|
"Error chmodding script file to 0600 in remote "+
|
2018-08-29 13:44:21 -04:00
|
|
|
"machine: %s", err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
p.config.envVarFile = remoteVFName
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-23 05:10:14 -05:00
|
|
|
// Create environment variables to set before executing the command
|
|
|
|
flattenedEnvVars := p.createFlattenedEnvVars()
|
2013-07-14 21:07:49 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
for _, path := range scripts {
|
|
|
|
ui.Say(fmt.Sprintf("Provisioning with shell script: %s", path))
|
2013-06-02 00:59:33 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
log.Printf("Opening %s for reading", path)
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Error opening shell script: %s", err)
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
2013-07-07 23:50:53 -04:00
|
|
|
defer f.Close()
|
2013-05-28 00:54:19 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
// Compile the command
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = &ExecuteCommandTemplate{
|
2018-08-29 13:44:21 -04:00
|
|
|
Vars: flattenedEnvVars,
|
|
|
|
EnvVarFile: p.config.envVarFile,
|
|
|
|
Path: p.config.RemotePath,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
2013-08-08 20:04:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error processing command: %s", err)
|
|
|
|
}
|
2013-06-20 16:45:54 -04:00
|
|
|
|
2013-08-23 14:21:20 -04:00
|
|
|
// Upload the file and run the command. Do this in the context of
|
|
|
|
// a single retryable function so that we don't end up with
|
|
|
|
// the case that the upload succeeded, a restart is initiated,
|
|
|
|
// and then the command is executed but the file doesn't exist
|
|
|
|
// any longer.
|
|
|
|
var cmd *packer.RemoteCmd
|
2013-08-20 02:02:06 -04:00
|
|
|
err = p.retryable(func() error {
|
2013-08-23 14:21:20 -04:00
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:21:59 -04:00
|
|
|
var r io.Reader = f
|
|
|
|
if !p.config.Binary {
|
|
|
|
r = &UnixReader{Reader: r}
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:08:41 -04:00
|
|
|
if err := comm.Upload(p.config.RemotePath, r, nil); err != nil {
|
2013-08-23 14:21:20 -04:00
|
|
|
return fmt.Errorf("Error uploading script: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-04-26 23:20:47 -04:00
|
|
|
cmd = &packer.RemoteCmd{
|
2015-06-11 00:18:46 -04:00
|
|
|
Command: fmt.Sprintf("chmod 0755 %s", p.config.RemotePath),
|
2014-04-26 23:20:47 -04:00
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf(
|
2015-06-11 00:18:46 -04:00
|
|
|
"Error chmodding script file to 0755 in remote "+
|
2014-04-26 23:20:47 -04:00
|
|
|
"machine: %s", err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
|
2013-08-23 14:21:20 -04:00
|
|
|
cmd = &packer.RemoteCmd{Command: command}
|
2013-08-20 02:02:06 -04:00
|
|
|
return cmd.StartWithUi(comm, ui)
|
|
|
|
})
|
2016-10-19 21:30:19 -04:00
|
|
|
|
2013-08-20 02:02:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
|
2016-10-19 21:30:19 -04:00
|
|
|
// If the exit code indicates a remote disconnect, fail unless
|
|
|
|
// we were expecting it.
|
|
|
|
if cmd.ExitStatus == packer.CmdDisconnect {
|
2017-10-12 02:35:24 -04:00
|
|
|
if !p.config.ExpectDisconnect {
|
2017-10-26 12:52:34 -04:00
|
|
|
return fmt.Errorf("Script disconnected unexpectedly. " +
|
2017-10-26 13:41:49 -04:00
|
|
|
"If you expected your script to disconnect, i.e. from a " +
|
|
|
|
"restart, you can try adding `\"expect_disconnect\": true` " +
|
|
|
|
"to the shell provisioner parameters.")
|
2016-10-19 21:30:19 -04:00
|
|
|
}
|
|
|
|
} else if cmd.ExitStatus != 0 {
|
2013-07-23 23:44:32 -04:00
|
|
|
return fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus)
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
2015-06-15 18:02:59 -04:00
|
|
|
|
2015-11-18 22:02:45 -05:00
|
|
|
if !p.config.SkipClean {
|
2015-11-15 22:37:09 -05:00
|
|
|
|
|
|
|
// 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.
|
2018-08-30 14:02:56 -04:00
|
|
|
err = p.cleanupRemoteFile(p.config.RemotePath, comm)
|
2015-11-15 22:37:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-21 22:53:21 -04:00
|
|
|
}
|
2018-08-30 14:02:56 -04:00
|
|
|
err = p.cleanupRemoteFile(p.config.envVarFile, comm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-21 22:53:21 -04:00
|
|
|
}
|
2015-06-15 18:02:59 -04:00
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
2013-06-26 20:52:49 -04:00
|
|
|
|
2018-10-24 08:04:08 -04:00
|
|
|
if p.config.RawPauseAfter != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Pausing %s after this provisioner...", p.config.PauseAfter))
|
|
|
|
select {
|
|
|
|
case <-time.After(p.config.PauseAfter):
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 20:52:49 -04:00
|
|
|
return nil
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
2013-08-20 02:02:06 -04:00
|
|
|
|
2018-08-30 14:02:56 -04:00
|
|
|
func (p *Provisioner) cleanupRemoteFile(path string, comm packer.Communicator) error {
|
|
|
|
err := p.retryable(func() error {
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("rm -f %s", path),
|
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error removing temporary script at %s: %s",
|
|
|
|
path, err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
// treat disconnects as retryable by returning an error
|
|
|
|
if cmd.ExitStatus == packer.CmdDisconnect {
|
|
|
|
return fmt.Errorf("Disconnect while removing temporary script.")
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error removing temporary script at %s!",
|
|
|
|
path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:23:36 -04:00
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
// Just hard quit. It isn't a big deal if what we're doing keeps
|
|
|
|
// running on the other side.
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2013-08-20 02:02:06 -04:00
|
|
|
// retryable will retry the given function over and over until a
|
|
|
|
// non-error is returned.
|
|
|
|
func (p *Provisioner) retryable(f func() error) error {
|
|
|
|
startTimeout := time.After(p.config.startRetryTimeout)
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
if err = f(); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an error and log it
|
|
|
|
err = fmt.Errorf("Retryable error: %s", err)
|
2017-03-28 21:06:50 -04:00
|
|
|
log.Print(err.Error())
|
2013-08-20 02:02:06 -04:00
|
|
|
|
|
|
|
// Check if we timed out, otherwise we retry. It is safe to
|
|
|
|
// retry since the only error case above is if the command
|
|
|
|
// failed to START.
|
|
|
|
select {
|
|
|
|
case <-startTimeout:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 05:10:14 -05:00
|
|
|
|
2018-08-29 13:44:21 -04:00
|
|
|
func (p *Provisioner) escapeEnvVars() ([]string, map[string]string) {
|
2017-01-23 05:10:14 -05:00
|
|
|
envVars := make(map[string]string)
|
|
|
|
|
|
|
|
// Always available Packer provided env vars
|
|
|
|
envVars["PACKER_BUILD_NAME"] = fmt.Sprintf("%s", p.config.PackerBuildName)
|
|
|
|
envVars["PACKER_BUILDER_TYPE"] = fmt.Sprintf("%s", p.config.PackerBuilderType)
|
2018-12-05 19:39:46 -05:00
|
|
|
|
|
|
|
// expose ip address variables
|
2017-01-23 05:10:14 -05: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
|
2017-01-23 05:10:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split vars into key/value components
|
|
|
|
for _, envVar := range p.config.Vars {
|
|
|
|
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)
|
|
|
|
|
2018-08-29 13:44:21 -04:00
|
|
|
return keys, envVars
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) createEnvVarFileContent() string {
|
|
|
|
keys, envVars := p.escapeEnvVars()
|
|
|
|
|
|
|
|
flattened := ""
|
|
|
|
// Re-assemble vars surrounding value with single quotes and flatten
|
|
|
|
for _, key := range keys {
|
|
|
|
flattened += fmt.Sprintf("export %s='%s'\n", key, envVars[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
return flattened
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) createFlattenedEnvVars() (flattened string) {
|
|
|
|
keys, envVars := p.escapeEnvVars()
|
|
|
|
|
2017-01-23 05:10:14 -05:00
|
|
|
// Re-assemble vars surrounding value with single quotes and flatten
|
|
|
|
for _, key := range keys {
|
|
|
|
flattened += fmt.Sprintf("%s='%s' ", key, envVars[key])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|