2018-10-10 23:42:57 +02:00
|
|
|
package pg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2018-10-10 23:42:57 +02:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteClient is a remote client that stores data in a Postgres database
|
|
|
|
type RemoteClient struct {
|
|
|
|
Client *sql.DB
|
|
|
|
Name string
|
|
|
|
SchemaName string
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
info *statemgr.LockInfo
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Get() (*remote.Payload, error) {
|
|
|
|
query := `SELECT data FROM %s.%s WHERE name = $1`
|
2019-03-05 02:01:08 +01:00
|
|
|
row := c.Client.QueryRow(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
2018-10-10 23:42:57 +02:00
|
|
|
var data []byte
|
|
|
|
err := row.Scan(&data)
|
2019-02-26 01:05:53 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
// No existing state returns empty.
|
2018-10-10 23:42:57 +02:00
|
|
|
return nil, nil
|
2019-02-26 01:05:53 +01:00
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
default:
|
|
|
|
md5 := md5.Sum(data)
|
|
|
|
return &remote.Payload{
|
|
|
|
Data: data,
|
|
|
|
MD5: md5[:],
|
|
|
|
}, nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Put(data []byte) error {
|
|
|
|
query := `INSERT INTO %s.%s (name, data) VALUES ($1, $2)
|
|
|
|
ON CONFLICT (name) DO UPDATE
|
|
|
|
SET data = $2 WHERE %s.name = $1`
|
2019-03-05 02:01:08 +01:00
|
|
|
_, err := c.Client.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName, statesTableName), c.Name, data)
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Delete() error {
|
|
|
|
query := `DELETE FROM %s.%s WHERE name = $1`
|
2019-03-05 02:01:08 +01:00
|
|
|
_, err := c.Client.Exec(fmt.Sprintf(query, c.SchemaName, statesTableName), c.Name)
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
|
2019-02-26 01:05:53 +01:00
|
|
|
var err error
|
|
|
|
var lockID string
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
if info.ID == "" {
|
2019-02-26 01:05:53 +01:00
|
|
|
lockID, err = uuid.GenerateUUID()
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
info.ID = lockID
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:56 +01:00
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
//
|
|
|
|
lockUnlock := func(pgLockId string) error {
|
|
|
|
query := `SELECT pg_advisory_unlock(%s)`
|
|
|
|
row := c.Client.QueryRow(fmt.Sprintf(query, pgLockId))
|
|
|
|
var didUnlock []byte
|
|
|
|
err := row.Scan(&didUnlock)
|
|
|
|
if err != nil {
|
2020-08-11 17:43:01 +02:00
|
|
|
return &statemgr.LockError{Info: info, Err: err}
|
2019-03-05 20:19:56 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to acquire locks for the existing row `id` and the creation lock `-1`.
|
|
|
|
query := `SELECT %s.id, pg_try_advisory_lock(%s.id), pg_try_advisory_lock(-1) FROM %s.%s WHERE %s.name = $1`
|
2019-03-05 02:01:08 +01:00
|
|
|
row := c.Client.QueryRow(fmt.Sprintf(query, statesTableName, statesTableName, c.SchemaName, statesTableName, statesTableName), c.Name)
|
2019-03-05 20:19:56 +01:00
|
|
|
var pgLockId, didLock, didLockForCreate []byte
|
|
|
|
err = row.Scan(&pgLockId, &didLock, &didLockForCreate)
|
2019-02-26 01:05:53 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2019-03-05 20:19:56 +01:00
|
|
|
// No rows means we're creating the workspace. Take the creation lock.
|
2019-03-05 02:01:08 +01:00
|
|
|
innerRow := c.Client.QueryRow(`SELECT pg_try_advisory_lock(-1)`)
|
2019-02-26 01:05:53 +01:00
|
|
|
var innerDidLock []byte
|
|
|
|
err := innerRow.Scan(&innerDidLock)
|
|
|
|
if err != nil {
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: info, Err: err}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-02-26 01:05:53 +01:00
|
|
|
if string(innerDidLock) == "false" {
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: info, Err: fmt.Errorf("Already locked for workspace creation: %s", c.Name)}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-03-05 02:01:08 +01:00
|
|
|
info.Path = "-1"
|
2019-02-26 01:05:53 +01:00
|
|
|
case err != nil:
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: info, Err: err}
|
2019-02-26 01:05:53 +01:00
|
|
|
case string(didLock) == "false":
|
2019-03-05 20:19:56 +01:00
|
|
|
// Existing workspace is already locked. Release the attempted creation lock.
|
|
|
|
lockUnlock("-1")
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: info, Err: fmt.Errorf("Workspace is already locked: %s", c.Name)}
|
2019-03-05 20:19:56 +01:00
|
|
|
case string(didLockForCreate) == "false":
|
|
|
|
// Someone has the creation lock already. Release the existing workspace because it might not be safe to touch.
|
|
|
|
lockUnlock(string(pgLockId))
|
2020-08-11 17:43:01 +02:00
|
|
|
return "", &statemgr.LockError{Info: info, Err: fmt.Errorf("Cannot lock workspace; already locked for workspace creation: %s", c.Name)}
|
2019-02-26 01:05:53 +01:00
|
|
|
default:
|
2019-03-05 20:19:56 +01:00
|
|
|
// Existing workspace is now locked. Release the attempted creation lock.
|
|
|
|
lockUnlock("-1")
|
2019-03-05 02:01:08 +01:00
|
|
|
info.Path = string(pgLockId)
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2019-03-05 02:01:08 +01:00
|
|
|
c.info = info
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *RemoteClient) getLockInfo() (*statemgr.LockInfo, error) {
|
2019-02-26 01:05:53 +01:00
|
|
|
return c.info, nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemoteClient) Unlock(id string) error {
|
2019-03-05 02:01:08 +01:00
|
|
|
if c.info != nil && c.info.Path != "" {
|
|
|
|
query := `SELECT pg_advisory_unlock(%s)`
|
|
|
|
row := c.Client.QueryRow(fmt.Sprintf(query, c.info.Path))
|
|
|
|
var didUnlock []byte
|
|
|
|
err := row.Scan(&didUnlock)
|
2019-02-26 01:05:53 +01:00
|
|
|
if err != nil {
|
2020-08-11 17:43:01 +02:00
|
|
|
return &statemgr.LockError{Info: c.info, Err: err}
|
2019-02-26 01:05:53 +01:00
|
|
|
}
|
2019-03-05 02:01:08 +01:00
|
|
|
c.info = nil
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|