fix
This commit is contained in:
parent
4eea5eb880
commit
aff440648a
@ -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.
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|||||||
@ -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),
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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).
|
||||||
Loading…
x
Reference in New Issue
Block a user