github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/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 conf, err := c.Config(configPath) 47 if err != nil { 48 c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) 49 return 1 50 } 51 52 // Load the backend 53 b, err := c.Backend(&BackendOpts{ 54 Config: conf, 55 }) 56 if err != nil { 57 c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) 58 return 1 59 } 60 61 env := c.Workspace() 62 st, err := b.State(env) 63 if err != nil { 64 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 65 return 1 66 } 67 68 isLocal := false 69 switch s := st.(type) { 70 case *state.BackupState: 71 if _, ok := s.Real.(*state.LocalState); ok { 72 isLocal = true 73 } 74 case *state.LocalState: 75 isLocal = true 76 } 77 78 if !force { 79 // Forcing this doesn't do anything, but doesn't break anything either, 80 // and allows us to run the basic command test too. 81 if isLocal { 82 c.Ui.Error("Local state cannot be unlocked by another process") 83 return 1 84 } 85 86 desc := "Terraform will remove the lock on the remote state.\n" + 87 "This will allow local Terraform commands to modify this state, even though it\n" + 88 "may be still be in use. Only 'yes' will be accepted to confirm." 89 90 v, err := c.UIInput().Input(&terraform.InputOpts{ 91 Id: "force-unlock", 92 Query: "Do you really want to force-unlock?", 93 Description: desc, 94 }) 95 if err != nil { 96 c.Ui.Error(fmt.Sprintf("Error asking for confirmation: %s", err)) 97 return 1 98 } 99 if v != "yes" { 100 c.Ui.Output("force-unlock cancelled.") 101 return 1 102 } 103 } 104 105 if err := st.Unlock(lockID); err != nil { 106 c.Ui.Error(fmt.Sprintf("Failed to unlock state: %s", err)) 107 return 1 108 } 109 110 c.Ui.Output(c.Colorize().Color(strings.TrimSpace(outputUnlockSuccess))) 111 return 0 112 } 113 114 func (c *UnlockCommand) Help() string { 115 helpText := ` 116 Usage: terraform force-unlock LOCK_ID [DIR] 117 118 Manually unlock the state for the defined configuration. 119 120 This will not modify your infrastructure. This command removes the lock on the 121 state for the current configuration. The behavior of this lock is dependent 122 on the backend being used. Local state files cannot be unlocked by another 123 process. 124 125 Options: 126 127 -force Don't ask for input for unlock confirmation. 128 ` 129 return strings.TrimSpace(helpText) 130 } 131 132 func (c *UnlockCommand) Synopsis() string { 133 return "Manually unlock the terraform state" 134 } 135 136 const outputUnlockSuccess = ` 137 [reset][bold][green]Terraform state has been successfully unlocked![reset][green] 138 139 The state has been unlocked, and Terraform commands should now be able to 140 obtain a new lock on the remote state. 141 `