Skip to content
Browse files

Wrappers to ease setting up of vm's

Initial commit of some code to simplify running virtual machines.
  • Loading branch information...
1 parent 7a532f5 commit e1d027bd35ce302603e0adf21d3b61c503bff2fc @msantos committed Apr 1, 2011
Showing with 237 additions and 1 deletion.
  1. +3 −1 ebin/evum.app
  2. +160 −0 src/evm.erl
  3. +74 −0 src/evm_cfg.erl
View
4 ebin/evum.app
@@ -7,7 +7,9 @@
evum_mcons,
evum_ctl,
evum_switch,
- mcons
+ mcons,
+ evm,
+ evm_cfg
]},
{registered, []},
{applications, [
View
160 src/evm.erl
@@ -0,0 +1,160 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(evm).
+
+-export([
+ args/1,
+ start/3
+ ]).
+-export([
+ check_image/1,
+ name/1,
+ path/1
+ ]).
+
+
+args(Dist) ->
+ evum:make_args(evm_cfg:dist(args, Dist)).
+
+start(Label, Dist, Location) when is_list(Label), is_atom(Dist), is_atom(Location) ->
+ Image = check_image(Dist),
+ start_evum(),
+ {ok,Ref} = start_vm(Label, Image, Dist),
+
+ wait_prompt(),
+
+ start_net(Ref, Location),
+ {ok, Ref}.
+
+wait_prompt() ->
+ receive
+ % buildroot
+ <<"#", _/binary>> -> ok;
+ % OpenWRT
+ <<"root@", _/binary>> ->
+ error_logger:info_report([{prompt, openwrt}]),
+ ok;
+ _ -> wait_prompt()
+ after
+ % Random Linux system image
+ 10000 ->
+ error_logger:info_report([{prompt, timeout}]),
+ ok
+ end.
+
+start_evum() ->
+ case lists:member(evum_ctl, erlang:registered()) of
+ true ->
+ ok;
+ false ->
+ {ok,Switch} = evum_switch:start(),
+ evum_ctl:start(Switch)
+ end.
+
+start_vm(Label, Image, Dist) ->
+ Arg = evm_cfg:dist(args, Dist),
+ Cow = Image ++ ".cow-" ++ Label ++ "," ++ Image,
+ {ok,Ref} = evum:start(Arg ++ [{ubd, Cow}]),
+ {ok,Ref}.
+
+start_net(Ref, Location) ->
+ Base = evm_cfg:net(base_ip, Location),
+ First = evm_cfg:net(first_ip, Location),
+ Mask = evm_cfg:net(netmask, Location),
+ GW = evm_cfg:net(gw, Location),
+
+ Ping = case evm_cfg:net(ping, Location) of
+ undefined -> GW;
+ IP -> IP
+ end,
+
+ evum:ifconfig(Ref, {
+ Base ++ integer_to_list(First),
+ Mask,
+ GW
+ }),
+
+ evum:ping(Ref, Ping).
+
+
+check_image(Dist) ->
+ URI = evm_cfg:dist(uri, Dist),
+ Path = path(filename:basename(URI)),
+ File = path(evm_cfg:dist(file, Dist)),
+
+ case file:read_file_info(File) of
+ {ok, _} ->
+ ok;
+ {error, enoent} ->
+ Path1 = download_image(URI, Path),
+ error_logger:info_report([{path, Path1}]),
+ ok = file:rename(Path1, File)
+ end,
+
+ File.
+
+download_image(URI, File) ->
+ inets:start(),
+ ssl:start(),
+ {ok, _} = httpc:request(get, {URI, []}, [], [{stream, File}]),
+ decompress_image(File).
+
+decompress_image(File) ->
+ decompress(name(File)).
+
+decompress({File, ".tar"}) ->
+ os:cmd("tar xf " ++ File ++ ".tar"),
+ decompress(name(File));
+decompress({File, ".bz2"}) ->
+ os:cmd("bzip2 -d " ++ File ++ ".bz2"),
+ decompress(name(File));
+decompress({File, ".gz"}) ->
+ os:cmd("gzip -d " ++ File ++ ".gz"),
+ decompress(name(File));
+decompress({File, Ext}) ->
+ File ++ Ext.
+
+name(File) ->
+ {filename:rootname(File), filename:extension(File)}.
+
+path(File) ->
+ Path = filename:join([
+ filename:dirname(
+ code:which(?MODULE)),
+ "..",
+ "priv",
+ "img",
+ File
+ ]),
+
+ ok = filelib:ensure_dir(Path),
+ Path.
+
View
74 src/evm_cfg.erl
@@ -0,0 +1,74 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(evm_cfg).
+
+-export([
+ path/0,
+ config/1,
+
+ network/1,
+ image/1,
+
+ val/2,
+ net/2,
+ dist/2
+ ]).
+
+-define(CONFIG, "evm.cfg").
+
+
+path() ->
+ filename:join([
+ filename:dirname(code:which(?MODULE)),
+ "..",
+ "priv",
+ ?CONFIG
+ ]).
+
+config(Key) when is_atom(Key) ->
+ {ok, Cfg} = file:consult(path()),
+ proplists:get_value(Key, Cfg).
+
+network(Location) when is_atom(Location) ->
+ proplists:get_value(Location, config(network)).
+
+image(Dist) when is_atom(Dist) ->
+ proplists:get_value(Dist, config(image)).
+
+val(Type, {Key, Location}) when is_atom(Type), is_atom(Key), is_atom(Location) ->
+ proplists:get_value(Key, ?MODULE:Type(Location)).
+
+net(Key, Location) ->
+ val(network, {Key, Location}).
+
+dist(Key, Dist) ->
+ val(image, {Key, Dist}).
+

0 comments on commit e1d027b

Please sign in to comment.
Something went wrong with that request. Please try again.