github.com/kevholditch/terraform@v0.9.7-0.20170613192930-9706042ddd51/command/refresh.go (about)

     1  package command
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/backend"
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  // RefreshCommand is a cli.Command implementation that refreshes the state
    14  // file.
    15  type RefreshCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *RefreshCommand) Run(args []string) int {
    20  	args = c.Meta.process(args, true)
    21  
    22  	cmdFlags := c.Meta.flagSet("refresh")
    23  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    24  	cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
    25  	cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
    26  	cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
    27  	cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
    28  	cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
    29  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    30  	if err := cmdFlags.Parse(args); err != nil {
    31  		return 1
    32  	}
    33  
    34  	configPath, err := ModulePath(cmdFlags.Args())
    35  	if err != nil {
    36  		c.Ui.Error(err.Error())
    37  		return 1
    38  	}
    39  
    40  	// Load the module
    41  	mod, err := c.Module(configPath)
    42  	if err != nil {
    43  		c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
    44  		return 1
    45  	}
    46  
    47  	var conf *config.Config
    48  	if mod != nil {
    49  		conf = mod.Config()
    50  	}
    51  
    52  	// Load the backend
    53  	b, err := c.Backend(&BackendOpts{
    54  		Config: conf,
    55  	})
    56  	if err != nil {
    57  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    58  		return 1
    59  	}
    60  
    61  	// Build the operation
    62  	opReq := c.Operation()
    63  	opReq.Type = backend.OperationTypeRefresh
    64  	opReq.Module = mod
    65  
    66  	// Perform the operation
    67  	op, err := b.Operation(context.Background(), opReq)
    68  	if err != nil {
    69  		c.Ui.Error(fmt.Sprintf("Error starting operation: %s", err))
    70  		return 1
    71  	}
    72  
    73  	// Wait for the operation to complete
    74  	<-op.Done()
    75  	if err := op.Err; err != nil {
    76  		c.Ui.Error(err.Error())
    77  		return 1
    78  	}
    79  
    80  	// Output the outputs
    81  	if outputs := outputsAsString(op.State, terraform.RootModulePath, nil, true); outputs != "" {
    82  		c.Ui.Output(c.Colorize().Color(outputs))
    83  	}
    84  
    85  	return 0
    86  }
    87  
    88  func (c *RefreshCommand) Help() string {
    89  	helpText := `
    90  Usage: terraform refresh [options] [dir]
    91  
    92    Update the state file of your infrastructure with metadata that matches
    93    the physical resources they are tracking.
    94  
    95    This will not modify your infrastructure, but it can modify your
    96    state file to update metadata. This metadata might cause new changes
    97    to occur when you generate a plan or call apply next.
    98  
    99  Options:
   100  
   101    -backup=path        Path to backup the existing state file before
   102                        modifying. Defaults to the "-state-out" path with
   103                        ".backup" extension. Set to "-" to disable backup.
   104  
   105    -input=true         Ask for input for variables if not directly set.
   106  
   107    -lock=true          Lock the state file when locking is supported.
   108  
   109    -lock-timeout=0s    Duration to retry a state lock.
   110  
   111    -no-color           If specified, output won't contain any color.
   112  
   113    -state=path         Path to read and save state (unless state-out
   114                        is specified). Defaults to "terraform.tfstate".
   115  
   116    -state-out=path     Path to write updated state file. By default, the
   117                        "-state" path will be used.
   118  
   119    -target=resource    Resource to target. Operation will be limited to this
   120                        resource and its dependencies. This flag can be used
   121                        multiple times.
   122  
   123    -var 'foo=bar'      Set a variable in the Terraform configuration. This
   124                        flag can be set multiple times.
   125  
   126    -var-file=foo       Set variables in the Terraform configuration from
   127                        a file. If "terraform.tfvars" is present, it will be
   128                        automatically loaded if this flag is not specified.
   129  
   130  `
   131  	return strings.TrimSpace(helpText)
   132  }
   133  
   134  func (c *RefreshCommand) Synopsis() string {
   135  	return "Update local state file against real resources"
   136  }