backend/local: use new command/state package for better UX
This commit is contained in:
parent
34f438b635
commit
65982bd412
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
"github.com/hashicorp/terraform/state"
|
clistate "github.com/hashicorp/terraform/command/state"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,22 +35,14 @@ func (b *Local) opApply(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// context acquired the state, and therefor the lock.
|
// If we're locking state, unlock when we're done
|
||||||
// Unlock it when the operation is complete
|
if op.LockState {
|
||||||
defer func() {
|
defer func() {
|
||||||
if s, ok := opState.(state.Locker); op.LockState && ok {
|
if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil {
|
||||||
if err := s.Unlock(); err != nil {
|
runningOp.Err = multierror.Append(runningOp.Err, err)
|
||||||
runningOp.Err = multierror.Append(runningOp.Err,
|
|
||||||
errwrap.Wrapf("Error unlocking state:\n\n"+
|
|
||||||
"{{err}}\n\n"+
|
|
||||||
"The Terraform operation completed but there was an error unlocking the state.\n"+
|
|
||||||
"This may require unlocking the state manually with the `terraform unlock` command\n",
|
|
||||||
err,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}()
|
}
|
||||||
|
|
||||||
// Setup the state
|
// Setup the state
|
||||||
runningOp.State = tfCtx.State()
|
runningOp.State = tfCtx.State()
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
|
clistate "github.com/hashicorp/terraform/command/state"
|
||||||
"github.com/hashicorp/terraform/state"
|
"github.com/hashicorp/terraform/state"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
@ -28,8 +29,9 @@ func (b *Local) context(op *backend.Operation) (*terraform.Context, state.State,
|
||||||
return nil, nil, errwrap.Wrapf("Error loading state: {{err}}", err)
|
return nil, nil, errwrap.Wrapf("Error loading state: {{err}}", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, ok := s.(state.Locker); op.LockState && ok {
|
if op.LockState {
|
||||||
if err := s.Lock(op.Type.String()); err != nil {
|
err := clistate.Lock(s, op.Type.String(), b.CLI, b.Colorize())
|
||||||
|
if err != nil {
|
||||||
return nil, nil, errwrap.Wrapf("Error locking state: {{err}}", err)
|
return nil, nil, errwrap.Wrapf("Error locking state: {{err}}", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
"github.com/hashicorp/terraform/command/format"
|
"github.com/hashicorp/terraform/command/format"
|
||||||
|
clistate "github.com/hashicorp/terraform/command/state"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
"github.com/hashicorp/terraform/state"
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -58,15 +59,14 @@ func (b *Local) opPlan(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// context acquired the state, and therefor the lock.
|
// If we're locking state, unlock when we're done
|
||||||
// Unlock it when the operation is complete
|
if op.LockState {
|
||||||
defer func() {
|
defer func() {
|
||||||
if s, ok := opState.(state.Locker); op.LockState && ok {
|
if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil {
|
||||||
if err := s.Unlock(); err != nil {
|
runningOp.Err = multierror.Append(runningOp.Err, err)
|
||||||
log.Printf("[ERROR]: %s", err)
|
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}()
|
}
|
||||||
|
|
||||||
// Setup the state
|
// Setup the state
|
||||||
runningOp.State = tfCtx.State()
|
runningOp.State = tfCtx.State()
|
||||||
|
|
|
@ -3,12 +3,12 @@ package local
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
"github.com/hashicorp/terraform/state"
|
clistate "github.com/hashicorp/terraform/command/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Local) opRefresh(
|
func (b *Local) opRefresh(
|
||||||
|
@ -48,15 +48,14 @@ func (b *Local) opRefresh(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// context acquired the state, and therefor the lock.
|
// If we're locking state, unlock when we're done
|
||||||
// Unlock it when the operation is complete
|
if op.LockState {
|
||||||
defer func() {
|
defer func() {
|
||||||
if s, ok := opState.(state.Locker); op.LockState && ok {
|
if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil {
|
||||||
if err := s.Unlock(); err != nil {
|
runningOp.Err = multierror.Append(runningOp.Err, err)
|
||||||
log.Printf("[ERROR]: %s", err)
|
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}()
|
}
|
||||||
|
|
||||||
// Set our state
|
// Set our state
|
||||||
runningOp.State = opState.State()
|
runningOp.State = opState.State()
|
||||||
|
|
Loading…
Reference in New Issue