Switch from Path to PathOut on LocalState.written

After LocalState writes to a state file, we will refresh off the new
state file rather than the original Path argument.
This commit is contained in:
James Bardin 2017-01-30 15:33:21 -05:00
parent 8f7f1917f2
commit 3fdcbda3aa
2 changed files with 17 additions and 25 deletions

View File

@ -3,6 +3,7 @@ package state
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -37,8 +38,7 @@ type LocalState struct {
Path string Path string
PathOut string PathOut string
// the file handles corresponding to Path and PathOut // the file handle corresponding to PathOut
stateFile *os.File
stateFileOut *os.File stateFileOut *os.File
state *terraform.State state *terraform.State
@ -66,7 +66,7 @@ func (s *LocalState) Lock(reason string) error {
} }
if err := s.lock(); err != nil { if err := s.lock(); err != nil {
if info, err := s.lockInfo(); err != nil { if info, err := s.lockInfo(); err == nil {
return info.Err() return info.Err()
} }
return fmt.Errorf("state file %q locked: %s", s.Path, err) return fmt.Errorf("state file %q locked: %s", s.Path, err)
@ -82,23 +82,11 @@ func (s *LocalState) Unlock() error {
// Open the state file, creating the directories and file as needed. // Open the state file, creating the directories and file as needed.
func (s *LocalState) createStateFiles() error { func (s *LocalState) createStateFiles() error {
f, err := createFileAndDirs(s.Path)
if err != nil {
return err
}
s.stateFile = f
if s.PathOut == "" { if s.PathOut == "" {
s.PathOut = s.Path s.PathOut = s.Path
} }
if s.PathOut == s.Path { f, err := createFileAndDirs(s.PathOut)
s.stateFileOut = s.stateFile
return nil
}
f, err = createFileAndDirs(s.PathOut)
if err != nil { if err != nil {
return err return err
} }
@ -168,18 +156,22 @@ func (s *LocalState) PersistState() error {
// StateRefresher impl. // StateRefresher impl.
func (s *LocalState) RefreshState() error { func (s *LocalState) RefreshState() error {
if s.stateFile == nil { var reader io.Reader
if err := s.createStateFiles(); err != nil { if !s.written {
// we haven't written a state file yet, so load from Path
f, err := os.Open(s.Path)
if err != nil {
return err return err
} }
defer f.Close()
reader = f
} else {
// we have a state file, make sure we're at the start
s.stateFileOut.Seek(0, os.SEEK_SET)
reader = s.stateFileOut
} }
// make sure we're at the start of the file state, err := terraform.ReadState(reader)
if _, err := s.stateFile.Seek(0, os.SEEK_SET); err != nil {
return err
}
state, err := terraform.ReadState(s.stateFile)
// if there's no state we just assign the nil return value // if there's no state we just assign the nil return value
if err != nil && err != terraform.ErrNoState { if err != nil && err != terraform.ErrNoState {
return err return err

View File

@ -17,7 +17,7 @@ func (s *LocalState) lock() error {
Len: 0, Len: 0,
} }
fd := s.stateFile.Fd() fd := s.stateFileOut.Fd()
return syscall.FcntlFlock(fd, syscall.F_SETLK, flock) return syscall.FcntlFlock(fd, syscall.F_SETLK, flock)
} }