Skip to content

Commit e3dd8bf

Browse files
committed
Allow setting sname and cookie to enable distribution.
1 parent 5c223cb commit e3dd8bf

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/ierl_cmd_install.erl

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ opt_spec() ->
1111
"Install a Jupyter kernelspec for this backend",
1212
[
1313
{name, $n, "name", string, "Install the kernel under this name "
14-
"(defaults to the backend name"
14+
"(defaults to the backend name)"
1515
},
1616
{copy, $c, "copy", boolean, "Copy the escript to make a standalone "
1717
"kernel"},
18-
{node, undefined, "node", string, "Remote node to run against"}
18+
{node, undefined, "node", string, "Remote node to run against"},
19+
{sname, undefined, "sname", string, "Short name for this node "
20+
"(defaults to the backend name"},
21+
{cookie, undefined, "cookie", string, "Cookie"}
1922
]
2023
}.
2124

@@ -24,20 +27,18 @@ exec({BName, Backend}, ParsedArgs, _Rest) ->
2427
application:ensure_all_started(jsx),
2528

2629
% TODO: Default to BName_Node
27-
2830
Name = proplists:get_value(name, ParsedArgs, BName),
2931
Copy = proplists:get_value(copy, ParsedArgs, false),
3032

31-
Args = #{ copy => Copy },
33+
Args = #{ copy => Copy, args => [] },
3234

33-
Args1 = case proplists:get_value(node, ParsedArgs, undefined) of
34-
undefined ->
35-
Args;
36-
Val ->
37-
Args#{
38-
args => [<<"--node">>, jup_util:ensure_binary(Val)]
39-
}
40-
end,
35+
Args1 = lists:foldl(
36+
fun (PName, A) ->
37+
forward_arg(PName, ParsedArgs, A)
38+
end,
39+
Args,
40+
[node, sname, cookie]
41+
),
4142

4243
% TODO Parse rest and add to spec
4344

@@ -46,3 +47,16 @@ exec({BName, Backend}, ParsedArgs, _Rest) ->
4647
io:format("Built kernel spec, storing~n"),
4748
ierl_kernelspec:write(Name, Spec),
4849
io:format("Installed kernel ~s with backend ~s~n", [Name, Backend]).
50+
51+
52+
forward_arg(Name, ParsedArgs, Args) ->
53+
case proplists:get_value(Name, ParsedArgs, undefined) of
54+
undefined ->
55+
Args;
56+
Val ->
57+
Param = jup_util:ensure_binary(io_lib:format("--~p", [Name])),
58+
Args#{
59+
args => maps:get(args, Args, [])
60+
++ [Param, jup_util:ensure_binary(Val)]
61+
}
62+
end.

src/ierl_cmd_run.erl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ opt_spec() ->
1212
[
1313
{conn_file, $f, "conn-file", string, "Connection file provided by"
1414
" Jupyter"},
15-
{node, undefined, "node", string, "Node to run this kernel on"}
15+
{node, undefined, "node", string, "Node to run this kernel on"},
16+
{sname, undefined, "sname", string, "Short name for this node "
17+
"(defaults to the backend name"},
18+
{cookie, undefined, "cookie", string, "Cookie"}
1619
]
1720
}.
1821

@@ -33,6 +36,23 @@ exec({BName, Backend}, ParsedArgs, Rest) ->
3336
list_to_atom(Val)
3437
end,
3538

39+
SName = proplists:get_value(sname, ParsedArgs, atom_to_list(BName)),
40+
41+
SName1 = binary_to_atom(
42+
list_to_binary(
43+
[SName, "_", base64:encode(crypto:strong_rand_bytes(6))]
44+
),
45+
utf8
46+
),
47+
48+
net_kernel:start([SName1, shortnames]),
49+
lager:info("Set node name to ~p", [SName1]),
50+
51+
case proplists:get_value(cookie, ParsedArgs, undefined) of
52+
undefined -> ok;
53+
Val1 -> erlang:setcookie(node(), list_to_atom(Val1))
54+
end,
55+
3656
lager:info("Starting Erlang kernel with connection file ~s against ~p",
3757
[JsonFile, Node]),
3858

0 commit comments

Comments
 (0)