erlware / relx

Sane, simple release creation for Erlang
http://erlware.github.io/relx
Apache License 2.0
697 stars 232 forks source link

Generating multiple .config files from one template but with different overlay vars #757

Open seanhinde opened 5 years ago

seanhinde commented 5 years ago

I need to generate multiple config files in a single release from a single template. Something like:

host1/sys.config
host2/sys.config

where host1/sys.config has configuration specific to host1 etc.

I can't see a way to achieve this today. Am I missing something or would this be a feature worth adding?

Maybe something along the lines of a specific overlay file per template instantiation like:

{template, "priv/app.config", "etc/host1/sys.config", "host1_overlay_vars.config"},
{template, "priv/app.config", "etc/host2/sys.config", "host2_overlay_vars.config"},
lrascao commented 5 years ago

what is the gist of the diff between host1 and host2? maybe you overlay variables instead of multiple configs

seanhinde commented 5 years ago

There are a bunch of variables in the template that are deployment host specific, and we include all the generated configurations for all hosts in our release RPM.

When the system is started on the target host the start script picks the right sys.config based on the local hostname. They are things like which mnesia node to pair with etc.

It allows us to have a single RPM across all environments.

I do want to use overlay variables at build release time, but it seems that there can only be one overlay vars file per release.

seanhinde commented 5 years ago

Adding this clause to rlx_prv_overlay:do_individual_overlay/4 does what I need:

do_individual_overlay(State, _Files, OverlayVars0, {template, From, To, OverlayFilename}) ->
    OverlayVars = read_overlay_vars(State, OverlayVars0, [OverlayFilename]),
    file_render_do(OverlayVars, From,
                   fun(FromFile) ->
                           file_render_do(OverlayVars, To,
                                          fun(ToFile) ->
                                                  write_template(OverlayVars,
                                                                 absolute_path_from(State, FromFile),
                                                                 absolute_path_to(State, ToFile))
                                          end)
                   end).
lrascao commented 5 years ago

I do want to use overlay variables at build release time, but it seems that there can only be one overlay vars file per release.

you can use rebar3 profiles for this, for each of them you can specify a different overlay file

seanhinde commented 5 years ago

I was looking at profiles. How do I generate one release that uses multiple profiles at the same time?

ferd commented 4 years ago

rebar3 as profile1,profile2,profile3 <command>

seanhinde commented 4 years ago

Yep, that works. It's a bit messy when there are 20 profiles. It's no longer just rebar3 release, it's rebar3 as prod,host1,host2,host3,.......hostN release. The profile list can be retrieved by a script, but this all pushes us towards a Makefile.

I still like the option to have a per template vars file - the rebar.config is cleaner, and the build process is as simple as can be.

I will probably keep my small patch locally - it works well enough for my uses. There is some template releated code in tar creation I didn't investigate, but aside from that it's a small change and maybe you will consider the idea for future release.

Thanks for all the help. Happy to close.

tsloughter commented 4 years ago

@seanhinde you have a patch for this? Have you sent a PR? It might be worth including.

seanhinde commented 9 months ago

I'm back on the project that found this change very useful. PR here https://github.com/erlware/relx/pull/933

One thing in there I'm not sure about. Feedback welcome.