Permalink
Browse files

release.

  • Loading branch information...
2 parents ee71790 + 1a86c6a commit a181a53035d9b1a0caa454bc3c1c2620639a728d @Licenser Licenser committed Apr 21, 2013
View
@@ -49,6 +49,9 @@ docs:
## Developer targets
##
+xref: compile
+ @$(REBAR) xref skip_deps=true
+
stage : rel
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf rel/wiggle/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) rel/wiggle/lib;)
@@ -1,21 +1,21 @@
{application, wiggle,
[
{description, ""},
- {vsn, "0.4.6"},
+ {vsn, "0.4.7"},
{registered, []},
{applications, [
kernel,
stdlib,
- libsnarl,
- libsniffle,
- libhowl,
- libchunter,
- jsx,
- lager,
- mimetypes,
- cowboy
+ libsnarl,
+ libsniffle,
+ libhowl,
+ libchunter,
+ jsx,
+ lager,
+ mimetypes,
+ cowboy
]},
{mod, { wiggle_app, []}},
{env, [{acceptors, 100},
- {port, 8080}]}
+ {port, 8080}]}
]}.
@@ -5,13 +5,18 @@
-ignore_xref([start/0]).
start() ->
+ lager:start(),
application:start(mdns_client_lib),
application:start(libsnarlmatch),
+ application:start(libchunter),
application:start(libsnarl),
+ application:start(libhowl),
application:start(libsniffle),
application:start(jsx),
application:start(lager),
application:start(mimetypes),
+ application:start(crypto),
+ application:start(ranch),
application:start(cowboy),
application:start(wiggle).
@@ -14,22 +14,23 @@ start(_StartType, _StartArgs) ->
{ok, Port} = application:get_env(wiggle, port),
{ok, Acceptors} = application:get_env(wiggle, acceptors),
- Dispatch = [{'_', [{[<<"api">>, '_', <<"users">>, '...'], wiggle_user_handler, []},
- {[<<"api">>, '_', <<"sessions">>, '...'], wiggle_session_handler, []},
- {[<<"api">>, '_', <<"groups">>, '...'], wiggle_group_handler, []},
- {[<<"api">>, '_', <<"cloud">>, '...'], wiggle_cloud_handler, []},
- {[<<"api">>, '_', <<"hypervisors">>, '...'], wiggle_hypervisor_handler, []},
- {[<<"api">>, '_', <<"dtrace">>, '_', <<"stream">>], wiggle_dtrace_stream, []},
- {[<<"api">>, '_', <<"dtrace">>, '...'], wiggle_dtrace_handler, []},
- {[<<"api">>, '_', <<"vms">>, '_', <<"console">>], wiggle_console_handler, []},
- {[<<"api">>, '_', <<"vms">>, '_', <<"vnc">>], wiggle_vnc_handler, []},
- {[<<"api">>, '_', <<"vms">>, '...'], wiggle_vm_handler, []},
- {[<<"api">>, '_', <<"ipranges">>, '...'], wiggle_iprange_handler, []},
- {[<<"api">>, '_', <<"datasets">>, '...'], wiggle_dataset_handler, []},
- {[<<"api">>, '_', <<"packages">>, '...'], wiggle_package_handler, []}]}],
- {ok, _} = cowboy:start_listener(http, Acceptors,
- cowboy_tcp_transport, [{port, Port}],
- cowboy_http_protocol, [{dispatch, Dispatch}]),
+ Dispatch = cowboy_router:compile(
+ [{'_', [{<<"/api/:version/users/[...]">>, wiggle_user_handler, []},
+ {<<"/api/:version/sessions/[...]">>, wiggle_session_handler, []},
+ {<<"/api/:version/groups/[...]">>, wiggle_group_handler, []},
+ {<<"/api/:version/cloud/[...]">>, wiggle_cloud_handler, []},
+ {<<"/api/:version/hypervisors/[...]">>, wiggle_hypervisor_handler, []},
+ {<<"/api/:version/dtrace/:uuid/stream">>, wiggle_dtrace_stream, []},
+ {<<"/api/:version/dtrace/[...]">>, wiggle_dtrace_handler, []},
+ {<<"/api/:version/vms/:uuid/console">>, wiggle_console_handler, []},
+ {<<"/api/:version/vms/:uuid/vnc">>, wiggle_vnc_handler, []},
+ {<<"/api/:version/vms/[...]">>, wiggle_vm_handler, []},
+ {<<"/api/:version/ipranges/[...]">>, wiggle_iprange_handler, []},
+ {<<"/api/:version/datasets/[...]">>, wiggle_dataset_handler, []},
+ {<<"/api/:version/packages/[...]">>, wiggle_package_handler, []}]}]
+ ),
+ {ok, _} = cowboy:start_http(http, Acceptors, [{port, Port}],
+ [{env, [{dispatch, Dispatch}]}]),
wiggle_sup:start_link().
@@ -40,11 +40,10 @@
-record(state, {path, method, version, token, content, reply, obj, body}).
init(_Transport, _Req, []) ->
- {upgrade, protocol, cowboy_http_rest}.
+ {upgrade, protocol, cowboy_rest}.
rest_init(Req, _) ->
- wiggle_handler:initial_state(Req, <<"cloud">>).
-
+ wiggle_handler:initial_state(Req).
service_available(Req, State = #state{path = [<<"connection">>]}) ->
{true, Req, State};
@@ -60,12 +59,12 @@ service_available(Req, State) ->
end.
options(Req, State) ->
- Methods = allowed_methods(Req, State, State#state.path),
- {ok, Req1} = cowboy_http_req:set_resp_header(
- <<"Access-Control-Allow-Methods">>,
- string:join(
- lists:map(fun erlang:atom_to_list/1,
- ['HEAD', 'OPTIONS' | Methods]), ", "), Req),
+ Methods = allowed_methods(State#state.version, State#state.token, State#state.path),
+ Req1 = cowboy_req:set_resp_header(
+ <<"access-control-allow-methods">>,
+ string:join(
+ lists:map(fun erlang:binary_to_list/1,
+ [<<"HEAD">>, <<"OPTIONS">> | Methods]), ", "), Req),
{ok, Req1, State}.
content_types_provided(Req, State) ->
@@ -78,33 +77,33 @@ content_types_accepted(Req, State) ->
{wiggle_handler:accepted(), Req, State}.
allowed_methods(Req, State) ->
- {['HEAD', 'OPTIONS' | allowed_methods(State#state.version, State#state.token, State#state.path)], Req, State}.
+ {[<<"HEAD">>, <<"OPTIONS">> | allowed_methods(State#state.version, State#state.token, State#state.path)], Req, State}.
allowed_methods(_Version, _Token, [<<"connection">>]) ->
- ['GET'];
+ [<<"GET">>];
allowed_methods(_Version, _Token, []) ->
- ['GET'].
+ [<<"GET">>].
resource_exists(Req, State = #state{path = []}) ->
{true, Req, State};
resource_exists(Req, State = #state{path = [<<"connection">>]}) ->
{true, Req, State}.
-is_authorized(Req, State = #state{method = 'OPTIONS'}) ->
+is_authorized(Req, State = #state{method = <<"OPTIONS">>}) ->
{true, Req, State};
is_authorized(Req, State = #state{path = [<<"connection">>]}) ->
{true, Req, State};
is_authorized(Req, State = #state{token = undefined}) ->
- {{false, <<"X-Snarl-Token">>}, Req, State};
+ {{false, <<"x-snarl-token">>}, Req, State};
is_authorized(Req, State) ->
{true, Req, State}.
-forbidden(Req, State = #state{method = 'OPTIONS'}) ->
+forbidden(Req, State = #state{method = <<"OPTIONS">>}) ->
{false, Req, State};
forbidden(Req, State = #state{path = [<<"connection">>]}) ->
@@ -142,19 +141,19 @@ handle_request(Req, State = #state{path = []}) ->
case libsniffle:cloud_status() of
{ok, {Metrics, Warnings}} ->
Vers0 = case libsnarl:version() of
- SrvVer when is_binary(SrvVer) ->
+ {ok, SrvVer} when is_binary(SrvVer) ->
[{snarl, SrvVer}];
_ ->
[{snarl, <<"not connected">>}]
end,
Vers1 = case libsniffle:version() of
- SrvVer1 when is_binary(SrvVer1) ->
+ {ok, SrvVer1} when is_binary(SrvVer1) ->
[{sniffle, SrvVer1} | Vers0];
_ ->
[{sniffle, <<"not connected">>} | Vers0]
end,
Vers2 = case libhowl:version() of
- SrvVer2 when is_binary(SrvVer2) ->
+ {ok, SrvVer2} when is_binary(SrvVer2) ->
[{howl, SrvVer2} | Vers1];
_ ->
[{howl, <<"not connected">>} | Vers1]
@@ -1,80 +1,59 @@
-module(wiggle_console_handler).
--behaviour(cowboy_http_handler).
--behaviour(cowboy_http_websocket_handler).
+-behaviour(cowboy_websocket_handler).
+
+-export([init/3]).
--export([init/3,
- handle/2,
- terminate/2]).
-export([websocket_init/3,
websocket_handle/3,
websocket_info/3,
websocket_terminate/3]).
-init({_Any, http}, Req, []) ->
- case cowboy_http_req:header('Upgrade', Req) of
- {undefined, Req2} -> {ok, Req2, undefined};
- {<<"websocket">>, _Req2} -> {upgrade, protocol, cowboy_http_websocket};
- {<<"WebSocket">>, _Req2} -> {upgrade, protocol, cowboy_http_websocket}
- end.
+-ignore_xref([init/3]).
-handle(Req, State) ->
- {ok, Req1} = cowboy_http_req:reply(200, [], <<"">>, Req),
- {ok, Req1, State}.
+init({_Any, http}, _Req, []) ->
+ {upgrade, protocol, cowboy_websocket}.
-terminate(_Req, _State) ->
- ok.
+
+e(Code, Req) ->
+ e(Code, <<"">>, Req).
+
+e(Code, Msg, Req) ->
+ {ok, Req1} = cowboy_req:reply(Code, [], Msg, Req),
+ {shutdown, Req1}.
websocket_init(_Any, Req, []) ->
- {[<<"api">>, _, <<"vms">>, ID, <<"console">>], Req1} = cowboy_http_req:path(Req),
- {ok, Req2} = cowboy_http_req:set_resp_header(
- <<"Access-Control-Allow-Headers">>,
- <<"X-Snarl-Token">>, Req1),
- {ok, Req3} = cowboy_http_req:set_resp_header(
- <<"Access-Control-Expose-Headers">>,
- <<"X-Snarl-Token">>, Req2),
- {ok, Req4} = cowboy_http_req:set_resp_header(
- <<"Allow-Access-Control-Credentials">>,
- <<"true">>, Req3),
- {Token, Req5} = case cowboy_http_req:header(<<"X-Snarl-Token">>, Req4) of
- {undefined, ReqX} ->
- {TokenX, ReqX1} = cowboy_http_req:cookie(<<"X-Snarl-Token">>, ReqX),
- {TokenX, ReqX1};
- {TokenX, ReqX} ->
- {ok, ReqX1} = cowboy_http_req:set_resp_header(<<"X-Snarl-Token">>, TokenX, ReqX),
- {TokenX, ReqX1}
- end,
- case libsnarl:allowed({token, Token}, [<<"vms">>, ID, <<"console">>]) of
- true ->
- case libsniffle:vm_get(ID) of
- {ok, VM} ->
- case jsxd:get(<<"hypervisor">>, VM) of
- {ok, HID} ->
- case libsniffle:hypervisor_get(HID) of
- {ok, H} ->
- HostBin = proplists:get_value(<<"host">>, H),
- Host = binary_to_list(HostBin),
- Port = proplists:get_value(<<"port">>, H),
- {ok, Console} = libchunter:console_open(Host, Port, ID, self()),
- {ok, Req5, {Console}};
+ {ID, Req1} = cowboy_req:binding(uuid, Req),
+ Req2 = wiggle_handler:set_access_header(Req1),
+ case wiggle_handler:get_token(Req2) of
+ {undefined, Req3} ->
+ e(401, Req3);
+ {Token, Req3} ->
+ case libsnarl:allowed({token, Token}, [<<"vms">>, ID, <<"console">>]) of
+ true ->
+ case libsniffle:vm_get(ID) of
+ {ok, VM} ->
+ case jsxd:get(<<"hypervisor">>, VM) of
+ {ok, HID} ->
+ case libsniffle:hypervisor_get(HID) of
+ {ok, H} ->
+ {ok,HostBin} = jsxd:get(<<"host">>, H),
+ Host = binary_to_list(HostBin),
+ {ok, Port} = jsxd:get(<<"port">>, H),
+ {ok, Console} = libchunter:console_open(Host, Port, ID, self()),
+ {ok, Req3, {Console}};
+ _ ->
+ e(505, <<"could not find hypervisor">>, Req3)
+ end;
_ ->
- {ok, Req6} = cowboy_http_req:reply(505, [{'Content-Type', <<"text/html">>}],
- <<"could not find hypervisor">>, Req5),
- {shutdown, Req6}
+ e(505, <<"could not find hypervisor">>, Req3)
end;
- _ ->
- {ok, Req6} = cowboy_http_req:reply(505, [{'Content-Type', <<"text/html">>}],
- <<"could not find hypervisor">>, Req5),
- {shutdown, Req6}
+ E ->
+ e(505, list_to_binary(io_lib:format("~p", [E])), Req3)
end;
- E ->
- {ok, Req6} = cowboy_http_req:reply(505, [{'Content-Type', <<"text/html">>}],
- list_to_binary(io_lib:format("~p", [E])), Req5),
- {shutdown, Req6}
- end;
- false ->
- {ok, Req6} = cowboy_http_req:reply(401, [{'Content-Type', <<"text/html">>}], <<"">>, Req5),
- {shutdown, Req6}
+ false ->
+ e(401, Req3)
+ end
end.
websocket_handle({text, Msg}, Req, {Console} = State) ->
Oops, something went wrong.

0 comments on commit a181a53

Please sign in to comment.