./ct_report/coverage/mongoose_server_api.COVER.html

1 -module(mongoose_server_api).
2
3 -export([status/0, get_cookie/0, join_cluster/1, leave_cluster/0,
4 remove_from_cluster/1, stop/0, restart/0, remove_node/1, set_loglevel/1,
5 get_loglevel/0]).
6
7 -spec get_loglevel() -> {ok, mongoose_logs:atom_log_level()}.
8 get_loglevel() ->
9 20 {ok, mongoose_logs:get_global_loglevel()}.
10
11 -spec set_loglevel(mongoose_logs:atom_log_level()) -> {ok, iolist()} | {invalid_level, iolist()}.
12 set_loglevel(Level) ->
13 20 case mongoose_logs:set_global_loglevel(Level) of
14 ok ->
15 20 {ok, "Log level successfully set."};
16 {error, _} ->
17
:-(
{invalid_level, io_lib:format("Log level ~p does not exist.", [Level])}
18 end.
19
20 -spec status() -> {ok, {boolean(), iolist(), iolist(), iodata()}}.
21 status() ->
22 2 {InternalStatus, ProvidedStatus} = init:get_status(),
23 2 String1 = io_lib:format("The node ~p is ~p. Status: ~p.",
24 [node(), InternalStatus, ProvidedStatus]),
25 2 Result =
26 case lists:keysearch(mongooseim, 1, application:which_applications()) of
27 false ->
28
:-(
{false, String1 ++ " MongooseIM is not running in that node.",
29 "MongooseIM is not running in that node", "MongooseIM is not running in that node"};
30 {value, {_, _, Version}} ->
31 2 [Number | Rest] = string:tokens(Version, "-"),
32 2 {true,
33 String1 ++ io_lib:format(" MongooseIM ~s is running in that node.", [Version]),
34 Number,
35 get_commit_hash(Rest)}
36 end,
37 2 {ok, Result}.
38
39 2 get_commit_hash([_, "g" ++ Hash]) -> Hash;
40
:-(
get_commit_hash(_) -> <<>>.
41
42 -spec get_cookie() -> {ok, iolist()}.
43 get_cookie() ->
44 2 {ok, atom_to_list(erlang:get_cookie())}.
45
46 -spec join_cluster(string()) -> {ok, iolist()}
47 | {pang | already_joined | mnesia_error | error, iolist()}.
48 join_cluster(NodeString) ->
49 13 NodeAtom = list_to_atom(NodeString),
50 13 NodeList = mnesia:system_info(db_nodes),
51 13 case lists:member(NodeAtom, NodeList) of
52 true ->
53 2 String = io_lib:format(
54 "The MongooseIM node ~s has already joined the cluster.", [NodeString]),
55 2 {already_joined, String};
56 _ ->
57 11 do_join_cluster(NodeAtom)
58 end.
59
60 do_join_cluster(Node) ->
61 11 try mongoose_cluster:join(Node) of
62 ok ->
63 10 String = io_lib:format("You have successfully added the MongooseIM node"
64 " ~p to the cluster with node member ~p.", [node(), Node]),
65 10 {ok, String}
66 catch
67 error:pang ->
68 1 String = io_lib:format(
69 "Timeout while attempting to connect to a MongooseIM node ~s~n", [Node]),
70 1 {pang, String};
71 error:{cant_get_storage_type, {T, E, R}} ->
72
:-(
String =
73 io_lib:format("Cannot get storage type for table ~p. Reason: ~p:~p", [T, E, R]),
74
:-(
{mnesia_error, String};
75 E:R ->
76
:-(
String =
77 io_lib:format("Failed to join the cluster. Reason: ~p:~p", [E, R]),
78
:-(
{error, String}
79 end.
80
81 -spec leave_cluster() -> {ok, string()} | {error | not_in_cluster, iolist()}.
82 leave_cluster() ->
83 14 NodeList = mnesia:system_info(running_db_nodes),
84 14 ThisNode = node(),
85 14 case NodeList of
86 [ThisNode] ->
87 6 String = io_lib:format("The MongooseIM node ~p is not in the cluster~n", [node()]),
88 6 {not_in_cluster, String};
89 _ ->
90 8 do_leave_cluster()
91 end.
92
93 do_leave_cluster() ->
94 8 try mongoose_cluster:leave() of
95 ok ->
96 8 String = io_lib:format(
97 "The MongooseIM node ~p has successfully left the cluster~n", [node()]),
98 8 {ok, String}
99 catch
100 E:R ->
101
:-(
String = io_lib:format("Failed to leave the cluster. Reason: ~p:~p", [E, R]),
102
:-(
{error, String}
103 end.
104
105 -spec remove_from_cluster(string()) -> {ok, iolist()} |
106 {node_is_alive | mnesia_error | rpc_error, iolist()}.
107 remove_from_cluster(NodeString) ->
108 5 Node = list_to_atom(NodeString),
109 5 IsNodeAlive = mongoose_cluster:is_node_alive(Node),
110 5 case IsNodeAlive of
111 true ->
112 3 remove_rpc_alive_node(Node);
113 false ->
114 2 remove_dead_node(Node)
115 end.
116
117 remove_dead_node(DeadNode) ->
118 2 try mongoose_cluster:remove_from_cluster(DeadNode) of
119 ok ->
120 2 String =
121 io_lib:format(
122 "The dead MongooseIM node ~p has been removed from the cluster~n", [DeadNode]),
123 2 {ok, String}
124 catch
125 error:{node_is_alive, DeadNode} ->
126
:-(
String = io_lib:format(
127 "The MongooseIM node ~p is alive but shoud not be.", [DeadNode]),
128
:-(
{node_is_alive, String};
129 error:{del_table_copy_schema, R} ->
130
:-(
String = io_lib:format("Cannot delete table schema. Reason: ~p", [R]),
131
:-(
{mnesia_error, String}
132 end.
133
134 remove_rpc_alive_node(AliveNode) ->
135 3 case rpc:call(AliveNode, mongoose_cluster, leave, []) of
136 {badrpc, Reason} ->
137
:-(
String =
138 io_lib:format(
139 "Cannot remove the MongooseIM node ~p~n. RPC Reason: ~p", [AliveNode, Reason]),
140
:-(
{rpc_error, String};
141 ok ->
142 3 String = io_lib:format(
143 "The MongooseIM node ~p has been removed from the cluster", [AliveNode]),
144 3 {ok, String};
145 Unknown ->
146
:-(
String = io_lib:format("Unknown error: ~p", [Unknown]),
147
:-(
{rpc_error, String}
148 end.
149
150 -spec stop() -> ok.
151 stop() ->
152
:-(
timer:sleep(500),
153
:-(
init:stop().
154
155 -spec restart() -> ok.
156 restart() ->
157
:-(
timer:sleep(500),
158
:-(
init:restart().
159
160 -spec remove_node(string()) -> {ok, iolist()}.
161 remove_node(Node) ->
162
:-(
mnesia:del_table_copy(schema, list_to_atom(Node)),
163
:-(
{ok, "MongooseIM node removed from the Mnesia schema"}.
Line Hits Source