2017-08-03 19:10:50 +02:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2021-01-27 23:04:52 +01:00
|
|
|
etcdv3 "go.etcd.io/etcd/clientv3"
|
|
|
|
etcdv3sync "go.etcd.io/etcd/clientv3/concurrency"
|
2017-08-03 19:10:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lockAcquireTimeout = 2 * time.Second
|
|
|
|
lockInfoSuffix = ".lockinfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteClient is a remote client that will store data in etcd.
|
|
|
|
type RemoteClient struct {
|
|
|
|
Client *etcdv3.Client
|
|
|
|
DoLock bool
|
|
|
|
Key string
|
|
|
|
|
|
|
|
etcdMutex *etcdv3sync.Mutex
|
|
|
|
etcdSession *etcdv3sync.Session
|
2020-08-11 17:43:01 +02:00
|
|
|
info *statemgr.LockInfo
|
2017-08-03 19:10:50 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
modRevision int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
res, err := c.Client.KV.Get(context.TODO(), c.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.Count == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if res.Count >= 2 {
|
|
|
|
return nil, fmt.Errorf("Expected a single result but got %d.", res.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.modRevision = res.Kvs[0].ModRevision
|
|
|
|
|
|
|
|
payload := res.Kvs[0].Value
|
|
|
|
md5 := md5.Sum(payload)
|
|
|
|
|
|
|
|
return &remote.Payload{
|
|
|
|
Data: payload,
|
|
|
|
MD5: md5[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Put(data []byte) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
res, err := etcdv3.NewKV(c.Client).Txn(context.TODO()).If(
|
|
|
|
etcdv3.Compare(etcdv3.ModRevision(c.Key), "=", c.modRevision),
|
|
|
|
).Then(
|
|
|
|
etcdv3.OpPut(c.Key, string(data)),
|
|
|
|
etcdv3.OpGet(c.Key),
|
|
|
|
).Commit()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !res.Succeeded {
|
|
|
|
return fmt.Errorf("The transaction did not succeed.")
|
|
|
|
}
|
|
|
|
if len(res.Responses) != 2 {
|
|
|
|
return fmt.Errorf("Expected two responses but got %d.", len(res.Responses))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.modRevision = res.Responses[1].GetResponseRange().Kvs[0].ModRevision
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
_, err := c.Client.KV.Delete(context.TODO(), c.Key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
|
2017-08-03 19:10:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if !c.DoLock {
|
|
|
|
return "", nil
|
|
|
|
}
|
2017-09-08 17:22:14 +02:00
|
|
|
if c.etcdSession != nil {
|
|
|
|
return "", fmt.Errorf("state %q already locked", c.Key)
|
|
|
|
}
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
c.info = info
|
|
|
|
return c.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Unlock(id string) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if !c.DoLock {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.unlock(id)
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) deleteLockInfo(info *statemgr.LockInfo) error {
|
2017-09-08 16:21:06 +02:00
|
|
|
res, err := c.Client.KV.Delete(context.TODO(), c.Key+lockInfoSuffix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.Deleted == 0 {
|
|
|
|
return fmt.Errorf("No keys deleted for %s when deleting lock info.", c.Key+lockInfoSuffix)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) getLockInfo() (*statemgr.LockInfo, error) {
|
2017-08-03 19:10:50 +02:00
|
|
|
res, err := c.Client.KV.Get(context.TODO(), c.Key+lockInfoSuffix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.Count == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
li := &statemgr.LockInfo{}
|
2017-08-03 19:10:50 +02:00
|
|
|
err = json.Unmarshal(res.Kvs[0].Value, li)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error unmarshaling lock info: %s.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return li, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) putLockInfo(info *statemgr.LockInfo) error {
|
2017-09-08 18:46:39 +02:00
|
|
|
c.info.Path = c.etcdMutex.Key()
|
2017-08-03 19:10:50 +02:00
|
|
|
c.info.Created = time.Now().UTC()
|
|
|
|
|
|
|
|
_, err := c.Client.KV.Put(context.TODO(), c.Key+lockInfoSuffix, string(c.info.Marshal()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) lock() (string, error) {
|
|
|
|
session, err := etcdv3sync.NewSession(c.Client)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), lockAcquireTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-09-08 17:59:39 +02:00
|
|
|
mutex := etcdv3sync.NewMutex(session, c.Key)
|
2017-08-03 19:10:50 +02:00
|
|
|
if err1 := mutex.Lock(ctx); err1 != nil {
|
|
|
|
lockInfo, err2 := c.getLockInfo()
|
|
|
|
if err2 != nil {
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Err: err2}
|
2017-08-03 19:10:50 +02:00
|
|
|
}
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: lockInfo, Err: err1}
|
2017-08-03 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.etcdMutex = mutex
|
|
|
|
c.etcdSession = session
|
|
|
|
|
|
|
|
err = c.putLockInfo(c.info)
|
|
|
|
if err != nil {
|
|
|
|
if unlockErr := c.unlock(c.info.ID); unlockErr != nil {
|
|
|
|
err = multierror.Append(err, unlockErr)
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.info.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) unlock(id string) error {
|
|
|
|
if c.etcdMutex == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs error
|
|
|
|
|
2017-09-08 16:21:06 +02:00
|
|
|
if err := c.deleteLockInfo(c.info); err != nil {
|
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
2017-08-03 19:10:50 +02:00
|
|
|
if err := c.etcdMutex.Unlock(context.TODO()); err != nil {
|
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
|
|
|
if err := c.etcdSession.Close(); err != nil {
|
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.etcdMutex = nil
|
|
|
|
c.etcdSession = nil
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|