Skip to content

Loading…

functions to get/update app.config settings #15

Closed
wants to merge 4 commits into from

1 participant

@aschepis

I had posted a Gist to my blog with some erlang i wrote to get and update values in app.config in order to facilitate automated deployment of a Riak cluster. Mark Phillups suggested I contribute it here.

Let me know if there is anything I need to change in the script or overview file.

Right now the main limitation of the update_config_value function is that it only takes one value at a time and so you're doing a lot of extra work if you are updating many values. I intend to update it to take an array of tuples that contain all of the settings to update so that i can do one full sweep of the terms read in from the app.config file and process all the updates at once.

aschepis added some commits
@aschepis aschepis add app.config edit script
erlang script that can retrieve or update values in the Riak app.config
file.  Suitable for use in building automation tools.
4bc3a65
@aschepis aschepis GitHub for Mac: Throw-away commit. f6b5b2a
@aschepis aschepis Revert "GitHub for Mac: Throw-away commit."
This reverts commit f6b5b2ac96f14c032ffbe6d10a2f6006af131a23.
a197041
@aschepis aschepis add doc for app_config_edit functions 3dfb7f2
@aschepis aschepis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 27, 2011
  1. @aschepis

    add app.config edit script

    aschepis committed
    erlang script that can retrieve or update values in the Riak app.config
    file.  Suitable for use in building automation tools.
  2. @aschepis
  3. @aschepis

    Revert "GitHub for Mac: Throw-away commit."

    aschepis committed
    This reverts commit f6b5b2ac96f14c032ffbe6d10a2f6006af131a23.
  4. @aschepis
This page is out of date. Refresh to see the latest.
View
1 function-contrib-gollum/_Layout.html
@@ -115,6 +115,7 @@
<ul>
<li><a href="/bucket_exporter.html">Export Data (Erlang)</a></li>
<li><a href="/bucket_importer.html">Import Data (Erlang)</a></li>
+ <li><a href="/app_config_edit.html">Programatically Edit Riak app.config Files (Erlang)</a></li>
<li><a href="/yaml_importer.html">Import YAML (Ruby)</a></li>
<li><a href="/other-functions.html">More...</a></li>
</ul>
View
69 function-contrib-gollum/app_config_edit.textile
@@ -0,0 +1,69 @@
+h1. Programatically Editing app.config files
+
+[[Source File on GitHub|https://github.com/basho/riak_function_contrib/blob/master/other/erlang/app_config_edit.erl]]
+
+These Erlang functions allow you to read or edit values in the Riak app.config file.
+
+There are two exported functions:
+app_config_edit:get_config_value(app config, section, name)
+app_config_edit:update_config_value(app config, section, name, new value)
+
+```erlang
+-module(app_config_edit).
+-export([get_config_value/3, update_config_value/4, example/1]).
+
+example(FilePath) ->
+ {ok,[AppConfig]} = file:consult(FilePath),
+ [Http] = get_config_value(AppConfig, riak_core, http),
+ io:format("HTTP Address: ~p~n", [Http]),
+ Updated = update_config_value(AppConfig, riak_core, http, [{"0.0.0.0", 8080}]),
+ io:format("Original Values:~n~p~n", [AppConfig]),
+ io:format("Updated Values:~n~p~n", [Updated]).
+
+update_config_value([], _, _, _) ->
+ [];
+update_config_value([Section | Rest], SectionName, Name, NewValue) ->
+ case Section of
+ {SectionName, Values} ->
+ [{SectionName, update_config_value_in_section(Values, Name, NewValue)} | update_config_value(Rest, SectionName, Name, NewValue)];
+ Other ->
+ [Other | update_config_value(Rest, SectionName, Name, NewValue)]
+ end.
+
+update_config_value_in_section([], _, _) ->
+ [];
+update_config_value_in_section([Value | Rest], Name, NewValue) ->
+ case Value of
+ {Name, _V} ->
+ [{Name, NewValue} | update_config_value_in_section(Rest, Name, NewValue)];
+ Other ->
+ [Other | update_config_value_in_section(Rest, Name, NewValue)]
+ end.
+
+
+get_config_value([], _, _) ->
+ not_found;
+get_config_value([Section | Rest], SectionName, Name) ->
+ case Section of
+ {SectionName, Values} ->
+ get_config_value_in_section(Values, Name);
+ _ ->
+ get_config_value(Rest, SectionName, Name)
+ end.
+
+get_config_value_in_section([], _) ->
+ not_found;
+get_config_value_in_section([Value | Rest], Name) ->
+ case Value of
+ {Name, V} ->
+ V;
+ _ ->
+ get_config_value_in_section(Rest, Name)
+ end.
+```
+
+h3. Ideas for using these function
+
+These functions were originally written to help facilitate automated deployment of a Riak cluster. You could write a
+program that uses these functions to configure Riak appropriately for your environment and avoid errors that
+can occur by doing these tasks manually.
View
70 other/erlang/app_config_edit.erl
@@ -0,0 +1,70 @@
+%% -------------------------------------------------------------------
+%%
+%%
+%% 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.
+%%
+%% -------------------------------------------------------------------
+
+-module(app_config_edit).
+-export([get_config_value/3, update_config_value/4, example/1]).
+
+example(FilePath) ->
+ {ok,[AppConfig]} = file:consult(FilePath),
+ [Http] = get_config_value(AppConfig, riak_core, http),
+ io:format("HTTP Address: ~p~n", [Http]),
+ Updated = update_config_value(AppConfig, riak_core, http, [{"0.0.0.0", 8080}]),
+ io:format("Original Values:~n~p~n", [AppConfig]),
+ io:format("Updated Values:~n~p~n", [Updated]).
+
+update_config_value([], _, _, _) ->
+ [];
+update_config_value([Section | Rest], SectionName, Name, NewValue) ->
+ case Section of
+ {SectionName, Values} ->
+ [{SectionName, update_config_value_in_section(Values, Name, NewValue)} | update_config_value(Rest, SectionName, Name, NewValue)];
+ Other ->
+ [Other | update_config_value(Rest, SectionName, Name, NewValue)]
+ end.
+
+update_config_value_in_section([], _, _) ->
+ [];
+update_config_value_in_section([Value | Rest], Name, NewValue) ->
+ case Value of
+ {Name, _V} ->
+ [{Name, NewValue} | update_config_value_in_section(Rest, Name, NewValue)];
+ Other ->
+ [Other | update_config_value_in_section(Rest, Name, NewValue)]
+ end.
+
+
+get_config_value([], _, _) ->
+ not_found;
+get_config_value([Section | Rest], SectionName, Name) ->
+ case Section of
+ {SectionName, Values} ->
+ get_config_value_in_section(Values, Name);
+ _ ->
+ get_config_value(Rest, SectionName, Name)
+ end.
+
+get_config_value_in_section([], _) ->
+ not_found;
+get_config_value_in_section([Value | Rest], Name) ->
+ case Value of
+ {Name, V} ->
+ V;
+ _ ->
+ get_config_value_in_section(Rest, Name)
+ end.
Something went wrong with that request. Please try again.