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