rebar3



Build better Erlang.

Releases shouldn't be a pain to build and run -- especially since you have all the necessary pieces sitting right there! -- and we've done our best to make that no longer the case.

What are releases and target systems anyway?

A release is the set of applications needed for booting an Erlang VM and starting your project. This is described through a release resource file (.rel) which is used to generate a .script and .boot. The boot file is the binary form of the script file and is what is used by the Erlang Run Time System (ERTS) to start an Erlang node, sort of like booting an operating system. Even running erl on the command line is using a boot script.

A target system is an Erlang system capable of being booted on another machine (virtual or otherwise), often ERTS is bundled along with the target system.

For more information checkout the chapter on releases (though it relies on reltool) from Learn You Some Erlang.

No Reltool

Reltool is out and relx is in. If you want to continue using reltool you can manually, it is still bundled with Erlang/OTP.

Getting Started

Add a relx section to your project's rebar.config:

{relx, [{release, {<release name>, "0.0.1"},
         [<app>]},

        {dev_mode, true},
        {include_erts, false},

        {extended_start_script, true}]}.

Running rebar3 release will build the release and provide a script for starting a node under _build/<profile>/rel/<release name>/bin/<release name>.

You can add multiple release sections to your project's rebar.config under relx.
You can either just specify different releases sharing the same configuration:

{relx, [{release, {<release name>, "0.0.1"},
         [<app>]},
        {release, {<release name>, "0.1.0"},
         [<app>]},

        {dev_mode, true},
        {include_erts, false},

        {extended_start_script, true}]}.

Or you can also specify releases with independent configurations:

{relx, [{release, {<release name>, "0.0.1"},
         [<app>],
         [{dev_mode, false},
          {include_erts, true}]},
        {release, {<release name>, "0.1.0"},
         [<app>],
         [{dev_mode, true}]}
       ]}.

You can build specific releases using rebar3 release -n <release_name>

Developing

While developing you'll likely want all your changes to applications be immediately available in the release. relx provides dev_mode for this. Instead of copying the applications that make up the release to the release structure it creates symlinks, so compiling and restarting or loading the changed modules, is all that is necessary.

{relx, [...
        {dev_mode, true},
        ...
       ]
}.

Configuration

VM Configuration

By default relx will give a basic vm.args file that sets a node name and cookie. For a complete list of options and their use check the Erlang documentation.

## Name of the node
-name {{release_name}}@127.0.0.1

## Cookie for distributed erlang
-setcookie {{release_name}}

To provide a custom vm.args, usually placed in the config/ directory at the root of your project, add this line to the relx section of your rebar.config:

{vm_args, "config/vm.args"}

Application Configuration

For passing application environment variables there is sys.config:

[
  {<app_name>, [...]}
].

The default sys.config is empty, so use your own simply add it to your relx section of rebar.config:

{sys_config, "config/sys.config"}

Dynamic Configuration

By setting RELX_REPLACE_OS_VARS=true both vm.args and sys.config files may contain OS environment variables that will be replaced with the current value from the environment the node is started in. This means a vm.args and sys.config for a release that starts a web server listening on a port could look like:

-name ${NODE_NAME}
[
 {appname, [{port, "${PORT}"}]}
].

And then be used to start multiple nodes of the same release with different name.

#!/bin/bash

export RELX_REPLACE_OS_VARS=true

for i in `seq 1 10`;
do
    NODE_NAME=node_$i PORT=808$i _build/default/rel/<release>/bin/<release> foreground &
    sleep 1
done  

Variable format in sys.config

Note that due to limitations in the release handling system in Erlang, environment variables in sys.config must be in a string (i.e. "${PORT}" and not ${PORT}). This is because the configuration is consulted by OTP tools that are neither rebar3 nor relx, and the unquoted format breaks the Erlang parsing for files.

Because the value is meant to be dynamic, it can also not be pre-filtered ahead of time without making it useless.

If variables must be a literal type other than a string (atom, integer, and so on), consider configuring it by adding -<appname> http_port ${PORT} in vm.args, since it does not suffer the same limitations.

Unsupported sys.config features

Using this feature requires copying sys.config to a different filename so that if dev_mode is true it does not overwrite the original copy. Because Erlang depends on the file being named sys.config if you include names of additional config files to load in the list of sys.config configuration values this will cause your release to fail to boot.

Meaning, if you want to use this feature you can not include a filename in sys.config like this example taken from the OTP documentation:

[{myapp,[{par1,val1},{par2,val2}]},
 "/home/user/myconfig"].

Overlays

Overlays provide the ability to define files and templates to include in the target system. For example, custom scripts for managing your node or the Procfile needed for running on Heroku.

{relx, [
    ...
    {overlay_vars, "vars.config"},
    {overlay, [{mkdir, "log/sasl"},
               {template, "priv/app.config", "etc/app.config"},
               {copy, "Procfile", "Procfile"}]}
]}.

Relx exposes variables along with the full power of a Mustache templating system (see mustache). You can look at the documentation there for the full syntax supported. The variables supported are listed below.

Splitting configurations

It is possible to split overlay files to deal with more complex situations. To explain this lets look at the a simplified example:

We build our application and want to distinguish between production and developing builds by having the overlay variable build spell out either "prod" or "dev" so the app.config file could include it in it's configuration and we can either enable or disable features.

For this we build three overlay files:

  • a dev.config - that is dev branch specific
  • a prod.config - that is prod branch specific
  • a base.config - that is not branch specific

