2014-10-09 00:15:14 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2015-02-22 20:07:51 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2014-10-09 00:15:14 +02:00
|
|
|
)
|
|
|
|
|
2015-03-05 01:17:30 +01:00
|
|
|
type RemotePushCommand struct {
|
2014-10-09 00:15:14 +02:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:17:30 +01:00
|
|
|
func (c *RemotePushCommand) Run(args []string) int {
|
2014-10-09 00:15:14 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:07:51 +01:00
|
|
|
// Read out our state
|
|
|
|
s, err := c.State()
|
2014-10-09 02:39:08 +02:00
|
|
|
if err != nil {
|
2015-02-22 20:07:51 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
|
2014-10-09 02:39:08 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-02-22 20:07:51 +01:00
|
|
|
localState := s.State()
|
|
|
|
|
|
|
|
// If remote state isn't enabled, it is a problem.
|
|
|
|
if !localState.IsRemote() {
|
2014-10-10 01:52:29 +02:00
|
|
|
c.Ui.Error("Remote state not enabled!")
|
2014-10-09 02:39:08 +02:00
|
|
|
return 1
|
2014-10-09 00:15:14 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 20:07:51 +01:00
|
|
|
// We need the CacheState structure in order to do anything
|
|
|
|
var cache *state.CacheState
|
|
|
|
if bs, ok := s.(*state.BackupState); ok {
|
|
|
|
if cs, ok := bs.Real.(*state.CacheState); ok {
|
|
|
|
cache = cs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cache == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Failed to extract internal CacheState from remote state.\n" +
|
|
|
|
"This is an internal error, please report it as a bug."))
|
2014-10-09 00:15:14 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:07:51 +01:00
|
|
|
// Refresh the cache state
|
|
|
|
if err := cache.Cache.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Failed to refresh from remote state: %s", err))
|
2014-10-09 00:15:14 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-02-22 20:07:51 +01:00
|
|
|
|
|
|
|
// Write it to the real storage
|
|
|
|
remote := cache.Durable
|
|
|
|
if err := remote.WriteState(cache.Cache.State()); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err := remote.PersistState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error saving state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-10-09 00:15:14 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:17:30 +01:00
|
|
|
func (c *RemotePushCommand) Help() string {
|
2014-10-09 00:15:14 +02:00
|
|
|
helpText := `
|
|
|
|
Usage: terraform push [options]
|
|
|
|
|
2014-10-10 01:52:29 +02:00
|
|
|
Uploads the latest state to the remote server.
|
2014-10-09 00:15:14 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-force Forces the upload of the local state, ignoring any
|
|
|
|
conflicts. This should be used carefully, as force pushing
|
|
|
|
can cause remote state information to be lost.
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:17:30 +01:00
|
|
|
func (c *RemotePushCommand) Synopsis() string {
|
2014-10-09 00:15:14 +02:00
|
|
|
return "Uploads the the local state to the remote server"
|
|
|
|
}
|