2017-10-12 21:02:45 +00:00
|
|
|
package lock
|
2015-01-20 00:37:48 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-12 21:02:45 +00:00
|
|
|
"flag"
|
2015-01-20 00:37:48 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-10-25 09:18:07 +00:00
|
|
|
osexec "os/exec"
|
2015-01-20 00:37:48 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2017-10-25 09:18:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/exec"
|
2015-01-20 00:37:48 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-10-12 21:02:45 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
2015-01-20 00:37:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// lockKillGracePeriod is how long we allow a child between
|
|
|
|
// a SIGTERM and a SIGKILL. This is to let the child cleanup
|
|
|
|
// any necessary state. We have to balance this with the risk
|
|
|
|
// of a split-brain where multiple children may be acting as if
|
|
|
|
// they hold a lock. This value is currently based on the default
|
|
|
|
// lock-delay value of 15 seconds. This only affects locks and not
|
|
|
|
// semaphores.
|
|
|
|
lockKillGracePeriod = 5 * time.Second
|
2016-01-06 02:34:22 +00:00
|
|
|
|
|
|
|
// defaultMonitorRetry is the number of 500 errors we will tolerate
|
|
|
|
// before declaring the lock gone.
|
|
|
|
defaultMonitorRetry = 3
|
|
|
|
|
|
|
|
// defaultMonitorRetryTime is the amount of time to wait between
|
|
|
|
// retries.
|
|
|
|
defaultMonitorRetryTime = 1 * time.Second
|
2015-01-20 00:37:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LockCommand is a Command implementation that is used to setup
|
2015-01-24 15:09:24 +00:00
|
|
|
// a "lock" which manages lock acquisition and invokes a sub-process
|
2017-10-12 21:02:45 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2016-10-09 04:10:40 +00:00
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
ShutdownCh <-chan struct{}
|
|
|
|
|
|
|
|
child *os.Process
|
|
|
|
childLock sync.Mutex
|
2017-07-27 05:09:19 +00:00
|
|
|
verbose bool
|
2016-01-06 00:40:35 +00:00
|
|
|
|
2017-10-11 12:51:18 +00:00
|
|
|
// flags
|
|
|
|
limit int
|
|
|
|
monitorRetry int
|
|
|
|
name string
|
|
|
|
passStdin bool
|
|
|
|
propagateChildCode bool
|
|
|
|
shell bool
|
|
|
|
timeout time.Duration
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:44:48 +00:00
|
|
|
func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
|
|
|
|
c := &cmd{UI: ui, ShutdownCh: shutdownCh}
|
2017-10-12 21:02:45 +00:00
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.BoolVar(&c.propagateChildCode, "child-exit-code", false,
|
2017-07-27 05:09:19 +00:00
|
|
|
"Exit 2 if the child process exited with an error if this is true, "+
|
|
|
|
"otherwise this doesn't propagate an error from the child. The "+
|
|
|
|
"default value is false.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.IntVar(&c.limit, "n", 1,
|
2016-10-09 04:10:40 +00:00
|
|
|
"Optional limit on the number of concurrent lock holders. The underlying "+
|
|
|
|
"implementation switches from a lock to a semaphore when the value is "+
|
|
|
|
"greater than 1. The default value is 1.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.IntVar(&c.monitorRetry, "monitor-retry", defaultMonitorRetry,
|
2017-02-08 00:16:33 +00:00
|
|
|
"Number of times to retry if Consul returns a 500 error while monitoring "+
|
2016-10-09 04:10:40 +00:00
|
|
|
"the lock. This allows riding out brief periods of unavailability "+
|
|
|
|
"without causing leader elections, but increases the amount of time "+
|
|
|
|
"required to detect a lost lock in some cases. The default value is 3, "+
|
|
|
|
"with a 1s wait between retries. Set this value to 0 to disable retires.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.StringVar(&c.name, "name", "",
|
2016-10-09 04:10:40 +00:00
|
|
|
"Optional name to associate with the lock session. It not provided, one "+
|
|
|
|
"is generated based on the provided child command.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.BoolVar(&c.passStdin, "pass-stdin", false,
|
2016-10-09 04:10:40 +00:00
|
|
|
"Pass stdin to the child process.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.BoolVar(&c.shell, "shell", true,
|
2017-10-04 23:48:00 +00:00
|
|
|
"Use a shell to run the command (can set a custom shell via the SHELL "+
|
|
|
|
"environment variable).")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.DurationVar(&c.timeout, "timeout", 0,
|
2016-10-09 04:10:40 +00:00
|
|
|
"Maximum amount of time to wait to acquire the lock, specified as a "+
|
2017-07-27 05:09:19 +00:00
|
|
|
"duration like \"1s\" or \"3h\". The default value is 0.")
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.BoolVar(&c.verbose, "verbose", false,
|
2016-10-09 04:10:40 +00:00
|
|
|
"Enable verbose (debugging) output.")
|
|
|
|
|
|
|
|
// Deprecations
|
2017-10-12 21:02:45 +00:00
|
|
|
c.flags.DurationVar(&c.timeout, "try", 0,
|
2016-10-09 04:10:40 +00:00
|
|
|
"DEPRECATED. Use -timeout instead.")
|
2017-10-12 21:02:45 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-10-11 12:51:18 +00:00
|
|
|
}
|
2016-10-09 04:10:40 +00:00
|
|
|
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
2017-10-11 12:51:18 +00:00
|
|
|
var lu *LockUnlock
|
|
|
|
return c.run(args, &lu)
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) run(args []string, lu **LockUnlock) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the limit
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.limit <= 0 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Lock holder limit must be positive"))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the prefix and child are provided
|
2017-10-12 21:02:45 +00:00
|
|
|
extra := c.flags.Args()
|
2015-01-20 00:37:48 +00:00
|
|
|
if len(extra) < 2 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Key prefix and child command must be specified")
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
prefix := extra[0]
|
2015-07-30 04:32:24 +00:00
|
|
|
prefix = strings.TrimPrefix(prefix, "/")
|
2015-01-20 00:37:48 +00:00
|
|
|
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.timeout < 0 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Timeout must be positive")
|
2017-02-07 01:50:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-01-20 01:38:00 +00:00
|
|
|
// Calculate a session name if none provided
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.name == "" {
|
|
|
|
c.name = fmt.Sprintf("Consul lock for '%s' at '%s'", strings.Join(extra[1:], " "), prefix)
|
2015-01-20 01:38:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
// Calculate oneshot
|
2017-10-11 12:51:18 +00:00
|
|
|
oneshot := c.timeout > 0
|
2016-01-06 00:40:35 +00:00
|
|
|
|
2016-01-06 01:59:58 +00:00
|
|
|
// Check the retry parameter
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.monitorRetry < 0 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Number for 'monitor-retry' must be >= 0")
|
2016-01-06 01:59:58 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
// Create and test the HTTP client
|
2017-10-12 21:02:45 +00:00
|
|
|
client, err := c.http.APIClient()
|
2015-01-20 00:37:48 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
_, err = client.Agent().NodeName()
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the lock or semaphore
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.limit == 1 {
|
|
|
|
*lu, err = c.setupLock(client, prefix, c.name, oneshot, c.timeout, c.monitorRetry)
|
2015-01-20 00:37:48 +00:00
|
|
|
} else {
|
2017-10-11 12:51:18 +00:00
|
|
|
*lu, err = c.setupSemaphore(client, c.limit, prefix, c.name, oneshot, c.timeout, c.monitorRetry)
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Lock setup failed: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt the acquisition
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Attempting lock acquisition")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2016-01-06 01:59:58 +00:00
|
|
|
lockCh, err := (*lu).lockFn(c.ShutdownCh)
|
2015-07-04 10:48:17 +00:00
|
|
|
if lockCh == nil {
|
|
|
|
if err == nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Shutdown triggered or timeout during lock acquisition")
|
2015-07-04 10:48:17 +00:00
|
|
|
} else {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Lock acquisition failed: %s", err))
|
2015-07-04 10:48:17 +00:00
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-07-22 23:07:44 +00:00
|
|
|
// Check if we were shutdown but managed to still acquire the lock
|
2017-07-27 05:09:19 +00:00
|
|
|
var childCode int
|
|
|
|
var childErr chan error
|
2015-07-22 23:07:44 +00:00
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Shutdown triggered during lock acquisition")
|
2015-07-22 23:07:44 +00:00
|
|
|
goto RELEASE
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
// Start the child process
|
2017-07-27 05:09:19 +00:00
|
|
|
childErr = make(chan error, 1)
|
2015-01-20 00:37:48 +00:00
|
|
|
go func() {
|
2017-10-12 21:02:45 +00:00
|
|
|
childErr <- c.startChild(c.flags.Args()[1:], c.passStdin, c.shell)
|
2015-01-20 00:37:48 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Monitor for shutdown, child termination, or lock loss
|
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Shutdown triggered, killing child")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
case <-lockCh:
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Lock lost, killing child")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2017-07-27 05:09:19 +00:00
|
|
|
case err := <-childErr:
|
|
|
|
if err != nil {
|
|
|
|
childCode = 2
|
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Child terminated, releasing lock")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
goto RELEASE
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:33:09 +00:00
|
|
|
// Prevent starting a new child. The lock is never released
|
|
|
|
// after this point.
|
|
|
|
c.childLock.Lock()
|
2017-07-27 05:09:19 +00:00
|
|
|
|
2015-08-31 23:33:09 +00:00
|
|
|
// Kill any existing child
|
2017-07-27 05:09:19 +00:00
|
|
|
if err := c.killChild(childErr); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("%s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE:
|
|
|
|
// Release the lock before termination
|
2016-01-06 01:59:58 +00:00
|
|
|
if err := (*lu).unlockFn(); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Lock release failed: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup the lock if no longer in use
|
2016-01-06 01:59:58 +00:00
|
|
|
if err := (*lu).cleanupFn(); err != nil {
|
|
|
|
if err != (*lu).inUseErr {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Lock cleanup failed: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return 1
|
|
|
|
} else if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Cleanup aborted, lock in use")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
} else if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Cleanup succeeded")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2017-07-27 05:09:19 +00:00
|
|
|
|
|
|
|
// If we detected an error from the child process then we propagate
|
|
|
|
// that.
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.propagateChildCode {
|
2017-07-27 05:09:19 +00:00
|
|
|
return childCode
|
|
|
|
}
|
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:40:35 +00:00
|
|
|
// setupLock is used to setup a new Lock given the API client, the key prefix to
|
|
|
|
// operate on, and an optional session name. If oneshot is true then we will set
|
2016-01-06 01:59:58 +00:00
|
|
|
// up for a single attempt at acquisition, using the given wait time. The retry
|
|
|
|
// parameter sets how many 500 errors the lock monitor will tolerate before
|
|
|
|
// giving up the lock.
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) setupLock(client *api.Client, prefix, name string,
|
2016-01-06 01:59:58 +00:00
|
|
|
oneshot bool, wait time.Duration, retry int) (*LockUnlock, error) {
|
2015-09-11 19:24:54 +00:00
|
|
|
// Use the DefaultSemaphoreKey extension, this way if a lock and
|
2015-01-20 01:26:17 +00:00
|
|
|
// semaphore are both used at the same prefix, we will get a conflict
|
|
|
|
// which we can report to the user.
|
|
|
|
key := path.Join(prefix, api.DefaultSemaphoreKey)
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Setting up lock at path: %s", key))
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
opts := api.LockOptions{
|
2016-01-06 02:34:22 +00:00
|
|
|
Key: key,
|
|
|
|
SessionName: name,
|
|
|
|
MonitorRetries: retry,
|
|
|
|
MonitorRetryTime: defaultMonitorRetryTime,
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
if oneshot {
|
|
|
|
opts.LockTryOnce = true
|
|
|
|
opts.LockWaitTime = wait
|
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
l, err := client.LockOpts(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lu := &LockUnlock{
|
|
|
|
lockFn: l.Lock,
|
|
|
|
unlockFn: l.Unlock,
|
|
|
|
cleanupFn: l.Destroy,
|
|
|
|
inUseErr: api.ErrLockInUse,
|
2016-01-06 01:59:58 +00:00
|
|
|
rawOpts: &opts,
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
return lu, nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:40:35 +00:00
|
|
|
// setupSemaphore is used to setup a new Semaphore given the API client, key
|
|
|
|
// prefix, session name, and slot holder limit. If oneshot is true then we will
|
2016-01-06 01:59:58 +00:00
|
|
|
// set up for a single attempt at acquisition, using the given wait time. The
|
|
|
|
// retry parameter sets how many 500 errors the lock monitor will tolerate
|
|
|
|
// before giving up the semaphore.
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) setupSemaphore(client *api.Client, limit int, prefix, name string,
|
2016-01-06 01:59:58 +00:00
|
|
|
oneshot bool, wait time.Duration, retry int) (*LockUnlock, error) {
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Setting up semaphore (limit %d) at prefix: %s", limit, prefix))
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
opts := api.SemaphoreOptions{
|
2016-01-06 02:34:22 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Limit: limit,
|
|
|
|
SessionName: name,
|
|
|
|
MonitorRetries: retry,
|
|
|
|
MonitorRetryTime: defaultMonitorRetryTime,
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
if oneshot {
|
|
|
|
opts.SemaphoreTryOnce = true
|
|
|
|
opts.SemaphoreWaitTime = wait
|
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
s, err := client.SemaphoreOpts(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lu := &LockUnlock{
|
|
|
|
lockFn: s.Acquire,
|
|
|
|
unlockFn: s.Release,
|
|
|
|
cleanupFn: s.Destroy,
|
|
|
|
inUseErr: api.ErrSemaphoreInUse,
|
2016-01-06 01:59:58 +00:00
|
|
|
rawOpts: &opts,
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
return lu, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// startChild is a long running routine used to start and
|
|
|
|
// wait for the child process to exit.
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) startChild(args []string, passStdin, shell bool) error {
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-10-04 23:48:00 +00:00
|
|
|
c.UI.Info("Starting handler")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2017-10-04 23:48:00 +00:00
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
// Create the command
|
2017-10-25 09:18:07 +00:00
|
|
|
var cmd *osexec.Cmd
|
2017-10-04 23:48:00 +00:00
|
|
|
var err error
|
|
|
|
if !shell {
|
2017-10-25 09:18:07 +00:00
|
|
|
cmd, err = exec.Subprocess(args)
|
2017-10-04 23:48:00 +00:00
|
|
|
} else {
|
2017-10-25 09:18:07 +00:00
|
|
|
cmd, err = exec.Script(strings.Join(args, " "))
|
2017-10-04 23:48:00 +00:00
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error executing handler: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the command streams
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"CONSUL_LOCK_HELD=true",
|
|
|
|
)
|
2015-08-26 07:10:04 +00:00
|
|
|
if passStdin {
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Stdin passed to handler process")
|
2015-08-26 07:10:04 +00:00
|
|
|
}
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
} else {
|
|
|
|
cmd.Stdin = nil
|
|
|
|
}
|
2015-01-20 00:37:48 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
// Start the child process
|
2015-08-05 16:06:51 +00:00
|
|
|
c.childLock.Lock()
|
2015-01-20 00:37:48 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error starting handler: %s", err))
|
2015-08-05 16:06:51 +00:00
|
|
|
c.childLock.Unlock()
|
2015-01-20 00:37:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:48:00 +00:00
|
|
|
// Set up signal forwarding.
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
logFn := func(err error) {
|
|
|
|
c.UI.Error(fmt.Sprintf("Warning, could not forward signal: %s", err))
|
|
|
|
}
|
|
|
|
agent.ForwardSignals(cmd, logFn, doneCh)
|
|
|
|
|
2015-01-20 00:37:48 +00:00
|
|
|
// Setup the child info
|
|
|
|
c.child = cmd.Process
|
|
|
|
c.childLock.Unlock()
|
|
|
|
|
|
|
|
// Wait for the child process
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error running handler: %s", err))
|
2015-01-20 00:37:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// killChild is used to forcefully kill the child, first using SIGTERM
|
|
|
|
// to allow for a graceful cleanup and then using SIGKILL for a hard
|
|
|
|
// termination.
|
2015-01-25 10:42:26 +00:00
|
|
|
// On Windows, the child is always hard terminated with a SIGKILL, even
|
2015-01-24 15:09:24 +00:00
|
|
|
// on the first attempt.
|
2017-10-12 21:02:45 +00:00
|
|
|
func (c *cmd) killChild(childErr chan error) error {
|
2015-01-20 00:37:48 +00:00
|
|
|
// Get the child process
|
|
|
|
child := c.child
|
|
|
|
|
|
|
|
// If there is no child process (failed to start), we can quit early
|
|
|
|
if child == nil {
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("No child process to kill")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:09:24 +00:00
|
|
|
// Attempt termination first
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Terminating child pid %d", child.Pid))
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2015-01-24 15:09:24 +00:00
|
|
|
if err := signalPid(child.Pid, syscall.SIGTERM); err != nil {
|
2015-01-20 00:37:48 +00:00
|
|
|
return fmt.Errorf("Failed to terminate %d: %v", child.Pid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for termination, or until a timeout
|
|
|
|
select {
|
2017-07-27 05:09:19 +00:00
|
|
|
case <-childErr:
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Child terminated")
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case <-time.After(lockKillGracePeriod):
|
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Child did not exit after grace period of %v",
|
2015-01-20 00:37:48 +00:00
|
|
|
lockKillGracePeriod))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:09:24 +00:00
|
|
|
// Send a final SIGKILL
|
2015-01-20 00:37:48 +00:00
|
|
|
if c.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Killing child pid %d", child.Pid))
|
2015-01-20 00:37:48 +00:00
|
|
|
}
|
2015-01-24 15:09:24 +00:00
|
|
|
if err := signalPid(child.Pid, syscall.SIGKILL); err != nil {
|
2015-01-20 00:37:48 +00:00
|
|
|
return fmt.Errorf("Failed to kill %d: %v", child.Pid, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
2017-10-12 21:02:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
2017-10-12 21:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockUnlock is used to abstract over the differences between
|
|
|
|
// a lock and a semaphore.
|
|
|
|
type LockUnlock struct {
|
|
|
|
lockFn func(<-chan struct{}) (<-chan struct{}, error)
|
|
|
|
unlockFn func() error
|
|
|
|
cleanupFn func() error
|
|
|
|
inUseErr error
|
|
|
|
rawOpts interface{}
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
const synopsis = "Execute a command holding a lock"
|
|
|
|
const help = `
|
|
|
|
Usage: consul lock [options] prefix child...
|
2017-10-11 12:51:18 +00:00
|
|
|
|
|
|
|
Acquires a lock or semaphore at a given path, and invokes a child process
|
|
|
|
when successful. The child process can assume the lock is held while it
|
|
|
|
executes. If the lock is lost or communication is disrupted the child
|
|
|
|
process will be sent a SIGTERM signal and given time to gracefully exit.
|
|
|
|
After the grace period expires the process will be hard terminated.
|
|
|
|
|
|
|
|
For Consul agents on Windows, the child process is always hard terminated
|
|
|
|
with a SIGKILL, since Windows has no POSIX compatible notion for SIGTERM.
|
|
|
|
|
|
|
|
When -n=1, only a single lock holder or leader exists providing mutual
|
|
|
|
exclusion. Setting a higher value switches to a semaphore allowing multiple
|
|
|
|
holders to coordinate.
|
|
|
|
|
|
|
|
The prefix provided must have write privileges.
|
2017-10-12 21:02:45 +00:00
|
|
|
`
|