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-05-28 00:54:19 -04:00
|
|
|
"fmt"
|
2013-06-11 16:42:15 -04:00
|
|
|
"errors"
|
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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// should be used to specify where the script goes.
|
|
|
|
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 {
|
|
|
|
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-11 16:52:32 -04:00
|
|
|
p.config.ExecuteCommand = "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-11 16:42:15 -04:00
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
if p.config.Path == "" && p.config.Inline == nil {
|
|
|
|
errs = append(errs, errors.New("Either a path or inline script must be specified."))
|
|
|
|
} else if p.config.Path != "" && p.config.Inline != nil {
|
|
|
|
errs = append(errs, errors.New("Only a path or an inline script can be specified, not both."))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.Path != "" {
|
|
|
|
if _, err := os.Stat(p.config.Path); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Bad script path: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
|
2013-06-11 16:52:32 -04:00
|
|
|
path := p.config.Path
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Set the path to the temporary file
|
|
|
|
path = tf.Name()
|
|
|
|
|
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
|
|
|
for _, command := range p.config.Inline {
|
|
|
|
if _, err := writer.WriteString(command+"\n"); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tf.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Provisioning with shell script: %s", path))
|
2013-06-02 00:59:33 -04:00
|
|
|
|
2013-06-11 16:52:32 -04:00
|
|
|
log.Printf("Opening %s for reading", path)
|
|
|
|
f, err := os.Open(path)
|
2013-05-28 00:54:19 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error opening shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-11 16:52:32 -04:00
|
|
|
log.Printf("Uploading %s => %s", path, p.config.RemotePath)
|
2013-05-28 00:54:19 -04:00
|
|
|
err = comm.Upload(p.config.RemotePath, f)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error uploading shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-07 02:14:31 -04:00
|
|
|
// Compile the command
|
|
|
|
var command bytes.Buffer
|
|
|
|
t := template.Must(template.New("command").Parse(p.config.ExecuteCommand))
|
|
|
|
t.Execute(&command, &ExecuteCommandTemplate{p.config.RemotePath})
|
|
|
|
|
2013-06-03 02:34:55 -04:00
|
|
|
// Setup the remote command
|
|
|
|
stdout_r, stdout_w := io.Pipe()
|
|
|
|
stderr_r, stderr_w := io.Pipe()
|
|
|
|
|
|
|
|
var cmd packer.RemoteCmd
|
2013-06-07 02:14:31 -04:00
|
|
|
cmd.Command = command.String()
|
2013-06-03 02:34:55 -04:00
|
|
|
cmd.Stdout = stdout_w
|
|
|
|
cmd.Stderr = stderr_w
|
|
|
|
|
|
|
|
log.Printf("Executing command: %s", cmd.Command)
|
|
|
|
err = comm.Start(&cmd)
|
2013-05-28 00:54:19 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed executing command: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:34:55 -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()
|
|
|
|
|
2013-06-03 13:56:12 -04:00
|
|
|
cmd.Wait()
|
2013-06-03 02:34:55 -04:00
|
|
|
exitChan <- cmd.ExitStatus
|
|
|
|
}()
|
2013-06-02 00:59:33 -04:00
|
|
|
|
2013-06-02 22:03:26 -04:00
|
|
|
OutputLoop:
|
2013-06-02 00:59:33 -04:00
|
|
|
for {
|
|
|
|
select {
|
2013-06-03 02:34:55 -04:00
|
|
|
case output := <-stderrChan:
|
2013-06-03 14:40:08 -04:00
|
|
|
ui.Message(strings.TrimSpace(output))
|
2013-06-03 02:34:55 -04:00
|
|
|
case output := <-stdoutChan:
|
2013-06-03 14:40:08 -04:00
|
|
|
ui.Message(strings.TrimSpace(output))
|
2013-06-03 02:34:55 -04:00
|
|
|
case exitStatus := <-exitChan:
|
2013-06-02 00:59:33 -04:00
|
|
|
log.Printf("shell provisioner exited with status %d", exitStatus)
|
2013-06-02 22:03:26 -04:00
|
|
|
break OutputLoop
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we finish off stdout/stderr because we may have gotten
|
|
|
|
// a message from the exit channel first.
|
2013-06-03 02:34:55 -04:00
|
|
|
for output := range stdoutChan {
|
2013-06-03 14:40:08 -04:00
|
|
|
ui.Message(output)
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:34:55 -04:00
|
|
|
for output := range stderrChan {
|
2013-06-03 14:40:08 -04:00
|
|
|
ui.Message(output)
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|