2017-03-02 07:15:08 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2017-03-02 07:58:51 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-03-02 07:15:08 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-03-02 07:58:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyEnvPrefix = "-env:"
|
2017-03-02 07:15:08 +01:00
|
|
|
)
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) Workspaces() ([]string, error) {
|
2017-03-02 07:58:51 +01:00
|
|
|
// List our raw path
|
|
|
|
prefix := b.configData.Get("path").(string) + keyEnvPrefix
|
2017-10-08 17:26:05 +02:00
|
|
|
keys, _, err := b.client.KV().Keys(prefix, "/", nil)
|
2017-03-02 07:58:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the envs, we use a map since we can get duplicates with
|
|
|
|
// path suffixes.
|
|
|
|
envs := map[string]struct{}{}
|
|
|
|
for _, key := range keys {
|
|
|
|
// Consul should ensure this but it doesn't hurt to check again
|
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
key = strings.TrimPrefix(key, prefix)
|
|
|
|
|
|
|
|
// Ignore anything with a "/" in it since we store the state
|
|
|
|
// directly in a key not a directory.
|
|
|
|
if idx := strings.IndexRune(key, '/'); idx >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
envs[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]string, 1, len(envs)+1)
|
|
|
|
result[0] = backend.DefaultStateName
|
|
|
|
for k, _ := range envs {
|
|
|
|
result = append(result, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2017-03-02 07:15:08 +01:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) DeleteWorkspace(name string) error {
|
2017-03-22 20:52:55 +01:00
|
|
|
if name == backend.DefaultStateName || name == "" {
|
2017-03-02 07:58:51 +01:00
|
|
|
return fmt.Errorf("can't delete default state")
|
|
|
|
}
|
2017-03-02 07:15:08 +01:00
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
// Determine the path of the data
|
|
|
|
path := b.path(name)
|
|
|
|
|
2017-03-02 08:01:28 +01:00
|
|
|
// Delete it. We just delete it without any locking since
|
|
|
|
// the DeleteState API is documented as such.
|
2017-10-08 17:26:05 +02:00
|
|
|
_, err := b.client.KV().Delete(path, nil)
|
2017-03-02 07:58:51 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
|
2017-03-02 07:15:08 +01:00
|
|
|
// Determine the path of the data
|
2017-03-02 07:58:51 +01:00
|
|
|
path := b.path(name)
|
2017-03-02 07:15:08 +01:00
|
|
|
|
2017-03-13 08:17:33 +01:00
|
|
|
// Determine whether to gzip or not
|
|
|
|
gzip := b.configData.Get("gzip").(bool)
|
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
// Build the state client
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
var stateMgr = &remote.State{
|
2017-03-02 07:15:08 +01:00
|
|
|
Client: &RemoteClient{
|
2017-10-08 17:26:05 +02:00
|
|
|
Client: b.client,
|
2017-05-28 22:07:24 +02:00
|
|
|
Path: path,
|
|
|
|
GZip: gzip,
|
|
|
|
lockState: b.lock,
|
2017-03-02 07:15:08 +01:00
|
|
|
},
|
2017-03-02 07:58:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:28:42 +01:00
|
|
|
if !b.lock {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
stateMgr.DisableLocks()
|
2017-03-15 01:28:42 +01:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:57:11 +02:00
|
|
|
// the default state always exists
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return stateMgr, nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
// Grab a lock, we use this to write an empty state if one doesn't
|
|
|
|
// exist already. We have to write an empty state as a sentinel value
|
|
|
|
// so States() knows it exists.
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
2017-03-31 23:04:56 +02:00
|
|
|
lockId, err := stateMgr.Lock(lockInfo)
|
2017-03-02 07:58:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to lock state in Consul: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
lockUnlock := func(parent error) error {
|
2017-03-31 23:04:56 +02:00
|
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
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 {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
if err := stateMgr.WriteState(states.NewState()); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateMgr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) path(name string) string {
|
|
|
|
path := b.configData.Get("path").(string)
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
path += fmt.Sprintf("%s%s", keyEnvPrefix, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
2017-03-02 07:15:08 +01:00
|
|
|
}
|
2017-03-02 07:58:51 +01:00
|
|
|
|
|
|
|
const errStateUnlock = `
|
|
|
|
Error unlocking Consul state. Lock ID: %s
|
|
|
|
|
|
|
|
Error: %s
|
|
|
|
|
|
|
|
You may have to force-unlock this state in order to use it again.
|
|
|
|
The Consul backend acquires a lock during initialization to ensure
|
|
|
|
the minimum required key/values are prepared.
|
|
|
|
`
|