Releases

📘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 from Adopting Erlang.

Getting Started

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

  1. {relx, [{release, {<release name>, "0.0.1"},
  2. [<app>]},
  3. {release, {<release name>, "0.1.0"},
  4. [<app>]},
  5. {dev_mode, true},
  6. {include_erts, false},
  7. {extended_start_script, true}]}.

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

<release_name> must be an atom, same for each <app> in the list of applications to include in the release.

<vsn> can be one of:

Version typeResult
string()A string is used as is for the version. Example: "0.1.0"
git | semverUses the latest git tag on the repo to construct the version.
{cmd, string()}Uses the result of executing the contents of string() in a shell. Example to use a file VERSION: {cmd, "cat VERSION | tr -d '[:space:]'"}
{git, short | long}Uses either the short (8 characters) or the full git ref of the current commit.
{file, File}Uses the content of a file. For example, a better way to use a VERSION file than using cmd: {file, "VERSION"}

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:

  1. {relx, [{release, {<release name>, "0.0.1"},
  2. [<app>]},
  3. {release, {<other release name>, "0.1.0"},
  4. [<app>]},
  5. {dev_mode, false},
  6. {include_erts, true},
  7. ]}.

Or you can also specify releases with independent configurations by using a 4-tuple release definition:

  1. {relx, [{release, {<release name>, "0.0.1"},
  2. [<app>],
  3. [{dev_mode, false},
  4. {include_erts, true}]},
  5. {release, {<release name>, "0.1.0"},
  6. [<app>],
  7. [{dev_mode, true}]}
  8. ]}.

You can build specific releases using rebar3 release -n

Build Configuration

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.

  1. {include_src, false}

Application exclusions

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

  1. {exclude_apps, [app1, app2]}

The Applications will be removed from the .app file of any Application in the release that had them listed under applications.

Module exclusions

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

  1. {exclude_modules, [
  2. {app1, [app1_mod1, app1_mod2]},
  3. {app2, [app2_mod1, app2_mod2]}
  4. ]}.

The modules will be removed from the Application’s .app file’s module list.

Modes

Other modes include prod and minimal:

  1. {relx, [...
  2. {mode, <mode>},
  3. ...
  4. ]
  5. }.
ModeExpanded Options
dev[{dev_mode, true}, {include_src, true}, {debug_info, keep}, {include_erts, false}]
prod[{include_src, false}, {debug_info, strip}, {include_erts, true}, {dev_mode, false}]
minimal[{include_src, false}, {debug_info, strip}, {include_erts, false}, {dev_mode, false}]

While developing you’ll likely want all your changes to applications be immediately available in the release. relx provides multiple modes, including a mode dev for this particular use case. 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.

The prod mode expands to options that are commonly used for production releases: include_src is false so source code is not included in the release, debug_info is set to strip which makes BEAM files smaller and only removes data that is used by tools you more than likely aren’t including in your release, and sets include_erts to true to bundle in the current Erlang runtime, making a release you can copy to a compatible target and run without first installing Erlang.

📘Rebar3 Prod Profile

When building in the rebar3 prod profile, like with rebar3 as prod release then the relx prod mode is enabled automatically.

The minimal mode is the same as prod except it does not include the Erlang runtime.

You can override options that the modes expand to by including explicit setting them. For example, if you did want to keep the debug info in the BEAM modules then you can use a configuration like:

  1. [
  2. {mode, prod},
  3. {debug_info, keep}
  4. ]

Verification Checks

Missing Functions

By default relx will check that the external functions used in the modules of the project applications included in a release exist. This means a warning will be given if a function is called that isn’t included in the release, even if it is a dependency in rebar.config or an application included in OTP.

This helps to protect against a common mistake that has bitten us all at some point, forgetting to add an application to the .app.src of the application that depends on it.

If for some reason you wish to disable this check you can set it to false in the relx config:

  1. {check_for_undefined_functions, false}

Stale Modules

The option src_tests will issue a warning if the source code for a module is missing or is newer than the object code:

  1. {src_tests, true}

This is useful to catch any modifications to dependency source files. Since rebar3 release will automatically compile all changes to the Applications in your project the dependencies should be the only modules that could possibly be stale.

Runtime 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.

  1. ## Name of the node
  2. -name {{release_name}}@127.0.0.1
  3. ## Cookie for distributed erlang
  4. -setcookie {{release_name}}

To provide a custom vm.args or vm.args.src, simply create the file in the top level config/ directory at the root of your project. If you name it something other than vm.args or vm.args.src you must add to the relx configuration:

  1. {vm_args, "config/vm_prod.args"}

or:

  1. {vm_args_src, "config/vm_prod.args.src"}

Application Configuration

For passing Application configuration at release runtime there is sys.config and sys.config.src:

  1. [
  2. {<app_name>, [{<key>, <val>}, ...]}
  3. ].

