Given a package with the below optional binds:
pkg_binds_optional=(
[be]="port"
[de]="port"
[es]="port"
[default]="port"
)
And bindees that satisfy those bindings by exporting config such as:
"cfg": {
"dedicated_market": true,
"healthcheck": "/health/heartbeat",
"port": 80,
"server_name": "www.mysite.be",
"server_aliases": [
"mysite.be"
]
}
I would like to be able to loop any/all binds to generate config conditionally based on the cfg
values exported.
{{#each bind as |key value|~}}
{{#if value.first.cfg.dedicated_market}}
// do something with {{value.first.cfg.server_name}}
{{/if~}}
{{~/each}}
However, value.first
here often gives me a ‘dead’ member which is exporting stale config. I realise that I could loop only the members of the bind considered to be ‘alive’ as below.
{{#eachAlive bind.de.members as |member|}}
{{#if member.cfg.dedicated_market}}
// do something with {{member.cfg.server_name}}
{{/if}}
{{/eachAlive}}
But the above solution has two flaws. First, it requires a templating block for every named bind and I’m keen to avoid maintaining a list of binds in both plan.sh
and my config files(s). Second, it will print the same line of config for every ‘alive’ member since I cannot break out of the loop in handlebars, for example if I have four members considered to be alive, my config would look like this:
// do something with www.mysite.de
// do something with www.mysite.de
// do something with www.mysite.de
// do something with www.mysite.de
So I’m searching for a way of retrieving a single member of a bind that’s considered to be alive, and I’m eager to be able to do it inside an {{#each bind as |key value|}}
block so I can loop all binds without explicitly naming them.
For the curious, my use case is that I want to use service discovery to register multiple HAProxy backends in my haproxy.conf
. Those services that bind as a named (dedicated) group, e.g. de
, be
, are expected to provide a server_name
that HAProxy will use to target them (through Host header-based ACL rules). Those services that bind as the ‘default’ group will form the default_backend
in haproxy.conf
.