2017-01-19 05:47:56 +01:00
package local
import (
"context"
2017-02-22 01:07:27 +01:00
"errors"
2017-01-19 05:47:56 +01:00
"fmt"
2017-02-22 01:07:27 +01:00
"io/ioutil"
2018-02-12 17:52:21 +01:00
"log"
2017-02-22 01:07:27 +01:00
"os"
"path/filepath"
"sort"
2017-01-19 05:47:56 +01:00
"sync"
2021-05-17 17:42:17 +02:00
"github.com/hashicorp/terraform/internal/backend"
2021-05-17 21:07:38 +02:00
"github.com/hashicorp/terraform/internal/command/views"
2021-05-17 21:17:09 +02:00
"github.com/hashicorp/terraform/internal/configs/configschema"
2021-10-27 22:33:35 +02:00
"github.com/hashicorp/terraform/internal/logging"
2021-05-17 21:43:35 +02:00
"github.com/hashicorp/terraform/internal/states/statemgr"
2021-05-17 21:46:19 +02:00
"github.com/hashicorp/terraform/internal/terraform"
2021-05-17 19:11:06 +02:00
"github.com/hashicorp/terraform/internal/tfdiags"
2018-03-21 02:43:02 +01:00
"github.com/zclconf/go-cty/cty"
2017-01-19 05:47:56 +01:00
)
2017-02-22 01:07:27 +01:00
const (
2017-05-31 02:13:43 +02:00
DefaultWorkspaceDir = "terraform.tfstate.d"
DefaultWorkspaceFile = "environment"
2017-02-22 01:07:27 +01:00
DefaultStateFilename = "terraform.tfstate"
DefaultBackupExtension = ".backup"
)
2017-01-19 05:47:56 +01:00
// Local is an implementation of EnhancedBackend that performs all operations
// locally. This is the "default" backend and implements normal Terraform
// behavior as it is well known.
type Local struct {
2017-05-31 02:13:43 +02:00
// The State* paths are set from the backend config, and may be left blank
// to use the defaults. If the actual paths for the local backend state are
2017-02-22 01:07:27 +01:00
// needed, use the StatePaths method.
//
2017-01-19 05:47:56 +01:00
// StatePath is the local path where state is read from.
//
// StateOutPath is the local path where the state will be written.
// If this is empty, it will default to StatePath.
//
// StateBackupPath is the local path where a backup file will be written.
2017-02-22 01:07:27 +01:00
// Set this to "-" to disable state backup.
2017-03-01 20:34:45 +01:00
//
2017-05-31 02:13:43 +02:00
// StateWorkspaceDir is the path to the folder containing data for
// non-default workspaces. This defaults to DefaultWorkspaceDir if not set.
StatePath string
StateOutPath string
StateBackupPath string
StateWorkspaceDir string
2017-01-19 05:47:56 +01:00
2018-11-15 01:03:14 +01:00
// The OverrideState* paths are set based on per-operation CLI arguments
// and will override what'd be built from the State* fields if non-empty.
// While the interpretation of the State* fields depends on the active
// workspace, the OverrideState* fields are always used literally.
OverrideStatePath string
OverrideStateOutPath string
OverrideStateBackupPath string
2017-02-27 22:43:31 +01:00
// We only want to create a single instance of a local state, so store them
// here as they're loaded.
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
states map [ string ] statemgr . Full
2017-02-27 22:43:31 +01:00
2017-01-19 05:47:56 +01:00
// Terraform context. Many of these will be overridden or merged by
// Operation. See Operation for more details.
ContextOpts * terraform . ContextOpts
// OpInput will ask for necessary input prior to performing any operations.
//
// OpValidation will perform validation prior to running an operation. The
// variable naming doesn't match the style of others since we have a func
// Validate.
OpInput bool
OpValidation bool
// Backend, if non-nil, will use this backend for non-enhanced behavior.
// This allows local behavior with remote state storage. It is a way to
// "upgrade" a non-enhanced backend to an enhanced backend with typical
// behavior.
//
// If this is nil, local performs normal state loading and storage.
Backend backend . Backend
2018-10-31 16:45:03 +01:00
// opLock locks operations
2017-01-19 05:47:56 +01:00
opLock sync . Mutex
}
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 _ backend . Backend = ( * Local ) ( nil )
2018-10-31 16:45:03 +01:00
// New returns a new initialized local backend.
func New ( ) * Local {
return NewWithBackend ( nil )
}
// NewWithBackend returns a new local backend initialized with a
// dedicated backend for non-enhanced behavior.
func NewWithBackend ( backend backend . Backend ) * Local {
return & Local {
Backend : backend ,
}
}
2018-03-21 02:43:02 +01:00
func ( b * Local ) ConfigSchema ( ) * configschema . Block {
if b . Backend != nil {
return b . Backend . ConfigSchema ( )
2018-07-04 12:11:35 +02:00
}
2018-03-21 02:43:02 +01:00
return & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"path" : {
Type : cty . String ,
2018-07-04 12:11:35 +02:00
Optional : true ,
} ,
2018-03-21 02:43:02 +01:00
"workspace_dir" : {
Type : cty . String ,
2018-07-04 12:11:35 +02:00
Optional : true ,
} ,
} ,
}
}
2019-02-26 00:37:20 +01:00
func ( b * Local ) PrepareConfig ( obj cty . Value ) ( cty . Value , tfdiags . Diagnostics ) {
2018-03-21 02:43:02 +01:00
if b . Backend != nil {
2019-02-26 00:37:20 +01:00
return b . Backend . PrepareConfig ( obj )
2018-07-04 12:11:35 +02:00
}
2018-03-21 02:43:02 +01:00
var diags tfdiags . Diagnostics
if val := obj . GetAttr ( "path" ) ; ! val . IsNull ( ) {
p := val . AsString ( )
if p == "" {
diags = diags . Append ( tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid local state file path" ,
` The "path" attribute value must not be empty. ` ,
cty . Path { cty . GetAttrStep { Name : "path" } } ,
) )
2018-07-04 12:11:35 +02:00
}
}
2018-03-21 02:43:02 +01:00
if val := obj . GetAttr ( "workspace_dir" ) ; ! val . IsNull ( ) {
p := val . AsString ( )
if p == "" {
diags = diags . Append ( tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid local workspace directory path" ,
` The "workspace_dir" attribute value must not be empty. ` ,
cty . Path { cty . GetAttrStep { Name : "workspace_dir" } } ,
) )
2018-07-04 12:11:35 +02:00
}
}
2019-02-26 00:37:20 +01:00
return obj , diags
2018-07-04 12:11:35 +02:00
}
2017-01-19 05:47:56 +01:00
2018-03-21 02:43:02 +01:00
func ( b * Local ) Configure ( obj cty . Value ) tfdiags . Diagnostics {
2017-01-19 05:47:56 +01:00
if b . Backend != nil {
2018-03-21 02:43:02 +01:00
return b . Backend . Configure ( obj )
2017-01-19 05:47:56 +01:00
}
2018-03-21 02:43:02 +01:00
var diags tfdiags . Diagnostics
if val := obj . GetAttr ( "path" ) ; ! val . IsNull ( ) {
p := val . AsString ( )
b . StatePath = p
b . StateOutPath = p
} else {
b . StatePath = DefaultStateFilename
b . StateOutPath = DefaultStateFilename
2017-01-19 05:47:56 +01:00
}
2018-03-21 02:43:02 +01:00
if val := obj . GetAttr ( "workspace_dir" ) ; ! val . IsNull ( ) {
p := val . AsString ( )
b . StateWorkspaceDir = p
} else {
b . StateWorkspaceDir = DefaultWorkspaceDir
}
return diags
2017-01-19 05:47:56 +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 * Local ) Workspaces ( ) ( [ ] string , error ) {
// If we have a backend handling state, defer to that.
2017-02-22 19:40:04 +01:00
if b . Backend != 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
return b . Backend . Workspaces ( )
2017-02-22 19:40:04 +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
// the listing always start with "default"
envs := [ ] string { backend . DefaultStateName }
2018-07-04 12:11:35 +02: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
entries , err := ioutil . ReadDir ( b . stateWorkspaceDir ( ) )
// no error if there's no envs configured
if os . IsNotExist ( err ) {
return envs , nil
2017-02-22 01:07:27 +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
if err != nil {
return nil , err
2017-02-22 01:07:27 +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
var listed [ ] string
for _ , entry := range entries {
if entry . IsDir ( ) {
listed = append ( listed , filepath . Base ( entry . Name ( ) ) )
2018-07-04 12:11:35 +02:00
}
}
2017-02-22 01:07:27 +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
sort . Strings ( listed )
envs = append ( envs , listed ... )
return envs , nil
2017-02-22 01:07:27 +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
// DeleteWorkspace removes a workspace.
//
// The "default" workspace cannot be removed.
func ( b * Local ) DeleteWorkspace ( name string ) error {
2017-02-22 19:40:04 +01:00
// If we have a backend handling state, defer to that.
if b . Backend != 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
return b . Backend . DeleteWorkspace ( name )
2017-02-22 19:40:04 +01:00
}
2017-02-22 01:07:27 +01:00
if name == "" {
return errors . New ( "empty state name" )
}
if name == backend . DefaultStateName {
return errors . New ( "cannot delete default state" )
}
2017-02-27 22:43:31 +01:00
delete ( b . states , name )
2017-05-31 02:13:43 +02:00
return os . RemoveAll ( filepath . Join ( b . stateWorkspaceDir ( ) , name ) )
2017-02-21 16:48:00 +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 * Local ) StateMgr ( name string ) ( statemgr . Full , error ) {
// If we have a backend handling state, delegate to that.
2017-02-22 19:40:04 +01:00
if b . Backend != 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
return b . Backend . StateMgr ( name )
2017-02-22 01:07:27 +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
if s , ok := b . states [ name ] ; ok {
return s , nil
2017-02-02 00:16:16 +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
if err := b . createState ( name ) ; err != nil {
2018-07-04 12:11:35 +02:00
return nil , err
2017-01-19 05:47:56 +01:00
}
2018-11-15 20:26:46 +01:00
statePath , stateOutPath , backupPath := b . StatePaths ( name )
log . Printf ( "[TRACE] backend/local: state manager for workspace %q will:\n - read initial snapshot from %s\n - write new snapshots to %s\n - create any backup at %s" , name , statePath , stateOutPath , backupPath )
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
s := statemgr . NewFilesystemBetweenPaths ( statePath , stateOutPath )
if backupPath != "" {
s . SetBackupPath ( backupPath )
2017-01-19 05:47:56 +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
if b . states == nil {
b . states = map [ string ] statemgr . Full { }
}
b . states [ name ] = s
return s , nil
2017-01-19 05:47:56 +01:00
}
// Operation implements backend.Enhanced
//
// This will initialize an in-memory terraform.Context to perform the
// operation within this process.
//
// The given operation parameter will be merged with the ContextOpts on
// the structure with the following rules. If a rule isn't specified and the
// name conflicts, assume that the field is overwritten if set.
func ( b * Local ) Operation ( ctx context . Context , op * backend . Operation ) ( * backend . RunningOperation , error ) {
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
if op . View == nil {
panic ( "Operation called with nil View" )
}
2017-01-19 05:47:56 +01:00
// Determine the function to call for our operation
2018-02-10 00:10:52 +01:00
var f func ( context . Context , context . Context , * backend . Operation , * backend . RunningOperation )
2017-01-19 05:47:56 +01:00
switch op . Type {
case backend . OperationTypeRefresh :
f = b . opRefresh
case backend . OperationTypePlan :
f = b . opPlan
case backend . OperationTypeApply :
f = b . opApply
default :
return nil , fmt . Errorf (
core: Functional-style API for terraform.Context
Previously terraform.Context was built in an unfortunate way where all of
the data was provided up front in terraform.NewContext and then mutated
directly by subsequent operations. That made the data flow hard to follow,
commonly leading to bugs, and also meant that we were forced to take
various actions too early in terraform.NewContext, rather than waiting
until a more appropriate time during an operation.
This (enormous) commit changes terraform.Context so that its fields are
broadly just unchanging data about the execution context (current
workspace name, available plugins, etc) whereas the main data Terraform
works with arrives via individual method arguments and is returned in
return values.
Specifically, this means that terraform.Context no longer "has-a" config,
state, and "planned changes", instead holding on to those only temporarily
during an operation. The caller is responsible for propagating the outcome
of one step into the next step so that the data flow between operations is
actually visible.
However, since that's a change to the main entry points in the "terraform"
package, this commit also touches every file in the codebase which
interacted with those APIs. Most of the noise here is in updating tests
to take the same actions using the new API style, but this also affects
the main-code callers in the backends and in the command package.
My goal here was to refactor without changing observable behavior, but in
practice there are a couple externally-visible behavior variations here
that seemed okay in service of the broader goal:
- The "terraform graph" command is no longer hooked directly into the
core graph builders, because that's no longer part of the public API.
However, I did include a couple new Context functions whose contract
is to produce a UI-oriented graph, and _for now_ those continue to
return the physical graph we use for those operations. There's no
exported API for generating the "validate" and "eval" graphs, because
neither is particularly interesting in its own right, and so
"terraform graph" no longer supports those graph types.
- terraform.NewContext no longer has the responsibility for collecting
all of the provider schemas up front. Instead, we wait until we need
them. However, that means that some of our error messages now have a
slightly different shape due to unwinding through a differently-shaped
call stack. As of this commit we also end up reloading the schemas
multiple times in some cases, which is functionally acceptable but
likely represents a performance regression. I intend to rework this to
use caching, but I'm saving that for a later commit because this one is
big enough already.
The proximal reason for this change is to resolve the chicken/egg problem
whereby there was previously no single point where we could apply "moved"
statements to the previous run state before creating a plan. With this
change in place, we can now do that as part of Context.Plan, prior to
forking the input state into the three separate state artifacts we use
during planning.
However, this is at least the third project in a row where the previous
API design led to piling more functionality into terraform.NewContext and
then working around the incorrect order of operations that produces, so
I intend that by paying the cost/risk of this large diff now we can in
turn reduce the cost/risk of future projects that relate to our main
workflow actions.
2021-08-24 21:06:38 +02:00
"unsupported operation type: %s\n\n" +
2017-01-19 05:47:56 +01:00
"This is a bug in Terraform and should be reported. The local backend\n" +
"is built-in to Terraform and should always support all operations." ,
op . Type )
}
// Lock
b . opLock . Lock ( )
// Build our running operation
2018-02-10 00:10:52 +01:00
// the runninCtx is only used to block until the operation returns.
runningCtx , done := context . WithCancel ( context . Background ( ) )
runningOp := & backend . RunningOperation {
Context : runningCtx ,
}
// stopCtx wraps the context passed in, and is used to signal a graceful Stop.
stopCtx , stop := context . WithCancel ( ctx )
runningOp . Stop = stop
// cancelCtx is used to cancel the operation immediately, usually
// indicating that the process is exiting.
cancelCtx , cancel := context . WithCancel ( context . Background ( ) )
runningOp . Cancel = cancel
2017-01-19 05:47:56 +01:00
2021-02-16 13:19:22 +01:00
op . StateLocker = op . StateLocker . WithContext ( stopCtx )
2018-02-23 02:43:21 +01:00
2017-01-19 05:47:56 +01:00
// Do it
go func ( ) {
2021-10-27 22:33:35 +02:00
defer logging . PanicHandler ( )
2018-02-10 00:10:52 +01:00
defer done ( )
defer stop ( )
defer cancel ( )
2017-01-19 05:47:56 +01:00
defer b . opLock . Unlock ( )
2018-02-10 00:10:52 +01:00
f ( stopCtx , cancelCtx , op , runningOp )
2017-01-19 05:47:56 +01:00
} ( )
// Return
return runningOp , nil
}
2018-10-31 16:45:03 +01:00
// opWait waits for the operation to complete, and a stop signal or a
2018-02-12 17:52:21 +01:00
// cancelation signal.
func ( b * Local ) opWait (
doneCh <- chan struct { } ,
stopCtx context . Context ,
cancelCtx context . Context ,
tfCtx * terraform . Context ,
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
opStateMgr statemgr . Persister ,
view views . Operation ) ( canceled bool ) {
2018-02-12 17:52:21 +01:00
// Wait for the operation to finish or for us to be interrupted so
// we can handle it properly.
select {
case <- stopCtx . Done ( ) :
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
view . Stopping ( )
2018-02-12 17:52:21 +01:00
// try to force a PersistState just in case the process is terminated
// before we can complete.
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 := opStateMgr . PersistState ( ) ; err != nil {
2018-02-12 17:52:21 +01:00
// We can't error out from here, but warn the user if there was an error.
// If this isn't transient, we will catch it again below, and
// attempt to save the state another way.
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
var diags tfdiags . Diagnostics
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Error saving current state" ,
fmt . Sprintf ( earlyStateWriteErrorFmt , err ) ,
) )
view . Diagnostics ( diags )
2018-02-12 17:52:21 +01:00
}
// Stop execution
2018-10-14 18:21:31 +02:00
log . Println ( "[TRACE] backend/local: waiting for the running operation to stop" )
2018-02-12 17:52:21 +01:00
go tfCtx . Stop ( )
select {
case <- cancelCtx . Done ( ) :
2018-10-14 18:21:31 +02:00
log . Println ( "[WARN] running operation was forcefully canceled" )
2018-02-12 17:52:21 +01:00
// if the operation was canceled, we need to return immediately
canceled = true
case <- doneCh :
2018-10-14 18:21:31 +02:00
log . Println ( "[TRACE] backend/local: graceful stop has completed" )
2018-02-12 17:52:21 +01:00
}
case <- cancelCtx . Done ( ) :
// this should not be called without first attempting to stop the
// operation
log . Println ( "[ERROR] running operation canceled without Stop" )
canceled = true
case <- doneCh :
}
return
}
2017-02-22 01:07:27 +01:00
// StatePaths returns the StatePath, StateOutPath, and StateBackupPath as
2017-02-27 22:43:31 +01:00
// configured from the CLI.
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 * Local ) StatePaths ( name string ) ( stateIn , stateOut , backupOut string ) {
2018-11-15 01:03:14 +01:00
statePath := b . OverrideStatePath
stateOutPath := b . OverrideStateOutPath
backupPath := b . OverrideStateBackupPath
2017-02-22 01:07:27 +01:00
2018-11-15 01:03:14 +01:00
isDefault := name == backend . DefaultStateName || name == ""
baseDir := ""
if ! isDefault {
baseDir = filepath . Join ( b . stateWorkspaceDir ( ) , name )
2017-02-27 22:43:31 +01:00
}
2018-11-15 01:03:14 +01:00
if statePath == "" {
if isDefault {
statePath = b . StatePath // s.StatePath applies only to the default workspace, since StateWorkspaceDir is used otherwise
}
2017-02-27 22:43:31 +01:00
if statePath == "" {
2018-11-15 01:03:14 +01:00
statePath = filepath . Join ( baseDir , DefaultStateFilename )
2017-02-22 01:07:27 +01:00
}
}
if stateOutPath == "" {
stateOutPath = statePath
}
2018-11-15 01:03:14 +01:00
if backupPath == "" {
backupPath = b . StateBackupPath
}
2017-02-22 01:07:27 +01:00
switch backupPath {
case "-" :
backupPath = ""
case "" :
backupPath = stateOutPath + DefaultBackupExtension
}
2017-03-01 01:18:16 +01:00
return statePath , stateOutPath , backupPath
2017-02-22 01:07:27 +01:00
}
2018-11-16 01:01:56 +01:00
// PathsConflictWith returns true if any state path used by a workspace in
// the receiver is the same as any state path used by the other given
// local backend instance.
//
// This should be used when "migrating" from one local backend configuration to
// another in order to avoid deleting the "old" state snapshots if they are
// in the same files as the "new" state snapshots.
func ( b * Local ) PathsConflictWith ( other * Local ) bool {
otherPaths := map [ string ] struct { } { }
otherWorkspaces , err := other . Workspaces ( )
if err != nil {
// If we can't enumerate the workspaces then we'll conservatively
// assume that paths _do_ overlap, since we can't be certain.
return true
}
for _ , name := range otherWorkspaces {
p , _ , _ := other . StatePaths ( name )
otherPaths [ p ] = struct { } { }
}
ourWorkspaces , err := other . Workspaces ( )
if err != nil {
// If we can't enumerate the workspaces then we'll conservatively
// assume that paths _do_ overlap, since we can't be certain.
return true
}
for _ , name := range ourWorkspaces {
p , _ , _ := b . StatePaths ( name )
if _ , exists := otherPaths [ p ] ; exists {
return true
}
}
return false
}
2017-02-27 22:43:31 +01:00
// this only ensures that the named directory exists
2017-02-22 01:07:27 +01:00
func ( b * Local ) createState ( name string ) error {
2017-02-27 22:43:31 +01:00
if name == backend . DefaultStateName {
return nil
2017-02-22 01:07:27 +01:00
}
2017-05-31 02:13:43 +02:00
stateDir := filepath . Join ( b . stateWorkspaceDir ( ) , name )
2017-02-27 22:43:31 +01:00
s , err := os . Stat ( stateDir )
if err == nil && s . IsDir ( ) {
// no need to check for os.IsNotExist, since that is covered by os.MkdirAll
// which will catch the other possible errors as well.
return nil
2017-02-22 01:07:27 +01:00
}
2017-02-27 22:43:31 +01:00
err = os . MkdirAll ( stateDir , 0755 )
2017-02-22 01:07:27 +01:00
if err != nil {
return err
}
return nil
}
2017-05-31 02:13:43 +02:00
// stateWorkspaceDir returns the directory where state environments are stored.
func ( b * Local ) stateWorkspaceDir ( ) string {
if b . StateWorkspaceDir != "" {
return b . StateWorkspaceDir
2017-03-09 11:47:21 +01:00
}
2017-05-31 02:13:43 +02:00
return DefaultWorkspaceDir
2017-03-09 11:47:21 +01:00
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
const earlyStateWriteErrorFmt = ` Error : % s
Terraform encountered an error attempting to save the state before cancelling the current operation . Once the operation is complete another attempt will be made to save the final state . `