2014-06-27 20:09:01 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RefreshCommand is a cli.Command implementation that refreshes the state
|
|
|
|
// file.
|
|
|
|
type RefreshCommand struct {
|
2014-07-13 05:21:46 +02:00
|
|
|
Meta
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Run(args []string) int {
|
2014-08-05 18:32:01 +02:00
|
|
|
args = c.Meta.process(args, true)
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
cmdFlags := c.Meta.flagSet("refresh")
|
2014-10-12 03:46:46 +02:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
2015-05-07 17:21:00 +02:00
|
|
|
cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
|
2014-10-12 03:46:46 +02:00
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
2014-06-27 20:09:01 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
var configPath string
|
2014-06-27 20:09:01 +02:00
|
|
|
args = cmdFlags.Args()
|
2014-07-12 06:56:43 +02:00
|
|
|
if len(args) > 1 {
|
2015-01-18 00:43:04 +01:00
|
|
|
c.Ui.Error("The refresh command expects at most one argument.")
|
2014-06-27 20:09:01 +02:00
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
2014-07-12 06:56:43 +02:00
|
|
|
} else if len(args) == 1 {
|
|
|
|
configPath = args[0]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
configPath, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
}
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 03:46:46 +02:00
|
|
|
// Check if remote state is enabled
|
2015-02-22 20:11:24 +01:00
|
|
|
state, err := c.State()
|
2014-10-12 03:46:46 +02:00
|
|
|
if err != nil {
|
2015-02-22 20:11:24 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
2014-10-12 03:46:46 +02:00
|
|
|
return 1
|
2014-07-28 00:09:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-12 07:01:33 +02:00
|
|
|
// Verify that the state path exists. The "ContextArg" function below
|
|
|
|
// will actually do this, but we want to provide a richer error message
|
|
|
|
// if possible.
|
2015-02-22 20:11:24 +01:00
|
|
|
if !state.State().IsRemote() {
|
2014-10-12 03:46:46 +02:00
|
|
|
if _, err := os.Stat(c.Meta.statePath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"The Terraform state file for your infrastructure does not\n"+
|
|
|
|
"exist. The 'refresh' command only works and only makes sense\n"+
|
|
|
|
"when there is existing state that Terraform is managing. Please\n"+
|
|
|
|
"double-check the value given below and try again. If you\n"+
|
|
|
|
"haven't created infrastructure with Terraform yet, use the\n"+
|
|
|
|
"'terraform apply' command.\n\n"+
|
|
|
|
"Path: %s",
|
|
|
|
c.Meta.statePath))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-12 07:00:55 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2014-10-12 03:46:46 +02:00
|
|
|
"There was an error reading the Terraform state that is needed\n"+
|
|
|
|
"for refreshing. The path and error are shown below.\n\n"+
|
|
|
|
"Path: %s\n\nError: %s",
|
|
|
|
c.Meta.statePath,
|
|
|
|
err))
|
2014-07-12 07:00:55 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
// Build the context based on the arguments given
|
2014-09-22 19:56:50 +02:00
|
|
|
ctx, _, err := c.Context(contextOpts{
|
2015-05-07 17:21:00 +02:00
|
|
|
Path: configPath,
|
|
|
|
StatePath: c.Meta.statePath,
|
|
|
|
Parallelism: c.Meta.parallelism,
|
2014-09-22 19:56:50 +02:00
|
|
|
})
|
2014-06-27 20:09:01 +02:00
|
|
|
if err != nil {
|
2014-07-12 06:56:43 +02:00
|
|
|
c.Ui.Error(err.Error())
|
2014-06-27 20:09:01 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-23 09:33:40 +01:00
|
|
|
|
2014-12-09 03:32:03 +01:00
|
|
|
if err := ctx.Input(c.InputMode()); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
|
2014-07-03 22:12:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
2014-06-27 20:09:01 +02:00
|
|
|
|
2015-11-23 09:33:40 +01:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:11:24 +01:00
|
|
|
newState, err := ctx.Refresh()
|
2014-06-27 20:09:01 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-10-12 03:46:46 +02:00
|
|
|
log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
|
2015-02-22 20:11:24 +01:00
|
|
|
if err := c.Meta.PersistState(newState); err != nil {
|
2014-06-27 20:09:01 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-05-09 21:46:07 +02:00
|
|
|
if outputs := outputsAsString(newState, ctx.Module().Config().Outputs); outputs != "" {
|
2015-06-15 17:40:56 +02:00
|
|
|
c.Ui.Output(c.Colorize().Color(outputs))
|
|
|
|
}
|
|
|
|
|
2014-06-27 20:09:01 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Help() string {
|
|
|
|
helpText := `
|
2014-07-12 06:56:43 +02:00
|
|
|
Usage: terraform refresh [options] [dir]
|
|
|
|
|
|
|
|
Update the state file of your infrastructure with metadata that matches
|
|
|
|
the physical resources they are tracking.
|
2014-06-27 20:09:01 +02:00
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
This will not modify your infrastructure, but it can modify your
|
|
|
|
state file to update metadata. This metadata might cause new changes
|
|
|
|
to occur when you generate a plan or call apply next.
|
2014-06-27 20:09:01 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-07-28 00:09:04 +02:00
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
2014-08-07 09:19:56 +02:00
|
|
|
".backup" extension. Set to "-" to disable backup.
|
2014-07-28 00:09:04 +02:00
|
|
|
|
2014-09-29 21:46:58 +02:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
-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.
|
2014-06-27 20:09:01 +02:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
-target=resource Resource to target. Operation will be limited to this
|
|
|
|
resource and its dependencies. This flag can be used
|
|
|
|
multiple times.
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
|
|
|
flag can be set multiple times.
|
|
|
|
|
|
|
|
-var-file=foo Set variables in the Terraform configuration from
|
|
|
|
a file. If "terraform.tfvars" is present, it will be
|
|
|
|
automatically loaded if this flag is not specified.
|
|
|
|
|
2014-06-27 20:09:01 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Synopsis() string {
|
2014-07-13 04:28:38 +02:00
|
|
|
return "Update local state file against real resources"
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|