2 %% Copyright (C) 2006 Damien Katz
4 %% This program is free software; you can redistribute it and/or
5 %% modify it under the terms of the GNU General Public License
6 %% as published by the Free Software Foundation; either version 2
7 %% of the License, or (at your option) any later version.
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program; if not, write to the Free Software
16 %% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -module(couch_server_sup
).
19 -behaviour(supervisor
).
21 -define(DEFAULT_INI
, "couch.ini").
23 -export([start_link
/1,stop
/0]).
25 %% supervisor callbacks
28 start_link(IniFilename
) ->
29 case whereis(couch_server_sup
) of
31 start_server(IniFilename
);
33 {error
, already_started
}
36 start_server(IniFilename
) ->
40 % no ini file specified, check the command line args
41 case init:get_argument(couch
) of
43 case lists:keysearch("ini", 1, Args
) of
44 {value
, {"ini", Filename
}} ->
45 file:read_file(Filename
);
53 % if IniFilename is specified, it is used
54 file:read_file(IniFilename
)
56 {ok
, Ini
} = couch_util:parse_ini(binary_to_list(IniBin
)),
57 ConsoleStartupMsg
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupMsg", "CouchDb server starting")),
58 ConsoleStartupCompleteMsg
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupCompleteMsg", "CouchDb server started")),
59 LogLevel
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "LogLevel", "error")),
60 DbRootDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "DbRootDir", ".")),
61 HttpConfigFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "HttpConfigFile", "couch_httpd.conf")),
62 FabricServer
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "FabricServer", "FabricServer")),
63 LuceneServer
= couch_util:ini_get_value(Ini
, "Couch", "LuceneServer", ""),
64 LogFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "LogFile", "couchdb.log")),
65 UtilDriverDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "UtilDriverDir", ".")),
68 io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel
]),
70 io:format("~s~n", [ConsoleStartupMsg
]),
72 % start the couch logger
73 ok
= couch_log:start(LogFile
, LogLevel
),
75 couch_log:debug("Startup Info:~n\tDbRootDir=~p~n\tHttpConfigFile=~p~n\tFabricServer=~p~n\tLogFile=~p~n\tUtilDriverDir=~p",
76 [DbRootDir
, HttpConfigFile
, FabricServer
, LogFile
, UtilDriverDir
]),
78 StartResult
= supervisor:start_link({local
, couch_server_sup
}, couch_server_sup
, {DbRootDir
, FabricServer
, HttpConfigFile
, UtilDriverDir
, LuceneServer
}),
82 % only output when startup was successful
83 io:format("~s~n", [ConsoleStartupCompleteMsg
]);
89 catch exit(whereis(couch_server_sup
), normal
),
93 case file:read_file(?DEFAULT_INI
) of
97 {ok
, <<>>} % default ini missing, just return a blank
100 init({DbServerRoot
, FabricServer
, HttpConfigFile
, UtilDriverDir
, LuceneServer
}) ->
101 case string:equal(LuceneServer
, "") of
103 {ok
, {{one_for_one
, 10, 3600},
105 {couch_server
, sup_start_link
, [DbServerRoot
]},
111 {couch_util
, start_link
, [UtilDriverDir
]},
117 {couch_fabric
, start_link
, [FabricServer
]},
123 {httpd
, start_link
, [HttpConfigFile
]},
131 {ok
, {{one_for_one
, 10, 3600},
133 {couch_server
, sup_start_link
, [DbServerRoot
]},
139 {couch_util
, start_link
, [UtilDriverDir
]},
145 {couch_fabric
, start_link
, [FabricServer
]},
151 {couch_lucene
, start_link
, [LuceneServer
]},
157 {httpd
, start_link
, [HttpConfigFile
]},