2014-06-27 13:09:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-07-29 14:13:50 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-06-27 13:09:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2017-03-07 22:09:48 -06:00
|
|
|
args, err := c.Meta.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-12 22:21:46 -05:00
|
|
|
|
2014-07-18 13:37:27 -05:00
|
|
|
cmdFlags := c.Meta.flagSet("refresh")
|
2014-10-11 20:46:46 -05:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
2015-05-07 10:21:00 -05:00
|
|
|
cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
|
2014-10-11 20:46:46 -05:00
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
2017-02-06 09:07:32 -06:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
2017-04-01 15:19:59 -05:00
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
configPath, err := ModulePath(cmdFlags.Args())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2017-06-15 13:26:12 -05:00
|
|
|
// Check for user-supplied plugin path
|
|
|
|
if c.pluginPath, err = c.loadPluginPath(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:31:05 -05:00
|
|
|
backendConfig, configDiags := c.loadBackendConfig(configPath)
|
|
|
|
diags = diags.Append(configDiags)
|
|
|
|
if configDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
return 1
|
2017-05-01 16:47:53 -05:00
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Load the backend
|
2018-03-27 17:31:05 -05:00
|
|
|
b, backendDiags := c.Backend(&BackendOpts{
|
|
|
|
Config: backendConfig,
|
2017-05-01 16:47:53 -05:00
|
|
|
})
|
2018-03-27 17:31:05 -05:00
|
|
|
diags = diags.Append(backendDiags)
|
|
|
|
if backendDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(diags)
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-23 02:33:40 -06:00
|
|
|
|
2018-03-27 17:31:05 -05:00
|
|
|
// Before we delegate to the backend, we'll print any warning diagnostics
|
|
|
|
// we've accumulated here, since the backend will start fresh with its own
|
|
|
|
// diagnostics.
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
diags = nil
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Build the operation
|
|
|
|
opReq := c.Operation()
|
|
|
|
opReq.Type = backend.OperationTypeRefresh
|
2018-03-27 17:31:05 -05:00
|
|
|
opReq.ConfigDir = configPath
|
|
|
|
opReq.ConfigLoader, err = c.initConfigLoader()
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
2018-03-27 17:31:05 -05:00
|
|
|
c.showDiagnostics(err)
|
|
|
|
return 1
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:31:05 -05:00
|
|
|
op, err := c.RunOperation(b, opReq)
|
|
|
|
if err != nil {
|
|
|
|
c.showDiagnostics(err)
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
2018-03-27 17:31:05 -05:00
|
|
|
if op.Result != backend.OperationSuccess {
|
|
|
|
return op.Result.ExitStatus()
|
2015-06-15 10:40:56 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:31:05 -05:00
|
|
|
// TODO: Print outputs, once this is updated to use new config types.
|
|
|
|
/*
|
|
|
|
if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" {
|
|
|
|
c.Ui.Output(c.Colorize().Color(outputs))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return op.Result.ExitStatus()
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-08-07 02:19:56 -05:00
|
|
|
".backup" extension. Set to "-" to disable backup.
|
2014-07-27 17:09:04 -05:00
|
|
|
|
2014-09-29 14:46:58 -05:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2017-02-06 09:07:32 -06:00
|
|
|
-lock=true Lock the state file when locking is supported.
|
2017-02-03 13:15:08 -06:00
|
|
|
|
2017-04-01 15:19:59 -05:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
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
|
|
|
|
2015-03-24 11:18:15 -05: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 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
|
2017-06-21 20:22:07 -05:00
|
|
|
a file. If "terraform.tfvars" or any ".auto.tfvars"
|
|
|
|
files are present, they will be automatically loaded.
|
2014-07-18 13:37:27 -05:00
|
|
|
|
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
|
|
|
}
|