2017-03-21 18:43:31 +01:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2017-04-12 19:30:49 +02:00
|
|
|
"errors"
|
2017-03-22 20:52:55 +01:00
|
|
|
"fmt"
|
2017-03-22 21:33:41 +01:00
|
|
|
"sort"
|
2017-03-22 20:52:55 +01:00
|
|
|
"strings"
|
|
|
|
|
2017-03-22 21:33:41 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2017-03-21 18:43:31 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
2017-03-22 21:45:21 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-03-21 18:43:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-22 21:33:41 +01:00
|
|
|
// This will be used as directory name, the odd looking colon is simply to
|
|
|
|
// reduce the chance of name conflicts with existing objects.
|
2017-03-22 20:52:55 +01:00
|
|
|
keyEnvPrefix = "env:"
|
2017-03-21 18:43:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Backend) States() ([]string, error) {
|
2017-03-22 21:33:41 +01:00
|
|
|
params := &s3.ListObjectsInput{
|
|
|
|
Bucket: &b.bucketName,
|
|
|
|
Prefix: aws.String(keyEnvPrefix + "/"),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.s3Client.ListObjects(params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:30:49 +02:00
|
|
|
envs := []string{backend.DefaultStateName}
|
2017-03-22 21:33:41 +01:00
|
|
|
for _, obj := range resp.Contents {
|
2017-04-12 19:30:49 +02:00
|
|
|
env := b.keyEnv(*obj.Key)
|
2017-03-22 21:33:41 +01:00
|
|
|
if env != "" {
|
|
|
|
envs = append(envs, env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:30:49 +02:00
|
|
|
sort.Strings(envs[1:])
|
2017-03-22 21:33:41 +01:00
|
|
|
return envs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the env name from the S3 key
|
2017-04-12 19:30:49 +02:00
|
|
|
func (b *Backend) keyEnv(key string) string {
|
|
|
|
// we have 3 parts, the prefix, the env name, and the key name
|
|
|
|
parts := strings.SplitN(key, "/", 3)
|
2017-03-22 21:33:41 +01:00
|
|
|
if len(parts) < 3 {
|
|
|
|
// no env here
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:30:49 +02:00
|
|
|
// shouldn't happen since we listed by prefix
|
2017-03-22 21:33:41 +01:00
|
|
|
if parts[0] != keyEnvPrefix {
|
2017-04-12 19:30:49 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// not our key, so don't include it in our listing
|
|
|
|
if parts[2] != b.keyName {
|
2017-03-22 21:33:41 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[1]
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) DeleteState(name string) error {
|
2017-03-22 20:52:55 +01:00
|
|
|
if name == backend.DefaultStateName || name == "" {
|
|
|
|
return fmt.Errorf("can't delete default state")
|
|
|
|
}
|
|
|
|
|
2017-03-22 21:33:41 +01:00
|
|
|
params := &s3.DeleteObjectInput{
|
|
|
|
Bucket: &b.bucketName,
|
|
|
|
Key: aws.String(b.path(name)),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.s3Client.DeleteObject(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:52:55 +01:00
|
|
|
return nil
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) State(name string) (state.State, error) {
|
2017-04-12 19:30:49 +02:00
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("missing state name")
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:52:55 +01:00
|
|
|
client := &RemoteClient{
|
|
|
|
s3Client: b.s3Client,
|
|
|
|
dynClient: b.dynClient,
|
|
|
|
bucketName: b.bucketName,
|
|
|
|
path: b.path(name),
|
|
|
|
serverSideEncryption: b.serverSideEncryption,
|
|
|
|
acl: b.acl,
|
|
|
|
kmsKeyID: b.kmsKeyID,
|
|
|
|
lockTable: b.lockTable,
|
|
|
|
}
|
|
|
|
|
2017-03-22 21:45:21 +01:00
|
|
|
stateMgr := &remote.State{Client: client}
|
|
|
|
|
|
|
|
//if this isn't the default state name, we need to create the object so
|
|
|
|
//it's listed by States.
|
2017-03-22 21:33:41 +01:00
|
|
|
if name != backend.DefaultStateName {
|
2017-03-22 21:45:21 +01:00
|
|
|
// take a lock on this state while we write it
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockId, err := client.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to lock s3 state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
lockUnlock := func(parent error) error {
|
|
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
|
|
|
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
|
|
|
|
}
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the value
|
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no state, we have to create an empty state
|
|
|
|
if v := stateMgr.State(); v == nil {
|
|
|
|
if err := stateMgr.WriteState(terraform.NewState()); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := stateMgr.PersistState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock, the state should now be initialized
|
|
|
|
if err := lockUnlock(nil); err != nil {
|
2017-03-22 21:33:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-22 21:45:21 +01:00
|
|
|
|
2017-03-22 21:33:41 +01:00
|
|
|
}
|
2017-03-22 20:52:55 +01:00
|
|
|
|
2017-03-22 21:45:21 +01:00
|
|
|
return stateMgr, nil
|
2017-03-22 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) client() *RemoteClient {
|
|
|
|
return &RemoteClient{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) path(name string) string {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return b.keyName
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join([]string{keyEnvPrefix, name, b.keyName}, "/")
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
2017-03-22 21:45:21 +01:00
|
|
|
|
|
|
|
const errStateUnlock = `
|
|
|
|
Error unlocking S3 state. Lock ID: %s
|
|
|
|
|
|
|
|
Error: %s
|
|
|
|
|
|
|
|
You may have to force-unlock this state in order to use it again.
|
|
|
|
`
|