This commit is contained in:
anlicheng 2025-09-17 10:24:49 +08:00
parent b20127ced9
commit fcb43d1792
4 changed files with 62 additions and 30 deletions

View File

@ -11,7 +11,7 @@
%% API %% API
-export([pull_image/1, check_image_exist/1]). -export([pull_image/1, check_image_exist/1]).
-export([create_container/3, check_container_exist/1, is_container_running/1]). -export([create_container/3, check_container_exist/1, is_container_running/1, start_container/1, stop_container/1]).
-spec pull_image(Image :: binary()) -> ok | {error, Reason :: any()}. -spec pull_image(Image :: binary()) -> ok | {error, Reason :: any()}.
pull_image(Image) when is_binary(Image) -> pull_image(Image) when is_binary(Image) ->
@ -34,6 +34,25 @@ pull_image(Image) when is_binary(Image) ->
{error, <<"exec command startup failed">>} {error, <<"exec command startup failed">>}
end. end.
-spec check_image_exist(Image :: binary()) -> boolean().
check_image_exist(Image) when is_binary(Image) ->
PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker images -q " ++ binary_to_list(Image),
lager:debug("cmd : ~p", [ExecCmd]),
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) ->
case gather_output(Port) of
{0, <<>>} ->
false;
{0, <<_:12/binary, $\n>>} ->
true;
{_ExitCode, _Error} ->
false
end;
_Error ->
false
end.
-spec create_container(ContainerName :: binary(), ContainerDir :: string(), Config :: map()) -> {ok, ContainerId :: binary()} | {error, Reason :: any()}. -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) -> create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerName), is_list(ContainerDir), is_map(Config) ->
Image = maps:get(<<"image">>, Config), Image = maps:get(<<"image">>, Config),
@ -83,17 +102,15 @@ is_container_running(ContainerId) when is_binary(ContainerId) ->
false false
end. end.
-spec check_image_exist(Image :: binary()) -> boolean(). -spec check_container_exist(ContainerName :: binary()) -> boolean().
check_image_exist(Image) when is_binary(Image) -> check_container_exist(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker images -q " ++ binary_to_list(Image), ExecCmd = "docker inspect --type=container " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1",
lager:debug("cmd : ~p", [ExecCmd]), lager:debug("check_container_exist cmd : ~p", [ExecCmd]),
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of
{0, <<>>} -> {0, _} ->
false;
{0, <<_:12/binary, $\n>>} ->
true; true;
{_ExitCode, _Error} -> {_ExitCode, _Error} ->
false false
@ -102,11 +119,26 @@ check_image_exist(Image) when is_binary(Image) ->
false false
end. end.
-spec check_container_exist(ContainerName :: binary()) -> boolean(). -spec start_container(ContainerName :: binary()) -> boolean().
check_container_exist(ContainerName) when is_binary(ContainerName) -> start_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker inspect --type=container " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", ExecCmd = "docker start " ++ binary_to_list(ContainerName),
lager:debug("check_container_exist cmd : ~p", [ExecCmd]), 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
end.
-spec stop_container(ContainerName :: binary()) -> boolean().
stop_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker stop " ++ binary_to_list(ContainerName),
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
%%% Created : 07. 5 2025 15:47 %%% Created : 07. 5 2025 15:47
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(docker_deploy). -module(docker_deployer).
-author("anlicheng"). -author("anlicheng").
-include("efka_tables.hrl"). -include("efka_tables.hrl").

View File

@ -8,7 +8,7 @@
%%% @end %%% @end
%%% Created : 19. 4 2025 14:55 %%% Created : 19. 4 2025 14:55
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(docker_inetd). -module(docker_manager).
-author("anlicheng"). -author("anlicheng").
-include("message_pb.hrl"). -include("message_pb.hrl").
@ -77,8 +77,8 @@ init([]) ->
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_call({deploy, TaskId, Config}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) -> handle_call({deploy, TaskId, Config}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) ->
%% %%
{ok, TaskPid} = efka_inetd_task:start_link(TaskId, RootDir, Config), {ok, TaskPid} = docker_deployer:start_link(TaskId, RootDir, Config),
efka_inetd_task:deploy(TaskPid), docker_deployer:deploy(TaskPid),
lager:debug("[efka_inetd] start deploy task_id: ~p, config: ~p", [TaskId, Config]), lager:debug("[efka_inetd] start deploy task_id: ~p, config: ~p", [TaskId, Config]),
{reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}}; {reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}};

View File

@ -29,12 +29,12 @@ init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
ChildSpecs = [ ChildSpecs = [
#{ #{
id => 'efka_docker_events', id => 'docker_events',
start => {'efka_docker_events', start_link, []}, start => {'docker_events', start_link, []},
restart => permanent, restart => permanent,
shutdown => 2000, shutdown => 2000,
type => worker, type => worker,
modules => ['efka_docker_events'] modules => ['docker_events']
}, },
#{ #{
@ -46,14 +46,14 @@ init([]) ->
modules => ['efka_model_sup'] modules => ['efka_model_sup']
}, },
#{ %#{
id => 'efka_inetd_task_log', % id => 'efka_inetd_task_log',
start => {'efka_inetd_task_log', start_link, []}, % start => {'efka_inetd_task_log', start_link, []},
restart => permanent, % restart => permanent,
shutdown => 2000, % shutdown => 2000,
type => worker, % type => worker,
modules => ['efka_inetd_task_log'] % modules => ['efka_inetd_task_log']
}, %},
#{ #{
id => 'efka_subscription', id => 'efka_subscription',
@ -65,12 +65,12 @@ init([]) ->
}, },
#{ #{
id => 'efka_inetd', id => 'docker_manager',
start => {'efka_inetd', start_link, []}, start => {'docker_manager', start_link, []},
restart => permanent, restart => permanent,
shutdown => 2000, shutdown => 2000,
type => worker, type => worker,
modules => ['efka_inetd'] modules => ['docker_manager']
} }
%#{ %#{