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-05-28 00:54:19 -04:00
|
|
|
"fmt"
|
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-05-28 00:54:19 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
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
|
|
|
// TODO(mitchellh): config
|
|
|
|
type config struct {
|
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.
|
|
|
|
RemotePath string
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raw interface{}, ui packer.Ui) {
|
2013-05-27 18:06:34 -04:00
|
|
|
// TODO: errors
|
|
|
|
_ = mapstructure.Decode(raw, &p.config)
|
|
|
|
|
|
|
|
if p.config.RemotePath == "" {
|
|
|
|
p.config.RemotePath = DefaultRemotePath
|
|
|
|
}
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
|
2013-06-02 00:59:33 -04:00
|
|
|
ui.Say(fmt.Sprintf("Provisioning with shell script: %s", p.config.Path))
|
|
|
|
|
2013-05-28 00:54:19 -04:00
|
|
|
log.Printf("Opening %s for reading", p.config.Path)
|
|
|
|
f, err := os.Open(p.config.Path)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error opening shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Uploading %s => %s", p.config.Path, p.config.RemotePath)
|
|
|
|
err = comm.Upload(p.config.RemotePath, f)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error uploading shell script: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command := fmt.Sprintf("chmod +x %s && %s", p.config.RemotePath, p.config.RemotePath)
|
|
|
|
log.Printf("Executing command: %s", command)
|
|
|
|
cmd, err := comm.Start(command)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed executing command: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-02 00:59:33 -04:00
|
|
|
exit := cmd.ExitChan()
|
|
|
|
stderr := cmd.StderrChan()
|
|
|
|
stdout := cmd.StdoutChan()
|
|
|
|
|
2013-06-02 22:03:26 -04:00
|
|
|
OutputLoop:
|
2013-06-02 00:59:33 -04:00
|
|
|
for {
|
|
|
|
select {
|
2013-06-02 22:03:26 -04:00
|
|
|
case output, ok := <-stderr:
|
|
|
|
if !ok {
|
|
|
|
stderr = nil
|
|
|
|
} else {
|
|
|
|
ui.Say(output)
|
|
|
|
}
|
|
|
|
case output, ok := <-stdout:
|
|
|
|
if !ok {
|
|
|
|
stdout = nil
|
|
|
|
} else {
|
|
|
|
ui.Say(output)
|
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
case exitStatus := <-exit:
|
|
|
|
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-02 22:03:26 -04:00
|
|
|
if stdout != nil {
|
|
|
|
for output := range stdout {
|
|
|
|
ui.Say(output)
|
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-06-02 22:03:26 -04:00
|
|
|
if stderr != nil {
|
|
|
|
for output := range stderr {
|
|
|
|
ui.Say(output)
|
|
|
|
}
|
2013-06-02 00:59:33 -04:00
|
|
|
}
|
2013-05-24 01:38:40 -04:00
|
|
|
}
|