command/refresh: Remote enable

This commit is contained in:
Armon Dadgar 2014-10-11 18:46:46 -07:00 committed by Mitchell Hashimoto
parent 0fb87e499d
commit b6265cf1a2
1 changed files with 32 additions and 56 deletions

View File

@ -6,7 +6,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/remote"
) )
// RefreshCommand is a cli.Command implementation that refreshes the state // RefreshCommand is a cli.Command implementation that refreshes the state
@ -16,14 +16,12 @@ type RefreshCommand struct {
} }
func (c *RefreshCommand) Run(args []string) int { func (c *RefreshCommand) Run(args []string) int {
var statePath, stateOutPath, backupPath string
args = c.Meta.process(args, true) args = c.Meta.process(args, true)
cmdFlags := c.Meta.flagSet("refresh") cmdFlags := c.Meta.flagSet("refresh")
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path") cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path") cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
cmdFlags.StringVar(&backupPath, "backup", "", "path") cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
return 1 return 1
@ -45,48 +43,45 @@ func (c *RefreshCommand) Run(args []string) int {
} }
} }
// If we don't specify an output path, default to out normal state // Check if remote state is enabled
// path. remoteEnabled, err := remote.HaveLocalState()
if stateOutPath == "" { if err != nil {
stateOutPath = statePath c.Ui.Error(fmt.Sprintf("Failed to check for remote state: %v", err))
} return 1
// If we don't specify a backup path, default to state out with
// the extension
if backupPath == "" {
backupPath = stateOutPath + DefaultBackupExtention
} }
// Verify that the state path exists. The "ContextArg" function below // Verify that the state path exists. The "ContextArg" function below
// will actually do this, but we want to provide a richer error message // will actually do this, but we want to provide a richer error message
// if possible. // if possible.
if _, err := os.Stat(statePath); err != nil { if !remoteEnabled {
if os.IsNotExist(err) { 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
}
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"The Terraform state file for your infrastructure does not\n"+ "There was an error reading the Terraform state that is needed\n"+
"exist. The 'refresh' command only works and only makes sense\n"+ "for refreshing. The path and error are shown below.\n\n"+
"when there is existing state that Terraform is managing. Please\n"+ "Path: %s\n\nError: %s",
"double-check the value given below and try again. If you\n"+ c.Meta.statePath,
"haven't created infrastructure with Terraform yet, use the\n"+ err))
"'terraform apply' command.\n\n"+
"Path: %s",
statePath))
return 1 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
} }
// Build the context based on the arguments given // Build the context based on the arguments given
ctx, _, err := c.Context(contextOpts{ ctx, _, err := c.Context(contextOpts{
Path: configPath, Path: configPath,
StatePath: statePath, StatePath: c.Meta.statePath,
}) })
if err != nil { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
@ -100,33 +95,14 @@ func (c *RefreshCommand) Run(args []string) int {
return 1 return 1
} }
// Create a backup of the state before updating
if backupPath != "-" && c.state != nil {
log.Printf("[INFO] Writing backup state to: %s", backupPath)
f, err := os.Create(backupPath)
if err == nil {
err = terraform.WriteState(c.state, f)
f.Close()
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
return 1
}
}
state, err := ctx.Refresh() state, err := ctx.Refresh()
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err)) c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
return 1 return 1
} }
log.Printf("[INFO] Writing state output to: %s", stateOutPath) log.Printf("[INFO] Writing state output to: %s", c.Meta.StateOutPath())
f, err := os.Create(stateOutPath) if err := c.Meta.PersistState(state); err != nil {
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)) c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
return 1 return 1
} }