/
riaknostic_config.erl
281 lines (259 loc) · 9.48 KB
/
riaknostic_config.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
%% -------------------------------------------------------------------
%%
%% riaknostic - automated diagnostic tools for Riak
%%
%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------
%% @doc Provides convenient access to Riak configuration values. When
%% the {@link riaknostic. riaknostic} module calls {@link
%% prepare/0. prepare/0}, Riak's <code>app.config</code> and
%% <code>vm.args</code> files will be parsed and memoized, and lager
%% will be started on the console at the configured severity level.
%% @end
-module(riaknostic_config).
-export([prepare/0,
data_directories/0,
get_app_env/1,
get_app_env/2,
get_vm_env/1,
base_dir/0,
etc_dir/0,
node_name/0,
cookie/0,
user/0]).
%% @doc Prepares appropriate configuration so the riaknostic script
%% can run. This is called by the riaknostic module and you do
%% not need to invoke it.
-spec prepare() -> ok | {error, iodata()}.
prepare() ->
prepare([fun start_lager/0, fun load_app_config/0, fun load_vm_args/0]).
prepare([]) ->
ok;
prepare([Fun|T]) ->
case Fun() of
{error, Reason} ->
{error, Reason};
_ ->
prepare(T)
end.
%% @doc Determines where Riak is configured to store data. Returns a
%% list of paths to directories defined by storage backends.
-spec data_directories() -> [ file:filename() ].
data_directories() ->
KVBackend = get_app_env([riak_kv, storage_backend]),
SearchBackend = get_app_env([riak_search, storage_backend], merge_index_backend),
Dirs = case get_app_env([riak_search, enabled]) of
true ->
data_directory(KVBackend) ++ data_directory(SearchBackend);
_ ->
data_directory(KVBackend)
end,
[ filename:absname(Dir, base_dir()) || Dir <- Dirs, Dir =/= undefined ].
%% @doc Get a key out of the app.config file, or if it doesn't exist,
%% return the Default. You specify a nested key using a list of atoms,
%% e.g. <code>[riak_kv, storage_backend]</code>.
%% @see get_app_env/1
-spec get_app_env([atom()], term()) -> term().
get_app_env(Keys, Default) ->
case get_app_env(Keys) of
undefined ->
Default;
Value ->
Value
end.
%% @doc Get a key out of the app.config file. You specify a nested
%% key using a list of atoms, e.g. <code>[riak_kv, storage_backend]</code>.
%% @equiv get_app_env(Keys, undefined)
-spec get_app_env([atom()]) -> undefined | term().
get_app_env(Keys) ->
{ok, Env} = application:get_env(riaknostic, app_config),
find_nested_key(Keys, Env).
%% @doc Get an -env flag out of the vm.args file.
-spec get_vm_env(string()) -> string() | undefined.
get_vm_env(Key) ->
case application:get_env(riaknostic, vm_env) of
undefined ->
undefined;
{ok, PList} ->
proplists:get_value(Key, PList)
end.
%% @doc Determines the user/uid that the installed Riak runs as.
-spec user() -> string().
user() ->
case application:get_env(riaknostic, user) of
{ok, Value} ->
Value;
_ -> undefined
end.
%% @doc The base directory from which the Riak script runs.
-spec base_dir() -> file:filename().
base_dir() ->
case application:get_env(riaknostic, base) of
undefined ->
filename:absname(".");
{ok, Path} ->
filename:absname(Path)
end.
%% @doc The Riak configuration directory.
-spec etc_dir() -> file:filename().
etc_dir() ->
case application:get_env(riaknostic, etc) of
undefined ->
filename:absname("./etc", base_dir());
{ok, Path} ->
filename:absname(Path, base_dir())
end.
%% @doc The local Riak node name. Includes whether the node uses short
%% or long nodenames for distributed Erlang.
-spec node_name() -> {shortnames | longnames, Name::string()}.
node_name() ->
case application:get_env(riaknostic, node_name) of
undefined ->
undefined;
{ok, Node} ->
Node
end.
%% @doc The Riak node's distributed Erlang cookie.
-spec cookie() -> atom().
cookie() ->
case application:get_env(riaknostic, cookie) of
undefined ->
undefined;
{ok, Cookie} ->
list_to_atom(Cookie)
end.
%% Private functions
start_lager() ->
application:load(lager),
case application:get_env(riaknostic, log_level) of
undefined ->
{error, "Log level not set!"};
{ok, Level} ->
application:set_env(lager, crash_log, undefined),
application:set_env(lager, handlers, [{lager_console_backend, Level}]),
lager:start()
end.
load_app_config() ->
case filelib:is_dir(etc_dir()) of
false ->
{error, "Cannot find Riak configuration directory!"};
true ->
AppConfig = filename:join([etc_dir(), "app.config"]),
case file:consult(AppConfig) of
{ok, [Config]} ->
application:set_env(riaknostic, app_config, Config);
_ ->
{error, io_lib:format("Riak config file ~s is malformed!", [AppConfig])}
end
end.
load_vm_args() ->
case filelib:is_dir(etc_dir()) of
false ->
{error, "Cannot find Riak configuration directory!"};
true ->
VmArgs = filename:join([etc_dir(), "vm.args"]),
case file:read_file(VmArgs) of
{error, Reason} ->
{error, io_lib:format("Could not read ~s, received error ~w!", [VmArgs, Reason])};
{ok, Binary} ->
load_vm_args(Binary)
end
end.
load_vm_args(Bin) when is_binary(Bin) ->
load_vm_args(re:split(Bin, "\s*\r?\n\s*", [{return, list}, trim]));
load_vm_args([]) ->
ok;
load_vm_args([[$#|_]|T]) ->
load_vm_args(T);
load_vm_args([""|T]) ->
load_vm_args(T);
load_vm_args(["-sname " ++ NodeName|T]) ->
application:set_env(riaknostic, node_name, {shortnames, string:strip(NodeName)}),
load_vm_args(T);
load_vm_args(["-name " ++ NodeName|T]) ->
application:set_env(riaknostic, node_name, {longnames, string:strip(NodeName)}),
load_vm_args(T);
load_vm_args(["-setcookie " ++ Cookie|T]) ->
application:set_env(riaknostic, cookie, string:strip(Cookie)),
load_vm_args(T);
load_vm_args(["-env " ++ Env|T]) ->
[Key, Value] = re:split(Env, "\s+", [{return, list}, trim]),
add_or_insert_env(vm_env, {Key, Value}),
load_vm_args(T);
load_vm_args([[$+|EmuFlags]|T]) ->
[Flag|Rest] = re:split(EmuFlags, "\s+", [{return,list}, trim]),
add_or_insert_env(emu_flags, {[$+|Flag], Rest}),
load_vm_args(T);
load_vm_args([[$-|InitFlags]|T]) ->
[Flag|Rest] = re:split(InitFlags, "\s+", [{return,list}, trim]),
add_or_insert_env(init_flags, {[$-|Flag], Rest}),
load_vm_args(T);
load_vm_args([Line|_]) ->
{error, io_lib:format("Erroneous line in vm.args: ~s", [Line])}.
add_or_insert_env(Key, Value) ->
case application:get_env(riaknostic, Key) of
undefined ->
application:set_env(riaknostic, Key, [Value]);
{ok, List} ->
application:set_env(riaknostic, Key, [Value|List])
end.
find_nested_key(_, undefined) ->
undefined;
find_nested_key([], Val) ->
Val;
find_nested_key([Key|T], PList) ->
find_nested_key(T, proplists:get_value(Key, PList)).
%% Determine the data directory(ies) for the configured storage backend
-spec data_directory(atom()) -> [ file:filename() ].
data_directory(riak_kv_bitcask_backend) ->
[ get_app_env([bitcask, data_root]) ];
data_directory(riak_kv_eleveldb_backend) ->
[ get_app_env([eleveldb, data_root]) ];
data_directory(merge_index_backend) ->
[ get_app_env([merge_index, data_root]) ];
data_directory(riak_kv_innostore_backend) ->
[ get_app_env([innostore, data_home_dir]),
get_app_env([innostore, log_group_home_dir]) ];
data_directory(riak_kv_multi_backend) ->
[ multi_data_directory(Backend) ||
Backend <- get_app_env([riak_kv, multi_backend]),
element(2, Backend) =/= riak_kv_memory_backend ];
data_directory(_) -> %% Memory or unknown backend
[].
%% Extracts data paths from multi_backend config
multi_data_directory({_, riak_kv_bitcask_backend, Props}) ->
case proplists:get_value(data_root, Props) of
undefined ->
get_app_env([bitcask, data_root]);
Path when is_list(Path) ->
Path
end;
multi_data_directory({_, riak_kv_eleveldb_backend, Props}) ->
case proplists:get_value(data_root, Props) of
undefined ->
get_app_env([eleveldb, data_root]);
Path when is_list(Path) ->
Path
end;
multi_data_directory({_, riak_kv_innostore_backend, Props}) ->
case proplists:get_value(data_home_dir, Props) of
undefined ->
get_app_env([innostore, data_home_dir]);
Path when is_list(Path) ->
Path
end.