refactor(update_appup): Minor code cleanup

This commit is contained in:
k32 2021-10-01 22:58:13 +02:00
parent aca6367561
commit f793883e35
1 changed files with 65 additions and 37 deletions

View File

@ -12,7 +12,7 @@ main(Args) ->
_ = maps:map(fun(App, Changes) -> process_app(Baseline, Check, App, Changes) end, Changed), _ = maps:map(fun(App, Changes) -> process_app(Baseline, Check, App, Changes) end, Changed),
ok; ok;
undefined -> undefined ->
io:format(standard_error, "No appup update is needed for this release, done~n", []), log("No appup update is needed for this release, nothing to be done~n", []),
ok ok
end. end.
@ -25,10 +25,10 @@ parse_args(["--skip-build"|Rest], State) ->
parse_args([], _) -> parse_args([], _) ->
fail("A script that creates stubs for appup files fail("A script that creates stubs for appup files
Usage:~n update_appup.escript [--check] [--skip-build] <current_release_tag> Usage: update_appup.escript [--check] [--skip-build] <current_release_tag>
--check Don't update the appup files, just check that they are complete --check Don't update the appup files, just check that they are complete
--skip-build Don't rebuild the releases. May produce wrong appup files. --skip-build Don't rebuild the releases. May produce wrong appup files if changes are made.
"). ").
process_app(_, _, App, {[], [], []}) -> process_app(_, _, App, {[], [], []}) ->
@ -69,7 +69,7 @@ update_appup(_, File, {[], [], []}) ->
%% No changes in the app. Just check syntax of the existing appup: %% No changes in the app. Just check syntax of the existing appup:
_ = read_appup(File); _ = read_appup(File);
update_appup(PredVersion, File, Changes) -> update_appup(PredVersion, File, Changes) ->
io:format("~nUpdating appup: ~p~n", [File]), log("Updating appup: ~p~n", [File]),
{_, Upgrade0, Downgrade0} = read_appup(File), {_, Upgrade0, Downgrade0} = read_appup(File),
Upgrade = update_actions(PredVersion, Changes, Upgrade0), Upgrade = update_actions(PredVersion, Changes, Upgrade0),
Downgrade = update_actions(PredVersion, Changes, Downgrade0), Downgrade = update_actions(PredVersion, Changes, Downgrade0),
@ -82,7 +82,7 @@ render_appfile(File, Upgrade, Downgrade) ->
ok = file:write_file(File, IOList). ok = file:write_file(File, IOList).
update_actions(PredVersion, Changes, Versions) -> update_actions(PredVersion, Changes, Versions) ->
lists:map(fun(L) -> do_update_actions(Changes, L) end, ensure_pred_version(PredVersion, Versions)). lists:map(fun(L) -> do_update_actions(Changes, L) end, ensure_pred_versions(PredVersion, Versions)).
do_update_actions(_, Ret = {<<".*">>, _}) -> do_update_actions(_, Ret = {<<".*">>, _}) ->
Ret; Ret;
@ -97,10 +97,15 @@ process_changes({New0, Changed0, Deleted0}, OldActions) ->
OldActions ++ [{load_module, M, brutal_purge, soft_purge, []} || M <- Changed ++ New] OldActions ++ [{load_module, M, brutal_purge, soft_purge, []} || M <- Changed ++ New]
++ [{delete_module, M} || M <- Deleted]. ++ [{delete_module, M} || M <- Deleted].
ensure_pred_version(PredVersion, Versions) -> ensure_pred_versions(PredVersion, Versions) ->
case lists:keyfind(PredVersion, 1, Versions) of {Maj, Min, Patch} = parse_semver(PredVersion),
PredVersions = [semver(Maj, Min, P) || P <- lists:seq(0, Patch)],
lists:foldl(fun ensure_version/2, Versions, PredVersions).
ensure_version(Version, Versions) ->
case lists:keyfind(Version, 1, Versions) of
false -> false ->
[{PredVersion, []}|Versions]; [{Version, []}|Versions];
_ -> _ ->
Versions Versions
end. end.
@ -143,12 +148,12 @@ find_beams(Dir) ->
[filename:join(Dir, I) || I <- filelib:wildcard("**/ebin/*.beam", Dir)]. [filename:join(Dir, I) || I <- filelib:wildcard("**/ebin/*.beam", Dir)].
prepare(Baseline, Prepare) -> prepare(Baseline, Prepare) ->
io:format("~n===================================~n" log("~n===================================~n"
"Baseline: ~s" "Baseline: ~s"
"~n===================================~n", [Baseline]), "~n===================================~n", [Baseline]),
io:format("Building the current version...~n"), log("Building the current version...~n"),
Prepare andalso success(cmd("make", #{args => ["emqx-rel"]}), "Failed to build HEAD"), Prepare andalso bash("make emqx-rel"),
io:format("Downloading the preceding release...~n"), log("Downloading the preceding release...~n"),
{ok, PredRootDir} = build_pred_release(Baseline, Prepare), {ok, PredRootDir} = build_pred_release(Baseline, Prepare),
BeamDir = "_build/emqx/rel/emqx/lib/", BeamDir = "_build/emqx/rel/emqx/lib/",
{BeamDir, filename:join(PredRootDir, BeamDir)}. {BeamDir, filename:join(PredRootDir, BeamDir)}.
@ -158,34 +163,29 @@ build_pred_release(Baseline, Prepare) ->
BaseDir = "/tmp/emqx-baseline/", BaseDir = "/tmp/emqx-baseline/",
Dir = filename:basename(Repo, ".git") ++ [$-|Baseline], Dir = filename:basename(Repo, ".git") ++ [$-|Baseline],
%% TODO: shallow clone %% TODO: shallow clone
Script = "mkdir -p ${BASEDIR} && cd ${BASEDIR} && { git clone --branch ${TAG} ${REPO} ${DIR} || true; } && cd ${DIR} && make emqx-rel", Script = "mkdir -p ${BASEDIR} &&
cd ${BASEDIR} &&
{ git clone --branch ${TAG} ${REPO} ${DIR} || true; } &&
cd ${DIR} &&
make emqx-rel",
Env = [{"REPO", Repo}, {"TAG", Baseline}, {"BASEDIR", BaseDir}, {"DIR", Dir}], Env = [{"REPO", Repo}, {"TAG", Baseline}, {"BASEDIR", BaseDir}, {"DIR", Dir}],
Prepare andalso Prepare andalso bash(Script, Env),
success( cmd("bash", #{ args => ["-c", Script]
, env => Env
})
, "Failed to build the baseline release"
),
{ok, filename:join(BaseDir, Dir)}. {ok, filename:join(BaseDir, Dir)}.
%% @doc Find whether we are in emqx or emqx-ee %% @doc Find whether we are in emqx or emqx-ee
find_upstream_repo() -> find_upstream_repo() ->
Str = os:cmd("git remote get-url origin"), Str = os:cmd("git remote get-url origin"),
case re:run(Str, "/([^/]+).git$", [{capture, all_but_first, list}]) of case re(Str, "/([^/]+).git$") of
{match, ["emqx"]} -> "git@github.com:emqx/emqx.git"; {match, ["emqx"]} -> "git@github.com:emqx/emqx.git";
{match, ["emqx-ee"]} -> "git@github.com:emqx/emqx-ee.git"; {match, ["emqx-ee"]} -> "git@github.com:emqx/emqx-ee.git";
Ret -> fail("Cannot detect the correct upstream repo: ~p", [Ret]) Ret -> fail("Cannot detect the correct upstream repo: ~p", [Ret])
end. end.
find_pred_tag(CurrentRelease) -> find_pred_tag(CurrentRelease) ->
case re:run(CurrentRelease, "^([0-9]+)\.([0-9]+)\.([0-9]+)$", [{capture, all_but_first, list}]) of {Maj, Min, Patch} = parse_semver(CurrentRelease),
{match, [Maj, Min, Patch]} -> case Patch of
case list_to_integer(Patch) of
0 -> undefined; 0 -> undefined;
P -> {ok, lists:flatten(io_lib:format("~s.~s.~p", [Maj, Min, P - 1]))} _ -> {ok, semver(Maj, Min, Patch - 1)}
end;
Err ->
fail("The current release tag doesn't follow semver pattern: ~p", [Err])
end. end.
-spec hashsums(file:filename()) -> #{App => #{module() => binary()}} -spec hashsums(file:filename()) -> #{App => #{module() => binary()}}
@ -197,7 +197,7 @@ hashsums([], Acc) ->
Acc; Acc;
hashsums([File|Rest], Acc0) -> hashsums([File|Rest], Acc0) ->
[_, "ebin", Dir|_] = lists:reverse(filename:split(File)), [_, "ebin", Dir|_] = lists:reverse(filename:split(File)),
{match, [AppStr]} = re:run(Dir, "^(.*)-[^-]+$", [{capture, all_but_first, list}]), {match, [AppStr]} = re(Dir, "^(.*)-[^-]+$"),
App = list_to_atom(AppStr), App = list_to_atom(AppStr),
{ok, {Module, MD5}} = beam_lib:md5(File), {ok, {Module, MD5}} = beam_lib:md5(File),
Acc = maps:update_with( App Acc = maps:update_with( App
@ -207,6 +207,21 @@ hashsums([File|Rest], Acc0) ->
), ),
hashsums(Rest, Acc). hashsums(Rest, Acc).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Utility functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parse_semver(Version) ->
case re(Version, "^([0-9]+)\.([0-9]+)\.([0-9]+)$") of
{match, [Maj, Min, Patch]} ->
{list_to_integer(Maj), list_to_integer(Min), list_to_integer(Patch)};
Err ->
error({not_a_semver, Version})
end.
semver(Maj, Min, Patch) ->
lists:flatten(io_lib:format("~p.~p.~p", [Maj, Min, Patch])).
%% Locate a file in a specified application %% Locate a file in a specified application
locate(App, Suffix) -> locate(App, Suffix) ->
AppStr = atom_to_list(App), AppStr = atom_to_list(App),
@ -217,6 +232,15 @@ locate(App, Suffix) ->
undefined undefined
end. end.
bash(Script) ->
bash(Script, []).
bash(Script, Env) ->
case cmd("bash", #{args => ["-c", Script], env => Env}) of
0 -> true;
_ -> fail("Failed to run command: ~s", [Script])
end.
%% Spawn an executable and return the exit status %% Spawn an executable and return the exit status
cmd(Exec, Params) -> cmd(Exec, Params) ->
case os:find_executable(Exec) of case os:find_executable(Exec) of
@ -236,14 +260,18 @@ cmd(Exec, Params) ->
end end
end. end.
success(0, _) ->
true;
success(_, Msg) ->
fail(Msg).
fail(Str) -> fail(Str) ->
fail(Str, []). fail(Str, []).
fail(Str, Args) -> fail(Str, Args) ->
io:format(standard_error, Str ++ "~n", Args), log(Str ++ "~n", Args),
halt(1). halt(1).
re(Subject, RE) ->
re:run(Subject, RE, [{capture, all_but_first, list}]).
log(Msg) ->
log(Msg, []).
log(Msg, Args) ->
io:format(standard_error, Msg, Args).