chore: add a script to simplify appup update

This commit is contained in:
Zaiming (Stone) Shi 2022-03-01 09:47:32 +01:00
parent 52ff180e55
commit 0996b79d96
2 changed files with 99 additions and 17 deletions

61
scripts/update-appup.sh Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env bash
## This script wrapps update_appup.escript,
## it provides a more commonly used set of default args.
## Arg1: EMQX PROFILE
set -euo pipefail
usage() {
echo "$0 PROFILE PREV_VERSION"
}
# ensure dir
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
PROFILE="${1:-}"
case "$PROFILE" in
emqx-ee)
DIR='enterprise'
TAG_PREFIX='e'
;;
emqx)
DIR='broker'
TAG_PREFIX='v'
;;
emqx-edge)
DIR='edge'
TAG_PREFIX='v'
;;
*)
echo "Unknown profile $PROFILE"
usage
exit 1
;;
esac
PREV_VERSION="$(git describe --tag --match "${TAG_PREFIX}*" | grep -oE "${TAG_PREFIX}4\.[0-9]+\.[0-9]+")"
PREV_VERSION="${PREV_VERSION#[e|v]}"
shift 1
ESCRIPT_ARGS="$*"
SYSTEM="${SYSTEM:-$(./scripts/get-distro.sh)}"
ARCH="${ARCH:-$(uname -m)}"
case "$ARCH" in
x86_64)
ARCH='amd64'
;;
aarch64)
ARCH='arm64'
;;
arm*)
ARCH=arm
;;
esac
PACKAGE_NAME="${PROFILE}-${SYSTEM}-${PREV_VERSION}-${ARCH}.zip"
DOWNLOAD_URL="https://www.emqx.com/downloads/${DIR}/v${PREV_VERSION}/${PACKAGE_NAME}"
# shellcheck disable=SC2086
./scripts/update_appup.escript --binary-rel-url "$DOWNLOAD_URL" $ESCRIPT_ARGS "$PREV_VERSION"

View File

