2014-09-01 04:50:41 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-09-01 21:44:41 +00:00
|
|
|
"unicode"
|
2014-09-01 04:50:41 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2014-09-01 04:50:41 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-09-01 18:30:56 +00:00
|
|
|
// rExecPrefix is the prefix in the KV store used to
|
|
|
|
// store the remote exec data
|
|
|
|
rExecPrefix = "_rexec"
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
// rExecFileName is the name of the file we append to
|
|
|
|
// the path, e.g. _rexec/session_id/job
|
|
|
|
rExecFileName = "job"
|
|
|
|
|
|
|
|
// rExecAck is the suffix added to an ack path
|
|
|
|
rExecAckSuffix = "/ack"
|
|
|
|
|
|
|
|
// rExecAck is the suffix added to an exit code
|
|
|
|
rExecExitSuffix = "/exit"
|
|
|
|
|
|
|
|
// rExecOutputDivider is used to namespace the output
|
|
|
|
rExecOutputDivider = "/out/"
|
2014-09-01 20:13:24 +00:00
|
|
|
|
|
|
|
// rExecReplicationWait is how long we wait for replication
|
|
|
|
rExecReplicationWait = 200 * time.Millisecond
|
|
|
|
|
|
|
|
// rExecQuietWait is how long we wait for no responses
|
|
|
|
// before assuming the job is done.
|
|
|
|
rExecQuietWait = 2 * time.Second
|
2015-01-07 22:22:40 +00:00
|
|
|
|
2015-01-07 22:27:29 +00:00
|
|
|
// rExecTTL is how long we default the session TTL to
|
|
|
|
rExecTTL = "15s"
|
2015-01-07 22:22:40 +00:00
|
|
|
|
|
|
|
// rExecRenewInterval is how often we renew the session TTL
|
|
|
|
// when doing an exec in a foreign DC.
|
|
|
|
rExecRenewInterval = 5 * time.Second
|
2014-09-01 04:50:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rExecConf is used to pass around configuration
|
|
|
|
type rExecConf struct {
|
2017-02-08 21:57:46 +00:00
|
|
|
prefix string
|
2014-09-01 04:50:41 +00:00
|
|
|
|
2015-01-07 22:22:40 +00:00
|
|
|
foreignDC bool
|
|
|
|
localDC string
|
|
|
|
localNode string
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
node string
|
|
|
|
service string
|
|
|
|
tag string
|
|
|
|
|
|
|
|
wait time.Duration
|
|
|
|
replWait time.Duration
|
|
|
|
|
|
|
|
cmd string
|
|
|
|
script []byte
|
|
|
|
|
|
|
|
verbose bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecEvent is the event we broadcast using a user-event
|
|
|
|
type rExecEvent struct {
|
|
|
|
Prefix string
|
|
|
|
Session string
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecSpec is the file we upload to specify the parameters
|
|
|
|
// of the remote execution.
|
|
|
|
type rExecSpec struct {
|
|
|
|
// Command is a single command to run directly in the shell
|
|
|
|
Command string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Script should be spilled to a file and executed
|
|
|
|
Script []byte `json:",omitempty"`
|
|
|
|
|
|
|
|
// Wait is how long we are waiting on a quiet period to terminate
|
|
|
|
Wait time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecAck is used to transmit an acknowledgement
|
|
|
|
type rExecAck struct {
|
|
|
|
Node string
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecHeart is used to transmit a heartbeat
|
|
|
|
type rExecHeart struct {
|
|
|
|
Node string
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecOutput is used to transmit a chunk of output
|
|
|
|
type rExecOutput struct {
|
|
|
|
Node string
|
|
|
|
Output []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// rExecExit is used to transmit an exit code
|
|
|
|
type rExecExit struct {
|
|
|
|
Node string
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecCommand is a Command implementation that is used to
|
|
|
|
// do remote execution of commands
|
|
|
|
type ExecCommand struct {
|
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
|
|
|
BaseCommand
|
2017-02-08 21:57:46 +00:00
|
|
|
|
2014-09-01 05:46:08 +00:00
|
|
|
ShutdownCh <-chan struct{}
|
2014-09-01 04:50:41 +00:00
|
|
|
conf rExecConf
|
|
|
|
client *consulapi.Client
|
|
|
|
sessionID string
|
2015-01-07 22:22:40 +00:00
|
|
|
stopCh chan struct{}
|
2014-09-01 04:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExecCommand) Run(args []string) int {
|
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
|
|
|
f := c.BaseCommand.NewFlagSet(c)
|
2017-02-08 21:57:46 +00:00
|
|
|
f.StringVar(&c.conf.node, "node", "",
|
|
|
|
"Regular expression to filter on node names.")
|
|
|
|
f.StringVar(&c.conf.service, "service", "",
|
|
|
|
"Regular expression to filter on service instances.")
|
|
|
|
f.StringVar(&c.conf.tag, "tag", "",
|
|
|
|
"Regular expression to filter on service tags. Must be used with -service.")
|
|
|
|
f.StringVar(&c.conf.prefix, "prefix", rExecPrefix,
|
|
|
|
"Prefix in the KV store to use for request data.")
|
|
|
|
f.DurationVar(&c.conf.wait, "wait", rExecQuietWait,
|
|
|
|
"Period to wait with no responses before terminating execution.")
|
|
|
|
f.DurationVar(&c.conf.replWait, "wait-repl", rExecReplicationWait,
|
|
|
|
"Period to wait for replication before firing event. This is an "+
|
|
|
|
"optimization to allow stale reads to be performed.")
|
|
|
|
f.BoolVar(&c.conf.verbose, "verbose", false,
|
|
|
|
"Enables verbose output.")
|
|
|
|
|
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
|
|
|
if err := c.BaseCommand.Parse(args); err != nil {
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join the commands to execute
|
2017-02-08 21:57:46 +00:00
|
|
|
c.conf.cmd = strings.Join(f.Args(), " ")
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
// If there is no command, read stdin for a script input
|
2014-09-01 20:02:15 +00:00
|
|
|
if c.conf.cmd == "-" {
|
|
|
|
c.conf.cmd = ""
|
2014-09-01 04:50:41 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err := io.Copy(&buf, os.Stdin)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read stdin: %v", err))
|
|
|
|
c.UI.Error("")
|
|
|
|
c.UI.Error(c.Help())
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.conf.script = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a command or script
|
|
|
|
if c.conf.cmd == "" && len(c.conf.script) == 0 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Must specify a command to execute")
|
|
|
|
c.UI.Error("")
|
|
|
|
c.UI.Error(c.Help())
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the configuration
|
|
|
|
if err := c.conf.validate(); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(err.Error())
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and test the HTTP client
|
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
|
|
|
client, err := c.BaseCommand.HTTPClient()
|
2014-09-01 04:50:41 +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))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
2015-01-07 22:22:40 +00:00
|
|
|
info, err := client.Agent().Self()
|
2014-09-01 04:50:41 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.client = client
|
|
|
|
|
2015-01-07 22:22:40 +00:00
|
|
|
// Check if this is a foreign datacenter
|
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
|
|
|
if c.BaseCommand.HTTPDatacenter() != "" && c.BaseCommand.HTTPDatacenter() != info["Config"]["Datacenter"] {
|
2015-01-07 22:22:40 +00:00
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info("Remote exec in foreign datacenter, using Session TTL")
|
2015-01-07 22:22:40 +00:00
|
|
|
}
|
|
|
|
c.conf.foreignDC = true
|
|
|
|
c.conf.localDC = info["Config"]["Datacenter"].(string)
|
|
|
|
c.conf.localNode = info["Config"]["NodeName"].(string)
|
|
|
|
}
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
// Create the job spec
|
|
|
|
spec, err := c.makeRExecSpec()
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to create job spec: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a session for this
|
|
|
|
c.sessionID, err = c.createSession()
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to create session: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer c.destroySession()
|
2014-09-01 21:44:41 +00:00
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Created remote execution session: %s", c.sessionID))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
// Upload the payload
|
|
|
|
if err := c.uploadPayload(spec); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to create job file: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer c.destroyData()
|
2014-09-01 21:44:41 +00:00
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Uploaded remote execution spec"))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
// Wait for replication. This is done so that when the event is
|
|
|
|
// received, the job file can be read using a stale read. If the
|
|
|
|
// stale read fails, we expect a consistent read to be done, so
|
|
|
|
// largely this is a heuristic.
|
|
|
|
select {
|
|
|
|
case <-time.After(c.conf.replWait):
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the event
|
|
|
|
id, err := c.fireEvent()
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to fire event: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Fired remote execution event: %s", id))
|
2014-09-01 04:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the job to finish now
|
|
|
|
return c.waitForJob()
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForJob is used to poll for results and wait until the job is terminated
|
|
|
|
func (c *ExecCommand) waitForJob() int {
|
|
|
|
// Although the session destroy is already deferred, we do it again here,
|
|
|
|
// because invalidation of the session before destroyData() ensures there is
|
|
|
|
// no race condition allowing an agent to upload data (the acquire will fail).
|
|
|
|
defer c.destroySession()
|
|
|
|
start := time.Now()
|
|
|
|
ackCh := make(chan rExecAck, 128)
|
|
|
|
heartCh := make(chan rExecHeart, 128)
|
|
|
|
outputCh := make(chan rExecOutput, 128)
|
|
|
|
exitCh := make(chan rExecExit, 128)
|
|
|
|
doneCh := make(chan struct{})
|
2014-09-01 05:46:08 +00:00
|
|
|
errCh := make(chan struct{}, 1)
|
2014-09-01 04:50:41 +00:00
|
|
|
defer close(doneCh)
|
2014-09-01 05:46:08 +00:00
|
|
|
go c.streamResults(doneCh, ackCh, heartCh, outputCh, exitCh, errCh)
|
2017-04-21 00:02:42 +00:00
|
|
|
target := &TargetedUI{UI: c.UI}
|
2014-09-01 21:44:41 +00:00
|
|
|
|
|
|
|
var ackCount, exitCount, badExit int
|
2014-09-01 04:50:41 +00:00
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
// Determine wait time. We provide a larger window if we know about
|
|
|
|
// nodes which are still working.
|
|
|
|
waitIntv := c.conf.wait
|
|
|
|
if ackCount > exitCount {
|
2014-09-01 20:13:24 +00:00
|
|
|
waitIntv *= 2
|
2014-09-01 04:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-ackCh:
|
|
|
|
ackCount++
|
2014-09-01 21:44:41 +00:00
|
|
|
if c.conf.verbose {
|
|
|
|
target.Target = e.Node
|
|
|
|
target.Info("acknowledged")
|
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
|
2014-09-01 20:02:15 +00:00
|
|
|
case h := <-heartCh:
|
|
|
|
if c.conf.verbose {
|
2014-09-01 21:44:41 +00:00
|
|
|
target.Target = h.Node
|
|
|
|
target.Info("heartbeat received")
|
2014-09-01 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
case e := <-outputCh:
|
2014-09-01 21:44:41 +00:00
|
|
|
target.Target = e.Node
|
|
|
|
target.Output(string(e.Output))
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
case e := <-exitCh:
|
|
|
|
exitCount++
|
2014-09-01 21:44:41 +00:00
|
|
|
target.Target = e.Node
|
|
|
|
target.Info(fmt.Sprintf("finished with exit code %d", e.Code))
|
|
|
|
if e.Code != 0 {
|
|
|
|
badExit++
|
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
case <-time.After(waitIntv):
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("%d / %d node(s) completed / acknowledged", exitCount, ackCount))
|
2014-09-01 21:44:41 +00:00
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Completed in %0.2f seconds",
|
2014-09-01 21:44:41 +00:00
|
|
|
float64(time.Now().Sub(start))/float64(time.Second)))
|
|
|
|
}
|
2017-04-13 00:51:16 +00:00
|
|
|
if exitCount < ackCount {
|
|
|
|
badExit++
|
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
break OUTER
|
|
|
|
|
2014-09-01 05:46:08 +00:00
|
|
|
case <-errCh:
|
|
|
|
return 1
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
case <-c.ShutdownCh:
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2014-09-01 21:44:41 +00:00
|
|
|
|
|
|
|
if badExit > 0 {
|
|
|
|
return 2
|
|
|
|
}
|
2014-09-01 04:50:41 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// streamResults is used to perform blocking queries against the KV endpoint and stream in
|
|
|
|
// notice of various events into waitForJob
|
|
|
|
func (c *ExecCommand) streamResults(doneCh chan struct{}, ackCh chan rExecAck, heartCh chan rExecHeart,
|
2014-09-01 05:46:08 +00:00
|
|
|
outputCh chan rExecOutput, exitCh chan rExecExit, errCh chan struct{}) {
|
2014-09-01 04:50:41 +00:00
|
|
|
kv := c.client.KV()
|
|
|
|
opts := consulapi.QueryOptions{WaitTime: c.conf.wait}
|
|
|
|
dir := path.Join(c.conf.prefix, c.sessionID) + "/"
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Check if we've been signaled to exit
|
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block on waiting for new keys
|
|
|
|
keys, qm, err := kv.Keys(dir, "", &opts)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read results: %s", err))
|
2014-09-01 04:50:41 +00:00
|
|
|
goto ERR_EXIT
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path the no-change case
|
|
|
|
if qm.LastIndex == opts.WaitIndex {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
opts.WaitIndex = qm.LastIndex
|
|
|
|
|
|
|
|
// Handle each key
|
|
|
|
for _, key := range keys {
|
|
|
|
// Ignore if we've seen it
|
|
|
|
if _, ok := seen[key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[key] = struct{}{}
|
|
|
|
|
|
|
|
// Trim the directory
|
|
|
|
full := key
|
|
|
|
key = strings.TrimPrefix(key, dir)
|
|
|
|
|
|
|
|
// Handle the key type
|
|
|
|
switch {
|
|
|
|
case key == rExecFileName:
|
|
|
|
continue
|
|
|
|
case strings.HasSuffix(key, rExecAckSuffix):
|
|
|
|
ackCh <- rExecAck{Node: strings.TrimSuffix(key, rExecAckSuffix)}
|
|
|
|
|
|
|
|
case strings.HasSuffix(key, rExecExitSuffix):
|
|
|
|
pair, _, err := kv.Get(full, nil)
|
2015-02-16 22:32:50 +00:00
|
|
|
if err != nil || pair == nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read key '%s': %v", full, err))
|
2014-09-01 04:50:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
code, err := strconv.ParseInt(string(pair.Value), 10, 32)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse exit code '%s': %v", pair.Value, err))
|
2014-09-01 04:50:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
exitCh <- rExecExit{
|
|
|
|
Node: strings.TrimSuffix(key, rExecExitSuffix),
|
|
|
|
Code: int(code),
|
|
|
|
}
|
|
|
|
|
|
|
|
case strings.LastIndex(key, rExecOutputDivider) != -1:
|
|
|
|
pair, _, err := kv.Get(full, nil)
|
2015-02-16 22:32:50 +00:00
|
|
|
if err != nil || pair == nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read key '%s': %v", full, err))
|
2014-09-01 04:50:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
idx := strings.LastIndex(key, rExecOutputDivider)
|
|
|
|
node := key[:idx]
|
|
|
|
if len(pair.Value) == 0 {
|
|
|
|
heartCh <- rExecHeart{Node: node}
|
|
|
|
} else {
|
|
|
|
outputCh <- rExecOutput{Node: node, Output: pair.Value}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Unknown key '%s', ignoring.", key))
|
2014-09-01 04:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_EXIT:
|
|
|
|
select {
|
2014-09-01 05:46:08 +00:00
|
|
|
case errCh <- struct{}{}:
|
2014-09-01 04:50:41 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate checks that the configuration is sane
|
|
|
|
func (conf *rExecConf) validate() error {
|
|
|
|
// Validate the filters
|
|
|
|
if conf.node != "" {
|
|
|
|
if _, err := regexp.Compile(conf.node); err != nil {
|
|
|
|
return fmt.Errorf("Failed to compile node filter regexp: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.service != "" {
|
|
|
|
if _, err := regexp.Compile(conf.service); err != nil {
|
|
|
|
return fmt.Errorf("Failed to compile service filter regexp: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.tag != "" {
|
|
|
|
if _, err := regexp.Compile(conf.tag); err != nil {
|
|
|
|
return fmt.Errorf("Failed to compile tag filter regexp: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.tag != "" && conf.service == "" {
|
|
|
|
return fmt.Errorf("Cannot provide tag filter without service filter.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createSession is used to create a new session for this command
|
|
|
|
func (c *ExecCommand) createSession() (string, error) {
|
2015-01-07 22:27:29 +00:00
|
|
|
var id string
|
|
|
|
var err error
|
2015-01-07 22:22:40 +00:00
|
|
|
if c.conf.foreignDC {
|
2015-01-07 22:27:29 +00:00
|
|
|
id, err = c.createSessionForeign()
|
|
|
|
} else {
|
|
|
|
id, err = c.createSessionLocal()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
c.stopCh = make(chan struct{})
|
|
|
|
go c.renewSession(id, c.stopCh)
|
2015-01-07 22:22:40 +00:00
|
|
|
}
|
2015-01-07 22:27:29 +00:00
|
|
|
return id, err
|
2015-01-07 22:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// createSessionLocal is used to create a new session in a local datacenter
|
|
|
|
// This is simpler since we can use the local agent to create the session.
|
|
|
|
func (c *ExecCommand) createSessionLocal() (string, error) {
|
2014-09-01 04:50:41 +00:00
|
|
|
session := c.client.Session()
|
|
|
|
se := consulapi.SessionEntry{
|
2015-01-07 22:22:40 +00:00
|
|
|
Name: "Remote Exec",
|
|
|
|
Behavior: consulapi.SessionBehaviorDelete,
|
2015-01-07 22:27:29 +00:00
|
|
|
TTL: rExecTTL,
|
2014-09-01 04:50:41 +00:00
|
|
|
}
|
|
|
|
id, _, err := session.Create(&se, nil)
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2015-01-07 22:22:40 +00:00
|
|
|
// createSessionLocal is used to create a new session in a foreign datacenter
|
|
|
|
// This is more complex since the local agent cannot be used to create
|
|
|
|
// a session, and we must associate with a node in the remote datacenter.
|
|
|
|
func (c *ExecCommand) createSessionForeign() (string, error) {
|
|
|
|
// Look for a remote node to bind to
|
|
|
|
health := c.client.Health()
|
|
|
|
services, _, err := health.Service("consul", "", true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to find Consul server in remote datacenter: %v", err)
|
|
|
|
}
|
|
|
|
if len(services) == 0 {
|
|
|
|
return "", fmt.Errorf("Failed to find Consul server in remote datacenter")
|
|
|
|
}
|
|
|
|
node := services[0].Node.Node
|
|
|
|
if c.conf.verbose {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Binding session to remote node %s@%s",
|
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
|
|
|
node, c.BaseCommand.HTTPDatacenter()))
|
2015-01-07 22:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session := c.client.Session()
|
|
|
|
se := consulapi.SessionEntry{
|
|
|
|
Name: fmt.Sprintf("Remote Exec via %s@%s", c.conf.localNode, c.conf.localDC),
|
|
|
|
Node: node,
|
|
|
|
Checks: []string{},
|
|
|
|
Behavior: consulapi.SessionBehaviorDelete,
|
2015-01-07 22:27:29 +00:00
|
|
|
TTL: rExecTTL,
|
2015-01-07 22:22:40 +00:00
|
|
|
}
|
|
|
|
id, _, err := session.CreateNoChecks(&se, nil)
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// renewSession is a long running routine that periodically renews
|
|
|
|
// the session TTL. This is used for foreign sessions where we depend
|
|
|
|
// on TTLs.
|
|
|
|
func (c *ExecCommand) renewSession(id string, stopCh chan struct{}) {
|
|
|
|
session := c.client.Session()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(rExecRenewInterval):
|
|
|
|
_, _, err := session.Renew(id, nil)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Session renew failed: %v", err))
|
2015-01-07 22:22:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
// destroySession is used to destroy the associated session
|
|
|
|
func (c *ExecCommand) destroySession() error {
|
2015-01-07 22:22:40 +00:00
|
|
|
// Stop the session renew if any
|
|
|
|
if c.stopCh != nil {
|
|
|
|
close(c.stopCh)
|
|
|
|
c.stopCh = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the session explicitly
|
2014-09-01 04:50:41 +00:00
|
|
|
session := c.client.Session()
|
|
|
|
_, err := session.Destroy(c.sessionID, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeRExecSpec creates a serialized job specification
|
|
|
|
// that can be uploaded which will be parsed by agents to
|
|
|
|
// determine what to do.
|
|
|
|
func (c *ExecCommand) makeRExecSpec() ([]byte, error) {
|
|
|
|
spec := &rExecSpec{
|
|
|
|
Command: c.conf.cmd,
|
|
|
|
Script: c.conf.script,
|
|
|
|
Wait: c.conf.wait,
|
|
|
|
}
|
|
|
|
return json.Marshal(spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadPayload is used to upload the request payload
|
|
|
|
func (c *ExecCommand) uploadPayload(payload []byte) error {
|
|
|
|
kv := c.client.KV()
|
|
|
|
pair := consulapi.KVPair{
|
|
|
|
Key: path.Join(c.conf.prefix, c.sessionID, rExecFileName),
|
|
|
|
Value: payload,
|
|
|
|
Session: c.sessionID,
|
|
|
|
}
|
|
|
|
ok, _, err := kv.Acquire(&pair, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to acquire key %s", pair.Key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroyData is used to nuke all the data associated with
|
|
|
|
// this remote exec. We just do a recursive delete of our
|
|
|
|
// data directory.
|
|
|
|
func (c *ExecCommand) destroyData() error {
|
|
|
|
kv := c.client.KV()
|
|
|
|
dir := path.Join(c.conf.prefix, c.sessionID)
|
|
|
|
_, err := kv.DeleteTree(dir, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fireEvent is used to fire the event that will notify nodes
|
|
|
|
// about the remote execution. Returns the event ID or error
|
|
|
|
func (c *ExecCommand) fireEvent() (string, error) {
|
|
|
|
// Create the user event payload
|
|
|
|
msg := &rExecEvent{
|
|
|
|
Prefix: c.conf.prefix,
|
|
|
|
Session: c.sessionID,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the user event
|
|
|
|
event := c.client.Event()
|
|
|
|
params := &consulapi.UserEvent{
|
|
|
|
Name: "_rexec",
|
|
|
|
Payload: buf,
|
|
|
|
NodeFilter: c.conf.node,
|
|
|
|
ServiceFilter: c.conf.service,
|
|
|
|
TagFilter: c.conf.tag,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the event
|
|
|
|
id, _, err := event.Fire(params, nil)
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExecCommand) Synopsis() string {
|
|
|
|
return "Executes a command on Consul nodes"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExecCommand) Help() string {
|
|
|
|
helpText := `
|
2014-09-01 20:02:15 +00:00
|
|
|
Usage: consul exec [options] [-|command...]
|
2014-09-01 04:50:41 +00:00
|
|
|
|
|
|
|
Evaluates a command on remote Consul nodes. The nodes responding can
|
|
|
|
be filtered using regular expressions on node name, service, and tag
|
2014-09-01 20:02:15 +00:00
|
|
|
definitions. If a command is '-', stdin will be read until EOF
|
2014-09-01 04:50:41 +00:00
|
|
|
and used as a script input.
|
|
|
|
|
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
|
|
|
` + c.BaseCommand.Help()
|
2017-02-08 21:57:46 +00:00
|
|
|
|
2014-09-01 04:50:41 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2014-09-01 21:44:41 +00:00
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
// TargetedUI is a UI that wraps another UI implementation and modifies
|
2014-09-01 21:44:41 +00:00
|
|
|
// the output to indicate a specific target. Specifically, all Say output
|
|
|
|
// is prefixed with the target name. Message output is not prefixed but
|
|
|
|
// is offset by the length of the target so that output is lined up properly
|
|
|
|
// with Say output. Machine-readable output has the proper target set.
|
2017-04-21 00:02:42 +00:00
|
|
|
type TargetedUI struct {
|
2014-09-01 21:44:41 +00:00
|
|
|
Target string
|
2017-04-21 00:02:42 +00:00
|
|
|
UI cli.Ui
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
func (u *TargetedUI) Ask(query string) (string, error) {
|
|
|
|
return u.UI.Ask(u.prefixLines(true, query))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
func (u *TargetedUI) Info(message string) {
|
|
|
|
u.UI.Info(u.prefixLines(true, message))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
func (u *TargetedUI) Output(message string) {
|
|
|
|
u.UI.Output(u.prefixLines(false, message))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
func (u *TargetedUI) Error(message string) {
|
|
|
|
u.UI.Error(u.prefixLines(true, message))
|
2014-09-01 21:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
func (u *TargetedUI) prefixLines(arrow bool, message string) string {
|
2014-09-01 21:44:41 +00:00
|
|
|
arrowText := "==>"
|
|
|
|
if !arrow {
|
|
|
|
arrowText = strings.Repeat(" ", len(arrowText))
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
|
|
|
|
for _, line := range strings.Split(message, "\n") {
|
|
|
|
result.WriteString(fmt.Sprintf("%s %s: %s\n", arrowText, u.Target, line))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimRightFunc(result.String(), unicode.IsSpace)
|
|
|
|
}
|