20022014 Ericsson AB. All Rights Reserved. The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use this file except in compliance with the License. You should have received a copy of the Erlang Public License along with this software. If not, it can be retrieved online at http://www.erlang.org/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Creating and Upgrading a Target System Peter Högfeldt 2002-09-17 A create_target.xml
Introduction

When creating a system using Erlang/OTP, the most simple way is to install Erlang/OTP somewhere, install the application specific code somewhere else, and then start the Erlang runtime system, making sure the code path includes the application specific code.

Often it is not desirable to use an Erlang/OTP system as is. A developer may create new Erlang/OTP compliant applications for a particular purpose, and several original Erlang/OTP applications may be irrelevant for the purpose in question. Thus, there is a need to be able to create a new system based on a given Erlang/OTP system, where dispensable applications are removed, and a set of new applications are included. Documentation and source code is irrelevant and is therefore not included in the new system.

This chapter is about creating such a system, which we call a target system.

In the following sections we consider creating target systems with different requirements of functionality:

a basic target system that can be started by calling the ordinary erl script, a simple target system where also code replacement in run-time can be performed, and an embedded target system where there is also support for logging output from the system to file for later inspection, and where the system can be started automatically at boot time.

We only consider the case when Erlang/OTP is running on a UNIX system.

In the sasl application there is an example Erlang module target_system.erl that contains functions for creating and installing a target system. This module is used in the examples below, and the source code of the module is listed at the end of this chapter.

Creating a Target System

It is assumed that you have a working Erlang/OTP system structured according to the OTP Design Principles.

Step 1. First create a .rel file (see rel(4)) that specifies the erts version and lists all applications that should be included in the new basic target system. An example is the following mysystem.rel file:

%% mysystem.rel {release, {"MYSYSTEM", "FIRST"}, {erts, "5.10.4"}, [{kernel, "2.16.4"}, {stdlib, "1.19.4"}, {sasl, "2.3.4"}, {pea, "1.0"}]}.

The listed applications are not only original Erlang/OTP applications but possibly also new applications that you have written yourself (here exemplified by the application pea).

Step 2. From the directory where the mysystem.rel file reside, start the Erlang/OTP system:

os> erl -pa /home/user/target_system/myapps/pea-1.0/ebin

where also the path to the pea-1.0 ebin directory is provided.

Step 3. Now create the target system:

1> target_system:create("mysystem").

The target_system:create/1 function does the following:

Reads the mysystem.rel file, and creates a new file plain.rel which is identical to former, except that it only lists the kernel and stdlib applications. From the mysystem.rel and plain.rel files creates the files mysystem.script, mysystem.boot, plain.script, and plain.boot through a call to systools:make_script/2.

Creates the file mysystem.tar.gz by a call to systools:make_tar/2. That file has the following contents:

erts-5.10.4/bin/ releases/FIRST/start.boot releases/FIRST/mysystem.rel releases/mysystem.rel lib/kernel-2.16.4/ lib/stdlib-1.19.4/ lib/sasl-2.3.4/ lib/pea-1.0/

The file releases/FIRST/start.boot is a copy of our mysystem.boot

The release resource file mysystem.rel is duplicated in the tar file. Originally, this file was only stored in the releases directory in order to make it possible for the release_handler to extract this file separately. After unpacking the tar file, release_handler would automatically copy the file to releases/FIRST. However, sometimes the tar file is unpacked without involving the release_handler (e.g. when unpacking the first target system) and therefore the file is now instead duplicated in the tar file so no manual copying is necessary.

Creates the temporary directory tmp and extracts the tar file mysystem.tar.gz into that directory. Deletes the erl and start files from tmp/erts-5.10.4/bin. These files will be created again from source when installing the release. Creates the directory tmp/bin. Copies the previously created file plain.boot to tmp/bin/start.boot. Copies the files epmd, run_erl, and to_erl from the directory tmp/erts-5.10.4/bin to the directory tmp/bin. Creates the directory tmp/log, which will be used if the system is started as embedded with the bin/start script. Creates the file tmp/releases/start_erl.data with the contents "5.10.4 FIRST". This file is to be passed as data file to the start_erl script. Recreates the file mysystem.tar.gz from the directories in the directory tmp, and removes tmp.
Installing a Target System

