Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
address comments from rnewson
  • Loading branch information
wohali committed Jul 11, 2017
1 parent 92dd9d1 commit e153d485b6b195054c87bbfcca1c2a5f54d979eb
Showing 2 changed files with 40 additions and 52 deletions.
@@ -45,31 +45,31 @@ is_cluster_enabled() ->
BindAddress = config:get("chttpd", "bind_address"),
Admins = config:get("admins"),
case {BindAddress, Admins} of
{"127.0.0.1", _} -> no;
{_,[]} -> no;
{_,_} -> ok
{"127.0.0.1", _} -> false;
{_,[]} -> false;
{_,_} -> true
end.

is_single_node_enabled(Dbs) ->
% admins != empty AND dbs exist
Admins = config:get("admins"),
HasDbs = has_cluster_system_dbs(Dbs),
case {Admins, HasDbs} of
{[], _} -> no;
{_, no} -> no;
{_,_} -> ok
{[], _} -> false;
{_, false} -> false;
{_,_} -> true
end.

cluster_system_dbs() ->
["_users", "_replicator", "_global_changes"].


has_cluster_system_dbs([]) ->
ok;
true;
has_cluster_system_dbs([Db|Dbs]) ->
case catch fabric:get_db_info(Db) of
{ok, _} -> has_cluster_system_dbs(Dbs);
_ -> no
_ -> false
end.

enable_cluster(Options) ->
@@ -126,9 +126,9 @@ enable_cluster_http(Options) ->
{error, Else}
end.

enable_cluster_int(_Options, ok) ->
enable_cluster_int(_Options, true) ->
{error, cluster_enabled};
enable_cluster_int(Options, no) ->
enable_cluster_int(Options, false) ->

% if no admin in config and no admin in req -> error
CurrentAdmins = config:get("admins"),
@@ -140,26 +140,26 @@ enable_cluster_int(Options, no) ->
end
},
ok = require_admins(CurrentAdmins, NewCredentials),

% if bind_address == 127.0.0.1 and no bind_address in req -> error
CurrentBindAddress = config:get("chttpd","bind_address"),
NewBindAddress = proplists:get_value(bind_address, Options),
ok = require_bind_address(CurrentBindAddress, NewBindAddress),

NodeCount = couch_util:get_value(node_count, Options),
ok = require_node_count(NodeCount),

Port = proplists:get_value(port, Options),

setup_node(NewCredentials, NewBindAddress, NodeCount, Port),
couch_log:notice("Enable Cluster: ~p~n", [Options]).

set_admin(Username, Password) ->
config:set("admins", binary_to_list(Username), binary_to_list(Password)).

setup_node(NewCredentials, NewBindAddress, NodeCount, Port) ->
case NewCredentials of
{undefined, undefined} ->
ok;
{Username, Password} ->
config:set("admins", binary_to_list(Username), binary_to_list(Password))
set_admin(Username, Password)
end,

case NewBindAddress of
@@ -182,17 +182,12 @@ setup_node(NewCredentials, NewBindAddress, NodeCount, Port) ->


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.
Dbs = proplists:get_value(ensure_dbs_exist, Options, cluster_system_dbs()),
finish_cluster_int(Dbs, has_cluster_system_dbs(Dbs)).

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


@@ -207,29 +202,22 @@ enable_single_node(Options) ->
end
},
ok = require_admins(CurrentAdmins, NewCredentials),

% skip bind_address validation, anything is fine
NewBindAddress = proplists:get_value(bind_address, Options),

Port = proplists:get_value(port, Options),

setup_node(NewCredentials, NewBindAddress, 1, Port),
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,
Dbs = proplists:get_value(ensure_dbs_exist, Options, cluster_system_dbs()),
finish_cluster_int(Dbs, has_cluster_system_dbs(Dbs))
couch_log:notice("Enable Single Node: ~p~n", [Options]).


add_node(Options) ->
add_node_int(Options, is_cluster_enabled()).

add_node_int(_Options, no) ->
add_node_int(_Options, false) ->
{error, cluster_not_enabled};
add_node_int(Options, ok) ->
add_node_int(Options, true) ->
couch_log:notice("add node_int: ~p~n", [Options]),
ErlangCookie = erlang:get_cookie(),

@@ -32,25 +32,25 @@ handle_setup_req(#httpd{method='GET'}=Req) ->
Dbs = chttpd:qs_json_value(Req, "ensure_dbs_exist", setup:cluster_system_dbs()),
couch_log:notice("Dbs: ~p~n", [Dbs]),
case erlang:list_to_integer(config:get("cluster", "n", undefined)) of
1 ->
case setup:is_single_node_enabled(Dbs) of
no ->
chttpd:send_json(Req, 200, {[{state, single_node_disabled}]});
ok ->
chttpd:send_json(Req, 200, {[{state, single_node_enabled}]})
end;
_ ->
case setup:is_cluster_enabled() of
no ->
chttpd:send_json(Req, 200, {[{state, cluster_disabled}]});
ok ->
case setup:has_cluster_system_dbs(Dbs) of
no ->
chttpd:send_json(Req, 200, {[{state, cluster_enabled}]});
ok ->
chttpd:send_json(Req, 200, {[{state, cluster_finished}]})
1 ->
case setup:is_single_node_enabled(Dbs) of
false ->
chttpd:send_json(Req, 200, {[{state, single_node_disabled}]});
true ->
chttpd:send_json(Req, 200, {[{state, single_node_enabled}]})
end;
_ ->
case setup:is_cluster_enabled() of
false ->
chttpd:send_json(Req, 200, {[{state, cluster_disabled}]});
true ->
case setup:has_cluster_system_dbs(Dbs) of
false ->
chttpd:send_json(Req, 200, {[{state, cluster_enabled}]});
true ->
chttpd:send_json(Req, 200, {[{state, cluster_finished}]})
end
end
end
end;
handle_setup_req(#httpd{}=Req) ->
chttpd:send_method_not_allowed(Req, "GET,POST").

0 comments on commit e153d48

Please sign in to comment.