github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/command/unlock.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/state"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // UnlockCommand is a cli.Command implementation that manually unlocks
    13  // the state.
    14  type UnlockCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *UnlockCommand) Run(args []string) int {
    19  	args = c.Meta.process(args, false)
    20  
    21  	force := false
    22  	cmdFlags := c.Meta.flagSet("force-unlock")
    23  	cmdFlags.BoolVar(&force, "force", false, "force")
    24  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    25  	if err := cmdFlags.Parse(args); err != nil {
    26  		return 1
    27  	}
    28  
    29  	args = cmdFlags.Args()
    30  	if len(args) == 0 {
    31  		c.Ui.Error("unlock requires a lock id argument")
    32  		return cli.RunResultHelp
    33  	}
    34  
    35  	lockID := args[0]
    36  	args = args[1:]
    37  
    38  	// assume everything is initialized. The user can manually init if this is
    39  	// required.
    40  	configPath, err := ModulePath(args)
    41  	if err != nil {
    42  		c.Ui.Error(err.Error())
    43  		return 1
    44  	}
    45  
    46  	// Load the backend
    47  	b, err := c.Backend(&BackendOpts{
    48  		ConfigPath: configPath,
    49  	})
    50  	if err != nil {
    51  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    52  		return 1
    53  	}
    54  
    55  	env := c.Env()
    56  	st, err := b.State(env)
    57  	if err != nil {
    58  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    59  		return 1
    60  	}
    61  
    62  	s, ok := st.(state.Locker)
    63  	if !ok {
    64  		c.Ui.Error("The remote state backend in use does not support locking, and therefor\n" +
    65  			"cannot be unlocked.")
    66  		return 1
    67  	}
    68  
    69  	isLocal := false
    70  	switch s := st.(type) {
    71  	case *state.BackupState:
    72  		if _, ok := s.Real.(*state.LocalState); ok {
    73  			isLocal = true
    74  		}
    75  	case *state.LocalState:
    76  		isLocal = true
    77  	}
    78  
    79  	if !force {
    80  		// Forcing this doesn't do anything, but doesn't break anything either,
    81  		// and allows us to run the basic command test too.
    82  		if isLocal {
    83  			c.Ui.Error("Local state cannot be unlocked by another process")
    84  			return 1
    85  		}
    86  
    87  		desc := "Terraform will remove the lock on the remote state.\n" +
    88  			"This will allow local Terraform commands to modify this state, even though it\n" +
    89  			"may be still be in use. Only 'yes' will be accepted to confirm."
    90  
    91  		v, err := c.UIInput().Input(&terraform.InputOpts{
    92  			Id:          "force-unlock",
    93  			Query:       "Do you really want to force-unlock?",
    94  			Description: desc,
    95  		})
    96  		if err != nil {
    97  			c.Ui.Error(fmt.Sprintf("Error asking for confirmation: %s", err))
    98  			return 1
    99  		}
   100  		if v != "yes" {
   101  			c.Ui.Output("force-unlock cancelled.")
   102  			return 1
   103  		}
   104  	}
   105  
   106  	if err := s.Unlock(lockID); err != nil {
   107  		c.Ui.Error(fmt.Sprintf("Failed to unlock state: %s", err))
   108  		return 1
   109  	}
   110  
   111  	c.Ui.Output(c.Colorize().Color(strings.TrimSpace(outputUnlockSuccess)))
   112  	return 0
   113  }
   114  
   115  func (c *UnlockCommand) Help() string {
   116  	helpText := `
   117  Usage: terraform force-unlock LOCK_ID [DIR]
   118  
   119    Manually unlock the state for the defined configuration.
   120  
   121    This will not modify your infrastructure. This command removes the lock on the
   122    state for the current configuration. The behavior of this lock is dependent
   123    on the backend being used. Local state files cannot be unlocked by another
   124    process.
   125  
   126  Options:
   127  
   128    -force                 Don't ask for input for unlock confirmation.
   129  `
   130  	return strings.TrimSpace(helpText)
   131  }
   132  
   133  func (c *UnlockCommand) Synopsis() string {
   134  	return "Manually unlock the terraform state"
   135  }
   136  
   137  const outputUnlockSuccess = `
   138  [reset][bold][green]Terraform state has been successfully unlocked![reset][green]
   139  
   140  The state has been unlocked, and Terraform commands should now be able to
   141  obtain a new lock on the remote state.
   142  `