diff --git a/apps/efka/src/docker/docker_commands.erl b/apps/efka/src/docker/docker_commands.erl index 6b0b940..6e439e5 100644 --- a/apps/efka/src/docker/docker_commands.erl +++ b/apps/efka/src/docker/docker_commands.erl @@ -11,7 +11,7 @@ %% API -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()}. pull_image(Image) when is_binary(Image) -> @@ -34,6 +34,25 @@ pull_image(Image) when is_binary(Image) -> {error, <<"exec command startup failed">>} 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()}. create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerName), is_list(ContainerDir), is_map(Config) -> Image = maps:get(<<"image">>, Config), @@ -83,17 +102,15 @@ is_container_running(ContainerId) when is_binary(ContainerId) -> false end. --spec check_image_exist(Image :: binary()) -> boolean(). -check_image_exist(Image) when is_binary(Image) -> +-spec check_container_exist(ContainerName :: binary()) -> boolean(). +check_container_exist(ContainerName) when is_binary(ContainerName) -> PortSettings = [stream, exit_status, use_stdio, binary], - ExecCmd = "docker images -q " ++ binary_to_list(Image), - lager:debug("cmd : ~p", [ExecCmd]), + ExecCmd = "docker inspect --type=container " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", + 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, <<>>} -> - false; - {0, <<_:12/binary, $\n>>} -> + {0, _} -> true; {_ExitCode, _Error} -> false @@ -102,11 +119,26 @@ check_image_exist(Image) when is_binary(Image) -> false end. --spec check_container_exist(ContainerName :: binary()) -> boolean(). -check_container_exist(ContainerName) when is_binary(ContainerName) -> +-spec start_container(ContainerName :: binary()) -> boolean(). +start_container(ContainerName) when is_binary(ContainerName) -> PortSettings = [stream, exit_status, use_stdio, binary], - ExecCmd = "docker inspect --type=container " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", - lager:debug("check_container_exist cmd : ~p", [ExecCmd]), + ExecCmd = "docker start " ++ binary_to_list(ContainerName), + 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 Port when is_port(Port) -> case gather_output(Port) of diff --git a/apps/efka/src/docker/docker_deploy.erl b/apps/efka/src/docker/docker_deployer.erl similarity index 99% rename from apps/efka/src/docker/docker_deploy.erl rename to apps/efka/src/docker/docker_deployer.erl index 296db6f..e6440ce 100644 --- a/apps/efka/src/docker/docker_deploy.erl +++ b/apps/efka/src/docker/docker_deployer.erl @@ -6,7 +6,7 @@ %%% @end %%% Created : 07. 5月 2025 15:47 %%%------------------------------------------------------------------- --module(docker_deploy). +-module(docker_deployer). -author("anlicheng"). -include("efka_tables.hrl"). diff --git a/apps/efka/src/docker/docker_inetd.erl b/apps/efka/src/docker/docker_manager.erl similarity index 98% rename from apps/efka/src/docker/docker_inetd.erl rename to apps/efka/src/docker/docker_manager.erl index 8e4dae9..6ea39e8 100644 --- a/apps/efka/src/docker/docker_inetd.erl +++ b/apps/efka/src/docker/docker_manager.erl @@ -8,7 +8,7 @@ %%% @end %%% Created : 19. 4月 2025 14:55 %%%------------------------------------------------------------------- --module(docker_inetd). +-module(docker_manager). -author("anlicheng"). -include("message_pb.hrl"). @@ -77,8 +77,8 @@ init([]) -> {stop, Reason :: term(), NewState :: #state{}}). handle_call({deploy, TaskId, Config}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) -> %% 创建目录 - {ok, TaskPid} = efka_inetd_task:start_link(TaskId, RootDir, Config), - efka_inetd_task:deploy(TaskPid), + {ok, TaskPid} = docker_deployer:start_link(TaskId, RootDir, Config), + docker_deployer:deploy(TaskPid), lager:debug("[efka_inetd] start deploy task_id: ~p, config: ~p", [TaskId, Config]), {reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}}; diff --git a/apps/efka/src/efka_sup.erl b/apps/efka/src/efka_sup.erl index 7bead74..574d285 100644 --- a/apps/efka/src/efka_sup.erl +++ b/apps/efka/src/efka_sup.erl @@ -29,12 +29,12 @@ init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, ChildSpecs = [ #{ - id => 'efka_docker_events', - start => {'efka_docker_events', start_link, []}, + id => 'docker_events', + start => {'docker_events', start_link, []}, restart => permanent, shutdown => 2000, type => worker, - modules => ['efka_docker_events'] + modules => ['docker_events'] }, #{ @@ -46,14 +46,14 @@ init([]) -> modules => ['efka_model_sup'] }, - #{ - id => 'efka_inetd_task_log', - start => {'efka_inetd_task_log', start_link, []}, - restart => permanent, - shutdown => 2000, - type => worker, - modules => ['efka_inetd_task_log'] - }, + %#{ + % id => 'efka_inetd_task_log', + % start => {'efka_inetd_task_log', start_link, []}, + % restart => permanent, + % shutdown => 2000, + % type => worker, + % modules => ['efka_inetd_task_log'] + %}, #{ id => 'efka_subscription', @@ -65,12 +65,12 @@ init([]) -> }, #{ - id => 'efka_inetd', - start => {'efka_inetd', start_link, []}, + id => 'docker_manager', + start => {'docker_manager', start_link, []}, restart => permanent, shutdown => 2000, type => worker, - modules => ['efka_inetd'] + modules => ['docker_manager'] } %#{