From deeffc09d72dbc39f2a7e3f71335bb7630f85c20 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 23 Sep 2025 16:41:46 +0800 Subject: [PATCH] fix udp commands --- apps/efka/src/docker/docker_uds_commands.erl | 55 +++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/apps/efka/src/docker/docker_uds_commands.erl b/apps/efka/src/docker/docker_uds_commands.erl index 283731f..926defd 100644 --- a/apps/efka/src/docker/docker_uds_commands.erl +++ b/apps/efka/src/docker/docker_uds_commands.erl @@ -21,7 +21,7 @@ test() -> test_create_container() -> M = #{ <<"image">> => <<"docker.1ms.run/library/nginx:latest">>, - <<"container_name">> => <<"my_nginx_new">>, + <<"container_name">> => <<"my_nginx_new1">>, <<"command">> => [ <<"nginx">>, <<"-g">>, @@ -96,7 +96,7 @@ test_create_container() -> <<"retries">> => 3 } }, - create_container(<<"my_nginx_xx">>, "/usr/local/code/efka/", M). + create_container(<<"my_nginx_xx3">>, "/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,22 +117,33 @@ 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">>, <>], - Options = build_options(Config), - lists:foreach(fun({K, V}) -> - lager:debug("~p => ~p", [K, V]) - end, maps:to_list(Options)), + %% 增加自定义的用来放配置文件的目录 + Volumes0 = maps:get(<<"volumes">>, Config, []), + Volumes = [<>|Volumes0], + NewConfig = Config#{<<"volumes">> => Volumes}, + 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])), - Res = docker_http:request("POST", Url, Body, []), - lager:debug("res is: ~p", [Res]). + 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. -spec is_container_running(ContainerId :: binary()) -> boolean(). is_container_running(ContainerId) when is_binary(ContainerId) -> @@ -232,7 +243,6 @@ build_options(Config) when is_map(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), @@ -286,7 +296,7 @@ build_networks(Config) -> [] -> #{}; _ -> NetCfg = maps:from_list([{N, #{}} || N <- Nets]), - #{<<"NetworkingConfig">> => #{<<"EndpointsConfig">> => NetCfg}} + #{<<"EndpointsConfig">> => NetCfg} end. @@ -331,23 +341,6 @@ 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