diff --git a/virtuerl/src/virtuerl_api.erl b/virtuerl/src/virtuerl_api.erl index 8d1c25a..183511b 100644 --- a/virtuerl/src/virtuerl_api.erl +++ b/virtuerl/src/virtuerl_api.erl @@ -185,4 +185,10 @@ handle(domain, 'GET', #{id := ID}, Req) -> end; handle(domain, 'DELETE', #{id := ID}, Req) -> io:format("DOMAIN DELETE: ~p~n", [ID]), - mochiweb_request:respond({204, [{"Content-Type", "application/json"}], <<"">>}, Req). + Resp = virtuerl_mgt:domain_delete(#{id => list_to_binary(ID)}), + case Resp of + ok -> + mochiweb_request:respond({204, [{"Content-Type", "application/json"}], <<"">>}, Req); + _ -> + mochiweb_request:respond({500, [{"Content-Type", "text/plain"}], "Error"}, Req) + end. diff --git a/virtuerl/src/virtuerl_mgt.erl b/virtuerl/src/virtuerl_mgt.erl index 61ce019..7589c97 100644 --- a/virtuerl/src/virtuerl_mgt.erl +++ b/virtuerl/src/virtuerl_mgt.erl @@ -11,7 +11,7 @@ -export([start_link/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, handle_continue/2]). --export([create_vm/0, domain_create/1, domain_get/1]). +-export([create_vm/0, domain_create/1, domain_get/1, domain_delete/1]). -define(SERVER, ?MODULE). @@ -141,7 +141,15 @@ handle_call({domain_get, #{id := DomainID}}, _From, State) -> {ok, DomRet}; [] -> notfound end, - {reply, Reply, State}. + {reply, Reply, State}; +handle_call({domain_delete, #{id := DomainID}}, _From, State) -> + {Table} = State, + Res = dets:delete(Table, DomainID), + dets:sync(Table), + ok = supervisor:terminate_child(virtuerl_sup, DomainID), + ok = supervisor:delete_child(virtuerl_sup, DomainID), + ok = gen_server:call(virtuerl_net, {net_update}), + {reply, Res, State}. handle_cast(_Request, State) -> {noreply, State}. diff --git a/virtuerl/src/virtuerl_net.erl b/virtuerl/src/virtuerl_net.erl index cb87b22..90d5c7e 100644 --- a/virtuerl/src/virtuerl_net.erl +++ b/virtuerl/src/virtuerl_net.erl @@ -184,7 +184,7 @@ add_bridges([], _) -> add_bridges([Cidrs|T], Ifnames) -> Ifname = generate_unique_bridge_name(Ifnames), AddrAddCmd = [io_lib:format("ip addr add ~s dev ~s~n", [Cidr, Ifname]) || Cidr <- Cidrs], - Cmd = lists:flatten([io_lib:format("ip link add name ~s type bridge~n", [Ifname]), AddrAddCmd]), + Cmd = lists:flatten([io_lib:format("ip link add name ~s type bridge~nip link set ~s up~n", [Ifname, Ifname]), AddrAddCmd]), io:format(Cmd), os:cmd(Cmd), add_bridges(T, Ifnames), @@ -233,7 +233,7 @@ end, sets:to_list(TapsToDelete)), add_taps(M) when is_map(M) -> add_taps(maps:to_list(M)); add_taps([]) -> ok; add_taps([{Tap, Bridge}|T]) -> - Cmd = io_lib:format("ip tuntap add dev ~s mode tap~nip link set dev ~s master ~s~n", [Tap, Tap, Bridge]), + Cmd = io_lib:format("ip tuntap add dev ~s mode tap~nip link set dev ~s master ~s~nip link set ~s up~n", [Tap, Tap, Bridge, Tap]), io:format(Cmd), os:cmd(Cmd), add_taps(T).