2014-06-27 13:09:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RefreshCommand is a cli.Command implementation that refreshes the state
|
|
|
|
// file.
|
|
|
|
type RefreshCommand struct {
|
2014-07-12 22:21:46 -05:00
|
|
|
Meta
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Run(args []string) int {
|
2014-07-27 17:09:04 -05:00
|
|
|
var statePath, stateOutPath, backupPath string
|
2014-06-27 13:09:01 -05:00
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
args = c.Meta.process(args)
|
|
|
|
|
2014-07-18 13:37:27 -05:00
|
|
|
cmdFlags := c.Meta.flagSet("refresh")
|
2014-07-11 23:56:43 -05:00
|
|
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
|
|
|
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
2014-07-27 17:09:04 -05:00
|
|
|
cmdFlags.StringVar(&backupPath, "backup", "", "path")
|
2014-06-27 13:09:01 -05:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
var configPath string
|
2014-06-27 13:09:01 -05:00
|
|
|
args = cmdFlags.Args()
|
2014-07-11 23:56:43 -05:00
|
|
|
if len(args) > 1 {
|
|
|
|
c.Ui.Error("The apply command expacts at most one argument.")
|
2014-06-27 13:09:01 -05:00
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
2014-07-11 23:56:43 -05: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 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
// If we don't specify an output path, default to out normal state
|
|
|
|
// path.
|
|
|
|
if stateOutPath == "" {
|
|
|
|
stateOutPath = statePath
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
2014-07-27 17:09:04 -05:00
|
|
|
// If we don't specify a backup path, default to state out with
|
|
|
|
// the extention
|
|
|
|
if backupPath == "" {
|
|
|
|
backupPath = stateOutPath + DefaultBackupExtention
|
|
|
|
}
|
|
|
|
|
2014-07-12 00:01:33 -05: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.
|
2014-07-12 00:00:55 -05:00
|
|
|
if _, err := os.Stat(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",
|
|
|
|
statePath))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"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",
|
|
|
|
statePath,
|
|
|
|
err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
// Build the context based on the arguments given
|
2014-07-26 19:45:38 -05:00
|
|
|
ctx, _, err := c.Context(configPath, statePath)
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
2014-07-11 23:56:43 -05:00
|
|
|
c.Ui.Error(err.Error())
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
2014-07-03 15:12:45 -05:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-27 13:09:01 -05:00
|
|
|
|
2014-07-27 17:09:04 -05:00
|
|
|
// Create a backup of the state before updating
|
2014-07-27 21:58:12 -05:00
|
|
|
if backupPath != "-" && c.State != nil {
|
2014-07-27 17:09:04 -05:00
|
|
|
log.Printf("[INFO] Writing backup state to: %s", backupPath)
|
|
|
|
f, err := os.Create(backupPath)
|
|
|
|
if err == nil {
|
|
|
|
err = terraform.WriteState(c.State, f)
|
2014-07-28 00:56:55 -05:00
|
|
|
f.Close()
|
2014-07-27 17:09:04 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
state, err := ctx.Refresh()
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
log.Printf("[INFO] Writing state output to: %s", stateOutPath)
|
|
|
|
f, err := os.Create(stateOutPath)
|
2014-06-27 13:09:01 -05:00
|
|
|
if err == nil {
|
|
|
|
defer f.Close()
|
|
|
|
err = terraform.WriteState(state, f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Help() string {
|
|
|
|
helpText := `
|
2014-07-11 23:56:43 -05: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 13:09:01 -05:00
|
|
|
|
2014-07-11 23:56:43 -05: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 13:09:01 -05:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-07-27 17:09:04 -05:00
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
|
|
|
".backup" extention. Set to "-" to disable backup.
|
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-07-11 23:56:43 -05: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 13:09:01 -05:00
|
|
|
|
2014-07-18 13:37:27 -05: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 13:09:01 -05:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RefreshCommand) Synopsis() string {
|
2014-07-12 21:28:38 -05:00
|
|
|
return "Update local state file against real resources"
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|