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