Skip to content

Commit

Permalink
consul: use postStart
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Dec 9, 2021
1 parent d94d456 commit 4319cbe
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions modules/consul.nix
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,33 @@ in {

path = with pkgs; [ envoy ];

postStart = ''
set -exuo pipefail
PATH="${makeBinPath [ pkgs.jq cfg.package pkgs.coreutils ]}"
set +x
# During bootstrap the vault generated token are not yet available
if [ -s /run/keys/consul-default-token ]
then
CONSUL_HTTP_TOKEN="$(< /run/keys/consul-default-token)"
export CONSUL_HTTP_TOKEN
# Therefore, on core nodes, use the sops out-of-band bootstrapped master token
elif [ -s /etc/consul.d/secrets.json ]
then
# as of writing: core nodes are observed to posess the master token
# while clients do not
jq -e .acl.tokens.master /etc/consul.d/secrets.json || exit 5
CONSUL_HTTP_TOKEN="$(jq -e -r .acl.tokens.master /etc/consul.d/secrets.json)"
export CONSUL_HTTP_TOKEN
else
# Unknown state, should never reach this.
exit 6
fi
set -x
while ! consul info &>/dev/null; do sleep 3; done
'';

serviceConfig = let
preScript = let
start-pre = pkgs.writeShellScriptBin "consul-start-pre" ''
Expand All @@ -427,35 +454,6 @@ in {
'';
in "!${start-pre}/bin/consul-start-pre";

postScript = let
start-post = pkgs.writeShellScriptBin "consul-start-post" ''
set -exuo pipefail
PATH="${makeBinPath [ pkgs.jq cfg.package pkgs.coreutils ]}"
set +x
# During bootstrap the vault generated token are not yet available
if [ -s /run/keys/consul-default-token ]
then
CONSUL_HTTP_TOKEN="$(< /run/keys/consul-default-token)"
export CONSUL_HTTP_TOKEN
# Therefore, on core nodes, use the sops out-of-band bootstrapped master token
elif [ -s /etc/consul.d/secrets.json ]
then
# as of writing: core nodes are observed to posess the master token
# while clients do not
jq -e .acl.tokens.master /etc/consul.d/secrets.json || exit 5
CONSUL_HTTP_TOKEN="$(jq -e -r .acl.tokens.master /etc/consul.d/secrets.json)"
export CONSUL_HTTP_TOKEN
else
# Unknown state, should never reach this.
exit 6
fi
set -x
while ! consul info &>/dev/null; do sleep 3; done
'';
in "!${start-post}/bin/consul-start-post";

reloadScript = let
reload = pkgs.writeShellScriptBin "consul-reload" ''
set -exuo pipefail
Expand Down Expand Up @@ -492,7 +490,6 @@ in {
ExecReload = reloadScript;
ExecStart =
"@${cfg.package}/bin/consul consul agent -config-dir /etc/${cfg.configDir}";
ExecStartPost = postScript;
Restart = "on-failure";
RestartSec = "10s";
DynamicUser = true;
Expand Down

0 comments on commit 4319cbe

Please sign in to comment.