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