From 5298e93a180e6db87a33f26eb6a2db06e8065dc7 Mon Sep 17 00:00:00 2001 From: joewilliams Date: Thu, 27 Jan 2011 18:15:25 +0100 Subject: Add 'generate-upgrade' command To support OTP release upgrades I have added support for building upgrade packages. Support for this is included in the rebar_upgrade module, specifically generate_upgrade/2. It requires one variable to be set on the command line 'previous_release' which is the absolute path or relative path from 'rel/' to the previous release one is upgrading from. Running an upgrade will create the needed files, including a relup and result in a tarball containing the upgrade being written to 'rel/'. When done it cleans up the temporary files systools created. Usage: $ rebar generate-upgrade previous_release=/path/to/old/version This also includes a dummy application that can be used to test upgrades as well as an example. Special thanks to Daniel Reverri, Jesper Louis Andersen and Richard Jones for comments and patches. --- src/rebar_upgrade.erl | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 src/rebar_upgrade.erl (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl new file mode 100644 index 0000000..4c0d2f7 --- /dev/null +++ b/src/rebar_upgrade.erl @@ -0,0 +1,206 @@ +%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2011 Joe Williams +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- + +-module(rebar_upgrade). + +-include("rebar.hrl"). +-include_lib("kernel/include/file.hrl"). + +-export(['generate-upgrade'/2]). + +%% public api + +'generate-upgrade'(_Config, ReltoolFile) -> + case rebar_config:get_global(previous_release, false) of + false -> + ?ABORT("previous_release=PATH is required to " + "create upgrade package~n", []); + OldVerPath -> + %% Run checks to make sure that building a package is possible + {NewName, NewVer} = run_checks(OldVerPath, ReltoolFile), + NameVer = NewName ++ "_" ++ NewVer, + + %% Save the code path prior to doing anything + OrigPath = code:get_path(), + + %% Prepare the environment for building the package + ok = setup(OldVerPath, NewName, NewVer, NameVer), + + %% Build the package + run_systools(NameVer, NewName), + + %% Boot file changes + boot_files(NewVer, NewName), + + %% Extract upgrade and tar it back up with changes + make_tar(NameVer), + + %% Clean up files that systools created + ok = cleanup(NameVer, NewName, NewVer), + + %% Restore original path + true = code:set_path(OrigPath), + + ok + end. + +%% internal api + +run_checks(OldVerPath, ReltoolFile) -> + true = prop_check(filelib:is_dir(OldVerPath), + "Release directory doesn't exist (~p)~n", [OldVerPath]), + + {Name, Ver} = get_release_name(ReltoolFile), + + NamePath = filename:join([".", Name]), + true = prop_check(filelib:is_dir(NamePath), + "Release directory doesn't exist (~p)~n", [NamePath]), + + {NewName, NewVer} = get_release_version(Name, NamePath), + {OldName, OldVer} = get_release_version(Name, OldVerPath), + + true = prop_check(NewName == OldName, + "New and old .rel release names do not match~n", []), + true = prop_check(Name == NewName, + "Reltool and .rel release names do not match~n", []), + true = prop_check(NewVer =/= OldVer, + "New and old .rel contain the same version~n", []), + true = prop_check(Ver == NewVer, + "Reltool and .rel versions do not match~n", []), + + {NewName, NewVer}. + +get_release_name(ReltoolFile) -> + %% expect sys to be the first proplist in reltool.config + case file:consult(ReltoolFile) of + {ok, [{sys, Config}| _]} -> + %% expect the first rel in the proplist to be the one you want + {rel, Name, Ver, _} = proplists:lookup(rel, Config), + {Name, Ver}; + _ -> + ?ABORT("Failed to parse ~s~n", [ReltoolFile]) + end. + +get_release_version(Name, Path) -> + [RelFile] = filelib:wildcard(filename:join([Path, "releases", "*", + Name ++ ".rel"])), + [BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""), + {ok, [{release, {Name1, Ver}, _, _}]} = + file:consult(filename:join([binary_to_list(BinDir), + Name ++ ".rel"])), + {Name1, Ver}. + +prop_check(true, _, _) -> true; +prop_check(false, Msg, Args) -> ?ABORT(Msg, Args). + +setup(OldVerPath, NewName, NewVer, NameVer) -> + NewRelPath = filename:join([".", NewName]), + Src = filename:join([NewRelPath, "releases", + NewVer, NewName ++ ".rel"]), + Dst = filename:join([".", NameVer ++ ".rel"]), + {ok, _} = file:copy(Src, Dst), + ok = code:add_pathsa( + lists:append([ + filelib:wildcard(filename:join([OldVerPath, + "releases", "*"])), + filelib:wildcard(filename:join([OldVerPath, + "lib", "*", "ebin"])), + filelib:wildcard(filename:join([NewRelPath, + "lib", "*", "ebin"])), + filelib:wildcard(filename:join([NewRelPath, "*"])) + ])). + +run_systools(NewVer, Name) -> + Opts = [silent], + NameList = [Name], + case systools:make_relup(NewVer, NameList, NameList, Opts) of + {error, _, _Message} -> + ?ABORT("Systools aborted with: ~p~n", [_Message]); + _ -> + ?DEBUG("Relup created~n", []), + case systools:make_script(NewVer, Opts) of + {error, _, _Message1} -> + ?ABORT("Systools aborted with: ~p~n", [_Message1]); + _ -> + ?DEBUG("Script created~n", []), + case systools:make_tar(NewVer, Opts) of + {error, _, _Message2} -> + ?ABORT("Systools aborted with: ~p~n", [_Message2]); + _ -> + ok + end + end + end. + +boot_files(Ver, Name) -> + ok = file:make_dir(filename:join([".", "releases"])), + ok = file:make_dir(filename:join([".", "releases", Ver])), + ok = file:make_symlink(filename:join(["start.boot"]), + filename:join([".", "releases", Ver, Name ++ ".boot"])), + {ok, _} = file:copy(filename:join([".", Name, "releases", Ver, "start_clean.boot"]), + filename:join([".", "releases", Ver, "start_clean.boot"])). + +make_tar(NameVer) -> + Filename = NameVer ++ ".tar.gz", + ok = erl_tar:extract(Filename, [compressed]), + ok = file:delete(Filename), + {ok, Tar} = erl_tar:open(Filename, [write, compressed]), + ok = erl_tar:add(Tar, "lib", []), + ok = erl_tar:add(Tar, "releases", []), + ok = erl_tar:close(Tar), + ?CONSOLE("~s upgrade package created~n", [NameVer]). + +cleanup(NameVer, Name, Ver) -> + ?DEBUG("Removing files needed for building the upgrade~n", []), + Files = [ + filename:join([".", "releases", Ver, Name ++ ".boot"]), + filename:join([".", NameVer ++ ".rel"]), + filename:join([".", NameVer ++ ".boot"]), + filename:join([".", NameVer ++ ".script"]), + filename:join([".", "relup"]) + ], + [ok = file:delete(F) || F <- Files], + + ok = remove_dir_tree("releases"), + ok = remove_dir_tree("lib"). + +%% taken from http://www.erlang.org/doc/system_principles/create_target.html +remove_dir_tree(Dir) -> + remove_all_files(".", [Dir]). +remove_all_files(Dir, Files) -> + lists:foreach(fun(File) -> + FilePath = filename:join([Dir, File]), + {ok, FileInfo} = file:read_file_info(FilePath), + case FileInfo#file_info.type of + directory -> + {ok, DirFiles} = file:list_dir(FilePath), + remove_all_files(FilePath, DirFiles), + file:del_dir(FilePath); + _ -> + file:delete(FilePath) + end + end, Files). -- cgit v1.1 From 0e729bf90e64a00bd8915e227d83f82877038e9e Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Sat, 29 Jan 2011 15:50:24 +0100 Subject: Fix Dialyzer warnings in rebar_upgrade --- src/rebar_upgrade.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl index 4c0d2f7..0026cee 100644 --- a/src/rebar_upgrade.erl +++ b/src/rebar_upgrade.erl @@ -54,7 +54,7 @@ run_systools(NameVer, NewName), %% Boot file changes - boot_files(NewVer, NewName), + {ok, _} = boot_files(NewVer, NewName), %% Extract upgrade and tar it back up with changes make_tar(NameVer), @@ -183,7 +183,7 @@ cleanup(NameVer, Name, Ver) -> filename:join([".", NameVer ++ ".script"]), filename:join([".", "relup"]) ], - [ok = file:delete(F) || F <- Files], + lists:foreach(fun(F) -> ok = file:delete(F) end, Files), ok = remove_dir_tree("releases"), ok = remove_dir_tree("lib"). -- cgit v1.1 From c466076ffb5a1ea4c00d49fefff0dcfbceb58236 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Mon, 31 Jan 2011 17:43:31 +0100 Subject: Clean up emacs file local variables --- src/rebar_upgrade.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl index 0026cee..c882cc8 100644 --- a/src/rebar_upgrade.erl +++ b/src/rebar_upgrade.erl @@ -1,4 +1,4 @@ -%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- %% ex: ts=4 sw=4 et %% ------------------------------------------------------------------- %% -- cgit v1.1 From 63de05d914f3c2bef6dcfc6cf966400d93c9c80d Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Fri, 28 Jan 2011 16:08:27 +0100 Subject: Clean up code --- src/rebar_upgrade.erl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl index c882cc8..639ddef 100644 --- a/src/rebar_upgrade.erl +++ b/src/rebar_upgrade.erl @@ -159,10 +159,12 @@ run_systools(NewVer, Name) -> boot_files(Ver, Name) -> ok = file:make_dir(filename:join([".", "releases"])), ok = file:make_dir(filename:join([".", "releases", Ver])), - ok = file:make_symlink(filename:join(["start.boot"]), - filename:join([".", "releases", Ver, Name ++ ".boot"])), - {ok, _} = file:copy(filename:join([".", Name, "releases", Ver, "start_clean.boot"]), - filename:join([".", "releases", Ver, "start_clean.boot"])). + ok = file:make_symlink( + filename:join(["start.boot"]), + filename:join([".", "releases", Ver, Name ++ ".boot"])), + {ok, _} = file:copy( + filename:join([".", Name, "releases", Ver, "start_clean.boot"]), + filename:join([".", "releases", Ver, "start_clean.boot"])). make_tar(NameVer) -> Filename = NameVer ++ ".tar.gz", -- cgit v1.1 From 4e8dcfbfade08b48fe38bfe041459960b0ff74c3 Mon Sep 17 00:00:00 2001 From: joewilliams Date: Wed, 16 Feb 2011 09:46:40 -0800 Subject: Clean up rebar_appups and rebar_upgrade --- src/rebar_upgrade.erl | 96 ++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 59 deletions(-) (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl index 639ddef..a1b34df 100644 --- a/src/rebar_upgrade.erl +++ b/src/rebar_upgrade.erl @@ -4,7 +4,7 @@ %% %% rebar: Erlang Build Tools %% -%% Copyright (c) 2011 Joe Williams +%% Copyright (c) 2011 Joe Williams (joe@joetify.com) %% %% Permission is hereby granted, free of charge, to any person obtaining a copy %% of this software and associated documentation files (the "Software"), to deal @@ -32,91 +32,69 @@ -export(['generate-upgrade'/2]). -%% public api +%% ==================================================================== +%% Public API +%% ==================================================================== 'generate-upgrade'(_Config, ReltoolFile) -> - case rebar_config:get_global(previous_release, false) of - false -> - ?ABORT("previous_release=PATH is required to " - "create upgrade package~n", []); - OldVerPath -> - %% Run checks to make sure that building a package is possible - {NewName, NewVer} = run_checks(OldVerPath, ReltoolFile), - NameVer = NewName ++ "_" ++ NewVer, + %% Get the old release path + OldVerPath = rebar_rel_utils:get_previous_release_path(), + + %% Run checks to make sure that building a package is possible + {NewName, NewVer} = run_checks(OldVerPath, ReltoolFile), + NameVer = NewName ++ "_" ++ NewVer, - %% Save the code path prior to doing anything - OrigPath = code:get_path(), + %% Save the code path prior to doing anything + OrigPath = code:get_path(), - %% Prepare the environment for building the package - ok = setup(OldVerPath, NewName, NewVer, NameVer), + %% Prepare the environment for building the package + ok = setup(OldVerPath, NewName, NewVer, NameVer), - %% Build the package - run_systools(NameVer, NewName), + %% Build the package + run_systools(NameVer, NewName), - %% Boot file changes - {ok, _} = boot_files(NewVer, NewName), + %% Boot file changes + {ok, _} = boot_files(NewVer, NewName), - %% Extract upgrade and tar it back up with changes - make_tar(NameVer), + %% Extract upgrade and tar it back up with changes + make_tar(NameVer), - %% Clean up files that systools created - ok = cleanup(NameVer, NewName, NewVer), + %% Clean up files that systools created + ok = cleanup(NameVer, NewName, NewVer), - %% Restore original path - true = code:set_path(OrigPath), + %% Restore original path + true = code:set_path(OrigPath), - ok - end. + ok. -%% internal api +%% =================================================================== +%% Internal functions +%% ================================================================== run_checks(OldVerPath, ReltoolFile) -> - true = prop_check(filelib:is_dir(OldVerPath), + true = rebar_utils:prop_check(filelib:is_dir(OldVerPath), "Release directory doesn't exist (~p)~n", [OldVerPath]), - {Name, Ver} = get_release_name(ReltoolFile), + {Name, Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolFile), NamePath = filename:join([".", Name]), - true = prop_check(filelib:is_dir(NamePath), + true = rebar_utils:prop_check(filelib:is_dir(NamePath), "Release directory doesn't exist (~p)~n", [NamePath]), - {NewName, NewVer} = get_release_version(Name, NamePath), - {OldName, OldVer} = get_release_version(Name, OldVerPath), + {NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NamePath), + {OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath), - true = prop_check(NewName == OldName, + true = rebar_utils:prop_check(NewName == OldName, "New and old .rel release names do not match~n", []), - true = prop_check(Name == NewName, + true = rebar_utils:prop_check(Name == NewName, "Reltool and .rel release names do not match~n", []), - true = prop_check(NewVer =/= OldVer, + true = rebar_utils:prop_check(NewVer =/= OldVer, "New and old .rel contain the same version~n", []), - true = prop_check(Ver == NewVer, + true = rebar_utils:prop_check(Ver == NewVer, "Reltool and .rel versions do not match~n", []), {NewName, NewVer}. -get_release_name(ReltoolFile) -> - %% expect sys to be the first proplist in reltool.config - case file:consult(ReltoolFile) of - {ok, [{sys, Config}| _]} -> - %% expect the first rel in the proplist to be the one you want - {rel, Name, Ver, _} = proplists:lookup(rel, Config), - {Name, Ver}; - _ -> - ?ABORT("Failed to parse ~s~n", [ReltoolFile]) - end. - -get_release_version(Name, Path) -> - [RelFile] = filelib:wildcard(filename:join([Path, "releases", "*", - Name ++ ".rel"])), - [BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""), - {ok, [{release, {Name1, Ver}, _, _}]} = - file:consult(filename:join([binary_to_list(BinDir), - Name ++ ".rel"])), - {Name1, Ver}. - -prop_check(true, _, _) -> true; -prop_check(false, Msg, Args) -> ?ABORT(Msg, Args). - setup(OldVerPath, NewName, NewVer, NameVer) -> NewRelPath = filename:join([".", NewName]), Src = filename:join([NewRelPath, "releases", -- cgit v1.1 From 1e9b2b844b844a990269f68535f5bbf02ac522b3 Mon Sep 17 00:00:00 2001 From: joewilliams Date: Thu, 17 Feb 2011 10:12:32 -0800 Subject: Clean up trailing whitespace --- src/rebar_upgrade.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rebar_upgrade.erl') diff --git a/src/rebar_upgrade.erl b/src/rebar_upgrade.erl index a1b34df..39b2c61 100644 --- a/src/rebar_upgrade.erl +++ b/src/rebar_upgrade.erl @@ -39,7 +39,7 @@ 'generate-upgrade'(_Config, ReltoolFile) -> %% Get the old release path OldVerPath = rebar_rel_utils:get_previous_release_path(), - + %% Run checks to make sure that building a package is possible {NewName, NewVer} = run_checks(OldVerPath, ReltoolFile), NameVer = NewName ++ "_" ++ NewVer, -- cgit v1.1