command/push: no more remote package

This commit is contained in:
Mitchell Hashimoto 2015-02-22 11:07:51 -08:00
parent f2c6c12535
commit 0d39a5d9a7
2 changed files with 50 additions and 20 deletions

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/terraform/remote" "github.com/hashicorp/terraform/state"
) )
type PushCommand struct { type PushCommand struct {
@ -22,31 +22,52 @@ func (c *PushCommand) Run(args []string) int {
return 1 return 1
} }
// Check for a remote state file // 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 to push the state // We need the CacheState structure in order to do anything
change, err := remote.PushState(local.Remote, force) var cache *state.CacheState
if err != nil { if bs, ok := s.(*state.BackupState); ok {
c.Ui.Error(fmt.Sprintf("Failed to push state: %v", err)) if cs, ok := bs.Real.(*state.CacheState); ok {
cache = cs
}
}
if cache == nil {
c.Ui.Error(fmt.Sprintf(
"Failed to extract internal CacheState from remote state.\n" +
"This is an internal error, please report it as a bug."))
return 1 return 1
} }
// Use an error exit code if the update was not a success // Refresh the cache state
if !change.SuccessfulPush() { if err := cache.Cache.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("%s", change)) c.Ui.Error(fmt.Sprintf(
"Failed to refresh from remote state: %s", err))
return 1 return 1
} else {
c.Ui.Output(fmt.Sprintf("%s", change))
} }
// Write it to the real storage
remote := cache.Durable
if err := remote.WriteState(cache.Cache.State()); err != nil {
c.Ui.Error(fmt.Sprintf("Error writing state: %s", err))
return 1
}
if err := remote.PersistState(); err != nil {
c.Ui.Error(fmt.Sprintf("Error saving state: %s", err))
return 1
}
return 0 return 0
} }

View File

@ -1,10 +1,10 @@
package command package command
import ( import (
"bytes" "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"
) )
@ -41,10 +41,19 @@ func TestPush_local(t *testing.T) {
s.Remote = conf s.Remote = conf
// 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 := &PushCommand{ c := &PushCommand{