Skip to content

Commit

Permalink
use the new clistate.Locker in commands
Browse files Browse the repository at this point in the history
Use the new StateLocker field to provide a wrapper for locking the state
during terraform.Context creation in the commands that directly
manipulate the state.
  • Loading branch information
jbardin committed Feb 23, 2018
1 parent e9a7680 commit bdd475e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 73 deletions.
36 changes: 12 additions & 24 deletions command/meta_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,11 @@ func (m *Meta) backendFromPlan(opts *BackendOpts) (backend.Backend, error) {
}

if m.stateLock {
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 {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(realMgr, "backend from plan"); err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

if err := realMgr.RefreshState(); err != nil {
Expand Down Expand Up @@ -960,14 +957,11 @@ func (m *Meta) backend_C_r_s(
}

if m.stateLock {
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 {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// Store the metadata in our saved state location
Expand Down Expand Up @@ -1039,14 +1033,11 @@ func (m *Meta) backend_C_r_S_changed(
}

if m.stateLock {
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 {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// Update the backend state
Expand Down Expand Up @@ -1178,14 +1169,11 @@ func (m *Meta) backend_C_R_S_unchanged(
}

if m.stateLock {
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 {
stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
return nil, fmt.Errorf("Error locking state: %s", err)
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// Unset the remote state
Expand Down
15 changes: 7 additions & 8 deletions command/meta_backend_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,19 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error {
}

if m.stateLock {
lockCtx, cancel := context.WithTimeout(context.Background(), m.stateLockTimeout)
defer cancel()
lockCtx := context.Background()

unlockOne, err := clistate.Lock(lockCtx, stateOne, "migration", "source state", m.Ui, m.Colorize())
if err != nil {
lockerOne := clistate.NewLocker(lockCtx, m.stateLockTimeout, m.Ui, m.Colorize())
if err := lockerOne.Lock(stateOne, "migration source state"); err != nil {
return fmt.Errorf("Error locking source state: %s", err)
}
defer unlockOne(nil)
defer lockerOne.Unlock(nil)

unlockTwo, err := clistate.Lock(lockCtx, stateTwo, "migration", "destination state", m.Ui, m.Colorize())
if err != nil {
lockerTwo := clistate.NewLocker(lockCtx, m.stateLockTimeout, m.Ui, m.Colorize())
if err := lockerTwo.Lock(stateTwo, "migration destination state"); err != nil {
return fmt.Errorf("Error locking destination state: %s", err)
}
defer unlockTwo(nil)
defer lockerTwo.Unlock(nil)

// We now own a lock, so double check that we have the version
// corresponding to the lock.
Expand Down
10 changes: 3 additions & 7 deletions command/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,12 @@ func (c *TaintCommand) Run(args []string) int {
}

if c.stateLock {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()

unlock, err := clistate.Lock(lockCtx, st, "taint", "", c.Ui, c.Colorize())
if err != nil {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(st, "taint"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}

defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// Get the actual state structure
Expand Down
9 changes: 3 additions & 6 deletions command/untaint.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,12 @@ func (c *UntaintCommand) Run(args []string) int {
}

if c.stateLock {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()

unlock, err := clistate.Lock(lockCtx, st, "untaint", "", c.Ui, c.Colorize())
if err != nil {
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
if err := stateLocker.Lock(st, "untaint"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// Get the actual state structure
Expand Down
43 changes: 21 additions & 22 deletions command/workspace_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ 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
Expand All @@ -108,28 +119,16 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
return 1
}

// 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)
}
// 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)

err = b.DeleteState(delEnv)
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions command/workspace_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,12 @@ func (c *WorkspaceNewCommand) Run(args []string) int {
}

if c.stateLock {
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 {
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
}
defer unlock(nil)
defer stateLocker.Unlock(nil)
}

// read the existing state file
Expand Down

0 comments on commit bdd475e

Please sign in to comment.