This commit is contained in:
anlicheng 2025-09-17 10:48:15 +08:00
parent 4eea5eb880
commit aff440648a
4 changed files with 27 additions and 36 deletions

View File

@ -119,7 +119,7 @@ check_container_exist(ContainerName) when is_binary(ContainerName) ->
false false
end. end.
-spec start_container(ContainerName :: binary()) -> boolean(). -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], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1",
@ -127,15 +127,15 @@ start_container(ContainerName) when is_binary(ContainerName) ->
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of
{0, _} -> {0, _} ->
true; ok;
{_ExitCode, _Error} -> {_ExitCode, _Error} ->
false {error, <<"启动失败"/utf8>>}
end; end;
_Error -> _Error ->
false {error, <<"启动失败"/utf8>>}
end. end.
-spec stop_container(ContainerName :: binary()) -> boolean(). -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], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker stop " ++ binary_to_list(ContainerName), ExecCmd = "docker stop " ++ binary_to_list(ContainerName),
@ -143,12 +143,12 @@ stop_container(ContainerName) when is_binary(ContainerName) ->
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of
{0, _} -> {0, _} ->
true; ok;
{_ExitCode, _Error} -> {_ExitCode, _Error} ->
false {error, <<>>}
end; end;
_Error -> _Error ->
false {error, <<>>}
end. end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -179,11 +179,11 @@ do_deploy(TaskId, RootDir, Config) when is_integer(TaskId), is_list(RootDir), is
%% envs参数 %% envs参数
%maybe_create_env_file(ContainerDir, maps:get(<<"envs">>, Config, [])), %maybe_create_env_file(ContainerDir, maps:get(<<"envs">>, Config, [])),
%% , : "/etc/容器名称/" %% , : "/etc/容器名称/"
case efka_docker_command:check_container_exist(ContainerName) of case docker_commands:check_container_exist(ContainerName) of
true -> true ->
{error, <<"container exist">>}; {error, <<"container exist">>};
false -> false ->
case efka_docker_command:create_container(ContainerName, ContainerDir, Config) of case docker_commands:create_container(ContainerName, ContainerDir, Config) of
{ok, ContainerId} -> {ok, ContainerId} ->
{ok, ContainerId}; {ok, ContainerId};
{error, Reason} -> {error, Reason} ->
@ -206,7 +206,6 @@ try_pull_image(Image) when is_binary(Image) ->
maybe_create_env_file(_ContainerDir, []) -> maybe_create_env_file(_ContainerDir, []) ->
ok; ok;
maybe_create_env_file(ContainerDir, Envs) when is_list(Envs)-> maybe_create_env_file(ContainerDir, Envs) when is_list(Envs)->
lager:debug("envs: ~p", [Envs]),
TargetFile = ContainerDir ++ "env", TargetFile = ContainerDir ++ "env",
{ok, IoDevice} = file:open(TargetFile, [write, binary]), {ok, IoDevice} = file:open(TargetFile, [write, binary]),
lists:foreach(fun(Env) -> file:write(IoDevice, <<Env/binary, $\n>>) end, Envs), lists:foreach(fun(Env) -> file:write(IoDevice, <<Env/binary, $\n>>) end, Envs),

View File

@ -84,30 +84,20 @@ handle_call({deploy, TaskId, Config}, _From, State = #state{root_dir = RootDir,
%% : %% :
handle_call({start_container, ContainerId}, _From, State) -> handle_call({start_container, ContainerId}, _From, State) ->
case efka_container:get_pid(ContainerId) of case docker_commands:start_container(ContainerId) of
undefined -> ok ->
case efka_container_sup:start_container(ContainerId) of {reply, ok, State};
{ok, _} -> {error, Reason} ->
%% , efka重启的时候 {reply, {error, Reason}, State}
ok = service_model:change_status(ContainerId, 1),
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
ContainerPid when is_pid(ContainerPid) ->
{reply, {error, <<"service is running">>}, State}
end; end;
%% , status字段 %% , status字段
handle_call({stop_container, ContainerId}, _From, State = #state{}) -> handle_call({stop_container, ContainerId}, _From, State = #state{}) ->
case efka_container:get_pid(ContainerId) of case docker_commands:stop_container(ContainerId) of
undefined -> ok ->
{reply, {error, <<"service not running">>}, State}; {reply, ok, State};
ContainerPid when is_pid(ContainerPid) -> {error, Reason} ->
efka_container_sup:stop_container(ContainerPid), {reply, {error, Reason}, State}
%% , efka重启的时候
ok = service_model:change_status(ContainerId, 0),
{reply, ok, State}
end; end;
handle_call(_Request, _From, State = #state{}) -> handle_call(_Request, _From, State = #state{}) ->
@ -136,11 +126,13 @@ handle_info({'EXIT', TaskPid, Reason}, State = #state{task_map = TaskMap}) ->
case Reason of case Reason of
normal -> normal ->
lager:debug("[efka_inetd] task_pid: ~p, exit normal", [TaskPid]), lager:debug("[efka_inetd] task_pid: ~p, exit normal", [TaskPid]),
efka_inetd_task_log:flush(TaskId); %efka_inetd_task_log:flush(TaskId);
ok;
Error -> Error ->
lager:notice("[efka_inetd] task_pid: ~p, exit with error: ~p", [TaskPid, Error]), lager:notice("[efka_inetd] task_pid: ~p, exit with error: ~p", [TaskPid, Error]),
efka_inetd_task_log:stash(TaskId, <<"task aborted">>), %efka_inetd_task_log:stash(TaskId, <<"task aborted">>),
efka_inetd_task_log:flush(TaskId) %efka_inetd_task_log:flush(TaskId)
ok
end, end,
{noreply, State#state{task_map = NTaskMap}} {noreply, State#state{task_map = NTaskMap}}
end; end;

View File

@ -2,11 +2,11 @@
%%% @author anlicheng %%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY> %%% @copyright (C) 2025, <COMPANY>
%%% @doc %%% @doc
%%% %%% TODO
%%% @end %%% @end
%%% Created : 09. 5 2025 16:45 %%% Created : 09. 5 2025 16:45
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(docker_deploy_log). -module(docker_task_log).
-author("anlicheng"). -author("anlicheng").
-behaviour(gen_server). -behaviour(gen_server).