unlock state in console, import, graph, and push

The state locking improvements for the regular command had the side
effect of locking the state in the console, import, graph and push
commands. Those commands had been updated to get a state via the
Backend.Context method, which locks the state whenever possible, and now
need to call Unlock directly.

Add Unlock calls to all commands that call Context directly.
This commit is contained in:
James Bardin 2018-03-20 12:44:12 -04:00
parent a5c4f7e08e
commit 90a75422fb
5 changed files with 35 additions and 0 deletions

View File

@ -81,6 +81,13 @@ func (c *ConsoleCommand) Run(args []string) int {
return 1 return 1
} }
defer func() {
err := opReq.StateLocker.Unlock(nil)
if err != nil {
c.Ui.Error(err.Error())
}
}()
// Setup the UI so we can output directly to stdout // Setup the UI so we can output directly to stdout
ui := &cli.BasicUi{ ui := &cli.BasicUi{
Writer: wrappedstreams.Stdout(), Writer: wrappedstreams.Stdout(),

View File

@ -112,6 +112,13 @@ func (c *GraphCommand) Run(args []string) int {
return 1 return 1
} }
defer func() {
err := opReq.StateLocker.Unlock(nil)
if err != nil {
c.Ui.Error(err.Error())
}
}()
// Determine the graph type // Determine the graph type
graphType := terraform.GraphTypePlan graphType := terraform.GraphTypePlan
if plan != nil { if plan != nil {

View File

@ -184,6 +184,13 @@ func (c *ImportCommand) Run(args []string) int {
return 1 return 1
} }
defer func() {
err := opReq.StateLocker.Unlock(nil)
if err != nil {
c.Ui.Error(err.Error())
}
}()
// Perform the import. Note that as you can see it is possible for this // Perform the import. Note that as you can see it is possible for this
// API to import more than one resource at once. For now, we only allow // API to import more than one resource at once. For now, we only allow
// one while we stabilize this feature. // one while we stabilize this feature.

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -175,11 +176,17 @@ func TestImport_remoteState(t *testing.T) {
"test_instance.foo", "test_instance.foo",
"bar", "bar",
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
fmt.Println(ui.OutputWriter) fmt.Println(ui.OutputWriter)
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
// verify that the local state was unlocked after import
if _, err := os.Stat(filepath.Join(td, fmt.Sprintf(".%s.lock.info", statePath))); !os.IsNotExist(err) {
t.Fatal("state left locked after import")
}
// Verify that we were called // Verify that we were called
if !configured { if !configured {
t.Fatal("Configure should be called") t.Fatal("Configure should be called")

View File

@ -146,6 +146,13 @@ func (c *PushCommand) Run(args []string) int {
return 1 return 1
} }
defer func() {
err := opReq.StateLocker.Unlock(nil)
if err != nil {
c.Ui.Error(err.Error())
}
}()
// Get the configuration // Get the configuration
config := ctx.Module().Config() config := ctx.Module().Config()
if name == "" { if name == "" {