From 91c46de4aa3d3a910033f91803148a614749964a Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Sat, 26 Feb 2022 20:33:42 +0800 Subject: [PATCH] fix(relup): inject relup instructions to the end of relup file --- rebar.config.erl | 3 +- scripts/inject-relup.escript | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 scripts/inject-relup.escript diff --git a/rebar.config.erl b/rebar.config.erl index ce8933570..11db1abb1 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -129,7 +129,8 @@ prod_overrides() -> [{add, [ {erl_opts, [deterministic]}]}]. relup_deps(Profile) -> - {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", compile, "scripts/inject-deps.escript " ++ atom_to_list(Profile)}]}. + {post_hooks, [{"(linux|darwin|solaris|freebsd|netbsd|openbsd)", release, + "scripts/inject-relup.escript " ++ filename:join(["_build", Profile, "rel", "emqx"])}]}. profiles() -> Vsn = get_vsn(), diff --git a/scripts/inject-relup.escript b/scripts/inject-relup.escript new file mode 100755 index 000000000..a7ba23689 --- /dev/null +++ b/scripts/inject-relup.escript @@ -0,0 +1,58 @@ +#!/usr/bin/env escript + +%% This script injects implicit relup instructions for emqx applications. + +-mode(compile). + +-define(ERROR(FORMAT, ARGS), io:format(standard_error, "[inject-relup] " ++ FORMAT ++"~n", ARGS)). +-define(INFO(FORMAT, ARGS), io:format(user, "[inject-relup] " ++ FORMAT ++"~n", ARGS)). + +usage() -> + "Usage: " ++ escript:script_name() ++ " ". + +main([Dir]) -> + case filelib:is_dir(Dir) of + true -> + ok = inject(Dir); + false -> + ?ERROR("not a valid dir: ~p", [Dir]), + erlang:halt(1) + end; +main(_Args) -> + ?ERROR("~s", [usage()]), + erlang:halt(1). + +inject(Dir) -> + RelupFiles = filelib:wildcard(filename:join([Dir, "releases", "*", "relup"])), + lists:foreach(fun(File) -> + case file:consult(File) of + {ok, [{CurrRelVsn, UpVsnRUs, DnVsnRUs}]} -> + ?INFO("injecting instructions to: ~p", [File]), + UpdatedContent = [{CurrRelVsn, inject_relup_instrs(CurrRelVsn, UpVsnRUs), + inject_relup_instrs(CurrRelVsn, DnVsnRUs)}], + file:write_file("/tmp/" ++ filename:basename(File), term_to_text(UpdatedContent)); + {ok, _BadFormat} -> + ?ERROR("bad formatted relup file: ~p", [File]); + {error, Reason} -> + ?ERROR("read relup file ~p failed: ~p", [File, Reason]) + end + end, RelupFiles). + +inject_relup_instrs(CurrRelVsn, RUs) -> + [{Vsn, Desc, append_emqx_relup_instrs(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(CurrRelVsn, Vsn, Instrs) -> + case lists:reverse(Instrs) of + [{apply, {emqx_relup, post_release_upgrade, _}} | _] -> + Instrs; + RInstrs -> + lists:reverse([instr_post_release_upgrade(CurrRelVsn, Vsn) | RInstrs]) + end. + +instr_post_release_upgrade(CurrRelVsn, Vsn) -> + {apply, {emqx_relup, post_release_upgrade, [CurrRelVsn, Vsn]}}. + +term_to_text(Term) -> + io_lib:format("~p.", [Term]). \ No newline at end of file