diff --git a/priv/templates/simplenode.install_upgrade.escript b/priv/templates/simplenode.install_upgrade.escript old mode 100755 new mode 100644 index 0d4cbe3..253b7fa --- a/priv/templates/simplenode.install_upgrade.escript +++ b/priv/templates/simplenode.install_upgrade.escript @@ -2,51 +2,54 @@ %%! -noshell -noinput %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- %% ex: ft=erlang ts=4 sw=4 et +%% This file is left for backward-compatibility. +%% You, probably, shouldn't include it to new projects. --define(TIMEOUT, 60000). --define(INFO(Fmt,Args), io:format(Fmt,Args)). - -%% TODO: This script currently does NOT support slim releases. -%% Necessary steps to upgrade a slim release are as follows: -%% 1. unpack relup archive manually -%% 2. copy releases directory and necessary libraries -%% 3. using release_hander:set_unpacked/2 . -%% For more details, see https://github.com/rebar/rebar/pull/52 -%% and https://github.com/rebar/rebar/issues/202 main([NodeName, Cookie, ReleasePackage]) -> - TargetNode = start_distribution(NodeName, Cookie), - {ok, Vsn} = rpc:call(TargetNode, release_handler, unpack_release, - [ReleasePackage], ?TIMEOUT), - ?INFO("Unpacked Release ~p~n", [Vsn]), - {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, - check_install_release, [Vsn], ?TIMEOUT), - {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, - install_release, [Vsn], ?TIMEOUT), - ?INFO("Installed Release ~p~n", [Vsn]), - ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), - ?INFO("Made Release ~p Permanent~n", [Vsn]); + io:format("WARNING: 'install_upgrade.escript' is deprecated! " + "Use 'nodetool upgrade' instead.~n"), + NodeRoot = filename:dirname(filename:dirname(escript:script_name())), + NodeTool = which_nodetool(NodeRoot), + process_flag(trap_exit, true), + Port = erlang:open_port( + {spawn_executable, NodeTool}, + [{args, ["-sname", NodeName, + "-setcookie", Cookie, + "upgrade", ReleasePackage]}, + binary, exit_status, use_stdio, stderr_to_stdout, hide]), + port_loop(Port); main(_) -> - init:stop(1). + halt(1). -start_distribution(NodeName, Cookie) -> - MyNode = make_script_node(NodeName), - {ok, _Pid} = net_kernel:start([MyNode, shortnames]), - erlang:set_cookie(node(), list_to_atom(Cookie)), - TargetNode = make_target_node(NodeName), - case {net_kernel:hidden_connect_node(TargetNode), - net_adm:ping(TargetNode)} of - {true, pong} -> - ok; - {_, pang} -> - io:format("Node ~p not responding to pings.\n", [TargetNode]), - init:stop(1) - end, - TargetNode. -make_target_node(Node) -> - [_, Host] = string:tokens(atom_to_list(node()), "@"), - list_to_atom(lists:concat([Node, "@", Host])). +which_nodetool(NodeRoot) -> + %% ${RELEASE_ROOT}/ + %% bin/install_upgrade.escript + %% bin/nodetool ? + %% erts-/bin/nodetool ? + %% releases//nodetool ? + %% releases/start_erl.data + {ok, Content} = file:read_file(filename:join([NodeRoot, "releases", "start_erl.data"])), + [ErtsVsn, AppVsn] = binary:split(Content, <<" ">>), + Probes = [ + filename:join([NodeRoot, "bin", "nodetool"]), + filename:join([NodeRoot, <<"erts-", ErtsVsn/binary>>, "bin", "nodetool"]), + filename:join([NodeRoot, "releases", AppVsn, "bin", "nodetool"]) + ], + case lists:dropwhile(fun(Path) -> not filelib:is_regular(Path) end, Probes) of + [] -> + io:format("ERROR: can't find 'nodetool' in ~p.~n", [Probes]), + halt(2); + [Path | _] -> + Path + end. -make_script_node(Node) -> - list_to_atom(lists:concat([Node, "_upgrader_", os:getpid()])). +port_loop(Port) -> + receive + {Port, {data, Data}} -> + io:put_chars(Data), + port_loop(Port); + {Port, {exit_status, Status}} -> + halt(Status) + end. diff --git a/priv/templates/simplenode.nodetool b/priv/templates/simplenode.nodetool index 7ae8a27..a97f756 100755 --- a/priv/templates/simplenode.nodetool +++ b/priv/templates/simplenode.nodetool @@ -109,9 +109,27 @@ main(Args) -> {value, Value, _Bindings} -> io:format("~p\n", [Value]) end; + ["upgrade", ReleasePackage] -> + %% TODO: This script currently does NOT support slim releases. + %% Necessary steps to upgrade a slim release are as follows: + %% 1. unpack relup archive manually + %% 2. copy releases directory and necessary libraries + %% 3. using release_hander:set_unpacked/2 . + %% For more details, see https://github.com/rebar/rebar/pull/52 + %% and https://github.com/rebar/rebar/issues/202 + {ok, Vsn} = rpc:call(TargetNode, release_handler, unpack_release, + [ReleasePackage], 60000), + io:format("Unpacked Release ~p\n", [Vsn]), + {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, + check_install_release, [Vsn], 60000), + {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, + install_release, [Vsn], 60000), + io:format("Installed Release ~p\n", [Vsn]), + ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], 60000), + io:format("Made Release ~p Permanent\n", [Vsn]); Other -> io:format("Other: ~p\n", [Other]), - io:format("Usage: nodetool {chkconfig|getpid|ping|stop|restart|reboot|rpc|rpc_infinity|rpcterms|eval}\n") + io:format("Usage: nodetool {chkconfig|getpid|ping|stop|restart|reboot|rpc|rpc_infinity|rpcterms|eval|upgrade}\n") end, net_kernel:stop(). diff --git a/priv/templates/simplenode.reltool.config b/priv/templates/simplenode.reltool.config index eae8ab3..9b76db4 100644 --- a/priv/templates/simplenode.reltool.config +++ b/priv/templates/simplenode.reltool.config @@ -38,6 +38,7 @@ {copy, "files/{{nodeid}}", "bin/{{nodeid}}"}, {copy, "files/{{nodeid}}.cmd", "bin/{{nodeid}}.cmd"}, {copy, "files/start_erl.cmd", "bin/start_erl.cmd"}, + %% Following line may be safely removed in new projects {copy, "files/install_upgrade.escript", "bin/install_upgrade.escript"}, {copy, "files/sys.config", "releases/\{\{rel_vsn\}\}/sys.config"}, {copy, "files/vm.args", "releases/\{\{rel_vsn\}\}/vm.args"} diff --git a/priv/templates/simplenode.runner b/priv/templates/simplenode.runner index b33d475..01df613 100755 --- a/priv/templates/simplenode.runner +++ b/priv/templates/simplenode.runner @@ -335,10 +335,7 @@ case "$1" in exit $ES fi - node_name=`echo $NAME_ARG | awk '{print $2}'` - erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'` - - $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2 + $NODETOOL upgrade $2 ;; console|console_clean|console_boot) diff --git a/priv/templates/simplenode.windows.runner.cmd b/priv/templates/simplenode.windows.runner.cmd index d45f438..db3b054 100644 --- a/priv/templates/simplenode.windows.runner.cmd +++ b/priv/templates/simplenode.windows.runner.cmd @@ -95,7 +95,7 @@ start "%node_name% attach" %werl% -boot "%clean_boot_script%" -remsh %node_name% @echo NOTE {package base name} MUST NOT include the .tar.gz suffix @goto :EOF ) -@%escript% %node_root%\bin\install_upgrade.escript %node_name% %erlang_cookie% %2 +@%escript% %nodetool% -sname "%node_name%" -setcookie "%erlang_cookie%" upgrade %2 @goto :EOF :set_trim diff --git a/test/upgrade_project/rel/files/dummy b/test/upgrade_project/rel/files/dummy index d20c86e..9ea6b3d 100755 --- a/test/upgrade_project/rel/files/dummy +++ b/test/upgrade_project/rel/files/dummy @@ -276,10 +276,7 @@ case "$1" in exit $ES fi - node_name=`echo $NAME_ARG | awk '{print $2}'` - erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'` - - $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2 + $NODETOOL upgrade $2 ;; console|console_clean|console_boot)