Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Apr 1, 2015
2 parents 7d34490 + 1fa8f0d commit 3a10d44
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 28 deletions.
21 changes: 8 additions & 13 deletions apps/wiggle/priv/schema/vm/vm_create.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"alias": {
"type": "string",
"required": "true",
"pattern": "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
"required": "true"
},
"routes": {
"type": "array",
Expand Down Expand Up @@ -60,8 +59,7 @@
},
"grouping": {
"type": "string",
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"required": true
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
},
"requirements": {
"type": "array",
Expand Down Expand Up @@ -93,15 +91,12 @@
}
},
"networks": {
"type": "array",
"items": {
"type": "object",
"patternProperties": {
"net\\d+": {
"type": "string",
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"required": true
}
"type": "object",
"patternProperties": {
"net\\d+": {
"type": "string",
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"required": true
}
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/wiggle/src/wiggle.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
e2qc,
fifo_s3,
folsom_ddb,
dqe,
erlydtl,
cowboy
]},
Expand Down
150 changes: 138 additions & 12 deletions apps/wiggle/src/wiggle_vm_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ allowed_methods(_Version, _Token, []) ->
allowed_methods(_Version, _Token, [?UUID(_Vm)]) ->
[<<"GET">>, <<"PUT">>, <<"DELETE">>];

allowed_methods(?V2, _Token, [?UUID(_Vm), <<"metrics">>| _]) ->
[<<"GET">>];

allowed_methods(?V2, _Token, [?UUID(_Vm), <<"config">>]) ->
[<<"POST">>];
[<<"PUT">>];

allowed_methods(?V2, _Token, [?UUID(_Vm), <<"package">>]) ->
[<<"POST">>];
[<<"PUT">>];

allowed_methods(?V2, _Token, [?UUID(_Vm), <<"state">>]) ->
[<<"POST">>];
[<<"PUT">>];

allowed_methods(_Version, _Token, [<<"dry_run">>]) ->
[<<"PUT">>];
Expand Down Expand Up @@ -145,6 +148,9 @@ get(State = #state{path = [?UUID(Vm), <<"fw_rules">>, IDB]}) ->
E
end;

get(#state{path = [?UUID(_Vm), <<"metrics">>]}) ->
{ok, now()};

get(State = #state{path = [?UUID(Vm) | _]}) ->
Start = now(),
R = case application:get_env(wiggle, vm_ttl) of
Expand Down Expand Up @@ -174,6 +180,10 @@ permission_required(#state{method = <<"POST">>, path = []}) ->
permission_required(#state{method = <<"GET">>, path = [?UUID(Vm)]}) ->
{ok, [<<"vms">>, Vm, <<"get">>]};

permission_required(#state{version = ?V2, method = <<"GET">>,
path = [?UUID(Vm), <<"metrics">> | _]}) ->
{ok, [<<"vms">>, Vm, <<"get">>]};

permission_required(#state{method = <<"DELETE">>, path = [?UUID(Vm)]}) ->
{ok, [<<"vms">>, Vm, <<"delete">>]};

Expand Down Expand Up @@ -241,22 +251,22 @@ permission_required(#state{method = <<"PUT">>, body = undefiend}) ->
permission_required(#state{method = <<"PUT">>, body = Decoded, version = ?V1,
path = [?UUID(Vm)]}) ->
case Decoded of
[{<<"action">>, Act}] ->
[{<<"action">>, Act} | _] ->
{ok, [<<"vms">>, Vm, Act]};
_ ->
{ok, [<<"vms">>, Vm, <<"edit">>]}
end;

permission_required(#state{method = <<"POST">>, body = [{<<"action">>, Act}],
permission_required(#state{method = <<"PUT">>, body = [{<<"action">>, Act} | _],
version = ?V2, path = [?UUID(Vm), <<"state">>]}) ->
{ok, [<<"vms">>, Vm, Act]};


permission_required(#state{method = <<"POST">>, version = ?V2,
permission_required(#state{method = <<"PUT">>, version = ?V2,
path = [?UUID(Vm), <<"config">>]}) ->
{ok, [<<"vms">>, Vm, <<"edit">>]};

permission_required(#state{method = <<"POST">>, version = ?V2,
permission_required(#state{method = <<"PUT">>, version = ?V2,
path = [?UUID(Vm), <<"package">>]}) ->
{ok, [<<"vms">>, Vm, <<"edit">>]};

Expand Down Expand Up @@ -324,17 +334,17 @@ schema(#state{method = <<"POST">>, path = []}) ->
vm_create;

%% Changes a VM state
schema(#state{method = <<"POST">>, path = [?UUID(_Vm), <<"state">>],
schema(#state{method = <<"PUT">>, path = [?UUID(_Vm), <<"state">>],
version = ?V2}) ->
vm_update_state;


%% Updates a VM Config, we don't have validation that in the V! api
schema(#state{method = <<"POST">>, path = [?UUID(_Vm), <<"config">>],
schema(#state{method = <<"PUT">>, path = [?UUID(_Vm), <<"config">>],
version = ?V2}) ->
vm_update_config;

schema(#state{method = <<"POST">>, path = [?UUID(_Vm), <<"package">>],
schema(#state{method = <<"PUT">>, path = [?UUID(_Vm), <<"package">>],
version = ?V2}) ->
vm_update_package;

Expand Down Expand Up @@ -402,7 +412,6 @@ read(Req, State = #state{path = [?UUID(_Vm), <<"backups">>, SnapID], obj = Obj})
{jsxd:set(<<"uuid">>, SnapID, SnapObj), Req, State};
_ ->
{null, Req, State}

end;

read(Req, State = #state{path = [?UUID(_Vm), <<"services">>], obj = Obj}) ->
Expand All @@ -412,7 +421,21 @@ read(Req, State = #state{path = [?UUID(_Vm), <<"services">>, Service], obj = Obj
{jsxd:get([Service], [{}], ft_vm:services(Obj)), Req, State};

read(Req, State = #state{path = [?UUID(_Vm)], obj = Obj}) ->
{to_json(Obj), Req, State}.
{to_json(Obj), Req, State};

read(Req, State = #state{path = [?UUID(Vm), <<"metrics">>]}) ->
{QS, Req1} = cowboy_req:qs_vals(Req),
{ok, Q} = perf(Vm, lists:sort(QS)),
lager:debug("[metrics] Running query ~s", [Q]),
{T, {ok, Res}} = timer:tc(dqe, run, [Q]),
lager:debug("[metrics] The query took ~pus", [T]),
JSON = [[{<<"n">>, Name},
{<<"r">>, Resolution},
{<<"v">>, mmath_bin:to_list(Data)}]
|| {Name, Data, Resolution} <- Res],

{JSON, Req1, State}.


%%--------------------------------------------------------------------
%% PUT
Expand Down Expand Up @@ -860,3 +883,106 @@ find_rule(ID, VM) ->
{error, oh_shit}
end.

%%--------------------------------------------------------------------
%% Internal
%%--------------------------------------------------------------------

valid_time(_Time) ->
true. %% TODO!

valid_pit(_PIT) ->
true. %% TODO

perf(UUID, QS) ->
Zone = perf_zone_id(UUID),
Elems = perf_cpu(Zone) ++ perf_mem(Zone) ++ perf_swap(Zone)
++ perf_net(Zone, <<"net0">>) ++ perf_zfs(Zone),
case lists:keytake(<<"aggr">>, 1, QS) of
false ->
perf1(Elems, QS);
{value, {<<"aggr">>, Res}, QS1} ->
case valid_time(Res) of
true ->
Elems1 = apply_aggr("avg", Res, Elems),
perf1(Elems1, QS1);
false ->
{error, bad_resolution}
end
end.

perf1(Elems, [{<<"last">>, Last}]) ->
case valid_time(Last) of
true ->
{ok, apply_query(Elems, ["LAST ", Last])};
false ->
{error, bad_last}
end;


perf1(Elems, [{<<"after">>, After}, {<<"for">>, For}]) ->
case valid_pit(After) andalso valid_time(For) of
true ->
{ok, apply_query(Elems, ["AFTER ", After, " FOR ", For])};
false ->
{error, bad_after}
end;

perf1(Elems, [{<<"before">>, Before}, {<<"for">>, For}]) ->
case valid_pit(Before) andalso valid_time(For) of
true ->
{ok, apply_query(Elems, ["BEFORE ", Before, " FOR ", For])};
false ->
{error, bad_before}
end;


perf1(Elems, []) ->
{ok, apply_query(Elems, "LAST 1m")};

perf1(_Elems, _) ->
{error, bad_qs}.

perf_zone_id(<<Z:30/binary, _/binary>>) ->
Z.

perf_cpu(Zone) ->
[{[$', Zone | "'.'cpu'.'usage' BUCKET zone"], "cpu-usage"},
{[$', Zone | "'.'cpu'.'effective' BUCKET zone"], "cpu-effective"},
{[$', Zone | "'.'cpu'.'nwait' BUCKET zone"], "cpu-nwait"}].


perf_mem(Zone) ->
[{[$', Zone | "'.'mem'.'usage' BUCKET zone"], "memory-usage"},
{[$', Zone | "'.'mem'.'value' BUCKET zone"], "memory-value"}].


perf_swap(Zone) ->
[{[$', Zone | "'.'swap'.'usage' BUCKET zone"], "swapory-usage"},
{[$', Zone | "'.'swap'.'value' BUCKET zone"], "swapory-value"}].


perf_net(Zone, Nic) ->
[{["derivate('", Zone, "'.'net'.'", Nic, "'.'opackets64' BUCKET zone)"],
["net-send-ops-", Nic]},
{["derivate('", Zone, "'.'net'.'", Nic, "'.'ipackets64' BUCKET zone)"],
["net-recv-ops-", Nic]},
{["divide(derivate('", Zone, "'.'net'.'", Nic, "'.'obytes64' BUCKET zone), 1024)"],
["net-send-kb-", Nic]},
{["divide(derivate('", Zone, "'.'net'.'", Nic, "'.'rbytes64' BUCKET zone), 1024)"],
["net-recv-kb-", Nic]}].

perf_zfs(Zone) ->
[{["divide(derivate('", Zone, "'.'zfs'.'nread' BUCKET zone), 1024)"], "zfs-read-kb"},
{["divide(derivate('", Zone, "'.'zfs'.'nwritten' BUCKET zone), 1024)"], "zfs-write-kb"},
{["derivate('", Zone, "'.'zfs'.'reads' BUCKET zone)"], "zfs-read-ops"},
{["derivate('", Zone, "'.'zfs'.'writes' BUCKET zone)"], "zfs-write-ops"}].

apply_aggr(Aggr, Res, Elements) ->
[{[Aggr, $(, Qry, ", ", Res, $)], Alias} ||
{Qry, Alias} <- Elements].


apply_query(Elements, Range) ->
Elements1 = [[Qry, " AS '", Alias, "'"] || {Qry, Alias} <- Elements],
iolist_to_binary(["SELECT ", string:join(Elements1, ", "), " ", Range]).

3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
{oauth2, "0.6.*", {git, "https://github.com/kivra/oauth2.git", {tag, "0.6.0"}}},
{erlydtl, ".*", {git, "https://github.com/erlydtl/erlydtl.git", {tag, "0.10.0"}}},
{jesse, ".*", {git, "https://github.com/klarna/jesse.git", {tag, "1.1.5"}}},
{folsom_ddb, ".*", {git, "https://github.com/dalmatinerdb/folsom_ddb.git", {tag, "0.1.4"}}}
{folsom_ddb, ".*", {git, "https://github.com/dalmatinerdb/folsom_ddb.git", {tag, "0.1.4"}}},
{dqe, ".*", {git, "https://github.com/dalmatinerdb/dqe.git", {tag, "0.1.3"}}}
]}.

{plugin_dir, "deps/cuttlefish/src"}.
Expand Down
3 changes: 2 additions & 1 deletion rel/reltool.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
{template, "../deps/fifo_s3/priv/fifo_s3.schema", "share/schema/05-fifo_S3.schema"},
{template, "../deps/folsom_ddb/priv/folsom_ddb.schema", "share/schema/07-folsom_ddb.schema"},
{template, "../deps/ensq_rpc/priv/ensq_rpc.schema", "share/schema/08-ensq_rpc.schema"},
{template, "../deps/cuttlefish/priv/erlang_vm.schema", "share/schema/09-vm.schema"},
{template, "../deps/dqe/priv/dqe.schema", "share/schema/09-dqe.schema"},
{template, "../deps/cuttlefish/priv/erlang_vm.schema", "share/schema/10-vm.schema"},
{copy, "../plugins", "plugins"},
{copy, "../snmp", "snmp"},
{copy, "../LICENSE", "LICENSE"},
Expand Down
2 changes: 1 addition & 1 deletion schema/wiggle.schema
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,5 @@

{mapping, "folsom_ddb.prefix", "folsom_ddb.prefix", [
merge,
{default, "sniffle"}
{default, "wiggle"}
]}.

0 comments on commit 3a10d44

Please sign in to comment.