Basic repo function

This commit is contained in:
Craig Everett 2017-12-08 23:01:44 +09:00
parent 8d97664056
commit d9cd20a41f

100
zx
View File

@ -92,7 +92,7 @@ start(["list", "realms"]) ->
start(["list", "packages", Realm]) -> start(["list", "packages", Realm]) ->
ok = valid_realm(Realm), ok = valid_realm(Realm),
list_packages(Realm); list_packages(Realm);
start(["list", "versions", Package]) -> start(["list", "versions", PackageName]) ->
Package = string_to_package(PackageName), Package = string_to_package(PackageName),
list_versions(Package); list_versions(Package);
start(["list", "pending", PackageName]) -> start(["list", "pending", PackageName]) ->
@ -118,8 +118,7 @@ start(["reject", PackageString]) ->
PackageID = package_id(PackageString), PackageID = package_id(PackageString),
reject(PackageID); reject(PackageID);
start(["resign", PackageString]) -> start(["resign", PackageString]) ->
PackageID = package_id(PackageString), resign(PackageString);
resign(PackageID);
start(["drop", "dep", PackageString]) -> start(["drop", "dep", PackageString]) ->
PackageID = package_id(PackageString), PackageID = package_id(PackageString),
drop_dep(PackageID); drop_dep(PackageID);
@ -610,7 +609,7 @@ list_packages(Realm) ->
{ok, Packages} -> {ok, Packages} ->
Print = fun({R, N}) -> io:format("~ts-~ts~n", [R, N]) end, Print = fun({R, N}) -> io:format("~ts-~ts~n", [R, N]) end,
ok = lists:foreach(Print, Packages), ok = lists:foreach(Print, Packages),
halt(0); halt(0)
end. end.
@ -719,7 +718,7 @@ valid_package(Bad) ->
%% Convert a string to a package() type if possible. If not then halt the system. %% Convert a string to a package() type if possible. If not then halt the system.
string_to_package(String) -> string_to_package(String) ->
case string:lexemes(Package, "-") of case string:lexemes(String, "-") of
[Realm, Name] -> [Realm, Name] ->
Package = {Realm, Name}, Package = {Realm, Name},
ok = valid_package(Package), ok = valid_package(Package),
@ -837,11 +836,11 @@ add_maintainer(Package, UserName) ->
review(PackageString) -> review(PackageString) ->
PackageID = package_id(PackageString), PackageID = {Realm, _, _} = package_id(PackageString),
Socket = connect_auth_or_die(), Socket = connect_auth_or_die(Realm),
ok = send(Socket, {review, PackageID}), ok = send(Socket, {review, PackageID}),
sending = recv_or_die(Socket),
{ok, ZrpBin} = recv_or_die(Socket), {ok, ZrpBin} = recv_or_die(Socket),
ok = send(Socket, ok),
ok = disconnect(Socket), ok = disconnect(Socket),
{ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]), {ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]),
{"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files), {"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files),
@ -857,7 +856,7 @@ review(PackageString) ->
ok -> ok ->
log(info, "Will unpack to directory ./~ts", [PackageString]); log(info, "Will unpack to directory ./~ts", [PackageString]);
{error, Error} -> {error, Error} ->
Message = "Creating dir ./~ts failed with ~ts. Aborting." Message = "Creating dir ./~ts failed with ~ts. Aborting.",
ok = log(error, Message, [PackageString, Error]), ok = log(error, Message, [PackageString, Error]),
halt(1) halt(1)
end, end,
@ -886,7 +885,6 @@ resign(PackageString) ->
PackageID = {Realm, _, _} = package_id(PackageString), PackageID = {Realm, _, _} = package_id(PackageString),
Socket = connect_auth_or_die(Realm), Socket = connect_auth_or_die(Realm),
ok = send(Socket, {resign, PackageID}), ok = send(Socket, {resign, PackageID}),
sending = recv_or_die(Socket),
{ok, ZrpBin} = recv_or_die(Socket), {ok, ZrpBin} = recv_or_die(Socket),
{ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]), {ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]),
{"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files), {"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files),
@ -897,22 +895,24 @@ resign(PackageString) ->
TgzFile = PackageString ++ ".tgz", TgzFile = PackageString ++ ".tgz",
{TgzFile, TgzData} = lists:keyfind(TgzFile, 1, Files), {TgzFile, TgzData} = lists:keyfind(TgzFile, 1, Files),
ok = verify(TgzData, Signature, PubKey), ok = verify(TgzData, Signature, PubKey),
RealmConf = read_realm_conf(Realm), RealmConf = load_realm_conf(Realm),
{package_keys, PackageKeys} = lists:keyfind(package_keys, 1, RealmConf), {package_keys, PackageKeys} = lists:keyfind(package_keys, 1, RealmConf),
PackageKeyName = select(PackageKeys), KeySelection = [{K, {R, K}} || {R, K} <- [element(1, K) || K <- PackageKeys]],
PackageKeyID = {Realm, PackageKeyName} PackageKeyID = select(KeySelection),
{ok, PackageKey} = loadkey(private, PackageKeyID), {ok, PackageKey} = loadkey(private, PackageKeyID),
ReSignature = public_key:sign(TgzData, sha512, PackageKey), ReSignature = public_key:sign(TgzData, sha512, PackageKey),
FinalMeta = maps:put(sig, {PackageKeyID, ReSignature}), FinalMeta = maps:put(sig, {PackageKeyID, ReSignature}, Meta),
NewFiles = lists:keystore("zomp.meta", 1, term_to_binary(FinalMeta)), NewMetaBin = term_to_binary(FinalMeta),
ResignedZrp = PackageString ++ ".zrp", NewFiles = lists:keystore("zomp.meta", 1, Files, {"zomp.meta", NewMetaBin}),
ResignedZrp = PackageString ++ ".zrp.resign",
ok = erl_tar:create(ResignedZrp, NewFiles), ok = erl_tar:create(ResignedZrp, NewFiles),
{ok, ResignedBin} = file:read_file(ResignedFile), {ok, ResignedBin} = file:read_file(ResignedZrp),
ok = send(Socket, {resigned, ResignedBin}), ok = gen_tcp:send(Socket, ResignedBin),
ok = recv_or_die(Socket), ok = recv_or_die(Socket),
ok = file:delete(ResignedZrp), ok = file:delete(ResignedZrp),
ok = recv_or_die(Socket),
ok = disconnect(Socket), ok = disconnect(Socket),
ok = log(info, M, [PackageID]), ok = log(info, "Resigned ~ts", [PackageString]),
halt(0). halt(0).
@ -1281,34 +1281,11 @@ submit(PackageFile) ->
{Realm, Package, Version} = maps:get(package_id, Meta), {Realm, Package, Version} = maps:get(package_id, Meta),
{ok, Socket} = connect_auth(Realm), {ok, Socket} = connect_auth(Realm),
ok = send(Socket, {submit, {Realm, Package, Version}}), ok = send(Socket, {submit, {Realm, Package, Version}}),
ok = ok = recv_or_die(Socket),
receive
{tcp, Socket, RB1} ->
case binary_to_term(RB1) of
ready ->
ok;
{error, Reason} ->
ok = log(info, "Server refused with ~tp", [Reason]),
halt(0)
end;
{tcp_closed, Socket} ->
halt_on_unexpected_close()
after 5000 ->
ok = log(warning, "Server timed out!"),
halt(0)
end,
ok = gen_tcp:send(Socket, PackageData), ok = gen_tcp:send(Socket, PackageData),
ok = log(info, "Done sending contents of ~tp", [PackageFile]), ok = log(info, "Done sending contents of ~tp", [PackageFile]),
ok = Outcome = recv_or_die(Socket),
receive log(info, "Response: ~tp", [Outcome]),
{tcp, Socket, RB2} ->
Outcome = binary_to_term(RB2),
log(info, "Response: ~tp", [Outcome]);
{tcp_closed, Socket} ->
halt_on_unexpected_close()
after 5000 ->
log(warning, "Server timed out!")
end,
ok = disconnect(Socket), ok = disconnect(Socket),
halt(0). halt(0).
@ -1331,17 +1308,17 @@ send(Socket, Message) ->
recv_or_die(Socket) -> recv_or_die(Socket) ->
receive receive
{tcp, Socket, Bin} -> {tcp, Socket, Bin} ->
case binary_to_term(Bin, [safe]) of case binary_to_term(Bin) of
ok -> ok ->
ok; ok;
{ok, Response} -> {ok, Response} ->
{ok, Response}; {ok, Response};
{error, bad_realm} -> {error, bad_realm} ->
ok = log(warning, "No such realm at the connected node."). ok = log(warning, "No such realm at the connected node."),
halt(1); halt(1);
{error, bad_package} -> {error, bad_package} ->
ok = log(warning, "No such package."). ok = log(warning, "No such package."),
halt(1) halt(1);
{error, bad_version} -> {error, bad_version} ->
ok = log(warning, "No such version."), ok = log(warning, "No such version."),
halt(1); halt(1);
@ -1350,6 +1327,16 @@ recv_or_die(Socket) ->
halt(1); halt(1);
{error, bad_message} -> {error, bad_message} ->
ok = log(error, "Oh noes! zx sent an illegal message!"), ok = log(error, "Oh noes! zx sent an illegal message!"),
halt(1);
{error, already_exists} ->
ok = log(warning, "Server refuses: already_exists"),
halt(1);
{error, {system, Reason}} ->
Message = "Node experienced system error: ~160tp",
ok = log(warning, Message, [{error, Reason}]),
halt(1);
Unexpected ->
ok = log(warning, "Unexpected message: ~tp", [Unexpected]),
halt(1) halt(1)
end; end;
{tcp_closed, Socket} -> {tcp_closed, Socket} ->
@ -2046,6 +2033,19 @@ dialyze() ->
%%% Create Realm & Sysop %%% Create Realm & Sysop
-spec create_user(realm(), username()) -> no_return().
%% @private
%% Validate the realm and username provided, prompt the user to either select a keypair
%% to use or generate a new one, and bundle a .zuser file for conveyance of the user
%% data and his relevant keys (for import into an existing zomp server via `add'
%% command like "add packager", "add maintainer" and "add sysop".
create_user(Realm, Username) ->
Message = "Would be generating a user file for {~160tp, ~160to}.",
ok = log(info, Message, [Realm, Username]),
halt(0).
-spec create_realm() -> no_return(). -spec create_realm() -> no_return().
%% @private %% @private
%% Prompt the user to input the information necessary to create a new zomp realm, %% Prompt the user to input the information necessary to create a new zomp realm,
@ -2479,7 +2479,7 @@ extract_zrp(FileName) ->
Message = "~ts is not a valid zrp archive.", Message = "~ts is not a valid zrp archive.",
error_exit(Message, [FileName], ?FILE, ?LINE); error_exit(Message, [FileName], ?FILE, ?LINE);
{error, Reason} -> {error, Reason} ->
Message = "Extracting package file failed with: ~tp.", Message = "Extracting package file failed with: ~160tp.",
error_exit(Message, [Reason], ?FILE, ?LINE) error_exit(Message, [Reason], ?FILE, ?LINE)
end. end.