Commit 8cb2fd76 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

provisioner/shell: Error if a script fails

parent e5a7fc6b
...@@ -100,7 +100,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -100,7 +100,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
return nil return nil
} }
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
scripts := make([]string, len(p.config.Scripts)) scripts := make([]string, len(p.config.Scripts))
copy(scripts, p.config.Scripts) copy(scripts, p.config.Scripts)
...@@ -109,8 +109,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -109,8 +109,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
if p.config.Inline != nil { if p.config.Inline != nil {
tf, err := ioutil.TempFile("", "packer-shell") tf, err := ioutil.TempFile("", "packer-shell")
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err)) return fmt.Errorf("Error preparing shell script: %s", err)
return
} }
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
...@@ -121,14 +120,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -121,14 +120,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
writer := bufio.NewWriter(tf) writer := bufio.NewWriter(tf)
for _, command := range p.config.Inline { for _, command := range p.config.Inline {
if _, err := writer.WriteString(command + "\n"); err != nil { if _, err := writer.WriteString(command + "\n"); err != nil {
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err)) return fmt.Errorf("Error preparing shell script: %s", err)
return
} }
} }
if err := writer.Flush(); err != nil { if err := writer.Flush(); err != nil {
ui.Error(fmt.Sprintf("Error preparing shell script: %s", err)) return fmt.Errorf("Error preparing shell script: %s", err)
return
} }
tf.Close() tf.Close()
...@@ -140,15 +137,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -140,15 +137,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
log.Printf("Opening %s for reading", path) log.Printf("Opening %s for reading", path)
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error opening shell script: %s", err)) return fmt.Errorf("Error opening shell script: %s", err)
return
} }
log.Printf("Uploading %s => %s", path, p.config.RemotePath) log.Printf("Uploading %s => %s", path, p.config.RemotePath)
err = comm.Upload(p.config.RemotePath, f) err = comm.Upload(p.config.RemotePath, f)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error uploading shell script: %s", err)) return fmt.Errorf("Error uploading shell script: %s", err)
return
} }
// Compile the command // Compile the command
...@@ -168,8 +163,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -168,8 +163,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
log.Printf("Executing command: %s", cmd.Command) log.Printf("Executing command: %s", cmd.Command)
err = comm.Start(&cmd) err = comm.Start(&cmd)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed executing command: %s", err)) return fmt.Errorf("Failed executing command: %s", err)
return
} }
exitChan := make(chan int, 1) exitChan := make(chan int, 1)
...@@ -195,7 +189,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -195,7 +189,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
log.Printf("shell provisioner exited with status %d", exitStatus) log.Printf("shell provisioner exited with status %d", exitStatus)
if exitStatus != 0 { if exitStatus != 0 {
ui.Error(fmt.Sprintf("Script excited with non-zero exit status: %d", exitStatus)) return fmt.Errorf("Script exited with non-zero exit status: %d", exitStatus)
} }
break OutputLoop break OutputLoop
...@@ -212,4 +206,6 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -212,4 +206,6 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
ui.Message(output) ui.Message(output)
} }
} }
return nil
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment