2006-01-27 06:02:33 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_update.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : ejabberd code updater
|
|
|
|
%%% Created : 27 Jan 2006 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2006-01-27 06:02:33 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_update).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2006-01-27 06:02:33 +01:00
|
|
|
|
|
|
|
%% API
|
2009-05-27 19:28:55 +02:00
|
|
|
-export([update/0, update/1, update_info/0]).
|
2006-01-27 06:02:33 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2008-03-21 15:44:16 +01:00
|
|
|
|
2006-01-27 06:02:33 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2009-05-27 19:28:55 +02:00
|
|
|
|
|
|
|
%% Update all the modified modules
|
2006-01-27 06:02:33 +01:00
|
|
|
update() ->
|
2006-02-27 05:43:16 +01:00
|
|
|
case update_info() of
|
2006-02-27 05:47:30 +01:00
|
|
|
{ok, Dir, _UpdatedBeams, _Script, LowLevelScript, _Check} ->
|
2006-02-27 05:43:16 +01:00
|
|
|
Eval =
|
2011-11-03 19:46:59 +01:00
|
|
|
eval_script(
|
2006-02-27 05:43:16 +01:00
|
|
|
LowLevelScript, [],
|
|
|
|
[{ejabberd, "", filename:join(Dir, "..")}]),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Eval: ~p~n", [Eval]),
|
2006-02-27 05:43:16 +01:00
|
|
|
Eval;
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
2009-05-27 19:28:55 +02:00
|
|
|
%% Update only the specified modules
|
|
|
|
update(ModulesToUpdate) ->
|
|
|
|
case update_info() of
|
|
|
|
{ok, Dir, UpdatedBeamsAll, _Script, _LowLevelScript, _Check} ->
|
|
|
|
UpdatedBeamsNow =
|
|
|
|
[A || A <- UpdatedBeamsAll, B <- ModulesToUpdate, A == B],
|
|
|
|
{_, LowLevelScript, _} = build_script(Dir, UpdatedBeamsNow),
|
|
|
|
Eval =
|
2011-11-03 19:46:59 +01:00
|
|
|
eval_script(
|
2009-05-27 19:28:55 +02:00
|
|
|
LowLevelScript, [],
|
|
|
|
[{ejabberd, "", filename:join(Dir, "..")}]),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Eval: ~p~n", [Eval]),
|
2009-05-27 19:28:55 +02:00
|
|
|
Eval;
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
2011-11-03 19:46:59 +01:00
|
|
|
eval_script(Script, Apps, LibDirs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
release_handler_1:eval_script(Script, Apps, LibDirs, [], []).
|
2011-11-03 19:46:59 +01:00
|
|
|
|
2009-05-27 19:28:55 +02:00
|
|
|
%% Get information about the modified modules
|
2006-02-27 05:43:16 +01:00
|
|
|
update_info() ->
|
2006-01-27 06:02:33 +01:00
|
|
|
Dir = filename:dirname(code:which(ejabberd)),
|
|
|
|
case file:list_dir(Dir) of
|
|
|
|
{ok, Files} ->
|
2009-05-27 19:28:55 +02:00
|
|
|
update_info(Dir, Files);
|
2006-01-27 06:02:33 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
2009-05-27 19:28:55 +02:00
|
|
|
update_info(Dir, Files) ->
|
|
|
|
Beams = lists:sort(get_beams(Files)),
|
|
|
|
UpdatedBeams = get_updated_beams(Beams),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("BEAM files: ~p~n", [UpdatedBeams]),
|
2009-05-27 19:28:55 +02:00
|
|
|
{Script, LowLevelScript, Check} = build_script(Dir, UpdatedBeams),
|
|
|
|
{ok, Dir, UpdatedBeams, Script, LowLevelScript, Check}.
|
|
|
|
|
|
|
|
get_beams(Files) ->
|
|
|
|
[list_to_atom(filename:rootname(FN))
|
|
|
|
|| FN <- Files, lists:suffix(".beam", FN)].
|
|
|
|
|
|
|
|
%% Return only the beams that have different version
|
|
|
|
get_updated_beams(Beams) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(Module) ->
|
|
|
|
NewVsn = get_new_version(Module),
|
|
|
|
case code:is_loaded(Module) of
|
|
|
|
{file, _} ->
|
|
|
|
CurVsn = get_current_version(Module),
|
|
|
|
(NewVsn /= CurVsn
|
|
|
|
andalso NewVsn /= unknown_version);
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, Beams).
|
|
|
|
|
|
|
|
get_new_version(Module) ->
|
|
|
|
Path = code:which(Module),
|
|
|
|
VersionRes = beam_lib:version(Path),
|
|
|
|
case VersionRes of
|
|
|
|
{ok, {Module, NewVsn}} -> NewVsn;
|
|
|
|
%% If a m1.erl has -module("m2"):
|
|
|
|
_ -> unknown_version
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_current_version(Module) ->
|
|
|
|
Attrs = Module:module_info(attributes),
|
2011-12-21 12:58:58 +01:00
|
|
|
case lists:keysearch(vsn, 1, Attrs) of
|
|
|
|
{value, {vsn, CurVsn}} -> CurVsn;
|
|
|
|
_ -> unknown_version
|
|
|
|
end.
|
2009-05-27 19:28:55 +02:00
|
|
|
|
|
|
|
%% @spec(Dir::string(), UpdatedBeams::[atom()]) -> {Script,LowLevelScript,Check}
|
|
|
|
build_script(Dir, UpdatedBeams) ->
|
|
|
|
Script = make_script(UpdatedBeams),
|
|
|
|
LowLevelScript = make_low_level_script(UpdatedBeams, Script),
|
|
|
|
Check =
|
|
|
|
release_handler_1:check_script(
|
|
|
|
LowLevelScript,
|
|
|
|
[{ejabberd, "", filename:join(Dir, "..")}]),
|
2013-01-03 11:26:16 +01:00
|
|
|
Check1 = case Check of
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, []} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Script: ~p~n", [Script]),
|
|
|
|
?DEBUG("Low level script: ~p~n", [LowLevelScript]),
|
|
|
|
?DEBUG("Check: ~p~n", [Check]),
|
2015-10-07 00:06:58 +02:00
|
|
|
ok;
|
|
|
|
_ ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Script: ~p~n", [Script]),
|
|
|
|
?ERROR_MSG("Low level script: ~p~n", [LowLevelScript]),
|
|
|
|
?ERROR_MSG("Check: ~p~n", [Check]),
|
2015-10-07 00:06:58 +02:00
|
|
|
error
|
|
|
|
end,
|
2013-01-03 11:26:16 +01:00
|
|
|
{Script, LowLevelScript, Check1}.
|
2009-05-27 19:28:55 +02:00
|
|
|
|
|
|
|
%% Copied from Erlang/OTP file: lib/sasl/src/systools.hrl
|
2019-06-24 19:32:34 +02:00
|
|
|
-record(application,
|
2006-01-27 06:02:33 +01:00
|
|
|
{name, %% Name of the application, atom().
|
|
|
|
type = permanent, %% Application start type, atom().
|
|
|
|
vsn = "", %% Version of the application, string().
|
|
|
|
id = "", %% Id of the application, string().
|
|
|
|
description = "", %% Description of application, string().
|
2019-06-24 19:32:34 +02:00
|
|
|
modules = [], %% [Module | {Module,Vsn}] of modules
|
|
|
|
%% incorporated in the application,
|
2006-01-27 06:02:33 +01:00
|
|
|
%% Module = atom(), Vsn = string().
|
|
|
|
uses = [], %% [Application] list of applications required
|
|
|
|
%% by the application, Application = atom().
|
|
|
|
includes = [], %% [Application] list of applications included
|
|
|
|
%% by the application, Application = atom().
|
2019-06-24 19:32:34 +02:00
|
|
|
regs = [], %% [RegNames] a list of registered process
|
2006-01-27 06:02:33 +01:00
|
|
|
%% names used by the application, RegNames =
|
|
|
|
%% atom().
|
2019-06-24 19:32:34 +02:00
|
|
|
env = [], %% [{Key,Value}] environment variable of
|
2006-01-27 06:02:33 +01:00
|
|
|
%% application, Key = Value = term().
|
2019-06-24 19:32:34 +02:00
|
|
|
maxT = infinity, %% Max time an application may exist,
|
2006-01-27 06:02:33 +01:00
|
|
|
%% integer() | infinity.
|
|
|
|
maxP = infinity, %% Max number of processes in an application,
|
|
|
|
%% integer() | infinity.
|
2019-06-24 19:32:34 +02:00
|
|
|
mod = [], %% [] | {Mod, StartArgs}, Mod= atom(),
|
2006-01-27 06:02:33 +01:00
|
|
|
%% StartArgs = list().
|
|
|
|
start_phases = [], %% [] | {Phase, PhaseArgs}, Phase = atom(),
|
|
|
|
%% PhaseArgs = list().
|
2019-06-24 19:32:34 +02:00
|
|
|
dir = "" %% The directory where the .app file was
|
2006-01-27 06:02:33 +01:00
|
|
|
%% found (internal use).
|
|
|
|
}).
|
|
|
|
|
|
|
|
|
|
|
|
make_script(UpdatedBeams) ->
|
|
|
|
lists:map(
|
|
|
|
fun(Module) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, {Module, [{attributes, NewAttrs}]}} =
|
2006-01-27 06:02:33 +01:00
|
|
|
beam_lib:chunks(code:which(Module), [attributes]),
|
2006-01-29 05:38:31 +01:00
|
|
|
CurAttrs = Module:module_info(attributes),
|
|
|
|
case lists:keysearch(update_info, 1, NewAttrs) of
|
|
|
|
{value, {_, [{update, _}]}} ->
|
|
|
|
case lists:keysearch(update_info, 1, CurAttrs) of
|
|
|
|
{value, {_, [{update, Extra}]}} ->
|
|
|
|
{update, Module, {advanced, Extra}};
|
|
|
|
false ->
|
|
|
|
{update, Module, {advanced, 0}}
|
|
|
|
end;
|
2006-01-27 06:02:33 +01:00
|
|
|
false ->
|
|
|
|
{load_module, Module}
|
|
|
|
end
|
|
|
|
end, UpdatedBeams).
|
|
|
|
|
|
|
|
make_low_level_script(UpdatedBeams, Script) ->
|
|
|
|
EJDApp = #application{name = ejabberd,
|
|
|
|
modules = UpdatedBeams},
|
|
|
|
{ok, LowLevelScript} =
|
|
|
|
systools_rc:translate_scripts([Script], [EJDApp], [EJDApp]),
|
|
|
|
LowLevelScript.
|