<commit_before>-module(admin_cluster).
-export([routes/0,
         init/1,
         content_types_provided/2,
         to_json/2,
         is_authorized/2,
         service_available/2
        ]).

%% webmachine dependencies
-include_lib("webmachine/include/webmachine.hrl").

%% mappings to the various content types supported for this resource
-define(CONTENT_TYPES,[{"application/json",to_json}]).

%% defines the webmachine routes this module handles
routes () ->
    [{admin_routes:cluster_route(["list"]),?MODULE,list},
     {admin_routes:cluster_route(["join",node]),?MODULE,join},
     {admin_routes:cluster_route(["down",node]),?MODULE,down}].

%% entry-point for the resource from webmachine
init (Action) ->
    {ok,Action}.

%% redirect to SSL port if using HTTP
service_available (RD,C) ->
    riak_control_security:scheme_is_available(RD,C).

%% validate username and password
is_authorized (RD,C) ->
    riak_control_security:enforce_auth(RD,C).

%% return the list of available content types for webmachine
content_types_provided (Req,C) ->
    {?CONTENT_TYPES,Req,C}.

%% all actions return the same format
cluster_action_result (Error={error,_},Req,C) ->
    {{error,mochijson2:encode({struct,[Error]})},Req,C};
cluster_action_result (Error={badrpc,_},Req,C) ->
    {{error,mochijson2:encode({struct,[Error]})},Req,C};
cluster_action_result (_,Req,C) ->
    {mochijson2:encode({struct,[{result,ok}]}),Req,C}.

%% get a list of all the nodes in the ring and their status
to_json (Req,C=list) ->
    {ok,_V,Nodes}=riak_control_session:get_nodes(),
    Status=[{struct,[{"name",N},
                     {"status",S},
                     {"reachable",Online}
                    ]}
            || {N,S,Online,_} <- Nodes],
    {mochijson2:encode(Status),Req,C};

%% join this node to the cluster of another ring
to_json (Req,C=join) ->
    {ok,Ring}=riak_core_ring_manager:get_my_ring(),
    NodeStr=dict:fetch(node,wrq:path_info(Req)),
    Node=list_to_atom(NodeStr),

    %% if we're a member of a single-node cluster (us) then we're
    %% going to join the other node's ring, otherwise we'll make
    %% the target node join our ring.
    case riak_core_ring:all_members(Ring) of
        [_Me] ->
            %% we're a single-node cluster, join the other guy...
            Result=riak_core:join(Node),
            cluster_action_result(Result,Req,C);
        _ ->
            %% we have a cluster, make them join us
            Result=rpc:call(Node,riak_core,join,[node()]),
            cluster_action_result(Result,Req,C)
    end;

%% mark a node in the cluster as down
to_json (Req,C=down) ->
    NodeStr=dict:fetch(node,wrq:path_info(Req)),
    Node=list_to_existing_atom(NodeStr),
    Result=riak_core:down(Node),
    cluster_action_result(Result,Req,C).

<commit_msg>The node list knows which node you are connected to via the UI.<commit_after>-module(admin_cluster).
-export([routes/0,
         init/1,
         content_types_provided/2,
         to_json/2,
         is_authorized/2,
         service_available/2
        ]).

%% webmachine dependencies
-include_lib("webmachine/include/webmachine.hrl").

%% mappings to the various content types supported for this resource
-define(CONTENT_TYPES,[{"application/json",to_json}]).

%% defines the webmachine routes this module handles
routes () ->
    [{admin_routes:cluster_route(["list"]),?MODULE,list},
     {admin_routes:cluster_route(["join",node]),?MODULE,join},
     {admin_routes:cluster_route(["down",node]),?MODULE,down}].

%% entry-point for the resource from webmachine
init (Action) ->
    {ok,Action}.

%% redirect to SSL port if using HTTP
service_available (RD,C) ->
    riak_control_security:scheme_is_available(RD,C).

%% validate username and password
is_authorized (RD,C) ->
    riak_control_security:enforce_auth(RD,C).

%% return the list of available content types for webmachine
content_types_provided (Req,C) ->
    {?CONTENT_TYPES,Req,C}.

%% all actions return the same format
cluster_action_result (Error={error,_},Req,C) ->
    {{error,mochijson2:encode({struct,[Error]})},Req,C};
cluster_action_result (Error={badrpc,_},Req,C) ->
    {{error,mochijson2:encode({struct,[Error]})},Req,C};
cluster_action_result (_,Req,C) ->
    {mochijson2:encode({struct,[{result,ok}]}),Req,C}.

%% get a list of all the nodes in the ring and their status
to_json (Req,C=list) ->
    {ok,_V,Nodes}=riak_control_session:get_nodes(),
    Status=[{struct,[{"name",N},
                     {"status",S},
                     {"reachable",Online},
                     {"me",N == node()}
                    ]}
            || {N,S,Online,_} <- Nodes],
    {mochijson2:encode(Status),Req,C};

%% join this node to the cluster of another ring
to_json (Req,C=join) ->
    {ok,Ring}=riak_core_ring_manager:get_my_ring(),
    NodeStr=dict:fetch(node,wrq:path_info(Req)),
    Node=list_to_atom(NodeStr),

    %% if we're a member of a single-node cluster (us) then we're
    %% going to join the other node's ring, otherwise we'll make
    %% the target node join our ring.
    case riak_core_ring:all_members(Ring) of
        [_Me] ->
            %% we're a single-node cluster, join the other guy...
            Result=riak_core:join(Node),
            cluster_action_result(Result,Req,C);
        _ ->
            %% we have a cluster, make them join us
            Result=rpc:call(Node,riak_core,join,[node()]),
            cluster_action_result(Result,Req,C)
    end;

%% mark a node in the cluster as down
to_json (Req,C=down) ->
    NodeStr=dict:fetch(node,wrq:path_info(Req)),
    Node=list_to_existing_atom(NodeStr),
    Result=riak_core:down(Node),
    cluster_action_result(Result,Req,C).


{'fraction_non_alphanumeric': 0.14461863660384006, 'fraction_numerical': 0.0035229874933943982, 'mean_word_length': 3.6012965964343597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899015', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1847, 'n_words': 388}