agent: remove agent from command

Decouple command and agent a bit more.
This commit is contained in:
Frank Schroeder 2017-06-02 12:17:55 +02:00 committed by Frank Schröder
parent d977aa1fe7
commit cb98754cd6
1 changed files with 28 additions and 31 deletions

View File

@ -48,7 +48,6 @@ type Command struct {
args []string args []string
logFilter *logutils.LevelFilter logFilter *logutils.LevelFilter
logOutput io.Writer logOutput io.Writer
agent *Agent
} }
// readConfig is responsible for setup of our configuration using // readConfig is responsible for setup of our configuration using
@ -470,13 +469,13 @@ func (cmd *Command) checkpointResults(results *checkpoint.CheckResponse, err err
} }
// startupJoin is invoked to handle any joins specified to take place at start time // startupJoin is invoked to handle any joins specified to take place at start time
func (cmd *Command) startupJoin(cfg *Config) error { func (cmd *Command) startupJoin(agent *Agent, cfg *Config) error {
if len(cfg.StartJoin) == 0 { if len(cfg.StartJoin) == 0 {
return nil return nil
} }
cmd.UI.Output("Joining cluster...") cmd.UI.Output("Joining cluster...")
n, err := cmd.agent.JoinLAN(cfg.StartJoin) n, err := agent.JoinLAN(cfg.StartJoin)
if err != nil { if err != nil {
return err return err
} }
@ -486,13 +485,13 @@ func (cmd *Command) startupJoin(cfg *Config) error {
} }
// startupJoinWan is invoked to handle any joins -wan specified to take place at start time // startupJoinWan is invoked to handle any joins -wan specified to take place at start time
func (cmd *Command) startupJoinWan(cfg *Config) error { func (cmd *Command) startupJoinWan(agent *Agent, cfg *Config) error {
if len(cfg.StartJoinWan) == 0 { if len(cfg.StartJoinWan) == 0 {
return nil return nil
} }
cmd.UI.Output("Joining -wan cluster...") cmd.UI.Output("Joining -wan cluster...")
n, err := cmd.agent.JoinWAN(cfg.StartJoinWan) n, err := agent.JoinWAN(cfg.StartJoinWan)
if err != nil { if err != nil {
return err return err
} }
@ -634,7 +633,6 @@ func (cmd *Command) Run(args []string) int {
cmd.UI.Error(fmt.Sprintf("Error starting agent: %s", err)) cmd.UI.Error(fmt.Sprintf("Error starting agent: %s", err))
return 1 return 1
} }
cmd.agent = agent
// Setup update checking // Setup update checking
if !config.DisableUpdateCheck { if !config.DisableUpdateCheck {
@ -660,16 +658,16 @@ func (cmd *Command) Run(args []string) int {
}() }()
} }
defer cmd.agent.Shutdown() defer agent.Shutdown()
// Join startup nodes if specified // Join startup nodes if specified
if err := cmd.startupJoin(config); err != nil { if err := cmd.startupJoin(agent, config); err != nil {
cmd.UI.Error(err.Error()) cmd.UI.Error(err.Error())
return 1 return 1
} }
// Join startup nodes if specified // Join startup nodes if specified
if err := cmd.startupJoinWan(config); err != nil { if err := cmd.startupJoinWan(agent, config); err != nil {
cmd.UI.Error(err.Error()) cmd.UI.Error(err.Error())
return 1 return 1
} }
@ -705,7 +703,7 @@ func (cmd *Command) Run(args []string) int {
} }
// Let the agent know we've finished registration // Let the agent know we've finished registration
cmd.agent.StartSync() agent.StartSync()
cmd.UI.Output("Consul agent running!") cmd.UI.Output("Consul agent running!")
cmd.UI.Info(fmt.Sprintf(" Version: '%s'", cmd.HumanVersion)) cmd.UI.Info(fmt.Sprintf(" Version: '%s'", cmd.HumanVersion))
@ -718,19 +716,18 @@ func (cmd *Command) Run(args []string) int {
cmd.UI.Info(fmt.Sprintf(" Cluster Addr: %v (LAN: %d, WAN: %d)", config.AdvertiseAddr, cmd.UI.Info(fmt.Sprintf(" Cluster Addr: %v (LAN: %d, WAN: %d)", config.AdvertiseAddr,
config.Ports.SerfLan, config.Ports.SerfWan)) config.Ports.SerfLan, config.Ports.SerfWan))
cmd.UI.Info(fmt.Sprintf("Gossip encrypt: %v, RPC-TLS: %v, TLS-Incoming: %v", cmd.UI.Info(fmt.Sprintf("Gossip encrypt: %v, RPC-TLS: %v, TLS-Incoming: %v",
cmd.agent.GossipEncrypted(), config.VerifyOutgoing, config.VerifyIncoming)) agent.GossipEncrypted(), config.VerifyOutgoing, config.VerifyIncoming))
// Enable log streaming // Enable log streaming
cmd.UI.Info("") cmd.UI.Info("")
cmd.UI.Output("Log data will now stream in as it occurs:\n") cmd.UI.Output("Log data will now stream in as it occurs:\n")
logGate.Flush() logGate.Flush()
// Wait for exit return cmd.wait(agent, config)
return cmd.handleSignals(config)
} }
// handleSignals blocks until we get an exit-causing signal // wait blocks until we get an exit-causing signal
func (cmd *Command) handleSignals(cfg *Config) int { func (cmd *Command) wait(agent *Agent, cfg *Config) int {
signalCh := make(chan os.Signal, 4) signalCh := make(chan os.Signal, 4)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)
@ -742,15 +739,15 @@ WAIT:
select { select {
case s := <-signalCh: case s := <-signalCh:
sig = s sig = s
case ch := <-cmd.agent.ReloadCh(): case ch := <-agent.ReloadCh():
sig = syscall.SIGHUP sig = syscall.SIGHUP
reloadErrCh = ch reloadErrCh = ch
case <-cmd.ShutdownCh: case <-cmd.ShutdownCh:
sig = os.Interrupt sig = os.Interrupt
case err := <-cmd.agent.RetryJoinCh(): case err := <-agent.RetryJoinCh():
cmd.UI.Error(err.Error()) cmd.UI.Error(err.Error())
return 1 return 1
case <-cmd.agent.ShutdownCh(): case <-agent.ShutdownCh():
// Agent is already shutdown! // Agent is already shutdown!
return 0 return 0
} }
@ -764,7 +761,7 @@ WAIT:
// Check if this is a SIGHUP // Check if this is a SIGHUP
if sig == syscall.SIGHUP { if sig == syscall.SIGHUP {
conf, err := cmd.handleReload(cfg) conf, err := cmd.handleReload(agent, cfg)
if conf != nil { if conf != nil {
cfg = conf cfg = conf
} }
@ -795,7 +792,7 @@ WAIT:
gracefulCh := make(chan struct{}) gracefulCh := make(chan struct{})
cmd.UI.Output("Gracefully shutting down agent...") cmd.UI.Output("Gracefully shutting down agent...")
go func() { go func() {
if err := cmd.agent.Leave(); err != nil { if err := agent.Leave(); err != nil {
cmd.UI.Error(fmt.Sprintf("Error: %s", err)) cmd.UI.Error(fmt.Sprintf("Error: %s", err))
return return
} }
@ -814,7 +811,7 @@ WAIT:
} }
// handleReload is invoked when we should reload our configs, e.g. SIGHUP // handleReload is invoked when we should reload our configs, e.g. SIGHUP
func (cmd *Command) handleReload(cfg *Config) (*Config, error) { func (cmd *Command) handleReload(agent *Agent, cfg *Config) (*Config, error) {
cmd.UI.Output("Reloading configuration...") cmd.UI.Output("Reloading configuration...")
var errs error var errs error
newConf := cmd.readConfig() newConf := cmd.readConfig()
@ -837,35 +834,35 @@ func (cmd *Command) handleReload(cfg *Config) (*Config, error) {
} }
// Bulk update the services and checks // Bulk update the services and checks
cmd.agent.PauseSync() agent.PauseSync()
defer cmd.agent.ResumeSync() defer agent.ResumeSync()
// Snapshot the current state, and restore it afterwards // Snapshot the current state, and restore it afterwards
snap := cmd.agent.snapshotCheckState() snap := agent.snapshotCheckState()
defer cmd.agent.restoreCheckState(snap) defer agent.restoreCheckState(snap)
// First unload all checks, services, and metadata. This lets us begin the reload // First unload all checks, services, and metadata. This lets us begin the reload
// with a clean slate. // with a clean slate.
if err := cmd.agent.unloadServices(); err != nil { if err := agent.unloadServices(); err != nil {
errs = multierror.Append(errs, fmt.Errorf("Failed unloading services: %s", err)) errs = multierror.Append(errs, fmt.Errorf("Failed unloading services: %s", err))
return nil, errs return nil, errs
} }
if err := cmd.agent.unloadChecks(); err != nil { if err := agent.unloadChecks(); err != nil {
errs = multierror.Append(errs, fmt.Errorf("Failed unloading checks: %s", err)) errs = multierror.Append(errs, fmt.Errorf("Failed unloading checks: %s", err))
return nil, errs return nil, errs
} }
cmd.agent.unloadMetadata() agent.unloadMetadata()
// Reload service/check definitions and metadata. // Reload service/check definitions and metadata.
if err := cmd.agent.loadServices(newConf); err != nil { if err := agent.loadServices(newConf); err != nil {
errs = multierror.Append(errs, fmt.Errorf("Failed reloading services: %s", err)) errs = multierror.Append(errs, fmt.Errorf("Failed reloading services: %s", err))
return nil, errs return nil, errs
} }
if err := cmd.agent.loadChecks(newConf); err != nil { if err := agent.loadChecks(newConf); err != nil {
errs = multierror.Append(errs, fmt.Errorf("Failed reloading checks: %s", err)) errs = multierror.Append(errs, fmt.Errorf("Failed reloading checks: %s", err))
return nil, errs return nil, errs
} }
if err := cmd.agent.loadMetadata(newConf); err != nil { if err := agent.loadMetadata(newConf); err != nil {
errs = multierror.Append(errs, fmt.Errorf("Failed reloading metadata: %s", err)) errs = multierror.Append(errs, fmt.Errorf("Failed reloading metadata: %s", err))
return nil, errs return nil, errs
} }