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-08-06 18:30:36 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-05-24 01:38:40 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-11 16:52:32 -04:00
|
|
|
"io/ioutil"
|
2013-05-28 00:54:19 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2013-06-03 02:39:04 -04:00
|
|
|
"strings"
|
2013-08-12 18:49:55 -04:00
|
|
|
"time"
|
2013-05-24 01:38:40 -04:00
|
|
|
)
|
|
|
|
|
2013-05-27 18:06:34 -04:00
|
|
|
const DefaultRemotePath = "/tmp/script.sh"
|
|
|
|
|
2013-05-24 01:38:40 -04:00
|
|
|
type config struct {
|
2013-08-09 17:21:31 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
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"`
|
|
|
|
|
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.
|
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"`
|
|
|
|
|
|
|
|
startRetryTimeout time.Duration
|
2013-08-15 22:17:23 -04:00
|
|
|
tpl *packer.ConfigTemplate
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config config
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-08-06 18:30:36 -04:00
|
|
|
md, err := common.DecodeConfig(&p.config, raws...)
|
2013-07-13 20:28:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
2013-08-08 19:36:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-09 17:21:31 -04:00
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
2013-08-08 19:36:48 -04:00
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
// Accumulate any errors
|
2013-08-06 18:30:36 -04:00
|
|
|
errs := common.CheckUnusedConfig(md)
|
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 == "" {
|
|
|
|
p.config.InlineShebang = "/bin/sh"
|
|
|
|
}
|
|
|
|
|
2013-08-12 18:49:55 -04:00
|
|
|
if p.config.RawStartRetryTimeout == "" {
|
|
|
|
p.config.RawStartRetryTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:06:34 -04:00
|
|
|
if p.config.RemotePath == "" {
|
|
|
|
p.config.RemotePath = DefaultRemotePath
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-08 19:36:48 -04:00
|
|
|
templates := map[string]*string{
|
2013-08-12 18:49:55 -04:00
|
|
|
"inline_shebang": &p.config.InlineShebang,
|
|
|
|
"script": &p.config.Script,
|
|
|
|
"start_retry_timeout": &p.config.RawStartRetryTimeout,
|
|
|
|
"remote_path": &p.config.RemotePath,
|
2013-08-08 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = p.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceTemplates := map[string][]string{
|
|
|
|
"inline": p.config.Inline,
|
|
|
|
"scripts": p.config.Scripts,
|
|
|
|
"environment_vars": p.config.Vars,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, slice := range sliceTemplates {
|
|
|
|
for i, elem := range slice {
|
|
|
|
var err error
|
|
|
|
slice[i], err = p.config.tpl.Process(elem, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
for _, kv := range p.config.Vars {
|
|
|
|
vs := strings.Split(kv, "=")
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
envVars[0] = "PACKER_BUILD_NAME=" + p.config.PackerBuildName
|
|
|
|
envVars[1] = "PACKER_BUILDER_TYPE=" + p.config.PackerBuilderType
|
|
|
|
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-20 16:45:54 -04:00
|
|
|
log.Printf("Uploading %s => %s", path, p.config.RemotePath)
|
2013-08-20 02:02:06 -04:00
|
|
|
err = p.retryable(func() error {
|
|
|
|
return comm.Upload(p.config.RemotePath, f)
|
|
|
|
})
|
2013-06-20 16:45:54 -04:00
|
|
|
if err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Error uploading shell script: %s", err)
|
2013-06-20 16:45:54 -04:00
|
|
|
}
|
2013-05-28 00:54:19 -04:00
|
|
|
|
2013-07-07 23:50:53 -04:00
|
|
|
// Close the original file since we copied it
|
|
|
|
f.Close()
|
|
|
|
|
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
|
2013-08-08 20:04:46 -04:00
|
|
|
command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteCommandTemplate{
|
2013-08-08 19:36:48 -04:00
|
|
|
Vars: flattendVars,
|
|
|
|
Path: p.config.RemotePath,
|
|
|
|
})
|
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-08 19:36:48 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: command}
|
2013-06-20 16:45:54 -04:00
|
|
|
log.Printf("Executing command: %s", cmd.Command)
|
2013-08-20 02:02:06 -04:00
|
|
|
err = p.retryable(func() error {
|
|
|
|
return cmd.StartWithUi(comm, ui)
|
|
|
|
})
|
2013-08-12 18:49:55 -04:00
|
|
|
|
2013-08-20 02:02:06 -04:00
|
|
|
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
|
|
|
}
|
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
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|