Skip to content

Commit

Permalink
load plugin config from priv/default.d/*.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Aug 1, 2013
1 parent c6a4b5d commit 059935c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 58 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ if TESTS
mkdir -p $(top_builddir)/tmp/lib
mkdir -p $(top_builddir)/tmp/log
mkdir -p $(top_builddir)/tmp/run/couchdb
mkdir -p $(top_builddir)/tmp/plugins
endif

install-data-hook:
Expand Down
1 change: 1 addition & 0 deletions bin/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ couchdb: couchdb.tpl
-e "s|%localstatelogdir%|@localstatelogdir@|g" \
-e "s|%localstatelibdir%|@localstatelibdir@|g" \
-e "s|%localstatedir%|@localstatedir@|g" \
-e "s|%locallibdir%|@locallibdir@|g" \
-e "s|%bug_uri%|@bug_uri@|g" \
-e "s|%package_author_address%|@package_author_address@|g" \
-e "s|%package_author_name%|@package_author_name@|g" \
Expand Down
39 changes: 23 additions & 16 deletions bin/couchdb.tpl.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ HEART_BEAT_TIMEOUT=11
HEART_COMMAND="%bindir%/%couchdb_command_name% -k"
INTERACTIVE=false
KILL=false
PLUGINS_DIR=%locallibdir%/plugins
LOCAL_CONFIG_DIR=%localconfdir%/local.d
LOCAL_CONFIG_FILE=%localconfdir%/%localini%
PID_FILE=%localstatedir%/run/couchdb/couchdb.pid
Expand Down Expand Up @@ -144,6 +145,8 @@ _add_config_dir () {
_load_config () {
_add_config_file "$DEFAULT_CONFIG_FILE"
_add_config_dir "$DEFAULT_CONFIG_DIR"
# We initialize plugins here to get the desired default config load order
_find_plugins
_add_config_file "$LOCAL_CONFIG_FILE"
_add_config_dir "$LOCAL_CONFIG_DIR"
if [ "$COUCHDB_ADDITIONAL_CONFIG_FILE" != '' ]
Expand Down Expand Up @@ -216,6 +219,26 @@ check_environment () {
fi
}

_find_plugins () {
# Find plugins and add them to the Erlang path and load their default
# configurations. This should be called from _load_config.
if test -d "$PLUGINS_DIR"; then
for plugin in "$PLUGINS_DIR"/*; do
if echo "$COUCH_PLUGIN_BLACKLIST" | grep "$plugin" > /dev/null 2> /dev/null; then
: # Do not use this plugin.
else
if echo "$ERL_ZFLAGS" | grep "$plugin/ebin" > /dev/null 2> /dev/null; then
: # It's already loaded.
else
ERL_ZFLAGS="$ERL_ZFLAGS -pz '$plugin/ebin'"
fi
_add_config_dir "$plugin/priv/default.d"
fi
done
export ERL_ZFLAGS
fi
}

start_couchdb () {
if test ! "$RECURSED" = "true"; then
if check_status 2> /dev/null; then
Expand All @@ -232,22 +255,6 @@ start_couchdb () {
interactive_option="+Bd -noinput"
fi

# Find plugins and add them to the Erlang path.
if test -d "%localerlanglibdir%/../../plugins"; then
for plugin in "%localerlanglibdir%/../../plugins"/*; do
if echo "$ERL_ZFLAGS" | grep "$plugin/ebin" > /dev/null 2> /dev/null; then
: # It's already loaded.
else
if echo "$COUCH_PLUGIN_BLACKLIST" | grep "$plugin" > /dev/null 2> /dev/null; then
: # Do not use this plugin.
else
ERL_ZFLAGS="$ERL_ZFLAGS -pz '$plugin/ebin'"
fi
fi
done
export ERL_ZFLAGS
fi

command="%ERL% $interactive_option $ERL_START_OPTIONS \
-env ERL_LIBS $ERL_LIBS:%localerlanglibdir% -couch_ini $start_arguments -s couch"
if test "$BACKGROUND" = "true" -a "$RECURSED" = "false"; then
Expand Down
10 changes: 7 additions & 3 deletions share/doc/src/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ order.

1. ``PREFIX/default.ini``

2. ``PREFIX/default.d/*``
2. ``PREFIX/default.d/*.ini``

3. ``PREFIX/local.ini``
3. ``PLUGINS_DIR/*/priv/default.d/*.ini``

4. ``PREFIX/local.d/*``
4. ``PREFIX/local.ini``

5. ``PREFIX/local.d/*.ini``

Settings in successive documents override the settings in earlier
entries. For example, setting the ``bind_address`` parameter in
Expand All @@ -45,6 +47,8 @@ entries. For example, setting the ``bind_address`` parameter in
The ``default.ini`` file may be overwritten during an upgrade or
re-installation, so localised changes should be made to the
``local.ini`` file or files within the ``local.d`` directory.
When a plugin is installed at runtime, its configuration will be
loaded as-is, possibly overriding local configuration.

.. _update-notifications:

Expand Down
1 change: 1 addition & 0 deletions src/couch_plugins/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## License for the specific language governing permissions and limitations under
## the License.

couch_pluginslibdir = $(localerlanglibdir)/couch_plugins-0.1
couch_pluginsebindir = $(couch_pluginslibdir)/ebin

couch_pluginsebin_DATA = $(compiled_files)
Expand Down
8 changes: 4 additions & 4 deletions src/couch_plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ to the Erlang code path
(`code:add_path("/tmp/couchdb_plugins/geocouch-couchdb1.2.x_v0.3.0-12-g4ea0bea-R15B03/ebin")`)
and loads the included application (`application:load(geocouch)`).

Then it looks into the `./config` directory that lives next to `ebin/`
in the plugin directory for a file `config.erlt` (“erl-terms”). with a
list of configuration parameters to load. We parse the file and set
the config directives one by one.
Then it looks into the `./priv/default.d` directory that lives next to
`ebin/` in the plugin directory for configuration `.ini` files and loads them.
On next startup these configuration files are loaded after global defaults,
and before any local configuration.

If that all goes to plan, we report success back to the HTTP caller.

Expand Down
53 changes: 19 additions & 34 deletions src/couch_plugins/src/couch_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

-define(PLUGIN_DIR, "/tmp/couchdb_plugins").

log(T) ->
log(T) ->
?LOG_DEBUG("[couch_plugins] ~p ~n", [T]).

%% "geocouch", "http://localhost:8000/dist", "1.0.0"
Expand All @@ -45,44 +45,30 @@ install({Name, _BaseUrl, Version, Checksums}=Plugin) ->
log("loaded plugin"),
ok.

-spec load_config(string(), string()) -> ok | {error, string()}.
-spec load_config(string(), string()) -> ok.
load_config(Name, Version) ->
ConfigFile = ?PLUGIN_DIR ++ "/" ++ get_file_slug(Name, Version) ++ "/priv/config.erlt",
load_config_file(file_exists(ConfigFile), ConfigFile).

-spec load_config_file(boolean(), string()) -> ok | {error, string()}.
load_config_file(false, _) -> ok;
load_config_file(true, ConfigFile) ->
% read file
{ok, ConfigFileData} = file:read_file(ConfigFile),
% split by \n
Lines = binary:split(ConfigFileData, <<"\n">>, [global]),
% feed each line...
lists:foreach(
fun(<<>>) ->
ok; % skip empty lines
(<<";", _Rest/binary>>) ->
ok; % ignore comments
(Line) ->
% ...to couch_util:parse_term()...
case couch_util:parse_term(Line) of
{ok, {{Section, Key}, Value}} ->
% ...and set the configs
?LOG_DEBUG("parsed Line correctly: ~p", [Line]),
couch_config:set(Section, Key, Value);
Else ->
?LOG_ERROR("Error parsing plugin config from line ~s", [Line]),
Else
end
end, Lines),
ok.
lists:foreach(
fun load_config_file/1,
filelib:wildcard(
filename:join(
[?PLUGIN_DIR, get_file_slug(Name, Version),
"priv", "default.d", "*.ini"]))).

-spec load_config_file(string()) -> ok.
load_config_file(File) ->
{ok, Config} = couch_config:parse_ini_file(File),
lists:foreach(fun set_config/1, Config).

-spec set_config({{string(), string()}, string()}) -> ok.
set_config({{Section, Key}, Value}) ->
ok = couch_config:set(Section, Key, Value, false).

-spec add_code_path(string(), string()) -> ok | {error, bad_directory}.
add_code_path(Name, Version) ->
PluginPath = ?PLUGIN_DIR ++ "/" ++ get_file_slug(Name, Version) ++ "/ebin",
case code:add_path(PluginPath) of
true -> ok;
Else ->
Else ->
?LOG_ERROR("Failed to add PluginPath: '~s'", [PluginPath]),
Else
end.
Expand All @@ -103,7 +89,7 @@ untargz(Filename) ->
ok = filelib:ensure_dir(?PLUGIN_DIR),
% untar
erl_tar:extract({binary, TarData}, [{cwd, ?PLUGIN_DIR}, keep_old_files]).


% downloads a pluygin .tar.gz into a local plugins directory
-spec download(string()) -> ok | {error, string()}.
Expand Down Expand Up @@ -256,4 +242,3 @@ does_file_exist(_Else) -> true.
% - in couch 1.x.x context
% - in bigcouch context
% - what is a server-user owned data/ dir we can use for this, that isn’t db_dir or index_dir or log or var/run or /tmp

2 changes: 1 addition & 1 deletion src/couchdb/couch_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ register(Fun, Pid) ->
init(IniFiles) ->
ets:new(?MODULE, [named_table, set, protected]),
try
lists:map(fun(IniFile) ->
lists:foreach(fun(IniFile) ->
{ok, ParsedIniValues} = parse_ini_file(IniFile),
ets:insert(?MODULE, ParsedIniValues)
end, IniFiles),
Expand Down
1 change: 1 addition & 0 deletions utils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ run: ../bin/couchdb.tpl
-e "s|%localstatelogdir%|$(abs_top_builddir)/tmp/log|g" \
-e "s|%localstatelibdir%|$(abs_top_builddir)/tmp/lib|g" \
-e "s|%localstatedir%|$(abs_top_builddir)/tmp|g" \
-e "s|%locallibdir%|$(abs_top_builddir)/tmp|g" \
-e "s|%bug_uri%|@bug_uri@|g" \
-e "s|%package_author_address%|@package_author_address@|g" \
-e "s|%package_author_name%|@package_author_name@|g" \
Expand Down

0 comments on commit 059935c

Please sign in to comment.