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