Debian Codegraph @

priv/templates/simplenode.nodetool

raw
1 #!/usr/bin/env escript
2 %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
3 %% ex: ft=erlang ts=4 sw=4 et
4 %% -------------------------------------------------------------------
5 %%
6 %% nodetool: Helper Script for interacting with live nodes
7 %%
8 %% -------------------------------------------------------------------
9 main(Args) ->
10 ok = start_epmd(),
11 %% Extract the args
12 {RestArgs, TargetNode} = process_args(Args, [], undefined),
13
14 %% any commands that don't need a running node
15 case RestArgs of
16 ["chkconfig", File] ->
17 case file:consult(File) of
18 {ok, _} ->
19 io:format("ok\n"),
20 halt(0);
21 {error, {Line, Mod, Term}} ->
22 io:format(standard_error, ["Error on line ",
23 file:format_error({Line, Mod, Term}), "\n"], []),
24 halt(1);
25 {error, R} ->
26 io:format(standard_error, ["Error reading config file: ",
27 file:format_error(R), "\n"], []),
28 halt(1)
29 end;
30 _ ->
31 ok
32 end,
33
34 %% See if the node is currently running -- if it's not, we'll bail
35 case {net_kernel:hidden_connect_node(TargetNode),
36 net_adm:ping(TargetNode)} of
37 {true, pong} ->
38 ok;
39 {false,pong} ->
40 io:format("Failed to connect to node ~p .\n", [TargetNode]),
41 halt(1);
42 {_, pang} ->
43 io:format("Node ~p not responding to pings.\n", [TargetNode]),
44 halt(1)
45 end,
46
47 case RestArgs of
48 ["getpid"] ->
49 io:format("~p\n",
50 [list_to_integer(rpc:call(TargetNode, os, getpid, []))]);
51 ["ping"] ->
52 %% If we got this far, the node already responsed to a
53 %% ping, so just dump a "pong"
54 io:format("pong\n");
55 ["stop"] ->
56 io:format("~p\n", [rpc:call(TargetNode, init, stop, [], 60000)]);
57 ["restart"] ->
58 io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]);
59 ["reboot"] ->
60 io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]);
61 ["rpc", Module, Function | RpcArgs] ->
62 case rpc:call(TargetNode,
63 list_to_atom(Module),
64 list_to_atom(Function),
65 [RpcArgs], 60000) of
66 ok ->
67 ok;
68 {badrpc, Reason} ->
69 io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
70 halt(1);
71 _ ->
72 halt(1)
73 end;
74 ["rpc_infinity", Module, Function | RpcArgs] ->
75 case rpc:call(TargetNode,
76 list_to_atom(Module),
77 list_to_atom(Function),
78 [RpcArgs], infinity) of
79 ok ->
80 ok;
81 {badrpc, Reason} ->
82 io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
83 halt(1);
84 _ ->
85 halt(1)
86 end;
87 ["rpcterms", Module, Function, ArgsAsString] ->
88 case rpc:call(TargetNode,
89 list_to_atom(Module),
90 list_to_atom(Function),
91 consult(ArgsAsString), 60000) of
92 {badrpc, Reason} ->
93 io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
94 halt(1);
95 Other ->
96 io:format("~p\n", [Other])
97 end;
98 ["eval", Str0] ->
99 Str = string:strip(Str0, right, $.) ++ ".",
100 Bindings = erl_eval:new_bindings(),
101 case rpc:call(TargetNode,
102 erl_eval,
103 exprs,
104 [parse(Str), Bindings],
105 60000) of
106 {badrpc, Reason} ->
107 io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
108 halt(1);
109 {value, Value, _Bindings} ->
110 io:format("~p\n", [Value])
111 end;
112 ["upgrade", ReleasePackage] ->
113 %% TODO: This script currently does NOT support slim releases.
114 %% Necessary steps to upgrade a slim release are as follows:
115 %% 1. unpack relup archive manually
116 %% 2. copy releases directory and necessary libraries
117 %% 3. using release_hander:set_unpacked/2 .
118 %% For more details, see https://github.com/rebar/rebar/pull/52
119 %% and https://github.com/rebar/rebar/issues/202
120 {ok, Vsn} = rpc:call(TargetNode, release_handler, unpack_release,
121 [ReleasePackage], 60000),
122 io:format("Unpacked Release ~p\n", [Vsn]),
123 {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler,
124 check_install_release, [Vsn], 60000),
125 {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler,
126 install_release, [Vsn], 60000),
127 io:format("Installed Release ~p\n", [Vsn]),
128 ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], 60000),
129 io:format("Made Release ~p Permanent\n", [Vsn]);
130 Other ->
131 io:format("Other: ~p\n", [Other]),
132 io:format("Usage: nodetool {chkconfig|getpid|ping|stop|restart|reboot|rpc|rpc_infinity|rpcterms|eval|upgrade}\n")
133 end,
134 net_kernel:stop().
135
136 process_args([], Acc, TargetNode) ->
137 {lists:reverse(Acc), TargetNode};
138 process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) ->
139 erlang:set_cookie(node(), list_to_atom(Cookie)),
140 process_args(Rest, Acc, TargetNode);
141 process_args(["-name", TargetName | Rest], Acc, _) ->
142 ThisNode = append_node_suffix(TargetName, "_maint_"),
143 {ok, _} = net_kernel:start([ThisNode, longnames]),
144 process_args(Rest, Acc, nodename(TargetName));
145 process_args(["-sname", TargetName | Rest], Acc, _) ->
146 ThisNode = append_node_suffix(TargetName, "_maint_"),
147 {ok, _} = net_kernel:start([ThisNode, shortnames]),
148 process_args(Rest, Acc, nodename(TargetName));
149 process_args([Arg | Rest], Acc, Opts) ->
150 process_args(Rest, [Arg | Acc], Opts).
151
152
153 start_epmd() ->
154 [] = os:cmd(epmd_path() ++ " -daemon"),
155 ok.
156
157 epmd_path() ->
158 ErtsBinDir = filename:dirname(escript:script_name()),
159 Name = "epmd",
160 case os:find_executable(Name, ErtsBinDir) of
161 false ->
162 case os:find_executable(Name) of
163 false ->
164 io:format("Could not find epmd.~n"),
165 halt(1);
166 GlobalEpmd ->
167 GlobalEpmd
168 end;
169 Epmd ->
170 Epmd
171 end.
172
173
174 nodename(Name) ->
175 case string:tokens(Name, "@") of
176 [_Node, _Host] ->
177 list_to_atom(Name);
178 [Node] ->
179 [_, Host] = string:tokens(atom_to_list(node()), "@"),
180 list_to_atom(lists:concat([Node, "@", Host]))
181 end.
182
183 append_node_suffix(Name, Suffix) ->
184 case string:tokens(Name, "@") of
185 [Node, Host] ->
186 list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host]));
187 [Node] ->
188 list_to_atom(lists:concat([Node, Suffix, os:getpid()]))
189 end.
190
191
192 %%
193 %% Given a string or binary, parse it into a list of terms, ala file:consult/0
194 %%
195 consult(Str) when is_list(Str) ->
196 consult([], Str, []);
197 consult(Bin) when is_binary(Bin)->
198 consult([], binary_to_list(Bin), []).
199
200 consult(Cont, Str, Acc) ->
201 case erl_scan:tokens(Cont, Str, 0) of
202 {done, Result, Remaining} ->
203 case Result of
204 {ok, Tokens, _} ->
205 {ok, Term} = erl_parse:parse_term(Tokens),
206 consult([], Remaining, [Term | Acc]);
207 {eof, _Other} ->
208 lists:reverse(Acc);
209 {error, Info, _} ->
210 {error, Info}
211 end;
212 {more, Cont1} ->
213 consult(Cont1, eof, Acc)
214 end.
215
216 parse(Str) ->
217 {ok, Tokens, _} = erl_scan:string(Str),
218 {ok, Exprs} = erl_parse:parse_exprs(Tokens),
219 Exprs.