command/pull: remove remote package

This commit is contained in:
Mitchell Hashimoto 2015-02-22 11:02:19 -08:00
parent b8a66cb6ca
commit f2c6c12535
4 changed files with 77 additions and 22 deletions

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/terraform/remote" "github.com/hashicorp/terraform/state"
) )
type PullCommand struct { type PullCommand struct {
@ -20,32 +20,50 @@ func (c *PullCommand) Run(args []string) int {
return 1 return 1
} }
// Recover the local state if any // Read out our state
local, _, err := remote.ReadLocalState() s, err := c.State()
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("%s", err)) c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
return 1 return 1
} }
if local == nil || local.Remote == nil { localState := s.State()
// If remote state isn't enabled, it is a problem.
if !localState.IsRemote() {
c.Ui.Error("Remote state not enabled!") c.Ui.Error("Remote state not enabled!")
return 1 return 1
} }
// Attempt the state refresh // We need the CacheState structure in order to do anything
change, err := remote.RefreshState(local.Remote) var cache *state.CacheState
if err != nil { if bs, ok := s.(*state.BackupState); ok {
if cs, ok := bs.Real.(*state.CacheState); ok {
cache = cs
}
}
if cache == nil {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"Failed to refresh from remote state: %v", err)) "Failed to extract internal CacheState from remote state.\n" +
"This is an internal error, please report it as a bug."))
return 1
}
// Refresh the state
if err := cache.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf(
"Failed to refresh from remote state: %s", err))
return 1 return 1
} }
// Use an error exit code if the update was not a success // Use an error exit code if the update was not a success
change := cache.RefreshResult()
if !change.SuccessfulPull() { if !change.SuccessfulPull() {
c.Ui.Error(fmt.Sprintf("%s", change)) c.Ui.Error(fmt.Sprintf("%s", change))
return 1 return 1
} else { } else {
c.Ui.Output(fmt.Sprintf("%s", change)) c.Ui.Output(fmt.Sprintf("%s", change))
} }
return 0 return 0
} }

View File

@ -7,9 +7,10 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path/filepath"
"testing" "testing"
"github.com/hashicorp/terraform/remote"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -46,10 +47,19 @@ func TestPull_local(t *testing.T) {
defer srv.Close() defer srv.Close()
// Store the local state // Store the local state
buf := bytes.NewBuffer(nil) statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
terraform.WriteState(s, buf) if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
remote.EnsureDirectory() t.Fatalf("err: %s", err)
remote.Persist(buf) }
f, err := os.Create(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
err = terraform.WriteState(s, f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &PullCommand{ c := &PullCommand{

View File

@ -70,10 +70,15 @@ func State(opts *StateOpts) (state.State, string, error) {
} }
} else { } else {
if result != nil { if result != nil {
// We already have a remote state... that is an error. if !local.State().Empty() {
return nil, "", fmt.Errorf( // We already have a remote state... that is an error.
"Remote state found, but state file '%s' also present.", return nil, "", fmt.Errorf(
opts.LocalPath) "Remote state found, but state file '%s' also present.",
opts.LocalPath)
}
// Empty state
local = nil
} }
} }
if err != nil { if err != nil {
@ -81,10 +86,12 @@ func State(opts *StateOpts) (state.State, string, error) {
"Error reading local state: {{err}}", err) "Error reading local state: {{err}}", err)
} }
result = local if local != nil {
resultPath = opts.LocalPath result = local
if opts.LocalPathOut != "" { resultPath = opts.LocalPath
resultPath = opts.LocalPathOut if opts.LocalPathOut != "" {
resultPath = opts.LocalPathOut
}
} }
} }

View File

@ -211,3 +211,23 @@ func (sc CacheRefreshResult) String() string {
return fmt.Sprintf("Unknown state change type: %d", sc) return fmt.Sprintf("Unknown state change type: %d", sc)
} }
} }
// SuccessfulPull is used to clasify the CacheRefreshResult for
// a refresh operation. This is different by operation, but can be used
// to determine a proper exit code.
func (sc CacheRefreshResult) SuccessfulPull() bool {
switch sc {
case CacheRefreshNoop:
return true
case CacheRefreshInit:
return true
case CacheRefreshUpdateLocal:
return true
case CacheRefreshLocalNewer:
return false
case CacheRefreshConflict:
return false
default:
return false
}
}