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-06-11 16:52:32 -04:00
|
|
|
"io/ioutil"
|
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"
|
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
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-05-24 01:38:40 -04:00
|
|
|
)
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
type Config struct {
|
2013-08-09 17:21:31 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2013-09-20 13:21:59 -04:00
|
|
|
// If true, the script contains binary and line endings will not be
|
|
|
|
// converted from Windows to Unix-style.
|
|
|
|
Binary bool
|
|
|
|
|
2013-06-11 16:42:15 -04:00
|
|
|
// An inline script to execute. Multiple strings are all executed
|
|
|
|
// in the context of a single shell.
|
|
|
|
Inline []string
|
|
|
|
|
2013-07-07 20:52:20 -04:00
|
|
|
// The shebang value used when running inline scripts.
|
|
|
|
InlineShebang string `mapstructure:"inline_shebang"`
|
|
|
|
|
2013-05-27 18:06:34 -04:00
|
|
|
// The local path of the shell script to upload and execute.
|
2013-06-27 13:56:46 -04:00
|
|
|
Script string
|
2013-05-27 18:06:34 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
// An array of multiple scripts to run.
|
|
|
|
Scripts []string
|
|
|
|
|
2013-06-27 08:39:16 -04:00
|
|
|
// An array of environment variables that will be injected before
|
|
|
|
// your command(s) are executed.
|
|
|
|
Vars []string `mapstructure:"environment_vars"`
|
|
|
|
|
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-05-27 18:06:34 -04:00
|
|
|
// The remote path where the local shell script will be uploaded to.
|
|
|
|
// This should be set to a writable file that is in a pre-existing directory.
|
2016-04-26 19:04:29 -04:00
|
|
|
// This defaults to remote_folder/remote_file
|
2013-06-07 02:14:31 -04:00
|
|
|
RemotePath string `mapstructure:"remote_path"`
|
|
|
|
|
|
|
|
// The command used to execute the script. The '{{ .Path }}' variable
|
2013-06-27 08:39:16 -04:00
|
|
|
// should be used to specify where the script goes, {{ .Vars }}
|
|
|
|
// can be used to inject the environment_vars into the environment.
|
2013-06-07 02:14:31 -04:00
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2013-07-14 21:07:49 -04:00
|
|
|
|
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
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
startRetryTimeout time.Duration
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
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 {
|
2013-06-27 08:39:16 -04:00
|
|
|
Vars string
|
2013-06-07 02:14:31 -04:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
2013-07-03 20:07:25 -04:00
|
|
|
p.config.ExecuteCommand = "chmod +x {{.Path}}; {{.Vars}} {{.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'
|
2014-10-14 17:20:36 -04:00
|
|
|
for idx, 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))
|
2014-10-14 17:20:36 -04:00
|
|
|
} else {
|
2015-06-13 14:11:20 -04:00
|
|
|
// Replace single quotes so they parse
|
|
|
|
vs[1] = strings.Replace(vs[1], "'", `'"'"'`, -1)
|
|
|
|
|
2014-10-14 17:20:36 -04:00
|
|
|
// Single quote env var values
|
|
|
|
p.config.Vars[idx] = fmt.Sprintf("%s='%s'", vs[0], vs[1])
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
tf, err := ioutil.TempFile("", "packer-shell")
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2013-07-14 21:07:49 -04:00
|
|
|
// Build our variables up by adding in the build name and builder type
|
|
|
|
envVars := make([]string, len(p.config.Vars)+2)
|
2014-10-28 11:23:18 -04:00
|
|
|
envVars[0] = fmt.Sprintf("PACKER_BUILD_NAME='%s'", p.config.PackerBuildName)
|
|
|
|
envVars[1] = fmt.Sprintf("PACKER_BUILDER_TYPE='%s'", p.config.PackerBuilderType)
|
2013-07-14 21:07:49 -04:00
|
|
|
copy(envVars[2:], p.config.Vars)
|
|
|
|
|
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-27 08:39:16 -04:00
|
|
|
// Flatten the environment variables
|
2013-07-14 21:07:49 -04:00
|
|
|
flattendVars := strings.Join(envVars, " ")
|
2013-06-27 08:39:16 -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{
|
2013-08-08 19:36:48 -04:00
|
|
|
Vars: flattendVars,
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:03:35 -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)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-07-23 23:44:32 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
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.
|
|
|
|
err = p.retryable(func() error {
|
|
|
|
cmd = &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("rm -f %s", p.config.RemotePath),
|
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error removing temporary script at %s: %s",
|
|
|
|
p.config.RemotePath, err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-21 22:53:21 -04:00
|
|
|
}
|
2015-11-15 22:37:09 -05:00
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
2015-06-21 22:53:21 -04:00
|
|
|
return fmt.Errorf(
|
2015-11-15 22:37:09 -05:00
|
|
|
"Error removing temporary script at %s!",
|
|
|
|
p.config.RemotePath)
|
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
|
|
|
|
|
|
|
return nil
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
2013-08-20 02:02:06 -04:00
|
|
|
|
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)
|
|
|
|
log.Printf(err.Error())
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|