2015-02-21 20:52:55 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2017-01-30 21:42:35 +01:00
|
|
|
"bytes"
|
2017-01-10 00:15:48 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-01-30 21:33:21 +01:00
|
|
|
"io"
|
2017-01-10 00:15:48 +01:00
|
|
|
"io/ioutil"
|
2015-02-21 20:52:55 +01:00
|
|
|
"os"
|
2015-02-23 18:53:20 +01:00
|
|
|
"path/filepath"
|
2017-05-25 17:05:48 +02:00
|
|
|
"sync"
|
2017-01-10 00:15:48 +01:00
|
|
|
"time"
|
2015-02-21 20:52:55 +01:00
|
|
|
|
2017-02-15 20:01:18 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2015-02-21 20:52:55 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LocalState manages a state storage that is local to the filesystem.
|
|
|
|
type LocalState struct {
|
2017-05-25 17:05:48 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
|
2015-02-22 01:11:47 +01:00
|
|
|
// Path is the path to read the state from. PathOut is the path to
|
|
|
|
// write the state to. If PathOut is not specified, Path will be used.
|
|
|
|
// If PathOut already exists, it will be overwritten.
|
|
|
|
Path string
|
|
|
|
PathOut string
|
2015-02-21 20:52:55 +01:00
|
|
|
|
2017-01-30 21:33:21 +01:00
|
|
|
// the file handle corresponding to PathOut
|
2017-01-10 00:15:48 +01:00
|
|
|
stateFileOut *os.File
|
2017-02-14 17:51:20 +01:00
|
|
|
|
2017-02-15 16:53:04 +01:00
|
|
|
// While the stateFileOut will correspond to the lock directly,
|
|
|
|
// store and check the lock ID to maintain a strict state.Locker
|
|
|
|
// implementation.
|
|
|
|
lockID string
|
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
// created is set to true if stateFileOut didn't exist before we created it.
|
2017-02-04 00:58:18 +01:00
|
|
|
// This is mostly so we can clean up emtpy files during tests, but doesn't
|
|
|
|
// hurt to remove file we never wrote to.
|
|
|
|
created bool
|
2017-01-10 00:15:48 +01:00
|
|
|
|
2015-02-24 06:26:33 +01:00
|
|
|
state *terraform.State
|
|
|
|
readState *terraform.State
|
|
|
|
written bool
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
// SetState will force a specific state in-memory for this local state.
|
|
|
|
func (s *LocalState) SetState(state *terraform.State) {
|
2017-05-25 17:05:48 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2017-07-05 21:34:30 +02:00
|
|
|
s.state = state.DeepCopy()
|
|
|
|
s.readState = state.DeepCopy()
|
2017-01-10 00:15:48 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
// StateReader impl.
|
|
|
|
func (s *LocalState) State() *terraform.State {
|
|
|
|
return s.state.DeepCopy()
|
2017-01-10 00:15:48 +01:00
|
|
|
}
|
|
|
|
|
2015-02-21 20:52:55 +01:00
|
|
|
// WriteState for LocalState always persists the state as well.
|
2017-01-10 00:15:48 +01:00
|
|
|
// TODO: this should use a more robust method of writing state, by first
|
|
|
|
// writing to a temp file on the same filesystem, and renaming the file over
|
|
|
|
// the original.
|
2015-02-21 20:52:55 +01:00
|
|
|
//
|
|
|
|
// StateWriter impl.
|
|
|
|
func (s *LocalState) WriteState(state *terraform.State) error {
|
2017-05-25 17:05:48 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2017-01-10 00:15:48 +01:00
|
|
|
if s.stateFileOut == nil {
|
|
|
|
if err := s.createStateFiles(); err != nil {
|
2015-02-22 03:00:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 02:53:43 +01:00
|
|
|
defer s.stateFileOut.Sync()
|
2015-02-22 03:00:08 +01:00
|
|
|
|
2017-07-05 21:34:30 +02:00
|
|
|
s.state = state.DeepCopy() // don't want mutations before we actually get this written to disk
|
|
|
|
|
|
|
|
if s.readState != nil && s.state != nil {
|
|
|
|
// We don't trust callers to properly manage serials. Instead, we assume
|
|
|
|
// that a WriteState is always for the next version after what was
|
|
|
|
// most recently read.
|
|
|
|
s.state.Serial = s.readState.Serial
|
|
|
|
}
|
2017-01-10 00:15:48 +01:00
|
|
|
|
|
|
|
if _, err := s.stateFileOut.Seek(0, os.SEEK_SET); err != nil {
|
2015-02-23 18:53:20 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-01-10 00:15:48 +01:00
|
|
|
if err := s.stateFileOut.Truncate(0); err != nil {
|
2015-02-21 20:52:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-28 02:53:43 +01:00
|
|
|
if state == nil {
|
|
|
|
// if we have no state, don't write anything else.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-05 21:34:30 +02:00
|
|
|
if !s.state.MarshalEqual(s.readState) {
|
|
|
|
s.state.Serial++
|
|
|
|
}
|
2015-02-24 06:26:33 +01:00
|
|
|
|
2017-01-10 00:15:48 +01:00
|
|
|
if err := terraform.WriteState(s.state, s.stateFileOut); err != nil {
|
2015-02-22 01:11:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.written = true
|
|
|
|
return nil
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PersistState for LocalState is a no-op since WriteState always persists.
|
|
|
|
//
|
|
|
|
// StatePersister impl.
|
|
|
|
func (s *LocalState) PersistState() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateRefresher impl.
|
|
|
|
func (s *LocalState) RefreshState() error {
|
2017-05-25 17:05:48 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2018-03-19 22:19:50 +01:00
|
|
|
if s.PathOut == "" {
|
|
|
|
s.PathOut = s.Path
|
|
|
|
}
|
|
|
|
|
2017-01-30 21:33:21 +01:00
|
|
|
var reader io.Reader
|
2018-03-19 22:19:50 +01:00
|
|
|
|
|
|
|
// The s.Path file is only OK to read if we have not written any state out
|
|
|
|
// (in which case the same state needs to be read in), and no state output file
|
|
|
|
// has been opened (possibly via a lock) or the input path is different
|
|
|
|
// than the output path.
|
|
|
|
// This is important for Windows, as if the input file is the same as the
|
|
|
|
// output file, and the output file has been locked already, we can't open
|
|
|
|
// the file again.
|
|
|
|
if !s.written && (s.stateFileOut == nil || s.Path != s.PathOut) {
|
2017-01-30 21:33:21 +01:00
|
|
|
// we haven't written a state file yet, so load from Path
|
|
|
|
f, err := os.Open(s.Path)
|
|
|
|
if err != nil {
|
2017-01-30 21:42:35 +01:00
|
|
|
// It is okay if the file doesn't exist, we treat that as a nil state
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need a non-nil reader for ReadState and an empty buffer works
|
|
|
|
// to return EOF immediately
|
|
|
|
reader = bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
defer f.Close()
|
|
|
|
reader = f
|
2015-02-22 01:06:27 +01:00
|
|
|
}
|
2017-01-30 21:33:21 +01:00
|
|
|
} else {
|
2017-02-09 21:35:49 +01:00
|
|
|
// no state to refresh
|
|
|
|
if s.stateFileOut == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-30 21:33:21 +01:00
|
|
|
// we have a state file, make sure we're at the start
|
|
|
|
s.stateFileOut.Seek(0, os.SEEK_SET)
|
|
|
|
reader = s.stateFileOut
|
2017-01-10 00:15:48 +01:00
|
|
|
}
|
2015-02-22 01:06:27 +01:00
|
|
|
|
2017-01-30 21:33:21 +01:00
|
|
|
state, err := terraform.ReadState(reader)
|
2017-01-10 00:15:48 +01:00
|
|
|
// if there's no state we just assign the nil return value
|
|
|
|
if err != nil && err != terraform.ErrNoState {
|
|
|
|
return err
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.state = state
|
2017-07-05 21:34:30 +02:00
|
|
|
s.readState = s.state.DeepCopy()
|
2015-02-21 20:52:55 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-10 00:15:48 +01:00
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
// Lock implements a local filesystem state.Locker.
|
2017-02-15 00:51:32 +01:00
|
|
|
func (s *LocalState) Lock(info *LockInfo) (string, error) {
|
2017-05-25 17:05:48 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
if s.stateFileOut == nil {
|
|
|
|
if err := s.createStateFiles(); err != nil {
|
2017-02-15 00:51:32 +01:00
|
|
|
return "", err
|
2017-02-14 17:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 16:53:24 +01:00
|
|
|
if s.lockID != "" {
|
|
|
|
return "", fmt.Errorf("state %q already locked", s.stateFileOut.Name())
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:51:20 +01:00
|
|
|
if err := s.lock(); err != nil {
|
2017-02-15 20:01:18 +01:00
|
|
|
info, infoErr := s.lockInfo()
|
|
|
|
if infoErr != nil {
|
|
|
|
err = multierror.Append(err, infoErr)
|
2017-02-14 17:51:20 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 20:01:18 +01:00
|
|
|
lockErr := &LockError{
|
|
|
|
Info: info,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", lockErr
|
2017-02-14 17:51:20 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 16:53:04 +01:00
|
|
|
s.lockID = info.ID
|
|
|
|
return s.lockID, s.writeLockInfo(info)
|
2017-02-14 17:51:20 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 00:51:32 +01:00
|
|
|
func (s *LocalState) Unlock(id string) error {
|
2017-05-25 17:05:48 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2017-02-15 16:53:04 +01:00
|
|
|
if s.lockID == "" {
|
|
|
|
return fmt.Errorf("LocalState not locked")
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != s.lockID {
|
|
|
|
idErr := fmt.Errorf("invalid lock id: %q. current id: %q", id, s.lockID)
|
|
|
|
info, err := s.lockInfo()
|
2017-02-15 20:01:18 +01:00
|
|
|
if err != nil {
|
|
|
|
err = multierror.Append(idErr, err)
|
2017-02-15 16:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &LockError{
|
|
|
|
Err: idErr,
|
|
|
|
Info: info,
|
|
|
|
}
|
2017-02-14 17:51:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(s.lockInfoPath())
|
|
|
|
|
|
|
|
fileName := s.stateFileOut.Name()
|
|
|
|
|
|
|
|
unlockErr := s.unlock()
|
|
|
|
|
|
|
|
s.stateFileOut.Close()
|
|
|
|
s.stateFileOut = nil
|
2017-02-15 16:53:04 +01:00
|
|
|
s.lockID = ""
|
2017-02-14 17:51:20 +01:00
|
|
|
|
|
|
|
// clean up the state file if we created it an never wrote to it
|
|
|
|
stat, err := os.Stat(fileName)
|
|
|
|
if err == nil && stat.Size() == 0 && s.created {
|
|
|
|
os.Remove(fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return unlockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the state file, creating the directories and file as needed.
|
|
|
|
func (s *LocalState) createStateFiles() error {
|
|
|
|
if s.PathOut == "" {
|
|
|
|
s.PathOut = s.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// yes this could race, but we only use it to clean up empty files
|
|
|
|
if _, err := os.Stat(s.PathOut); os.IsNotExist(err) {
|
|
|
|
s.created = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the directories
|
|
|
|
if err := os.MkdirAll(filepath.Dir(s.PathOut), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(s.PathOut, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stateFileOut = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-10 00:15:48 +01:00
|
|
|
// return the path for the lockInfo metadata.
|
|
|
|
func (s *LocalState) lockInfoPath() string {
|
|
|
|
stateDir, stateName := filepath.Split(s.Path)
|
|
|
|
if stateName == "" {
|
|
|
|
panic("empty state file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
if stateName[0] == '.' {
|
|
|
|
stateName = stateName[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(stateDir, fmt.Sprintf(".%s.lock.info", stateName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// lockInfo returns the data in a lock info file
|
2017-02-07 17:12:02 +01:00
|
|
|
func (s *LocalState) lockInfo() (*LockInfo, error) {
|
2017-01-10 00:15:48 +01:00
|
|
|
path := s.lockInfoPath()
|
|
|
|
infoData, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:12:02 +01:00
|
|
|
info := LockInfo{}
|
2017-01-10 00:15:48 +01:00
|
|
|
err = json.Unmarshal(infoData, &info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("state file %q locked, but could not unmarshal lock info: %s", s.Path, err)
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write a new lock info file
|
2017-02-15 00:51:32 +01:00
|
|
|
func (s *LocalState) writeLockInfo(info *LockInfo) error {
|
2017-01-10 00:15:48 +01:00
|
|
|
path := s.lockInfoPath()
|
2017-02-15 00:51:32 +01:00
|
|
|
info.Path = s.Path
|
|
|
|
info.Created = time.Now().UTC()
|
2017-01-10 00:15:48 +01:00
|
|
|
|
2017-02-15 20:01:18 +01:00
|
|
|
err := ioutil.WriteFile(path, info.Marshal(), 0600)
|
2017-01-10 00:15:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write lock info for %q: %s", s.Path, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|