Step 4. Install the created target system in a suitable directory.

2> target_system:install("mysystem", "/usr/local/erl-target").

The function target_system:install/2 does the following:

Extracts the tar file mysystem.tar.gz into the target directory /usr/local/erl-target. In the target directory reads the file releases/start_erl.data in order to find the Erlang runtime system version ("5.10.4"). Substitutes %FINAL_ROOTDIR% and %EMU% for /usr/local/erl-target and beam, respectively, in the files erl.src, start.src, and start_erl.src of the target erts-5.10.4/bin directory, and puts the resulting files erl, start, and run_erl in the target bin directory. Finally the target releases/RELEASES file is created from data in the releases/mysystem.rel file.
Starting a Target System

Now we have a target system that can be started in various ways.

We start it as a basic target system by invoking

os> /usr/local/erl-target/bin/erl

where only the kernel and stdlib applications are started, i.e. the system is started as an ordinary development system. There are only two files needed for all this to work: bin/erl file (obtained from erts-5.10.4/bin/erl.src) and the bin/start.boot file (a copy of plain.boot).

We can also start a distributed system (requires bin/epmd).

To start all applications specified in the original mysystem.rel file, use the -boot flag as follows:

os> /usr/local/erl-target/bin/erl -boot /usr/local/erl-target/releases/FIRST/start

We start a simple target system as above. The only difference is that also the file releases/RELEASES is present for code replacement in run-time to work.

To start an embedded target system the shell script bin/start is used. That shell script calls bin/run_erl, which in turn calls bin/start_erl (roughly, start_erl is an embedded variant of erl).

The shell script start, which is generated from erts-5.10.4/bin/start.src during installation, is only an example. You should edit it to suite your needs. Typically it is executed when the UNIX system boots.

run_erl is a wrapper that provides logging of output from the run-time system to file. It also provides a simple mechanism for attaching to the Erlang shell (to_erl).

start_erl requires the root directory ("/usr/local/erl-target"), the releases directory ("/usr/local/erl-target/releases"), and the location of the start_erl.data file. It reads the run-time system version ("5.10.4") and release version ("FIRST") from the start_erl.data file, starts the run-time system of the version found, and provides -boot flag specifying the boot file of the release version found ("releases/FIRST/start.boot").

start_erl also assumes that there is sys.config in release version directory ("releases/FIRST/sys.config"). That is the topic of the next section (see below).

The start_erl shell script should normally not be altered by the user.

System Configuration Parameters

As was pointed out above start_erl requires a sys.config in the release version directory ("releases/FIRST/sys.config"). If there is no such a file, the system start will fail. Hence such a file has to be added as well.

If you have system configuration data that are neither file location dependent nor site dependent, it may be convenient to create the sys.config early, so that it becomes a part of the target system tar file created by target_system:create/1. In fact, if you create, in the current directory, not only the mysystem.rel file, but also a sys.config file, that latter file will be tacitly put in the appropriate directory.

Differences from the Install Script

The above install/2 procedure differs somewhat from that of the ordinary Install shell script. In fact, create/1 makes the release package as complete as possible, and leave to the install/2 procedure to finish by only considering location dependent files.

Creating the Next Version

In this example the pea application has been changed, and so are erts, kernel, stdlib and sasl.

Step 1. Create the .rel file:

%% mysystem2.rel {release, {"MYSYSTEM", "SECOND"}, {erts, "6.0"}, [{kernel, "3.0"}, {stdlib, "2.0"}, {sasl, "2.4"}, {pea, "2.0"}]}.

Step 2. Create the application upgrade file (see appup(4)) for pea, for example:

%% pea.appup {"2.0", [{"1.0",[{load_module,pea_lib}]}], [{"1.0",[{load_module,pea_lib}]}]}.

Step 3. From the directory where the mysystem2.rel file reside, start the Erlang/OTP system:

