Skip to content

Instantly share code, notes, and snippets.

@kocolosk
Created April 15, 2009 03:26
Show Gist options
  • Save kocolosk/95584 to your computer and use it in GitHub Desktop.
Save kocolosk/95584 to your computer and use it in GitHub Desktop.
diff --git a/bin/couchdb.tpl.in b/bin/couchdb.tpl.in
index 502d6b3..8d93b06 100644
--- a/bin/couchdb.tpl.in
+++ b/bin/couchdb.tpl.in
@@ -185,17 +185,10 @@ start_couchdb () {
start_arguments="$start_arguments \\\"$file\\\""
background_start_arguments="$background_start_arguments -c \\\"$file\\\""
done
- command="`%ICU_CONFIG% --invoke` \
- %ERL% $interactive_option -smp auto -sasl errlog_type error +K true \
- -pa %localerlanglibdir%/%couchdbebindir% \
- %localerlanglibdir%/%mochiwebebindir% \
- %localerlanglibdir%/%ibrowseebindir% \
- -eval \"application:load(ibrowse)\" \
- -eval \"application:load(crypto)\" \
- -eval \"application:load(couch)\" \
- -eval \"crypto:start()\" \
- -eval \"ibrowse:start()\" \
- -eval \"couch_server:start([$start_arguments]), receive done -> done end.\" "
+ command="`%ICU_CONFIG% --invoke` %ERL% $interactive_option -smp auto \
+ -sasl errlog_type error +K true -sname couchdb \
+ -env ERL_LIBS $ERL_LIBS:%localerlanglibdir% \
+ -couch_ini $ini_files -s couch_app"
if test "$BACKGROUND" = "true" \
-a "$RECURSED" = "false"; then
$0 $background_start_arguments -b -r $RESPAWN_TIMEOUT -p $PID_FILE \
diff --git a/src/couchdb/Makefile.am b/src/couchdb/Makefile.am
index 8b78222..250f113 100644
--- a/src/couchdb/Makefile.am
+++ b/src/couchdb/Makefile.am
@@ -40,6 +40,7 @@ CLEANFILES = $(compiled_files) $(doc_base)
# CLEANFILES = $(doc_modules) edoc-info
source_files = \
+ couch_app.erl \
couch_btree.erl \
couch_config.erl \
couch_config_writer.erl \
@@ -82,6 +83,7 @@ EXTRA_DIST = $(source_files) couch_db.hrl couch_stats.hrl
compiled_files = \
couch.app \
+ couch_app.beam \
couch_btree.beam \
couch_config.beam \
couch_config_writer.beam \
@@ -152,7 +154,10 @@ compiled_files = \
couch.app: couch.app.tpl
sed -e "s|%package_name%|@package_name@|g" \
- -e "s|%version%|@version@|g" > \
+ -e "s|%version%|@version@|g" \
+ -e "s|%localconfdir%|@localconfdir@|g" \
+ -e "s|@defaultini@|default.ini|g" \
+ -e "s|@localini@|local.ini|g" > \
$@ < $<
chmod +x $@
diff --git a/src/couchdb/couch.app.tpl.in b/src/couchdb/couch.app.tpl.in
index e0100cb..bfb5e3e 100644
--- a/src/couchdb/couch.app.tpl.in
+++ b/src/couchdb/couch.app.tpl.in
@@ -1,27 +1,72 @@
-{application,couch,
- [{description,"@package_name@"},
- {vsn,"@version@"},
- {modules,[couch_btree,
- couch_db,
- couch_db_updater,
- couch_doc,
- couch_query_servers,
- couch_file,
- couch_server,
- couch_server_sup,
- couch_stream,
- couch_key_tree,
- couch_view,
- couch_util,
- couch_httpd,
- couch_event_sup,
- couch_db_update_notifier,
- couch_db_update_notifier_sup,
- couch_log,
- couch_rep]},
- {registered,[couch_server,
- couch_server_sup,
- couch_view,
- couch_query_servers,
- couch_db_update_notifier_sup]},
- {applications,[kernel,stdlib,crypto,ibrowse,mochiweb]}]}.
+{application,couch, [
+ {description, "@package_name@"},
+ {vsn, "@version@"},
+ {modules,[
+ couch_app,
+ couch_btree,
+ couch_config,
+ couch_config_writer,
+ couch_db,
+ couch_db_update_notifier,
+ couch_db_update_notifier_sup,
+ couch_db_updater,
+ couch_doc,
+ couch_event_sup,
+ couch_external_manager,
+ couch_external_server,
+ couch_file,
+ couch_httpd,
+ couch_httpd_db,
+ couch_httpd_external,
+ couch_httpd_misc_handlers,
+ couch_httpd_show,
+ couch_httpd_stats_handlers,
+ couch_httpd_view,
+ couch_key_tree,
+ couch_log,
+ couch_os_process,
+ couch_query_servers,
+ couch_ref_counter,
+ couch_rep,
+ couch_rep_sup,
+ couch_server,
+ couch_server_sup,
+ couch_stats_aggregator,
+ couch_stats_collector,
+ couch_stream,
+ couch_task_status,
+ couch_util,
+ couch_view,
+ couch_view_compactor,
+ couch_view_group,
+ couch_view_updater
+ ]},
+ {registered,[
+ couch_db_update,
+ couch_config,
+ couch_task_status,
+ couch_rep_sup,
+ couch_secondary_services,
+ couch_primary_services,
+ couch_log,
+ couch_httpd,
+ couch_external_manager,
+ couch_view,
+ couch_stats_aggregator,
+ couch_server,
+ couch_server_sup,
+ couch_query_servers,
+ couch_db_update_notifier_sup
+ ]},
+ {mod, {couch_app, [
+ "%localconfdir%/@defaultini@",
+ "%localconfdir%/@localini@"
+ ]}},
+ {applications,[
+ kernel,
+ stdlib,
+ crypto,
+ ibrowse,
+ mochiweb
+ ]}
+]}.
diff --git a/src/couchdb/couch_app.erl b/src/couchdb/couch_app.erl
new file mode 100644
index 0000000..79115ee
--- /dev/null
+++ b/src/couchdb/couch_app.erl
@@ -0,0 +1,52 @@
+-module(couch_app).
+-behaviour(application).
+-export([start/0, start/2, stop/1]).
+
+%% @doc Convenience function for starting couch with -s couch_app from the
+%% command-line. It never returns.
+start() ->
+ IniFiles = get_ini_files(),
+ ok = ensure_started(crypto),
+ ok = ensure_started(sasl),
+ ok = ensure_started(ibrowse),
+ ok = ensure_started(mochiweb),
+ application:load(couch),
+ start(temporary, IniFiles),
+ receive done -> "this will never happen" end.
+
+%% @spec start(Type, StartArgs) -> {ok, pid()} | {ok, pid(), State} |
+%% {error, Reason}
+%% where
+%% Type = normal | {takeover, Node} | {failover, Node}
+%% StartArgs = [IniFile::string()]
+start(_Type, StartArgs) ->
+ couch_server:start(StartArgs).
+
+%% @spec stop(State) -> ok
+stop(_State) ->
+ couch_server:stop().
+
+%% internal functions
+
+%% @spec ensure_started(atom()) -> ok | Error
+%% @doc make sure an OTP application is started
+ensure_started(App) ->
+ case application:start(App) of
+ ok ->
+ ok;
+ {error, {already_started, App}} ->
+ ok;
+ Error ->
+ Error
+ end.
+
+
+%% @spec get_ini_files() -> [IniFile::string()]
+%% @doc Grab the -couch_ini value from the command-line, return list of strings
+get_ini_files() ->
+ case init:get_argument(couch_ini) of
+ {ok, [Values]} ->
+ Values;
+ _ ->
+ exit(couch_ini_missing)
+ end.
diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl
index 870d0d7..198695b 100644
--- a/src/couchdb/couch_server.erl
+++ b/src/couchdb/couch_server.erl
@@ -12,9 +12,8 @@
-module(couch_server).
-behaviour(gen_server).
--behaviour(application).
--export([start/0,start/1,start/2,stop/0,stop/1,restart/0]).
+-export([start/0,start/1,stop/0,stop/1,restart/0]).
-export([open/2,create/2,delete/2,all_databases/0,get_version/0]).
-export([init/1, handle_call/3,sup_start_link/0]).
-export([handle_cast/2,code_change/3,handle_info/2,terminate/2]).
@@ -36,9 +35,6 @@ start() ->
start(IniFiles) ->
couch_server_sup:start_link(IniFiles).
-start(_Type, _Args) ->
- start().
-
restart() ->
stop(),
start().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment