2015-02-26 19:29:23 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-04-01 21:42:13 +02:00
|
|
|
"context"
|
2015-02-26 19:29:23 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2016-05-08 11:51:46 +02: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
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2017-04-01 20:58:19 +02:00
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
2016-05-08 11:51:46 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-26 19:29:23 +01:00
|
|
|
)
|
|
|
|
|
2015-02-26 23:30:02 +01:00
|
|
|
// TaintCommand is a cli.Command implementation that manually taints
|
|
|
|
// a resource, marking it for recreation.
|
2015-02-26 19:29:23 +01:00
|
|
|
type TaintCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TaintCommand) Run(args []string) int {
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := c.Meta.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2015-02-26 19:29:23 +01:00
|
|
|
|
2015-02-26 19:56:45 +01:00
|
|
|
var allowMissing bool
|
2015-02-26 19:44:25 +01:00
|
|
|
var module string
|
2015-02-26 19:29:23 +01:00
|
|
|
cmdFlags := c.Meta.flagSet("taint")
|
2015-02-26 19:56:45 +01:00
|
|
|
cmdFlags.BoolVar(&allowMissing, "allow-missing", false, "module")
|
2015-02-26 19:44:25 +01:00
|
|
|
cmdFlags.StringVar(&module, "module", "", "module")
|
2015-02-26 19:29:23 +01:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
2017-02-06 16:07:32 +01:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
2017-04-01 22:19:59 +02:00
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
2015-02-26 19:29:23 +01:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require the one argument for the resource to taint
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error("The taint command expects exactly one argument.")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
2015-02-26 19:56:45 +01:00
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
name := args[0]
|
2015-02-26 19:56:45 +01:00
|
|
|
if module == "" {
|
|
|
|
module = "root"
|
|
|
|
} else {
|
|
|
|
module = "root." + module
|
|
|
|
}
|
2015-02-26 19:29:23 +01:00
|
|
|
|
2016-05-08 11:51:46 +02:00
|
|
|
rsk, err := terraform.ParseResourceStateKey(name)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse resource name: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rsk.Mode.Taintable() {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Resource '%s' cannot be tainted", name))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the backend
|
2018-03-28 00:31:05 +02:00
|
|
|
b, backendDiags := c.Backend(nil)
|
|
|
|
if backendDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(backendDiags)
|
2017-01-19 05:50:45 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the state
|
2017-05-31 02:13:43 +02:00
|
|
|
env := c.Workspace()
|
2017-02-28 19:13:03 +01:00
|
|
|
st, err := b.State(env)
|
2015-02-26 19:29:23 +01:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-04-01 21:42:13 +02:00
|
|
|
if c.stateLock {
|
2018-02-23 17:28:47 +01:00
|
|
|
stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize())
|
|
|
|
if err := stateLocker.Lock(st, "taint"); err != nil {
|
2017-02-14 20:33:55 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
|
2017-02-03 20:23:24 +01:00
|
|
|
return 1
|
|
|
|
}
|
2018-02-23 17:28:47 +01:00
|
|
|
defer stateLocker.Unlock(nil)
|
2017-02-03 20:23:24 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 20:32:04 +02:00
|
|
|
if err := st.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
// Get the actual state structure
|
2017-02-03 20:23:24 +01:00
|
|
|
s := st.State()
|
2015-02-26 19:29:23 +01:00
|
|
|
if s.Empty() {
|
2015-02-26 19:56:45 +01:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The state is empty. The most common reason for this is that\n" +
|
|
|
|
"an invalid state file path was given or Terraform has never\n " +
|
|
|
|
"been run for this infrastructure. Infrastructure must exist\n" +
|
|
|
|
"for it to be tainted."))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Get the ModuleState where we will taint. This is provided in a legacy
|
|
|
|
// string form that doesn't support module instance keys, so we'll shim
|
|
|
|
// it here.
|
|
|
|
modPath := addrs.Module(strings.Split(module, ".")).UnkeyedInstanceShim()
|
2015-02-26 19:44:25 +01:00
|
|
|
mod := s.ModuleByPath(modPath)
|
|
|
|
if mod == nil {
|
2015-02-26 19:56:45 +01:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:44:25 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The module %s could not be found. There is nothing to taint.",
|
|
|
|
module))
|
|
|
|
return 1
|
|
|
|
}
|
2015-02-26 19:29:23 +01:00
|
|
|
|
|
|
|
// If there are no resources in this module, it is an error
|
|
|
|
if len(mod.Resources) == 0 {
|
2015-02-26 19:56:45 +01:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The module %s has no resources. There is nothing to taint.",
|
2015-02-26 19:44:25 +01:00
|
|
|
module))
|
2015-02-26 19:29:23 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the resource we're looking for
|
|
|
|
rs, ok := mod.Resources[name]
|
|
|
|
if !ok {
|
2015-02-26 19:56:45 +01:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The resource %s couldn't be found in the module %s.",
|
|
|
|
name,
|
2015-02-26 19:44:25 +01:00
|
|
|
module))
|
2015-02-26 19:29:23 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Taint the resource
|
|
|
|
rs.Taint()
|
|
|
|
|
|
|
|
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
2017-02-03 20:23:24 +01:00
|
|
|
if err := st.WriteState(s); err != nil {
|
2017-01-19 05:50:45 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2017-02-03 20:23:24 +01:00
|
|
|
if err := st.PersistState(); err != nil {
|
2015-02-26 19:29:23 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:44:25 +01:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2015-02-26 19:37:08 +01:00
|
|
|
"The resource %s in the module %s has been marked as tainted!",
|
2015-02-26 19:44:25 +01:00
|
|
|
name, module))
|
2015-02-26 19:29:23 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TaintCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform taint [options] name
|
|
|
|
|
|
|
|
Manually mark a resource as tainted, forcing a destroy and recreate
|
|
|
|
on the next plan/apply.
|
|
|
|
|
|
|
|
This will not modify your infrastructure. This command changes your
|
|
|
|
state to mark a resource as tainted so that during the next plan or
|
|
|
|
apply, that resource will be destroyed and recreated. This command on
|
|
|
|
its own will not modify infrastructure. This command can be undone by
|
|
|
|
reverting the state backup file that is created.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2015-02-26 19:56:45 +01:00
|
|
|
-allow-missing If specified, the command will succeed (exit code 0)
|
|
|
|
even if the resource is missing.
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
|
|
|
".backup" extension. Set to "-" to disable backup.
|
|
|
|
|
2017-02-06 16:07:32 +01:00
|
|
|
-lock=true Lock the state file when locking is supported.
|
2017-02-03 20:23:24 +01:00
|
|
|
|
2017-04-01 22:19:59 +02:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2015-02-26 19:44:25 +01:00
|
|
|
-module=path The module path where the resource lives. By
|
|
|
|
default this will be root. Child modules can be specified
|
|
|
|
by names. Ex. "consul" or "consul.vpc" (nested modules).
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
|
|
|
-state=path Path to read and save state (unless state-out
|
|
|
|
is specified). Defaults to "terraform.tfstate".
|
|
|
|
|
|
|
|
-state-out=path Path to write updated state file. By default, the
|
|
|
|
"-state" path will be used.
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TaintCommand) Synopsis() string {
|
|
|
|
return "Manually mark a resource for recreation"
|
|
|
|
}
|
2015-02-26 19:56:45 +01:00
|
|
|
|
|
|
|
func (c *TaintCommand) allowMissingExit(name, module string) int {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"The resource %s in the module %s was not found, but\n"+
|
|
|
|
"-allow-missing is set, so we're exiting successfully.",
|
|
|
|
name, module))
|
|
|
|
return 0
|
|
|
|
}
|