add -reconfigure option for init

The reconfigure flag will force init to ignore any saved backend state.
This is useful when a user does not want any backend migration to
happen, or if the saved configuration can't be loaded at all for some
reason.
This commit is contained in:
James Bardin 2017-04-20 17:26:50 -04:00
parent 7f4a371f2c
commit 7aa2ce8341
3 changed files with 16 additions and 3 deletions

View File

@ -30,6 +30,7 @@ func (c *InitCommand) Run(args []string) int {
cmdFlags.BoolVar(&c.forceInitCopy, "force-copy", false, "suppress prompts about copying state data") cmdFlags.BoolVar(&c.forceInitCopy, "force-copy", false, "suppress prompts about copying state data")
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state") cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout") cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
cmdFlags.BoolVar(&c.reconfigure, "reconfigure", false, "reconfigure")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
@ -223,6 +224,10 @@ Options:
times. The backend type must be in the configuration times. The backend type must be in the configuration
itself. itself.
-force-copy Suppress prompts about copying state data. This is
equivalent to providing a "yes" to all confirmation
prompts.
-get=true Download any modules for this configuration. -get=true Download any modules for this configuration.
-input=true Ask for input if necessary. If false, will error if -input=true Ask for input if necessary. If false, will error if
@ -234,9 +239,7 @@ Options:
-no-color If specified, output won't contain any color. -no-color If specified, output won't contain any color.
-force-copy Suppress prompts about copying state data. This is -reconfigure Reconfigure the backend, ignoring any saved configuration.
equivalent to providing a "yes" to all confirmation
prompts.
` `
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }

View File

@ -95,6 +95,8 @@ type Meta struct {
// //
// forceInitCopy suppresses confirmation for copying state data during // forceInitCopy suppresses confirmation for copying state data during
// init. // init.
//
// reconfigure forces init to ignore any stored configuration.
statePath string statePath string
stateOutPath string stateOutPath string
backupPath string backupPath string
@ -104,6 +106,7 @@ type Meta struct {
stateLock bool stateLock bool
stateLockTimeout time.Duration stateLockTimeout time.Duration
forceInitCopy bool forceInitCopy bool
reconfigure bool
} }
// initStatePaths is used to initialize the default values for // initStatePaths is used to initialize the default values for

View File

@ -352,6 +352,13 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, error) {
s = terraform.NewState() s = terraform.NewState()
} }
// if we want to force reconfiguration of the backend, we set the backend
// state to nil on this copy. This will direct us through the correct
// configuration path in the switch statement below.
if m.reconfigure {
s.Backend = nil
}
// Upon return, we want to set the state we're using in-memory so that // Upon return, we want to set the state we're using in-memory so that
// we can access it for commands. // we can access it for commands.
m.backendState = nil m.backendState = nil