2015-02-26 12:29:23 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2016-05-08 04:51:46 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-26 12:29:23 -06:00
|
|
|
)
|
|
|
|
|
2015-02-26 16:30:02 -06:00
|
|
|
// TaintCommand is a cli.Command implementation that manually taints
|
|
|
|
// a resource, marking it for recreation.
|
2015-02-26 12:29:23 -06:00
|
|
|
type TaintCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TaintCommand) Run(args []string) int {
|
|
|
|
args = c.Meta.process(args, false)
|
|
|
|
|
2015-02-26 12:56:45 -06:00
|
|
|
var allowMissing bool
|
2015-02-26 12:44:25 -06:00
|
|
|
var module string
|
2015-02-26 12:29:23 -06:00
|
|
|
cmdFlags := c.Meta.flagSet("taint")
|
2015-02-26 12:56:45 -06:00
|
|
|
cmdFlags.BoolVar(&allowMissing, "allow-missing", false, "module")
|
2015-02-26 12:44:25 -06:00
|
|
|
cmdFlags.StringVar(&module, "module", "", "module")
|
2015-02-26 12:29:23 -06:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
|
|
|
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 12:56:45 -06:00
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
name := args[0]
|
2015-02-26 12:56:45 -06:00
|
|
|
if module == "" {
|
|
|
|
module = "root"
|
|
|
|
} else {
|
|
|
|
module = "root." + module
|
|
|
|
}
|
2015-02-26 12:29:23 -06:00
|
|
|
|
2016-05-08 04:51:46 -05: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
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
// Get the state that we'll be modifying
|
|
|
|
state, err := c.State()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actual state structure
|
|
|
|
s := state.State()
|
|
|
|
if s.Empty() {
|
2015-02-26 12:56:45 -06:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06: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
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:44:25 -06:00
|
|
|
// Get the proper module we want to taint
|
|
|
|
modPath := strings.Split(module, ".")
|
|
|
|
mod := s.ModuleByPath(modPath)
|
|
|
|
if mod == nil {
|
2015-02-26 12:56:45 -06:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:44:25 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The module %s could not be found. There is nothing to taint.",
|
|
|
|
module))
|
|
|
|
return 1
|
|
|
|
}
|
2015-02-26 12:29:23 -06:00
|
|
|
|
|
|
|
// If there are no resources in this module, it is an error
|
|
|
|
if len(mod.Resources) == 0 {
|
2015-02-26 12:56:45 -06:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The module %s has no resources. There is nothing to taint.",
|
2015-02-26 12:44:25 -06:00
|
|
|
module))
|
2015-02-26 12:29:23 -06:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the resource we're looking for
|
|
|
|
rs, ok := mod.Resources[name]
|
|
|
|
if !ok {
|
2015-02-26 12:56:45 -06:00
|
|
|
if allowMissing {
|
|
|
|
return c.allowMissingExit(name, module)
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The resource %s couldn't be found in the module %s.",
|
|
|
|
name,
|
2015-02-26 12:44:25 -06:00
|
|
|
module))
|
2015-02-26 12:29:23 -06:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Taint the resource
|
|
|
|
rs.Taint()
|
|
|
|
|
|
|
|
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
|
|
|
if err := c.Meta.PersistState(s); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:44:25 -06:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2015-02-26 12:37:08 -06:00
|
|
|
"The resource %s in the module %s has been marked as tainted!",
|
2015-02-26 12:44:25 -06:00
|
|
|
name, module))
|
2015-02-26 12:29:23 -06: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 12:56:45 -06:00
|
|
|
-allow-missing If specified, the command will succeed (exit code 0)
|
|
|
|
even if the resource is missing.
|
|
|
|
|
2015-02-26 12:29:23 -06: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.
|
|
|
|
|
2015-02-26 12:44:25 -06: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 12:29:23 -06: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 12:56:45 -06: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
|
|
|
|
}
|