Merge pull request #19940 from hashicorp/svh/b-state-locking

command/state: add proper locking
This commit is contained in:
Sander van Harmelen 2019-01-08 18:25:58 +01:00 committed by GitHub
commit ab2b17b98e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -1,10 +1,12 @@
package command
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/command/clistate"
"github.com/hashicorp/terraform/states"
"github.com/mitchellh/cli"
)
@ -47,8 +49,18 @@ func (c *StateMvCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return 1
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(stateFromMgr, "state-mv"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking source state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
}
if err := stateFromMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
c.Ui.Error(fmt.Sprintf("Failed to refresh source state: %s", err))
return 1
}
@ -71,8 +83,18 @@ func (c *StateMvCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return 1
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(stateToMgr, "state-mv"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking destination state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
}
if err := stateToMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
c.Ui.Error(fmt.Sprintf("Failed to refresh destination state: %s", err))
return 1
}

View File

@ -84,7 +84,7 @@ func (c *StatePushCommand) Run(args []string) int {
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(stateMgr, "taint"); err != nil {
if err := stateLocker.Lock(stateMgr, "state-push"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}

View File

@ -1,11 +1,13 @@
package command
import (
"context"
"fmt"
"sort"
"strings"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/command/clistate"
"github.com/hashicorp/terraform/states"
"github.com/mitchellh/cli"
)
@ -44,6 +46,16 @@ func (c *StateRmCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return 1
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(stateMgr, "state-rm"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
}
if err := stateMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
return 1