2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
|
|
|
"github.com/hashicorp/terraform/internal/plans/planfile"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statefile"
|
|
|
|
"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"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Local) opPlan(
|
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) {
|
2018-03-21 02:43:02 +01:00
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
log.Printf("[INFO] backend/local: starting Plan operation")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
if op.PlanFile != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Can't re-plan a saved plan",
|
2018-10-31 16:45:03 +01:00
|
|
|
"The plan command was given a saved plan file as its input. This command generates "+
|
|
|
|
"a new plan, and so it requires a configuration directory as its argument.",
|
2018-03-21 02:43:02 +01:00
|
|
|
))
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2017-01-30 04:51:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// Local planning requires a config, unless we're planning to destroy.
|
2021-04-06 01:28:59 +02:00
|
|
|
if op.PlanMode != plans.DestroyMode && !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
|
|
|
"Plan requires configuration to be present. Planning without a configuration would "+
|
|
|
|
"mark everything for destruction, which is normally not what is desired. If you "+
|
|
|
|
"would like to destroy everything, run plan with the -destroy option. Otherwise, "+
|
|
|
|
"create a Terraform configuration file (.tf file) and try again.",
|
2018-03-21 02:43:02 +01:00
|
|
|
))
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2018-03-21 02:43:02 +01:00
|
|
|
return
|
2017-01-30 04:51:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = new(terraform.ContextOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, configSnap, opState, ctxDiags := b.context(op)
|
|
|
|
diags = diags.Append(ctxDiags)
|
|
|
|
if ctxDiags.HasErrors() {
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
2020-08-11 17:23:42 +02:00
|
|
|
// the state was locked during succesfull context creation; unlock the state
|
|
|
|
// when the operation completes
|
|
|
|
defer func() {
|
2021-02-16 13:19:22 +01:00
|
|
|
diags := op.StateLocker.Unlock()
|
|
|
|
if diags.HasErrors() {
|
2021-02-25 16:02:23 +01:00
|
|
|
op.View.Diagnostics(diags)
|
2020-08-11 17:23:42 +02:00
|
|
|
runningOp.Result = backend.OperationFailure
|
|
|
|
}
|
|
|
|
}()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
runningOp.State = tfCtx.State()
|
|
|
|
|
2017-12-02 23:31:28 +01:00
|
|
|
// Perform the plan in a goroutine so 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 plan *plans.Plan
|
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 planDiags tfdiags.Diagnostics
|
2017-12-02 23:31:28 +01:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
log.Printf("[INFO] backend/local: plan calling Plan")
|
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
|
|
|
plan, planDiags = tfCtx.Plan()
|
2017-12-02 23:31:28 +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
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, tfCtx, opState, op.View) {
|
2018-10-14 18:21:31 +02:00
|
|
|
// If we get in here then the operation was cancelled, which is always
|
|
|
|
// considered to be a failure.
|
|
|
|
log.Printf("[INFO] backend/local: plan operation was force-cancelled by interrupt")
|
|
|
|
runningOp.Result = backend.OperationFailure
|
2018-02-10 00:10:52 +01:00
|
|
|
return
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2018-10-14 18:21:31 +02:00
|
|
|
log.Printf("[INFO] backend/local: plan operation completed")
|
2017-01-19 05:47:56 +01:00
|
|
|
|
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(planDiags)
|
|
|
|
if planDiags.HasErrors() {
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2017-12-02 23:31:28 +01:00
|
|
|
return
|
|
|
|
}
|
2020-07-27 22:35:55 +02:00
|
|
|
|
2020-05-27 01:59:06 +02:00
|
|
|
// Record whether this plan includes any side-effects that could be applied.
|
2021-05-07 00:22:48 +02:00
|
|
|
runningOp.PlanEmpty = !plan.CanApply()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
// Save the plan to disk
|
|
|
|
if path := op.PlanOutPath; path != "" {
|
2018-10-09 21:19:24 +02:00
|
|
|
if op.PlanOutBackend == nil {
|
|
|
|
// This is always a bug in the operation caller; it's not valid
|
|
|
|
// to set PlanOutPath without also setting PlanOutBackend.
|
2018-10-31 16:45:03 +01:00
|
|
|
diags = diags.Append(fmt.Errorf(
|
|
|
|
"PlanOutPath set without also setting PlanOutBackend (this is a bug in Terraform)"),
|
|
|
|
)
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2018-10-09 21:19:24 +02:00
|
|
|
return
|
2018-09-29 00:57:27 +02:00
|
|
|
}
|
2018-10-09 21:19:24 +02:00
|
|
|
plan.Backend = *op.PlanOutBackend
|
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
|
|
|
// We may have updated the state in the refresh step above, but we
|
|
|
|
// will freeze that updated state in the plan file for now and
|
|
|
|
// only write it if this plan is subsequently applied.
|
2021-05-05 00:59:58 +02:00
|
|
|
plannedStateFile := statemgr.PlannedStateUpdate(opState, plan.PriorState)
|
|
|
|
|
|
|
|
// We also include a file containing the state as it existed before
|
|
|
|
// we took any action at all, but this one isn't intended to ever
|
|
|
|
// be saved to the backend (an equivalent snapshot should already be
|
|
|
|
// there) and so we just use a stub state file header in this case.
|
|
|
|
// NOTE: This won't be exactly identical to the latest state snapshot
|
|
|
|
// in the backend because it's still been subject to state upgrading
|
|
|
|
// to make it consumable by the current Terraform version, and
|
|
|
|
// intentionally doesn't preserve the header info.
|
|
|
|
prevStateFile := &statefile.File{
|
|
|
|
State: plan.PrevRunState,
|
|
|
|
}
|
2017-03-20 18:05:24 +01:00
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
log.Printf("[INFO] backend/local: writing plan output to: %s", path)
|
2021-05-05 00:59:58 +02:00
|
|
|
err := planfile.Create(path, configSnap, prevStateFile, plannedStateFile, plan)
|
2017-01-19 05:47:56 +01:00
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Failed to write plan file",
|
|
|
|
fmt.Sprintf("The plan file could not be written: %s.", err),
|
|
|
|
))
|
2021-02-12 19:59:14 +01:00
|
|
|
op.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Render the plan
|
2021-05-06 00:24:58 +02:00
|
|
|
op.View.Plan(plan, tfCtx.Schemas())
|
2017-09-01 04:19:06 +02: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
|
|
|
// 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.
|
2021-02-25 16:02:23 +01:00
|
|
|
op.View.Diagnostics(diags)
|
2017-09-01 04:19:06 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
if !runningOp.PlanEmpty {
|
|
|
|
op.View.PlanNextStep(op.PlanOutPath)
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|