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-07 02:14:31 -04:00
|
|
|
"bytes"
|
2013-06-11 16:42:15 -04:00
|
|
|
"errors"
|
2013-06-11 17:09:31 -04:00
|
|
|
"fmt"
|
2013-06-03 02:34:55 -04:00
|
|
|
"github.com/mitchellh/iochan"
|
2013-05-27 18:06:34 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-05-24 01:38:40 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-03 02:34:55 -04:00
|
|
|
"io"
|
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-06-07 02:14:31 -04:00
|
|
|
"text/template"
|
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-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-05-27 18:06:34 -04:00
|
|
|
// The local path of the shell script to upload and execute.
|
|
|
|
Path string
|
|
|
|
|
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-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-06-06 20:01:12 -04:00
|
|
|
for _, raw := range raws {
|
2013-06-06 20:07:42 -04:00
|
|
|
if err := mapstructure.Decode(raw, &p.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-06 20:01:12 -04:00
|
|
|
}
|
2013-05-27 18:06:34 -04:00
|
|
|
|
2013-06-07 02:14:31 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
2013-06-27 08:39:16 -04:00
|
|
|
// Don't leave an empty space if Vars isn't configured
|
|
|
|
if p.config.Vars == nil {
|
|
|
|
p.config.ExecuteCommand = "sh {{.Path}}"
|
|
|
|
} else {
|
|
|
|
p.config.ExecuteCommand = "{{.Vars}} sh {{.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-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-11 16:42:15 -04:00
|
|
|
errs := make([]error, 0)
|
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
if p.config.Path != "" && len(p.config.Scripts) > 0 {
|
|
|
|
errs = append(errs, errors.New("Only one of path or scripts can be specified."))
|
2013-06-11 16:42:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Path != "" {
|
2013-06-20 16:45:54 -04:00
|
|
|
p.config.Scripts = []string{p.config.Path}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.Scripts) == 0 && p.config.Inline == nil {
|
|
|
|
errs = append(errs, errors.New("Either a script file or inline script must be specified."))
|
|
|
|
} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {
|
|
|
|
errs = append(errs, errors.New("Only a script file or an inline script can be specified, not both."))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range p.config.Scripts {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
errs = append(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] == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 16:42:15 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
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-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-05-28 00:54:19 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
log.Printf("Uploading %s => %s", path, p.config.RemotePath)
|
|
|
|
err = comm.Upload(p.config.RemotePath, f)
|
|
|
|
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-06-27 08:39:16 -04:00
|
|
|
// Flatten the environment variables
|
|
|
|
flattendVars := strings.Join(p.config.Vars, " ")
|
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
// Compile the command
|
|
|
|
var command bytes.Buffer
|
|
|
|
t := template.Must(template.New("command").Parse(p.config.ExecuteCommand))
|
2013-06-27 08:39:16 -04:00
|
|
|
t.Execute(&command, &ExecuteCommandTemplate{flattendVars, p.config.RemotePath})
|
2013-06-20 16:45:54 -04:00
|
|
|
|
|
|
|
// Setup the remote command
|
|
|
|
stdout_r, stdout_w := io.Pipe()
|
|
|
|
stderr_r, stderr_w := io.Pipe()
|
|
|
|
|
|
|
|
var cmd packer.RemoteCmd
|
|
|
|
cmd.Command = command.String()
|
|
|
|
cmd.Stdout = stdout_w
|
|
|
|
cmd.Stderr = stderr_w
|
2013-05-28 00:54:19 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
log.Printf("Executing command: %s", cmd.Command)
|
|
|
|
err = comm.Start(&cmd)
|
|
|
|
if err != nil {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Failed executing command: %s", err)
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
exitChan := make(chan int, 1)
|
|
|
|
stdoutChan := iochan.DelimReader(stdout_r, '\n')
|
|
|
|
stderrChan := iochan.DelimReader(stderr_r, '\n')
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdout_w.Close()
|
|
|
|
defer stderr_w.Close()
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
exitChan <- cmd.ExitStatus
|
|
|
|
}()
|
|
|
|
|
|
|
|
OutputLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case output := <-stderrChan:
|
|
|
|
ui.Message(strings.TrimSpace(output))
|
|
|
|
case output := <-stdoutChan:
|
|
|
|
ui.Message(strings.TrimSpace(output))
|
|
|
|
case exitStatus := <-exitChan:
|
|
|
|
log.Printf("shell provisioner exited with status %d", exitStatus)
|
2013-06-23 20:37:40 -04:00
|
|
|
|
|
|
|
if exitStatus != 0 {
|
2013-06-26 20:52:49 -04:00
|
|
|
return fmt.Errorf("Script exited with non-zero exit status: %d", exitStatus)
|
2013-06-23 20:37:40 -04:00
|
|
|
}
|
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
break OutputLoop
|
|
|
|
}
|
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
|
2013-06-20 16:45:54 -04:00
|
|
|
// Make sure we finish off stdout/stderr because we may have gotten
|
|
|
|
// a message from the exit channel first.
|
|
|
|
for output := range stdoutChan {
|
|
|
|
ui.Message(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
for output := range stderrChan {
|
|
|
|
ui.Message(output)
|
|
|
|
}
|
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
|
|
|
}
|