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