|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/remote" |
| 9 | + "github.com/hashicorp/terraform/terraform" |
| 10 | +) |
| 11 | + |
| 12 | +type PushCommand struct { |
| 13 | + Meta |
| 14 | +} |
| 15 | + |
| 16 | +func (c *PushCommand) Run(args []string) int { |
| 17 | + var force bool |
| 18 | + var remoteConf terraform.RemoteState |
| 19 | + args = c.Meta.process(args, false) |
| 20 | + cmdFlags := flag.NewFlagSet("push", flag.ContinueOnError) |
| 21 | + cmdFlags.StringVar(&remoteConf.Name, "remote", "", "") |
| 22 | + cmdFlags.StringVar(&remoteConf.Server, "remote-server", "", "") |
| 23 | + cmdFlags.StringVar(&remoteConf.AuthToken, "remote-auth", "", "") |
| 24 | + cmdFlags.BoolVar(&force, "force", false, "") |
| 25 | + cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } |
| 26 | + if err := cmdFlags.Parse(args); err != nil { |
| 27 | + return 1 |
| 28 | + } |
| 29 | + |
| 30 | + // Validate the remote configuration if given |
| 31 | + var conf *terraform.RemoteState |
| 32 | + if !remoteConf.Empty() { |
| 33 | + if err := remote.ValidateConfig(&remoteConf); err != nil { |
| 34 | + c.Ui.Error(fmt.Sprintf("%s", err)) |
| 35 | + return 1 |
| 36 | + } |
| 37 | + conf = &remoteConf |
| 38 | + } else { |
| 39 | + // Recover the local state if any |
| 40 | + local, _, err := remote.ReadLocalState() |
| 41 | + if err != nil { |
| 42 | + c.Ui.Error(fmt.Sprintf("%s", err)) |
| 43 | + return 1 |
| 44 | + } |
| 45 | + if local == nil || local.Remote == nil { |
| 46 | + c.Ui.Error("No remote state server configured") |
| 47 | + return 1 |
| 48 | + } |
| 49 | + conf = local.Remote |
| 50 | + } |
| 51 | + |
| 52 | + // Attempt to push the state |
| 53 | + change, err := remote.PushState(conf, force) |
| 54 | + if err != nil { |
| 55 | + c.Ui.Error(fmt.Sprintf( |
| 56 | + "Failed to push state: %v", err)) |
| 57 | + return 1 |
| 58 | + } |
| 59 | + |
| 60 | + // Use an error exit code if the update was not a success |
| 61 | + if !change.SuccessfulPush() { |
| 62 | + c.Ui.Error(fmt.Sprintf("%s", change)) |
| 63 | + return 1 |
| 64 | + } else { |
| 65 | + c.Ui.Output(fmt.Sprintf("%s", change)) |
| 66 | + } |
| 67 | + return 0 |
| 68 | +} |
| 69 | + |
| 70 | +func (c *PushCommand) Help() string { |
| 71 | + helpText := ` |
| 72 | +Usage: terraform push [options] |
| 73 | +
|
| 74 | + Uploads the local state file to the remote server. This is done automatically |
| 75 | + by commands when remote state if configured, but can also be done manually |
| 76 | + using this command. |
| 77 | +
|
| 78 | +Options: |
| 79 | +
|
| 80 | + -force Forces the upload of the local state, ignoring any |
| 81 | + conflicts. This should be used carefully, as force pushing |
| 82 | + can cause remote state information to be lost. |
| 83 | +
|
| 84 | + -remote=name Name of the state file in the state storage server. |
| 85 | + Optional, default does not use remote storage. |
| 86 | +
|
| 87 | + -remote-auth=token Authentication token for state storage server. |
| 88 | + Optional, defaults to blank. |
| 89 | +
|
| 90 | + -remote-server=url URL of the remote storage server. |
| 91 | +
|
| 92 | +` |
| 93 | + return strings.TrimSpace(helpText) |
| 94 | +} |
| 95 | + |
| 96 | +func (c *PushCommand) Synopsis() string { |
| 97 | + return "Uploads the the local state to the remote server" |
| 98 | +} |
0 commit comments