fix docker commands

This commit is contained in:
anlicheng 2025-09-23 17:20:10 +08:00
parent 24bf90778c
commit 743ed8813e
3 changed files with 50 additions and 57 deletions

View File

@ -27,6 +27,8 @@ receive_response(ConnPid, StreamRef) ->
receive receive
{gun_response, ConnPid, StreamRef, nofin, Status, Headers} -> {gun_response, ConnPid, StreamRef, nofin, Status, Headers} ->
receive_body(ConnPid, StreamRef, Status, Headers, <<>>); receive_body(ConnPid, StreamRef, Status, Headers, <<>>);
{gun_response, ConnPid, StreamRef, fin, Status, Headers} ->
{ok, Status, Headers, <<>>};
{gun_down, ConnPid, _, Reason, _} -> {gun_down, ConnPid, _, Reason, _} ->
{error, {http_closed, Reason}} {error, {http_closed, Reason}}
after 5000 -> after 5000 ->

View File

@ -20,8 +20,10 @@ test() ->
test1() -> test1() ->
Id = <<"redpanda-console">>, Id = <<"redpanda-console">>,
is_container_running(Id). StopRes = stop_container(Id),
lager:debug("stop res: ~p", [StopRes]),
StartRes = start_container(Id),
lager:debug("start res: ~p", [StartRes]).
test_create_container() -> test_create_container() ->
M = #{ M = #{
@ -161,52 +163,51 @@ is_container_running(ContainerId) when is_binary(ContainerId) ->
-spec check_container_exist(ContainerName :: binary()) -> boolean(). -spec check_container_exist(ContainerName :: binary()) -> boolean().
check_container_exist(ContainerName) when is_binary(ContainerName) -> check_container_exist(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], case inspect_container(ContainerName) of
ExecCmd = "docker inspect --type=container " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", {ok, #{<<"Id">> := Id}} when is_binary(Id) ->
lager:debug("check_container_exist cmd : ~p", [ExecCmd]), true;
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of _ ->
Port when is_port(Port) ->
case gather_output(Port) of
{0, _} ->
true;
{_ExitCode, _Error} ->
false
end;
_Error ->
false false
end. end.
-spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}. -spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
start_container(ContainerName) when is_binary(ContainerName) -> start_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], Url = lists:flatten(io_lib:format("/containers/~s/start", [binary_to_list(ContainerName)])),
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " 2>&1", Headers = [
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of {<<"Content-Type">>, <<"application/json">>}
Port when is_port(Port) -> ],
case gather_output(Port) of case docker_http:request("POST", Url, undefined, Headers) of
{0, _} -> {ok, 204, _Headers, _} ->
ok; ok;
{_ExitCode, Error} -> {ok, 304, _Headers, _} ->
{error, Error} {error, <<"container already started">>};
end; {ok, _StatusCode, _Header, ErrorResp} ->
_Error -> case catch jiffy:decode(ErrorResp) of
{error, <<"启动失败"/utf8>>} #{<<"message">> := Msg} ->
{error, Msg};
_ ->
{error, ErrorResp}
end
end. end.
-spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}. -spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
stop_container(ContainerName) when is_binary(ContainerName) -> stop_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], Url = lists:flatten(io_lib:format("/containers/~s/stop", [binary_to_list(ContainerName)])),
ExecCmd = "docker stop " ++ binary_to_list(ContainerName) ++ " 2>&1", Headers = [
lager:debug("[docker_commands] cmd : ~p", [ExecCmd]), {<<"Content-Type">>, <<"application/json">>}
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of ],
Port when is_port(Port) -> case docker_http:request("POST", Url, undefined, Headers) of
case gather_output(Port) of {ok, 204, _Headers, _} ->
{0, _} -> ok;
ok; {ok, 304, _Headers, _} ->
{_ExitCode, Error} -> {error, <<"container already stopped">>};
{error, Error} {ok, _StatusCode, _Header, ErrorResp} ->
end; case catch jiffy:decode(ErrorResp) of
_Error -> #{<<"message">> := Msg} ->
false {error, Msg};
_ ->
{error, ErrorResp}
end
end. end.
-spec inspect_container(ContainerId :: binary()) -> {ok, Json :: map()} | {error, Error :: any()}. -spec inspect_container(ContainerId :: binary()) -> {ok, Json :: map()} | {error, Error :: any()}.
@ -232,16 +233,6 @@ inspect_container(ContainerId) when is_binary(ContainerId) ->
%%% helper methods %%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gather_output(Port) ->
gather_output(Port, <<>>).
gather_output(Port, Acc) ->
receive
{Port, {data, Data}} ->
gather_output(Port, [Acc, Data]);
{Port, {exit_status, Status}} ->
{Status, iolist_to_binary(Acc)}
end.
%% JSON Map %% JSON Map
build_options(Config) when is_map(Config) -> build_options(Config) when is_map(Config) ->
#{ #{

View File

@ -37,14 +37,14 @@ init([]) ->
modules => ['efka_service_sup'] modules => ['efka_service_sup']
}, },
#{ %#{
id => 'docker_events', % id => 'docker_events',
start => {'docker_events', start_link, []}, % start => {'docker_events', start_link, []},
restart => permanent, % restart => permanent,
shutdown => 2000, % shutdown => 2000,
type => worker, % type => worker,
modules => ['docker_events'] % modules => ['docker_events']
}, %},
#{ #{
id => 'efka_model_sup', id => 'efka_model_sup',