2014-10-01 01:05:16 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-03 20:14:39 +02:00
|
|
|
"crypto/md5"
|
2014-10-01 01:05:16 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-10-03 20:14:39 +02:00
|
|
|
"io/ioutil"
|
2014-10-01 01:05:16 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LocalDirectory is the directory created in the working
|
|
|
|
// dir to hold the remote state file.
|
|
|
|
LocalDirectory = ".terraform"
|
|
|
|
|
|
|
|
// HiddenStateFile is the name of the state file in the
|
|
|
|
// LocalDirectory
|
|
|
|
HiddenStateFile = "terraform.tfstate"
|
|
|
|
|
|
|
|
// BackupHiddenStateFile is the path we backup the state
|
|
|
|
// file to before modifications are made
|
|
|
|
BackupHiddenStateFile = "terraform.tfstate.backup"
|
|
|
|
)
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
// StateChangeResult is used to communicate to a caller
|
|
|
|
// what actions have been taken when updating a state file
|
|
|
|
type StateChangeResult int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StateChangeNoop indicates nothing has happened,
|
|
|
|
// but that does not indicate an error. Everything is
|
|
|
|
// just up to date. (Push/Pull)
|
|
|
|
StateChangeNoop StateChangeResult = iota
|
|
|
|
|
2014-10-03 21:00:21 +02:00
|
|
|
// StateChangeInit indicates that there is no local or
|
|
|
|
// remote state, and that the state was initialized
|
|
|
|
StateChangeInit
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
// StateChangeUpdateLocal indicates the local state
|
|
|
|
// was updated. (Pull)
|
|
|
|
StateChangeUpdateLocal
|
|
|
|
|
|
|
|
// StateChangeUpdateRemote indicates the remote state
|
|
|
|
// was updated. (Push)
|
|
|
|
StateChangeUpdateRemote
|
|
|
|
|
|
|
|
// StateChangeLocalNewer means the pull was a no-op
|
|
|
|
// because the local state is newer than that of the
|
|
|
|
// server. This means a Push should take place. (Pull)
|
|
|
|
StateChangeLocalNewer
|
|
|
|
|
|
|
|
// StateChangeRemoteNewer means the push was a no-op
|
|
|
|
// because the remote state is newer than that of the
|
|
|
|
// local state. This means a Pull should take place.
|
|
|
|
// (Push)
|
|
|
|
StateChangeRemoteNewer
|
|
|
|
|
|
|
|
// StateChangeConflict means that the push or pull
|
|
|
|
// was a no-op because there is a conflict. This means
|
|
|
|
// there are multiple state definitions at the same
|
|
|
|
// serial number with different contents. This requires
|
|
|
|
// an operator to intervene and resolve the conflict.
|
|
|
|
// Shame on the user for doing concurrent apply.
|
|
|
|
// (Push/Pull)
|
|
|
|
StateChangeConflict
|
|
|
|
)
|
|
|
|
|
|
|
|
func (sc StateChangeResult) String() string {
|
|
|
|
switch sc {
|
|
|
|
case StateChangeNoop:
|
|
|
|
return "Local and remote state in sync"
|
2014-10-03 21:00:21 +02:00
|
|
|
case StateChangeInit:
|
|
|
|
return "Local state initialized"
|
2014-10-03 20:14:39 +02:00
|
|
|
case StateChangeUpdateLocal:
|
|
|
|
return "Local state updated"
|
|
|
|
case StateChangeUpdateRemote:
|
|
|
|
return "Remote state updated"
|
|
|
|
case StateChangeLocalNewer:
|
|
|
|
return "Local state is newer than remote state, push required"
|
|
|
|
case StateChangeRemoteNewer:
|
|
|
|
return "Remote state is newer than local state, pull required"
|
|
|
|
case StateChangeConflict:
|
|
|
|
return "Local and remote state conflict, manual resolution required"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("Unknown state change type: %d", sc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessfulPull is used to clasify the StateChangeResult for
|
|
|
|
// a pull operation. This is different by operation, but can be used
|
|
|
|
// to determine a proper exit code.
|
|
|
|
func (sc StateChangeResult) SuccessfulPull() bool {
|
|
|
|
switch sc {
|
|
|
|
case StateChangeNoop:
|
|
|
|
return true
|
2014-10-03 21:00:21 +02:00
|
|
|
case StateChangeInit:
|
|
|
|
return true
|
2014-10-03 20:14:39 +02:00
|
|
|
case StateChangeUpdateLocal:
|
|
|
|
return true
|
|
|
|
case StateChangeLocalNewer:
|
|
|
|
return false
|
|
|
|
case StateChangeConflict:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:28:47 +02:00
|
|
|
// SuccessfulPush is used to clasify the StateChangeResult for
|
|
|
|
// a push operation. This is different by operation, but can be used
|
|
|
|
// to determine a proper exit code
|
|
|
|
func (sc StateChangeResult) SuccessfulPush() bool {
|
|
|
|
switch sc {
|
|
|
|
case StateChangeNoop:
|
|
|
|
return true
|
|
|
|
case StateChangeUpdateRemote:
|
|
|
|
return true
|
|
|
|
case StateChangeRemoteNewer:
|
|
|
|
return false
|
|
|
|
case StateChangeConflict:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:05:16 +02:00
|
|
|
// EnsureDirectory is used to make sure the local storage
|
|
|
|
// directory exists
|
|
|
|
func EnsureDirectory() error {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get current directory: %v", err)
|
|
|
|
}
|
|
|
|
path := filepath.Join(cwd, LocalDirectory)
|
|
|
|
if err := os.Mkdir(path, 0770); err != nil {
|
2014-10-02 06:29:37 +02:00
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-01 01:05:16 +02:00
|
|
|
return fmt.Errorf("Failed to make directory '%s': %v", path, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HiddenStatePath is used to return the path to the hidden state file,
|
|
|
|
// should there be one.
|
2014-10-10 01:28:05 +02:00
|
|
|
// TODO: Rename to LocalStatePath
|
2014-10-01 01:05:16 +02:00
|
|
|
func HiddenStatePath() (string, error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to get current directory: %v", err)
|
|
|
|
}
|
|
|
|
path := filepath.Join(cwd, LocalDirectory, HiddenStateFile)
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 01:28:05 +02:00
|
|
|
// HaveLocalState is used to check if we have a local state file
|
|
|
|
func HaveLocalState() (bool, error) {
|
|
|
|
path, err := HiddenStatePath()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return ExistsFile(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistsFile is used to check if a given file exists
|
|
|
|
func ExistsFile(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidConfig does a purely logical validation of the remote config
|
|
|
|
func ValidConfig(conf *terraform.RemoteState) error {
|
2014-12-04 05:05:29 +01:00
|
|
|
// Default the type to Atlas
|
|
|
|
if conf.Type == "" {
|
|
|
|
conf.Type = "atlas"
|
2014-10-01 01:05:16 +02:00
|
|
|
}
|
2014-12-05 01:57:11 +01:00
|
|
|
_, err := NewClientByState(conf)
|
2014-12-04 05:05:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-01 01:05:16 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:48:46 +02:00
|
|
|
// ReadLocalState is used to read and parse the local state file
|
|
|
|
func ReadLocalState() (*terraform.State, []byte, error) {
|
|
|
|
path, err := HiddenStatePath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the existing file
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
return nil, nil, fmt.Errorf("Failed to open state file '%s': %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the state
|
|
|
|
state, err := terraform.ReadState(bytes.NewReader(raw))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to read state file '%s': %v", path, err)
|
|
|
|
}
|
|
|
|
return state, raw, nil
|
|
|
|
}
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
// RefreshState is used to read the remote state given
|
|
|
|
// the configuration for the remote endpoint, and update
|
|
|
|
// the local state if necessary.
|
|
|
|
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
2014-10-12 03:57:12 +02:00
|
|
|
if conf == nil {
|
|
|
|
return StateChangeNoop, fmt.Errorf("Missing remote server configuration")
|
|
|
|
}
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
// Read the state from the server
|
2014-12-05 01:57:11 +01:00
|
|
|
client, err := NewClientByState(conf)
|
2014-12-04 05:05:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to create remote client: %v", err)
|
|
|
|
}
|
2014-10-07 02:34:36 +02:00
|
|
|
payload, err := client.GetState()
|
2014-10-03 20:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to read remote state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the remote state
|
|
|
|
var remoteState *terraform.State
|
|
|
|
if payload != nil {
|
|
|
|
remoteState, err = terraform.ReadState(bytes.NewReader(payload.State))
|
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to parse remote state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we understand the remote version!
|
|
|
|
if remoteState.Version > terraform.StateVersion {
|
|
|
|
return StateChangeNoop, fmt.Errorf(
|
|
|
|
`Remote state is version %d, this version of Terraform only understands up to %d`, remoteState.Version, terraform.StateVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:48:46 +02:00
|
|
|
// Decode the state
|
|
|
|
localState, raw, err := ReadLocalState()
|
2014-10-03 20:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to handle the matrix of cases in reconciling
|
|
|
|
// the local and remote state. Primarily the concern is
|
|
|
|
// around the Serial number which should grow monotonically.
|
|
|
|
// Additionally, we use the MD5 to detect a conflict for
|
|
|
|
// a given Serial.
|
|
|
|
switch {
|
|
|
|
case remoteState == nil && localState == nil:
|
|
|
|
// Initialize a blank state
|
|
|
|
out, _ := blankState(conf)
|
|
|
|
if err := Persist(bytes.NewReader(out)); err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to persist state: %v", err)
|
|
|
|
}
|
2014-10-03 21:00:21 +02:00
|
|
|
return StateChangeInit, nil
|
2014-10-03 20:14:39 +02:00
|
|
|
|
|
|
|
case remoteState == nil && localState != nil:
|
|
|
|
// User should probably do a push, nothing to do
|
|
|
|
return StateChangeLocalNewer, nil
|
|
|
|
|
|
|
|
case remoteState != nil && localState == nil:
|
2014-10-08 21:05:26 +02:00
|
|
|
goto PERSIST
|
|
|
|
|
|
|
|
case remoteState.Serial < localState.Serial:
|
|
|
|
// User should probably do a push, nothing to do
|
|
|
|
return StateChangeLocalNewer, nil
|
|
|
|
|
2014-10-03 20:14:39 +02:00
|
|
|
case remoteState.Serial > localState.Serial:
|
2014-10-08 21:05:26 +02:00
|
|
|
goto PERSIST
|
2014-10-03 20:14:39 +02:00
|
|
|
|
|
|
|
case remoteState.Serial == localState.Serial:
|
|
|
|
// Check for a hash collision on the local/remote state
|
|
|
|
localMD5 := md5.Sum(raw)
|
|
|
|
if bytes.Equal(localMD5[:md5.Size], payload.MD5) {
|
|
|
|
// Hash collision, everything is up-to-date
|
|
|
|
return StateChangeNoop, nil
|
|
|
|
} else {
|
|
|
|
// This is very bad. This means we have 2 state files
|
|
|
|
// with the same Serial but a different hash. Most probably
|
|
|
|
// explaination is two parallel apply operations. This
|
|
|
|
// requires a manual reconciliation.
|
|
|
|
return StateChangeConflict, nil
|
|
|
|
}
|
2014-10-08 21:05:26 +02:00
|
|
|
default:
|
|
|
|
// We should not reach this point
|
|
|
|
panic("Unhandled remote update case")
|
2014-10-03 20:14:39 +02:00
|
|
|
}
|
2014-10-01 01:05:16 +02:00
|
|
|
|
2014-10-08 21:05:26 +02:00
|
|
|
PERSIST:
|
|
|
|
// Update the local state from the remote state
|
|
|
|
if err := Persist(bytes.NewReader(payload.State)); err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to persist state: %v", err)
|
|
|
|
}
|
|
|
|
return StateChangeUpdateLocal, nil
|
2014-10-01 01:05:16 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:28:47 +02:00
|
|
|
// PushState is used to read the local state and
|
|
|
|
// update the remote state if necessary. The state push
|
|
|
|
// can be 'forced' to override any conflict detection
|
|
|
|
// on the server-side.
|
|
|
|
func PushState(conf *terraform.RemoteState, force bool) (StateChangeResult, error) {
|
2014-10-08 19:48:46 +02:00
|
|
|
// Read the local state
|
|
|
|
_, raw, err := ReadLocalState()
|
2014-10-08 19:28:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is no local state
|
|
|
|
if raw == nil {
|
|
|
|
return StateChangeNoop, fmt.Errorf("No local state to push")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the state to the server
|
2014-12-05 01:57:11 +01:00
|
|
|
client, err := NewClientByState(conf)
|
2014-12-04 05:05:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return StateChangeNoop,
|
|
|
|
fmt.Errorf("Failed to create remote client: %v", err)
|
|
|
|
}
|
2014-10-08 19:28:47 +02:00
|
|
|
err = client.PutState(raw, force)
|
|
|
|
|
|
|
|
// Handle the various edge cases
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return StateChangeUpdateRemote, nil
|
|
|
|
case ErrServerNewer:
|
|
|
|
return StateChangeRemoteNewer, nil
|
|
|
|
case ErrConflict:
|
|
|
|
return StateChangeConflict, nil
|
|
|
|
default:
|
|
|
|
return StateChangeNoop, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 03:07:05 +01:00
|
|
|
// DeleteState is used to delete the remote state given
|
|
|
|
// the configuration for the remote endpoint.
|
|
|
|
func DeleteState(conf *terraform.RemoteState) error {
|
|
|
|
if conf == nil {
|
|
|
|
return fmt.Errorf("Missing remote server configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the client
|
|
|
|
client, err := NewClientByState(conf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create remote client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the state
|
|
|
|
err = client.DeleteState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to delete remote state: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:05:16 +02:00
|
|
|
// blankState is used to return a serialized form of a blank state
|
|
|
|
// with only the remote info.
|
2014-10-03 20:14:39 +02:00
|
|
|
func blankState(conf *terraform.RemoteState) ([]byte, error) {
|
2014-10-01 01:05:16 +02:00
|
|
|
blank := terraform.NewState()
|
|
|
|
blank.Remote = conf
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err := terraform.WriteState(blank, buf)
|
2014-10-03 20:14:39 +02:00
|
|
|
return buf.Bytes(), err
|
2014-10-01 01:05:16 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 01:28:05 +02:00
|
|
|
// PersistState is used to persist out the given terraform state
|
|
|
|
// in our local state cache location.
|
|
|
|
func PersistState(s *terraform.State) error {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if err := terraform.WriteState(s, buf); err != nil {
|
|
|
|
return fmt.Errorf("Failed to encode state: %v", err)
|
|
|
|
}
|
|
|
|
if err := Persist(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:05:16 +02:00
|
|
|
// Persist is used to write out the state given by a reader (likely
|
|
|
|
// being streamed from a remote server) to the local storage.
|
|
|
|
func Persist(r io.Reader) error {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get current directory: %v", err)
|
|
|
|
}
|
|
|
|
statePath := filepath.Join(cwd, LocalDirectory, HiddenStateFile)
|
|
|
|
backupPath := filepath.Join(cwd, LocalDirectory, BackupHiddenStateFile)
|
|
|
|
|
|
|
|
// Backup the old file if it exists
|
2014-10-12 02:49:37 +02:00
|
|
|
if err := CopyFile(statePath, backupPath); err != nil {
|
2014-10-01 01:05:16 +02:00
|
|
|
return fmt.Errorf("Failed to backup state file '%s' to '%s': %v", statePath, backupPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the state path
|
|
|
|
fh, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to open state file '%s': %v", statePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the new state
|
|
|
|
_, err = io.Copy(fh, r)
|
|
|
|
fh.Close()
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(statePath)
|
|
|
|
return fmt.Errorf("Failed to persist state file: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-12 02:49:37 +02:00
|
|
|
// CopyFile is used to copy from a source file if it exists to a destination.
|
2014-10-01 01:05:16 +02:00
|
|
|
// This is used to create a backup of the state file.
|
2014-10-12 02:49:37 +02:00
|
|
|
func CopyFile(src, dst string) error {
|
2014-10-01 01:05:16 +02:00
|
|
|
srcFH, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFH.Close()
|
|
|
|
|
|
|
|
dstFH, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFH.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(dstFH, srcFH)
|
|
|
|
return err
|
|
|
|
}
|