Source

riak-old / riakctl

#!/usr/bin/env escript
%% -*- mode: erlang -*-

-export([main/1]).

usage() ->
    io:format("~s cmd options~n",
	      [filename:basename(escript:script_name())]),
    halt(1).

to_string(S) when is_atom(S) ->
    atom_to_list(S);
to_string(S) -> S.

get_from_config(hostname) ->
    case {riak:get_app_env(riak_nodename),
	  riak:get_app_env(riak_hostname)} of
	{Host, undefined} ->
	    ["-sname ", to_string(Host)];
	{Host, Node} ->
	    ["-name ", to_string(Host),
	     "@", to_string(Node)];
	_ ->
	    "-sname riak"
    end;
get_from_config(cookie) ->
    case riak:get_app_env(riak_cookie) of
	undefined ->
	    "";
	Cookie ->
	    ["-setcookie ", to_string(Cookie)]
    end;
get_from_config(sasl_logfile) ->
    case riak:get_app_env(sasl_logfile) of
	undefined ->
	    "";
	File ->
	    ["-sasl sasl_error_logger \\\{file,\\ \\\"", to_string(File), "\\\"\\\}"]
    end;
get_from_config(heart_command) ->
    case riak:get_app_env(riak_heart_command) of
	undefined ->
	    "";
	Cmd ->
	    to_string(Cmd)
    end.

init() ->
    error_logger:tty(false),
    code:add_patha(filename:join(filename:dirname(escript:script_name()),
				 "./ebin")).
init(Config) ->
    init(),
    riak_app:read_config(Config).    

event_loger(Node, NodeName, Cookie, FileName) ->
    init(),
    net_kernel:start(NodeName),
    case Cookie of
	undefined ->
	    ok;
	_ ->
	    erlang:set_cookie(node(), Cookie)
    end,
    case net_adm:ping(Node) of
	pang ->
	    io:format("Node ~s not avalible~n", [atom_to_list(Node)]);
	pong ->
	    riak_event_logger:start(Node, FileName),
	    receive
		_ -> ok
	    end
    end.
    

main(["start-fresh", Config]) ->
    init(Config),
    os:putenv("HEART_COMMAND", get_from_config(heart_command)),
    os:cmd(["erl +A 32 -heart -detached -connect_all false -pa deps/*/ebin -pa ebin ",
	    get_from_config(hostname), " ",
	    get_from_config(cookie), " ",
	    get_from_config(sasl_logfile), " -run riak start ",
	    Config]);
main(["start-join", Config, Node])->
    init(Config),
    os:putenv("HEART_COMMAND", get_from_config(heart_command)),
    os:cmd(["erl +A 32 -heart -detached -connect_all false -pa deps/*/ebin -pa ebin ",
	    get_from_config(hostname), " ",
	    get_from_config(cookie), " ",
	    get_from_config(sasl_logfile), " -run riak start ",
	    Config, " -run riak_startup join_cluster ", Node]);
main(["start-restart", Config])->
    init(Config),
    os:putenv("HEART_COMMAND", get_from_config(heart_command)),
    os:cmd(["erl +A 32 -heart -detached -connect_all false -pa deps/*/ebin -pa ebin ",
	    get_from_config(hostname), " ",
	    get_from_config(cookie), " ",
	    get_from_config(sasl_logfile), " -run riak start ",
	    Config, " -run riak_startup rejoin"]);
main(["start-loger", "-sname", NodeName, Node, Cookie, Filename]) ->
    event_loger(list_to_atom(Node), [list_to_atom(NodeName), shortnames],
		list_to_atom(Cookie), Filename);
main(["start-loger", "-name", NodeName, Node, Cookie, Filename]) ->
    event_loger(list_to_atom(Node), [list_to_atom(NodeName)],
		list_to_atom(Cookie), Filename);

main(["start-loger", "-sname", NodeName, Node, Cookie]) ->
    main(["start-loger", "-sname", NodeName, Node, Cookie, ""]);
main(["start-loger", "-name", NodeName, Node, Cookie]) ->
    main(["start-loger", "-name", NodeName, Node, Cookie, ""]);

main(["start-loger", "-sname", NodeName, Node]) ->
    main(["start-loger", "-sname", NodeName, Node, "undefined"]);
main(["start-loger", "-name", NodeName, Node]) ->
    main(["start-loger", "-name", NodeName, Node, "undefined"]);

main(["start-loger", Node, Cookie, Filename]) ->
    main(["start-loger", "-name", "riak-logger", Node, Cookie, Filename]);

main(["start-loger", Node, Cookie]) ->
    main(["start-loger", Node, Cookie, ""]);

main(["start-loger", Node]) ->
    main(["start-loger", Node, "nocookie"]);

main(_)->
    usage().
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.