diff --git a/apps/efka/src/docker/docker_deployer.erl b/apps/efka/src/docker/docker_deployer.erl index c3c1085..e764bdf 100644 --- a/apps/efka/src/docker/docker_deployer.erl +++ b/apps/efka/src/docker/docker_deployer.erl @@ -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>>); diff --git a/apps/efka/src/docker/docker_manager.erl b/apps/efka/src/docker/docker_manager.erl index ee662d6..8c68c49 100644 --- a/apps/efka/src/docker/docker_manager.erl +++ b/apps/efka/src/docker/docker_manager.erl @@ -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}. diff --git a/apps/efka/src/efka_remote_agent.erl b/apps/efka/src/efka_remote_agent.erl index 583806f..350f5e9 100644 --- a/apps/efka/src/efka_remote_agent.erl +++ b/apps/efka/src/efka_remote_agent.erl @@ -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