command: apply saves state even if error occurs
This commit is contained in:
parent
5263306f1f
commit
6a7e3668a2
|
@ -60,20 +60,15 @@ func (c *ApplyCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
errCh := make(chan error)
|
||||
stateCh := make(chan *terraform.State)
|
||||
var state *terraform.State
|
||||
var applyErr error
|
||||
doneCh := make(chan struct{})
|
||||
go func() {
|
||||
state, err := ctx.Apply()
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
|
||||
stateCh <- state
|
||||
defer close(doneCh)
|
||||
state, applyErr = ctx.Apply()
|
||||
}()
|
||||
|
||||
err = nil
|
||||
var state *terraform.State
|
||||
select {
|
||||
case <-c.ShutdownCh:
|
||||
c.Ui.Output("Interrupt received. Gracefully shutting down...")
|
||||
|
@ -88,26 +83,26 @@ func (c *ApplyCommand) Run(args []string) int {
|
|||
"Two interrupts received. Exiting immediately. Note that data\n" +
|
||||
"loss may have occurred.")
|
||||
return 1
|
||||
case state = <-stateCh:
|
||||
case err = <-errCh:
|
||||
case <-doneCh:
|
||||
}
|
||||
case state = <-stateCh:
|
||||
case err = <-errCh:
|
||||
case <-doneCh:
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", err))
|
||||
return 1
|
||||
if state != nil {
|
||||
// Write state out to the file
|
||||
f, err := os.Create(stateOutPath)
|
||||
if err == nil {
|
||||
err = terraform.WriteState(state, f)
|
||||
f.Close()
|
||||
}
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// Write state out to the file
|
||||
f, err := os.Create(stateOutPath)
|
||||
if err == nil {
|
||||
err = terraform.WriteState(state, f)
|
||||
f.Close()
|
||||
}
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
|
||||
if applyErr != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", applyErr))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"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) {
|
||||
planPath := testPlanFile(t, &terraform.Plan{
|
||||
Config: new(config.Config),
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
resource "test_instance" "foo" {
|
||||
ami = "bar"
|
||||
}
|
||||
|
||||
resource "test_instance" "bar" {
|
||||
error = "true"
|
||||
}
|
|
@ -738,6 +738,7 @@ func (c *Context) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
|
|||
// Call the callack
|
||||
log.Printf("[INFO] Walking: %s", rn.Resource.Id)
|
||||
if err := cb(rn.Resource); err != nil {
|
||||
log.Printf("[ERROR] Error walking '%s': %s", rn.Resource.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue