Compare commits

..

No commits in common. "deeffc09d72dbc39f2a7e3f71335bb7630f85c20" and "2ec28bfc6e5609ed10aedc6ec4a863ba60e4c7b8" have entirely different histories.

View File

@ -21,7 +21,7 @@ test() ->
test_create_container() ->
M = #{
<<"image">> => <<"docker.1ms.run/library/nginx:latest">>,
<<"container_name">> => <<"my_nginx_new1">>,
<<"container_name">> => <<"my_nginx_new">>,
<<"command">> => [
<<"nginx">>,
<<"-g">>,
@ -96,7 +96,7 @@ test_create_container() ->
<<"retries">> => 3
}
},
create_container(<<"my_nginx_xx3">>, "/usr/local/code/efka/", M).
create_container(<<"my_nginx_xx">>, "/usr/local/code/efka/", M).
-spec pull_image(Image :: binary(), Callback :: fun((Msg :: binary()) -> no_return())) -> ok | {error, ExitCode :: integer()}.
pull_image(Image, Callback) when is_binary(Image), is_function(Callback, 1) ->
@ -117,33 +117,22 @@ check_image_exist(Image) when is_binary(Image) ->
-spec create_container(ContainerName :: binary(), ContainerDir :: string(), Config :: map()) -> {ok, ContainerId :: binary()} | {error, Reason :: any()}.
create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerName), is_list(ContainerDir), is_map(Config) ->
Url = io_lib:format("/containers/create?name=~s", [binary_to_list(ContainerName)]),
Image = maps:get(<<"image">>, Config),
%%
BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)),
BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/usr/local/etc/">>],
Options = build_options(Config),
%%
Volumes0 = maps:get(<<"volumes">>, Config, []),
Volumes = [<<BinContainerDir/binary, ":/usr/local/etc/">>|Volumes0],
NewConfig = Config#{<<"volumes">> => Volumes},
lists:foreach(fun({K, V}) ->
lager:debug("~p => ~p", [K, V])
end, maps:to_list(Options)),
Options = build_options(NewConfig),
lists:foreach(fun({K, V}) -> lager:debug("~p => ~p", [K, V]) end, maps:to_list(Options)),
Body = iolist_to_binary(jiffy:encode(Options, [force_utf8])),
Headers = [
{<<"Content-Type">>, <<"application/json">>}
],
case docker_http:request("POST", Url, Body, Headers) of
{ok, 201, _Headers, Resp} ->
#{<<"Id">> := ContainerId} = jiffy:decode(Resp, [return_maps]),
{ok, ContainerId};
{ok, _StatusCode, _, ErrorResp} ->
case catch jiffy:decode(ErrorResp, [return_maps]) of
#{<<"message">> := Msg} ->
{error, Msg};
_ ->
{error, ErrorResp}
end
end.
Res = docker_http:request("POST", Url, Body, []),
lager:debug("res is: ~p", [Res]).
-spec is_container_running(ContainerId :: binary()) -> boolean().
is_container_running(ContainerId) when is_binary(ContainerId) ->
@ -228,21 +217,14 @@ 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),
build_cap_add_drop(Config),
@ -251,10 +233,20 @@ 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) ->
@ -266,28 +258,26 @@ build_expose(Config) ->
case Ports of
[] -> #{};
_ ->
maps:from_list([{<<P/binary, "/tcp">>, #{}} || P <- Ports])
Exposed = maps:from_list([{<<P/binary, "/tcp">>, #{}} || P <- Ports]),
#{<<"ExposedPorts">> => Exposed}
end.
build_volumes(Config) ->
Vols = maps:get(<<"volumes">>, Config, []),
case Vols of
[] ->
#{};
[] -> #{};
_ ->
maps:from_list(lists:map(fun(V) ->
HostBinds = Vols,
VolMap = maps:from_list(lists:map(fun(V) ->
[_Host, Cont] = binary:split(V, <<":">>, []),
{Cont, #{}}
end, Vols))
end.
build_binds(Config) ->
Vols = maps:get(<<"volumes">>, Config, []),
case Vols of
[] ->
#{};
_ ->
#{<<"Binds">> => Vols}
end, Vols)),
#{
<<"HostConfig">> => #{
<<"Binds">> => HostBinds
},
<<"Volumes">> => VolMap
}
end.
build_networks(Config) ->
@ -296,9 +286,40 @@ build_networks(Config) ->
[] -> #{};
_ ->
NetCfg = maps:from_list([{N, #{}} || N <- Nets]),
#{<<"EndpointsConfig">> => NetCfg}
#{<<"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
@ -312,18 +333,20 @@ 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) ->
@ -341,6 +364,23 @@ parse_duration(Bin) ->
end.
%% --- ---
build_volumes(Config) ->
Vols = maps:get(<<"volumes">>, Config, []),
case Vols of
[] -> #{};
_ ->
HostBinds = Vols,
VolMap = maps:from_list(lists:map(fun(V) ->
[_Host, Cont] = binary:split(V, <<":">>, []),
{Cont, #{}}
end, Vols)),
#{
<<"HostConfig">> => #{
<<"Binds">> => HostBinds
},
<<"Volumes">> => VolMap
}
end.
build_restart(Config) ->
case maps:get(<<"restart">>, Config, undefined) of