From 4500b9a982f0e0803318d604a993ded16820dcd3 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Sat, 19 Sep 2015 14:03:02 +0200 Subject: Initial commit --- src/simplec_hostsfile.erl | 164 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 src/simplec_hostsfile.erl (limited to 'src/simplec_hostsfile.erl') diff --git a/src/simplec_hostsfile.erl b/src/simplec_hostsfile.erl new file mode 100644 index 0000000..fba64af --- /dev/null +++ b/src/simplec_hostsfile.erl @@ -0,0 +1,164 @@ +%%%------------------------------------------------------------------- +%%% @author Guido +%%% @copyright (C) 2015, Guido Günther +%%% @doc +%%% +%%% @end +%%% Created : 30 Aug 2015 by Guido +%%%------------------------------------------------------------------- +-module(simplec_hostsfile). + +-behaviour(gen_server). + +-include("simplec.hrl"). + +%% API +-export([start_link/1, + write/1]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-define(SERVER, ?MODULE). + +-record(state, {config}). + +%%%=================================================================== +%%% API +%%%=================================================================== + +%%-------------------------------------------------------------------- +%% @doc +%% Starts the server +%% +%% @spec start_link() -> {ok, Pid} | ignore | {error, Error} +%% @end +%%-------------------------------------------------------------------- +start_link(Config) -> + gen_server:start_link({local, ?SERVER}, ?MODULE, Config, []). + + +%%-------------------------------------------------------------------- +%% @doc +%% Writes out the hosts +%% @end +%%-------------------------------------------------------------------- +write(Hosts) -> + gen_server:cast(simplec_hostsfile, {hosts, Hosts}). + + +%%%=================================================================== +%%% gen_server callbacks +%%%=================================================================== + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Initializes the server +%% +%% @spec init(Args) -> {ok, State} | +%% {ok, State, Timeout} | +%% ignore | +%% {stop, Reason} +%% @end +%%-------------------------------------------------------------------- +init(Config) -> + State = #state{config=Config}, + % Make sure the file exists + write_hostsfile(State, {hosts, []}), + {ok, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Handling call messages +%% +%% @spec handle_call(Request, From, State) -> +%% {reply, Reply, State} | +%% {reply, Reply, State, Timeout} | +%% {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, Reply, State} | +%% {stop, Reason, State} +%% @end +%%-------------------------------------------------------------------- +handle_call(_Request, _From, State) -> + Reply = ok, + {reply, Reply, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Handling cast messages +%% +%% @spec handle_cast(Msg, State) -> {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} +%% @end +%%-------------------------------------------------------------------- +handle_cast({hosts, Hosts}, State) -> + write_hostsfile(State, {hosts, Hosts}), + {noreply, State}. + + + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Handling all non call/cast messages +%% +%% @spec handle_info(Info, State) -> {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} +%% @end +%%-------------------------------------------------------------------- +handle_info(_Info, State) -> + {noreply, State}. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% This function is called by a gen_server when it is about to +%% terminate. It should be the opposite of Module:init/1 and do any +%% necessary cleaning up. When it returns, the gen_server terminates +%% with Reason. The return value is ignored. +%% +%% @spec terminate(Reason, State) -> void() +%% @end +%%-------------------------------------------------------------------- +terminate(_Reason, _State) -> + ok. + +%%-------------------------------------------------------------------- +%% @private +%% @doc +%% Convert process state when code is changed +%% +%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState} +%% @end +%%-------------------------------------------------------------------- +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== + + +write_line(Host, Addr) -> + write_line(Host, Addr, []). + +write_line(_Host, [], Aggr) -> + Aggr; +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}) -> + File = filename:join(Config#config.dir, + "libvirt-" ++ http_uri:encode(Config#config.url) ++ ".hosts"), + TmpFile = File ++ [".tmp"], + Data = [ write_line(Host, Addrs) || {Host, Addrs} <- Hosts ], + file:write_file(TmpFile, Data), + file:rename(TmpFile, File). -- cgit v1.2.3