This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_push.go
96 lines (79 loc) · 2.26 KB
/
remote_push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/terraform/state"
)
type RemotePushCommand struct {
Meta
}
func (c *RemotePushCommand) 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
}
// Read out our state
s, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
return 1
}
localState := s.State()
// If remote state isn't enabled, it is a problem.
if !localState.IsRemote() {
c.Ui.Error("Remote state not enabled!")
return 1
}
// 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."))
return 1
}
// Refresh the cache state
if err := cache.Cache.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf(
"Failed to refresh from remote state: %s", err))
return 1
}
// 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
}
c.Ui.Output(c.Colorize().Color(
"[reset][bold][green]State successfully pushed!"))
return 0
}
func (c *RemotePushCommand) Help() string {
helpText := `
Usage: terraform push [options]
Uploads the latest state to the remote server.
Options:
-no-color If specified, output won't contain any color.
-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)
}
func (c *RemotePushCommand) Synopsis() string {
return "Uploads the local state to the remote server"
}