@ -32,8 +32,8 @@ Options:
--make-command A command used to assemble the release --make-command A command used to assemble the release
--release-dir Release directory --release-dir Release directory
--src-dirs Directories where source code is found. Defaults to '{src,apps,lib-*}/**/' --src-dirs Directories where source code is found. Defaults to '{src,apps,lib-*}/**/'
--binary-rel-url Binary release URL pattern. %TAG% variable is substituted with the release tag. --binary-rel-url Binary release URL pattern.
E.g. \"https://github.com/emqx/emqx/releases/download/v%TAG%/emqx-centos7-%TAG%-amd64.zip\" E.g. https://www.emqx.com/downloads/broker/v4.3.12/emqx-centos7-4.3.12-amd64.zip
". ".
-record(app, -record(app,
@ -171,9 +171,10 @@ download_prev_release(Tag, #{binary_rel_url := {ok, URL0}, clone_url := Repo}) -
BaseDir = "/tmp/emqx-baseline-bin/", BaseDir = "/tmp/emqx-baseline-bin/",
Dir = filename:basename(Repo, ".git") ++ [$-|Tag], Dir = filename:basename(Repo, ".git") ++ [$-|Tag],
Filename = filename:join(BaseDir, Dir), Filename = filename:join(BaseDir, Dir),
Script = "mkdir -p ${OUTFILE} && Script = "echo \"Download: ${OUTFILE}\" &&
wget -c -O ${OUTFILE}.zip ${URL} && mkdir -p ${OUTFILE} &&
unzip -n -d ${OUTFILE} ${OUTFILE}.zip", curl -f -L -o ${OUTFILE}.zip ${URL} &&
unzip -q -n -d ${OUTFILE} ${OUTFILE}.zip",
Env = [{"TAG", Tag}, {"OUTFILE", Filename}, {"URL", URL}], Env = [{"TAG", Tag}, {"OUTFILE", Filename}, {"URL", URL}],
bash(Script, Env), bash(Script, Env),
{ok, Filename}. {ok, Filename}.
@ -208,8 +209,8 @@ find_appup_actions(App,
{OldUpgrade0, OldDowngrade0} = find_old_appup_actions(App, PrevVersion), {OldUpgrade0, OldDowngrade0} = find_old_appup_actions(App, PrevVersion),
OldUpgrade = ensure_all_patch_versions(App, CurrVersion, OldUpgrade0), OldUpgrade = ensure_all_patch_versions(App, CurrVersion, OldUpgrade0),
OldDowngrade = ensure_all_patch_versions(App, CurrVersion, OldDowngrade0), OldDowngrade = ensure_all_patch_versions(App, CurrVersion, OldDowngrade0),
Upgrade = merge_update_actions(App, diff_app(App, CurrAppIdx, PrevAppIdx), OldUpgrade), Upgrade = merge_update_actions(App, diff_app(up, App, CurrAppIdx, PrevAppIdx), OldUpgrade),
Downgrade = merge_update_actions(App, diff_app(App, PrevAppIdx, CurrAppIdx), OldDowngrade), Downgrade = merge_update_actions(App, diff_app(down, App, PrevAppIdx, CurrAppIdx), OldDowngrade),
if OldUpgrade =:= Upgrade andalso OldDowngrade =:= Downgrade -> if OldUpgrade =:= Upgrade andalso OldDowngrade =:= Downgrade ->
%% The appup file has been already updated: %% The appup file has been already updated:
[]; [];
@ -521,7 +522,7 @@ index_app(AppFile) ->
, modules = Modules , modules = Modules
}}. }}.
diff_app(App, diff_app(UpOrDown, App,
#app{version = NewVersion, modules = NewModules}, #app{version = NewVersion, modules = NewModules},
#app{version = OldVersion, modules = OldModules}) -> #app{version = OldVersion, modules = OldModules}) ->
{New, Changed} = {New, Changed} =
@ -539,18 +540,31 @@ diff_app(App,
, NewModules , NewModules
), ),
Deleted = maps:keys(maps:without(maps:keys(NewModules), OldModules)), Deleted = maps:keys(maps:without(maps:keys(NewModules), OldModules)),
NChanges = length(New) + length(Changed) + length(Deleted), Changes = lists:filter(fun({_T, L}) -> length(L) > 0 end,
if NewVersion =:= OldVersion andalso NChanges > 0 -> [{added, New}, {changed, Changed}, {deleted, Deleted}]),
set_invalid(), case NewVersion =:= OldVersion of
log("ERROR: Application '~p' contains changes, but its version is not updated~n", [App]); true when Changes =:= [] ->
NewVersion > OldVersion -> %% no change
log("INFO: Application '~p' has been updated: ~p -> ~p~n", [App, OldVersion, NewVersion]),
ok; ok;
true -> true ->
set_invalid(),
case UpOrDown =:= up of
true ->
%% only log for the upgrade case because it would be the same result
log("ERROR: Application '~p' contains changes, but its version is not updated. ~s",
[App, format_changes(Changes)]);
false ->
ok
end;
false ->
log("INFO: Application '~p' has been updated: ~p --[~p]--> ~p~n", [App, OldVersion, UpOrDown, NewVersion]),
ok ok
end, end,
{New, Changed, Deleted}. {New, Changed, Deleted}.
format_changes(Changes) ->
lists:map(fun({Tag, List}) -> io_lib:format("~p: ~p~n", [Tag, List]) end, Changes).
-spec hashsums(file:filename()) -> #{module() => binary()}. -spec hashsums(file:filename()) -> #{module() => binary()}.
hashsums(EbinDir) -> hashsums(EbinDir) ->
maps:from_list(lists:map( maps:from_list(lists:map(
@ -607,13 +621,20 @@ locate(ebin_current, App, Suffix) ->
locate(src, App, Suffix) -> locate(src, App, Suffix) ->
AppStr = atom_to_list(App), AppStr = atom_to_list(App),
SrcDirs = getopt(src_dirs), SrcDirs = getopt(src_dirs),
case filelib:wildcard(SrcDirs ++ AppStr ++ Suffix) of case find_app(SrcDirs ++ AppStr ++ Suffix) of
[File] -> [File] ->
{ok, File}; {ok, File};
[] -> [] ->
undefined undefined;
Files ->
error({more_than_one_app_found, Files})
end. end.
find_app(Pattern) ->
%% exclude _build dir inside apps
lists:filter(fun(S) -> string:find(S, "/_build/") =:= nomatch end,
filelib:wildcard(Pattern)).
bash(Script) -> bash(Script) ->
bash(Script, []). bash(Script, []).