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