2015-02-21 13:52:55 -06:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2017-03-31 16:27:39 -05:00
|
|
|
"context"
|
2017-02-14 16:43:42 -06:00
|
|
|
"fmt"
|
2017-02-15 09:25:04 -06:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2017-02-14 16:43:42 -06:00
|
|
|
"time"
|
|
|
|
|
2017-02-15 09:25:04 -06:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
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 16:24:45 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-10-19 20:48:08 -05:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2015-02-21 13:52:55 -06: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 16:24:45 -05:00
|
|
|
// State is a deprecated alias for statemgr.Full
|
|
|
|
type State = statemgr.Full
|
2015-02-21 18:04:32 -06: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 16:24:45 -05:00
|
|
|
// StateReader is a deprecated alias for statemgr.Reader
|
|
|
|
type StateReader = statemgr.Reader
|
2015-02-21 13:52:55 -06: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 16:24:45 -05:00
|
|
|
// StateWriter is a deprecated alias for statemgr.Writer
|
|
|
|
type StateWriter = statemgr.Writer
|
2015-02-21 13:52:55 -06: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 16:24:45 -05:00
|
|
|
// StateRefresher is a deprecated alias for statemgr.Refresher
|
|
|
|
type StateRefresher = statemgr.Refresher
|
2015-02-21 13:52:55 -06: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 16:24:45 -05:00
|
|
|
// StatePersister is a deprecated alias for statemgr.Persister
|
|
|
|
type StatePersister = statemgr.Persister
|
2017-01-09 17:00:05 -06: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 16:24:45 -05:00
|
|
|
// Locker is a deprecated alias for statemgr.Locker
|
|
|
|
type Locker = statemgr.Locker
|
2017-02-14 16:43:42 -06:00
|
|
|
|
2017-04-03 13:26:05 -05:00
|
|
|
// test hook to verify that LockWithContext has attempted a lock
|
|
|
|
var postLockHook func()
|
|
|
|
|
2017-04-01 13:58:19 -05:00
|
|
|
// Lock the state, using the provided context for timeout and cancellation.
|
2017-04-01 16:56:03 -05:00
|
|
|
// This backs off slightly to an upper limit.
|
2017-04-01 13:58:19 -05:00
|
|
|
func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
|
2017-04-01 16:56:03 -05:00
|
|
|
delay := time.Second
|
|
|
|
maxDelay := 16 * time.Second
|
2017-03-31 16:27:39 -05:00
|
|
|
for {
|
|
|
|
id, err := s.Lock(info)
|
|
|
|
if err == nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
le, ok := err.(*LockError)
|
|
|
|
if !ok {
|
|
|
|
// not a lock error, so we can't retry
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:06:47 -05:00
|
|
|
if le == nil || le.Info == nil || le.Info.ID == "" {
|
|
|
|
// If we dont' have a complete LockError, there's something wrong with the lock
|
|
|
|
return "", err
|
2017-03-31 16:27:39 -05:00
|
|
|
}
|
|
|
|
|
2017-04-03 13:26:05 -05:00
|
|
|
if postLockHook != nil {
|
|
|
|
postLockHook()
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:27:39 -05:00
|
|
|
// there's an existing lock, wait and try again
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// return the last lock error with the info
|
|
|
|
return "", err
|
2017-04-01 16:56:03 -05:00
|
|
|
case <-time.After(delay):
|
|
|
|
if delay < maxDelay {
|
|
|
|
delay *= 2
|
|
|
|
}
|
2017-03-31 16:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 09:25:04 -06:00
|
|
|
// Generate a LockInfo structure, populating the required fields.
|
|
|
|
func NewLockInfo() *LockInfo {
|
2019-02-21 19:45:41 -06:00
|
|
|
id, err := uuid.GenerateUUID()
|
2017-02-15 09:25:04 -06:00
|
|
|
if err != nil {
|
|
|
|
// this of course shouldn't happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't error out on user and hostname, as we don't require them
|
2017-02-15 13:01:18 -06:00
|
|
|
userName := ""
|
|
|
|
if userInfo, err := user.Current(); err == nil {
|
|
|
|
userName = userInfo.Username
|
|
|
|
}
|
2017-02-15 09:25:04 -06:00
|
|
|
host, _ := os.Hostname()
|
|
|
|
|
|
|
|
info := &LockInfo{
|
|
|
|
ID: id,
|
2017-02-15 13:01:18 -06:00
|
|
|
Who: fmt.Sprintf("%s@%s", userName, host),
|
2017-10-19 20:48:08 -05:00
|
|
|
Version: version.Version,
|
2017-02-15 09:25:04 -06:00
|
|
|
Created: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
// LockInfo is a deprecated lias for statemgr.LockInfo
|
|
|
|
type LockInfo = statemgr.LockInfo
|
2017-02-14 16:43:42 -06: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 16:24:45 -05:00
|
|
|
// LockError is a deprecated alias for statemgr.LockError
|
|
|
|
type LockError = statemgr.LockError
|