-
Notifications
You must be signed in to change notification settings - Fork 23
/
ejabberd_node_utils.erl
197 lines (157 loc) · 7.74 KB
/
ejabberd_node_utils.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
%%==============================================================================
%% Copyright 2014 Erlang Solutions Ltd.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%==============================================================================
-module(ejabberd_node_utils).
-export([init/1, init/2,
restart_application/1, restart_application/2,
call_fun/3, call_fun/4,
call_ctl/2, call_ctl/3,
file_exists/1, file_exists/2,
backup_config_file/1, backup_config_file/2,
restore_config_file/1, restore_config_file/2,
modify_config_file/2, modify_config_file/4,
get_cwd/2]).
-include_lib("common_test/include/ct.hrl").
-define(CWD(Node, Config), ?config({ejabberd_cwd, Node}, Config)).
-define(CURRENT_CFG_PATH(Node, Config),
filename:join([?CWD(Node, Config), "etc", "ejabberd.cfg"])).
-define(BACKUP_CFG_PATH(Node, Config),
filename:join([?CWD(Node, Config), "etc","ejabberd.cfg.bak"])).
-define(CFG_TEMPLATE_PATH(Node, Config),
filename:join([?CWD(Node, Config), "..", "..", "rel", "files",
"ejabberd.cfg"])).
-define(CFG_VARS_PATH(Node, Config, File),
filename:join([?CWD(Node, Config), "..", "..", "rel",
File])).
-define(CTL_PATH(Node, Config),
filename:join([?CWD(Node, Config), "bin", "mongooseimctl"])).
-type ct_config() :: list({Key :: term(), Value :: term()}).
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
-spec init(ct_config()) -> ct_config().
init(Config) ->
Node = ct:get_config(ejabberd_node),
init(Node, Config).
init(Node, Config) ->
set_ejabberd_node_cwd(Node, Config).
-spec restart_application(atom()) -> ok.
restart_application(ApplicationName) ->
Node = ct:get_config(ejabberd_node),
restart_application(Node, ApplicationName).
-spec restart_application(node(), atom()) -> ok.
restart_application(Node, ApplicationName) ->
ok = ejabberd_node_utils:call_fun(Node, application, stop, [ApplicationName]),
ok = ejabberd_node_utils:call_fun(Node, application, start, [ApplicationName]).
-spec backup_config_file(ct_config()) -> ct_config().
backup_config_file(Config) ->
Node = ct:get_config(ejabberd_node),
backup_config_file(Node, Config).
-spec backup_config_file(node(), ct_config()) -> ct_config().
backup_config_file(Node, Config) ->
{ok, _} = call_fun(Node, file, copy, [?CURRENT_CFG_PATH(Node, Config),
?BACKUP_CFG_PATH(Node, Config)]).
-spec restore_config_file(ct_config()) -> ct_config().
restore_config_file(Config) ->
Node = ct:get_config(ejabberd_node),
restore_config_file(Node, Config).
-spec restore_config_file(node(), ct_config()) -> ct_config().
restore_config_file(Node, Config) ->
ok = call_fun(Node, file, rename, [?BACKUP_CFG_PATH(Node, Config),
?CURRENT_CFG_PATH(Node, Config)]).
-spec call_fun(module(), atom(), []) -> term() | {badrpc, term()}.
call_fun(M, F, A) ->
Node = ct:get_config(ejabberd_node),
call_fun(Node, M, F, A).
-spec call_fun(node(), module(), atom(), []) -> term() | {badrpc, term()}.
call_fun(Node, M, F, A) ->
rpc:call(Node, M, F, A).
%% @doc Calls the mongooseimctl script with given command `Cmd'.
%%
%% For example to restart mongooseim call `call_ctl(restart, Config).'.
-spec call_ctl(atom(), ct_config()) -> term() | term().
call_ctl(Cmd, Config) ->
Node = ct:get_config(ejabberd_node),
call_ctl(Node, Cmd, Config).
-spec call_ctl(node(), atom(), ct_config()) -> term() | term().
call_ctl(Node, Cmd, Config) ->
OsCmd = io_lib:format("~p ~p", [?CTL_PATH(Node, Config), atom_to_list(Cmd)]),
os:cmd(OsCmd).
-spec file_exists(file:name_all()) -> term() | {badrpc, term()}.
file_exists(Filename) ->
call_fun(filelib, is_file, [Filename]).
-spec file_exists(node(), file:name_all()) -> term() | {badrpc, term()}.
file_exists(Node, Filename) ->
call_fun(Node, filelib, is_file, [Filename]).
%% @doc Modifies default ejabberd config file: `etc/ejabberd.cfg'.
%%
%% This function assumes that the config file was generated from template
%% file in `rel/files/ejabberd.cfg' using variables from `rel/vars.config'.
%% The modification procedure overrides given variables provided in
%% `rel/vars.config'.
%%
%% For example to change `hosts' value in the configuration file one
%% has to call the function as follows:
%% ```NewHosts = {hosts, "[\"" ++ binary_to_list(Domain) ++ "\"]"},
%% modify_config_file([NewHosts], Config).'''
-spec modify_config_file([{ConfigVariable, Value}], ct_config()) -> ok when
ConfigVariable :: atom(),
Value :: string().
modify_config_file(CfgVarsToChange, Config) ->
Node = ct:get_config(ejabberd_node),
modify_config_file(Node, "vars.config", CfgVarsToChange, Config).
-spec modify_config_file(node(), string(), [{ConfigVariable, Value}], ct_config()) -> ok when
ConfigVariable :: atom(),
Value :: string().
modify_config_file(Node, VarsFile, CfgVarsToChange, Config) ->
CurrentCfgPath = ?CURRENT_CFG_PATH(Node, Config),
{ok, CfgTemplate} = ejabberd_node_utils:call_fun(Node,
file, read_file, [?CFG_TEMPLATE_PATH(Node, Config)]),
{ok, DefaultVars} = ejabberd_node_utils:call_fun(Node, file, consult,
[?CFG_VARS_PATH(Node, Config, "vars.config")]),
{ok, NodeVars} = ejabberd_node_utils:call_fun(Node, file, consult,
[?CFG_VARS_PATH(Node, Config, VarsFile)]),
PresetVars = case proplists:get_value(preset, Config) of
undefined ->
[];
Name ->
Presets = ct:get_config(ejabberd_presets),
proplists:get_value(list_to_existing_atom(Name), Presets)
end,
CfgVars1 = dict:to_list(dict:merge(fun(_, V, _) -> V end,
dict:from_list(NodeVars),
dict:from_list(DefaultVars))),
CfgVars = dict:to_list(dict:merge(fun(_, V, _) -> V end,
dict:from_list(PresetVars),
dict:from_list(CfgVars1))),
UpdatedCfgVars = update_config_variables(CfgVarsToChange, CfgVars),
CfgTemplateList = binary_to_list(CfgTemplate),
UpdatedCfgFile = mustache:render(CfgTemplateList,
dict:from_list(UpdatedCfgVars)),
ok = ejabberd_node_utils:call_fun(Node, file, write_file, [CurrentCfgPath,
UpdatedCfgFile]).
-spec get_cwd(node(), ct_config()) -> string().
get_cwd(Node, Config) ->
?CWD(Node, Config).
%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------
set_ejabberd_node_cwd(Node, Config) ->
{ok, Cwd} = call_fun(Node, file, get_cwd, []),
[{{ejabberd_cwd, Node}, Cwd} | Config].
update_config_variables(CfgVarsToChange, CfgVars) ->
lists:foldl(fun({Var, Val}, Acc) ->
lists:keystore(Var, 1, Acc,{Var, Val})
end, CfgVars, CfgVarsToChange).