diff --git a/src/docker/docker_manager.erl b/src/docker/docker_manager.erl index 3461a43..0f0f357 100644 --- a/src/docker/docker_manager.erl +++ b/src/docker/docker_manager.erl @@ -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}. diff --git a/src/efka_client.erl b/src/efka_client.erl index 78944b5..adc1958 100644 --- a/src/efka_client.erl +++ b/src/efka_client.erl @@ -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.