2015-02-21 20:52:55 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2017-03-31 23:27:39 +02:00
|
|
|
"context"
|
2017-02-14 23:43:42 +01:00
|
|
|
"fmt"
|
2017-02-15 16:25:04 +01:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2017-02-14 23:43:42 +01:00
|
|
|
"time"
|
|
|
|
|
2017-02-15 16:25:04 +01: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 23:24:45 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-10-20 03:48:08 +02:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2015-02-21 20:52:55 +01:00
|
|
|
)
|
|
|
|
|
2017-02-15 16:25:04 +01:00
|
|
|
var rngSource *rand.Rand
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rngSource = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// State is a deprecated alias for statemgr.Full
|
|
|
|
type State = statemgr.Full
|
2015-02-22 01:04:32 +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
|
|
|
// StateReader is a deprecated alias for statemgr.Reader
|
|
|
|
type StateReader = statemgr.Reader
|
2015-02-21 20:52:55 +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
|
|
|
// StateWriter is a deprecated alias for statemgr.Writer
|
|
|
|
type StateWriter = statemgr.Writer
|
2015-02-21 20:52:55 +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
|
|
|
// StateRefresher is a deprecated alias for statemgr.Refresher
|
|
|
|
type StateRefresher = statemgr.Refresher
|
2015-02-21 20:52:55 +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
|
|
|
// StatePersister is a deprecated alias for statemgr.Persister
|
|
|
|
type StatePersister = statemgr.Persister
|
2017-01-10 00:00:05 +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
|
|
|
// Locker is a deprecated alias for statemgr.Locker
|
|
|
|
type Locker = statemgr.Locker
|
2017-02-14 23:43:42 +01:00
|
|
|
|
2017-04-03 20:26:05 +02:00
|
|
|
// test hook to verify that LockWithContext has attempted a lock
|
|
|
|
var postLockHook func()
|
|
|
|
|
2017-04-01 20:58:19 +02:00
|
|
|
// Lock the state, using the provided context for timeout and cancellation.
|
2017-04-01 23:56:03 +02:00
|
|
|
// This backs off slightly to an upper limit.
|
2017-04-01 20:58:19 +02:00
|
|
|
func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
|
2017-04-01 23:56:03 +02:00
|
|
|
delay := time.Second
|
|
|
|
maxDelay := 16 * time.Second
|
2017-03-31 23:27:39 +02: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 15:06:47 +02: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 23:27:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 20:26:05 +02:00
|
|
|
if postLockHook != nil {
|
|
|
|
postLockHook()
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:27:39 +02: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 23:56:03 +02:00
|
|
|
case <-time.After(delay):
|
|
|
|
if delay < maxDelay {
|
|
|
|
delay *= 2
|
|
|
|
}
|
2017-03-31 23:27:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:25:04 +01:00
|
|
|
// Generate a LockInfo structure, populating the required fields.
|
|
|
|
func NewLockInfo() *LockInfo {
|
|
|
|
// this doesn't need to be cryptographically secure, just unique.
|
|
|
|
// Using math/rand alleviates the need to check handle the read error.
|
|
|
|
// Use a uuid format to match other IDs used throughout Terraform.
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
rngSource.Read(buf)
|
|
|
|
|
|
|
|
id, err := uuid.FormatUUID(buf)
|
|
|
|
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 20:01:18 +01:00
|
|
|
userName := ""
|
|
|
|
if userInfo, err := user.Current(); err == nil {
|
|
|
|
userName = userInfo.Username
|
|
|
|
}
|
2017-02-15 16:25:04 +01:00
|
|
|
host, _ := os.Hostname()
|
|
|
|
|
|
|
|
info := &LockInfo{
|
|
|
|
ID: id,
|
2017-02-15 20:01:18 +01:00
|
|
|
Who: fmt.Sprintf("%s@%s", userName, host),
|
2017-10-20 03:48:08 +02:00
|
|
|
Version: version.Version,
|
2017-02-15 16:25:04 +01: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 23:24:45 +02:00
|
|
|
// LockInfo is a deprecated lias for statemgr.LockInfo
|
|
|
|
type LockInfo = statemgr.LockInfo
|
2017-02-14 23:43:42 +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
|
|
|
// LockError is a deprecated alias for statemgr.LockError
|
|
|
|
type LockError = statemgr.LockError
|