-
Notifications
You must be signed in to change notification settings - Fork 461
/
whistle_apps_cli.erl
98 lines (88 loc) · 3.61 KB
/
whistle_apps_cli.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
%%%-------------------------------------------------------------------
%%% @author James Aimonetti <james@2600hz.org>
%%% @copyright (C) 2010, James Aimonetti
%%% @doc
%%% CLI interface to Whistle Apps (whapps) Container
%%% @end
%%% Created : 10 Jan 2011 by James Aimonetti <james@2600hz.org>
%%%-------------------------------------------------------------------
-module(whistle_apps_cli).
-export([help/2, set_amqp_host/2, set_couch_host/2, start_app/2, stop_app/2, running_apps/2]).
-include_lib("../../lib/erlctl/lib/erlctl-0.3/include/erlctl.hrl").
help(always,_) ->
lists:foreach(
fun({E,D}) ->
format(E ++ "~n", D);
(E) ->
format(E ++ "~n", [])
end, usage()),
ok.
usage() ->
Opts = erlctl:get_opts(),
App = proplists:get_value(app,Opts),
Script = proplists:get_value(script,Opts),
[
{"Usage for ~s:",[App]}
,{" ~s <command> ...",[Script]}
,""
,"Commands:"
,{" set_amqp_host <host> Set the amqp host (e.g. ~p)", [net_adm:localhost()]}
,{" set_couch_host <host> Set the amqp host (e.g. ~p)", [net_adm:localhost()]}
," start_app <whapp> Start (if not already) the whapp"
," stop_app <whapp> Stop (if started) the whapp"
," running_apps List of whapps currently running"
].
set_amqp_host(always, [Host]=Arg) ->
Node = list_to_atom(lists:flatten(["whistle_apps@", net_adm:localhost()])),
format("Setting AMQP host to ~p on ~p~n", [Host, Node]),
case rpc_call(Node, whapps_controller, set_amqp_host, Arg) of
{ok, ok} ->
{ok, "Set whistle controller's amqp host to ~p", [Host]};
{ok, Other} ->
{ok, "Something unexpected happened while setting the amqp host: ~p", [Other]}
end.
set_couch_host(always, [Host]) ->
Node = list_to_atom(lists:flatten(["whistle_apps@", net_adm:localhost()])),
format("Setting CouchDB Host to ~p on ~p~n", [Host, Node]),
User = io:get_line("CouchDB Username: "),
Pass = io:get_line("CouchDB Password: "),
case rpc_call(Node, whapps_controller, set_couch_host, [Host, string:strip(User, right, $\n), string:strip(Pass, right, $\n)]) of
{ok, ok} ->
{ok, "Set whistle controller's couch host to ~p", [Host]};
{ok, Other} ->
{ok, "Something unexpected happened while setting the couch host: ~p", [Other]}
end.
start_app(always, [Whapp]) ->
Node = list_to_atom(lists:flatten(["whistle_apps@", net_adm:localhost()])),
format("Starting whapp ~p on ~p~n", [Whapp, Node]),
case rpc_call(Node, whapps_controller, start_app, [list_to_atom(Whapp)]) of
{ok, ok} ->
{ok, "~p started successfully", [Whapp]};
{ok, Other} ->
{ok, "Something unexpected happened while starting ~p: ~p", [Whapp, Other]}
end.
stop_app(always, [Whapp]) ->
Node = list_to_atom(lists:flatten(["whistle_apps@", net_adm:localhost()])),
format("Stopping whapp ~p on ~p~n", [Whapp, Node]),
case rpc_call(Node, whapps_controller, stop_app, [list_to_atom(Whapp)]) of
{ok, ok} ->
{ok, "~p stopped successfully", [Whapp]};
{ok, Other} ->
{ok, "Something unexpected happened while stopping ~p: ~p", [Whapp, Other]}
end.
running_apps(always, []) ->
Node = list_to_atom(lists:flatten(["whistle_apps@", net_adm:localhost()])),
format("Searching for running whapps on ~p~n", [Node]),
case rpc_call(Node, whapps_controller, running_apps, []) of
{ok, Apps} when is_list(Apps) ->
{ok, "~p~n", [Apps]};
{ok, Other} ->
{ok, "Something unexpected happened while looking for running whapps: ~p", [Other]}
end.
rpc_call(Node, M, F, A) ->
case net_adm:ping(Node) of
pong ->
{ok, rpc:call(Node, M, F, A)};
pang ->
{ok, "~p not reachable", [Node]}
end.