github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/command/remote_push.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/terraform/state" 9 ) 10 11 type RemotePushCommand struct { 12 Meta 13 } 14 15 func (c *RemotePushCommand) Run(args []string) int { 16 var force bool 17 args = c.Meta.process(args, false) 18 cmdFlags := flag.NewFlagSet("push", flag.ContinueOnError) 19 cmdFlags.BoolVar(&force, "force", false, "") 20 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 21 if err := cmdFlags.Parse(args); err != nil { 22 return 1 23 } 24 25 // Read out our state 26 s, err := c.State() 27 if err != nil { 28 c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err)) 29 return 1 30 } 31 localState := s.State() 32 33 // If remote state isn't enabled, it is a problem. 34 if !localState.IsRemote() { 35 c.Ui.Error("Remote state not enabled!") 36 return 1 37 } 38 39 // We need the CacheState structure in order to do anything 40 var cache *state.CacheState 41 if bs, ok := s.(*state.BackupState); ok { 42 if cs, ok := bs.Real.(*state.CacheState); ok { 43 cache = cs 44 } 45 } 46 if cache == nil { 47 c.Ui.Error(fmt.Sprintf( 48 "Failed to extract internal CacheState from remote state.\n" + 49 "This is an internal error, please report it as a bug.")) 50 return 1 51 } 52 53 // Refresh the cache state 54 if err := cache.Cache.RefreshState(); err != nil { 55 c.Ui.Error(fmt.Sprintf( 56 "Failed to refresh from remote state: %s", err)) 57 return 1 58 } 59 60 // Write it to the real storage 61 remote := cache.Durable 62 if err := remote.WriteState(cache.Cache.State()); err != nil { 63 c.Ui.Error(fmt.Sprintf("Error writing state: %s", err)) 64 return 1 65 } 66 if err := remote.PersistState(); err != nil { 67 c.Ui.Error(fmt.Sprintf("Error saving state: %s", err)) 68 return 1 69 } 70 71 return 0 72 } 73 74 func (c *RemotePushCommand) Help() string { 75 helpText := ` 76 Usage: terraform push [options] 77 78 Uploads the latest state to the remote server. 79 80 Options: 81 82 -force Forces the upload of the local state, ignoring any 83 conflicts. This should be used carefully, as force pushing 84 can cause remote state information to be lost. 85 86 ` 87 return strings.TrimSpace(helpText) 88 } 89 90 func (c *RemotePushCommand) Synopsis() string { 91 return "Uploads the the local state to the remote server" 92 }