github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/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.Usage = func() { c.Ui.Error(c.Help()) } 28 if err := cmdFlags.Parse(args); err != nil { 29 return 1 30 } 31 32 configPath, err := ModulePath(cmdFlags.Args()) 33 if err != nil { 34 c.Ui.Error(err.Error()) 35 return 1 36 } 37 38 // Load the module 39 mod, err := c.Module(configPath) 40 if err != nil { 41 c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) 42 return 1 43 } 44 45 // Load the backend 46 b, err := c.Backend(&BackendOpts{ConfigPath: configPath}) 47 if err != nil { 48 c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) 49 return 1 50 } 51 52 // Build the operation 53 opReq := c.Operation() 54 opReq.Type = backend.OperationTypeRefresh 55 opReq.Module = mod 56 opReq.LockState = c.Meta.stateLock 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 -no-color If specified, output won't contain any color. 102 103 -state=path Path to read and save state (unless state-out 104 is specified). Defaults to "terraform.tfstate". 105 106 -state-out=path Path to write updated state file. By default, the 107 "-state" path will be used. 108 109 -target=resource Resource to target. Operation will be limited to this 110 resource and its dependencies. This flag can be used 111 multiple times. 112 113 -var 'foo=bar' Set a variable in the Terraform configuration. This 114 flag can be set multiple times. 115 116 -var-file=foo Set variables in the Terraform configuration from 117 a file. If "terraform.tfvars" is present, it will be 118 automatically loaded if this flag is not specified. 119 120 ` 121 return strings.TrimSpace(helpText) 122 } 123 124 func (c *RefreshCommand) Synopsis() string { 125 return "Update local state file against real resources" 126 }