2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2017-05-18 00:26:21 +02:00
|
|
|
"bytes"
|
2017-01-19 05:47:56 +01:00
|
|
|
"context"
|
2017-05-18 00:26:21 +02:00
|
|
|
"errors"
|
2017-01-19 05:47:56 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
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/statefile"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Local) opApply(
|
2018-02-10 00:10:52 +01:00
|
|
|
stopCtx context.Context,
|
|
|
|
cancelCtx context.Context,
|
2017-01-19 05:47:56 +01:00
|
|
|
op *backend.Operation,
|
|
|
|
runningOp *backend.RunningOperation) {
|
|
|
|
log.Printf("[INFO] backend/local: starting Apply operation")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-02-16 01:00:59 +01:00
|
|
|
|
|
|
|
// If we have a nil module at this point, then set it to an empty tree
|
|
|
|
// to avoid any potential crashes.
|
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 op.PlanFile == nil && !op.Destroy && !op.HasConfig() {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"No configuration files",
|
2018-10-31 16:45:03 +01:00
|
|
|
"Apply requires configuration to be present. Applying without a configuration "+
|
|
|
|
"would mark everything for destruction, which is normally not what is desired. "+
|
|
|
|
"If you would like to destroy everything, run 'terraform destroy' instead.",
|
2018-03-21 02:43:02 +01:00
|
|
|
))
|
|
|
|
b.ReportResult(runningOp, diags)
|
|
|
|
return
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Setup our count hook that keeps track of resource changes
|
|
|
|
countHook := new(CountHook)
|
|
|
|
stateHook := new(StateHook)
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = new(terraform.ContextOpts)
|
|
|
|
}
|
|
|
|
old := b.ContextOpts.Hooks
|
|
|
|
defer func() { b.ContextOpts.Hooks = old }()
|
|
|
|
b.ContextOpts.Hooks = append(b.ContextOpts.Hooks, countHook, stateHook)
|
|
|
|
|
|
|
|
// Get our context
|
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
|
|
|
tfCtx, _, opState, contextDiags := b.context(op)
|
|
|
|
diags = diags.Append(contextDiags)
|
|
|
|
if contextDiags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state
|
|
|
|
runningOp.State = tfCtx.State()
|
|
|
|
|
|
|
|
// If we weren't given a plan, then we refresh/plan
|
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 op.PlanFile == nil {
|
2017-01-19 05:47:56 +01:00
|
|
|
// If we're refreshing before apply, perform that
|
|
|
|
if op.PlanRefresh {
|
|
|
|
log.Printf("[INFO] backend/local: apply calling Refresh")
|
2019-11-21 15:14:50 +01:00
|
|
|
_, refreshDiags := tfCtx.Refresh()
|
|
|
|
diags = diags.Append(refreshDiags)
|
|
|
|
if diags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
runningOp.Result = backend.OperationFailure
|
|
|
|
b.ShowDiagnostics(diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the plan
|
|
|
|
log.Printf("[INFO] backend/local: apply calling Plan")
|
2019-09-12 21:11:54 +02:00
|
|
|
plan, planDiags := tfCtx.Plan()
|
|
|
|
diags = diags.Append(planDiags)
|
|
|
|
if planDiags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 03:06:28 +02:00
|
|
|
|
2018-08-29 21:12:18 +02:00
|
|
|
trivialPlan := plan.Changes.Empty()
|
2016-06-21 03:06:28 +02:00
|
|
|
hasUI := op.UIOut != nil && op.UIIn != nil
|
2018-02-01 01:14:42 +01:00
|
|
|
mustConfirm := hasUI && ((op.Destroy && (!op.DestroyForce && !op.AutoApprove)) || (!op.Destroy && !op.AutoApprove && !trivialPlan))
|
command/format: improve consistency of plan results
Previously the rendered plan output was constructed directly from the
core plan and then annotated with counts derived from the count hook.
At various places we applied little adjustments to deal with the fact that
the user-facing diff model is not identical to the internal diff model,
including the special handling of data source reads and destroys. Since
this logic was just muddled into the rendering code, it behaved
inconsistently with the tally of adds, updates and deletes.
This change reworks the plan formatter so that it happens in two stages:
- First, we produce a specialized Plan object that is tailored for use
in the UI. This applies all the relevant logic to transform the
physical model into the user model.
- Second, we do a straightforward visual rendering of the display-oriented
plan object.
For the moment this is slightly overkill since there's only one rendering
path, but it does give us the benefit of letting the counts be derived
from the same data as the full detailed diff, ensuring that they'll stay
consistent.
Later we may choose to have other UIs for plans, such as a
machine-readable output intended to drive a web UI. In that case, we'd
want the web UI to consume a serialization of the _display-oriented_ plan
so that it doesn't need to re-implement all of these UI special cases.
This introduces to core a new diff action type for "refresh". Currently
this is used _only_ in the UI layer, to represent data source reads.
Later it would be good to use this type for the core diff as well, to
improve consistency, but that is left for another day to keep this change
focused on the UI.
2017-08-24 01:23:02 +02:00
|
|
|
if mustConfirm {
|
2017-06-13 03:30:57 +02:00
|
|
|
var desc, query string
|
|
|
|
if op.Destroy {
|
2018-06-19 22:35:28 +02:00
|
|
|
if op.Workspace != "default" {
|
|
|
|
query = "Do you really want to destroy all resources in workspace \"" + op.Workspace + "\"?"
|
|
|
|
} else {
|
|
|
|
query = "Do you really want to destroy all resources?"
|
|
|
|
}
|
command/format: improve consistency of plan results
Previously the rendered plan output was constructed directly from the
core plan and then annotated with counts derived from the count hook.
At various places we applied little adjustments to deal with the fact that
the user-facing diff model is not identical to the internal diff model,
including the special handling of data source reads and destroys. Since
this logic was just muddled into the rendering code, it behaved
inconsistently with the tally of adds, updates and deletes.
This change reworks the plan formatter so that it happens in two stages:
- First, we produce a specialized Plan object that is tailored for use
in the UI. This applies all the relevant logic to transform the
physical model into the user model.
- Second, we do a straightforward visual rendering of the display-oriented
plan object.
For the moment this is slightly overkill since there's only one rendering
path, but it does give us the benefit of letting the counts be derived
from the same data as the full detailed diff, ensuring that they'll stay
consistent.
Later we may choose to have other UIs for plans, such as a
machine-readable output intended to drive a web UI. In that case, we'd
want the web UI to consume a serialization of the _display-oriented_ plan
so that it doesn't need to re-implement all of these UI special cases.
This introduces to core a new diff action type for "refresh". Currently
this is used _only_ in the UI layer, to represent data source reads.
Later it would be good to use this type for the core diff as well, to
improve consistency, but that is left for another day to keep this change
focused on the UI.
2017-08-24 01:23:02 +02:00
|
|
|
desc = "Terraform will destroy all your managed infrastructure, as shown above.\n" +
|
2017-06-13 03:30:57 +02:00
|
|
|
"There is no undo. Only 'yes' will be accepted to confirm."
|
|
|
|
} else {
|
2018-06-19 22:35:28 +02:00
|
|
|
if op.Workspace != "default" {
|
|
|
|
query = "Do you want to perform these actions in workspace \"" + op.Workspace + "\"?"
|
|
|
|
} else {
|
|
|
|
query = "Do you want to perform these actions?"
|
|
|
|
}
|
2017-09-01 04:19:06 +02:00
|
|
|
desc = "Terraform will perform the actions described above.\n" +
|
2017-06-13 03:30:57 +02:00
|
|
|
"Only 'yes' will be accepted to approve."
|
|
|
|
}
|
|
|
|
|
|
|
|
if !trivialPlan {
|
|
|
|
// Display the plan of what we are going to apply/destroy.
|
2019-03-06 01:18:55 +01:00
|
|
|
b.renderPlan(plan, runningOp.State, tfCtx.Schemas())
|
2017-09-01 04:19:06 +02:00
|
|
|
b.CLI.Output("")
|
2017-06-13 03:30:57 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:11:54 +02:00
|
|
|
// We'll show any accumulated warnings before we display the prompt,
|
|
|
|
// so the user can consider them when deciding how to answer.
|
|
|
|
if len(diags) > 0 {
|
|
|
|
b.ShowDiagnostics(diags)
|
|
|
|
diags = nil // reset so we won't show the same diagnostics again later
|
|
|
|
}
|
|
|
|
|
2019-03-07 21:07:13 +01:00
|
|
|
v, err := op.UIIn.Input(stopCtx, &terraform.InputOpts{
|
2016-06-21 03:06:28 +02:00
|
|
|
Id: "approve",
|
2017-06-13 03:30:57 +02:00
|
|
|
Query: query,
|
2016-06-21 03:06:28 +02:00
|
|
|
Description: desc,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(errwrap.Wrapf("Error asking for approval: {{err}}", err))
|
|
|
|
b.ReportResult(runningOp, diags)
|
2016-06-21 03:06:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if v != "yes" {
|
2017-06-13 03:30:57 +02:00
|
|
|
if op.Destroy {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.CLI.Info("Destroy cancelled.")
|
2017-06-13 03:30:57 +02:00
|
|
|
} else {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.CLI.Info("Apply cancelled.")
|
2017-06-13 03:30:57 +02:00
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
runningOp.Result = backend.OperationFailure
|
2016-06-21 03:06:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup our hook for continuous state updates
|
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
|
|
|
stateHook.StateMgr = opState
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
// Start the apply in a goroutine so that we can be interrupted.
|
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 applyState *states.State
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
var applyDiags tfdiags.Diagnostics
|
2017-01-19 05:47:56 +01:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
2018-10-12 01:36:09 +02:00
|
|
|
log.Printf("[INFO] backend/local: apply calling Apply")
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
_, applyDiags = tfCtx.Apply()
|
2017-04-14 16:53:08 +02:00
|
|
|
// we always want the state, even if apply failed
|
|
|
|
applyState = tfCtx.State()
|
2017-01-19 05:47:56 +01:00
|
|
|
}()
|
|
|
|
|
2018-02-12 17:52:21 +01:00
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, tfCtx, opState) {
|
2018-02-10 00:10:52 +01:00
|
|
|
return
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the final state
|
|
|
|
runningOp.State = applyState
|
2018-10-31 16:45:03 +01:00
|
|
|
err := statemgr.WriteAndPersist(opState, applyState)
|
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 {
|
2019-03-27 16:01:07 +01:00
|
|
|
// Export the state file from the state manager and assign the new
|
|
|
|
// state. This is needed to preserve the existing serial and lineage.
|
|
|
|
stateFile := statemgr.Export(opState)
|
|
|
|
if stateFile == nil {
|
|
|
|
stateFile = &statefile.File{}
|
|
|
|
}
|
|
|
|
stateFile.State = applyState
|
|
|
|
|
|
|
|
diags = diags.Append(b.backupStateForError(stateFile, err))
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
diags = diags.Append(applyDiags)
|
|
|
|
if applyDiags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// If we've accumulated any warnings along the way then we'll show them
|
|
|
|
// here just before we show the summary and next steps. If we encountered
|
|
|
|
// errors then we would've returned early at some other point above.
|
|
|
|
b.ShowDiagnostics(diags)
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// If we have a UI, output the results
|
|
|
|
if b.CLI != nil {
|
|
|
|
if op.Destroy {
|
|
|
|
b.CLI.Output(b.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold][green]\n"+
|
|
|
|
"Destroy complete! Resources: %d destroyed.",
|
|
|
|
countHook.Removed)))
|
|
|
|
} else {
|
|
|
|
b.CLI.Output(b.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold][green]\n"+
|
|
|
|
"Apply complete! Resources: %d added, %d changed, %d destroyed.",
|
|
|
|
countHook.Added,
|
|
|
|
countHook.Changed,
|
|
|
|
countHook.Removed)))
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:30:44 +02:00
|
|
|
// only show the state file help message if the state is local.
|
|
|
|
if (countHook.Added > 0 || countHook.Changed > 0) && b.StateOutPath != "" {
|
2017-01-19 05:47:56 +01:00
|
|
|
b.CLI.Output(b.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset]\n"+
|
|
|
|
"The state of your infrastructure has been saved to the path\n"+
|
|
|
|
"below. This state is required to modify and destroy your\n"+
|
|
|
|
"infrastructure, so keep it safe. To inspect the complete state\n"+
|
|
|
|
"use the `terraform show` command.\n\n"+
|
|
|
|
"State path: %s",
|
|
|
|
b.StateOutPath)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 01:00:59 +01:00
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
// backupStateForError is called in a scenario where we're unable to persist the
|
|
|
|
// state for some reason, and will attempt to save a backup copy of the state
|
|
|
|
// to local disk to help the user recover. This is a "last ditch effort" sort
|
|
|
|
// of thing, so we really don't want to end up in this codepath; we should do
|
|
|
|
// everything we possibly can to get the state saved _somewhere_.
|
2019-03-27 16:01:07 +01:00
|
|
|
func (b *Local) backupStateForError(stateFile *statefile.File, err error) error {
|
2017-05-18 00:26:21 +02:00
|
|
|
b.CLI.Error(fmt.Sprintf("Failed to save state: %s\n", 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
|
|
|
local := statemgr.NewFilesystem("errored.tfstate")
|
2019-03-27 16:01:07 +01:00
|
|
|
writeErr := local.WriteStateForMigration(stateFile, true)
|
2017-05-18 00:26:21 +02:00
|
|
|
if writeErr != nil {
|
|
|
|
b.CLI.Error(fmt.Sprintf(
|
|
|
|
"Also failed to create local state file for recovery: %s\n\n", writeErr,
|
|
|
|
))
|
|
|
|
// To avoid leaving the user with no state at all, our last resort
|
|
|
|
// is to print the JSON state out onto the terminal. This is an awful
|
|
|
|
// UX, so we should definitely avoid doing this if at all possible,
|
|
|
|
// but at least the user has _some_ path to recover if we end up
|
|
|
|
// here for some reason.
|
|
|
|
stateBuf := new(bytes.Buffer)
|
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
|
|
|
jsonErr := statefile.Write(stateFile, stateBuf)
|
2017-05-18 00:26:21 +02:00
|
|
|
if jsonErr != nil {
|
|
|
|
b.CLI.Error(fmt.Sprintf(
|
|
|
|
"Also failed to JSON-serialize the state to print it: %s\n\n", jsonErr,
|
|
|
|
))
|
|
|
|
return errors.New(stateWriteFatalError)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.CLI.Output(stateBuf.String())
|
|
|
|
|
|
|
|
return errors.New(stateWriteConsoleFallbackError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(stateWriteBackedUpError)
|
|
|
|
}
|
|
|
|
|
|
|
|
const stateWriteBackedUpError = `Failed to persist state to backend.
|
|
|
|
|
|
|
|
The error shown above has prevented Terraform from writing the updated state
|
|
|
|
to the configured backend. To allow for recovery, the state has been written
|
|
|
|
to the file "errored.tfstate" in the current working directory.
|
|
|
|
|
|
|
|
Running "terraform apply" again at this point will create a forked state,
|
|
|
|
making it harder to recover.
|
|
|
|
|
|
|
|
To retry writing this state, use the following command:
|
|
|
|
terraform state push errored.tfstate
|
|
|
|
`
|
|
|
|
|
|
|
|
const stateWriteConsoleFallbackError = `Failed to persist state to backend.
|
|
|
|
|
|
|
|
The errors shown above prevented Terraform from writing the updated state to
|
|
|
|
the configured backend and from creating a local backup file. As a fallback,
|
|
|
|
the raw state data is printed above as a JSON object.
|
|
|
|
|
|
|
|
To retry writing this state, copy the state data (from the first { to the
|
|
|
|
last } inclusive) and save it into a local file called errored.tfstate, then
|
|
|
|
run the following command:
|
|
|
|
terraform state push errored.tfstate
|
|
|
|
`
|
|
|
|
|
|
|
|
const stateWriteFatalError = `Failed to save state after apply.
|
|
|
|
|
|
|
|
A catastrophic error has prevented Terraform from persisting the state file
|
|
|
|
or creating a backup. Unfortunately this means that the record of any resources
|
|
|
|
created during this apply has been lost, and such resources may exist outside
|
|
|
|
of Terraform's management.
|
|
|
|
|
|
|
|
For resources that support import, it is possible to recover by manually
|
|
|
|
importing each resource using its id from the target system.
|
|
|
|
|
|
|
|
This is a serious bug in Terraform and should be reported.
|
|
|
|
`
|
2017-05-25 15:29:51 +02:00
|
|
|
|
|
|
|
const earlyStateWriteErrorFmt = `Error saving current state: %s
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
Terraform encountered an error attempting to save the state before cancelling
|
2017-05-25 15:29:51 +02:00
|
|
|
the current operation. Once the operation is complete another attempt will be
|
|
|
|
made to save the final state.
|
|
|
|
`
|