terraform/command/refresh.go

153 lines
4.1 KiB
Go
Raw Normal View History

2014-06-27 20:09:01 +02:00
package command
import (
2017-01-19 05:50:45 +01:00
"context"
2014-06-27 20:09:01 +02:00
"fmt"
"strings"
2017-01-19 05:50:45 +01:00
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
2014-06-27 20:09:01 +02:00
)
// RefreshCommand is a cli.Command implementation that refreshes the state
// file.
type RefreshCommand struct {
Meta
2014-06-27 20:09:01 +02:00
}
func (c *RefreshCommand) Run(args []string) int {
args, err := c.Meta.process(args, true)
if err != nil {
return 1
}
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")
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")
2017-02-06 16:07:32 +01:00
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
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
}
2017-01-19 05:50:45 +01:00
configPath, err := ModulePath(cmdFlags.Args())
if err != nil {
c.Ui.Error(err.Error())
2014-06-27 20:09:01 +02:00
return 1
}
var diags tfdiags.Diagnostics
2017-01-19 05:50:45 +01:00
// Load the module
mod, diags := c.Module(configPath)
if diags.HasErrors() {
c.showDiagnostics(diags)
2014-10-12 03:46:46 +02:00
return 1
2014-07-28 00:09:04 +02: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
}
var conf *config.Config
if mod != nil {
conf = mod.Config()
}
2017-01-19 05:50:45 +01:00
// Load the backend
b, err := c.Backend(&BackendOpts{
Config: conf,
})
2014-06-27 20:09:01 +02:00
if err != nil {
2017-01-19 05:50:45 +01:00
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
2014-06-27 20:09:01 +02:00
return 1
}
2017-01-19 05:50:45 +01:00
// Build the operation
opReq := c.Operation()
opReq.Type = backend.OperationTypeRefresh
opReq.Module = mod
2014-06-27 20:09:01 +02:00
2017-01-19 05:50:45 +01:00
// Perform the operation
op, err := b.Operation(context.Background(), opReq)
2014-06-27 20:09:01 +02:00
if err != nil {
2017-01-19 05:50:45 +01:00
c.Ui.Error(fmt.Sprintf("Error starting operation: %s", err))
2014-06-27 20:09:01 +02:00
return 1
}
2017-01-19 05:50:45 +01:00
// Wait for the operation to complete
<-op.Done()
if err := op.Err; err != nil {
diags = diags.Append(err)
}
c.showDiagnostics(diags)
if diags.HasErrors() {
2014-06-27 20:09:01 +02:00
return 1
}
2017-01-19 05:50:45 +01:00
// Output the outputs
if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
2014-06-27 20:09:01 +02:00
return 0
}
func (c *RefreshCommand) Help() string {
helpText := `
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
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
".backup" extension. Set to "-" to disable backup.
2014-07-28 00:09:04 +02:00
-input=true Ask for input for variables if not directly set.
2017-02-06 16:07:32 +01:00
-lock=true Lock the state file when locking is supported.
-lock-timeout=0s Duration to retry a state lock.
-no-color If specified, output won't contain any color.
-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
-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
2017-06-22 03:22:07 +02:00
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.
2014-07-18 20:37:27 +02:00
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
}