// persistRemoteState is used to handle persisting a state file // when remote state management is enabled func (m *Meta) persistRemoteState(s *terraform.State) error { log.Printf("[INFO] Persisting state to local cache") if err := remote.PersistState(s); err != nil { return err } log.Printf("[INFO] Uploading state to remote store") change, err := remote.PushState(s.Remote, false) if err != nil { return err } if !change.SuccessfulPush() { return fmt.Errorf("Failed to upload state: %s", change) } return nil }
func (c *PushCommand) Run(args []string) int { var force bool args = c.Meta.process(args, false) cmdFlags := flag.NewFlagSet("push", flag.ContinueOnError) cmdFlags.BoolVar(&force, "force", false, "") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { return 1 } // Check for a remote state file local, _, err := remote.ReadLocalState() if err != nil { c.Ui.Error(fmt.Sprintf("%s", err)) return 1 } if local == nil || local.Remote == nil { c.Ui.Error("Remote state not enabled!") return 1 } // Attempt to push the state change, err := remote.PushState(local.Remote, force) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to push state: %v", err)) return 1 } // Use an error exit code if the update was not a success if !change.SuccessfulPush() { c.Ui.Error(fmt.Sprintf("%s", change)) return 1 } else { c.Ui.Output(fmt.Sprintf("%s", change)) } return 0 }