2017-10-17 10:55:04 +00:00
|
|
|
package agent
|
2013-12-19 20:18:06 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-19 19:16:00 +00:00
|
|
|
"context"
|
2017-10-17 10:55:04 +00:00
|
|
|
"flag"
|
2013-12-20 01:14:46 +00:00
|
|
|
"fmt"
|
2021-06-23 16:45:40 +00:00
|
|
|
"io/ioutil"
|
2013-12-21 00:39:32 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2014-09-02 21:23:43 +00:00
|
|
|
"path/filepath"
|
2013-12-19 20:18:06 +00:00
|
|
|
"strings"
|
2013-12-21 00:39:32 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
"github.com/hashicorp/go-checkpoint"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-06-02 21:12:44 +00:00
|
|
|
mcli "github.com/mitchellh/cli"
|
2020-12-21 18:25:32 +00:00
|
|
|
|
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-09-25 18:40:42 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2021-06-02 21:12:44 +00:00
|
|
|
"github.com/hashicorp/consul/command/cli"
|
2017-10-17 10:55:04 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
2018-06-20 18:30:17 +00:00
|
|
|
"github.com/hashicorp/consul/service_os"
|
2021-06-02 20:29:29 +00:00
|
|
|
consulversion "github.com/hashicorp/consul/version"
|
2013-12-19 20:18:06 +00:00
|
|
|
)
|
|
|
|
|
2021-06-02 20:29:29 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
2022-03-18 16:55:12 +00:00
|
|
|
buildDate, err := time.Parse(time.RFC3339, consulversion.BuildDate)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Fatal error with internal time set; check makefile for build date %v %v \n", buildDate, err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
c := &cmd{
|
2021-06-02 21:12:44 +00:00
|
|
|
ui: ui,
|
2021-06-02 20:29:29 +00:00
|
|
|
revision: consulversion.GitCommit,
|
|
|
|
version: consulversion.Version,
|
|
|
|
versionPrerelease: consulversion.VersionPrerelease,
|
|
|
|
versionHuman: consulversion.GetHumanVersion(),
|
2022-03-18 16:55:12 +00:00
|
|
|
buildDate: buildDate,
|
2020-12-21 21:18:03 +00:00
|
|
|
flags: flag.NewFlagSet("", flag.ContinueOnError),
|
2017-10-17 10:55:04 +00:00
|
|
|
}
|
2020-12-21 21:18:03 +00:00
|
|
|
config.AddFlags(c.flags, &c.configLoadOpts)
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-10-17 10:55:04 +00:00
|
|
|
return c
|
|
|
|
}
|
2014-06-09 18:57:15 +00:00
|
|
|
|
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
|
|
|
// AgentCommand is a Command implementation that runs a Consul agent.
|
2013-12-19 20:18:06 +00:00
|
|
|
// The command will not end unless a shutdown message is sent on the
|
|
|
|
// ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly
|
|
|
|
// exit.
|
2017-10-17 10:55:04 +00:00
|
|
|
type cmd struct {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui cli.Ui
|
2017-10-17 10:55:04 +00:00
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2017-10-17 10:55:04 +00:00
|
|
|
revision string
|
|
|
|
version string
|
|
|
|
versionPrerelease string
|
|
|
|
versionHuman string
|
2022-03-18 16:55:12 +00:00
|
|
|
buildDate time.Time
|
2020-12-21 18:25:32 +00:00
|
|
|
configLoadOpts config.LoadOpts
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.InterceptLogger
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
code := c.run(args)
|
|
|
|
if c.logger != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Exit code", "code", code)
|
2017-10-17 10:55:04 +00:00
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|
2017-05-10 15:45:17 +00:00
|
|
|
|
2014-09-02 21:23:43 +00:00
|
|
|
// checkpointResults is used to handler periodic results from our update checker
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) checkpointResults(results *checkpoint.CheckResponse, err error) {
|
2014-09-02 21:23:43 +00:00
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Failed to check for updates", "error", err)
|
2014-09-02 21:23:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if results.Outdated {
|
2020-03-17 14:53:15 +00:00
|
|
|
c.logger.Info("Newer Consul version available", "new_version", results.CurrentVersion, "current_version", c.version)
|
2014-09-02 21:23:43 +00:00
|
|
|
}
|
|
|
|
for _, alert := range results.Alerts {
|
|
|
|
switch alert.Level {
|
|
|
|
case "info":
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Bulletin", "alert_level", alert.Level, "alert_message", alert.Message, "alert_URL", alert.URL)
|
2014-09-02 21:23:43 +00:00
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Bulletin", "alert_level", alert.Level, "alert_message", alert.Message, "alert_URL", alert.URL)
|
2014-09-02 21:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) startupUpdateCheck(config *config.RuntimeConfig) {
|
2017-06-02 13:29:40 +00:00
|
|
|
version := config.Version
|
|
|
|
if config.VersionPrerelease != "" {
|
|
|
|
version += fmt.Sprintf("-%s", config.VersionPrerelease)
|
|
|
|
}
|
|
|
|
updateParams := &checkpoint.CheckParams{
|
|
|
|
Product: "consul",
|
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
if !config.DisableAnonymousSignature {
|
|
|
|
updateParams.SignatureFile = filepath.Join(config.DataDir, "checkpoint-signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule a periodic check with expected interval of 24 hours
|
2017-10-17 10:55:04 +00:00
|
|
|
checkpoint.CheckInterval(updateParams, 24*time.Hour, c.checkpointResults)
|
2017-06-02 13:29:40 +00:00
|
|
|
|
|
|
|
// Do an immediate check within the next 30 seconds
|
|
|
|
go func() {
|
|
|
|
time.Sleep(lib.RandomStagger(30 * time.Second))
|
2017-10-17 10:55:04 +00:00
|
|
|
c.checkpointResults(checkpoint.Check(updateParams))
|
2017-06-02 13:29:40 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-04-11 23:59:16 +00:00
|
|
|
// startupJoin is invoked to handle any joins specified to take place at start time
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) startupJoin(agent *agent.Agent, cfg *config.RuntimeConfig) error {
|
2017-09-25 18:40:42 +00:00
|
|
|
if len(cfg.StartJoinAddrsLAN) == 0 {
|
2014-04-11 23:59:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
c.logger.Info("Joining cluster")
|
2021-10-26 20:08:55 +00:00
|
|
|
// NOTE: For partitioned servers you are only capable of using start join
|
|
|
|
// to join nodes in the default partition.
|
|
|
|
n, err := agent.JoinLAN(cfg.StartJoinAddrsLAN, agent.AgentEnterpriseMeta())
|
2014-04-11 23:59:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Join completed. Initial agents synced with", "agent_count", n)
|
2014-04-11 23:59:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:14:59 +00:00
|
|
|
// startupJoinWan is invoked to handle any joins -wan specified to take place at start time
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) startupJoinWan(agent *agent.Agent, cfg *config.RuntimeConfig) error {
|
2017-09-25 18:40:42 +00:00
|
|
|
if len(cfg.StartJoinAddrsWAN) == 0 {
|
2014-11-14 15:02:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
c.logger.Info("Joining wan cluster")
|
2017-09-25 18:40:42 +00:00
|
|
|
n, err := agent.JoinWAN(cfg.StartJoinAddrsWAN)
|
2014-11-14 15:02:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
c.logger.Info("Join wan completed. Initial agents synced with", "agent_count", n)
|
2014-11-14 15:02:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
func (c *cmd) run(args []string) int {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui := &mcli.PrefixedUi{
|
|
|
|
OutputPrefix: "==> ",
|
|
|
|
InfoPrefix: " ",
|
|
|
|
ErrorPrefix: "==> ",
|
|
|
|
Ui: c.ui,
|
|
|
|
}
|
|
|
|
|
2018-05-31 23:59:51 +00:00
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "help requested") {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui.Error(fmt.Sprintf("error parsing flags: %v", err))
|
2018-05-31 23:59:51 +00:00
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-01 22:17:28 +00:00
|
|
|
if len(c.flags.Args()) > 0 {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui.Error(fmt.Sprintf("Unexpected extra arguments: %v", c.flags.Args()))
|
2020-05-01 22:17:28 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:12:44 +00:00
|
|
|
// FIXME: logs should always go to stderr, but previously they were sent to
|
|
|
|
// stdout, so continue to use Stdout for now, and fix this in a future release.
|
|
|
|
logGate := &logging.GatedWriter{Writer: c.ui.Stdout()}
|
2020-12-21 18:25:32 +00:00
|
|
|
loader := func(source config.Source) (config.LoadResult, error) {
|
|
|
|
c.configLoadOpts.DefaultConfig = source
|
|
|
|
return config.Load(c.configLoadOpts)
|
2020-08-08 01:08:43 +00:00
|
|
|
}
|
|
|
|
bd, err := agent.NewBaseDeps(loader, logGate)
|
|
|
|
if err != nil {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui.Error(err.Error())
|
2020-08-08 01:08:43 +00:00
|
|
|
return 1
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
2020-08-08 01:08:43 +00:00
|
|
|
c.logger = bd.Logger
|
|
|
|
agent, err := agent.New(bd)
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
2021-06-02 21:12:44 +00:00
|
|
|
ui.Error(err.Error())
|
2013-12-21 00:39:32 +00:00
|
|
|
return 1
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-08-08 01:08:43 +00:00
|
|
|
config := bd.RuntimeConfig
|
2021-06-02 21:26:50 +00:00
|
|
|
if config.Logging.LogJSON {
|
|
|
|
// Hide all non-error output when JSON logging is enabled.
|
|
|
|
ui.Ui = &cli.BasicUI{
|
2021-06-23 16:45:40 +00:00
|
|
|
BasicUi: mcli.BasicUi{ErrorWriter: c.ui.Stderr(), Writer: ioutil.Discard},
|
2021-06-02 21:26:50 +00:00
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2021-06-02 21:26:50 +00:00
|
|
|
ui.Output("Starting Consul agent...")
|
2017-06-02 13:35:06 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
segment := config.SegmentName
|
|
|
|
if config.ServerMode {
|
|
|
|
segment = "<all>"
|
|
|
|
}
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Version: '%s'", c.versionHuman))
|
2022-06-02 02:29:40 +00:00
|
|
|
if strings.Contains(c.versionHuman, "dev") {
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Revision: '%s'", c.revision))
|
2022-06-02 02:29:40 +00:00
|
|
|
}
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Build Date: '%s'", c.buildDate))
|
|
|
|
ui.Info(fmt.Sprintf(" Node ID: '%s'", config.NodeID))
|
|
|
|
ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName))
|
2021-07-08 15:03:38 +00:00
|
|
|
if ap := config.PartitionOrEmpty(); ap != "" {
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Partition: '%s'", ap))
|
2021-07-08 15:03:38 +00:00
|
|
|
}
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Datacenter: '%s' (Segment: '%s')", config.Datacenter, segment))
|
|
|
|
ui.Info(fmt.Sprintf(" Server: %v (Bootstrap: %v)", config.ServerMode, config.Bootstrap))
|
|
|
|
ui.Info(fmt.Sprintf(" Client Addr: %v (HTTP: %d, HTTPS: %d, gRPC: %d, DNS: %d)", config.ClientAddrs,
|
2021-09-08 15:48:41 +00:00
|
|
|
config.HTTPPort, config.HTTPSPort, config.GRPCPort, config.DNSPort))
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Cluster Addr: %v (LAN: %d, WAN: %d)", config.AdvertiseAddrLAN,
|
2019-06-27 20:22:07 +00:00
|
|
|
config.SerfPortLAN, config.SerfPortWAN))
|
2022-08-09 11:54:49 +00:00
|
|
|
ui.Info(fmt.Sprintf("Gossip Encryption: %t", config.EncryptKey != ""))
|
|
|
|
ui.Info(fmt.Sprintf(" Auto-Encrypt-TLS: %t", config.AutoEncryptTLS || config.AutoEncryptAllowTLS))
|
|
|
|
ui.Info(fmt.Sprintf(" HTTPS TLS: Verify Incoming: %t, Verify Outgoing: %t, Min Version: %s",
|
|
|
|
config.TLS.HTTPS.VerifyIncoming, config.TLS.HTTPS.VerifyOutgoing, config.TLS.HTTPS.TLSMinVersion))
|
|
|
|
ui.Info(fmt.Sprintf(" gRPC TLS: Verify Incoming: %t, Min Version: %s", config.TLS.GRPC.VerifyIncoming, config.TLS.GRPC.TLSMinVersion))
|
|
|
|
ui.Info(fmt.Sprintf(" Internal RPC TLS: Verify Incoming: %t, Verify Outgoing: %t (Verify Hostname: %t), Min Version: %s",
|
|
|
|
config.TLS.InternalRPC.VerifyIncoming, config.TLS.InternalRPC.VerifyOutgoing, config.TLS.InternalRPC.VerifyServerHostname, config.TLS.InternalRPC.TLSMinVersion))
|
2019-06-27 20:22:07 +00:00
|
|
|
// Enable log streaming
|
2021-06-02 21:26:50 +00:00
|
|
|
ui.Output("")
|
|
|
|
ui.Output("Log data will now stream in as it occurs:\n")
|
2019-06-27 20:22:07 +00:00
|
|
|
logGate.Flush()
|
|
|
|
|
|
|
|
// wait for signal
|
|
|
|
signalCh := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)
|
|
|
|
|
2020-06-19 19:16:00 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
var sig os.Signal
|
|
|
|
select {
|
|
|
|
case s := <-signalCh:
|
|
|
|
sig = s
|
2020-06-19 19:16:00 +00:00
|
|
|
case <-ctx.Done():
|
2019-06-27 20:22:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGPIPE:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
err := fmt.Errorf("cannot reload before agent started")
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Caught", "signal", sig, "error", err)
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Caught", "signal", sig)
|
2020-06-19 19:16:00 +00:00
|
|
|
cancel()
|
2019-06-27 20:22:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-19 19:16:00 +00:00
|
|
|
err = agent.Start(ctx)
|
2019-06-27 20:22:07 +00:00
|
|
|
signal.Stop(signalCh)
|
2020-06-19 19:16:00 +00:00
|
|
|
cancel()
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Error starting agent", "error", err)
|
2013-12-20 01:14:46 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-06-20 07:29:20 +00:00
|
|
|
|
|
|
|
// shutdown agent before endpoints
|
|
|
|
defer agent.ShutdownEndpoints()
|
|
|
|
defer agent.ShutdownAgent()
|
2017-05-19 09:53:41 +00:00
|
|
|
|
2019-05-07 15:15:34 +00:00
|
|
|
if !config.DisableUpdateCheck && !config.DevMode {
|
2017-10-17 10:55:04 +00:00
|
|
|
c.startupUpdateCheck(config)
|
2013-12-23 19:38:51 +00:00
|
|
|
}
|
2015-08-25 23:40:55 +00:00
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
if err := c.startupJoin(agent, config); err != nil {
|
2021-06-02 21:12:44 +00:00
|
|
|
c.logger.Error(err.Error())
|
2014-04-11 23:59:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:55:04 +00:00
|
|
|
if err := c.startupJoinWan(agent, config); err != nil {
|
2021-06-02 21:12:44 +00:00
|
|
|
c.logger.Error(err.Error())
|
2014-11-14 15:02:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// Let the agent know we've finished registration
|
2017-06-02 10:17:55 +00:00
|
|
|
agent.StartSync()
|
2017-05-22 11:13:44 +00:00
|
|
|
|
2021-06-02 21:17:43 +00:00
|
|
|
c.logger.Info("Consul agent running!")
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2017-06-02 13:35:06 +00:00
|
|
|
// wait for signal
|
2019-06-27 20:22:07 +00:00
|
|
|
signalCh = make(chan os.Signal, 10)
|
2016-11-30 17:42:10 +00:00
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2017-06-02 13:04:34 +00:00
|
|
|
for {
|
|
|
|
var sig os.Signal
|
|
|
|
select {
|
|
|
|
case s := <-signalCh:
|
|
|
|
sig = s
|
2018-06-20 18:30:17 +00:00
|
|
|
case <-service_os.Shutdown_Channel():
|
|
|
|
sig = os.Interrupt
|
2017-06-02 13:04:34 +00:00
|
|
|
case err := <-agent.RetryJoinCh():
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Retry join failed", "error", err)
|
2017-06-02 13:04:34 +00:00
|
|
|
return 1
|
2020-07-02 17:31:47 +00:00
|
|
|
case <-agent.Failed():
|
|
|
|
// The deferred Shutdown method will log the appropriate error
|
|
|
|
return 1
|
2017-06-02 13:04:34 +00:00
|
|
|
case <-agent.ShutdownCh():
|
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
|
|
|
// agent is already down!
|
2017-06-02 13:04:34 +00:00
|
|
|
return 0
|
|
|
|
}
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2017-06-02 13:04:34 +00:00
|
|
|
switch sig {
|
|
|
|
case syscall.SIGPIPE:
|
|
|
|
continue
|
2016-11-30 17:42:10 +00:00
|
|
|
|
2017-06-02 13:04:34 +00:00
|
|
|
case syscall.SIGHUP:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Caught", "signal", sig)
|
2017-03-29 06:46:58 +00:00
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
err := agent.ReloadConfig()
|
2017-06-02 13:04:34 +00:00
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Reload config failed", "error", err)
|
2017-06-02 13:04:34 +00:00
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
config = agent.GetConfig()
|
2017-06-02 13:04:34 +00:00
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Caught", "signal", sig)
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
graceful := (sig == os.Interrupt && !(config.SkipLeaveOnInt)) || (sig == syscall.SIGTERM && (config.LeaveOnTerm))
|
2017-06-02 13:04:34 +00:00
|
|
|
if !graceful {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Graceful shutdown disabled. Exiting")
|
2017-06-02 13:04:34 +00:00
|
|
|
return 1
|
|
|
|
}
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Gracefully shutting down agent...")
|
2017-06-02 13:04:34 +00:00
|
|
|
gracefulCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if err := agent.Leave(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Error("Error on leave", "error", err)
|
2017-06-02 13:04:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
close(gracefulCh)
|
|
|
|
}()
|
|
|
|
|
2017-06-07 11:12:34 +00:00
|
|
|
gracefulTimeout := 15 * time.Second
|
2017-06-02 13:04:34 +00:00
|
|
|
select {
|
|
|
|
case <-signalCh:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Caught second signal, Exiting", "signal", sig)
|
2017-06-02 13:04:34 +00:00
|
|
|
return 1
|
|
|
|
case <-time.After(gracefulTimeout):
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Timeout on graceful leave. Exiting")
|
2017-06-02 13:04:34 +00:00
|
|
|
return 1
|
|
|
|
case <-gracefulCh:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("Graceful exit completed")
|
2017-06-02 13:04:34 +00:00
|
|
|
return 0
|
|
|
|
}
|
2013-12-21 00:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Runs a Consul agent"
|
|
|
|
const help = `
|
|
|
|
Usage: consul agent [options]
|
2013-12-19 20:18:06 +00:00
|
|
|
|
|
|
|
Starts the Consul agent and runs until an interrupt is received. The
|
2017-10-17 13:44:20 +00:00
|
|
|
agent represents a single node in a cluster.
|
|
|
|
`
|