fix deploy

This commit is contained in:
anlicheng 2025-09-24 17:19:35 +08:00
parent 8dd984401a
commit 7c1c1ed9af
3 changed files with 57 additions and 1 deletions

View File

@ -198,6 +198,16 @@ do_deploy(TaskId, ContainerDir, Config) when is_integer(TaskId), is_list(Contain
efka_remote_agent:task_event_stream(TaskId, <<"开始创建容器: "/utf8, ContainerName/binary>>),
case docker_commands:create_container(ContainerName, ContainerDir, Config) of
{ok, ContainerId} ->
%%
ConfigFile = docker_container_helper:get_config_file(ContainerDir),
case file:open(ConfigFile, [write, exclusive]) of
{ok, FD} ->
ok = file:write(FD, <<>>),
file:close(FD);
{error, Reason} ->
Reason1 = list_to_binary(io_lib:format("~p", [Reason])),
efka_remote_agent:task_event_stream(TaskId, <<"创建配置文件失败: "/utf8, Reason1/binary>>)
end,
ShortContainerId = binary:part(ContainerId, 1, 12),
efka_remote_agent:task_event_stream(TaskId, <<"容器创建成功: "/utf8, ShortContainerId/binary>>),
efka_remote_agent:task_event_stream(TaskId, <<"任务完成"/utf8>>);

View File

@ -15,7 +15,7 @@
%% API
-export([start_link/0]).
-export([deploy/2, start_container/1, stop_container/1, config_container/2]).
-export([deploy/2, start_container/1, stop_container/1, config_container/2, kill_container/1, remove_container/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -48,6 +48,14 @@ start_container(ContainerId) when is_binary(ContainerId) ->
stop_container(ContainerId) when is_binary(ContainerId) ->
gen_server:call(?SERVER, {stop_container, ContainerId}).
-spec kill_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
kill_container(ContainerId) when is_binary(ContainerId) ->
gen_server:call(?SERVER, {kill_container, ContainerId}).
-spec remove_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
remove_container(ContainerId) when is_binary(ContainerId) ->
gen_server:call(?SERVER, {remove_container, ContainerId}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
@ -123,6 +131,24 @@ handle_call({stop_container, ContainerId}, _From, State = #state{}) ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({kill_container, ContainerId}, _From, State = #state{}) ->
case docker_commands:kill_container(ContainerId) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({remove_container, ContainerId}, _From, State = #state{}) ->
case docker_commands:remove_container(ContainerId) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.

View File

@ -263,6 +263,26 @@ handle_event(info, {server_rpc, PacketId, #rpc_container{method = <<"stop">>, co
end,
{keep_state, State};
handle_event(info, {server_rpc, PacketId, #rpc_container{method = <<"kill">>, container_name = ContainerName}}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
%% efka_inetd收到消息后就立即返回了
case docker_manager:kill_container(ContainerName) of
ok ->
efka_transport:rpc_reply(TransportPid, PacketId, reply_success(<<"ok">>));
{error, Reason} when is_binary(Reason) ->
efka_transport:rpc_reply(TransportPid, PacketId, reply_error(Reason))
end,
{keep_state, State};
handle_event(info, {server_rpc, PacketId, #rpc_container{method = <<"remove">>, container_name = ContainerName}}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
%% efka_inetd收到消息后就立即返回了
case docker_manager:remove_container(ContainerName) of
ok ->
efka_transport:rpc_reply(TransportPid, PacketId, reply_success(<<"ok">>));
{error, Reason} when is_binary(Reason) ->
efka_transport:rpc_reply(TransportPid, PacketId, reply_error(Reason))
end,
{keep_state, State};
%% config.json配置信息
handle_event(info, {server_rpc, PacketId, #rpc_container{method = <<"config">>, container_name = ContainerName, params = Config}}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
case docker_manager:config_container(ContainerName, Config) of