For dev builds we will use dev.config as overlay_vars and for prod we will be using prod.config.

{data_dir, "/data/yolo_app"}.
{version, "1.0.0"}.
{run_user, "root"}.
%% Include the base config
"./base.config".
%% The build we have
{build, "dev"}.
%% Include the base config
"./base.config".
%% The build we have
{build, "prod"}.

Predefined variables

Splitting configurations

It is possible to split overlay files to deal with more complex situations. To explain this lets look at the a simplified example:

We build our application and want to distinguish between production and developing builds by having the overlay variable build spell out either "prod" or "dev" so the app.config file could include it in it's configuration and we can either enable or disable features.

For this we build three overlay files:

  • a dev.config - that is dev branch specific
  • a prod.config - that is prod branch specific
  • a base.config - that is not branch specific

For dev builds we will use dev.config as overlay_vars and for prod we will be using prod.config.

{data_dir, "/data/yolo_app"}.
{version, "1.0.0"}.
{run_user, "root"}.
%% Include the base config
"./base.config".
%% The build we have
{build, "dev"}.
%% Include the base config
"./base.config".
%% The build we have
{build, "prod"}.

Predefined variables

  • log : The current log level in the format of (<logname>:<loglevel>)
  • output_dir : The current output directory for the built release
  • target_dir : The same as output_dir, exists for backwards compatibility
  • overridden : The current list of overridden apps (a list of app names)
  • goals : The list of user specified goals in the system
  • lib_dirs : The list of library directories, both user specified and derived
  • config_file : The list of config file used in the system
  • providers : The list of provider names used for this run of relx
  • sys_config : The location of the sys config file
  • root_dir : The root dir of the current project
  • default_release_name : The current default release name for the relx run
  • default_release_version : The current default release version for the relx run
  • default_release : The current default release for the relx run
  • release_erts_version : The version of the Erlang Runtime System in use
  • erts_vsn : The same as release_erts_version (for backwards compatibility)
  • release_name : The currently executing release
  • release_version : the currently executing version
  • rel_vsn : Same as release_version. Exists for backwards compatibility
  • release_applications : A list of applications included in the release

Deployable

To build a release capable of being copied to other nodes we must turn off dev_mode so applications are copied to the release lib dir instead of being symlinks.

$ rebar3 release -d false

Or create a profile that turns off dev_mode:

{profiles, [{prod, [{relx, [{dev_mode, false}]}]}]}.

Target System

A target system can not have symlinks like those created when using dev_mode and often we want to include ERTS along with the system so it does not need to be previously installed on the target.

{profiles, [{prod, [{relx, [{dev_mode, false}
                           ,{include_erts, true}]}]}]}.

Now we can also build a compressed archive to copy to the target:

$ rebar3 as prod tar
===> Verifying default dependencies...
===> Compiling myrel
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
          .../myrel/apps
          /usr/lib/erlang/lib
===> Resolved myrel-0.1.0
===> Including Erts from /usr/lib/erlang
===> release successfully created!
===> tarball .../myrel/_build/rel/myrel/myrel-0.1.0.tar.gz successfully created!

Now a tarball myrel-0.1.0.tar.gz can be copied to another compatible system and booted:

$ mkdir myrel
$ mv myrel-0.1.0.tar.gz myrel/
$ cd myrel
$ tar -zxvf myrel-0.1.0.tar.gz
$ bin/myrel console

Without Erts

To use the ERTS and base applications like kernel and stdlib on the target, set include_erts and system_libs to false in the relx configuration tuple:

{include_erts, false},
{system_libs, false},
...

Source Code Inclusion in Release

By default the release will include source files of your applications, if present.
If you don't want to include the source files, set include_src to false.

{include_src, false}

Application exclusions

The following allows you to remove specific applications from the output release.

{exclude_apps, [app1, app2]}

Module exclusions

The following directive allows you to remove application modules from the output release.

{exclude_modules, [
    {app1, [app1_mod1, app1_mod2]},
    {app2, [app2_mod1, app2_mod2]}
]}.

Cross Compiling

If you wish to include an Erlang Run Time System that is not the version you are using to run rebar3, for example you are building on MacOSX but wish to include an ERTS that was built for a version of GNU/Linux, you can supply a path instead of a boolean for include_erts and provide a path for system_libs, still within the relx configuration tuple:

{include_erts, "/path/to/erlang"},
{system_libs, "/path/to/erlang"},
...

Using these paths with profiles can yield easier ways to set up cross-compiling.

Booting, Upgrade and Inspecting

The extended start script that comes with relx and is set to be used by rebar3's release template, {extended_start_script, true}, provides a few ways of starting and connecting to your release.

For local development you'll like use console. In production you'll want start or foreground.

start creates a pipe that can be connected to later with the command attach. The Erlang VM calls fsync on every line of output in this mode, so foreground might be better for your use cases.

To open a console with a node started with foreground use remote_console.

Upgrade and Downgrade

For a detailed workflow including version increments and appup generation checkout Richard Jones relflow tool built around rebar3.

For the basic release upgrade after install of a release assume we have a release named myrel with a version 0.0.1 and 0.0.2:

  • Generate a release upgrade rebar3 relup
  • Installing: Installing a release on a running system will unpack and upgrade the version: bin/myrel install 0.0.1
  • Upgrading: If the version is already unpacked you can simply call upgrade to upgrade to the version: bin/myrel upgrade 0.0.2
  • Downgrading: To downgrade to the previous version use the downgrade command: bin/myrel downgrade 0.0.1

References

Releases