WIP: diff in progress

This commit is contained in:
Scott Lystig Fritchie 2015-12-11 15:33:31 +09:00
parent 3826af8ee2
commit 1db232db1b
2 changed files with 78 additions and 8 deletions

View file

@ -856,6 +856,18 @@ d_find(Key, {KV_old, KV_new, IsNew}) ->
end
end.
d_get(Key, {KV_old, KV_new, IsNew}) ->
%% Bah, use 'dict' return value convention.
case gb_trees:lookup(Key, KV_new) of
{value, Val} when IsNew ->
Val;
_ ->
case gb_trees:lookup(Key, KV_old) of
{value, Val} ->
Val
end
end.
d_store(Key, Val, {KV_old, KV_new, false}) ->
{gb_trees:enter(Key, Val, KV_old), KV_new, false};
d_store(Key, Val, {KV_old, KV_new, true}) ->
@ -900,5 +912,57 @@ is_stringy(L) ->
is_porty(Port) ->
is_integer(Port) andalso 1024 =< Port andalso Port =< 65535.
diff_env({KV_old, KV_new, _IsNew}) ->
yo.
diff_env({KV_old, KV_new, _IsNew}=E, RelativeHost) ->
Old_list = gb_trees:to_list(KV_old),
New_list = gb_trees:to_list(KV_new),
Keys_old = [K || {K,_V} <- Old_list],
Keys_new = [K || {K,_V} <- New_list],
Append = fun(Item) ->
fun(K, D) ->
L = gb_trees:get(K, D),
gb_trees:enter(K, L ++ [Item], D)
end
end,
DiffD = lists:foldl(fun(K, D) ->
gb_trees:enter(K, [], D)
end, gb_trees:empty(), lists:usort(Keys_old++Keys_new)),
DiffD2 = lists:foldl(Append(old), DiffD, Keys_old),
DiffD3 = lists:foldl(Append(new), DiffD2, Keys_new),
%% DiffD3 values will be exactly one of: [old], [old,new], [new]
put(final, []),
Add = fun(X) -> put(final, [X|get(final)]) end,
%% Find all new FLUs and define them.
[begin
{flu, Name, Host, _Port, _Ps} = V,
if Host == RelativeHost; Host == any ->
{ok, P_srvr} = d_find({kv,{p_srvr,Name}}, E),
Add(P_srvr)
end
end || {{kv,{flu,Name}}, V} <- New_list],
%% Find new chains on this host and define them.
%% Find modified chains on this host and re-define them.
[begin
{chain, Name, CMode, AllList, Witnesses, Props} = V,
FLUsA = [d_get({kv,{flu,FLU}}, E) || FLU <- AllList],
FLUsW = [d_get({kv,{flu,FLU}}, E) || FLU <- Witnesses],
TheFLU_Hosts =
[Host || {flu, _Name, Host, _Port, _Ps} <- FLUsA ++ FLUsW],
case (lists:member(RelativeHost, TheFLU_Hosts)
orelse RelativeHost == all) of
true ->
case gb_trees:lookup({kv,{chain,Name}}, KV_old) of
{value, OldT} ->
Add({yo,change,Name});
none ->
Add({yo,new,Name})
end;
false ->
ok
end
end || {{kv,{chain,Name}}, V} <- New_list],
{DiffD3, lists:reverse(get(final))}.

View file

@ -190,10 +190,15 @@ ast_run_test() ->
R1 = [
{host, "localhost", "localhost", "localhost", []},
{flu, "f0", "localhost", PortBase+0, []},
switch_old_and_new,
{flu, "f1", "localhost", PortBase+1, []},
{chain, "ca", ["f0"], []},
{chain, "cb", ["f1"], []},
switch_old_and_new,
{flu, "f2", "localhost", PortBase+2, []},
{chain, "ca", ["f0", "f1", "f2"], []}
{flu, "f3", "localhost", PortBase+3, []},
{flu, "f4", "localhost", PortBase+4, []},
{chain, "ca", ["f0", "f2"], []},
{chain, "cc", ["f3", "f4"], []}
],
{ok, Env1} = machi_lifecycle_mgr:run_ast(R1),
@ -209,7 +214,7 @@ ast_run_test() ->
{flu, "f1", "other", PortBase+888, []}, % dupe flu name
{flu, "f7", "localhost", PortBase+1, []}, % dupe host+port
{chain, "ca", ["f7"], []}, % unknown flu
{chain, "cb", ["f0"], []}, % flu previously assigned
{chain, "cc", ["f0"], []}, % flu previously assigned
{chain, "ca", cp_mode, ["f0", "f1", "f2"], [], []} % mode change
],
[begin
@ -217,9 +222,10 @@ ast_run_test() ->
{error, _} = machi_lifecycle_mgr:run_ast(R1 ++ [Neg])
end || Neg <- Negative_after_R1],
%% %% The 'run' phase doesn't blow smoke. What about 'diff'.
%% {ok, X2} = machi_lifecycle_mgr:diff_env(Env1),
%% io:format(user, "X2: ~p\n", [X2]),
%% The 'run' phase doesn't blow smoke. What about 'diff'?
{X2a, X2b} = machi_lifecycle_mgr:diff_env(Env1, "localhost"),
io:format(user, "X2a: ~p\n", [gb_trees:to_list(X2a)]),
io:format(user, "X2b: ~p\n", [X2b]),
ok.