Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add support for new ensure_dbs_exist option to GET, POST/finish_cluster
Addresses apache/couchdb:593
  • Loading branch information
wohali committed Jul 10, 2017
1 parent c38d7aa commit 18314a6daa56a13f4b4e6f71ce61224a8940f925
Showing 3 changed files with 23 additions and 12 deletions.
@@ -1,2 +1,4 @@
ebin
.rebar
*~
*.swp
@@ -12,8 +12,8 @@

-module(setup).

-export([enable_cluster/1, finish_cluster/0, add_node/1, receive_cookie/1]).
-export([is_cluster_enabled/0, has_cluster_system_dbs/0]).
-export([enable_cluster/1, finish_cluster/1, add_node/1, receive_cookie/1]).
-export([is_cluster_enabled/0, has_cluster_system_dbs/1, cluster_system_dbs/0]).

-include_lib("../couch/include/couch_db.hrl").

@@ -54,9 +54,6 @@ cluster_system_dbs() ->
["_users", "_replicator", "_global_changes"].


has_cluster_system_dbs() ->
has_cluster_system_dbs(cluster_system_dbs()).

has_cluster_system_dbs([]) ->
ok;
has_cluster_system_dbs([Db|Dbs]) ->
@@ -172,12 +169,19 @@ set_admin(Username, Password) ->
config:set("admins", binary_to_list(Username), binary_to_list(Password)).


finish_cluster() ->
finish_cluster_int(has_cluster_system_dbs()).
finish_cluster_int(ok) ->
finish_cluster(Options) ->
Dbs = proplists:get_value(ensure_dbs_exist, Options),
case Dbs of
undefined ->
finish_cluster_int(cluster_system_dbs(), has_cluster_system_dbs(cluster_system_dbs()));
Dbs ->
finish_cluster_int(Dbs, has_cluster_system_dbs(Dbs))
end.

finish_cluster_int(_Dbs, ok) ->
{error, cluster_finished};
finish_cluster_int(no) ->
lists:foreach(fun fabric:create_db/1, cluster_system_dbs()).
finish_cluster_int(Dbs, no) ->
lists:foreach(fun fabric:create_db/1, Dbs).


add_node(Options) ->
@@ -29,11 +29,12 @@ handle_setup_req(#httpd{method='POST'}=Req) ->
end;
handle_setup_req(#httpd{method='GET'}=Req) ->
ok = chttpd:verify_is_server_admin(Req),
Dbs = chttpd:qs_json_value(Req, "ensure_dbs_exist", setup:cluster_system_dbs()),
case setup:is_cluster_enabled() of
no ->
chttpd:send_json(Req, 200, {[{state, cluster_disabled}]});
ok ->
case setup:has_cluster_system_dbs() of
case setup:has_cluster_system_dbs(Dbs) of
no ->
chttpd:send_json(Req, 200, {[{state, cluster_enabled}]});
ok ->
@@ -74,7 +75,11 @@ handle_action("enable_cluster", Setup) ->

handle_action("finish_cluster", Setup) ->
couch_log:notice("finish_cluster: ~p~n", [Setup]),
case setup:finish_cluster() of

Options = get_options([
{ensure_dbs_exist, <<"ensure_dbs_exist">>}
], Setup),
case setup:finish_cluster(Options) of
{error, cluster_finished} ->
{error, <<"Cluster is already finished">>};
Else ->

0 comments on commit 18314a6

Please sign in to comment.