Debian Codegraph @

test/upgrade_project/rel/files/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 Other ->
113 io:format("Other: ~p\n", [Other]),
114 io:format("Usage: nodetool {chkconfig|getpid|ping|stop|restart|reboot|rpc|rpc_infinity|rpcterms|eval}\n")
115 end,
116 net_kernel:stop().
117
118 process_args([], Acc, TargetNode) ->
119 {lists:reverse(Acc), TargetNode};
120 process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) ->
121 erlang:set_cookie(node(), list_to_atom(Cookie)),
122 process_args(Rest, Acc, TargetNode);
123 process_args(["-name", TargetName | Rest], Acc, _) ->
124 ThisNode = append_node_suffix(TargetName, "_maint_"),
125 {ok, _} = net_kernel:start([ThisNode, longnames]),
126 process_args(Rest, Acc, nodename(TargetName));
127 process_args(["-sname", TargetName | Rest], Acc, _) ->
128 ThisNode = append_node_suffix(TargetName, "_maint_"),
129 {ok, _} = net_kernel:start([ThisNode, shortnames]),
130 process_args(Rest, Acc, nodename(TargetName));
131 process_args([Arg | Rest], Acc, Opts) ->
132 process_args(Rest, [Arg | Acc], Opts).
133
134
135 start_epmd() ->
136 [] = os:cmd(epmd_path() ++ " -daemon"),
137 ok.
138
139 epmd_path() ->
140 ErtsBinDir = filename:dirname(escript:script_name()),
141 Name = "epmd",
142 case os:find_executable(Name, ErtsBinDir) of
143 false ->
144 case os:find_executable(Name) of
145 false ->
146 io:format("Could not find epmd.~n"),
147 halt(1);
148 GlobalEpmd ->
149 GlobalEpmd
150 end;
151 Epmd ->
152 Epmd
153 end.
154
155
156 nodename(Name) ->
157 case string:tokens(Name, "@") of
158 [_Node, _Host] ->
159 list_to_atom(Name);
160 [Node] ->
161 [_, Host] = string:tokens(atom_to_list(node()), "@"),
162 list_to_atom(lists:concat([Node, "@", Host]))
163 end.
164
165 append_node_suffix(Name, Suffix) ->
166 case string:tokens(Name, "@") of
167 [Node, Host] ->
168 list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host]));
169 [Node] ->
170 list_to_atom(lists:concat([Node, Suffix, os:getpid()]))
171 end.
172
173
174 %%
175 %% Given a string or binary, parse it into a list of terms, ala file:consult/0
176 %%
177 consult(Str) when is_list(Str) ->
178 consult([], Str, []);
179 consult(Bin) when is_binary(Bin)->
180 consult([], binary_to_list(Bin), []).
181
182 consult(Cont, Str, Acc) ->
183 case erl_scan:tokens(Cont, Str, 0) of
184 {done, Result, Remaining} ->
185 case Result of
186 {ok, Tokens, _} ->
187 {ok, Term} = erl_parse:parse_term(Tokens),
188 consult([], Remaining, [Term | Acc]);
189 {eof, _Other} ->
190 lists:reverse(Acc);
191 {error, Info, _} ->
192 {error, Info}
193 end;
194 {more, Cont1} ->
195 consult(Cont1, eof, Acc)
196 end.
197
198 parse(Str) ->
199 {ok, Tokens, _} = erl_scan:string(Str),
200 {ok, Exprs} = erl_parse:parse_exprs(Tokens),
201 Exprs.