2017-01-19 05:49:01 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2017-02-07 16:05:53 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"time"
|
2017-01-19 05:49:01 +01:00
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2017-02-07 16:05:53 +01:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2017-02-07 17:12:02 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-01-19 05:49:01 +01:00
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
)
|
|
|
|
|
2017-02-07 16:05:53 +01:00
|
|
|
const (
|
|
|
|
lockSuffix = "/.lock"
|
|
|
|
lockInfoSuffix = "/.lockinfo"
|
|
|
|
)
|
|
|
|
|
2017-01-19 05:49:01 +01:00
|
|
|
// RemoteClient is a remote client that stores data in Consul.
|
|
|
|
type RemoteClient struct {
|
|
|
|
Client *consulapi.Client
|
|
|
|
Path string
|
2017-02-07 16:05:53 +01:00
|
|
|
|
|
|
|
consulLock *consulapi.Lock
|
|
|
|
lockCh <-chan struct{}
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
|
|
|
pair, _, err := c.Client.KV().Get(c.Path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pair == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
md5 := md5.Sum(pair.Value)
|
|
|
|
return &remote.Payload{
|
|
|
|
Data: pair.Value,
|
|
|
|
MD5: md5[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Put(data []byte) error {
|
|
|
|
kv := c.Client.KV()
|
|
|
|
_, err := kv.Put(&consulapi.KVPair{
|
|
|
|
Key: c.Path,
|
|
|
|
Value: data,
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
|
|
|
kv := c.Client.KV()
|
|
|
|
_, err := kv.Delete(c.Path, nil)
|
|
|
|
return err
|
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2017-02-15 00:19:47 +01:00
|
|
|
func (c *RemoteClient) putLockInfo(info *state.LockInfo) error {
|
|
|
|
info.Path = c.Path
|
|
|
|
info.Created = time.Now().UTC()
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2017-02-15 00:19:47 +01:00
|
|
|
js, err := json.Marshal(info)
|
2017-02-07 16:05:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
kv := c.Client.KV()
|
|
|
|
_, err = kv.Put(&consulapi.KVPair{
|
|
|
|
Key: c.Path + lockInfoSuffix,
|
|
|
|
Value: js,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:12:02 +01:00
|
|
|
func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
|
2017-02-07 16:05:53 +01:00
|
|
|
path := c.Path + lockInfoSuffix
|
|
|
|
pair, _, err := c.Client.KV().Get(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pair == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:12:02 +01:00
|
|
|
li := &state.LockInfo{}
|
2017-02-07 16:05:53 +01:00
|
|
|
err = json.Unmarshal(pair.Value, li)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error unmarshaling lock info: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return li, nil
|
|
|
|
}
|
|
|
|
|
2017-02-15 00:19:47 +01:00
|
|
|
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
|
2017-02-07 16:05:53 +01:00
|
|
|
select {
|
|
|
|
case <-c.lockCh:
|
|
|
|
// We had a lock, but lost it.
|
|
|
|
// Since we typically only call lock once, we shouldn't ever see this.
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", errors.New("lost consul lock")
|
2017-02-07 16:05:53 +01:00
|
|
|
default:
|
|
|
|
if c.lockCh != nil {
|
|
|
|
// we have an active lock already
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", nil
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.consulLock == nil {
|
|
|
|
opts := &consulapi.LockOptions{
|
|
|
|
Key: c.Path + lockSuffix,
|
|
|
|
// We currently don't procide any options to block terraform and
|
|
|
|
// retry lock acquisition, but we can wait briefly in case the
|
|
|
|
// lock is about to be freed.
|
|
|
|
LockWaitTime: time.Second,
|
|
|
|
LockTryOnce: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := c.Client.LockOpts(opts)
|
|
|
|
if err != nil {
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", nil
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.consulLock = lock
|
|
|
|
}
|
|
|
|
|
|
|
|
lockCh, err := c.consulLock.Lock(make(chan struct{}))
|
|
|
|
if err != nil {
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", err
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if lockCh == nil {
|
|
|
|
lockInfo, e := c.getLockInfo()
|
|
|
|
if e != nil {
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", e
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", lockInfo.Err()
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c.lockCh = lockCh
|
|
|
|
|
|
|
|
err = c.putLockInfo(info)
|
|
|
|
if err != nil {
|
2017-02-15 00:19:47 +01:00
|
|
|
err = multierror.Append(err, c.Unlock(""))
|
|
|
|
return "", err
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 00:19:47 +01:00
|
|
|
return "", nil
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 00:19:47 +01:00
|
|
|
func (c *RemoteClient) Unlock(id string) error {
|
2017-02-07 16:05:53 +01:00
|
|
|
if c.consulLock == nil || c.lockCh == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.lockCh:
|
|
|
|
return errors.New("consul lock was lost")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.consulLock.Unlock()
|
|
|
|
c.lockCh = nil
|
|
|
|
|
|
|
|
kv := c.Client.KV()
|
|
|
|
_, delErr := kv.Delete(c.Path+lockInfoSuffix, nil)
|
|
|
|
if delErr != nil {
|
|
|
|
err = multierror.Append(err, delErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|