fix build options
This commit is contained in:
parent
2ec28bfc6e
commit
df1741af8a
@ -217,13 +217,21 @@ gather_output(Port, Acc) ->
|
||||
|
||||
%% 构建最终 JSON Map
|
||||
build_options(Config) when is_map(Config) ->
|
||||
maps:merge(
|
||||
#{
|
||||
<<"Image">> => maps:get(<<"image">>, Config, <<>>),
|
||||
<<"Cmd">> => maps:get(<<"command">>, Config, []),
|
||||
<<"Entrypoint">> => maps:get(<<"entrypoint">>, Config, []),
|
||||
<<"Env">> => maps:get(<<"envs">>, Config, []),
|
||||
<<"Labels">> => maps:get(<<"labels">>, Config, #{}),
|
||||
<<"Volumes">> => build_volumes(Config),
|
||||
<<"User">> => maps:get(<<"user">>, Config, <<>>),
|
||||
<<"WorkingDir">> => maps:get(<<"working_dir">>, Config, <<>>),
|
||||
<<"Hostname">> => maps:get(<<"hostname">>, Config, <<>>),
|
||||
<<"ExposedPorts">> => build_expose(Config),
|
||||
<<"NetworkingConfig">> => build_networks(Config),
|
||||
<<"Healthcheck">> => build_healthcheck(Config),
|
||||
<<"HostConfig">> => fold_merge([
|
||||
build_binds(Config),
|
||||
build_volumes(Config),
|
||||
build_restart(Config),
|
||||
build_privileged(Config),
|
||||
@ -233,20 +241,10 @@ build_options(Config) when is_map(Config) ->
|
||||
build_cpu(Config),
|
||||
build_ulimits(Config),
|
||||
build_tmpfs(Config),
|
||||
build_sysctls(Config),
|
||||
build_extra_hosts(Config)
|
||||
])
|
||||
},
|
||||
fold_merge([
|
||||
build_expose(Config),
|
||||
build_volumes(Config),
|
||||
build_networks(Config),
|
||||
build_labels(Config),
|
||||
build_user(Config),
|
||||
build_working_dir(Config),
|
||||
build_hostname(Config),
|
||||
build_healthcheck(Config)
|
||||
])
|
||||
).
|
||||
}.
|
||||
|
||||
%% 工具函数
|
||||
fold_merge(List) ->
|
||||
@ -258,26 +256,28 @@ build_expose(Config) ->
|
||||
case Ports of
|
||||
[] -> #{};
|
||||
_ ->
|
||||
Exposed = maps:from_list([{<<P/binary, "/tcp">>, #{}} || P <- Ports]),
|
||||
#{<<"ExposedPorts">> => Exposed}
|
||||
maps:from_list([{<<P/binary, "/tcp">>, #{}} || P <- Ports])
|
||||
end.
|
||||
|
||||
build_volumes(Config) ->
|
||||
Vols = maps:get(<<"volumes">>, Config, []),
|
||||
case Vols of
|
||||
[] -> #{};
|
||||
[] ->
|
||||
#{};
|
||||
_ ->
|
||||
HostBinds = Vols,
|
||||
VolMap = maps:from_list(lists:map(fun(V) ->
|
||||
maps:from_list(lists:map(fun(V) ->
|
||||
[_Host, Cont] = binary:split(V, <<":">>, []),
|
||||
{Cont, #{}}
|
||||
end, Vols)),
|
||||
#{
|
||||
<<"HostConfig">> => #{
|
||||
<<"Binds">> => HostBinds
|
||||
},
|
||||
<<"Volumes">> => VolMap
|
||||
}
|
||||
end, Vols))
|
||||
end.
|
||||
|
||||
build_binds(Config) ->
|
||||
Vols = maps:get(<<"volumes">>, Config, []),
|
||||
case Vols of
|
||||
[] ->
|
||||
#{};
|
||||
_ ->
|
||||
#{<<"Binds">> => Vols}
|
||||
end.
|
||||
|
||||
build_networks(Config) ->
|
||||
@ -289,37 +289,6 @@ build_networks(Config) ->
|
||||
#{<<"NetworkingConfig">> => #{<<"EndpointsConfig">> => NetCfg}}
|
||||
end.
|
||||
|
||||
build_labels(Config) ->
|
||||
Labels = maps:get(<<"labels">>, Config, #{}),
|
||||
case maps:size(Labels) of
|
||||
0 -> #{};
|
||||
_ -> #{<<"Labels">> => Labels}
|
||||
end.
|
||||
|
||||
|
||||
build_user(Config) ->
|
||||
case maps:get(<<"user">>, Config, undefined) of
|
||||
undefined ->
|
||||
#{};
|
||||
U ->
|
||||
#{<<"User">> => U}
|
||||
end.
|
||||
|
||||
build_working_dir(Config) ->
|
||||
case maps:get(<<"working_dir">>, Config, undefined) of
|
||||
undefined ->
|
||||
#{};
|
||||
D ->
|
||||
#{<<"WorkingDir">> => D}
|
||||
end.
|
||||
|
||||
build_hostname(Config) ->
|
||||
case maps:get(<<"hostname">>, Config, undefined) of
|
||||
undefined ->
|
||||
#{};
|
||||
H ->
|
||||
#{<<"Hostname">> => H}
|
||||
end.
|
||||
|
||||
parse_mem(Val) ->
|
||||
case binary:last(Val) of
|
||||
@ -333,20 +302,18 @@ parse_mem(Val) ->
|
||||
list_to_integer(binary_to_list(Val))
|
||||
end.
|
||||
|
||||
|
||||
|
||||
build_healthcheck(Config) ->
|
||||
HC = maps:get(<<"healthcheck">>, Config, #{}),
|
||||
case maps:size(HC) of
|
||||
0 -> #{};
|
||||
0 ->
|
||||
#{};
|
||||
_ ->
|
||||
HCMap = #{
|
||||
#{
|
||||
<<"Test">> => maps:get(<<"test">>, HC, []),
|
||||
<<"Interval">> => parse_duration(maps:get(<<"interval">>, HC, <<"0s">>)),
|
||||
<<"Timeout">> => parse_duration(maps:get(<<"timeout">>, HC, <<"0s">>)),
|
||||
<<"Retries">> => maps:get(<<"retries">>, HC, 0)
|
||||
},
|
||||
#{<<"Healthcheck">> => HCMap}
|
||||
}
|
||||
end.
|
||||
|
||||
parse_duration(Bin) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user