summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-01-30 16:15:54 +0100
committerGuido Günther <agx@sigxcpu.org>2016-01-30 23:29:55 +0100
commite2398689f525794a28e6ef57eb906c56763bfff1 (patch)
treead7dae09091f53f6e9db0f37b1da9062a395103b
parent4500b9a982f0e0803318d604a993ded16820dcd3 (diff)
Support multiple HV connections
-rw-r--r--include/simplec.hrl2
-rw-r--r--src/simplec.app.src3
-rw-r--r--src/simplec_app.erl4
-rw-r--r--src/simplec_hostsfile.erl20
-rw-r--r--src/simplec_sup.erl12
-rw-r--r--src/simplec_vms.erl12
6 files changed, 31 insertions, 22 deletions
diff --git a/include/simplec.hrl b/include/simplec.hrl
index 1a21df1..605f45c 100644
--- a/include/simplec.hrl
+++ b/include/simplec.hrl
@@ -1 +1 @@
--record(config, {url, dir}). \ No newline at end of file
+-record(config, {uris, dir}).
diff --git a/src/simplec.app.src b/src/simplec.app.src
index 943f1e9..90ddf5d 100644
--- a/src/simplec.app.src
+++ b/src/simplec.app.src
@@ -10,7 +10,8 @@
stdlib
]},
{mod, { simplec_app, []}},
- {env, [{uri, "qemu:///system"},
+ {env, [{uris, ["qemu:///system",
+ "lxc:///"]},
{dir, "priv"}
]}
]}.
diff --git a/src/simplec_app.erl b/src/simplec_app.erl
index 5936dd0..2e52081 100644
--- a/src/simplec_app.erl
+++ b/src/simplec_app.erl
@@ -12,9 +12,9 @@
%% ===================================================================
start(_StartType, _StartArgs) ->
- {ok, Uri} = application:get_env(simplec, uri),
+ {ok, Uris} = application:get_env(simplec, uris),
{ok, Dir} = application:get_env(simplec, dir),
- C = #config{url=Uri, dir=Dir},
+ C = #config{uris=Uris, dir=Dir},
simplec_sup:start_link(C).
stop(_State) ->
diff --git a/src/simplec_hostsfile.erl b/src/simplec_hostsfile.erl
index fba64af..0257155 100644
--- a/src/simplec_hostsfile.erl
+++ b/src/simplec_hostsfile.erl
@@ -14,7 +14,7 @@
%% API
-export([start_link/1,
- write/1]).
+ write/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -44,8 +44,8 @@ start_link(Config) ->
%% Writes out the hosts
%% @end
%%--------------------------------------------------------------------
-write(Hosts) ->
- gen_server:cast(simplec_hostsfile, {hosts, Hosts}).
+write(Hosts, Url) ->
+ gen_server:cast(simplec_hostsfile, [{hosts, Hosts}, {url, Url}]).
%%%===================================================================
@@ -63,10 +63,10 @@ write(Hosts) ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-init(Config) ->
+init(Config=#config{uris=Uris}) ->
State = #state{config=Config},
- % Make sure the file exists
- write_hostsfile(State, {hosts, []}),
+ % Make sure the file exists and has no stale state
+ [ write_hostsfile([{hosts, []}, {url, U}], State) || U <- Uris ],
{ok, State}.
%%--------------------------------------------------------------------
@@ -97,8 +97,8 @@ handle_call(_Request, _From, State) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-handle_cast({hosts, Hosts}, State) ->
- write_hostsfile(State, {hosts, Hosts}),
+handle_cast(Args, State) ->
+ write_hostsfile(Args, State),
{noreply, State}.
@@ -155,9 +155,9 @@ write_line(Host, [H|T], Aggr) ->
write_line(Host, T, [io_lib:format("~s ~s~n",[H,Host])|Aggr]).
-write_hostsfile(#state{config=Config}, {hosts, Hosts}) ->
+write_hostsfile([{hosts, Hosts}, {url, Url}], #state{config=Config}) ->
File = filename:join(Config#config.dir,
- "libvirt-" ++ http_uri:encode(Config#config.url) ++ ".hosts"),
+ "libvirt-" ++ http_uri:encode(Url) ++ ".hosts"),
TmpFile = File ++ [".tmp"],
Data = [ write_line(Host, Addrs) || {Host, Addrs} <- Hosts ],
file:write_file(TmpFile, Data),
diff --git a/src/simplec_sup.erl b/src/simplec_sup.erl
index f4bc3fa..41fa90b 100644
--- a/src/simplec_sup.erl
+++ b/src/simplec_sup.erl
@@ -10,6 +10,8 @@
-behaviour(supervisor).
+-include("simplec.hrl").
+
%% API
-export([start_link/1]).
@@ -58,8 +60,14 @@ init(Config) ->
MaxSecondsBetweenRestarts = 3600,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
- Childs = [?CHILD_WITH_ARGS(simplec_vms, worker, Config),
- ?CHILD_WITH_ARGS(simplec_hostsfile, worker, Config)],
+
+ VmChilds = [{list_to_atom(Url),
+ {simplec_vms, start_link, [{url, Url}]},
+ permanent,
+ 5000,
+ worker,
+ [simplec_vms]} || Url <- Config#config.uris],
+ Childs = VmChilds ++ [?CHILD_WITH_ARGS(simplec_hostsfile, worker, Config)],
{ok, {SupFlags, Childs}}.
%%%===================================================================
diff --git a/src/simplec_vms.erl b/src/simplec_vms.erl
index 1d3e076..506c76c 100644
--- a/src/simplec_vms.erl
+++ b/src/simplec_vms.erl
@@ -35,8 +35,9 @@
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
-start_link(Args) ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, Args, []).
+start_link(Args = {url, Url}) ->
+ Name = list_to_atom(lists:flatten(io_lib:format("~p_~s", [?SERVER, Url] ))),
+ gen_server:start_link({local, Name}, ?MODULE, Args, []).
%%%===================================================================
%%% gen_server callbacks
@@ -53,8 +54,7 @@ start_link(Args) ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-init(Config) ->
- Url = Config#config.url,
+init({url, Url}) ->
io:format("Monitoring ~s~n~n", [Url]),
{ok, Ref} = verx_client:start(),
{ok, [1]} = verx:auth_polkit(Ref),
@@ -104,11 +104,11 @@ handle_cast(Msg, State) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-handle_info(timeout, State) ->
+handle_info(timeout, State = #state{url=Url}) ->
{ok, Res} = get_addresses(State#state.verxref),
case Res == State#state.cur of
false -> print_addresses(Res),
- simplec_hostsfile:write(Res);
+ simplec_hostsfile:write(Res, Url);
true -> true
end,
{noreply, State#state{cur=Res}, ?QUERY_INTERVAL};