2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
2022-11-10 16:26:01 +00:00
|
|
|
"io"
|
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"
|
2023-05-16 18:47:02 +00:00
|
|
|
"github.com/hashicorp/consul/agent/hcp"
|
2022-09-26 18:58:15 +00:00
|
|
|
hcpbootstrap "github.com/hashicorp/consul/agent/hcp/bootstrap"
|
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
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
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: "==> ",
|
2022-09-26 18:58:15 +00:00
|
|
|
InfoPrefix: " ", // Note that startupLogger also uses this prefix
|
2021-06-02 21:12:44 +00:00
|
|
|
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
|
|
|
}
|
2022-09-26 18:58:15 +00:00
|
|
|
|
|
|
|
// wait for signal
|
|
|
|
signalCh := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// startup logger is a shim since we need to be about to log both before and
|
|
|
|
// after logging is setup properly but before agent has started fully. This
|
|
|
|
// takes care of that!
|
|
|
|
suLogger := newStartupLogger()
|
|
|
|
go handleStartupSignals(ctx, cancel, signalCh, suLogger)
|
|
|
|
|
|
|
|
// See if we need to bootstrap config from HCP before we go any further with
|
2023-04-27 20:27:39 +00:00
|
|
|
// agent startup. First do a preliminary load of agent configuration using the given loader.
|
|
|
|
// This is just to peek whether bootstrapping from HCP is enabled. The result is discarded
|
|
|
|
// on the call to agent.NewBaseDeps so that the wrapped loader takes effect.
|
|
|
|
res, err := loader(nil)
|
2022-09-26 18:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2023-04-27 20:27:39 +00:00
|
|
|
if res.RuntimeConfig.IsCloudEnabled() {
|
|
|
|
client, err := hcp.NewClient(res.RuntimeConfig.Cloud)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error("error building HCP HTTP client: " + err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// We override loader with the one returned as it was modified to include HCP-provided config.
|
|
|
|
loader, err = hcpbootstrap.LoadConfig(ctx, client, res.RuntimeConfig.DataDir, loader, ui)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 18:58:15 +00:00
|
|
|
|
2022-10-24 22:02:38 +00:00
|
|
|
bd, err := agent.NewBaseDeps(loader, logGate, nil)
|
2020-08-08 01:08:43 +00:00
|
|
|
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
|
|
|
|
2022-09-26 18:58:15 +00:00
|
|
|
// Upgrade our startupLogger to use the real logger now we have it
|
|
|
|
suLogger.SetLogger(c.logger)
|
|
|
|
|
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{
|
2022-11-10 16:26:01 +00:00
|
|
|
BasicUi: mcli.BasicUi{ErrorWriter: c.ui.Stderr(), Writer: io.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>"
|
|
|
|
}
|
2023-05-16 18:47:02 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Version: '%s'", c.versionHuman))
|
2022-06-02 02:29:40 +00:00
|
|
|
if strings.Contains(c.versionHuman, "dev") {
|
2023-05-16 18:47:02 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Revision: '%s'", c.revision))
|
2022-06-02 02:29:40 +00:00
|
|
|
}
|
2023-05-16 18:47:02 +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 != "" {
|
2023-05-16 18:47:02 +00:00
|
|
|
ui.Info(fmt.Sprintf(" Partition: '%s'", ap))
|
2021-07-08 15:03:38 +00:00
|
|
|
}
|
2023-05-16 18:47:02 +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, gRPC-TLS: %d, DNS: %d)", config.ClientAddrs,
|
2022-08-19 17:07:22 +00:00
|
|
|
config.HTTPPort, config.HTTPSPort, config.GRPCPort, config.GRPCTLSPort, config.DNSPort))
|
2023-05-16 18:47:02 +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))
|
2023-05-16 18:47:02 +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(" ACL Enabled: %t", config.ACLsEnabled))
|
|
|
|
ui.Info(fmt.Sprintf("ACL Default Policy: %s", config.ACLResolverSettings.ACLDefaultPolicy))
|
|
|
|
ui.Info(fmt.Sprintf(" HTTPS TLS: Verify Incoming: %t, Verify Outgoing: %t, Min Version: %s",
|
2022-08-09 11:54:49 +00:00
|
|
|
config.TLS.HTTPS.VerifyIncoming, config.TLS.HTTPS.VerifyOutgoing, config.TLS.HTTPS.TLSMinVersion))
|
2023-05-16 18:47:02 +00:00
|
|
|
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",
|
2022-08-09 11:54:49 +00:00
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:58:15 +00:00
|
|
|
func handleStartupSignals(ctx context.Context, cancel func(), signalCh chan os.Signal, logger *startupLogger) {
|
|
|
|
for {
|
|
|
|
var sig os.Signal
|
|
|
|
select {
|
|
|
|
case s := <-signalCh:
|
|
|
|
sig = s
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGPIPE:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
err := fmt.Errorf("cannot reload before agent started")
|
|
|
|
logger.Error("Caught", "signal", sig, "error", err)
|
|
|
|
|
|
|
|
default:
|
|
|
|
logger.Info("Caught", "signal", sig)
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
`
|