From 28bd2fcfa463dc04a01bdb07d11521e51f0294d1 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Sun, 27 Feb 2022 20:04:21 +0800 Subject: [PATCH] fix(relup): release upgrade failed with {bad_lib_vsn,emqx,"4.4.2"} --- scripts/inject-relup.escript | 46 +++++++++++++++++++++++++++--------- src/emqx_relup.erl | 14 +++++++++-- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/scripts/inject-relup.escript b/scripts/inject-relup.escript index e414b9c31..1a2dae78a 100755 --- a/scripts/inject-relup.escript +++ b/scripts/inject-relup.escript @@ -30,38 +30,62 @@ inject_dir(Dir) -> lists:foreach(fun inject_file/1, RelupFiles). inject_file(File) -> + EmqxVsn = emqx_vsn_from_rel_file(File), case file:script(File) of {ok, {CurrRelVsn, UpVsnRUs, DnVsnRUs}} -> ?INFO("injecting instructions to: ~p", [File]), - UpdatedContent = {CurrRelVsn, inject_relup_instrs(up, CurrRelVsn, UpVsnRUs), - inject_relup_instrs(down, CurrRelVsn, DnVsnRUs)}, + UpdatedContent = {CurrRelVsn, inject_relup_instrs(up, EmqxVsn, CurrRelVsn, UpVsnRUs), + inject_relup_instrs(down, EmqxVsn, CurrRelVsn, DnVsnRUs)}, ok = file:write_file(File, term_to_text(UpdatedContent)); {ok, _BadFormat} -> - ?ERROR("bad formatted relup file: ~p", [File]); + ?ERROR("bad formatted relup file: ~p", [File]), + error({bad_relup_format, File}); {error, Reason} -> - ?ERROR("read relup file ~p failed: ~p", [File, Reason]) + ?ERROR("read relup file ~p failed: ~p", [File, Reason]), + error({read_relup_error, Reason}) end. -inject_relup_instrs(Type, CurrRelVsn, RUs) -> - [{Vsn, Desc, append_emqx_relup_instrs(Type, CurrRelVsn, Vsn, Instrs)} || {Vsn, Desc, Instrs} <- RUs]. +inject_relup_instrs(Type, EmqxVsn, CurrRelVsn, RUs) -> + [{Vsn, Desc, append_emqx_relup_instrs(Type, EmqxVsn, CurrRelVsn, Vsn, Instrs)} + || {Vsn, Desc, Instrs} <- RUs]. %% The `{apply, emqx_relup, post_release_upgrade, []}` will be appended to the end of %% the instruction lists. -append_emqx_relup_instrs(Type, CurrRelVsn, Vsn, Instrs) -> +append_emqx_relup_instrs(Type, EmqxVsn, CurrRelVsn, Vsn, Instrs) -> CallbackFun = relup_callback_func(Type), Extra = #{}, %% we may need some extended args case lists:reverse(Instrs) of [{apply, {emqx_relup, CallbackFun, _}} | _] -> Instrs; RInstrs -> - lists:reverse([ {load_object_code, {emqx, CurrRelVsn, [emqx_relup]}} - , {load, {emqx_relup, brutal_purge, soft_purge}} - , {apply, {emqx_relup, CallbackFun, [CurrRelVsn, Vsn, Extra]}} - | RInstrs]) + Instrs2 = lists:reverse( + [ {apply, {emqx_relup, CallbackFun, [CurrRelVsn, Vsn, Extra]}} + , {load, {emqx_relup, brutal_purge, soft_purge}} + | RInstrs]), + %% we have to put 'load_object_code' before 'point_of_no_return' + %% so here we simply put it to the beginning + [{load_object_code, {emqx, EmqxVsn, [emqx_relup]}} | Instrs2] end. relup_callback_func(up) -> post_release_upgrade; relup_callback_func(down) -> post_release_downgrade. +emqx_vsn_from_rel_file(RelupFile) -> + RelDir = filename:dirname(RelupFile), + RelFile = filename:join([RelDir, "emqx.rel"]), + case file:script(RelFile) of + {ok, {release, {_RelName, _RelVsn}, _Erts, Apps}} -> + case lists:keysearch(emqx, 1, Apps) of + {value, {emqx, EmqxVsn}} -> + EmqxVsn; + false -> + error({emqx_vsn_cannot_found, RelFile}) + end; + {ok, _BadFormat} -> + ?ERROR("bad formatted .rel file: ~p", [RelFile]); + {error, Reason} -> + ?ERROR("read .rel file ~p failed: ~p", [RelFile, Reason]) + end. + term_to_text(Term) -> io_lib:format("~p.", [Term]). diff --git a/src/emqx_relup.erl b/src/emqx_relup.erl index e844ea0ba..b435f8ae8 100644 --- a/src/emqx_relup.erl +++ b/src/emqx_relup.erl @@ -10,8 +10,18 @@ post_release_upgrade(_CurrRelVsn, _FromVsn, _) -> post_release_downgrade(_CurrRelVsn, _ToVsn, _) -> reload_components(). +-ifdef(EMQX_ENTERPRISE). reload_components() -> - io:format("reloading resource providers ..."), + io:format("reloading resource providers ...~n"), emqx_rule_engine:load_providers(), - io:format("loading plugins ..."), + io:format("reloading module providers ...~n"), + emqx_modules:load_providers(), + io:format("loading plugins ...~n"), emqx_plugins:load(). +-else. +reload_components() -> + io:format("reloading resource providers ...~n"), + emqx_rule_engine:load_providers(), + io:format("loading plugins ...~n"), + emqx_plugins:load(). +-endif.