From 4a7b554cf757521ee9ddaa570c7d45d219935e95 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Mar 2015 17:57:45 -0700 Subject: [PATCH] command/remote-config: do a pull with `terraform remote config` --- command/remote_config.go | 53 +++++++++++++++---- .../docs/commands/remote-config.html.markdown | 5 +- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/command/remote_config.go b/command/remote_config.go index fa6d069299..92017c4849 100644 --- a/command/remote_config.go +++ b/command/remote_config.go @@ -86,29 +86,63 @@ func (c *RemoteConfigCommand) Run(args []string) int { return c.disableRemoteState() } - // Ensure there is no conflict + // Ensure there is no conflict, and then do the correct operation + var result int haveCache := !remoteState.Empty() haveLocal := !localState.Empty() switch { case haveCache && haveLocal: c.Ui.Error(fmt.Sprintf("Remote state is enabled, but non-managed state file '%s' is also present!", c.conf.statePath)) - return 1 + result = 1 case !haveCache && !haveLocal: // If we don't have either state file, initialize a blank state file - return c.initBlankState() + result = c.initBlankState() case haveCache && !haveLocal: // Update the remote state target potentially - return c.updateRemoteConfig() + result = c.updateRemoteConfig() case !haveCache && haveLocal: // Enable remote state management - return c.enableRemoteState() + result = c.enableRemoteState() + } + + // If there was an error, return right away + if result != 0 { + return result + } + + // If we're not pulling, then do nothing + if !c.conf.pullOnDisable { + return result } - panic("unhandled case") + // Otherwise, refresh the state + stateResult, err := c.StateRaw(c.StateOpts()) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error while performing the initial pull. The error message is shown\n"+ + "below. Note that remote state was properly configured, so you don't\n"+ + "need to reconfigure. You can now use `push` and `pull` directly.\n"+ + "\n%s", err)) + return 1 + } + + state := stateResult.State + if err := state.RefreshState(); err != nil { + c.Ui.Error(fmt.Sprintf( + "Error while performing the initial pull. The error message is shown\n"+ + "below. Note that remote state was properly configured, so you don't\n"+ + "need to reconfigure. You can now use `push` and `pull` directly.\n"+ + "\n%s", err)) + return 1 + } + + c.Ui.Output(c.Colorize().Color(fmt.Sprintf( + "[reset][bold][green]Remote state configured and pulled."))) + return 0 } // disableRemoteState is used to disable remote state management, @@ -326,9 +360,10 @@ Options: -disable Disables remote state management and migrates the state to the -state path. - -pull=true Controls if the remote state is pulled before disabling. - This defaults to true to ensure the latest state is cached - before disabling. + -pull=true If disabling, this controls if the remote state is + pulled before disabling. If enabling, this controls + if the remote state is pulled after enabling. This + defaults to true. -state=path Path to read state. Defaults to "terraform.tfstate" unless remote state is enabled. diff --git a/website/source/docs/commands/remote-config.html.markdown b/website/source/docs/commands/remote-config.html.markdown index 3fd6c9b17a..5e247d5407 100644 --- a/website/source/docs/commands/remote-config.html.markdown +++ b/website/source/docs/commands/remote-config.html.markdown @@ -73,8 +73,9 @@ The command-line flags are all optional. The list of available flags are: * `-path=path` - Path of the remote state in Consul. Required for the Consul backend. -* `-pull=true` - Controls if the remote state is pulled before disabling. - This defaults to true to ensure the latest state is cached before disabling. +* `-pull=true` - Controls if the remote state is pulled before disabling + or after enabling. This defaults to true to ensure the latest state + is available under both conditions. * `-state=path` - Path to read state. Defaults to "terraform.tfstate" unless remote state is enabled.