2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
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"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
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
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
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
|
|
|
|
2019-11-21 19:43:13 -05:00
|
|
|
shell.ProvisionerRemoteSpecific `mapstructure:",squash"`
|
|
|
|
|
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
|
2019-10-31 10:49:34 -04:00
|
|
|
PauseAfter time.Duration `mapstructure:"pause_after"`
|
2018-10-24 08:04:08 -04:00
|
|
|
|
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.
|
2019-10-31 10:49:34 -04:00
|
|
|
StartRetryTimeout time.Duration `mapstructure:"start_retry_timeout"`
|
2013-08-12 18:49:55 -04:00
|
|
|
|
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
|
|
|
|
2018-08-29 13:44:21 -04:00
|
|
|
// name of the tmp environment variable file, if UseEnvVarFile is true
|
|
|
|
envVarFile string
|
2019-10-31 14:41:48 -04:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2020-05-19 05:49:48 -04:00
|
|
|
config Config
|
|
|
|
generatedData map[string]interface{}
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
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{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: "shell",
|
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...)
|
2019-10-31 14:41:48 -04:00
|
|
|
|
2013-08-08 19:36:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
if p.config.EnvVarFormat == "" {
|
|
|
|
p.config.EnvVarFormat = "%s='%s' "
|
|
|
|
|
|
|
|
if p.config.UseEnvVarFile == true {
|
|
|
|
p.config.EnvVarFormat = "export %s='%s'\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-31 10:49:34 -04:00
|
|
|
if p.config.StartRetryTimeout == 0 {
|
|
|
|
p.config.StartRetryTimeout = 5 * time.Minute
|
2013-08-12 18:49:55 -04:00
|
|
|
}
|
|
|
|
|
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 == "" {
|
2020-01-21 20:33:49 -05:00
|
|
|
p.config.RemotePath = fmt.Sprintf("%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-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
|
|
|
}
|
|
|
|
|
2020-03-12 12:54:31 -04:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error {
|
|
|
|
if generatedData == nil {
|
|
|
|
generatedData = make(map[string]interface{})
|
|
|
|
}
|
2020-05-19 05:49:48 -04:00
|
|
|
p.generatedData = generatedData
|
2020-03-12 12:54:31 -04:00
|
|
|
|
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 {
|
2020-03-12 12:54:31 -04:00
|
|
|
p.config.ctx.Data = generatedData
|
2020-03-12 12:59:34 -04:00
|
|
|
command, err := interpolate.Render(command, &p.config.ctx)
|
2020-03-12 12:54:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error interpolating Inline: %s", err)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
// upload the var file
|
|
|
|
var cmd *packer.RemoteCmd
|
2019-10-31 10:49:34 -04:00
|
|
|
err = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
|
2018-08-29 13:44:21 -04:00
|
|
|
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),
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := comm.Start(ctx, cmd); err != nil {
|
2020-01-21 20:33:49 -05:00
|
|
|
return fmt.Errorf("Error chmodding script file to 0600 in remote machine: %s", err)
|
2018-08-29 13:44:21 -04:00
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
p.config.envVarFile = remoteVFName
|
|
|
|
return nil
|
|
|
|
})
|
2019-10-15 03:45:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-29 13:44:21 -04:00
|
|
|
}
|
|
|
|
|
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
|
2020-03-12 12:54:31 -04:00
|
|
|
// These are extra variables that will be made available for interpolation.
|
|
|
|
generatedData["Vars"] = flattenedEnvVars
|
|
|
|
generatedData["EnvVarFile"] = p.config.envVarFile
|
|
|
|
generatedData["Path"] = p.config.RemotePath
|
|
|
|
p.config.ctx.Data = generatedData
|
|
|
|
|
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
|
2019-10-31 10:49:34 -04:00
|
|
|
err = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) 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
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := comm.Start(ctx, cmd); err != nil {
|
2014-04-26 23:20:47 -04:00
|
|
|
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}
|
2019-04-03 11:14:55 -04:00
|
|
|
return cmd.RunWithUi(ctx, comm, ui)
|
2013-08-20 02:02:06 -04:00
|
|
|
})
|
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.
|
2019-04-03 11:14:55 -04:00
|
|
|
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` " +
|
2019-03-14 07:47:22 -04:00
|
|
|
"or `\"valid_exit_codes\": [0, 2300218]` to the shell " +
|
|
|
|
"provisioner parameters.")
|
2016-10-19 21:30:19 -04:00
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
} else if err := p.config.ValidExitCode(cmd.ExitStatus()); err != nil {
|
2019-03-14 07:47:22 -04:00
|
|
|
return err
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
2015-06-15 18:02:59 -04:00
|
|
|
|
2020-01-21 20:33:49 -05:00
|
|
|
if p.config.SkipClean {
|
|
|
|
continue
|
|
|
|
}
|
2015-11-15 22:37:09 -05:00
|
|
|
|
2020-01-21 20:33:49 -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.
|
|
|
|
if err := p.cleanupRemoteFile(p.config.RemotePath, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.config.SkipClean {
|
|
|
|
if err := p.cleanupRemoteFile(p.config.envVarFile, comm); err != nil {
|
|
|
|
return err
|
2015-06-15 18:02:59 -04:00
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
2013-06-26 20:52:49 -04:00
|
|
|
|
2019-10-31 10:49:34 -04:00
|
|
|
if p.config.PauseAfter != 0 {
|
2018-10-24 08:04:08 -04:00
|
|
|
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 {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2019-10-31 10:49:34 -04:00
|
|
|
err := retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
|
2018-08-30 14:02:56 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("rm -f %s", path),
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := comm.Start(ctx, cmd); err != nil {
|
2018-08-30 14:02:56 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error removing temporary script at %s: %s",
|
|
|
|
path, err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
// treat disconnects as retryable by returning an error
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() == packer.CmdDisconnect {
|
2018-08-30 14:02:56 -04:00
|
|
|
return fmt.Errorf("Disconnect while removing temporary script.")
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2018-08-30 14:02:56 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error removing temporary script at %s!",
|
|
|
|
path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-04 15:31:30 -05:00
|
|
|
envVars["PACKER_BUILD_NAME"] = p.config.PackerBuildName
|
|
|
|
envVars["PACKER_BUILDER_TYPE"] = p.config.PackerBuilderType
|
2018-12-05 19:39:46 -05:00
|
|
|
|
|
|
|
// expose ip address variables
|
2020-05-19 05:49:48 -04:00
|
|
|
httpAddr := p.generatedData["PackerHTTPAddr"]
|
|
|
|
if httpAddr != nil && httpAddr != common.HttpAddrNotImplemented {
|
|
|
|
envVars["PACKER_HTTP_ADDR"] = httpAddr.(string)
|
2018-12-05 19:39:46 -05:00
|
|
|
}
|
2020-05-19 05:49:48 -04:00
|
|
|
httpIP := p.generatedData["PackerHTTPIP"]
|
|
|
|
if httpIP != nil && httpIP != common.HttpIPNotImplemented {
|
|
|
|
envVars["PACKER_HTTP_IP"] = httpIP.(string)
|
2018-12-05 19:39:46 -05:00
|
|
|
}
|
2020-05-19 05:49:48 -04:00
|
|
|
httpPort := p.generatedData["PackerHTTPPort"]
|
|
|
|
if httpPort != nil && httpPort != common.HttpPortNotImplemented {
|
|
|
|
envVars["PACKER_HTTP_PORT"] = httpPort.(string)
|
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()
|
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
var flattened string
|
2018-08-29 13:44:21 -04:00
|
|
|
for _, key := range keys {
|
2019-10-31 14:41:48 -04:00
|
|
|
flattened += fmt.Sprintf(p.config.EnvVarFormat, key, envVars[key])
|
2018-08-29 13:44:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return flattened
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
func (p *Provisioner) createFlattenedEnvVars() string {
|
2018-08-29 13:44:21 -04:00
|
|
|
keys, envVars := p.escapeEnvVars()
|
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
// Re-assemble vars into specified format and flatten
|
|
|
|
var flattened string
|
2017-01-23 05:10:14 -05:00
|
|
|
for _, key := range keys {
|
2019-10-31 14:41:48 -04:00
|
|
|
flattened += fmt.Sprintf(p.config.EnvVarFormat, key, envVars[key])
|
2017-01-23 05:10:14 -05:00
|
|
|
}
|
2019-10-31 14:41:48 -04:00
|
|
|
|
|
|
|
return flattened
|
2017-01-23 05:10:14 -05:00
|
|
|
}
|