github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/command/state/state.go (about)

     1  // Package state exposes common helpers for working with state from the CLI.
     2  //
     3  // This is a separate package so that backends can use this for consistent
     4  // messaging without creating a circular reference to the command package.
     5  package message
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/hashicorp/errwrap"
    13  	"github.com/hashicorp/terraform/helper/slowmessage"
    14  	"github.com/hashicorp/terraform/state"
    15  	"github.com/mitchellh/cli"
    16  	"github.com/mitchellh/colorstring"
    17  )
    18  
    19  const (
    20  	LockThreshold    = 400 * time.Millisecond
    21  	LockMessage      = "Acquiring state lock. This may take a few moments..."
    22  	LockErrorMessage = `Error acquiring the state lock: {{err}}
    23  
    24  Terraform acquires a state lock to protect the state from being written
    25  by multiple users at the same time. Please resolve the issue above and try
    26  again. For most commands, you can disable locking with the "-lock=false"
    27  flag, but this is not recommended.`
    28  
    29  	UnlockMessage      = "Releasing state lock. This may take a few moments..."
    30  	UnlockErrorMessage = `
    31  [reset][bold][red]Error releasing the state lock![reset][red]
    32  
    33  Error message: %s
    34  
    35  Terraform acquires a lock when accessing your state to prevent others
    36  running Terraform to potentially modify the state at the same time. An
    37  error occurred while releasing this lock. This could mean that the lock
    38  did or did not release properly. If the lock didn't release properly,
    39  Terraform may not be able to run future commands since it'll appear as if
    40  the lock is held.
    41  
    42  In this scenario, please call the "force-unlock" command to unlock the
    43  state manually. This is a very dangerous operation since if it is done
    44  erroneously it could result in two people modifying state at the same time.
    45  Only call this command if you're certain that the unlock above failed and
    46  that no one else is holding a lock.
    47  `
    48  )
    49  
    50  // Lock locks the given state and outputs to the user if locking
    51  // is taking longer than the threshold.
    52  func Lock(s state.State, info string, ui cli.Ui, color *colorstring.Colorize) error {
    53  	sl, ok := s.(state.Locker)
    54  	if !ok {
    55  		return nil
    56  	}
    57  
    58  	err := slowmessage.Do(LockThreshold, func() error {
    59  		return sl.Lock(info)
    60  	}, func() {
    61  		if ui != nil {
    62  			ui.Output(color.Color(LockMessage))
    63  		}
    64  	})
    65  
    66  	if err != nil {
    67  		err = errwrap.Wrapf(strings.TrimSpace(LockErrorMessage), err)
    68  	}
    69  
    70  	return err
    71  }
    72  
    73  // Unlock unlocks the given state and outputs to the user if the
    74  // unlock fails what can be done.
    75  func Unlock(s state.State, ui cli.Ui, color *colorstring.Colorize) error {
    76  	sl, ok := s.(state.Locker)
    77  	if !ok {
    78  		return nil
    79  	}
    80  
    81  	err := slowmessage.Do(LockThreshold, sl.Unlock, func() {
    82  		if ui != nil {
    83  			ui.Output(color.Color(UnlockMessage))
    84  		}
    85  	})
    86  
    87  	if err != nil {
    88  		ui.Output(color.Color(fmt.Sprintf(
    89  			"\n"+strings.TrimSpace(UnlockErrorMessage)+"\n", err)))
    90  
    91  		err = fmt.Errorf(
    92  			"Error releasing the state lock. Please see the longer error message above.")
    93  	}
    94  
    95  	return err
    96  }