If either file config/sys.config.src or config/sys.config exists in the project then relx will automatically include one of them (.src takes precedence if both exist) in the release.

To set a specific file to use as the Application configuration file it can be set with either sys_config or sys_config_src:

  1. {sys_config, "config/sys_prod.config"}
  1. {sys_config_src, "config/sys_prod.config.src"}

The files will be renamed to sys.config or sys.config.src when included into the release.

If none exists then a file with an empty list is used.

Read more about Erlang configuration in the config docs and in the systools docs.

Environment Variable Replacement

With OTP-21+ and rebar3 3.6+

Starting with Erlang/OTP 21 and rebar3 3.6.0 the configuration options sys_config_src and vm_args_src are available for explicitly including templates that will be rendered at runtime, substituting variables defined as ${VARIABLE} with their equivalent value in the shell environment.

As of rebar3 3.14.0 the configs will be included if they exist, so only if the files are not named config/sys.config.src and config/vm.args.src do you need to include {sys_config_src, } or {vm_args_src, } in the relx config.

  1. %% sys.config.src
  2. [
  3. {appname, [{port, ${PORT}}]}
  4. ].
  1. # vm.args.src
  2. -name ${NODE_NAME}
  1. %% rebar.config
  2. {relx, [{release, {<release name>, "0.0.1"},
  3. [<app>]},
  4. {mode, dev}]}.

There is no need to set RELX_REPLACE_OS_VARS=true when using .src files for configuration. In the following section we’ll see older forms of runtime configuration.

Before OTP-21 and rebar3 3.6

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:

  1. # vm.args
  2. -name ${NODE_NAME}
  1. %% sys.config
  2. [
  3. {appname, [{port, "${PORT}"}]}
  4. ].

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

  1. #!/bin/bash
  2. export RELX_REPLACE_OS_VARS=true
  3. for i in `seq 1 10`;
  4. do
  5. NODE_NAME=node_$i PORT=808$i _build/default/rel/<release>/bin/<release> foreground &
  6. sleep 1
  7. done

Overlays: Build-Time Configuration

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.

  1. {relx, [
  2. ...
  3. {overlay_vars, "vars.config"},
  4. {overlay, [{mkdir, "log/sasl"},
  5. {template, "priv/app.config", "etc/app.config"},
  6. {copy, "Procfile", "Procfile"}]}
  7. ]}.

The supported actions are:

  • mkdir to create a directory within the release

  • copy to copy a file from a local directory to a location within the release

  • template to behave the way copy does, but with variable expansion in it.

Relx’s templating 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.

There is a set of variables made available by default which are described in the next session, and custom variables can otherwise be declared in the file specified in {overlay_vars, "vars.config"}, which should have the following format:

  1. %% some variables
  2. {key, value}.
  3. {other_key, other_val}.
  4. %% includes variables from another file
  5. "./some_file.config".

The default variables are defined below.

Predefined Overlay variables

NameDescription
logThe current log level in the format of (:)
output_dirThe current output directory for the built release
target_dirThe same as output_dir, exists for backwards compatibility
overriddenThe current list of overridden apps (a list of app names)
goalsThe list of user specified goals in the system
lib_dirsThe list of library directories, both user specified and derived
config_fileThe list of config file used in the system
providersThe list of provider names used for this run of relx
sys_configThe location of the sys config file
root_dirThe root dir of the current project
default_release_nameThe current default release name for the relx run
default_release_versionThe current default release version for the relx run
default_releaseThe current default release for the relx run
release_erts_versionThe version of the Erlang Runtime System in use
erts_vsnThe same as release_erts_version (for backwards compatibility)
release_nameThe currently executing release
release_versionthe currently executing version
rel_vsnSame as release_version. Exists for backwards compatibility
release_applicationsA list of applications included in the release
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:

  • dev.config
  • prod.config
  • base.config

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

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

Deployable Tarball

With ERTS Included

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.

Rebar3 will automatically add {mode, prod} to the relx configuration if the prod profile is used to build the release. For example:

  1. $ rebar3 as prod tar
  2. ===> Verifying dependencies...
  3. ===> Analyzing applications...
  4. ===> Compiling relx_overlays
  5. ===> Assembling release myrel-0.1.0...
  6. ===> Release successfully assembled: _build/prod/rel/myrel
  7. ===> Building release tarball myrel-0.1.0.tar.gz...
  8. ===> Tarball successfully created: _build/prod/rel/myrel/myrel-0.1.0.tar.gz

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

  1. $ mkdir myrel
  2. $ mv myrel-0.1.0.tar.gz myrel/
  3. $ cd myrel
  4. $ tar -zxvf myrel-0.1.0.tar.gz
  5. $ bin/myrel console

Without ERTS

When it is required to leave ERTS out of a release the prod profile configuration can be set in rebar.config under profiles. For example, to use the ERTS and base applications like kernel and stdlib on the target, set mode to minimal and system_libs to false in the relx configuration tuple:

  1. {profiles, [{prod, [{relx, [{mode, minimal},
  2. {system_libs, false}]}]}]}.

