command: apply saves state even if error occurs

This commit is contained in:
Mitchell Hashimoto 2014-07-07 21:20:48 -07:00
parent 5263306f1f
commit 6a7e3668a2
4 changed files with 98 additions and 25 deletions

View File

@ -60,20 +60,15 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
errCh := make(chan error) var state *terraform.State
stateCh := make(chan *terraform.State) var applyErr error
doneCh := make(chan struct{})
go func() { go func() {
state, err := ctx.Apply() defer close(doneCh)
if err != nil { state, applyErr = ctx.Apply()
errCh <- err
return
}
stateCh <- state
}() }()
err = nil err = nil
var state *terraform.State
select { select {
case <-c.ShutdownCh: case <-c.ShutdownCh:
c.Ui.Output("Interrupt received. Gracefully shutting down...") c.Ui.Output("Interrupt received. Gracefully shutting down...")
@ -88,18 +83,12 @@ func (c *ApplyCommand) Run(args []string) int {
"Two interrupts received. Exiting immediately. Note that data\n" + "Two interrupts received. Exiting immediately. Note that data\n" +
"loss may have occurred.") "loss may have occurred.")
return 1 return 1
case state = <-stateCh: case <-doneCh:
case err = <-errCh:
} }
case state = <-stateCh: case <-doneCh:
case err = <-errCh:
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", err))
return 1
} }
if state != nil {
// Write state out to the file // Write state out to the file
f, err := os.Create(stateOutPath) f, err := os.Create(stateOutPath)
if err == nil { if err == nil {
@ -110,6 +99,12 @@ func (c *ApplyCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err)) c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
return 1 return 1
} }
}
if applyErr != nil {
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", applyErr))
return 1
}
c.Ui.Output(strings.TrimSpace(state.String())) c.Ui.Output(strings.TrimSpace(state.String()))

View File

@ -1,8 +1,10 @@
package command package command
import ( import (
"fmt"
"os" "os"
"reflect" "reflect"
"sync"
"testing" "testing"
"time" "time"
@ -67,6 +69,74 @@ func TestApply_configInvalid(t *testing.T) {
} }
} }
func TestApply_error(t *testing.T) {
statePath := testTempFile(t)
p := testProvider()
ui := new(cli.MockUi)
c := &ApplyCommand{
ContextOpts: testCtxConfig(p),
Ui: ui,
}
var lock sync.Mutex
errored := false
p.ApplyFn = func(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
lock.Lock()
defer lock.Unlock()
if !errored {
errored = true
return nil, fmt.Errorf("error")
}
return &terraform.ResourceState{ID: "foo"}, nil
}
p.DiffFn = func(
*terraform.ResourceState,
*terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
return &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ami": &terraform.ResourceAttrDiff{
New: "bar",
},
},
}, nil
}
args := []string{
"-init",
statePath,
testFixturePath("apply-error"),
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
f, err := os.Open(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
state, err := terraform.ReadState(f)
if err != nil {
t.Fatalf("err: %s", err)
}
if state == nil {
t.Fatal("state should not be nil")
}
if len(state.Resources) == 0 {
t.Fatal("no resources in state")
}
}
func TestApply_plan(t *testing.T) { func TestApply_plan(t *testing.T) {
planPath := testPlanFile(t, &terraform.Plan{ planPath := testPlanFile(t, &terraform.Plan{
Config: new(config.Config), Config: new(config.Config),

View File

@ -0,0 +1,7 @@
resource "test_instance" "foo" {
ami = "bar"
}
resource "test_instance" "bar" {
error = "true"
}

View File

@ -738,6 +738,7 @@ func (c *Context) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
// Call the callack // Call the callack
log.Printf("[INFO] Walking: %s", rn.Resource.Id) log.Printf("[INFO] Walking: %s", rn.Resource.Id)
if err := cb(rn.Resource); err != nil { if err := cb(rn.Resource); err != nil {
log.Printf("[ERROR] Error walking '%s': %s", rn.Resource.Id, err)
return err return err
} }