Revert "use the new clistate.Locker in commands"

This reverts commit 400f6ca4c5.
This commit is contained in:
James Bardin 2018-02-23 11:31:06 -05:00
parent 400f6ca4c5
commit 3de0d24554
6 changed files with 73 additions and 49 deletions

View File

@ -583,11 +583,14 @@ func (m *Meta) backendFromPlan(opts *BackendOpts) (backend.Backend, error) {
}
if m.stateLock {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(realMgr, "backend from plan"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, realMgr, "backend from plan", "", m.Ui, m.Colorize())
if err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
if err := realMgr.RefreshState(); err != nil {
@ -957,11 +960,14 @@ func (m *Meta) backend_C_r_s(
}
if m.stateLock {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, sMgr, "backend from config", "", m.Ui, m.Colorize())
if err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// Store the metadata in our saved state location
@ -1033,11 +1039,14 @@ func (m *Meta) backend_C_r_S_changed(
}
if m.stateLock {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, sMgr, "backend from config", "", m.Ui, m.Colorize())
if err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// Update the backend state
@ -1169,11 +1178,14 @@ func (m *Meta) backend_C_R_S_unchanged(
}
if m.stateLock {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, sMgr, "backend from config", "", m.Ui, m.Colorize())
if err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// Unset the remote state

View File

@ -233,19 +233,20 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error {
}
if m.stateLock {
lockCtx := context.Background()
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
lockerOne := clistate.NewLocker(lockCtx, m.stateLockTimeout, m.Ui, m.Colorize())
if err := lockerOne.Lock(stateOne, "migration source state"); err != nil {
unlockOne, err := clistate.Lock(lockCtx, stateOne, "migration", "source state", m.Ui, m.Colorize())
if err != nil {
return fmt.Errorf("Error locking source state: %s", err)
}
defer lockerOne.Unlock(nil)
defer unlockOne(nil)
lockerTwo := clistate.NewLocker(lockCtx, m.stateLockTimeout, m.Ui, m.Colorize())
if err := lockerTwo.Lock(stateTwo, "migration destination state"); err != nil {
unlockTwo, err := clistate.Lock(lockCtx, stateTwo, "migration", "destination state", m.Ui, m.Colorize())
if err != nil {
return fmt.Errorf("Error locking destination state: %s", err)
}
defer lockerTwo.Unlock(nil)
defer unlockTwo(nil)
// We now own a lock, so double check that we have the version
// corresponding to the lock.

View File

@ -83,12 +83,16 @@ func (c *TaintCommand) Run(args []string) int {
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(st, "taint"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, st, "taint", "", c.Ui, c.Colorize())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// Get the actual state structure

View File

@ -71,12 +71,15 @@ func (c *UntaintCommand) Run(args []string) int {
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(st, "untaint"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, st, "untaint", "", c.Ui, c.Colorize())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// Get the actual state structure

View File

@ -96,17 +96,6 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
return 1
}
var stateLocker clistate.Locker
if c.stateLock {
stateLocker = clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(sMgr, "workspace_delete"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
} else {
stateLocker = clistate.NewNoopLocker()
}
if err := sMgr.RefreshState(); err != nil {
c.Ui.Error(err.Error())
return 1
@ -119,16 +108,28 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
return 1
}
// We need to release the lock just before deleting the state, in case
// the backend can't remove the resource while holding the lock. This
// is currently true for Windows local files.
//
// TODO: While there is little safety in locking while deleting the
// state, it might be nice to be able to coordinate processes around
// state deletion, i.e. in a CI environment. Adding Delete() as a
// required method of States would allow the removal of the resource to
// be delegated from the Backend to the State itself.
stateLocker.Unlock(nil)
// Honor the lock request, for consistency and one final safety check.
if c.stateLock {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, sMgr, "workspace delete", "", c.Ui, c.Colorize())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
// We need to release the lock just before deleting the state, in case
// the backend can't remove the resource while holding the lock. This
// is currently true for Windows local files.
//
// TODO: While there is little safety in locking while deleting the
// state, it might be nice to be able to coordinate processes around
// state deletion, i.e. in a CI environment. Adding Delete() as a
// required method of States would allow the removal of the resource to
// be delegated from the Backend to the State itself.
unlock(nil)
}
err = b.DeleteState(delEnv)
if err != nil {

View File

@ -114,12 +114,15 @@ func (c *WorkspaceNewCommand) Run(args []string) int {
}
if c.stateLock {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(sMgr, "workspace_delete"); err != nil {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()
unlock, err := clistate.Lock(lockCtx, sMgr, "workspace_new", "", c.Ui, c.Colorize())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer stateLocker.Unlock(nil)
defer unlock(nil)
}
// read the existing state file