2014-06-27 13:09:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-01-18 22:50:45 -06:00
|
|
|
"context"
|
2014-06-27 13:09:01 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-07-29 14:13:50 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2017-05-01 16:47:53 -05:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2016-07-29 14:13:50 -05:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
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 {
|
2014-08-05 11:32:01 -05:00
|
|
|
args = c.Meta.process(args, true)
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Load the module
|
|
|
|
mod, err := c.Module(configPath)
|
2014-10-11 20:46:46 -05:00
|
|
|
if err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
|
2014-10-11 20:46:46 -05:00
|
|
|
return 1
|
2014-07-27 17:09:04 -05:00
|
|
|
}
|
|
|
|
|
2017-05-01 16:47:53 -05:00
|
|
|
var conf *config.Config
|
|
|
|
if mod != nil {
|
|
|
|
conf = mod.Config()
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Load the backend
|
2017-05-01 16:47:53 -05:00
|
|
|
b, err := c.Backend(&BackendOpts{
|
|
|
|
Config: conf,
|
|
|
|
})
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-23 02:33:40 -06:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Build the operation
|
|
|
|
opReq := c.Operation()
|
|
|
|
opReq.Type = backend.OperationTypeRefresh
|
|
|
|
opReq.Module = mod
|
2014-06-27 13:09:01 -05:00
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Perform the operation
|
|
|
|
op, err := b.Operation(context.Background(), opReq)
|
2014-06-27 13:09:01 -05:00
|
|
|
if err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error starting operation: %s", err))
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Wait for the operation to complete
|
|
|
|
<-op.Done()
|
|
|
|
if err := op.Err; err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-06-27 13:09:01 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Output the outputs
|
|
|
|
if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" {
|
2015-06-15 10:40:56 -05:00
|
|
|
c.Ui.Output(c.Colorize().Color(outputs))
|
|
|
|
}
|
|
|
|
|
2014-06-27 13:09:01 -05:00
|
|
|
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
|
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
|
|
|
|
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
|
|
|
}
|