This commit is contained in:
anlicheng 2026-04-20 18:50:46 +08:00
parent 58687e19a5
commit 43e02a5f16
2 changed files with 20 additions and 103 deletions

View File

@ -16,8 +16,7 @@
%% API
-export([start_link/0]).
-export([deploy/2, start_container/1, stop_container/1, stop_container/2, config_container/2, kill_container/1, kill_container/2, remove_container/1, remove_container/3]).
-export([get_containers/0]).
-export([deploy/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -34,47 +33,10 @@
%%% API
%%%===================================================================
-spec get_containers() -> {ok, Containers :: [map()]} | {error, Reason :: binary()}.
get_containers() ->
gen_server:call(?SERVER, get_containers).
-spec deploy(TaskId :: integer(), Params :: message_pb:'ContainerDeployParams'()) -> ok | {error, Reason :: binary()}.
deploy(TaskId, Params) when is_integer(TaskId), is_record(Params, 'ContainerDeployParams') ->
gen_server:call(?SERVER, {deploy, TaskId, Params}).
-spec config_container(ContainerName :: binary(), Config :: binary()) -> ok | {error, Reason :: binary()}.
config_container(ContainerName, Config) when is_binary(ContainerName), is_binary(Config) ->
gen_server:call(?SERVER, {config_container, ContainerName, Config}).
-spec start_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
start_container(ContainerId) when is_binary(ContainerId) ->
gen_server:call(?SERVER, {start_container, ContainerId}).
-spec stop_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
stop_container(ContainerId) when is_binary(ContainerId) ->
stop_container(ContainerId, 0).
-spec stop_container(ServiceId :: binary(), TimeoutSeconds :: non_neg_integer()) -> ok | {error, Reason :: term()}.
stop_container(ContainerId, TimeoutSeconds) when is_binary(ContainerId), is_integer(TimeoutSeconds), TimeoutSeconds >= 0 ->
gen_server:call(?SERVER, {stop_container, ContainerId, TimeoutSeconds}).
-spec kill_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
kill_container(ContainerId) when is_binary(ContainerId) ->
kill_container(ContainerId, <<>>).
-spec kill_container(ServiceId :: binary(), Signal :: binary()) -> ok | {error, Reason :: term()}.
kill_container(ContainerId, Signal) when is_binary(ContainerId), is_binary(Signal) ->
gen_server:call(?SERVER, {kill_container, ContainerId, Signal}).
-spec remove_container(ServiceId :: binary()) -> ok | {error, Reason :: term()}.
remove_container(ContainerId) when is_binary(ContainerId) ->
remove_container(ContainerId, false, false).
-spec remove_container(ServiceId :: binary(), Force :: boolean(), RemoveVolumes :: boolean()) -> ok | {error, Reason :: term()}.
remove_container(ContainerId, Force, RemoveVolumes)
when is_binary(ContainerId), is_boolean(Force), is_boolean(RemoveVolumes) ->
gen_server:call(?SERVER, {remove_container, ContainerId, Force, RemoveVolumes}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
@ -115,69 +77,6 @@ handle_call({deploy, TaskId, Params = #'ContainerDeployParams'{
logger:debug("[docker_manager] start deploy task_id: ~p, params: ~p", [TaskId, Params]),
{reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}};
%%
handle_call({config_container, ContainerName, Config}, _From, State = #state{root_dir = RootDir}) ->
case docker_helper:get_container_dir(RootDir, ContainerName) of
{ok, ContainerDir} ->
%%
ConfigFile = docker_helper:get_config_file(ContainerDir),
case file:write_file(ConfigFile, Config, [write, binary]) of
ok ->
logger:warning("[docker_manager] write config file: ~p success", [ConfigFile]),
{reply, ok, State};
{error, Reason} ->
logger:warning("[docker_manager] write config file: ~p, get error: ~p", [ConfigFile, Reason]),
{reply, {error, <<"write config failed">>}, State}
end;
error ->
{reply, {error, <<"error">>}, State}
end;
%% :
handle_call({start_container, ContainerId}, _From, State) ->
case docker_commands:start_container(ContainerId) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({stop_container, ContainerId, TimeoutSeconds}, _From, State = #state{}) ->
case docker_commands:stop_container(ContainerId, TimeoutSeconds) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({kill_container, ContainerId, Signal}, _From, State = #state{}) ->
case docker_commands:kill_container(ContainerId, Signal) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call(get_containers, _From, State = #state{}) ->
case docker_commands:get_containers() of
{ok, Containers} ->
{reply, {ok, Containers}, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({remove_container, ContainerId, Force, RemoveVolumes}, _From, State = #state{}) ->
case docker_commands:remove_container(ContainerId, Force, RemoveVolumes) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.

View File

@ -322,7 +322,7 @@ handle_container_request(#'ContainerRequest'{action = {remove, #'ContainerReques
docker_commands:remove_container(ContainerTarget, to_bool(Force), to_bool(RemoveVolumes));
handle_container_request(#'ContainerRequest'{action = {config, #'ContainerRequest.Config'{target = Target, config = Config}}}) ->
ContainerTarget = container_target(Target),
docker_manager:config_container(ContainerTarget, iolist_to_binary(Config)).
update_container_config(ContainerTarget, iolist_to_binary(Config)).
-spec send_result_reply(ssl:sslsocket(), integer(), binary()) -> ok.
send_result_reply(Socket, PacketId, Payload) when is_binary(Payload) ->
@ -364,3 +364,21 @@ to_bool(false) ->
false;
to_bool(0) ->
false.
-spec update_container_config(binary(), binary()) -> ok | {error, binary()}.
update_container_config(ContainerName, Config) when is_binary(ContainerName), is_binary(Config) ->
{ok, RootDir} = application:get_env(efka, root_dir),
case docker_helper:get_container_dir(RootDir, ContainerName) of
{ok, ContainerDir} ->
ConfigFile = docker_helper:get_config_file(ContainerDir),
case file:write_file(ConfigFile, Config, [write, binary]) of
ok ->
logger:warning("[efka_client] write config file: ~p success", [ConfigFile]),
ok;
{error, Reason} ->
logger:warning("[efka_client] write config file: ~p, get error: ~p", [ConfigFile, Reason]),
{error, <<"write config failed">>}
end;
error ->
{error, <<"error">>}
end.