os> erl -pa /home/user/target_system/myapps/pea-2.0/ebin

giving the path to the new version of pea.

Step 4. Create the release upgrade file (see relup(4)):

1> systools:make_relup("mysystem2",["mysystem"],["mysystem"],[{path,["/home/user/target_system/myapps/pea-1.0/ebin","/my/old/erlang/lib/*/ebin"]}]).

where "mysystem" is the base release and "mysystem2" is the release to upgrade to.

Note that the path option is used for pointing out the old version of all applications. (The new versions are already in the code path - assuming of course that the erlang node on which this is executed is running the correct version of Erlang/OTP.)

Step 5. Create the new release:

2> target_system:create("mysystem2").

Given that the relup file generated in step 4 above is now located in the current directory, it will automatically be included in the release package.

Upgrading the Target System

This part is done on the target node, and for this example we want the node to be running as an embedded system with the -heart option, allowing automatic restart of the node. See Starting a Target System above for more information.

We add -heart to bin/start:

#!/bin/sh ROOTDIR=/usr/local/erl-target/ if [ -z "$RELDIR" ] then RELDIR=$ROOTDIR/releases fi START_ERL_DATA=${1:-$RELDIR/start_erl.data} $ROOTDIR/bin/run_erl -daemon /tmp/ $ROOTDIR/log "exec $ROOTDIR/bin/start_erl $ROOTDIR $RELDIR $START_ERL_DATA -heart

And we use the simplest possible sys.config, which we store in releases/FIRST:

%% sys.config [].

Finally, in order to prepare the upgrade, we need to put the new release package in the releases directory of the first target system:

os> cp mysystem2.tar.gz /usr/local/erl-target/releases

And assuming that the node has been started like this:

os> /usr/local/erl-target/bin/start

it can be accessed like this:

os> /usr/local/erl-target/bin/to_erl /tmp/erlang.pipe.1

Also note that logs can be found in /usr/local/erl-target/log. This directory is specified as an argument to run_erlin the start script listed above.

Step 1. Unpack the release:

1> {ok,Vsn} = release_handler:unpack_release("mysystem2").

Step 2. Install the release:

2> release_handler:install_release(Vsn).
{continue_after_restart,"FIRST",[]}
heart: Tue Apr  1 12:15:10 2014: Erlang has closed.
heart: Tue Apr  1 12:15:11 2014: Executed "/usr/local/erl-target/bin/start /usr/local/erl-target/releases/new_start_erl.data" -> 0. Terminating.
[End]

The above return value and output after the call to release_handler:install_release/1 means that the release_handler has restarted the node by using heart. This will always be done when the upgrade involves a change of erts, kernel, stdlib or sasl. See Upgrade when Erlang/OTP has Changed for more infomation about this.

The node will be accessible via a new pipe:

os> /usr/local/erl-target/bin/to_erl /tmp/erlang.pipe.2

Let's see which releases we have in our system:

1> release_handler:which_releases().
[{"MYSYSTEM","SECOND",
  ["kernel-3.0","stdlib-2.0","sasl-2.4","pea-2.0"],
  current},
 {"MYSYSTEM","FIRST",
  ["kernel-2.16.4","stdlib-1.19.4","sasl-2.3.4","pea-1.0"],
  permanent}]

Our new release, "SECOND", is now the current release, but we can also see that our "FIRST" release is still permanent. This means that if the node would be restarted at this point, it would come up running the "FIRST" release again.

Step 3. Make the new release permanent:

2> release_handler:make_permanent("SECOND").

Now look at the releases again:

3> release_handler:which_releases().
[{"MYSYSTEM","SECOND",
  ["kernel-3.0","stdlib-2.0","sasl-2.4","pea-2.0"],
  permanent},
 {"MYSYSTEM","FIRST",
  ["kernel-2.16.4","stdlib-1.19.4","sasl-2.3.4","pea-1.0"],
  old}]

Here we see that the new release version is permanent, so it would be safe to restart the node.

Listing of target_system.erl

This module can also be found in the examples directory of the sasl application.