github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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  	c.Ui.Output(c.Colorize().Color(
    72  		"[reset][bold][green]State successfully pushed!"))
    73  	return 0
    74  }
    75  
    76  func (c *RemotePushCommand) Help() string {
    77  	helpText := `
    78  Usage: terraform push [options]
    79  
    80    Uploads the latest state to the remote server.
    81  
    82  Options:
    83  
    84    -force                 Forces the upload of the local state, ignoring any
    85                           conflicts. This should be used carefully, as force pushing
    86  						 can cause remote state information to be lost.
    87  
    88  `
    89  	return strings.TrimSpace(helpText)
    90  }
    91  
    92  func (c *RemotePushCommand) Synopsis() string {
    93  	return "Uploads the local state to the remote server"
    94  }