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]) ->
ok = valid_realm(Realm),
list_packages(Realm);
start(["list", "versions", Package]) ->
start(["list", "versions", PackageName]) ->
Package = string_to_package(PackageName),
list_versions(Package);
start(["list", "pending", PackageName]) ->
@ -118,8 +118,7 @@ start(["reject", PackageString]) ->
PackageID = package_id(PackageString),
reject(PackageID);
start(["resign", PackageString]) ->
PackageID = package_id(PackageString),
resign(PackageID);
resign(PackageString);
start(["drop", "dep", PackageString]) ->
PackageID = package_id(PackageString),
drop_dep(PackageID);
@ -610,7 +609,7 @@ list_packages(Realm) ->
{ok, Packages} ->
Print = fun({R, N}) -> io:format("~ts-~ts~n", [R, N]) end,
ok = lists:foreach(Print, Packages),
halt(0);
halt(0)
end.
@ -719,7 +718,7 @@ valid_package(Bad) ->
%% Convert a string to a package() type if possible. If not then halt the system.
string_to_package(String) ->
case string:lexemes(Package, "-") of
case string:lexemes(String, "-") of
[Realm, Name] ->
Package = {Realm, Name},
ok = valid_package(Package),
@ -837,11 +836,11 @@ add_maintainer(Package, UserName) ->
review(PackageString) ->
PackageID = package_id(PackageString),
Socket = connect_auth_or_die(),
PackageID = {Realm, _, _} = package_id(PackageString),
Socket = connect_auth_or_die(Realm),
ok = send(Socket, {review, PackageID}),
sending = recv_or_die(Socket),
{ok, ZrpBin} = recv_or_die(Socket),
ok = send(Socket, ok),
ok = disconnect(Socket),
{ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]),
{"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files),
@ -857,7 +856,7 @@ review(PackageString) ->
ok ->
log(info, "Will unpack to directory ./~ts", [PackageString]);
{error, Error} ->
Message = "Creating dir ./~ts failed with ~ts. Aborting."
Message = "Creating dir ./~ts failed with ~ts. Aborting.",
ok = log(error, Message, [PackageString, Error]),
halt(1)
end,
@ -886,7 +885,6 @@ resign(PackageString) ->
PackageID = {Realm, _, _} = package_id(PackageString),
Socket = connect_auth_or_die(Realm),
ok = send(Socket, {resign, PackageID}),
sending = recv_or_die(Socket),
{ok, ZrpBin} = recv_or_die(Socket),
{ok, Files} = erl_tar:extract({binary, ZrpBin}, [memory]),
{"zomp.meta", MetaBin} = lists:keyfind("zomp.meta", 1, Files),
@ -897,22 +895,24 @@ resign(PackageString) ->
TgzFile = PackageString ++ ".tgz",
{TgzFile, TgzData} = lists:keyfind(TgzFile, 1, Files),
ok = verify(TgzData, Signature, PubKey),
RealmConf = read_realm_conf(Realm),
RealmConf = load_realm_conf(Realm),
{package_keys, PackageKeys} = lists:keyfind(package_keys, 1, RealmConf),
PackageKeyName = select(PackageKeys),
PackageKeyID = {Realm, PackageKeyName}
KeySelection = [{K, {R, K}} || {R, K} <- [element(1, K) || K <- PackageKeys]],
PackageKeyID = select(KeySelection),
{ok, PackageKey} = loadkey(private, PackageKeyID),
ReSignature = public_key:sign(TgzData, sha512, PackageKey),
FinalMeta = maps:put(sig, {PackageKeyID, ReSignature}),
NewFiles = lists:keystore("zomp.meta", 1, term_to_binary(FinalMeta)),
ResignedZrp = PackageString ++ ".zrp",
FinalMeta = maps:put(sig, {PackageKeyID, ReSignature}, Meta),
NewMetaBin = term_to_binary(FinalMeta),
NewFiles = lists:keystore("zomp.meta", 1, Files, {"zomp.meta", NewMetaBin}),
ResignedZrp = PackageString ++ ".zrp.resign",
ok = erl_tar:create(ResignedZrp, NewFiles),
{ok, ResignedBin} = file:read_file(ResignedFile),
ok = send(Socket, {resigned, ResignedBin}),
{ok, ResignedBin} = file:read_file(ResignedZrp),
ok = gen_tcp:send(Socket, ResignedBin),
ok = recv_or_die(Socket),
ok = file:delete(ResignedZrp),
ok = recv_or_die(Socket),
ok = disconnect(Socket),
ok = log(info, M, [PackageID]),
ok = log(info, "Resigned ~ts", [PackageString]),
halt(0).
@ -1281,34 +1281,11 @@ submit(PackageFile) ->
{Realm, Package, Version} = maps:get(package_id, Meta),
{ok, Socket} = connect_auth(Realm),
ok = send(Socket, {submit, {Realm, Package, Version}}),
ok =
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 = recv_or_die(Socket),
ok = gen_tcp:send(Socket, PackageData),
ok = log(info, "Done sending contents of ~tp", [PackageFile]),
ok =
receive
{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,
Outcome = recv_or_die(Socket),
log(info, "Response: ~tp", [Outcome]),
ok = disconnect(Socket),
halt(0).
@ -1331,17 +1308,17 @@ send(Socket, Message) ->
recv_or_die(Socket) ->
receive
{tcp, Socket, Bin} ->
case binary_to_term(Bin, [safe]) of
case binary_to_term(Bin) of
ok ->
ok;
{ok, Response} ->
{ok, Response};
{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);
{error, bad_package} ->
ok = log(warning, "No such package.").
halt(1)
ok = log(warning, "No such package."),
halt(1);
{error, bad_version} ->
ok = log(warning, "No such version."),
halt(1);
@ -1350,6 +1327,16 @@ recv_or_die(Socket) ->
halt(1);
{error, bad_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)
end;
{tcp_closed, Socket} ->
@ -2046,6 +2033,19 @@ dialyze() ->
%%% 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().
%% @private
%% 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.",
error_exit(Message, [FileName], ?FILE, ?LINE);
{error, Reason} ->
Message = "Extracting package file failed with: ~tp.",
Message = "Extracting package file failed with: ~160tp.",
error_exit(Message, [Reason], ?FILE, ?LINE)
end.