Or manually set include_erts to false:

  1. {profiles, [{prod, [{relx, [{include_erts, false},
  2. {system_libs, false}]}]}]}

Now when running rebar3 as prod tar the generated tarball will not include ERTS or Applications like kernel and stdlib.

With ERTS Built for Another System

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:

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

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

Extended Start Script

Commands

The extended start script that comes with relx provides a few ways of starting and connecting to your release.

For local development you’ll likely use console. In production you’ll want foreground, no matter if you are starting manual in something like tmux, using an init system like systemd or running the release in a Docker container.

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

A full list of commands is below.

CommandDescription
foregroundStart release with output to stdout
remoteConnect remote shell to running node
consoleStart the release with an interactive shell
console_cleanStart an interactive shell without the release’s applications
rpc [Mod [Fun [Args]]]]Run apply(Mod, Fun, Args) on running node
eval [Exprs]Run expressions on running node
statusVerify node is running and then run status hook scripts
restartRestart the applications but not the VM
rebootReboot the entire VM
stopStop the running node
pidPrint the PID of the OS process
pingPrint pong if the node is alive
daemonStart release in the background with run_erl (named pipes)
daemon_attachConnect to node started as daemon with to_erl (named pipes)

Release Handling: Install and Upgrade

Additionally, the extended start script contains commands for using release_handler:

CommandDescription
unpack [Version]Unpack a release tarball
install [Version]Install a release
uninstall [Version]Uninstall a release
upgrade [Version]Upgrade the running release to a new version
downgrade [Version]Downgrade the running release to a new version
versionsPrint versions of the release available

To understand how these work see the OTP Design Principles chapter Release Handling.

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

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

  • Installing: Installing a release on a running system will unpack and upgrade the version: bin/myrel install 0.0.1

  • Listing: you can inspect what versions are currently available: bin/myrel versions

  • 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

Hooks

It is possible to define hooks on specific operations of the extended start script, the operations are start, stop, install_upgrade, pre and post hooks for each of these operations are available.

The hooks can either be builtin (ie. they are already included in the release) or custom (scripts written by the user for custom functionality), the builtin scripts offer pre-packaged functionality, they are:

  • pid : Writes the beam pid to a configurable file location

(/var/run/.pid by default).

  • wait_for_vm_start : Waits for the vm to start (ie. when it can be pinged).

  • wait_for_process : Waits for a configurable name to appear in the Erlang process registry.

  1. {extended_start_script_hooks, [
  2. {pre_start, [{custom, "hooks/pre_start"}]},
  3. {post_start, [
  4. {pid, "/tmp/foo.pid"},
  5. {wait_for_process, some_process},
  6. {custom, "hooks/post_start"}
  7. ]},
  8. {pre_stop, [
  9. {custom, "hooks/pre_stop"}]},
  10. {post_stop, [{custom, "hooks/post_stop"}]},
  11. ]},
  12. {post_stop, [{custom, "hooks/post_stop"}]}
  13. ]}.

Extensions

The extended start script that is generated comes with a builtin set of commands that allows you to manage your release, these are foreground, stop, restart, etc.

Sometimes it’s useful to expose some custom commands that are specific to your application. For example if you’re running a game server it would be convenient to just call bin/gameserver games that outputs useful information.

Extended start script extensions allow you to create a custom shell script that gets appended to the list of commands available to your start script. The extension shell script can take arguments and has access to all shell variables defined in the start script itself. You begin by defining the extension in your rebar.config, for example:

  1. %% start script extensions
  2. {extended_start_script_extensions, [
  3. {status, "extensions/status"}
  4. ]}.

Here you are adding the status script extension that will invoke an extensions/status shell script.

This path is relative to the location of the start script on the generated release so you probably will want to use a overlay to place it at the correct location:

  1. {copy, "scripts/extensions/status", "bin/extensions/status"},

The extension script itself is standard shell script, the game server example described could be implemented in the following way:

  1. #!/bin/bash
  2. case $1 in
  3. help)
  4. echo "bin/gameserver status"
  5. ;;
  6. *)
  7. ;;
  8. esac
  9. # get the status tuple from gameserver
  10. Status=$(relx_nodetool eval "pool_debug:status(json).")
  11. # now print it out
  12. code="Json = binary_to_list($Status),
  13. io:format(\"~p~n\", [Json]),
  14. halt()."
  15. echo $(erl -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$code")

Other Configurations

The RELX_RPC_TIMEOUT environment value can be set on the target system running a release to choose how long the scripts can wait before giving up on contacting the running Erlang system. It defaults to the NODETOOL_TIMEOUT value (converted from milliseconds to seconds) if no value is specified, and if NODETOOL_TIMEOUT itself is not set, the default is 60 seconds.

References