Merge pull request #19940 from hashicorp/svh/b-state-locking
command/state: add proper locking
This commit is contained in:
commit
ab2b17b98e
|
@ -1,10 +1,12 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/addrs"
|
"github.com/hashicorp/terraform/addrs"
|
||||||
|
"github.com/hashicorp/terraform/command/clistate"
|
||||||
"github.com/hashicorp/terraform/states"
|
"github.com/hashicorp/terraform/states"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
@ -47,8 +49,18 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
||||||
return 1
|
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 {
|
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
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,8 +83,18 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
||||||
return 1
|
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 {
|
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
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,7 +84,7 @@ func (c *StatePushCommand) Run(args []string) int {
|
||||||
|
|
||||||
if c.stateLock {
|
if c.stateLock {
|
||||||
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
|
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))
|
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/addrs"
|
"github.com/hashicorp/terraform/addrs"
|
||||||
|
"github.com/hashicorp/terraform/command/clistate"
|
||||||
"github.com/hashicorp/terraform/states"
|
"github.com/hashicorp/terraform/states"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
@ -44,6 +46,16 @@ func (c *StateRmCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
||||||
return 1
|
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 {
|
if err := stateMgr.RefreshState(); err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
|
Loading…
Reference in New Issue