Releases
A directory of beams on the code path is not a system. A release is the thing that can be copied to a machine and started: the Erlang runtime, the applications of the system with the exact versions that were built, a boot script that starts them in order, and a configuration file. Erlang builds it with relx, which ships inside rebar3, so no package from outside OTP is needed. The applications lesson builds the application this lesson puts in a release.
A release project
The release template writes a project whose product is a release rather than a library. The difference is the apps folder: the application lives in apps/myrel, and the top of the project holds only the build configuration and the runtime configuration of the release:
rebar3 new release myrel
===> Writing myrel/apps/myrel/src/myrel_app.erl ===> Writing myrel/apps/myrel/src/myrel_sup.erl ===> Writing myrel/apps/myrel/src/myrel.app.src ===> Writing myrel/rebar.config ===> Writing myrel/config/sys.config ===> Writing myrel/config/vm.args ===> Writing myrel/.gitignore ===> Writing myrel/LICENSE.md ===> Writing myrel/README.md
The layout of the project, before the first build:
myrel/ ├── apps/ │ └── myrel/ │ └── src/ │ ├── myrel.app.src │ ├── myrel_app.erl │ └── myrel_sup.erl ├── config/ │ ├── sys.config │ └── vm.args ├── rebar.config ├── .gitignore ├── LICENSE.md └── README.md
rebar.config names the release and what goes into it. Its relx section holds a release tuple with the name, the version and the list of applications, and the paths of the two configuration files; the project layout lesson reads the rest of the file.
Build the release
rebar3 release compiles the applications, resolves the versions of everything in the release, copies the modules in, and assembles a folder that runs by itself:
rebar3 release
===> Verifying dependencies... ===> Analyzing applications... ===> Compiling myrel ===> Assembling release myrel-0.1.0... ===> Release successfully assembled: _build/default/rel/myrel
That command takes longer than a compile and cannot be run by the checker of this page, because the release it assembles is meant to be started, so the output above was captured by hand from the same image. The result is a folder under _build, and nothing in it points back at the project: that is what makes it a release.
What a release holds
The assembled folder has four parts. lib holds every application of the release at the version the build chose, kernel and stdlib included. releases holds the boot scripts of the release and its manifest. bin holds the start script. log is where the default configuration writes:
_build/default/rel/myrel/ ├── bin/ │ ├── myrel │ ├── myrel-0.1.0 │ ├── install_upgrade.escript │ └── nodetool ├── lib/ │ ├── kernel-10.2.7.5/ │ ├── stdlib-6.2.2.5/ │ ├── sasl-4.2.2.1/ │ └── myrel-0.1.0/ ├── releases/ │ ├── RELEASES │ ├── start_erl.data │ └── 0.1.0/ │ ├── myrel.boot │ ├── myrel.rel │ ├── myrel.script │ ├── sys.config │ └── vm.args └── log/
myrel.boot is the boot script: the list of applications the runtime starts, in order, with the modules each of them loads. myrel.rel is the source of that script, and start_erl.data names the runtime version and the release version, which is how bin/myrel finds the right boot script after an upgrade. The manifest lists every release the folder has ever held:
releases/RELEASES
{release,"myrel","0.1.0","15.2.7.14", [{kernel,"10.2.7.5","./lib/kernel-10.2.7.5"}, {stdlib,"6.2.2.5","./lib/stdlib-6.2.2.5"}, {myrel,"0.1.0","./lib/myrel-0.1.0"}, {sasl,"4.2.2.1","./lib/sasl-4.2.2.1"}], permanent}.
Each entry names an application, its version and the folder under lib that holds it, and the last element is the status of the release in the running system. An upgrade adds an entry; it never edits the old one, which is what makes a rollback possible.
The keys a release copies
The shell returns to the project folder, where the programs of this page run:
cd ..
The release builder reads the application resource of every application it copies, so the keys of that file decide what ends up in the release and in what order it starts. A program can read the same keys of a resource it loads:
app_keys.erl
-module(app_keys). -export([main/0]). main() -> Spec = {application, demo, [{description, "A counter as an OTP application"}, {vsn, "0.1.0"}, {modules, [counter_app, counter_sup, counter]}, {registered, [counter_sup]}, {applications, [kernel, stdlib]}, {mod, {counter_app, []}}]}, ok = application:load(Spec), io:format("vsn: ~p~n", [application:get_key(demo, vsn)]), io:format("modules: ~p~n", [application:get_key(demo, modules)]), io:format("applications: ~p~n", [application:get_key(demo, applications)]), io:format("mod: ~p~n", [application:get_key(demo, mod)]), ok = application:unload(demo).
erlc app_keys.erl
erl -noshell -eval 'app_keys:main(), halt().'
vsn: {ok,"0.1.0"} modules: {ok,[counter_app,counter_sup,counter]} applications: {ok,[kernel,stdlib]} mod: {ok,{counter_app,[]}}
The version and the modules are what the release records; the applications list is what the boot script starts before this one, which is why a missing entry shows up as undef at boot rather than at build time.
The sys.config
config/sys.config is the configuration of the whole node: one entry per application, and the values become the application environment at boot. The scaffold writes an empty list, and a project fills it with the settings of its own applications. The file is a list of Erlang terms, so file:consult/1 reads it, and a value read that way can be handed to application:load/1 as the env key:
sys_config.erl
-module(sys_config). -export([main/0]). main() -> ok = file:write_file("sys.config", "[{demo, [{limit, 7}]}].\n"), {ok, [Config]} = file:consult("sys.config"), {demo, Env} = lists:keyfind(demo, 1, Config), io:format("env: ~p~n", [Env]), Spec = {application, demo, [{description, "demo"}, {vsn, "0.1.0"}, {modules, []}, {applications, [kernel, stdlib]}, {env, Env}]}, ok = application:load(Spec), io:format("limit: ~p~n", [application:get_env(demo, limit)]), ok = application:unload(demo).
erlc sys_config.erl
erl -noshell -eval 'sys_config:main(), halt().'
env: [{limit,7}] limit: {ok,7}
file:consult/1 answers the terms of the file in one list, so the pattern is {ok, [Config]} for a file with one term, and lists:keyfind/3 picks the application. In a release the file is read by the boot process, not by the application, and vm.args next to it holds the flags of the runtime: the node name, the cookie and the scheduler settings.
Running and upgrading
bin/myrel foreground boots the release in the terminal and
bin/myrel daemon in the background; console
adds a shell, and bin/myrel eval "Expression" runs one expression
against the running node. The command does not depend on a project, a build tool or the
source: it needs the folder and nothing else. This page shows those commands without
their output, because the checker of the page cannot start a node that never ends.
An upgrade is the reason for the releases folder. A new version of
an application ships with an .appup file that says how to move
each module from the old version to the new one, and the release tools turn the two
versions into a relup: a script that loads the new code, runs
code_change/3 in every process that is still running, and can be
reversed. The hot upgrades lesson writes
the callback, and the
design principles lesson says
what a release must not depend on for that to work.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a release add to a folder of compiled applications?
Answer
A runtime, a boot script and a configuration
The release folder carries the applications at fixed versions, the boot script that starts them and the config, and runs without the project.
-
Which tool builds the release inside rebar3?
Answer
relx
The
relxsection ofrebar.confignames the release, and rebar3 ships relx. -
Which folder of the release holds the applications?
Answer
libEvery application of the release is copied under
lib/<name>-<version>. -
What does
releases/start_erl.datahold?Answer
The runtime version and the release version
The start script reads it to find the boot script of the current release.
-
What is the
RELEASESfile for?Answer
It lists every release the folder has held, with versions and status
An upgrade adds an entry, and the old one stays, which is what makes a rollback possible.
-
What is a
relup?Answer
A script that moves a running system from one release to another
The release tools build it from the two versions and their
.appupfiles, and the release handler runs it. -
What does this module print?
-module(q1). -export([main/0]). main() -> ok = application:load({application, demo, [{description, "demo"}, {vsn, "0.1.0"}, {modules, [alpha, beta]}, {applications, [kernel, stdlib]}]}), io:format("~p~n", [application:get_key(demo, modules)]).Answer
{ok,[alpha,beta]}get_key/2answers the value of the key in an ok tuple, and the modules are the list the resource declares. -
What does this module print?
-module(q2). -export([main/0]). main() -> ok = file:write_file("sys.config", "[{demo, [{limit, 7}]}].\n"), {ok, [Config]} = file:consult("sys.config"), {demo, Env} = lists:keyfind(demo, 1, Config), io:format("~p~n", [proplists:get_value(limit, Env)]).Answer
7The file holds one term, its demo entry is a proplist, and
get_valuereads the number. -
What does this module print?
-module(q3). -export([main/0]). main() -> ok = application:load({application, demo, [{description, "demo"}, {vsn, "2.0.0"}, {modules, []}, {applications, [kernel, stdlib]}]}), io:format("~p~n", [application:get_key(demo, vsn)]), ok = application:unload(demo), io:format("~p~n", [application:get_key(demo, vsn)]).Answer
{ok,"2.0.0"}/undefinedAfter
unload/1the application is not loaded, so the key isundefined. -
What does this module print?
-module(q4). -export([main/0]). main() -> ok = application:load({application, demo, [{description, "demo"}, {vsn, "0.1.0"}, {modules, []}, {applications, [kernel, stdlib]}]}), {ok, _Started} = application:ensure_all_started(demo), {ok, _Again} = application:ensure_all_started(demo), io:format("~p~n", [lists:keymember(demo, 1, application:which_applications())]).Answer
trueStarting an application twice is not an error: the second call finds it running and the application is in the list.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) and rebar3 3.27.3 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Автор статьи: Arthur Isaev