agent: fix 'consul leave' shutdown race (#2880)
agent: fix 'consul leave' shutdown race (#2880)

When the agent is triggered to shutdown via an external 'consul leave'
command delivered via the HTTP API then the client expects to receive a
response when the agent is down. This creates a race on when to shutdown
the agent itself like the RPC server, the checks and the state and the
external endpoints like DNS and HTTP. Ideally, the external endpoints
should be shutdown before the internal state but if the goal is to
respond reliably that the agent is down then this is not possible.

This patch splits the agent shutdown into two parts implemented in a
single method to keep it simple and unambiguos for the caller. The first
stage shuts down the internal state, checks, RPC server, ...
synchronously and then triggers the shutdown of the external endpoints
asychronously. This way the caller is guaranteed that the internal state
services are down when Shutdown returns and there remains enough time to
send a response.

Fixes #2880

youhong316/consul,42wim/consul,tugbabodrumlu/consul,tugbabodrumlu/consul,i0rek/consul,hashicorp/consul,vamage/consul,youhong316/consul,scalp42/consul,vamage/consul,scalp42/consul,zeroae/consul,zeroae/consul,zeroae/consul,scalp42/consul,hashicorp/consul,i0rek/consul,youhong316/consul,42wim/consul,i0rek/consul,tugbabodrumlu/consul,calgaryscientific/consul,kingland/consul,tugbabodrumlu/consul,calgaryscientific/consul,calgaryscientific/consul,kingland/consul,calgaryscientific/consul,42wim/consul,youhong316/consul,tugbabodrumlu/consul,i0rek/consul,vamage/consul,hashicorp/consul,42wim/consul,hashicorp/consul,scalp42/consul,calgaryscientific/consul,vamage/consul,vamage/consul,kingland/consul,kingland/consul,42wim/consul,scalp42/consul,i0rek/consul,zeroae/consul,zeroae/consul,kingland/consul,youhong316/consul