1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">
<chapter>
<header>
<copyright>
<year>2002</year><year>2014</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
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.
</legalnotice>
<title>Creating and Upgrading a Target System</title>
<prepared>Peter Högfeldt</prepared>
<responsible></responsible>
<docno></docno>
<approved></approved>
<checked></checked>
<date>2002-09-17</date>
<rev>A</rev>
<file>create_target.xml</file>
</header>
<section>
<title>Introduction</title>
<p>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.</p>
<p>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.</p>
<p>This chapter is about creating such a system, which we call a
<em>target system</em>.</p>
<p>In the following sections we consider creating target systems with
different requirements of functionality:</p>
<list type="bulleted">
<item>a <em>basic target system</em> that can be started by
calling the ordinary <c>erl</c> script, </item>
<item>a <em>simple target system</em> where also code
replacement in run-time can be performed, and</item>
<item>an <em>embedded target system</em> 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. </item>
</list>
<p>We only consider the case when Erlang/OTP is running on a UNIX
system.</p>
<p>In the <c>sasl</c> application there is an example Erlang
module <c>target_system.erl</c> 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.</p>
</section>
<section>
<marker id="create"/>
<title>Creating a Target System</title>
<p>It is assumed that you have a working Erlang/OTP system structured
according to the OTP Design Principles.</p>
<p><em>Step 1.</em> First create a <c>.rel</c> file (see <seealso
marker="sasl:rel">rel(4)</seealso>) that specifies the <c>erts</c>
version and lists all applications that should be included in the
new basic target system. An example is the following
<c>mysystem.rel</c> file:</p>
<code type="none">
%% mysystem.rel
{release,
{"MYSYSTEM", "FIRST"},
{erts, "5.10.4"},
[{kernel, "2.16.4"},
{stdlib, "1.19.4"},
{sasl, "2.3.4"},
{pea, "1.0"}]}.</code>
<p>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
<c>pea</c>). </p>
<p><em>Step 2.</em> From the directory where the <c>mysystem.rel</c>
file reside, start the Erlang/OTP system:</p>
<pre>
os> <input>erl -pa /home/user/target_system/myapps/pea-1.0/ebin</input></pre>
<p>where also the path to the <c>pea-1.0</c> ebin directory is
provided. </p>
<p><em>Step 3.</em> Now create the target system: </p>
<pre>
1> <input>target_system:create("mysystem").</input></pre>
<p>The <c>target_system:create/1</c> function does the following:</p>
<list type="ordered">
<item>Reads the <c>mysystem.rel</c> file, and creates a new file
<c>plain.rel</c> which is identical to former, except that it
only lists the <c>kernel</c> and <c>stdlib</c> applications. </item>
<item>From the <c>mysystem.rel</c> and <c>plain.rel</c> files
creates the files <c>mysystem.script</c>,
<c>mysystem.boot</c>, <c>plain.script</c>, and
<c>plain.boot</c> through a call to
<c>systools:make_script/2</c>.</item>
<item>
<p>Creates the file <c>mysystem.tar.gz</c> by a call to
<c>systools:make_tar/2</c>. That file has the following
contents:</p>
<code type="none">
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/ </code>
<p>The file <c>releases/FIRST/start.boot</c> is a copy of our
<c>mysystem.boot</c></p>
<p>The release resource file <c>mysystem.rel</c> is duplicated
in the tar file. Originally, this file was only stored in
the <c>releases</c> directory in order to make it possible
for the <c>release_handler</c> to extract this file
separately. After unpacking the tar
file, <c>release_handler</c> would automatically copy the
file to <c>releases/FIRST</c>. However, sometimes the tar
file is unpacked without involving
the <c>release_handler</c> (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.</p>
</item>
<item>Creates the temporary directory <c>tmp</c> and extracts the tar file
<c>mysystem.tar.gz</c> into that directory. </item>
<item>Deletes the <c>erl</c> and <c>start</c> files from
<c>tmp/erts-5.10.4/bin</c>. These files will be created again from
source when installing the release.</item>
<item>Creates the directory <c>tmp/bin</c>.</item>
<item>Copies the previously created file <c>plain.boot</c> to
<c>tmp/bin/start.boot</c>.</item>
<item>Copies the files <c>epmd</c>, <c>run_erl</c>, and
<c>to_erl</c> from the directory <c>tmp/erts-5.10.4/bin</c> to
the directory <c>tmp/bin</c>.</item>
<item>Creates the directory <c>tmp/log</c>, which will be used
if the system is started as embedded with the <c>bin/start</c>
script.</item>
<item>Creates the file <c>tmp/releases/start_erl.data</c> with
the contents "5.10.4 FIRST". This file is to be passed as data
file to the <c>start_erl</c> script.
</item>
<item>Recreates the file <c>mysystem.tar.gz</c> from the directories
in the directory <c>tmp</c>, and removes <c>tmp</c>.</item>
</list>
</section>
<section>
<title>Installing a Target System</title>
<p><em>Step 4.</em> Install the created target system in a
suitable directory. </p>
<pre>
2> <input>target_system:install("mysystem", "/usr/local/erl-target").</input></pre>
<p>The function <c>target_system:install/2</c> does the following:
</p>
<list type="ordered">
<item>Extracts the tar file <c>mysystem.tar.gz</c> into the target
directory <c>/usr/local/erl-target</c>.</item>
<item>In the target directory reads the file <c>releases/start_erl.data</c>
in order to find the Erlang runtime system version ("5.10.4").</item>
<item>Substitutes <c>%FINAL_ROOTDIR%</c> and <c>%EMU%</c> for
<c>/usr/local/erl-target</c> and <c>beam</c>, respectively, in
the files <c>erl.src</c>, <c>start.src</c>, and
<c>start_erl.src</c> of the target <c>erts-5.10.4/bin</c>
directory, and puts the resulting files <c>erl</c>,
<c>start</c>, and <c>run_erl</c> in the target <c>bin</c>
directory.</item>
<item>Finally the target <c>releases/RELEASES</c> file is created
from data in the <c>releases/mysystem.rel</c> file.</item>
</list>
</section>
<section>
<marker id="start"/>
<title>Starting a Target System</title>
<p>Now we have a target system that can be started in various ways.</p>
<p>We start it as a <em>basic target system</em> by invoking</p>
<pre>
os> <input>/usr/local/erl-target/bin/erl</input></pre>
<p>where only the <c>kernel</c> and <c>stdlib</c> 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:
<c>bin/erl</c> file (obtained from <c>erts-5.10.4/bin/erl.src</c>)
and the <c>bin/start.boot</c> file (a copy of <c>plain.boot</c>).</p>
<p>We can also start a distributed system (requires <c>bin/epmd</c>).</p>
<p>To start all applications specified in the original
<c>mysystem.rel</c> file, use the <c>-boot</c> flag as follows:</p>
<pre>
os> <input>/usr/local/erl-target/bin/erl -boot /usr/local/erl-target/releases/FIRST/start</input></pre>
<p>We start a <em>simple target system</em> as above. The only difference
is that also the file <c>releases/RELEASES</c> is present for
code replacement in run-time to work.</p>
<p>To start an <em>embedded target system</em> the shell script
<c>bin/start</c> is used. That shell script calls
<c>bin/run_erl</c>, which in turn calls <c>bin/start_erl</c>
(roughly, <c>start_erl</c> is an embedded variant of
<c>erl</c>). </p>
<p>The shell script <c>start</c>, 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.</p>
<p><c>run_erl</c> 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 (<c>to_erl</c>).</p>
<p><c>start_erl</c> requires the root directory
(<c>"/usr/local/erl-target"</c>), the releases directory
(<c>"/usr/local/erl-target/releases"</c>), and the location of
the <c>start_erl.data</c> file. It reads the run-time system
version (<c>"5.10.4"</c>) and release version (<c>"FIRST"</c>) from
the <c>start_erl.data</c> file, starts the run-time system of the
version found, and provides <c>-boot</c> flag specifying the boot
file of the release version found
(<c>"releases/FIRST/start.boot"</c>).</p>
<p><c>start_erl</c> also assumes that there is <c>sys.config</c> in
release version directory (<c>"releases/FIRST/sys.config"</c>). That
is the topic of the next section (see below).</p>
<p>The <c>start_erl</c> shell script should normally not be
altered by the user.</p>
</section>
<section>
<title>System Configuration Parameters</title>
<p>As was pointed out above <c>start_erl</c> requires a
<c>sys.config</c> in the release version directory
(<c>"releases/FIRST/sys.config"</c>). If there is no such a
file, the system start will fail. Hence such a file has to
be added as well.</p>
<p></p>
<p>If you have system configuration data that are neither file
location dependent nor site dependent, it may be convenient to
create the <c>sys.config</c> early, so that it becomes a part of
the target system tar file created by
<c>target_system:create/1</c>. In fact, if you create, in the
current directory, not only the <c>mysystem.rel</c> file, but
also a <c>sys.config</c> file, that latter file will be tacitly
put in the appropriate directory.</p>
</section>
<section>
<title>Differences from the Install Script</title>
<p>The above <c>install/2</c> procedure differs somewhat from that
of the ordinary <c>Install</c> shell script. In fact, <c>create/1</c>
makes the release package as complete as possible, and leave to the
<c>install/2</c> procedure to finish by only considering location
dependent files.</p>
</section>
<section>
<title>Creating the Next Version</title>
<p>
In this example the <c>pea</c> application has been changed, and
so are <c>erts</c>, <c>kernel</c>, <c>stdlib</c> and
<c>sasl</c>.
</p>
<p>
<em>Step 1.</em> Create the <c>.rel</c> file:
</p>
<code type="none">
%% mysystem2.rel
{release,
{"MYSYSTEM", "SECOND"},
{erts, "6.0"},
[{kernel, "3.0"},
{stdlib, "2.0"},
{sasl, "2.4"},
{pea, "2.0"}]}.</code>
<p>
<em>Step 2.</em> Create the application upgrade file (see
<seealso marker="sasl:appup">appup(4)</seealso>) for <c>pea</c>,
for example:
</p>
<code type="none">
%% pea.appup
{"2.0",
[{"1.0",[{load_module,pea_lib}]}],
[{"1.0",[{load_module,pea_lib}]}]}.</code>
<p>
<em>Step 3.</em> From the directory where the
<c>mysystem2.rel</c> file reside, start the Erlang/OTP system:
</p>
<pre>
os> <input>erl -pa /home/user/target_system/myapps/pea-2.0/ebin</input></pre>
<p>giving the path to the new version of <c>pea</c>. </p>
<p>
<em>Step 4.</em> Create the release upgrade file (see <seealso
marker="sasl:relup">relup(4)</seealso>):
</p>
<pre>
1> <input>systools:make_relup("mysystem2",["mysystem"],["mysystem"],[{path,["/home/user/target_system/myapps/pea-1.0/ebin","/my/old/erlang/lib/*/ebin"]}]).</input></pre>
<p>
where <c>"mysystem"</c> is the base release and
<c>"mysystem2"</c> is the release to upgrade to.
</p>
<p>
Note that the <c>path</c> 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.)
</p>
<p>
<em>Step 5.</em> Create the new release:
</p>
<pre>
2> <input>target_system:create("mysystem2").</input></pre>
<p>
Given that the <c>relup</c> file generated in step 4 above is
now located in the current directory, it will automatically be
included in the release package.
</p>
</section>
<section>
<title>Upgrading the Target System</title>
<p>
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
<c>-heart</c> option, allowing automatic restart of the
node. See <seealso marker="#start">Starting a Target
System</seealso> above for more information.
</p>
<p>
We add <c>-heart</c> to <c>bin/start</c>:
</p>
<code type="none">
#!/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</code>
<p>
And we use the simplest possible <c>sys.config</c>, which we
store in <c>releases/FIRST</c>:
</p>
<code type="none">
%% sys.config
[].</code>
<p>
Finally, in order to prepare the upgrade, we need to put the new
release package in the <c>releases</c> directory of the first
target system:
</p>
<pre>
os> <input>cp mysystem2.tar.gz /usr/local/erl-target/releases</input></pre>
<p>
And assuming that the node has been started like this:
</p>
<pre>
os> <input>/usr/local/erl-target/bin/start</input></pre>
<p>
it can be accessed like this:
</p>
<pre>
os> <input>/usr/local/erl-target/bin/to_erl /tmp/erlang.pipe.1</input></pre>
<p>
Also note that logs can be found in
<c>/usr/local/erl-target/log</c>. This directory is specified as
an argument to <c>run_erl</c>in the start script listed above.
</p>
<p>
<em>Step 1.</em> Unpack the release:
</p>
<pre>
1> <input>{ok,Vsn} = release_handler:unpack_release("mysystem2").</input></pre>
<p>
<em>Step 2.</em> Install the release:
</p>
<pre>
2> <input>release_handler:install_release(Vsn).</input>
<output>{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]</output></pre>
<p>
The above return value and output after the call to
<c>release_handler:install_release/1</c> means that the
<c>release_handler</c> has restarted the node by using
<c>heart</c>. This will always be done when the upgrade involves
a change of <c>erts</c>, <c>kernel</c>, <c>stdlib</c> or
<c>sasl</c>. See <seealso marker="upgrade">Upgrade when
Erlang/OTP has Changed</seealso> for more infomation about this.
</p>
<p>
The node will be accessible via a new pipe:
</p>
<pre>
os> <input>/usr/local/erl-target/bin/to_erl /tmp/erlang.pipe.2</input></pre>
<p>
Let's see which releases we have in our system:
</p>
<pre>
1> <input>release_handler:which_releases().</input>
<output>[{"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}]</output></pre>
<p>
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.
</p>
<p>
<em>Step 3.</em> Make the new release permanent:
</p>
<pre>
2> <input>release_handler:make_permanent("SECOND").</input></pre>
<p>
Now look at the releases again:
</p>
<pre>
3> <input>release_handler:which_releases().</input>
<output>[{"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}]</output></pre>
<p>
Here we see that the new release version is <c>permanent</c>, so
it would be safe to restart the node.
</p>
</section>
<section>
<title>Listing of target_system.erl</title>
<p>This module can also be found in the <c>examples</c> directory
of the <c>sasl</c> application.</p>
<codeinclude file="../../../lib/sasl/examples/src/target_system.erl" tag="%module" type="erl"></codeinclude>
</section>
</chapter>
|